mal.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 <linux/slab.h>
  29. #include "core.h"
  30. #include <asm/dcr-regs.h>
  31. static int mal_count;
  32. int __devinit mal_register_commac(struct mal_instance *mal,
  33. 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. mal_disable_eob_irq(mal);
  207. __napi_schedule(&mal->napi);
  208. } else
  209. MAL_DBG2(mal, "already in poll" NL);
  210. }
  211. static irqreturn_t mal_txeob(int irq, void *dev_instance)
  212. {
  213. struct mal_instance *mal = dev_instance;
  214. u32 r = get_mal_dcrn(mal, MAL_TXEOBISR);
  215. MAL_DBG2(mal, "txeob %08x" NL, r);
  216. mal_schedule_poll(mal);
  217. set_mal_dcrn(mal, MAL_TXEOBISR, r);
  218. #ifdef CONFIG_PPC_DCR_NATIVE
  219. if (mal_has_feature(mal, MAL_FTR_CLEAR_ICINTSTAT))
  220. mtdcri(SDR0, DCRN_SDR_ICINTSTAT,
  221. (mfdcri(SDR0, DCRN_SDR_ICINTSTAT) | ICINTSTAT_ICTX));
  222. #endif
  223. return IRQ_HANDLED;
  224. }
  225. static irqreturn_t mal_rxeob(int irq, void *dev_instance)
  226. {
  227. struct mal_instance *mal = dev_instance;
  228. u32 r = get_mal_dcrn(mal, MAL_RXEOBISR);
  229. MAL_DBG2(mal, "rxeob %08x" NL, r);
  230. mal_schedule_poll(mal);
  231. set_mal_dcrn(mal, MAL_RXEOBISR, r);
  232. #ifdef CONFIG_PPC_DCR_NATIVE
  233. if (mal_has_feature(mal, MAL_FTR_CLEAR_ICINTSTAT))
  234. mtdcri(SDR0, DCRN_SDR_ICINTSTAT,
  235. (mfdcri(SDR0, DCRN_SDR_ICINTSTAT) | ICINTSTAT_ICRX));
  236. #endif
  237. return IRQ_HANDLED;
  238. }
  239. static irqreturn_t mal_txde(int irq, void *dev_instance)
  240. {
  241. struct mal_instance *mal = dev_instance;
  242. u32 deir = get_mal_dcrn(mal, MAL_TXDEIR);
  243. set_mal_dcrn(mal, MAL_TXDEIR, deir);
  244. MAL_DBG(mal, "txde %08x" NL, deir);
  245. if (net_ratelimit())
  246. printk(KERN_ERR
  247. "mal%d: TX descriptor error (TXDEIR = 0x%08x)\n",
  248. mal->index, deir);
  249. return IRQ_HANDLED;
  250. }
  251. static irqreturn_t mal_rxde(int irq, void *dev_instance)
  252. {
  253. struct mal_instance *mal = dev_instance;
  254. struct list_head *l;
  255. u32 deir = get_mal_dcrn(mal, MAL_RXDEIR);
  256. MAL_DBG(mal, "rxde %08x" NL, deir);
  257. list_for_each(l, &mal->list) {
  258. struct mal_commac *mc = list_entry(l, struct mal_commac, list);
  259. if (deir & mc->rx_chan_mask) {
  260. set_bit(MAL_COMMAC_RX_STOPPED, &mc->flags);
  261. mc->ops->rxde(mc->dev);
  262. }
  263. }
  264. mal_schedule_poll(mal);
  265. set_mal_dcrn(mal, MAL_RXDEIR, deir);
  266. return IRQ_HANDLED;
  267. }
  268. static irqreturn_t mal_int(int irq, void *dev_instance)
  269. {
  270. struct mal_instance *mal = dev_instance;
  271. u32 esr = get_mal_dcrn(mal, MAL_ESR);
  272. if (esr & MAL_ESR_EVB) {
  273. /* descriptor error */
  274. if (esr & MAL_ESR_DE) {
  275. if (esr & MAL_ESR_CIDT)
  276. return mal_rxde(irq, dev_instance);
  277. else
  278. return mal_txde(irq, dev_instance);
  279. } else { /* SERR */
  280. return mal_serr(irq, dev_instance);
  281. }
  282. }
  283. return IRQ_HANDLED;
  284. }
  285. void mal_poll_disable(struct mal_instance *mal, struct mal_commac *commac)
  286. {
  287. /* Spinlock-type semantics: only one caller disable poll at a time */
  288. while (test_and_set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags))
  289. msleep(1);
  290. /* Synchronize with the MAL NAPI poller */
  291. napi_synchronize(&mal->napi);
  292. }
  293. void mal_poll_enable(struct mal_instance *mal, struct mal_commac *commac)
  294. {
  295. smp_wmb();
  296. clear_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
  297. /* Feels better to trigger a poll here to catch up with events that
  298. * may have happened on this channel while disabled. It will most
  299. * probably be delayed until the next interrupt but that's mostly a
  300. * non-issue in the context where this is called.
  301. */
  302. napi_schedule(&mal->napi);
  303. }
  304. static int mal_poll(struct napi_struct *napi, int budget)
  305. {
  306. struct mal_instance *mal = container_of(napi, struct mal_instance, napi);
  307. struct list_head *l;
  308. int received = 0;
  309. unsigned long flags;
  310. MAL_DBG2(mal, "poll(%d)" NL, budget);
  311. again:
  312. /* Process TX skbs */
  313. list_for_each(l, &mal->poll_list) {
  314. struct mal_commac *mc =
  315. list_entry(l, struct mal_commac, poll_list);
  316. mc->ops->poll_tx(mc->dev);
  317. }
  318. /* Process RX skbs.
  319. *
  320. * We _might_ need something more smart here to enforce polling
  321. * fairness.
  322. */
  323. list_for_each(l, &mal->poll_list) {
  324. struct mal_commac *mc =
  325. list_entry(l, struct mal_commac, poll_list);
  326. int n;
  327. if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
  328. continue;
  329. n = mc->ops->poll_rx(mc->dev, budget);
  330. if (n) {
  331. received += n;
  332. budget -= n;
  333. if (budget <= 0)
  334. goto more_work; // XXX What if this is the last one ?
  335. }
  336. }
  337. /* We need to disable IRQs to protect from RXDE IRQ here */
  338. spin_lock_irqsave(&mal->lock, flags);
  339. __napi_complete(napi);
  340. mal_enable_eob_irq(mal);
  341. spin_unlock_irqrestore(&mal->lock, flags);
  342. /* Check for "rotting" packet(s) */
  343. list_for_each(l, &mal->poll_list) {
  344. struct mal_commac *mc =
  345. list_entry(l, struct mal_commac, poll_list);
  346. if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
  347. continue;
  348. if (unlikely(mc->ops->peek_rx(mc->dev) ||
  349. test_bit(MAL_COMMAC_RX_STOPPED, &mc->flags))) {
  350. MAL_DBG2(mal, "rotting packet" NL);
  351. if (napi_reschedule(napi))
  352. mal_disable_eob_irq(mal);
  353. else
  354. MAL_DBG2(mal, "already in poll list" NL);
  355. if (budget > 0)
  356. goto again;
  357. else
  358. goto more_work;
  359. }
  360. mc->ops->poll_tx(mc->dev);
  361. }
  362. more_work:
  363. MAL_DBG2(mal, "poll() %d <- %d" NL, budget, received);
  364. return received;
  365. }
  366. static void mal_reset(struct mal_instance *mal)
  367. {
  368. int n = 10;
  369. MAL_DBG(mal, "reset" NL);
  370. set_mal_dcrn(mal, MAL_CFG, MAL_CFG_SR);
  371. /* Wait for reset to complete (1 system clock) */
  372. while ((get_mal_dcrn(mal, MAL_CFG) & MAL_CFG_SR) && n)
  373. --n;
  374. if (unlikely(!n))
  375. printk(KERN_ERR "mal%d: reset timeout\n", mal->index);
  376. }
  377. int mal_get_regs_len(struct mal_instance *mal)
  378. {
  379. return sizeof(struct emac_ethtool_regs_subhdr) +
  380. sizeof(struct mal_regs);
  381. }
  382. void *mal_dump_regs(struct mal_instance *mal, void *buf)
  383. {
  384. struct emac_ethtool_regs_subhdr *hdr = buf;
  385. struct mal_regs *regs = (struct mal_regs *)(hdr + 1);
  386. int i;
  387. hdr->version = mal->version;
  388. hdr->index = mal->index;
  389. regs->tx_count = mal->num_tx_chans;
  390. regs->rx_count = mal->num_rx_chans;
  391. regs->cfg = get_mal_dcrn(mal, MAL_CFG);
  392. regs->esr = get_mal_dcrn(mal, MAL_ESR);
  393. regs->ier = get_mal_dcrn(mal, MAL_IER);
  394. regs->tx_casr = get_mal_dcrn(mal, MAL_TXCASR);
  395. regs->tx_carr = get_mal_dcrn(mal, MAL_TXCARR);
  396. regs->tx_eobisr = get_mal_dcrn(mal, MAL_TXEOBISR);
  397. regs->tx_deir = get_mal_dcrn(mal, MAL_TXDEIR);
  398. regs->rx_casr = get_mal_dcrn(mal, MAL_RXCASR);
  399. regs->rx_carr = get_mal_dcrn(mal, MAL_RXCARR);
  400. regs->rx_eobisr = get_mal_dcrn(mal, MAL_RXEOBISR);
  401. regs->rx_deir = get_mal_dcrn(mal, MAL_RXDEIR);
  402. for (i = 0; i < regs->tx_count; ++i)
  403. regs->tx_ctpr[i] = get_mal_dcrn(mal, MAL_TXCTPR(i));
  404. for (i = 0; i < regs->rx_count; ++i) {
  405. regs->rx_ctpr[i] = get_mal_dcrn(mal, MAL_RXCTPR(i));
  406. regs->rcbs[i] = get_mal_dcrn(mal, MAL_RCBS(i));
  407. }
  408. return regs + 1;
  409. }
  410. static int __devinit mal_probe(struct platform_device *ofdev)
  411. {
  412. struct mal_instance *mal;
  413. int err = 0, i, bd_size;
  414. int index = mal_count++;
  415. unsigned int dcr_base;
  416. const u32 *prop;
  417. u32 cfg;
  418. unsigned long irqflags;
  419. irq_handler_t hdlr_serr, hdlr_txde, hdlr_rxde;
  420. mal = kzalloc(sizeof(struct mal_instance), GFP_KERNEL);
  421. if (!mal) {
  422. printk(KERN_ERR
  423. "mal%d: out of memory allocating MAL structure!\n",
  424. index);
  425. return -ENOMEM;
  426. }
  427. mal->index = index;
  428. mal->ofdev = ofdev;
  429. mal->version = of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal2") ? 2 : 1;
  430. MAL_DBG(mal, "probe" NL);
  431. prop = of_get_property(ofdev->dev.of_node, "num-tx-chans", NULL);
  432. if (prop == NULL) {
  433. printk(KERN_ERR
  434. "mal%d: can't find MAL num-tx-chans property!\n",
  435. index);
  436. err = -ENODEV;
  437. goto fail;
  438. }
  439. mal->num_tx_chans = prop[0];
  440. prop = of_get_property(ofdev->dev.of_node, "num-rx-chans", NULL);
  441. if (prop == NULL) {
  442. printk(KERN_ERR
  443. "mal%d: can't find MAL num-rx-chans property!\n",
  444. index);
  445. err = -ENODEV;
  446. goto fail;
  447. }
  448. mal->num_rx_chans = prop[0];
  449. dcr_base = dcr_resource_start(ofdev->dev.of_node, 0);
  450. if (dcr_base == 0) {
  451. printk(KERN_ERR
  452. "mal%d: can't find DCR resource!\n", index);
  453. err = -ENODEV;
  454. goto fail;
  455. }
  456. mal->dcr_host = dcr_map(ofdev->dev.of_node, dcr_base, 0x100);
  457. if (!DCR_MAP_OK(mal->dcr_host)) {
  458. printk(KERN_ERR
  459. "mal%d: failed to map DCRs !\n", index);
  460. err = -ENODEV;
  461. goto fail;
  462. }
  463. if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-405ez")) {
  464. #if defined(CONFIG_IBM_NEW_EMAC_MAL_CLR_ICINTSTAT) && \
  465. defined(CONFIG_IBM_NEW_EMAC_MAL_COMMON_ERR)
  466. mal->features |= (MAL_FTR_CLEAR_ICINTSTAT |
  467. MAL_FTR_COMMON_ERR_INT);
  468. #else
  469. printk(KERN_ERR "%s: Support for 405EZ not enabled!\n",
  470. ofdev->dev.of_node->full_name);
  471. err = -ENODEV;
  472. goto fail;
  473. #endif
  474. }
  475. mal->txeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  476. mal->rxeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 1);
  477. mal->serr_irq = irq_of_parse_and_map(ofdev->dev.of_node, 2);
  478. if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
  479. mal->txde_irq = mal->rxde_irq = mal->serr_irq;
  480. } else {
  481. mal->txde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 3);
  482. mal->rxde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 4);
  483. }
  484. if (mal->txeob_irq == NO_IRQ || mal->rxeob_irq == NO_IRQ ||
  485. mal->serr_irq == NO_IRQ || mal->txde_irq == NO_IRQ ||
  486. mal->rxde_irq == NO_IRQ) {
  487. printk(KERN_ERR
  488. "mal%d: failed to map interrupts !\n", index);
  489. err = -ENODEV;
  490. goto fail_unmap;
  491. }
  492. INIT_LIST_HEAD(&mal->poll_list);
  493. INIT_LIST_HEAD(&mal->list);
  494. spin_lock_init(&mal->lock);
  495. init_dummy_netdev(&mal->dummy_dev);
  496. netif_napi_add(&mal->dummy_dev, &mal->napi, mal_poll,
  497. CONFIG_IBM_NEW_EMAC_POLL_WEIGHT);
  498. /* Load power-on reset defaults */
  499. mal_reset(mal);
  500. /* Set the MAL configuration register */
  501. cfg = (mal->version == 2) ? MAL2_CFG_DEFAULT : MAL1_CFG_DEFAULT;
  502. cfg |= MAL_CFG_PLBB | MAL_CFG_OPBBL | MAL_CFG_LEA;
  503. /* Current Axon is not happy with priority being non-0, it can
  504. * deadlock, fix it up here
  505. */
  506. if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-axon"))
  507. cfg &= ~(MAL2_CFG_RPP_10 | MAL2_CFG_WPP_10);
  508. /* Apply configuration */
  509. set_mal_dcrn(mal, MAL_CFG, cfg);
  510. /* Allocate space for BD rings */
  511. BUG_ON(mal->num_tx_chans <= 0 || mal->num_tx_chans > 32);
  512. BUG_ON(mal->num_rx_chans <= 0 || mal->num_rx_chans > 32);
  513. bd_size = sizeof(struct mal_descriptor) *
  514. (NUM_TX_BUFF * mal->num_tx_chans +
  515. NUM_RX_BUFF * mal->num_rx_chans);
  516. mal->bd_virt =
  517. dma_alloc_coherent(&ofdev->dev, bd_size, &mal->bd_dma,
  518. GFP_KERNEL);
  519. if (mal->bd_virt == NULL) {
  520. printk(KERN_ERR
  521. "mal%d: out of memory allocating RX/TX descriptors!\n",
  522. index);
  523. err = -ENOMEM;
  524. goto fail_unmap;
  525. }
  526. memset(mal->bd_virt, 0, bd_size);
  527. for (i = 0; i < mal->num_tx_chans; ++i)
  528. set_mal_dcrn(mal, MAL_TXCTPR(i), mal->bd_dma +
  529. sizeof(struct mal_descriptor) *
  530. mal_tx_bd_offset(mal, i));
  531. for (i = 0; i < mal->num_rx_chans; ++i)
  532. set_mal_dcrn(mal, MAL_RXCTPR(i), mal->bd_dma +
  533. sizeof(struct mal_descriptor) *
  534. mal_rx_bd_offset(mal, i));
  535. if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
  536. irqflags = IRQF_SHARED;
  537. hdlr_serr = hdlr_txde = hdlr_rxde = mal_int;
  538. } else {
  539. irqflags = 0;
  540. hdlr_serr = mal_serr;
  541. hdlr_txde = mal_txde;
  542. hdlr_rxde = mal_rxde;
  543. }
  544. err = request_irq(mal->serr_irq, hdlr_serr, irqflags, "MAL SERR", mal);
  545. if (err)
  546. goto fail2;
  547. err = request_irq(mal->txde_irq, hdlr_txde, irqflags, "MAL TX DE", mal);
  548. if (err)
  549. goto fail3;
  550. err = request_irq(mal->txeob_irq, mal_txeob, 0, "MAL TX EOB", mal);
  551. if (err)
  552. goto fail4;
  553. err = request_irq(mal->rxde_irq, hdlr_rxde, irqflags, "MAL RX DE", mal);
  554. if (err)
  555. goto fail5;
  556. err = request_irq(mal->rxeob_irq, mal_rxeob, 0, "MAL RX EOB", mal);
  557. if (err)
  558. goto fail6;
  559. /* Enable all MAL SERR interrupt sources */
  560. if (mal->version == 2)
  561. set_mal_dcrn(mal, MAL_IER, MAL2_IER_EVENTS);
  562. else
  563. set_mal_dcrn(mal, MAL_IER, MAL1_IER_EVENTS);
  564. /* Enable EOB interrupt */
  565. mal_enable_eob_irq(mal);
  566. printk(KERN_INFO
  567. "MAL v%d %s, %d TX channels, %d RX channels\n",
  568. mal->version, ofdev->dev.of_node->full_name,
  569. mal->num_tx_chans, mal->num_rx_chans);
  570. /* Advertise this instance to the rest of the world */
  571. wmb();
  572. dev_set_drvdata(&ofdev->dev, mal);
  573. mal_dbg_register(mal);
  574. return 0;
  575. fail6:
  576. free_irq(mal->rxde_irq, mal);
  577. fail5:
  578. free_irq(mal->txeob_irq, mal);
  579. fail4:
  580. free_irq(mal->txde_irq, mal);
  581. fail3:
  582. free_irq(mal->serr_irq, mal);
  583. fail2:
  584. dma_free_coherent(&ofdev->dev, bd_size, mal->bd_virt, mal->bd_dma);
  585. fail_unmap:
  586. dcr_unmap(mal->dcr_host, 0x100);
  587. fail:
  588. kfree(mal);
  589. return err;
  590. }
  591. static int __devexit mal_remove(struct platform_device *ofdev)
  592. {
  593. struct mal_instance *mal = dev_get_drvdata(&ofdev->dev);
  594. MAL_DBG(mal, "remove" NL);
  595. /* Synchronize with scheduled polling */
  596. napi_disable(&mal->napi);
  597. if (!list_empty(&mal->list)) {
  598. /* This is *very* bad */
  599. printk(KERN_EMERG
  600. "mal%d: commac list is not empty on remove!\n",
  601. mal->index);
  602. WARN_ON(1);
  603. }
  604. dev_set_drvdata(&ofdev->dev, NULL);
  605. free_irq(mal->serr_irq, mal);
  606. free_irq(mal->txde_irq, mal);
  607. free_irq(mal->txeob_irq, mal);
  608. free_irq(mal->rxde_irq, mal);
  609. free_irq(mal->rxeob_irq, mal);
  610. mal_reset(mal);
  611. mal_dbg_unregister(mal);
  612. dma_free_coherent(&ofdev->dev,
  613. sizeof(struct mal_descriptor) *
  614. (NUM_TX_BUFF * mal->num_tx_chans +
  615. NUM_RX_BUFF * mal->num_rx_chans), mal->bd_virt,
  616. mal->bd_dma);
  617. kfree(mal);
  618. return 0;
  619. }
  620. static struct of_device_id mal_platform_match[] =
  621. {
  622. {
  623. .compatible = "ibm,mcmal",
  624. },
  625. {
  626. .compatible = "ibm,mcmal2",
  627. },
  628. /* Backward compat */
  629. {
  630. .type = "mcmal-dma",
  631. .compatible = "ibm,mcmal",
  632. },
  633. {
  634. .type = "mcmal-dma",
  635. .compatible = "ibm,mcmal2",
  636. },
  637. {},
  638. };
  639. static struct platform_driver mal_of_driver = {
  640. .driver = {
  641. .name = "mcmal",
  642. .owner = THIS_MODULE,
  643. .of_match_table = mal_platform_match,
  644. },
  645. .probe = mal_probe,
  646. .remove = mal_remove,
  647. };
  648. int __init mal_init(void)
  649. {
  650. return platform_driver_register(&mal_of_driver);
  651. }
  652. void mal_exit(void)
  653. {
  654. platform_driver_unregister(&mal_of_driver);
  655. }