via-velocity.c 85 KB

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