via-velocity.c 86 KB

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