via-velocity.c 88 KB

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