mv643xx_eth.c 87 KB

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