mv643xx_eth.c 98 KB

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