spider_net.c 70 KB

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