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