mv643xx_eth.c 96 KB

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