via-velocity.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This code is derived from the VIA reference driver (copyright message
  4. * below) provided to Red Hat by VIA Networking Technologies, Inc. for
  5. * addition to the Linux kernel.
  6. *
  7. * The code has been merged into one source file, cleaned up to follow
  8. * Linux coding style, ported to the Linux 2.6 kernel tree and cleaned
  9. * for 64bit hardware platforms.
  10. *
  11. * TODO
  12. * rx_copybreak/alignment
  13. * More testing
  14. *
  15. * The changes are (c) Copyright 2004, Red Hat Inc. <alan@lxorguk.ukuu.org.uk>
  16. * Additional fixes and clean up: Francois Romieu
  17. *
  18. * This source has not been verified for use in safety critical systems.
  19. *
  20. * Please direct queries about the revamped driver to the linux-kernel
  21. * list not VIA.
  22. *
  23. * Original code:
  24. *
  25. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  26. * All rights reserved.
  27. *
  28. * Author: Chuang Liang-Shing, AJ Jiang
  29. *
  30. * Date: Jan 24, 2003
  31. *
  32. * MODULE_LICENSE("GPL");
  33. */
  34. #include <linux/module.h>
  35. #include <linux/types.h>
  36. #include <linux/bitops.h>
  37. #include <linux/init.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/mm.h>
  40. #include <linux/errno.h>
  41. #include <linux/ioport.h>
  42. #include <linux/pci.h>
  43. #include <linux/kernel.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/etherdevice.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/delay.h>
  48. #include <linux/timer.h>
  49. #include <linux/slab.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/string.h>
  52. #include <linux/wait.h>
  53. #include <linux/io.h>
  54. #include <linux/if.h>
  55. #include <linux/uaccess.h>
  56. #include <linux/proc_fs.h>
  57. #include <linux/of_address.h>
  58. #include <linux/of_device.h>
  59. #include <linux/of_irq.h>
  60. #include <linux/inetdevice.h>
  61. #include <linux/platform_device.h>
  62. #include <linux/reboot.h>
  63. #include <linux/ethtool.h>
  64. #include <linux/mii.h>
  65. #include <linux/in.h>
  66. #include <linux/if_arp.h>
  67. #include <linux/if_vlan.h>
  68. #include <linux/ip.h>
  69. #include <linux/tcp.h>
  70. #include <linux/udp.h>
  71. #include <linux/crc-ccitt.h>
  72. #include <linux/crc32.h>
  73. #include "via-velocity.h"
  74. enum velocity_bus_type {
  75. BUS_PCI,
  76. BUS_PLATFORM,
  77. };
  78. static int velocity_nics;
  79. static int msglevel = MSG_LEVEL_INFO;
  80. static void velocity_set_power_state(struct velocity_info *vptr, char state)
  81. {
  82. void *addr = vptr->mac_regs;
  83. if (vptr->pdev)
  84. pci_set_power_state(vptr->pdev, state);
  85. else
  86. writeb(state, addr + 0x154);
  87. }
  88. /**
  89. * mac_get_cam_mask - Read a CAM mask
  90. * @regs: register block for this velocity
  91. * @mask: buffer to store mask
  92. *
  93. * Fetch the mask bits of the selected CAM and store them into the
  94. * provided mask buffer.
  95. */
  96. static void mac_get_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
  97. {
  98. int i;
  99. /* Select CAM mask */
  100. BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  101. writeb(0, &regs->CAMADDR);
  102. /* read mask */
  103. for (i = 0; i < 8; i++)
  104. *mask++ = readb(&(regs->MARCAM[i]));
  105. /* disable CAMEN */
  106. writeb(0, &regs->CAMADDR);
  107. /* Select mar */
  108. BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  109. }
  110. /**
  111. * mac_set_cam_mask - Set a CAM mask
  112. * @regs: register block for this velocity
  113. * @mask: CAM mask to load
  114. *
  115. * Store a new mask into a CAM
  116. */
  117. static void mac_set_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
  118. {
  119. int i;
  120. /* Select CAM mask */
  121. BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  122. writeb(CAMADDR_CAMEN, &regs->CAMADDR);
  123. for (i = 0; i < 8; i++)
  124. writeb(*mask++, &(regs->MARCAM[i]));
  125. /* disable CAMEN */
  126. writeb(0, &regs->CAMADDR);
  127. /* Select mar */
  128. BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  129. }
  130. static void mac_set_vlan_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
  131. {
  132. int i;
  133. /* Select CAM mask */
  134. BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  135. writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, &regs->CAMADDR);
  136. for (i = 0; i < 8; i++)
  137. writeb(*mask++, &(regs->MARCAM[i]));
  138. /* disable CAMEN */
  139. writeb(0, &regs->CAMADDR);
  140. /* Select mar */
  141. BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  142. }
  143. /**
  144. * mac_set_cam - set CAM data
  145. * @regs: register block of this velocity
  146. * @idx: Cam index
  147. * @addr: 2 or 6 bytes of CAM data
  148. *
  149. * Load an address or vlan tag into a CAM
  150. */
  151. static void mac_set_cam(struct mac_regs __iomem *regs, int idx, const u8 *addr)
  152. {
  153. int i;
  154. /* Select CAM mask */
  155. BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  156. idx &= (64 - 1);
  157. writeb(CAMADDR_CAMEN | idx, &regs->CAMADDR);
  158. for (i = 0; i < 6; i++)
  159. writeb(*addr++, &(regs->MARCAM[i]));
  160. BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);
  161. udelay(10);
  162. writeb(0, &regs->CAMADDR);
  163. /* Select mar */
  164. BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  165. }
  166. static void mac_set_vlan_cam(struct mac_regs __iomem *regs, int idx,
  167. const u8 *addr)
  168. {
  169. /* Select CAM mask */
  170. BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  171. idx &= (64 - 1);
  172. writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL | idx, &regs->CAMADDR);
  173. writew(*((u16 *) addr), &regs->MARCAM[0]);
  174. BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);
  175. udelay(10);
  176. writeb(0, &regs->CAMADDR);
  177. /* Select mar */
  178. BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
  179. }
  180. /**
  181. * mac_wol_reset - reset WOL after exiting low power
  182. * @regs: register block of this velocity
  183. *
  184. * Called after we drop out of wake on lan mode in order to
  185. * reset the Wake on lan features. This function doesn't restore
  186. * the rest of the logic from the result of sleep/wakeup
  187. */
  188. static void mac_wol_reset(struct mac_regs __iomem *regs)
  189. {
  190. /* Turn off SWPTAG right after leaving power mode */
  191. BYTE_REG_BITS_OFF(STICKHW_SWPTAG, &regs->STICKHW);
  192. /* clear sticky bits */
  193. BYTE_REG_BITS_OFF((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
  194. BYTE_REG_BITS_OFF(CHIPGCR_FCGMII, &regs->CHIPGCR);
  195. BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
  196. /* disable force PME-enable */
  197. writeb(WOLCFG_PMEOVR, &regs->WOLCFGClr);
  198. /* disable power-event config bit */
  199. writew(0xFFFF, &regs->WOLCRClr);
  200. /* clear power status */
  201. writew(0xFFFF, &regs->WOLSRClr);
  202. }
  203. static const struct ethtool_ops velocity_ethtool_ops;
  204. /*
  205. Define module options
  206. */
  207. MODULE_AUTHOR("VIA Networking Technologies, Inc.");
  208. MODULE_LICENSE("GPL");
  209. MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
  210. #define VELOCITY_PARAM(N, D) \
  211. static int N[MAX_UNITS] = OPTION_DEFAULT;\
  212. module_param_array(N, int, NULL, 0); \
  213. MODULE_PARM_DESC(N, D);
  214. #define RX_DESC_MIN 64
  215. #define RX_DESC_MAX 255
  216. #define RX_DESC_DEF 64
  217. VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
  218. #define TX_DESC_MIN 16
  219. #define TX_DESC_MAX 256
  220. #define TX_DESC_DEF 64
  221. VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
  222. #define RX_THRESH_MIN 0
  223. #define RX_THRESH_MAX 3
  224. #define RX_THRESH_DEF 0
  225. /* rx_thresh[] is used for controlling the receive fifo threshold.
  226. 0: indicate the rxfifo threshold is 128 bytes.
  227. 1: indicate the rxfifo threshold is 512 bytes.
  228. 2: indicate the rxfifo threshold is 1024 bytes.
  229. 3: indicate the rxfifo threshold is store & forward.
  230. */
  231. VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
  232. #define DMA_LENGTH_MIN 0
  233. #define DMA_LENGTH_MAX 7
  234. #define DMA_LENGTH_DEF 6
  235. /* DMA_length[] is used for controlling the DMA length
  236. 0: 8 DWORDs
  237. 1: 16 DWORDs
  238. 2: 32 DWORDs
  239. 3: 64 DWORDs
  240. 4: 128 DWORDs
  241. 5: 256 DWORDs
  242. 6: SF(flush till emply)
  243. 7: SF(flush till emply)
  244. */
  245. VELOCITY_PARAM(DMA_length, "DMA length");
  246. #define IP_ALIG_DEF 0
  247. /* IP_byte_align[] is used for IP header DWORD byte aligned
  248. 0: indicate the IP header won't be DWORD byte aligned.(Default) .
  249. 1: indicate the IP header will be DWORD byte aligned.
  250. In some environment, the IP header should be DWORD byte aligned,
  251. or the packet will be droped when we receive it. (eg: IPVS)
  252. */
  253. VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
  254. #define FLOW_CNTL_DEF 1
  255. #define FLOW_CNTL_MIN 1
  256. #define FLOW_CNTL_MAX 5
  257. /* flow_control[] is used for setting the flow control ability of NIC.
  258. 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
  259. 2: enable TX flow control.
  260. 3: enable RX flow control.
  261. 4: enable RX/TX flow control.
  262. 5: disable
  263. */
  264. VELOCITY_PARAM(flow_control, "Enable flow control ability");
  265. #define MED_LNK_DEF 0
  266. #define MED_LNK_MIN 0
  267. #define MED_LNK_MAX 5
  268. /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
  269. 0: indicate autonegotiation for both speed and duplex mode
  270. 1: indicate 100Mbps half duplex mode
  271. 2: indicate 100Mbps full duplex mode
  272. 3: indicate 10Mbps half duplex mode
  273. 4: indicate 10Mbps full duplex mode
  274. 5: indicate 1000Mbps full duplex mode
  275. Note:
  276. if EEPROM have been set to the force mode, this option is ignored
  277. by driver.
  278. */
  279. VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
  280. #define WOL_OPT_DEF 0
  281. #define WOL_OPT_MIN 0
  282. #define WOL_OPT_MAX 7
  283. /* wol_opts[] is used for controlling wake on lan behavior.
  284. 0: Wake up if recevied a magic packet. (Default)
  285. 1: Wake up if link status is on/off.
  286. 2: Wake up if recevied an arp packet.
  287. 4: Wake up if recevied any unicast packet.
  288. Those value can be sumed up to support more than one option.
  289. */
  290. VELOCITY_PARAM(wol_opts, "Wake On Lan options");
  291. static int rx_copybreak = 200;
  292. module_param(rx_copybreak, int, 0644);
  293. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
  294. /*
  295. * Internal board variants. At the moment we have only one
  296. */
  297. static struct velocity_info_tbl chip_info_table[] = {
  298. {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
  299. { }
  300. };
  301. /*
  302. * Describe the PCI device identifiers that we support in this
  303. * device driver. Used for hotplug autoloading.
  304. */
  305. static const struct pci_device_id velocity_pci_id_table[] = {
  306. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
  307. { }
  308. };
  309. MODULE_DEVICE_TABLE(pci, velocity_pci_id_table);
  310. /**
  311. * Describe the OF device identifiers that we support in this
  312. * device driver. Used for devicetree nodes.
  313. */
  314. static const struct of_device_id velocity_of_ids[] = {
  315. { .compatible = "via,velocity-vt6110", .data = &chip_info_table[0] },
  316. { /* Sentinel */ },
  317. };
  318. MODULE_DEVICE_TABLE(of, velocity_of_ids);
  319. /**
  320. * get_chip_name - identifier to name
  321. * @id: chip identifier
  322. *
  323. * Given a chip identifier return a suitable description. Returns
  324. * a pointer a static string valid while the driver is loaded.
  325. */
  326. static const char *get_chip_name(enum chip_type chip_id)
  327. {
  328. int i;
  329. for (i = 0; chip_info_table[i].name != NULL; i++)
  330. if (chip_info_table[i].chip_id == chip_id)
  331. break;
  332. return chip_info_table[i].name;
  333. }
  334. /**
  335. * velocity_set_int_opt - parser for integer options
  336. * @opt: pointer to option value
  337. * @val: value the user requested (or -1 for default)
  338. * @min: lowest value allowed
  339. * @max: highest value allowed
  340. * @def: default value
  341. * @name: property name
  342. * @dev: device name
  343. *
  344. * Set an integer property in the module options. This function does
  345. * all the verification and checking as well as reporting so that
  346. * we don't duplicate code for each option.
  347. */
  348. static void velocity_set_int_opt(int *opt, int val, int min, int max, int def,
  349. char *name, const char *devname)
  350. {
  351. if (val == -1)
  352. *opt = def;
  353. else if (val < min || val > max) {
  354. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
  355. devname, name, min, max);
  356. *opt = def;
  357. } else {
  358. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
  359. devname, name, val);
  360. *opt = val;
  361. }
  362. }
  363. /**
  364. * velocity_set_bool_opt - parser for boolean options
  365. * @opt: pointer to option value
  366. * @val: value the user requested (or -1 for default)
  367. * @def: default value (yes/no)
  368. * @flag: numeric value to set for true.
  369. * @name: property name
  370. * @dev: device name
  371. *
  372. * Set a boolean property in the module options. This function does
  373. * all the verification and checking as well as reporting so that
  374. * we don't duplicate code for each option.
  375. */
  376. static void velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag,
  377. char *name, const char *devname)
  378. {
  379. (*opt) &= (~flag);
  380. if (val == -1)
  381. *opt |= (def ? flag : 0);
  382. else if (val < 0 || val > 1) {
  383. printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
  384. devname, name);
  385. *opt |= (def ? flag : 0);
  386. } else {
  387. printk(KERN_INFO "%s: set parameter %s to %s\n",
  388. devname, name, val ? "TRUE" : "FALSE");
  389. *opt |= (val ? flag : 0);
  390. }
  391. }
  392. /**
  393. * velocity_get_options - set options on device
  394. * @opts: option structure for the device
  395. * @index: index of option to use in module options array
  396. * @devname: device name
  397. *
  398. * Turn the module and command options into a single structure
  399. * for the current device
  400. */
  401. static void velocity_get_options(struct velocity_opt *opts, int index,
  402. const char *devname)
  403. {
  404. velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
  405. velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
  406. velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
  407. velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
  408. velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
  409. velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
  410. velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
  411. velocity_set_int_opt(&opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
  412. opts->numrx = (opts->numrx & ~3);
  413. }
  414. /**
  415. * velocity_init_cam_filter - initialise CAM
  416. * @vptr: velocity to program
  417. *
  418. * Initialize the content addressable memory used for filters. Load
  419. * appropriately according to the presence of VLAN
  420. */
  421. static void velocity_init_cam_filter(struct velocity_info *vptr)
  422. {
  423. struct mac_regs __iomem *regs = vptr->mac_regs;
  424. unsigned int vid, i = 0;
  425. /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
  426. WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
  427. WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);
  428. /* Disable all CAMs */
  429. memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
  430. memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
  431. mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
  432. mac_set_cam_mask(regs, vptr->mCAMmask);
  433. /* Enable VCAMs */
  434. for_each_set_bit(vid, vptr->active_vlans, VLAN_N_VID) {
  435. mac_set_vlan_cam(regs, i, (u8 *) &vid);
  436. vptr->vCAMmask[i / 8] |= 0x1 << (i % 8);
  437. if (++i >= VCAM_SIZE)
  438. break;
  439. }
  440. mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
  441. }
  442. static int velocity_vlan_rx_add_vid(struct net_device *dev,
  443. __be16 proto, u16 vid)
  444. {
  445. struct velocity_info *vptr = netdev_priv(dev);
  446. spin_lock_irq(&vptr->lock);
  447. set_bit(vid, vptr->active_vlans);
  448. velocity_init_cam_filter(vptr);
  449. spin_unlock_irq(&vptr->lock);
  450. return 0;
  451. }
  452. static int velocity_vlan_rx_kill_vid(struct net_device *dev,
  453. __be16 proto, u16 vid)
  454. {
  455. struct velocity_info *vptr = netdev_priv(dev);
  456. spin_lock_irq(&vptr->lock);
  457. clear_bit(vid, vptr->active_vlans);
  458. velocity_init_cam_filter(vptr);
  459. spin_unlock_irq(&vptr->lock);
  460. return 0;
  461. }
  462. static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
  463. {
  464. vptr->rx.dirty = vptr->rx.filled = vptr->rx.curr = 0;
  465. }
  466. /**
  467. * velocity_rx_reset - handle a receive reset
  468. * @vptr: velocity we are resetting
  469. *
  470. * Reset the ownership and status for the receive ring side.
  471. * Hand all the receive queue to the NIC.
  472. */
  473. static void velocity_rx_reset(struct velocity_info *vptr)
  474. {
  475. struct mac_regs __iomem *regs = vptr->mac_regs;
  476. int i;
  477. velocity_init_rx_ring_indexes(vptr);
  478. /*
  479. * Init state, all RD entries belong to the NIC
  480. */
  481. for (i = 0; i < vptr->options.numrx; ++i)
  482. vptr->rx.ring[i].rdesc0.len |= OWNED_BY_NIC;
  483. writew(vptr->options.numrx, &regs->RBRDU);
  484. writel(vptr->rx.pool_dma, &regs->RDBaseLo);
  485. writew(0, &regs->RDIdx);
  486. writew(vptr->options.numrx - 1, &regs->RDCSize);
  487. }
  488. /**
  489. * velocity_get_opt_media_mode - get media selection
  490. * @vptr: velocity adapter
  491. *
  492. * Get the media mode stored in EEPROM or module options and load
  493. * mii_status accordingly. The requested link state information
  494. * is also returned.
  495. */
  496. static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
  497. {
  498. u32 status = 0;
  499. switch (vptr->options.spd_dpx) {
  500. case SPD_DPX_AUTO:
  501. status = VELOCITY_AUTONEG_ENABLE;
  502. break;
  503. case SPD_DPX_100_FULL:
  504. status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
  505. break;
  506. case SPD_DPX_10_FULL:
  507. status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
  508. break;
  509. case SPD_DPX_100_HALF:
  510. status = VELOCITY_SPEED_100;
  511. break;
  512. case SPD_DPX_10_HALF:
  513. status = VELOCITY_SPEED_10;
  514. break;
  515. case SPD_DPX_1000_FULL:
  516. status = VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
  517. break;
  518. }
  519. vptr->mii_status = status;
  520. return status;
  521. }
  522. /**
  523. * safe_disable_mii_autopoll - autopoll off
  524. * @regs: velocity registers
  525. *
  526. * Turn off the autopoll and wait for it to disable on the chip
  527. */
  528. static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
  529. {
  530. u16 ww;
  531. /* turn off MAUTO */
  532. writeb(0, &regs->MIICR);
  533. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  534. udelay(1);
  535. if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  536. break;
  537. }
  538. }
  539. /**
  540. * enable_mii_autopoll - turn on autopolling
  541. * @regs: velocity registers
  542. *
  543. * Enable the MII link status autopoll feature on the Velocity
  544. * hardware. Wait for it to enable.
  545. */
  546. static void enable_mii_autopoll(struct mac_regs __iomem *regs)
  547. {
  548. int ii;
  549. writeb(0, &(regs->MIICR));
  550. writeb(MIIADR_SWMPL, &regs->MIIADR);
  551. for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
  552. udelay(1);
  553. if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  554. break;
  555. }
  556. writeb(MIICR_MAUTO, &regs->MIICR);
  557. for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
  558. udelay(1);
  559. if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  560. break;
  561. }
  562. }
  563. /**
  564. * velocity_mii_read - read MII data
  565. * @regs: velocity registers
  566. * @index: MII register index
  567. * @data: buffer for received data
  568. *
  569. * Perform a single read of an MII 16bit register. Returns zero
  570. * on success or -ETIMEDOUT if the PHY did not respond.
  571. */
  572. static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
  573. {
  574. u16 ww;
  575. /*
  576. * Disable MIICR_MAUTO, so that mii addr can be set normally
  577. */
  578. safe_disable_mii_autopoll(regs);
  579. writeb(index, &regs->MIIADR);
  580. BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
  581. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  582. if (!(readb(&regs->MIICR) & MIICR_RCMD))
  583. break;
  584. }
  585. *data = readw(&regs->MIIDATA);
  586. enable_mii_autopoll(regs);
  587. if (ww == W_MAX_TIMEOUT)
  588. return -ETIMEDOUT;
  589. return 0;
  590. }
  591. /**
  592. * mii_check_media_mode - check media state
  593. * @regs: velocity registers
  594. *
  595. * Check the current MII status and determine the link status
  596. * accordingly
  597. */
  598. static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
  599. {
  600. u32 status = 0;
  601. u16 ANAR;
  602. if (!MII_REG_BITS_IS_ON(BMSR_LSTATUS, MII_BMSR, regs))
  603. status |= VELOCITY_LINK_FAIL;
  604. if (MII_REG_BITS_IS_ON(ADVERTISE_1000FULL, MII_CTRL1000, regs))
  605. status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
  606. else if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF, MII_CTRL1000, regs))
  607. status |= (VELOCITY_SPEED_1000);
  608. else {
  609. velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
  610. if (ANAR & ADVERTISE_100FULL)
  611. status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
  612. else if (ANAR & ADVERTISE_100HALF)
  613. status |= VELOCITY_SPEED_100;
  614. else if (ANAR & ADVERTISE_10FULL)
  615. status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
  616. else
  617. status |= (VELOCITY_SPEED_10);
  618. }
  619. if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
  620. velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
  621. if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
  622. == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
  623. if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
  624. status |= VELOCITY_AUTONEG_ENABLE;
  625. }
  626. }
  627. return status;
  628. }
  629. /**
  630. * velocity_mii_write - write MII data
  631. * @regs: velocity registers
  632. * @index: MII register index
  633. * @data: 16bit data for the MII register
  634. *
  635. * Perform a single write to an MII 16bit register. Returns zero
  636. * on success or -ETIMEDOUT if the PHY did not respond.
  637. */
  638. static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
  639. {
  640. u16 ww;
  641. /*
  642. * Disable MIICR_MAUTO, so that mii addr can be set normally
  643. */
  644. safe_disable_mii_autopoll(regs);
  645. /* MII reg offset */
  646. writeb(mii_addr, &regs->MIIADR);
  647. /* set MII data */
  648. writew(data, &regs->MIIDATA);
  649. /* turn on MIICR_WCMD */
  650. BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
  651. /* W_MAX_TIMEOUT is the timeout period */
  652. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  653. udelay(5);
  654. if (!(readb(&regs->MIICR) & MIICR_WCMD))
  655. break;
  656. }
  657. enable_mii_autopoll(regs);
  658. if (ww == W_MAX_TIMEOUT)
  659. return -ETIMEDOUT;
  660. return 0;
  661. }
  662. /**
  663. * set_mii_flow_control - flow control setup
  664. * @vptr: velocity interface
  665. *
  666. * Set up the flow control on this interface according to
  667. * the supplied user/eeprom options.
  668. */
  669. static void set_mii_flow_control(struct velocity_info *vptr)
  670. {
  671. /*Enable or Disable PAUSE in ANAR */
  672. switch (vptr->options.flow_cntl) {
  673. case FLOW_CNTL_TX:
  674. MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
  675. MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
  676. break;
  677. case FLOW_CNTL_RX:
  678. MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
  679. MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
  680. break;
  681. case FLOW_CNTL_TX_RX:
  682. MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
  683. MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
  684. break;
  685. case FLOW_CNTL_DISABLE:
  686. MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
  687. MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
  688. break;
  689. default:
  690. break;
  691. }
  692. }
  693. /**
  694. * mii_set_auto_on - autonegotiate on
  695. * @vptr: velocity
  696. *
  697. * Enable autonegotation on this interface
  698. */
  699. static void mii_set_auto_on(struct velocity_info *vptr)
  700. {
  701. if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs))
  702. MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
  703. else
  704. MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs);
  705. }
  706. static u32 check_connection_type(struct mac_regs __iomem *regs)
  707. {
  708. u32 status = 0;
  709. u8 PHYSR0;
  710. u16 ANAR;
  711. PHYSR0 = readb(&regs->PHYSR0);
  712. /*
  713. if (!(PHYSR0 & PHYSR0_LINKGD))
  714. status|=VELOCITY_LINK_FAIL;
  715. */
  716. if (PHYSR0 & PHYSR0_FDPX)
  717. status |= VELOCITY_DUPLEX_FULL;
  718. if (PHYSR0 & PHYSR0_SPDG)
  719. status |= VELOCITY_SPEED_1000;
  720. else if (PHYSR0 & PHYSR0_SPD10)
  721. status |= VELOCITY_SPEED_10;
  722. else
  723. status |= VELOCITY_SPEED_100;
  724. if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
  725. velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
  726. if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
  727. == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
  728. if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
  729. status |= VELOCITY_AUTONEG_ENABLE;
  730. }
  731. }
  732. return status;
  733. }
  734. /**
  735. * velocity_set_media_mode - set media mode
  736. * @mii_status: old MII link state
  737. *
  738. * Check the media link state and configure the flow control
  739. * PHY and also velocity hardware setup accordingly. In particular
  740. * we need to set up CD polling and frame bursting.
  741. */
  742. static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
  743. {
  744. struct mac_regs __iomem *regs = vptr->mac_regs;
  745. vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
  746. /* Set mii link status */
  747. set_mii_flow_control(vptr);
  748. if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
  749. MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
  750. /*
  751. * If connection type is AUTO
  752. */
  753. if (mii_status & VELOCITY_AUTONEG_ENABLE) {
  754. VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
  755. /* clear force MAC mode bit */
  756. BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
  757. /* set duplex mode of MAC according to duplex mode of MII */
  758. MII_REG_BITS_ON(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF, MII_ADVERTISE, vptr->mac_regs);
  759. MII_REG_BITS_ON(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
  760. MII_REG_BITS_ON(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs);
  761. /* enable AUTO-NEGO mode */
  762. mii_set_auto_on(vptr);
  763. } else {
  764. u16 CTRL1000;
  765. u16 ANAR;
  766. u8 CHIPGCR;
  767. /*
  768. * 1. if it's 3119, disable frame bursting in halfduplex mode
  769. * and enable it in fullduplex mode
  770. * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
  771. * 3. only enable CD heart beat counter in 10HD mode
  772. */
  773. /* set force MAC mode bit */
  774. BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
  775. CHIPGCR = readb(&regs->CHIPGCR);
  776. if (mii_status & VELOCITY_SPEED_1000)
  777. CHIPGCR |= CHIPGCR_FCGMII;
  778. else
  779. CHIPGCR &= ~CHIPGCR_FCGMII;
  780. if (mii_status & VELOCITY_DUPLEX_FULL) {
  781. CHIPGCR |= CHIPGCR_FCFDX;
  782. writeb(CHIPGCR, &regs->CHIPGCR);
  783. VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
  784. if (vptr->rev_id < REV_ID_VT3216_A0)
  785. BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
  786. } else {
  787. CHIPGCR &= ~CHIPGCR_FCFDX;
  788. VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
  789. writeb(CHIPGCR, &regs->CHIPGCR);
  790. if (vptr->rev_id < REV_ID_VT3216_A0)
  791. BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
  792. }
  793. velocity_mii_read(vptr->mac_regs, MII_CTRL1000, &CTRL1000);
  794. CTRL1000 &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  795. if ((mii_status & VELOCITY_SPEED_1000) &&
  796. (mii_status & VELOCITY_DUPLEX_FULL)) {
  797. CTRL1000 |= ADVERTISE_1000FULL;
  798. }
  799. velocity_mii_write(vptr->mac_regs, MII_CTRL1000, CTRL1000);
  800. if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
  801. BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
  802. else
  803. BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
  804. /* MII_REG_BITS_OFF(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs); */
  805. velocity_mii_read(vptr->mac_regs, MII_ADVERTISE, &ANAR);
  806. ANAR &= (~(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF));
  807. if (mii_status & VELOCITY_SPEED_100) {
  808. if (mii_status & VELOCITY_DUPLEX_FULL)
  809. ANAR |= ADVERTISE_100FULL;
  810. else
  811. ANAR |= ADVERTISE_100HALF;
  812. } else if (mii_status & VELOCITY_SPEED_10) {
  813. if (mii_status & VELOCITY_DUPLEX_FULL)
  814. ANAR |= ADVERTISE_10FULL;
  815. else
  816. ANAR |= ADVERTISE_10HALF;
  817. }
  818. velocity_mii_write(vptr->mac_regs, MII_ADVERTISE, ANAR);
  819. /* enable AUTO-NEGO mode */
  820. mii_set_auto_on(vptr);
  821. /* MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs); */
  822. }
  823. /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
  824. /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
  825. return VELOCITY_LINK_CHANGE;
  826. }
  827. /**
  828. * velocity_print_link_status - link status reporting
  829. * @vptr: velocity to report on
  830. *
  831. * Turn the link status of the velocity card into a kernel log
  832. * description of the new link state, detailing speed and duplex
  833. * status
  834. */
  835. static void velocity_print_link_status(struct velocity_info *vptr)
  836. {
  837. if (vptr->mii_status & VELOCITY_LINK_FAIL) {
  838. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->netdev->name);
  839. } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
  840. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->netdev->name);
  841. if (vptr->mii_status & VELOCITY_SPEED_1000)
  842. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
  843. else if (vptr->mii_status & VELOCITY_SPEED_100)
  844. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
  845. else
  846. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
  847. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  848. VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
  849. else
  850. VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
  851. } else {
  852. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->netdev->name);
  853. switch (vptr->options.spd_dpx) {
  854. case SPD_DPX_1000_FULL:
  855. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps full duplex\n");
  856. break;
  857. case SPD_DPX_100_HALF:
  858. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
  859. break;
  860. case SPD_DPX_100_FULL:
  861. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
  862. break;
  863. case SPD_DPX_10_HALF:
  864. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
  865. break;
  866. case SPD_DPX_10_FULL:
  867. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
  868. break;
  869. default:
  870. break;
  871. }
  872. }
  873. }
  874. /**
  875. * enable_flow_control_ability - flow control
  876. * @vptr: veloity to configure
  877. *
  878. * Set up flow control according to the flow control options
  879. * determined by the eeprom/configuration.
  880. */
  881. static void enable_flow_control_ability(struct velocity_info *vptr)
  882. {
  883. struct mac_regs __iomem *regs = vptr->mac_regs;
  884. switch (vptr->options.flow_cntl) {
  885. case FLOW_CNTL_DEFAULT:
  886. if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
  887. writel(CR0_FDXRFCEN, &regs->CR0Set);
  888. else
  889. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  890. if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
  891. writel(CR0_FDXTFCEN, &regs->CR0Set);
  892. else
  893. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  894. break;
  895. case FLOW_CNTL_TX:
  896. writel(CR0_FDXTFCEN, &regs->CR0Set);
  897. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  898. break;
  899. case FLOW_CNTL_RX:
  900. writel(CR0_FDXRFCEN, &regs->CR0Set);
  901. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  902. break;
  903. case FLOW_CNTL_TX_RX:
  904. writel(CR0_FDXTFCEN, &regs->CR0Set);
  905. writel(CR0_FDXRFCEN, &regs->CR0Set);
  906. break;
  907. case FLOW_CNTL_DISABLE:
  908. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  909. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  910. break;
  911. default:
  912. break;
  913. }
  914. }
  915. /**
  916. * velocity_soft_reset - soft reset
  917. * @vptr: velocity to reset
  918. *
  919. * Kick off a soft reset of the velocity adapter and then poll
  920. * until the reset sequence has completed before returning.
  921. */
  922. static int velocity_soft_reset(struct velocity_info *vptr)
  923. {
  924. struct mac_regs __iomem *regs = vptr->mac_regs;
  925. int i = 0;
  926. writel(CR0_SFRST, &regs->CR0Set);
  927. for (i = 0; i < W_MAX_TIMEOUT; i++) {
  928. udelay(5);
  929. if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
  930. break;
  931. }
  932. if (i == W_MAX_TIMEOUT) {
  933. writel(CR0_FORSRST, &regs->CR0Set);
  934. /* FIXME: PCI POSTING */
  935. /* delay 2ms */
  936. mdelay(2);
  937. }
  938. return 0;
  939. }
  940. /**
  941. * velocity_set_multi - filter list change callback
  942. * @dev: network device
  943. *
  944. * Called by the network layer when the filter lists need to change
  945. * for a velocity adapter. Reload the CAMs with the new address
  946. * filter ruleset.
  947. */
  948. static void velocity_set_multi(struct net_device *dev)
  949. {
  950. struct velocity_info *vptr = netdev_priv(dev);
  951. struct mac_regs __iomem *regs = vptr->mac_regs;
  952. u8 rx_mode;
  953. int i;
  954. struct netdev_hw_addr *ha;
  955. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  956. writel(0xffffffff, &regs->MARCAM[0]);
  957. writel(0xffffffff, &regs->MARCAM[4]);
  958. rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
  959. } else if ((netdev_mc_count(dev) > vptr->multicast_limit) ||
  960. (dev->flags & IFF_ALLMULTI)) {
  961. writel(0xffffffff, &regs->MARCAM[0]);
  962. writel(0xffffffff, &regs->MARCAM[4]);
  963. rx_mode = (RCR_AM | RCR_AB);
  964. } else {
  965. int offset = MCAM_SIZE - vptr->multicast_limit;
  966. mac_get_cam_mask(regs, vptr->mCAMmask);
  967. i = 0;
  968. netdev_for_each_mc_addr(ha, dev) {
  969. mac_set_cam(regs, i + offset, ha->addr);
  970. vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
  971. i++;
  972. }
  973. mac_set_cam_mask(regs, vptr->mCAMmask);
  974. rx_mode = RCR_AM | RCR_AB | RCR_AP;
  975. }
  976. if (dev->mtu > 1500)
  977. rx_mode |= RCR_AL;
  978. BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
  979. }
  980. /*
  981. * MII access , media link mode setting functions
  982. */
  983. /**
  984. * mii_init - set up MII
  985. * @vptr: velocity adapter
  986. * @mii_status: links tatus
  987. *
  988. * Set up the PHY for the current link state.
  989. */
  990. static void mii_init(struct velocity_info *vptr, u32 mii_status)
  991. {
  992. u16 BMCR;
  993. switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
  994. case PHYID_ICPLUS_IP101A:
  995. MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP),
  996. MII_ADVERTISE, vptr->mac_regs);
  997. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  998. MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION,
  999. vptr->mac_regs);
  1000. else
  1001. MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION,
  1002. vptr->mac_regs);
  1003. MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
  1004. break;
  1005. case PHYID_CICADA_CS8201:
  1006. /*
  1007. * Reset to hardware default
  1008. */
  1009. MII_REG_BITS_OFF((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
  1010. /*
  1011. * Turn on ECHODIS bit in NWay-forced full mode and turn it
  1012. * off it in NWay-forced half mode for NWay-forced v.s.
  1013. * legacy-forced issue.
  1014. */
  1015. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  1016. MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
  1017. else
  1018. MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
  1019. /*
  1020. * Turn on Link/Activity LED enable bit for CIS8201
  1021. */
  1022. MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
  1023. break;
  1024. case PHYID_VT3216_32BIT:
  1025. case PHYID_VT3216_64BIT:
  1026. /*
  1027. * Reset to hardware default
  1028. */
  1029. MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
  1030. /*
  1031. * Turn on ECHODIS bit in NWay-forced full mode and turn it
  1032. * off it in NWay-forced half mode for NWay-forced v.s.
  1033. * legacy-forced issue
  1034. */
  1035. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  1036. MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
  1037. else
  1038. MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
  1039. break;
  1040. case PHYID_MARVELL_1000:
  1041. case PHYID_MARVELL_1000S:
  1042. /*
  1043. * Assert CRS on Transmit
  1044. */
  1045. MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
  1046. /*
  1047. * Reset to hardware default
  1048. */
  1049. MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
  1050. break;
  1051. default:
  1052. ;
  1053. }
  1054. velocity_mii_read(vptr->mac_regs, MII_BMCR, &BMCR);
  1055. if (BMCR & BMCR_ISOLATE) {
  1056. BMCR &= ~BMCR_ISOLATE;
  1057. velocity_mii_write(vptr->mac_regs, MII_BMCR, BMCR);
  1058. }
  1059. }
  1060. /**
  1061. * setup_queue_timers - Setup interrupt timers
  1062. *
  1063. * Setup interrupt frequency during suppression (timeout if the frame
  1064. * count isn't filled).
  1065. */
  1066. static void setup_queue_timers(struct velocity_info *vptr)
  1067. {
  1068. /* Only for newer revisions */
  1069. if (vptr->rev_id >= REV_ID_VT3216_A0) {
  1070. u8 txqueue_timer = 0;
  1071. u8 rxqueue_timer = 0;
  1072. if (vptr->mii_status & (VELOCITY_SPEED_1000 |
  1073. VELOCITY_SPEED_100)) {
  1074. txqueue_timer = vptr->options.txqueue_timer;
  1075. rxqueue_timer = vptr->options.rxqueue_timer;
  1076. }
  1077. writeb(txqueue_timer, &vptr->mac_regs->TQETMR);
  1078. writeb(rxqueue_timer, &vptr->mac_regs->RQETMR);
  1079. }
  1080. }
  1081. /**
  1082. * setup_adaptive_interrupts - Setup interrupt suppression
  1083. *
  1084. * @vptr velocity adapter
  1085. *
  1086. * The velocity is able to suppress interrupt during high interrupt load.
  1087. * This function turns on that feature.
  1088. */
  1089. static void setup_adaptive_interrupts(struct velocity_info *vptr)
  1090. {
  1091. struct mac_regs __iomem *regs = vptr->mac_regs;
  1092. u16 tx_intsup = vptr->options.tx_intsup;
  1093. u16 rx_intsup = vptr->options.rx_intsup;
  1094. /* Setup default interrupt mask (will be changed below) */
  1095. vptr->int_mask = INT_MASK_DEF;
  1096. /* Set Tx Interrupt Suppression Threshold */
  1097. writeb(CAMCR_PS0, &regs->CAMCR);
  1098. if (tx_intsup != 0) {
  1099. vptr->int_mask &= ~(ISR_PTXI | ISR_PTX0I | ISR_PTX1I |
  1100. ISR_PTX2I | ISR_PTX3I);
  1101. writew(tx_intsup, &regs->ISRCTL);
  1102. } else
  1103. writew(ISRCTL_TSUPDIS, &regs->ISRCTL);
  1104. /* Set Rx Interrupt Suppression Threshold */
  1105. writeb(CAMCR_PS1, &regs->CAMCR);
  1106. if (rx_intsup != 0) {
  1107. vptr->int_mask &= ~ISR_PRXI;
  1108. writew(rx_intsup, &regs->ISRCTL);
  1109. } else
  1110. writew(ISRCTL_RSUPDIS, &regs->ISRCTL);
  1111. /* Select page to interrupt hold timer */
  1112. writeb(0, &regs->CAMCR);
  1113. }
  1114. /**
  1115. * velocity_init_registers - initialise MAC registers
  1116. * @vptr: velocity to init
  1117. * @type: type of initialisation (hot or cold)
  1118. *
  1119. * Initialise the MAC on a reset or on first set up on the
  1120. * hardware.
  1121. */
  1122. static void velocity_init_registers(struct velocity_info *vptr,
  1123. enum velocity_init_type type)
  1124. {
  1125. struct mac_regs __iomem *regs = vptr->mac_regs;
  1126. struct net_device *netdev = vptr->netdev;
  1127. int i, mii_status;
  1128. mac_wol_reset(regs);
  1129. switch (type) {
  1130. case VELOCITY_INIT_RESET:
  1131. case VELOCITY_INIT_WOL:
  1132. netif_stop_queue(netdev);
  1133. /*
  1134. * Reset RX to prevent RX pointer not on the 4X location
  1135. */
  1136. velocity_rx_reset(vptr);
  1137. mac_rx_queue_run(regs);
  1138. mac_rx_queue_wake(regs);
  1139. mii_status = velocity_get_opt_media_mode(vptr);
  1140. if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
  1141. velocity_print_link_status(vptr);
  1142. if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
  1143. netif_wake_queue(netdev);
  1144. }
  1145. enable_flow_control_ability(vptr);
  1146. mac_clear_isr(regs);
  1147. writel(CR0_STOP, &regs->CR0Clr);
  1148. writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
  1149. &regs->CR0Set);
  1150. break;
  1151. case VELOCITY_INIT_COLD:
  1152. default:
  1153. /*
  1154. * Do reset
  1155. */
  1156. velocity_soft_reset(vptr);
  1157. mdelay(5);
  1158. if (!vptr->no_eeprom) {
  1159. mac_eeprom_reload(regs);
  1160. for (i = 0; i < 6; i++)
  1161. writeb(netdev->dev_addr[i], regs->PAR + i);
  1162. }
  1163. /*
  1164. * clear Pre_ACPI bit.
  1165. */
  1166. BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
  1167. mac_set_rx_thresh(regs, vptr->options.rx_thresh);
  1168. mac_set_dma_length(regs, vptr->options.DMA_length);
  1169. writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
  1170. /*
  1171. * Back off algorithm use original IEEE standard
  1172. */
  1173. BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), &regs->CFGB);
  1174. /*
  1175. * Init CAM filter
  1176. */
  1177. velocity_init_cam_filter(vptr);
  1178. /*
  1179. * Set packet filter: Receive directed and broadcast address
  1180. */
  1181. velocity_set_multi(netdev);
  1182. /*
  1183. * Enable MII auto-polling
  1184. */
  1185. enable_mii_autopoll(regs);
  1186. setup_adaptive_interrupts(vptr);
  1187. writel(vptr->rx.pool_dma, &regs->RDBaseLo);
  1188. writew(vptr->options.numrx - 1, &regs->RDCSize);
  1189. mac_rx_queue_run(regs);
  1190. mac_rx_queue_wake(regs);
  1191. writew(vptr->options.numtx - 1, &regs->TDCSize);
  1192. for (i = 0; i < vptr->tx.numq; i++) {
  1193. writel(vptr->tx.pool_dma[i], &regs->TDBaseLo[i]);
  1194. mac_tx_queue_run(regs, i);
  1195. }
  1196. init_flow_control_register(vptr);
  1197. writel(CR0_STOP, &regs->CR0Clr);
  1198. writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), &regs->CR0Set);
  1199. mii_status = velocity_get_opt_media_mode(vptr);
  1200. netif_stop_queue(netdev);
  1201. mii_init(vptr, mii_status);
  1202. if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
  1203. velocity_print_link_status(vptr);
  1204. if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
  1205. netif_wake_queue(netdev);
  1206. }
  1207. enable_flow_control_ability(vptr);
  1208. mac_hw_mibs_init(regs);
  1209. mac_write_int_mask(vptr->int_mask, regs);
  1210. mac_clear_isr(regs);
  1211. }
  1212. }
  1213. static void velocity_give_many_rx_descs(struct velocity_info *vptr)
  1214. {
  1215. struct mac_regs __iomem *regs = vptr->mac_regs;
  1216. int avail, dirty, unusable;
  1217. /*
  1218. * RD number must be equal to 4X per hardware spec
  1219. * (programming guide rev 1.20, p.13)
  1220. */
  1221. if (vptr->rx.filled < 4)
  1222. return;
  1223. wmb();
  1224. unusable = vptr->rx.filled & 0x0003;
  1225. dirty = vptr->rx.dirty - unusable;
  1226. for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
  1227. dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
  1228. vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
  1229. }
  1230. writew(vptr->rx.filled & 0xfffc, &regs->RBRDU);
  1231. vptr->rx.filled = unusable;
  1232. }
  1233. /**
  1234. * velocity_init_dma_rings - set up DMA rings
  1235. * @vptr: Velocity to set up
  1236. *
  1237. * Allocate PCI mapped DMA rings for the receive and transmit layer
  1238. * to use.
  1239. */
  1240. static int velocity_init_dma_rings(struct velocity_info *vptr)
  1241. {
  1242. struct velocity_opt *opt = &vptr->options;
  1243. const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
  1244. const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
  1245. dma_addr_t pool_dma;
  1246. void *pool;
  1247. unsigned int i;
  1248. /*
  1249. * Allocate all RD/TD rings a single pool.
  1250. *
  1251. * dma_alloc_coherent() fulfills the requirement for 64 bytes
  1252. * alignment
  1253. */
  1254. pool = dma_alloc_coherent(vptr->dev, tx_ring_size * vptr->tx.numq +
  1255. rx_ring_size, &pool_dma, GFP_ATOMIC);
  1256. if (!pool) {
  1257. dev_err(vptr->dev, "%s : DMA memory allocation failed.\n",
  1258. vptr->netdev->name);
  1259. return -ENOMEM;
  1260. }
  1261. vptr->rx.ring = pool;
  1262. vptr->rx.pool_dma = pool_dma;
  1263. pool += rx_ring_size;
  1264. pool_dma += rx_ring_size;
  1265. for (i = 0; i < vptr->tx.numq; i++) {
  1266. vptr->tx.rings[i] = pool;
  1267. vptr->tx.pool_dma[i] = pool_dma;
  1268. pool += tx_ring_size;
  1269. pool_dma += tx_ring_size;
  1270. }
  1271. return 0;
  1272. }
  1273. static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
  1274. {
  1275. vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
  1276. }
  1277. /**
  1278. * velocity_alloc_rx_buf - allocate aligned receive buffer
  1279. * @vptr: velocity
  1280. * @idx: ring index
  1281. *
  1282. * Allocate a new full sized buffer for the reception of a frame and
  1283. * map it into PCI space for the hardware to use. The hardware
  1284. * requires *64* byte alignment of the buffer which makes life
  1285. * less fun than would be ideal.
  1286. */
  1287. static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
  1288. {
  1289. struct rx_desc *rd = &(vptr->rx.ring[idx]);
  1290. struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
  1291. rd_info->skb = netdev_alloc_skb(vptr->netdev, vptr->rx.buf_sz + 64);
  1292. if (rd_info->skb == NULL)
  1293. return -ENOMEM;
  1294. /*
  1295. * Do the gymnastics to get the buffer head for data at
  1296. * 64byte alignment.
  1297. */
  1298. skb_reserve(rd_info->skb,
  1299. 64 - ((unsigned long) rd_info->skb->data & 63));
  1300. rd_info->skb_dma = dma_map_single(vptr->dev, rd_info->skb->data,
  1301. vptr->rx.buf_sz, DMA_FROM_DEVICE);
  1302. /*
  1303. * Fill in the descriptor to match
  1304. */
  1305. *((u32 *) & (rd->rdesc0)) = 0;
  1306. rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
  1307. rd->pa_low = cpu_to_le32(rd_info->skb_dma);
  1308. rd->pa_high = 0;
  1309. return 0;
  1310. }
  1311. static int velocity_rx_refill(struct velocity_info *vptr)
  1312. {
  1313. int dirty = vptr->rx.dirty, done = 0;
  1314. do {
  1315. struct rx_desc *rd = vptr->rx.ring + dirty;
  1316. /* Fine for an all zero Rx desc at init time as well */
  1317. if (rd->rdesc0.len & OWNED_BY_NIC)
  1318. break;
  1319. if (!vptr->rx.info[dirty].skb) {
  1320. if (velocity_alloc_rx_buf(vptr, dirty) < 0)
  1321. break;
  1322. }
  1323. done++;
  1324. dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
  1325. } while (dirty != vptr->rx.curr);
  1326. if (done) {
  1327. vptr->rx.dirty = dirty;
  1328. vptr->rx.filled += done;
  1329. }
  1330. return done;
  1331. }
  1332. /**
  1333. * velocity_free_rd_ring - free receive ring
  1334. * @vptr: velocity to clean up
  1335. *
  1336. * Free the receive buffers for each ring slot and any
  1337. * attached socket buffers that need to go away.
  1338. */
  1339. static void velocity_free_rd_ring(struct velocity_info *vptr)
  1340. {
  1341. int i;
  1342. if (vptr->rx.info == NULL)
  1343. return;
  1344. for (i = 0; i < vptr->options.numrx; i++) {
  1345. struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
  1346. struct rx_desc *rd = vptr->rx.ring + i;
  1347. memset(rd, 0, sizeof(*rd));
  1348. if (!rd_info->skb)
  1349. continue;
  1350. dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
  1351. DMA_FROM_DEVICE);
  1352. rd_info->skb_dma = 0;
  1353. dev_kfree_skb(rd_info->skb);
  1354. rd_info->skb = NULL;
  1355. }
  1356. kfree(vptr->rx.info);
  1357. vptr->rx.info = NULL;
  1358. }
  1359. /**
  1360. * velocity_init_rd_ring - set up receive ring
  1361. * @vptr: velocity to configure
  1362. *
  1363. * Allocate and set up the receive buffers for each ring slot and
  1364. * assign them to the network adapter.
  1365. */
  1366. static int velocity_init_rd_ring(struct velocity_info *vptr)
  1367. {
  1368. int ret = -ENOMEM;
  1369. vptr->rx.info = kcalloc(vptr->options.numrx,
  1370. sizeof(struct velocity_rd_info), GFP_KERNEL);
  1371. if (!vptr->rx.info)
  1372. goto out;
  1373. velocity_init_rx_ring_indexes(vptr);
  1374. if (velocity_rx_refill(vptr) != vptr->options.numrx) {
  1375. VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
  1376. "%s: failed to allocate RX buffer.\n", vptr->netdev->name);
  1377. velocity_free_rd_ring(vptr);
  1378. goto out;
  1379. }
  1380. ret = 0;
  1381. out:
  1382. return ret;
  1383. }
  1384. /**
  1385. * velocity_init_td_ring - set up transmit ring
  1386. * @vptr: velocity
  1387. *
  1388. * Set up the transmit ring and chain the ring pointers together.
  1389. * Returns zero on success or a negative posix errno code for
  1390. * failure.
  1391. */
  1392. static int velocity_init_td_ring(struct velocity_info *vptr)
  1393. {
  1394. int j;
  1395. /* Init the TD ring entries */
  1396. for (j = 0; j < vptr->tx.numq; j++) {
  1397. vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
  1398. sizeof(struct velocity_td_info),
  1399. GFP_KERNEL);
  1400. if (!vptr->tx.infos[j]) {
  1401. while (--j >= 0)
  1402. kfree(vptr->tx.infos[j]);
  1403. return -ENOMEM;
  1404. }
  1405. vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
  1406. }
  1407. return 0;
  1408. }
  1409. /**
  1410. * velocity_free_dma_rings - free PCI ring pointers
  1411. * @vptr: Velocity to free from
  1412. *
  1413. * Clean up the PCI ring buffers allocated to this velocity.
  1414. */
  1415. static void velocity_free_dma_rings(struct velocity_info *vptr)
  1416. {
  1417. const int size = vptr->options.numrx * sizeof(struct rx_desc) +
  1418. vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
  1419. dma_free_coherent(vptr->dev, size, vptr->rx.ring, vptr->rx.pool_dma);
  1420. }
  1421. static int velocity_init_rings(struct velocity_info *vptr, int mtu)
  1422. {
  1423. int ret;
  1424. velocity_set_rxbufsize(vptr, mtu);
  1425. ret = velocity_init_dma_rings(vptr);
  1426. if (ret < 0)
  1427. goto out;
  1428. ret = velocity_init_rd_ring(vptr);
  1429. if (ret < 0)
  1430. goto err_free_dma_rings_0;
  1431. ret = velocity_init_td_ring(vptr);
  1432. if (ret < 0)
  1433. goto err_free_rd_ring_1;
  1434. out:
  1435. return ret;
  1436. err_free_rd_ring_1:
  1437. velocity_free_rd_ring(vptr);
  1438. err_free_dma_rings_0:
  1439. velocity_free_dma_rings(vptr);
  1440. goto out;
  1441. }
  1442. /**
  1443. * velocity_free_tx_buf - free transmit buffer
  1444. * @vptr: velocity
  1445. * @tdinfo: buffer
  1446. *
  1447. * Release an transmit buffer. If the buffer was preallocated then
  1448. * recycle it, if not then unmap the buffer.
  1449. */
  1450. static void velocity_free_tx_buf(struct velocity_info *vptr,
  1451. struct velocity_td_info *tdinfo, struct tx_desc *td)
  1452. {
  1453. struct sk_buff *skb = tdinfo->skb;
  1454. int i;
  1455. /*
  1456. * Don't unmap the pre-allocated tx_bufs
  1457. */
  1458. for (i = 0; i < tdinfo->nskb_dma; i++) {
  1459. size_t pktlen = max_t(size_t, skb->len, ETH_ZLEN);
  1460. /* For scatter-gather */
  1461. if (skb_shinfo(skb)->nr_frags > 0)
  1462. pktlen = max_t(size_t, pktlen,
  1463. td->td_buf[i].size & ~TD_QUEUE);
  1464. dma_unmap_single(vptr->dev, tdinfo->skb_dma[i],
  1465. le16_to_cpu(pktlen), DMA_TO_DEVICE);
  1466. }
  1467. dev_consume_skb_irq(skb);
  1468. tdinfo->skb = NULL;
  1469. }
  1470. /*
  1471. * FIXME: could we merge this with velocity_free_tx_buf ?
  1472. */
  1473. static void velocity_free_td_ring_entry(struct velocity_info *vptr,
  1474. int q, int n)
  1475. {
  1476. struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
  1477. int i;
  1478. if (td_info == NULL)
  1479. return;
  1480. if (td_info->skb) {
  1481. for (i = 0; i < td_info->nskb_dma; i++) {
  1482. if (td_info->skb_dma[i]) {
  1483. dma_unmap_single(vptr->dev, td_info->skb_dma[i],
  1484. td_info->skb->len, DMA_TO_DEVICE);
  1485. td_info->skb_dma[i] = 0;
  1486. }
  1487. }
  1488. dev_kfree_skb(td_info->skb);
  1489. td_info->skb = NULL;
  1490. }
  1491. }
  1492. /**
  1493. * velocity_free_td_ring - free td ring
  1494. * @vptr: velocity
  1495. *
  1496. * Free up the transmit ring for this particular velocity adapter.
  1497. * We free the ring contents but not the ring itself.
  1498. */
  1499. static void velocity_free_td_ring(struct velocity_info *vptr)
  1500. {
  1501. int i, j;
  1502. for (j = 0; j < vptr->tx.numq; j++) {
  1503. if (vptr->tx.infos[j] == NULL)
  1504. continue;
  1505. for (i = 0; i < vptr->options.numtx; i++)
  1506. velocity_free_td_ring_entry(vptr, j, i);
  1507. kfree(vptr->tx.infos[j]);
  1508. vptr->tx.infos[j] = NULL;
  1509. }
  1510. }
  1511. static void velocity_free_rings(struct velocity_info *vptr)
  1512. {
  1513. velocity_free_td_ring(vptr);
  1514. velocity_free_rd_ring(vptr);
  1515. velocity_free_dma_rings(vptr);
  1516. }
  1517. /**
  1518. * velocity_error - handle error from controller
  1519. * @vptr: velocity
  1520. * @status: card status
  1521. *
  1522. * Process an error report from the hardware and attempt to recover
  1523. * the card itself. At the moment we cannot recover from some
  1524. * theoretically impossible errors but this could be fixed using
  1525. * the pci_device_failed logic to bounce the hardware
  1526. *
  1527. */
  1528. static void velocity_error(struct velocity_info *vptr, int status)
  1529. {
  1530. if (status & ISR_TXSTLI) {
  1531. struct mac_regs __iomem *regs = vptr->mac_regs;
  1532. printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(&regs->TDIdx[0]));
  1533. BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
  1534. writew(TRDCSR_RUN, &regs->TDCSRClr);
  1535. netif_stop_queue(vptr->netdev);
  1536. /* FIXME: port over the pci_device_failed code and use it
  1537. here */
  1538. }
  1539. if (status & ISR_SRCI) {
  1540. struct mac_regs __iomem *regs = vptr->mac_regs;
  1541. int linked;
  1542. if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
  1543. vptr->mii_status = check_connection_type(regs);
  1544. /*
  1545. * If it is a 3119, disable frame bursting in
  1546. * halfduplex mode and enable it in fullduplex
  1547. * mode
  1548. */
  1549. if (vptr->rev_id < REV_ID_VT3216_A0) {
  1550. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  1551. BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
  1552. else
  1553. BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
  1554. }
  1555. /*
  1556. * Only enable CD heart beat counter in 10HD mode
  1557. */
  1558. if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
  1559. BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
  1560. else
  1561. BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
  1562. setup_queue_timers(vptr);
  1563. }
  1564. /*
  1565. * Get link status from PHYSR0
  1566. */
  1567. linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
  1568. if (linked) {
  1569. vptr->mii_status &= ~VELOCITY_LINK_FAIL;
  1570. netif_carrier_on(vptr->netdev);
  1571. } else {
  1572. vptr->mii_status |= VELOCITY_LINK_FAIL;
  1573. netif_carrier_off(vptr->netdev);
  1574. }
  1575. velocity_print_link_status(vptr);
  1576. enable_flow_control_ability(vptr);
  1577. /*
  1578. * Re-enable auto-polling because SRCI will disable
  1579. * auto-polling
  1580. */
  1581. enable_mii_autopoll(regs);
  1582. if (vptr->mii_status & VELOCITY_LINK_FAIL)
  1583. netif_stop_queue(vptr->netdev);
  1584. else
  1585. netif_wake_queue(vptr->netdev);
  1586. }
  1587. if (status & ISR_MIBFI)
  1588. velocity_update_hw_mibs(vptr);
  1589. if (status & ISR_LSTEI)
  1590. mac_rx_queue_wake(vptr->mac_regs);
  1591. }
  1592. /**
  1593. * tx_srv - transmit interrupt service
  1594. * @vptr; Velocity
  1595. *
  1596. * Scan the queues looking for transmitted packets that
  1597. * we can complete and clean up. Update any statistics as
  1598. * necessary/
  1599. */
  1600. static int velocity_tx_srv(struct velocity_info *vptr)
  1601. {
  1602. struct tx_desc *td;
  1603. int qnum;
  1604. int full = 0;
  1605. int idx;
  1606. int works = 0;
  1607. struct velocity_td_info *tdinfo;
  1608. struct net_device_stats *stats = &vptr->netdev->stats;
  1609. for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
  1610. for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
  1611. idx = (idx + 1) % vptr->options.numtx) {
  1612. /*
  1613. * Get Tx Descriptor
  1614. */
  1615. td = &(vptr->tx.rings[qnum][idx]);
  1616. tdinfo = &(vptr->tx.infos[qnum][idx]);
  1617. if (td->tdesc0.len & OWNED_BY_NIC)
  1618. break;
  1619. if ((works++ > 15))
  1620. break;
  1621. if (td->tdesc0.TSR & TSR0_TERR) {
  1622. stats->tx_errors++;
  1623. stats->tx_dropped++;
  1624. if (td->tdesc0.TSR & TSR0_CDH)
  1625. stats->tx_heartbeat_errors++;
  1626. if (td->tdesc0.TSR & TSR0_CRS)
  1627. stats->tx_carrier_errors++;
  1628. if (td->tdesc0.TSR & TSR0_ABT)
  1629. stats->tx_aborted_errors++;
  1630. if (td->tdesc0.TSR & TSR0_OWC)
  1631. stats->tx_window_errors++;
  1632. } else {
  1633. stats->tx_packets++;
  1634. stats->tx_bytes += tdinfo->skb->len;
  1635. }
  1636. velocity_free_tx_buf(vptr, tdinfo, td);
  1637. vptr->tx.used[qnum]--;
  1638. }
  1639. vptr->tx.tail[qnum] = idx;
  1640. if (AVAIL_TD(vptr, qnum) < 1)
  1641. full = 1;
  1642. }
  1643. /*
  1644. * Look to see if we should kick the transmit network
  1645. * layer for more work.
  1646. */
  1647. if (netif_queue_stopped(vptr->netdev) && (full == 0) &&
  1648. (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
  1649. netif_wake_queue(vptr->netdev);
  1650. }
  1651. return works;
  1652. }
  1653. /**
  1654. * velocity_rx_csum - checksum process
  1655. * @rd: receive packet descriptor
  1656. * @skb: network layer packet buffer
  1657. *
  1658. * Process the status bits for the received packet and determine
  1659. * if the checksum was computed and verified by the hardware
  1660. */
  1661. static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
  1662. {
  1663. skb_checksum_none_assert(skb);
  1664. if (rd->rdesc1.CSM & CSM_IPKT) {
  1665. if (rd->rdesc1.CSM & CSM_IPOK) {
  1666. if ((rd->rdesc1.CSM & CSM_TCPKT) ||
  1667. (rd->rdesc1.CSM & CSM_UDPKT)) {
  1668. if (!(rd->rdesc1.CSM & CSM_TUPOK))
  1669. return;
  1670. }
  1671. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1672. }
  1673. }
  1674. }
  1675. /**
  1676. * velocity_rx_copy - in place Rx copy for small packets
  1677. * @rx_skb: network layer packet buffer candidate
  1678. * @pkt_size: received data size
  1679. * @rd: receive packet descriptor
  1680. * @dev: network device
  1681. *
  1682. * Replace the current skb that is scheduled for Rx processing by a
  1683. * shorter, immediately allocated skb, if the received packet is small
  1684. * enough. This function returns a negative value if the received
  1685. * packet is too big or if memory is exhausted.
  1686. */
  1687. static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
  1688. struct velocity_info *vptr)
  1689. {
  1690. int ret = -1;
  1691. if (pkt_size < rx_copybreak) {
  1692. struct sk_buff *new_skb;
  1693. new_skb = netdev_alloc_skb_ip_align(vptr->netdev, pkt_size);
  1694. if (new_skb) {
  1695. new_skb->ip_summed = rx_skb[0]->ip_summed;
  1696. skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
  1697. *rx_skb = new_skb;
  1698. ret = 0;
  1699. }
  1700. }
  1701. return ret;
  1702. }
  1703. /**
  1704. * velocity_iph_realign - IP header alignment
  1705. * @vptr: velocity we are handling
  1706. * @skb: network layer packet buffer
  1707. * @pkt_size: received data size
  1708. *
  1709. * Align IP header on a 2 bytes boundary. This behavior can be
  1710. * configured by the user.
  1711. */
  1712. static inline void velocity_iph_realign(struct velocity_info *vptr,
  1713. struct sk_buff *skb, int pkt_size)
  1714. {
  1715. if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
  1716. memmove(skb->data + 2, skb->data, pkt_size);
  1717. skb_reserve(skb, 2);
  1718. }
  1719. }
  1720. /**
  1721. * velocity_receive_frame - received packet processor
  1722. * @vptr: velocity we are handling
  1723. * @idx: ring index
  1724. *
  1725. * A packet has arrived. We process the packet and if appropriate
  1726. * pass the frame up the network stack
  1727. */
  1728. static int velocity_receive_frame(struct velocity_info *vptr, int idx)
  1729. {
  1730. struct net_device_stats *stats = &vptr->netdev->stats;
  1731. struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
  1732. struct rx_desc *rd = &(vptr->rx.ring[idx]);
  1733. int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
  1734. struct sk_buff *skb;
  1735. if (unlikely(rd->rdesc0.RSR & (RSR_STP | RSR_EDP | RSR_RL))) {
  1736. if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP))
  1737. VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame spans multiple RDs.\n", vptr->netdev->name);
  1738. stats->rx_length_errors++;
  1739. return -EINVAL;
  1740. }
  1741. if (rd->rdesc0.RSR & RSR_MAR)
  1742. stats->multicast++;
  1743. skb = rd_info->skb;
  1744. dma_sync_single_for_cpu(vptr->dev, rd_info->skb_dma,
  1745. vptr->rx.buf_sz, DMA_FROM_DEVICE);
  1746. velocity_rx_csum(rd, skb);
  1747. if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
  1748. velocity_iph_realign(vptr, skb, pkt_len);
  1749. rd_info->skb = NULL;
  1750. dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
  1751. DMA_FROM_DEVICE);
  1752. } else {
  1753. dma_sync_single_for_device(vptr->dev, rd_info->skb_dma,
  1754. vptr->rx.buf_sz, DMA_FROM_DEVICE);
  1755. }
  1756. skb_put(skb, pkt_len - 4);
  1757. skb->protocol = eth_type_trans(skb, vptr->netdev);
  1758. if (rd->rdesc0.RSR & RSR_DETAG) {
  1759. u16 vid = swab16(le16_to_cpu(rd->rdesc1.PQTAG));
  1760. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  1761. }
  1762. netif_receive_skb(skb);
  1763. stats->rx_bytes += pkt_len;
  1764. stats->rx_packets++;
  1765. return 0;
  1766. }
  1767. /**
  1768. * velocity_rx_srv - service RX interrupt
  1769. * @vptr: velocity
  1770. *
  1771. * Walk the receive ring of the velocity adapter and remove
  1772. * any received packets from the receive queue. Hand the ring
  1773. * slots back to the adapter for reuse.
  1774. */
  1775. static int velocity_rx_srv(struct velocity_info *vptr, int budget_left)
  1776. {
  1777. struct net_device_stats *stats = &vptr->netdev->stats;
  1778. int rd_curr = vptr->rx.curr;
  1779. int works = 0;
  1780. while (works < budget_left) {
  1781. struct rx_desc *rd = vptr->rx.ring + rd_curr;
  1782. if (!vptr->rx.info[rd_curr].skb)
  1783. break;
  1784. if (rd->rdesc0.len & OWNED_BY_NIC)
  1785. break;
  1786. rmb();
  1787. /*
  1788. * Don't drop CE or RL error frame although RXOK is off
  1789. */
  1790. if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
  1791. if (velocity_receive_frame(vptr, rd_curr) < 0)
  1792. stats->rx_dropped++;
  1793. } else {
  1794. if (rd->rdesc0.RSR & RSR_CRC)
  1795. stats->rx_crc_errors++;
  1796. if (rd->rdesc0.RSR & RSR_FAE)
  1797. stats->rx_frame_errors++;
  1798. stats->rx_dropped++;
  1799. }
  1800. rd->size |= RX_INTEN;
  1801. rd_curr++;
  1802. if (rd_curr >= vptr->options.numrx)
  1803. rd_curr = 0;
  1804. works++;
  1805. }
  1806. vptr->rx.curr = rd_curr;
  1807. if ((works > 0) && (velocity_rx_refill(vptr) > 0))
  1808. velocity_give_many_rx_descs(vptr);
  1809. VAR_USED(stats);
  1810. return works;
  1811. }
  1812. static int velocity_poll(struct napi_struct *napi, int budget)
  1813. {
  1814. struct velocity_info *vptr = container_of(napi,
  1815. struct velocity_info, napi);
  1816. unsigned int rx_done;
  1817. unsigned long flags;
  1818. /*
  1819. * Do rx and tx twice for performance (taken from the VIA
  1820. * out-of-tree driver).
  1821. */
  1822. rx_done = velocity_rx_srv(vptr, budget);
  1823. spin_lock_irqsave(&vptr->lock, flags);
  1824. velocity_tx_srv(vptr);
  1825. /* If budget not fully consumed, exit the polling mode */
  1826. if (rx_done < budget) {
  1827. napi_complete_done(napi, rx_done);
  1828. mac_enable_int(vptr->mac_regs);
  1829. }
  1830. spin_unlock_irqrestore(&vptr->lock, flags);
  1831. return rx_done;
  1832. }
  1833. /**
  1834. * velocity_intr - interrupt callback
  1835. * @irq: interrupt number
  1836. * @dev_instance: interrupting device
  1837. *
  1838. * Called whenever an interrupt is generated by the velocity
  1839. * adapter IRQ line. We may not be the source of the interrupt
  1840. * and need to identify initially if we are, and if not exit as
  1841. * efficiently as possible.
  1842. */
  1843. static irqreturn_t velocity_intr(int irq, void *dev_instance)
  1844. {
  1845. struct net_device *dev = dev_instance;
  1846. struct velocity_info *vptr = netdev_priv(dev);
  1847. u32 isr_status;
  1848. spin_lock(&vptr->lock);
  1849. isr_status = mac_read_isr(vptr->mac_regs);
  1850. /* Not us ? */
  1851. if (isr_status == 0) {
  1852. spin_unlock(&vptr->lock);
  1853. return IRQ_NONE;
  1854. }
  1855. /* Ack the interrupt */
  1856. mac_write_isr(vptr->mac_regs, isr_status);
  1857. if (likely(napi_schedule_prep(&vptr->napi))) {
  1858. mac_disable_int(vptr->mac_regs);
  1859. __napi_schedule(&vptr->napi);
  1860. }
  1861. if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
  1862. velocity_error(vptr, isr_status);
  1863. spin_unlock(&vptr->lock);
  1864. return IRQ_HANDLED;
  1865. }
  1866. /**
  1867. * velocity_open - interface activation callback
  1868. * @dev: network layer device to open
  1869. *
  1870. * Called when the network layer brings the interface up. Returns
  1871. * a negative posix error code on failure, or zero on success.
  1872. *
  1873. * All the ring allocation and set up is done on open for this
  1874. * adapter to minimise memory usage when inactive
  1875. */
  1876. static int velocity_open(struct net_device *dev)
  1877. {
  1878. struct velocity_info *vptr = netdev_priv(dev);
  1879. int ret;
  1880. ret = velocity_init_rings(vptr, dev->mtu);
  1881. if (ret < 0)
  1882. goto out;
  1883. /* Ensure chip is running */
  1884. velocity_set_power_state(vptr, PCI_D0);
  1885. velocity_init_registers(vptr, VELOCITY_INIT_COLD);
  1886. ret = request_irq(dev->irq, velocity_intr, IRQF_SHARED,
  1887. dev->name, dev);
  1888. if (ret < 0) {
  1889. /* Power down the chip */
  1890. velocity_set_power_state(vptr, PCI_D3hot);
  1891. velocity_free_rings(vptr);
  1892. goto out;
  1893. }
  1894. velocity_give_many_rx_descs(vptr);
  1895. mac_enable_int(vptr->mac_regs);
  1896. netif_start_queue(dev);
  1897. napi_enable(&vptr->napi);
  1898. vptr->flags |= VELOCITY_FLAGS_OPENED;
  1899. out:
  1900. return ret;
  1901. }
  1902. /**
  1903. * velocity_shutdown - shut down the chip
  1904. * @vptr: velocity to deactivate
  1905. *
  1906. * Shuts down the internal operations of the velocity and
  1907. * disables interrupts, autopolling, transmit and receive
  1908. */
  1909. static void velocity_shutdown(struct velocity_info *vptr)
  1910. {
  1911. struct mac_regs __iomem *regs = vptr->mac_regs;
  1912. mac_disable_int(regs);
  1913. writel(CR0_STOP, &regs->CR0Set);
  1914. writew(0xFFFF, &regs->TDCSRClr);
  1915. writeb(0xFF, &regs->RDCSRClr);
  1916. safe_disable_mii_autopoll(regs);
  1917. mac_clear_isr(regs);
  1918. }
  1919. /**
  1920. * velocity_change_mtu - MTU change callback
  1921. * @dev: network device
  1922. * @new_mtu: desired MTU
  1923. *
  1924. * Handle requests from the networking layer for MTU change on
  1925. * this interface. It gets called on a change by the network layer.
  1926. * Return zero for success or negative posix error code.
  1927. */
  1928. static int velocity_change_mtu(struct net_device *dev, int new_mtu)
  1929. {
  1930. struct velocity_info *vptr = netdev_priv(dev);
  1931. int ret = 0;
  1932. if (!netif_running(dev)) {
  1933. dev->mtu = new_mtu;
  1934. goto out_0;
  1935. }
  1936. if (dev->mtu != new_mtu) {
  1937. struct velocity_info *tmp_vptr;
  1938. unsigned long flags;
  1939. struct rx_info rx;
  1940. struct tx_info tx;
  1941. tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
  1942. if (!tmp_vptr) {
  1943. ret = -ENOMEM;
  1944. goto out_0;
  1945. }
  1946. tmp_vptr->netdev = dev;
  1947. tmp_vptr->pdev = vptr->pdev;
  1948. tmp_vptr->dev = vptr->dev;
  1949. tmp_vptr->options = vptr->options;
  1950. tmp_vptr->tx.numq = vptr->tx.numq;
  1951. ret = velocity_init_rings(tmp_vptr, new_mtu);
  1952. if (ret < 0)
  1953. goto out_free_tmp_vptr_1;
  1954. napi_disable(&vptr->napi);
  1955. spin_lock_irqsave(&vptr->lock, flags);
  1956. netif_stop_queue(dev);
  1957. velocity_shutdown(vptr);
  1958. rx = vptr->rx;
  1959. tx = vptr->tx;
  1960. vptr->rx = tmp_vptr->rx;
  1961. vptr->tx = tmp_vptr->tx;
  1962. tmp_vptr->rx = rx;
  1963. tmp_vptr->tx = tx;
  1964. dev->mtu = new_mtu;
  1965. velocity_init_registers(vptr, VELOCITY_INIT_COLD);
  1966. velocity_give_many_rx_descs(vptr);
  1967. napi_enable(&vptr->napi);
  1968. mac_enable_int(vptr->mac_regs);
  1969. netif_start_queue(dev);
  1970. spin_unlock_irqrestore(&vptr->lock, flags);
  1971. velocity_free_rings(tmp_vptr);
  1972. out_free_tmp_vptr_1:
  1973. kfree(tmp_vptr);
  1974. }
  1975. out_0:
  1976. return ret;
  1977. }
  1978. #ifdef CONFIG_NET_POLL_CONTROLLER
  1979. /**
  1980. * velocity_poll_controller - Velocity Poll controller function
  1981. * @dev: network device
  1982. *
  1983. *
  1984. * Used by NETCONSOLE and other diagnostic tools to allow network I/P
  1985. * with interrupts disabled.
  1986. */
  1987. static void velocity_poll_controller(struct net_device *dev)
  1988. {
  1989. disable_irq(dev->irq);
  1990. velocity_intr(dev->irq, dev);
  1991. enable_irq(dev->irq);
  1992. }
  1993. #endif
  1994. /**
  1995. * velocity_mii_ioctl - MII ioctl handler
  1996. * @dev: network device
  1997. * @ifr: the ifreq block for the ioctl
  1998. * @cmd: the command
  1999. *
  2000. * Process MII requests made via ioctl from the network layer. These
  2001. * are used by tools like kudzu to interrogate the link state of the
  2002. * hardware
  2003. */
  2004. static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2005. {
  2006. struct velocity_info *vptr = netdev_priv(dev);
  2007. struct mac_regs __iomem *regs = vptr->mac_regs;
  2008. unsigned long flags;
  2009. struct mii_ioctl_data *miidata = if_mii(ifr);
  2010. int err;
  2011. switch (cmd) {
  2012. case SIOCGMIIPHY:
  2013. miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
  2014. break;
  2015. case SIOCGMIIREG:
  2016. if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
  2017. return -ETIMEDOUT;
  2018. break;
  2019. case SIOCSMIIREG:
  2020. spin_lock_irqsave(&vptr->lock, flags);
  2021. err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
  2022. spin_unlock_irqrestore(&vptr->lock, flags);
  2023. check_connection_type(vptr->mac_regs);
  2024. if (err)
  2025. return err;
  2026. break;
  2027. default:
  2028. return -EOPNOTSUPP;
  2029. }
  2030. return 0;
  2031. }
  2032. /**
  2033. * velocity_ioctl - ioctl entry point
  2034. * @dev: network device
  2035. * @rq: interface request ioctl
  2036. * @cmd: command code
  2037. *
  2038. * Called when the user issues an ioctl request to the network
  2039. * device in question. The velocity interface supports MII.
  2040. */
  2041. static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2042. {
  2043. struct velocity_info *vptr = netdev_priv(dev);
  2044. int ret;
  2045. /* If we are asked for information and the device is power
  2046. saving then we need to bring the device back up to talk to it */
  2047. if (!netif_running(dev))
  2048. velocity_set_power_state(vptr, PCI_D0);
  2049. switch (cmd) {
  2050. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  2051. case SIOCGMIIREG: /* Read MII PHY register. */
  2052. case SIOCSMIIREG: /* Write to MII PHY register. */
  2053. ret = velocity_mii_ioctl(dev, rq, cmd);
  2054. break;
  2055. default:
  2056. ret = -EOPNOTSUPP;
  2057. }
  2058. if (!netif_running(dev))
  2059. velocity_set_power_state(vptr, PCI_D3hot);
  2060. return ret;
  2061. }
  2062. /**
  2063. * velocity_get_status - statistics callback
  2064. * @dev: network device
  2065. *
  2066. * Callback from the network layer to allow driver statistics
  2067. * to be resynchronized with hardware collected state. In the
  2068. * case of the velocity we need to pull the MIB counters from
  2069. * the hardware into the counters before letting the network
  2070. * layer display them.
  2071. */
  2072. static struct net_device_stats *velocity_get_stats(struct net_device *dev)
  2073. {
  2074. struct velocity_info *vptr = netdev_priv(dev);
  2075. /* If the hardware is down, don't touch MII */
  2076. if (!netif_running(dev))
  2077. return &dev->stats;
  2078. spin_lock_irq(&vptr->lock);
  2079. velocity_update_hw_mibs(vptr);
  2080. spin_unlock_irq(&vptr->lock);
  2081. dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
  2082. dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
  2083. dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
  2084. // unsigned long rx_dropped; /* no space in linux buffers */
  2085. dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
  2086. /* detailed rx_errors: */
  2087. // unsigned long rx_length_errors;
  2088. // unsigned long rx_over_errors; /* receiver ring buff overflow */
  2089. dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
  2090. // unsigned long rx_frame_errors; /* recv'd frame alignment error */
  2091. // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  2092. // unsigned long rx_missed_errors; /* receiver missed packet */
  2093. /* detailed tx_errors */
  2094. // unsigned long tx_fifo_errors;
  2095. return &dev->stats;
  2096. }
  2097. /**
  2098. * velocity_close - close adapter callback
  2099. * @dev: network device
  2100. *
  2101. * Callback from the network layer when the velocity is being
  2102. * deactivated by the network layer
  2103. */
  2104. static int velocity_close(struct net_device *dev)
  2105. {
  2106. struct velocity_info *vptr = netdev_priv(dev);
  2107. napi_disable(&vptr->napi);
  2108. netif_stop_queue(dev);
  2109. velocity_shutdown(vptr);
  2110. if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
  2111. velocity_get_ip(vptr);
  2112. free_irq(dev->irq, dev);
  2113. velocity_free_rings(vptr);
  2114. vptr->flags &= (~VELOCITY_FLAGS_OPENED);
  2115. return 0;
  2116. }
  2117. /**
  2118. * velocity_xmit - transmit packet callback
  2119. * @skb: buffer to transmit
  2120. * @dev: network device
  2121. *
  2122. * Called by the networ layer to request a packet is queued to
  2123. * the velocity. Returns zero on success.
  2124. */
  2125. static netdev_tx_t velocity_xmit(struct sk_buff *skb,
  2126. struct net_device *dev)
  2127. {
  2128. struct velocity_info *vptr = netdev_priv(dev);
  2129. int qnum = 0;
  2130. struct tx_desc *td_ptr;
  2131. struct velocity_td_info *tdinfo;
  2132. unsigned long flags;
  2133. int pktlen;
  2134. int index, prev;
  2135. int i = 0;
  2136. if (skb_padto(skb, ETH_ZLEN))
  2137. goto out;
  2138. /* The hardware can handle at most 7 memory segments, so merge
  2139. * the skb if there are more */
  2140. if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
  2141. dev_kfree_skb_any(skb);
  2142. return NETDEV_TX_OK;
  2143. }
  2144. pktlen = skb_shinfo(skb)->nr_frags == 0 ?
  2145. max_t(unsigned int, skb->len, ETH_ZLEN) :
  2146. skb_headlen(skb);
  2147. spin_lock_irqsave(&vptr->lock, flags);
  2148. index = vptr->tx.curr[qnum];
  2149. td_ptr = &(vptr->tx.rings[qnum][index]);
  2150. tdinfo = &(vptr->tx.infos[qnum][index]);
  2151. td_ptr->tdesc1.TCR = TCR0_TIC;
  2152. td_ptr->td_buf[0].size &= ~TD_QUEUE;
  2153. /*
  2154. * Map the linear network buffer into PCI space and
  2155. * add it to the transmit ring.
  2156. */
  2157. tdinfo->skb = skb;
  2158. tdinfo->skb_dma[0] = dma_map_single(vptr->dev, skb->data, pktlen,
  2159. DMA_TO_DEVICE);
  2160. td_ptr->tdesc0.len = cpu_to_le16(pktlen);
  2161. td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
  2162. td_ptr->td_buf[0].pa_high = 0;
  2163. td_ptr->td_buf[0].size = cpu_to_le16(pktlen);
  2164. /* Handle fragments */
  2165. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2166. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2167. tdinfo->skb_dma[i + 1] = skb_frag_dma_map(vptr->dev,
  2168. frag, 0,
  2169. skb_frag_size(frag),
  2170. DMA_TO_DEVICE);
  2171. td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
  2172. td_ptr->td_buf[i + 1].pa_high = 0;
  2173. td_ptr->td_buf[i + 1].size = cpu_to_le16(skb_frag_size(frag));
  2174. }
  2175. tdinfo->nskb_dma = i + 1;
  2176. td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
  2177. if (skb_vlan_tag_present(skb)) {
  2178. td_ptr->tdesc1.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
  2179. td_ptr->tdesc1.TCR |= TCR0_VETAG;
  2180. }
  2181. /*
  2182. * Handle hardware checksum
  2183. */
  2184. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2185. const struct iphdr *ip = ip_hdr(skb);
  2186. if (ip->protocol == IPPROTO_TCP)
  2187. td_ptr->tdesc1.TCR |= TCR0_TCPCK;
  2188. else if (ip->protocol == IPPROTO_UDP)
  2189. td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
  2190. td_ptr->tdesc1.TCR |= TCR0_IPCK;
  2191. }
  2192. prev = index - 1;
  2193. if (prev < 0)
  2194. prev = vptr->options.numtx - 1;
  2195. td_ptr->tdesc0.len |= OWNED_BY_NIC;
  2196. vptr->tx.used[qnum]++;
  2197. vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
  2198. if (AVAIL_TD(vptr, qnum) < 1)
  2199. netif_stop_queue(dev);
  2200. td_ptr = &(vptr->tx.rings[qnum][prev]);
  2201. td_ptr->td_buf[0].size |= TD_QUEUE;
  2202. mac_tx_queue_wake(vptr->mac_regs, qnum);
  2203. spin_unlock_irqrestore(&vptr->lock, flags);
  2204. out:
  2205. return NETDEV_TX_OK;
  2206. }
  2207. static const struct net_device_ops velocity_netdev_ops = {
  2208. .ndo_open = velocity_open,
  2209. .ndo_stop = velocity_close,
  2210. .ndo_start_xmit = velocity_xmit,
  2211. .ndo_get_stats = velocity_get_stats,
  2212. .ndo_validate_addr = eth_validate_addr,
  2213. .ndo_set_mac_address = eth_mac_addr,
  2214. .ndo_set_rx_mode = velocity_set_multi,
  2215. .ndo_change_mtu = velocity_change_mtu,
  2216. .ndo_do_ioctl = velocity_ioctl,
  2217. .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
  2218. .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
  2219. #ifdef CONFIG_NET_POLL_CONTROLLER
  2220. .ndo_poll_controller = velocity_poll_controller,
  2221. #endif
  2222. };
  2223. /**
  2224. * velocity_init_info - init private data
  2225. * @pdev: PCI device
  2226. * @vptr: Velocity info
  2227. * @info: Board type
  2228. *
  2229. * Set up the initial velocity_info struct for the device that has been
  2230. * discovered.
  2231. */
  2232. static void velocity_init_info(struct velocity_info *vptr,
  2233. const struct velocity_info_tbl *info)
  2234. {
  2235. vptr->chip_id = info->chip_id;
  2236. vptr->tx.numq = info->txqueue;
  2237. vptr->multicast_limit = MCAM_SIZE;
  2238. spin_lock_init(&vptr->lock);
  2239. }
  2240. /**
  2241. * velocity_get_pci_info - retrieve PCI info for device
  2242. * @vptr: velocity device
  2243. * @pdev: PCI device it matches
  2244. *
  2245. * Retrieve the PCI configuration space data that interests us from
  2246. * the kernel PCI layer
  2247. */
  2248. static int velocity_get_pci_info(struct velocity_info *vptr)
  2249. {
  2250. struct pci_dev *pdev = vptr->pdev;
  2251. pci_set_master(pdev);
  2252. vptr->ioaddr = pci_resource_start(pdev, 0);
  2253. vptr->memaddr = pci_resource_start(pdev, 1);
  2254. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
  2255. dev_err(&pdev->dev,
  2256. "region #0 is not an I/O resource, aborting.\n");
  2257. return -EINVAL;
  2258. }
  2259. if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
  2260. dev_err(&pdev->dev,
  2261. "region #1 is an I/O resource, aborting.\n");
  2262. return -EINVAL;
  2263. }
  2264. if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
  2265. dev_err(&pdev->dev, "region #1 is too small.\n");
  2266. return -EINVAL;
  2267. }
  2268. return 0;
  2269. }
  2270. /**
  2271. * velocity_get_platform_info - retrieve platform info for device
  2272. * @vptr: velocity device
  2273. * @pdev: platform device it matches
  2274. *
  2275. * Retrieve the Platform configuration data that interests us
  2276. */
  2277. static int velocity_get_platform_info(struct velocity_info *vptr)
  2278. {
  2279. struct resource res;
  2280. int ret;
  2281. if (of_get_property(vptr->dev->of_node, "no-eeprom", NULL))
  2282. vptr->no_eeprom = 1;
  2283. ret = of_address_to_resource(vptr->dev->of_node, 0, &res);
  2284. if (ret) {
  2285. dev_err(vptr->dev, "unable to find memory address\n");
  2286. return ret;
  2287. }
  2288. vptr->memaddr = res.start;
  2289. if (resource_size(&res) < VELOCITY_IO_SIZE) {
  2290. dev_err(vptr->dev, "memory region is too small.\n");
  2291. return -EINVAL;
  2292. }
  2293. return 0;
  2294. }
  2295. /**
  2296. * velocity_print_info - per driver data
  2297. * @vptr: velocity
  2298. *
  2299. * Print per driver data as the kernel driver finds Velocity
  2300. * hardware
  2301. */
  2302. static void velocity_print_info(struct velocity_info *vptr)
  2303. {
  2304. struct net_device *dev = vptr->netdev;
  2305. printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
  2306. printk(KERN_INFO "%s: Ethernet Address: %pM\n",
  2307. dev->name, dev->dev_addr);
  2308. }
  2309. static u32 velocity_get_link(struct net_device *dev)
  2310. {
  2311. struct velocity_info *vptr = netdev_priv(dev);
  2312. struct mac_regs __iomem *regs = vptr->mac_regs;
  2313. return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
  2314. }
  2315. /**
  2316. * velocity_probe - set up discovered velocity device
  2317. * @pdev: PCI device
  2318. * @ent: PCI device table entry that matched
  2319. * @bustype: bus that device is connected to
  2320. *
  2321. * Configure a discovered adapter from scratch. Return a negative
  2322. * errno error code on failure paths.
  2323. */
  2324. static int velocity_probe(struct device *dev, int irq,
  2325. const struct velocity_info_tbl *info,
  2326. enum velocity_bus_type bustype)
  2327. {
  2328. static int first = 1;
  2329. struct net_device *netdev;
  2330. int i;
  2331. const char *drv_string;
  2332. struct velocity_info *vptr;
  2333. struct mac_regs __iomem *regs;
  2334. int ret = -ENOMEM;
  2335. /* FIXME: this driver, like almost all other ethernet drivers,
  2336. * can support more than MAX_UNITS.
  2337. */
  2338. if (velocity_nics >= MAX_UNITS) {
  2339. dev_notice(dev, "already found %d NICs.\n", velocity_nics);
  2340. return -ENODEV;
  2341. }
  2342. netdev = alloc_etherdev(sizeof(struct velocity_info));
  2343. if (!netdev)
  2344. goto out;
  2345. /* Chain it all together */
  2346. SET_NETDEV_DEV(netdev, dev);
  2347. vptr = netdev_priv(netdev);
  2348. if (first) {
  2349. printk(KERN_INFO "%s Ver. %s\n",
  2350. VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
  2351. printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
  2352. printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
  2353. first = 0;
  2354. }
  2355. netdev->irq = irq;
  2356. vptr->netdev = netdev;
  2357. vptr->dev = dev;
  2358. velocity_init_info(vptr, info);
  2359. if (bustype == BUS_PCI) {
  2360. vptr->pdev = to_pci_dev(dev);
  2361. ret = velocity_get_pci_info(vptr);
  2362. if (ret < 0)
  2363. goto err_free_dev;
  2364. } else {
  2365. vptr->pdev = NULL;
  2366. ret = velocity_get_platform_info(vptr);
  2367. if (ret < 0)
  2368. goto err_free_dev;
  2369. }
  2370. regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
  2371. if (regs == NULL) {
  2372. ret = -EIO;
  2373. goto err_free_dev;
  2374. }
  2375. vptr->mac_regs = regs;
  2376. vptr->rev_id = readb(&regs->rev_id);
  2377. mac_wol_reset(regs);
  2378. for (i = 0; i < 6; i++)
  2379. netdev->dev_addr[i] = readb(&regs->PAR[i]);
  2380. drv_string = dev_driver_string(dev);
  2381. velocity_get_options(&vptr->options, velocity_nics, drv_string);
  2382. /*
  2383. * Mask out the options cannot be set to the chip
  2384. */
  2385. vptr->options.flags &= info->flags;
  2386. /*
  2387. * Enable the chip specified capbilities
  2388. */
  2389. vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
  2390. vptr->wol_opts = vptr->options.wol_opts;
  2391. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  2392. vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
  2393. netdev->netdev_ops = &velocity_netdev_ops;
  2394. netdev->ethtool_ops = &velocity_ethtool_ops;
  2395. netif_napi_add(netdev, &vptr->napi, velocity_poll,
  2396. VELOCITY_NAPI_WEIGHT);
  2397. netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  2398. NETIF_F_HW_VLAN_CTAG_TX;
  2399. netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
  2400. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX |
  2401. NETIF_F_IP_CSUM;
  2402. /* MTU range: 64 - 9000 */
  2403. netdev->min_mtu = VELOCITY_MIN_MTU;
  2404. netdev->max_mtu = VELOCITY_MAX_MTU;
  2405. ret = register_netdev(netdev);
  2406. if (ret < 0)
  2407. goto err_iounmap;
  2408. if (!velocity_get_link(netdev)) {
  2409. netif_carrier_off(netdev);
  2410. vptr->mii_status |= VELOCITY_LINK_FAIL;
  2411. }
  2412. velocity_print_info(vptr);
  2413. dev_set_drvdata(vptr->dev, netdev);
  2414. /* and leave the chip powered down */
  2415. velocity_set_power_state(vptr, PCI_D3hot);
  2416. velocity_nics++;
  2417. out:
  2418. return ret;
  2419. err_iounmap:
  2420. netif_napi_del(&vptr->napi);
  2421. iounmap(regs);
  2422. err_free_dev:
  2423. free_netdev(netdev);
  2424. goto out;
  2425. }
  2426. /**
  2427. * velocity_remove - device unplug
  2428. * @dev: device being removed
  2429. *
  2430. * Device unload callback. Called on an unplug or on module
  2431. * unload for each active device that is present. Disconnects
  2432. * the device from the network layer and frees all the resources
  2433. */
  2434. static int velocity_remove(struct device *dev)
  2435. {
  2436. struct net_device *netdev = dev_get_drvdata(dev);
  2437. struct velocity_info *vptr = netdev_priv(netdev);
  2438. unregister_netdev(netdev);
  2439. netif_napi_del(&vptr->napi);
  2440. iounmap(vptr->mac_regs);
  2441. free_netdev(netdev);
  2442. velocity_nics--;
  2443. return 0;
  2444. }
  2445. static int velocity_pci_probe(struct pci_dev *pdev,
  2446. const struct pci_device_id *ent)
  2447. {
  2448. const struct velocity_info_tbl *info =
  2449. &chip_info_table[ent->driver_data];
  2450. int ret;
  2451. ret = pci_enable_device(pdev);
  2452. if (ret < 0)
  2453. return ret;
  2454. ret = pci_request_regions(pdev, VELOCITY_NAME);
  2455. if (ret < 0) {
  2456. dev_err(&pdev->dev, "No PCI resources.\n");
  2457. goto fail1;
  2458. }
  2459. ret = velocity_probe(&pdev->dev, pdev->irq, info, BUS_PCI);
  2460. if (ret == 0)
  2461. return 0;
  2462. pci_release_regions(pdev);
  2463. fail1:
  2464. pci_disable_device(pdev);
  2465. return ret;
  2466. }
  2467. static void velocity_pci_remove(struct pci_dev *pdev)
  2468. {
  2469. velocity_remove(&pdev->dev);
  2470. pci_release_regions(pdev);
  2471. pci_disable_device(pdev);
  2472. }
  2473. static int velocity_platform_probe(struct platform_device *pdev)
  2474. {
  2475. const struct of_device_id *of_id;
  2476. const struct velocity_info_tbl *info;
  2477. int irq;
  2478. of_id = of_match_device(velocity_of_ids, &pdev->dev);
  2479. if (!of_id)
  2480. return -EINVAL;
  2481. info = of_id->data;
  2482. irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  2483. if (!irq)
  2484. return -EINVAL;
  2485. return velocity_probe(&pdev->dev, irq, info, BUS_PLATFORM);
  2486. }
  2487. static int velocity_platform_remove(struct platform_device *pdev)
  2488. {
  2489. velocity_remove(&pdev->dev);
  2490. return 0;
  2491. }
  2492. #ifdef CONFIG_PM_SLEEP
  2493. /**
  2494. * wol_calc_crc - WOL CRC
  2495. * @pattern: data pattern
  2496. * @mask_pattern: mask
  2497. *
  2498. * Compute the wake on lan crc hashes for the packet header
  2499. * we are interested in.
  2500. */
  2501. static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
  2502. {
  2503. u16 crc = 0xFFFF;
  2504. u8 mask;
  2505. int i, j;
  2506. for (i = 0; i < size; i++) {
  2507. mask = mask_pattern[i];
  2508. /* Skip this loop if the mask equals to zero */
  2509. if (mask == 0x00)
  2510. continue;
  2511. for (j = 0; j < 8; j++) {
  2512. if ((mask & 0x01) == 0) {
  2513. mask >>= 1;
  2514. continue;
  2515. }
  2516. mask >>= 1;
  2517. crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
  2518. }
  2519. }
  2520. /* Finally, invert the result once to get the correct data */
  2521. crc = ~crc;
  2522. return bitrev32(crc) >> 16;
  2523. }
  2524. /**
  2525. * velocity_set_wol - set up for wake on lan
  2526. * @vptr: velocity to set WOL status on
  2527. *
  2528. * Set a card up for wake on lan either by unicast or by
  2529. * ARP packet.
  2530. *
  2531. * FIXME: check static buffer is safe here
  2532. */
  2533. static int velocity_set_wol(struct velocity_info *vptr)
  2534. {
  2535. struct mac_regs __iomem *regs = vptr->mac_regs;
  2536. enum speed_opt spd_dpx = vptr->options.spd_dpx;
  2537. static u8 buf[256];
  2538. int i;
  2539. static u32 mask_pattern[2][4] = {
  2540. {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
  2541. {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
  2542. };
  2543. writew(0xFFFF, &regs->WOLCRClr);
  2544. writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
  2545. writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
  2546. /*
  2547. if (vptr->wol_opts & VELOCITY_WOL_PHY)
  2548. writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
  2549. */
  2550. if (vptr->wol_opts & VELOCITY_WOL_UCAST)
  2551. writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
  2552. if (vptr->wol_opts & VELOCITY_WOL_ARP) {
  2553. struct arp_packet *arp = (struct arp_packet *) buf;
  2554. u16 crc;
  2555. memset(buf, 0, sizeof(struct arp_packet) + 7);
  2556. for (i = 0; i < 4; i++)
  2557. writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
  2558. arp->type = htons(ETH_P_ARP);
  2559. arp->ar_op = htons(1);
  2560. memcpy(arp->ar_tip, vptr->ip_addr, 4);
  2561. crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
  2562. (u8 *) & mask_pattern[0][0]);
  2563. writew(crc, &regs->PatternCRC[0]);
  2564. writew(WOLCR_ARP_EN, &regs->WOLCRSet);
  2565. }
  2566. BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
  2567. BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
  2568. writew(0x0FFF, &regs->WOLSRClr);
  2569. if (spd_dpx == SPD_DPX_1000_FULL)
  2570. goto mac_done;
  2571. if (spd_dpx != SPD_DPX_AUTO)
  2572. goto advertise_done;
  2573. if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
  2574. if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
  2575. MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
  2576. MII_REG_BITS_OFF(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
  2577. }
  2578. if (vptr->mii_status & VELOCITY_SPEED_1000)
  2579. MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
  2580. advertise_done:
  2581. BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
  2582. {
  2583. u8 GCR;
  2584. GCR = readb(&regs->CHIPGCR);
  2585. GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
  2586. writeb(GCR, &regs->CHIPGCR);
  2587. }
  2588. mac_done:
  2589. BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
  2590. /* Turn on SWPTAG just before entering power mode */
  2591. BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
  2592. /* Go to bed ..... */
  2593. BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
  2594. return 0;
  2595. }
  2596. /**
  2597. * velocity_save_context - save registers
  2598. * @vptr: velocity
  2599. * @context: buffer for stored context
  2600. *
  2601. * Retrieve the current configuration from the velocity hardware
  2602. * and stash it in the context structure, for use by the context
  2603. * restore functions. This allows us to save things we need across
  2604. * power down states
  2605. */
  2606. static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
  2607. {
  2608. struct mac_regs __iomem *regs = vptr->mac_regs;
  2609. u16 i;
  2610. u8 __iomem *ptr = (u8 __iomem *)regs;
  2611. for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
  2612. *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
  2613. for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
  2614. *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
  2615. for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
  2616. *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
  2617. }
  2618. static int velocity_suspend(struct device *dev)
  2619. {
  2620. struct net_device *netdev = dev_get_drvdata(dev);
  2621. struct velocity_info *vptr = netdev_priv(netdev);
  2622. unsigned long flags;
  2623. if (!netif_running(vptr->netdev))
  2624. return 0;
  2625. netif_device_detach(vptr->netdev);
  2626. spin_lock_irqsave(&vptr->lock, flags);
  2627. if (vptr->pdev)
  2628. pci_save_state(vptr->pdev);
  2629. if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
  2630. velocity_get_ip(vptr);
  2631. velocity_save_context(vptr, &vptr->context);
  2632. velocity_shutdown(vptr);
  2633. velocity_set_wol(vptr);
  2634. if (vptr->pdev)
  2635. pci_enable_wake(vptr->pdev, PCI_D3hot, 1);
  2636. velocity_set_power_state(vptr, PCI_D3hot);
  2637. } else {
  2638. velocity_save_context(vptr, &vptr->context);
  2639. velocity_shutdown(vptr);
  2640. if (vptr->pdev)
  2641. pci_disable_device(vptr->pdev);
  2642. velocity_set_power_state(vptr, PCI_D3hot);
  2643. }
  2644. spin_unlock_irqrestore(&vptr->lock, flags);
  2645. return 0;
  2646. }
  2647. /**
  2648. * velocity_restore_context - restore registers
  2649. * @vptr: velocity
  2650. * @context: buffer for stored context
  2651. *
  2652. * Reload the register configuration from the velocity context
  2653. * created by velocity_save_context.
  2654. */
  2655. static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
  2656. {
  2657. struct mac_regs __iomem *regs = vptr->mac_regs;
  2658. int i;
  2659. u8 __iomem *ptr = (u8 __iomem *)regs;
  2660. for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
  2661. writel(*((u32 *) (context->mac_reg + i)), ptr + i);
  2662. /* Just skip cr0 */
  2663. for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
  2664. /* Clear */
  2665. writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
  2666. /* Set */
  2667. writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
  2668. }
  2669. for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
  2670. writel(*((u32 *) (context->mac_reg + i)), ptr + i);
  2671. for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
  2672. writel(*((u32 *) (context->mac_reg + i)), ptr + i);
  2673. for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
  2674. writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
  2675. }
  2676. static int velocity_resume(struct device *dev)
  2677. {
  2678. struct net_device *netdev = dev_get_drvdata(dev);
  2679. struct velocity_info *vptr = netdev_priv(netdev);
  2680. unsigned long flags;
  2681. int i;
  2682. if (!netif_running(vptr->netdev))
  2683. return 0;
  2684. velocity_set_power_state(vptr, PCI_D0);
  2685. if (vptr->pdev) {
  2686. pci_enable_wake(vptr->pdev, PCI_D0, 0);
  2687. pci_restore_state(vptr->pdev);
  2688. }
  2689. mac_wol_reset(vptr->mac_regs);
  2690. spin_lock_irqsave(&vptr->lock, flags);
  2691. velocity_restore_context(vptr, &vptr->context);
  2692. velocity_init_registers(vptr, VELOCITY_INIT_WOL);
  2693. mac_disable_int(vptr->mac_regs);
  2694. velocity_tx_srv(vptr);
  2695. for (i = 0; i < vptr->tx.numq; i++) {
  2696. if (vptr->tx.used[i])
  2697. mac_tx_queue_wake(vptr->mac_regs, i);
  2698. }
  2699. mac_enable_int(vptr->mac_regs);
  2700. spin_unlock_irqrestore(&vptr->lock, flags);
  2701. netif_device_attach(vptr->netdev);
  2702. return 0;
  2703. }
  2704. #endif /* CONFIG_PM_SLEEP */
  2705. static SIMPLE_DEV_PM_OPS(velocity_pm_ops, velocity_suspend, velocity_resume);
  2706. /*
  2707. * Definition for our device driver. The PCI layer interface
  2708. * uses this to handle all our card discover and plugging
  2709. */
  2710. static struct pci_driver velocity_pci_driver = {
  2711. .name = VELOCITY_NAME,
  2712. .id_table = velocity_pci_id_table,
  2713. .probe = velocity_pci_probe,
  2714. .remove = velocity_pci_remove,
  2715. .driver = {
  2716. .pm = &velocity_pm_ops,
  2717. },
  2718. };
  2719. static struct platform_driver velocity_platform_driver = {
  2720. .probe = velocity_platform_probe,
  2721. .remove = velocity_platform_remove,
  2722. .driver = {
  2723. .name = "via-velocity",
  2724. .of_match_table = velocity_of_ids,
  2725. .pm = &velocity_pm_ops,
  2726. },
  2727. };
  2728. /**
  2729. * velocity_ethtool_up - pre hook for ethtool
  2730. * @dev: network device
  2731. *
  2732. * Called before an ethtool operation. We need to make sure the
  2733. * chip is out of D3 state before we poke at it.
  2734. */
  2735. static int velocity_ethtool_up(struct net_device *dev)
  2736. {
  2737. struct velocity_info *vptr = netdev_priv(dev);
  2738. if (!netif_running(dev))
  2739. velocity_set_power_state(vptr, PCI_D0);
  2740. return 0;
  2741. }
  2742. /**
  2743. * velocity_ethtool_down - post hook for ethtool
  2744. * @dev: network device
  2745. *
  2746. * Called after an ethtool operation. Restore the chip back to D3
  2747. * state if it isn't running.
  2748. */
  2749. static void velocity_ethtool_down(struct net_device *dev)
  2750. {
  2751. struct velocity_info *vptr = netdev_priv(dev);
  2752. if (!netif_running(dev))
  2753. velocity_set_power_state(vptr, PCI_D3hot);
  2754. }
  2755. static int velocity_get_link_ksettings(struct net_device *dev,
  2756. struct ethtool_link_ksettings *cmd)
  2757. {
  2758. struct velocity_info *vptr = netdev_priv(dev);
  2759. struct mac_regs __iomem *regs = vptr->mac_regs;
  2760. u32 status;
  2761. u32 supported, advertising;
  2762. status = check_connection_type(vptr->mac_regs);
  2763. supported = SUPPORTED_TP |
  2764. SUPPORTED_Autoneg |
  2765. SUPPORTED_10baseT_Half |
  2766. SUPPORTED_10baseT_Full |
  2767. SUPPORTED_100baseT_Half |
  2768. SUPPORTED_100baseT_Full |
  2769. SUPPORTED_1000baseT_Half |
  2770. SUPPORTED_1000baseT_Full;
  2771. advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
  2772. if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
  2773. advertising |=
  2774. ADVERTISED_10baseT_Half |
  2775. ADVERTISED_10baseT_Full |
  2776. ADVERTISED_100baseT_Half |
  2777. ADVERTISED_100baseT_Full |
  2778. ADVERTISED_1000baseT_Half |
  2779. ADVERTISED_1000baseT_Full;
  2780. } else {
  2781. switch (vptr->options.spd_dpx) {
  2782. case SPD_DPX_1000_FULL:
  2783. advertising |= ADVERTISED_1000baseT_Full;
  2784. break;
  2785. case SPD_DPX_100_HALF:
  2786. advertising |= ADVERTISED_100baseT_Half;
  2787. break;
  2788. case SPD_DPX_100_FULL:
  2789. advertising |= ADVERTISED_100baseT_Full;
  2790. break;
  2791. case SPD_DPX_10_HALF:
  2792. advertising |= ADVERTISED_10baseT_Half;
  2793. break;
  2794. case SPD_DPX_10_FULL:
  2795. advertising |= ADVERTISED_10baseT_Full;
  2796. break;
  2797. default:
  2798. break;
  2799. }
  2800. }
  2801. if (status & VELOCITY_SPEED_1000)
  2802. cmd->base.speed = SPEED_1000;
  2803. else if (status & VELOCITY_SPEED_100)
  2804. cmd->base.speed = SPEED_100;
  2805. else
  2806. cmd->base.speed = SPEED_10;
  2807. cmd->base.autoneg = (status & VELOCITY_AUTONEG_ENABLE) ?
  2808. AUTONEG_ENABLE : AUTONEG_DISABLE;
  2809. cmd->base.port = PORT_TP;
  2810. cmd->base.phy_address = readb(&regs->MIIADR) & 0x1F;
  2811. if (status & VELOCITY_DUPLEX_FULL)
  2812. cmd->base.duplex = DUPLEX_FULL;
  2813. else
  2814. cmd->base.duplex = DUPLEX_HALF;
  2815. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  2816. supported);
  2817. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  2818. advertising);
  2819. return 0;
  2820. }
  2821. static int velocity_set_link_ksettings(struct net_device *dev,
  2822. const struct ethtool_link_ksettings *cmd)
  2823. {
  2824. struct velocity_info *vptr = netdev_priv(dev);
  2825. u32 speed = cmd->base.speed;
  2826. u32 curr_status;
  2827. u32 new_status = 0;
  2828. int ret = 0;
  2829. curr_status = check_connection_type(vptr->mac_regs);
  2830. curr_status &= (~VELOCITY_LINK_FAIL);
  2831. new_status |= ((cmd->base.autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
  2832. new_status |= ((speed == SPEED_1000) ? VELOCITY_SPEED_1000 : 0);
  2833. new_status |= ((speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
  2834. new_status |= ((speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
  2835. new_status |= ((cmd->base.duplex == DUPLEX_FULL) ?
  2836. VELOCITY_DUPLEX_FULL : 0);
  2837. if ((new_status & VELOCITY_AUTONEG_ENABLE) &&
  2838. (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE))) {
  2839. ret = -EINVAL;
  2840. } else {
  2841. enum speed_opt spd_dpx;
  2842. if (new_status & VELOCITY_AUTONEG_ENABLE)
  2843. spd_dpx = SPD_DPX_AUTO;
  2844. else if ((new_status & VELOCITY_SPEED_1000) &&
  2845. (new_status & VELOCITY_DUPLEX_FULL)) {
  2846. spd_dpx = SPD_DPX_1000_FULL;
  2847. } else if (new_status & VELOCITY_SPEED_100)
  2848. spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
  2849. SPD_DPX_100_FULL : SPD_DPX_100_HALF;
  2850. else if (new_status & VELOCITY_SPEED_10)
  2851. spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
  2852. SPD_DPX_10_FULL : SPD_DPX_10_HALF;
  2853. else
  2854. return -EOPNOTSUPP;
  2855. vptr->options.spd_dpx = spd_dpx;
  2856. velocity_set_media_mode(vptr, new_status);
  2857. }
  2858. return ret;
  2859. }
  2860. static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  2861. {
  2862. struct velocity_info *vptr = netdev_priv(dev);
  2863. strlcpy(info->driver, VELOCITY_NAME, sizeof(info->driver));
  2864. strlcpy(info->version, VELOCITY_VERSION, sizeof(info->version));
  2865. if (vptr->pdev)
  2866. strlcpy(info->bus_info, pci_name(vptr->pdev),
  2867. sizeof(info->bus_info));
  2868. else
  2869. strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
  2870. }
  2871. static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2872. {
  2873. struct velocity_info *vptr = netdev_priv(dev);
  2874. wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
  2875. wol->wolopts |= WAKE_MAGIC;
  2876. /*
  2877. if (vptr->wol_opts & VELOCITY_WOL_PHY)
  2878. wol.wolopts|=WAKE_PHY;
  2879. */
  2880. if (vptr->wol_opts & VELOCITY_WOL_UCAST)
  2881. wol->wolopts |= WAKE_UCAST;
  2882. if (vptr->wol_opts & VELOCITY_WOL_ARP)
  2883. wol->wolopts |= WAKE_ARP;
  2884. memcpy(&wol->sopass, vptr->wol_passwd, 6);
  2885. }
  2886. static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2887. {
  2888. struct velocity_info *vptr = netdev_priv(dev);
  2889. if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
  2890. return -EFAULT;
  2891. vptr->wol_opts = VELOCITY_WOL_MAGIC;
  2892. /*
  2893. if (wol.wolopts & WAKE_PHY) {
  2894. vptr->wol_opts|=VELOCITY_WOL_PHY;
  2895. vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
  2896. }
  2897. */
  2898. if (wol->wolopts & WAKE_MAGIC) {
  2899. vptr->wol_opts |= VELOCITY_WOL_MAGIC;
  2900. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  2901. }
  2902. if (wol->wolopts & WAKE_UCAST) {
  2903. vptr->wol_opts |= VELOCITY_WOL_UCAST;
  2904. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  2905. }
  2906. if (wol->wolopts & WAKE_ARP) {
  2907. vptr->wol_opts |= VELOCITY_WOL_ARP;
  2908. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  2909. }
  2910. memcpy(vptr->wol_passwd, wol->sopass, 6);
  2911. return 0;
  2912. }
  2913. static u32 velocity_get_msglevel(struct net_device *dev)
  2914. {
  2915. return msglevel;
  2916. }
  2917. static void velocity_set_msglevel(struct net_device *dev, u32 value)
  2918. {
  2919. msglevel = value;
  2920. }
  2921. static int get_pending_timer_val(int val)
  2922. {
  2923. int mult_bits = val >> 6;
  2924. int mult = 1;
  2925. switch (mult_bits)
  2926. {
  2927. case 1:
  2928. mult = 4; break;
  2929. case 2:
  2930. mult = 16; break;
  2931. case 3:
  2932. mult = 64; break;
  2933. case 0:
  2934. default:
  2935. break;
  2936. }
  2937. return (val & 0x3f) * mult;
  2938. }
  2939. static void set_pending_timer_val(int *val, u32 us)
  2940. {
  2941. u8 mult = 0;
  2942. u8 shift = 0;
  2943. if (us >= 0x3f) {
  2944. mult = 1; /* mult with 4 */
  2945. shift = 2;
  2946. }
  2947. if (us >= 0x3f * 4) {
  2948. mult = 2; /* mult with 16 */
  2949. shift = 4;
  2950. }
  2951. if (us >= 0x3f * 16) {
  2952. mult = 3; /* mult with 64 */
  2953. shift = 6;
  2954. }
  2955. *val = (mult << 6) | ((us >> shift) & 0x3f);
  2956. }
  2957. static int velocity_get_coalesce(struct net_device *dev,
  2958. struct ethtool_coalesce *ecmd)
  2959. {
  2960. struct velocity_info *vptr = netdev_priv(dev);
  2961. ecmd->tx_max_coalesced_frames = vptr->options.tx_intsup;
  2962. ecmd->rx_max_coalesced_frames = vptr->options.rx_intsup;
  2963. ecmd->rx_coalesce_usecs = get_pending_timer_val(vptr->options.rxqueue_timer);
  2964. ecmd->tx_coalesce_usecs = get_pending_timer_val(vptr->options.txqueue_timer);
  2965. return 0;
  2966. }
  2967. static int velocity_set_coalesce(struct net_device *dev,
  2968. struct ethtool_coalesce *ecmd)
  2969. {
  2970. struct velocity_info *vptr = netdev_priv(dev);
  2971. int max_us = 0x3f * 64;
  2972. unsigned long flags;
  2973. /* 6 bits of */
  2974. if (ecmd->tx_coalesce_usecs > max_us)
  2975. return -EINVAL;
  2976. if (ecmd->rx_coalesce_usecs > max_us)
  2977. return -EINVAL;
  2978. if (ecmd->tx_max_coalesced_frames > 0xff)
  2979. return -EINVAL;
  2980. if (ecmd->rx_max_coalesced_frames > 0xff)
  2981. return -EINVAL;
  2982. vptr->options.rx_intsup = ecmd->rx_max_coalesced_frames;
  2983. vptr->options.tx_intsup = ecmd->tx_max_coalesced_frames;
  2984. set_pending_timer_val(&vptr->options.rxqueue_timer,
  2985. ecmd->rx_coalesce_usecs);
  2986. set_pending_timer_val(&vptr->options.txqueue_timer,
  2987. ecmd->tx_coalesce_usecs);
  2988. /* Setup the interrupt suppression and queue timers */
  2989. spin_lock_irqsave(&vptr->lock, flags);
  2990. mac_disable_int(vptr->mac_regs);
  2991. setup_adaptive_interrupts(vptr);
  2992. setup_queue_timers(vptr);
  2993. mac_write_int_mask(vptr->int_mask, vptr->mac_regs);
  2994. mac_clear_isr(vptr->mac_regs);
  2995. mac_enable_int(vptr->mac_regs);
  2996. spin_unlock_irqrestore(&vptr->lock, flags);
  2997. return 0;
  2998. }
  2999. static const char velocity_gstrings[][ETH_GSTRING_LEN] = {
  3000. "rx_all",
  3001. "rx_ok",
  3002. "tx_ok",
  3003. "rx_error",
  3004. "rx_runt_ok",
  3005. "rx_runt_err",
  3006. "rx_64",
  3007. "tx_64",
  3008. "rx_65_to_127",
  3009. "tx_65_to_127",
  3010. "rx_128_to_255",
  3011. "tx_128_to_255",
  3012. "rx_256_to_511",
  3013. "tx_256_to_511",
  3014. "rx_512_to_1023",
  3015. "tx_512_to_1023",
  3016. "rx_1024_to_1518",
  3017. "tx_1024_to_1518",
  3018. "tx_ether_collisions",
  3019. "rx_crc_errors",
  3020. "rx_jumbo",
  3021. "tx_jumbo",
  3022. "rx_mac_control_frames",
  3023. "tx_mac_control_frames",
  3024. "rx_frame_alignment_errors",
  3025. "rx_long_ok",
  3026. "rx_long_err",
  3027. "tx_sqe_errors",
  3028. "rx_no_buf",
  3029. "rx_symbol_errors",
  3030. "in_range_length_errors",
  3031. "late_collisions"
  3032. };
  3033. static void velocity_get_strings(struct net_device *dev, u32 sset, u8 *data)
  3034. {
  3035. switch (sset) {
  3036. case ETH_SS_STATS:
  3037. memcpy(data, *velocity_gstrings, sizeof(velocity_gstrings));
  3038. break;
  3039. }
  3040. }
  3041. static int velocity_get_sset_count(struct net_device *dev, int sset)
  3042. {
  3043. switch (sset) {
  3044. case ETH_SS_STATS:
  3045. return ARRAY_SIZE(velocity_gstrings);
  3046. default:
  3047. return -EOPNOTSUPP;
  3048. }
  3049. }
  3050. static void velocity_get_ethtool_stats(struct net_device *dev,
  3051. struct ethtool_stats *stats, u64 *data)
  3052. {
  3053. if (netif_running(dev)) {
  3054. struct velocity_info *vptr = netdev_priv(dev);
  3055. u32 *p = vptr->mib_counter;
  3056. int i;
  3057. spin_lock_irq(&vptr->lock);
  3058. velocity_update_hw_mibs(vptr);
  3059. spin_unlock_irq(&vptr->lock);
  3060. for (i = 0; i < ARRAY_SIZE(velocity_gstrings); i++)
  3061. *data++ = *p++;
  3062. }
  3063. }
  3064. static const struct ethtool_ops velocity_ethtool_ops = {
  3065. .get_drvinfo = velocity_get_drvinfo,
  3066. .get_wol = velocity_ethtool_get_wol,
  3067. .set_wol = velocity_ethtool_set_wol,
  3068. .get_msglevel = velocity_get_msglevel,
  3069. .set_msglevel = velocity_set_msglevel,
  3070. .get_link = velocity_get_link,
  3071. .get_strings = velocity_get_strings,
  3072. .get_sset_count = velocity_get_sset_count,
  3073. .get_ethtool_stats = velocity_get_ethtool_stats,
  3074. .get_coalesce = velocity_get_coalesce,
  3075. .set_coalesce = velocity_set_coalesce,
  3076. .begin = velocity_ethtool_up,
  3077. .complete = velocity_ethtool_down,
  3078. .get_link_ksettings = velocity_get_link_ksettings,
  3079. .set_link_ksettings = velocity_set_link_ksettings,
  3080. };
  3081. #if defined(CONFIG_PM) && defined(CONFIG_INET)
  3082. static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
  3083. {
  3084. struct in_ifaddr *ifa = ptr;
  3085. struct net_device *dev = ifa->ifa_dev->dev;
  3086. if (dev_net(dev) == &init_net &&
  3087. dev->netdev_ops == &velocity_netdev_ops)
  3088. velocity_get_ip(netdev_priv(dev));
  3089. return NOTIFY_DONE;
  3090. }
  3091. static struct notifier_block velocity_inetaddr_notifier = {
  3092. .notifier_call = velocity_netdev_event,
  3093. };
  3094. static void velocity_register_notifier(void)
  3095. {
  3096. register_inetaddr_notifier(&velocity_inetaddr_notifier);
  3097. }
  3098. static void velocity_unregister_notifier(void)
  3099. {
  3100. unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
  3101. }
  3102. #else
  3103. #define velocity_register_notifier() do {} while (0)
  3104. #define velocity_unregister_notifier() do {} while (0)
  3105. #endif /* defined(CONFIG_PM) && defined(CONFIG_INET) */
  3106. /**
  3107. * velocity_init_module - load time function
  3108. *
  3109. * Called when the velocity module is loaded. The PCI driver
  3110. * is registered with the PCI layer, and in turn will call
  3111. * the probe functions for each velocity adapter installed
  3112. * in the system.
  3113. */
  3114. static int __init velocity_init_module(void)
  3115. {
  3116. int ret_pci, ret_platform;
  3117. velocity_register_notifier();
  3118. ret_pci = pci_register_driver(&velocity_pci_driver);
  3119. ret_platform = platform_driver_register(&velocity_platform_driver);
  3120. /* if both_registers failed, remove the notifier */
  3121. if ((ret_pci < 0) && (ret_platform < 0)) {
  3122. velocity_unregister_notifier();
  3123. return ret_pci;
  3124. }
  3125. return 0;
  3126. }
  3127. /**
  3128. * velocity_cleanup - module unload
  3129. *
  3130. * When the velocity hardware is unloaded this function is called.
  3131. * It will clean up the notifiers and the unregister the PCI
  3132. * driver interface for this hardware. This in turn cleans up
  3133. * all discovered interfaces before returning from the function
  3134. */
  3135. static void __exit velocity_cleanup_module(void)
  3136. {
  3137. velocity_unregister_notifier();
  3138. pci_unregister_driver(&velocity_pci_driver);
  3139. platform_driver_unregister(&velocity_platform_driver);
  3140. }
  3141. module_init(velocity_init_module);
  3142. module_exit(velocity_cleanup_module);