mv643xx_eth.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310
  1. /*
  2. * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
  3. * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
  4. *
  5. * Based on the 64360 driver from:
  6. * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
  7. * Rabeeh Khoury <rabeeh@marvell.com>
  8. *
  9. * Copyright (C) 2003 PMC-Sierra, Inc.,
  10. * written by Manish Lachwani
  11. *
  12. * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
  13. *
  14. * Copyright (C) 2004-2006 MontaVista Software, Inc.
  15. * Dale Farnsworth <dale@farnsworth.org>
  16. *
  17. * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
  18. * <sjhill@realitydiluted.com>
  19. *
  20. * Copyright (C) 2007-2008 Marvell Semiconductor
  21. * Lennert Buytenhek <buytenh@marvell.com>
  22. *
  23. * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
  24. *
  25. * This program is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU General Public License
  27. * as published by the Free Software Foundation; either version 2
  28. * of the License, or (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  37. */
  38. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39. #include <linux/init.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/in.h>
  42. #include <linux/ip.h>
  43. #include <net/tso.h>
  44. #include <linux/tcp.h>
  45. #include <linux/udp.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/delay.h>
  48. #include <linux/ethtool.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/module.h>
  51. #include <linux/kernel.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/workqueue.h>
  54. #include <linux/phy.h>
  55. #include <linux/mv643xx_eth.h>
  56. #include <linux/io.h>
  57. #include <linux/interrupt.h>
  58. #include <linux/types.h>
  59. #include <linux/slab.h>
  60. #include <linux/clk.h>
  61. #include <linux/of.h>
  62. #include <linux/of_irq.h>
  63. #include <linux/of_net.h>
  64. #include <linux/of_mdio.h>
  65. static char mv643xx_eth_driver_name[] = "mv643xx_eth";
  66. static char mv643xx_eth_driver_version[] = "1.4";
  67. /*
  68. * Registers shared between all ports.
  69. */
  70. #define PHY_ADDR 0x0000
  71. #define WINDOW_BASE(w) (0x0200 + ((w) << 3))
  72. #define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
  73. #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
  74. #define WINDOW_BAR_ENABLE 0x0290
  75. #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
  76. /*
  77. * Main per-port registers. These live at offset 0x0400 for
  78. * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
  79. */
  80. #define PORT_CONFIG 0x0000
  81. #define UNICAST_PROMISCUOUS_MODE 0x00000001
  82. #define PORT_CONFIG_EXT 0x0004
  83. #define MAC_ADDR_LOW 0x0014
  84. #define MAC_ADDR_HIGH 0x0018
  85. #define SDMA_CONFIG 0x001c
  86. #define TX_BURST_SIZE_16_64BIT 0x01000000
  87. #define TX_BURST_SIZE_4_64BIT 0x00800000
  88. #define BLM_TX_NO_SWAP 0x00000020
  89. #define BLM_RX_NO_SWAP 0x00000010
  90. #define RX_BURST_SIZE_16_64BIT 0x00000008
  91. #define RX_BURST_SIZE_4_64BIT 0x00000004
  92. #define PORT_SERIAL_CONTROL 0x003c
  93. #define SET_MII_SPEED_TO_100 0x01000000
  94. #define SET_GMII_SPEED_TO_1000 0x00800000
  95. #define SET_FULL_DUPLEX_MODE 0x00200000
  96. #define MAX_RX_PACKET_9700BYTE 0x000a0000
  97. #define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000
  98. #define DO_NOT_FORCE_LINK_FAIL 0x00000400
  99. #define SERIAL_PORT_CONTROL_RESERVED 0x00000200
  100. #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008
  101. #define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004
  102. #define FORCE_LINK_PASS 0x00000002
  103. #define SERIAL_PORT_ENABLE 0x00000001
  104. #define PORT_STATUS 0x0044
  105. #define TX_FIFO_EMPTY 0x00000400
  106. #define TX_IN_PROGRESS 0x00000080
  107. #define PORT_SPEED_MASK 0x00000030
  108. #define PORT_SPEED_1000 0x00000010
  109. #define PORT_SPEED_100 0x00000020
  110. #define PORT_SPEED_10 0x00000000
  111. #define FLOW_CONTROL_ENABLED 0x00000008
  112. #define FULL_DUPLEX 0x00000004
  113. #define LINK_UP 0x00000002
  114. #define TXQ_COMMAND 0x0048
  115. #define TXQ_FIX_PRIO_CONF 0x004c
  116. #define PORT_SERIAL_CONTROL1 0x004c
  117. #define CLK125_BYPASS_EN 0x00000010
  118. #define TX_BW_RATE 0x0050
  119. #define TX_BW_MTU 0x0058
  120. #define TX_BW_BURST 0x005c
  121. #define INT_CAUSE 0x0060
  122. #define INT_TX_END 0x07f80000
  123. #define INT_TX_END_0 0x00080000
  124. #define INT_RX 0x000003fc
  125. #define INT_RX_0 0x00000004
  126. #define INT_EXT 0x00000002
  127. #define INT_CAUSE_EXT 0x0064
  128. #define INT_EXT_LINK_PHY 0x00110000
  129. #define INT_EXT_TX 0x000000ff
  130. #define INT_MASK 0x0068
  131. #define INT_MASK_EXT 0x006c
  132. #define TX_FIFO_URGENT_THRESHOLD 0x0074
  133. #define RX_DISCARD_FRAME_CNT 0x0084
  134. #define RX_OVERRUN_FRAME_CNT 0x0088
  135. #define TXQ_FIX_PRIO_CONF_MOVED 0x00dc
  136. #define TX_BW_RATE_MOVED 0x00e0
  137. #define TX_BW_MTU_MOVED 0x00e8
  138. #define TX_BW_BURST_MOVED 0x00ec
  139. #define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4))
  140. #define RXQ_COMMAND 0x0280
  141. #define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2))
  142. #define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4))
  143. #define TXQ_BW_CONF(q) (0x0304 + ((q) << 4))
  144. #define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4))
  145. /*
  146. * Misc per-port registers.
  147. */
  148. #define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
  149. #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
  150. #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
  151. #define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
  152. /*
  153. * SDMA configuration register default value.
  154. */
  155. #if defined(__BIG_ENDIAN)
  156. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  157. (RX_BURST_SIZE_4_64BIT | \
  158. TX_BURST_SIZE_4_64BIT)
  159. #elif defined(__LITTLE_ENDIAN)
  160. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  161. (RX_BURST_SIZE_4_64BIT | \
  162. BLM_RX_NO_SWAP | \
  163. BLM_TX_NO_SWAP | \
  164. TX_BURST_SIZE_4_64BIT)
  165. #else
  166. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  167. #endif
  168. /*
  169. * Misc definitions.
  170. */
  171. #define DEFAULT_RX_QUEUE_SIZE 128
  172. #define DEFAULT_TX_QUEUE_SIZE 512
  173. #define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
  174. #define TSO_HEADER_SIZE 128
  175. /* Max number of allowed TCP segments for software TSO */
  176. #define MV643XX_MAX_TSO_SEGS 100
  177. #define MV643XX_MAX_SKB_DESCS (MV643XX_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
  178. #define IS_TSO_HEADER(txq, addr) \
  179. ((addr >= txq->tso_hdrs_dma) && \
  180. (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
  181. #define DESC_DMA_MAP_SINGLE 0
  182. #define DESC_DMA_MAP_PAGE 1
  183. /*
  184. * RX/TX descriptors.
  185. */
  186. #if defined(__BIG_ENDIAN)
  187. struct rx_desc {
  188. u16 byte_cnt; /* Descriptor buffer byte count */
  189. u16 buf_size; /* Buffer size */
  190. u32 cmd_sts; /* Descriptor command status */
  191. u32 next_desc_ptr; /* Next descriptor pointer */
  192. u32 buf_ptr; /* Descriptor buffer pointer */
  193. };
  194. struct tx_desc {
  195. u16 byte_cnt; /* buffer byte count */
  196. u16 l4i_chk; /* CPU provided TCP checksum */
  197. u32 cmd_sts; /* Command/status field */
  198. u32 next_desc_ptr; /* Pointer to next descriptor */
  199. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  200. };
  201. #elif defined(__LITTLE_ENDIAN)
  202. struct rx_desc {
  203. u32 cmd_sts; /* Descriptor command status */
  204. u16 buf_size; /* Buffer size */
  205. u16 byte_cnt; /* Descriptor buffer byte count */
  206. u32 buf_ptr; /* Descriptor buffer pointer */
  207. u32 next_desc_ptr; /* Next descriptor pointer */
  208. };
  209. struct tx_desc {
  210. u32 cmd_sts; /* Command/status field */
  211. u16 l4i_chk; /* CPU provided TCP checksum */
  212. u16 byte_cnt; /* buffer byte count */
  213. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  214. u32 next_desc_ptr; /* Pointer to next descriptor */
  215. };
  216. #else
  217. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  218. #endif
  219. /* RX & TX descriptor command */
  220. #define BUFFER_OWNED_BY_DMA 0x80000000
  221. /* RX & TX descriptor status */
  222. #define ERROR_SUMMARY 0x00000001
  223. /* RX descriptor status */
  224. #define LAYER_4_CHECKSUM_OK 0x40000000
  225. #define RX_ENABLE_INTERRUPT 0x20000000
  226. #define RX_FIRST_DESC 0x08000000
  227. #define RX_LAST_DESC 0x04000000
  228. #define RX_IP_HDR_OK 0x02000000
  229. #define RX_PKT_IS_IPV4 0x01000000
  230. #define RX_PKT_IS_ETHERNETV2 0x00800000
  231. #define RX_PKT_LAYER4_TYPE_MASK 0x00600000
  232. #define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000
  233. #define RX_PKT_IS_VLAN_TAGGED 0x00080000
  234. /* TX descriptor command */
  235. #define TX_ENABLE_INTERRUPT 0x00800000
  236. #define GEN_CRC 0x00400000
  237. #define TX_FIRST_DESC 0x00200000
  238. #define TX_LAST_DESC 0x00100000
  239. #define ZERO_PADDING 0x00080000
  240. #define GEN_IP_V4_CHECKSUM 0x00040000
  241. #define GEN_TCP_UDP_CHECKSUM 0x00020000
  242. #define UDP_FRAME 0x00010000
  243. #define MAC_HDR_EXTRA_4_BYTES 0x00008000
  244. #define GEN_TCP_UDP_CHK_FULL 0x00000400
  245. #define MAC_HDR_EXTRA_8_BYTES 0x00000200
  246. #define TX_IHL_SHIFT 11
  247. /* global *******************************************************************/
  248. struct mv643xx_eth_shared_private {
  249. /*
  250. * Ethernet controller base address.
  251. */
  252. void __iomem *base;
  253. /*
  254. * Per-port MBUS window access register value.
  255. */
  256. u32 win_protect;
  257. /*
  258. * Hardware-specific parameters.
  259. */
  260. int extended_rx_coal_limit;
  261. int tx_bw_control;
  262. int tx_csum_limit;
  263. struct clk *clk;
  264. };
  265. #define TX_BW_CONTROL_ABSENT 0
  266. #define TX_BW_CONTROL_OLD_LAYOUT 1
  267. #define TX_BW_CONTROL_NEW_LAYOUT 2
  268. static int mv643xx_eth_open(struct net_device *dev);
  269. static int mv643xx_eth_stop(struct net_device *dev);
  270. /* per-port *****************************************************************/
  271. struct mib_counters {
  272. u64 good_octets_received;
  273. u32 bad_octets_received;
  274. u32 internal_mac_transmit_err;
  275. u32 good_frames_received;
  276. u32 bad_frames_received;
  277. u32 broadcast_frames_received;
  278. u32 multicast_frames_received;
  279. u32 frames_64_octets;
  280. u32 frames_65_to_127_octets;
  281. u32 frames_128_to_255_octets;
  282. u32 frames_256_to_511_octets;
  283. u32 frames_512_to_1023_octets;
  284. u32 frames_1024_to_max_octets;
  285. u64 good_octets_sent;
  286. u32 good_frames_sent;
  287. u32 excessive_collision;
  288. u32 multicast_frames_sent;
  289. u32 broadcast_frames_sent;
  290. u32 unrec_mac_control_received;
  291. u32 fc_sent;
  292. u32 good_fc_received;
  293. u32 bad_fc_received;
  294. u32 undersize_received;
  295. u32 fragments_received;
  296. u32 oversize_received;
  297. u32 jabber_received;
  298. u32 mac_receive_error;
  299. u32 bad_crc_event;
  300. u32 collision;
  301. u32 late_collision;
  302. /* Non MIB hardware counters */
  303. u32 rx_discard;
  304. u32 rx_overrun;
  305. };
  306. struct rx_queue {
  307. int index;
  308. int rx_ring_size;
  309. int rx_desc_count;
  310. int rx_curr_desc;
  311. int rx_used_desc;
  312. struct rx_desc *rx_desc_area;
  313. dma_addr_t rx_desc_dma;
  314. int rx_desc_area_size;
  315. struct sk_buff **rx_skb;
  316. };
  317. struct tx_queue {
  318. int index;
  319. int tx_ring_size;
  320. int tx_desc_count;
  321. int tx_curr_desc;
  322. int tx_used_desc;
  323. int tx_stop_threshold;
  324. int tx_wake_threshold;
  325. char *tso_hdrs;
  326. dma_addr_t tso_hdrs_dma;
  327. struct tx_desc *tx_desc_area;
  328. char *tx_desc_mapping; /* array to track the type of the dma mapping */
  329. dma_addr_t tx_desc_dma;
  330. int tx_desc_area_size;
  331. struct sk_buff_head tx_skb;
  332. unsigned long tx_packets;
  333. unsigned long tx_bytes;
  334. unsigned long tx_dropped;
  335. };
  336. struct mv643xx_eth_private {
  337. struct mv643xx_eth_shared_private *shared;
  338. void __iomem *base;
  339. int port_num;
  340. struct net_device *dev;
  341. struct timer_list mib_counters_timer;
  342. spinlock_t mib_counters_lock;
  343. struct mib_counters mib_counters;
  344. struct work_struct tx_timeout_task;
  345. struct napi_struct napi;
  346. u32 int_mask;
  347. u8 oom;
  348. u8 work_link;
  349. u8 work_tx;
  350. u8 work_tx_end;
  351. u8 work_rx;
  352. u8 work_rx_refill;
  353. int skb_size;
  354. /*
  355. * RX state.
  356. */
  357. int rx_ring_size;
  358. unsigned long rx_desc_sram_addr;
  359. int rx_desc_sram_size;
  360. int rxq_count;
  361. struct timer_list rx_oom;
  362. struct rx_queue rxq[8];
  363. /*
  364. * TX state.
  365. */
  366. int tx_ring_size;
  367. unsigned long tx_desc_sram_addr;
  368. int tx_desc_sram_size;
  369. int txq_count;
  370. struct tx_queue txq[8];
  371. /*
  372. * Hardware-specific parameters.
  373. */
  374. struct clk *clk;
  375. unsigned int t_clk;
  376. };
  377. /* port register accessors **************************************************/
  378. static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
  379. {
  380. return readl(mp->shared->base + offset);
  381. }
  382. static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
  383. {
  384. return readl(mp->base + offset);
  385. }
  386. static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
  387. {
  388. writel(data, mp->shared->base + offset);
  389. }
  390. static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
  391. {
  392. writel(data, mp->base + offset);
  393. }
  394. /* rxq/txq helper functions *************************************************/
  395. static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
  396. {
  397. return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
  398. }
  399. static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
  400. {
  401. return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
  402. }
  403. static void rxq_enable(struct rx_queue *rxq)
  404. {
  405. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  406. wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
  407. }
  408. static void rxq_disable(struct rx_queue *rxq)
  409. {
  410. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  411. u8 mask = 1 << rxq->index;
  412. wrlp(mp, RXQ_COMMAND, mask << 8);
  413. while (rdlp(mp, RXQ_COMMAND) & mask)
  414. udelay(10);
  415. }
  416. static void txq_reset_hw_ptr(struct tx_queue *txq)
  417. {
  418. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  419. u32 addr;
  420. addr = (u32)txq->tx_desc_dma;
  421. addr += txq->tx_curr_desc * sizeof(struct tx_desc);
  422. wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
  423. }
  424. static void txq_enable(struct tx_queue *txq)
  425. {
  426. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  427. wrlp(mp, TXQ_COMMAND, 1 << txq->index);
  428. }
  429. static void txq_disable(struct tx_queue *txq)
  430. {
  431. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  432. u8 mask = 1 << txq->index;
  433. wrlp(mp, TXQ_COMMAND, mask << 8);
  434. while (rdlp(mp, TXQ_COMMAND) & mask)
  435. udelay(10);
  436. }
  437. static void txq_maybe_wake(struct tx_queue *txq)
  438. {
  439. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  440. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  441. if (netif_tx_queue_stopped(nq)) {
  442. __netif_tx_lock(nq, smp_processor_id());
  443. if (txq->tx_desc_count <= txq->tx_wake_threshold)
  444. netif_tx_wake_queue(nq);
  445. __netif_tx_unlock(nq);
  446. }
  447. }
  448. static int rxq_process(struct rx_queue *rxq, int budget)
  449. {
  450. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  451. struct net_device_stats *stats = &mp->dev->stats;
  452. int rx;
  453. rx = 0;
  454. while (rx < budget && rxq->rx_desc_count) {
  455. struct rx_desc *rx_desc;
  456. unsigned int cmd_sts;
  457. struct sk_buff *skb;
  458. u16 byte_cnt;
  459. rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
  460. cmd_sts = rx_desc->cmd_sts;
  461. if (cmd_sts & BUFFER_OWNED_BY_DMA)
  462. break;
  463. rmb();
  464. skb = rxq->rx_skb[rxq->rx_curr_desc];
  465. rxq->rx_skb[rxq->rx_curr_desc] = NULL;
  466. rxq->rx_curr_desc++;
  467. if (rxq->rx_curr_desc == rxq->rx_ring_size)
  468. rxq->rx_curr_desc = 0;
  469. dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
  470. rx_desc->buf_size, DMA_FROM_DEVICE);
  471. rxq->rx_desc_count--;
  472. rx++;
  473. mp->work_rx_refill |= 1 << rxq->index;
  474. byte_cnt = rx_desc->byte_cnt;
  475. /*
  476. * Update statistics.
  477. *
  478. * Note that the descriptor byte count includes 2 dummy
  479. * bytes automatically inserted by the hardware at the
  480. * start of the packet (which we don't count), and a 4
  481. * byte CRC at the end of the packet (which we do count).
  482. */
  483. stats->rx_packets++;
  484. stats->rx_bytes += byte_cnt - 2;
  485. /*
  486. * In case we received a packet without first / last bits
  487. * on, or the error summary bit is set, the packet needs
  488. * to be dropped.
  489. */
  490. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
  491. != (RX_FIRST_DESC | RX_LAST_DESC))
  492. goto err;
  493. /*
  494. * The -4 is for the CRC in the trailer of the
  495. * received packet
  496. */
  497. skb_put(skb, byte_cnt - 2 - 4);
  498. if (cmd_sts & LAYER_4_CHECKSUM_OK)
  499. skb->ip_summed = CHECKSUM_UNNECESSARY;
  500. skb->protocol = eth_type_trans(skb, mp->dev);
  501. napi_gro_receive(&mp->napi, skb);
  502. continue;
  503. err:
  504. stats->rx_dropped++;
  505. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  506. (RX_FIRST_DESC | RX_LAST_DESC)) {
  507. if (net_ratelimit())
  508. netdev_err(mp->dev,
  509. "received packet spanning multiple descriptors\n");
  510. }
  511. if (cmd_sts & ERROR_SUMMARY)
  512. stats->rx_errors++;
  513. dev_kfree_skb(skb);
  514. }
  515. if (rx < budget)
  516. mp->work_rx &= ~(1 << rxq->index);
  517. return rx;
  518. }
  519. static int rxq_refill(struct rx_queue *rxq, int budget)
  520. {
  521. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  522. int refilled;
  523. refilled = 0;
  524. while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
  525. struct sk_buff *skb;
  526. int rx;
  527. struct rx_desc *rx_desc;
  528. int size;
  529. skb = netdev_alloc_skb(mp->dev, mp->skb_size);
  530. if (skb == NULL) {
  531. mp->oom = 1;
  532. goto oom;
  533. }
  534. if (SKB_DMA_REALIGN)
  535. skb_reserve(skb, SKB_DMA_REALIGN);
  536. refilled++;
  537. rxq->rx_desc_count++;
  538. rx = rxq->rx_used_desc++;
  539. if (rxq->rx_used_desc == rxq->rx_ring_size)
  540. rxq->rx_used_desc = 0;
  541. rx_desc = rxq->rx_desc_area + rx;
  542. size = skb_end_pointer(skb) - skb->data;
  543. rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
  544. skb->data, size,
  545. DMA_FROM_DEVICE);
  546. rx_desc->buf_size = size;
  547. rxq->rx_skb[rx] = skb;
  548. wmb();
  549. rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
  550. wmb();
  551. /*
  552. * The hardware automatically prepends 2 bytes of
  553. * dummy data to each received packet, so that the
  554. * IP header ends up 16-byte aligned.
  555. */
  556. skb_reserve(skb, 2);
  557. }
  558. if (refilled < budget)
  559. mp->work_rx_refill &= ~(1 << rxq->index);
  560. oom:
  561. return refilled;
  562. }
  563. /* tx ***********************************************************************/
  564. static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
  565. {
  566. int frag;
  567. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  568. const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  569. if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
  570. return 1;
  571. }
  572. return 0;
  573. }
  574. static inline __be16 sum16_as_be(__sum16 sum)
  575. {
  576. return (__force __be16)sum;
  577. }
  578. static int skb_tx_csum(struct mv643xx_eth_private *mp, struct sk_buff *skb,
  579. u16 *l4i_chk, u32 *command, int length)
  580. {
  581. int ret;
  582. u32 cmd = 0;
  583. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  584. int hdr_len;
  585. int tag_bytes;
  586. BUG_ON(skb->protocol != htons(ETH_P_IP) &&
  587. skb->protocol != htons(ETH_P_8021Q));
  588. hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
  589. tag_bytes = hdr_len - ETH_HLEN;
  590. if (length - hdr_len > mp->shared->tx_csum_limit ||
  591. unlikely(tag_bytes & ~12)) {
  592. ret = skb_checksum_help(skb);
  593. if (!ret)
  594. goto no_csum;
  595. return ret;
  596. }
  597. if (tag_bytes & 4)
  598. cmd |= MAC_HDR_EXTRA_4_BYTES;
  599. if (tag_bytes & 8)
  600. cmd |= MAC_HDR_EXTRA_8_BYTES;
  601. cmd |= GEN_TCP_UDP_CHECKSUM | GEN_TCP_UDP_CHK_FULL |
  602. GEN_IP_V4_CHECKSUM |
  603. ip_hdr(skb)->ihl << TX_IHL_SHIFT;
  604. /* TODO: Revisit this. With the usage of GEN_TCP_UDP_CHK_FULL
  605. * it seems we don't need to pass the initial checksum. */
  606. switch (ip_hdr(skb)->protocol) {
  607. case IPPROTO_UDP:
  608. cmd |= UDP_FRAME;
  609. *l4i_chk = 0;
  610. break;
  611. case IPPROTO_TCP:
  612. *l4i_chk = 0;
  613. break;
  614. default:
  615. WARN(1, "protocol not supported");
  616. }
  617. } else {
  618. no_csum:
  619. /* Errata BTS #50, IHL must be 5 if no HW checksum */
  620. cmd |= 5 << TX_IHL_SHIFT;
  621. }
  622. *command = cmd;
  623. return 0;
  624. }
  625. static inline int
  626. txq_put_data_tso(struct net_device *dev, struct tx_queue *txq,
  627. struct sk_buff *skb, char *data, int length,
  628. bool last_tcp, bool is_last)
  629. {
  630. int tx_index;
  631. u32 cmd_sts;
  632. struct tx_desc *desc;
  633. tx_index = txq->tx_curr_desc++;
  634. if (txq->tx_curr_desc == txq->tx_ring_size)
  635. txq->tx_curr_desc = 0;
  636. desc = &txq->tx_desc_area[tx_index];
  637. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
  638. desc->l4i_chk = 0;
  639. desc->byte_cnt = length;
  640. if (length <= 8 && (uintptr_t)data & 0x7) {
  641. /* Copy unaligned small data fragment to TSO header data area */
  642. memcpy(txq->tso_hdrs + tx_index * TSO_HEADER_SIZE,
  643. data, length);
  644. desc->buf_ptr = txq->tso_hdrs_dma
  645. + tx_index * TSO_HEADER_SIZE;
  646. } else {
  647. /* Alignment is okay, map buffer and hand off to hardware */
  648. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
  649. desc->buf_ptr = dma_map_single(dev->dev.parent, data,
  650. length, DMA_TO_DEVICE);
  651. if (unlikely(dma_mapping_error(dev->dev.parent,
  652. desc->buf_ptr))) {
  653. WARN(1, "dma_map_single failed!\n");
  654. return -ENOMEM;
  655. }
  656. }
  657. cmd_sts = BUFFER_OWNED_BY_DMA;
  658. if (last_tcp) {
  659. /* last descriptor in the TCP packet */
  660. cmd_sts |= ZERO_PADDING | TX_LAST_DESC;
  661. /* last descriptor in SKB */
  662. if (is_last)
  663. cmd_sts |= TX_ENABLE_INTERRUPT;
  664. }
  665. desc->cmd_sts = cmd_sts;
  666. return 0;
  667. }
  668. static inline void
  669. txq_put_hdr_tso(struct sk_buff *skb, struct tx_queue *txq, int length,
  670. u32 *first_cmd_sts, bool first_desc)
  671. {
  672. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  673. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  674. int tx_index;
  675. struct tx_desc *desc;
  676. int ret;
  677. u32 cmd_csum = 0;
  678. u16 l4i_chk = 0;
  679. u32 cmd_sts;
  680. tx_index = txq->tx_curr_desc;
  681. desc = &txq->tx_desc_area[tx_index];
  682. ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_csum, length);
  683. if (ret)
  684. WARN(1, "failed to prepare checksum!");
  685. /* Should we set this? Can't use the value from skb_tx_csum()
  686. * as it's not the correct initial L4 checksum to use. */
  687. desc->l4i_chk = 0;
  688. desc->byte_cnt = hdr_len;
  689. desc->buf_ptr = txq->tso_hdrs_dma +
  690. txq->tx_curr_desc * TSO_HEADER_SIZE;
  691. cmd_sts = cmd_csum | BUFFER_OWNED_BY_DMA | TX_FIRST_DESC |
  692. GEN_CRC;
  693. /* Defer updating the first command descriptor until all
  694. * following descriptors have been written.
  695. */
  696. if (first_desc)
  697. *first_cmd_sts = cmd_sts;
  698. else
  699. desc->cmd_sts = cmd_sts;
  700. txq->tx_curr_desc++;
  701. if (txq->tx_curr_desc == txq->tx_ring_size)
  702. txq->tx_curr_desc = 0;
  703. }
  704. static int txq_submit_tso(struct tx_queue *txq, struct sk_buff *skb,
  705. struct net_device *dev)
  706. {
  707. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  708. int total_len, data_left, ret;
  709. int desc_count = 0;
  710. struct tso_t tso;
  711. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  712. struct tx_desc *first_tx_desc;
  713. u32 first_cmd_sts = 0;
  714. /* Count needed descriptors */
  715. if ((txq->tx_desc_count + tso_count_descs(skb)) >= txq->tx_ring_size) {
  716. netdev_dbg(dev, "not enough descriptors for TSO!\n");
  717. return -EBUSY;
  718. }
  719. first_tx_desc = &txq->tx_desc_area[txq->tx_curr_desc];
  720. /* Initialize the TSO handler, and prepare the first payload */
  721. tso_start(skb, &tso);
  722. total_len = skb->len - hdr_len;
  723. while (total_len > 0) {
  724. bool first_desc = (desc_count == 0);
  725. char *hdr;
  726. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  727. total_len -= data_left;
  728. desc_count++;
  729. /* prepare packet headers: MAC + IP + TCP */
  730. hdr = txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE;
  731. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  732. txq_put_hdr_tso(skb, txq, data_left, &first_cmd_sts,
  733. first_desc);
  734. while (data_left > 0) {
  735. int size;
  736. desc_count++;
  737. size = min_t(int, tso.size, data_left);
  738. ret = txq_put_data_tso(dev, txq, skb, tso.data, size,
  739. size == data_left,
  740. total_len == 0);
  741. if (ret)
  742. goto err_release;
  743. data_left -= size;
  744. tso_build_data(skb, &tso, size);
  745. }
  746. }
  747. __skb_queue_tail(&txq->tx_skb, skb);
  748. skb_tx_timestamp(skb);
  749. /* ensure all other descriptors are written before first cmd_sts */
  750. wmb();
  751. first_tx_desc->cmd_sts = first_cmd_sts;
  752. /* clear TX_END status */
  753. mp->work_tx_end &= ~(1 << txq->index);
  754. /* ensure all descriptors are written before poking hardware */
  755. wmb();
  756. txq_enable(txq);
  757. txq->tx_desc_count += desc_count;
  758. return 0;
  759. err_release:
  760. /* TODO: Release all used data descriptors; header descriptors must not
  761. * be DMA-unmapped.
  762. */
  763. return ret;
  764. }
  765. static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
  766. {
  767. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  768. int nr_frags = skb_shinfo(skb)->nr_frags;
  769. int frag;
  770. for (frag = 0; frag < nr_frags; frag++) {
  771. skb_frag_t *this_frag;
  772. int tx_index;
  773. struct tx_desc *desc;
  774. this_frag = &skb_shinfo(skb)->frags[frag];
  775. tx_index = txq->tx_curr_desc++;
  776. if (txq->tx_curr_desc == txq->tx_ring_size)
  777. txq->tx_curr_desc = 0;
  778. desc = &txq->tx_desc_area[tx_index];
  779. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_PAGE;
  780. /*
  781. * The last fragment will generate an interrupt
  782. * which will free the skb on TX completion.
  783. */
  784. if (frag == nr_frags - 1) {
  785. desc->cmd_sts = BUFFER_OWNED_BY_DMA |
  786. ZERO_PADDING | TX_LAST_DESC |
  787. TX_ENABLE_INTERRUPT;
  788. } else {
  789. desc->cmd_sts = BUFFER_OWNED_BY_DMA;
  790. }
  791. desc->l4i_chk = 0;
  792. desc->byte_cnt = skb_frag_size(this_frag);
  793. desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
  794. this_frag, 0, desc->byte_cnt,
  795. DMA_TO_DEVICE);
  796. }
  797. }
  798. static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb,
  799. struct net_device *dev)
  800. {
  801. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  802. int nr_frags = skb_shinfo(skb)->nr_frags;
  803. int tx_index;
  804. struct tx_desc *desc;
  805. u32 cmd_sts;
  806. u16 l4i_chk;
  807. int length, ret;
  808. cmd_sts = 0;
  809. l4i_chk = 0;
  810. if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
  811. if (net_ratelimit())
  812. netdev_err(dev, "tx queue full?!\n");
  813. return -EBUSY;
  814. }
  815. ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_sts, skb->len);
  816. if (ret)
  817. return ret;
  818. cmd_sts |= TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
  819. tx_index = txq->tx_curr_desc++;
  820. if (txq->tx_curr_desc == txq->tx_ring_size)
  821. txq->tx_curr_desc = 0;
  822. desc = &txq->tx_desc_area[tx_index];
  823. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
  824. if (nr_frags) {
  825. txq_submit_frag_skb(txq, skb);
  826. length = skb_headlen(skb);
  827. } else {
  828. cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
  829. length = skb->len;
  830. }
  831. desc->l4i_chk = l4i_chk;
  832. desc->byte_cnt = length;
  833. desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
  834. length, DMA_TO_DEVICE);
  835. __skb_queue_tail(&txq->tx_skb, skb);
  836. skb_tx_timestamp(skb);
  837. /* ensure all other descriptors are written before first cmd_sts */
  838. wmb();
  839. desc->cmd_sts = cmd_sts;
  840. /* clear TX_END status */
  841. mp->work_tx_end &= ~(1 << txq->index);
  842. /* ensure all descriptors are written before poking hardware */
  843. wmb();
  844. txq_enable(txq);
  845. txq->tx_desc_count += nr_frags + 1;
  846. return 0;
  847. }
  848. static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  849. {
  850. struct mv643xx_eth_private *mp = netdev_priv(dev);
  851. int length, queue, ret;
  852. struct tx_queue *txq;
  853. struct netdev_queue *nq;
  854. queue = skb_get_queue_mapping(skb);
  855. txq = mp->txq + queue;
  856. nq = netdev_get_tx_queue(dev, queue);
  857. if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
  858. netdev_printk(KERN_DEBUG, dev,
  859. "failed to linearize skb with tiny unaligned fragment\n");
  860. return NETDEV_TX_BUSY;
  861. }
  862. length = skb->len;
  863. if (skb_is_gso(skb))
  864. ret = txq_submit_tso(txq, skb, dev);
  865. else
  866. ret = txq_submit_skb(txq, skb, dev);
  867. if (!ret) {
  868. txq->tx_bytes += length;
  869. txq->tx_packets++;
  870. if (txq->tx_desc_count >= txq->tx_stop_threshold)
  871. netif_tx_stop_queue(nq);
  872. } else {
  873. txq->tx_dropped++;
  874. dev_kfree_skb_any(skb);
  875. }
  876. return NETDEV_TX_OK;
  877. }
  878. /* tx napi ******************************************************************/
  879. static void txq_kick(struct tx_queue *txq)
  880. {
  881. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  882. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  883. u32 hw_desc_ptr;
  884. u32 expected_ptr;
  885. __netif_tx_lock(nq, smp_processor_id());
  886. if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
  887. goto out;
  888. hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
  889. expected_ptr = (u32)txq->tx_desc_dma +
  890. txq->tx_curr_desc * sizeof(struct tx_desc);
  891. if (hw_desc_ptr != expected_ptr)
  892. txq_enable(txq);
  893. out:
  894. __netif_tx_unlock(nq);
  895. mp->work_tx_end &= ~(1 << txq->index);
  896. }
  897. static int txq_reclaim(struct tx_queue *txq, int budget, int force)
  898. {
  899. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  900. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  901. int reclaimed;
  902. __netif_tx_lock_bh(nq);
  903. reclaimed = 0;
  904. while (reclaimed < budget && txq->tx_desc_count > 0) {
  905. int tx_index;
  906. struct tx_desc *desc;
  907. u32 cmd_sts;
  908. char desc_dma_map;
  909. tx_index = txq->tx_used_desc;
  910. desc = &txq->tx_desc_area[tx_index];
  911. desc_dma_map = txq->tx_desc_mapping[tx_index];
  912. cmd_sts = desc->cmd_sts;
  913. if (cmd_sts & BUFFER_OWNED_BY_DMA) {
  914. if (!force)
  915. break;
  916. desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
  917. }
  918. txq->tx_used_desc = tx_index + 1;
  919. if (txq->tx_used_desc == txq->tx_ring_size)
  920. txq->tx_used_desc = 0;
  921. reclaimed++;
  922. txq->tx_desc_count--;
  923. if (!IS_TSO_HEADER(txq, desc->buf_ptr)) {
  924. if (desc_dma_map == DESC_DMA_MAP_PAGE)
  925. dma_unmap_page(mp->dev->dev.parent,
  926. desc->buf_ptr,
  927. desc->byte_cnt,
  928. DMA_TO_DEVICE);
  929. else
  930. dma_unmap_single(mp->dev->dev.parent,
  931. desc->buf_ptr,
  932. desc->byte_cnt,
  933. DMA_TO_DEVICE);
  934. }
  935. if (cmd_sts & TX_ENABLE_INTERRUPT) {
  936. struct sk_buff *skb = __skb_dequeue(&txq->tx_skb);
  937. if (!WARN_ON(!skb))
  938. dev_kfree_skb(skb);
  939. }
  940. if (cmd_sts & ERROR_SUMMARY) {
  941. netdev_info(mp->dev, "tx error\n");
  942. mp->dev->stats.tx_errors++;
  943. }
  944. }
  945. __netif_tx_unlock_bh(nq);
  946. if (reclaimed < budget)
  947. mp->work_tx &= ~(1 << txq->index);
  948. return reclaimed;
  949. }
  950. /* tx rate control **********************************************************/
  951. /*
  952. * Set total maximum TX rate (shared by all TX queues for this port)
  953. * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
  954. */
  955. static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
  956. {
  957. int token_rate;
  958. int mtu;
  959. int bucket_size;
  960. token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
  961. if (token_rate > 1023)
  962. token_rate = 1023;
  963. mtu = (mp->dev->mtu + 255) >> 8;
  964. if (mtu > 63)
  965. mtu = 63;
  966. bucket_size = (burst + 255) >> 8;
  967. if (bucket_size > 65535)
  968. bucket_size = 65535;
  969. switch (mp->shared->tx_bw_control) {
  970. case TX_BW_CONTROL_OLD_LAYOUT:
  971. wrlp(mp, TX_BW_RATE, token_rate);
  972. wrlp(mp, TX_BW_MTU, mtu);
  973. wrlp(mp, TX_BW_BURST, bucket_size);
  974. break;
  975. case TX_BW_CONTROL_NEW_LAYOUT:
  976. wrlp(mp, TX_BW_RATE_MOVED, token_rate);
  977. wrlp(mp, TX_BW_MTU_MOVED, mtu);
  978. wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
  979. break;
  980. }
  981. }
  982. static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
  983. {
  984. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  985. int token_rate;
  986. int bucket_size;
  987. token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
  988. if (token_rate > 1023)
  989. token_rate = 1023;
  990. bucket_size = (burst + 255) >> 8;
  991. if (bucket_size > 65535)
  992. bucket_size = 65535;
  993. wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
  994. wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
  995. }
  996. static void txq_set_fixed_prio_mode(struct tx_queue *txq)
  997. {
  998. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  999. int off;
  1000. u32 val;
  1001. /*
  1002. * Turn on fixed priority mode.
  1003. */
  1004. off = 0;
  1005. switch (mp->shared->tx_bw_control) {
  1006. case TX_BW_CONTROL_OLD_LAYOUT:
  1007. off = TXQ_FIX_PRIO_CONF;
  1008. break;
  1009. case TX_BW_CONTROL_NEW_LAYOUT:
  1010. off = TXQ_FIX_PRIO_CONF_MOVED;
  1011. break;
  1012. }
  1013. if (off) {
  1014. val = rdlp(mp, off);
  1015. val |= 1 << txq->index;
  1016. wrlp(mp, off, val);
  1017. }
  1018. }
  1019. /* mii management interface *************************************************/
  1020. static void mv643xx_eth_adjust_link(struct net_device *dev)
  1021. {
  1022. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1023. u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  1024. u32 autoneg_disable = FORCE_LINK_PASS |
  1025. DISABLE_AUTO_NEG_SPEED_GMII |
  1026. DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
  1027. DISABLE_AUTO_NEG_FOR_DUPLEX;
  1028. if (dev->phydev->autoneg == AUTONEG_ENABLE) {
  1029. /* enable auto negotiation */
  1030. pscr &= ~autoneg_disable;
  1031. goto out_write;
  1032. }
  1033. pscr |= autoneg_disable;
  1034. if (dev->phydev->speed == SPEED_1000) {
  1035. /* force gigabit, half duplex not supported */
  1036. pscr |= SET_GMII_SPEED_TO_1000;
  1037. pscr |= SET_FULL_DUPLEX_MODE;
  1038. goto out_write;
  1039. }
  1040. pscr &= ~SET_GMII_SPEED_TO_1000;
  1041. if (dev->phydev->speed == SPEED_100)
  1042. pscr |= SET_MII_SPEED_TO_100;
  1043. else
  1044. pscr &= ~SET_MII_SPEED_TO_100;
  1045. if (dev->phydev->duplex == DUPLEX_FULL)
  1046. pscr |= SET_FULL_DUPLEX_MODE;
  1047. else
  1048. pscr &= ~SET_FULL_DUPLEX_MODE;
  1049. out_write:
  1050. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1051. }
  1052. /* statistics ***************************************************************/
  1053. static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
  1054. {
  1055. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1056. struct net_device_stats *stats = &dev->stats;
  1057. unsigned long tx_packets = 0;
  1058. unsigned long tx_bytes = 0;
  1059. unsigned long tx_dropped = 0;
  1060. int i;
  1061. for (i = 0; i < mp->txq_count; i++) {
  1062. struct tx_queue *txq = mp->txq + i;
  1063. tx_packets += txq->tx_packets;
  1064. tx_bytes += txq->tx_bytes;
  1065. tx_dropped += txq->tx_dropped;
  1066. }
  1067. stats->tx_packets = tx_packets;
  1068. stats->tx_bytes = tx_bytes;
  1069. stats->tx_dropped = tx_dropped;
  1070. return stats;
  1071. }
  1072. static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
  1073. {
  1074. return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
  1075. }
  1076. static void mib_counters_clear(struct mv643xx_eth_private *mp)
  1077. {
  1078. int i;
  1079. for (i = 0; i < 0x80; i += 4)
  1080. mib_read(mp, i);
  1081. /* Clear non MIB hw counters also */
  1082. rdlp(mp, RX_DISCARD_FRAME_CNT);
  1083. rdlp(mp, RX_OVERRUN_FRAME_CNT);
  1084. }
  1085. static void mib_counters_update(struct mv643xx_eth_private *mp)
  1086. {
  1087. struct mib_counters *p = &mp->mib_counters;
  1088. spin_lock_bh(&mp->mib_counters_lock);
  1089. p->good_octets_received += mib_read(mp, 0x00);
  1090. p->bad_octets_received += mib_read(mp, 0x08);
  1091. p->internal_mac_transmit_err += mib_read(mp, 0x0c);
  1092. p->good_frames_received += mib_read(mp, 0x10);
  1093. p->bad_frames_received += mib_read(mp, 0x14);
  1094. p->broadcast_frames_received += mib_read(mp, 0x18);
  1095. p->multicast_frames_received += mib_read(mp, 0x1c);
  1096. p->frames_64_octets += mib_read(mp, 0x20);
  1097. p->frames_65_to_127_octets += mib_read(mp, 0x24);
  1098. p->frames_128_to_255_octets += mib_read(mp, 0x28);
  1099. p->frames_256_to_511_octets += mib_read(mp, 0x2c);
  1100. p->frames_512_to_1023_octets += mib_read(mp, 0x30);
  1101. p->frames_1024_to_max_octets += mib_read(mp, 0x34);
  1102. p->good_octets_sent += mib_read(mp, 0x38);
  1103. p->good_frames_sent += mib_read(mp, 0x40);
  1104. p->excessive_collision += mib_read(mp, 0x44);
  1105. p->multicast_frames_sent += mib_read(mp, 0x48);
  1106. p->broadcast_frames_sent += mib_read(mp, 0x4c);
  1107. p->unrec_mac_control_received += mib_read(mp, 0x50);
  1108. p->fc_sent += mib_read(mp, 0x54);
  1109. p->good_fc_received += mib_read(mp, 0x58);
  1110. p->bad_fc_received += mib_read(mp, 0x5c);
  1111. p->undersize_received += mib_read(mp, 0x60);
  1112. p->fragments_received += mib_read(mp, 0x64);
  1113. p->oversize_received += mib_read(mp, 0x68);
  1114. p->jabber_received += mib_read(mp, 0x6c);
  1115. p->mac_receive_error += mib_read(mp, 0x70);
  1116. p->bad_crc_event += mib_read(mp, 0x74);
  1117. p->collision += mib_read(mp, 0x78);
  1118. p->late_collision += mib_read(mp, 0x7c);
  1119. /* Non MIB hardware counters */
  1120. p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
  1121. p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
  1122. spin_unlock_bh(&mp->mib_counters_lock);
  1123. }
  1124. static void mib_counters_timer_wrapper(unsigned long _mp)
  1125. {
  1126. struct mv643xx_eth_private *mp = (void *)_mp;
  1127. mib_counters_update(mp);
  1128. mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
  1129. }
  1130. /* interrupt coalescing *****************************************************/
  1131. /*
  1132. * Hardware coalescing parameters are set in units of 64 t_clk
  1133. * cycles. I.e.:
  1134. *
  1135. * coal_delay_in_usec = 64000000 * register_value / t_clk_rate
  1136. *
  1137. * register_value = coal_delay_in_usec * t_clk_rate / 64000000
  1138. *
  1139. * In the ->set*() methods, we round the computed register value
  1140. * to the nearest integer.
  1141. */
  1142. static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
  1143. {
  1144. u32 val = rdlp(mp, SDMA_CONFIG);
  1145. u64 temp;
  1146. if (mp->shared->extended_rx_coal_limit)
  1147. temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
  1148. else
  1149. temp = (val & 0x003fff00) >> 8;
  1150. temp *= 64000000;
  1151. temp += mp->t_clk / 2;
  1152. do_div(temp, mp->t_clk);
  1153. return (unsigned int)temp;
  1154. }
  1155. static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
  1156. {
  1157. u64 temp;
  1158. u32 val;
  1159. temp = (u64)usec * mp->t_clk;
  1160. temp += 31999999;
  1161. do_div(temp, 64000000);
  1162. val = rdlp(mp, SDMA_CONFIG);
  1163. if (mp->shared->extended_rx_coal_limit) {
  1164. if (temp > 0xffff)
  1165. temp = 0xffff;
  1166. val &= ~0x023fff80;
  1167. val |= (temp & 0x8000) << 10;
  1168. val |= (temp & 0x7fff) << 7;
  1169. } else {
  1170. if (temp > 0x3fff)
  1171. temp = 0x3fff;
  1172. val &= ~0x003fff00;
  1173. val |= (temp & 0x3fff) << 8;
  1174. }
  1175. wrlp(mp, SDMA_CONFIG, val);
  1176. }
  1177. static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
  1178. {
  1179. u64 temp;
  1180. temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
  1181. temp *= 64000000;
  1182. temp += mp->t_clk / 2;
  1183. do_div(temp, mp->t_clk);
  1184. return (unsigned int)temp;
  1185. }
  1186. static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
  1187. {
  1188. u64 temp;
  1189. temp = (u64)usec * mp->t_clk;
  1190. temp += 31999999;
  1191. do_div(temp, 64000000);
  1192. if (temp > 0x3fff)
  1193. temp = 0x3fff;
  1194. wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
  1195. }
  1196. /* ethtool ******************************************************************/
  1197. struct mv643xx_eth_stats {
  1198. char stat_string[ETH_GSTRING_LEN];
  1199. int sizeof_stat;
  1200. int netdev_off;
  1201. int mp_off;
  1202. };
  1203. #define SSTAT(m) \
  1204. { #m, FIELD_SIZEOF(struct net_device_stats, m), \
  1205. offsetof(struct net_device, stats.m), -1 }
  1206. #define MIBSTAT(m) \
  1207. { #m, FIELD_SIZEOF(struct mib_counters, m), \
  1208. -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
  1209. static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
  1210. SSTAT(rx_packets),
  1211. SSTAT(tx_packets),
  1212. SSTAT(rx_bytes),
  1213. SSTAT(tx_bytes),
  1214. SSTAT(rx_errors),
  1215. SSTAT(tx_errors),
  1216. SSTAT(rx_dropped),
  1217. SSTAT(tx_dropped),
  1218. MIBSTAT(good_octets_received),
  1219. MIBSTAT(bad_octets_received),
  1220. MIBSTAT(internal_mac_transmit_err),
  1221. MIBSTAT(good_frames_received),
  1222. MIBSTAT(bad_frames_received),
  1223. MIBSTAT(broadcast_frames_received),
  1224. MIBSTAT(multicast_frames_received),
  1225. MIBSTAT(frames_64_octets),
  1226. MIBSTAT(frames_65_to_127_octets),
  1227. MIBSTAT(frames_128_to_255_octets),
  1228. MIBSTAT(frames_256_to_511_octets),
  1229. MIBSTAT(frames_512_to_1023_octets),
  1230. MIBSTAT(frames_1024_to_max_octets),
  1231. MIBSTAT(good_octets_sent),
  1232. MIBSTAT(good_frames_sent),
  1233. MIBSTAT(excessive_collision),
  1234. MIBSTAT(multicast_frames_sent),
  1235. MIBSTAT(broadcast_frames_sent),
  1236. MIBSTAT(unrec_mac_control_received),
  1237. MIBSTAT(fc_sent),
  1238. MIBSTAT(good_fc_received),
  1239. MIBSTAT(bad_fc_received),
  1240. MIBSTAT(undersize_received),
  1241. MIBSTAT(fragments_received),
  1242. MIBSTAT(oversize_received),
  1243. MIBSTAT(jabber_received),
  1244. MIBSTAT(mac_receive_error),
  1245. MIBSTAT(bad_crc_event),
  1246. MIBSTAT(collision),
  1247. MIBSTAT(late_collision),
  1248. MIBSTAT(rx_discard),
  1249. MIBSTAT(rx_overrun),
  1250. };
  1251. static int
  1252. mv643xx_eth_get_link_ksettings_phy(struct mv643xx_eth_private *mp,
  1253. struct ethtool_link_ksettings *cmd)
  1254. {
  1255. struct net_device *dev = mp->dev;
  1256. int err;
  1257. u32 supported, advertising;
  1258. err = phy_ethtool_ksettings_get(dev->phydev, cmd);
  1259. /*
  1260. * The MAC does not support 1000baseT_Half.
  1261. */
  1262. ethtool_convert_link_mode_to_legacy_u32(&supported,
  1263. cmd->link_modes.supported);
  1264. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  1265. cmd->link_modes.advertising);
  1266. supported &= ~SUPPORTED_1000baseT_Half;
  1267. advertising &= ~ADVERTISED_1000baseT_Half;
  1268. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  1269. supported);
  1270. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  1271. advertising);
  1272. return err;
  1273. }
  1274. static int
  1275. mv643xx_eth_get_link_ksettings_phyless(struct mv643xx_eth_private *mp,
  1276. struct ethtool_link_ksettings *cmd)
  1277. {
  1278. u32 port_status;
  1279. u32 supported, advertising;
  1280. port_status = rdlp(mp, PORT_STATUS);
  1281. supported = SUPPORTED_MII;
  1282. advertising = ADVERTISED_MII;
  1283. switch (port_status & PORT_SPEED_MASK) {
  1284. case PORT_SPEED_10:
  1285. cmd->base.speed = SPEED_10;
  1286. break;
  1287. case PORT_SPEED_100:
  1288. cmd->base.speed = SPEED_100;
  1289. break;
  1290. case PORT_SPEED_1000:
  1291. cmd->base.speed = SPEED_1000;
  1292. break;
  1293. default:
  1294. cmd->base.speed = -1;
  1295. break;
  1296. }
  1297. cmd->base.duplex = (port_status & FULL_DUPLEX) ?
  1298. DUPLEX_FULL : DUPLEX_HALF;
  1299. cmd->base.port = PORT_MII;
  1300. cmd->base.phy_address = 0;
  1301. cmd->base.autoneg = AUTONEG_DISABLE;
  1302. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  1303. supported);
  1304. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  1305. advertising);
  1306. return 0;
  1307. }
  1308. static void
  1309. mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1310. {
  1311. wol->supported = 0;
  1312. wol->wolopts = 0;
  1313. if (dev->phydev)
  1314. phy_ethtool_get_wol(dev->phydev, wol);
  1315. }
  1316. static int
  1317. mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1318. {
  1319. int err;
  1320. if (!dev->phydev)
  1321. return -EOPNOTSUPP;
  1322. err = phy_ethtool_set_wol(dev->phydev, wol);
  1323. /* Given that mv643xx_eth works without the marvell-specific PHY driver,
  1324. * this debugging hint is useful to have.
  1325. */
  1326. if (err == -EOPNOTSUPP)
  1327. netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
  1328. return err;
  1329. }
  1330. static int
  1331. mv643xx_eth_get_link_ksettings(struct net_device *dev,
  1332. struct ethtool_link_ksettings *cmd)
  1333. {
  1334. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1335. if (dev->phydev)
  1336. return mv643xx_eth_get_link_ksettings_phy(mp, cmd);
  1337. else
  1338. return mv643xx_eth_get_link_ksettings_phyless(mp, cmd);
  1339. }
  1340. static int
  1341. mv643xx_eth_set_link_ksettings(struct net_device *dev,
  1342. const struct ethtool_link_ksettings *cmd)
  1343. {
  1344. struct ethtool_link_ksettings c = *cmd;
  1345. u32 advertising;
  1346. int ret;
  1347. if (!dev->phydev)
  1348. return -EINVAL;
  1349. /*
  1350. * The MAC does not support 1000baseT_Half.
  1351. */
  1352. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  1353. c.link_modes.advertising);
  1354. advertising &= ~ADVERTISED_1000baseT_Half;
  1355. ethtool_convert_legacy_u32_to_link_mode(c.link_modes.advertising,
  1356. advertising);
  1357. ret = phy_ethtool_ksettings_set(dev->phydev, &c);
  1358. if (!ret)
  1359. mv643xx_eth_adjust_link(dev);
  1360. return ret;
  1361. }
  1362. static void mv643xx_eth_get_drvinfo(struct net_device *dev,
  1363. struct ethtool_drvinfo *drvinfo)
  1364. {
  1365. strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
  1366. sizeof(drvinfo->driver));
  1367. strlcpy(drvinfo->version, mv643xx_eth_driver_version,
  1368. sizeof(drvinfo->version));
  1369. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  1370. strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  1371. }
  1372. static int
  1373. mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1374. {
  1375. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1376. ec->rx_coalesce_usecs = get_rx_coal(mp);
  1377. ec->tx_coalesce_usecs = get_tx_coal(mp);
  1378. return 0;
  1379. }
  1380. static int
  1381. mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1382. {
  1383. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1384. set_rx_coal(mp, ec->rx_coalesce_usecs);
  1385. set_tx_coal(mp, ec->tx_coalesce_usecs);
  1386. return 0;
  1387. }
  1388. static void
  1389. mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
  1390. {
  1391. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1392. er->rx_max_pending = 4096;
  1393. er->tx_max_pending = 4096;
  1394. er->rx_pending = mp->rx_ring_size;
  1395. er->tx_pending = mp->tx_ring_size;
  1396. }
  1397. static int
  1398. mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
  1399. {
  1400. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1401. if (er->rx_mini_pending || er->rx_jumbo_pending)
  1402. return -EINVAL;
  1403. mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
  1404. mp->tx_ring_size = clamp_t(unsigned int, er->tx_pending,
  1405. MV643XX_MAX_SKB_DESCS * 2, 4096);
  1406. if (mp->tx_ring_size != er->tx_pending)
  1407. netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
  1408. mp->tx_ring_size, er->tx_pending);
  1409. if (netif_running(dev)) {
  1410. mv643xx_eth_stop(dev);
  1411. if (mv643xx_eth_open(dev)) {
  1412. netdev_err(dev,
  1413. "fatal error on re-opening device after ring param change\n");
  1414. return -ENOMEM;
  1415. }
  1416. }
  1417. return 0;
  1418. }
  1419. static int
  1420. mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
  1421. {
  1422. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1423. bool rx_csum = features & NETIF_F_RXCSUM;
  1424. wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
  1425. return 0;
  1426. }
  1427. static void mv643xx_eth_get_strings(struct net_device *dev,
  1428. uint32_t stringset, uint8_t *data)
  1429. {
  1430. int i;
  1431. if (stringset == ETH_SS_STATS) {
  1432. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1433. memcpy(data + i * ETH_GSTRING_LEN,
  1434. mv643xx_eth_stats[i].stat_string,
  1435. ETH_GSTRING_LEN);
  1436. }
  1437. }
  1438. }
  1439. static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
  1440. struct ethtool_stats *stats,
  1441. uint64_t *data)
  1442. {
  1443. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1444. int i;
  1445. mv643xx_eth_get_stats(dev);
  1446. mib_counters_update(mp);
  1447. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1448. const struct mv643xx_eth_stats *stat;
  1449. void *p;
  1450. stat = mv643xx_eth_stats + i;
  1451. if (stat->netdev_off >= 0)
  1452. p = ((void *)mp->dev) + stat->netdev_off;
  1453. else
  1454. p = ((void *)mp) + stat->mp_off;
  1455. data[i] = (stat->sizeof_stat == 8) ?
  1456. *(uint64_t *)p : *(uint32_t *)p;
  1457. }
  1458. }
  1459. static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
  1460. {
  1461. if (sset == ETH_SS_STATS)
  1462. return ARRAY_SIZE(mv643xx_eth_stats);
  1463. return -EOPNOTSUPP;
  1464. }
  1465. static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
  1466. .get_drvinfo = mv643xx_eth_get_drvinfo,
  1467. .nway_reset = phy_ethtool_nway_reset,
  1468. .get_link = ethtool_op_get_link,
  1469. .get_coalesce = mv643xx_eth_get_coalesce,
  1470. .set_coalesce = mv643xx_eth_set_coalesce,
  1471. .get_ringparam = mv643xx_eth_get_ringparam,
  1472. .set_ringparam = mv643xx_eth_set_ringparam,
  1473. .get_strings = mv643xx_eth_get_strings,
  1474. .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
  1475. .get_sset_count = mv643xx_eth_get_sset_count,
  1476. .get_ts_info = ethtool_op_get_ts_info,
  1477. .get_wol = mv643xx_eth_get_wol,
  1478. .set_wol = mv643xx_eth_set_wol,
  1479. .get_link_ksettings = mv643xx_eth_get_link_ksettings,
  1480. .set_link_ksettings = mv643xx_eth_set_link_ksettings,
  1481. };
  1482. /* address handling *********************************************************/
  1483. static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
  1484. {
  1485. unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
  1486. unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
  1487. addr[0] = (mac_h >> 24) & 0xff;
  1488. addr[1] = (mac_h >> 16) & 0xff;
  1489. addr[2] = (mac_h >> 8) & 0xff;
  1490. addr[3] = mac_h & 0xff;
  1491. addr[4] = (mac_l >> 8) & 0xff;
  1492. addr[5] = mac_l & 0xff;
  1493. }
  1494. static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
  1495. {
  1496. wrlp(mp, MAC_ADDR_HIGH,
  1497. (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
  1498. wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
  1499. }
  1500. static u32 uc_addr_filter_mask(struct net_device *dev)
  1501. {
  1502. struct netdev_hw_addr *ha;
  1503. u32 nibbles;
  1504. if (dev->flags & IFF_PROMISC)
  1505. return 0;
  1506. nibbles = 1 << (dev->dev_addr[5] & 0x0f);
  1507. netdev_for_each_uc_addr(ha, dev) {
  1508. if (memcmp(dev->dev_addr, ha->addr, 5))
  1509. return 0;
  1510. if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
  1511. return 0;
  1512. nibbles |= 1 << (ha->addr[5] & 0x0f);
  1513. }
  1514. return nibbles;
  1515. }
  1516. static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
  1517. {
  1518. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1519. u32 port_config;
  1520. u32 nibbles;
  1521. int i;
  1522. uc_addr_set(mp, dev->dev_addr);
  1523. port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
  1524. nibbles = uc_addr_filter_mask(dev);
  1525. if (!nibbles) {
  1526. port_config |= UNICAST_PROMISCUOUS_MODE;
  1527. nibbles = 0xffff;
  1528. }
  1529. for (i = 0; i < 16; i += 4) {
  1530. int off = UNICAST_TABLE(mp->port_num) + i;
  1531. u32 v;
  1532. v = 0;
  1533. if (nibbles & 1)
  1534. v |= 0x00000001;
  1535. if (nibbles & 2)
  1536. v |= 0x00000100;
  1537. if (nibbles & 4)
  1538. v |= 0x00010000;
  1539. if (nibbles & 8)
  1540. v |= 0x01000000;
  1541. nibbles >>= 4;
  1542. wrl(mp, off, v);
  1543. }
  1544. wrlp(mp, PORT_CONFIG, port_config);
  1545. }
  1546. static int addr_crc(unsigned char *addr)
  1547. {
  1548. int crc = 0;
  1549. int i;
  1550. for (i = 0; i < 6; i++) {
  1551. int j;
  1552. crc = (crc ^ addr[i]) << 8;
  1553. for (j = 7; j >= 0; j--) {
  1554. if (crc & (0x100 << j))
  1555. crc ^= 0x107 << j;
  1556. }
  1557. }
  1558. return crc;
  1559. }
  1560. static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
  1561. {
  1562. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1563. u32 *mc_spec;
  1564. u32 *mc_other;
  1565. struct netdev_hw_addr *ha;
  1566. int i;
  1567. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI))
  1568. goto promiscuous;
  1569. /* Allocate both mc_spec and mc_other tables */
  1570. mc_spec = kcalloc(128, sizeof(u32), GFP_ATOMIC);
  1571. if (!mc_spec)
  1572. goto promiscuous;
  1573. mc_other = &mc_spec[64];
  1574. netdev_for_each_mc_addr(ha, dev) {
  1575. u8 *a = ha->addr;
  1576. u32 *table;
  1577. u8 entry;
  1578. if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
  1579. table = mc_spec;
  1580. entry = a[5];
  1581. } else {
  1582. table = mc_other;
  1583. entry = addr_crc(a);
  1584. }
  1585. table[entry >> 2] |= 1 << (8 * (entry & 3));
  1586. }
  1587. for (i = 0; i < 64; i++) {
  1588. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1589. mc_spec[i]);
  1590. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1591. mc_other[i]);
  1592. }
  1593. kfree(mc_spec);
  1594. return;
  1595. promiscuous:
  1596. for (i = 0; i < 64; i++) {
  1597. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1598. 0x01010101u);
  1599. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1600. 0x01010101u);
  1601. }
  1602. }
  1603. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  1604. {
  1605. mv643xx_eth_program_unicast_filter(dev);
  1606. mv643xx_eth_program_multicast_filter(dev);
  1607. }
  1608. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  1609. {
  1610. struct sockaddr *sa = addr;
  1611. if (!is_valid_ether_addr(sa->sa_data))
  1612. return -EADDRNOTAVAIL;
  1613. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  1614. netif_addr_lock_bh(dev);
  1615. mv643xx_eth_program_unicast_filter(dev);
  1616. netif_addr_unlock_bh(dev);
  1617. return 0;
  1618. }
  1619. /* rx/tx queue initialisation ***********************************************/
  1620. static int rxq_init(struct mv643xx_eth_private *mp, int index)
  1621. {
  1622. struct rx_queue *rxq = mp->rxq + index;
  1623. struct rx_desc *rx_desc;
  1624. int size;
  1625. int i;
  1626. rxq->index = index;
  1627. rxq->rx_ring_size = mp->rx_ring_size;
  1628. rxq->rx_desc_count = 0;
  1629. rxq->rx_curr_desc = 0;
  1630. rxq->rx_used_desc = 0;
  1631. size = rxq->rx_ring_size * sizeof(struct rx_desc);
  1632. if (index == 0 && size <= mp->rx_desc_sram_size) {
  1633. rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
  1634. mp->rx_desc_sram_size);
  1635. rxq->rx_desc_dma = mp->rx_desc_sram_addr;
  1636. } else {
  1637. rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
  1638. size, &rxq->rx_desc_dma,
  1639. GFP_KERNEL);
  1640. }
  1641. if (rxq->rx_desc_area == NULL) {
  1642. netdev_err(mp->dev,
  1643. "can't allocate rx ring (%d bytes)\n", size);
  1644. goto out;
  1645. }
  1646. memset(rxq->rx_desc_area, 0, size);
  1647. rxq->rx_desc_area_size = size;
  1648. rxq->rx_skb = kcalloc(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
  1649. GFP_KERNEL);
  1650. if (rxq->rx_skb == NULL)
  1651. goto out_free;
  1652. rx_desc = rxq->rx_desc_area;
  1653. for (i = 0; i < rxq->rx_ring_size; i++) {
  1654. int nexti;
  1655. nexti = i + 1;
  1656. if (nexti == rxq->rx_ring_size)
  1657. nexti = 0;
  1658. rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
  1659. nexti * sizeof(struct rx_desc);
  1660. }
  1661. return 0;
  1662. out_free:
  1663. if (index == 0 && size <= mp->rx_desc_sram_size)
  1664. iounmap(rxq->rx_desc_area);
  1665. else
  1666. dma_free_coherent(mp->dev->dev.parent, size,
  1667. rxq->rx_desc_area,
  1668. rxq->rx_desc_dma);
  1669. out:
  1670. return -ENOMEM;
  1671. }
  1672. static void rxq_deinit(struct rx_queue *rxq)
  1673. {
  1674. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  1675. int i;
  1676. rxq_disable(rxq);
  1677. for (i = 0; i < rxq->rx_ring_size; i++) {
  1678. if (rxq->rx_skb[i]) {
  1679. dev_kfree_skb(rxq->rx_skb[i]);
  1680. rxq->rx_desc_count--;
  1681. }
  1682. }
  1683. if (rxq->rx_desc_count) {
  1684. netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
  1685. rxq->rx_desc_count);
  1686. }
  1687. if (rxq->index == 0 &&
  1688. rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
  1689. iounmap(rxq->rx_desc_area);
  1690. else
  1691. dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
  1692. rxq->rx_desc_area, rxq->rx_desc_dma);
  1693. kfree(rxq->rx_skb);
  1694. }
  1695. static int txq_init(struct mv643xx_eth_private *mp, int index)
  1696. {
  1697. struct tx_queue *txq = mp->txq + index;
  1698. struct tx_desc *tx_desc;
  1699. int size;
  1700. int ret;
  1701. int i;
  1702. txq->index = index;
  1703. txq->tx_ring_size = mp->tx_ring_size;
  1704. /* A queue must always have room for at least one skb.
  1705. * Therefore, stop the queue when the free entries reaches
  1706. * the maximum number of descriptors per skb.
  1707. */
  1708. txq->tx_stop_threshold = txq->tx_ring_size - MV643XX_MAX_SKB_DESCS;
  1709. txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
  1710. txq->tx_desc_count = 0;
  1711. txq->tx_curr_desc = 0;
  1712. txq->tx_used_desc = 0;
  1713. size = txq->tx_ring_size * sizeof(struct tx_desc);
  1714. if (index == 0 && size <= mp->tx_desc_sram_size) {
  1715. txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
  1716. mp->tx_desc_sram_size);
  1717. txq->tx_desc_dma = mp->tx_desc_sram_addr;
  1718. } else {
  1719. txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
  1720. size, &txq->tx_desc_dma,
  1721. GFP_KERNEL);
  1722. }
  1723. if (txq->tx_desc_area == NULL) {
  1724. netdev_err(mp->dev,
  1725. "can't allocate tx ring (%d bytes)\n", size);
  1726. return -ENOMEM;
  1727. }
  1728. memset(txq->tx_desc_area, 0, size);
  1729. txq->tx_desc_area_size = size;
  1730. tx_desc = txq->tx_desc_area;
  1731. for (i = 0; i < txq->tx_ring_size; i++) {
  1732. struct tx_desc *txd = tx_desc + i;
  1733. int nexti;
  1734. nexti = i + 1;
  1735. if (nexti == txq->tx_ring_size)
  1736. nexti = 0;
  1737. txd->cmd_sts = 0;
  1738. txd->next_desc_ptr = txq->tx_desc_dma +
  1739. nexti * sizeof(struct tx_desc);
  1740. }
  1741. txq->tx_desc_mapping = kcalloc(txq->tx_ring_size, sizeof(char),
  1742. GFP_KERNEL);
  1743. if (!txq->tx_desc_mapping) {
  1744. ret = -ENOMEM;
  1745. goto err_free_desc_area;
  1746. }
  1747. /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
  1748. txq->tso_hdrs = dma_alloc_coherent(mp->dev->dev.parent,
  1749. txq->tx_ring_size * TSO_HEADER_SIZE,
  1750. &txq->tso_hdrs_dma, GFP_KERNEL);
  1751. if (txq->tso_hdrs == NULL) {
  1752. ret = -ENOMEM;
  1753. goto err_free_desc_mapping;
  1754. }
  1755. skb_queue_head_init(&txq->tx_skb);
  1756. return 0;
  1757. err_free_desc_mapping:
  1758. kfree(txq->tx_desc_mapping);
  1759. err_free_desc_area:
  1760. if (index == 0 && size <= mp->tx_desc_sram_size)
  1761. iounmap(txq->tx_desc_area);
  1762. else
  1763. dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
  1764. txq->tx_desc_area, txq->tx_desc_dma);
  1765. return ret;
  1766. }
  1767. static void txq_deinit(struct tx_queue *txq)
  1768. {
  1769. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  1770. txq_disable(txq);
  1771. txq_reclaim(txq, txq->tx_ring_size, 1);
  1772. BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
  1773. if (txq->index == 0 &&
  1774. txq->tx_desc_area_size <= mp->tx_desc_sram_size)
  1775. iounmap(txq->tx_desc_area);
  1776. else
  1777. dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
  1778. txq->tx_desc_area, txq->tx_desc_dma);
  1779. kfree(txq->tx_desc_mapping);
  1780. if (txq->tso_hdrs)
  1781. dma_free_coherent(mp->dev->dev.parent,
  1782. txq->tx_ring_size * TSO_HEADER_SIZE,
  1783. txq->tso_hdrs, txq->tso_hdrs_dma);
  1784. }
  1785. /* netdev ops and related ***************************************************/
  1786. static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
  1787. {
  1788. u32 int_cause;
  1789. u32 int_cause_ext;
  1790. int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
  1791. if (int_cause == 0)
  1792. return 0;
  1793. int_cause_ext = 0;
  1794. if (int_cause & INT_EXT) {
  1795. int_cause &= ~INT_EXT;
  1796. int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
  1797. }
  1798. if (int_cause) {
  1799. wrlp(mp, INT_CAUSE, ~int_cause);
  1800. mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
  1801. ~(rdlp(mp, TXQ_COMMAND) & 0xff);
  1802. mp->work_rx |= (int_cause & INT_RX) >> 2;
  1803. }
  1804. int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
  1805. if (int_cause_ext) {
  1806. wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
  1807. if (int_cause_ext & INT_EXT_LINK_PHY)
  1808. mp->work_link = 1;
  1809. mp->work_tx |= int_cause_ext & INT_EXT_TX;
  1810. }
  1811. return 1;
  1812. }
  1813. static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
  1814. {
  1815. struct net_device *dev = (struct net_device *)dev_id;
  1816. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1817. if (unlikely(!mv643xx_eth_collect_events(mp)))
  1818. return IRQ_NONE;
  1819. wrlp(mp, INT_MASK, 0);
  1820. napi_schedule(&mp->napi);
  1821. return IRQ_HANDLED;
  1822. }
  1823. static void handle_link_event(struct mv643xx_eth_private *mp)
  1824. {
  1825. struct net_device *dev = mp->dev;
  1826. u32 port_status;
  1827. int speed;
  1828. int duplex;
  1829. int fc;
  1830. port_status = rdlp(mp, PORT_STATUS);
  1831. if (!(port_status & LINK_UP)) {
  1832. if (netif_carrier_ok(dev)) {
  1833. int i;
  1834. netdev_info(dev, "link down\n");
  1835. netif_carrier_off(dev);
  1836. for (i = 0; i < mp->txq_count; i++) {
  1837. struct tx_queue *txq = mp->txq + i;
  1838. txq_reclaim(txq, txq->tx_ring_size, 1);
  1839. txq_reset_hw_ptr(txq);
  1840. }
  1841. }
  1842. return;
  1843. }
  1844. switch (port_status & PORT_SPEED_MASK) {
  1845. case PORT_SPEED_10:
  1846. speed = 10;
  1847. break;
  1848. case PORT_SPEED_100:
  1849. speed = 100;
  1850. break;
  1851. case PORT_SPEED_1000:
  1852. speed = 1000;
  1853. break;
  1854. default:
  1855. speed = -1;
  1856. break;
  1857. }
  1858. duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
  1859. fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
  1860. netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
  1861. speed, duplex ? "full" : "half", fc ? "en" : "dis");
  1862. if (!netif_carrier_ok(dev))
  1863. netif_carrier_on(dev);
  1864. }
  1865. static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
  1866. {
  1867. struct mv643xx_eth_private *mp;
  1868. int work_done;
  1869. mp = container_of(napi, struct mv643xx_eth_private, napi);
  1870. if (unlikely(mp->oom)) {
  1871. mp->oom = 0;
  1872. del_timer(&mp->rx_oom);
  1873. }
  1874. work_done = 0;
  1875. while (work_done < budget) {
  1876. u8 queue_mask;
  1877. int queue;
  1878. int work_tbd;
  1879. if (mp->work_link) {
  1880. mp->work_link = 0;
  1881. handle_link_event(mp);
  1882. work_done++;
  1883. continue;
  1884. }
  1885. queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
  1886. if (likely(!mp->oom))
  1887. queue_mask |= mp->work_rx_refill;
  1888. if (!queue_mask) {
  1889. if (mv643xx_eth_collect_events(mp))
  1890. continue;
  1891. break;
  1892. }
  1893. queue = fls(queue_mask) - 1;
  1894. queue_mask = 1 << queue;
  1895. work_tbd = budget - work_done;
  1896. if (work_tbd > 16)
  1897. work_tbd = 16;
  1898. if (mp->work_tx_end & queue_mask) {
  1899. txq_kick(mp->txq + queue);
  1900. } else if (mp->work_tx & queue_mask) {
  1901. work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
  1902. txq_maybe_wake(mp->txq + queue);
  1903. } else if (mp->work_rx & queue_mask) {
  1904. work_done += rxq_process(mp->rxq + queue, work_tbd);
  1905. } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
  1906. work_done += rxq_refill(mp->rxq + queue, work_tbd);
  1907. } else {
  1908. BUG();
  1909. }
  1910. }
  1911. if (work_done < budget) {
  1912. if (mp->oom)
  1913. mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
  1914. napi_complete_done(napi, work_done);
  1915. wrlp(mp, INT_MASK, mp->int_mask);
  1916. }
  1917. return work_done;
  1918. }
  1919. static inline void oom_timer_wrapper(unsigned long data)
  1920. {
  1921. struct mv643xx_eth_private *mp = (void *)data;
  1922. napi_schedule(&mp->napi);
  1923. }
  1924. static void port_start(struct mv643xx_eth_private *mp)
  1925. {
  1926. struct net_device *dev = mp->dev;
  1927. u32 pscr;
  1928. int i;
  1929. /*
  1930. * Perform PHY reset, if there is a PHY.
  1931. */
  1932. if (dev->phydev) {
  1933. struct ethtool_link_ksettings cmd;
  1934. mv643xx_eth_get_link_ksettings(dev, &cmd);
  1935. phy_init_hw(dev->phydev);
  1936. mv643xx_eth_set_link_ksettings(
  1937. dev, (const struct ethtool_link_ksettings *)&cmd);
  1938. phy_start(dev->phydev);
  1939. }
  1940. /*
  1941. * Configure basic link parameters.
  1942. */
  1943. pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  1944. pscr |= SERIAL_PORT_ENABLE;
  1945. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1946. pscr |= DO_NOT_FORCE_LINK_FAIL;
  1947. if (!dev->phydev)
  1948. pscr |= FORCE_LINK_PASS;
  1949. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1950. /*
  1951. * Configure TX path and queues.
  1952. */
  1953. tx_set_rate(mp, 1000000000, 16777216);
  1954. for (i = 0; i < mp->txq_count; i++) {
  1955. struct tx_queue *txq = mp->txq + i;
  1956. txq_reset_hw_ptr(txq);
  1957. txq_set_rate(txq, 1000000000, 16777216);
  1958. txq_set_fixed_prio_mode(txq);
  1959. }
  1960. /*
  1961. * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
  1962. * frames to RX queue #0, and include the pseudo-header when
  1963. * calculating receive checksums.
  1964. */
  1965. mv643xx_eth_set_features(mp->dev, mp->dev->features);
  1966. /*
  1967. * Treat BPDUs as normal multicasts, and disable partition mode.
  1968. */
  1969. wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
  1970. /*
  1971. * Add configured unicast addresses to address filter table.
  1972. */
  1973. mv643xx_eth_program_unicast_filter(mp->dev);
  1974. /*
  1975. * Enable the receive queues.
  1976. */
  1977. for (i = 0; i < mp->rxq_count; i++) {
  1978. struct rx_queue *rxq = mp->rxq + i;
  1979. u32 addr;
  1980. addr = (u32)rxq->rx_desc_dma;
  1981. addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
  1982. wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
  1983. rxq_enable(rxq);
  1984. }
  1985. }
  1986. static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
  1987. {
  1988. int skb_size;
  1989. /*
  1990. * Reserve 2+14 bytes for an ethernet header (the hardware
  1991. * automatically prepends 2 bytes of dummy data to each
  1992. * received packet), 16 bytes for up to four VLAN tags, and
  1993. * 4 bytes for the trailing FCS -- 36 bytes total.
  1994. */
  1995. skb_size = mp->dev->mtu + 36;
  1996. /*
  1997. * Make sure that the skb size is a multiple of 8 bytes, as
  1998. * the lower three bits of the receive descriptor's buffer
  1999. * size field are ignored by the hardware.
  2000. */
  2001. mp->skb_size = (skb_size + 7) & ~7;
  2002. /*
  2003. * If NET_SKB_PAD is smaller than a cache line,
  2004. * netdev_alloc_skb() will cause skb->data to be misaligned
  2005. * to a cache line boundary. If this is the case, include
  2006. * some extra space to allow re-aligning the data area.
  2007. */
  2008. mp->skb_size += SKB_DMA_REALIGN;
  2009. }
  2010. static int mv643xx_eth_open(struct net_device *dev)
  2011. {
  2012. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2013. int err;
  2014. int i;
  2015. wrlp(mp, INT_CAUSE, 0);
  2016. wrlp(mp, INT_CAUSE_EXT, 0);
  2017. rdlp(mp, INT_CAUSE_EXT);
  2018. err = request_irq(dev->irq, mv643xx_eth_irq,
  2019. IRQF_SHARED, dev->name, dev);
  2020. if (err) {
  2021. netdev_err(dev, "can't assign irq\n");
  2022. return -EAGAIN;
  2023. }
  2024. mv643xx_eth_recalc_skb_size(mp);
  2025. napi_enable(&mp->napi);
  2026. mp->int_mask = INT_EXT;
  2027. for (i = 0; i < mp->rxq_count; i++) {
  2028. err = rxq_init(mp, i);
  2029. if (err) {
  2030. while (--i >= 0)
  2031. rxq_deinit(mp->rxq + i);
  2032. goto out;
  2033. }
  2034. rxq_refill(mp->rxq + i, INT_MAX);
  2035. mp->int_mask |= INT_RX_0 << i;
  2036. }
  2037. if (mp->oom) {
  2038. mp->rx_oom.expires = jiffies + (HZ / 10);
  2039. add_timer(&mp->rx_oom);
  2040. }
  2041. for (i = 0; i < mp->txq_count; i++) {
  2042. err = txq_init(mp, i);
  2043. if (err) {
  2044. while (--i >= 0)
  2045. txq_deinit(mp->txq + i);
  2046. goto out_free;
  2047. }
  2048. mp->int_mask |= INT_TX_END_0 << i;
  2049. }
  2050. add_timer(&mp->mib_counters_timer);
  2051. port_start(mp);
  2052. wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
  2053. wrlp(mp, INT_MASK, mp->int_mask);
  2054. return 0;
  2055. out_free:
  2056. for (i = 0; i < mp->rxq_count; i++)
  2057. rxq_deinit(mp->rxq + i);
  2058. out:
  2059. free_irq(dev->irq, dev);
  2060. return err;
  2061. }
  2062. static void port_reset(struct mv643xx_eth_private *mp)
  2063. {
  2064. unsigned int data;
  2065. int i;
  2066. for (i = 0; i < mp->rxq_count; i++)
  2067. rxq_disable(mp->rxq + i);
  2068. for (i = 0; i < mp->txq_count; i++)
  2069. txq_disable(mp->txq + i);
  2070. while (1) {
  2071. u32 ps = rdlp(mp, PORT_STATUS);
  2072. if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
  2073. break;
  2074. udelay(10);
  2075. }
  2076. /* Reset the Enable bit in the Configuration Register */
  2077. data = rdlp(mp, PORT_SERIAL_CONTROL);
  2078. data &= ~(SERIAL_PORT_ENABLE |
  2079. DO_NOT_FORCE_LINK_FAIL |
  2080. FORCE_LINK_PASS);
  2081. wrlp(mp, PORT_SERIAL_CONTROL, data);
  2082. }
  2083. static int mv643xx_eth_stop(struct net_device *dev)
  2084. {
  2085. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2086. int i;
  2087. wrlp(mp, INT_MASK_EXT, 0x00000000);
  2088. wrlp(mp, INT_MASK, 0x00000000);
  2089. rdlp(mp, INT_MASK);
  2090. napi_disable(&mp->napi);
  2091. del_timer_sync(&mp->rx_oom);
  2092. netif_carrier_off(dev);
  2093. if (dev->phydev)
  2094. phy_stop(dev->phydev);
  2095. free_irq(dev->irq, dev);
  2096. port_reset(mp);
  2097. mv643xx_eth_get_stats(dev);
  2098. mib_counters_update(mp);
  2099. del_timer_sync(&mp->mib_counters_timer);
  2100. for (i = 0; i < mp->rxq_count; i++)
  2101. rxq_deinit(mp->rxq + i);
  2102. for (i = 0; i < mp->txq_count; i++)
  2103. txq_deinit(mp->txq + i);
  2104. return 0;
  2105. }
  2106. static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2107. {
  2108. int ret;
  2109. if (!dev->phydev)
  2110. return -ENOTSUPP;
  2111. ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
  2112. if (!ret)
  2113. mv643xx_eth_adjust_link(dev);
  2114. return ret;
  2115. }
  2116. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  2117. {
  2118. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2119. dev->mtu = new_mtu;
  2120. mv643xx_eth_recalc_skb_size(mp);
  2121. tx_set_rate(mp, 1000000000, 16777216);
  2122. if (!netif_running(dev))
  2123. return 0;
  2124. /*
  2125. * Stop and then re-open the interface. This will allocate RX
  2126. * skbs of the new MTU.
  2127. * There is a possible danger that the open will not succeed,
  2128. * due to memory being full.
  2129. */
  2130. mv643xx_eth_stop(dev);
  2131. if (mv643xx_eth_open(dev)) {
  2132. netdev_err(dev,
  2133. "fatal error on re-opening device after MTU change\n");
  2134. }
  2135. return 0;
  2136. }
  2137. static void tx_timeout_task(struct work_struct *ugly)
  2138. {
  2139. struct mv643xx_eth_private *mp;
  2140. mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
  2141. if (netif_running(mp->dev)) {
  2142. netif_tx_stop_all_queues(mp->dev);
  2143. port_reset(mp);
  2144. port_start(mp);
  2145. netif_tx_wake_all_queues(mp->dev);
  2146. }
  2147. }
  2148. static void mv643xx_eth_tx_timeout(struct net_device *dev)
  2149. {
  2150. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2151. netdev_info(dev, "tx timeout\n");
  2152. schedule_work(&mp->tx_timeout_task);
  2153. }
  2154. #ifdef CONFIG_NET_POLL_CONTROLLER
  2155. static void mv643xx_eth_netpoll(struct net_device *dev)
  2156. {
  2157. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2158. wrlp(mp, INT_MASK, 0x00000000);
  2159. rdlp(mp, INT_MASK);
  2160. mv643xx_eth_irq(dev->irq, dev);
  2161. wrlp(mp, INT_MASK, mp->int_mask);
  2162. }
  2163. #endif
  2164. /* platform glue ************************************************************/
  2165. static void
  2166. mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
  2167. const struct mbus_dram_target_info *dram)
  2168. {
  2169. void __iomem *base = msp->base;
  2170. u32 win_enable;
  2171. u32 win_protect;
  2172. int i;
  2173. for (i = 0; i < 6; i++) {
  2174. writel(0, base + WINDOW_BASE(i));
  2175. writel(0, base + WINDOW_SIZE(i));
  2176. if (i < 4)
  2177. writel(0, base + WINDOW_REMAP_HIGH(i));
  2178. }
  2179. win_enable = 0x3f;
  2180. win_protect = 0;
  2181. for (i = 0; i < dram->num_cs; i++) {
  2182. const struct mbus_dram_window *cs = dram->cs + i;
  2183. writel((cs->base & 0xffff0000) |
  2184. (cs->mbus_attr << 8) |
  2185. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  2186. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  2187. win_enable &= ~(1 << i);
  2188. win_protect |= 3 << (2 * i);
  2189. }
  2190. writel(win_enable, base + WINDOW_BAR_ENABLE);
  2191. msp->win_protect = win_protect;
  2192. }
  2193. static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
  2194. {
  2195. /*
  2196. * Check whether we have a 14-bit coal limit field in bits
  2197. * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
  2198. * SDMA config register.
  2199. */
  2200. writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
  2201. if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
  2202. msp->extended_rx_coal_limit = 1;
  2203. else
  2204. msp->extended_rx_coal_limit = 0;
  2205. /*
  2206. * Check whether the MAC supports TX rate control, and if
  2207. * yes, whether its associated registers are in the old or
  2208. * the new place.
  2209. */
  2210. writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
  2211. if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
  2212. msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
  2213. } else {
  2214. writel(7, msp->base + 0x0400 + TX_BW_RATE);
  2215. if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
  2216. msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
  2217. else
  2218. msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
  2219. }
  2220. }
  2221. #if defined(CONFIG_OF)
  2222. static const struct of_device_id mv643xx_eth_shared_ids[] = {
  2223. { .compatible = "marvell,orion-eth", },
  2224. { .compatible = "marvell,kirkwood-eth", },
  2225. { }
  2226. };
  2227. MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids);
  2228. #endif
  2229. #if defined(CONFIG_OF_IRQ) && !defined(CONFIG_MV64X60)
  2230. #define mv643xx_eth_property(_np, _name, _v) \
  2231. do { \
  2232. u32 tmp; \
  2233. if (!of_property_read_u32(_np, "marvell," _name, &tmp)) \
  2234. _v = tmp; \
  2235. } while (0)
  2236. static struct platform_device *port_platdev[3];
  2237. static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev,
  2238. struct device_node *pnp)
  2239. {
  2240. struct platform_device *ppdev;
  2241. struct mv643xx_eth_platform_data ppd;
  2242. struct resource res;
  2243. const char *mac_addr;
  2244. int ret;
  2245. int dev_num = 0;
  2246. memset(&ppd, 0, sizeof(ppd));
  2247. ppd.shared = pdev;
  2248. memset(&res, 0, sizeof(res));
  2249. if (!of_irq_to_resource(pnp, 0, &res)) {
  2250. dev_err(&pdev->dev, "missing interrupt on %s\n", pnp->name);
  2251. return -EINVAL;
  2252. }
  2253. if (of_property_read_u32(pnp, "reg", &ppd.port_number)) {
  2254. dev_err(&pdev->dev, "missing reg property on %s\n", pnp->name);
  2255. return -EINVAL;
  2256. }
  2257. if (ppd.port_number >= 3) {
  2258. dev_err(&pdev->dev, "invalid reg property on %s\n", pnp->name);
  2259. return -EINVAL;
  2260. }
  2261. while (dev_num < 3 && port_platdev[dev_num])
  2262. dev_num++;
  2263. if (dev_num == 3) {
  2264. dev_err(&pdev->dev, "too many ports registered\n");
  2265. return -EINVAL;
  2266. }
  2267. mac_addr = of_get_mac_address(pnp);
  2268. if (mac_addr)
  2269. memcpy(ppd.mac_addr, mac_addr, ETH_ALEN);
  2270. mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size);
  2271. mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr);
  2272. mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size);
  2273. mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size);
  2274. mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr);
  2275. mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size);
  2276. ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0);
  2277. if (!ppd.phy_node) {
  2278. ppd.phy_addr = MV643XX_ETH_PHY_NONE;
  2279. of_property_read_u32(pnp, "speed", &ppd.speed);
  2280. of_property_read_u32(pnp, "duplex", &ppd.duplex);
  2281. }
  2282. ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num);
  2283. if (!ppdev)
  2284. return -ENOMEM;
  2285. ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  2286. ppdev->dev.of_node = pnp;
  2287. ret = platform_device_add_resources(ppdev, &res, 1);
  2288. if (ret)
  2289. goto port_err;
  2290. ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
  2291. if (ret)
  2292. goto port_err;
  2293. ret = platform_device_add(ppdev);
  2294. if (ret)
  2295. goto port_err;
  2296. port_platdev[dev_num] = ppdev;
  2297. return 0;
  2298. port_err:
  2299. platform_device_put(ppdev);
  2300. return ret;
  2301. }
  2302. static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
  2303. {
  2304. struct mv643xx_eth_shared_platform_data *pd;
  2305. struct device_node *pnp, *np = pdev->dev.of_node;
  2306. int ret;
  2307. /* bail out if not registered from DT */
  2308. if (!np)
  2309. return 0;
  2310. pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
  2311. if (!pd)
  2312. return -ENOMEM;
  2313. pdev->dev.platform_data = pd;
  2314. mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit);
  2315. for_each_available_child_of_node(np, pnp) {
  2316. ret = mv643xx_eth_shared_of_add_port(pdev, pnp);
  2317. if (ret) {
  2318. of_node_put(pnp);
  2319. return ret;
  2320. }
  2321. }
  2322. return 0;
  2323. }
  2324. static void mv643xx_eth_shared_of_remove(void)
  2325. {
  2326. int n;
  2327. for (n = 0; n < 3; n++) {
  2328. platform_device_del(port_platdev[n]);
  2329. port_platdev[n] = NULL;
  2330. }
  2331. }
  2332. #else
  2333. static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
  2334. {
  2335. return 0;
  2336. }
  2337. static inline void mv643xx_eth_shared_of_remove(void)
  2338. {
  2339. }
  2340. #endif
  2341. static int mv643xx_eth_shared_probe(struct platform_device *pdev)
  2342. {
  2343. static int mv643xx_eth_version_printed;
  2344. struct mv643xx_eth_shared_platform_data *pd;
  2345. struct mv643xx_eth_shared_private *msp;
  2346. const struct mbus_dram_target_info *dram;
  2347. struct resource *res;
  2348. int ret;
  2349. if (!mv643xx_eth_version_printed++)
  2350. pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
  2351. mv643xx_eth_driver_version);
  2352. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2353. if (res == NULL)
  2354. return -EINVAL;
  2355. msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL);
  2356. if (msp == NULL)
  2357. return -ENOMEM;
  2358. platform_set_drvdata(pdev, msp);
  2359. msp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  2360. if (msp->base == NULL)
  2361. return -ENOMEM;
  2362. msp->clk = devm_clk_get(&pdev->dev, NULL);
  2363. if (!IS_ERR(msp->clk))
  2364. clk_prepare_enable(msp->clk);
  2365. /*
  2366. * (Re-)program MBUS remapping windows if we are asked to.
  2367. */
  2368. dram = mv_mbus_dram_info();
  2369. if (dram)
  2370. mv643xx_eth_conf_mbus_windows(msp, dram);
  2371. ret = mv643xx_eth_shared_of_probe(pdev);
  2372. if (ret)
  2373. return ret;
  2374. pd = dev_get_platdata(&pdev->dev);
  2375. msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
  2376. pd->tx_csum_limit : 9 * 1024;
  2377. infer_hw_params(msp);
  2378. return 0;
  2379. }
  2380. static int mv643xx_eth_shared_remove(struct platform_device *pdev)
  2381. {
  2382. struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
  2383. mv643xx_eth_shared_of_remove();
  2384. if (!IS_ERR(msp->clk))
  2385. clk_disable_unprepare(msp->clk);
  2386. return 0;
  2387. }
  2388. static struct platform_driver mv643xx_eth_shared_driver = {
  2389. .probe = mv643xx_eth_shared_probe,
  2390. .remove = mv643xx_eth_shared_remove,
  2391. .driver = {
  2392. .name = MV643XX_ETH_SHARED_NAME,
  2393. .of_match_table = of_match_ptr(mv643xx_eth_shared_ids),
  2394. },
  2395. };
  2396. static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
  2397. {
  2398. int addr_shift = 5 * mp->port_num;
  2399. u32 data;
  2400. data = rdl(mp, PHY_ADDR);
  2401. data &= ~(0x1f << addr_shift);
  2402. data |= (phy_addr & 0x1f) << addr_shift;
  2403. wrl(mp, PHY_ADDR, data);
  2404. }
  2405. static int phy_addr_get(struct mv643xx_eth_private *mp)
  2406. {
  2407. unsigned int data;
  2408. data = rdl(mp, PHY_ADDR);
  2409. return (data >> (5 * mp->port_num)) & 0x1f;
  2410. }
  2411. static void set_params(struct mv643xx_eth_private *mp,
  2412. struct mv643xx_eth_platform_data *pd)
  2413. {
  2414. struct net_device *dev = mp->dev;
  2415. unsigned int tx_ring_size;
  2416. if (is_valid_ether_addr(pd->mac_addr))
  2417. memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
  2418. else
  2419. uc_addr_get(mp, dev->dev_addr);
  2420. mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
  2421. if (pd->rx_queue_size)
  2422. mp->rx_ring_size = pd->rx_queue_size;
  2423. mp->rx_desc_sram_addr = pd->rx_sram_addr;
  2424. mp->rx_desc_sram_size = pd->rx_sram_size;
  2425. mp->rxq_count = pd->rx_queue_count ? : 1;
  2426. tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
  2427. if (pd->tx_queue_size)
  2428. tx_ring_size = pd->tx_queue_size;
  2429. mp->tx_ring_size = clamp_t(unsigned int, tx_ring_size,
  2430. MV643XX_MAX_SKB_DESCS * 2, 4096);
  2431. if (mp->tx_ring_size != tx_ring_size)
  2432. netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
  2433. mp->tx_ring_size, tx_ring_size);
  2434. mp->tx_desc_sram_addr = pd->tx_sram_addr;
  2435. mp->tx_desc_sram_size = pd->tx_sram_size;
  2436. mp->txq_count = pd->tx_queue_count ? : 1;
  2437. }
  2438. static int get_phy_mode(struct mv643xx_eth_private *mp)
  2439. {
  2440. struct device *dev = mp->dev->dev.parent;
  2441. int iface = -1;
  2442. if (dev->of_node)
  2443. iface = of_get_phy_mode(dev->of_node);
  2444. /* Historical default if unspecified. We could also read/write
  2445. * the interface state in the PSC1
  2446. */
  2447. if (iface < 0)
  2448. iface = PHY_INTERFACE_MODE_GMII;
  2449. return iface;
  2450. }
  2451. static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
  2452. int phy_addr)
  2453. {
  2454. struct phy_device *phydev;
  2455. int start;
  2456. int num;
  2457. int i;
  2458. char phy_id[MII_BUS_ID_SIZE + 3];
  2459. if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
  2460. start = phy_addr_get(mp) & 0x1f;
  2461. num = 32;
  2462. } else {
  2463. start = phy_addr & 0x1f;
  2464. num = 1;
  2465. }
  2466. /* Attempt to connect to the PHY using orion-mdio */
  2467. phydev = ERR_PTR(-ENODEV);
  2468. for (i = 0; i < num; i++) {
  2469. int addr = (start + i) & 0x1f;
  2470. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  2471. "orion-mdio-mii", addr);
  2472. phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
  2473. get_phy_mode(mp));
  2474. if (!IS_ERR(phydev)) {
  2475. phy_addr_set(mp, addr);
  2476. break;
  2477. }
  2478. }
  2479. return phydev;
  2480. }
  2481. static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
  2482. {
  2483. struct net_device *dev = mp->dev;
  2484. struct phy_device *phy = dev->phydev;
  2485. if (speed == 0) {
  2486. phy->autoneg = AUTONEG_ENABLE;
  2487. phy->speed = 0;
  2488. phy->duplex = 0;
  2489. phy->advertising = phy->supported | ADVERTISED_Autoneg;
  2490. } else {
  2491. phy->autoneg = AUTONEG_DISABLE;
  2492. phy->advertising = 0;
  2493. phy->speed = speed;
  2494. phy->duplex = duplex;
  2495. }
  2496. phy_start_aneg(phy);
  2497. }
  2498. static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
  2499. {
  2500. struct net_device *dev = mp->dev;
  2501. u32 pscr;
  2502. pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  2503. if (pscr & SERIAL_PORT_ENABLE) {
  2504. pscr &= ~SERIAL_PORT_ENABLE;
  2505. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  2506. }
  2507. pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
  2508. if (!dev->phydev) {
  2509. pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
  2510. if (speed == SPEED_1000)
  2511. pscr |= SET_GMII_SPEED_TO_1000;
  2512. else if (speed == SPEED_100)
  2513. pscr |= SET_MII_SPEED_TO_100;
  2514. pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
  2515. pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
  2516. if (duplex == DUPLEX_FULL)
  2517. pscr |= SET_FULL_DUPLEX_MODE;
  2518. }
  2519. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  2520. }
  2521. static const struct net_device_ops mv643xx_eth_netdev_ops = {
  2522. .ndo_open = mv643xx_eth_open,
  2523. .ndo_stop = mv643xx_eth_stop,
  2524. .ndo_start_xmit = mv643xx_eth_xmit,
  2525. .ndo_set_rx_mode = mv643xx_eth_set_rx_mode,
  2526. .ndo_set_mac_address = mv643xx_eth_set_mac_address,
  2527. .ndo_validate_addr = eth_validate_addr,
  2528. .ndo_do_ioctl = mv643xx_eth_ioctl,
  2529. .ndo_change_mtu = mv643xx_eth_change_mtu,
  2530. .ndo_set_features = mv643xx_eth_set_features,
  2531. .ndo_tx_timeout = mv643xx_eth_tx_timeout,
  2532. .ndo_get_stats = mv643xx_eth_get_stats,
  2533. #ifdef CONFIG_NET_POLL_CONTROLLER
  2534. .ndo_poll_controller = mv643xx_eth_netpoll,
  2535. #endif
  2536. };
  2537. static int mv643xx_eth_probe(struct platform_device *pdev)
  2538. {
  2539. struct mv643xx_eth_platform_data *pd;
  2540. struct mv643xx_eth_private *mp;
  2541. struct net_device *dev;
  2542. struct phy_device *phydev = NULL;
  2543. struct resource *res;
  2544. int err;
  2545. pd = dev_get_platdata(&pdev->dev);
  2546. if (pd == NULL) {
  2547. dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
  2548. return -ENODEV;
  2549. }
  2550. if (pd->shared == NULL) {
  2551. dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
  2552. return -ENODEV;
  2553. }
  2554. dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
  2555. if (!dev)
  2556. return -ENOMEM;
  2557. SET_NETDEV_DEV(dev, &pdev->dev);
  2558. mp = netdev_priv(dev);
  2559. platform_set_drvdata(pdev, mp);
  2560. mp->shared = platform_get_drvdata(pd->shared);
  2561. mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
  2562. mp->port_num = pd->port_number;
  2563. mp->dev = dev;
  2564. /* Kirkwood resets some registers on gated clocks. Especially
  2565. * CLK125_BYPASS_EN must be cleared but is not available on
  2566. * all other SoCs/System Controllers using this driver.
  2567. */
  2568. if (of_device_is_compatible(pdev->dev.of_node,
  2569. "marvell,kirkwood-eth-port"))
  2570. wrlp(mp, PORT_SERIAL_CONTROL1,
  2571. rdlp(mp, PORT_SERIAL_CONTROL1) & ~CLK125_BYPASS_EN);
  2572. /*
  2573. * Start with a default rate, and if there is a clock, allow
  2574. * it to override the default.
  2575. */
  2576. mp->t_clk = 133000000;
  2577. mp->clk = devm_clk_get(&pdev->dev, NULL);
  2578. if (!IS_ERR(mp->clk)) {
  2579. clk_prepare_enable(mp->clk);
  2580. mp->t_clk = clk_get_rate(mp->clk);
  2581. } else if (!IS_ERR(mp->shared->clk)) {
  2582. mp->t_clk = clk_get_rate(mp->shared->clk);
  2583. }
  2584. set_params(mp, pd);
  2585. netif_set_real_num_tx_queues(dev, mp->txq_count);
  2586. netif_set_real_num_rx_queues(dev, mp->rxq_count);
  2587. err = 0;
  2588. if (pd->phy_node) {
  2589. phydev = of_phy_connect(mp->dev, pd->phy_node,
  2590. mv643xx_eth_adjust_link, 0,
  2591. get_phy_mode(mp));
  2592. if (!phydev)
  2593. err = -ENODEV;
  2594. else
  2595. phy_addr_set(mp, phydev->mdio.addr);
  2596. } else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
  2597. phydev = phy_scan(mp, pd->phy_addr);
  2598. if (IS_ERR(phydev))
  2599. err = PTR_ERR(phydev);
  2600. else
  2601. phy_init(mp, pd->speed, pd->duplex);
  2602. }
  2603. if (err == -ENODEV) {
  2604. err = -EPROBE_DEFER;
  2605. goto out;
  2606. }
  2607. if (err)
  2608. goto out;
  2609. dev->ethtool_ops = &mv643xx_eth_ethtool_ops;
  2610. init_pscr(mp, pd->speed, pd->duplex);
  2611. mib_counters_clear(mp);
  2612. setup_timer(&mp->mib_counters_timer, mib_counters_timer_wrapper,
  2613. (unsigned long)mp);
  2614. mp->mib_counters_timer.expires = jiffies + 30 * HZ;
  2615. spin_lock_init(&mp->mib_counters_lock);
  2616. INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
  2617. netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, NAPI_POLL_WEIGHT);
  2618. setup_timer(&mp->rx_oom, oom_timer_wrapper, (unsigned long)mp);
  2619. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2620. BUG_ON(!res);
  2621. dev->irq = res->start;
  2622. dev->netdev_ops = &mv643xx_eth_netdev_ops;
  2623. dev->watchdog_timeo = 2 * HZ;
  2624. dev->base_addr = 0;
  2625. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
  2626. dev->vlan_features = dev->features;
  2627. dev->features |= NETIF_F_RXCSUM;
  2628. dev->hw_features = dev->features;
  2629. dev->priv_flags |= IFF_UNICAST_FLT;
  2630. dev->gso_max_segs = MV643XX_MAX_TSO_SEGS;
  2631. /* MTU range: 64 - 9500 */
  2632. dev->min_mtu = 64;
  2633. dev->max_mtu = 9500;
  2634. if (mp->shared->win_protect)
  2635. wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
  2636. netif_carrier_off(dev);
  2637. wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
  2638. set_rx_coal(mp, 250);
  2639. set_tx_coal(mp, 0);
  2640. err = register_netdev(dev);
  2641. if (err)
  2642. goto out;
  2643. netdev_notice(dev, "port %d with MAC address %pM\n",
  2644. mp->port_num, dev->dev_addr);
  2645. if (mp->tx_desc_sram_size > 0)
  2646. netdev_notice(dev, "configured with sram\n");
  2647. return 0;
  2648. out:
  2649. if (!IS_ERR(mp->clk))
  2650. clk_disable_unprepare(mp->clk);
  2651. free_netdev(dev);
  2652. return err;
  2653. }
  2654. static int mv643xx_eth_remove(struct platform_device *pdev)
  2655. {
  2656. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2657. struct net_device *dev = mp->dev;
  2658. unregister_netdev(mp->dev);
  2659. if (dev->phydev)
  2660. phy_disconnect(dev->phydev);
  2661. cancel_work_sync(&mp->tx_timeout_task);
  2662. if (!IS_ERR(mp->clk))
  2663. clk_disable_unprepare(mp->clk);
  2664. free_netdev(mp->dev);
  2665. return 0;
  2666. }
  2667. static void mv643xx_eth_shutdown(struct platform_device *pdev)
  2668. {
  2669. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2670. /* Mask all interrupts on ethernet port */
  2671. wrlp(mp, INT_MASK, 0);
  2672. rdlp(mp, INT_MASK);
  2673. if (netif_running(mp->dev))
  2674. port_reset(mp);
  2675. }
  2676. static struct platform_driver mv643xx_eth_driver = {
  2677. .probe = mv643xx_eth_probe,
  2678. .remove = mv643xx_eth_remove,
  2679. .shutdown = mv643xx_eth_shutdown,
  2680. .driver = {
  2681. .name = MV643XX_ETH_NAME,
  2682. },
  2683. };
  2684. static struct platform_driver * const drivers[] = {
  2685. &mv643xx_eth_shared_driver,
  2686. &mv643xx_eth_driver,
  2687. };
  2688. static int __init mv643xx_eth_init_module(void)
  2689. {
  2690. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  2691. }
  2692. module_init(mv643xx_eth_init_module);
  2693. static void __exit mv643xx_eth_cleanup_module(void)
  2694. {
  2695. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  2696. }
  2697. module_exit(mv643xx_eth_cleanup_module);
  2698. MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
  2699. "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
  2700. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  2701. MODULE_LICENSE("GPL");
  2702. MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
  2703. MODULE_ALIAS("platform:" MV643XX_ETH_NAME);