mv643xx_eth.c 96 KB

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