mal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * drivers/net/ethernet/ibm/emac/mal.c
  3. *
  4. * Memory Access Layer (MAL) support
  5. *
  6. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  7. * <benh@kernel.crashing.org>
  8. *
  9. * Based on the arch/ppc version of the driver:
  10. *
  11. * Copyright (c) 2004, 2005 Zultys Technologies.
  12. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  13. *
  14. * Based on original work by
  15. * Benjamin Herrenschmidt <benh@kernel.crashing.org>,
  16. * David Gibson <hermes@gibson.dropbear.id.au>,
  17. *
  18. * Armin Kuster <akuster@mvista.com>
  19. * Copyright 2002 MontaVista Softare Inc.
  20. *
  21. * This program is free software; you can redistribute it and/or modify it
  22. * under the terms of the GNU General Public License as published by the
  23. * Free Software Foundation; either version 2 of the License, or (at your
  24. * option) any later version.
  25. *
  26. */
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/of_irq.h>
  30. #include "core.h"
  31. #include <asm/dcr-regs.h>
  32. static int mal_count;
  33. int mal_register_commac(struct mal_instance *mal, struct mal_commac *commac)
  34. {
  35. unsigned long flags;
  36. spin_lock_irqsave(&mal->lock, flags);
  37. MAL_DBG(mal, "reg(%08x, %08x)" NL,
  38. commac->tx_chan_mask, commac->rx_chan_mask);
  39. /* Don't let multiple commacs claim the same channel(s) */
  40. if ((mal->tx_chan_mask & commac->tx_chan_mask) ||
  41. (mal->rx_chan_mask & commac->rx_chan_mask)) {
  42. spin_unlock_irqrestore(&mal->lock, flags);
  43. printk(KERN_WARNING "mal%d: COMMAC channels conflict!\n",
  44. mal->index);
  45. return -EBUSY;
  46. }
  47. if (list_empty(&mal->list))
  48. napi_enable(&mal->napi);
  49. mal->tx_chan_mask |= commac->tx_chan_mask;
  50. mal->rx_chan_mask |= commac->rx_chan_mask;
  51. list_add(&commac->list, &mal->list);
  52. spin_unlock_irqrestore(&mal->lock, flags);
  53. return 0;
  54. }
  55. void mal_unregister_commac(struct mal_instance *mal,
  56. struct mal_commac *commac)
  57. {
  58. unsigned long flags;
  59. spin_lock_irqsave(&mal->lock, flags);
  60. MAL_DBG(mal, "unreg(%08x, %08x)" NL,
  61. commac->tx_chan_mask, commac->rx_chan_mask);
  62. mal->tx_chan_mask &= ~commac->tx_chan_mask;
  63. mal->rx_chan_mask &= ~commac->rx_chan_mask;
  64. list_del_init(&commac->list);
  65. if (list_empty(&mal->list))
  66. napi_disable(&mal->napi);
  67. spin_unlock_irqrestore(&mal->lock, flags);
  68. }
  69. int mal_set_rcbs(struct mal_instance *mal, int channel, unsigned long size)
  70. {
  71. BUG_ON(channel < 0 || channel >= mal->num_rx_chans ||
  72. size > MAL_MAX_RX_SIZE);
  73. MAL_DBG(mal, "set_rbcs(%d, %lu)" NL, channel, size);
  74. if (size & 0xf) {
  75. printk(KERN_WARNING
  76. "mal%d: incorrect RX size %lu for the channel %d\n",
  77. mal->index, size, channel);
  78. return -EINVAL;
  79. }
  80. set_mal_dcrn(mal, MAL_RCBS(channel), size >> 4);
  81. return 0;
  82. }
  83. int mal_tx_bd_offset(struct mal_instance *mal, int channel)
  84. {
  85. BUG_ON(channel < 0 || channel >= mal->num_tx_chans);
  86. return channel * NUM_TX_BUFF;
  87. }
  88. int mal_rx_bd_offset(struct mal_instance *mal, int channel)
  89. {
  90. BUG_ON(channel < 0 || channel >= mal->num_rx_chans);
  91. return mal->num_tx_chans * NUM_TX_BUFF + channel * NUM_RX_BUFF;
  92. }
  93. void mal_enable_tx_channel(struct mal_instance *mal, int channel)
  94. {
  95. unsigned long flags;
  96. spin_lock_irqsave(&mal->lock, flags);
  97. MAL_DBG(mal, "enable_tx(%d)" NL, channel);
  98. set_mal_dcrn(mal, MAL_TXCASR,
  99. get_mal_dcrn(mal, MAL_TXCASR) | MAL_CHAN_MASK(channel));
  100. spin_unlock_irqrestore(&mal->lock, flags);
  101. }
  102. void mal_disable_tx_channel(struct mal_instance *mal, int channel)
  103. {
  104. set_mal_dcrn(mal, MAL_TXCARR, MAL_CHAN_MASK(channel));
  105. MAL_DBG(mal, "disable_tx(%d)" NL, channel);
  106. }
  107. void mal_enable_rx_channel(struct mal_instance *mal, int channel)
  108. {
  109. unsigned long flags;
  110. /*
  111. * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
  112. * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
  113. * for the bitmask
  114. */
  115. if (!(channel % 8))
  116. channel >>= 3;
  117. spin_lock_irqsave(&mal->lock, flags);
  118. MAL_DBG(mal, "enable_rx(%d)" NL, channel);
  119. set_mal_dcrn(mal, MAL_RXCASR,
  120. get_mal_dcrn(mal, MAL_RXCASR) | MAL_CHAN_MASK(channel));
  121. spin_unlock_irqrestore(&mal->lock, flags);
  122. }
  123. void mal_disable_rx_channel(struct mal_instance *mal, int channel)
  124. {
  125. /*
  126. * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
  127. * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
  128. * for the bitmask
  129. */
  130. if (!(channel % 8))
  131. channel >>= 3;
  132. set_mal_dcrn(mal, MAL_RXCARR, MAL_CHAN_MASK(channel));
  133. MAL_DBG(mal, "disable_rx(%d)" NL, channel);
  134. }
  135. void mal_poll_add(struct mal_instance *mal, struct mal_commac *commac)
  136. {
  137. unsigned long flags;
  138. spin_lock_irqsave(&mal->lock, flags);
  139. MAL_DBG(mal, "poll_add(%p)" NL, commac);
  140. /* starts disabled */
  141. set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
  142. list_add_tail(&commac->poll_list, &mal->poll_list);
  143. spin_unlock_irqrestore(&mal->lock, flags);
  144. }
  145. void mal_poll_del(struct mal_instance *mal, struct mal_commac *commac)
  146. {
  147. unsigned long flags;
  148. spin_lock_irqsave(&mal->lock, flags);
  149. MAL_DBG(mal, "poll_del(%p)" NL, commac);
  150. list_del(&commac->poll_list);
  151. spin_unlock_irqrestore(&mal->lock, flags);
  152. }
  153. /* synchronized by mal_poll() */
  154. static inline void mal_enable_eob_irq(struct mal_instance *mal)
  155. {
  156. MAL_DBG2(mal, "enable_irq" NL);
  157. // XXX might want to cache MAL_CFG as the DCR read can be slooooow
  158. set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) | MAL_CFG_EOPIE);
  159. }
  160. /* synchronized by NAPI state */
  161. static inline void mal_disable_eob_irq(struct mal_instance *mal)
  162. {
  163. // XXX might want to cache MAL_CFG as the DCR read can be slooooow
  164. set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) & ~MAL_CFG_EOPIE);
  165. MAL_DBG2(mal, "disable_irq" NL);
  166. }
  167. static irqreturn_t mal_serr(int irq, void *dev_instance)
  168. {
  169. struct mal_instance *mal = dev_instance;
  170. u32 esr = get_mal_dcrn(mal, MAL_ESR);
  171. /* Clear the error status register */
  172. set_mal_dcrn(mal, MAL_ESR, esr);
  173. MAL_DBG(mal, "SERR %08x" NL, esr);
  174. if (esr & MAL_ESR_EVB) {
  175. if (esr & MAL_ESR_DE) {
  176. /* We ignore Descriptor error,
  177. * TXDE or RXDE interrupt will be generated anyway.
  178. */
  179. return IRQ_HANDLED;
  180. }
  181. if (esr & MAL_ESR_PEIN) {
  182. /* PLB error, it's probably buggy hardware or
  183. * incorrect physical address in BD (i.e. bug)
  184. */
  185. if (net_ratelimit())
  186. printk(KERN_ERR
  187. "mal%d: system error, "
  188. "PLB (ESR = 0x%08x)\n",
  189. mal->index, esr);
  190. return IRQ_HANDLED;
  191. }
  192. /* OPB error, it's probably buggy hardware or incorrect
  193. * EBC setup
  194. */
  195. if (net_ratelimit())
  196. printk(KERN_ERR
  197. "mal%d: system error, OPB (ESR = 0x%08x)\n",
  198. mal->index, esr);
  199. }
  200. return IRQ_HANDLED;
  201. }
  202. static inline void mal_schedule_poll(struct mal_instance *mal)
  203. {
  204. if (likely(napi_schedule_prep(&mal->napi))) {
  205. MAL_DBG2(mal, "schedule_poll" NL);
  206. spin_lock(&mal->lock);
  207. mal_disable_eob_irq(mal);
  208. spin_unlock(&mal->lock);
  209. __napi_schedule(&mal->napi);
  210. } else
  211. MAL_DBG2(mal, "already in poll" NL);
  212. }
  213. static irqreturn_t mal_txeob(int irq, void *dev_instance)
  214. {
  215. struct mal_instance *mal = dev_instance;
  216. u32 r = get_mal_dcrn(mal, MAL_TXEOBISR);
  217. MAL_DBG2(mal, "txeob %08x" NL, r);
  218. mal_schedule_poll(mal);
  219. set_mal_dcrn(mal, MAL_TXEOBISR, r);
  220. #ifdef CONFIG_PPC_DCR_NATIVE
  221. if (mal_has_feature(mal, MAL_FTR_CLEAR_ICINTSTAT))
  222. mtdcri(SDR0, DCRN_SDR_ICINTSTAT,
  223. (mfdcri(SDR0, DCRN_SDR_ICINTSTAT) | ICINTSTAT_ICTX));
  224. #endif
  225. return IRQ_HANDLED;
  226. }
  227. static irqreturn_t mal_rxeob(int irq, void *dev_instance)
  228. {
  229. struct mal_instance *mal = dev_instance;
  230. u32 r = get_mal_dcrn(mal, MAL_RXEOBISR);
  231. MAL_DBG2(mal, "rxeob %08x" NL, r);
  232. mal_schedule_poll(mal);
  233. set_mal_dcrn(mal, MAL_RXEOBISR, r);
  234. #ifdef CONFIG_PPC_DCR_NATIVE
  235. if (mal_has_feature(mal, MAL_FTR_CLEAR_ICINTSTAT))
  236. mtdcri(SDR0, DCRN_SDR_ICINTSTAT,
  237. (mfdcri(SDR0, DCRN_SDR_ICINTSTAT) | ICINTSTAT_ICRX));
  238. #endif
  239. return IRQ_HANDLED;
  240. }
  241. static irqreturn_t mal_txde(int irq, void *dev_instance)
  242. {
  243. struct mal_instance *mal = dev_instance;
  244. u32 deir = get_mal_dcrn(mal, MAL_TXDEIR);
  245. set_mal_dcrn(mal, MAL_TXDEIR, deir);
  246. MAL_DBG(mal, "txde %08x" NL, deir);
  247. if (net_ratelimit())
  248. printk(KERN_ERR
  249. "mal%d: TX descriptor error (TXDEIR = 0x%08x)\n",
  250. mal->index, deir);
  251. return IRQ_HANDLED;
  252. }
  253. static irqreturn_t mal_rxde(int irq, void *dev_instance)
  254. {
  255. struct mal_instance *mal = dev_instance;
  256. struct list_head *l;
  257. u32 deir = get_mal_dcrn(mal, MAL_RXDEIR);
  258. MAL_DBG(mal, "rxde %08x" NL, deir);
  259. list_for_each(l, &mal->list) {
  260. struct mal_commac *mc = list_entry(l, struct mal_commac, list);
  261. if (deir & mc->rx_chan_mask) {
  262. set_bit(MAL_COMMAC_RX_STOPPED, &mc->flags);
  263. mc->ops->rxde(mc->dev);
  264. }
  265. }
  266. mal_schedule_poll(mal);
  267. set_mal_dcrn(mal, MAL_RXDEIR, deir);
  268. return IRQ_HANDLED;
  269. }
  270. static irqreturn_t mal_int(int irq, void *dev_instance)
  271. {
  272. struct mal_instance *mal = dev_instance;
  273. u32 esr = get_mal_dcrn(mal, MAL_ESR);
  274. if (esr & MAL_ESR_EVB) {
  275. /* descriptor error */
  276. if (esr & MAL_ESR_DE) {
  277. if (esr & MAL_ESR_CIDT)
  278. return mal_rxde(irq, dev_instance);
  279. else
  280. return mal_txde(irq, dev_instance);
  281. } else { /* SERR */
  282. return mal_serr(irq, dev_instance);
  283. }
  284. }
  285. return IRQ_HANDLED;
  286. }
  287. void mal_poll_disable(struct mal_instance *mal, struct mal_commac *commac)
  288. {
  289. /* Spinlock-type semantics: only one caller disable poll at a time */
  290. while (test_and_set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags))
  291. msleep(1);
  292. /* Synchronize with the MAL NAPI poller */
  293. napi_synchronize(&mal->napi);
  294. }
  295. void mal_poll_enable(struct mal_instance *mal, struct mal_commac *commac)
  296. {
  297. smp_wmb();
  298. clear_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
  299. /* Feels better to trigger a poll here to catch up with events that
  300. * may have happened on this channel while disabled. It will most
  301. * probably be delayed until the next interrupt but that's mostly a
  302. * non-issue in the context where this is called.
  303. */
  304. napi_schedule(&mal->napi);
  305. }
  306. static int mal_poll(struct napi_struct *napi, int budget)
  307. {
  308. struct mal_instance *mal = container_of(napi, struct mal_instance, napi);
  309. struct list_head *l;
  310. int received = 0;
  311. unsigned long flags;
  312. MAL_DBG2(mal, "poll(%d)" NL, budget);
  313. /* Process TX skbs */
  314. list_for_each(l, &mal->poll_list) {
  315. struct mal_commac *mc =
  316. list_entry(l, struct mal_commac, poll_list);
  317. mc->ops->poll_tx(mc->dev);
  318. }
  319. /* Process RX skbs.
  320. *
  321. * We _might_ need something more smart here to enforce polling
  322. * fairness.
  323. */
  324. list_for_each(l, &mal->poll_list) {
  325. struct mal_commac *mc =
  326. list_entry(l, struct mal_commac, poll_list);
  327. int n;
  328. if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
  329. continue;
  330. n = mc->ops->poll_rx(mc->dev, budget);
  331. if (n) {
  332. received += n;
  333. budget -= n;
  334. if (budget <= 0)
  335. goto more_work; // XXX What if this is the last one ?
  336. }
  337. }
  338. /* We need to disable IRQs to protect from RXDE IRQ here */
  339. spin_lock_irqsave(&mal->lock, flags);
  340. __napi_complete(napi);
  341. mal_enable_eob_irq(mal);
  342. spin_unlock_irqrestore(&mal->lock, flags);
  343. /* Check for "rotting" packet(s) */
  344. list_for_each(l, &mal->poll_list) {
  345. struct mal_commac *mc =
  346. list_entry(l, struct mal_commac, poll_list);
  347. if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
  348. continue;
  349. if (unlikely(mc->ops->peek_rx(mc->dev) ||
  350. test_bit(MAL_COMMAC_RX_STOPPED, &mc->flags))) {
  351. MAL_DBG2(mal, "rotting packet" NL);
  352. if (!napi_reschedule(napi))
  353. goto more_work;
  354. spin_lock_irqsave(&mal->lock, flags);
  355. mal_disable_eob_irq(mal);
  356. spin_unlock_irqrestore(&mal->lock, flags);
  357. }
  358. mc->ops->poll_tx(mc->dev);
  359. }
  360. more_work:
  361. MAL_DBG2(mal, "poll() %d <- %d" NL, budget, received);
  362. return received;
  363. }
  364. static void mal_reset(struct mal_instance *mal)
  365. {
  366. int n = 10;
  367. MAL_DBG(mal, "reset" NL);
  368. set_mal_dcrn(mal, MAL_CFG, MAL_CFG_SR);
  369. /* Wait for reset to complete (1 system clock) */
  370. while ((get_mal_dcrn(mal, MAL_CFG) & MAL_CFG_SR) && n)
  371. --n;
  372. if (unlikely(!n))
  373. printk(KERN_ERR "mal%d: reset timeout\n", mal->index);
  374. }
  375. int mal_get_regs_len(struct mal_instance *mal)
  376. {
  377. return sizeof(struct emac_ethtool_regs_subhdr) +
  378. sizeof(struct mal_regs);
  379. }
  380. void *mal_dump_regs(struct mal_instance *mal, void *buf)
  381. {
  382. struct emac_ethtool_regs_subhdr *hdr = buf;
  383. struct mal_regs *regs = (struct mal_regs *)(hdr + 1);
  384. int i;
  385. hdr->version = mal->version;
  386. hdr->index = mal->index;
  387. regs->tx_count = mal->num_tx_chans;
  388. regs->rx_count = mal->num_rx_chans;
  389. regs->cfg = get_mal_dcrn(mal, MAL_CFG);
  390. regs->esr = get_mal_dcrn(mal, MAL_ESR);
  391. regs->ier = get_mal_dcrn(mal, MAL_IER);
  392. regs->tx_casr = get_mal_dcrn(mal, MAL_TXCASR);
  393. regs->tx_carr = get_mal_dcrn(mal, MAL_TXCARR);
  394. regs->tx_eobisr = get_mal_dcrn(mal, MAL_TXEOBISR);
  395. regs->tx_deir = get_mal_dcrn(mal, MAL_TXDEIR);
  396. regs->rx_casr = get_mal_dcrn(mal, MAL_RXCASR);
  397. regs->rx_carr = get_mal_dcrn(mal, MAL_RXCARR);
  398. regs->rx_eobisr = get_mal_dcrn(mal, MAL_RXEOBISR);
  399. regs->rx_deir = get_mal_dcrn(mal, MAL_RXDEIR);
  400. for (i = 0; i < regs->tx_count; ++i)
  401. regs->tx_ctpr[i] = get_mal_dcrn(mal, MAL_TXCTPR(i));
  402. for (i = 0; i < regs->rx_count; ++i) {
  403. regs->rx_ctpr[i] = get_mal_dcrn(mal, MAL_RXCTPR(i));
  404. regs->rcbs[i] = get_mal_dcrn(mal, MAL_RCBS(i));
  405. }
  406. return regs + 1;
  407. }
  408. static int mal_probe(struct platform_device *ofdev)
  409. {
  410. struct mal_instance *mal;
  411. int err = 0, i, bd_size;
  412. int index = mal_count++;
  413. unsigned int dcr_base;
  414. const u32 *prop;
  415. u32 cfg;
  416. unsigned long irqflags;
  417. irq_handler_t hdlr_serr, hdlr_txde, hdlr_rxde;
  418. mal = kzalloc(sizeof(struct mal_instance), GFP_KERNEL);
  419. if (!mal)
  420. return -ENOMEM;
  421. mal->index = index;
  422. mal->ofdev = ofdev;
  423. mal->version = of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal2") ? 2 : 1;
  424. MAL_DBG(mal, "probe" NL);
  425. prop = of_get_property(ofdev->dev.of_node, "num-tx-chans", NULL);
  426. if (prop == NULL) {
  427. printk(KERN_ERR
  428. "mal%d: can't find MAL num-tx-chans property!\n",
  429. index);
  430. err = -ENODEV;
  431. goto fail;
  432. }
  433. mal->num_tx_chans = prop[0];
  434. prop = of_get_property(ofdev->dev.of_node, "num-rx-chans", NULL);
  435. if (prop == NULL) {
  436. printk(KERN_ERR
  437. "mal%d: can't find MAL num-rx-chans property!\n",
  438. index);
  439. err = -ENODEV;
  440. goto fail;
  441. }
  442. mal->num_rx_chans = prop[0];
  443. dcr_base = dcr_resource_start(ofdev->dev.of_node, 0);
  444. if (dcr_base == 0) {
  445. printk(KERN_ERR
  446. "mal%d: can't find DCR resource!\n", index);
  447. err = -ENODEV;
  448. goto fail;
  449. }
  450. mal->dcr_host = dcr_map(ofdev->dev.of_node, dcr_base, 0x100);
  451. if (!DCR_MAP_OK(mal->dcr_host)) {
  452. printk(KERN_ERR
  453. "mal%d: failed to map DCRs !\n", index);
  454. err = -ENODEV;
  455. goto fail;
  456. }
  457. if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-405ez")) {
  458. #if defined(CONFIG_IBM_EMAC_MAL_CLR_ICINTSTAT) && \
  459. defined(CONFIG_IBM_EMAC_MAL_COMMON_ERR)
  460. mal->features |= (MAL_FTR_CLEAR_ICINTSTAT |
  461. MAL_FTR_COMMON_ERR_INT);
  462. #else
  463. printk(KERN_ERR "%s: Support for 405EZ not enabled!\n",
  464. ofdev->dev.of_node->full_name);
  465. err = -ENODEV;
  466. goto fail;
  467. #endif
  468. }
  469. mal->txeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  470. mal->rxeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 1);
  471. mal->serr_irq = irq_of_parse_and_map(ofdev->dev.of_node, 2);
  472. if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
  473. mal->txde_irq = mal->rxde_irq = mal->serr_irq;
  474. } else {
  475. mal->txde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 3);
  476. mal->rxde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 4);
  477. }
  478. if (mal->txeob_irq == NO_IRQ || mal->rxeob_irq == NO_IRQ ||
  479. mal->serr_irq == NO_IRQ || mal->txde_irq == NO_IRQ ||
  480. mal->rxde_irq == NO_IRQ) {
  481. printk(KERN_ERR
  482. "mal%d: failed to map interrupts !\n", index);
  483. err = -ENODEV;
  484. goto fail_unmap;
  485. }
  486. INIT_LIST_HEAD(&mal->poll_list);
  487. INIT_LIST_HEAD(&mal->list);
  488. spin_lock_init(&mal->lock);
  489. init_dummy_netdev(&mal->dummy_dev);
  490. netif_napi_add(&mal->dummy_dev, &mal->napi, mal_poll,
  491. CONFIG_IBM_EMAC_POLL_WEIGHT);
  492. /* Load power-on reset defaults */
  493. mal_reset(mal);
  494. /* Set the MAL configuration register */
  495. cfg = (mal->version == 2) ? MAL2_CFG_DEFAULT : MAL1_CFG_DEFAULT;
  496. cfg |= MAL_CFG_PLBB | MAL_CFG_OPBBL | MAL_CFG_LEA;
  497. /* Current Axon is not happy with priority being non-0, it can
  498. * deadlock, fix it up here
  499. */
  500. if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-axon"))
  501. cfg &= ~(MAL2_CFG_RPP_10 | MAL2_CFG_WPP_10);
  502. /* Apply configuration */
  503. set_mal_dcrn(mal, MAL_CFG, cfg);
  504. /* Allocate space for BD rings */
  505. BUG_ON(mal->num_tx_chans <= 0 || mal->num_tx_chans > 32);
  506. BUG_ON(mal->num_rx_chans <= 0 || mal->num_rx_chans > 32);
  507. bd_size = sizeof(struct mal_descriptor) *
  508. (NUM_TX_BUFF * mal->num_tx_chans +
  509. NUM_RX_BUFF * mal->num_rx_chans);
  510. mal->bd_virt = dma_zalloc_coherent(&ofdev->dev, bd_size, &mal->bd_dma,
  511. GFP_KERNEL);
  512. if (mal->bd_virt == NULL) {
  513. err = -ENOMEM;
  514. goto fail_unmap;
  515. }
  516. for (i = 0; i < mal->num_tx_chans; ++i)
  517. set_mal_dcrn(mal, MAL_TXCTPR(i), mal->bd_dma +
  518. sizeof(struct mal_descriptor) *
  519. mal_tx_bd_offset(mal, i));
  520. for (i = 0; i < mal->num_rx_chans; ++i)
  521. set_mal_dcrn(mal, MAL_RXCTPR(i), mal->bd_dma +
  522. sizeof(struct mal_descriptor) *
  523. mal_rx_bd_offset(mal, i));
  524. if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
  525. irqflags = IRQF_SHARED;
  526. hdlr_serr = hdlr_txde = hdlr_rxde = mal_int;
  527. } else {
  528. irqflags = 0;
  529. hdlr_serr = mal_serr;
  530. hdlr_txde = mal_txde;
  531. hdlr_rxde = mal_rxde;
  532. }
  533. err = request_irq(mal->serr_irq, hdlr_serr, irqflags, "MAL SERR", mal);
  534. if (err)
  535. goto fail2;
  536. err = request_irq(mal->txde_irq, hdlr_txde, irqflags, "MAL TX DE", mal);
  537. if (err)
  538. goto fail3;
  539. err = request_irq(mal->txeob_irq, mal_txeob, 0, "MAL TX EOB", mal);
  540. if (err)
  541. goto fail4;
  542. err = request_irq(mal->rxde_irq, hdlr_rxde, irqflags, "MAL RX DE", mal);
  543. if (err)
  544. goto fail5;
  545. err = request_irq(mal->rxeob_irq, mal_rxeob, 0, "MAL RX EOB", mal);
  546. if (err)
  547. goto fail6;
  548. /* Enable all MAL SERR interrupt sources */
  549. set_mal_dcrn(mal, MAL_IER, MAL_IER_EVENTS);
  550. /* Enable EOB interrupt */
  551. mal_enable_eob_irq(mal);
  552. printk(KERN_INFO
  553. "MAL v%d %s, %d TX channels, %d RX channels\n",
  554. mal->version, ofdev->dev.of_node->full_name,
  555. mal->num_tx_chans, mal->num_rx_chans);
  556. /* Advertise this instance to the rest of the world */
  557. wmb();
  558. platform_set_drvdata(ofdev, mal);
  559. mal_dbg_register(mal);
  560. return 0;
  561. fail6:
  562. free_irq(mal->rxde_irq, mal);
  563. fail5:
  564. free_irq(mal->txeob_irq, mal);
  565. fail4:
  566. free_irq(mal->txde_irq, mal);
  567. fail3:
  568. free_irq(mal->serr_irq, mal);
  569. fail2:
  570. dma_free_coherent(&ofdev->dev, bd_size, mal->bd_virt, mal->bd_dma);
  571. fail_unmap:
  572. dcr_unmap(mal->dcr_host, 0x100);
  573. fail:
  574. kfree(mal);
  575. return err;
  576. }
  577. static int mal_remove(struct platform_device *ofdev)
  578. {
  579. struct mal_instance *mal = platform_get_drvdata(ofdev);
  580. MAL_DBG(mal, "remove" NL);
  581. /* Synchronize with scheduled polling */
  582. napi_disable(&mal->napi);
  583. if (!list_empty(&mal->list))
  584. /* This is *very* bad */
  585. WARN(1, KERN_EMERG
  586. "mal%d: commac list is not empty on remove!\n",
  587. mal->index);
  588. free_irq(mal->serr_irq, mal);
  589. free_irq(mal->txde_irq, mal);
  590. free_irq(mal->txeob_irq, mal);
  591. free_irq(mal->rxde_irq, mal);
  592. free_irq(mal->rxeob_irq, mal);
  593. mal_reset(mal);
  594. mal_dbg_unregister(mal);
  595. dma_free_coherent(&ofdev->dev,
  596. sizeof(struct mal_descriptor) *
  597. (NUM_TX_BUFF * mal->num_tx_chans +
  598. NUM_RX_BUFF * mal->num_rx_chans), mal->bd_virt,
  599. mal->bd_dma);
  600. kfree(mal);
  601. return 0;
  602. }
  603. static const struct of_device_id mal_platform_match[] =
  604. {
  605. {
  606. .compatible = "ibm,mcmal",
  607. },
  608. {
  609. .compatible = "ibm,mcmal2",
  610. },
  611. /* Backward compat */
  612. {
  613. .type = "mcmal-dma",
  614. .compatible = "ibm,mcmal",
  615. },
  616. {
  617. .type = "mcmal-dma",
  618. .compatible = "ibm,mcmal2",
  619. },
  620. {},
  621. };
  622. static struct platform_driver mal_of_driver = {
  623. .driver = {
  624. .name = "mcmal",
  625. .of_match_table = mal_platform_match,
  626. },
  627. .probe = mal_probe,
  628. .remove = mal_remove,
  629. };
  630. int __init mal_init(void)
  631. {
  632. return platform_driver_register(&mal_of_driver);
  633. }
  634. void mal_exit(void)
  635. {
  636. platform_driver_unregister(&mal_of_driver);
  637. }