via-velocity.c 89 KB

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