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