jme.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328
  1. /*
  2. * JMicron JMC2x0 series PCIe Ethernet Linux Device Driver
  3. *
  4. * Copyright 2008 JMicron Technology Corporation
  5. * http://www.jmicron.com/
  6. * Copyright (c) 2009 - 2010 Guo-Fu Tseng <cooldavid@cooldavid.org>
  7. *
  8. * Author: Guo-Fu Tseng <cooldavid@cooldavid.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/pci.h>
  28. #include <linux/pci-aspm.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/mii.h>
  33. #include <linux/crc32.h>
  34. #include <linux/delay.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/in.h>
  37. #include <linux/ip.h>
  38. #include <linux/ipv6.h>
  39. #include <linux/tcp.h>
  40. #include <linux/udp.h>
  41. #include <linux/if_vlan.h>
  42. #include <linux/slab.h>
  43. #include <net/ip6_checksum.h>
  44. #include "jme.h"
  45. static int force_pseudohp = -1;
  46. static int no_pseudohp = -1;
  47. static int no_extplug = -1;
  48. module_param(force_pseudohp, int, 0);
  49. MODULE_PARM_DESC(force_pseudohp,
  50. "Enable pseudo hot-plug feature manually by driver instead of BIOS.");
  51. module_param(no_pseudohp, int, 0);
  52. MODULE_PARM_DESC(no_pseudohp, "Disable pseudo hot-plug feature.");
  53. module_param(no_extplug, int, 0);
  54. MODULE_PARM_DESC(no_extplug,
  55. "Do not use external plug signal for pseudo hot-plug.");
  56. static int
  57. jme_mdio_read(struct net_device *netdev, int phy, int reg)
  58. {
  59. struct jme_adapter *jme = netdev_priv(netdev);
  60. int i, val, again = (reg == MII_BMSR) ? 1 : 0;
  61. read_again:
  62. jwrite32(jme, JME_SMI, SMI_OP_REQ |
  63. smi_phy_addr(phy) |
  64. smi_reg_addr(reg));
  65. wmb();
  66. for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
  67. udelay(20);
  68. val = jread32(jme, JME_SMI);
  69. if ((val & SMI_OP_REQ) == 0)
  70. break;
  71. }
  72. if (i == 0) {
  73. pr_err("phy(%d) read timeout : %d\n", phy, reg);
  74. return 0;
  75. }
  76. if (again--)
  77. goto read_again;
  78. return (val & SMI_DATA_MASK) >> SMI_DATA_SHIFT;
  79. }
  80. static void
  81. jme_mdio_write(struct net_device *netdev,
  82. int phy, int reg, int val)
  83. {
  84. struct jme_adapter *jme = netdev_priv(netdev);
  85. int i;
  86. jwrite32(jme, JME_SMI, SMI_OP_WRITE | SMI_OP_REQ |
  87. ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
  88. smi_phy_addr(phy) | smi_reg_addr(reg));
  89. wmb();
  90. for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
  91. udelay(20);
  92. if ((jread32(jme, JME_SMI) & SMI_OP_REQ) == 0)
  93. break;
  94. }
  95. if (i == 0)
  96. pr_err("phy(%d) write timeout : %d\n", phy, reg);
  97. }
  98. static inline void
  99. jme_reset_phy_processor(struct jme_adapter *jme)
  100. {
  101. u32 val;
  102. jme_mdio_write(jme->dev,
  103. jme->mii_if.phy_id,
  104. MII_ADVERTISE, ADVERTISE_ALL |
  105. ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  106. if (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
  107. jme_mdio_write(jme->dev,
  108. jme->mii_if.phy_id,
  109. MII_CTRL1000,
  110. ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  111. val = jme_mdio_read(jme->dev,
  112. jme->mii_if.phy_id,
  113. MII_BMCR);
  114. jme_mdio_write(jme->dev,
  115. jme->mii_if.phy_id,
  116. MII_BMCR, val | BMCR_RESET);
  117. }
  118. static void
  119. jme_setup_wakeup_frame(struct jme_adapter *jme,
  120. const u32 *mask, u32 crc, int fnr)
  121. {
  122. int i;
  123. /*
  124. * Setup CRC pattern
  125. */
  126. jwrite32(jme, JME_WFOI, WFOI_CRC_SEL | (fnr & WFOI_FRAME_SEL));
  127. wmb();
  128. jwrite32(jme, JME_WFODP, crc);
  129. wmb();
  130. /*
  131. * Setup Mask
  132. */
  133. for (i = 0 ; i < WAKEUP_FRAME_MASK_DWNR ; ++i) {
  134. jwrite32(jme, JME_WFOI,
  135. ((i << WFOI_MASK_SHIFT) & WFOI_MASK_SEL) |
  136. (fnr & WFOI_FRAME_SEL));
  137. wmb();
  138. jwrite32(jme, JME_WFODP, mask[i]);
  139. wmb();
  140. }
  141. }
  142. static inline void
  143. jme_mac_rxclk_off(struct jme_adapter *jme)
  144. {
  145. jme->reg_gpreg1 |= GPREG1_RXCLKOFF;
  146. jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
  147. }
  148. static inline void
  149. jme_mac_rxclk_on(struct jme_adapter *jme)
  150. {
  151. jme->reg_gpreg1 &= ~GPREG1_RXCLKOFF;
  152. jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
  153. }
  154. static inline void
  155. jme_mac_txclk_off(struct jme_adapter *jme)
  156. {
  157. jme->reg_ghc &= ~(GHC_TO_CLK_SRC | GHC_TXMAC_CLK_SRC);
  158. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  159. }
  160. static inline void
  161. jme_mac_txclk_on(struct jme_adapter *jme)
  162. {
  163. u32 speed = jme->reg_ghc & GHC_SPEED;
  164. if (speed == GHC_SPEED_1000M)
  165. jme->reg_ghc |= GHC_TO_CLK_GPHY | GHC_TXMAC_CLK_GPHY;
  166. else
  167. jme->reg_ghc |= GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
  168. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  169. }
  170. static inline void
  171. jme_reset_ghc_speed(struct jme_adapter *jme)
  172. {
  173. jme->reg_ghc &= ~(GHC_SPEED | GHC_DPX);
  174. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  175. }
  176. static inline void
  177. jme_reset_250A2_workaround(struct jme_adapter *jme)
  178. {
  179. jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
  180. GPREG1_RSSPATCH);
  181. jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
  182. }
  183. static inline void
  184. jme_assert_ghc_reset(struct jme_adapter *jme)
  185. {
  186. jme->reg_ghc |= GHC_SWRST;
  187. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  188. }
  189. static inline void
  190. jme_clear_ghc_reset(struct jme_adapter *jme)
  191. {
  192. jme->reg_ghc &= ~GHC_SWRST;
  193. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  194. }
  195. static void
  196. jme_reset_mac_processor(struct jme_adapter *jme)
  197. {
  198. static const u32 mask[WAKEUP_FRAME_MASK_DWNR] = {0, 0, 0, 0};
  199. u32 crc = 0xCDCDCDCD;
  200. u32 gpreg0;
  201. int i;
  202. jme_reset_ghc_speed(jme);
  203. jme_reset_250A2_workaround(jme);
  204. jme_mac_rxclk_on(jme);
  205. jme_mac_txclk_on(jme);
  206. udelay(1);
  207. jme_assert_ghc_reset(jme);
  208. udelay(1);
  209. jme_mac_rxclk_off(jme);
  210. jme_mac_txclk_off(jme);
  211. udelay(1);
  212. jme_clear_ghc_reset(jme);
  213. udelay(1);
  214. jme_mac_rxclk_on(jme);
  215. jme_mac_txclk_on(jme);
  216. udelay(1);
  217. jme_mac_rxclk_off(jme);
  218. jme_mac_txclk_off(jme);
  219. jwrite32(jme, JME_RXDBA_LO, 0x00000000);
  220. jwrite32(jme, JME_RXDBA_HI, 0x00000000);
  221. jwrite32(jme, JME_RXQDC, 0x00000000);
  222. jwrite32(jme, JME_RXNDA, 0x00000000);
  223. jwrite32(jme, JME_TXDBA_LO, 0x00000000);
  224. jwrite32(jme, JME_TXDBA_HI, 0x00000000);
  225. jwrite32(jme, JME_TXQDC, 0x00000000);
  226. jwrite32(jme, JME_TXNDA, 0x00000000);
  227. jwrite32(jme, JME_RXMCHT_LO, 0x00000000);
  228. jwrite32(jme, JME_RXMCHT_HI, 0x00000000);
  229. for (i = 0 ; i < WAKEUP_FRAME_NR ; ++i)
  230. jme_setup_wakeup_frame(jme, mask, crc, i);
  231. if (jme->fpgaver)
  232. gpreg0 = GPREG0_DEFAULT | GPREG0_LNKINTPOLL;
  233. else
  234. gpreg0 = GPREG0_DEFAULT;
  235. jwrite32(jme, JME_GPREG0, gpreg0);
  236. }
  237. static inline void
  238. jme_clear_pm_enable_wol(struct jme_adapter *jme)
  239. {
  240. jwrite32(jme, JME_PMCS, PMCS_STMASK | jme->reg_pmcs);
  241. }
  242. static inline void
  243. jme_clear_pm_disable_wol(struct jme_adapter *jme)
  244. {
  245. jwrite32(jme, JME_PMCS, PMCS_STMASK);
  246. }
  247. static int
  248. jme_reload_eeprom(struct jme_adapter *jme)
  249. {
  250. u32 val;
  251. int i;
  252. val = jread32(jme, JME_SMBCSR);
  253. if (val & SMBCSR_EEPROMD) {
  254. val |= SMBCSR_CNACK;
  255. jwrite32(jme, JME_SMBCSR, val);
  256. val |= SMBCSR_RELOAD;
  257. jwrite32(jme, JME_SMBCSR, val);
  258. mdelay(12);
  259. for (i = JME_EEPROM_RELOAD_TIMEOUT; i > 0; --i) {
  260. mdelay(1);
  261. if ((jread32(jme, JME_SMBCSR) & SMBCSR_RELOAD) == 0)
  262. break;
  263. }
  264. if (i == 0) {
  265. pr_err("eeprom reload timeout\n");
  266. return -EIO;
  267. }
  268. }
  269. return 0;
  270. }
  271. static void
  272. jme_load_macaddr(struct net_device *netdev)
  273. {
  274. struct jme_adapter *jme = netdev_priv(netdev);
  275. unsigned char macaddr[ETH_ALEN];
  276. u32 val;
  277. spin_lock_bh(&jme->macaddr_lock);
  278. val = jread32(jme, JME_RXUMA_LO);
  279. macaddr[0] = (val >> 0) & 0xFF;
  280. macaddr[1] = (val >> 8) & 0xFF;
  281. macaddr[2] = (val >> 16) & 0xFF;
  282. macaddr[3] = (val >> 24) & 0xFF;
  283. val = jread32(jme, JME_RXUMA_HI);
  284. macaddr[4] = (val >> 0) & 0xFF;
  285. macaddr[5] = (val >> 8) & 0xFF;
  286. memcpy(netdev->dev_addr, macaddr, ETH_ALEN);
  287. spin_unlock_bh(&jme->macaddr_lock);
  288. }
  289. static inline void
  290. jme_set_rx_pcc(struct jme_adapter *jme, int p)
  291. {
  292. switch (p) {
  293. case PCC_OFF:
  294. jwrite32(jme, JME_PCCRX0,
  295. ((PCC_OFF_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  296. ((PCC_OFF_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  297. break;
  298. case PCC_P1:
  299. jwrite32(jme, JME_PCCRX0,
  300. ((PCC_P1_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  301. ((PCC_P1_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  302. break;
  303. case PCC_P2:
  304. jwrite32(jme, JME_PCCRX0,
  305. ((PCC_P2_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  306. ((PCC_P2_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  307. break;
  308. case PCC_P3:
  309. jwrite32(jme, JME_PCCRX0,
  310. ((PCC_P3_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  311. ((PCC_P3_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  312. break;
  313. default:
  314. break;
  315. }
  316. wmb();
  317. if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
  318. netif_info(jme, rx_status, jme->dev, "Switched to PCC_P%d\n", p);
  319. }
  320. static void
  321. jme_start_irq(struct jme_adapter *jme)
  322. {
  323. register struct dynpcc_info *dpi = &(jme->dpi);
  324. jme_set_rx_pcc(jme, PCC_P1);
  325. dpi->cur = PCC_P1;
  326. dpi->attempt = PCC_P1;
  327. dpi->cnt = 0;
  328. jwrite32(jme, JME_PCCTX,
  329. ((PCC_TX_TO << PCCTXTO_SHIFT) & PCCTXTO_MASK) |
  330. ((PCC_TX_CNT << PCCTX_SHIFT) & PCCTX_MASK) |
  331. PCCTXQ0_EN
  332. );
  333. /*
  334. * Enable Interrupts
  335. */
  336. jwrite32(jme, JME_IENS, INTR_ENABLE);
  337. }
  338. static inline void
  339. jme_stop_irq(struct jme_adapter *jme)
  340. {
  341. /*
  342. * Disable Interrupts
  343. */
  344. jwrite32f(jme, JME_IENC, INTR_ENABLE);
  345. }
  346. static u32
  347. jme_linkstat_from_phy(struct jme_adapter *jme)
  348. {
  349. u32 phylink, bmsr;
  350. phylink = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 17);
  351. bmsr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMSR);
  352. if (bmsr & BMSR_ANCOMP)
  353. phylink |= PHY_LINK_AUTONEG_COMPLETE;
  354. return phylink;
  355. }
  356. static inline void
  357. jme_set_phyfifo_5level(struct jme_adapter *jme)
  358. {
  359. jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0004);
  360. }
  361. static inline void
  362. jme_set_phyfifo_8level(struct jme_adapter *jme)
  363. {
  364. jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0000);
  365. }
  366. static int
  367. jme_check_link(struct net_device *netdev, int testonly)
  368. {
  369. struct jme_adapter *jme = netdev_priv(netdev);
  370. u32 phylink, cnt = JME_SPDRSV_TIMEOUT, bmcr;
  371. char linkmsg[64];
  372. int rc = 0;
  373. linkmsg[0] = '\0';
  374. if (jme->fpgaver)
  375. phylink = jme_linkstat_from_phy(jme);
  376. else
  377. phylink = jread32(jme, JME_PHY_LINK);
  378. if (phylink & PHY_LINK_UP) {
  379. if (!(phylink & PHY_LINK_AUTONEG_COMPLETE)) {
  380. /*
  381. * If we did not enable AN
  382. * Speed/Duplex Info should be obtained from SMI
  383. */
  384. phylink = PHY_LINK_UP;
  385. bmcr = jme_mdio_read(jme->dev,
  386. jme->mii_if.phy_id,
  387. MII_BMCR);
  388. phylink |= ((bmcr & BMCR_SPEED1000) &&
  389. (bmcr & BMCR_SPEED100) == 0) ?
  390. PHY_LINK_SPEED_1000M :
  391. (bmcr & BMCR_SPEED100) ?
  392. PHY_LINK_SPEED_100M :
  393. PHY_LINK_SPEED_10M;
  394. phylink |= (bmcr & BMCR_FULLDPLX) ?
  395. PHY_LINK_DUPLEX : 0;
  396. strcat(linkmsg, "Forced: ");
  397. } else {
  398. /*
  399. * Keep polling for speed/duplex resolve complete
  400. */
  401. while (!(phylink & PHY_LINK_SPEEDDPU_RESOLVED) &&
  402. --cnt) {
  403. udelay(1);
  404. if (jme->fpgaver)
  405. phylink = jme_linkstat_from_phy(jme);
  406. else
  407. phylink = jread32(jme, JME_PHY_LINK);
  408. }
  409. if (!cnt)
  410. pr_err("Waiting speed resolve timeout\n");
  411. strcat(linkmsg, "ANed: ");
  412. }
  413. if (jme->phylink == phylink) {
  414. rc = 1;
  415. goto out;
  416. }
  417. if (testonly)
  418. goto out;
  419. jme->phylink = phylink;
  420. /*
  421. * The speed/duplex setting of jme->reg_ghc already cleared
  422. * by jme_reset_mac_processor()
  423. */
  424. switch (phylink & PHY_LINK_SPEED_MASK) {
  425. case PHY_LINK_SPEED_10M:
  426. jme->reg_ghc |= GHC_SPEED_10M;
  427. strcat(linkmsg, "10 Mbps, ");
  428. break;
  429. case PHY_LINK_SPEED_100M:
  430. jme->reg_ghc |= GHC_SPEED_100M;
  431. strcat(linkmsg, "100 Mbps, ");
  432. break;
  433. case PHY_LINK_SPEED_1000M:
  434. jme->reg_ghc |= GHC_SPEED_1000M;
  435. strcat(linkmsg, "1000 Mbps, ");
  436. break;
  437. default:
  438. break;
  439. }
  440. if (phylink & PHY_LINK_DUPLEX) {
  441. jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT);
  442. jwrite32(jme, JME_TXTRHD, TXTRHD_FULLDUPLEX);
  443. jme->reg_ghc |= GHC_DPX;
  444. } else {
  445. jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT |
  446. TXMCS_BACKOFF |
  447. TXMCS_CARRIERSENSE |
  448. TXMCS_COLLISION);
  449. jwrite32(jme, JME_TXTRHD, TXTRHD_HALFDUPLEX);
  450. }
  451. jwrite32(jme, JME_GHC, jme->reg_ghc);
  452. if (is_buggy250(jme->pdev->device, jme->chiprev)) {
  453. jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
  454. GPREG1_RSSPATCH);
  455. if (!(phylink & PHY_LINK_DUPLEX))
  456. jme->reg_gpreg1 |= GPREG1_HALFMODEPATCH;
  457. switch (phylink & PHY_LINK_SPEED_MASK) {
  458. case PHY_LINK_SPEED_10M:
  459. jme_set_phyfifo_8level(jme);
  460. jme->reg_gpreg1 |= GPREG1_RSSPATCH;
  461. break;
  462. case PHY_LINK_SPEED_100M:
  463. jme_set_phyfifo_5level(jme);
  464. jme->reg_gpreg1 |= GPREG1_RSSPATCH;
  465. break;
  466. case PHY_LINK_SPEED_1000M:
  467. jme_set_phyfifo_8level(jme);
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
  474. strcat(linkmsg, (phylink & PHY_LINK_DUPLEX) ?
  475. "Full-Duplex, " :
  476. "Half-Duplex, ");
  477. strcat(linkmsg, (phylink & PHY_LINK_MDI_STAT) ?
  478. "MDI-X" :
  479. "MDI");
  480. netif_info(jme, link, jme->dev, "Link is up at %s\n", linkmsg);
  481. netif_carrier_on(netdev);
  482. } else {
  483. if (testonly)
  484. goto out;
  485. netif_info(jme, link, jme->dev, "Link is down\n");
  486. jme->phylink = 0;
  487. netif_carrier_off(netdev);
  488. }
  489. out:
  490. return rc;
  491. }
  492. static int
  493. jme_setup_tx_resources(struct jme_adapter *jme)
  494. {
  495. struct jme_ring *txring = &(jme->txring[0]);
  496. txring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
  497. TX_RING_ALLOC_SIZE(jme->tx_ring_size),
  498. &(txring->dmaalloc),
  499. GFP_ATOMIC);
  500. if (!txring->alloc)
  501. goto err_set_null;
  502. /*
  503. * 16 Bytes align
  504. */
  505. txring->desc = (void *)ALIGN((unsigned long)(txring->alloc),
  506. RING_DESC_ALIGN);
  507. txring->dma = ALIGN(txring->dmaalloc, RING_DESC_ALIGN);
  508. txring->next_to_use = 0;
  509. atomic_set(&txring->next_to_clean, 0);
  510. atomic_set(&txring->nr_free, jme->tx_ring_size);
  511. txring->bufinf = kcalloc(jme->tx_ring_size,
  512. sizeof(struct jme_buffer_info),
  513. GFP_ATOMIC);
  514. if (unlikely(!(txring->bufinf)))
  515. goto err_free_txring;
  516. /*
  517. * Initialize Transmit Descriptors
  518. */
  519. memset(txring->alloc, 0, TX_RING_ALLOC_SIZE(jme->tx_ring_size));
  520. return 0;
  521. err_free_txring:
  522. dma_free_coherent(&(jme->pdev->dev),
  523. TX_RING_ALLOC_SIZE(jme->tx_ring_size),
  524. txring->alloc,
  525. txring->dmaalloc);
  526. err_set_null:
  527. txring->desc = NULL;
  528. txring->dmaalloc = 0;
  529. txring->dma = 0;
  530. txring->bufinf = NULL;
  531. return -ENOMEM;
  532. }
  533. static void
  534. jme_free_tx_resources(struct jme_adapter *jme)
  535. {
  536. int i;
  537. struct jme_ring *txring = &(jme->txring[0]);
  538. struct jme_buffer_info *txbi;
  539. if (txring->alloc) {
  540. if (txring->bufinf) {
  541. for (i = 0 ; i < jme->tx_ring_size ; ++i) {
  542. txbi = txring->bufinf + i;
  543. if (txbi->skb) {
  544. dev_kfree_skb(txbi->skb);
  545. txbi->skb = NULL;
  546. }
  547. txbi->mapping = 0;
  548. txbi->len = 0;
  549. txbi->nr_desc = 0;
  550. txbi->start_xmit = 0;
  551. }
  552. kfree(txring->bufinf);
  553. }
  554. dma_free_coherent(&(jme->pdev->dev),
  555. TX_RING_ALLOC_SIZE(jme->tx_ring_size),
  556. txring->alloc,
  557. txring->dmaalloc);
  558. txring->alloc = NULL;
  559. txring->desc = NULL;
  560. txring->dmaalloc = 0;
  561. txring->dma = 0;
  562. txring->bufinf = NULL;
  563. }
  564. txring->next_to_use = 0;
  565. atomic_set(&txring->next_to_clean, 0);
  566. atomic_set(&txring->nr_free, 0);
  567. }
  568. static inline void
  569. jme_enable_tx_engine(struct jme_adapter *jme)
  570. {
  571. /*
  572. * Select Queue 0
  573. */
  574. jwrite32(jme, JME_TXCS, TXCS_DEFAULT | TXCS_SELECT_QUEUE0);
  575. wmb();
  576. /*
  577. * Setup TX Queue 0 DMA Bass Address
  578. */
  579. jwrite32(jme, JME_TXDBA_LO, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
  580. jwrite32(jme, JME_TXDBA_HI, (__u64)(jme->txring[0].dma) >> 32);
  581. jwrite32(jme, JME_TXNDA, (__u64)jme->txring[0].dma & 0xFFFFFFFFUL);
  582. /*
  583. * Setup TX Descptor Count
  584. */
  585. jwrite32(jme, JME_TXQDC, jme->tx_ring_size);
  586. /*
  587. * Enable TX Engine
  588. */
  589. wmb();
  590. jwrite32f(jme, JME_TXCS, jme->reg_txcs |
  591. TXCS_SELECT_QUEUE0 |
  592. TXCS_ENABLE);
  593. /*
  594. * Start clock for TX MAC Processor
  595. */
  596. jme_mac_txclk_on(jme);
  597. }
  598. static inline void
  599. jme_disable_tx_engine(struct jme_adapter *jme)
  600. {
  601. int i;
  602. u32 val;
  603. /*
  604. * Disable TX Engine
  605. */
  606. jwrite32(jme, JME_TXCS, jme->reg_txcs | TXCS_SELECT_QUEUE0);
  607. wmb();
  608. val = jread32(jme, JME_TXCS);
  609. for (i = JME_TX_DISABLE_TIMEOUT ; (val & TXCS_ENABLE) && i > 0 ; --i) {
  610. mdelay(1);
  611. val = jread32(jme, JME_TXCS);
  612. rmb();
  613. }
  614. if (!i)
  615. pr_err("Disable TX engine timeout\n");
  616. /*
  617. * Stop clock for TX MAC Processor
  618. */
  619. jme_mac_txclk_off(jme);
  620. }
  621. static void
  622. jme_set_clean_rxdesc(struct jme_adapter *jme, int i)
  623. {
  624. struct jme_ring *rxring = &(jme->rxring[0]);
  625. register struct rxdesc *rxdesc = rxring->desc;
  626. struct jme_buffer_info *rxbi = rxring->bufinf;
  627. rxdesc += i;
  628. rxbi += i;
  629. rxdesc->dw[0] = 0;
  630. rxdesc->dw[1] = 0;
  631. rxdesc->desc1.bufaddrh = cpu_to_le32((__u64)rxbi->mapping >> 32);
  632. rxdesc->desc1.bufaddrl = cpu_to_le32(
  633. (__u64)rxbi->mapping & 0xFFFFFFFFUL);
  634. rxdesc->desc1.datalen = cpu_to_le16(rxbi->len);
  635. if (jme->dev->features & NETIF_F_HIGHDMA)
  636. rxdesc->desc1.flags = RXFLAG_64BIT;
  637. wmb();
  638. rxdesc->desc1.flags |= RXFLAG_OWN | RXFLAG_INT;
  639. }
  640. static int
  641. jme_make_new_rx_buf(struct jme_adapter *jme, int i)
  642. {
  643. struct jme_ring *rxring = &(jme->rxring[0]);
  644. struct jme_buffer_info *rxbi = rxring->bufinf + i;
  645. struct sk_buff *skb;
  646. dma_addr_t mapping;
  647. skb = netdev_alloc_skb(jme->dev,
  648. jme->dev->mtu + RX_EXTRA_LEN);
  649. if (unlikely(!skb))
  650. return -ENOMEM;
  651. mapping = pci_map_page(jme->pdev, virt_to_page(skb->data),
  652. offset_in_page(skb->data), skb_tailroom(skb),
  653. PCI_DMA_FROMDEVICE);
  654. if (unlikely(pci_dma_mapping_error(jme->pdev, mapping))) {
  655. dev_kfree_skb(skb);
  656. return -ENOMEM;
  657. }
  658. if (likely(rxbi->mapping))
  659. pci_unmap_page(jme->pdev, rxbi->mapping,
  660. rxbi->len, PCI_DMA_FROMDEVICE);
  661. rxbi->skb = skb;
  662. rxbi->len = skb_tailroom(skb);
  663. rxbi->mapping = mapping;
  664. return 0;
  665. }
  666. static void
  667. jme_free_rx_buf(struct jme_adapter *jme, int i)
  668. {
  669. struct jme_ring *rxring = &(jme->rxring[0]);
  670. struct jme_buffer_info *rxbi = rxring->bufinf;
  671. rxbi += i;
  672. if (rxbi->skb) {
  673. pci_unmap_page(jme->pdev,
  674. rxbi->mapping,
  675. rxbi->len,
  676. PCI_DMA_FROMDEVICE);
  677. dev_kfree_skb(rxbi->skb);
  678. rxbi->skb = NULL;
  679. rxbi->mapping = 0;
  680. rxbi->len = 0;
  681. }
  682. }
  683. static void
  684. jme_free_rx_resources(struct jme_adapter *jme)
  685. {
  686. int i;
  687. struct jme_ring *rxring = &(jme->rxring[0]);
  688. if (rxring->alloc) {
  689. if (rxring->bufinf) {
  690. for (i = 0 ; i < jme->rx_ring_size ; ++i)
  691. jme_free_rx_buf(jme, i);
  692. kfree(rxring->bufinf);
  693. }
  694. dma_free_coherent(&(jme->pdev->dev),
  695. RX_RING_ALLOC_SIZE(jme->rx_ring_size),
  696. rxring->alloc,
  697. rxring->dmaalloc);
  698. rxring->alloc = NULL;
  699. rxring->desc = NULL;
  700. rxring->dmaalloc = 0;
  701. rxring->dma = 0;
  702. rxring->bufinf = NULL;
  703. }
  704. rxring->next_to_use = 0;
  705. atomic_set(&rxring->next_to_clean, 0);
  706. }
  707. static int
  708. jme_setup_rx_resources(struct jme_adapter *jme)
  709. {
  710. int i;
  711. struct jme_ring *rxring = &(jme->rxring[0]);
  712. rxring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
  713. RX_RING_ALLOC_SIZE(jme->rx_ring_size),
  714. &(rxring->dmaalloc),
  715. GFP_ATOMIC);
  716. if (!rxring->alloc)
  717. goto err_set_null;
  718. /*
  719. * 16 Bytes align
  720. */
  721. rxring->desc = (void *)ALIGN((unsigned long)(rxring->alloc),
  722. RING_DESC_ALIGN);
  723. rxring->dma = ALIGN(rxring->dmaalloc, RING_DESC_ALIGN);
  724. rxring->next_to_use = 0;
  725. atomic_set(&rxring->next_to_clean, 0);
  726. rxring->bufinf = kcalloc(jme->rx_ring_size,
  727. sizeof(struct jme_buffer_info),
  728. GFP_ATOMIC);
  729. if (unlikely(!(rxring->bufinf)))
  730. goto err_free_rxring;
  731. /*
  732. * Initiallize Receive Descriptors
  733. */
  734. for (i = 0 ; i < jme->rx_ring_size ; ++i) {
  735. if (unlikely(jme_make_new_rx_buf(jme, i))) {
  736. jme_free_rx_resources(jme);
  737. return -ENOMEM;
  738. }
  739. jme_set_clean_rxdesc(jme, i);
  740. }
  741. return 0;
  742. err_free_rxring:
  743. dma_free_coherent(&(jme->pdev->dev),
  744. RX_RING_ALLOC_SIZE(jme->rx_ring_size),
  745. rxring->alloc,
  746. rxring->dmaalloc);
  747. err_set_null:
  748. rxring->desc = NULL;
  749. rxring->dmaalloc = 0;
  750. rxring->dma = 0;
  751. rxring->bufinf = NULL;
  752. return -ENOMEM;
  753. }
  754. static inline void
  755. jme_enable_rx_engine(struct jme_adapter *jme)
  756. {
  757. /*
  758. * Select Queue 0
  759. */
  760. jwrite32(jme, JME_RXCS, jme->reg_rxcs |
  761. RXCS_QUEUESEL_Q0);
  762. wmb();
  763. /*
  764. * Setup RX DMA Bass Address
  765. */
  766. jwrite32(jme, JME_RXDBA_LO, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
  767. jwrite32(jme, JME_RXDBA_HI, (__u64)(jme->rxring[0].dma) >> 32);
  768. jwrite32(jme, JME_RXNDA, (__u64)(jme->rxring[0].dma) & 0xFFFFFFFFUL);
  769. /*
  770. * Setup RX Descriptor Count
  771. */
  772. jwrite32(jme, JME_RXQDC, jme->rx_ring_size);
  773. /*
  774. * Setup Unicast Filter
  775. */
  776. jme_set_unicastaddr(jme->dev);
  777. jme_set_multi(jme->dev);
  778. /*
  779. * Enable RX Engine
  780. */
  781. wmb();
  782. jwrite32f(jme, JME_RXCS, jme->reg_rxcs |
  783. RXCS_QUEUESEL_Q0 |
  784. RXCS_ENABLE |
  785. RXCS_QST);
  786. /*
  787. * Start clock for RX MAC Processor
  788. */
  789. jme_mac_rxclk_on(jme);
  790. }
  791. static inline void
  792. jme_restart_rx_engine(struct jme_adapter *jme)
  793. {
  794. /*
  795. * Start RX Engine
  796. */
  797. jwrite32(jme, JME_RXCS, jme->reg_rxcs |
  798. RXCS_QUEUESEL_Q0 |
  799. RXCS_ENABLE |
  800. RXCS_QST);
  801. }
  802. static inline void
  803. jme_disable_rx_engine(struct jme_adapter *jme)
  804. {
  805. int i;
  806. u32 val;
  807. /*
  808. * Disable RX Engine
  809. */
  810. jwrite32(jme, JME_RXCS, jme->reg_rxcs);
  811. wmb();
  812. val = jread32(jme, JME_RXCS);
  813. for (i = JME_RX_DISABLE_TIMEOUT ; (val & RXCS_ENABLE) && i > 0 ; --i) {
  814. mdelay(1);
  815. val = jread32(jme, JME_RXCS);
  816. rmb();
  817. }
  818. if (!i)
  819. pr_err("Disable RX engine timeout\n");
  820. /*
  821. * Stop clock for RX MAC Processor
  822. */
  823. jme_mac_rxclk_off(jme);
  824. }
  825. static u16
  826. jme_udpsum(struct sk_buff *skb)
  827. {
  828. u16 csum = 0xFFFFu;
  829. if (skb->len < (ETH_HLEN + sizeof(struct iphdr)))
  830. return csum;
  831. if (skb->protocol != htons(ETH_P_IP))
  832. return csum;
  833. skb_set_network_header(skb, ETH_HLEN);
  834. if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
  835. (skb->len < (ETH_HLEN +
  836. (ip_hdr(skb)->ihl << 2) +
  837. sizeof(struct udphdr)))) {
  838. skb_reset_network_header(skb);
  839. return csum;
  840. }
  841. skb_set_transport_header(skb,
  842. ETH_HLEN + (ip_hdr(skb)->ihl << 2));
  843. csum = udp_hdr(skb)->check;
  844. skb_reset_transport_header(skb);
  845. skb_reset_network_header(skb);
  846. return csum;
  847. }
  848. static int
  849. jme_rxsum_ok(struct jme_adapter *jme, u16 flags, struct sk_buff *skb)
  850. {
  851. if (!(flags & (RXWBFLAG_TCPON | RXWBFLAG_UDPON | RXWBFLAG_IPV4)))
  852. return false;
  853. if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_TCPON | RXWBFLAG_TCPCS))
  854. == RXWBFLAG_TCPON)) {
  855. if (flags & RXWBFLAG_IPV4)
  856. netif_err(jme, rx_err, jme->dev, "TCP Checksum error\n");
  857. return false;
  858. }
  859. if (unlikely((flags & (RXWBFLAG_MF | RXWBFLAG_UDPON | RXWBFLAG_UDPCS))
  860. == RXWBFLAG_UDPON) && jme_udpsum(skb)) {
  861. if (flags & RXWBFLAG_IPV4)
  862. netif_err(jme, rx_err, jme->dev, "UDP Checksum error\n");
  863. return false;
  864. }
  865. if (unlikely((flags & (RXWBFLAG_IPV4 | RXWBFLAG_IPCS))
  866. == RXWBFLAG_IPV4)) {
  867. netif_err(jme, rx_err, jme->dev, "IPv4 Checksum error\n");
  868. return false;
  869. }
  870. return true;
  871. }
  872. static void
  873. jme_alloc_and_feed_skb(struct jme_adapter *jme, int idx)
  874. {
  875. struct jme_ring *rxring = &(jme->rxring[0]);
  876. struct rxdesc *rxdesc = rxring->desc;
  877. struct jme_buffer_info *rxbi = rxring->bufinf;
  878. struct sk_buff *skb;
  879. int framesize;
  880. rxdesc += idx;
  881. rxbi += idx;
  882. skb = rxbi->skb;
  883. pci_dma_sync_single_for_cpu(jme->pdev,
  884. rxbi->mapping,
  885. rxbi->len,
  886. PCI_DMA_FROMDEVICE);
  887. if (unlikely(jme_make_new_rx_buf(jme, idx))) {
  888. pci_dma_sync_single_for_device(jme->pdev,
  889. rxbi->mapping,
  890. rxbi->len,
  891. PCI_DMA_FROMDEVICE);
  892. ++(NET_STAT(jme).rx_dropped);
  893. } else {
  894. framesize = le16_to_cpu(rxdesc->descwb.framesize)
  895. - RX_PREPAD_SIZE;
  896. skb_reserve(skb, RX_PREPAD_SIZE);
  897. skb_put(skb, framesize);
  898. skb->protocol = eth_type_trans(skb, jme->dev);
  899. if (jme_rxsum_ok(jme, le16_to_cpu(rxdesc->descwb.flags), skb))
  900. skb->ip_summed = CHECKSUM_UNNECESSARY;
  901. else
  902. skb_checksum_none_assert(skb);
  903. if (rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_TAGON)) {
  904. u16 vid = le16_to_cpu(rxdesc->descwb.vlan);
  905. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  906. NET_STAT(jme).rx_bytes += 4;
  907. }
  908. jme->jme_rx(skb);
  909. if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_DEST)) ==
  910. cpu_to_le16(RXWBFLAG_DEST_MUL))
  911. ++(NET_STAT(jme).multicast);
  912. NET_STAT(jme).rx_bytes += framesize;
  913. ++(NET_STAT(jme).rx_packets);
  914. }
  915. jme_set_clean_rxdesc(jme, idx);
  916. }
  917. static int
  918. jme_process_receive(struct jme_adapter *jme, int limit)
  919. {
  920. struct jme_ring *rxring = &(jme->rxring[0]);
  921. struct rxdesc *rxdesc;
  922. int i, j, ccnt, desccnt, mask = jme->rx_ring_mask;
  923. if (unlikely(!atomic_dec_and_test(&jme->rx_cleaning)))
  924. goto out_inc;
  925. if (unlikely(atomic_read(&jme->link_changing) != 1))
  926. goto out_inc;
  927. if (unlikely(!netif_carrier_ok(jme->dev)))
  928. goto out_inc;
  929. i = atomic_read(&rxring->next_to_clean);
  930. while (limit > 0) {
  931. rxdesc = rxring->desc;
  932. rxdesc += i;
  933. if ((rxdesc->descwb.flags & cpu_to_le16(RXWBFLAG_OWN)) ||
  934. !(rxdesc->descwb.desccnt & RXWBDCNT_WBCPL))
  935. goto out;
  936. --limit;
  937. rmb();
  938. desccnt = rxdesc->descwb.desccnt & RXWBDCNT_DCNT;
  939. if (unlikely(desccnt > 1 ||
  940. rxdesc->descwb.errstat & RXWBERR_ALLERR)) {
  941. if (rxdesc->descwb.errstat & RXWBERR_CRCERR)
  942. ++(NET_STAT(jme).rx_crc_errors);
  943. else if (rxdesc->descwb.errstat & RXWBERR_OVERUN)
  944. ++(NET_STAT(jme).rx_fifo_errors);
  945. else
  946. ++(NET_STAT(jme).rx_errors);
  947. if (desccnt > 1)
  948. limit -= desccnt - 1;
  949. for (j = i, ccnt = desccnt ; ccnt-- ; ) {
  950. jme_set_clean_rxdesc(jme, j);
  951. j = (j + 1) & (mask);
  952. }
  953. } else {
  954. jme_alloc_and_feed_skb(jme, i);
  955. }
  956. i = (i + desccnt) & (mask);
  957. }
  958. out:
  959. atomic_set(&rxring->next_to_clean, i);
  960. out_inc:
  961. atomic_inc(&jme->rx_cleaning);
  962. return limit > 0 ? limit : 0;
  963. }
  964. static void
  965. jme_attempt_pcc(struct dynpcc_info *dpi, int atmp)
  966. {
  967. if (likely(atmp == dpi->cur)) {
  968. dpi->cnt = 0;
  969. return;
  970. }
  971. if (dpi->attempt == atmp) {
  972. ++(dpi->cnt);
  973. } else {
  974. dpi->attempt = atmp;
  975. dpi->cnt = 0;
  976. }
  977. }
  978. static void
  979. jme_dynamic_pcc(struct jme_adapter *jme)
  980. {
  981. register struct dynpcc_info *dpi = &(jme->dpi);
  982. if ((NET_STAT(jme).rx_bytes - dpi->last_bytes) > PCC_P3_THRESHOLD)
  983. jme_attempt_pcc(dpi, PCC_P3);
  984. else if ((NET_STAT(jme).rx_packets - dpi->last_pkts) > PCC_P2_THRESHOLD ||
  985. dpi->intr_cnt > PCC_INTR_THRESHOLD)
  986. jme_attempt_pcc(dpi, PCC_P2);
  987. else
  988. jme_attempt_pcc(dpi, PCC_P1);
  989. if (unlikely(dpi->attempt != dpi->cur && dpi->cnt > 5)) {
  990. if (dpi->attempt < dpi->cur)
  991. tasklet_schedule(&jme->rxclean_task);
  992. jme_set_rx_pcc(jme, dpi->attempt);
  993. dpi->cur = dpi->attempt;
  994. dpi->cnt = 0;
  995. }
  996. }
  997. static void
  998. jme_start_pcc_timer(struct jme_adapter *jme)
  999. {
  1000. struct dynpcc_info *dpi = &(jme->dpi);
  1001. dpi->last_bytes = NET_STAT(jme).rx_bytes;
  1002. dpi->last_pkts = NET_STAT(jme).rx_packets;
  1003. dpi->intr_cnt = 0;
  1004. jwrite32(jme, JME_TMCSR,
  1005. TMCSR_EN | ((0xFFFFFF - PCC_INTERVAL_US) & TMCSR_CNT));
  1006. }
  1007. static inline void
  1008. jme_stop_pcc_timer(struct jme_adapter *jme)
  1009. {
  1010. jwrite32(jme, JME_TMCSR, 0);
  1011. }
  1012. static void
  1013. jme_shutdown_nic(struct jme_adapter *jme)
  1014. {
  1015. u32 phylink;
  1016. phylink = jme_linkstat_from_phy(jme);
  1017. if (!(phylink & PHY_LINK_UP)) {
  1018. /*
  1019. * Disable all interrupt before issue timer
  1020. */
  1021. jme_stop_irq(jme);
  1022. jwrite32(jme, JME_TIMER2, TMCSR_EN | 0xFFFFFE);
  1023. }
  1024. }
  1025. static void
  1026. jme_pcc_tasklet(unsigned long arg)
  1027. {
  1028. struct jme_adapter *jme = (struct jme_adapter *)arg;
  1029. struct net_device *netdev = jme->dev;
  1030. if (unlikely(test_bit(JME_FLAG_SHUTDOWN, &jme->flags))) {
  1031. jme_shutdown_nic(jme);
  1032. return;
  1033. }
  1034. if (unlikely(!netif_carrier_ok(netdev) ||
  1035. (atomic_read(&jme->link_changing) != 1)
  1036. )) {
  1037. jme_stop_pcc_timer(jme);
  1038. return;
  1039. }
  1040. if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
  1041. jme_dynamic_pcc(jme);
  1042. jme_start_pcc_timer(jme);
  1043. }
  1044. static inline void
  1045. jme_polling_mode(struct jme_adapter *jme)
  1046. {
  1047. jme_set_rx_pcc(jme, PCC_OFF);
  1048. }
  1049. static inline void
  1050. jme_interrupt_mode(struct jme_adapter *jme)
  1051. {
  1052. jme_set_rx_pcc(jme, PCC_P1);
  1053. }
  1054. static inline int
  1055. jme_pseudo_hotplug_enabled(struct jme_adapter *jme)
  1056. {
  1057. u32 apmc;
  1058. apmc = jread32(jme, JME_APMC);
  1059. return apmc & JME_APMC_PSEUDO_HP_EN;
  1060. }
  1061. static void
  1062. jme_start_shutdown_timer(struct jme_adapter *jme)
  1063. {
  1064. u32 apmc;
  1065. apmc = jread32(jme, JME_APMC) | JME_APMC_PCIE_SD_EN;
  1066. apmc &= ~JME_APMC_EPIEN_CTRL;
  1067. if (!no_extplug) {
  1068. jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_EN);
  1069. wmb();
  1070. }
  1071. jwrite32f(jme, JME_APMC, apmc);
  1072. jwrite32f(jme, JME_TIMER2, 0);
  1073. set_bit(JME_FLAG_SHUTDOWN, &jme->flags);
  1074. jwrite32(jme, JME_TMCSR,
  1075. TMCSR_EN | ((0xFFFFFF - APMC_PHP_SHUTDOWN_DELAY) & TMCSR_CNT));
  1076. }
  1077. static void
  1078. jme_stop_shutdown_timer(struct jme_adapter *jme)
  1079. {
  1080. u32 apmc;
  1081. jwrite32f(jme, JME_TMCSR, 0);
  1082. jwrite32f(jme, JME_TIMER2, 0);
  1083. clear_bit(JME_FLAG_SHUTDOWN, &jme->flags);
  1084. apmc = jread32(jme, JME_APMC);
  1085. apmc &= ~(JME_APMC_PCIE_SD_EN | JME_APMC_EPIEN_CTRL);
  1086. jwrite32f(jme, JME_APMC, apmc | JME_APMC_EPIEN_CTRL_DIS);
  1087. wmb();
  1088. jwrite32f(jme, JME_APMC, apmc);
  1089. }
  1090. static void
  1091. jme_link_change_tasklet(unsigned long arg)
  1092. {
  1093. struct jme_adapter *jme = (struct jme_adapter *)arg;
  1094. struct net_device *netdev = jme->dev;
  1095. int rc;
  1096. while (!atomic_dec_and_test(&jme->link_changing)) {
  1097. atomic_inc(&jme->link_changing);
  1098. netif_info(jme, intr, jme->dev, "Get link change lock failed\n");
  1099. while (atomic_read(&jme->link_changing) != 1)
  1100. netif_info(jme, intr, jme->dev, "Waiting link change lock\n");
  1101. }
  1102. if (jme_check_link(netdev, 1) && jme->old_mtu == netdev->mtu)
  1103. goto out;
  1104. jme->old_mtu = netdev->mtu;
  1105. netif_stop_queue(netdev);
  1106. if (jme_pseudo_hotplug_enabled(jme))
  1107. jme_stop_shutdown_timer(jme);
  1108. jme_stop_pcc_timer(jme);
  1109. tasklet_disable(&jme->txclean_task);
  1110. tasklet_disable(&jme->rxclean_task);
  1111. tasklet_disable(&jme->rxempty_task);
  1112. if (netif_carrier_ok(netdev)) {
  1113. jme_disable_rx_engine(jme);
  1114. jme_disable_tx_engine(jme);
  1115. jme_reset_mac_processor(jme);
  1116. jme_free_rx_resources(jme);
  1117. jme_free_tx_resources(jme);
  1118. if (test_bit(JME_FLAG_POLL, &jme->flags))
  1119. jme_polling_mode(jme);
  1120. netif_carrier_off(netdev);
  1121. }
  1122. jme_check_link(netdev, 0);
  1123. if (netif_carrier_ok(netdev)) {
  1124. rc = jme_setup_rx_resources(jme);
  1125. if (rc) {
  1126. pr_err("Allocating resources for RX error, Device STOPPED!\n");
  1127. goto out_enable_tasklet;
  1128. }
  1129. rc = jme_setup_tx_resources(jme);
  1130. if (rc) {
  1131. pr_err("Allocating resources for TX error, Device STOPPED!\n");
  1132. goto err_out_free_rx_resources;
  1133. }
  1134. jme_enable_rx_engine(jme);
  1135. jme_enable_tx_engine(jme);
  1136. netif_start_queue(netdev);
  1137. if (test_bit(JME_FLAG_POLL, &jme->flags))
  1138. jme_interrupt_mode(jme);
  1139. jme_start_pcc_timer(jme);
  1140. } else if (jme_pseudo_hotplug_enabled(jme)) {
  1141. jme_start_shutdown_timer(jme);
  1142. }
  1143. goto out_enable_tasklet;
  1144. err_out_free_rx_resources:
  1145. jme_free_rx_resources(jme);
  1146. out_enable_tasklet:
  1147. tasklet_enable(&jme->txclean_task);
  1148. tasklet_enable(&jme->rxclean_task);
  1149. tasklet_enable(&jme->rxempty_task);
  1150. out:
  1151. atomic_inc(&jme->link_changing);
  1152. }
  1153. static void
  1154. jme_rx_clean_tasklet(unsigned long arg)
  1155. {
  1156. struct jme_adapter *jme = (struct jme_adapter *)arg;
  1157. struct dynpcc_info *dpi = &(jme->dpi);
  1158. jme_process_receive(jme, jme->rx_ring_size);
  1159. ++(dpi->intr_cnt);
  1160. }
  1161. static int
  1162. jme_poll(JME_NAPI_HOLDER(holder), JME_NAPI_WEIGHT(budget))
  1163. {
  1164. struct jme_adapter *jme = jme_napi_priv(holder);
  1165. int rest;
  1166. rest = jme_process_receive(jme, JME_NAPI_WEIGHT_VAL(budget));
  1167. while (atomic_read(&jme->rx_empty) > 0) {
  1168. atomic_dec(&jme->rx_empty);
  1169. ++(NET_STAT(jme).rx_dropped);
  1170. jme_restart_rx_engine(jme);
  1171. }
  1172. atomic_inc(&jme->rx_empty);
  1173. if (rest) {
  1174. JME_RX_COMPLETE(netdev, holder);
  1175. jme_interrupt_mode(jme);
  1176. }
  1177. JME_NAPI_WEIGHT_SET(budget, rest);
  1178. return JME_NAPI_WEIGHT_VAL(budget) - rest;
  1179. }
  1180. static void
  1181. jme_rx_empty_tasklet(unsigned long arg)
  1182. {
  1183. struct jme_adapter *jme = (struct jme_adapter *)arg;
  1184. if (unlikely(atomic_read(&jme->link_changing) != 1))
  1185. return;
  1186. if (unlikely(!netif_carrier_ok(jme->dev)))
  1187. return;
  1188. netif_info(jme, rx_status, jme->dev, "RX Queue Full!\n");
  1189. jme_rx_clean_tasklet(arg);
  1190. while (atomic_read(&jme->rx_empty) > 0) {
  1191. atomic_dec(&jme->rx_empty);
  1192. ++(NET_STAT(jme).rx_dropped);
  1193. jme_restart_rx_engine(jme);
  1194. }
  1195. atomic_inc(&jme->rx_empty);
  1196. }
  1197. static void
  1198. jme_wake_queue_if_stopped(struct jme_adapter *jme)
  1199. {
  1200. struct jme_ring *txring = &(jme->txring[0]);
  1201. smp_wmb();
  1202. if (unlikely(netif_queue_stopped(jme->dev) &&
  1203. atomic_read(&txring->nr_free) >= (jme->tx_wake_threshold))) {
  1204. netif_info(jme, tx_done, jme->dev, "TX Queue Waked\n");
  1205. netif_wake_queue(jme->dev);
  1206. }
  1207. }
  1208. static void
  1209. jme_tx_clean_tasklet(unsigned long arg)
  1210. {
  1211. struct jme_adapter *jme = (struct jme_adapter *)arg;
  1212. struct jme_ring *txring = &(jme->txring[0]);
  1213. struct txdesc *txdesc = txring->desc;
  1214. struct jme_buffer_info *txbi = txring->bufinf, *ctxbi, *ttxbi;
  1215. int i, j, cnt = 0, max, err, mask;
  1216. tx_dbg(jme, "Into txclean\n");
  1217. if (unlikely(!atomic_dec_and_test(&jme->tx_cleaning)))
  1218. goto out;
  1219. if (unlikely(atomic_read(&jme->link_changing) != 1))
  1220. goto out;
  1221. if (unlikely(!netif_carrier_ok(jme->dev)))
  1222. goto out;
  1223. max = jme->tx_ring_size - atomic_read(&txring->nr_free);
  1224. mask = jme->tx_ring_mask;
  1225. for (i = atomic_read(&txring->next_to_clean) ; cnt < max ; ) {
  1226. ctxbi = txbi + i;
  1227. if (likely(ctxbi->skb &&
  1228. !(txdesc[i].descwb.flags & TXWBFLAG_OWN))) {
  1229. tx_dbg(jme, "txclean: %d+%d@%lu\n",
  1230. i, ctxbi->nr_desc, jiffies);
  1231. err = txdesc[i].descwb.flags & TXWBFLAG_ALLERR;
  1232. for (j = 1 ; j < ctxbi->nr_desc ; ++j) {
  1233. ttxbi = txbi + ((i + j) & (mask));
  1234. txdesc[(i + j) & (mask)].dw[0] = 0;
  1235. pci_unmap_page(jme->pdev,
  1236. ttxbi->mapping,
  1237. ttxbi->len,
  1238. PCI_DMA_TODEVICE);
  1239. ttxbi->mapping = 0;
  1240. ttxbi->len = 0;
  1241. }
  1242. dev_kfree_skb(ctxbi->skb);
  1243. cnt += ctxbi->nr_desc;
  1244. if (unlikely(err)) {
  1245. ++(NET_STAT(jme).tx_carrier_errors);
  1246. } else {
  1247. ++(NET_STAT(jme).tx_packets);
  1248. NET_STAT(jme).tx_bytes += ctxbi->len;
  1249. }
  1250. ctxbi->skb = NULL;
  1251. ctxbi->len = 0;
  1252. ctxbi->start_xmit = 0;
  1253. } else {
  1254. break;
  1255. }
  1256. i = (i + ctxbi->nr_desc) & mask;
  1257. ctxbi->nr_desc = 0;
  1258. }
  1259. tx_dbg(jme, "txclean: done %d@%lu\n", i, jiffies);
  1260. atomic_set(&txring->next_to_clean, i);
  1261. atomic_add(cnt, &txring->nr_free);
  1262. jme_wake_queue_if_stopped(jme);
  1263. out:
  1264. atomic_inc(&jme->tx_cleaning);
  1265. }
  1266. static void
  1267. jme_intr_msi(struct jme_adapter *jme, u32 intrstat)
  1268. {
  1269. /*
  1270. * Disable interrupt
  1271. */
  1272. jwrite32f(jme, JME_IENC, INTR_ENABLE);
  1273. if (intrstat & (INTR_LINKCH | INTR_SWINTR)) {
  1274. /*
  1275. * Link change event is critical
  1276. * all other events are ignored
  1277. */
  1278. jwrite32(jme, JME_IEVE, intrstat);
  1279. tasklet_schedule(&jme->linkch_task);
  1280. goto out_reenable;
  1281. }
  1282. if (intrstat & INTR_TMINTR) {
  1283. jwrite32(jme, JME_IEVE, INTR_TMINTR);
  1284. tasklet_schedule(&jme->pcc_task);
  1285. }
  1286. if (intrstat & (INTR_PCCTXTO | INTR_PCCTX)) {
  1287. jwrite32(jme, JME_IEVE, INTR_PCCTXTO | INTR_PCCTX | INTR_TX0);
  1288. tasklet_schedule(&jme->txclean_task);
  1289. }
  1290. if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
  1291. jwrite32(jme, JME_IEVE, (intrstat & (INTR_PCCRX0TO |
  1292. INTR_PCCRX0 |
  1293. INTR_RX0EMP)) |
  1294. INTR_RX0);
  1295. }
  1296. if (test_bit(JME_FLAG_POLL, &jme->flags)) {
  1297. if (intrstat & INTR_RX0EMP)
  1298. atomic_inc(&jme->rx_empty);
  1299. if ((intrstat & (INTR_PCCRX0TO | INTR_PCCRX0 | INTR_RX0EMP))) {
  1300. if (likely(JME_RX_SCHEDULE_PREP(jme))) {
  1301. jme_polling_mode(jme);
  1302. JME_RX_SCHEDULE(jme);
  1303. }
  1304. }
  1305. } else {
  1306. if (intrstat & INTR_RX0EMP) {
  1307. atomic_inc(&jme->rx_empty);
  1308. tasklet_hi_schedule(&jme->rxempty_task);
  1309. } else if (intrstat & (INTR_PCCRX0TO | INTR_PCCRX0)) {
  1310. tasklet_hi_schedule(&jme->rxclean_task);
  1311. }
  1312. }
  1313. out_reenable:
  1314. /*
  1315. * Re-enable interrupt
  1316. */
  1317. jwrite32f(jme, JME_IENS, INTR_ENABLE);
  1318. }
  1319. static irqreturn_t
  1320. jme_intr(int irq, void *dev_id)
  1321. {
  1322. struct net_device *netdev = dev_id;
  1323. struct jme_adapter *jme = netdev_priv(netdev);
  1324. u32 intrstat;
  1325. intrstat = jread32(jme, JME_IEVE);
  1326. /*
  1327. * Check if it's really an interrupt for us
  1328. */
  1329. if (unlikely((intrstat & INTR_ENABLE) == 0))
  1330. return IRQ_NONE;
  1331. /*
  1332. * Check if the device still exist
  1333. */
  1334. if (unlikely(intrstat == ~((typeof(intrstat))0)))
  1335. return IRQ_NONE;
  1336. jme_intr_msi(jme, intrstat);
  1337. return IRQ_HANDLED;
  1338. }
  1339. static irqreturn_t
  1340. jme_msi(int irq, void *dev_id)
  1341. {
  1342. struct net_device *netdev = dev_id;
  1343. struct jme_adapter *jme = netdev_priv(netdev);
  1344. u32 intrstat;
  1345. intrstat = jread32(jme, JME_IEVE);
  1346. jme_intr_msi(jme, intrstat);
  1347. return IRQ_HANDLED;
  1348. }
  1349. static void
  1350. jme_reset_link(struct jme_adapter *jme)
  1351. {
  1352. jwrite32(jme, JME_TMCSR, TMCSR_SWIT);
  1353. }
  1354. static void
  1355. jme_restart_an(struct jme_adapter *jme)
  1356. {
  1357. u32 bmcr;
  1358. spin_lock_bh(&jme->phy_lock);
  1359. bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
  1360. bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
  1361. jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
  1362. spin_unlock_bh(&jme->phy_lock);
  1363. }
  1364. static int
  1365. jme_request_irq(struct jme_adapter *jme)
  1366. {
  1367. int rc;
  1368. struct net_device *netdev = jme->dev;
  1369. irq_handler_t handler = jme_intr;
  1370. int irq_flags = IRQF_SHARED;
  1371. if (!pci_enable_msi(jme->pdev)) {
  1372. set_bit(JME_FLAG_MSI, &jme->flags);
  1373. handler = jme_msi;
  1374. irq_flags = 0;
  1375. }
  1376. rc = request_irq(jme->pdev->irq, handler, irq_flags, netdev->name,
  1377. netdev);
  1378. if (rc) {
  1379. netdev_err(netdev,
  1380. "Unable to request %s interrupt (return: %d)\n",
  1381. test_bit(JME_FLAG_MSI, &jme->flags) ? "MSI" : "INTx",
  1382. rc);
  1383. if (test_bit(JME_FLAG_MSI, &jme->flags)) {
  1384. pci_disable_msi(jme->pdev);
  1385. clear_bit(JME_FLAG_MSI, &jme->flags);
  1386. }
  1387. } else {
  1388. netdev->irq = jme->pdev->irq;
  1389. }
  1390. return rc;
  1391. }
  1392. static void
  1393. jme_free_irq(struct jme_adapter *jme)
  1394. {
  1395. free_irq(jme->pdev->irq, jme->dev);
  1396. if (test_bit(JME_FLAG_MSI, &jme->flags)) {
  1397. pci_disable_msi(jme->pdev);
  1398. clear_bit(JME_FLAG_MSI, &jme->flags);
  1399. jme->dev->irq = jme->pdev->irq;
  1400. }
  1401. }
  1402. static inline void
  1403. jme_new_phy_on(struct jme_adapter *jme)
  1404. {
  1405. u32 reg;
  1406. reg = jread32(jme, JME_PHY_PWR);
  1407. reg &= ~(PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
  1408. PHY_PWR_DWN2 | PHY_PWR_CLKSEL);
  1409. jwrite32(jme, JME_PHY_PWR, reg);
  1410. pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
  1411. reg &= ~PE1_GPREG0_PBG;
  1412. reg |= PE1_GPREG0_ENBG;
  1413. pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
  1414. }
  1415. static inline void
  1416. jme_new_phy_off(struct jme_adapter *jme)
  1417. {
  1418. u32 reg;
  1419. reg = jread32(jme, JME_PHY_PWR);
  1420. reg |= PHY_PWR_DWN1SEL | PHY_PWR_DWN1SW |
  1421. PHY_PWR_DWN2 | PHY_PWR_CLKSEL;
  1422. jwrite32(jme, JME_PHY_PWR, reg);
  1423. pci_read_config_dword(jme->pdev, PCI_PRIV_PE1, &reg);
  1424. reg &= ~PE1_GPREG0_PBG;
  1425. reg |= PE1_GPREG0_PDD3COLD;
  1426. pci_write_config_dword(jme->pdev, PCI_PRIV_PE1, reg);
  1427. }
  1428. static inline void
  1429. jme_phy_on(struct jme_adapter *jme)
  1430. {
  1431. u32 bmcr;
  1432. bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
  1433. bmcr &= ~BMCR_PDOWN;
  1434. jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
  1435. if (new_phy_power_ctrl(jme->chip_main_rev))
  1436. jme_new_phy_on(jme);
  1437. }
  1438. static inline void
  1439. jme_phy_off(struct jme_adapter *jme)
  1440. {
  1441. u32 bmcr;
  1442. bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
  1443. bmcr |= BMCR_PDOWN;
  1444. jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, bmcr);
  1445. if (new_phy_power_ctrl(jme->chip_main_rev))
  1446. jme_new_phy_off(jme);
  1447. }
  1448. static int
  1449. jme_phy_specreg_read(struct jme_adapter *jme, u32 specreg)
  1450. {
  1451. u32 phy_addr;
  1452. phy_addr = JM_PHY_SPEC_REG_READ | specreg;
  1453. jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
  1454. phy_addr);
  1455. return jme_mdio_read(jme->dev, jme->mii_if.phy_id,
  1456. JM_PHY_SPEC_DATA_REG);
  1457. }
  1458. static void
  1459. jme_phy_specreg_write(struct jme_adapter *jme, u32 ext_reg, u32 phy_data)
  1460. {
  1461. u32 phy_addr;
  1462. phy_addr = JM_PHY_SPEC_REG_WRITE | ext_reg;
  1463. jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_DATA_REG,
  1464. phy_data);
  1465. jme_mdio_write(jme->dev, jme->mii_if.phy_id, JM_PHY_SPEC_ADDR_REG,
  1466. phy_addr);
  1467. }
  1468. static int
  1469. jme_phy_calibration(struct jme_adapter *jme)
  1470. {
  1471. u32 ctrl1000, phy_data;
  1472. jme_phy_off(jme);
  1473. jme_phy_on(jme);
  1474. /* Enabel PHY test mode 1 */
  1475. ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
  1476. ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
  1477. ctrl1000 |= PHY_GAD_TEST_MODE_1;
  1478. jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
  1479. phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
  1480. phy_data &= ~JM_PHY_EXT_COMM_2_CALI_MODE_0;
  1481. phy_data |= JM_PHY_EXT_COMM_2_CALI_LATCH |
  1482. JM_PHY_EXT_COMM_2_CALI_ENABLE;
  1483. jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
  1484. msleep(20);
  1485. phy_data = jme_phy_specreg_read(jme, JM_PHY_EXT_COMM_2_REG);
  1486. phy_data &= ~(JM_PHY_EXT_COMM_2_CALI_ENABLE |
  1487. JM_PHY_EXT_COMM_2_CALI_MODE_0 |
  1488. JM_PHY_EXT_COMM_2_CALI_LATCH);
  1489. jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_2_REG, phy_data);
  1490. /* Disable PHY test mode */
  1491. ctrl1000 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_CTRL1000);
  1492. ctrl1000 &= ~PHY_GAD_TEST_MODE_MSK;
  1493. jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_CTRL1000, ctrl1000);
  1494. return 0;
  1495. }
  1496. static int
  1497. jme_phy_setEA(struct jme_adapter *jme)
  1498. {
  1499. u32 phy_comm0 = 0, phy_comm1 = 0;
  1500. u8 nic_ctrl;
  1501. pci_read_config_byte(jme->pdev, PCI_PRIV_SHARE_NICCTRL, &nic_ctrl);
  1502. if ((nic_ctrl & 0x3) == JME_FLAG_PHYEA_ENABLE)
  1503. return 0;
  1504. switch (jme->pdev->device) {
  1505. case PCI_DEVICE_ID_JMICRON_JMC250:
  1506. if (((jme->chip_main_rev == 5) &&
  1507. ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
  1508. (jme->chip_sub_rev == 3))) ||
  1509. (jme->chip_main_rev >= 6)) {
  1510. phy_comm0 = 0x008A;
  1511. phy_comm1 = 0x4109;
  1512. }
  1513. if ((jme->chip_main_rev == 3) &&
  1514. ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
  1515. phy_comm0 = 0xE088;
  1516. break;
  1517. case PCI_DEVICE_ID_JMICRON_JMC260:
  1518. if (((jme->chip_main_rev == 5) &&
  1519. ((jme->chip_sub_rev == 0) || (jme->chip_sub_rev == 1) ||
  1520. (jme->chip_sub_rev == 3))) ||
  1521. (jme->chip_main_rev >= 6)) {
  1522. phy_comm0 = 0x008A;
  1523. phy_comm1 = 0x4109;
  1524. }
  1525. if ((jme->chip_main_rev == 3) &&
  1526. ((jme->chip_sub_rev == 1) || (jme->chip_sub_rev == 2)))
  1527. phy_comm0 = 0xE088;
  1528. if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 0))
  1529. phy_comm0 = 0x608A;
  1530. if ((jme->chip_main_rev == 2) && (jme->chip_sub_rev == 2))
  1531. phy_comm0 = 0x408A;
  1532. break;
  1533. default:
  1534. return -ENODEV;
  1535. }
  1536. if (phy_comm0)
  1537. jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_0_REG, phy_comm0);
  1538. if (phy_comm1)
  1539. jme_phy_specreg_write(jme, JM_PHY_EXT_COMM_1_REG, phy_comm1);
  1540. return 0;
  1541. }
  1542. static int
  1543. jme_open(struct net_device *netdev)
  1544. {
  1545. struct jme_adapter *jme = netdev_priv(netdev);
  1546. int rc;
  1547. jme_clear_pm_disable_wol(jme);
  1548. JME_NAPI_ENABLE(jme);
  1549. tasklet_init(&jme->linkch_task, jme_link_change_tasklet,
  1550. (unsigned long) jme);
  1551. tasklet_init(&jme->txclean_task, jme_tx_clean_tasklet,
  1552. (unsigned long) jme);
  1553. tasklet_init(&jme->rxclean_task, jme_rx_clean_tasklet,
  1554. (unsigned long) jme);
  1555. tasklet_init(&jme->rxempty_task, jme_rx_empty_tasklet,
  1556. (unsigned long) jme);
  1557. rc = jme_request_irq(jme);
  1558. if (rc)
  1559. goto err_out;
  1560. jme_start_irq(jme);
  1561. jme_phy_on(jme);
  1562. if (test_bit(JME_FLAG_SSET, &jme->flags))
  1563. jme_set_link_ksettings(netdev, &jme->old_cmd);
  1564. else
  1565. jme_reset_phy_processor(jme);
  1566. jme_phy_calibration(jme);
  1567. jme_phy_setEA(jme);
  1568. jme_reset_link(jme);
  1569. return 0;
  1570. err_out:
  1571. netif_stop_queue(netdev);
  1572. netif_carrier_off(netdev);
  1573. return rc;
  1574. }
  1575. static void
  1576. jme_set_100m_half(struct jme_adapter *jme)
  1577. {
  1578. u32 bmcr, tmp;
  1579. jme_phy_on(jme);
  1580. bmcr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMCR);
  1581. tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
  1582. BMCR_SPEED1000 | BMCR_FULLDPLX);
  1583. tmp |= BMCR_SPEED100;
  1584. if (bmcr != tmp)
  1585. jme_mdio_write(jme->dev, jme->mii_if.phy_id, MII_BMCR, tmp);
  1586. if (jme->fpgaver)
  1587. jwrite32(jme, JME_GHC, GHC_SPEED_100M | GHC_LINK_POLL);
  1588. else
  1589. jwrite32(jme, JME_GHC, GHC_SPEED_100M);
  1590. }
  1591. #define JME_WAIT_LINK_TIME 2000 /* 2000ms */
  1592. static void
  1593. jme_wait_link(struct jme_adapter *jme)
  1594. {
  1595. u32 phylink, to = JME_WAIT_LINK_TIME;
  1596. msleep(1000);
  1597. phylink = jme_linkstat_from_phy(jme);
  1598. while (!(phylink & PHY_LINK_UP) && (to -= 10) > 0) {
  1599. usleep_range(10000, 11000);
  1600. phylink = jme_linkstat_from_phy(jme);
  1601. }
  1602. }
  1603. static void
  1604. jme_powersave_phy(struct jme_adapter *jme)
  1605. {
  1606. if (jme->reg_pmcs && device_may_wakeup(&jme->pdev->dev)) {
  1607. jme_set_100m_half(jme);
  1608. if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
  1609. jme_wait_link(jme);
  1610. jme_clear_pm_enable_wol(jme);
  1611. } else {
  1612. jme_phy_off(jme);
  1613. }
  1614. }
  1615. static int
  1616. jme_close(struct net_device *netdev)
  1617. {
  1618. struct jme_adapter *jme = netdev_priv(netdev);
  1619. netif_stop_queue(netdev);
  1620. netif_carrier_off(netdev);
  1621. jme_stop_irq(jme);
  1622. jme_free_irq(jme);
  1623. JME_NAPI_DISABLE(jme);
  1624. tasklet_kill(&jme->linkch_task);
  1625. tasklet_kill(&jme->txclean_task);
  1626. tasklet_kill(&jme->rxclean_task);
  1627. tasklet_kill(&jme->rxempty_task);
  1628. jme_disable_rx_engine(jme);
  1629. jme_disable_tx_engine(jme);
  1630. jme_reset_mac_processor(jme);
  1631. jme_free_rx_resources(jme);
  1632. jme_free_tx_resources(jme);
  1633. jme->phylink = 0;
  1634. jme_phy_off(jme);
  1635. return 0;
  1636. }
  1637. static int
  1638. jme_alloc_txdesc(struct jme_adapter *jme,
  1639. struct sk_buff *skb)
  1640. {
  1641. struct jme_ring *txring = &(jme->txring[0]);
  1642. int idx, nr_alloc, mask = jme->tx_ring_mask;
  1643. idx = txring->next_to_use;
  1644. nr_alloc = skb_shinfo(skb)->nr_frags + 2;
  1645. if (unlikely(atomic_read(&txring->nr_free) < nr_alloc))
  1646. return -1;
  1647. atomic_sub(nr_alloc, &txring->nr_free);
  1648. txring->next_to_use = (txring->next_to_use + nr_alloc) & mask;
  1649. return idx;
  1650. }
  1651. static int
  1652. jme_fill_tx_map(struct pci_dev *pdev,
  1653. struct txdesc *txdesc,
  1654. struct jme_buffer_info *txbi,
  1655. struct page *page,
  1656. u32 page_offset,
  1657. u32 len,
  1658. bool hidma)
  1659. {
  1660. dma_addr_t dmaaddr;
  1661. dmaaddr = pci_map_page(pdev,
  1662. page,
  1663. page_offset,
  1664. len,
  1665. PCI_DMA_TODEVICE);
  1666. if (unlikely(pci_dma_mapping_error(pdev, dmaaddr)))
  1667. return -EINVAL;
  1668. pci_dma_sync_single_for_device(pdev,
  1669. dmaaddr,
  1670. len,
  1671. PCI_DMA_TODEVICE);
  1672. txdesc->dw[0] = 0;
  1673. txdesc->dw[1] = 0;
  1674. txdesc->desc2.flags = TXFLAG_OWN;
  1675. txdesc->desc2.flags |= (hidma) ? TXFLAG_64BIT : 0;
  1676. txdesc->desc2.datalen = cpu_to_le16(len);
  1677. txdesc->desc2.bufaddrh = cpu_to_le32((__u64)dmaaddr >> 32);
  1678. txdesc->desc2.bufaddrl = cpu_to_le32(
  1679. (__u64)dmaaddr & 0xFFFFFFFFUL);
  1680. txbi->mapping = dmaaddr;
  1681. txbi->len = len;
  1682. return 0;
  1683. }
  1684. static void jme_drop_tx_map(struct jme_adapter *jme, int startidx, int count)
  1685. {
  1686. struct jme_ring *txring = &(jme->txring[0]);
  1687. struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
  1688. int mask = jme->tx_ring_mask;
  1689. int j;
  1690. for (j = 0 ; j < count ; j++) {
  1691. ctxbi = txbi + ((startidx + j + 2) & (mask));
  1692. pci_unmap_page(jme->pdev,
  1693. ctxbi->mapping,
  1694. ctxbi->len,
  1695. PCI_DMA_TODEVICE);
  1696. ctxbi->mapping = 0;
  1697. ctxbi->len = 0;
  1698. }
  1699. }
  1700. static int
  1701. jme_map_tx_skb(struct jme_adapter *jme, struct sk_buff *skb, int idx)
  1702. {
  1703. struct jme_ring *txring = &(jme->txring[0]);
  1704. struct txdesc *txdesc = txring->desc, *ctxdesc;
  1705. struct jme_buffer_info *txbi = txring->bufinf, *ctxbi;
  1706. bool hidma = jme->dev->features & NETIF_F_HIGHDMA;
  1707. int i, nr_frags = skb_shinfo(skb)->nr_frags;
  1708. int mask = jme->tx_ring_mask;
  1709. const struct skb_frag_struct *frag;
  1710. u32 len;
  1711. int ret = 0;
  1712. for (i = 0 ; i < nr_frags ; ++i) {
  1713. frag = &skb_shinfo(skb)->frags[i];
  1714. ctxdesc = txdesc + ((idx + i + 2) & (mask));
  1715. ctxbi = txbi + ((idx + i + 2) & (mask));
  1716. ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi,
  1717. skb_frag_page(frag),
  1718. frag->page_offset, skb_frag_size(frag), hidma);
  1719. if (ret) {
  1720. jme_drop_tx_map(jme, idx, i);
  1721. goto out;
  1722. }
  1723. }
  1724. len = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
  1725. ctxdesc = txdesc + ((idx + 1) & (mask));
  1726. ctxbi = txbi + ((idx + 1) & (mask));
  1727. ret = jme_fill_tx_map(jme->pdev, ctxdesc, ctxbi, virt_to_page(skb->data),
  1728. offset_in_page(skb->data), len, hidma);
  1729. if (ret)
  1730. jme_drop_tx_map(jme, idx, i);
  1731. out:
  1732. return ret;
  1733. }
  1734. static int
  1735. jme_tx_tso(struct sk_buff *skb, __le16 *mss, u8 *flags)
  1736. {
  1737. *mss = cpu_to_le16(skb_shinfo(skb)->gso_size << TXDESC_MSS_SHIFT);
  1738. if (*mss) {
  1739. *flags |= TXFLAG_LSEN;
  1740. if (skb->protocol == htons(ETH_P_IP)) {
  1741. struct iphdr *iph = ip_hdr(skb);
  1742. iph->check = 0;
  1743. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  1744. iph->daddr, 0,
  1745. IPPROTO_TCP,
  1746. 0);
  1747. } else {
  1748. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  1749. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ip6h->saddr,
  1750. &ip6h->daddr, 0,
  1751. IPPROTO_TCP,
  1752. 0);
  1753. }
  1754. return 0;
  1755. }
  1756. return 1;
  1757. }
  1758. static void
  1759. jme_tx_csum(struct jme_adapter *jme, struct sk_buff *skb, u8 *flags)
  1760. {
  1761. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1762. u8 ip_proto;
  1763. switch (skb->protocol) {
  1764. case htons(ETH_P_IP):
  1765. ip_proto = ip_hdr(skb)->protocol;
  1766. break;
  1767. case htons(ETH_P_IPV6):
  1768. ip_proto = ipv6_hdr(skb)->nexthdr;
  1769. break;
  1770. default:
  1771. ip_proto = 0;
  1772. break;
  1773. }
  1774. switch (ip_proto) {
  1775. case IPPROTO_TCP:
  1776. *flags |= TXFLAG_TCPCS;
  1777. break;
  1778. case IPPROTO_UDP:
  1779. *flags |= TXFLAG_UDPCS;
  1780. break;
  1781. default:
  1782. netif_err(jme, tx_err, jme->dev, "Error upper layer protocol\n");
  1783. break;
  1784. }
  1785. }
  1786. }
  1787. static inline void
  1788. jme_tx_vlan(struct sk_buff *skb, __le16 *vlan, u8 *flags)
  1789. {
  1790. if (skb_vlan_tag_present(skb)) {
  1791. *flags |= TXFLAG_TAGON;
  1792. *vlan = cpu_to_le16(skb_vlan_tag_get(skb));
  1793. }
  1794. }
  1795. static int
  1796. jme_fill_tx_desc(struct jme_adapter *jme, struct sk_buff *skb, int idx)
  1797. {
  1798. struct jme_ring *txring = &(jme->txring[0]);
  1799. struct txdesc *txdesc;
  1800. struct jme_buffer_info *txbi;
  1801. u8 flags;
  1802. int ret = 0;
  1803. txdesc = (struct txdesc *)txring->desc + idx;
  1804. txbi = txring->bufinf + idx;
  1805. txdesc->dw[0] = 0;
  1806. txdesc->dw[1] = 0;
  1807. txdesc->dw[2] = 0;
  1808. txdesc->dw[3] = 0;
  1809. txdesc->desc1.pktsize = cpu_to_le16(skb->len);
  1810. /*
  1811. * Set OWN bit at final.
  1812. * When kernel transmit faster than NIC.
  1813. * And NIC trying to send this descriptor before we tell
  1814. * it to start sending this TX queue.
  1815. * Other fields are already filled correctly.
  1816. */
  1817. wmb();
  1818. flags = TXFLAG_OWN | TXFLAG_INT;
  1819. /*
  1820. * Set checksum flags while not tso
  1821. */
  1822. if (jme_tx_tso(skb, &txdesc->desc1.mss, &flags))
  1823. jme_tx_csum(jme, skb, &flags);
  1824. jme_tx_vlan(skb, &txdesc->desc1.vlan, &flags);
  1825. ret = jme_map_tx_skb(jme, skb, idx);
  1826. if (ret)
  1827. return ret;
  1828. txdesc->desc1.flags = flags;
  1829. /*
  1830. * Set tx buffer info after telling NIC to send
  1831. * For better tx_clean timing
  1832. */
  1833. wmb();
  1834. txbi->nr_desc = skb_shinfo(skb)->nr_frags + 2;
  1835. txbi->skb = skb;
  1836. txbi->len = skb->len;
  1837. txbi->start_xmit = jiffies;
  1838. if (!txbi->start_xmit)
  1839. txbi->start_xmit = (0UL-1);
  1840. return 0;
  1841. }
  1842. static void
  1843. jme_stop_queue_if_full(struct jme_adapter *jme)
  1844. {
  1845. struct jme_ring *txring = &(jme->txring[0]);
  1846. struct jme_buffer_info *txbi = txring->bufinf;
  1847. int idx = atomic_read(&txring->next_to_clean);
  1848. txbi += idx;
  1849. smp_wmb();
  1850. if (unlikely(atomic_read(&txring->nr_free) < (MAX_SKB_FRAGS+2))) {
  1851. netif_stop_queue(jme->dev);
  1852. netif_info(jme, tx_queued, jme->dev, "TX Queue Paused\n");
  1853. smp_wmb();
  1854. if (atomic_read(&txring->nr_free)
  1855. >= (jme->tx_wake_threshold)) {
  1856. netif_wake_queue(jme->dev);
  1857. netif_info(jme, tx_queued, jme->dev, "TX Queue Fast Waked\n");
  1858. }
  1859. }
  1860. if (unlikely(txbi->start_xmit &&
  1861. (jiffies - txbi->start_xmit) >= TX_TIMEOUT &&
  1862. txbi->skb)) {
  1863. netif_stop_queue(jme->dev);
  1864. netif_info(jme, tx_queued, jme->dev,
  1865. "TX Queue Stopped %d@%lu\n", idx, jiffies);
  1866. }
  1867. }
  1868. /*
  1869. * This function is already protected by netif_tx_lock()
  1870. */
  1871. static netdev_tx_t
  1872. jme_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  1873. {
  1874. struct jme_adapter *jme = netdev_priv(netdev);
  1875. int idx;
  1876. if (unlikely(skb_is_gso(skb) && skb_cow_head(skb, 0))) {
  1877. dev_kfree_skb_any(skb);
  1878. ++(NET_STAT(jme).tx_dropped);
  1879. return NETDEV_TX_OK;
  1880. }
  1881. idx = jme_alloc_txdesc(jme, skb);
  1882. if (unlikely(idx < 0)) {
  1883. netif_stop_queue(netdev);
  1884. netif_err(jme, tx_err, jme->dev,
  1885. "BUG! Tx ring full when queue awake!\n");
  1886. return NETDEV_TX_BUSY;
  1887. }
  1888. if (jme_fill_tx_desc(jme, skb, idx))
  1889. return NETDEV_TX_OK;
  1890. jwrite32(jme, JME_TXCS, jme->reg_txcs |
  1891. TXCS_SELECT_QUEUE0 |
  1892. TXCS_QUEUE0S |
  1893. TXCS_ENABLE);
  1894. tx_dbg(jme, "xmit: %d+%d@%lu\n",
  1895. idx, skb_shinfo(skb)->nr_frags + 2, jiffies);
  1896. jme_stop_queue_if_full(jme);
  1897. return NETDEV_TX_OK;
  1898. }
  1899. static void
  1900. jme_set_unicastaddr(struct net_device *netdev)
  1901. {
  1902. struct jme_adapter *jme = netdev_priv(netdev);
  1903. u32 val;
  1904. val = (netdev->dev_addr[3] & 0xff) << 24 |
  1905. (netdev->dev_addr[2] & 0xff) << 16 |
  1906. (netdev->dev_addr[1] & 0xff) << 8 |
  1907. (netdev->dev_addr[0] & 0xff);
  1908. jwrite32(jme, JME_RXUMA_LO, val);
  1909. val = (netdev->dev_addr[5] & 0xff) << 8 |
  1910. (netdev->dev_addr[4] & 0xff);
  1911. jwrite32(jme, JME_RXUMA_HI, val);
  1912. }
  1913. static int
  1914. jme_set_macaddr(struct net_device *netdev, void *p)
  1915. {
  1916. struct jme_adapter *jme = netdev_priv(netdev);
  1917. struct sockaddr *addr = p;
  1918. if (netif_running(netdev))
  1919. return -EBUSY;
  1920. spin_lock_bh(&jme->macaddr_lock);
  1921. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1922. jme_set_unicastaddr(netdev);
  1923. spin_unlock_bh(&jme->macaddr_lock);
  1924. return 0;
  1925. }
  1926. static void
  1927. jme_set_multi(struct net_device *netdev)
  1928. {
  1929. struct jme_adapter *jme = netdev_priv(netdev);
  1930. u32 mc_hash[2] = {};
  1931. spin_lock_bh(&jme->rxmcs_lock);
  1932. jme->reg_rxmcs |= RXMCS_BRDFRAME | RXMCS_UNIFRAME;
  1933. if (netdev->flags & IFF_PROMISC) {
  1934. jme->reg_rxmcs |= RXMCS_ALLFRAME;
  1935. } else if (netdev->flags & IFF_ALLMULTI) {
  1936. jme->reg_rxmcs |= RXMCS_ALLMULFRAME;
  1937. } else if (netdev->flags & IFF_MULTICAST) {
  1938. struct netdev_hw_addr *ha;
  1939. int bit_nr;
  1940. jme->reg_rxmcs |= RXMCS_MULFRAME | RXMCS_MULFILTERED;
  1941. netdev_for_each_mc_addr(ha, netdev) {
  1942. bit_nr = ether_crc(ETH_ALEN, ha->addr) & 0x3F;
  1943. mc_hash[bit_nr >> 5] |= 1 << (bit_nr & 0x1F);
  1944. }
  1945. jwrite32(jme, JME_RXMCHT_LO, mc_hash[0]);
  1946. jwrite32(jme, JME_RXMCHT_HI, mc_hash[1]);
  1947. }
  1948. wmb();
  1949. jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
  1950. spin_unlock_bh(&jme->rxmcs_lock);
  1951. }
  1952. static int
  1953. jme_change_mtu(struct net_device *netdev, int new_mtu)
  1954. {
  1955. struct jme_adapter *jme = netdev_priv(netdev);
  1956. netdev->mtu = new_mtu;
  1957. netdev_update_features(netdev);
  1958. jme_restart_rx_engine(jme);
  1959. jme_reset_link(jme);
  1960. return 0;
  1961. }
  1962. static void
  1963. jme_tx_timeout(struct net_device *netdev)
  1964. {
  1965. struct jme_adapter *jme = netdev_priv(netdev);
  1966. jme->phylink = 0;
  1967. jme_reset_phy_processor(jme);
  1968. if (test_bit(JME_FLAG_SSET, &jme->flags))
  1969. jme_set_link_ksettings(netdev, &jme->old_cmd);
  1970. /*
  1971. * Force to Reset the link again
  1972. */
  1973. jme_reset_link(jme);
  1974. }
  1975. static void
  1976. jme_get_drvinfo(struct net_device *netdev,
  1977. struct ethtool_drvinfo *info)
  1978. {
  1979. struct jme_adapter *jme = netdev_priv(netdev);
  1980. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1981. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1982. strlcpy(info->bus_info, pci_name(jme->pdev), sizeof(info->bus_info));
  1983. }
  1984. static int
  1985. jme_get_regs_len(struct net_device *netdev)
  1986. {
  1987. return JME_REG_LEN;
  1988. }
  1989. static void
  1990. mmapio_memcpy(struct jme_adapter *jme, u32 *p, u32 reg, int len)
  1991. {
  1992. int i;
  1993. for (i = 0 ; i < len ; i += 4)
  1994. p[i >> 2] = jread32(jme, reg + i);
  1995. }
  1996. static void
  1997. mdio_memcpy(struct jme_adapter *jme, u32 *p, int reg_nr)
  1998. {
  1999. int i;
  2000. u16 *p16 = (u16 *)p;
  2001. for (i = 0 ; i < reg_nr ; ++i)
  2002. p16[i] = jme_mdio_read(jme->dev, jme->mii_if.phy_id, i);
  2003. }
  2004. static void
  2005. jme_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
  2006. {
  2007. struct jme_adapter *jme = netdev_priv(netdev);
  2008. u32 *p32 = (u32 *)p;
  2009. memset(p, 0xFF, JME_REG_LEN);
  2010. regs->version = 1;
  2011. mmapio_memcpy(jme, p32, JME_MAC, JME_MAC_LEN);
  2012. p32 += 0x100 >> 2;
  2013. mmapio_memcpy(jme, p32, JME_PHY, JME_PHY_LEN);
  2014. p32 += 0x100 >> 2;
  2015. mmapio_memcpy(jme, p32, JME_MISC, JME_MISC_LEN);
  2016. p32 += 0x100 >> 2;
  2017. mmapio_memcpy(jme, p32, JME_RSS, JME_RSS_LEN);
  2018. p32 += 0x100 >> 2;
  2019. mdio_memcpy(jme, p32, JME_PHY_REG_NR);
  2020. }
  2021. static int
  2022. jme_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
  2023. {
  2024. struct jme_adapter *jme = netdev_priv(netdev);
  2025. ecmd->tx_coalesce_usecs = PCC_TX_TO;
  2026. ecmd->tx_max_coalesced_frames = PCC_TX_CNT;
  2027. if (test_bit(JME_FLAG_POLL, &jme->flags)) {
  2028. ecmd->use_adaptive_rx_coalesce = false;
  2029. ecmd->rx_coalesce_usecs = 0;
  2030. ecmd->rx_max_coalesced_frames = 0;
  2031. return 0;
  2032. }
  2033. ecmd->use_adaptive_rx_coalesce = true;
  2034. switch (jme->dpi.cur) {
  2035. case PCC_P1:
  2036. ecmd->rx_coalesce_usecs = PCC_P1_TO;
  2037. ecmd->rx_max_coalesced_frames = PCC_P1_CNT;
  2038. break;
  2039. case PCC_P2:
  2040. ecmd->rx_coalesce_usecs = PCC_P2_TO;
  2041. ecmd->rx_max_coalesced_frames = PCC_P2_CNT;
  2042. break;
  2043. case PCC_P3:
  2044. ecmd->rx_coalesce_usecs = PCC_P3_TO;
  2045. ecmd->rx_max_coalesced_frames = PCC_P3_CNT;
  2046. break;
  2047. default:
  2048. break;
  2049. }
  2050. return 0;
  2051. }
  2052. static int
  2053. jme_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecmd)
  2054. {
  2055. struct jme_adapter *jme = netdev_priv(netdev);
  2056. struct dynpcc_info *dpi = &(jme->dpi);
  2057. if (netif_running(netdev))
  2058. return -EBUSY;
  2059. if (ecmd->use_adaptive_rx_coalesce &&
  2060. test_bit(JME_FLAG_POLL, &jme->flags)) {
  2061. clear_bit(JME_FLAG_POLL, &jme->flags);
  2062. jme->jme_rx = netif_rx;
  2063. dpi->cur = PCC_P1;
  2064. dpi->attempt = PCC_P1;
  2065. dpi->cnt = 0;
  2066. jme_set_rx_pcc(jme, PCC_P1);
  2067. jme_interrupt_mode(jme);
  2068. } else if (!(ecmd->use_adaptive_rx_coalesce) &&
  2069. !(test_bit(JME_FLAG_POLL, &jme->flags))) {
  2070. set_bit(JME_FLAG_POLL, &jme->flags);
  2071. jme->jme_rx = netif_receive_skb;
  2072. jme_interrupt_mode(jme);
  2073. }
  2074. return 0;
  2075. }
  2076. static void
  2077. jme_get_pauseparam(struct net_device *netdev,
  2078. struct ethtool_pauseparam *ecmd)
  2079. {
  2080. struct jme_adapter *jme = netdev_priv(netdev);
  2081. u32 val;
  2082. ecmd->tx_pause = (jme->reg_txpfc & TXPFC_PF_EN) != 0;
  2083. ecmd->rx_pause = (jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0;
  2084. spin_lock_bh(&jme->phy_lock);
  2085. val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
  2086. spin_unlock_bh(&jme->phy_lock);
  2087. ecmd->autoneg =
  2088. (val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0;
  2089. }
  2090. static int
  2091. jme_set_pauseparam(struct net_device *netdev,
  2092. struct ethtool_pauseparam *ecmd)
  2093. {
  2094. struct jme_adapter *jme = netdev_priv(netdev);
  2095. u32 val;
  2096. if (((jme->reg_txpfc & TXPFC_PF_EN) != 0) ^
  2097. (ecmd->tx_pause != 0)) {
  2098. if (ecmd->tx_pause)
  2099. jme->reg_txpfc |= TXPFC_PF_EN;
  2100. else
  2101. jme->reg_txpfc &= ~TXPFC_PF_EN;
  2102. jwrite32(jme, JME_TXPFC, jme->reg_txpfc);
  2103. }
  2104. spin_lock_bh(&jme->rxmcs_lock);
  2105. if (((jme->reg_rxmcs & RXMCS_FLOWCTRL) != 0) ^
  2106. (ecmd->rx_pause != 0)) {
  2107. if (ecmd->rx_pause)
  2108. jme->reg_rxmcs |= RXMCS_FLOWCTRL;
  2109. else
  2110. jme->reg_rxmcs &= ~RXMCS_FLOWCTRL;
  2111. jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
  2112. }
  2113. spin_unlock_bh(&jme->rxmcs_lock);
  2114. spin_lock_bh(&jme->phy_lock);
  2115. val = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_ADVERTISE);
  2116. if (((val & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) != 0) ^
  2117. (ecmd->autoneg != 0)) {
  2118. if (ecmd->autoneg)
  2119. val |= (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  2120. else
  2121. val &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  2122. jme_mdio_write(jme->dev, jme->mii_if.phy_id,
  2123. MII_ADVERTISE, val);
  2124. }
  2125. spin_unlock_bh(&jme->phy_lock);
  2126. return 0;
  2127. }
  2128. static void
  2129. jme_get_wol(struct net_device *netdev,
  2130. struct ethtool_wolinfo *wol)
  2131. {
  2132. struct jme_adapter *jme = netdev_priv(netdev);
  2133. wol->supported = WAKE_MAGIC | WAKE_PHY;
  2134. wol->wolopts = 0;
  2135. if (jme->reg_pmcs & (PMCS_LFEN | PMCS_LREN))
  2136. wol->wolopts |= WAKE_PHY;
  2137. if (jme->reg_pmcs & PMCS_MFEN)
  2138. wol->wolopts |= WAKE_MAGIC;
  2139. }
  2140. static int
  2141. jme_set_wol(struct net_device *netdev,
  2142. struct ethtool_wolinfo *wol)
  2143. {
  2144. struct jme_adapter *jme = netdev_priv(netdev);
  2145. if (wol->wolopts & (WAKE_MAGICSECURE |
  2146. WAKE_UCAST |
  2147. WAKE_MCAST |
  2148. WAKE_BCAST |
  2149. WAKE_ARP))
  2150. return -EOPNOTSUPP;
  2151. jme->reg_pmcs = 0;
  2152. if (wol->wolopts & WAKE_PHY)
  2153. jme->reg_pmcs |= PMCS_LFEN | PMCS_LREN;
  2154. if (wol->wolopts & WAKE_MAGIC)
  2155. jme->reg_pmcs |= PMCS_MFEN;
  2156. return 0;
  2157. }
  2158. static int
  2159. jme_get_link_ksettings(struct net_device *netdev,
  2160. struct ethtool_link_ksettings *cmd)
  2161. {
  2162. struct jme_adapter *jme = netdev_priv(netdev);
  2163. spin_lock_bh(&jme->phy_lock);
  2164. mii_ethtool_get_link_ksettings(&jme->mii_if, cmd);
  2165. spin_unlock_bh(&jme->phy_lock);
  2166. return 0;
  2167. }
  2168. static int
  2169. jme_set_link_ksettings(struct net_device *netdev,
  2170. const struct ethtool_link_ksettings *cmd)
  2171. {
  2172. struct jme_adapter *jme = netdev_priv(netdev);
  2173. int rc, fdc = 0;
  2174. if (cmd->base.speed == SPEED_1000 &&
  2175. cmd->base.autoneg != AUTONEG_ENABLE)
  2176. return -EINVAL;
  2177. /*
  2178. * Check If user changed duplex only while force_media.
  2179. * Hardware would not generate link change interrupt.
  2180. */
  2181. if (jme->mii_if.force_media &&
  2182. cmd->base.autoneg != AUTONEG_ENABLE &&
  2183. (jme->mii_if.full_duplex != cmd->base.duplex))
  2184. fdc = 1;
  2185. spin_lock_bh(&jme->phy_lock);
  2186. rc = mii_ethtool_set_link_ksettings(&jme->mii_if, cmd);
  2187. spin_unlock_bh(&jme->phy_lock);
  2188. if (!rc) {
  2189. if (fdc)
  2190. jme_reset_link(jme);
  2191. jme->old_cmd = *cmd;
  2192. set_bit(JME_FLAG_SSET, &jme->flags);
  2193. }
  2194. return rc;
  2195. }
  2196. static int
  2197. jme_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  2198. {
  2199. int rc;
  2200. struct jme_adapter *jme = netdev_priv(netdev);
  2201. struct mii_ioctl_data *mii_data = if_mii(rq);
  2202. unsigned int duplex_chg;
  2203. if (cmd == SIOCSMIIREG) {
  2204. u16 val = mii_data->val_in;
  2205. if (!(val & (BMCR_RESET|BMCR_ANENABLE)) &&
  2206. (val & BMCR_SPEED1000))
  2207. return -EINVAL;
  2208. }
  2209. spin_lock_bh(&jme->phy_lock);
  2210. rc = generic_mii_ioctl(&jme->mii_if, mii_data, cmd, &duplex_chg);
  2211. spin_unlock_bh(&jme->phy_lock);
  2212. if (!rc && (cmd == SIOCSMIIREG)) {
  2213. if (duplex_chg)
  2214. jme_reset_link(jme);
  2215. jme_get_link_ksettings(netdev, &jme->old_cmd);
  2216. set_bit(JME_FLAG_SSET, &jme->flags);
  2217. }
  2218. return rc;
  2219. }
  2220. static u32
  2221. jme_get_link(struct net_device *netdev)
  2222. {
  2223. struct jme_adapter *jme = netdev_priv(netdev);
  2224. return jread32(jme, JME_PHY_LINK) & PHY_LINK_UP;
  2225. }
  2226. static u32
  2227. jme_get_msglevel(struct net_device *netdev)
  2228. {
  2229. struct jme_adapter *jme = netdev_priv(netdev);
  2230. return jme->msg_enable;
  2231. }
  2232. static void
  2233. jme_set_msglevel(struct net_device *netdev, u32 value)
  2234. {
  2235. struct jme_adapter *jme = netdev_priv(netdev);
  2236. jme->msg_enable = value;
  2237. }
  2238. static netdev_features_t
  2239. jme_fix_features(struct net_device *netdev, netdev_features_t features)
  2240. {
  2241. if (netdev->mtu > 1900)
  2242. features &= ~(NETIF_F_ALL_TSO | NETIF_F_CSUM_MASK);
  2243. return features;
  2244. }
  2245. static int
  2246. jme_set_features(struct net_device *netdev, netdev_features_t features)
  2247. {
  2248. struct jme_adapter *jme = netdev_priv(netdev);
  2249. spin_lock_bh(&jme->rxmcs_lock);
  2250. if (features & NETIF_F_RXCSUM)
  2251. jme->reg_rxmcs |= RXMCS_CHECKSUM;
  2252. else
  2253. jme->reg_rxmcs &= ~RXMCS_CHECKSUM;
  2254. jwrite32(jme, JME_RXMCS, jme->reg_rxmcs);
  2255. spin_unlock_bh(&jme->rxmcs_lock);
  2256. return 0;
  2257. }
  2258. #ifdef CONFIG_NET_POLL_CONTROLLER
  2259. static void jme_netpoll(struct net_device *dev)
  2260. {
  2261. unsigned long flags;
  2262. local_irq_save(flags);
  2263. jme_intr(dev->irq, dev);
  2264. local_irq_restore(flags);
  2265. }
  2266. #endif
  2267. static int
  2268. jme_nway_reset(struct net_device *netdev)
  2269. {
  2270. struct jme_adapter *jme = netdev_priv(netdev);
  2271. jme_restart_an(jme);
  2272. return 0;
  2273. }
  2274. static u8
  2275. jme_smb_read(struct jme_adapter *jme, unsigned int addr)
  2276. {
  2277. u32 val;
  2278. int to;
  2279. val = jread32(jme, JME_SMBCSR);
  2280. to = JME_SMB_BUSY_TIMEOUT;
  2281. while ((val & SMBCSR_BUSY) && --to) {
  2282. msleep(1);
  2283. val = jread32(jme, JME_SMBCSR);
  2284. }
  2285. if (!to) {
  2286. netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
  2287. return 0xFF;
  2288. }
  2289. jwrite32(jme, JME_SMBINTF,
  2290. ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
  2291. SMBINTF_HWRWN_READ |
  2292. SMBINTF_HWCMD);
  2293. val = jread32(jme, JME_SMBINTF);
  2294. to = JME_SMB_BUSY_TIMEOUT;
  2295. while ((val & SMBINTF_HWCMD) && --to) {
  2296. msleep(1);
  2297. val = jread32(jme, JME_SMBINTF);
  2298. }
  2299. if (!to) {
  2300. netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
  2301. return 0xFF;
  2302. }
  2303. return (val & SMBINTF_HWDATR) >> SMBINTF_HWDATR_SHIFT;
  2304. }
  2305. static void
  2306. jme_smb_write(struct jme_adapter *jme, unsigned int addr, u8 data)
  2307. {
  2308. u32 val;
  2309. int to;
  2310. val = jread32(jme, JME_SMBCSR);
  2311. to = JME_SMB_BUSY_TIMEOUT;
  2312. while ((val & SMBCSR_BUSY) && --to) {
  2313. msleep(1);
  2314. val = jread32(jme, JME_SMBCSR);
  2315. }
  2316. if (!to) {
  2317. netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
  2318. return;
  2319. }
  2320. jwrite32(jme, JME_SMBINTF,
  2321. ((data << SMBINTF_HWDATW_SHIFT) & SMBINTF_HWDATW) |
  2322. ((addr << SMBINTF_HWADDR_SHIFT) & SMBINTF_HWADDR) |
  2323. SMBINTF_HWRWN_WRITE |
  2324. SMBINTF_HWCMD);
  2325. val = jread32(jme, JME_SMBINTF);
  2326. to = JME_SMB_BUSY_TIMEOUT;
  2327. while ((val & SMBINTF_HWCMD) && --to) {
  2328. msleep(1);
  2329. val = jread32(jme, JME_SMBINTF);
  2330. }
  2331. if (!to) {
  2332. netif_err(jme, hw, jme->dev, "SMB Bus Busy\n");
  2333. return;
  2334. }
  2335. mdelay(2);
  2336. }
  2337. static int
  2338. jme_get_eeprom_len(struct net_device *netdev)
  2339. {
  2340. struct jme_adapter *jme = netdev_priv(netdev);
  2341. u32 val;
  2342. val = jread32(jme, JME_SMBCSR);
  2343. return (val & SMBCSR_EEPROMD) ? JME_SMB_LEN : 0;
  2344. }
  2345. static int
  2346. jme_get_eeprom(struct net_device *netdev,
  2347. struct ethtool_eeprom *eeprom, u8 *data)
  2348. {
  2349. struct jme_adapter *jme = netdev_priv(netdev);
  2350. int i, offset = eeprom->offset, len = eeprom->len;
  2351. /*
  2352. * ethtool will check the boundary for us
  2353. */
  2354. eeprom->magic = JME_EEPROM_MAGIC;
  2355. for (i = 0 ; i < len ; ++i)
  2356. data[i] = jme_smb_read(jme, i + offset);
  2357. return 0;
  2358. }
  2359. static int
  2360. jme_set_eeprom(struct net_device *netdev,
  2361. struct ethtool_eeprom *eeprom, u8 *data)
  2362. {
  2363. struct jme_adapter *jme = netdev_priv(netdev);
  2364. int i, offset = eeprom->offset, len = eeprom->len;
  2365. if (eeprom->magic != JME_EEPROM_MAGIC)
  2366. return -EINVAL;
  2367. /*
  2368. * ethtool will check the boundary for us
  2369. */
  2370. for (i = 0 ; i < len ; ++i)
  2371. jme_smb_write(jme, i + offset, data[i]);
  2372. return 0;
  2373. }
  2374. static const struct ethtool_ops jme_ethtool_ops = {
  2375. .get_drvinfo = jme_get_drvinfo,
  2376. .get_regs_len = jme_get_regs_len,
  2377. .get_regs = jme_get_regs,
  2378. .get_coalesce = jme_get_coalesce,
  2379. .set_coalesce = jme_set_coalesce,
  2380. .get_pauseparam = jme_get_pauseparam,
  2381. .set_pauseparam = jme_set_pauseparam,
  2382. .get_wol = jme_get_wol,
  2383. .set_wol = jme_set_wol,
  2384. .get_link = jme_get_link,
  2385. .get_msglevel = jme_get_msglevel,
  2386. .set_msglevel = jme_set_msglevel,
  2387. .nway_reset = jme_nway_reset,
  2388. .get_eeprom_len = jme_get_eeprom_len,
  2389. .get_eeprom = jme_get_eeprom,
  2390. .set_eeprom = jme_set_eeprom,
  2391. .get_link_ksettings = jme_get_link_ksettings,
  2392. .set_link_ksettings = jme_set_link_ksettings,
  2393. };
  2394. static int
  2395. jme_pci_dma64(struct pci_dev *pdev)
  2396. {
  2397. if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
  2398. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  2399. if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
  2400. return 1;
  2401. if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250 &&
  2402. !pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
  2403. if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40)))
  2404. return 1;
  2405. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
  2406. if (!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  2407. return 0;
  2408. return -1;
  2409. }
  2410. static inline void
  2411. jme_phy_init(struct jme_adapter *jme)
  2412. {
  2413. u16 reg26;
  2414. reg26 = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 26);
  2415. jme_mdio_write(jme->dev, jme->mii_if.phy_id, 26, reg26 | 0x1000);
  2416. }
  2417. static inline void
  2418. jme_check_hw_ver(struct jme_adapter *jme)
  2419. {
  2420. u32 chipmode;
  2421. chipmode = jread32(jme, JME_CHIPMODE);
  2422. jme->fpgaver = (chipmode & CM_FPGAVER_MASK) >> CM_FPGAVER_SHIFT;
  2423. jme->chiprev = (chipmode & CM_CHIPREV_MASK) >> CM_CHIPREV_SHIFT;
  2424. jme->chip_main_rev = jme->chiprev & 0xF;
  2425. jme->chip_sub_rev = (jme->chiprev >> 4) & 0xF;
  2426. }
  2427. static const struct net_device_ops jme_netdev_ops = {
  2428. .ndo_open = jme_open,
  2429. .ndo_stop = jme_close,
  2430. .ndo_validate_addr = eth_validate_addr,
  2431. .ndo_do_ioctl = jme_ioctl,
  2432. .ndo_start_xmit = jme_start_xmit,
  2433. .ndo_set_mac_address = jme_set_macaddr,
  2434. .ndo_set_rx_mode = jme_set_multi,
  2435. .ndo_change_mtu = jme_change_mtu,
  2436. .ndo_tx_timeout = jme_tx_timeout,
  2437. .ndo_fix_features = jme_fix_features,
  2438. .ndo_set_features = jme_set_features,
  2439. #ifdef CONFIG_NET_POLL_CONTROLLER
  2440. .ndo_poll_controller = jme_netpoll,
  2441. #endif
  2442. };
  2443. static int
  2444. jme_init_one(struct pci_dev *pdev,
  2445. const struct pci_device_id *ent)
  2446. {
  2447. int rc = 0, using_dac, i;
  2448. struct net_device *netdev;
  2449. struct jme_adapter *jme;
  2450. u16 bmcr, bmsr;
  2451. u32 apmc;
  2452. /*
  2453. * set up PCI device basics
  2454. */
  2455. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  2456. PCIE_LINK_STATE_CLKPM);
  2457. rc = pci_enable_device(pdev);
  2458. if (rc) {
  2459. pr_err("Cannot enable PCI device\n");
  2460. goto err_out;
  2461. }
  2462. using_dac = jme_pci_dma64(pdev);
  2463. if (using_dac < 0) {
  2464. pr_err("Cannot set PCI DMA Mask\n");
  2465. rc = -EIO;
  2466. goto err_out_disable_pdev;
  2467. }
  2468. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  2469. pr_err("No PCI resource region found\n");
  2470. rc = -ENOMEM;
  2471. goto err_out_disable_pdev;
  2472. }
  2473. rc = pci_request_regions(pdev, DRV_NAME);
  2474. if (rc) {
  2475. pr_err("Cannot obtain PCI resource region\n");
  2476. goto err_out_disable_pdev;
  2477. }
  2478. pci_set_master(pdev);
  2479. /*
  2480. * alloc and init net device
  2481. */
  2482. netdev = alloc_etherdev(sizeof(*jme));
  2483. if (!netdev) {
  2484. rc = -ENOMEM;
  2485. goto err_out_release_regions;
  2486. }
  2487. netdev->netdev_ops = &jme_netdev_ops;
  2488. netdev->ethtool_ops = &jme_ethtool_ops;
  2489. netdev->watchdog_timeo = TX_TIMEOUT;
  2490. netdev->hw_features = NETIF_F_IP_CSUM |
  2491. NETIF_F_IPV6_CSUM |
  2492. NETIF_F_SG |
  2493. NETIF_F_TSO |
  2494. NETIF_F_TSO6 |
  2495. NETIF_F_RXCSUM;
  2496. netdev->features = NETIF_F_IP_CSUM |
  2497. NETIF_F_IPV6_CSUM |
  2498. NETIF_F_SG |
  2499. NETIF_F_TSO |
  2500. NETIF_F_TSO6 |
  2501. NETIF_F_HW_VLAN_CTAG_TX |
  2502. NETIF_F_HW_VLAN_CTAG_RX;
  2503. if (using_dac)
  2504. netdev->features |= NETIF_F_HIGHDMA;
  2505. /* MTU range: 1280 - 9202*/
  2506. netdev->min_mtu = IPV6_MIN_MTU;
  2507. netdev->max_mtu = MAX_ETHERNET_JUMBO_PACKET_SIZE - ETH_HLEN;
  2508. SET_NETDEV_DEV(netdev, &pdev->dev);
  2509. pci_set_drvdata(pdev, netdev);
  2510. /*
  2511. * init adapter info
  2512. */
  2513. jme = netdev_priv(netdev);
  2514. jme->pdev = pdev;
  2515. jme->dev = netdev;
  2516. jme->jme_rx = netif_rx;
  2517. jme->old_mtu = netdev->mtu = 1500;
  2518. jme->phylink = 0;
  2519. jme->tx_ring_size = 1 << 10;
  2520. jme->tx_ring_mask = jme->tx_ring_size - 1;
  2521. jme->tx_wake_threshold = 1 << 9;
  2522. jme->rx_ring_size = 1 << 9;
  2523. jme->rx_ring_mask = jme->rx_ring_size - 1;
  2524. jme->msg_enable = JME_DEF_MSG_ENABLE;
  2525. jme->regs = ioremap(pci_resource_start(pdev, 0),
  2526. pci_resource_len(pdev, 0));
  2527. if (!(jme->regs)) {
  2528. pr_err("Mapping PCI resource region error\n");
  2529. rc = -ENOMEM;
  2530. goto err_out_free_netdev;
  2531. }
  2532. if (no_pseudohp) {
  2533. apmc = jread32(jme, JME_APMC) & ~JME_APMC_PSEUDO_HP_EN;
  2534. jwrite32(jme, JME_APMC, apmc);
  2535. } else if (force_pseudohp) {
  2536. apmc = jread32(jme, JME_APMC) | JME_APMC_PSEUDO_HP_EN;
  2537. jwrite32(jme, JME_APMC, apmc);
  2538. }
  2539. NETIF_NAPI_SET(netdev, &jme->napi, jme_poll, NAPI_POLL_WEIGHT)
  2540. spin_lock_init(&jme->phy_lock);
  2541. spin_lock_init(&jme->macaddr_lock);
  2542. spin_lock_init(&jme->rxmcs_lock);
  2543. atomic_set(&jme->link_changing, 1);
  2544. atomic_set(&jme->rx_cleaning, 1);
  2545. atomic_set(&jme->tx_cleaning, 1);
  2546. atomic_set(&jme->rx_empty, 1);
  2547. tasklet_init(&jme->pcc_task,
  2548. jme_pcc_tasklet,
  2549. (unsigned long) jme);
  2550. jme->dpi.cur = PCC_P1;
  2551. jme->reg_ghc = 0;
  2552. jme->reg_rxcs = RXCS_DEFAULT;
  2553. jme->reg_rxmcs = RXMCS_DEFAULT;
  2554. jme->reg_txpfc = 0;
  2555. jme->reg_pmcs = PMCS_MFEN;
  2556. jme->reg_gpreg1 = GPREG1_DEFAULT;
  2557. if (jme->reg_rxmcs & RXMCS_CHECKSUM)
  2558. netdev->features |= NETIF_F_RXCSUM;
  2559. /*
  2560. * Get Max Read Req Size from PCI Config Space
  2561. */
  2562. pci_read_config_byte(pdev, PCI_DCSR_MRRS, &jme->mrrs);
  2563. jme->mrrs &= PCI_DCSR_MRRS_MASK;
  2564. switch (jme->mrrs) {
  2565. case MRRS_128B:
  2566. jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_128B;
  2567. break;
  2568. case MRRS_256B:
  2569. jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_256B;
  2570. break;
  2571. default:
  2572. jme->reg_txcs = TXCS_DEFAULT | TXCS_DMASIZE_512B;
  2573. break;
  2574. }
  2575. /*
  2576. * Must check before reset_mac_processor
  2577. */
  2578. jme_check_hw_ver(jme);
  2579. jme->mii_if.dev = netdev;
  2580. if (jme->fpgaver) {
  2581. jme->mii_if.phy_id = 0;
  2582. for (i = 1 ; i < 32 ; ++i) {
  2583. bmcr = jme_mdio_read(netdev, i, MII_BMCR);
  2584. bmsr = jme_mdio_read(netdev, i, MII_BMSR);
  2585. if (bmcr != 0xFFFFU && (bmcr != 0 || bmsr != 0)) {
  2586. jme->mii_if.phy_id = i;
  2587. break;
  2588. }
  2589. }
  2590. if (!jme->mii_if.phy_id) {
  2591. rc = -EIO;
  2592. pr_err("Can not find phy_id\n");
  2593. goto err_out_unmap;
  2594. }
  2595. jme->reg_ghc |= GHC_LINK_POLL;
  2596. } else {
  2597. jme->mii_if.phy_id = 1;
  2598. }
  2599. if (pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
  2600. jme->mii_if.supports_gmii = true;
  2601. else
  2602. jme->mii_if.supports_gmii = false;
  2603. jme->mii_if.phy_id_mask = 0x1F;
  2604. jme->mii_if.reg_num_mask = 0x1F;
  2605. jme->mii_if.mdio_read = jme_mdio_read;
  2606. jme->mii_if.mdio_write = jme_mdio_write;
  2607. jme_clear_pm_disable_wol(jme);
  2608. device_init_wakeup(&pdev->dev, true);
  2609. jme_set_phyfifo_5level(jme);
  2610. jme->pcirev = pdev->revision;
  2611. if (!jme->fpgaver)
  2612. jme_phy_init(jme);
  2613. jme_phy_off(jme);
  2614. /*
  2615. * Reset MAC processor and reload EEPROM for MAC Address
  2616. */
  2617. jme_reset_mac_processor(jme);
  2618. rc = jme_reload_eeprom(jme);
  2619. if (rc) {
  2620. pr_err("Reload eeprom for reading MAC Address error\n");
  2621. goto err_out_unmap;
  2622. }
  2623. jme_load_macaddr(netdev);
  2624. /*
  2625. * Tell stack that we are not ready to work until open()
  2626. */
  2627. netif_carrier_off(netdev);
  2628. rc = register_netdev(netdev);
  2629. if (rc) {
  2630. pr_err("Cannot register net device\n");
  2631. goto err_out_unmap;
  2632. }
  2633. netif_info(jme, probe, jme->dev, "%s%s chiprev:%x pcirev:%x macaddr:%pM\n",
  2634. (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250) ?
  2635. "JMC250 Gigabit Ethernet" :
  2636. (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC260) ?
  2637. "JMC260 Fast Ethernet" : "Unknown",
  2638. (jme->fpgaver != 0) ? " (FPGA)" : "",
  2639. (jme->fpgaver != 0) ? jme->fpgaver : jme->chiprev,
  2640. jme->pcirev, netdev->dev_addr);
  2641. return 0;
  2642. err_out_unmap:
  2643. iounmap(jme->regs);
  2644. err_out_free_netdev:
  2645. free_netdev(netdev);
  2646. err_out_release_regions:
  2647. pci_release_regions(pdev);
  2648. err_out_disable_pdev:
  2649. pci_disable_device(pdev);
  2650. err_out:
  2651. return rc;
  2652. }
  2653. static void
  2654. jme_remove_one(struct pci_dev *pdev)
  2655. {
  2656. struct net_device *netdev = pci_get_drvdata(pdev);
  2657. struct jme_adapter *jme = netdev_priv(netdev);
  2658. unregister_netdev(netdev);
  2659. iounmap(jme->regs);
  2660. free_netdev(netdev);
  2661. pci_release_regions(pdev);
  2662. pci_disable_device(pdev);
  2663. }
  2664. static void
  2665. jme_shutdown(struct pci_dev *pdev)
  2666. {
  2667. struct net_device *netdev = pci_get_drvdata(pdev);
  2668. struct jme_adapter *jme = netdev_priv(netdev);
  2669. jme_powersave_phy(jme);
  2670. pci_pme_active(pdev, true);
  2671. }
  2672. #ifdef CONFIG_PM_SLEEP
  2673. static int
  2674. jme_suspend(struct device *dev)
  2675. {
  2676. struct pci_dev *pdev = to_pci_dev(dev);
  2677. struct net_device *netdev = pci_get_drvdata(pdev);
  2678. struct jme_adapter *jme = netdev_priv(netdev);
  2679. if (!netif_running(netdev))
  2680. return 0;
  2681. atomic_dec(&jme->link_changing);
  2682. netif_device_detach(netdev);
  2683. netif_stop_queue(netdev);
  2684. jme_stop_irq(jme);
  2685. tasklet_disable(&jme->txclean_task);
  2686. tasklet_disable(&jme->rxclean_task);
  2687. tasklet_disable(&jme->rxempty_task);
  2688. if (netif_carrier_ok(netdev)) {
  2689. if (test_bit(JME_FLAG_POLL, &jme->flags))
  2690. jme_polling_mode(jme);
  2691. jme_stop_pcc_timer(jme);
  2692. jme_disable_rx_engine(jme);
  2693. jme_disable_tx_engine(jme);
  2694. jme_reset_mac_processor(jme);
  2695. jme_free_rx_resources(jme);
  2696. jme_free_tx_resources(jme);
  2697. netif_carrier_off(netdev);
  2698. jme->phylink = 0;
  2699. }
  2700. tasklet_enable(&jme->txclean_task);
  2701. tasklet_enable(&jme->rxclean_task);
  2702. tasklet_enable(&jme->rxempty_task);
  2703. jme_powersave_phy(jme);
  2704. return 0;
  2705. }
  2706. static int
  2707. jme_resume(struct device *dev)
  2708. {
  2709. struct pci_dev *pdev = to_pci_dev(dev);
  2710. struct net_device *netdev = pci_get_drvdata(pdev);
  2711. struct jme_adapter *jme = netdev_priv(netdev);
  2712. if (!netif_running(netdev))
  2713. return 0;
  2714. jme_clear_pm_disable_wol(jme);
  2715. jme_phy_on(jme);
  2716. if (test_bit(JME_FLAG_SSET, &jme->flags))
  2717. jme_set_link_ksettings(netdev, &jme->old_cmd);
  2718. else
  2719. jme_reset_phy_processor(jme);
  2720. jme_phy_calibration(jme);
  2721. jme_phy_setEA(jme);
  2722. netif_device_attach(netdev);
  2723. atomic_inc(&jme->link_changing);
  2724. jme_reset_link(jme);
  2725. jme_start_irq(jme);
  2726. return 0;
  2727. }
  2728. static SIMPLE_DEV_PM_OPS(jme_pm_ops, jme_suspend, jme_resume);
  2729. #define JME_PM_OPS (&jme_pm_ops)
  2730. #else
  2731. #define JME_PM_OPS NULL
  2732. #endif
  2733. static const struct pci_device_id jme_pci_tbl[] = {
  2734. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC250) },
  2735. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMC260) },
  2736. { }
  2737. };
  2738. static struct pci_driver jme_driver = {
  2739. .name = DRV_NAME,
  2740. .id_table = jme_pci_tbl,
  2741. .probe = jme_init_one,
  2742. .remove = jme_remove_one,
  2743. .shutdown = jme_shutdown,
  2744. .driver.pm = JME_PM_OPS,
  2745. };
  2746. static int __init
  2747. jme_init_module(void)
  2748. {
  2749. pr_info("JMicron JMC2XX ethernet driver version %s\n", DRV_VERSION);
  2750. return pci_register_driver(&jme_driver);
  2751. }
  2752. static void __exit
  2753. jme_cleanup_module(void)
  2754. {
  2755. pci_unregister_driver(&jme_driver);
  2756. }
  2757. module_init(jme_init_module);
  2758. module_exit(jme_cleanup_module);
  2759. MODULE_AUTHOR("Guo-Fu Tseng <cooldavid@cooldavid.org>");
  2760. MODULE_DESCRIPTION("JMicron JMC2x0 PCI Express Ethernet driver");
  2761. MODULE_LICENSE("GPL");
  2762. MODULE_VERSION(DRV_VERSION);
  2763. MODULE_DEVICE_TABLE(pci, jme_pci_tbl);