mv643xx_eth.c 78 KB

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