mal.c 19 KB

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