mv643xx_eth.c 96 KB

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