mal.c 19 KB

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