jme.c 71 KB

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