mal.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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 "core.h"
  30. #include <asm/dcr-regs.h>
  31. static int mal_count;
  32. int mal_register_commac(struct mal_instance *mal, struct mal_commac *commac)
  33. {
  34. unsigned long flags;
  35. spin_lock_irqsave(&mal->lock, flags);
  36. MAL_DBG(mal, "reg(%08x, %08x)" NL,
  37. commac->tx_chan_mask, commac->rx_chan_mask);
  38. /* Don't let multiple commacs claim the same channel(s) */
  39. if ((mal->tx_chan_mask & commac->tx_chan_mask) ||
  40. (mal->rx_chan_mask & commac->rx_chan_mask)) {
  41. spin_unlock_irqrestore(&mal->lock, flags);
  42. printk(KERN_WARNING "mal%d: COMMAC channels conflict!\n",
  43. mal->index);
  44. return -EBUSY;
  45. }
  46. if (list_empty(&mal->list))
  47. napi_enable(&mal->napi);
  48. mal->tx_chan_mask |= commac->tx_chan_mask;
  49. mal->rx_chan_mask |= commac->rx_chan_mask;
  50. list_add(&commac->list, &mal->list);
  51. spin_unlock_irqrestore(&mal->lock, flags);
  52. return 0;
  53. }
  54. void mal_unregister_commac(struct mal_instance *mal,
  55. struct mal_commac *commac)
  56. {
  57. unsigned long flags;
  58. spin_lock_irqsave(&mal->lock, flags);
  59. MAL_DBG(mal, "unreg(%08x, %08x)" NL,
  60. commac->tx_chan_mask, commac->rx_chan_mask);
  61. mal->tx_chan_mask &= ~commac->tx_chan_mask;
  62. mal->rx_chan_mask &= ~commac->rx_chan_mask;
  63. list_del_init(&commac->list);
  64. if (list_empty(&mal->list))
  65. napi_disable(&mal->napi);
  66. spin_unlock_irqrestore(&mal->lock, flags);
  67. }
  68. int mal_set_rcbs(struct mal_instance *mal, int channel, unsigned long size)
  69. {
  70. BUG_ON(channel < 0 || channel >= mal->num_rx_chans ||
  71. size > MAL_MAX_RX_SIZE);
  72. MAL_DBG(mal, "set_rbcs(%d, %lu)" NL, channel, size);
  73. if (size & 0xf) {
  74. printk(KERN_WARNING
  75. "mal%d: incorrect RX size %lu for the channel %d\n",
  76. mal->index, size, channel);
  77. return -EINVAL;
  78. }
  79. set_mal_dcrn(mal, MAL_RCBS(channel), size >> 4);
  80. return 0;
  81. }
  82. int mal_tx_bd_offset(struct mal_instance *mal, int channel)
  83. {
  84. BUG_ON(channel < 0 || channel >= mal->num_tx_chans);
  85. return channel * NUM_TX_BUFF;
  86. }
  87. int mal_rx_bd_offset(struct mal_instance *mal, int channel)
  88. {
  89. BUG_ON(channel < 0 || channel >= mal->num_rx_chans);
  90. return mal->num_tx_chans * NUM_TX_BUFF + channel * NUM_RX_BUFF;
  91. }
  92. void mal_enable_tx_channel(struct mal_instance *mal, int channel)
  93. {
  94. unsigned long flags;
  95. spin_lock_irqsave(&mal->lock, flags);
  96. MAL_DBG(mal, "enable_tx(%d)" NL, channel);
  97. set_mal_dcrn(mal, MAL_TXCASR,
  98. get_mal_dcrn(mal, MAL_TXCASR) | MAL_CHAN_MASK(channel));
  99. spin_unlock_irqrestore(&mal->lock, flags);
  100. }
  101. void mal_disable_tx_channel(struct mal_instance *mal, int channel)
  102. {
  103. set_mal_dcrn(mal, MAL_TXCARR, MAL_CHAN_MASK(channel));
  104. MAL_DBG(mal, "disable_tx(%d)" NL, channel);
  105. }
  106. void mal_enable_rx_channel(struct mal_instance *mal, int channel)
  107. {
  108. unsigned long flags;
  109. /*
  110. * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
  111. * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
  112. * for the bitmask
  113. */
  114. if (!(channel % 8))
  115. channel >>= 3;
  116. spin_lock_irqsave(&mal->lock, flags);
  117. MAL_DBG(mal, "enable_rx(%d)" NL, channel);
  118. set_mal_dcrn(mal, MAL_RXCASR,
  119. get_mal_dcrn(mal, MAL_RXCASR) | MAL_CHAN_MASK(channel));
  120. spin_unlock_irqrestore(&mal->lock, flags);
  121. }
  122. void mal_disable_rx_channel(struct mal_instance *mal, int channel)
  123. {
  124. /*
  125. * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
  126. * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
  127. * for the bitmask
  128. */
  129. if (!(channel % 8))
  130. channel >>= 3;
  131. set_mal_dcrn(mal, MAL_RXCARR, MAL_CHAN_MASK(channel));
  132. MAL_DBG(mal, "disable_rx(%d)" NL, channel);
  133. }
  134. void mal_poll_add(struct mal_instance *mal, struct mal_commac *commac)
  135. {
  136. unsigned long flags;
  137. spin_lock_irqsave(&mal->lock, flags);
  138. MAL_DBG(mal, "poll_add(%p)" NL, commac);
  139. /* starts disabled */
  140. set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
  141. list_add_tail(&commac->poll_list, &mal->poll_list);
  142. spin_unlock_irqrestore(&mal->lock, flags);
  143. }
  144. void mal_poll_del(struct mal_instance *mal, struct mal_commac *commac)
  145. {
  146. unsigned long flags;
  147. spin_lock_irqsave(&mal->lock, flags);
  148. MAL_DBG(mal, "poll_del(%p)" NL, commac);
  149. list_del(&commac->poll_list);
  150. spin_unlock_irqrestore(&mal->lock, flags);
  151. }
  152. /* synchronized by mal_poll() */
  153. static inline void mal_enable_eob_irq(struct mal_instance *mal)
  154. {
  155. MAL_DBG2(mal, "enable_irq" NL);
  156. // XXX might want to cache MAL_CFG as the DCR read can be slooooow
  157. set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) | MAL_CFG_EOPIE);
  158. }
  159. /* synchronized by NAPI state */
  160. static inline void mal_disable_eob_irq(struct mal_instance *mal)
  161. {
  162. // XXX might want to cache MAL_CFG as the DCR read can be slooooow
  163. set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) & ~MAL_CFG_EOPIE);
  164. MAL_DBG2(mal, "disable_irq" NL);
  165. }
  166. static irqreturn_t mal_serr(int irq, void *dev_instance)
  167. {
  168. struct mal_instance *mal = dev_instance;
  169. u32 esr = get_mal_dcrn(mal, MAL_ESR);
  170. /* Clear the error status register */
  171. set_mal_dcrn(mal, MAL_ESR, esr);
  172. MAL_DBG(mal, "SERR %08x" NL, esr);
  173. if (esr & MAL_ESR_EVB) {
  174. if (esr & MAL_ESR_DE) {
  175. /* We ignore Descriptor error,
  176. * TXDE or RXDE interrupt will be generated anyway.
  177. */
  178. return IRQ_HANDLED;
  179. }
  180. if (esr & MAL_ESR_PEIN) {
  181. /* PLB error, it's probably buggy hardware or
  182. * incorrect physical address in BD (i.e. bug)
  183. */
  184. if (net_ratelimit())
  185. printk(KERN_ERR
  186. "mal%d: system error, "
  187. "PLB (ESR = 0x%08x)\n",
  188. mal->index, esr);
  189. return IRQ_HANDLED;
  190. }
  191. /* OPB error, it's probably buggy hardware or incorrect
  192. * EBC setup
  193. */
  194. if (net_ratelimit())
  195. printk(KERN_ERR
  196. "mal%d: system error, OPB (ESR = 0x%08x)\n",
  197. mal->index, esr);
  198. }
  199. return IRQ_HANDLED;
  200. }
  201. static inline void mal_schedule_poll(struct mal_instance *mal)
  202. {
  203. if (likely(napi_schedule_prep(&mal->napi))) {
  204. MAL_DBG2(mal, "schedule_poll" NL);
  205. mal_disable_eob_irq(mal);
  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. again:
  311. /* Process TX skbs */
  312. list_for_each(l, &mal->poll_list) {
  313. struct mal_commac *mc =
  314. list_entry(l, struct mal_commac, poll_list);
  315. mc->ops->poll_tx(mc->dev);
  316. }
  317. /* Process RX skbs.
  318. *
  319. * We _might_ need something more smart here to enforce polling
  320. * fairness.
  321. */
  322. list_for_each(l, &mal->poll_list) {
  323. struct mal_commac *mc =
  324. list_entry(l, struct mal_commac, poll_list);
  325. int n;
  326. if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
  327. continue;
  328. n = mc->ops->poll_rx(mc->dev, budget);
  329. if (n) {
  330. received += n;
  331. budget -= n;
  332. if (budget <= 0)
  333. goto more_work; // XXX What if this is the last one ?
  334. }
  335. }
  336. /* We need to disable IRQs to protect from RXDE IRQ here */
  337. spin_lock_irqsave(&mal->lock, flags);
  338. __napi_complete(napi);
  339. mal_enable_eob_irq(mal);
  340. spin_unlock_irqrestore(&mal->lock, flags);
  341. /* Check for "rotting" packet(s) */
  342. list_for_each(l, &mal->poll_list) {
  343. struct mal_commac *mc =
  344. list_entry(l, struct mal_commac, poll_list);
  345. if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
  346. continue;
  347. if (unlikely(mc->ops->peek_rx(mc->dev) ||
  348. test_bit(MAL_COMMAC_RX_STOPPED, &mc->flags))) {
  349. MAL_DBG2(mal, "rotting packet" NL);
  350. if (napi_reschedule(napi))
  351. mal_disable_eob_irq(mal);
  352. else
  353. MAL_DBG2(mal, "already in poll list" NL);
  354. if (budget > 0)
  355. goto again;
  356. else
  357. goto more_work;
  358. }
  359. mc->ops->poll_tx(mc->dev);
  360. }
  361. more_work:
  362. MAL_DBG2(mal, "poll() %d <- %d" NL, budget, received);
  363. return received;
  364. }
  365. static void mal_reset(struct mal_instance *mal)
  366. {
  367. int n = 10;
  368. MAL_DBG(mal, "reset" NL);
  369. set_mal_dcrn(mal, MAL_CFG, MAL_CFG_SR);
  370. /* Wait for reset to complete (1 system clock) */
  371. while ((get_mal_dcrn(mal, MAL_CFG) & MAL_CFG_SR) && n)
  372. --n;
  373. if (unlikely(!n))
  374. printk(KERN_ERR "mal%d: reset timeout\n", mal->index);
  375. }
  376. int mal_get_regs_len(struct mal_instance *mal)
  377. {
  378. return sizeof(struct emac_ethtool_regs_subhdr) +
  379. sizeof(struct mal_regs);
  380. }
  381. void *mal_dump_regs(struct mal_instance *mal, void *buf)
  382. {
  383. struct emac_ethtool_regs_subhdr *hdr = buf;
  384. struct mal_regs *regs = (struct mal_regs *)(hdr + 1);
  385. int i;
  386. hdr->version = mal->version;
  387. hdr->index = mal->index;
  388. regs->tx_count = mal->num_tx_chans;
  389. regs->rx_count = mal->num_rx_chans;
  390. regs->cfg = get_mal_dcrn(mal, MAL_CFG);
  391. regs->esr = get_mal_dcrn(mal, MAL_ESR);
  392. regs->ier = get_mal_dcrn(mal, MAL_IER);
  393. regs->tx_casr = get_mal_dcrn(mal, MAL_TXCASR);
  394. regs->tx_carr = get_mal_dcrn(mal, MAL_TXCARR);
  395. regs->tx_eobisr = get_mal_dcrn(mal, MAL_TXEOBISR);
  396. regs->tx_deir = get_mal_dcrn(mal, MAL_TXDEIR);
  397. regs->rx_casr = get_mal_dcrn(mal, MAL_RXCASR);
  398. regs->rx_carr = get_mal_dcrn(mal, MAL_RXCARR);
  399. regs->rx_eobisr = get_mal_dcrn(mal, MAL_RXEOBISR);
  400. regs->rx_deir = get_mal_dcrn(mal, MAL_RXDEIR);
  401. for (i = 0; i < regs->tx_count; ++i)
  402. regs->tx_ctpr[i] = get_mal_dcrn(mal, MAL_TXCTPR(i));
  403. for (i = 0; i < regs->rx_count; ++i) {
  404. regs->rx_ctpr[i] = get_mal_dcrn(mal, MAL_RXCTPR(i));
  405. regs->rcbs[i] = get_mal_dcrn(mal, MAL_RCBS(i));
  406. }
  407. return regs + 1;
  408. }
  409. static int mal_probe(struct platform_device *ofdev)
  410. {
  411. struct mal_instance *mal;
  412. int err = 0, i, bd_size;
  413. int index = mal_count++;
  414. unsigned int dcr_base;
  415. const u32 *prop;
  416. u32 cfg;
  417. unsigned long irqflags;
  418. irq_handler_t hdlr_serr, hdlr_txde, hdlr_rxde;
  419. mal = kzalloc(sizeof(struct mal_instance), GFP_KERNEL);
  420. if (!mal)
  421. return -ENOMEM;
  422. mal->index = index;
  423. mal->ofdev = ofdev;
  424. mal->version = of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal2") ? 2 : 1;
  425. MAL_DBG(mal, "probe" NL);
  426. prop = of_get_property(ofdev->dev.of_node, "num-tx-chans", NULL);
  427. if (prop == NULL) {
  428. printk(KERN_ERR
  429. "mal%d: can't find MAL num-tx-chans property!\n",
  430. index);
  431. err = -ENODEV;
  432. goto fail;
  433. }
  434. mal->num_tx_chans = prop[0];
  435. prop = of_get_property(ofdev->dev.of_node, "num-rx-chans", NULL);
  436. if (prop == NULL) {
  437. printk(KERN_ERR
  438. "mal%d: can't find MAL num-rx-chans property!\n",
  439. index);
  440. err = -ENODEV;
  441. goto fail;
  442. }
  443. mal->num_rx_chans = prop[0];
  444. dcr_base = dcr_resource_start(ofdev->dev.of_node, 0);
  445. if (dcr_base == 0) {
  446. printk(KERN_ERR
  447. "mal%d: can't find DCR resource!\n", index);
  448. err = -ENODEV;
  449. goto fail;
  450. }
  451. mal->dcr_host = dcr_map(ofdev->dev.of_node, dcr_base, 0x100);
  452. if (!DCR_MAP_OK(mal->dcr_host)) {
  453. printk(KERN_ERR
  454. "mal%d: failed to map DCRs !\n", index);
  455. err = -ENODEV;
  456. goto fail;
  457. }
  458. if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-405ez")) {
  459. #if defined(CONFIG_IBM_EMAC_MAL_CLR_ICINTSTAT) && \
  460. defined(CONFIG_IBM_EMAC_MAL_COMMON_ERR)
  461. mal->features |= (MAL_FTR_CLEAR_ICINTSTAT |
  462. MAL_FTR_COMMON_ERR_INT);
  463. #else
  464. printk(KERN_ERR "%s: Support for 405EZ not enabled!\n",
  465. ofdev->dev.of_node->full_name);
  466. err = -ENODEV;
  467. goto fail;
  468. #endif
  469. }
  470. mal->txeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  471. mal->rxeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 1);
  472. mal->serr_irq = irq_of_parse_and_map(ofdev->dev.of_node, 2);
  473. if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
  474. mal->txde_irq = mal->rxde_irq = mal->serr_irq;
  475. } else {
  476. mal->txde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 3);
  477. mal->rxde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 4);
  478. }
  479. if (mal->txeob_irq == NO_IRQ || mal->rxeob_irq == NO_IRQ ||
  480. mal->serr_irq == NO_IRQ || mal->txde_irq == NO_IRQ ||
  481. mal->rxde_irq == NO_IRQ) {
  482. printk(KERN_ERR
  483. "mal%d: failed to map interrupts !\n", index);
  484. err = -ENODEV;
  485. goto fail_unmap;
  486. }
  487. INIT_LIST_HEAD(&mal->poll_list);
  488. INIT_LIST_HEAD(&mal->list);
  489. spin_lock_init(&mal->lock);
  490. init_dummy_netdev(&mal->dummy_dev);
  491. netif_napi_add(&mal->dummy_dev, &mal->napi, mal_poll,
  492. CONFIG_IBM_EMAC_POLL_WEIGHT);
  493. /* Load power-on reset defaults */
  494. mal_reset(mal);
  495. /* Set the MAL configuration register */
  496. cfg = (mal->version == 2) ? MAL2_CFG_DEFAULT : MAL1_CFG_DEFAULT;
  497. cfg |= MAL_CFG_PLBB | MAL_CFG_OPBBL | MAL_CFG_LEA;
  498. /* Current Axon is not happy with priority being non-0, it can
  499. * deadlock, fix it up here
  500. */
  501. if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-axon"))
  502. cfg &= ~(MAL2_CFG_RPP_10 | MAL2_CFG_WPP_10);
  503. /* Apply configuration */
  504. set_mal_dcrn(mal, MAL_CFG, cfg);
  505. /* Allocate space for BD rings */
  506. BUG_ON(mal->num_tx_chans <= 0 || mal->num_tx_chans > 32);
  507. BUG_ON(mal->num_rx_chans <= 0 || mal->num_rx_chans > 32);
  508. bd_size = sizeof(struct mal_descriptor) *
  509. (NUM_TX_BUFF * mal->num_tx_chans +
  510. NUM_RX_BUFF * mal->num_rx_chans);
  511. mal->bd_virt = dma_alloc_coherent(&ofdev->dev, bd_size, &mal->bd_dma,
  512. GFP_KERNEL | __GFP_ZERO);
  513. if (mal->bd_virt == NULL) {
  514. err = -ENOMEM;
  515. goto fail_unmap;
  516. }
  517. for (i = 0; i < mal->num_tx_chans; ++i)
  518. set_mal_dcrn(mal, MAL_TXCTPR(i), mal->bd_dma +
  519. sizeof(struct mal_descriptor) *
  520. mal_tx_bd_offset(mal, i));
  521. for (i = 0; i < mal->num_rx_chans; ++i)
  522. set_mal_dcrn(mal, MAL_RXCTPR(i), mal->bd_dma +
  523. sizeof(struct mal_descriptor) *
  524. mal_rx_bd_offset(mal, i));
  525. if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
  526. irqflags = IRQF_SHARED;
  527. hdlr_serr = hdlr_txde = hdlr_rxde = mal_int;
  528. } else {
  529. irqflags = 0;
  530. hdlr_serr = mal_serr;
  531. hdlr_txde = mal_txde;
  532. hdlr_rxde = mal_rxde;
  533. }
  534. err = request_irq(mal->serr_irq, hdlr_serr, irqflags, "MAL SERR", mal);
  535. if (err)
  536. goto fail2;
  537. err = request_irq(mal->txde_irq, hdlr_txde, irqflags, "MAL TX DE", mal);
  538. if (err)
  539. goto fail3;
  540. err = request_irq(mal->txeob_irq, mal_txeob, 0, "MAL TX EOB", mal);
  541. if (err)
  542. goto fail4;
  543. err = request_irq(mal->rxde_irq, hdlr_rxde, irqflags, "MAL RX DE", mal);
  544. if (err)
  545. goto fail5;
  546. err = request_irq(mal->rxeob_irq, mal_rxeob, 0, "MAL RX EOB", mal);
  547. if (err)
  548. goto fail6;
  549. /* Enable all MAL SERR interrupt sources */
  550. if (mal->version == 2)
  551. set_mal_dcrn(mal, MAL_IER, MAL2_IER_EVENTS);
  552. else
  553. set_mal_dcrn(mal, MAL_IER, MAL1_IER_EVENTS);
  554. /* Enable EOB interrupt */
  555. mal_enable_eob_irq(mal);
  556. printk(KERN_INFO
  557. "MAL v%d %s, %d TX channels, %d RX channels\n",
  558. mal->version, ofdev->dev.of_node->full_name,
  559. mal->num_tx_chans, mal->num_rx_chans);
  560. /* Advertise this instance to the rest of the world */
  561. wmb();
  562. dev_set_drvdata(&ofdev->dev, mal);
  563. mal_dbg_register(mal);
  564. return 0;
  565. fail6:
  566. free_irq(mal->rxde_irq, mal);
  567. fail5:
  568. free_irq(mal->txeob_irq, mal);
  569. fail4:
  570. free_irq(mal->txde_irq, mal);
  571. fail3:
  572. free_irq(mal->serr_irq, mal);
  573. fail2:
  574. dma_free_coherent(&ofdev->dev, bd_size, mal->bd_virt, mal->bd_dma);
  575. fail_unmap:
  576. dcr_unmap(mal->dcr_host, 0x100);
  577. fail:
  578. kfree(mal);
  579. return err;
  580. }
  581. static int mal_remove(struct platform_device *ofdev)
  582. {
  583. struct mal_instance *mal = dev_get_drvdata(&ofdev->dev);
  584. MAL_DBG(mal, "remove" NL);
  585. /* Synchronize with scheduled polling */
  586. napi_disable(&mal->napi);
  587. if (!list_empty(&mal->list))
  588. /* This is *very* bad */
  589. WARN(1, KERN_EMERG
  590. "mal%d: commac list is not empty on remove!\n",
  591. mal->index);
  592. dev_set_drvdata(&ofdev->dev, NULL);
  593. free_irq(mal->serr_irq, mal);
  594. free_irq(mal->txde_irq, mal);
  595. free_irq(mal->txeob_irq, mal);
  596. free_irq(mal->rxde_irq, mal);
  597. free_irq(mal->rxeob_irq, mal);
  598. mal_reset(mal);
  599. mal_dbg_unregister(mal);
  600. dma_free_coherent(&ofdev->dev,
  601. sizeof(struct mal_descriptor) *
  602. (NUM_TX_BUFF * mal->num_tx_chans +
  603. NUM_RX_BUFF * mal->num_rx_chans), mal->bd_virt,
  604. mal->bd_dma);
  605. kfree(mal);
  606. return 0;
  607. }
  608. static struct of_device_id mal_platform_match[] =
  609. {
  610. {
  611. .compatible = "ibm,mcmal",
  612. },
  613. {
  614. .compatible = "ibm,mcmal2",
  615. },
  616. /* Backward compat */
  617. {
  618. .type = "mcmal-dma",
  619. .compatible = "ibm,mcmal",
  620. },
  621. {
  622. .type = "mcmal-dma",
  623. .compatible = "ibm,mcmal2",
  624. },
  625. {},
  626. };
  627. static struct platform_driver mal_of_driver = {
  628. .driver = {
  629. .name = "mcmal",
  630. .owner = THIS_MODULE,
  631. .of_match_table = mal_platform_match,
  632. },
  633. .probe = mal_probe,
  634. .remove = mal_remove,
  635. };
  636. int __init mal_init(void)
  637. {
  638. return platform_driver_register(&mal_of_driver);
  639. }
  640. void mal_exit(void)
  641. {
  642. platform_driver_unregister(&mal_of_driver);
  643. }