spider_net.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  1. /*
  2. * Network device driver for Cell Processor-Based Blade and Celleb platform
  3. *
  4. * (C) Copyright IBM Corp. 2005
  5. * (C) Copyright 2006 TOSHIBA CORPORATION
  6. *
  7. * Authors : Utz Bacher <utz.bacher@de.ibm.com>
  8. * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/compiler.h>
  25. #include <linux/crc32.h>
  26. #include <linux/delay.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/firmware.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/in.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/gfp.h>
  35. #include <linux/ioport.h>
  36. #include <linux/ip.h>
  37. #include <linux/kernel.h>
  38. #include <linux/mii.h>
  39. #include <linux/module.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/device.h>
  42. #include <linux/pci.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/tcp.h>
  45. #include <linux/types.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/wait.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/bitops.h>
  50. #include <asm/pci-bridge.h>
  51. #include <net/checksum.h>
  52. #include "spider_net.h"
  53. MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
  54. "<Jens.Osterkamp@de.ibm.com>");
  55. MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
  56. MODULE_LICENSE("GPL");
  57. MODULE_VERSION(VERSION);
  58. MODULE_FIRMWARE(SPIDER_NET_FIRMWARE_NAME);
  59. static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
  60. static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
  61. module_param(rx_descriptors, int, 0444);
  62. module_param(tx_descriptors, int, 0444);
  63. MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
  64. "in rx chains");
  65. MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
  66. "in tx chain");
  67. char spider_net_driver_name[] = "spidernet";
  68. static const struct pci_device_id spider_net_pci_tbl[] = {
  69. { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
  70. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  71. { 0, }
  72. };
  73. MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
  74. /**
  75. * spider_net_read_reg - reads an SMMIO register of a card
  76. * @card: device structure
  77. * @reg: register to read from
  78. *
  79. * returns the content of the specified SMMIO register.
  80. */
  81. static inline u32
  82. spider_net_read_reg(struct spider_net_card *card, u32 reg)
  83. {
  84. /* We use the powerpc specific variants instead of readl_be() because
  85. * we know spidernet is not a real PCI device and we can thus avoid the
  86. * performance hit caused by the PCI workarounds.
  87. */
  88. return in_be32(card->regs + reg);
  89. }
  90. /**
  91. * spider_net_write_reg - writes to an SMMIO register of a card
  92. * @card: device structure
  93. * @reg: register to write to
  94. * @value: value to write into the specified SMMIO register
  95. */
  96. static inline void
  97. spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
  98. {
  99. /* We use the powerpc specific variants instead of writel_be() because
  100. * we know spidernet is not a real PCI device and we can thus avoid the
  101. * performance hit caused by the PCI workarounds.
  102. */
  103. out_be32(card->regs + reg, value);
  104. }
  105. /**
  106. * spider_net_write_phy - write to phy register
  107. * @netdev: adapter to be written to
  108. * @mii_id: id of MII
  109. * @reg: PHY register
  110. * @val: value to be written to phy register
  111. *
  112. * spider_net_write_phy_register writes to an arbitrary PHY
  113. * register via the spider GPCWOPCMD register. We assume the queue does
  114. * not run full (not more than 15 commands outstanding).
  115. **/
  116. static void
  117. spider_net_write_phy(struct net_device *netdev, int mii_id,
  118. int reg, int val)
  119. {
  120. struct spider_net_card *card = netdev_priv(netdev);
  121. u32 writevalue;
  122. writevalue = ((u32)mii_id << 21) |
  123. ((u32)reg << 16) | ((u32)val);
  124. spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
  125. }
  126. /**
  127. * spider_net_read_phy - read from phy register
  128. * @netdev: network device to be read from
  129. * @mii_id: id of MII
  130. * @reg: PHY register
  131. *
  132. * Returns value read from PHY register
  133. *
  134. * spider_net_write_phy reads from an arbitrary PHY
  135. * register via the spider GPCROPCMD register
  136. **/
  137. static int
  138. spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
  139. {
  140. struct spider_net_card *card = netdev_priv(netdev);
  141. u32 readvalue;
  142. readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
  143. spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
  144. /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
  145. * interrupt, as we poll for the completion of the read operation
  146. * in spider_net_read_phy. Should take about 50 us */
  147. do {
  148. readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
  149. } while (readvalue & SPIDER_NET_GPREXEC);
  150. readvalue &= SPIDER_NET_GPRDAT_MASK;
  151. return readvalue;
  152. }
  153. /**
  154. * spider_net_setup_aneg - initial auto-negotiation setup
  155. * @card: device structure
  156. **/
  157. static void
  158. spider_net_setup_aneg(struct spider_net_card *card)
  159. {
  160. struct mii_phy *phy = &card->phy;
  161. u32 advertise = 0;
  162. u16 bmsr, estat;
  163. bmsr = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
  164. estat = spider_net_read_phy(card->netdev, phy->mii_id, MII_ESTATUS);
  165. if (bmsr & BMSR_10HALF)
  166. advertise |= ADVERTISED_10baseT_Half;
  167. if (bmsr & BMSR_10FULL)
  168. advertise |= ADVERTISED_10baseT_Full;
  169. if (bmsr & BMSR_100HALF)
  170. advertise |= ADVERTISED_100baseT_Half;
  171. if (bmsr & BMSR_100FULL)
  172. advertise |= ADVERTISED_100baseT_Full;
  173. if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_TFULL))
  174. advertise |= SUPPORTED_1000baseT_Full;
  175. if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_THALF))
  176. advertise |= SUPPORTED_1000baseT_Half;
  177. sungem_phy_probe(phy, phy->mii_id);
  178. phy->def->ops->setup_aneg(phy, advertise);
  179. }
  180. /**
  181. * spider_net_rx_irq_off - switch off rx irq on this spider card
  182. * @card: device structure
  183. *
  184. * switches off rx irq by masking them out in the GHIINTnMSK register
  185. */
  186. static void
  187. spider_net_rx_irq_off(struct spider_net_card *card)
  188. {
  189. u32 regvalue;
  190. regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
  191. spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
  192. }
  193. /**
  194. * spider_net_rx_irq_on - switch on rx irq on this spider card
  195. * @card: device structure
  196. *
  197. * switches on rx irq by enabling them in the GHIINTnMSK register
  198. */
  199. static void
  200. spider_net_rx_irq_on(struct spider_net_card *card)
  201. {
  202. u32 regvalue;
  203. regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
  204. spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
  205. }
  206. /**
  207. * spider_net_set_promisc - sets the unicast address or the promiscuous mode
  208. * @card: card structure
  209. *
  210. * spider_net_set_promisc sets the unicast destination address filter and
  211. * thus either allows for non-promisc mode or promisc mode
  212. */
  213. static void
  214. spider_net_set_promisc(struct spider_net_card *card)
  215. {
  216. u32 macu, macl;
  217. struct net_device *netdev = card->netdev;
  218. if (netdev->flags & IFF_PROMISC) {
  219. /* clear destination entry 0 */
  220. spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
  221. spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
  222. spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
  223. SPIDER_NET_PROMISC_VALUE);
  224. } else {
  225. macu = netdev->dev_addr[0];
  226. macu <<= 8;
  227. macu |= netdev->dev_addr[1];
  228. memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
  229. macu |= SPIDER_NET_UA_DESCR_VALUE;
  230. spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
  231. spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
  232. spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
  233. SPIDER_NET_NONPROMISC_VALUE);
  234. }
  235. }
  236. /**
  237. * spider_net_get_descr_status -- returns the status of a descriptor
  238. * @descr: descriptor to look at
  239. *
  240. * returns the status as in the dmac_cmd_status field of the descriptor
  241. */
  242. static inline int
  243. spider_net_get_descr_status(struct spider_net_hw_descr *hwdescr)
  244. {
  245. return hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
  246. }
  247. /**
  248. * spider_net_free_chain - free descriptor chain
  249. * @card: card structure
  250. * @chain: address of chain
  251. *
  252. */
  253. static void
  254. spider_net_free_chain(struct spider_net_card *card,
  255. struct spider_net_descr_chain *chain)
  256. {
  257. struct spider_net_descr *descr;
  258. descr = chain->ring;
  259. do {
  260. descr->bus_addr = 0;
  261. descr->hwdescr->next_descr_addr = 0;
  262. descr = descr->next;
  263. } while (descr != chain->ring);
  264. dma_free_coherent(&card->pdev->dev, chain->num_desc,
  265. chain->hwring, chain->dma_addr);
  266. }
  267. /**
  268. * spider_net_init_chain - alloc and link descriptor chain
  269. * @card: card structure
  270. * @chain: address of chain
  271. *
  272. * We manage a circular list that mirrors the hardware structure,
  273. * except that the hardware uses bus addresses.
  274. *
  275. * Returns 0 on success, <0 on failure
  276. */
  277. static int
  278. spider_net_init_chain(struct spider_net_card *card,
  279. struct spider_net_descr_chain *chain)
  280. {
  281. int i;
  282. struct spider_net_descr *descr;
  283. struct spider_net_hw_descr *hwdescr;
  284. dma_addr_t buf;
  285. size_t alloc_size;
  286. alloc_size = chain->num_desc * sizeof(struct spider_net_hw_descr);
  287. chain->hwring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
  288. &chain->dma_addr, GFP_KERNEL);
  289. if (!chain->hwring)
  290. return -ENOMEM;
  291. memset(chain->ring, 0, chain->num_desc * sizeof(struct spider_net_descr));
  292. /* Set up the hardware pointers in each descriptor */
  293. descr = chain->ring;
  294. hwdescr = chain->hwring;
  295. buf = chain->dma_addr;
  296. for (i=0; i < chain->num_desc; i++, descr++, hwdescr++) {
  297. hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
  298. hwdescr->next_descr_addr = 0;
  299. descr->hwdescr = hwdescr;
  300. descr->bus_addr = buf;
  301. descr->next = descr + 1;
  302. descr->prev = descr - 1;
  303. buf += sizeof(struct spider_net_hw_descr);
  304. }
  305. /* do actual circular list */
  306. (descr-1)->next = chain->ring;
  307. chain->ring->prev = descr-1;
  308. spin_lock_init(&chain->lock);
  309. chain->head = chain->ring;
  310. chain->tail = chain->ring;
  311. return 0;
  312. }
  313. /**
  314. * spider_net_free_rx_chain_contents - frees descr contents in rx chain
  315. * @card: card structure
  316. *
  317. * returns 0 on success, <0 on failure
  318. */
  319. static void
  320. spider_net_free_rx_chain_contents(struct spider_net_card *card)
  321. {
  322. struct spider_net_descr *descr;
  323. descr = card->rx_chain.head;
  324. do {
  325. if (descr->skb) {
  326. pci_unmap_single(card->pdev, descr->hwdescr->buf_addr,
  327. SPIDER_NET_MAX_FRAME,
  328. PCI_DMA_BIDIRECTIONAL);
  329. dev_kfree_skb(descr->skb);
  330. descr->skb = NULL;
  331. }
  332. descr = descr->next;
  333. } while (descr != card->rx_chain.head);
  334. }
  335. /**
  336. * spider_net_prepare_rx_descr - Reinitialize RX descriptor
  337. * @card: card structure
  338. * @descr: descriptor to re-init
  339. *
  340. * Return 0 on success, <0 on failure.
  341. *
  342. * Allocates a new rx skb, iommu-maps it and attaches it to the
  343. * descriptor. Mark the descriptor as activated, ready-to-use.
  344. */
  345. static int
  346. spider_net_prepare_rx_descr(struct spider_net_card *card,
  347. struct spider_net_descr *descr)
  348. {
  349. struct spider_net_hw_descr *hwdescr = descr->hwdescr;
  350. dma_addr_t buf;
  351. int offset;
  352. int bufsize;
  353. /* we need to round up the buffer size to a multiple of 128 */
  354. bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
  355. (~(SPIDER_NET_RXBUF_ALIGN - 1));
  356. /* and we need to have it 128 byte aligned, therefore we allocate a
  357. * bit more */
  358. /* allocate an skb */
  359. descr->skb = netdev_alloc_skb(card->netdev,
  360. bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
  361. if (!descr->skb) {
  362. if (netif_msg_rx_err(card) && net_ratelimit())
  363. dev_err(&card->netdev->dev,
  364. "Not enough memory to allocate rx buffer\n");
  365. card->spider_stats.alloc_rx_skb_error++;
  366. return -ENOMEM;
  367. }
  368. hwdescr->buf_size = bufsize;
  369. hwdescr->result_size = 0;
  370. hwdescr->valid_size = 0;
  371. hwdescr->data_status = 0;
  372. hwdescr->data_error = 0;
  373. offset = ((unsigned long)descr->skb->data) &
  374. (SPIDER_NET_RXBUF_ALIGN - 1);
  375. if (offset)
  376. skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
  377. /* iommu-map the skb */
  378. buf = pci_map_single(card->pdev, descr->skb->data,
  379. SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
  380. if (pci_dma_mapping_error(card->pdev, buf)) {
  381. dev_kfree_skb_any(descr->skb);
  382. descr->skb = NULL;
  383. if (netif_msg_rx_err(card) && net_ratelimit())
  384. dev_err(&card->netdev->dev, "Could not iommu-map rx buffer\n");
  385. card->spider_stats.rx_iommu_map_error++;
  386. hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
  387. } else {
  388. hwdescr->buf_addr = buf;
  389. wmb();
  390. hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
  391. SPIDER_NET_DMAC_NOINTR_COMPLETE;
  392. }
  393. return 0;
  394. }
  395. /**
  396. * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
  397. * @card: card structure
  398. *
  399. * spider_net_enable_rxchtails sets the RX DMAC chain tail addresses in the
  400. * chip by writing to the appropriate register. DMA is enabled in
  401. * spider_net_enable_rxdmac.
  402. */
  403. static inline void
  404. spider_net_enable_rxchtails(struct spider_net_card *card)
  405. {
  406. /* assume chain is aligned correctly */
  407. spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
  408. card->rx_chain.tail->bus_addr);
  409. }
  410. /**
  411. * spider_net_enable_rxdmac - enables a receive DMA controller
  412. * @card: card structure
  413. *
  414. * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
  415. * in the GDADMACCNTR register
  416. */
  417. static inline void
  418. spider_net_enable_rxdmac(struct spider_net_card *card)
  419. {
  420. wmb();
  421. spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
  422. SPIDER_NET_DMA_RX_VALUE);
  423. }
  424. /**
  425. * spider_net_disable_rxdmac - disables the receive DMA controller
  426. * @card: card structure
  427. *
  428. * spider_net_disable_rxdmac terminates processing on the DMA controller
  429. * by turing off the DMA controller, with the force-end flag set.
  430. */
  431. static inline void
  432. spider_net_disable_rxdmac(struct spider_net_card *card)
  433. {
  434. spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
  435. SPIDER_NET_DMA_RX_FEND_VALUE);
  436. }
  437. /**
  438. * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
  439. * @card: card structure
  440. *
  441. * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
  442. */
  443. static void
  444. spider_net_refill_rx_chain(struct spider_net_card *card)
  445. {
  446. struct spider_net_descr_chain *chain = &card->rx_chain;
  447. unsigned long flags;
  448. /* one context doing the refill (and a second context seeing that
  449. * and omitting it) is ok. If called by NAPI, we'll be called again
  450. * as spider_net_decode_one_descr is called several times. If some
  451. * interrupt calls us, the NAPI is about to clean up anyway. */
  452. if (!spin_trylock_irqsave(&chain->lock, flags))
  453. return;
  454. while (spider_net_get_descr_status(chain->head->hwdescr) ==
  455. SPIDER_NET_DESCR_NOT_IN_USE) {
  456. if (spider_net_prepare_rx_descr(card, chain->head))
  457. break;
  458. chain->head = chain->head->next;
  459. }
  460. spin_unlock_irqrestore(&chain->lock, flags);
  461. }
  462. /**
  463. * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains
  464. * @card: card structure
  465. *
  466. * Returns 0 on success, <0 on failure.
  467. */
  468. static int
  469. spider_net_alloc_rx_skbs(struct spider_net_card *card)
  470. {
  471. struct spider_net_descr_chain *chain = &card->rx_chain;
  472. struct spider_net_descr *start = chain->tail;
  473. struct spider_net_descr *descr = start;
  474. /* Link up the hardware chain pointers */
  475. do {
  476. descr->prev->hwdescr->next_descr_addr = descr->bus_addr;
  477. descr = descr->next;
  478. } while (descr != start);
  479. /* Put at least one buffer into the chain. if this fails,
  480. * we've got a problem. If not, spider_net_refill_rx_chain
  481. * will do the rest at the end of this function. */
  482. if (spider_net_prepare_rx_descr(card, chain->head))
  483. goto error;
  484. else
  485. chain->head = chain->head->next;
  486. /* This will allocate the rest of the rx buffers;
  487. * if not, it's business as usual later on. */
  488. spider_net_refill_rx_chain(card);
  489. spider_net_enable_rxdmac(card);
  490. return 0;
  491. error:
  492. spider_net_free_rx_chain_contents(card);
  493. return -ENOMEM;
  494. }
  495. /**
  496. * spider_net_get_multicast_hash - generates hash for multicast filter table
  497. * @addr: multicast address
  498. *
  499. * returns the hash value.
  500. *
  501. * spider_net_get_multicast_hash calculates a hash value for a given multicast
  502. * address, that is used to set the multicast filter tables
  503. */
  504. static u8
  505. spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
  506. {
  507. u32 crc;
  508. u8 hash;
  509. char addr_for_crc[ETH_ALEN] = { 0, };
  510. int i, bit;
  511. for (i = 0; i < ETH_ALEN * 8; i++) {
  512. bit = (addr[i / 8] >> (i % 8)) & 1;
  513. addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
  514. }
  515. crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
  516. hash = (crc >> 27);
  517. hash <<= 3;
  518. hash |= crc & 7;
  519. hash &= 0xff;
  520. return hash;
  521. }
  522. /**
  523. * spider_net_set_multi - sets multicast addresses and promisc flags
  524. * @netdev: interface device structure
  525. *
  526. * spider_net_set_multi configures multicast addresses as needed for the
  527. * netdev interface. It also sets up multicast, allmulti and promisc
  528. * flags appropriately
  529. */
  530. static void
  531. spider_net_set_multi(struct net_device *netdev)
  532. {
  533. struct netdev_hw_addr *ha;
  534. u8 hash;
  535. int i;
  536. u32 reg;
  537. struct spider_net_card *card = netdev_priv(netdev);
  538. DECLARE_BITMAP(bitmask, SPIDER_NET_MULTICAST_HASHES) = {};
  539. spider_net_set_promisc(card);
  540. if (netdev->flags & IFF_ALLMULTI) {
  541. for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
  542. set_bit(i, bitmask);
  543. }
  544. goto write_hash;
  545. }
  546. /* well, we know, what the broadcast hash value is: it's xfd
  547. hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
  548. set_bit(0xfd, bitmask);
  549. netdev_for_each_mc_addr(ha, netdev) {
  550. hash = spider_net_get_multicast_hash(netdev, ha->addr);
  551. set_bit(hash, bitmask);
  552. }
  553. write_hash:
  554. for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
  555. reg = 0;
  556. if (test_bit(i * 4, bitmask))
  557. reg += 0x08;
  558. reg <<= 8;
  559. if (test_bit(i * 4 + 1, bitmask))
  560. reg += 0x08;
  561. reg <<= 8;
  562. if (test_bit(i * 4 + 2, bitmask))
  563. reg += 0x08;
  564. reg <<= 8;
  565. if (test_bit(i * 4 + 3, bitmask))
  566. reg += 0x08;
  567. spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
  568. }
  569. }
  570. /**
  571. * spider_net_prepare_tx_descr - fill tx descriptor with skb data
  572. * @card: card structure
  573. * @skb: packet to use
  574. *
  575. * returns 0 on success, <0 on failure.
  576. *
  577. * fills out the descriptor structure with skb data and len. Copies data,
  578. * if needed (32bit DMA!)
  579. */
  580. static int
  581. spider_net_prepare_tx_descr(struct spider_net_card *card,
  582. struct sk_buff *skb)
  583. {
  584. struct spider_net_descr_chain *chain = &card->tx_chain;
  585. struct spider_net_descr *descr;
  586. struct spider_net_hw_descr *hwdescr;
  587. dma_addr_t buf;
  588. unsigned long flags;
  589. buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
  590. if (pci_dma_mapping_error(card->pdev, buf)) {
  591. if (netif_msg_tx_err(card) && net_ratelimit())
  592. dev_err(&card->netdev->dev, "could not iommu-map packet (%p, %i). "
  593. "Dropping packet\n", skb->data, skb->len);
  594. card->spider_stats.tx_iommu_map_error++;
  595. return -ENOMEM;
  596. }
  597. spin_lock_irqsave(&chain->lock, flags);
  598. descr = card->tx_chain.head;
  599. if (descr->next == chain->tail->prev) {
  600. spin_unlock_irqrestore(&chain->lock, flags);
  601. pci_unmap_single(card->pdev, buf, skb->len, PCI_DMA_TODEVICE);
  602. return -ENOMEM;
  603. }
  604. hwdescr = descr->hwdescr;
  605. chain->head = descr->next;
  606. descr->skb = skb;
  607. hwdescr->buf_addr = buf;
  608. hwdescr->buf_size = skb->len;
  609. hwdescr->next_descr_addr = 0;
  610. hwdescr->data_status = 0;
  611. hwdescr->dmac_cmd_status =
  612. SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_TXFRMTL;
  613. spin_unlock_irqrestore(&chain->lock, flags);
  614. if (skb->ip_summed == CHECKSUM_PARTIAL)
  615. switch (ip_hdr(skb)->protocol) {
  616. case IPPROTO_TCP:
  617. hwdescr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
  618. break;
  619. case IPPROTO_UDP:
  620. hwdescr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
  621. break;
  622. }
  623. /* Chain the bus address, so that the DMA engine finds this descr. */
  624. wmb();
  625. descr->prev->hwdescr->next_descr_addr = descr->bus_addr;
  626. card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
  627. return 0;
  628. }
  629. static int
  630. spider_net_set_low_watermark(struct spider_net_card *card)
  631. {
  632. struct spider_net_descr *descr = card->tx_chain.tail;
  633. struct spider_net_hw_descr *hwdescr;
  634. unsigned long flags;
  635. int status;
  636. int cnt=0;
  637. int i;
  638. /* Measure the length of the queue. Measurement does not
  639. * need to be precise -- does not need a lock. */
  640. while (descr != card->tx_chain.head) {
  641. status = descr->hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
  642. if (status == SPIDER_NET_DESCR_NOT_IN_USE)
  643. break;
  644. descr = descr->next;
  645. cnt++;
  646. }
  647. /* If TX queue is short, don't even bother with interrupts */
  648. if (cnt < card->tx_chain.num_desc/4)
  649. return cnt;
  650. /* Set low-watermark 3/4th's of the way into the queue. */
  651. descr = card->tx_chain.tail;
  652. cnt = (cnt*3)/4;
  653. for (i=0;i<cnt; i++)
  654. descr = descr->next;
  655. /* Set the new watermark, clear the old watermark */
  656. spin_lock_irqsave(&card->tx_chain.lock, flags);
  657. descr->hwdescr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
  658. if (card->low_watermark && card->low_watermark != descr) {
  659. hwdescr = card->low_watermark->hwdescr;
  660. hwdescr->dmac_cmd_status =
  661. hwdescr->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
  662. }
  663. card->low_watermark = descr;
  664. spin_unlock_irqrestore(&card->tx_chain.lock, flags);
  665. return cnt;
  666. }
  667. /**
  668. * spider_net_release_tx_chain - processes sent tx descriptors
  669. * @card: adapter structure
  670. * @brutal: if set, don't care about whether descriptor seems to be in use
  671. *
  672. * returns 0 if the tx ring is empty, otherwise 1.
  673. *
  674. * spider_net_release_tx_chain releases the tx descriptors that spider has
  675. * finished with (if non-brutal) or simply release tx descriptors (if brutal).
  676. * If some other context is calling this function, we return 1 so that we're
  677. * scheduled again (if we were scheduled) and will not lose initiative.
  678. */
  679. static int
  680. spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
  681. {
  682. struct net_device *dev = card->netdev;
  683. struct spider_net_descr_chain *chain = &card->tx_chain;
  684. struct spider_net_descr *descr;
  685. struct spider_net_hw_descr *hwdescr;
  686. struct sk_buff *skb;
  687. u32 buf_addr;
  688. unsigned long flags;
  689. int status;
  690. while (1) {
  691. spin_lock_irqsave(&chain->lock, flags);
  692. if (chain->tail == chain->head) {
  693. spin_unlock_irqrestore(&chain->lock, flags);
  694. return 0;
  695. }
  696. descr = chain->tail;
  697. hwdescr = descr->hwdescr;
  698. status = spider_net_get_descr_status(hwdescr);
  699. switch (status) {
  700. case SPIDER_NET_DESCR_COMPLETE:
  701. dev->stats.tx_packets++;
  702. dev->stats.tx_bytes += descr->skb->len;
  703. break;
  704. case SPIDER_NET_DESCR_CARDOWNED:
  705. if (!brutal) {
  706. spin_unlock_irqrestore(&chain->lock, flags);
  707. return 1;
  708. }
  709. /* fallthrough, if we release the descriptors
  710. * brutally (then we don't care about
  711. * SPIDER_NET_DESCR_CARDOWNED) */
  712. case SPIDER_NET_DESCR_RESPONSE_ERROR:
  713. case SPIDER_NET_DESCR_PROTECTION_ERROR:
  714. case SPIDER_NET_DESCR_FORCE_END:
  715. if (netif_msg_tx_err(card))
  716. dev_err(&card->netdev->dev, "forcing end of tx descriptor "
  717. "with status x%02x\n", status);
  718. dev->stats.tx_errors++;
  719. break;
  720. default:
  721. dev->stats.tx_dropped++;
  722. if (!brutal) {
  723. spin_unlock_irqrestore(&chain->lock, flags);
  724. return 1;
  725. }
  726. }
  727. chain->tail = descr->next;
  728. hwdescr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
  729. skb = descr->skb;
  730. descr->skb = NULL;
  731. buf_addr = hwdescr->buf_addr;
  732. spin_unlock_irqrestore(&chain->lock, flags);
  733. /* unmap the skb */
  734. if (skb) {
  735. pci_unmap_single(card->pdev, buf_addr, skb->len,
  736. PCI_DMA_TODEVICE);
  737. dev_consume_skb_any(skb);
  738. }
  739. }
  740. return 0;
  741. }
  742. /**
  743. * spider_net_kick_tx_dma - enables TX DMA processing
  744. * @card: card structure
  745. *
  746. * This routine will start the transmit DMA running if
  747. * it is not already running. This routine ned only be
  748. * called when queueing a new packet to an empty tx queue.
  749. * Writes the current tx chain head as start address
  750. * of the tx descriptor chain and enables the transmission
  751. * DMA engine.
  752. */
  753. static inline void
  754. spider_net_kick_tx_dma(struct spider_net_card *card)
  755. {
  756. struct spider_net_descr *descr;
  757. if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
  758. SPIDER_NET_TX_DMA_EN)
  759. goto out;
  760. descr = card->tx_chain.tail;
  761. for (;;) {
  762. if (spider_net_get_descr_status(descr->hwdescr) ==
  763. SPIDER_NET_DESCR_CARDOWNED) {
  764. spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
  765. descr->bus_addr);
  766. spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
  767. SPIDER_NET_DMA_TX_VALUE);
  768. break;
  769. }
  770. if (descr == card->tx_chain.head)
  771. break;
  772. descr = descr->next;
  773. }
  774. out:
  775. mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
  776. }
  777. /**
  778. * spider_net_xmit - transmits a frame over the device
  779. * @skb: packet to send out
  780. * @netdev: interface device structure
  781. *
  782. * returns 0 on success, !0 on failure
  783. */
  784. static int
  785. spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
  786. {
  787. int cnt;
  788. struct spider_net_card *card = netdev_priv(netdev);
  789. spider_net_release_tx_chain(card, 0);
  790. if (spider_net_prepare_tx_descr(card, skb) != 0) {
  791. netdev->stats.tx_dropped++;
  792. netif_stop_queue(netdev);
  793. return NETDEV_TX_BUSY;
  794. }
  795. cnt = spider_net_set_low_watermark(card);
  796. if (cnt < 5)
  797. spider_net_kick_tx_dma(card);
  798. return NETDEV_TX_OK;
  799. }
  800. /**
  801. * spider_net_cleanup_tx_ring - cleans up the TX ring
  802. * @card: card structure
  803. *
  804. * spider_net_cleanup_tx_ring is called by either the tx_timer
  805. * or from the NAPI polling routine.
  806. * This routine releases resources associted with transmitted
  807. * packets, including updating the queue tail pointer.
  808. */
  809. static void
  810. spider_net_cleanup_tx_ring(struct spider_net_card *card)
  811. {
  812. if ((spider_net_release_tx_chain(card, 0) != 0) &&
  813. (card->netdev->flags & IFF_UP)) {
  814. spider_net_kick_tx_dma(card);
  815. netif_wake_queue(card->netdev);
  816. }
  817. }
  818. /**
  819. * spider_net_do_ioctl - called for device ioctls
  820. * @netdev: interface device structure
  821. * @ifr: request parameter structure for ioctl
  822. * @cmd: command code for ioctl
  823. *
  824. * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
  825. * -EOPNOTSUPP is returned, if an unknown ioctl was requested
  826. */
  827. static int
  828. spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  829. {
  830. switch (cmd) {
  831. default:
  832. return -EOPNOTSUPP;
  833. }
  834. }
  835. /**
  836. * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
  837. * @descr: descriptor to process
  838. * @card: card structure
  839. *
  840. * Fills out skb structure and passes the data to the stack.
  841. * The descriptor state is not changed.
  842. */
  843. static void
  844. spider_net_pass_skb_up(struct spider_net_descr *descr,
  845. struct spider_net_card *card)
  846. {
  847. struct spider_net_hw_descr *hwdescr = descr->hwdescr;
  848. struct sk_buff *skb = descr->skb;
  849. struct net_device *netdev = card->netdev;
  850. u32 data_status = hwdescr->data_status;
  851. u32 data_error = hwdescr->data_error;
  852. skb_put(skb, hwdescr->valid_size);
  853. /* the card seems to add 2 bytes of junk in front
  854. * of the ethernet frame */
  855. #define SPIDER_MISALIGN 2
  856. skb_pull(skb, SPIDER_MISALIGN);
  857. skb->protocol = eth_type_trans(skb, netdev);
  858. /* checksum offload */
  859. skb_checksum_none_assert(skb);
  860. if (netdev->features & NETIF_F_RXCSUM) {
  861. if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
  862. SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
  863. !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
  864. skb->ip_summed = CHECKSUM_UNNECESSARY;
  865. }
  866. if (data_status & SPIDER_NET_VLAN_PACKET) {
  867. /* further enhancements: HW-accel VLAN */
  868. }
  869. /* update netdevice statistics */
  870. netdev->stats.rx_packets++;
  871. netdev->stats.rx_bytes += skb->len;
  872. /* pass skb up to stack */
  873. netif_receive_skb(skb);
  874. }
  875. static void show_rx_chain(struct spider_net_card *card)
  876. {
  877. struct spider_net_descr_chain *chain = &card->rx_chain;
  878. struct spider_net_descr *start= chain->tail;
  879. struct spider_net_descr *descr= start;
  880. struct spider_net_hw_descr *hwd = start->hwdescr;
  881. struct device *dev = &card->netdev->dev;
  882. u32 curr_desc, next_desc;
  883. int status;
  884. int tot = 0;
  885. int cnt = 0;
  886. int off = start - chain->ring;
  887. int cstat = hwd->dmac_cmd_status;
  888. dev_info(dev, "Total number of descrs=%d\n",
  889. chain->num_desc);
  890. dev_info(dev, "Chain tail located at descr=%d, status=0x%x\n",
  891. off, cstat);
  892. curr_desc = spider_net_read_reg(card, SPIDER_NET_GDACTDPA);
  893. next_desc = spider_net_read_reg(card, SPIDER_NET_GDACNEXTDA);
  894. status = cstat;
  895. do
  896. {
  897. hwd = descr->hwdescr;
  898. off = descr - chain->ring;
  899. status = hwd->dmac_cmd_status;
  900. if (descr == chain->head)
  901. dev_info(dev, "Chain head is at %d, head status=0x%x\n",
  902. off, status);
  903. if (curr_desc == descr->bus_addr)
  904. dev_info(dev, "HW curr desc (GDACTDPA) is at %d, status=0x%x\n",
  905. off, status);
  906. if (next_desc == descr->bus_addr)
  907. dev_info(dev, "HW next desc (GDACNEXTDA) is at %d, status=0x%x\n",
  908. off, status);
  909. if (hwd->next_descr_addr == 0)
  910. dev_info(dev, "chain is cut at %d\n", off);
  911. if (cstat != status) {
  912. int from = (chain->num_desc + off - cnt) % chain->num_desc;
  913. int to = (chain->num_desc + off - 1) % chain->num_desc;
  914. dev_info(dev, "Have %d (from %d to %d) descrs "
  915. "with stat=0x%08x\n", cnt, from, to, cstat);
  916. cstat = status;
  917. cnt = 0;
  918. }
  919. cnt ++;
  920. tot ++;
  921. descr = descr->next;
  922. } while (descr != start);
  923. dev_info(dev, "Last %d descrs with stat=0x%08x "
  924. "for a total of %d descrs\n", cnt, cstat, tot);
  925. #ifdef DEBUG
  926. /* Now dump the whole ring */
  927. descr = start;
  928. do
  929. {
  930. struct spider_net_hw_descr *hwd = descr->hwdescr;
  931. status = spider_net_get_descr_status(hwd);
  932. cnt = descr - chain->ring;
  933. dev_info(dev, "Descr %d stat=0x%08x skb=%p\n",
  934. cnt, status, descr->skb);
  935. dev_info(dev, "bus addr=%08x buf addr=%08x sz=%d\n",
  936. descr->bus_addr, hwd->buf_addr, hwd->buf_size);
  937. dev_info(dev, "next=%08x result sz=%d valid sz=%d\n",
  938. hwd->next_descr_addr, hwd->result_size,
  939. hwd->valid_size);
  940. dev_info(dev, "dmac=%08x data stat=%08x data err=%08x\n",
  941. hwd->dmac_cmd_status, hwd->data_status,
  942. hwd->data_error);
  943. dev_info(dev, "\n");
  944. descr = descr->next;
  945. } while (descr != start);
  946. #endif
  947. }
  948. /**
  949. * spider_net_resync_head_ptr - Advance head ptr past empty descrs
  950. *
  951. * If the driver fails to keep up and empty the queue, then the
  952. * hardware wil run out of room to put incoming packets. This
  953. * will cause the hardware to skip descrs that are full (instead
  954. * of halting/retrying). Thus, once the driver runs, it wil need
  955. * to "catch up" to where the hardware chain pointer is at.
  956. */
  957. static void spider_net_resync_head_ptr(struct spider_net_card *card)
  958. {
  959. unsigned long flags;
  960. struct spider_net_descr_chain *chain = &card->rx_chain;
  961. struct spider_net_descr *descr;
  962. int i, status;
  963. /* Advance head pointer past any empty descrs */
  964. descr = chain->head;
  965. status = spider_net_get_descr_status(descr->hwdescr);
  966. if (status == SPIDER_NET_DESCR_NOT_IN_USE)
  967. return;
  968. spin_lock_irqsave(&chain->lock, flags);
  969. descr = chain->head;
  970. status = spider_net_get_descr_status(descr->hwdescr);
  971. for (i=0; i<chain->num_desc; i++) {
  972. if (status != SPIDER_NET_DESCR_CARDOWNED) break;
  973. descr = descr->next;
  974. status = spider_net_get_descr_status(descr->hwdescr);
  975. }
  976. chain->head = descr;
  977. spin_unlock_irqrestore(&chain->lock, flags);
  978. }
  979. static int spider_net_resync_tail_ptr(struct spider_net_card *card)
  980. {
  981. struct spider_net_descr_chain *chain = &card->rx_chain;
  982. struct spider_net_descr *descr;
  983. int i, status;
  984. /* Advance tail pointer past any empty and reaped descrs */
  985. descr = chain->tail;
  986. status = spider_net_get_descr_status(descr->hwdescr);
  987. for (i=0; i<chain->num_desc; i++) {
  988. if ((status != SPIDER_NET_DESCR_CARDOWNED) &&
  989. (status != SPIDER_NET_DESCR_NOT_IN_USE)) break;
  990. descr = descr->next;
  991. status = spider_net_get_descr_status(descr->hwdescr);
  992. }
  993. chain->tail = descr;
  994. if ((i == chain->num_desc) || (i == 0))
  995. return 1;
  996. return 0;
  997. }
  998. /**
  999. * spider_net_decode_one_descr - processes an RX descriptor
  1000. * @card: card structure
  1001. *
  1002. * Returns 1 if a packet has been sent to the stack, otherwise 0.
  1003. *
  1004. * Processes an RX descriptor by iommu-unmapping the data buffer
  1005. * and passing the packet up to the stack. This function is called
  1006. * in softirq context, e.g. either bottom half from interrupt or
  1007. * NAPI polling context.
  1008. */
  1009. static int
  1010. spider_net_decode_one_descr(struct spider_net_card *card)
  1011. {
  1012. struct net_device *dev = card->netdev;
  1013. struct spider_net_descr_chain *chain = &card->rx_chain;
  1014. struct spider_net_descr *descr = chain->tail;
  1015. struct spider_net_hw_descr *hwdescr = descr->hwdescr;
  1016. u32 hw_buf_addr;
  1017. int status;
  1018. status = spider_net_get_descr_status(hwdescr);
  1019. /* Nothing in the descriptor, or ring must be empty */
  1020. if ((status == SPIDER_NET_DESCR_CARDOWNED) ||
  1021. (status == SPIDER_NET_DESCR_NOT_IN_USE))
  1022. return 0;
  1023. /* descriptor definitively used -- move on tail */
  1024. chain->tail = descr->next;
  1025. /* unmap descriptor */
  1026. hw_buf_addr = hwdescr->buf_addr;
  1027. hwdescr->buf_addr = 0xffffffff;
  1028. pci_unmap_single(card->pdev, hw_buf_addr,
  1029. SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
  1030. if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
  1031. (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
  1032. (status == SPIDER_NET_DESCR_FORCE_END) ) {
  1033. if (netif_msg_rx_err(card))
  1034. dev_err(&dev->dev,
  1035. "dropping RX descriptor with state %d\n", status);
  1036. dev->stats.rx_dropped++;
  1037. goto bad_desc;
  1038. }
  1039. if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
  1040. (status != SPIDER_NET_DESCR_FRAME_END) ) {
  1041. if (netif_msg_rx_err(card))
  1042. dev_err(&card->netdev->dev,
  1043. "RX descriptor with unknown state %d\n", status);
  1044. card->spider_stats.rx_desc_unk_state++;
  1045. goto bad_desc;
  1046. }
  1047. /* The cases we'll throw away the packet immediately */
  1048. if (hwdescr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
  1049. if (netif_msg_rx_err(card))
  1050. dev_err(&card->netdev->dev,
  1051. "error in received descriptor found, "
  1052. "data_status=x%08x, data_error=x%08x\n",
  1053. hwdescr->data_status, hwdescr->data_error);
  1054. goto bad_desc;
  1055. }
  1056. if (hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_BAD_STATUS) {
  1057. dev_err(&card->netdev->dev, "bad status, cmd_status=x%08x\n",
  1058. hwdescr->dmac_cmd_status);
  1059. pr_err("buf_addr=x%08x\n", hw_buf_addr);
  1060. pr_err("buf_size=x%08x\n", hwdescr->buf_size);
  1061. pr_err("next_descr_addr=x%08x\n", hwdescr->next_descr_addr);
  1062. pr_err("result_size=x%08x\n", hwdescr->result_size);
  1063. pr_err("valid_size=x%08x\n", hwdescr->valid_size);
  1064. pr_err("data_status=x%08x\n", hwdescr->data_status);
  1065. pr_err("data_error=x%08x\n", hwdescr->data_error);
  1066. pr_err("which=%ld\n", descr - card->rx_chain.ring);
  1067. card->spider_stats.rx_desc_error++;
  1068. goto bad_desc;
  1069. }
  1070. /* Ok, we've got a packet in descr */
  1071. spider_net_pass_skb_up(descr, card);
  1072. descr->skb = NULL;
  1073. hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
  1074. return 1;
  1075. bad_desc:
  1076. if (netif_msg_rx_err(card))
  1077. show_rx_chain(card);
  1078. dev_kfree_skb_irq(descr->skb);
  1079. descr->skb = NULL;
  1080. hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
  1081. return 0;
  1082. }
  1083. /**
  1084. * spider_net_poll - NAPI poll function called by the stack to return packets
  1085. * @netdev: interface device structure
  1086. * @budget: number of packets we can pass to the stack at most
  1087. *
  1088. * returns 0 if no more packets available to the driver/stack. Returns 1,
  1089. * if the quota is exceeded, but the driver has still packets.
  1090. *
  1091. * spider_net_poll returns all packets from the rx descriptors to the stack
  1092. * (using netif_receive_skb). If all/enough packets are up, the driver
  1093. * reenables interrupts and returns 0. If not, 1 is returned.
  1094. */
  1095. static int spider_net_poll(struct napi_struct *napi, int budget)
  1096. {
  1097. struct spider_net_card *card = container_of(napi, struct spider_net_card, napi);
  1098. int packets_done = 0;
  1099. while (packets_done < budget) {
  1100. if (!spider_net_decode_one_descr(card))
  1101. break;
  1102. packets_done++;
  1103. }
  1104. if ((packets_done == 0) && (card->num_rx_ints != 0)) {
  1105. if (!spider_net_resync_tail_ptr(card))
  1106. packets_done = budget;
  1107. spider_net_resync_head_ptr(card);
  1108. }
  1109. card->num_rx_ints = 0;
  1110. spider_net_refill_rx_chain(card);
  1111. spider_net_enable_rxdmac(card);
  1112. spider_net_cleanup_tx_ring(card);
  1113. /* if all packets are in the stack, enable interrupts and return 0 */
  1114. /* if not, return 1 */
  1115. if (packets_done < budget) {
  1116. napi_complete(napi);
  1117. spider_net_rx_irq_on(card);
  1118. card->ignore_rx_ramfull = 0;
  1119. }
  1120. return packets_done;
  1121. }
  1122. /**
  1123. * spider_net_change_mtu - changes the MTU of an interface
  1124. * @netdev: interface device structure
  1125. * @new_mtu: new MTU value
  1126. *
  1127. * returns 0 on success, <0 on failure
  1128. */
  1129. static int
  1130. spider_net_change_mtu(struct net_device *netdev, int new_mtu)
  1131. {
  1132. /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
  1133. * and mtu is outbound only anyway */
  1134. if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
  1135. (new_mtu > SPIDER_NET_MAX_MTU) )
  1136. return -EINVAL;
  1137. netdev->mtu = new_mtu;
  1138. return 0;
  1139. }
  1140. /**
  1141. * spider_net_set_mac - sets the MAC of an interface
  1142. * @netdev: interface device structure
  1143. * @ptr: pointer to new MAC address
  1144. *
  1145. * Returns 0 on success, <0 on failure. Currently, we don't support this
  1146. * and will always return EOPNOTSUPP.
  1147. */
  1148. static int
  1149. spider_net_set_mac(struct net_device *netdev, void *p)
  1150. {
  1151. struct spider_net_card *card = netdev_priv(netdev);
  1152. u32 macl, macu, regvalue;
  1153. struct sockaddr *addr = p;
  1154. if (!is_valid_ether_addr(addr->sa_data))
  1155. return -EADDRNOTAVAIL;
  1156. memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
  1157. /* switch off GMACTPE and GMACRPE */
  1158. regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
  1159. regvalue &= ~((1 << 5) | (1 << 6));
  1160. spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
  1161. /* write mac */
  1162. macu = (netdev->dev_addr[0]<<24) + (netdev->dev_addr[1]<<16) +
  1163. (netdev->dev_addr[2]<<8) + (netdev->dev_addr[3]);
  1164. macl = (netdev->dev_addr[4]<<8) + (netdev->dev_addr[5]);
  1165. spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
  1166. spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
  1167. /* switch GMACTPE and GMACRPE back on */
  1168. regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
  1169. regvalue |= ((1 << 5) | (1 << 6));
  1170. spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
  1171. spider_net_set_promisc(card);
  1172. return 0;
  1173. }
  1174. /**
  1175. * spider_net_link_reset
  1176. * @netdev: net device structure
  1177. *
  1178. * This is called when the PHY_LINK signal is asserted. For the blade this is
  1179. * not connected so we should never get here.
  1180. *
  1181. */
  1182. static void
  1183. spider_net_link_reset(struct net_device *netdev)
  1184. {
  1185. struct spider_net_card *card = netdev_priv(netdev);
  1186. del_timer_sync(&card->aneg_timer);
  1187. /* clear interrupt, block further interrupts */
  1188. spider_net_write_reg(card, SPIDER_NET_GMACST,
  1189. spider_net_read_reg(card, SPIDER_NET_GMACST));
  1190. spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);
  1191. /* reset phy and setup aneg */
  1192. card->aneg_count = 0;
  1193. card->medium = BCM54XX_COPPER;
  1194. spider_net_setup_aneg(card);
  1195. mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
  1196. }
  1197. /**
  1198. * spider_net_handle_error_irq - handles errors raised by an interrupt
  1199. * @card: card structure
  1200. * @status_reg: interrupt status register 0 (GHIINT0STS)
  1201. *
  1202. * spider_net_handle_error_irq treats or ignores all error conditions
  1203. * found when an interrupt is presented
  1204. */
  1205. static void
  1206. spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg,
  1207. u32 error_reg1, u32 error_reg2)
  1208. {
  1209. u32 i;
  1210. int show_error = 1;
  1211. /* check GHIINT0STS ************************************/
  1212. if (status_reg)
  1213. for (i = 0; i < 32; i++)
  1214. if (status_reg & (1<<i))
  1215. switch (i)
  1216. {
  1217. /* let error_reg1 and error_reg2 evaluation decide, what to do
  1218. case SPIDER_NET_PHYINT:
  1219. case SPIDER_NET_GMAC2INT:
  1220. case SPIDER_NET_GMAC1INT:
  1221. case SPIDER_NET_GFIFOINT:
  1222. case SPIDER_NET_DMACINT:
  1223. case SPIDER_NET_GSYSINT:
  1224. break; */
  1225. case SPIDER_NET_GIPSINT:
  1226. show_error = 0;
  1227. break;
  1228. case SPIDER_NET_GPWOPCMPINT:
  1229. /* PHY write operation completed */
  1230. show_error = 0;
  1231. break;
  1232. case SPIDER_NET_GPROPCMPINT:
  1233. /* PHY read operation completed */
  1234. /* we don't use semaphores, as we poll for the completion
  1235. * of the read operation in spider_net_read_phy. Should take
  1236. * about 50 us */
  1237. show_error = 0;
  1238. break;
  1239. case SPIDER_NET_GPWFFINT:
  1240. /* PHY command queue full */
  1241. if (netif_msg_intr(card))
  1242. dev_err(&card->netdev->dev, "PHY write queue full\n");
  1243. show_error = 0;
  1244. break;
  1245. /* case SPIDER_NET_GRMDADRINT: not used. print a message */
  1246. /* case SPIDER_NET_GRMARPINT: not used. print a message */
  1247. /* case SPIDER_NET_GRMMPINT: not used. print a message */
  1248. case SPIDER_NET_GDTDEN0INT:
  1249. /* someone has set TX_DMA_EN to 0 */
  1250. show_error = 0;
  1251. break;
  1252. case SPIDER_NET_GDDDEN0INT: /* fallthrough */
  1253. case SPIDER_NET_GDCDEN0INT: /* fallthrough */
  1254. case SPIDER_NET_GDBDEN0INT: /* fallthrough */
  1255. case SPIDER_NET_GDADEN0INT:
  1256. /* someone has set RX_DMA_EN to 0 */
  1257. show_error = 0;
  1258. break;
  1259. /* RX interrupts */
  1260. case SPIDER_NET_GDDFDCINT:
  1261. case SPIDER_NET_GDCFDCINT:
  1262. case SPIDER_NET_GDBFDCINT:
  1263. case SPIDER_NET_GDAFDCINT:
  1264. /* case SPIDER_NET_GDNMINT: not used. print a message */
  1265. /* case SPIDER_NET_GCNMINT: not used. print a message */
  1266. /* case SPIDER_NET_GBNMINT: not used. print a message */
  1267. /* case SPIDER_NET_GANMINT: not used. print a message */
  1268. /* case SPIDER_NET_GRFNMINT: not used. print a message */
  1269. show_error = 0;
  1270. break;
  1271. /* TX interrupts */
  1272. case SPIDER_NET_GDTFDCINT:
  1273. show_error = 0;
  1274. break;
  1275. case SPIDER_NET_GTTEDINT:
  1276. show_error = 0;
  1277. break;
  1278. case SPIDER_NET_GDTDCEINT:
  1279. /* chain end. If a descriptor should be sent, kick off
  1280. * tx dma
  1281. if (card->tx_chain.tail != card->tx_chain.head)
  1282. spider_net_kick_tx_dma(card);
  1283. */
  1284. show_error = 0;
  1285. break;
  1286. /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
  1287. /* case SPIDER_NET_GFREECNTINT: not used. print a message */
  1288. }
  1289. /* check GHIINT1STS ************************************/
  1290. if (error_reg1)
  1291. for (i = 0; i < 32; i++)
  1292. if (error_reg1 & (1<<i))
  1293. switch (i)
  1294. {
  1295. case SPIDER_NET_GTMFLLINT:
  1296. /* TX RAM full may happen on a usual case.
  1297. * Logging is not needed. */
  1298. show_error = 0;
  1299. break;
  1300. case SPIDER_NET_GRFDFLLINT: /* fallthrough */
  1301. case SPIDER_NET_GRFCFLLINT: /* fallthrough */
  1302. case SPIDER_NET_GRFBFLLINT: /* fallthrough */
  1303. case SPIDER_NET_GRFAFLLINT: /* fallthrough */
  1304. case SPIDER_NET_GRMFLLINT:
  1305. /* Could happen when rx chain is full */
  1306. if (card->ignore_rx_ramfull == 0) {
  1307. card->ignore_rx_ramfull = 1;
  1308. spider_net_resync_head_ptr(card);
  1309. spider_net_refill_rx_chain(card);
  1310. spider_net_enable_rxdmac(card);
  1311. card->num_rx_ints ++;
  1312. napi_schedule(&card->napi);
  1313. }
  1314. show_error = 0;
  1315. break;
  1316. /* case SPIDER_NET_GTMSHTINT: problem, print a message */
  1317. case SPIDER_NET_GDTINVDINT:
  1318. /* allrighty. tx from previous descr ok */
  1319. show_error = 0;
  1320. break;
  1321. /* chain end */
  1322. case SPIDER_NET_GDDDCEINT: /* fallthrough */
  1323. case SPIDER_NET_GDCDCEINT: /* fallthrough */
  1324. case SPIDER_NET_GDBDCEINT: /* fallthrough */
  1325. case SPIDER_NET_GDADCEINT:
  1326. spider_net_resync_head_ptr(card);
  1327. spider_net_refill_rx_chain(card);
  1328. spider_net_enable_rxdmac(card);
  1329. card->num_rx_ints ++;
  1330. napi_schedule(&card->napi);
  1331. show_error = 0;
  1332. break;
  1333. /* invalid descriptor */
  1334. case SPIDER_NET_GDDINVDINT: /* fallthrough */
  1335. case SPIDER_NET_GDCINVDINT: /* fallthrough */
  1336. case SPIDER_NET_GDBINVDINT: /* fallthrough */
  1337. case SPIDER_NET_GDAINVDINT:
  1338. /* Could happen when rx chain is full */
  1339. spider_net_resync_head_ptr(card);
  1340. spider_net_refill_rx_chain(card);
  1341. spider_net_enable_rxdmac(card);
  1342. card->num_rx_ints ++;
  1343. napi_schedule(&card->napi);
  1344. show_error = 0;
  1345. break;
  1346. /* case SPIDER_NET_GDTRSERINT: problem, print a message */
  1347. /* case SPIDER_NET_GDDRSERINT: problem, print a message */
  1348. /* case SPIDER_NET_GDCRSERINT: problem, print a message */
  1349. /* case SPIDER_NET_GDBRSERINT: problem, print a message */
  1350. /* case SPIDER_NET_GDARSERINT: problem, print a message */
  1351. /* case SPIDER_NET_GDSERINT: problem, print a message */
  1352. /* case SPIDER_NET_GDTPTERINT: problem, print a message */
  1353. /* case SPIDER_NET_GDDPTERINT: problem, print a message */
  1354. /* case SPIDER_NET_GDCPTERINT: problem, print a message */
  1355. /* case SPIDER_NET_GDBPTERINT: problem, print a message */
  1356. /* case SPIDER_NET_GDAPTERINT: problem, print a message */
  1357. default:
  1358. show_error = 1;
  1359. break;
  1360. }
  1361. /* check GHIINT2STS ************************************/
  1362. if (error_reg2)
  1363. for (i = 0; i < 32; i++)
  1364. if (error_reg2 & (1<<i))
  1365. switch (i)
  1366. {
  1367. /* there is nothing we can (want to) do at this time. Log a
  1368. * message, we can switch on and off the specific values later on
  1369. case SPIDER_NET_GPROPERINT:
  1370. case SPIDER_NET_GMCTCRSNGINT:
  1371. case SPIDER_NET_GMCTLCOLINT:
  1372. case SPIDER_NET_GMCTTMOTINT:
  1373. case SPIDER_NET_GMCRCAERINT:
  1374. case SPIDER_NET_GMCRCALERINT:
  1375. case SPIDER_NET_GMCRALNERINT:
  1376. case SPIDER_NET_GMCROVRINT:
  1377. case SPIDER_NET_GMCRRNTINT:
  1378. case SPIDER_NET_GMCRRXERINT:
  1379. case SPIDER_NET_GTITCSERINT:
  1380. case SPIDER_NET_GTIFMTERINT:
  1381. case SPIDER_NET_GTIPKTRVKINT:
  1382. case SPIDER_NET_GTISPINGINT:
  1383. case SPIDER_NET_GTISADNGINT:
  1384. case SPIDER_NET_GTISPDNGINT:
  1385. case SPIDER_NET_GRIFMTERINT:
  1386. case SPIDER_NET_GRIPKTRVKINT:
  1387. case SPIDER_NET_GRISPINGINT:
  1388. case SPIDER_NET_GRISADNGINT:
  1389. case SPIDER_NET_GRISPDNGINT:
  1390. break;
  1391. */
  1392. default:
  1393. break;
  1394. }
  1395. if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
  1396. dev_err(&card->netdev->dev, "Error interrupt, GHIINT0STS = 0x%08x, "
  1397. "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
  1398. status_reg, error_reg1, error_reg2);
  1399. /* clear interrupt sources */
  1400. spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
  1401. spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
  1402. }
  1403. /**
  1404. * spider_net_interrupt - interrupt handler for spider_net
  1405. * @irq: interrupt number
  1406. * @ptr: pointer to net_device
  1407. *
  1408. * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
  1409. * interrupt found raised by card.
  1410. *
  1411. * This is the interrupt handler, that turns off
  1412. * interrupts for this device and makes the stack poll the driver
  1413. */
  1414. static irqreturn_t
  1415. spider_net_interrupt(int irq, void *ptr)
  1416. {
  1417. struct net_device *netdev = ptr;
  1418. struct spider_net_card *card = netdev_priv(netdev);
  1419. u32 status_reg, error_reg1, error_reg2;
  1420. status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
  1421. error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
  1422. error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
  1423. if (!(status_reg & SPIDER_NET_INT0_MASK_VALUE) &&
  1424. !(error_reg1 & SPIDER_NET_INT1_MASK_VALUE) &&
  1425. !(error_reg2 & SPIDER_NET_INT2_MASK_VALUE))
  1426. return IRQ_NONE;
  1427. if (status_reg & SPIDER_NET_RXINT ) {
  1428. spider_net_rx_irq_off(card);
  1429. napi_schedule(&card->napi);
  1430. card->num_rx_ints ++;
  1431. }
  1432. if (status_reg & SPIDER_NET_TXINT)
  1433. napi_schedule(&card->napi);
  1434. if (status_reg & SPIDER_NET_LINKINT)
  1435. spider_net_link_reset(netdev);
  1436. if (status_reg & SPIDER_NET_ERRINT )
  1437. spider_net_handle_error_irq(card, status_reg,
  1438. error_reg1, error_reg2);
  1439. /* clear interrupt sources */
  1440. spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
  1441. return IRQ_HANDLED;
  1442. }
  1443. #ifdef CONFIG_NET_POLL_CONTROLLER
  1444. /**
  1445. * spider_net_poll_controller - artificial interrupt for netconsole etc.
  1446. * @netdev: interface device structure
  1447. *
  1448. * see Documentation/networking/netconsole.txt
  1449. */
  1450. static void
  1451. spider_net_poll_controller(struct net_device *netdev)
  1452. {
  1453. disable_irq(netdev->irq);
  1454. spider_net_interrupt(netdev->irq, netdev);
  1455. enable_irq(netdev->irq);
  1456. }
  1457. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1458. /**
  1459. * spider_net_enable_interrupts - enable interrupts
  1460. * @card: card structure
  1461. *
  1462. * spider_net_enable_interrupt enables several interrupts
  1463. */
  1464. static void
  1465. spider_net_enable_interrupts(struct spider_net_card *card)
  1466. {
  1467. spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
  1468. SPIDER_NET_INT0_MASK_VALUE);
  1469. spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
  1470. SPIDER_NET_INT1_MASK_VALUE);
  1471. spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
  1472. SPIDER_NET_INT2_MASK_VALUE);
  1473. }
  1474. /**
  1475. * spider_net_disable_interrupts - disable interrupts
  1476. * @card: card structure
  1477. *
  1478. * spider_net_disable_interrupts disables all the interrupts
  1479. */
  1480. static void
  1481. spider_net_disable_interrupts(struct spider_net_card *card)
  1482. {
  1483. spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
  1484. spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
  1485. spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
  1486. spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);
  1487. }
  1488. /**
  1489. * spider_net_init_card - initializes the card
  1490. * @card: card structure
  1491. *
  1492. * spider_net_init_card initializes the card so that other registers can
  1493. * be used
  1494. */
  1495. static void
  1496. spider_net_init_card(struct spider_net_card *card)
  1497. {
  1498. spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
  1499. SPIDER_NET_CKRCTRL_STOP_VALUE);
  1500. spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
  1501. SPIDER_NET_CKRCTRL_RUN_VALUE);
  1502. /* trigger ETOMOD signal */
  1503. spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
  1504. spider_net_read_reg(card, SPIDER_NET_GMACOPEMD) | 0x4);
  1505. spider_net_disable_interrupts(card);
  1506. }
  1507. /**
  1508. * spider_net_enable_card - enables the card by setting all kinds of regs
  1509. * @card: card structure
  1510. *
  1511. * spider_net_enable_card sets a lot of SMMIO registers to enable the device
  1512. */
  1513. static void
  1514. spider_net_enable_card(struct spider_net_card *card)
  1515. {
  1516. int i;
  1517. /* the following array consists of (register),(value) pairs
  1518. * that are set in this function. A register of 0 ends the list */
  1519. u32 regs[][2] = {
  1520. { SPIDER_NET_GRESUMINTNUM, 0 },
  1521. { SPIDER_NET_GREINTNUM, 0 },
  1522. /* set interrupt frame number registers */
  1523. /* clear the single DMA engine registers first */
  1524. { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
  1525. { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
  1526. { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
  1527. { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
  1528. /* then set, what we really need */
  1529. { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
  1530. /* timer counter registers and stuff */
  1531. { SPIDER_NET_GFREECNNUM, 0 },
  1532. { SPIDER_NET_GONETIMENUM, 0 },
  1533. { SPIDER_NET_GTOUTFRMNUM, 0 },
  1534. /* RX mode setting */
  1535. { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
  1536. /* TX mode setting */
  1537. { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
  1538. /* IPSEC mode setting */
  1539. { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
  1540. { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
  1541. { SPIDER_NET_GMRWOLCTRL, 0 },
  1542. { SPIDER_NET_GTESTMD, 0x10000000 },
  1543. { SPIDER_NET_GTTQMSK, 0x00400040 },
  1544. { SPIDER_NET_GMACINTEN, 0 },
  1545. /* flow control stuff */
  1546. { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
  1547. { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
  1548. { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
  1549. { 0, 0}
  1550. };
  1551. i = 0;
  1552. while (regs[i][0]) {
  1553. spider_net_write_reg(card, regs[i][0], regs[i][1]);
  1554. i++;
  1555. }
  1556. /* clear unicast filter table entries 1 to 14 */
  1557. for (i = 1; i <= 14; i++) {
  1558. spider_net_write_reg(card,
  1559. SPIDER_NET_GMRUAFILnR + i * 8,
  1560. 0x00080000);
  1561. spider_net_write_reg(card,
  1562. SPIDER_NET_GMRUAFILnR + i * 8 + 4,
  1563. 0x00000000);
  1564. }
  1565. spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
  1566. spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
  1567. /* set chain tail address for RX chains and
  1568. * enable DMA */
  1569. spider_net_enable_rxchtails(card);
  1570. spider_net_enable_rxdmac(card);
  1571. spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
  1572. spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
  1573. SPIDER_NET_LENLMT_VALUE);
  1574. spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
  1575. SPIDER_NET_OPMODE_VALUE);
  1576. spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
  1577. SPIDER_NET_GDTBSTA);
  1578. }
  1579. /**
  1580. * spider_net_download_firmware - loads firmware into the adapter
  1581. * @card: card structure
  1582. * @firmware_ptr: pointer to firmware data
  1583. *
  1584. * spider_net_download_firmware loads the firmware data into the
  1585. * adapter. It assumes the length etc. to be allright.
  1586. */
  1587. static int
  1588. spider_net_download_firmware(struct spider_net_card *card,
  1589. const void *firmware_ptr)
  1590. {
  1591. int sequencer, i;
  1592. const u32 *fw_ptr = firmware_ptr;
  1593. /* stop sequencers */
  1594. spider_net_write_reg(card, SPIDER_NET_GSINIT,
  1595. SPIDER_NET_STOP_SEQ_VALUE);
  1596. for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
  1597. sequencer++) {
  1598. spider_net_write_reg(card,
  1599. SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
  1600. for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
  1601. spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
  1602. sequencer * 8, *fw_ptr);
  1603. fw_ptr++;
  1604. }
  1605. }
  1606. if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
  1607. return -EIO;
  1608. spider_net_write_reg(card, SPIDER_NET_GSINIT,
  1609. SPIDER_NET_RUN_SEQ_VALUE);
  1610. return 0;
  1611. }
  1612. /**
  1613. * spider_net_init_firmware - reads in firmware parts
  1614. * @card: card structure
  1615. *
  1616. * Returns 0 on success, <0 on failure
  1617. *
  1618. * spider_net_init_firmware opens the sequencer firmware and does some basic
  1619. * checks. This function opens and releases the firmware structure. A call
  1620. * to download the firmware is performed before the release.
  1621. *
  1622. * Firmware format
  1623. * ===============
  1624. * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
  1625. * the program for each sequencer. Use the command
  1626. * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
  1627. * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
  1628. * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
  1629. *
  1630. * to generate spider_fw.bin, if you have sequencer programs with something
  1631. * like the following contents for each sequencer:
  1632. * <ONE LINE COMMENT>
  1633. * <FIRST 4-BYTES-WORD FOR SEQUENCER>
  1634. * <SECOND 4-BYTES-WORD FOR SEQUENCER>
  1635. * ...
  1636. * <1024th 4-BYTES-WORD FOR SEQUENCER>
  1637. */
  1638. static int
  1639. spider_net_init_firmware(struct spider_net_card *card)
  1640. {
  1641. struct firmware *firmware = NULL;
  1642. struct device_node *dn;
  1643. const u8 *fw_prop = NULL;
  1644. int err = -ENOENT;
  1645. int fw_size;
  1646. if (request_firmware((const struct firmware **)&firmware,
  1647. SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
  1648. if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
  1649. netif_msg_probe(card) ) {
  1650. dev_err(&card->netdev->dev,
  1651. "Incorrect size of spidernet firmware in " \
  1652. "filesystem. Looking in host firmware...\n");
  1653. goto try_host_fw;
  1654. }
  1655. err = spider_net_download_firmware(card, firmware->data);
  1656. release_firmware(firmware);
  1657. if (err)
  1658. goto try_host_fw;
  1659. goto done;
  1660. }
  1661. try_host_fw:
  1662. dn = pci_device_to_OF_node(card->pdev);
  1663. if (!dn)
  1664. goto out_err;
  1665. fw_prop = of_get_property(dn, "firmware", &fw_size);
  1666. if (!fw_prop)
  1667. goto out_err;
  1668. if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
  1669. netif_msg_probe(card) ) {
  1670. dev_err(&card->netdev->dev,
  1671. "Incorrect size of spidernet firmware in host firmware\n");
  1672. goto done;
  1673. }
  1674. err = spider_net_download_firmware(card, fw_prop);
  1675. done:
  1676. return err;
  1677. out_err:
  1678. if (netif_msg_probe(card))
  1679. dev_err(&card->netdev->dev,
  1680. "Couldn't find spidernet firmware in filesystem " \
  1681. "or host firmware\n");
  1682. return err;
  1683. }
  1684. /**
  1685. * spider_net_open - called upon ifonfig up
  1686. * @netdev: interface device structure
  1687. *
  1688. * returns 0 on success, <0 on failure
  1689. *
  1690. * spider_net_open allocates all the descriptors and memory needed for
  1691. * operation, sets up multicast list and enables interrupts
  1692. */
  1693. int
  1694. spider_net_open(struct net_device *netdev)
  1695. {
  1696. struct spider_net_card *card = netdev_priv(netdev);
  1697. int result;
  1698. result = spider_net_init_firmware(card);
  1699. if (result)
  1700. goto init_firmware_failed;
  1701. /* start probing with copper */
  1702. card->aneg_count = 0;
  1703. card->medium = BCM54XX_COPPER;
  1704. spider_net_setup_aneg(card);
  1705. if (card->phy.def->phy_id)
  1706. mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
  1707. result = spider_net_init_chain(card, &card->tx_chain);
  1708. if (result)
  1709. goto alloc_tx_failed;
  1710. card->low_watermark = NULL;
  1711. result = spider_net_init_chain(card, &card->rx_chain);
  1712. if (result)
  1713. goto alloc_rx_failed;
  1714. /* Allocate rx skbs */
  1715. result = spider_net_alloc_rx_skbs(card);
  1716. if (result)
  1717. goto alloc_skbs_failed;
  1718. spider_net_set_multi(netdev);
  1719. /* further enhancement: setup hw vlan, if needed */
  1720. result = -EBUSY;
  1721. if (request_irq(netdev->irq, spider_net_interrupt,
  1722. IRQF_SHARED, netdev->name, netdev))
  1723. goto register_int_failed;
  1724. spider_net_enable_card(card);
  1725. netif_start_queue(netdev);
  1726. netif_carrier_on(netdev);
  1727. napi_enable(&card->napi);
  1728. spider_net_enable_interrupts(card);
  1729. return 0;
  1730. register_int_failed:
  1731. spider_net_free_rx_chain_contents(card);
  1732. alloc_skbs_failed:
  1733. spider_net_free_chain(card, &card->rx_chain);
  1734. alloc_rx_failed:
  1735. spider_net_free_chain(card, &card->tx_chain);
  1736. alloc_tx_failed:
  1737. del_timer_sync(&card->aneg_timer);
  1738. init_firmware_failed:
  1739. return result;
  1740. }
  1741. /**
  1742. * spider_net_link_phy
  1743. * @data: used for pointer to card structure
  1744. *
  1745. */
  1746. static void spider_net_link_phy(unsigned long data)
  1747. {
  1748. struct spider_net_card *card = (struct spider_net_card *)data;
  1749. struct mii_phy *phy = &card->phy;
  1750. /* if link didn't come up after SPIDER_NET_ANEG_TIMEOUT tries, setup phy again */
  1751. if (card->aneg_count > SPIDER_NET_ANEG_TIMEOUT) {
  1752. pr_debug("%s: link is down trying to bring it up\n",
  1753. card->netdev->name);
  1754. switch (card->medium) {
  1755. case BCM54XX_COPPER:
  1756. /* enable fiber with autonegotiation first */
  1757. if (phy->def->ops->enable_fiber)
  1758. phy->def->ops->enable_fiber(phy, 1);
  1759. card->medium = BCM54XX_FIBER;
  1760. break;
  1761. case BCM54XX_FIBER:
  1762. /* fiber didn't come up, try to disable fiber autoneg */
  1763. if (phy->def->ops->enable_fiber)
  1764. phy->def->ops->enable_fiber(phy, 0);
  1765. card->medium = BCM54XX_UNKNOWN;
  1766. break;
  1767. case BCM54XX_UNKNOWN:
  1768. /* copper, fiber with and without failed,
  1769. * retry from beginning */
  1770. spider_net_setup_aneg(card);
  1771. card->medium = BCM54XX_COPPER;
  1772. break;
  1773. }
  1774. card->aneg_count = 0;
  1775. mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
  1776. return;
  1777. }
  1778. /* link still not up, try again later */
  1779. if (!(phy->def->ops->poll_link(phy))) {
  1780. card->aneg_count++;
  1781. mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
  1782. return;
  1783. }
  1784. /* link came up, get abilities */
  1785. phy->def->ops->read_link(phy);
  1786. spider_net_write_reg(card, SPIDER_NET_GMACST,
  1787. spider_net_read_reg(card, SPIDER_NET_GMACST));
  1788. spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0x4);
  1789. if (phy->speed == 1000)
  1790. spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0x00000001);
  1791. else
  1792. spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0);
  1793. card->aneg_count = 0;
  1794. pr_info("%s: link up, %i Mbps, %s-duplex %sautoneg.\n",
  1795. card->netdev->name, phy->speed,
  1796. phy->duplex == 1 ? "Full" : "Half",
  1797. phy->autoneg == 1 ? "" : "no ");
  1798. }
  1799. /**
  1800. * spider_net_setup_phy - setup PHY
  1801. * @card: card structure
  1802. *
  1803. * returns 0 on success, <0 on failure
  1804. *
  1805. * spider_net_setup_phy is used as part of spider_net_probe.
  1806. **/
  1807. static int
  1808. spider_net_setup_phy(struct spider_net_card *card)
  1809. {
  1810. struct mii_phy *phy = &card->phy;
  1811. spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
  1812. SPIDER_NET_DMASEL_VALUE);
  1813. spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
  1814. SPIDER_NET_PHY_CTRL_VALUE);
  1815. phy->dev = card->netdev;
  1816. phy->mdio_read = spider_net_read_phy;
  1817. phy->mdio_write = spider_net_write_phy;
  1818. for (phy->mii_id = 1; phy->mii_id <= 31; phy->mii_id++) {
  1819. unsigned short id;
  1820. id = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
  1821. if (id != 0x0000 && id != 0xffff) {
  1822. if (!sungem_phy_probe(phy, phy->mii_id)) {
  1823. pr_info("Found %s.\n", phy->def->name);
  1824. break;
  1825. }
  1826. }
  1827. }
  1828. return 0;
  1829. }
  1830. /**
  1831. * spider_net_workaround_rxramfull - work around firmware bug
  1832. * @card: card structure
  1833. *
  1834. * no return value
  1835. **/
  1836. static void
  1837. spider_net_workaround_rxramfull(struct spider_net_card *card)
  1838. {
  1839. int i, sequencer = 0;
  1840. /* cancel reset */
  1841. spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
  1842. SPIDER_NET_CKRCTRL_RUN_VALUE);
  1843. /* empty sequencer data */
  1844. for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
  1845. sequencer++) {
  1846. spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
  1847. sequencer * 8, 0x0);
  1848. for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
  1849. spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
  1850. sequencer * 8, 0x0);
  1851. }
  1852. }
  1853. /* set sequencer operation */
  1854. spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
  1855. /* reset */
  1856. spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
  1857. SPIDER_NET_CKRCTRL_STOP_VALUE);
  1858. }
  1859. /**
  1860. * spider_net_stop - called upon ifconfig down
  1861. * @netdev: interface device structure
  1862. *
  1863. * always returns 0
  1864. */
  1865. int
  1866. spider_net_stop(struct net_device *netdev)
  1867. {
  1868. struct spider_net_card *card = netdev_priv(netdev);
  1869. napi_disable(&card->napi);
  1870. netif_carrier_off(netdev);
  1871. netif_stop_queue(netdev);
  1872. del_timer_sync(&card->tx_timer);
  1873. del_timer_sync(&card->aneg_timer);
  1874. spider_net_disable_interrupts(card);
  1875. free_irq(netdev->irq, netdev);
  1876. spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
  1877. SPIDER_NET_DMA_TX_FEND_VALUE);
  1878. /* turn off DMA, force end */
  1879. spider_net_disable_rxdmac(card);
  1880. /* release chains */
  1881. spider_net_release_tx_chain(card, 1);
  1882. spider_net_free_rx_chain_contents(card);
  1883. spider_net_free_chain(card, &card->tx_chain);
  1884. spider_net_free_chain(card, &card->rx_chain);
  1885. return 0;
  1886. }
  1887. /**
  1888. * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
  1889. * function (to be called not under interrupt status)
  1890. * @data: data, is interface device structure
  1891. *
  1892. * called as task when tx hangs, resets interface (if interface is up)
  1893. */
  1894. static void
  1895. spider_net_tx_timeout_task(struct work_struct *work)
  1896. {
  1897. struct spider_net_card *card =
  1898. container_of(work, struct spider_net_card, tx_timeout_task);
  1899. struct net_device *netdev = card->netdev;
  1900. if (!(netdev->flags & IFF_UP))
  1901. goto out;
  1902. netif_device_detach(netdev);
  1903. spider_net_stop(netdev);
  1904. spider_net_workaround_rxramfull(card);
  1905. spider_net_init_card(card);
  1906. if (spider_net_setup_phy(card))
  1907. goto out;
  1908. spider_net_open(netdev);
  1909. spider_net_kick_tx_dma(card);
  1910. netif_device_attach(netdev);
  1911. out:
  1912. atomic_dec(&card->tx_timeout_task_counter);
  1913. }
  1914. /**
  1915. * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
  1916. * @netdev: interface device structure
  1917. *
  1918. * called, if tx hangs. Schedules a task that resets the interface
  1919. */
  1920. static void
  1921. spider_net_tx_timeout(struct net_device *netdev)
  1922. {
  1923. struct spider_net_card *card;
  1924. card = netdev_priv(netdev);
  1925. atomic_inc(&card->tx_timeout_task_counter);
  1926. if (netdev->flags & IFF_UP)
  1927. schedule_work(&card->tx_timeout_task);
  1928. else
  1929. atomic_dec(&card->tx_timeout_task_counter);
  1930. card->spider_stats.tx_timeouts++;
  1931. }
  1932. static const struct net_device_ops spider_net_ops = {
  1933. .ndo_open = spider_net_open,
  1934. .ndo_stop = spider_net_stop,
  1935. .ndo_start_xmit = spider_net_xmit,
  1936. .ndo_set_rx_mode = spider_net_set_multi,
  1937. .ndo_set_mac_address = spider_net_set_mac,
  1938. .ndo_change_mtu = spider_net_change_mtu,
  1939. .ndo_do_ioctl = spider_net_do_ioctl,
  1940. .ndo_tx_timeout = spider_net_tx_timeout,
  1941. .ndo_validate_addr = eth_validate_addr,
  1942. /* HW VLAN */
  1943. #ifdef CONFIG_NET_POLL_CONTROLLER
  1944. /* poll controller */
  1945. .ndo_poll_controller = spider_net_poll_controller,
  1946. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1947. };
  1948. /**
  1949. * spider_net_setup_netdev_ops - initialization of net_device operations
  1950. * @netdev: net_device structure
  1951. *
  1952. * fills out function pointers in the net_device structure
  1953. */
  1954. static void
  1955. spider_net_setup_netdev_ops(struct net_device *netdev)
  1956. {
  1957. netdev->netdev_ops = &spider_net_ops;
  1958. netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
  1959. /* ethtool ops */
  1960. netdev->ethtool_ops = &spider_net_ethtool_ops;
  1961. }
  1962. /**
  1963. * spider_net_setup_netdev - initialization of net_device
  1964. * @card: card structure
  1965. *
  1966. * Returns 0 on success or <0 on failure
  1967. *
  1968. * spider_net_setup_netdev initializes the net_device structure
  1969. **/
  1970. static int
  1971. spider_net_setup_netdev(struct spider_net_card *card)
  1972. {
  1973. int result;
  1974. struct net_device *netdev = card->netdev;
  1975. struct device_node *dn;
  1976. struct sockaddr addr;
  1977. const u8 *mac;
  1978. SET_NETDEV_DEV(netdev, &card->pdev->dev);
  1979. pci_set_drvdata(card->pdev, netdev);
  1980. init_timer(&card->tx_timer);
  1981. card->tx_timer.function =
  1982. (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
  1983. card->tx_timer.data = (unsigned long) card;
  1984. netdev->irq = card->pdev->irq;
  1985. card->aneg_count = 0;
  1986. init_timer(&card->aneg_timer);
  1987. card->aneg_timer.function = spider_net_link_phy;
  1988. card->aneg_timer.data = (unsigned long) card;
  1989. netif_napi_add(netdev, &card->napi,
  1990. spider_net_poll, SPIDER_NET_NAPI_WEIGHT);
  1991. spider_net_setup_netdev_ops(netdev);
  1992. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
  1993. if (SPIDER_NET_RX_CSUM_DEFAULT)
  1994. netdev->features |= NETIF_F_RXCSUM;
  1995. netdev->features |= NETIF_F_IP_CSUM | NETIF_F_LLTX;
  1996. /* some time: NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  1997. * NETIF_F_HW_VLAN_CTAG_FILTER */
  1998. netdev->irq = card->pdev->irq;
  1999. card->num_rx_ints = 0;
  2000. card->ignore_rx_ramfull = 0;
  2001. dn = pci_device_to_OF_node(card->pdev);
  2002. if (!dn)
  2003. return -EIO;
  2004. mac = of_get_property(dn, "local-mac-address", NULL);
  2005. if (!mac)
  2006. return -EIO;
  2007. memcpy(addr.sa_data, mac, ETH_ALEN);
  2008. result = spider_net_set_mac(netdev, &addr);
  2009. if ((result) && (netif_msg_probe(card)))
  2010. dev_err(&card->netdev->dev,
  2011. "Failed to set MAC address: %i\n", result);
  2012. result = register_netdev(netdev);
  2013. if (result) {
  2014. if (netif_msg_probe(card))
  2015. dev_err(&card->netdev->dev,
  2016. "Couldn't register net_device: %i\n", result);
  2017. return result;
  2018. }
  2019. if (netif_msg_probe(card))
  2020. pr_info("Initialized device %s.\n", netdev->name);
  2021. return 0;
  2022. }
  2023. /**
  2024. * spider_net_alloc_card - allocates net_device and card structure
  2025. *
  2026. * returns the card structure or NULL in case of errors
  2027. *
  2028. * the card and net_device structures are linked to each other
  2029. */
  2030. static struct spider_net_card *
  2031. spider_net_alloc_card(void)
  2032. {
  2033. struct net_device *netdev;
  2034. struct spider_net_card *card;
  2035. size_t alloc_size;
  2036. alloc_size = sizeof(struct spider_net_card) +
  2037. (tx_descriptors + rx_descriptors) * sizeof(struct spider_net_descr);
  2038. netdev = alloc_etherdev(alloc_size);
  2039. if (!netdev)
  2040. return NULL;
  2041. card = netdev_priv(netdev);
  2042. card->netdev = netdev;
  2043. card->msg_enable = SPIDER_NET_DEFAULT_MSG;
  2044. INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
  2045. init_waitqueue_head(&card->waitq);
  2046. atomic_set(&card->tx_timeout_task_counter, 0);
  2047. card->rx_chain.num_desc = rx_descriptors;
  2048. card->rx_chain.ring = card->darray;
  2049. card->tx_chain.num_desc = tx_descriptors;
  2050. card->tx_chain.ring = card->darray + rx_descriptors;
  2051. return card;
  2052. }
  2053. /**
  2054. * spider_net_undo_pci_setup - releases PCI ressources
  2055. * @card: card structure
  2056. *
  2057. * spider_net_undo_pci_setup releases the mapped regions
  2058. */
  2059. static void
  2060. spider_net_undo_pci_setup(struct spider_net_card *card)
  2061. {
  2062. iounmap(card->regs);
  2063. pci_release_regions(card->pdev);
  2064. }
  2065. /**
  2066. * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
  2067. * @pdev: PCI device
  2068. *
  2069. * Returns the card structure or NULL if any errors occur
  2070. *
  2071. * spider_net_setup_pci_dev initializes pdev and together with the
  2072. * functions called in spider_net_open configures the device so that
  2073. * data can be transferred over it
  2074. * The net_device structure is attached to the card structure, if the
  2075. * function returns without error.
  2076. **/
  2077. static struct spider_net_card *
  2078. spider_net_setup_pci_dev(struct pci_dev *pdev)
  2079. {
  2080. struct spider_net_card *card;
  2081. unsigned long mmio_start, mmio_len;
  2082. if (pci_enable_device(pdev)) {
  2083. dev_err(&pdev->dev, "Couldn't enable PCI device\n");
  2084. return NULL;
  2085. }
  2086. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  2087. dev_err(&pdev->dev,
  2088. "Couldn't find proper PCI device base address.\n");
  2089. goto out_disable_dev;
  2090. }
  2091. if (pci_request_regions(pdev, spider_net_driver_name)) {
  2092. dev_err(&pdev->dev,
  2093. "Couldn't obtain PCI resources, aborting.\n");
  2094. goto out_disable_dev;
  2095. }
  2096. pci_set_master(pdev);
  2097. card = spider_net_alloc_card();
  2098. if (!card) {
  2099. dev_err(&pdev->dev,
  2100. "Couldn't allocate net_device structure, aborting.\n");
  2101. goto out_release_regions;
  2102. }
  2103. card->pdev = pdev;
  2104. /* fetch base address and length of first resource */
  2105. mmio_start = pci_resource_start(pdev, 0);
  2106. mmio_len = pci_resource_len(pdev, 0);
  2107. card->netdev->mem_start = mmio_start;
  2108. card->netdev->mem_end = mmio_start + mmio_len;
  2109. card->regs = ioremap(mmio_start, mmio_len);
  2110. if (!card->regs) {
  2111. dev_err(&pdev->dev,
  2112. "Couldn't obtain PCI resources, aborting.\n");
  2113. goto out_release_regions;
  2114. }
  2115. return card;
  2116. out_release_regions:
  2117. pci_release_regions(pdev);
  2118. out_disable_dev:
  2119. pci_disable_device(pdev);
  2120. return NULL;
  2121. }
  2122. /**
  2123. * spider_net_probe - initialization of a device
  2124. * @pdev: PCI device
  2125. * @ent: entry in the device id list
  2126. *
  2127. * Returns 0 on success, <0 on failure
  2128. *
  2129. * spider_net_probe initializes pdev and registers a net_device
  2130. * structure for it. After that, the device can be ifconfig'ed up
  2131. **/
  2132. static int
  2133. spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2134. {
  2135. int err = -EIO;
  2136. struct spider_net_card *card;
  2137. card = spider_net_setup_pci_dev(pdev);
  2138. if (!card)
  2139. goto out;
  2140. spider_net_workaround_rxramfull(card);
  2141. spider_net_init_card(card);
  2142. err = spider_net_setup_phy(card);
  2143. if (err)
  2144. goto out_undo_pci;
  2145. err = spider_net_setup_netdev(card);
  2146. if (err)
  2147. goto out_undo_pci;
  2148. return 0;
  2149. out_undo_pci:
  2150. spider_net_undo_pci_setup(card);
  2151. free_netdev(card->netdev);
  2152. out:
  2153. return err;
  2154. }
  2155. /**
  2156. * spider_net_remove - removal of a device
  2157. * @pdev: PCI device
  2158. *
  2159. * Returns 0 on success, <0 on failure
  2160. *
  2161. * spider_net_remove is called to remove the device and unregisters the
  2162. * net_device
  2163. **/
  2164. static void
  2165. spider_net_remove(struct pci_dev *pdev)
  2166. {
  2167. struct net_device *netdev;
  2168. struct spider_net_card *card;
  2169. netdev = pci_get_drvdata(pdev);
  2170. card = netdev_priv(netdev);
  2171. wait_event(card->waitq,
  2172. atomic_read(&card->tx_timeout_task_counter) == 0);
  2173. unregister_netdev(netdev);
  2174. /* switch off card */
  2175. spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
  2176. SPIDER_NET_CKRCTRL_STOP_VALUE);
  2177. spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
  2178. SPIDER_NET_CKRCTRL_RUN_VALUE);
  2179. spider_net_undo_pci_setup(card);
  2180. free_netdev(netdev);
  2181. }
  2182. static struct pci_driver spider_net_driver = {
  2183. .name = spider_net_driver_name,
  2184. .id_table = spider_net_pci_tbl,
  2185. .probe = spider_net_probe,
  2186. .remove = spider_net_remove
  2187. };
  2188. /**
  2189. * spider_net_init - init function when the driver is loaded
  2190. *
  2191. * spider_net_init registers the device driver
  2192. */
  2193. static int __init spider_net_init(void)
  2194. {
  2195. printk(KERN_INFO "Spidernet version %s.\n", VERSION);
  2196. if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
  2197. rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
  2198. pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
  2199. }
  2200. if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
  2201. rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
  2202. pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
  2203. }
  2204. if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
  2205. tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
  2206. pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
  2207. }
  2208. if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
  2209. tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
  2210. pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
  2211. }
  2212. return pci_register_driver(&spider_net_driver);
  2213. }
  2214. /**
  2215. * spider_net_cleanup - exit function when driver is unloaded
  2216. *
  2217. * spider_net_cleanup unregisters the device driver
  2218. */
  2219. static void __exit spider_net_cleanup(void)
  2220. {
  2221. pci_unregister_driver(&spider_net_driver);
  2222. }
  2223. module_init(spider_net_init);
  2224. module_exit(spider_net_cleanup);