via-velocity.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494
  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@lxorguk.ukuu.org.uk>
  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 irqreturn_t 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, const 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, const 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, const 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. static const struct net_device_ops velocity_netdev_ops = {
  702. .ndo_open = velocity_open,
  703. .ndo_stop = velocity_close,
  704. .ndo_start_xmit = velocity_xmit,
  705. .ndo_get_stats = velocity_get_stats,
  706. .ndo_validate_addr = eth_validate_addr,
  707. .ndo_set_mac_address = eth_mac_addr,
  708. .ndo_set_multicast_list = velocity_set_multi,
  709. .ndo_change_mtu = velocity_change_mtu,
  710. .ndo_do_ioctl = velocity_ioctl,
  711. .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
  712. .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
  713. .ndo_vlan_rx_register = velocity_vlan_rx_register,
  714. };
  715. /**
  716. * velocity_found1 - set up discovered velocity card
  717. * @pdev: PCI device
  718. * @ent: PCI device table entry that matched
  719. *
  720. * Configure a discovered adapter from scratch. Return a negative
  721. * errno error code on failure paths.
  722. */
  723. static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
  724. {
  725. static int first = 1;
  726. struct net_device *dev;
  727. int i;
  728. const char *drv_string;
  729. const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
  730. struct velocity_info *vptr;
  731. struct mac_regs __iomem * regs;
  732. int ret = -ENOMEM;
  733. /* FIXME: this driver, like almost all other ethernet drivers,
  734. * can support more than MAX_UNITS.
  735. */
  736. if (velocity_nics >= MAX_UNITS) {
  737. dev_notice(&pdev->dev, "already found %d NICs.\n",
  738. velocity_nics);
  739. return -ENODEV;
  740. }
  741. dev = alloc_etherdev(sizeof(struct velocity_info));
  742. if (!dev) {
  743. dev_err(&pdev->dev, "allocate net device failed.\n");
  744. goto out;
  745. }
  746. /* Chain it all together */
  747. SET_NETDEV_DEV(dev, &pdev->dev);
  748. vptr = netdev_priv(dev);
  749. if (first) {
  750. printk(KERN_INFO "%s Ver. %s\n",
  751. VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
  752. printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
  753. printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
  754. first = 0;
  755. }
  756. velocity_init_info(pdev, vptr, info);
  757. vptr->dev = dev;
  758. dev->irq = pdev->irq;
  759. ret = pci_enable_device(pdev);
  760. if (ret < 0)
  761. goto err_free_dev;
  762. ret = velocity_get_pci_info(vptr, pdev);
  763. if (ret < 0) {
  764. /* error message already printed */
  765. goto err_disable;
  766. }
  767. ret = pci_request_regions(pdev, VELOCITY_NAME);
  768. if (ret < 0) {
  769. dev_err(&pdev->dev, "No PCI resources.\n");
  770. goto err_disable;
  771. }
  772. regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
  773. if (regs == NULL) {
  774. ret = -EIO;
  775. goto err_release_res;
  776. }
  777. vptr->mac_regs = regs;
  778. mac_wol_reset(regs);
  779. dev->base_addr = vptr->ioaddr;
  780. for (i = 0; i < 6; i++)
  781. dev->dev_addr[i] = readb(&regs->PAR[i]);
  782. drv_string = dev_driver_string(&pdev->dev);
  783. velocity_get_options(&vptr->options, velocity_nics, drv_string);
  784. /*
  785. * Mask out the options cannot be set to the chip
  786. */
  787. vptr->options.flags &= info->flags;
  788. /*
  789. * Enable the chip specified capbilities
  790. */
  791. vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
  792. vptr->wol_opts = vptr->options.wol_opts;
  793. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  794. vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
  795. dev->irq = pdev->irq;
  796. dev->netdev_ops = &velocity_netdev_ops;
  797. dev->ethtool_ops = &velocity_ethtool_ops;
  798. #ifdef VELOCITY_ZERO_COPY_SUPPORT
  799. dev->features |= NETIF_F_SG;
  800. #endif
  801. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER |
  802. NETIF_F_HW_VLAN_RX;
  803. if (vptr->flags & VELOCITY_FLAGS_TX_CSUM)
  804. dev->features |= NETIF_F_IP_CSUM;
  805. ret = register_netdev(dev);
  806. if (ret < 0)
  807. goto err_iounmap;
  808. if (!velocity_get_link(dev)) {
  809. netif_carrier_off(dev);
  810. vptr->mii_status |= VELOCITY_LINK_FAIL;
  811. }
  812. velocity_print_info(vptr);
  813. pci_set_drvdata(pdev, dev);
  814. /* and leave the chip powered down */
  815. pci_set_power_state(pdev, PCI_D3hot);
  816. #ifdef CONFIG_PM
  817. {
  818. unsigned long flags;
  819. spin_lock_irqsave(&velocity_dev_list_lock, flags);
  820. list_add(&vptr->list, &velocity_dev_list);
  821. spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
  822. }
  823. #endif
  824. velocity_nics++;
  825. out:
  826. return ret;
  827. err_iounmap:
  828. iounmap(regs);
  829. err_release_res:
  830. pci_release_regions(pdev);
  831. err_disable:
  832. pci_disable_device(pdev);
  833. err_free_dev:
  834. free_netdev(dev);
  835. goto out;
  836. }
  837. /**
  838. * velocity_print_info - per driver data
  839. * @vptr: velocity
  840. *
  841. * Print per driver data as the kernel driver finds Velocity
  842. * hardware
  843. */
  844. static void __devinit velocity_print_info(struct velocity_info *vptr)
  845. {
  846. struct net_device *dev = vptr->dev;
  847. printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
  848. printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
  849. dev->name,
  850. dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
  851. dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
  852. }
  853. /**
  854. * velocity_init_info - init private data
  855. * @pdev: PCI device
  856. * @vptr: Velocity info
  857. * @info: Board type
  858. *
  859. * Set up the initial velocity_info struct for the device that has been
  860. * discovered.
  861. */
  862. static void __devinit velocity_init_info(struct pci_dev *pdev,
  863. struct velocity_info *vptr,
  864. const struct velocity_info_tbl *info)
  865. {
  866. memset(vptr, 0, sizeof(struct velocity_info));
  867. vptr->pdev = pdev;
  868. vptr->chip_id = info->chip_id;
  869. vptr->tx.numq = info->txqueue;
  870. vptr->multicast_limit = MCAM_SIZE;
  871. spin_lock_init(&vptr->lock);
  872. INIT_LIST_HEAD(&vptr->list);
  873. }
  874. /**
  875. * velocity_get_pci_info - retrieve PCI info for device
  876. * @vptr: velocity device
  877. * @pdev: PCI device it matches
  878. *
  879. * Retrieve the PCI configuration space data that interests us from
  880. * the kernel PCI layer
  881. */
  882. static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
  883. {
  884. vptr->rev_id = pdev->revision;
  885. pci_set_master(pdev);
  886. vptr->ioaddr = pci_resource_start(pdev, 0);
  887. vptr->memaddr = pci_resource_start(pdev, 1);
  888. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
  889. dev_err(&pdev->dev,
  890. "region #0 is not an I/O resource, aborting.\n");
  891. return -EINVAL;
  892. }
  893. if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
  894. dev_err(&pdev->dev,
  895. "region #1 is an I/O resource, aborting.\n");
  896. return -EINVAL;
  897. }
  898. if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
  899. dev_err(&pdev->dev, "region #1 is too small.\n");
  900. return -EINVAL;
  901. }
  902. vptr->pdev = pdev;
  903. return 0;
  904. }
  905. /**
  906. * velocity_init_dma_rings - set up DMA rings
  907. * @vptr: Velocity to set up
  908. *
  909. * Allocate PCI mapped DMA rings for the receive and transmit layer
  910. * to use.
  911. */
  912. static int velocity_init_dma_rings(struct velocity_info *vptr)
  913. {
  914. struct velocity_opt *opt = &vptr->options;
  915. const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
  916. const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
  917. struct pci_dev *pdev = vptr->pdev;
  918. dma_addr_t pool_dma;
  919. void *pool;
  920. unsigned int i;
  921. /*
  922. * Allocate all RD/TD rings a single pool.
  923. *
  924. * pci_alloc_consistent() fulfills the requirement for 64 bytes
  925. * alignment
  926. */
  927. pool = pci_alloc_consistent(pdev, tx_ring_size * vptr->tx.numq +
  928. rx_ring_size, &pool_dma);
  929. if (!pool) {
  930. dev_err(&pdev->dev, "%s : DMA memory allocation failed.\n",
  931. vptr->dev->name);
  932. return -ENOMEM;
  933. }
  934. vptr->rx.ring = pool;
  935. vptr->rx.pool_dma = pool_dma;
  936. pool += rx_ring_size;
  937. pool_dma += rx_ring_size;
  938. for (i = 0; i < vptr->tx.numq; i++) {
  939. vptr->tx.rings[i] = pool;
  940. vptr->tx.pool_dma[i] = pool_dma;
  941. pool += tx_ring_size;
  942. pool_dma += tx_ring_size;
  943. }
  944. return 0;
  945. }
  946. /**
  947. * velocity_free_dma_rings - free PCI ring pointers
  948. * @vptr: Velocity to free from
  949. *
  950. * Clean up the PCI ring buffers allocated to this velocity.
  951. */
  952. static void velocity_free_dma_rings(struct velocity_info *vptr)
  953. {
  954. const int size = vptr->options.numrx * sizeof(struct rx_desc) +
  955. vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
  956. pci_free_consistent(vptr->pdev, size, vptr->rx.ring, vptr->rx.pool_dma);
  957. }
  958. static void velocity_give_many_rx_descs(struct velocity_info *vptr)
  959. {
  960. struct mac_regs __iomem *regs = vptr->mac_regs;
  961. int avail, dirty, unusable;
  962. /*
  963. * RD number must be equal to 4X per hardware spec
  964. * (programming guide rev 1.20, p.13)
  965. */
  966. if (vptr->rx.filled < 4)
  967. return;
  968. wmb();
  969. unusable = vptr->rx.filled & 0x0003;
  970. dirty = vptr->rx.dirty - unusable;
  971. for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
  972. dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
  973. vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
  974. }
  975. writew(vptr->rx.filled & 0xfffc, &regs->RBRDU);
  976. vptr->rx.filled = unusable;
  977. }
  978. static int velocity_rx_refill(struct velocity_info *vptr)
  979. {
  980. int dirty = vptr->rx.dirty, done = 0;
  981. do {
  982. struct rx_desc *rd = vptr->rx.ring + dirty;
  983. /* Fine for an all zero Rx desc at init time as well */
  984. if (rd->rdesc0.len & OWNED_BY_NIC)
  985. break;
  986. if (!vptr->rx.info[dirty].skb) {
  987. if (velocity_alloc_rx_buf(vptr, dirty) < 0)
  988. break;
  989. }
  990. done++;
  991. dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
  992. } while (dirty != vptr->rx.curr);
  993. if (done) {
  994. vptr->rx.dirty = dirty;
  995. vptr->rx.filled += done;
  996. }
  997. return done;
  998. }
  999. static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
  1000. {
  1001. vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
  1002. }
  1003. /**
  1004. * velocity_init_rd_ring - set up receive ring
  1005. * @vptr: velocity to configure
  1006. *
  1007. * Allocate and set up the receive buffers for each ring slot and
  1008. * assign them to the network adapter.
  1009. */
  1010. static int velocity_init_rd_ring(struct velocity_info *vptr)
  1011. {
  1012. int ret = -ENOMEM;
  1013. vptr->rx.info = kcalloc(vptr->options.numrx,
  1014. sizeof(struct velocity_rd_info), GFP_KERNEL);
  1015. if (!vptr->rx.info)
  1016. goto out;
  1017. velocity_init_rx_ring_indexes(vptr);
  1018. if (velocity_rx_refill(vptr) != vptr->options.numrx) {
  1019. VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
  1020. "%s: failed to allocate RX buffer.\n", vptr->dev->name);
  1021. velocity_free_rd_ring(vptr);
  1022. goto out;
  1023. }
  1024. ret = 0;
  1025. out:
  1026. return ret;
  1027. }
  1028. /**
  1029. * velocity_free_rd_ring - free receive ring
  1030. * @vptr: velocity to clean up
  1031. *
  1032. * Free the receive buffers for each ring slot and any
  1033. * attached socket buffers that need to go away.
  1034. */
  1035. static void velocity_free_rd_ring(struct velocity_info *vptr)
  1036. {
  1037. int i;
  1038. if (vptr->rx.info == NULL)
  1039. return;
  1040. for (i = 0; i < vptr->options.numrx; i++) {
  1041. struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
  1042. struct rx_desc *rd = vptr->rx.ring + i;
  1043. memset(rd, 0, sizeof(*rd));
  1044. if (!rd_info->skb)
  1045. continue;
  1046. pci_unmap_single(vptr->pdev, rd_info->skb_dma, vptr->rx.buf_sz,
  1047. PCI_DMA_FROMDEVICE);
  1048. rd_info->skb_dma = 0;
  1049. dev_kfree_skb(rd_info->skb);
  1050. rd_info->skb = NULL;
  1051. }
  1052. kfree(vptr->rx.info);
  1053. vptr->rx.info = NULL;
  1054. }
  1055. /**
  1056. * velocity_init_td_ring - set up transmit ring
  1057. * @vptr: velocity
  1058. *
  1059. * Set up the transmit ring and chain the ring pointers together.
  1060. * Returns zero on success or a negative posix errno code for
  1061. * failure.
  1062. */
  1063. static int velocity_init_td_ring(struct velocity_info *vptr)
  1064. {
  1065. dma_addr_t curr;
  1066. int j;
  1067. /* Init the TD ring entries */
  1068. for (j = 0; j < vptr->tx.numq; j++) {
  1069. curr = vptr->tx.pool_dma[j];
  1070. vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
  1071. sizeof(struct velocity_td_info),
  1072. GFP_KERNEL);
  1073. if (!vptr->tx.infos[j]) {
  1074. while(--j >= 0)
  1075. kfree(vptr->tx.infos[j]);
  1076. return -ENOMEM;
  1077. }
  1078. vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
  1079. }
  1080. return 0;
  1081. }
  1082. /*
  1083. * FIXME: could we merge this with velocity_free_tx_buf ?
  1084. */
  1085. static void velocity_free_td_ring_entry(struct velocity_info *vptr,
  1086. int q, int n)
  1087. {
  1088. struct velocity_td_info * td_info = &(vptr->tx.infos[q][n]);
  1089. int i;
  1090. if (td_info == NULL)
  1091. return;
  1092. if (td_info->skb) {
  1093. for (i = 0; i < td_info->nskb_dma; i++)
  1094. {
  1095. if (td_info->skb_dma[i]) {
  1096. pci_unmap_single(vptr->pdev, td_info->skb_dma[i],
  1097. td_info->skb->len, PCI_DMA_TODEVICE);
  1098. td_info->skb_dma[i] = 0;
  1099. }
  1100. }
  1101. dev_kfree_skb(td_info->skb);
  1102. td_info->skb = NULL;
  1103. }
  1104. }
  1105. /**
  1106. * velocity_free_td_ring - free td ring
  1107. * @vptr: velocity
  1108. *
  1109. * Free up the transmit ring for this particular velocity adapter.
  1110. * We free the ring contents but not the ring itself.
  1111. */
  1112. static void velocity_free_td_ring(struct velocity_info *vptr)
  1113. {
  1114. int i, j;
  1115. for (j = 0; j < vptr->tx.numq; j++) {
  1116. if (vptr->tx.infos[j] == NULL)
  1117. continue;
  1118. for (i = 0; i < vptr->options.numtx; i++) {
  1119. velocity_free_td_ring_entry(vptr, j, i);
  1120. }
  1121. kfree(vptr->tx.infos[j]);
  1122. vptr->tx.infos[j] = NULL;
  1123. }
  1124. }
  1125. /**
  1126. * velocity_rx_srv - service RX interrupt
  1127. * @vptr: velocity
  1128. * @status: adapter status (unused)
  1129. *
  1130. * Walk the receive ring of the velocity adapter and remove
  1131. * any received packets from the receive queue. Hand the ring
  1132. * slots back to the adapter for reuse.
  1133. */
  1134. static int velocity_rx_srv(struct velocity_info *vptr, int status)
  1135. {
  1136. struct net_device_stats *stats = &vptr->dev->stats;
  1137. int rd_curr = vptr->rx.curr;
  1138. int works = 0;
  1139. do {
  1140. struct rx_desc *rd = vptr->rx.ring + rd_curr;
  1141. if (!vptr->rx.info[rd_curr].skb)
  1142. break;
  1143. if (rd->rdesc0.len & OWNED_BY_NIC)
  1144. break;
  1145. rmb();
  1146. /*
  1147. * Don't drop CE or RL error frame although RXOK is off
  1148. */
  1149. if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
  1150. if (velocity_receive_frame(vptr, rd_curr) < 0)
  1151. stats->rx_dropped++;
  1152. } else {
  1153. if (rd->rdesc0.RSR & RSR_CRC)
  1154. stats->rx_crc_errors++;
  1155. if (rd->rdesc0.RSR & RSR_FAE)
  1156. stats->rx_frame_errors++;
  1157. stats->rx_dropped++;
  1158. }
  1159. rd->size |= RX_INTEN;
  1160. rd_curr++;
  1161. if (rd_curr >= vptr->options.numrx)
  1162. rd_curr = 0;
  1163. } while (++works <= 15);
  1164. vptr->rx.curr = rd_curr;
  1165. if ((works > 0) && (velocity_rx_refill(vptr) > 0))
  1166. velocity_give_many_rx_descs(vptr);
  1167. VAR_USED(stats);
  1168. return works;
  1169. }
  1170. /**
  1171. * velocity_rx_csum - checksum process
  1172. * @rd: receive packet descriptor
  1173. * @skb: network layer packet buffer
  1174. *
  1175. * Process the status bits for the received packet and determine
  1176. * if the checksum was computed and verified by the hardware
  1177. */
  1178. static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
  1179. {
  1180. skb->ip_summed = CHECKSUM_NONE;
  1181. if (rd->rdesc1.CSM & CSM_IPKT) {
  1182. if (rd->rdesc1.CSM & CSM_IPOK) {
  1183. if ((rd->rdesc1.CSM & CSM_TCPKT) ||
  1184. (rd->rdesc1.CSM & CSM_UDPKT)) {
  1185. if (!(rd->rdesc1.CSM & CSM_TUPOK)) {
  1186. return;
  1187. }
  1188. }
  1189. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1190. }
  1191. }
  1192. }
  1193. /**
  1194. * velocity_rx_copy - in place Rx copy for small packets
  1195. * @rx_skb: network layer packet buffer candidate
  1196. * @pkt_size: received data size
  1197. * @rd: receive packet descriptor
  1198. * @dev: network device
  1199. *
  1200. * Replace the current skb that is scheduled for Rx processing by a
  1201. * shorter, immediatly allocated skb, if the received packet is small
  1202. * enough. This function returns a negative value if the received
  1203. * packet is too big or if memory is exhausted.
  1204. */
  1205. static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
  1206. struct velocity_info *vptr)
  1207. {
  1208. int ret = -1;
  1209. if (pkt_size < rx_copybreak) {
  1210. struct sk_buff *new_skb;
  1211. new_skb = netdev_alloc_skb(vptr->dev, pkt_size + 2);
  1212. if (new_skb) {
  1213. new_skb->ip_summed = rx_skb[0]->ip_summed;
  1214. skb_reserve(new_skb, 2);
  1215. skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
  1216. *rx_skb = new_skb;
  1217. ret = 0;
  1218. }
  1219. }
  1220. return ret;
  1221. }
  1222. /**
  1223. * velocity_iph_realign - IP header alignment
  1224. * @vptr: velocity we are handling
  1225. * @skb: network layer packet buffer
  1226. * @pkt_size: received data size
  1227. *
  1228. * Align IP header on a 2 bytes boundary. This behavior can be
  1229. * configured by the user.
  1230. */
  1231. static inline void velocity_iph_realign(struct velocity_info *vptr,
  1232. struct sk_buff *skb, int pkt_size)
  1233. {
  1234. if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
  1235. memmove(skb->data + 2, skb->data, pkt_size);
  1236. skb_reserve(skb, 2);
  1237. }
  1238. }
  1239. /**
  1240. * velocity_receive_frame - received packet processor
  1241. * @vptr: velocity we are handling
  1242. * @idx: ring index
  1243. *
  1244. * A packet has arrived. We process the packet and if appropriate
  1245. * pass the frame up the network stack
  1246. */
  1247. static int velocity_receive_frame(struct velocity_info *vptr, int idx)
  1248. {
  1249. void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
  1250. struct net_device_stats *stats = &vptr->dev->stats;
  1251. struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
  1252. struct rx_desc *rd = &(vptr->rx.ring[idx]);
  1253. int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
  1254. struct sk_buff *skb;
  1255. if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
  1256. VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->dev->name);
  1257. stats->rx_length_errors++;
  1258. return -EINVAL;
  1259. }
  1260. if (rd->rdesc0.RSR & RSR_MAR)
  1261. stats->multicast++;
  1262. skb = rd_info->skb;
  1263. pci_dma_sync_single_for_cpu(vptr->pdev, rd_info->skb_dma,
  1264. vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
  1265. /*
  1266. * Drop frame not meeting IEEE 802.3
  1267. */
  1268. if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
  1269. if (rd->rdesc0.RSR & RSR_RL) {
  1270. stats->rx_length_errors++;
  1271. return -EINVAL;
  1272. }
  1273. }
  1274. pci_action = pci_dma_sync_single_for_device;
  1275. velocity_rx_csum(rd, skb);
  1276. if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
  1277. velocity_iph_realign(vptr, skb, pkt_len);
  1278. pci_action = pci_unmap_single;
  1279. rd_info->skb = NULL;
  1280. }
  1281. pci_action(vptr->pdev, rd_info->skb_dma, vptr->rx.buf_sz,
  1282. PCI_DMA_FROMDEVICE);
  1283. skb_put(skb, pkt_len - 4);
  1284. skb->protocol = eth_type_trans(skb, vptr->dev);
  1285. if (vptr->vlgrp && (rd->rdesc0.RSR & RSR_DETAG)) {
  1286. vlan_hwaccel_rx(skb, vptr->vlgrp,
  1287. swab16(le16_to_cpu(rd->rdesc1.PQTAG)));
  1288. } else
  1289. netif_rx(skb);
  1290. stats->rx_bytes += pkt_len;
  1291. return 0;
  1292. }
  1293. /**
  1294. * velocity_alloc_rx_buf - allocate aligned receive buffer
  1295. * @vptr: velocity
  1296. * @idx: ring index
  1297. *
  1298. * Allocate a new full sized buffer for the reception of a frame and
  1299. * map it into PCI space for the hardware to use. The hardware
  1300. * requires *64* byte alignment of the buffer which makes life
  1301. * less fun than would be ideal.
  1302. */
  1303. static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
  1304. {
  1305. struct rx_desc *rd = &(vptr->rx.ring[idx]);
  1306. struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
  1307. rd_info->skb = dev_alloc_skb(vptr->rx.buf_sz + 64);
  1308. if (rd_info->skb == NULL)
  1309. return -ENOMEM;
  1310. /*
  1311. * Do the gymnastics to get the buffer head for data at
  1312. * 64byte alignment.
  1313. */
  1314. skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
  1315. rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data,
  1316. vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
  1317. /*
  1318. * Fill in the descriptor to match
  1319. */
  1320. *((u32 *) & (rd->rdesc0)) = 0;
  1321. rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
  1322. rd->pa_low = cpu_to_le32(rd_info->skb_dma);
  1323. rd->pa_high = 0;
  1324. return 0;
  1325. }
  1326. /**
  1327. * tx_srv - transmit interrupt service
  1328. * @vptr; Velocity
  1329. * @status:
  1330. *
  1331. * Scan the queues looking for transmitted packets that
  1332. * we can complete and clean up. Update any statistics as
  1333. * necessary/
  1334. */
  1335. static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
  1336. {
  1337. struct tx_desc *td;
  1338. int qnum;
  1339. int full = 0;
  1340. int idx;
  1341. int works = 0;
  1342. struct velocity_td_info *tdinfo;
  1343. struct net_device_stats *stats = &vptr->dev->stats;
  1344. for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
  1345. for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
  1346. idx = (idx + 1) % vptr->options.numtx) {
  1347. /*
  1348. * Get Tx Descriptor
  1349. */
  1350. td = &(vptr->tx.rings[qnum][idx]);
  1351. tdinfo = &(vptr->tx.infos[qnum][idx]);
  1352. if (td->tdesc0.len & OWNED_BY_NIC)
  1353. break;
  1354. if ((works++ > 15))
  1355. break;
  1356. if (td->tdesc0.TSR & TSR0_TERR) {
  1357. stats->tx_errors++;
  1358. stats->tx_dropped++;
  1359. if (td->tdesc0.TSR & TSR0_CDH)
  1360. stats->tx_heartbeat_errors++;
  1361. if (td->tdesc0.TSR & TSR0_CRS)
  1362. stats->tx_carrier_errors++;
  1363. if (td->tdesc0.TSR & TSR0_ABT)
  1364. stats->tx_aborted_errors++;
  1365. if (td->tdesc0.TSR & TSR0_OWC)
  1366. stats->tx_window_errors++;
  1367. } else {
  1368. stats->tx_packets++;
  1369. stats->tx_bytes += tdinfo->skb->len;
  1370. }
  1371. velocity_free_tx_buf(vptr, tdinfo);
  1372. vptr->tx.used[qnum]--;
  1373. }
  1374. vptr->tx.tail[qnum] = idx;
  1375. if (AVAIL_TD(vptr, qnum) < 1) {
  1376. full = 1;
  1377. }
  1378. }
  1379. /*
  1380. * Look to see if we should kick the transmit network
  1381. * layer for more work.
  1382. */
  1383. if (netif_queue_stopped(vptr->dev) && (full == 0)
  1384. && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
  1385. netif_wake_queue(vptr->dev);
  1386. }
  1387. return works;
  1388. }
  1389. /**
  1390. * velocity_print_link_status - link status reporting
  1391. * @vptr: velocity to report on
  1392. *
  1393. * Turn the link status of the velocity card into a kernel log
  1394. * description of the new link state, detailing speed and duplex
  1395. * status
  1396. */
  1397. static void velocity_print_link_status(struct velocity_info *vptr)
  1398. {
  1399. if (vptr->mii_status & VELOCITY_LINK_FAIL) {
  1400. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
  1401. } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
  1402. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->dev->name);
  1403. if (vptr->mii_status & VELOCITY_SPEED_1000)
  1404. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
  1405. else if (vptr->mii_status & VELOCITY_SPEED_100)
  1406. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
  1407. else
  1408. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
  1409. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  1410. VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
  1411. else
  1412. VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
  1413. } else {
  1414. VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
  1415. switch (vptr->options.spd_dpx) {
  1416. case SPD_DPX_100_HALF:
  1417. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
  1418. break;
  1419. case SPD_DPX_100_FULL:
  1420. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
  1421. break;
  1422. case SPD_DPX_10_HALF:
  1423. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
  1424. break;
  1425. case SPD_DPX_10_FULL:
  1426. VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
  1427. break;
  1428. default:
  1429. break;
  1430. }
  1431. }
  1432. }
  1433. /**
  1434. * velocity_error - handle error from controller
  1435. * @vptr: velocity
  1436. * @status: card status
  1437. *
  1438. * Process an error report from the hardware and attempt to recover
  1439. * the card itself. At the moment we cannot recover from some
  1440. * theoretically impossible errors but this could be fixed using
  1441. * the pci_device_failed logic to bounce the hardware
  1442. *
  1443. */
  1444. static void velocity_error(struct velocity_info *vptr, int status)
  1445. {
  1446. if (status & ISR_TXSTLI) {
  1447. struct mac_regs __iomem * regs = vptr->mac_regs;
  1448. printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(&regs->TDIdx[0]));
  1449. BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
  1450. writew(TRDCSR_RUN, &regs->TDCSRClr);
  1451. netif_stop_queue(vptr->dev);
  1452. /* FIXME: port over the pci_device_failed code and use it
  1453. here */
  1454. }
  1455. if (status & ISR_SRCI) {
  1456. struct mac_regs __iomem * regs = vptr->mac_regs;
  1457. int linked;
  1458. if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
  1459. vptr->mii_status = check_connection_type(regs);
  1460. /*
  1461. * If it is a 3119, disable frame bursting in
  1462. * halfduplex mode and enable it in fullduplex
  1463. * mode
  1464. */
  1465. if (vptr->rev_id < REV_ID_VT3216_A0) {
  1466. if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
  1467. BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
  1468. else
  1469. BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
  1470. }
  1471. /*
  1472. * Only enable CD heart beat counter in 10HD mode
  1473. */
  1474. if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10)) {
  1475. BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
  1476. } else {
  1477. BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
  1478. }
  1479. }
  1480. /*
  1481. * Get link status from PHYSR0
  1482. */
  1483. linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
  1484. if (linked) {
  1485. vptr->mii_status &= ~VELOCITY_LINK_FAIL;
  1486. netif_carrier_on(vptr->dev);
  1487. } else {
  1488. vptr->mii_status |= VELOCITY_LINK_FAIL;
  1489. netif_carrier_off(vptr->dev);
  1490. }
  1491. velocity_print_link_status(vptr);
  1492. enable_flow_control_ability(vptr);
  1493. /*
  1494. * Re-enable auto-polling because SRCI will disable
  1495. * auto-polling
  1496. */
  1497. enable_mii_autopoll(regs);
  1498. if (vptr->mii_status & VELOCITY_LINK_FAIL)
  1499. netif_stop_queue(vptr->dev);
  1500. else
  1501. netif_wake_queue(vptr->dev);
  1502. };
  1503. if (status & ISR_MIBFI)
  1504. velocity_update_hw_mibs(vptr);
  1505. if (status & ISR_LSTEI)
  1506. mac_rx_queue_wake(vptr->mac_regs);
  1507. }
  1508. /**
  1509. * velocity_free_tx_buf - free transmit buffer
  1510. * @vptr: velocity
  1511. * @tdinfo: buffer
  1512. *
  1513. * Release an transmit buffer. If the buffer was preallocated then
  1514. * recycle it, if not then unmap the buffer.
  1515. */
  1516. static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
  1517. {
  1518. struct sk_buff *skb = tdinfo->skb;
  1519. int i;
  1520. int pktlen;
  1521. /*
  1522. * Don't unmap the pre-allocated tx_bufs
  1523. */
  1524. if (tdinfo->skb_dma) {
  1525. pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
  1526. for (i = 0; i < tdinfo->nskb_dma; i++) {
  1527. #ifdef VELOCITY_ZERO_COPY_SUPPORT
  1528. pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], le16_to_cpu(td->tdesc1.len), PCI_DMA_TODEVICE);
  1529. #else
  1530. pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], pktlen, PCI_DMA_TODEVICE);
  1531. #endif
  1532. tdinfo->skb_dma[i] = 0;
  1533. }
  1534. }
  1535. dev_kfree_skb_irq(skb);
  1536. tdinfo->skb = NULL;
  1537. }
  1538. static int velocity_init_rings(struct velocity_info *vptr, int mtu)
  1539. {
  1540. int ret;
  1541. velocity_set_rxbufsize(vptr, mtu);
  1542. ret = velocity_init_dma_rings(vptr);
  1543. if (ret < 0)
  1544. goto out;
  1545. ret = velocity_init_rd_ring(vptr);
  1546. if (ret < 0)
  1547. goto err_free_dma_rings_0;
  1548. ret = velocity_init_td_ring(vptr);
  1549. if (ret < 0)
  1550. goto err_free_rd_ring_1;
  1551. out:
  1552. return ret;
  1553. err_free_rd_ring_1:
  1554. velocity_free_rd_ring(vptr);
  1555. err_free_dma_rings_0:
  1556. velocity_free_dma_rings(vptr);
  1557. goto out;
  1558. }
  1559. static void velocity_free_rings(struct velocity_info *vptr)
  1560. {
  1561. velocity_free_td_ring(vptr);
  1562. velocity_free_rd_ring(vptr);
  1563. velocity_free_dma_rings(vptr);
  1564. }
  1565. /**
  1566. * velocity_open - interface activation callback
  1567. * @dev: network layer device to open
  1568. *
  1569. * Called when the network layer brings the interface up. Returns
  1570. * a negative posix error code on failure, or zero on success.
  1571. *
  1572. * All the ring allocation and set up is done on open for this
  1573. * adapter to minimise memory usage when inactive
  1574. */
  1575. static int velocity_open(struct net_device *dev)
  1576. {
  1577. struct velocity_info *vptr = netdev_priv(dev);
  1578. int ret;
  1579. ret = velocity_init_rings(vptr, dev->mtu);
  1580. if (ret < 0)
  1581. goto out;
  1582. /* Ensure chip is running */
  1583. pci_set_power_state(vptr->pdev, PCI_D0);
  1584. velocity_give_many_rx_descs(vptr);
  1585. velocity_init_registers(vptr, VELOCITY_INIT_COLD);
  1586. ret = request_irq(vptr->pdev->irq, &velocity_intr, IRQF_SHARED,
  1587. dev->name, dev);
  1588. if (ret < 0) {
  1589. /* Power down the chip */
  1590. pci_set_power_state(vptr->pdev, PCI_D3hot);
  1591. velocity_free_rings(vptr);
  1592. goto out;
  1593. }
  1594. mac_enable_int(vptr->mac_regs);
  1595. netif_start_queue(dev);
  1596. vptr->flags |= VELOCITY_FLAGS_OPENED;
  1597. out:
  1598. return ret;
  1599. }
  1600. /**
  1601. * velocity_change_mtu - MTU change callback
  1602. * @dev: network device
  1603. * @new_mtu: desired MTU
  1604. *
  1605. * Handle requests from the networking layer for MTU change on
  1606. * this interface. It gets called on a change by the network layer.
  1607. * Return zero for success or negative posix error code.
  1608. */
  1609. static int velocity_change_mtu(struct net_device *dev, int new_mtu)
  1610. {
  1611. struct velocity_info *vptr = netdev_priv(dev);
  1612. int ret = 0;
  1613. if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
  1614. VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
  1615. vptr->dev->name);
  1616. ret = -EINVAL;
  1617. goto out_0;
  1618. }
  1619. if (!netif_running(dev)) {
  1620. dev->mtu = new_mtu;
  1621. goto out_0;
  1622. }
  1623. if (dev->mtu != new_mtu) {
  1624. struct velocity_info *tmp_vptr;
  1625. unsigned long flags;
  1626. struct rx_info rx;
  1627. struct tx_info tx;
  1628. tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
  1629. if (!tmp_vptr) {
  1630. ret = -ENOMEM;
  1631. goto out_0;
  1632. }
  1633. tmp_vptr->dev = dev;
  1634. tmp_vptr->pdev = vptr->pdev;
  1635. tmp_vptr->options = vptr->options;
  1636. tmp_vptr->tx.numq = vptr->tx.numq;
  1637. ret = velocity_init_rings(tmp_vptr, new_mtu);
  1638. if (ret < 0)
  1639. goto out_free_tmp_vptr_1;
  1640. spin_lock_irqsave(&vptr->lock, flags);
  1641. netif_stop_queue(dev);
  1642. velocity_shutdown(vptr);
  1643. rx = vptr->rx;
  1644. tx = vptr->tx;
  1645. vptr->rx = tmp_vptr->rx;
  1646. vptr->tx = tmp_vptr->tx;
  1647. tmp_vptr->rx = rx;
  1648. tmp_vptr->tx = tx;
  1649. dev->mtu = new_mtu;
  1650. velocity_give_many_rx_descs(vptr);
  1651. velocity_init_registers(vptr, VELOCITY_INIT_COLD);
  1652. mac_enable_int(vptr->mac_regs);
  1653. netif_start_queue(dev);
  1654. spin_unlock_irqrestore(&vptr->lock, flags);
  1655. velocity_free_rings(tmp_vptr);
  1656. out_free_tmp_vptr_1:
  1657. kfree(tmp_vptr);
  1658. }
  1659. out_0:
  1660. return ret;
  1661. }
  1662. /**
  1663. * velocity_shutdown - shut down the chip
  1664. * @vptr: velocity to deactivate
  1665. *
  1666. * Shuts down the internal operations of the velocity and
  1667. * disables interrupts, autopolling, transmit and receive
  1668. */
  1669. static void velocity_shutdown(struct velocity_info *vptr)
  1670. {
  1671. struct mac_regs __iomem * regs = vptr->mac_regs;
  1672. mac_disable_int(regs);
  1673. writel(CR0_STOP, &regs->CR0Set);
  1674. writew(0xFFFF, &regs->TDCSRClr);
  1675. writeb(0xFF, &regs->RDCSRClr);
  1676. safe_disable_mii_autopoll(regs);
  1677. mac_clear_isr(regs);
  1678. }
  1679. /**
  1680. * velocity_close - close adapter callback
  1681. * @dev: network device
  1682. *
  1683. * Callback from the network layer when the velocity is being
  1684. * deactivated by the network layer
  1685. */
  1686. static int velocity_close(struct net_device *dev)
  1687. {
  1688. struct velocity_info *vptr = netdev_priv(dev);
  1689. netif_stop_queue(dev);
  1690. velocity_shutdown(vptr);
  1691. if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
  1692. velocity_get_ip(vptr);
  1693. if (dev->irq != 0)
  1694. free_irq(dev->irq, dev);
  1695. /* Power down the chip */
  1696. pci_set_power_state(vptr->pdev, PCI_D3hot);
  1697. velocity_free_rings(vptr);
  1698. vptr->flags &= (~VELOCITY_FLAGS_OPENED);
  1699. return 0;
  1700. }
  1701. /**
  1702. * velocity_xmit - transmit packet callback
  1703. * @skb: buffer to transmit
  1704. * @dev: network device
  1705. *
  1706. * Called by the networ layer to request a packet is queued to
  1707. * the velocity. Returns zero on success.
  1708. */
  1709. static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
  1710. {
  1711. struct velocity_info *vptr = netdev_priv(dev);
  1712. int qnum = 0;
  1713. struct tx_desc *td_ptr;
  1714. struct velocity_td_info *tdinfo;
  1715. unsigned long flags;
  1716. int pktlen;
  1717. __le16 len;
  1718. int index;
  1719. if (skb_padto(skb, ETH_ZLEN))
  1720. goto out;
  1721. pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
  1722. len = cpu_to_le16(pktlen);
  1723. #ifdef VELOCITY_ZERO_COPY_SUPPORT
  1724. if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
  1725. kfree_skb(skb);
  1726. return 0;
  1727. }
  1728. #endif
  1729. spin_lock_irqsave(&vptr->lock, flags);
  1730. index = vptr->tx.curr[qnum];
  1731. td_ptr = &(vptr->tx.rings[qnum][index]);
  1732. tdinfo = &(vptr->tx.infos[qnum][index]);
  1733. td_ptr->tdesc1.TCR = TCR0_TIC;
  1734. td_ptr->td_buf[0].size &= ~TD_QUEUE;
  1735. #ifdef VELOCITY_ZERO_COPY_SUPPORT
  1736. if (skb_shinfo(skb)->nr_frags > 0) {
  1737. int nfrags = skb_shinfo(skb)->nr_frags;
  1738. tdinfo->skb = skb;
  1739. if (nfrags > 6) {
  1740. skb_copy_from_linear_data(skb, tdinfo->buf, skb->len);
  1741. tdinfo->skb_dma[0] = tdinfo->buf_dma;
  1742. td_ptr->tdesc0.len = len;
  1743. td_ptr->tx.buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
  1744. td_ptr->tx.buf[0].pa_high = 0;
  1745. td_ptr->tx.buf[0].size = len; /* queue is 0 anyway */
  1746. tdinfo->nskb_dma = 1;
  1747. } else {
  1748. int i = 0;
  1749. tdinfo->nskb_dma = 0;
  1750. tdinfo->skb_dma[i] = pci_map_single(vptr->pdev, skb->data,
  1751. skb_headlen(skb), PCI_DMA_TODEVICE);
  1752. td_ptr->tdesc0.len = len;
  1753. /* FIXME: support 48bit DMA later */
  1754. td_ptr->tx.buf[i].pa_low = cpu_to_le32(tdinfo->skb_dma);
  1755. td_ptr->tx.buf[i].pa_high = 0;
  1756. td_ptr->tx.buf[i].size = cpu_to_le16(skb_headlen(skb));
  1757. for (i = 0; i < nfrags; i++) {
  1758. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1759. void *addr = (void *)page_address(frag->page) + frag->page_offset;
  1760. tdinfo->skb_dma[i + 1] = pci_map_single(vptr->pdev, addr, frag->size, PCI_DMA_TODEVICE);
  1761. td_ptr->tx.buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
  1762. td_ptr->tx.buf[i + 1].pa_high = 0;
  1763. td_ptr->tx.buf[i + 1].size = cpu_to_le16(frag->size);
  1764. }
  1765. tdinfo->nskb_dma = i - 1;
  1766. }
  1767. } else
  1768. #endif
  1769. {
  1770. /*
  1771. * Map the linear network buffer into PCI space and
  1772. * add it to the transmit ring.
  1773. */
  1774. tdinfo->skb = skb;
  1775. tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
  1776. td_ptr->tdesc0.len = len;
  1777. td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
  1778. td_ptr->td_buf[0].pa_high = 0;
  1779. td_ptr->td_buf[0].size = len;
  1780. tdinfo->nskb_dma = 1;
  1781. }
  1782. td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
  1783. if (vptr->vlgrp && vlan_tx_tag_present(skb)) {
  1784. td_ptr->tdesc1.vlan = cpu_to_le16(vlan_tx_tag_get(skb));
  1785. td_ptr->tdesc1.TCR |= TCR0_VETAG;
  1786. }
  1787. /*
  1788. * Handle hardware checksum
  1789. */
  1790. if ((vptr->flags & VELOCITY_FLAGS_TX_CSUM)
  1791. && (skb->ip_summed == CHECKSUM_PARTIAL)) {
  1792. const struct iphdr *ip = ip_hdr(skb);
  1793. if (ip->protocol == IPPROTO_TCP)
  1794. td_ptr->tdesc1.TCR |= TCR0_TCPCK;
  1795. else if (ip->protocol == IPPROTO_UDP)
  1796. td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
  1797. td_ptr->tdesc1.TCR |= TCR0_IPCK;
  1798. }
  1799. {
  1800. int prev = index - 1;
  1801. if (prev < 0)
  1802. prev = vptr->options.numtx - 1;
  1803. td_ptr->tdesc0.len |= OWNED_BY_NIC;
  1804. vptr->tx.used[qnum]++;
  1805. vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
  1806. if (AVAIL_TD(vptr, qnum) < 1)
  1807. netif_stop_queue(dev);
  1808. td_ptr = &(vptr->tx.rings[qnum][prev]);
  1809. td_ptr->td_buf[0].size |= TD_QUEUE;
  1810. mac_tx_queue_wake(vptr->mac_regs, qnum);
  1811. }
  1812. dev->trans_start = jiffies;
  1813. spin_unlock_irqrestore(&vptr->lock, flags);
  1814. out:
  1815. return NETDEV_TX_OK;
  1816. }
  1817. /**
  1818. * velocity_intr - interrupt callback
  1819. * @irq: interrupt number
  1820. * @dev_instance: interrupting device
  1821. *
  1822. * Called whenever an interrupt is generated by the velocity
  1823. * adapter IRQ line. We may not be the source of the interrupt
  1824. * and need to identify initially if we are, and if not exit as
  1825. * efficiently as possible.
  1826. */
  1827. static irqreturn_t velocity_intr(int irq, void *dev_instance)
  1828. {
  1829. struct net_device *dev = dev_instance;
  1830. struct velocity_info *vptr = netdev_priv(dev);
  1831. u32 isr_status;
  1832. int max_count = 0;
  1833. spin_lock(&vptr->lock);
  1834. isr_status = mac_read_isr(vptr->mac_regs);
  1835. /* Not us ? */
  1836. if (isr_status == 0) {
  1837. spin_unlock(&vptr->lock);
  1838. return IRQ_NONE;
  1839. }
  1840. mac_disable_int(vptr->mac_regs);
  1841. /*
  1842. * Keep processing the ISR until we have completed
  1843. * processing and the isr_status becomes zero
  1844. */
  1845. while (isr_status != 0) {
  1846. mac_write_isr(vptr->mac_regs, isr_status);
  1847. if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
  1848. velocity_error(vptr, isr_status);
  1849. if (isr_status & (ISR_PRXI | ISR_PPRXI))
  1850. max_count += velocity_rx_srv(vptr, isr_status);
  1851. if (isr_status & (ISR_PTXI | ISR_PPTXI))
  1852. max_count += velocity_tx_srv(vptr, isr_status);
  1853. isr_status = mac_read_isr(vptr->mac_regs);
  1854. if (max_count > vptr->options.int_works)
  1855. {
  1856. printk(KERN_WARNING "%s: excessive work at interrupt.\n",
  1857. dev->name);
  1858. max_count = 0;
  1859. }
  1860. }
  1861. spin_unlock(&vptr->lock);
  1862. mac_enable_int(vptr->mac_regs);
  1863. return IRQ_HANDLED;
  1864. }
  1865. /**
  1866. * velocity_set_multi - filter list change callback
  1867. * @dev: network device
  1868. *
  1869. * Called by the network layer when the filter lists need to change
  1870. * for a velocity adapter. Reload the CAMs with the new address
  1871. * filter ruleset.
  1872. */
  1873. static void velocity_set_multi(struct net_device *dev)
  1874. {
  1875. struct velocity_info *vptr = netdev_priv(dev);
  1876. struct mac_regs __iomem * regs = vptr->mac_regs;
  1877. u8 rx_mode;
  1878. int i;
  1879. struct dev_mc_list *mclist;
  1880. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1881. writel(0xffffffff, &regs->MARCAM[0]);
  1882. writel(0xffffffff, &regs->MARCAM[4]);
  1883. rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
  1884. } else if ((dev->mc_count > vptr->multicast_limit)
  1885. || (dev->flags & IFF_ALLMULTI)) {
  1886. writel(0xffffffff, &regs->MARCAM[0]);
  1887. writel(0xffffffff, &regs->MARCAM[4]);
  1888. rx_mode = (RCR_AM | RCR_AB);
  1889. } else {
  1890. int offset = MCAM_SIZE - vptr->multicast_limit;
  1891. mac_get_cam_mask(regs, vptr->mCAMmask);
  1892. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
  1893. mac_set_cam(regs, i + offset, mclist->dmi_addr);
  1894. vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
  1895. }
  1896. mac_set_cam_mask(regs, vptr->mCAMmask);
  1897. rx_mode = RCR_AM | RCR_AB | RCR_AP;
  1898. }
  1899. if (dev->mtu > 1500)
  1900. rx_mode |= RCR_AL;
  1901. BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
  1902. }
  1903. /**
  1904. * velocity_get_status - statistics callback
  1905. * @dev: network device
  1906. *
  1907. * Callback from the network layer to allow driver statistics
  1908. * to be resynchronized with hardware collected state. In the
  1909. * case of the velocity we need to pull the MIB counters from
  1910. * the hardware into the counters before letting the network
  1911. * layer display them.
  1912. */
  1913. static struct net_device_stats *velocity_get_stats(struct net_device *dev)
  1914. {
  1915. struct velocity_info *vptr = netdev_priv(dev);
  1916. /* If the hardware is down, don't touch MII */
  1917. if(!netif_running(dev))
  1918. return &dev->stats;
  1919. spin_lock_irq(&vptr->lock);
  1920. velocity_update_hw_mibs(vptr);
  1921. spin_unlock_irq(&vptr->lock);
  1922. dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
  1923. dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
  1924. dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
  1925. // unsigned long rx_dropped; /* no space in linux buffers */
  1926. dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
  1927. /* detailed rx_errors: */
  1928. // unsigned long rx_length_errors;
  1929. // unsigned long rx_over_errors; /* receiver ring buff overflow */
  1930. dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
  1931. // unsigned long rx_frame_errors; /* recv'd frame alignment error */
  1932. // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  1933. // unsigned long rx_missed_errors; /* receiver missed packet */
  1934. /* detailed tx_errors */
  1935. // unsigned long tx_fifo_errors;
  1936. return &dev->stats;
  1937. }
  1938. /**
  1939. * velocity_ioctl - ioctl entry point
  1940. * @dev: network device
  1941. * @rq: interface request ioctl
  1942. * @cmd: command code
  1943. *
  1944. * Called when the user issues an ioctl request to the network
  1945. * device in question. The velocity interface supports MII.
  1946. */
  1947. static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1948. {
  1949. struct velocity_info *vptr = netdev_priv(dev);
  1950. int ret;
  1951. /* If we are asked for information and the device is power
  1952. saving then we need to bring the device back up to talk to it */
  1953. if (!netif_running(dev))
  1954. pci_set_power_state(vptr->pdev, PCI_D0);
  1955. switch (cmd) {
  1956. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  1957. case SIOCGMIIREG: /* Read MII PHY register. */
  1958. case SIOCSMIIREG: /* Write to MII PHY register. */
  1959. ret = velocity_mii_ioctl(dev, rq, cmd);
  1960. break;
  1961. default:
  1962. ret = -EOPNOTSUPP;
  1963. }
  1964. if (!netif_running(dev))
  1965. pci_set_power_state(vptr->pdev, PCI_D3hot);
  1966. return ret;
  1967. }
  1968. /*
  1969. * Definition for our device driver. The PCI layer interface
  1970. * uses this to handle all our card discover and plugging
  1971. */
  1972. static struct pci_driver velocity_driver = {
  1973. .name = VELOCITY_NAME,
  1974. .id_table = velocity_id_table,
  1975. .probe = velocity_found1,
  1976. .remove = __devexit_p(velocity_remove1),
  1977. #ifdef CONFIG_PM
  1978. .suspend = velocity_suspend,
  1979. .resume = velocity_resume,
  1980. #endif
  1981. };
  1982. /**
  1983. * velocity_init_module - load time function
  1984. *
  1985. * Called when the velocity module is loaded. The PCI driver
  1986. * is registered with the PCI layer, and in turn will call
  1987. * the probe functions for each velocity adapter installed
  1988. * in the system.
  1989. */
  1990. static int __init velocity_init_module(void)
  1991. {
  1992. int ret;
  1993. velocity_register_notifier();
  1994. ret = pci_register_driver(&velocity_driver);
  1995. if (ret < 0)
  1996. velocity_unregister_notifier();
  1997. return ret;
  1998. }
  1999. /**
  2000. * velocity_cleanup - module unload
  2001. *
  2002. * When the velocity hardware is unloaded this function is called.
  2003. * It will clean up the notifiers and the unregister the PCI
  2004. * driver interface for this hardware. This in turn cleans up
  2005. * all discovered interfaces before returning from the function
  2006. */
  2007. static void __exit velocity_cleanup_module(void)
  2008. {
  2009. velocity_unregister_notifier();
  2010. pci_unregister_driver(&velocity_driver);
  2011. }
  2012. module_init(velocity_init_module);
  2013. module_exit(velocity_cleanup_module);
  2014. /*
  2015. * MII access , media link mode setting functions
  2016. */
  2017. /**
  2018. * mii_init - set up MII
  2019. * @vptr: velocity adapter
  2020. * @mii_status: links tatus
  2021. *
  2022. * Set up the PHY for the current link state.
  2023. */
  2024. static void mii_init(struct velocity_info *vptr, u32 mii_status)
  2025. {
  2026. u16 BMCR;
  2027. switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
  2028. case PHYID_CICADA_CS8201:
  2029. /*
  2030. * Reset to hardware default
  2031. */
  2032. MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
  2033. /*
  2034. * Turn on ECHODIS bit in NWay-forced full mode and turn it
  2035. * off it in NWay-forced half mode for NWay-forced v.s.
  2036. * legacy-forced issue.
  2037. */
  2038. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  2039. MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
  2040. else
  2041. MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
  2042. /*
  2043. * Turn on Link/Activity LED enable bit for CIS8201
  2044. */
  2045. MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
  2046. break;
  2047. case PHYID_VT3216_32BIT:
  2048. case PHYID_VT3216_64BIT:
  2049. /*
  2050. * Reset to hardware default
  2051. */
  2052. MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
  2053. /*
  2054. * Turn on ECHODIS bit in NWay-forced full mode and turn it
  2055. * off it in NWay-forced half mode for NWay-forced v.s.
  2056. * legacy-forced issue
  2057. */
  2058. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  2059. MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
  2060. else
  2061. MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
  2062. break;
  2063. case PHYID_MARVELL_1000:
  2064. case PHYID_MARVELL_1000S:
  2065. /*
  2066. * Assert CRS on Transmit
  2067. */
  2068. MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
  2069. /*
  2070. * Reset to hardware default
  2071. */
  2072. MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
  2073. break;
  2074. default:
  2075. ;
  2076. }
  2077. velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
  2078. if (BMCR & BMCR_ISO) {
  2079. BMCR &= ~BMCR_ISO;
  2080. velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
  2081. }
  2082. }
  2083. /**
  2084. * safe_disable_mii_autopoll - autopoll off
  2085. * @regs: velocity registers
  2086. *
  2087. * Turn off the autopoll and wait for it to disable on the chip
  2088. */
  2089. static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs)
  2090. {
  2091. u16 ww;
  2092. /* turn off MAUTO */
  2093. writeb(0, &regs->MIICR);
  2094. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  2095. udelay(1);
  2096. if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  2097. break;
  2098. }
  2099. }
  2100. /**
  2101. * enable_mii_autopoll - turn on autopolling
  2102. * @regs: velocity registers
  2103. *
  2104. * Enable the MII link status autopoll feature on the Velocity
  2105. * hardware. Wait for it to enable.
  2106. */
  2107. static void enable_mii_autopoll(struct mac_regs __iomem * regs)
  2108. {
  2109. int ii;
  2110. writeb(0, &(regs->MIICR));
  2111. writeb(MIIADR_SWMPL, &regs->MIIADR);
  2112. for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
  2113. udelay(1);
  2114. if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  2115. break;
  2116. }
  2117. writeb(MIICR_MAUTO, &regs->MIICR);
  2118. for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
  2119. udelay(1);
  2120. if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  2121. break;
  2122. }
  2123. }
  2124. /**
  2125. * velocity_mii_read - read MII data
  2126. * @regs: velocity registers
  2127. * @index: MII register index
  2128. * @data: buffer for received data
  2129. *
  2130. * Perform a single read of an MII 16bit register. Returns zero
  2131. * on success or -ETIMEDOUT if the PHY did not respond.
  2132. */
  2133. static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
  2134. {
  2135. u16 ww;
  2136. /*
  2137. * Disable MIICR_MAUTO, so that mii addr can be set normally
  2138. */
  2139. safe_disable_mii_autopoll(regs);
  2140. writeb(index, &regs->MIIADR);
  2141. BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
  2142. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  2143. if (!(readb(&regs->MIICR) & MIICR_RCMD))
  2144. break;
  2145. }
  2146. *data = readw(&regs->MIIDATA);
  2147. enable_mii_autopoll(regs);
  2148. if (ww == W_MAX_TIMEOUT)
  2149. return -ETIMEDOUT;
  2150. return 0;
  2151. }
  2152. /**
  2153. * velocity_mii_write - write MII data
  2154. * @regs: velocity registers
  2155. * @index: MII register index
  2156. * @data: 16bit data for the MII register
  2157. *
  2158. * Perform a single write to an MII 16bit register. Returns zero
  2159. * on success or -ETIMEDOUT if the PHY did not respond.
  2160. */
  2161. static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
  2162. {
  2163. u16 ww;
  2164. /*
  2165. * Disable MIICR_MAUTO, so that mii addr can be set normally
  2166. */
  2167. safe_disable_mii_autopoll(regs);
  2168. /* MII reg offset */
  2169. writeb(mii_addr, &regs->MIIADR);
  2170. /* set MII data */
  2171. writew(data, &regs->MIIDATA);
  2172. /* turn on MIICR_WCMD */
  2173. BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
  2174. /* W_MAX_TIMEOUT is the timeout period */
  2175. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  2176. udelay(5);
  2177. if (!(readb(&regs->MIICR) & MIICR_WCMD))
  2178. break;
  2179. }
  2180. enable_mii_autopoll(regs);
  2181. if (ww == W_MAX_TIMEOUT)
  2182. return -ETIMEDOUT;
  2183. return 0;
  2184. }
  2185. /**
  2186. * velocity_get_opt_media_mode - get media selection
  2187. * @vptr: velocity adapter
  2188. *
  2189. * Get the media mode stored in EEPROM or module options and load
  2190. * mii_status accordingly. The requested link state information
  2191. * is also returned.
  2192. */
  2193. static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
  2194. {
  2195. u32 status = 0;
  2196. switch (vptr->options.spd_dpx) {
  2197. case SPD_DPX_AUTO:
  2198. status = VELOCITY_AUTONEG_ENABLE;
  2199. break;
  2200. case SPD_DPX_100_FULL:
  2201. status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
  2202. break;
  2203. case SPD_DPX_10_FULL:
  2204. status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
  2205. break;
  2206. case SPD_DPX_100_HALF:
  2207. status = VELOCITY_SPEED_100;
  2208. break;
  2209. case SPD_DPX_10_HALF:
  2210. status = VELOCITY_SPEED_10;
  2211. break;
  2212. }
  2213. vptr->mii_status = status;
  2214. return status;
  2215. }
  2216. /**
  2217. * mii_set_auto_on - autonegotiate on
  2218. * @vptr: velocity
  2219. *
  2220. * Enable autonegotation on this interface
  2221. */
  2222. static void mii_set_auto_on(struct velocity_info *vptr)
  2223. {
  2224. if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
  2225. MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
  2226. else
  2227. MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
  2228. }
  2229. /*
  2230. static void mii_set_auto_off(struct velocity_info * vptr)
  2231. {
  2232. MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
  2233. }
  2234. */
  2235. /**
  2236. * set_mii_flow_control - flow control setup
  2237. * @vptr: velocity interface
  2238. *
  2239. * Set up the flow control on this interface according to
  2240. * the supplied user/eeprom options.
  2241. */
  2242. static void set_mii_flow_control(struct velocity_info *vptr)
  2243. {
  2244. /*Enable or Disable PAUSE in ANAR */
  2245. switch (vptr->options.flow_cntl) {
  2246. case FLOW_CNTL_TX:
  2247. MII_REG_BITS_OFF(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_RX:
  2251. MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
  2252. MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
  2253. break;
  2254. case FLOW_CNTL_TX_RX:
  2255. MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
  2256. MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
  2257. break;
  2258. case FLOW_CNTL_DISABLE:
  2259. MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
  2260. MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
  2261. break;
  2262. default:
  2263. break;
  2264. }
  2265. }
  2266. /**
  2267. * velocity_set_media_mode - set media mode
  2268. * @mii_status: old MII link state
  2269. *
  2270. * Check the media link state and configure the flow control
  2271. * PHY and also velocity hardware setup accordingly. In particular
  2272. * we need to set up CD polling and frame bursting.
  2273. */
  2274. static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
  2275. {
  2276. u32 curr_status;
  2277. struct mac_regs __iomem * regs = vptr->mac_regs;
  2278. vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
  2279. curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
  2280. /* Set mii link status */
  2281. set_mii_flow_control(vptr);
  2282. /*
  2283. Check if new status is consisent with current status
  2284. if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
  2285. || (mii_status==curr_status)) {
  2286. vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
  2287. vptr->mii_status=check_connection_type(vptr->mac_regs);
  2288. VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
  2289. return 0;
  2290. }
  2291. */
  2292. if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201) {
  2293. MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
  2294. }
  2295. /*
  2296. * If connection type is AUTO
  2297. */
  2298. if (mii_status & VELOCITY_AUTONEG_ENABLE) {
  2299. VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
  2300. /* clear force MAC mode bit */
  2301. BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
  2302. /* set duplex mode of MAC according to duplex mode of MII */
  2303. MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
  2304. MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
  2305. MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
  2306. /* enable AUTO-NEGO mode */
  2307. mii_set_auto_on(vptr);
  2308. } else {
  2309. u16 ANAR;
  2310. u8 CHIPGCR;
  2311. /*
  2312. * 1. if it's 3119, disable frame bursting in halfduplex mode
  2313. * and enable it in fullduplex mode
  2314. * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
  2315. * 3. only enable CD heart beat counter in 10HD mode
  2316. */
  2317. /* set force MAC mode bit */
  2318. BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
  2319. CHIPGCR = readb(&regs->CHIPGCR);
  2320. CHIPGCR &= ~CHIPGCR_FCGMII;
  2321. if (mii_status & VELOCITY_DUPLEX_FULL) {
  2322. CHIPGCR |= CHIPGCR_FCFDX;
  2323. writeb(CHIPGCR, &regs->CHIPGCR);
  2324. VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
  2325. if (vptr->rev_id < REV_ID_VT3216_A0)
  2326. BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
  2327. } else {
  2328. CHIPGCR &= ~CHIPGCR_FCFDX;
  2329. VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
  2330. writeb(CHIPGCR, &regs->CHIPGCR);
  2331. if (vptr->rev_id < REV_ID_VT3216_A0)
  2332. BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
  2333. }
  2334. MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
  2335. if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10)) {
  2336. BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
  2337. } else {
  2338. BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
  2339. }
  2340. /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
  2341. velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
  2342. ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
  2343. if (mii_status & VELOCITY_SPEED_100) {
  2344. if (mii_status & VELOCITY_DUPLEX_FULL)
  2345. ANAR |= ANAR_TXFD;
  2346. else
  2347. ANAR |= ANAR_TX;
  2348. } else {
  2349. if (mii_status & VELOCITY_DUPLEX_FULL)
  2350. ANAR |= ANAR_10FD;
  2351. else
  2352. ANAR |= ANAR_10;
  2353. }
  2354. velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
  2355. /* enable AUTO-NEGO mode */
  2356. mii_set_auto_on(vptr);
  2357. /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
  2358. }
  2359. /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
  2360. /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
  2361. return VELOCITY_LINK_CHANGE;
  2362. }
  2363. /**
  2364. * mii_check_media_mode - check media state
  2365. * @regs: velocity registers
  2366. *
  2367. * Check the current MII status and determine the link status
  2368. * accordingly
  2369. */
  2370. static u32 mii_check_media_mode(struct mac_regs __iomem * regs)
  2371. {
  2372. u32 status = 0;
  2373. u16 ANAR;
  2374. if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
  2375. status |= VELOCITY_LINK_FAIL;
  2376. if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
  2377. status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
  2378. else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
  2379. status |= (VELOCITY_SPEED_1000);
  2380. else {
  2381. velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
  2382. if (ANAR & ANAR_TXFD)
  2383. status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
  2384. else if (ANAR & ANAR_TX)
  2385. status |= VELOCITY_SPEED_100;
  2386. else if (ANAR & ANAR_10FD)
  2387. status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
  2388. else
  2389. status |= (VELOCITY_SPEED_10);
  2390. }
  2391. if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
  2392. velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
  2393. if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
  2394. == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
  2395. if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
  2396. status |= VELOCITY_AUTONEG_ENABLE;
  2397. }
  2398. }
  2399. return status;
  2400. }
  2401. static u32 check_connection_type(struct mac_regs __iomem * regs)
  2402. {
  2403. u32 status = 0;
  2404. u8 PHYSR0;
  2405. u16 ANAR;
  2406. PHYSR0 = readb(&regs->PHYSR0);
  2407. /*
  2408. if (!(PHYSR0 & PHYSR0_LINKGD))
  2409. status|=VELOCITY_LINK_FAIL;
  2410. */
  2411. if (PHYSR0 & PHYSR0_FDPX)
  2412. status |= VELOCITY_DUPLEX_FULL;
  2413. if (PHYSR0 & PHYSR0_SPDG)
  2414. status |= VELOCITY_SPEED_1000;
  2415. else if (PHYSR0 & PHYSR0_SPD10)
  2416. status |= VELOCITY_SPEED_10;
  2417. else
  2418. status |= VELOCITY_SPEED_100;
  2419. if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
  2420. velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
  2421. if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
  2422. == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
  2423. if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
  2424. status |= VELOCITY_AUTONEG_ENABLE;
  2425. }
  2426. }
  2427. return status;
  2428. }
  2429. /**
  2430. * enable_flow_control_ability - flow control
  2431. * @vptr: veloity to configure
  2432. *
  2433. * Set up flow control according to the flow control options
  2434. * determined by the eeprom/configuration.
  2435. */
  2436. static void enable_flow_control_ability(struct velocity_info *vptr)
  2437. {
  2438. struct mac_regs __iomem * regs = vptr->mac_regs;
  2439. switch (vptr->options.flow_cntl) {
  2440. case FLOW_CNTL_DEFAULT:
  2441. if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
  2442. writel(CR0_FDXRFCEN, &regs->CR0Set);
  2443. else
  2444. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  2445. if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
  2446. writel(CR0_FDXTFCEN, &regs->CR0Set);
  2447. else
  2448. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  2449. break;
  2450. case FLOW_CNTL_TX:
  2451. writel(CR0_FDXTFCEN, &regs->CR0Set);
  2452. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  2453. break;
  2454. case FLOW_CNTL_RX:
  2455. writel(CR0_FDXRFCEN, &regs->CR0Set);
  2456. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  2457. break;
  2458. case FLOW_CNTL_TX_RX:
  2459. writel(CR0_FDXTFCEN, &regs->CR0Set);
  2460. writel(CR0_FDXRFCEN, &regs->CR0Set);
  2461. break;
  2462. case FLOW_CNTL_DISABLE:
  2463. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  2464. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  2465. break;
  2466. default:
  2467. break;
  2468. }
  2469. }
  2470. /**
  2471. * velocity_ethtool_up - pre hook for ethtool
  2472. * @dev: network device
  2473. *
  2474. * Called before an ethtool operation. We need to make sure the
  2475. * chip is out of D3 state before we poke at it.
  2476. */
  2477. static int velocity_ethtool_up(struct net_device *dev)
  2478. {
  2479. struct velocity_info *vptr = netdev_priv(dev);
  2480. if (!netif_running(dev))
  2481. pci_set_power_state(vptr->pdev, PCI_D0);
  2482. return 0;
  2483. }
  2484. /**
  2485. * velocity_ethtool_down - post hook for ethtool
  2486. * @dev: network device
  2487. *
  2488. * Called after an ethtool operation. Restore the chip back to D3
  2489. * state if it isn't running.
  2490. */
  2491. static void velocity_ethtool_down(struct net_device *dev)
  2492. {
  2493. struct velocity_info *vptr = netdev_priv(dev);
  2494. if (!netif_running(dev))
  2495. pci_set_power_state(vptr->pdev, PCI_D3hot);
  2496. }
  2497. static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2498. {
  2499. struct velocity_info *vptr = netdev_priv(dev);
  2500. struct mac_regs __iomem * regs = vptr->mac_regs;
  2501. u32 status;
  2502. status = check_connection_type(vptr->mac_regs);
  2503. cmd->supported = SUPPORTED_TP |
  2504. SUPPORTED_Autoneg |
  2505. SUPPORTED_10baseT_Half |
  2506. SUPPORTED_10baseT_Full |
  2507. SUPPORTED_100baseT_Half |
  2508. SUPPORTED_100baseT_Full |
  2509. SUPPORTED_1000baseT_Half |
  2510. SUPPORTED_1000baseT_Full;
  2511. if (status & VELOCITY_SPEED_1000)
  2512. cmd->speed = SPEED_1000;
  2513. else if (status & VELOCITY_SPEED_100)
  2514. cmd->speed = SPEED_100;
  2515. else
  2516. cmd->speed = SPEED_10;
  2517. cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  2518. cmd->port = PORT_TP;
  2519. cmd->transceiver = XCVR_INTERNAL;
  2520. cmd->phy_address = readb(&regs->MIIADR) & 0x1F;
  2521. if (status & VELOCITY_DUPLEX_FULL)
  2522. cmd->duplex = DUPLEX_FULL;
  2523. else
  2524. cmd->duplex = DUPLEX_HALF;
  2525. return 0;
  2526. }
  2527. static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2528. {
  2529. struct velocity_info *vptr = netdev_priv(dev);
  2530. u32 curr_status;
  2531. u32 new_status = 0;
  2532. int ret = 0;
  2533. curr_status = check_connection_type(vptr->mac_regs);
  2534. curr_status &= (~VELOCITY_LINK_FAIL);
  2535. new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
  2536. new_status |= ((cmd->speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
  2537. new_status |= ((cmd->speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
  2538. new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
  2539. if ((new_status & VELOCITY_AUTONEG_ENABLE) && (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE)))
  2540. ret = -EINVAL;
  2541. else
  2542. velocity_set_media_mode(vptr, new_status);
  2543. return ret;
  2544. }
  2545. static u32 velocity_get_link(struct net_device *dev)
  2546. {
  2547. struct velocity_info *vptr = netdev_priv(dev);
  2548. struct mac_regs __iomem * regs = vptr->mac_regs;
  2549. return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
  2550. }
  2551. static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  2552. {
  2553. struct velocity_info *vptr = netdev_priv(dev);
  2554. strcpy(info->driver, VELOCITY_NAME);
  2555. strcpy(info->version, VELOCITY_VERSION);
  2556. strcpy(info->bus_info, pci_name(vptr->pdev));
  2557. }
  2558. static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2559. {
  2560. struct velocity_info *vptr = netdev_priv(dev);
  2561. wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
  2562. wol->wolopts |= WAKE_MAGIC;
  2563. /*
  2564. if (vptr->wol_opts & VELOCITY_WOL_PHY)
  2565. wol.wolopts|=WAKE_PHY;
  2566. */
  2567. if (vptr->wol_opts & VELOCITY_WOL_UCAST)
  2568. wol->wolopts |= WAKE_UCAST;
  2569. if (vptr->wol_opts & VELOCITY_WOL_ARP)
  2570. wol->wolopts |= WAKE_ARP;
  2571. memcpy(&wol->sopass, vptr->wol_passwd, 6);
  2572. }
  2573. static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2574. {
  2575. struct velocity_info *vptr = netdev_priv(dev);
  2576. if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
  2577. return -EFAULT;
  2578. vptr->wol_opts = VELOCITY_WOL_MAGIC;
  2579. /*
  2580. if (wol.wolopts & WAKE_PHY) {
  2581. vptr->wol_opts|=VELOCITY_WOL_PHY;
  2582. vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
  2583. }
  2584. */
  2585. if (wol->wolopts & WAKE_MAGIC) {
  2586. vptr->wol_opts |= VELOCITY_WOL_MAGIC;
  2587. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  2588. }
  2589. if (wol->wolopts & WAKE_UCAST) {
  2590. vptr->wol_opts |= VELOCITY_WOL_UCAST;
  2591. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  2592. }
  2593. if (wol->wolopts & WAKE_ARP) {
  2594. vptr->wol_opts |= VELOCITY_WOL_ARP;
  2595. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  2596. }
  2597. memcpy(vptr->wol_passwd, wol->sopass, 6);
  2598. return 0;
  2599. }
  2600. static u32 velocity_get_msglevel(struct net_device *dev)
  2601. {
  2602. return msglevel;
  2603. }
  2604. static void velocity_set_msglevel(struct net_device *dev, u32 value)
  2605. {
  2606. msglevel = value;
  2607. }
  2608. static const struct ethtool_ops velocity_ethtool_ops = {
  2609. .get_settings = velocity_get_settings,
  2610. .set_settings = velocity_set_settings,
  2611. .get_drvinfo = velocity_get_drvinfo,
  2612. .get_wol = velocity_ethtool_get_wol,
  2613. .set_wol = velocity_ethtool_set_wol,
  2614. .get_msglevel = velocity_get_msglevel,
  2615. .set_msglevel = velocity_set_msglevel,
  2616. .get_link = velocity_get_link,
  2617. .begin = velocity_ethtool_up,
  2618. .complete = velocity_ethtool_down
  2619. };
  2620. /**
  2621. * velocity_mii_ioctl - MII ioctl handler
  2622. * @dev: network device
  2623. * @ifr: the ifreq block for the ioctl
  2624. * @cmd: the command
  2625. *
  2626. * Process MII requests made via ioctl from the network layer. These
  2627. * are used by tools like kudzu to interrogate the link state of the
  2628. * hardware
  2629. */
  2630. static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2631. {
  2632. struct velocity_info *vptr = netdev_priv(dev);
  2633. struct mac_regs __iomem * regs = vptr->mac_regs;
  2634. unsigned long flags;
  2635. struct mii_ioctl_data *miidata = if_mii(ifr);
  2636. int err;
  2637. switch (cmd) {
  2638. case SIOCGMIIPHY:
  2639. miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
  2640. break;
  2641. case SIOCGMIIREG:
  2642. if (!capable(CAP_NET_ADMIN))
  2643. return -EPERM;
  2644. if(velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
  2645. return -ETIMEDOUT;
  2646. break;
  2647. case SIOCSMIIREG:
  2648. if (!capable(CAP_NET_ADMIN))
  2649. return -EPERM;
  2650. spin_lock_irqsave(&vptr->lock, flags);
  2651. err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
  2652. spin_unlock_irqrestore(&vptr->lock, flags);
  2653. check_connection_type(vptr->mac_regs);
  2654. if(err)
  2655. return err;
  2656. break;
  2657. default:
  2658. return -EOPNOTSUPP;
  2659. }
  2660. return 0;
  2661. }
  2662. #ifdef CONFIG_PM
  2663. /**
  2664. * velocity_save_context - save registers
  2665. * @vptr: velocity
  2666. * @context: buffer for stored context
  2667. *
  2668. * Retrieve the current configuration from the velocity hardware
  2669. * and stash it in the context structure, for use by the context
  2670. * restore functions. This allows us to save things we need across
  2671. * power down states
  2672. */
  2673. static void velocity_save_context(struct velocity_info *vptr, struct velocity_context * context)
  2674. {
  2675. struct mac_regs __iomem * regs = vptr->mac_regs;
  2676. u16 i;
  2677. u8 __iomem *ptr = (u8 __iomem *)regs;
  2678. for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
  2679. *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
  2680. for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
  2681. *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
  2682. for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
  2683. *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
  2684. }
  2685. /**
  2686. * velocity_restore_context - restore registers
  2687. * @vptr: velocity
  2688. * @context: buffer for stored context
  2689. *
  2690. * Reload the register configuration from the velocity context
  2691. * created by velocity_save_context.
  2692. */
  2693. static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
  2694. {
  2695. struct mac_regs __iomem * regs = vptr->mac_regs;
  2696. int i;
  2697. u8 __iomem *ptr = (u8 __iomem *)regs;
  2698. for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4) {
  2699. writel(*((u32 *) (context->mac_reg + i)), ptr + i);
  2700. }
  2701. /* Just skip cr0 */
  2702. for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
  2703. /* Clear */
  2704. writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
  2705. /* Set */
  2706. writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
  2707. }
  2708. for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4) {
  2709. writel(*((u32 *) (context->mac_reg + i)), ptr + i);
  2710. }
  2711. for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4) {
  2712. writel(*((u32 *) (context->mac_reg + i)), ptr + i);
  2713. }
  2714. for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++) {
  2715. writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
  2716. }
  2717. }
  2718. /**
  2719. * wol_calc_crc - WOL CRC
  2720. * @pattern: data pattern
  2721. * @mask_pattern: mask
  2722. *
  2723. * Compute the wake on lan crc hashes for the packet header
  2724. * we are interested in.
  2725. */
  2726. static u16 wol_calc_crc(int size, u8 * pattern, u8 *mask_pattern)
  2727. {
  2728. u16 crc = 0xFFFF;
  2729. u8 mask;
  2730. int i, j;
  2731. for (i = 0; i < size; i++) {
  2732. mask = mask_pattern[i];
  2733. /* Skip this loop if the mask equals to zero */
  2734. if (mask == 0x00)
  2735. continue;
  2736. for (j = 0; j < 8; j++) {
  2737. if ((mask & 0x01) == 0) {
  2738. mask >>= 1;
  2739. continue;
  2740. }
  2741. mask >>= 1;
  2742. crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
  2743. }
  2744. }
  2745. /* Finally, invert the result once to get the correct data */
  2746. crc = ~crc;
  2747. return bitrev32(crc) >> 16;
  2748. }
  2749. /**
  2750. * velocity_set_wol - set up for wake on lan
  2751. * @vptr: velocity to set WOL status on
  2752. *
  2753. * Set a card up for wake on lan either by unicast or by
  2754. * ARP packet.
  2755. *
  2756. * FIXME: check static buffer is safe here
  2757. */
  2758. static int velocity_set_wol(struct velocity_info *vptr)
  2759. {
  2760. struct mac_regs __iomem * regs = vptr->mac_regs;
  2761. static u8 buf[256];
  2762. int i;
  2763. static u32 mask_pattern[2][4] = {
  2764. {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
  2765. {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
  2766. };
  2767. writew(0xFFFF, &regs->WOLCRClr);
  2768. writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
  2769. writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
  2770. /*
  2771. if (vptr->wol_opts & VELOCITY_WOL_PHY)
  2772. writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
  2773. */
  2774. if (vptr->wol_opts & VELOCITY_WOL_UCAST) {
  2775. writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
  2776. }
  2777. if (vptr->wol_opts & VELOCITY_WOL_ARP) {
  2778. struct arp_packet *arp = (struct arp_packet *) buf;
  2779. u16 crc;
  2780. memset(buf, 0, sizeof(struct arp_packet) + 7);
  2781. for (i = 0; i < 4; i++)
  2782. writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
  2783. arp->type = htons(ETH_P_ARP);
  2784. arp->ar_op = htons(1);
  2785. memcpy(arp->ar_tip, vptr->ip_addr, 4);
  2786. crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
  2787. (u8 *) & mask_pattern[0][0]);
  2788. writew(crc, &regs->PatternCRC[0]);
  2789. writew(WOLCR_ARP_EN, &regs->WOLCRSet);
  2790. }
  2791. BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
  2792. BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
  2793. writew(0x0FFF, &regs->WOLSRClr);
  2794. if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
  2795. if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
  2796. MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
  2797. MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
  2798. }
  2799. if (vptr->mii_status & VELOCITY_SPEED_1000)
  2800. MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
  2801. BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
  2802. {
  2803. u8 GCR;
  2804. GCR = readb(&regs->CHIPGCR);
  2805. GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
  2806. writeb(GCR, &regs->CHIPGCR);
  2807. }
  2808. BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
  2809. /* Turn on SWPTAG just before entering power mode */
  2810. BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
  2811. /* Go to bed ..... */
  2812. BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
  2813. return 0;
  2814. }
  2815. static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
  2816. {
  2817. struct net_device *dev = pci_get_drvdata(pdev);
  2818. struct velocity_info *vptr = netdev_priv(dev);
  2819. unsigned long flags;
  2820. if(!netif_running(vptr->dev))
  2821. return 0;
  2822. netif_device_detach(vptr->dev);
  2823. spin_lock_irqsave(&vptr->lock, flags);
  2824. pci_save_state(pdev);
  2825. #ifdef ETHTOOL_GWOL
  2826. if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
  2827. velocity_get_ip(vptr);
  2828. velocity_save_context(vptr, &vptr->context);
  2829. velocity_shutdown(vptr);
  2830. velocity_set_wol(vptr);
  2831. pci_enable_wake(pdev, PCI_D3hot, 1);
  2832. pci_set_power_state(pdev, PCI_D3hot);
  2833. } else {
  2834. velocity_save_context(vptr, &vptr->context);
  2835. velocity_shutdown(vptr);
  2836. pci_disable_device(pdev);
  2837. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2838. }
  2839. #else
  2840. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2841. #endif
  2842. spin_unlock_irqrestore(&vptr->lock, flags);
  2843. return 0;
  2844. }
  2845. static int velocity_resume(struct pci_dev *pdev)
  2846. {
  2847. struct net_device *dev = pci_get_drvdata(pdev);
  2848. struct velocity_info *vptr = netdev_priv(dev);
  2849. unsigned long flags;
  2850. int i;
  2851. if(!netif_running(vptr->dev))
  2852. return 0;
  2853. pci_set_power_state(pdev, PCI_D0);
  2854. pci_enable_wake(pdev, 0, 0);
  2855. pci_restore_state(pdev);
  2856. mac_wol_reset(vptr->mac_regs);
  2857. spin_lock_irqsave(&vptr->lock, flags);
  2858. velocity_restore_context(vptr, &vptr->context);
  2859. velocity_init_registers(vptr, VELOCITY_INIT_WOL);
  2860. mac_disable_int(vptr->mac_regs);
  2861. velocity_tx_srv(vptr, 0);
  2862. for (i = 0; i < vptr->tx.numq; i++) {
  2863. if (vptr->tx.used[i]) {
  2864. mac_tx_queue_wake(vptr->mac_regs, i);
  2865. }
  2866. }
  2867. mac_enable_int(vptr->mac_regs);
  2868. spin_unlock_irqrestore(&vptr->lock, flags);
  2869. netif_device_attach(vptr->dev);
  2870. return 0;
  2871. }
  2872. #ifdef CONFIG_INET
  2873. static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
  2874. {
  2875. struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
  2876. struct net_device *dev = ifa->ifa_dev->dev;
  2877. struct velocity_info *vptr;
  2878. unsigned long flags;
  2879. if (dev_net(dev) != &init_net)
  2880. return NOTIFY_DONE;
  2881. spin_lock_irqsave(&velocity_dev_list_lock, flags);
  2882. list_for_each_entry(vptr, &velocity_dev_list, list) {
  2883. if (vptr->dev == dev) {
  2884. velocity_get_ip(vptr);
  2885. break;
  2886. }
  2887. }
  2888. spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
  2889. return NOTIFY_DONE;
  2890. }
  2891. #endif
  2892. #endif