jme.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373
  1. /*
  2. * JMicron JMC2x0 series PCIe Ethernet Linux Device Driver
  3. *
  4. * Copyright 2008 JMicron Technology Corporation
  5. * http://www.jmicron.com/
  6. * Copyright (c) 2009 - 2010 Guo-Fu Tseng <cooldavid@cooldavid.org>
  7. *
  8. * Author: Guo-Fu Tseng <cooldavid@cooldavid.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/pci.h>
  28. #include <linux/pci-aspm.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/mii.h>
  33. #include <linux/crc32.h>
  34. #include <linux/delay.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/in.h>
  37. #include <linux/ip.h>
  38. #include <linux/ipv6.h>
  39. #include <linux/tcp.h>
  40. #include <linux/udp.h>
  41. #include <linux/if_vlan.h>
  42. #include <linux/slab.h>
  43. #include <net/ip6_checksum.h>
  44. #include "jme.h"
  45. static int force_pseudohp = -1;
  46. static int no_pseudohp = -1;
  47. static int no_extplug = -1;
  48. module_param(force_pseudohp, int, 0);
  49. MODULE_PARM_DESC(force_pseudohp,
  50. "Enable pseudo hot-plug feature manually by driver instead of BIOS.");
  51. module_param(no_pseudohp, int, 0);
  52. MODULE_PARM_DESC(no_pseudohp, "Disable pseudo hot-plug feature.");
  53. module_param(no_extplug, int, 0);
  54. MODULE_PARM_DESC(no_extplug,
  55. "Do not use external plug signal for pseudo hot-plug.");
  56. static int
  57. jme_mdio_read(struct net_device *netdev, int phy, int reg)
  58. {
  59. struct jme_adapter *jme = netdev_priv(netdev);
  60. int i, val, again = (reg == MII_BMSR) ? 1 : 0;
  61. read_again:
  62. jwrite32(jme, JME_SMI, SMI_OP_REQ |
  63. smi_phy_addr(phy) |
  64. smi_reg_addr(reg));
  65. wmb();
  66. for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
  67. udelay(20);
  68. val = jread32(jme, JME_SMI);
  69. if ((val & SMI_OP_REQ) == 0)
  70. break;
  71. }
  72. if (i == 0) {
  73. pr_err("phy(%d) read timeout : %d\n", phy, reg);
  74. return 0;
  75. }
  76. if (again--)
  77. goto read_again;
  78. return (val & SMI_DATA_MASK) >> SMI_DATA_SHIFT;
  79. }
  80. static void
  81. jme_mdio_write(struct net_device *netdev,
  82. int phy, int reg, int val)
  83. {
  84. struct jme_adapter *jme = netdev_priv(netdev);
  85. int i;
  86. jwrite32(jme, JME_SMI, SMI_OP_WRITE | SMI_OP_REQ |
  87. ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
  88. smi_phy_addr(phy) | smi_reg_addr(reg));
  89. wmb();
  90. for (i = JME_PHY_TIMEOUT * 50 ; i > 0 ; --i) {
  91. udelay(20);
  92. if ((jread32(jme, JME_SMI) & SMI_OP_REQ) == 0)
  93. break;
  94. }
  95. if (i == 0)
  96. pr_err("phy(%d) write timeout : %d\n", phy, reg);
  97. }
  98. static inline void
  99. jme_reset_phy_processor(struct jme_adapter *jme)
  100. {
  101. u32 val;
  102. jme_mdio_write(jme->dev,
  103. jme->mii_if.phy_id,
  104. MII_ADVERTISE, ADVERTISE_ALL |
  105. ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  106. if (jme->pdev->device == PCI_DEVICE_ID_JMICRON_JMC250)
  107. jme_mdio_write(jme->dev,
  108. jme->mii_if.phy_id,
  109. MII_CTRL1000,
  110. ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  111. val = jme_mdio_read(jme->dev,
  112. jme->mii_if.phy_id,
  113. MII_BMCR);
  114. jme_mdio_write(jme->dev,
  115. jme->mii_if.phy_id,
  116. MII_BMCR, val | BMCR_RESET);
  117. }
  118. static void
  119. jme_setup_wakeup_frame(struct jme_adapter *jme,
  120. const u32 *mask, u32 crc, int fnr)
  121. {
  122. int i;
  123. /*
  124. * Setup CRC pattern
  125. */
  126. jwrite32(jme, JME_WFOI, WFOI_CRC_SEL | (fnr & WFOI_FRAME_SEL));
  127. wmb();
  128. jwrite32(jme, JME_WFODP, crc);
  129. wmb();
  130. /*
  131. * Setup Mask
  132. */
  133. for (i = 0 ; i < WAKEUP_FRAME_MASK_DWNR ; ++i) {
  134. jwrite32(jme, JME_WFOI,
  135. ((i << WFOI_MASK_SHIFT) & WFOI_MASK_SEL) |
  136. (fnr & WFOI_FRAME_SEL));
  137. wmb();
  138. jwrite32(jme, JME_WFODP, mask[i]);
  139. wmb();
  140. }
  141. }
  142. static inline void
  143. jme_mac_rxclk_off(struct jme_adapter *jme)
  144. {
  145. jme->reg_gpreg1 |= GPREG1_RXCLKOFF;
  146. jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
  147. }
  148. static inline void
  149. jme_mac_rxclk_on(struct jme_adapter *jme)
  150. {
  151. jme->reg_gpreg1 &= ~GPREG1_RXCLKOFF;
  152. jwrite32f(jme, JME_GPREG1, jme->reg_gpreg1);
  153. }
  154. static inline void
  155. jme_mac_txclk_off(struct jme_adapter *jme)
  156. {
  157. jme->reg_ghc &= ~(GHC_TO_CLK_SRC | GHC_TXMAC_CLK_SRC);
  158. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  159. }
  160. static inline void
  161. jme_mac_txclk_on(struct jme_adapter *jme)
  162. {
  163. u32 speed = jme->reg_ghc & GHC_SPEED;
  164. if (speed == GHC_SPEED_1000M)
  165. jme->reg_ghc |= GHC_TO_CLK_GPHY | GHC_TXMAC_CLK_GPHY;
  166. else
  167. jme->reg_ghc |= GHC_TO_CLK_PCIE | GHC_TXMAC_CLK_PCIE;
  168. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  169. }
  170. static inline void
  171. jme_reset_ghc_speed(struct jme_adapter *jme)
  172. {
  173. jme->reg_ghc &= ~(GHC_SPEED | GHC_DPX);
  174. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  175. }
  176. static inline void
  177. jme_reset_250A2_workaround(struct jme_adapter *jme)
  178. {
  179. jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
  180. GPREG1_RSSPATCH);
  181. jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
  182. }
  183. static inline void
  184. jme_assert_ghc_reset(struct jme_adapter *jme)
  185. {
  186. jme->reg_ghc |= GHC_SWRST;
  187. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  188. }
  189. static inline void
  190. jme_clear_ghc_reset(struct jme_adapter *jme)
  191. {
  192. jme->reg_ghc &= ~GHC_SWRST;
  193. jwrite32f(jme, JME_GHC, jme->reg_ghc);
  194. }
  195. static void
  196. jme_reset_mac_processor(struct jme_adapter *jme)
  197. {
  198. static const u32 mask[WAKEUP_FRAME_MASK_DWNR] = {0, 0, 0, 0};
  199. u32 crc = 0xCDCDCDCD;
  200. u32 gpreg0;
  201. int i;
  202. jme_reset_ghc_speed(jme);
  203. jme_reset_250A2_workaround(jme);
  204. jme_mac_rxclk_on(jme);
  205. jme_mac_txclk_on(jme);
  206. udelay(1);
  207. jme_assert_ghc_reset(jme);
  208. udelay(1);
  209. jme_mac_rxclk_off(jme);
  210. jme_mac_txclk_off(jme);
  211. udelay(1);
  212. jme_clear_ghc_reset(jme);
  213. udelay(1);
  214. jme_mac_rxclk_on(jme);
  215. jme_mac_txclk_on(jme);
  216. udelay(1);
  217. jme_mac_rxclk_off(jme);
  218. jme_mac_txclk_off(jme);
  219. jwrite32(jme, JME_RXDBA_LO, 0x00000000);
  220. jwrite32(jme, JME_RXDBA_HI, 0x00000000);
  221. jwrite32(jme, JME_RXQDC, 0x00000000);
  222. jwrite32(jme, JME_RXNDA, 0x00000000);
  223. jwrite32(jme, JME_TXDBA_LO, 0x00000000);
  224. jwrite32(jme, JME_TXDBA_HI, 0x00000000);
  225. jwrite32(jme, JME_TXQDC, 0x00000000);
  226. jwrite32(jme, JME_TXNDA, 0x00000000);
  227. jwrite32(jme, JME_RXMCHT_LO, 0x00000000);
  228. jwrite32(jme, JME_RXMCHT_HI, 0x00000000);
  229. for (i = 0 ; i < WAKEUP_FRAME_NR ; ++i)
  230. jme_setup_wakeup_frame(jme, mask, crc, i);
  231. if (jme->fpgaver)
  232. gpreg0 = GPREG0_DEFAULT | GPREG0_LNKINTPOLL;
  233. else
  234. gpreg0 = GPREG0_DEFAULT;
  235. jwrite32(jme, JME_GPREG0, gpreg0);
  236. }
  237. static inline void
  238. jme_clear_pm_enable_wol(struct jme_adapter *jme)
  239. {
  240. jwrite32(jme, JME_PMCS, PMCS_STMASK | jme->reg_pmcs);
  241. }
  242. static inline void
  243. jme_clear_pm_disable_wol(struct jme_adapter *jme)
  244. {
  245. jwrite32(jme, JME_PMCS, PMCS_STMASK);
  246. }
  247. static int
  248. jme_reload_eeprom(struct jme_adapter *jme)
  249. {
  250. u32 val;
  251. int i;
  252. val = jread32(jme, JME_SMBCSR);
  253. if (val & SMBCSR_EEPROMD) {
  254. val |= SMBCSR_CNACK;
  255. jwrite32(jme, JME_SMBCSR, val);
  256. val |= SMBCSR_RELOAD;
  257. jwrite32(jme, JME_SMBCSR, val);
  258. mdelay(12);
  259. for (i = JME_EEPROM_RELOAD_TIMEOUT; i > 0; --i) {
  260. mdelay(1);
  261. if ((jread32(jme, JME_SMBCSR) & SMBCSR_RELOAD) == 0)
  262. break;
  263. }
  264. if (i == 0) {
  265. pr_err("eeprom reload timeout\n");
  266. return -EIO;
  267. }
  268. }
  269. return 0;
  270. }
  271. static void
  272. jme_load_macaddr(struct net_device *netdev)
  273. {
  274. struct jme_adapter *jme = netdev_priv(netdev);
  275. unsigned char macaddr[ETH_ALEN];
  276. u32 val;
  277. spin_lock_bh(&jme->macaddr_lock);
  278. val = jread32(jme, JME_RXUMA_LO);
  279. macaddr[0] = (val >> 0) & 0xFF;
  280. macaddr[1] = (val >> 8) & 0xFF;
  281. macaddr[2] = (val >> 16) & 0xFF;
  282. macaddr[3] = (val >> 24) & 0xFF;
  283. val = jread32(jme, JME_RXUMA_HI);
  284. macaddr[4] = (val >> 0) & 0xFF;
  285. macaddr[5] = (val >> 8) & 0xFF;
  286. memcpy(netdev->dev_addr, macaddr, ETH_ALEN);
  287. spin_unlock_bh(&jme->macaddr_lock);
  288. }
  289. static inline void
  290. jme_set_rx_pcc(struct jme_adapter *jme, int p)
  291. {
  292. switch (p) {
  293. case PCC_OFF:
  294. jwrite32(jme, JME_PCCRX0,
  295. ((PCC_OFF_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  296. ((PCC_OFF_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  297. break;
  298. case PCC_P1:
  299. jwrite32(jme, JME_PCCRX0,
  300. ((PCC_P1_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  301. ((PCC_P1_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  302. break;
  303. case PCC_P2:
  304. jwrite32(jme, JME_PCCRX0,
  305. ((PCC_P2_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  306. ((PCC_P2_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  307. break;
  308. case PCC_P3:
  309. jwrite32(jme, JME_PCCRX0,
  310. ((PCC_P3_TO << PCCRXTO_SHIFT) & PCCRXTO_MASK) |
  311. ((PCC_P3_CNT << PCCRX_SHIFT) & PCCRX_MASK));
  312. break;
  313. default:
  314. break;
  315. }
  316. wmb();
  317. if (!(test_bit(JME_FLAG_POLL, &jme->flags)))
  318. netif_info(jme, rx_status, jme->dev, "Switched to PCC_P%d\n", p);
  319. }
  320. static void
  321. jme_start_irq(struct jme_adapter *jme)
  322. {
  323. register struct dynpcc_info *dpi = &(jme->dpi);
  324. jme_set_rx_pcc(jme, PCC_P1);
  325. dpi->cur = PCC_P1;
  326. dpi->attempt = PCC_P1;
  327. dpi->cnt = 0;
  328. jwrite32(jme, JME_PCCTX,
  329. ((PCC_TX_TO << PCCTXTO_SHIFT) & PCCTXTO_MASK) |
  330. ((PCC_TX_CNT << PCCTX_SHIFT) & PCCTX_MASK) |
  331. PCCTXQ0_EN
  332. );
  333. /*
  334. * Enable Interrupts
  335. */
  336. jwrite32(jme, JME_IENS, INTR_ENABLE);
  337. }
  338. static inline void
  339. jme_stop_irq(struct jme_adapter *jme)
  340. {
  341. /*
  342. * Disable Interrupts
  343. */
  344. jwrite32f(jme, JME_IENC, INTR_ENABLE);
  345. }
  346. static u32
  347. jme_linkstat_from_phy(struct jme_adapter *jme)
  348. {
  349. u32 phylink, bmsr;
  350. phylink = jme_mdio_read(jme->dev, jme->mii_if.phy_id, 17);
  351. bmsr = jme_mdio_read(jme->dev, jme->mii_if.phy_id, MII_BMSR);
  352. if (bmsr & BMSR_ANCOMP)
  353. phylink |= PHY_LINK_AUTONEG_COMPLETE;
  354. return phylink;
  355. }
  356. static inline void
  357. jme_set_phyfifo_5level(struct jme_adapter *jme)
  358. {
  359. jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0004);
  360. }
  361. static inline void
  362. jme_set_phyfifo_8level(struct jme_adapter *jme)
  363. {
  364. jme_mdio_write(jme->dev, jme->mii_if.phy_id, 27, 0x0000);
  365. }
  366. static int
  367. jme_check_link(struct net_device *netdev, int testonly)
  368. {
  369. struct jme_adapter *jme = netdev_priv(netdev);
  370. u32 phylink, cnt = JME_SPDRSV_TIMEOUT, bmcr;
  371. char linkmsg[64];
  372. int rc = 0;
  373. linkmsg[0] = '\0';
  374. if (jme->fpgaver)
  375. phylink = jme_linkstat_from_phy(jme);
  376. else
  377. phylink = jread32(jme, JME_PHY_LINK);
  378. if (phylink & PHY_LINK_UP) {
  379. if (!(phylink & PHY_LINK_AUTONEG_COMPLETE)) {
  380. /*
  381. * If we did not enable AN
  382. * Speed/Duplex Info should be obtained from SMI
  383. */
  384. phylink = PHY_LINK_UP;
  385. bmcr = jme_mdio_read(jme->dev,
  386. jme->mii_if.phy_id,
  387. MII_BMCR);
  388. phylink |= ((bmcr & BMCR_SPEED1000) &&
  389. (bmcr & BMCR_SPEED100) == 0) ?
  390. PHY_LINK_SPEED_1000M :
  391. (bmcr & BMCR_SPEED100) ?
  392. PHY_LINK_SPEED_100M :
  393. PHY_LINK_SPEED_10M;
  394. phylink |= (bmcr & BMCR_FULLDPLX) ?
  395. PHY_LINK_DUPLEX : 0;
  396. strcat(linkmsg, "Forced: ");
  397. } else {
  398. /*
  399. * Keep polling for speed/duplex resolve complete
  400. */
  401. while (!(phylink & PHY_LINK_SPEEDDPU_RESOLVED) &&
  402. --cnt) {
  403. udelay(1);
  404. if (jme->fpgaver)
  405. phylink = jme_linkstat_from_phy(jme);
  406. else
  407. phylink = jread32(jme, JME_PHY_LINK);
  408. }
  409. if (!cnt)
  410. pr_err("Waiting speed resolve timeout\n");
  411. strcat(linkmsg, "ANed: ");
  412. }
  413. if (jme->phylink == phylink) {
  414. rc = 1;
  415. goto out;
  416. }
  417. if (testonly)
  418. goto out;
  419. jme->phylink = phylink;
  420. /*
  421. * The speed/duplex setting of jme->reg_ghc already cleared
  422. * by jme_reset_mac_processor()
  423. */
  424. switch (phylink & PHY_LINK_SPEED_MASK) {
  425. case PHY_LINK_SPEED_10M:
  426. jme->reg_ghc |= GHC_SPEED_10M;
  427. strcat(linkmsg, "10 Mbps, ");
  428. break;
  429. case PHY_LINK_SPEED_100M:
  430. jme->reg_ghc |= GHC_SPEED_100M;
  431. strcat(linkmsg, "100 Mbps, ");
  432. break;
  433. case PHY_LINK_SPEED_1000M:
  434. jme->reg_ghc |= GHC_SPEED_1000M;
  435. strcat(linkmsg, "1000 Mbps, ");
  436. break;
  437. default:
  438. break;
  439. }
  440. if (phylink & PHY_LINK_DUPLEX) {
  441. jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT);
  442. jwrite32(jme, JME_TXTRHD, TXTRHD_FULLDUPLEX);
  443. jme->reg_ghc |= GHC_DPX;
  444. } else {
  445. jwrite32(jme, JME_TXMCS, TXMCS_DEFAULT |
  446. TXMCS_BACKOFF |
  447. TXMCS_CARRIERSENSE |
  448. TXMCS_COLLISION);
  449. jwrite32(jme, JME_TXTRHD, TXTRHD_HALFDUPLEX);
  450. }
  451. jwrite32(jme, JME_GHC, jme->reg_ghc);
  452. if (is_buggy250(jme->pdev->device, jme->chiprev)) {
  453. jme->reg_gpreg1 &= ~(GPREG1_HALFMODEPATCH |
  454. GPREG1_RSSPATCH);
  455. if (!(phylink & PHY_LINK_DUPLEX))
  456. jme->reg_gpreg1 |= GPREG1_HALFMODEPATCH;
  457. switch (phylink & PHY_LINK_SPEED_MASK) {
  458. case PHY_LINK_SPEED_10M:
  459. jme_set_phyfifo_8level(jme);
  460. jme->reg_gpreg1 |= GPREG1_RSSPATCH;
  461. break;
  462. case PHY_LINK_SPEED_100M:
  463. jme_set_phyfifo_5level(jme);
  464. jme->reg_gpreg1 |= GPREG1_RSSPATCH;
  465. break;
  466. case PHY_LINK_SPEED_1000M:
  467. jme_set_phyfifo_8level(jme);
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. jwrite32(jme, JME_GPREG1, jme->reg_gpreg1);
  474. strcat(linkmsg, (phylink & PHY_LINK_DUPLEX) ?
  475. "Full-Duplex, " :
  476. "Half-Duplex, ");
  477. strcat(linkmsg, (phylink & PHY_LINK_MDI_STAT) ?
  478. "MDI-X" :
  479. "MDI");
  480. netif_info(jme, link, jme->dev, "Link is up at %s\n", linkmsg);
  481. netif_carrier_on(netdev);
  482. } else {
  483. if (testonly)
  484. goto out;
  485. netif_info(jme, link, jme->dev, "Link is down\n");
  486. jme->phylink = 0;
  487. netif_carrier_off(netdev);
  488. }
  489. out:
  490. return rc;
  491. }
  492. static int
  493. jme_setup_tx_resources(struct jme_adapter *jme)
  494. {
  495. struct jme_ring *txring = &(jme->txring[0]);
  496. txring->alloc = dma_alloc_coherent(&(jme->pdev->dev),
  497. TX_RING_ALLOC_SIZE(jme->tx_ring_size),
  498. &(txring->dmaalloc),
  499. GFP_ATOMIC);
  500. if (!txring->alloc)
  501. goto err_set_null;
  502. /*
  503. * 16 Bytes align
  504. */
  505. txring->desc = (void *)ALIGN((unsigned long)(txring->alloc),
  506. RING_DESC_ALIGN);
  507. txring->dma = ALIGN(txring->dmaalloc, RING_DESC_ALIGN);
  508. txring->next_to_use = 0;
  509. atomic_set(&txring->next_to_clean, 0);
  510. atomic_set(&txring->nr_free, jme->tx_ring_size);
  511. txring->bufinf = 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_CSUM_MASK);
  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);