defza.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* FDDI network adapter driver for DEC FDDIcontroller 700/700-C devices.
  3. *
  4. * Copyright (c) 2018 Maciej W. Rozycki
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * References:
  12. *
  13. * Dave Sawyer & Phil Weeks & Frank Itkowsky,
  14. * "DEC FDDIcontroller 700 Port Specification",
  15. * Revision 1.1, Digital Equipment Corporation
  16. */
  17. /* ------------------------------------------------------------------------- */
  18. /* FZA configurable parameters. */
  19. /* The number of transmit ring descriptors; either 0 for 512 or 1 for 1024. */
  20. #define FZA_RING_TX_MODE 0
  21. /* The number of receive ring descriptors; from 2 up to 256. */
  22. #define FZA_RING_RX_SIZE 256
  23. /* End of FZA configurable parameters. No need to change anything below. */
  24. /* ------------------------------------------------------------------------- */
  25. #include <linux/delay.h>
  26. #include <linux/device.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/io.h>
  31. #include <linux/io-64-nonatomic-lo-hi.h>
  32. #include <linux/ioport.h>
  33. #include <linux/kernel.h>
  34. #include <linux/list.h>
  35. #include <linux/module.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/fddidevice.h>
  38. #include <linux/sched.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/stat.h>
  42. #include <linux/tc.h>
  43. #include <linux/timer.h>
  44. #include <linux/types.h>
  45. #include <linux/wait.h>
  46. #include <asm/barrier.h>
  47. #include "defza.h"
  48. #define DRV_NAME "defza"
  49. #define DRV_VERSION "v.1.1.4"
  50. #define DRV_RELDATE "Oct 6 2018"
  51. static const char version[] =
  52. DRV_NAME ": " DRV_VERSION " " DRV_RELDATE " Maciej W. Rozycki\n";
  53. MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
  54. MODULE_DESCRIPTION("DEC FDDIcontroller 700 (DEFZA-xx) driver");
  55. MODULE_LICENSE("GPL");
  56. static int loopback;
  57. module_param(loopback, int, 0644);
  58. /* Ring Purger Multicast */
  59. static u8 hw_addr_purger[8] = { 0x09, 0x00, 0x2b, 0x02, 0x01, 0x05 };
  60. /* Directed Beacon Multicast */
  61. static u8 hw_addr_beacon[8] = { 0x01, 0x80, 0xc2, 0x00, 0x01, 0x00 };
  62. /* Shorthands for MMIO accesses that we require to be strongly ordered
  63. * WRT preceding MMIO accesses.
  64. */
  65. #define readw_o readw_relaxed
  66. #define readl_o readl_relaxed
  67. #define writew_o writew_relaxed
  68. #define writel_o writel_relaxed
  69. /* Shorthands for MMIO accesses that we are happy with being weakly ordered
  70. * WRT preceding MMIO accesses.
  71. */
  72. #define readw_u readw_relaxed
  73. #define readl_u readl_relaxed
  74. #define readq_u readq_relaxed
  75. #define writew_u writew_relaxed
  76. #define writel_u writel_relaxed
  77. #define writeq_u writeq_relaxed
  78. static inline struct sk_buff *fza_alloc_skb_irq(struct net_device *dev,
  79. unsigned int length)
  80. {
  81. return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
  82. }
  83. static inline struct sk_buff *fza_alloc_skb(struct net_device *dev,
  84. unsigned int length)
  85. {
  86. return __netdev_alloc_skb(dev, length, GFP_KERNEL);
  87. }
  88. static inline void fza_skb_align(struct sk_buff *skb, unsigned int v)
  89. {
  90. unsigned long x, y;
  91. x = (unsigned long)skb->data;
  92. y = ALIGN(x, v);
  93. skb_reserve(skb, y - x);
  94. }
  95. static inline void fza_reads(const void __iomem *from, void *to,
  96. unsigned long size)
  97. {
  98. if (sizeof(unsigned long) == 8) {
  99. const u64 __iomem *src = from;
  100. const u32 __iomem *src_trail;
  101. u64 *dst = to;
  102. u32 *dst_trail;
  103. for (size = (size + 3) / 4; size > 1; size -= 2)
  104. *dst++ = readq_u(src++);
  105. if (size) {
  106. src_trail = (u32 __iomem *)src;
  107. dst_trail = (u32 *)dst;
  108. *dst_trail = readl_u(src_trail);
  109. }
  110. } else {
  111. const u32 __iomem *src = from;
  112. u32 *dst = to;
  113. for (size = (size + 3) / 4; size; size--)
  114. *dst++ = readl_u(src++);
  115. }
  116. }
  117. static inline void fza_writes(const void *from, void __iomem *to,
  118. unsigned long size)
  119. {
  120. if (sizeof(unsigned long) == 8) {
  121. const u64 *src = from;
  122. const u32 *src_trail;
  123. u64 __iomem *dst = to;
  124. u32 __iomem *dst_trail;
  125. for (size = (size + 3) / 4; size > 1; size -= 2)
  126. writeq_u(*src++, dst++);
  127. if (size) {
  128. src_trail = (u32 *)src;
  129. dst_trail = (u32 __iomem *)dst;
  130. writel_u(*src_trail, dst_trail);
  131. }
  132. } else {
  133. const u32 *src = from;
  134. u32 __iomem *dst = to;
  135. for (size = (size + 3) / 4; size; size--)
  136. writel_u(*src++, dst++);
  137. }
  138. }
  139. static inline void fza_moves(const void __iomem *from, void __iomem *to,
  140. unsigned long size)
  141. {
  142. if (sizeof(unsigned long) == 8) {
  143. const u64 __iomem *src = from;
  144. const u32 __iomem *src_trail;
  145. u64 __iomem *dst = to;
  146. u32 __iomem *dst_trail;
  147. for (size = (size + 3) / 4; size > 1; size -= 2)
  148. writeq_u(readq_u(src++), dst++);
  149. if (size) {
  150. src_trail = (u32 __iomem *)src;
  151. dst_trail = (u32 __iomem *)dst;
  152. writel_u(readl_u(src_trail), dst_trail);
  153. }
  154. } else {
  155. const u32 __iomem *src = from;
  156. u32 __iomem *dst = to;
  157. for (size = (size + 3) / 4; size; size--)
  158. writel_u(readl_u(src++), dst++);
  159. }
  160. }
  161. static inline void fza_zeros(void __iomem *to, unsigned long size)
  162. {
  163. if (sizeof(unsigned long) == 8) {
  164. u64 __iomem *dst = to;
  165. u32 __iomem *dst_trail;
  166. for (size = (size + 3) / 4; size > 1; size -= 2)
  167. writeq_u(0, dst++);
  168. if (size) {
  169. dst_trail = (u32 __iomem *)dst;
  170. writel_u(0, dst_trail);
  171. }
  172. } else {
  173. u32 __iomem *dst = to;
  174. for (size = (size + 3) / 4; size; size--)
  175. writel_u(0, dst++);
  176. }
  177. }
  178. static inline void fza_regs_dump(struct fza_private *fp)
  179. {
  180. pr_debug("%s: iomem registers:\n", fp->name);
  181. pr_debug(" reset: 0x%04x\n", readw_o(&fp->regs->reset));
  182. pr_debug(" interrupt event: 0x%04x\n", readw_u(&fp->regs->int_event));
  183. pr_debug(" status: 0x%04x\n", readw_u(&fp->regs->status));
  184. pr_debug(" interrupt mask: 0x%04x\n", readw_u(&fp->regs->int_mask));
  185. pr_debug(" control A: 0x%04x\n", readw_u(&fp->regs->control_a));
  186. pr_debug(" control B: 0x%04x\n", readw_u(&fp->regs->control_b));
  187. }
  188. static inline void fza_do_reset(struct fza_private *fp)
  189. {
  190. /* Reset the board. */
  191. writew_o(FZA_RESET_INIT, &fp->regs->reset);
  192. readw_o(&fp->regs->reset); /* Synchronize. */
  193. readw_o(&fp->regs->reset); /* Read it back for a small delay. */
  194. writew_o(FZA_RESET_CLR, &fp->regs->reset);
  195. /* Enable all interrupt events we handle. */
  196. writew_o(fp->int_mask, &fp->regs->int_mask);
  197. readw_o(&fp->regs->int_mask); /* Synchronize. */
  198. }
  199. static inline void fza_do_shutdown(struct fza_private *fp)
  200. {
  201. /* Disable the driver mode. */
  202. writew_o(FZA_CONTROL_B_IDLE, &fp->regs->control_b);
  203. /* And reset the board. */
  204. writew_o(FZA_RESET_INIT, &fp->regs->reset);
  205. readw_o(&fp->regs->reset); /* Synchronize. */
  206. writew_o(FZA_RESET_CLR, &fp->regs->reset);
  207. readw_o(&fp->regs->reset); /* Synchronize. */
  208. }
  209. static int fza_reset(struct fza_private *fp)
  210. {
  211. unsigned long flags;
  212. uint status, state;
  213. long t;
  214. pr_info("%s: resetting the board...\n", fp->name);
  215. spin_lock_irqsave(&fp->lock, flags);
  216. fp->state_chg_flag = 0;
  217. fza_do_reset(fp);
  218. spin_unlock_irqrestore(&fp->lock, flags);
  219. /* DEC says RESET needs up to 30 seconds to complete. My DEFZA-AA
  220. * rev. C03 happily finishes in 9.7 seconds. :-) But we need to
  221. * be on the safe side...
  222. */
  223. t = wait_event_timeout(fp->state_chg_wait, fp->state_chg_flag,
  224. 45 * HZ);
  225. status = readw_u(&fp->regs->status);
  226. state = FZA_STATUS_GET_STATE(status);
  227. if (fp->state_chg_flag == 0) {
  228. pr_err("%s: RESET timed out!, state %x\n", fp->name, state);
  229. return -EIO;
  230. }
  231. if (state != FZA_STATE_UNINITIALIZED) {
  232. pr_err("%s: RESET failed!, state %x, failure ID %x\n",
  233. fp->name, state, FZA_STATUS_GET_TEST(status));
  234. return -EIO;
  235. }
  236. pr_info("%s: OK\n", fp->name);
  237. pr_debug("%s: RESET: %lums elapsed\n", fp->name,
  238. (45 * HZ - t) * 1000 / HZ);
  239. return 0;
  240. }
  241. static struct fza_ring_cmd __iomem *fza_cmd_send(struct net_device *dev,
  242. int command)
  243. {
  244. struct fza_private *fp = netdev_priv(dev);
  245. struct fza_ring_cmd __iomem *ring = fp->ring_cmd + fp->ring_cmd_index;
  246. unsigned int old_mask, new_mask;
  247. union fza_cmd_buf __iomem *buf;
  248. struct netdev_hw_addr *ha;
  249. int i;
  250. old_mask = fp->int_mask;
  251. new_mask = old_mask & ~FZA_MASK_STATE_CHG;
  252. writew_u(new_mask, &fp->regs->int_mask);
  253. readw_o(&fp->regs->int_mask); /* Synchronize. */
  254. fp->int_mask = new_mask;
  255. buf = fp->mmio + readl_u(&ring->buffer);
  256. if ((readl_u(&ring->cmd_own) & FZA_RING_OWN_MASK) !=
  257. FZA_RING_OWN_HOST) {
  258. pr_warn("%s: command buffer full, command: %u!\n", fp->name,
  259. command);
  260. return NULL;
  261. }
  262. switch (command) {
  263. case FZA_RING_CMD_INIT:
  264. writel_u(FZA_RING_TX_MODE, &buf->init.tx_mode);
  265. writel_u(FZA_RING_RX_SIZE, &buf->init.hst_rx_size);
  266. fza_zeros(&buf->init.counters, sizeof(buf->init.counters));
  267. break;
  268. case FZA_RING_CMD_MODCAM:
  269. i = 0;
  270. fza_writes(&hw_addr_purger, &buf->cam.hw_addr[i++],
  271. sizeof(*buf->cam.hw_addr));
  272. fza_writes(&hw_addr_beacon, &buf->cam.hw_addr[i++],
  273. sizeof(*buf->cam.hw_addr));
  274. netdev_for_each_mc_addr(ha, dev) {
  275. if (i >= FZA_CMD_CAM_SIZE)
  276. break;
  277. fza_writes(ha->addr, &buf->cam.hw_addr[i++],
  278. sizeof(*buf->cam.hw_addr));
  279. }
  280. while (i < FZA_CMD_CAM_SIZE)
  281. fza_zeros(&buf->cam.hw_addr[i++],
  282. sizeof(*buf->cam.hw_addr));
  283. break;
  284. case FZA_RING_CMD_PARAM:
  285. writel_u(loopback, &buf->param.loop_mode);
  286. writel_u(fp->t_max, &buf->param.t_max);
  287. writel_u(fp->t_req, &buf->param.t_req);
  288. writel_u(fp->tvx, &buf->param.tvx);
  289. writel_u(fp->lem_threshold, &buf->param.lem_threshold);
  290. fza_writes(&fp->station_id, &buf->param.station_id,
  291. sizeof(buf->param.station_id));
  292. /* Convert to milliseconds due to buggy firmware. */
  293. writel_u(fp->rtoken_timeout / 12500,
  294. &buf->param.rtoken_timeout);
  295. writel_u(fp->ring_purger, &buf->param.ring_purger);
  296. break;
  297. case FZA_RING_CMD_MODPROM:
  298. if (dev->flags & IFF_PROMISC) {
  299. writel_u(1, &buf->modprom.llc_prom);
  300. writel_u(1, &buf->modprom.smt_prom);
  301. } else {
  302. writel_u(0, &buf->modprom.llc_prom);
  303. writel_u(0, &buf->modprom.smt_prom);
  304. }
  305. if (dev->flags & IFF_ALLMULTI ||
  306. netdev_mc_count(dev) > FZA_CMD_CAM_SIZE - 2)
  307. writel_u(1, &buf->modprom.llc_multi);
  308. else
  309. writel_u(0, &buf->modprom.llc_multi);
  310. writel_u(1, &buf->modprom.llc_bcast);
  311. break;
  312. }
  313. /* Trigger the command. */
  314. writel_u(FZA_RING_OWN_FZA | command, &ring->cmd_own);
  315. writew_o(FZA_CONTROL_A_CMD_POLL, &fp->regs->control_a);
  316. fp->ring_cmd_index = (fp->ring_cmd_index + 1) % FZA_RING_CMD_SIZE;
  317. fp->int_mask = old_mask;
  318. writew_u(fp->int_mask, &fp->regs->int_mask);
  319. return ring;
  320. }
  321. static int fza_init_send(struct net_device *dev,
  322. struct fza_cmd_init *__iomem *init)
  323. {
  324. struct fza_private *fp = netdev_priv(dev);
  325. struct fza_ring_cmd __iomem *ring;
  326. unsigned long flags;
  327. u32 stat;
  328. long t;
  329. spin_lock_irqsave(&fp->lock, flags);
  330. fp->cmd_done_flag = 0;
  331. ring = fza_cmd_send(dev, FZA_RING_CMD_INIT);
  332. spin_unlock_irqrestore(&fp->lock, flags);
  333. if (!ring)
  334. /* This should never happen in the uninitialized state,
  335. * so do not try to recover and just consider it fatal.
  336. */
  337. return -ENOBUFS;
  338. /* INIT may take quite a long time (160ms for my C03). */
  339. t = wait_event_timeout(fp->cmd_done_wait, fp->cmd_done_flag, 3 * HZ);
  340. if (fp->cmd_done_flag == 0) {
  341. pr_err("%s: INIT command timed out!, state %x\n", fp->name,
  342. FZA_STATUS_GET_STATE(readw_u(&fp->regs->status)));
  343. return -EIO;
  344. }
  345. stat = readl_u(&ring->stat);
  346. if (stat != FZA_RING_STAT_SUCCESS) {
  347. pr_err("%s: INIT command failed!, status %02x, state %x\n",
  348. fp->name, stat,
  349. FZA_STATUS_GET_STATE(readw_u(&fp->regs->status)));
  350. return -EIO;
  351. }
  352. pr_debug("%s: INIT: %lums elapsed\n", fp->name,
  353. (3 * HZ - t) * 1000 / HZ);
  354. if (init)
  355. *init = fp->mmio + readl_u(&ring->buffer);
  356. return 0;
  357. }
  358. static void fza_rx_init(struct fza_private *fp)
  359. {
  360. int i;
  361. /* Fill the host receive descriptor ring. */
  362. for (i = 0; i < FZA_RING_RX_SIZE; i++) {
  363. writel_o(0, &fp->ring_hst_rx[i].rmc);
  364. writel_o((fp->rx_dma[i] + 0x1000) >> 9,
  365. &fp->ring_hst_rx[i].buffer1);
  366. writel_o(fp->rx_dma[i] >> 9 | FZA_RING_OWN_FZA,
  367. &fp->ring_hst_rx[i].buf0_own);
  368. }
  369. }
  370. static void fza_set_rx_mode(struct net_device *dev)
  371. {
  372. fza_cmd_send(dev, FZA_RING_CMD_MODCAM);
  373. fza_cmd_send(dev, FZA_RING_CMD_MODPROM);
  374. }
  375. union fza_buffer_txp {
  376. struct fza_buffer_tx *data_ptr;
  377. struct fza_buffer_tx __iomem *mmio_ptr;
  378. };
  379. static int fza_do_xmit(union fza_buffer_txp ub, int len,
  380. struct net_device *dev, int smt)
  381. {
  382. struct fza_private *fp = netdev_priv(dev);
  383. struct fza_buffer_tx __iomem *rmc_tx_ptr;
  384. int i, first, frag_len, left_len;
  385. u32 own, rmc;
  386. if (((((fp->ring_rmc_txd_index - 1 + fp->ring_rmc_tx_size) -
  387. fp->ring_rmc_tx_index) % fp->ring_rmc_tx_size) *
  388. FZA_TX_BUFFER_SIZE) < len)
  389. return 1;
  390. first = fp->ring_rmc_tx_index;
  391. left_len = len;
  392. frag_len = FZA_TX_BUFFER_SIZE;
  393. /* First descriptor is relinquished last. */
  394. own = FZA_RING_TX_OWN_HOST;
  395. /* First descriptor carries frame length; we don't use cut-through. */
  396. rmc = FZA_RING_TX_SOP | FZA_RING_TX_VBC | len;
  397. do {
  398. i = fp->ring_rmc_tx_index;
  399. rmc_tx_ptr = &fp->buffer_tx[i];
  400. if (left_len < FZA_TX_BUFFER_SIZE)
  401. frag_len = left_len;
  402. left_len -= frag_len;
  403. /* Length must be a multiple of 4 as only word writes are
  404. * permitted!
  405. */
  406. frag_len = (frag_len + 3) & ~3;
  407. if (smt)
  408. fza_moves(ub.mmio_ptr, rmc_tx_ptr, frag_len);
  409. else
  410. fza_writes(ub.data_ptr, rmc_tx_ptr, frag_len);
  411. if (left_len == 0)
  412. rmc |= FZA_RING_TX_EOP; /* Mark last frag. */
  413. writel_o(rmc, &fp->ring_rmc_tx[i].rmc);
  414. writel_o(own, &fp->ring_rmc_tx[i].own);
  415. ub.data_ptr++;
  416. fp->ring_rmc_tx_index = (fp->ring_rmc_tx_index + 1) %
  417. fp->ring_rmc_tx_size;
  418. /* Settings for intermediate frags. */
  419. own = FZA_RING_TX_OWN_RMC;
  420. rmc = 0;
  421. } while (left_len > 0);
  422. if (((((fp->ring_rmc_txd_index - 1 + fp->ring_rmc_tx_size) -
  423. fp->ring_rmc_tx_index) % fp->ring_rmc_tx_size) *
  424. FZA_TX_BUFFER_SIZE) < dev->mtu + dev->hard_header_len) {
  425. netif_stop_queue(dev);
  426. pr_debug("%s: queue stopped\n", fp->name);
  427. }
  428. writel_o(FZA_RING_TX_OWN_RMC, &fp->ring_rmc_tx[first].own);
  429. /* Go, go, go! */
  430. writew_o(FZA_CONTROL_A_TX_POLL, &fp->regs->control_a);
  431. return 0;
  432. }
  433. static int fza_do_recv_smt(struct fza_buffer_tx *data_ptr, int len,
  434. u32 rmc, struct net_device *dev)
  435. {
  436. struct fza_private *fp = netdev_priv(dev);
  437. struct fza_buffer_tx __iomem *smt_rx_ptr;
  438. u32 own;
  439. int i;
  440. i = fp->ring_smt_rx_index;
  441. own = readl_o(&fp->ring_smt_rx[i].own);
  442. if ((own & FZA_RING_OWN_MASK) == FZA_RING_OWN_FZA)
  443. return 1;
  444. smt_rx_ptr = fp->mmio + readl_u(&fp->ring_smt_rx[i].buffer);
  445. /* Length must be a multiple of 4 as only word writes are permitted! */
  446. fza_writes(data_ptr, smt_rx_ptr, (len + 3) & ~3);
  447. writel_o(rmc, &fp->ring_smt_rx[i].rmc);
  448. writel_o(FZA_RING_OWN_FZA, &fp->ring_smt_rx[i].own);
  449. fp->ring_smt_rx_index =
  450. (fp->ring_smt_rx_index + 1) % fp->ring_smt_rx_size;
  451. /* Grab it! */
  452. writew_o(FZA_CONTROL_A_SMT_RX_POLL, &fp->regs->control_a);
  453. return 0;
  454. }
  455. static void fza_tx(struct net_device *dev)
  456. {
  457. struct fza_private *fp = netdev_priv(dev);
  458. u32 own, rmc;
  459. int i;
  460. while (1) {
  461. i = fp->ring_rmc_txd_index;
  462. if (i == fp->ring_rmc_tx_index)
  463. break;
  464. own = readl_o(&fp->ring_rmc_tx[i].own);
  465. if ((own & FZA_RING_OWN_MASK) == FZA_RING_TX_OWN_RMC)
  466. break;
  467. rmc = readl_u(&fp->ring_rmc_tx[i].rmc);
  468. /* Only process the first descriptor. */
  469. if ((rmc & FZA_RING_TX_SOP) != 0) {
  470. if ((rmc & FZA_RING_TX_DCC_MASK) ==
  471. FZA_RING_TX_DCC_SUCCESS) {
  472. int pkt_len = (rmc & FZA_RING_PBC_MASK) - 3;
  473. /* Omit PRH. */
  474. fp->stats.tx_packets++;
  475. fp->stats.tx_bytes += pkt_len;
  476. } else {
  477. fp->stats.tx_errors++;
  478. switch (rmc & FZA_RING_TX_DCC_MASK) {
  479. case FZA_RING_TX_DCC_DTP_SOP:
  480. case FZA_RING_TX_DCC_DTP:
  481. case FZA_RING_TX_DCC_ABORT:
  482. fp->stats.tx_aborted_errors++;
  483. break;
  484. case FZA_RING_TX_DCC_UNDRRUN:
  485. fp->stats.tx_fifo_errors++;
  486. break;
  487. case FZA_RING_TX_DCC_PARITY:
  488. default:
  489. break;
  490. }
  491. }
  492. }
  493. fp->ring_rmc_txd_index = (fp->ring_rmc_txd_index + 1) %
  494. fp->ring_rmc_tx_size;
  495. }
  496. if (((((fp->ring_rmc_txd_index - 1 + fp->ring_rmc_tx_size) -
  497. fp->ring_rmc_tx_index) % fp->ring_rmc_tx_size) *
  498. FZA_TX_BUFFER_SIZE) >= dev->mtu + dev->hard_header_len) {
  499. if (fp->queue_active) {
  500. netif_wake_queue(dev);
  501. pr_debug("%s: queue woken\n", fp->name);
  502. }
  503. }
  504. }
  505. static inline int fza_rx_err(struct fza_private *fp,
  506. const u32 rmc, const u8 fc)
  507. {
  508. int len, min_len, max_len;
  509. len = rmc & FZA_RING_PBC_MASK;
  510. if (unlikely((rmc & FZA_RING_RX_BAD) != 0)) {
  511. fp->stats.rx_errors++;
  512. /* Check special status codes. */
  513. if ((rmc & (FZA_RING_RX_CRC | FZA_RING_RX_RRR_MASK |
  514. FZA_RING_RX_DA_MASK | FZA_RING_RX_SA_MASK)) ==
  515. (FZA_RING_RX_CRC | FZA_RING_RX_RRR_DADDR |
  516. FZA_RING_RX_DA_CAM | FZA_RING_RX_SA_ALIAS)) {
  517. if (len >= 8190)
  518. fp->stats.rx_length_errors++;
  519. return 1;
  520. }
  521. if ((rmc & (FZA_RING_RX_CRC | FZA_RING_RX_RRR_MASK |
  522. FZA_RING_RX_DA_MASK | FZA_RING_RX_SA_MASK)) ==
  523. (FZA_RING_RX_CRC | FZA_RING_RX_RRR_DADDR |
  524. FZA_RING_RX_DA_CAM | FZA_RING_RX_SA_CAM)) {
  525. /* Halt the interface to trigger a reset. */
  526. writew_o(FZA_CONTROL_A_HALT, &fp->regs->control_a);
  527. readw_o(&fp->regs->control_a); /* Synchronize. */
  528. return 1;
  529. }
  530. /* Check the MAC status. */
  531. switch (rmc & FZA_RING_RX_RRR_MASK) {
  532. case FZA_RING_RX_RRR_OK:
  533. if ((rmc & FZA_RING_RX_CRC) != 0)
  534. fp->stats.rx_crc_errors++;
  535. else if ((rmc & FZA_RING_RX_FSC_MASK) == 0 ||
  536. (rmc & FZA_RING_RX_FSB_ERR) != 0)
  537. fp->stats.rx_frame_errors++;
  538. return 1;
  539. case FZA_RING_RX_RRR_SADDR:
  540. case FZA_RING_RX_RRR_DADDR:
  541. case FZA_RING_RX_RRR_ABORT:
  542. /* Halt the interface to trigger a reset. */
  543. writew_o(FZA_CONTROL_A_HALT, &fp->regs->control_a);
  544. readw_o(&fp->regs->control_a); /* Synchronize. */
  545. return 1;
  546. case FZA_RING_RX_RRR_LENGTH:
  547. fp->stats.rx_frame_errors++;
  548. return 1;
  549. default:
  550. return 1;
  551. }
  552. }
  553. /* Packet received successfully; validate the length. */
  554. switch (fc & FDDI_FC_K_FORMAT_MASK) {
  555. case FDDI_FC_K_FORMAT_MANAGEMENT:
  556. if ((fc & FDDI_FC_K_CLASS_MASK) == FDDI_FC_K_CLASS_ASYNC)
  557. min_len = 37;
  558. else
  559. min_len = 17;
  560. break;
  561. case FDDI_FC_K_FORMAT_LLC:
  562. min_len = 20;
  563. break;
  564. default:
  565. min_len = 17;
  566. break;
  567. }
  568. max_len = 4495;
  569. if (len < min_len || len > max_len) {
  570. fp->stats.rx_errors++;
  571. fp->stats.rx_length_errors++;
  572. return 1;
  573. }
  574. return 0;
  575. }
  576. static void fza_rx(struct net_device *dev)
  577. {
  578. struct fza_private *fp = netdev_priv(dev);
  579. struct sk_buff *skb, *newskb;
  580. struct fza_fddihdr *frame;
  581. dma_addr_t dma, newdma;
  582. u32 own, rmc, buf;
  583. int i, len;
  584. u8 fc;
  585. while (1) {
  586. i = fp->ring_hst_rx_index;
  587. own = readl_o(&fp->ring_hst_rx[i].buf0_own);
  588. if ((own & FZA_RING_OWN_MASK) == FZA_RING_OWN_FZA)
  589. break;
  590. rmc = readl_u(&fp->ring_hst_rx[i].rmc);
  591. skb = fp->rx_skbuff[i];
  592. dma = fp->rx_dma[i];
  593. /* The RMC doesn't count the preamble and the starting
  594. * delimiter. We fix it up here for a total of 3 octets.
  595. */
  596. dma_rmb();
  597. len = (rmc & FZA_RING_PBC_MASK) + 3;
  598. frame = (struct fza_fddihdr *)skb->data;
  599. /* We need to get at real FC. */
  600. dma_sync_single_for_cpu(fp->bdev,
  601. dma +
  602. ((u8 *)&frame->hdr.fc - (u8 *)frame),
  603. sizeof(frame->hdr.fc),
  604. DMA_FROM_DEVICE);
  605. fc = frame->hdr.fc;
  606. if (fza_rx_err(fp, rmc, fc))
  607. goto err_rx;
  608. /* We have to 512-byte-align RX buffers... */
  609. newskb = fza_alloc_skb_irq(dev, FZA_RX_BUFFER_SIZE + 511);
  610. if (newskb) {
  611. fza_skb_align(newskb, 512);
  612. newdma = dma_map_single(fp->bdev, newskb->data,
  613. FZA_RX_BUFFER_SIZE,
  614. DMA_FROM_DEVICE);
  615. if (dma_mapping_error(fp->bdev, newdma)) {
  616. dev_kfree_skb_irq(newskb);
  617. newskb = NULL;
  618. }
  619. }
  620. if (newskb) {
  621. int pkt_len = len - 7; /* Omit P, SD and FCS. */
  622. int is_multi;
  623. int rx_stat;
  624. dma_unmap_single(fp->bdev, dma, FZA_RX_BUFFER_SIZE,
  625. DMA_FROM_DEVICE);
  626. /* Queue SMT frames to the SMT receive ring. */
  627. if ((fc & (FDDI_FC_K_CLASS_MASK |
  628. FDDI_FC_K_FORMAT_MASK)) ==
  629. (FDDI_FC_K_CLASS_ASYNC |
  630. FDDI_FC_K_FORMAT_MANAGEMENT) &&
  631. (rmc & FZA_RING_RX_DA_MASK) !=
  632. FZA_RING_RX_DA_PROM) {
  633. if (fza_do_recv_smt((struct fza_buffer_tx *)
  634. skb->data, len, rmc,
  635. dev)) {
  636. writel_o(FZA_CONTROL_A_SMT_RX_OVFL,
  637. &fp->regs->control_a);
  638. }
  639. }
  640. is_multi = ((frame->hdr.daddr[0] & 0x01) != 0);
  641. skb_reserve(skb, 3); /* Skip over P and SD. */
  642. skb_put(skb, pkt_len); /* And cut off FCS. */
  643. skb->protocol = fddi_type_trans(skb, dev);
  644. rx_stat = netif_rx(skb);
  645. if (rx_stat != NET_RX_DROP) {
  646. fp->stats.rx_packets++;
  647. fp->stats.rx_bytes += pkt_len;
  648. if (is_multi)
  649. fp->stats.multicast++;
  650. } else {
  651. fp->stats.rx_dropped++;
  652. }
  653. skb = newskb;
  654. dma = newdma;
  655. fp->rx_skbuff[i] = skb;
  656. fp->rx_dma[i] = dma;
  657. } else {
  658. fp->stats.rx_dropped++;
  659. pr_notice("%s: memory squeeze, dropping packet\n",
  660. fp->name);
  661. }
  662. err_rx:
  663. writel_o(0, &fp->ring_hst_rx[i].rmc);
  664. buf = (dma + 0x1000) >> 9;
  665. writel_o(buf, &fp->ring_hst_rx[i].buffer1);
  666. buf = dma >> 9 | FZA_RING_OWN_FZA;
  667. writel_o(buf, &fp->ring_hst_rx[i].buf0_own);
  668. fp->ring_hst_rx_index =
  669. (fp->ring_hst_rx_index + 1) % fp->ring_hst_rx_size;
  670. }
  671. }
  672. static void fza_tx_smt(struct net_device *dev)
  673. {
  674. struct fza_private *fp = netdev_priv(dev);
  675. struct fza_buffer_tx __iomem *smt_tx_ptr;
  676. int i, len;
  677. u32 own;
  678. while (1) {
  679. i = fp->ring_smt_tx_index;
  680. own = readl_o(&fp->ring_smt_tx[i].own);
  681. if ((own & FZA_RING_OWN_MASK) == FZA_RING_OWN_FZA)
  682. break;
  683. smt_tx_ptr = fp->mmio + readl_u(&fp->ring_smt_tx[i].buffer);
  684. len = readl_u(&fp->ring_smt_tx[i].rmc) & FZA_RING_PBC_MASK;
  685. if (!netif_queue_stopped(dev)) {
  686. if (dev_nit_active(dev)) {
  687. struct fza_buffer_tx *skb_data_ptr;
  688. struct sk_buff *skb;
  689. /* Length must be a multiple of 4 as only word
  690. * reads are permitted!
  691. */
  692. skb = fza_alloc_skb_irq(dev, (len + 3) & ~3);
  693. if (!skb)
  694. goto err_no_skb; /* Drop. */
  695. skb_data_ptr = (struct fza_buffer_tx *)
  696. skb->data;
  697. fza_reads(smt_tx_ptr, skb_data_ptr,
  698. (len + 3) & ~3);
  699. skb->dev = dev;
  700. skb_reserve(skb, 3); /* Skip over PRH. */
  701. skb_put(skb, len - 3);
  702. skb_reset_network_header(skb);
  703. dev_queue_xmit_nit(skb, dev);
  704. dev_kfree_skb_irq(skb);
  705. err_no_skb:
  706. ;
  707. }
  708. /* Queue the frame to the RMC transmit ring. */
  709. fza_do_xmit((union fza_buffer_txp)
  710. { .mmio_ptr = smt_tx_ptr },
  711. len, dev, 1);
  712. }
  713. writel_o(FZA_RING_OWN_FZA, &fp->ring_smt_tx[i].own);
  714. fp->ring_smt_tx_index =
  715. (fp->ring_smt_tx_index + 1) % fp->ring_smt_tx_size;
  716. }
  717. }
  718. static void fza_uns(struct net_device *dev)
  719. {
  720. struct fza_private *fp = netdev_priv(dev);
  721. u32 own;
  722. int i;
  723. while (1) {
  724. i = fp->ring_uns_index;
  725. own = readl_o(&fp->ring_uns[i].own);
  726. if ((own & FZA_RING_OWN_MASK) == FZA_RING_OWN_FZA)
  727. break;
  728. if (readl_u(&fp->ring_uns[i].id) == FZA_RING_UNS_RX_OVER) {
  729. fp->stats.rx_errors++;
  730. fp->stats.rx_over_errors++;
  731. }
  732. writel_o(FZA_RING_OWN_FZA, &fp->ring_uns[i].own);
  733. fp->ring_uns_index =
  734. (fp->ring_uns_index + 1) % FZA_RING_UNS_SIZE;
  735. }
  736. }
  737. static void fza_tx_flush(struct net_device *dev)
  738. {
  739. struct fza_private *fp = netdev_priv(dev);
  740. u32 own;
  741. int i;
  742. /* Clean up the SMT TX ring. */
  743. i = fp->ring_smt_tx_index;
  744. do {
  745. writel_o(FZA_RING_OWN_FZA, &fp->ring_smt_tx[i].own);
  746. fp->ring_smt_tx_index =
  747. (fp->ring_smt_tx_index + 1) % fp->ring_smt_tx_size;
  748. } while (i != fp->ring_smt_tx_index);
  749. /* Clean up the RMC TX ring. */
  750. i = fp->ring_rmc_tx_index;
  751. do {
  752. own = readl_o(&fp->ring_rmc_tx[i].own);
  753. if ((own & FZA_RING_OWN_MASK) == FZA_RING_TX_OWN_RMC) {
  754. u32 rmc = readl_u(&fp->ring_rmc_tx[i].rmc);
  755. writel_u(rmc | FZA_RING_TX_DTP,
  756. &fp->ring_rmc_tx[i].rmc);
  757. }
  758. fp->ring_rmc_tx_index =
  759. (fp->ring_rmc_tx_index + 1) % fp->ring_rmc_tx_size;
  760. } while (i != fp->ring_rmc_tx_index);
  761. /* Done. */
  762. writew_o(FZA_CONTROL_A_FLUSH_DONE, &fp->regs->control_a);
  763. }
  764. static irqreturn_t fza_interrupt(int irq, void *dev_id)
  765. {
  766. struct net_device *dev = dev_id;
  767. struct fza_private *fp = netdev_priv(dev);
  768. uint int_event;
  769. /* Get interrupt events. */
  770. int_event = readw_o(&fp->regs->int_event) & fp->int_mask;
  771. if (int_event == 0)
  772. return IRQ_NONE;
  773. /* Clear the events. */
  774. writew_u(int_event, &fp->regs->int_event);
  775. /* Now handle the events. The order matters. */
  776. /* Command finished interrupt. */
  777. if ((int_event & FZA_EVENT_CMD_DONE) != 0) {
  778. fp->irq_count_cmd_done++;
  779. spin_lock(&fp->lock);
  780. fp->cmd_done_flag = 1;
  781. wake_up(&fp->cmd_done_wait);
  782. spin_unlock(&fp->lock);
  783. }
  784. /* Transmit finished interrupt. */
  785. if ((int_event & FZA_EVENT_TX_DONE) != 0) {
  786. fp->irq_count_tx_done++;
  787. fza_tx(dev);
  788. }
  789. /* Host receive interrupt. */
  790. if ((int_event & FZA_EVENT_RX_POLL) != 0) {
  791. fp->irq_count_rx_poll++;
  792. fza_rx(dev);
  793. }
  794. /* SMT transmit interrupt. */
  795. if ((int_event & FZA_EVENT_SMT_TX_POLL) != 0) {
  796. fp->irq_count_smt_tx_poll++;
  797. fza_tx_smt(dev);
  798. }
  799. /* Transmit ring flush request. */
  800. if ((int_event & FZA_EVENT_FLUSH_TX) != 0) {
  801. fp->irq_count_flush_tx++;
  802. fza_tx_flush(dev);
  803. }
  804. /* Link status change interrupt. */
  805. if ((int_event & FZA_EVENT_LINK_ST_CHG) != 0) {
  806. uint status;
  807. fp->irq_count_link_st_chg++;
  808. status = readw_u(&fp->regs->status);
  809. if (FZA_STATUS_GET_LINK(status) == FZA_LINK_ON) {
  810. netif_carrier_on(dev);
  811. pr_info("%s: link available\n", fp->name);
  812. } else {
  813. netif_carrier_off(dev);
  814. pr_info("%s: link unavailable\n", fp->name);
  815. }
  816. }
  817. /* Unsolicited event interrupt. */
  818. if ((int_event & FZA_EVENT_UNS_POLL) != 0) {
  819. fp->irq_count_uns_poll++;
  820. fza_uns(dev);
  821. }
  822. /* State change interrupt. */
  823. if ((int_event & FZA_EVENT_STATE_CHG) != 0) {
  824. uint status, state;
  825. fp->irq_count_state_chg++;
  826. status = readw_u(&fp->regs->status);
  827. state = FZA_STATUS_GET_STATE(status);
  828. pr_debug("%s: state change: %x\n", fp->name, state);
  829. switch (state) {
  830. case FZA_STATE_RESET:
  831. break;
  832. case FZA_STATE_UNINITIALIZED:
  833. netif_carrier_off(dev);
  834. del_timer_sync(&fp->reset_timer);
  835. fp->ring_cmd_index = 0;
  836. fp->ring_uns_index = 0;
  837. fp->ring_rmc_tx_index = 0;
  838. fp->ring_rmc_txd_index = 0;
  839. fp->ring_hst_rx_index = 0;
  840. fp->ring_smt_tx_index = 0;
  841. fp->ring_smt_rx_index = 0;
  842. if (fp->state > state) {
  843. pr_info("%s: OK\n", fp->name);
  844. fza_cmd_send(dev, FZA_RING_CMD_INIT);
  845. }
  846. break;
  847. case FZA_STATE_INITIALIZED:
  848. if (fp->state > state) {
  849. fza_set_rx_mode(dev);
  850. fza_cmd_send(dev, FZA_RING_CMD_PARAM);
  851. }
  852. break;
  853. case FZA_STATE_RUNNING:
  854. case FZA_STATE_MAINTENANCE:
  855. fp->state = state;
  856. fza_rx_init(fp);
  857. fp->queue_active = 1;
  858. netif_wake_queue(dev);
  859. pr_debug("%s: queue woken\n", fp->name);
  860. break;
  861. case FZA_STATE_HALTED:
  862. fp->queue_active = 0;
  863. netif_stop_queue(dev);
  864. pr_debug("%s: queue stopped\n", fp->name);
  865. del_timer_sync(&fp->reset_timer);
  866. pr_warn("%s: halted, reason: %x\n", fp->name,
  867. FZA_STATUS_GET_HALT(status));
  868. fza_regs_dump(fp);
  869. pr_info("%s: resetting the board...\n", fp->name);
  870. fza_do_reset(fp);
  871. fp->timer_state = 0;
  872. fp->reset_timer.expires = jiffies + 45 * HZ;
  873. add_timer(&fp->reset_timer);
  874. break;
  875. default:
  876. pr_warn("%s: undefined state: %x\n", fp->name, state);
  877. break;
  878. }
  879. spin_lock(&fp->lock);
  880. fp->state_chg_flag = 1;
  881. wake_up(&fp->state_chg_wait);
  882. spin_unlock(&fp->lock);
  883. }
  884. return IRQ_HANDLED;
  885. }
  886. static void fza_reset_timer(struct timer_list *t)
  887. {
  888. struct fza_private *fp = from_timer(fp, t, reset_timer);
  889. if (!fp->timer_state) {
  890. pr_err("%s: RESET timed out!\n", fp->name);
  891. pr_info("%s: trying harder...\n", fp->name);
  892. /* Assert the board reset. */
  893. writew_o(FZA_RESET_INIT, &fp->regs->reset);
  894. readw_o(&fp->regs->reset); /* Synchronize. */
  895. fp->timer_state = 1;
  896. fp->reset_timer.expires = jiffies + HZ;
  897. } else {
  898. /* Clear the board reset. */
  899. writew_u(FZA_RESET_CLR, &fp->regs->reset);
  900. /* Enable all interrupt events we handle. */
  901. writew_o(fp->int_mask, &fp->regs->int_mask);
  902. readw_o(&fp->regs->int_mask); /* Synchronize. */
  903. fp->timer_state = 0;
  904. fp->reset_timer.expires = jiffies + 45 * HZ;
  905. }
  906. add_timer(&fp->reset_timer);
  907. }
  908. static int fza_set_mac_address(struct net_device *dev, void *addr)
  909. {
  910. return -EOPNOTSUPP;
  911. }
  912. static netdev_tx_t fza_start_xmit(struct sk_buff *skb, struct net_device *dev)
  913. {
  914. struct fza_private *fp = netdev_priv(dev);
  915. unsigned int old_mask, new_mask;
  916. int ret;
  917. u8 fc;
  918. skb_push(skb, 3); /* Make room for PRH. */
  919. /* Decode FC to set PRH. */
  920. fc = skb->data[3];
  921. skb->data[0] = 0;
  922. skb->data[1] = 0;
  923. skb->data[2] = FZA_PRH2_NORMAL;
  924. if ((fc & FDDI_FC_K_CLASS_MASK) == FDDI_FC_K_CLASS_SYNC)
  925. skb->data[0] |= FZA_PRH0_FRAME_SYNC;
  926. switch (fc & FDDI_FC_K_FORMAT_MASK) {
  927. case FDDI_FC_K_FORMAT_MANAGEMENT:
  928. if ((fc & FDDI_FC_K_CONTROL_MASK) == 0) {
  929. /* Token. */
  930. skb->data[0] |= FZA_PRH0_TKN_TYPE_IMM;
  931. skb->data[1] |= FZA_PRH1_TKN_SEND_NONE;
  932. } else {
  933. /* SMT or MAC. */
  934. skb->data[0] |= FZA_PRH0_TKN_TYPE_UNR;
  935. skb->data[1] |= FZA_PRH1_TKN_SEND_UNR;
  936. }
  937. skb->data[1] |= FZA_PRH1_CRC_NORMAL;
  938. break;
  939. case FDDI_FC_K_FORMAT_LLC:
  940. case FDDI_FC_K_FORMAT_FUTURE:
  941. skb->data[0] |= FZA_PRH0_TKN_TYPE_UNR;
  942. skb->data[1] |= FZA_PRH1_CRC_NORMAL | FZA_PRH1_TKN_SEND_UNR;
  943. break;
  944. case FDDI_FC_K_FORMAT_IMPLEMENTOR:
  945. skb->data[0] |= FZA_PRH0_TKN_TYPE_UNR;
  946. skb->data[1] |= FZA_PRH1_TKN_SEND_ORIG;
  947. break;
  948. }
  949. /* SMT transmit interrupts may sneak frames into the RMC
  950. * transmit ring. We disable them while queueing a frame
  951. * to maintain consistency.
  952. */
  953. old_mask = fp->int_mask;
  954. new_mask = old_mask & ~FZA_MASK_SMT_TX_POLL;
  955. writew_u(new_mask, &fp->regs->int_mask);
  956. readw_o(&fp->regs->int_mask); /* Synchronize. */
  957. fp->int_mask = new_mask;
  958. ret = fza_do_xmit((union fza_buffer_txp)
  959. { .data_ptr = (struct fza_buffer_tx *)skb->data },
  960. skb->len, dev, 0);
  961. fp->int_mask = old_mask;
  962. writew_u(fp->int_mask, &fp->regs->int_mask);
  963. if (ret) {
  964. /* Probably an SMT packet filled the remaining space,
  965. * so just stop the queue, but don't report it as an error.
  966. */
  967. netif_stop_queue(dev);
  968. pr_debug("%s: queue stopped\n", fp->name);
  969. fp->stats.tx_dropped++;
  970. }
  971. dev_kfree_skb(skb);
  972. return ret;
  973. }
  974. static int fza_open(struct net_device *dev)
  975. {
  976. struct fza_private *fp = netdev_priv(dev);
  977. struct fza_ring_cmd __iomem *ring;
  978. struct sk_buff *skb;
  979. unsigned long flags;
  980. dma_addr_t dma;
  981. int ret, i;
  982. u32 stat;
  983. long t;
  984. for (i = 0; i < FZA_RING_RX_SIZE; i++) {
  985. /* We have to 512-byte-align RX buffers... */
  986. skb = fza_alloc_skb(dev, FZA_RX_BUFFER_SIZE + 511);
  987. if (skb) {
  988. fza_skb_align(skb, 512);
  989. dma = dma_map_single(fp->bdev, skb->data,
  990. FZA_RX_BUFFER_SIZE,
  991. DMA_FROM_DEVICE);
  992. if (dma_mapping_error(fp->bdev, dma)) {
  993. dev_kfree_skb(skb);
  994. skb = NULL;
  995. }
  996. }
  997. if (!skb) {
  998. for (--i; i >= 0; i--) {
  999. dma_unmap_single(fp->bdev, fp->rx_dma[i],
  1000. FZA_RX_BUFFER_SIZE,
  1001. DMA_FROM_DEVICE);
  1002. dev_kfree_skb(fp->rx_skbuff[i]);
  1003. fp->rx_dma[i] = 0;
  1004. fp->rx_skbuff[i] = NULL;
  1005. }
  1006. return -ENOMEM;
  1007. }
  1008. fp->rx_skbuff[i] = skb;
  1009. fp->rx_dma[i] = dma;
  1010. }
  1011. ret = fza_init_send(dev, NULL);
  1012. if (ret != 0)
  1013. return ret;
  1014. /* Purger and Beacon multicasts need to be supplied before PARAM. */
  1015. fza_set_rx_mode(dev);
  1016. spin_lock_irqsave(&fp->lock, flags);
  1017. fp->cmd_done_flag = 0;
  1018. ring = fza_cmd_send(dev, FZA_RING_CMD_PARAM);
  1019. spin_unlock_irqrestore(&fp->lock, flags);
  1020. if (!ring)
  1021. return -ENOBUFS;
  1022. t = wait_event_timeout(fp->cmd_done_wait, fp->cmd_done_flag, 3 * HZ);
  1023. if (fp->cmd_done_flag == 0) {
  1024. pr_err("%s: PARAM command timed out!, state %x\n", fp->name,
  1025. FZA_STATUS_GET_STATE(readw_u(&fp->regs->status)));
  1026. return -EIO;
  1027. }
  1028. stat = readl_u(&ring->stat);
  1029. if (stat != FZA_RING_STAT_SUCCESS) {
  1030. pr_err("%s: PARAM command failed!, status %02x, state %x\n",
  1031. fp->name, stat,
  1032. FZA_STATUS_GET_STATE(readw_u(&fp->regs->status)));
  1033. return -EIO;
  1034. }
  1035. pr_debug("%s: PARAM: %lums elapsed\n", fp->name,
  1036. (3 * HZ - t) * 1000 / HZ);
  1037. return 0;
  1038. }
  1039. static int fza_close(struct net_device *dev)
  1040. {
  1041. struct fza_private *fp = netdev_priv(dev);
  1042. unsigned long flags;
  1043. uint state;
  1044. long t;
  1045. int i;
  1046. netif_stop_queue(dev);
  1047. pr_debug("%s: queue stopped\n", fp->name);
  1048. del_timer_sync(&fp->reset_timer);
  1049. spin_lock_irqsave(&fp->lock, flags);
  1050. fp->state = FZA_STATE_UNINITIALIZED;
  1051. fp->state_chg_flag = 0;
  1052. /* Shut the interface down. */
  1053. writew_o(FZA_CONTROL_A_SHUT, &fp->regs->control_a);
  1054. readw_o(&fp->regs->control_a); /* Synchronize. */
  1055. spin_unlock_irqrestore(&fp->lock, flags);
  1056. /* DEC says SHUT needs up to 10 seconds to complete. */
  1057. t = wait_event_timeout(fp->state_chg_wait, fp->state_chg_flag,
  1058. 15 * HZ);
  1059. state = FZA_STATUS_GET_STATE(readw_o(&fp->regs->status));
  1060. if (fp->state_chg_flag == 0) {
  1061. pr_err("%s: SHUT timed out!, state %x\n", fp->name, state);
  1062. return -EIO;
  1063. }
  1064. if (state != FZA_STATE_UNINITIALIZED) {
  1065. pr_err("%s: SHUT failed!, state %x\n", fp->name, state);
  1066. return -EIO;
  1067. }
  1068. pr_debug("%s: SHUT: %lums elapsed\n", fp->name,
  1069. (15 * HZ - t) * 1000 / HZ);
  1070. for (i = 0; i < FZA_RING_RX_SIZE; i++)
  1071. if (fp->rx_skbuff[i]) {
  1072. dma_unmap_single(fp->bdev, fp->rx_dma[i],
  1073. FZA_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
  1074. dev_kfree_skb(fp->rx_skbuff[i]);
  1075. fp->rx_dma[i] = 0;
  1076. fp->rx_skbuff[i] = NULL;
  1077. }
  1078. return 0;
  1079. }
  1080. static struct net_device_stats *fza_get_stats(struct net_device *dev)
  1081. {
  1082. struct fza_private *fp = netdev_priv(dev);
  1083. return &fp->stats;
  1084. }
  1085. static int fza_probe(struct device *bdev)
  1086. {
  1087. static const struct net_device_ops netdev_ops = {
  1088. .ndo_open = fza_open,
  1089. .ndo_stop = fza_close,
  1090. .ndo_start_xmit = fza_start_xmit,
  1091. .ndo_set_rx_mode = fza_set_rx_mode,
  1092. .ndo_set_mac_address = fza_set_mac_address,
  1093. .ndo_get_stats = fza_get_stats,
  1094. };
  1095. static int version_printed;
  1096. char rom_rev[4], fw_rev[4], rmc_rev[4];
  1097. struct tc_dev *tdev = to_tc_dev(bdev);
  1098. struct fza_cmd_init __iomem *init;
  1099. resource_size_t start, len;
  1100. struct net_device *dev;
  1101. struct fza_private *fp;
  1102. uint smt_ver, pmd_type;
  1103. void __iomem *mmio;
  1104. uint hw_addr[2];
  1105. int ret, i;
  1106. if (!version_printed) {
  1107. pr_info("%s", version);
  1108. version_printed = 1;
  1109. }
  1110. dev = alloc_fddidev(sizeof(*fp));
  1111. if (!dev)
  1112. return -ENOMEM;
  1113. SET_NETDEV_DEV(dev, bdev);
  1114. fp = netdev_priv(dev);
  1115. dev_set_drvdata(bdev, dev);
  1116. fp->bdev = bdev;
  1117. fp->name = dev_name(bdev);
  1118. /* Request the I/O MEM resource. */
  1119. start = tdev->resource.start;
  1120. len = tdev->resource.end - start + 1;
  1121. if (!request_mem_region(start, len, dev_name(bdev))) {
  1122. pr_err("%s: cannot reserve MMIO region\n", fp->name);
  1123. ret = -EBUSY;
  1124. goto err_out_kfree;
  1125. }
  1126. /* MMIO mapping setup. */
  1127. mmio = ioremap_nocache(start, len);
  1128. if (!mmio) {
  1129. pr_err("%s: cannot map MMIO\n", fp->name);
  1130. ret = -ENOMEM;
  1131. goto err_out_resource;
  1132. }
  1133. /* Initialize the new device structure. */
  1134. switch (loopback) {
  1135. case FZA_LOOP_NORMAL:
  1136. case FZA_LOOP_INTERN:
  1137. case FZA_LOOP_EXTERN:
  1138. break;
  1139. default:
  1140. loopback = FZA_LOOP_NORMAL;
  1141. }
  1142. fp->mmio = mmio;
  1143. dev->irq = tdev->interrupt;
  1144. pr_info("%s: DEC FDDIcontroller 700 or 700-C at 0x%08llx, irq %d\n",
  1145. fp->name, (long long)tdev->resource.start, dev->irq);
  1146. pr_debug("%s: mapped at: 0x%p\n", fp->name, mmio);
  1147. fp->regs = mmio + FZA_REG_BASE;
  1148. fp->ring_cmd = mmio + FZA_RING_CMD;
  1149. fp->ring_uns = mmio + FZA_RING_UNS;
  1150. init_waitqueue_head(&fp->state_chg_wait);
  1151. init_waitqueue_head(&fp->cmd_done_wait);
  1152. spin_lock_init(&fp->lock);
  1153. fp->int_mask = FZA_MASK_NORMAL;
  1154. timer_setup(&fp->reset_timer, fza_reset_timer, 0);
  1155. /* Sanitize the board. */
  1156. fza_regs_dump(fp);
  1157. fza_do_shutdown(fp);
  1158. ret = request_irq(dev->irq, fza_interrupt, IRQF_SHARED, fp->name, dev);
  1159. if (ret != 0) {
  1160. pr_err("%s: unable to get IRQ %d!\n", fp->name, dev->irq);
  1161. goto err_out_map;
  1162. }
  1163. /* Enable the driver mode. */
  1164. writew_o(FZA_CONTROL_B_DRIVER, &fp->regs->control_b);
  1165. /* For some reason transmit done interrupts can trigger during
  1166. * reset. This avoids a division error in the handler.
  1167. */
  1168. fp->ring_rmc_tx_size = FZA_RING_TX_SIZE;
  1169. ret = fza_reset(fp);
  1170. if (ret != 0)
  1171. goto err_out_irq;
  1172. ret = fza_init_send(dev, &init);
  1173. if (ret != 0)
  1174. goto err_out_irq;
  1175. fza_reads(&init->hw_addr, &hw_addr, sizeof(hw_addr));
  1176. memcpy(dev->dev_addr, &hw_addr, FDDI_K_ALEN);
  1177. fza_reads(&init->rom_rev, &rom_rev, sizeof(rom_rev));
  1178. fza_reads(&init->fw_rev, &fw_rev, sizeof(fw_rev));
  1179. fza_reads(&init->rmc_rev, &rmc_rev, sizeof(rmc_rev));
  1180. for (i = 3; i >= 0 && rom_rev[i] == ' '; i--)
  1181. rom_rev[i] = 0;
  1182. for (i = 3; i >= 0 && fw_rev[i] == ' '; i--)
  1183. fw_rev[i] = 0;
  1184. for (i = 3; i >= 0 && rmc_rev[i] == ' '; i--)
  1185. rmc_rev[i] = 0;
  1186. fp->ring_rmc_tx = mmio + readl_u(&init->rmc_tx);
  1187. fp->ring_rmc_tx_size = readl_u(&init->rmc_tx_size);
  1188. fp->ring_hst_rx = mmio + readl_u(&init->hst_rx);
  1189. fp->ring_hst_rx_size = readl_u(&init->hst_rx_size);
  1190. fp->ring_smt_tx = mmio + readl_u(&init->smt_tx);
  1191. fp->ring_smt_tx_size = readl_u(&init->smt_tx_size);
  1192. fp->ring_smt_rx = mmio + readl_u(&init->smt_rx);
  1193. fp->ring_smt_rx_size = readl_u(&init->smt_rx_size);
  1194. fp->buffer_tx = mmio + FZA_TX_BUFFER_ADDR(readl_u(&init->rmc_tx));
  1195. fp->t_max = readl_u(&init->def_t_max);
  1196. fp->t_req = readl_u(&init->def_t_req);
  1197. fp->tvx = readl_u(&init->def_tvx);
  1198. fp->lem_threshold = readl_u(&init->lem_threshold);
  1199. fza_reads(&init->def_station_id, &fp->station_id,
  1200. sizeof(fp->station_id));
  1201. fp->rtoken_timeout = readl_u(&init->rtoken_timeout);
  1202. fp->ring_purger = readl_u(&init->ring_purger);
  1203. smt_ver = readl_u(&init->smt_ver);
  1204. pmd_type = readl_u(&init->pmd_type);
  1205. pr_debug("%s: INIT parameters:\n", fp->name);
  1206. pr_debug(" tx_mode: %u\n", readl_u(&init->tx_mode));
  1207. pr_debug(" hst_rx_size: %u\n", readl_u(&init->hst_rx_size));
  1208. pr_debug(" rmc_rev: %.4s\n", rmc_rev);
  1209. pr_debug(" rom_rev: %.4s\n", rom_rev);
  1210. pr_debug(" fw_rev: %.4s\n", fw_rev);
  1211. pr_debug(" mop_type: %u\n", readl_u(&init->mop_type));
  1212. pr_debug(" hst_rx: 0x%08x\n", readl_u(&init->hst_rx));
  1213. pr_debug(" rmc_tx: 0x%08x\n", readl_u(&init->rmc_tx));
  1214. pr_debug(" rmc_tx_size: %u\n", readl_u(&init->rmc_tx_size));
  1215. pr_debug(" smt_tx: 0x%08x\n", readl_u(&init->smt_tx));
  1216. pr_debug(" smt_tx_size: %u\n", readl_u(&init->smt_tx_size));
  1217. pr_debug(" smt_rx: 0x%08x\n", readl_u(&init->smt_rx));
  1218. pr_debug(" smt_rx_size: %u\n", readl_u(&init->smt_rx_size));
  1219. /* TC systems are always LE, so don't bother swapping. */
  1220. pr_debug(" hw_addr: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  1221. (readl_u(&init->hw_addr[0]) >> 0) & 0xff,
  1222. (readl_u(&init->hw_addr[0]) >> 8) & 0xff,
  1223. (readl_u(&init->hw_addr[0]) >> 16) & 0xff,
  1224. (readl_u(&init->hw_addr[0]) >> 24) & 0xff,
  1225. (readl_u(&init->hw_addr[1]) >> 0) & 0xff,
  1226. (readl_u(&init->hw_addr[1]) >> 8) & 0xff,
  1227. (readl_u(&init->hw_addr[1]) >> 16) & 0xff,
  1228. (readl_u(&init->hw_addr[1]) >> 24) & 0xff);
  1229. pr_debug(" def_t_req: %u\n", readl_u(&init->def_t_req));
  1230. pr_debug(" def_tvx: %u\n", readl_u(&init->def_tvx));
  1231. pr_debug(" def_t_max: %u\n", readl_u(&init->def_t_max));
  1232. pr_debug(" lem_threshold: %u\n", readl_u(&init->lem_threshold));
  1233. /* Don't bother swapping, see above. */
  1234. pr_debug(" def_station_id: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  1235. (readl_u(&init->def_station_id[0]) >> 0) & 0xff,
  1236. (readl_u(&init->def_station_id[0]) >> 8) & 0xff,
  1237. (readl_u(&init->def_station_id[0]) >> 16) & 0xff,
  1238. (readl_u(&init->def_station_id[0]) >> 24) & 0xff,
  1239. (readl_u(&init->def_station_id[1]) >> 0) & 0xff,
  1240. (readl_u(&init->def_station_id[1]) >> 8) & 0xff,
  1241. (readl_u(&init->def_station_id[1]) >> 16) & 0xff,
  1242. (readl_u(&init->def_station_id[1]) >> 24) & 0xff);
  1243. pr_debug(" pmd_type_alt: %u\n", readl_u(&init->pmd_type_alt));
  1244. pr_debug(" smt_ver: %u\n", readl_u(&init->smt_ver));
  1245. pr_debug(" rtoken_timeout: %u\n", readl_u(&init->rtoken_timeout));
  1246. pr_debug(" ring_purger: %u\n", readl_u(&init->ring_purger));
  1247. pr_debug(" smt_ver_max: %u\n", readl_u(&init->smt_ver_max));
  1248. pr_debug(" smt_ver_min: %u\n", readl_u(&init->smt_ver_min));
  1249. pr_debug(" pmd_type: %u\n", readl_u(&init->pmd_type));
  1250. pr_info("%s: model %s, address %pMF\n",
  1251. fp->name,
  1252. pmd_type == FZA_PMD_TYPE_TW ?
  1253. "700-C (DEFZA-CA), ThinWire PMD selected" :
  1254. pmd_type == FZA_PMD_TYPE_STP ?
  1255. "700-C (DEFZA-CA), STP PMD selected" :
  1256. "700 (DEFZA-AA), MMF PMD",
  1257. dev->dev_addr);
  1258. pr_info("%s: ROM rev. %.4s, firmware rev. %.4s, RMC rev. %.4s, "
  1259. "SMT ver. %u\n", fp->name, rom_rev, fw_rev, rmc_rev, smt_ver);
  1260. /* Now that we fetched initial parameters just shut the interface
  1261. * until opened.
  1262. */
  1263. ret = fza_close(dev);
  1264. if (ret != 0)
  1265. goto err_out_irq;
  1266. /* The FZA-specific entries in the device structure. */
  1267. dev->netdev_ops = &netdev_ops;
  1268. ret = register_netdev(dev);
  1269. if (ret != 0)
  1270. goto err_out_irq;
  1271. pr_info("%s: registered as %s\n", fp->name, dev->name);
  1272. fp->name = (const char *)dev->name;
  1273. get_device(bdev);
  1274. return 0;
  1275. err_out_irq:
  1276. del_timer_sync(&fp->reset_timer);
  1277. fza_do_shutdown(fp);
  1278. free_irq(dev->irq, dev);
  1279. err_out_map:
  1280. iounmap(mmio);
  1281. err_out_resource:
  1282. release_mem_region(start, len);
  1283. err_out_kfree:
  1284. pr_err("%s: initialization failure, aborting!\n", fp->name);
  1285. free_netdev(dev);
  1286. return ret;
  1287. }
  1288. static int fza_remove(struct device *bdev)
  1289. {
  1290. struct net_device *dev = dev_get_drvdata(bdev);
  1291. struct fza_private *fp = netdev_priv(dev);
  1292. struct tc_dev *tdev = to_tc_dev(bdev);
  1293. resource_size_t start, len;
  1294. put_device(bdev);
  1295. unregister_netdev(dev);
  1296. del_timer_sync(&fp->reset_timer);
  1297. fza_do_shutdown(fp);
  1298. free_irq(dev->irq, dev);
  1299. iounmap(fp->mmio);
  1300. start = tdev->resource.start;
  1301. len = tdev->resource.end - start + 1;
  1302. release_mem_region(start, len);
  1303. free_netdev(dev);
  1304. return 0;
  1305. }
  1306. static struct tc_device_id const fza_tc_table[] = {
  1307. { "DEC ", "PMAF-AA " },
  1308. { }
  1309. };
  1310. MODULE_DEVICE_TABLE(tc, fza_tc_table);
  1311. static struct tc_driver fza_driver = {
  1312. .id_table = fza_tc_table,
  1313. .driver = {
  1314. .name = "defza",
  1315. .bus = &tc_bus_type,
  1316. .probe = fza_probe,
  1317. .remove = fza_remove,
  1318. },
  1319. };
  1320. static int fza_init(void)
  1321. {
  1322. return tc_register_driver(&fza_driver);
  1323. }
  1324. static void fza_exit(void)
  1325. {
  1326. tc_unregister_driver(&fza_driver);
  1327. }
  1328. module_init(fza_init);
  1329. module_exit(fza_exit);