via-velocity.c 94 KB

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