via-velocity.c 86 KB

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