via-velocity.c 89 KB

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