via-velocity.c 95 KB

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