mal.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. #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 __devinit mal_probe(struct of_device *ofdev,
  410. const struct of_device_id *match)
  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->node, "ibm,mcmal2") ? 2 : 1;
  430. MAL_DBG(mal, "probe" NL);
  431. prop = of_get_property(ofdev->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->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->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->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->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->node->full_name);
  471. err = -ENODEV;
  472. goto fail;
  473. #endif
  474. }
  475. mal->txeob_irq = irq_of_parse_and_map(ofdev->node, 0);
  476. mal->rxeob_irq = irq_of_parse_and_map(ofdev->node, 1);
  477. mal->serr_irq = irq_of_parse_and_map(ofdev->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->node, 3);
  482. mal->rxde_irq = irq_of_parse_and_map(ofdev->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->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->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 of_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 of_platform_driver mal_of_driver = {
  640. .name = "mcmal",
  641. .match_table = mal_platform_match,
  642. .probe = mal_probe,
  643. .remove = mal_remove,
  644. };
  645. int __init mal_init(void)
  646. {
  647. return of_register_platform_driver(&mal_of_driver);
  648. }
  649. void mal_exit(void)
  650. {
  651. of_unregister_platform_driver(&mal_of_driver);
  652. }