via-velocity.c 95 KB

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