via-velocity.c 91 KB

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