via-velocity.c 96 KB

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