mal.c 18 KB

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