spider_net.c 70 KB

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