spider_net.c 70 KB

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