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