via-velocity.c 86 KB

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