mv643xx_eth.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149
  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. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License
  25. * as published by the Free Software Foundation; either version 2
  26. * of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  36. */
  37. #include <linux/init.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/in.h>
  40. #include <linux/tcp.h>
  41. #include <linux/udp.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/delay.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/mii.h>
  51. #include <linux/mv643xx_eth.h>
  52. #include <asm/io.h>
  53. #include <asm/types.h>
  54. #include <asm/system.h>
  55. static char mv643xx_driver_name[] = "mv643xx_eth";
  56. static char mv643xx_driver_version[] = "1.0";
  57. #define MV643XX_CHECKSUM_OFFLOAD_TX
  58. #define MV643XX_NAPI
  59. #define MV643XX_TX_FAST_REFILL
  60. #undef MV643XX_COAL
  61. #define MV643XX_TX_COAL 100
  62. #ifdef MV643XX_COAL
  63. #define MV643XX_RX_COAL 100
  64. #endif
  65. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  66. #define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
  67. #else
  68. #define MAX_DESCS_PER_SKB 1
  69. #endif
  70. #define ETH_VLAN_HLEN 4
  71. #define ETH_FCS_LEN 4
  72. #define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
  73. #define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
  74. ETH_VLAN_HLEN + ETH_FCS_LEN)
  75. #define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
  76. dma_get_cache_alignment())
  77. /*
  78. * Registers shared between all ports.
  79. */
  80. #define PHY_ADDR 0x0000
  81. #define SMI_REG 0x0004
  82. #define WINDOW_BASE(w) (0x0200 + ((w) << 3))
  83. #define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
  84. #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
  85. #define WINDOW_BAR_ENABLE 0x0290
  86. #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
  87. /*
  88. * Per-port registers.
  89. */
  90. #define PORT_CONFIG(p) (0x0400 + ((p) << 10))
  91. #define UNICAST_PROMISCUOUS_MODE 0x00000001
  92. #define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
  93. #define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
  94. #define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
  95. #define SDMA_CONFIG(p) (0x041c + ((p) << 10))
  96. #define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
  97. #define PORT_STATUS(p) (0x0444 + ((p) << 10))
  98. #define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
  99. #define TX_BW_MTU(p) (0x0458 + ((p) << 10))
  100. #define INT_CAUSE(p) (0x0460 + ((p) << 10))
  101. #define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
  102. #define INT_MASK(p) (0x0468 + ((p) << 10))
  103. #define INT_MASK_EXT(p) (0x046c + ((p) << 10))
  104. #define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
  105. #define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
  106. #define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
  107. #define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
  108. #define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
  109. #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
  110. #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
  111. #define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
  112. /*
  113. * SDMA configuration register.
  114. */
  115. #define RX_BURST_SIZE_4_64BIT (2 << 1)
  116. #define BLM_RX_NO_SWAP (1 << 4)
  117. #define BLM_TX_NO_SWAP (1 << 5)
  118. #define TX_BURST_SIZE_4_64BIT (2 << 22)
  119. #if defined(__BIG_ENDIAN)
  120. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  121. RX_BURST_SIZE_4_64BIT | \
  122. TX_BURST_SIZE_4_64BIT
  123. #elif defined(__LITTLE_ENDIAN)
  124. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  125. RX_BURST_SIZE_4_64BIT | \
  126. BLM_RX_NO_SWAP | \
  127. BLM_TX_NO_SWAP | \
  128. TX_BURST_SIZE_4_64BIT
  129. #else
  130. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  131. #endif
  132. /* These macros describe Ethernet Port serial control reg (PSCR) bits */
  133. #define SERIAL_PORT_DISABLE (0 << 0)
  134. #define SERIAL_PORT_ENABLE (1 << 0)
  135. #define DO_NOT_FORCE_LINK_PASS (0 << 1)
  136. #define FORCE_LINK_PASS (1 << 1)
  137. #define ENABLE_AUTO_NEG_FOR_DUPLX (0 << 2)
  138. #define DISABLE_AUTO_NEG_FOR_DUPLX (1 << 2)
  139. #define ENABLE_AUTO_NEG_FOR_FLOW_CTRL (0 << 3)
  140. #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
  141. #define ADV_NO_FLOW_CTRL (0 << 4)
  142. #define ADV_SYMMETRIC_FLOW_CTRL (1 << 4)
  143. #define FORCE_FC_MODE_NO_PAUSE_DIS_TX (0 << 5)
  144. #define FORCE_FC_MODE_TX_PAUSE_DIS (1 << 5)
  145. #define FORCE_BP_MODE_NO_JAM (0 << 7)
  146. #define FORCE_BP_MODE_JAM_TX (1 << 7)
  147. #define FORCE_BP_MODE_JAM_TX_ON_RX_ERR (2 << 7)
  148. #define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
  149. #define FORCE_LINK_FAIL (0 << 10)
  150. #define DO_NOT_FORCE_LINK_FAIL (1 << 10)
  151. #define RETRANSMIT_16_ATTEMPTS (0 << 11)
  152. #define RETRANSMIT_FOREVER (1 << 11)
  153. #define ENABLE_AUTO_NEG_SPEED_GMII (0 << 13)
  154. #define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
  155. #define DTE_ADV_0 (0 << 14)
  156. #define DTE_ADV_1 (1 << 14)
  157. #define DISABLE_AUTO_NEG_BYPASS (0 << 15)
  158. #define ENABLE_AUTO_NEG_BYPASS (1 << 15)
  159. #define AUTO_NEG_NO_CHANGE (0 << 16)
  160. #define RESTART_AUTO_NEG (1 << 16)
  161. #define MAX_RX_PACKET_1518BYTE (0 << 17)
  162. #define MAX_RX_PACKET_1522BYTE (1 << 17)
  163. #define MAX_RX_PACKET_1552BYTE (2 << 17)
  164. #define MAX_RX_PACKET_9022BYTE (3 << 17)
  165. #define MAX_RX_PACKET_9192BYTE (4 << 17)
  166. #define MAX_RX_PACKET_9700BYTE (5 << 17)
  167. #define MAX_RX_PACKET_MASK (7 << 17)
  168. #define CLR_EXT_LOOPBACK (0 << 20)
  169. #define SET_EXT_LOOPBACK (1 << 20)
  170. #define SET_HALF_DUPLEX_MODE (0 << 21)
  171. #define SET_FULL_DUPLEX_MODE (1 << 21)
  172. #define DISABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (0 << 22)
  173. #define ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (1 << 22)
  174. #define SET_GMII_SPEED_TO_10_100 (0 << 23)
  175. #define SET_GMII_SPEED_TO_1000 (1 << 23)
  176. #define SET_MII_SPEED_TO_10 (0 << 24)
  177. #define SET_MII_SPEED_TO_100 (1 << 24)
  178. #define PORT_SERIAL_CONTROL_DEFAULT_VALUE \
  179. DO_NOT_FORCE_LINK_PASS | \
  180. ENABLE_AUTO_NEG_FOR_DUPLX | \
  181. DISABLE_AUTO_NEG_FOR_FLOW_CTRL | \
  182. ADV_SYMMETRIC_FLOW_CTRL | \
  183. FORCE_FC_MODE_NO_PAUSE_DIS_TX | \
  184. FORCE_BP_MODE_NO_JAM | \
  185. (1 << 9) /* reserved */ | \
  186. DO_NOT_FORCE_LINK_FAIL | \
  187. RETRANSMIT_16_ATTEMPTS | \
  188. ENABLE_AUTO_NEG_SPEED_GMII | \
  189. DTE_ADV_0 | \
  190. DISABLE_AUTO_NEG_BYPASS | \
  191. AUTO_NEG_NO_CHANGE | \
  192. MAX_RX_PACKET_9700BYTE | \
  193. CLR_EXT_LOOPBACK | \
  194. SET_FULL_DUPLEX_MODE | \
  195. ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX
  196. /* These macros describe Ethernet Serial Status reg (PSR) bits */
  197. #define PORT_STATUS_MODE_10_BIT (1 << 0)
  198. #define PORT_STATUS_LINK_UP (1 << 1)
  199. #define PORT_STATUS_FULL_DUPLEX (1 << 2)
  200. #define PORT_STATUS_FLOW_CONTROL (1 << 3)
  201. #define PORT_STATUS_GMII_1000 (1 << 4)
  202. #define PORT_STATUS_MII_100 (1 << 5)
  203. /* PSR bit 6 is undocumented */
  204. #define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
  205. #define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
  206. #define PORT_STATUS_PARTITION (1 << 9)
  207. #define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
  208. /* PSR bits 11-31 are reserved */
  209. #define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
  210. #define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
  211. #define DESC_SIZE 64
  212. #define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
  213. #define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
  214. #define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
  215. #define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
  216. #define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
  217. #define ETH_INT_CAUSE_EXT 0x00000002
  218. #define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
  219. #define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
  220. #define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
  221. #define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
  222. #define ETH_INT_CAUSE_PHY 0x00010000
  223. #define ETH_INT_CAUSE_STATE 0x00100000
  224. #define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
  225. ETH_INT_CAUSE_STATE)
  226. #define ETH_INT_MASK_ALL 0x00000000
  227. #define ETH_INT_MASK_ALL_EXT 0x00000000
  228. #define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
  229. #define PHY_WAIT_MICRO_SECONDS 10
  230. /* Buffer offset from buffer pointer */
  231. #define RX_BUF_OFFSET 0x2
  232. /* Gigabit Ethernet Unit Global Registers */
  233. /* MIB Counters register definitions */
  234. #define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
  235. #define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
  236. #define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
  237. #define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
  238. #define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
  239. #define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
  240. #define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
  241. #define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
  242. #define ETH_MIB_FRAMES_64_OCTETS 0x20
  243. #define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
  244. #define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
  245. #define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
  246. #define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
  247. #define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
  248. #define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
  249. #define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
  250. #define ETH_MIB_GOOD_FRAMES_SENT 0x40
  251. #define ETH_MIB_EXCESSIVE_COLLISION 0x44
  252. #define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
  253. #define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
  254. #define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
  255. #define ETH_MIB_FC_SENT 0x54
  256. #define ETH_MIB_GOOD_FC_RECEIVED 0x58
  257. #define ETH_MIB_BAD_FC_RECEIVED 0x5c
  258. #define ETH_MIB_UNDERSIZE_RECEIVED 0x60
  259. #define ETH_MIB_FRAGMENTS_RECEIVED 0x64
  260. #define ETH_MIB_OVERSIZE_RECEIVED 0x68
  261. #define ETH_MIB_JABBER_RECEIVED 0x6c
  262. #define ETH_MIB_MAC_RECEIVE_ERROR 0x70
  263. #define ETH_MIB_BAD_CRC_EVENT 0x74
  264. #define ETH_MIB_COLLISION 0x78
  265. #define ETH_MIB_LATE_COLLISION 0x7c
  266. /* Port serial status reg (PSR) */
  267. #define ETH_INTERFACE_PCM 0x00000001
  268. #define ETH_LINK_IS_UP 0x00000002
  269. #define ETH_PORT_AT_FULL_DUPLEX 0x00000004
  270. #define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
  271. #define ETH_GMII_SPEED_1000 0x00000010
  272. #define ETH_MII_SPEED_100 0x00000020
  273. #define ETH_TX_IN_PROGRESS 0x00000080
  274. #define ETH_BYPASS_ACTIVE 0x00000100
  275. #define ETH_PORT_AT_PARTITION_STATE 0x00000200
  276. #define ETH_PORT_TX_FIFO_EMPTY 0x00000400
  277. /* SMI reg */
  278. #define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
  279. #define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
  280. #define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
  281. #define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
  282. /* Interrupt Cause Register Bit Definitions */
  283. /* SDMA command status fields macros */
  284. /* Tx & Rx descriptors status */
  285. #define ETH_ERROR_SUMMARY 0x00000001
  286. /* Tx & Rx descriptors command */
  287. #define ETH_BUFFER_OWNED_BY_DMA 0x80000000
  288. /* Tx descriptors status */
  289. #define ETH_LC_ERROR 0
  290. #define ETH_UR_ERROR 0x00000002
  291. #define ETH_RL_ERROR 0x00000004
  292. #define ETH_LLC_SNAP_FORMAT 0x00000200
  293. /* Rx descriptors status */
  294. #define ETH_OVERRUN_ERROR 0x00000002
  295. #define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
  296. #define ETH_RESOURCE_ERROR 0x00000006
  297. #define ETH_VLAN_TAGGED 0x00080000
  298. #define ETH_BPDU_FRAME 0x00100000
  299. #define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
  300. #define ETH_OTHER_FRAME_TYPE 0x00400000
  301. #define ETH_LAYER_2_IS_ETH_V_2 0x00800000
  302. #define ETH_FRAME_TYPE_IP_V_4 0x01000000
  303. #define ETH_FRAME_HEADER_OK 0x02000000
  304. #define ETH_RX_LAST_DESC 0x04000000
  305. #define ETH_RX_FIRST_DESC 0x08000000
  306. #define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
  307. #define ETH_RX_ENABLE_INTERRUPT 0x20000000
  308. #define ETH_LAYER_4_CHECKSUM_OK 0x40000000
  309. /* Rx descriptors byte count */
  310. #define ETH_FRAME_FRAGMENTED 0x00000004
  311. /* Tx descriptors command */
  312. #define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
  313. #define ETH_FRAME_SET_TO_VLAN 0x00008000
  314. #define ETH_UDP_FRAME 0x00010000
  315. #define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
  316. #define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
  317. #define ETH_ZERO_PADDING 0x00080000
  318. #define ETH_TX_LAST_DESC 0x00100000
  319. #define ETH_TX_FIRST_DESC 0x00200000
  320. #define ETH_GEN_CRC 0x00400000
  321. #define ETH_TX_ENABLE_INTERRUPT 0x00800000
  322. #define ETH_AUTO_MODE 0x40000000
  323. #define ETH_TX_IHL_SHIFT 11
  324. /* typedefs */
  325. typedef enum _eth_func_ret_status {
  326. ETH_OK, /* Returned as expected. */
  327. ETH_ERROR, /* Fundamental error. */
  328. ETH_RETRY, /* Could not process request. Try later.*/
  329. ETH_END_OF_JOB, /* Ring has nothing to process. */
  330. ETH_QUEUE_FULL, /* Ring resource error. */
  331. ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
  332. } ETH_FUNC_RET_STATUS;
  333. /* These are for big-endian machines. Little endian needs different
  334. * definitions.
  335. */
  336. #if defined(__BIG_ENDIAN)
  337. struct eth_rx_desc {
  338. u16 byte_cnt; /* Descriptor buffer byte count */
  339. u16 buf_size; /* Buffer size */
  340. u32 cmd_sts; /* Descriptor command status */
  341. u32 next_desc_ptr; /* Next descriptor pointer */
  342. u32 buf_ptr; /* Descriptor buffer pointer */
  343. };
  344. struct eth_tx_desc {
  345. u16 byte_cnt; /* buffer byte count */
  346. u16 l4i_chk; /* CPU provided TCP checksum */
  347. u32 cmd_sts; /* Command/status field */
  348. u32 next_desc_ptr; /* Pointer to next descriptor */
  349. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  350. };
  351. #elif defined(__LITTLE_ENDIAN)
  352. struct eth_rx_desc {
  353. u32 cmd_sts; /* Descriptor command status */
  354. u16 buf_size; /* Buffer size */
  355. u16 byte_cnt; /* Descriptor buffer byte count */
  356. u32 buf_ptr; /* Descriptor buffer pointer */
  357. u32 next_desc_ptr; /* Next descriptor pointer */
  358. };
  359. struct eth_tx_desc {
  360. u32 cmd_sts; /* Command/status field */
  361. u16 l4i_chk; /* CPU provided TCP checksum */
  362. u16 byte_cnt; /* buffer byte count */
  363. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  364. u32 next_desc_ptr; /* Pointer to next descriptor */
  365. };
  366. #else
  367. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  368. #endif
  369. /* Unified struct for Rx and Tx operations. The user is not required to */
  370. /* be familier with neither Tx nor Rx descriptors. */
  371. struct pkt_info {
  372. unsigned short byte_cnt; /* Descriptor buffer byte count */
  373. unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
  374. unsigned int cmd_sts; /* Descriptor command status */
  375. dma_addr_t buf_ptr; /* Descriptor buffer pointer */
  376. struct sk_buff *return_info; /* User resource return information */
  377. };
  378. /* global *******************************************************************/
  379. struct mv643xx_shared_private {
  380. void __iomem *eth_base;
  381. /* used to protect SMI_REG, which is shared across ports */
  382. spinlock_t phy_lock;
  383. u32 win_protect;
  384. unsigned int t_clk;
  385. };
  386. /* per-port *****************************************************************/
  387. struct mv643xx_mib_counters {
  388. u64 good_octets_received;
  389. u32 bad_octets_received;
  390. u32 internal_mac_transmit_err;
  391. u32 good_frames_received;
  392. u32 bad_frames_received;
  393. u32 broadcast_frames_received;
  394. u32 multicast_frames_received;
  395. u32 frames_64_octets;
  396. u32 frames_65_to_127_octets;
  397. u32 frames_128_to_255_octets;
  398. u32 frames_256_to_511_octets;
  399. u32 frames_512_to_1023_octets;
  400. u32 frames_1024_to_max_octets;
  401. u64 good_octets_sent;
  402. u32 good_frames_sent;
  403. u32 excessive_collision;
  404. u32 multicast_frames_sent;
  405. u32 broadcast_frames_sent;
  406. u32 unrec_mac_control_received;
  407. u32 fc_sent;
  408. u32 good_fc_received;
  409. u32 bad_fc_received;
  410. u32 undersize_received;
  411. u32 fragments_received;
  412. u32 oversize_received;
  413. u32 jabber_received;
  414. u32 mac_receive_error;
  415. u32 bad_crc_event;
  416. u32 collision;
  417. u32 late_collision;
  418. };
  419. struct mv643xx_private {
  420. struct mv643xx_shared_private *shared;
  421. int port_num; /* User Ethernet port number */
  422. struct mv643xx_shared_private *shared_smi;
  423. u32 rx_sram_addr; /* Base address of rx sram area */
  424. u32 rx_sram_size; /* Size of rx sram area */
  425. u32 tx_sram_addr; /* Base address of tx sram area */
  426. u32 tx_sram_size; /* Size of tx sram area */
  427. int rx_resource_err; /* Rx ring resource error flag */
  428. /* Tx/Rx rings managment indexes fields. For driver use */
  429. /* Next available and first returning Rx resource */
  430. int rx_curr_desc_q, rx_used_desc_q;
  431. /* Next available and first returning Tx resource */
  432. int tx_curr_desc_q, tx_used_desc_q;
  433. #ifdef MV643XX_TX_FAST_REFILL
  434. u32 tx_clean_threshold;
  435. #endif
  436. struct eth_rx_desc *p_rx_desc_area;
  437. dma_addr_t rx_desc_dma;
  438. int rx_desc_area_size;
  439. struct sk_buff **rx_skb;
  440. struct eth_tx_desc *p_tx_desc_area;
  441. dma_addr_t tx_desc_dma;
  442. int tx_desc_area_size;
  443. struct sk_buff **tx_skb;
  444. struct work_struct tx_timeout_task;
  445. struct net_device *dev;
  446. struct napi_struct napi;
  447. struct net_device_stats stats;
  448. struct mv643xx_mib_counters mib_counters;
  449. spinlock_t lock;
  450. /* Size of Tx Ring per queue */
  451. int tx_ring_size;
  452. /* Number of tx descriptors in use */
  453. int tx_desc_count;
  454. /* Size of Rx Ring per queue */
  455. int rx_ring_size;
  456. /* Number of rx descriptors in use */
  457. int rx_desc_count;
  458. /*
  459. * Used in case RX Ring is empty, which can be caused when
  460. * system does not have resources (skb's)
  461. */
  462. struct timer_list timeout;
  463. u32 rx_int_coal;
  464. u32 tx_int_coal;
  465. struct mii_if_info mii;
  466. };
  467. /* port register accessors **************************************************/
  468. static inline u32 rdl(struct mv643xx_private *mp, int offset)
  469. {
  470. return readl(mp->shared->eth_base + offset);
  471. }
  472. static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
  473. {
  474. writel(data, mp->shared->eth_base + offset);
  475. }
  476. /* rxq/txq helper functions *************************************************/
  477. static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
  478. unsigned int queues)
  479. {
  480. wrl(mp, RXQ_COMMAND(mp->port_num), queues);
  481. }
  482. static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
  483. {
  484. unsigned int port_num = mp->port_num;
  485. u32 queues;
  486. /* Stop Rx port activity. Check port Rx activity. */
  487. queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
  488. if (queues) {
  489. /* Issue stop command for active queues only */
  490. wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
  491. /* Wait for all Rx activity to terminate. */
  492. /* Check port cause register that all Rx queues are stopped */
  493. while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
  494. udelay(PHY_WAIT_MICRO_SECONDS);
  495. }
  496. return queues;
  497. }
  498. static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
  499. unsigned int queues)
  500. {
  501. wrl(mp, TXQ_COMMAND(mp->port_num), queues);
  502. }
  503. static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
  504. {
  505. unsigned int port_num = mp->port_num;
  506. u32 queues;
  507. /* Stop Tx port activity. Check port Tx activity. */
  508. queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
  509. if (queues) {
  510. /* Issue stop command for active queues only */
  511. wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
  512. /* Wait for all Tx activity to terminate. */
  513. /* Check port cause register that all Tx queues are stopped */
  514. while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
  515. udelay(PHY_WAIT_MICRO_SECONDS);
  516. /* Wait for Tx FIFO to empty */
  517. while (rdl(mp, PORT_STATUS(port_num)) & ETH_PORT_TX_FIFO_EMPTY)
  518. udelay(PHY_WAIT_MICRO_SECONDS);
  519. }
  520. return queues;
  521. }
  522. /* rx ***********************************************************************/
  523. static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
  524. /*
  525. * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
  526. *
  527. * DESCRIPTION:
  528. * This routine returns a Rx buffer back to the Rx ring. It retrieves the
  529. * next 'used' descriptor and attached the returned buffer to it.
  530. * In case the Rx ring was in "resource error" condition, where there are
  531. * no available Rx resources, the function resets the resource error flag.
  532. *
  533. * INPUT:
  534. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  535. * struct pkt_info *p_pkt_info Information on returned buffer.
  536. *
  537. * OUTPUT:
  538. * New available Rx resource in Rx descriptor ring.
  539. *
  540. * RETURN:
  541. * ETH_ERROR in case the routine can not access Rx desc ring.
  542. * ETH_OK otherwise.
  543. */
  544. static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
  545. struct pkt_info *p_pkt_info)
  546. {
  547. int used_rx_desc; /* Where to return Rx resource */
  548. volatile struct eth_rx_desc *p_used_rx_desc;
  549. unsigned long flags;
  550. spin_lock_irqsave(&mp->lock, flags);
  551. /* Get 'used' Rx descriptor */
  552. used_rx_desc = mp->rx_used_desc_q;
  553. p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
  554. p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
  555. p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
  556. mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
  557. /* Flush the write pipe */
  558. /* Return the descriptor to DMA ownership */
  559. wmb();
  560. p_used_rx_desc->cmd_sts =
  561. ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
  562. wmb();
  563. /* Move the used descriptor pointer to the next descriptor */
  564. mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
  565. /* Any Rx return cancels the Rx resource error status */
  566. mp->rx_resource_err = 0;
  567. spin_unlock_irqrestore(&mp->lock, flags);
  568. return ETH_OK;
  569. }
  570. /*
  571. * mv643xx_eth_rx_refill_descs
  572. *
  573. * Fills / refills RX queue on a certain gigabit ethernet port
  574. *
  575. * Input : pointer to ethernet interface network device structure
  576. * Output : N/A
  577. */
  578. static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
  579. {
  580. struct mv643xx_private *mp = netdev_priv(dev);
  581. struct pkt_info pkt_info;
  582. struct sk_buff *skb;
  583. int unaligned;
  584. while (mp->rx_desc_count < mp->rx_ring_size) {
  585. skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
  586. if (!skb)
  587. break;
  588. mp->rx_desc_count++;
  589. unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
  590. if (unaligned)
  591. skb_reserve(skb, dma_get_cache_alignment() - unaligned);
  592. pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
  593. pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
  594. pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
  595. ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
  596. pkt_info.return_info = skb;
  597. if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
  598. printk(KERN_ERR
  599. "%s: Error allocating RX Ring\n", dev->name);
  600. break;
  601. }
  602. skb_reserve(skb, ETH_HW_IP_ALIGN);
  603. }
  604. /*
  605. * If RX ring is empty of SKB, set a timer to try allocating
  606. * again at a later time.
  607. */
  608. if (mp->rx_desc_count == 0) {
  609. printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
  610. mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
  611. add_timer(&mp->timeout);
  612. }
  613. }
  614. /*
  615. * mv643xx_eth_rx_refill_descs_timer_wrapper
  616. *
  617. * Timer routine to wake up RX queue filling task. This function is
  618. * used only in case the RX queue is empty, and all alloc_skb has
  619. * failed (due to out of memory event).
  620. *
  621. * Input : pointer to ethernet interface network device structure
  622. * Output : N/A
  623. */
  624. static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
  625. {
  626. mv643xx_eth_rx_refill_descs((struct net_device *)data);
  627. }
  628. /*
  629. * eth_port_receive - Get received information from Rx ring.
  630. *
  631. * DESCRIPTION:
  632. * This routine returns the received data to the caller. There is no
  633. * data copying during routine operation. All information is returned
  634. * using pointer to packet information struct passed from the caller.
  635. * If the routine exhausts Rx ring resources then the resource error flag
  636. * is set.
  637. *
  638. * INPUT:
  639. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  640. * struct pkt_info *p_pkt_info User packet buffer.
  641. *
  642. * OUTPUT:
  643. * Rx ring current and used indexes are updated.
  644. *
  645. * RETURN:
  646. * ETH_ERROR in case the routine can not access Rx desc ring.
  647. * ETH_QUEUE_FULL if Rx ring resources are exhausted.
  648. * ETH_END_OF_JOB if there is no received data.
  649. * ETH_OK otherwise.
  650. */
  651. static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
  652. struct pkt_info *p_pkt_info)
  653. {
  654. int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
  655. volatile struct eth_rx_desc *p_rx_desc;
  656. unsigned int command_status;
  657. unsigned long flags;
  658. /* Do not process Rx ring in case of Rx ring resource error */
  659. if (mp->rx_resource_err)
  660. return ETH_QUEUE_FULL;
  661. spin_lock_irqsave(&mp->lock, flags);
  662. /* Get the Rx Desc ring 'curr and 'used' indexes */
  663. rx_curr_desc = mp->rx_curr_desc_q;
  664. rx_used_desc = mp->rx_used_desc_q;
  665. p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
  666. /* The following parameters are used to save readings from memory */
  667. command_status = p_rx_desc->cmd_sts;
  668. rmb();
  669. /* Nothing to receive... */
  670. if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
  671. spin_unlock_irqrestore(&mp->lock, flags);
  672. return ETH_END_OF_JOB;
  673. }
  674. p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
  675. p_pkt_info->cmd_sts = command_status;
  676. p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
  677. p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
  678. p_pkt_info->l4i_chk = p_rx_desc->buf_size;
  679. /*
  680. * Clean the return info field to indicate that the
  681. * packet has been moved to the upper layers
  682. */
  683. mp->rx_skb[rx_curr_desc] = NULL;
  684. /* Update current index in data structure */
  685. rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
  686. mp->rx_curr_desc_q = rx_next_curr_desc;
  687. /* Rx descriptors exhausted. Set the Rx ring resource error flag */
  688. if (rx_next_curr_desc == rx_used_desc)
  689. mp->rx_resource_err = 1;
  690. spin_unlock_irqrestore(&mp->lock, flags);
  691. return ETH_OK;
  692. }
  693. /*
  694. * mv643xx_eth_receive
  695. *
  696. * This function is forward packets that are received from the port's
  697. * queues toward kernel core or FastRoute them to another interface.
  698. *
  699. * Input : dev - a pointer to the required interface
  700. * max - maximum number to receive (0 means unlimted)
  701. *
  702. * Output : number of served packets
  703. */
  704. static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
  705. {
  706. struct mv643xx_private *mp = netdev_priv(dev);
  707. struct net_device_stats *stats = &dev->stats;
  708. unsigned int received_packets = 0;
  709. struct sk_buff *skb;
  710. struct pkt_info pkt_info;
  711. while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
  712. dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
  713. DMA_FROM_DEVICE);
  714. mp->rx_desc_count--;
  715. received_packets++;
  716. /*
  717. * Update statistics.
  718. * Note byte count includes 4 byte CRC count
  719. */
  720. stats->rx_packets++;
  721. stats->rx_bytes += pkt_info.byte_cnt;
  722. skb = pkt_info.return_info;
  723. /*
  724. * In case received a packet without first / last bits on OR
  725. * the error summary bit is on, the packets needs to be dropeed.
  726. */
  727. if (((pkt_info.cmd_sts
  728. & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
  729. (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
  730. || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
  731. stats->rx_dropped++;
  732. if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
  733. ETH_RX_LAST_DESC)) !=
  734. (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
  735. if (net_ratelimit())
  736. printk(KERN_ERR
  737. "%s: Received packet spread "
  738. "on multiple descriptors\n",
  739. dev->name);
  740. }
  741. if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
  742. stats->rx_errors++;
  743. dev_kfree_skb_irq(skb);
  744. } else {
  745. /*
  746. * The -4 is for the CRC in the trailer of the
  747. * received packet
  748. */
  749. skb_put(skb, pkt_info.byte_cnt - 4);
  750. if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
  751. skb->ip_summed = CHECKSUM_UNNECESSARY;
  752. skb->csum = htons(
  753. (pkt_info.cmd_sts & 0x0007fff8) >> 3);
  754. }
  755. skb->protocol = eth_type_trans(skb, dev);
  756. #ifdef MV643XX_NAPI
  757. netif_receive_skb(skb);
  758. #else
  759. netif_rx(skb);
  760. #endif
  761. }
  762. dev->last_rx = jiffies;
  763. }
  764. mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
  765. return received_packets;
  766. }
  767. #ifdef MV643XX_NAPI
  768. /*
  769. * mv643xx_poll
  770. *
  771. * This function is used in case of NAPI
  772. */
  773. static int mv643xx_poll(struct napi_struct *napi, int budget)
  774. {
  775. struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
  776. struct net_device *dev = mp->dev;
  777. unsigned int port_num = mp->port_num;
  778. int work_done;
  779. #ifdef MV643XX_TX_FAST_REFILL
  780. if (++mp->tx_clean_threshold > 5) {
  781. mv643xx_eth_free_completed_tx_descs(dev);
  782. mp->tx_clean_threshold = 0;
  783. }
  784. #endif
  785. work_done = 0;
  786. if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
  787. != (u32) mp->rx_used_desc_q)
  788. work_done = mv643xx_eth_receive_queue(dev, budget);
  789. if (work_done < budget) {
  790. netif_rx_complete(dev, napi);
  791. wrl(mp, INT_CAUSE(port_num), 0);
  792. wrl(mp, INT_CAUSE_EXT(port_num), 0);
  793. wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
  794. }
  795. return work_done;
  796. }
  797. #endif
  798. /* tx ***********************************************************************/
  799. /**
  800. * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
  801. *
  802. * Hardware can't handle unaligned fragments smaller than 9 bytes.
  803. * This helper function detects that case.
  804. */
  805. static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
  806. {
  807. unsigned int frag;
  808. skb_frag_t *fragp;
  809. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  810. fragp = &skb_shinfo(skb)->frags[frag];
  811. if (fragp->size <= 8 && fragp->page_offset & 0x7)
  812. return 1;
  813. }
  814. return 0;
  815. }
  816. /**
  817. * eth_alloc_tx_desc_index - return the index of the next available tx desc
  818. */
  819. static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
  820. {
  821. int tx_desc_curr;
  822. BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
  823. tx_desc_curr = mp->tx_curr_desc_q;
  824. mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
  825. BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
  826. return tx_desc_curr;
  827. }
  828. /**
  829. * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
  830. *
  831. * Ensure the data for each fragment to be transmitted is mapped properly,
  832. * then fill in descriptors in the tx hw queue.
  833. */
  834. static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
  835. struct sk_buff *skb)
  836. {
  837. int frag;
  838. int tx_index;
  839. struct eth_tx_desc *desc;
  840. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  841. skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
  842. tx_index = eth_alloc_tx_desc_index(mp);
  843. desc = &mp->p_tx_desc_area[tx_index];
  844. desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
  845. /* Last Frag enables interrupt and frees the skb */
  846. if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
  847. desc->cmd_sts |= ETH_ZERO_PADDING |
  848. ETH_TX_LAST_DESC |
  849. ETH_TX_ENABLE_INTERRUPT;
  850. mp->tx_skb[tx_index] = skb;
  851. } else
  852. mp->tx_skb[tx_index] = NULL;
  853. desc = &mp->p_tx_desc_area[tx_index];
  854. desc->l4i_chk = 0;
  855. desc->byte_cnt = this_frag->size;
  856. desc->buf_ptr = dma_map_page(NULL, this_frag->page,
  857. this_frag->page_offset,
  858. this_frag->size,
  859. DMA_TO_DEVICE);
  860. }
  861. }
  862. static inline __be16 sum16_as_be(__sum16 sum)
  863. {
  864. return (__force __be16)sum;
  865. }
  866. /**
  867. * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
  868. *
  869. * Ensure the data for an skb to be transmitted is mapped properly,
  870. * then fill in descriptors in the tx hw queue and start the hardware.
  871. */
  872. static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
  873. struct sk_buff *skb)
  874. {
  875. int tx_index;
  876. struct eth_tx_desc *desc;
  877. u32 cmd_sts;
  878. int length;
  879. int nr_frags = skb_shinfo(skb)->nr_frags;
  880. cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
  881. tx_index = eth_alloc_tx_desc_index(mp);
  882. desc = &mp->p_tx_desc_area[tx_index];
  883. if (nr_frags) {
  884. eth_tx_fill_frag_descs(mp, skb);
  885. length = skb_headlen(skb);
  886. mp->tx_skb[tx_index] = NULL;
  887. } else {
  888. cmd_sts |= ETH_ZERO_PADDING |
  889. ETH_TX_LAST_DESC |
  890. ETH_TX_ENABLE_INTERRUPT;
  891. length = skb->len;
  892. mp->tx_skb[tx_index] = skb;
  893. }
  894. desc->byte_cnt = length;
  895. desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
  896. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  897. BUG_ON(skb->protocol != htons(ETH_P_IP));
  898. cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
  899. ETH_GEN_IP_V_4_CHECKSUM |
  900. ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
  901. switch (ip_hdr(skb)->protocol) {
  902. case IPPROTO_UDP:
  903. cmd_sts |= ETH_UDP_FRAME;
  904. desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
  905. break;
  906. case IPPROTO_TCP:
  907. desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
  908. break;
  909. default:
  910. BUG();
  911. }
  912. } else {
  913. /* Errata BTS #50, IHL must be 5 if no HW checksum */
  914. cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
  915. desc->l4i_chk = 0;
  916. }
  917. /* ensure all other descriptors are written before first cmd_sts */
  918. wmb();
  919. desc->cmd_sts = cmd_sts;
  920. /* ensure all descriptors are written before poking hardware */
  921. wmb();
  922. mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
  923. mp->tx_desc_count += nr_frags + 1;
  924. }
  925. /**
  926. * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
  927. *
  928. */
  929. static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  930. {
  931. struct mv643xx_private *mp = netdev_priv(dev);
  932. struct net_device_stats *stats = &dev->stats;
  933. unsigned long flags;
  934. BUG_ON(netif_queue_stopped(dev));
  935. if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
  936. stats->tx_dropped++;
  937. printk(KERN_DEBUG "%s: failed to linearize tiny "
  938. "unaligned fragment\n", dev->name);
  939. return NETDEV_TX_BUSY;
  940. }
  941. spin_lock_irqsave(&mp->lock, flags);
  942. if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
  943. printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
  944. netif_stop_queue(dev);
  945. spin_unlock_irqrestore(&mp->lock, flags);
  946. return NETDEV_TX_BUSY;
  947. }
  948. eth_tx_submit_descs_for_skb(mp, skb);
  949. stats->tx_bytes += skb->len;
  950. stats->tx_packets++;
  951. dev->trans_start = jiffies;
  952. if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
  953. netif_stop_queue(dev);
  954. spin_unlock_irqrestore(&mp->lock, flags);
  955. return NETDEV_TX_OK;
  956. }
  957. /* mii management interface *************************************************/
  958. static int ethernet_phy_get(struct mv643xx_private *mp);
  959. /*
  960. * eth_port_read_smi_reg - Read PHY registers
  961. *
  962. * DESCRIPTION:
  963. * This routine utilize the SMI interface to interact with the PHY in
  964. * order to perform PHY register read.
  965. *
  966. * INPUT:
  967. * struct mv643xx_private *mp Ethernet Port.
  968. * unsigned int phy_reg PHY register address offset.
  969. * unsigned int *value Register value buffer.
  970. *
  971. * OUTPUT:
  972. * Write the value of a specified PHY register into given buffer.
  973. *
  974. * RETURN:
  975. * false if the PHY is busy or read data is not in valid state.
  976. * true otherwise.
  977. *
  978. */
  979. static void eth_port_read_smi_reg(struct mv643xx_private *mp,
  980. unsigned int phy_reg, unsigned int *value)
  981. {
  982. void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
  983. int phy_addr = ethernet_phy_get(mp);
  984. unsigned long flags;
  985. int i;
  986. /* the SMI register is a shared resource */
  987. spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
  988. /* wait for the SMI register to become available */
  989. for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
  990. if (i == PHY_WAIT_ITERATIONS) {
  991. printk("%s: PHY busy timeout\n", mp->dev->name);
  992. goto out;
  993. }
  994. udelay(PHY_WAIT_MICRO_SECONDS);
  995. }
  996. writel((phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ,
  997. smi_reg);
  998. /* now wait for the data to be valid */
  999. for (i = 0; !(readl(smi_reg) & ETH_SMI_READ_VALID); i++) {
  1000. if (i == PHY_WAIT_ITERATIONS) {
  1001. printk("%s: PHY read timeout\n", mp->dev->name);
  1002. goto out;
  1003. }
  1004. udelay(PHY_WAIT_MICRO_SECONDS);
  1005. }
  1006. *value = readl(smi_reg) & 0xffff;
  1007. out:
  1008. spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
  1009. }
  1010. /*
  1011. * eth_port_write_smi_reg - Write to PHY registers
  1012. *
  1013. * DESCRIPTION:
  1014. * This routine utilize the SMI interface to interact with the PHY in
  1015. * order to perform writes to PHY registers.
  1016. *
  1017. * INPUT:
  1018. * struct mv643xx_private *mp Ethernet Port.
  1019. * unsigned int phy_reg PHY register address offset.
  1020. * unsigned int value Register value.
  1021. *
  1022. * OUTPUT:
  1023. * Write the given value to the specified PHY register.
  1024. *
  1025. * RETURN:
  1026. * false if the PHY is busy.
  1027. * true otherwise.
  1028. *
  1029. */
  1030. static void eth_port_write_smi_reg(struct mv643xx_private *mp,
  1031. unsigned int phy_reg, unsigned int value)
  1032. {
  1033. void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
  1034. int phy_addr = ethernet_phy_get(mp);
  1035. unsigned long flags;
  1036. int i;
  1037. /* the SMI register is a shared resource */
  1038. spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
  1039. /* wait for the SMI register to become available */
  1040. for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
  1041. if (i == PHY_WAIT_ITERATIONS) {
  1042. printk("%s: PHY busy timeout\n", mp->dev->name);
  1043. goto out;
  1044. }
  1045. udelay(PHY_WAIT_MICRO_SECONDS);
  1046. }
  1047. writel((phy_addr << 16) | (phy_reg << 21) |
  1048. ETH_SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
  1049. out:
  1050. spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
  1051. }
  1052. /* mib counters *************************************************************/
  1053. /*
  1054. * eth_clear_mib_counters - Clear all MIB counters
  1055. *
  1056. * DESCRIPTION:
  1057. * This function clears all MIB counters of a specific ethernet port.
  1058. * A read from the MIB counter will reset the counter.
  1059. *
  1060. * INPUT:
  1061. * struct mv643xx_private *mp Ethernet Port.
  1062. *
  1063. * OUTPUT:
  1064. * After reading all MIB counters, the counters resets.
  1065. *
  1066. * RETURN:
  1067. * MIB counter value.
  1068. *
  1069. */
  1070. static void eth_clear_mib_counters(struct mv643xx_private *mp)
  1071. {
  1072. unsigned int port_num = mp->port_num;
  1073. int i;
  1074. /* Perform dummy reads from MIB counters */
  1075. for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
  1076. i += 4)
  1077. rdl(mp, MIB_COUNTERS(port_num) + i);
  1078. }
  1079. static inline u32 read_mib(struct mv643xx_private *mp, int offset)
  1080. {
  1081. return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
  1082. }
  1083. static void eth_update_mib_counters(struct mv643xx_private *mp)
  1084. {
  1085. struct mv643xx_mib_counters *p = &mp->mib_counters;
  1086. int offset;
  1087. p->good_octets_received +=
  1088. read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
  1089. p->good_octets_received +=
  1090. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
  1091. for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
  1092. offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
  1093. offset += 4)
  1094. *(u32 *)((char *)p + offset) += read_mib(mp, offset);
  1095. p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
  1096. p->good_octets_sent +=
  1097. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
  1098. for (offset = ETH_MIB_GOOD_FRAMES_SENT;
  1099. offset <= ETH_MIB_LATE_COLLISION;
  1100. offset += 4)
  1101. *(u32 *)((char *)p + offset) += read_mib(mp, offset);
  1102. }
  1103. /* ethtool ******************************************************************/
  1104. struct mv643xx_stats {
  1105. char stat_string[ETH_GSTRING_LEN];
  1106. int sizeof_stat;
  1107. int stat_offset;
  1108. };
  1109. #define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
  1110. offsetof(struct mv643xx_private, m)
  1111. static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
  1112. { "rx_packets", MV643XX_STAT(stats.rx_packets) },
  1113. { "tx_packets", MV643XX_STAT(stats.tx_packets) },
  1114. { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
  1115. { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
  1116. { "rx_errors", MV643XX_STAT(stats.rx_errors) },
  1117. { "tx_errors", MV643XX_STAT(stats.tx_errors) },
  1118. { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
  1119. { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
  1120. { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
  1121. { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
  1122. { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
  1123. { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
  1124. { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
  1125. { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
  1126. { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
  1127. { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
  1128. { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
  1129. { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
  1130. { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
  1131. { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
  1132. { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
  1133. { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
  1134. { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
  1135. { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
  1136. { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
  1137. { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
  1138. { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
  1139. { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
  1140. { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
  1141. { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
  1142. { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
  1143. { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
  1144. { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
  1145. { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
  1146. { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
  1147. { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
  1148. { "collision", MV643XX_STAT(mib_counters.collision) },
  1149. { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
  1150. };
  1151. #define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
  1152. static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1153. {
  1154. struct mv643xx_private *mp = netdev_priv(dev);
  1155. int err;
  1156. spin_lock_irq(&mp->lock);
  1157. err = mii_ethtool_gset(&mp->mii, cmd);
  1158. spin_unlock_irq(&mp->lock);
  1159. /* The PHY may support 1000baseT_Half, but the mv643xx does not */
  1160. cmd->supported &= ~SUPPORTED_1000baseT_Half;
  1161. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  1162. return err;
  1163. }
  1164. static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1165. {
  1166. struct mv643xx_private *mp = netdev_priv(dev);
  1167. int err;
  1168. spin_lock_irq(&mp->lock);
  1169. err = mii_ethtool_sset(&mp->mii, cmd);
  1170. spin_unlock_irq(&mp->lock);
  1171. return err;
  1172. }
  1173. static void mv643xx_get_drvinfo(struct net_device *netdev,
  1174. struct ethtool_drvinfo *drvinfo)
  1175. {
  1176. strncpy(drvinfo->driver, mv643xx_driver_name, 32);
  1177. strncpy(drvinfo->version, mv643xx_driver_version, 32);
  1178. strncpy(drvinfo->fw_version, "N/A", 32);
  1179. strncpy(drvinfo->bus_info, "mv643xx", 32);
  1180. drvinfo->n_stats = MV643XX_STATS_LEN;
  1181. }
  1182. static int mv643xx_eth_nway_restart(struct net_device *dev)
  1183. {
  1184. struct mv643xx_private *mp = netdev_priv(dev);
  1185. return mii_nway_restart(&mp->mii);
  1186. }
  1187. static u32 mv643xx_eth_get_link(struct net_device *dev)
  1188. {
  1189. struct mv643xx_private *mp = netdev_priv(dev);
  1190. return mii_link_ok(&mp->mii);
  1191. }
  1192. static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
  1193. uint8_t *data)
  1194. {
  1195. int i;
  1196. switch(stringset) {
  1197. case ETH_SS_STATS:
  1198. for (i=0; i < MV643XX_STATS_LEN; i++) {
  1199. memcpy(data + i * ETH_GSTRING_LEN,
  1200. mv643xx_gstrings_stats[i].stat_string,
  1201. ETH_GSTRING_LEN);
  1202. }
  1203. break;
  1204. }
  1205. }
  1206. static void mv643xx_get_ethtool_stats(struct net_device *netdev,
  1207. struct ethtool_stats *stats, uint64_t *data)
  1208. {
  1209. struct mv643xx_private *mp = netdev->priv;
  1210. int i;
  1211. eth_update_mib_counters(mp);
  1212. for (i = 0; i < MV643XX_STATS_LEN; i++) {
  1213. char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
  1214. data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
  1215. sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
  1216. }
  1217. }
  1218. static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
  1219. {
  1220. switch (sset) {
  1221. case ETH_SS_STATS:
  1222. return MV643XX_STATS_LEN;
  1223. default:
  1224. return -EOPNOTSUPP;
  1225. }
  1226. }
  1227. static const struct ethtool_ops mv643xx_ethtool_ops = {
  1228. .get_settings = mv643xx_get_settings,
  1229. .set_settings = mv643xx_set_settings,
  1230. .get_drvinfo = mv643xx_get_drvinfo,
  1231. .get_link = mv643xx_eth_get_link,
  1232. .set_sg = ethtool_op_set_sg,
  1233. .get_sset_count = mv643xx_get_sset_count,
  1234. .get_ethtool_stats = mv643xx_get_ethtool_stats,
  1235. .get_strings = mv643xx_get_strings,
  1236. .nway_reset = mv643xx_eth_nway_restart,
  1237. };
  1238. /* address handling *********************************************************/
  1239. /*
  1240. * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
  1241. */
  1242. static void eth_port_uc_addr_get(struct mv643xx_private *mp,
  1243. unsigned char *p_addr)
  1244. {
  1245. unsigned int port_num = mp->port_num;
  1246. unsigned int mac_h;
  1247. unsigned int mac_l;
  1248. mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
  1249. mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
  1250. p_addr[0] = (mac_h >> 24) & 0xff;
  1251. p_addr[1] = (mac_h >> 16) & 0xff;
  1252. p_addr[2] = (mac_h >> 8) & 0xff;
  1253. p_addr[3] = mac_h & 0xff;
  1254. p_addr[4] = (mac_l >> 8) & 0xff;
  1255. p_addr[5] = mac_l & 0xff;
  1256. }
  1257. /*
  1258. * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
  1259. *
  1260. * DESCRIPTION:
  1261. * Go through all the DA filter tables (Unicast, Special Multicast &
  1262. * Other Multicast) and set each entry to 0.
  1263. *
  1264. * INPUT:
  1265. * struct mv643xx_private *mp Ethernet Port.
  1266. *
  1267. * OUTPUT:
  1268. * Multicast and Unicast packets are rejected.
  1269. *
  1270. * RETURN:
  1271. * None.
  1272. */
  1273. static void eth_port_init_mac_tables(struct mv643xx_private *mp)
  1274. {
  1275. unsigned int port_num = mp->port_num;
  1276. int table_index;
  1277. /* Clear DA filter unicast table (Ex_dFUT) */
  1278. for (table_index = 0; table_index <= 0xC; table_index += 4)
  1279. wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
  1280. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  1281. /* Clear DA filter special multicast table (Ex_dFSMT) */
  1282. wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
  1283. /* Clear DA filter other multicast table (Ex_dFOMT) */
  1284. wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
  1285. }
  1286. }
  1287. /*
  1288. * The entries in each table are indexed by a hash of a packet's MAC
  1289. * address. One bit in each entry determines whether the packet is
  1290. * accepted. There are 4 entries (each 8 bits wide) in each register
  1291. * of the table. The bits in each entry are defined as follows:
  1292. * 0 Accept=1, Drop=0
  1293. * 3-1 Queue (ETH_Q0=0)
  1294. * 7-4 Reserved = 0;
  1295. */
  1296. static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
  1297. int table, unsigned char entry)
  1298. {
  1299. unsigned int table_reg;
  1300. unsigned int tbl_offset;
  1301. unsigned int reg_offset;
  1302. tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
  1303. reg_offset = entry % 4; /* Entry offset within the register */
  1304. /* Set "accepts frame bit" at specified table entry */
  1305. table_reg = rdl(mp, table + tbl_offset);
  1306. table_reg |= 0x01 << (8 * reg_offset);
  1307. wrl(mp, table + tbl_offset, table_reg);
  1308. }
  1309. /*
  1310. * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
  1311. */
  1312. static void eth_port_uc_addr_set(struct mv643xx_private *mp,
  1313. unsigned char *p_addr)
  1314. {
  1315. unsigned int port_num = mp->port_num;
  1316. unsigned int mac_h;
  1317. unsigned int mac_l;
  1318. int table;
  1319. mac_l = (p_addr[4] << 8) | (p_addr[5]);
  1320. mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
  1321. (p_addr[3] << 0);
  1322. wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
  1323. wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
  1324. /* Accept frames with this address */
  1325. table = UNICAST_TABLE(port_num);
  1326. eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
  1327. }
  1328. /*
  1329. * mv643xx_eth_update_mac_address
  1330. *
  1331. * Update the MAC address of the port in the address table
  1332. *
  1333. * Input : pointer to ethernet interface network device structure
  1334. * Output : N/A
  1335. */
  1336. static void mv643xx_eth_update_mac_address(struct net_device *dev)
  1337. {
  1338. struct mv643xx_private *mp = netdev_priv(dev);
  1339. eth_port_init_mac_tables(mp);
  1340. eth_port_uc_addr_set(mp, dev->dev_addr);
  1341. }
  1342. /*
  1343. * mv643xx_eth_set_mac_address
  1344. *
  1345. * Change the interface's mac address.
  1346. * No special hardware thing should be done because interface is always
  1347. * put in promiscuous mode.
  1348. *
  1349. * Input : pointer to ethernet interface network device structure and
  1350. * a pointer to the designated entry to be added to the cache.
  1351. * Output : zero upon success, negative upon failure
  1352. */
  1353. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  1354. {
  1355. int i;
  1356. for (i = 0; i < 6; i++)
  1357. /* +2 is for the offset of the HW addr type */
  1358. dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
  1359. mv643xx_eth_update_mac_address(dev);
  1360. return 0;
  1361. }
  1362. /*
  1363. * eth_port_mc_addr - Multicast address settings.
  1364. *
  1365. * The MV device supports multicast using two tables:
  1366. * 1) Special Multicast Table for MAC addresses of the form
  1367. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
  1368. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  1369. * Table entries in the DA-Filter table.
  1370. * 2) Other Multicast Table for multicast of another type. A CRC-8bit
  1371. * is used as an index to the Other Multicast Table entries in the
  1372. * DA-Filter table. This function calculates the CRC-8bit value.
  1373. * In either case, eth_port_set_filter_table_entry() is then called
  1374. * to set to set the actual table entry.
  1375. */
  1376. static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
  1377. {
  1378. unsigned int port_num = mp->port_num;
  1379. unsigned int mac_h;
  1380. unsigned int mac_l;
  1381. unsigned char crc_result = 0;
  1382. int table;
  1383. int mac_array[48];
  1384. int crc[8];
  1385. int i;
  1386. if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
  1387. (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
  1388. table = SPECIAL_MCAST_TABLE(port_num);
  1389. eth_port_set_filter_table_entry(mp, table, p_addr[5]);
  1390. return;
  1391. }
  1392. /* Calculate CRC-8 out of the given address */
  1393. mac_h = (p_addr[0] << 8) | (p_addr[1]);
  1394. mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
  1395. (p_addr[4] << 8) | (p_addr[5] << 0);
  1396. for (i = 0; i < 32; i++)
  1397. mac_array[i] = (mac_l >> i) & 0x1;
  1398. for (i = 32; i < 48; i++)
  1399. mac_array[i] = (mac_h >> (i - 32)) & 0x1;
  1400. crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
  1401. mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
  1402. mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
  1403. mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
  1404. mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
  1405. crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
  1406. mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
  1407. mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
  1408. mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
  1409. mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
  1410. mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
  1411. mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
  1412. crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
  1413. mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
  1414. mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
  1415. mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
  1416. mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
  1417. mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
  1418. crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
  1419. mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
  1420. mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
  1421. mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
  1422. mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
  1423. mac_array[3] ^ mac_array[2] ^ mac_array[1];
  1424. crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
  1425. mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
  1426. mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
  1427. mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
  1428. mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
  1429. mac_array[3] ^ mac_array[2];
  1430. crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
  1431. mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
  1432. mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
  1433. mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
  1434. mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
  1435. mac_array[4] ^ mac_array[3];
  1436. crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
  1437. mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
  1438. mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
  1439. mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
  1440. mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
  1441. mac_array[4];
  1442. crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
  1443. mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
  1444. mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
  1445. mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
  1446. mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
  1447. for (i = 0; i < 8; i++)
  1448. crc_result = crc_result | (crc[i] << i);
  1449. table = OTHER_MCAST_TABLE(port_num);
  1450. eth_port_set_filter_table_entry(mp, table, crc_result);
  1451. }
  1452. /*
  1453. * Set the entire multicast list based on dev->mc_list.
  1454. */
  1455. static void eth_port_set_multicast_list(struct net_device *dev)
  1456. {
  1457. struct dev_mc_list *mc_list;
  1458. int i;
  1459. int table_index;
  1460. struct mv643xx_private *mp = netdev_priv(dev);
  1461. unsigned int eth_port_num = mp->port_num;
  1462. /* If the device is in promiscuous mode or in all multicast mode,
  1463. * we will fully populate both multicast tables with accept.
  1464. * This is guaranteed to yield a match on all multicast addresses...
  1465. */
  1466. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
  1467. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  1468. /* Set all entries in DA filter special multicast
  1469. * table (Ex_dFSMT)
  1470. * Set for ETH_Q0 for now
  1471. * Bits
  1472. * 0 Accept=1, Drop=0
  1473. * 3-1 Queue ETH_Q0=0
  1474. * 7-4 Reserved = 0;
  1475. */
  1476. wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
  1477. /* Set all entries in DA filter other multicast
  1478. * table (Ex_dFOMT)
  1479. * Set for ETH_Q0 for now
  1480. * Bits
  1481. * 0 Accept=1, Drop=0
  1482. * 3-1 Queue ETH_Q0=0
  1483. * 7-4 Reserved = 0;
  1484. */
  1485. wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
  1486. }
  1487. return;
  1488. }
  1489. /* We will clear out multicast tables every time we get the list.
  1490. * Then add the entire new list...
  1491. */
  1492. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  1493. /* Clear DA filter special multicast table (Ex_dFSMT) */
  1494. wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0);
  1495. /* Clear DA filter other multicast table (Ex_dFOMT) */
  1496. wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0);
  1497. }
  1498. /* Get pointer to net_device multicast list and add each one... */
  1499. for (i = 0, mc_list = dev->mc_list;
  1500. (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
  1501. i++, mc_list = mc_list->next)
  1502. if (mc_list->dmi_addrlen == 6)
  1503. eth_port_mc_addr(mp, mc_list->dmi_addr);
  1504. }
  1505. /*
  1506. * mv643xx_eth_set_rx_mode
  1507. *
  1508. * Change from promiscuos to regular rx mode
  1509. *
  1510. * Input : pointer to ethernet interface network device structure
  1511. * Output : N/A
  1512. */
  1513. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  1514. {
  1515. struct mv643xx_private *mp = netdev_priv(dev);
  1516. u32 config_reg;
  1517. config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
  1518. if (dev->flags & IFF_PROMISC)
  1519. config_reg |= UNICAST_PROMISCUOUS_MODE;
  1520. else
  1521. config_reg &= ~UNICAST_PROMISCUOUS_MODE;
  1522. wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
  1523. eth_port_set_multicast_list(dev);
  1524. }
  1525. /* rx/tx queue initialisation ***********************************************/
  1526. /*
  1527. * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
  1528. *
  1529. * DESCRIPTION:
  1530. * This function prepares a Rx chained list of descriptors and packet
  1531. * buffers in a form of a ring. The routine must be called after port
  1532. * initialization routine and before port start routine.
  1533. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  1534. * devices in the system (i.e. DRAM). This function uses the ethernet
  1535. * struct 'virtual to physical' routine (set by the user) to set the ring
  1536. * with physical addresses.
  1537. *
  1538. * INPUT:
  1539. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  1540. *
  1541. * OUTPUT:
  1542. * The routine updates the Ethernet port control struct with information
  1543. * regarding the Rx descriptors and buffers.
  1544. *
  1545. * RETURN:
  1546. * None.
  1547. */
  1548. static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
  1549. {
  1550. volatile struct eth_rx_desc *p_rx_desc;
  1551. int rx_desc_num = mp->rx_ring_size;
  1552. int i;
  1553. /* initialize the next_desc_ptr links in the Rx descriptors ring */
  1554. p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
  1555. for (i = 0; i < rx_desc_num; i++) {
  1556. p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
  1557. ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
  1558. }
  1559. /* Save Rx desc pointer to driver struct. */
  1560. mp->rx_curr_desc_q = 0;
  1561. mp->rx_used_desc_q = 0;
  1562. mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
  1563. }
  1564. static void mv643xx_eth_free_rx_rings(struct net_device *dev)
  1565. {
  1566. struct mv643xx_private *mp = netdev_priv(dev);
  1567. int curr;
  1568. /* Stop RX Queues */
  1569. mv643xx_eth_port_disable_rx(mp);
  1570. /* Free preallocated skb's on RX rings */
  1571. for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
  1572. if (mp->rx_skb[curr]) {
  1573. dev_kfree_skb(mp->rx_skb[curr]);
  1574. mp->rx_desc_count--;
  1575. }
  1576. }
  1577. if (mp->rx_desc_count)
  1578. printk(KERN_ERR
  1579. "%s: Error in freeing Rx Ring. %d skb's still"
  1580. " stuck in RX Ring - ignoring them\n", dev->name,
  1581. mp->rx_desc_count);
  1582. /* Free RX ring */
  1583. if (mp->rx_sram_size)
  1584. iounmap(mp->p_rx_desc_area);
  1585. else
  1586. dma_free_coherent(NULL, mp->rx_desc_area_size,
  1587. mp->p_rx_desc_area, mp->rx_desc_dma);
  1588. }
  1589. /*
  1590. * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
  1591. *
  1592. * DESCRIPTION:
  1593. * This function prepares a Tx chained list of descriptors and packet
  1594. * buffers in a form of a ring. The routine must be called after port
  1595. * initialization routine and before port start routine.
  1596. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  1597. * devices in the system (i.e. DRAM). This function uses the ethernet
  1598. * struct 'virtual to physical' routine (set by the user) to set the ring
  1599. * with physical addresses.
  1600. *
  1601. * INPUT:
  1602. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  1603. *
  1604. * OUTPUT:
  1605. * The routine updates the Ethernet port control struct with information
  1606. * regarding the Tx descriptors and buffers.
  1607. *
  1608. * RETURN:
  1609. * None.
  1610. */
  1611. static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
  1612. {
  1613. int tx_desc_num = mp->tx_ring_size;
  1614. struct eth_tx_desc *p_tx_desc;
  1615. int i;
  1616. /* Initialize the next_desc_ptr links in the Tx descriptors ring */
  1617. p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
  1618. for (i = 0; i < tx_desc_num; i++) {
  1619. p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
  1620. ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
  1621. }
  1622. mp->tx_curr_desc_q = 0;
  1623. mp->tx_used_desc_q = 0;
  1624. mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
  1625. }
  1626. /**
  1627. * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
  1628. *
  1629. * If force is non-zero, frees uncompleted descriptors as well
  1630. */
  1631. static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
  1632. {
  1633. struct mv643xx_private *mp = netdev_priv(dev);
  1634. struct eth_tx_desc *desc;
  1635. u32 cmd_sts;
  1636. struct sk_buff *skb;
  1637. unsigned long flags;
  1638. int tx_index;
  1639. dma_addr_t addr;
  1640. int count;
  1641. int released = 0;
  1642. while (mp->tx_desc_count > 0) {
  1643. spin_lock_irqsave(&mp->lock, flags);
  1644. /* tx_desc_count might have changed before acquiring the lock */
  1645. if (mp->tx_desc_count <= 0) {
  1646. spin_unlock_irqrestore(&mp->lock, flags);
  1647. return released;
  1648. }
  1649. tx_index = mp->tx_used_desc_q;
  1650. desc = &mp->p_tx_desc_area[tx_index];
  1651. cmd_sts = desc->cmd_sts;
  1652. if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
  1653. spin_unlock_irqrestore(&mp->lock, flags);
  1654. return released;
  1655. }
  1656. mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
  1657. mp->tx_desc_count--;
  1658. addr = desc->buf_ptr;
  1659. count = desc->byte_cnt;
  1660. skb = mp->tx_skb[tx_index];
  1661. if (skb)
  1662. mp->tx_skb[tx_index] = NULL;
  1663. if (cmd_sts & ETH_ERROR_SUMMARY) {
  1664. printk("%s: Error in TX\n", dev->name);
  1665. dev->stats.tx_errors++;
  1666. }
  1667. spin_unlock_irqrestore(&mp->lock, flags);
  1668. if (cmd_sts & ETH_TX_FIRST_DESC)
  1669. dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
  1670. else
  1671. dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
  1672. if (skb)
  1673. dev_kfree_skb_irq(skb);
  1674. released = 1;
  1675. }
  1676. return released;
  1677. }
  1678. static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
  1679. {
  1680. struct mv643xx_private *mp = netdev_priv(dev);
  1681. if (mv643xx_eth_free_tx_descs(dev, 0) &&
  1682. mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
  1683. netif_wake_queue(dev);
  1684. }
  1685. static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
  1686. {
  1687. mv643xx_eth_free_tx_descs(dev, 1);
  1688. }
  1689. static void mv643xx_eth_free_tx_rings(struct net_device *dev)
  1690. {
  1691. struct mv643xx_private *mp = netdev_priv(dev);
  1692. /* Stop Tx Queues */
  1693. mv643xx_eth_port_disable_tx(mp);
  1694. /* Free outstanding skb's on TX ring */
  1695. mv643xx_eth_free_all_tx_descs(dev);
  1696. BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
  1697. /* Free TX ring */
  1698. if (mp->tx_sram_size)
  1699. iounmap(mp->p_tx_desc_area);
  1700. else
  1701. dma_free_coherent(NULL, mp->tx_desc_area_size,
  1702. mp->p_tx_desc_area, mp->tx_desc_dma);
  1703. }
  1704. /* netdev ops and related ***************************************************/
  1705. static void eth_port_reset(struct mv643xx_private *mp);
  1706. /* Set the mv643xx port configuration register for the speed/duplex mode. */
  1707. static void mv643xx_eth_update_pscr(struct net_device *dev,
  1708. struct ethtool_cmd *ecmd)
  1709. {
  1710. struct mv643xx_private *mp = netdev_priv(dev);
  1711. int port_num = mp->port_num;
  1712. u32 o_pscr, n_pscr;
  1713. unsigned int queues;
  1714. o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
  1715. n_pscr = o_pscr;
  1716. /* clear speed, duplex and rx buffer size fields */
  1717. n_pscr &= ~(SET_MII_SPEED_TO_100 |
  1718. SET_GMII_SPEED_TO_1000 |
  1719. SET_FULL_DUPLEX_MODE |
  1720. MAX_RX_PACKET_MASK);
  1721. if (ecmd->duplex == DUPLEX_FULL)
  1722. n_pscr |= SET_FULL_DUPLEX_MODE;
  1723. if (ecmd->speed == SPEED_1000)
  1724. n_pscr |= SET_GMII_SPEED_TO_1000 |
  1725. MAX_RX_PACKET_9700BYTE;
  1726. else {
  1727. if (ecmd->speed == SPEED_100)
  1728. n_pscr |= SET_MII_SPEED_TO_100;
  1729. n_pscr |= MAX_RX_PACKET_1522BYTE;
  1730. }
  1731. if (n_pscr != o_pscr) {
  1732. if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
  1733. wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
  1734. else {
  1735. queues = mv643xx_eth_port_disable_tx(mp);
  1736. o_pscr &= ~SERIAL_PORT_ENABLE;
  1737. wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
  1738. wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
  1739. wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
  1740. if (queues)
  1741. mv643xx_eth_port_enable_tx(mp, queues);
  1742. }
  1743. }
  1744. }
  1745. /*
  1746. * mv643xx_eth_int_handler
  1747. *
  1748. * Main interrupt handler for the gigbit ethernet ports
  1749. *
  1750. * Input : irq - irq number (not used)
  1751. * dev_id - a pointer to the required interface's data structure
  1752. * regs - not used
  1753. * Output : N/A
  1754. */
  1755. static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
  1756. {
  1757. struct net_device *dev = (struct net_device *)dev_id;
  1758. struct mv643xx_private *mp = netdev_priv(dev);
  1759. u32 eth_int_cause, eth_int_cause_ext = 0;
  1760. unsigned int port_num = mp->port_num;
  1761. /* Read interrupt cause registers */
  1762. eth_int_cause = rdl(mp, INT_CAUSE(port_num)) & ETH_INT_UNMASK_ALL;
  1763. if (eth_int_cause & ETH_INT_CAUSE_EXT) {
  1764. eth_int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
  1765. & ETH_INT_UNMASK_ALL_EXT;
  1766. wrl(mp, INT_CAUSE_EXT(port_num), ~eth_int_cause_ext);
  1767. }
  1768. /* PHY status changed */
  1769. if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
  1770. struct ethtool_cmd cmd;
  1771. if (mii_link_ok(&mp->mii)) {
  1772. mii_ethtool_gset(&mp->mii, &cmd);
  1773. mv643xx_eth_update_pscr(dev, &cmd);
  1774. mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
  1775. if (!netif_carrier_ok(dev)) {
  1776. netif_carrier_on(dev);
  1777. if (mp->tx_ring_size - mp->tx_desc_count >=
  1778. MAX_DESCS_PER_SKB)
  1779. netif_wake_queue(dev);
  1780. }
  1781. } else if (netif_carrier_ok(dev)) {
  1782. netif_stop_queue(dev);
  1783. netif_carrier_off(dev);
  1784. }
  1785. }
  1786. #ifdef MV643XX_NAPI
  1787. if (eth_int_cause & ETH_INT_CAUSE_RX) {
  1788. /* schedule the NAPI poll routine to maintain port */
  1789. wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
  1790. /* wait for previous write to complete */
  1791. rdl(mp, INT_MASK(port_num));
  1792. netif_rx_schedule(dev, &mp->napi);
  1793. }
  1794. #else
  1795. if (eth_int_cause & ETH_INT_CAUSE_RX)
  1796. mv643xx_eth_receive_queue(dev, INT_MAX);
  1797. #endif
  1798. if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
  1799. mv643xx_eth_free_completed_tx_descs(dev);
  1800. /*
  1801. * If no real interrupt occured, exit.
  1802. * This can happen when using gigE interrupt coalescing mechanism.
  1803. */
  1804. if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
  1805. return IRQ_NONE;
  1806. return IRQ_HANDLED;
  1807. }
  1808. /*
  1809. * ethernet_phy_reset - Reset Ethernet port PHY.
  1810. *
  1811. * DESCRIPTION:
  1812. * This routine utilizes the SMI interface to reset the ethernet port PHY.
  1813. *
  1814. * INPUT:
  1815. * struct mv643xx_private *mp Ethernet Port.
  1816. *
  1817. * OUTPUT:
  1818. * The PHY is reset.
  1819. *
  1820. * RETURN:
  1821. * None.
  1822. *
  1823. */
  1824. static void ethernet_phy_reset(struct mv643xx_private *mp)
  1825. {
  1826. unsigned int phy_reg_data;
  1827. /* Reset the PHY */
  1828. eth_port_read_smi_reg(mp, 0, &phy_reg_data);
  1829. phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
  1830. eth_port_write_smi_reg(mp, 0, phy_reg_data);
  1831. /* wait for PHY to come out of reset */
  1832. do {
  1833. udelay(1);
  1834. eth_port_read_smi_reg(mp, 0, &phy_reg_data);
  1835. } while (phy_reg_data & 0x8000);
  1836. }
  1837. /*
  1838. * eth_port_start - Start the Ethernet port activity.
  1839. *
  1840. * DESCRIPTION:
  1841. * This routine prepares the Ethernet port for Rx and Tx activity:
  1842. * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
  1843. * has been initialized a descriptor's ring (using
  1844. * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
  1845. * 2. Initialize and enable the Ethernet configuration port by writing to
  1846. * the port's configuration and command registers.
  1847. * 3. Initialize and enable the SDMA by writing to the SDMA's
  1848. * configuration and command registers. After completing these steps,
  1849. * the ethernet port SDMA can starts to perform Rx and Tx activities.
  1850. *
  1851. * Note: Each Rx and Tx queue descriptor's list must be initialized prior
  1852. * to calling this function (use ether_init_tx_desc_ring for Tx queues
  1853. * and ether_init_rx_desc_ring for Rx queues).
  1854. *
  1855. * INPUT:
  1856. * dev - a pointer to the required interface
  1857. *
  1858. * OUTPUT:
  1859. * Ethernet port is ready to receive and transmit.
  1860. *
  1861. * RETURN:
  1862. * None.
  1863. */
  1864. static void eth_port_start(struct net_device *dev)
  1865. {
  1866. struct mv643xx_private *mp = netdev_priv(dev);
  1867. unsigned int port_num = mp->port_num;
  1868. int tx_curr_desc, rx_curr_desc;
  1869. u32 pscr;
  1870. struct ethtool_cmd ethtool_cmd;
  1871. /* Assignment of Tx CTRP of given queue */
  1872. tx_curr_desc = mp->tx_curr_desc_q;
  1873. wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
  1874. (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
  1875. /* Assignment of Rx CRDP of given queue */
  1876. rx_curr_desc = mp->rx_curr_desc_q;
  1877. wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
  1878. (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
  1879. /* Add the assigned Ethernet address to the port's address table */
  1880. eth_port_uc_addr_set(mp, dev->dev_addr);
  1881. /*
  1882. * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
  1883. * frames to RX queue #0.
  1884. */
  1885. wrl(mp, PORT_CONFIG(port_num), 0x00000000);
  1886. /*
  1887. * Treat BPDUs as normal multicasts, and disable partition mode.
  1888. */
  1889. wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
  1890. pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
  1891. pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
  1892. wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
  1893. pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
  1894. DISABLE_AUTO_NEG_SPEED_GMII |
  1895. DISABLE_AUTO_NEG_FOR_DUPLX |
  1896. DO_NOT_FORCE_LINK_FAIL |
  1897. SERIAL_PORT_CONTROL_RESERVED;
  1898. wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
  1899. pscr |= SERIAL_PORT_ENABLE;
  1900. wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
  1901. /* Assign port SDMA configuration */
  1902. wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
  1903. /* Enable port Rx. */
  1904. mv643xx_eth_port_enable_rx(mp, ETH_RX_QUEUES_ENABLED);
  1905. /* Disable port bandwidth limits by clearing MTU register */
  1906. wrl(mp, TX_BW_MTU(port_num), 0);
  1907. /* save phy settings across reset */
  1908. mv643xx_get_settings(dev, &ethtool_cmd);
  1909. ethernet_phy_reset(mp);
  1910. mv643xx_set_settings(dev, &ethtool_cmd);
  1911. }
  1912. #ifdef MV643XX_COAL
  1913. /*
  1914. * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
  1915. *
  1916. * DESCRIPTION:
  1917. * This routine sets the RX coalescing interrupt mechanism parameter.
  1918. * This parameter is a timeout counter, that counts in 64 t_clk
  1919. * chunks ; that when timeout event occurs a maskable interrupt
  1920. * occurs.
  1921. * The parameter is calculated using the tClk of the MV-643xx chip
  1922. * , and the required delay of the interrupt in usec.
  1923. *
  1924. * INPUT:
  1925. * struct mv643xx_private *mp Ethernet port
  1926. * unsigned int delay Delay in usec
  1927. *
  1928. * OUTPUT:
  1929. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  1930. *
  1931. * RETURN:
  1932. * The interrupt coalescing value set in the gigE port.
  1933. *
  1934. */
  1935. static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
  1936. unsigned int delay)
  1937. {
  1938. unsigned int port_num = mp->port_num;
  1939. unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
  1940. /* Set RX Coalescing mechanism */
  1941. wrl(mp, SDMA_CONFIG(port_num),
  1942. ((coal & 0x3fff) << 8) |
  1943. (rdl(mp, SDMA_CONFIG(port_num))
  1944. & 0xffc000ff));
  1945. return coal;
  1946. }
  1947. #endif
  1948. /*
  1949. * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
  1950. *
  1951. * DESCRIPTION:
  1952. * This routine sets the TX coalescing interrupt mechanism parameter.
  1953. * This parameter is a timeout counter, that counts in 64 t_clk
  1954. * chunks ; that when timeout event occurs a maskable interrupt
  1955. * occurs.
  1956. * The parameter is calculated using the t_cLK frequency of the
  1957. * MV-643xx chip and the required delay in the interrupt in uSec
  1958. *
  1959. * INPUT:
  1960. * struct mv643xx_private *mp Ethernet port
  1961. * unsigned int delay Delay in uSeconds
  1962. *
  1963. * OUTPUT:
  1964. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  1965. *
  1966. * RETURN:
  1967. * The interrupt coalescing value set in the gigE port.
  1968. *
  1969. */
  1970. static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
  1971. unsigned int delay)
  1972. {
  1973. unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
  1974. /* Set TX Coalescing mechanism */
  1975. wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
  1976. return coal;
  1977. }
  1978. /*
  1979. * eth_port_init - Initialize the Ethernet port driver
  1980. *
  1981. * DESCRIPTION:
  1982. * This function prepares the ethernet port to start its activity:
  1983. * 1) Completes the ethernet port driver struct initialization toward port
  1984. * start routine.
  1985. * 2) Resets the device to a quiescent state in case of warm reboot.
  1986. * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
  1987. * 4) Clean MAC tables. The reset status of those tables is unknown.
  1988. * 5) Set PHY address.
  1989. * Note: Call this routine prior to eth_port_start routine and after
  1990. * setting user values in the user fields of Ethernet port control
  1991. * struct.
  1992. *
  1993. * INPUT:
  1994. * struct mv643xx_private *mp Ethernet port control struct
  1995. *
  1996. * OUTPUT:
  1997. * See description.
  1998. *
  1999. * RETURN:
  2000. * None.
  2001. */
  2002. static void eth_port_init(struct mv643xx_private *mp)
  2003. {
  2004. mp->rx_resource_err = 0;
  2005. eth_port_reset(mp);
  2006. eth_port_init_mac_tables(mp);
  2007. }
  2008. /*
  2009. * mv643xx_eth_open
  2010. *
  2011. * This function is called when openning the network device. The function
  2012. * should initialize all the hardware, initialize cyclic Rx/Tx
  2013. * descriptors chain and buffers and allocate an IRQ to the network
  2014. * device.
  2015. *
  2016. * Input : a pointer to the network device structure
  2017. *
  2018. * Output : zero of success , nonzero if fails.
  2019. */
  2020. static int mv643xx_eth_open(struct net_device *dev)
  2021. {
  2022. struct mv643xx_private *mp = netdev_priv(dev);
  2023. unsigned int port_num = mp->port_num;
  2024. unsigned int size;
  2025. int err;
  2026. /* Clear any pending ethernet port interrupts */
  2027. wrl(mp, INT_CAUSE(port_num), 0);
  2028. wrl(mp, INT_CAUSE_EXT(port_num), 0);
  2029. /* wait for previous write to complete */
  2030. rdl(mp, INT_CAUSE_EXT(port_num));
  2031. err = request_irq(dev->irq, mv643xx_eth_int_handler,
  2032. IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
  2033. if (err) {
  2034. printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
  2035. return -EAGAIN;
  2036. }
  2037. eth_port_init(mp);
  2038. memset(&mp->timeout, 0, sizeof(struct timer_list));
  2039. mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
  2040. mp->timeout.data = (unsigned long)dev;
  2041. /* Allocate RX and TX skb rings */
  2042. mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
  2043. GFP_KERNEL);
  2044. if (!mp->rx_skb) {
  2045. printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
  2046. err = -ENOMEM;
  2047. goto out_free_irq;
  2048. }
  2049. mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
  2050. GFP_KERNEL);
  2051. if (!mp->tx_skb) {
  2052. printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
  2053. err = -ENOMEM;
  2054. goto out_free_rx_skb;
  2055. }
  2056. /* Allocate TX ring */
  2057. mp->tx_desc_count = 0;
  2058. size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
  2059. mp->tx_desc_area_size = size;
  2060. if (mp->tx_sram_size) {
  2061. mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
  2062. mp->tx_sram_size);
  2063. mp->tx_desc_dma = mp->tx_sram_addr;
  2064. } else
  2065. mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
  2066. &mp->tx_desc_dma,
  2067. GFP_KERNEL);
  2068. if (!mp->p_tx_desc_area) {
  2069. printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
  2070. dev->name, size);
  2071. err = -ENOMEM;
  2072. goto out_free_tx_skb;
  2073. }
  2074. BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
  2075. memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
  2076. ether_init_tx_desc_ring(mp);
  2077. /* Allocate RX ring */
  2078. mp->rx_desc_count = 0;
  2079. size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
  2080. mp->rx_desc_area_size = size;
  2081. if (mp->rx_sram_size) {
  2082. mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
  2083. mp->rx_sram_size);
  2084. mp->rx_desc_dma = mp->rx_sram_addr;
  2085. } else
  2086. mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
  2087. &mp->rx_desc_dma,
  2088. GFP_KERNEL);
  2089. if (!mp->p_rx_desc_area) {
  2090. printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
  2091. dev->name, size);
  2092. printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
  2093. dev->name);
  2094. if (mp->rx_sram_size)
  2095. iounmap(mp->p_tx_desc_area);
  2096. else
  2097. dma_free_coherent(NULL, mp->tx_desc_area_size,
  2098. mp->p_tx_desc_area, mp->tx_desc_dma);
  2099. err = -ENOMEM;
  2100. goto out_free_tx_skb;
  2101. }
  2102. memset((void *)mp->p_rx_desc_area, 0, size);
  2103. ether_init_rx_desc_ring(mp);
  2104. mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
  2105. #ifdef MV643XX_NAPI
  2106. napi_enable(&mp->napi);
  2107. #endif
  2108. eth_port_start(dev);
  2109. /* Interrupt Coalescing */
  2110. #ifdef MV643XX_COAL
  2111. mp->rx_int_coal =
  2112. eth_port_set_rx_coal(mp, MV643XX_RX_COAL);
  2113. #endif
  2114. mp->tx_int_coal =
  2115. eth_port_set_tx_coal(mp, MV643XX_TX_COAL);
  2116. /* Unmask phy and link status changes interrupts */
  2117. wrl(mp, INT_MASK_EXT(port_num), ETH_INT_UNMASK_ALL_EXT);
  2118. /* Unmask RX buffer and TX end interrupt */
  2119. wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
  2120. return 0;
  2121. out_free_tx_skb:
  2122. kfree(mp->tx_skb);
  2123. out_free_rx_skb:
  2124. kfree(mp->rx_skb);
  2125. out_free_irq:
  2126. free_irq(dev->irq, dev);
  2127. return err;
  2128. }
  2129. /*
  2130. * eth_port_reset - Reset Ethernet port
  2131. *
  2132. * DESCRIPTION:
  2133. * This routine resets the chip by aborting any SDMA engine activity and
  2134. * clearing the MIB counters. The Receiver and the Transmit unit are in
  2135. * idle state after this command is performed and the port is disabled.
  2136. *
  2137. * INPUT:
  2138. * struct mv643xx_private *mp Ethernet Port.
  2139. *
  2140. * OUTPUT:
  2141. * Channel activity is halted.
  2142. *
  2143. * RETURN:
  2144. * None.
  2145. *
  2146. */
  2147. static void eth_port_reset(struct mv643xx_private *mp)
  2148. {
  2149. unsigned int port_num = mp->port_num;
  2150. unsigned int reg_data;
  2151. mv643xx_eth_port_disable_tx(mp);
  2152. mv643xx_eth_port_disable_rx(mp);
  2153. /* Clear all MIB counters */
  2154. eth_clear_mib_counters(mp);
  2155. /* Reset the Enable bit in the Configuration Register */
  2156. reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
  2157. reg_data &= ~(SERIAL_PORT_ENABLE |
  2158. DO_NOT_FORCE_LINK_FAIL |
  2159. FORCE_LINK_PASS);
  2160. wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
  2161. }
  2162. /*
  2163. * mv643xx_eth_stop
  2164. *
  2165. * This function is used when closing the network device.
  2166. * It updates the hardware,
  2167. * release all memory that holds buffers and descriptors and release the IRQ.
  2168. * Input : a pointer to the device structure
  2169. * Output : zero if success , nonzero if fails
  2170. */
  2171. static int mv643xx_eth_stop(struct net_device *dev)
  2172. {
  2173. struct mv643xx_private *mp = netdev_priv(dev);
  2174. unsigned int port_num = mp->port_num;
  2175. /* Mask all interrupts on ethernet port */
  2176. wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
  2177. /* wait for previous write to complete */
  2178. rdl(mp, INT_MASK(port_num));
  2179. #ifdef MV643XX_NAPI
  2180. napi_disable(&mp->napi);
  2181. #endif
  2182. netif_carrier_off(dev);
  2183. netif_stop_queue(dev);
  2184. eth_port_reset(mp);
  2185. mv643xx_eth_free_tx_rings(dev);
  2186. mv643xx_eth_free_rx_rings(dev);
  2187. free_irq(dev->irq, dev);
  2188. return 0;
  2189. }
  2190. static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2191. {
  2192. struct mv643xx_private *mp = netdev_priv(dev);
  2193. return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
  2194. }
  2195. /*
  2196. * Changes MTU (maximum transfer unit) of the gigabit ethenret port
  2197. *
  2198. * Input : pointer to ethernet interface network device structure
  2199. * new mtu size
  2200. * Output : 0 upon success, -EINVAL upon failure
  2201. */
  2202. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  2203. {
  2204. if ((new_mtu > 9500) || (new_mtu < 64))
  2205. return -EINVAL;
  2206. dev->mtu = new_mtu;
  2207. if (!netif_running(dev))
  2208. return 0;
  2209. /*
  2210. * Stop and then re-open the interface. This will allocate RX
  2211. * skbs of the new MTU.
  2212. * There is a possible danger that the open will not succeed,
  2213. * due to memory being full, which might fail the open function.
  2214. */
  2215. mv643xx_eth_stop(dev);
  2216. if (mv643xx_eth_open(dev)) {
  2217. printk(KERN_ERR "%s: Fatal error on opening device\n",
  2218. dev->name);
  2219. }
  2220. return 0;
  2221. }
  2222. /*
  2223. * mv643xx_eth_tx_timeout_task
  2224. *
  2225. * Actual routine to reset the adapter when a timeout on Tx has occurred
  2226. */
  2227. static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
  2228. {
  2229. struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
  2230. tx_timeout_task);
  2231. struct net_device *dev = mp->dev;
  2232. if (!netif_running(dev))
  2233. return;
  2234. netif_stop_queue(dev);
  2235. eth_port_reset(mp);
  2236. eth_port_start(dev);
  2237. if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
  2238. netif_wake_queue(dev);
  2239. }
  2240. /*
  2241. * mv643xx_eth_tx_timeout
  2242. *
  2243. * Called upon a timeout on transmitting a packet
  2244. *
  2245. * Input : pointer to ethernet interface network device structure.
  2246. * Output : N/A
  2247. */
  2248. static void mv643xx_eth_tx_timeout(struct net_device *dev)
  2249. {
  2250. struct mv643xx_private *mp = netdev_priv(dev);
  2251. printk(KERN_INFO "%s: TX timeout ", dev->name);
  2252. /* Do the reset outside of interrupt context */
  2253. schedule_work(&mp->tx_timeout_task);
  2254. }
  2255. #ifdef CONFIG_NET_POLL_CONTROLLER
  2256. static void mv643xx_netpoll(struct net_device *netdev)
  2257. {
  2258. struct mv643xx_private *mp = netdev_priv(netdev);
  2259. int port_num = mp->port_num;
  2260. wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
  2261. /* wait for previous write to complete */
  2262. rdl(mp, INT_MASK(port_num));
  2263. mv643xx_eth_int_handler(netdev->irq, netdev);
  2264. wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
  2265. }
  2266. #endif
  2267. /*
  2268. * Wrappers for MII support library.
  2269. */
  2270. static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
  2271. {
  2272. struct mv643xx_private *mp = netdev_priv(dev);
  2273. int val;
  2274. eth_port_read_smi_reg(mp, location, &val);
  2275. return val;
  2276. }
  2277. static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
  2278. {
  2279. struct mv643xx_private *mp = netdev_priv(dev);
  2280. eth_port_write_smi_reg(mp, location, val);
  2281. }
  2282. /* platform glue ************************************************************/
  2283. static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
  2284. struct mbus_dram_target_info *dram)
  2285. {
  2286. void __iomem *base = msp->eth_base;
  2287. u32 win_enable;
  2288. u32 win_protect;
  2289. int i;
  2290. for (i = 0; i < 6; i++) {
  2291. writel(0, base + WINDOW_BASE(i));
  2292. writel(0, base + WINDOW_SIZE(i));
  2293. if (i < 4)
  2294. writel(0, base + WINDOW_REMAP_HIGH(i));
  2295. }
  2296. win_enable = 0x3f;
  2297. win_protect = 0;
  2298. for (i = 0; i < dram->num_cs; i++) {
  2299. struct mbus_dram_window *cs = dram->cs + i;
  2300. writel((cs->base & 0xffff0000) |
  2301. (cs->mbus_attr << 8) |
  2302. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  2303. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  2304. win_enable &= ~(1 << i);
  2305. win_protect |= 3 << (2 * i);
  2306. }
  2307. writel(win_enable, base + WINDOW_BAR_ENABLE);
  2308. msp->win_protect = win_protect;
  2309. }
  2310. static int mv643xx_eth_shared_probe(struct platform_device *pdev)
  2311. {
  2312. static int mv643xx_version_printed = 0;
  2313. struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
  2314. struct mv643xx_shared_private *msp;
  2315. struct resource *res;
  2316. int ret;
  2317. if (!mv643xx_version_printed++)
  2318. printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
  2319. ret = -EINVAL;
  2320. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2321. if (res == NULL)
  2322. goto out;
  2323. ret = -ENOMEM;
  2324. msp = kmalloc(sizeof(*msp), GFP_KERNEL);
  2325. if (msp == NULL)
  2326. goto out;
  2327. memset(msp, 0, sizeof(*msp));
  2328. msp->eth_base = ioremap(res->start, res->end - res->start + 1);
  2329. if (msp->eth_base == NULL)
  2330. goto out_free;
  2331. spin_lock_init(&msp->phy_lock);
  2332. msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
  2333. platform_set_drvdata(pdev, msp);
  2334. /*
  2335. * (Re-)program MBUS remapping windows if we are asked to.
  2336. */
  2337. if (pd != NULL && pd->dram != NULL)
  2338. mv643xx_eth_conf_mbus_windows(msp, pd->dram);
  2339. return 0;
  2340. out_free:
  2341. kfree(msp);
  2342. out:
  2343. return ret;
  2344. }
  2345. static int mv643xx_eth_shared_remove(struct platform_device *pdev)
  2346. {
  2347. struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
  2348. iounmap(msp->eth_base);
  2349. kfree(msp);
  2350. return 0;
  2351. }
  2352. static struct platform_driver mv643xx_eth_shared_driver = {
  2353. .probe = mv643xx_eth_shared_probe,
  2354. .remove = mv643xx_eth_shared_remove,
  2355. .driver = {
  2356. .name = MV643XX_ETH_SHARED_NAME,
  2357. .owner = THIS_MODULE,
  2358. },
  2359. };
  2360. /*
  2361. * ethernet_phy_set - Set the ethernet port PHY address.
  2362. *
  2363. * DESCRIPTION:
  2364. * This routine sets the given ethernet port PHY address.
  2365. *
  2366. * INPUT:
  2367. * struct mv643xx_private *mp Ethernet Port.
  2368. * int phy_addr PHY address.
  2369. *
  2370. * OUTPUT:
  2371. * None.
  2372. *
  2373. * RETURN:
  2374. * None.
  2375. *
  2376. */
  2377. static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
  2378. {
  2379. u32 reg_data;
  2380. int addr_shift = 5 * mp->port_num;
  2381. reg_data = rdl(mp, PHY_ADDR);
  2382. reg_data &= ~(0x1f << addr_shift);
  2383. reg_data |= (phy_addr & 0x1f) << addr_shift;
  2384. wrl(mp, PHY_ADDR, reg_data);
  2385. }
  2386. /*
  2387. * ethernet_phy_get - Get the ethernet port PHY address.
  2388. *
  2389. * DESCRIPTION:
  2390. * This routine returns the given ethernet port PHY address.
  2391. *
  2392. * INPUT:
  2393. * struct mv643xx_private *mp Ethernet Port.
  2394. *
  2395. * OUTPUT:
  2396. * None.
  2397. *
  2398. * RETURN:
  2399. * PHY address.
  2400. *
  2401. */
  2402. static int ethernet_phy_get(struct mv643xx_private *mp)
  2403. {
  2404. unsigned int reg_data;
  2405. reg_data = rdl(mp, PHY_ADDR);
  2406. return ((reg_data >> (5 * mp->port_num)) & 0x1f);
  2407. }
  2408. /*
  2409. * ethernet_phy_detect - Detect whether a phy is present
  2410. *
  2411. * DESCRIPTION:
  2412. * This function tests whether there is a PHY present on
  2413. * the specified port.
  2414. *
  2415. * INPUT:
  2416. * struct mv643xx_private *mp Ethernet Port.
  2417. *
  2418. * OUTPUT:
  2419. * None
  2420. *
  2421. * RETURN:
  2422. * 0 on success
  2423. * -ENODEV on failure
  2424. *
  2425. */
  2426. static int ethernet_phy_detect(struct mv643xx_private *mp)
  2427. {
  2428. unsigned int phy_reg_data0;
  2429. int auto_neg;
  2430. eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
  2431. auto_neg = phy_reg_data0 & 0x1000;
  2432. phy_reg_data0 ^= 0x1000; /* invert auto_neg */
  2433. eth_port_write_smi_reg(mp, 0, phy_reg_data0);
  2434. eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
  2435. if ((phy_reg_data0 & 0x1000) == auto_neg)
  2436. return -ENODEV; /* change didn't take */
  2437. phy_reg_data0 ^= 0x1000;
  2438. eth_port_write_smi_reg(mp, 0, phy_reg_data0);
  2439. return 0;
  2440. }
  2441. static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
  2442. int speed, int duplex,
  2443. struct ethtool_cmd *cmd)
  2444. {
  2445. struct mv643xx_private *mp = netdev_priv(dev);
  2446. memset(cmd, 0, sizeof(*cmd));
  2447. cmd->port = PORT_MII;
  2448. cmd->transceiver = XCVR_INTERNAL;
  2449. cmd->phy_address = phy_address;
  2450. if (speed == 0) {
  2451. cmd->autoneg = AUTONEG_ENABLE;
  2452. /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
  2453. cmd->speed = SPEED_100;
  2454. cmd->advertising = ADVERTISED_10baseT_Half |
  2455. ADVERTISED_10baseT_Full |
  2456. ADVERTISED_100baseT_Half |
  2457. ADVERTISED_100baseT_Full;
  2458. if (mp->mii.supports_gmii)
  2459. cmd->advertising |= ADVERTISED_1000baseT_Full;
  2460. } else {
  2461. cmd->autoneg = AUTONEG_DISABLE;
  2462. cmd->speed = speed;
  2463. cmd->duplex = duplex;
  2464. }
  2465. }
  2466. /*/
  2467. * mv643xx_eth_probe
  2468. *
  2469. * First function called after registering the network device.
  2470. * It's purpose is to initialize the device as an ethernet device,
  2471. * fill the ethernet device structure with pointers * to functions,
  2472. * and set the MAC address of the interface
  2473. *
  2474. * Input : struct device *
  2475. * Output : -ENOMEM if failed , 0 if success
  2476. */
  2477. static int mv643xx_eth_probe(struct platform_device *pdev)
  2478. {
  2479. struct mv643xx_eth_platform_data *pd;
  2480. int port_num;
  2481. struct mv643xx_private *mp;
  2482. struct net_device *dev;
  2483. u8 *p;
  2484. struct resource *res;
  2485. int err;
  2486. struct ethtool_cmd cmd;
  2487. int duplex = DUPLEX_HALF;
  2488. int speed = 0; /* default to auto-negotiation */
  2489. DECLARE_MAC_BUF(mac);
  2490. pd = pdev->dev.platform_data;
  2491. if (pd == NULL) {
  2492. printk(KERN_ERR "No mv643xx_eth_platform_data\n");
  2493. return -ENODEV;
  2494. }
  2495. if (pd->shared == NULL) {
  2496. printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
  2497. return -ENODEV;
  2498. }
  2499. dev = alloc_etherdev(sizeof(struct mv643xx_private));
  2500. if (!dev)
  2501. return -ENOMEM;
  2502. platform_set_drvdata(pdev, dev);
  2503. mp = netdev_priv(dev);
  2504. mp->dev = dev;
  2505. #ifdef MV643XX_NAPI
  2506. netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
  2507. #endif
  2508. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2509. BUG_ON(!res);
  2510. dev->irq = res->start;
  2511. dev->open = mv643xx_eth_open;
  2512. dev->stop = mv643xx_eth_stop;
  2513. dev->hard_start_xmit = mv643xx_eth_start_xmit;
  2514. dev->set_mac_address = mv643xx_eth_set_mac_address;
  2515. dev->set_multicast_list = mv643xx_eth_set_rx_mode;
  2516. /* No need to Tx Timeout */
  2517. dev->tx_timeout = mv643xx_eth_tx_timeout;
  2518. #ifdef CONFIG_NET_POLL_CONTROLLER
  2519. dev->poll_controller = mv643xx_netpoll;
  2520. #endif
  2521. dev->watchdog_timeo = 2 * HZ;
  2522. dev->base_addr = 0;
  2523. dev->change_mtu = mv643xx_eth_change_mtu;
  2524. dev->do_ioctl = mv643xx_eth_do_ioctl;
  2525. SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
  2526. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  2527. #ifdef MAX_SKB_FRAGS
  2528. /*
  2529. * Zero copy can only work if we use Discovery II memory. Else, we will
  2530. * have to map the buffers to ISA memory which is only 16 MB
  2531. */
  2532. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
  2533. #endif
  2534. #endif
  2535. /* Configure the timeout task */
  2536. INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
  2537. spin_lock_init(&mp->lock);
  2538. mp->shared = platform_get_drvdata(pd->shared);
  2539. port_num = mp->port_num = pd->port_number;
  2540. if (mp->shared->win_protect)
  2541. wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
  2542. mp->shared_smi = mp->shared;
  2543. if (pd->shared_smi != NULL)
  2544. mp->shared_smi = platform_get_drvdata(pd->shared_smi);
  2545. /* set default config values */
  2546. eth_port_uc_addr_get(mp, dev->dev_addr);
  2547. mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
  2548. mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
  2549. if (is_valid_ether_addr(pd->mac_addr))
  2550. memcpy(dev->dev_addr, pd->mac_addr, 6);
  2551. if (pd->phy_addr || pd->force_phy_addr)
  2552. ethernet_phy_set(mp, pd->phy_addr);
  2553. if (pd->rx_queue_size)
  2554. mp->rx_ring_size = pd->rx_queue_size;
  2555. if (pd->tx_queue_size)
  2556. mp->tx_ring_size = pd->tx_queue_size;
  2557. if (pd->tx_sram_size) {
  2558. mp->tx_sram_size = pd->tx_sram_size;
  2559. mp->tx_sram_addr = pd->tx_sram_addr;
  2560. }
  2561. if (pd->rx_sram_size) {
  2562. mp->rx_sram_size = pd->rx_sram_size;
  2563. mp->rx_sram_addr = pd->rx_sram_addr;
  2564. }
  2565. duplex = pd->duplex;
  2566. speed = pd->speed;
  2567. /* Hook up MII support for ethtool */
  2568. mp->mii.dev = dev;
  2569. mp->mii.mdio_read = mv643xx_mdio_read;
  2570. mp->mii.mdio_write = mv643xx_mdio_write;
  2571. mp->mii.phy_id = ethernet_phy_get(mp);
  2572. mp->mii.phy_id_mask = 0x3f;
  2573. mp->mii.reg_num_mask = 0x1f;
  2574. err = ethernet_phy_detect(mp);
  2575. if (err) {
  2576. pr_debug("%s: No PHY detected at addr %d\n",
  2577. dev->name, ethernet_phy_get(mp));
  2578. goto out;
  2579. }
  2580. ethernet_phy_reset(mp);
  2581. mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
  2582. mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
  2583. mv643xx_eth_update_pscr(dev, &cmd);
  2584. mv643xx_set_settings(dev, &cmd);
  2585. SET_NETDEV_DEV(dev, &pdev->dev);
  2586. err = register_netdev(dev);
  2587. if (err)
  2588. goto out;
  2589. p = dev->dev_addr;
  2590. printk(KERN_NOTICE
  2591. "%s: port %d with MAC address %s\n",
  2592. dev->name, port_num, print_mac(mac, p));
  2593. if (dev->features & NETIF_F_SG)
  2594. printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
  2595. if (dev->features & NETIF_F_IP_CSUM)
  2596. printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
  2597. dev->name);
  2598. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  2599. printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
  2600. #endif
  2601. #ifdef MV643XX_COAL
  2602. printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
  2603. dev->name);
  2604. #endif
  2605. #ifdef MV643XX_NAPI
  2606. printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
  2607. #endif
  2608. if (mp->tx_sram_size > 0)
  2609. printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
  2610. return 0;
  2611. out:
  2612. free_netdev(dev);
  2613. return err;
  2614. }
  2615. static int mv643xx_eth_remove(struct platform_device *pdev)
  2616. {
  2617. struct net_device *dev = platform_get_drvdata(pdev);
  2618. unregister_netdev(dev);
  2619. flush_scheduled_work();
  2620. free_netdev(dev);
  2621. platform_set_drvdata(pdev, NULL);
  2622. return 0;
  2623. }
  2624. static void mv643xx_eth_shutdown(struct platform_device *pdev)
  2625. {
  2626. struct net_device *dev = platform_get_drvdata(pdev);
  2627. struct mv643xx_private *mp = netdev_priv(dev);
  2628. unsigned int port_num = mp->port_num;
  2629. /* Mask all interrupts on ethernet port */
  2630. wrl(mp, INT_MASK(port_num), 0);
  2631. rdl(mp, INT_MASK(port_num));
  2632. eth_port_reset(mp);
  2633. }
  2634. static struct platform_driver mv643xx_eth_driver = {
  2635. .probe = mv643xx_eth_probe,
  2636. .remove = mv643xx_eth_remove,
  2637. .shutdown = mv643xx_eth_shutdown,
  2638. .driver = {
  2639. .name = MV643XX_ETH_NAME,
  2640. .owner = THIS_MODULE,
  2641. },
  2642. };
  2643. /*
  2644. * mv643xx_init_module
  2645. *
  2646. * Registers the network drivers into the Linux kernel
  2647. *
  2648. * Input : N/A
  2649. *
  2650. * Output : N/A
  2651. */
  2652. static int __init mv643xx_init_module(void)
  2653. {
  2654. int rc;
  2655. rc = platform_driver_register(&mv643xx_eth_shared_driver);
  2656. if (!rc) {
  2657. rc = platform_driver_register(&mv643xx_eth_driver);
  2658. if (rc)
  2659. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2660. }
  2661. return rc;
  2662. }
  2663. /*
  2664. * mv643xx_cleanup_module
  2665. *
  2666. * Registers the network drivers into the Linux kernel
  2667. *
  2668. * Input : N/A
  2669. *
  2670. * Output : N/A
  2671. */
  2672. static void __exit mv643xx_cleanup_module(void)
  2673. {
  2674. platform_driver_unregister(&mv643xx_eth_driver);
  2675. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2676. }
  2677. module_init(mv643xx_init_module);
  2678. module_exit(mv643xx_cleanup_module);
  2679. MODULE_LICENSE("GPL");
  2680. MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
  2681. " and Dale Farnsworth");
  2682. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  2683. MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
  2684. MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);