via-velocity.c 95 KB

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