spider_net.c 70 KB

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