jme.c 75 KB

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