mv643xx_eth.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033
  1. /*
  2. * drivers/net/mv643xx_eth.c - Driver for MV643XX 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 (lachwani@pmc-sierra.com)
  10. *
  11. * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
  12. *
  13. * Copyright (C) 2004-2005 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/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/etherdevice.h>
  38. #include <linux/bitops.h>
  39. #include <linux/delay.h>
  40. #include <linux/ethtool.h>
  41. #include <asm/io.h>
  42. #include <asm/types.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/system.h>
  45. #include <asm/delay.h>
  46. #include "mv643xx_eth.h"
  47. /*
  48. * The first part is the high level driver of the gigE ethernet ports.
  49. */
  50. /* Constants */
  51. #define VLAN_HLEN 4
  52. #define FCS_LEN 4
  53. #define WRAP NET_IP_ALIGN + ETH_HLEN + VLAN_HLEN + FCS_LEN
  54. #define RX_SKB_SIZE ((dev->mtu + WRAP + 7) & ~0x7)
  55. #define INT_CAUSE_UNMASK_ALL 0x0007ffff
  56. #define INT_CAUSE_UNMASK_ALL_EXT 0x0011ffff
  57. #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK
  58. #define INT_CAUSE_MASK_ALL 0x00000000
  59. #define INT_CAUSE_CHECK_BITS INT_CAUSE_UNMASK_ALL
  60. #define INT_CAUSE_CHECK_BITS_EXT INT_CAUSE_UNMASK_ALL_EXT
  61. #endif
  62. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  63. #define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
  64. #else
  65. #define MAX_DESCS_PER_SKB 1
  66. #endif
  67. #define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
  68. #define PHY_WAIT_MICRO_SECONDS 10
  69. /* Static function declarations */
  70. static int eth_port_link_is_up(unsigned int eth_port_num);
  71. static void eth_port_uc_addr_get(struct net_device *dev,
  72. unsigned char *MacAddr);
  73. static int mv643xx_eth_real_open(struct net_device *);
  74. static int mv643xx_eth_real_stop(struct net_device *);
  75. static int mv643xx_eth_change_mtu(struct net_device *, int);
  76. static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *);
  77. static void eth_port_init_mac_tables(unsigned int eth_port_num);
  78. #ifdef MV643XX_NAPI
  79. static int mv643xx_poll(struct net_device *dev, int *budget);
  80. #endif
  81. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
  82. static int ethernet_phy_detect(unsigned int eth_port_num);
  83. static struct ethtool_ops mv643xx_ethtool_ops;
  84. static char mv643xx_driver_name[] = "mv643xx_eth";
  85. static char mv643xx_driver_version[] = "1.0";
  86. static void __iomem *mv643xx_eth_shared_base;
  87. /* used to protect MV643XX_ETH_SMI_REG, which is shared across ports */
  88. static spinlock_t mv643xx_eth_phy_lock = SPIN_LOCK_UNLOCKED;
  89. static inline u32 mv_read(int offset)
  90. {
  91. void __iomem *reg_base;
  92. reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS;
  93. return readl(reg_base + offset);
  94. }
  95. static inline void mv_write(int offset, u32 data)
  96. {
  97. void __iomem *reg_base;
  98. reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS;
  99. writel(data, reg_base + offset);
  100. }
  101. /*
  102. * Changes MTU (maximum transfer unit) of the gigabit ethenret port
  103. *
  104. * Input : pointer to ethernet interface network device structure
  105. * new mtu size
  106. * Output : 0 upon success, -EINVAL upon failure
  107. */
  108. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  109. {
  110. struct mv643xx_private *mp = netdev_priv(dev);
  111. unsigned long flags;
  112. spin_lock_irqsave(&mp->lock, flags);
  113. if ((new_mtu > 9500) || (new_mtu < 64)) {
  114. spin_unlock_irqrestore(&mp->lock, flags);
  115. return -EINVAL;
  116. }
  117. dev->mtu = new_mtu;
  118. /*
  119. * Stop then re-open the interface. This will allocate RX skb's with
  120. * the new MTU.
  121. * There is a possible danger that the open will not successed, due
  122. * to memory is full, which might fail the open function.
  123. */
  124. if (netif_running(dev)) {
  125. if (mv643xx_eth_real_stop(dev))
  126. printk(KERN_ERR
  127. "%s: Fatal error on stopping device\n",
  128. dev->name);
  129. if (mv643xx_eth_real_open(dev))
  130. printk(KERN_ERR
  131. "%s: Fatal error on opening device\n",
  132. dev->name);
  133. }
  134. spin_unlock_irqrestore(&mp->lock, flags);
  135. return 0;
  136. }
  137. /*
  138. * mv643xx_eth_rx_task
  139. *
  140. * Fills / refills RX queue on a certain gigabit ethernet port
  141. *
  142. * Input : pointer to ethernet interface network device structure
  143. * Output : N/A
  144. */
  145. static void mv643xx_eth_rx_task(void *data)
  146. {
  147. struct net_device *dev = (struct net_device *)data;
  148. struct mv643xx_private *mp = netdev_priv(dev);
  149. struct pkt_info pkt_info;
  150. struct sk_buff *skb;
  151. if (test_and_set_bit(0, &mp->rx_task_busy))
  152. panic("%s: Error in test_set_bit / clear_bit", dev->name);
  153. while (mp->rx_ring_skbs < (mp->rx_ring_size - 5)) {
  154. skb = dev_alloc_skb(RX_SKB_SIZE);
  155. if (!skb)
  156. break;
  157. mp->rx_ring_skbs++;
  158. pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
  159. pkt_info.byte_cnt = RX_SKB_SIZE;
  160. pkt_info.buf_ptr = dma_map_single(NULL, skb->data, RX_SKB_SIZE,
  161. DMA_FROM_DEVICE);
  162. pkt_info.return_info = skb;
  163. if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
  164. printk(KERN_ERR
  165. "%s: Error allocating RX Ring\n", dev->name);
  166. break;
  167. }
  168. skb_reserve(skb, 2);
  169. }
  170. clear_bit(0, &mp->rx_task_busy);
  171. /*
  172. * If RX ring is empty of SKB, set a timer to try allocating
  173. * again in a later time .
  174. */
  175. if ((mp->rx_ring_skbs == 0) && (mp->rx_timer_flag == 0)) {
  176. printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
  177. /* After 100mSec */
  178. mp->timeout.expires = jiffies + (HZ / 10);
  179. add_timer(&mp->timeout);
  180. mp->rx_timer_flag = 1;
  181. }
  182. #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK
  183. else {
  184. /* Return interrupts */
  185. mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(mp->port_num),
  186. INT_CAUSE_UNMASK_ALL);
  187. }
  188. #endif
  189. }
  190. /*
  191. * mv643xx_eth_rx_task_timer_wrapper
  192. *
  193. * Timer routine to wake up RX queue filling task. This function is
  194. * used only in case the RX queue is empty, and all alloc_skb has
  195. * failed (due to out of memory event).
  196. *
  197. * Input : pointer to ethernet interface network device structure
  198. * Output : N/A
  199. */
  200. static void mv643xx_eth_rx_task_timer_wrapper(unsigned long data)
  201. {
  202. struct net_device *dev = (struct net_device *)data;
  203. struct mv643xx_private *mp = netdev_priv(dev);
  204. mp->rx_timer_flag = 0;
  205. mv643xx_eth_rx_task((void *)data);
  206. }
  207. /*
  208. * mv643xx_eth_update_mac_address
  209. *
  210. * Update the MAC address of the port in the address table
  211. *
  212. * Input : pointer to ethernet interface network device structure
  213. * Output : N/A
  214. */
  215. static void mv643xx_eth_update_mac_address(struct net_device *dev)
  216. {
  217. struct mv643xx_private *mp = netdev_priv(dev);
  218. unsigned int port_num = mp->port_num;
  219. eth_port_init_mac_tables(port_num);
  220. memcpy(mp->port_mac_addr, dev->dev_addr, 6);
  221. eth_port_uc_addr_set(port_num, mp->port_mac_addr);
  222. }
  223. /*
  224. * mv643xx_eth_set_rx_mode
  225. *
  226. * Change from promiscuos to regular rx mode
  227. *
  228. * Input : pointer to ethernet interface network device structure
  229. * Output : N/A
  230. */
  231. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  232. {
  233. struct mv643xx_private *mp = netdev_priv(dev);
  234. u32 config_reg;
  235. config_reg = ethernet_get_config_reg(mp->port_num);
  236. if (dev->flags & IFF_PROMISC)
  237. config_reg |= (u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE;
  238. else
  239. config_reg &= ~(u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE;
  240. ethernet_set_config_reg(mp->port_num, config_reg);
  241. }
  242. /*
  243. * mv643xx_eth_set_mac_address
  244. *
  245. * Change the interface's mac address.
  246. * No special hardware thing should be done because interface is always
  247. * put in promiscuous mode.
  248. *
  249. * Input : pointer to ethernet interface network device structure and
  250. * a pointer to the designated entry to be added to the cache.
  251. * Output : zero upon success, negative upon failure
  252. */
  253. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  254. {
  255. int i;
  256. for (i = 0; i < 6; i++)
  257. /* +2 is for the offset of the HW addr type */
  258. dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
  259. mv643xx_eth_update_mac_address(dev);
  260. return 0;
  261. }
  262. /*
  263. * mv643xx_eth_tx_timeout
  264. *
  265. * Called upon a timeout on transmitting a packet
  266. *
  267. * Input : pointer to ethernet interface network device structure.
  268. * Output : N/A
  269. */
  270. static void mv643xx_eth_tx_timeout(struct net_device *dev)
  271. {
  272. struct mv643xx_private *mp = netdev_priv(dev);
  273. printk(KERN_INFO "%s: TX timeout ", dev->name);
  274. /* Do the reset outside of interrupt context */
  275. schedule_work(&mp->tx_timeout_task);
  276. }
  277. /*
  278. * mv643xx_eth_tx_timeout_task
  279. *
  280. * Actual routine to reset the adapter when a timeout on Tx has occurred
  281. */
  282. static void mv643xx_eth_tx_timeout_task(struct net_device *dev)
  283. {
  284. struct mv643xx_private *mp = netdev_priv(dev);
  285. netif_device_detach(dev);
  286. eth_port_reset(mp->port_num);
  287. eth_port_start(mp);
  288. netif_device_attach(dev);
  289. }
  290. /*
  291. * mv643xx_eth_free_tx_queue
  292. *
  293. * Input : dev - a pointer to the required interface
  294. *
  295. * Output : 0 if was able to release skb , nonzero otherwise
  296. */
  297. static int mv643xx_eth_free_tx_queue(struct net_device *dev,
  298. unsigned int eth_int_cause_ext)
  299. {
  300. struct mv643xx_private *mp = netdev_priv(dev);
  301. struct net_device_stats *stats = &mp->stats;
  302. struct pkt_info pkt_info;
  303. int released = 1;
  304. if (!(eth_int_cause_ext & (BIT0 | BIT8)))
  305. return released;
  306. spin_lock(&mp->lock);
  307. /* Check only queue 0 */
  308. while (eth_tx_return_desc(mp, &pkt_info) == ETH_OK) {
  309. if (pkt_info.cmd_sts & BIT0) {
  310. printk("%s: Error in TX\n", dev->name);
  311. stats->tx_errors++;
  312. }
  313. /*
  314. * If return_info is different than 0, release the skb.
  315. * The case where return_info is not 0 is only in case
  316. * when transmitted a scatter/gather packet, where only
  317. * last skb releases the whole chain.
  318. */
  319. if (pkt_info.return_info) {
  320. if (skb_shinfo(pkt_info.return_info)->nr_frags)
  321. dma_unmap_page(NULL, pkt_info.buf_ptr,
  322. pkt_info.byte_cnt,
  323. DMA_TO_DEVICE);
  324. else
  325. dma_unmap_single(NULL, pkt_info.buf_ptr,
  326. pkt_info.byte_cnt,
  327. DMA_TO_DEVICE);
  328. dev_kfree_skb_irq(pkt_info.return_info);
  329. released = 0;
  330. /*
  331. * Decrement the number of outstanding skbs counter on
  332. * the TX queue.
  333. */
  334. if (mp->tx_ring_skbs == 0)
  335. panic("ERROR - TX outstanding SKBs"
  336. " counter is corrupted");
  337. mp->tx_ring_skbs--;
  338. } else
  339. dma_unmap_page(NULL, pkt_info.buf_ptr,
  340. pkt_info.byte_cnt, DMA_TO_DEVICE);
  341. }
  342. spin_unlock(&mp->lock);
  343. return released;
  344. }
  345. /*
  346. * mv643xx_eth_receive
  347. *
  348. * This function is forward packets that are received from the port's
  349. * queues toward kernel core or FastRoute them to another interface.
  350. *
  351. * Input : dev - a pointer to the required interface
  352. * max - maximum number to receive (0 means unlimted)
  353. *
  354. * Output : number of served packets
  355. */
  356. #ifdef MV643XX_NAPI
  357. static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
  358. #else
  359. static int mv643xx_eth_receive_queue(struct net_device *dev)
  360. #endif
  361. {
  362. struct mv643xx_private *mp = netdev_priv(dev);
  363. struct net_device_stats *stats = &mp->stats;
  364. unsigned int received_packets = 0;
  365. struct sk_buff *skb;
  366. struct pkt_info pkt_info;
  367. #ifdef MV643XX_NAPI
  368. while (eth_port_receive(mp, &pkt_info) == ETH_OK && budget > 0) {
  369. #else
  370. while (eth_port_receive(mp, &pkt_info) == ETH_OK) {
  371. #endif
  372. mp->rx_ring_skbs--;
  373. received_packets++;
  374. #ifdef MV643XX_NAPI
  375. budget--;
  376. #endif
  377. /* Update statistics. Note byte count includes 4 byte CRC count */
  378. stats->rx_packets++;
  379. stats->rx_bytes += pkt_info.byte_cnt;
  380. skb = pkt_info.return_info;
  381. /*
  382. * In case received a packet without first / last bits on OR
  383. * the error summary bit is on, the packets needs to be dropeed.
  384. */
  385. if (((pkt_info.cmd_sts
  386. & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
  387. (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
  388. || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
  389. stats->rx_dropped++;
  390. if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
  391. ETH_RX_LAST_DESC)) !=
  392. (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
  393. if (net_ratelimit())
  394. printk(KERN_ERR
  395. "%s: Received packet spread "
  396. "on multiple descriptors\n",
  397. dev->name);
  398. }
  399. if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
  400. stats->rx_errors++;
  401. dev_kfree_skb_irq(skb);
  402. } else {
  403. /*
  404. * The -4 is for the CRC in the trailer of the
  405. * received packet
  406. */
  407. skb_put(skb, pkt_info.byte_cnt - 4);
  408. skb->dev = dev;
  409. if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
  410. skb->ip_summed = CHECKSUM_UNNECESSARY;
  411. skb->csum = htons(
  412. (pkt_info.cmd_sts & 0x0007fff8) >> 3);
  413. }
  414. skb->protocol = eth_type_trans(skb, dev);
  415. #ifdef MV643XX_NAPI
  416. netif_receive_skb(skb);
  417. #else
  418. netif_rx(skb);
  419. #endif
  420. }
  421. }
  422. return received_packets;
  423. }
  424. /*
  425. * mv643xx_eth_int_handler
  426. *
  427. * Main interrupt handler for the gigbit ethernet ports
  428. *
  429. * Input : irq - irq number (not used)
  430. * dev_id - a pointer to the required interface's data structure
  431. * regs - not used
  432. * Output : N/A
  433. */
  434. static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id,
  435. struct pt_regs *regs)
  436. {
  437. struct net_device *dev = (struct net_device *)dev_id;
  438. struct mv643xx_private *mp = netdev_priv(dev);
  439. u32 eth_int_cause, eth_int_cause_ext = 0;
  440. unsigned int port_num = mp->port_num;
  441. /* Read interrupt cause registers */
  442. eth_int_cause = mv_read(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num)) &
  443. INT_CAUSE_UNMASK_ALL;
  444. if (eth_int_cause & BIT1)
  445. eth_int_cause_ext = mv_read(
  446. MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num)) &
  447. INT_CAUSE_UNMASK_ALL_EXT;
  448. #ifdef MV643XX_NAPI
  449. if (!(eth_int_cause & 0x0007fffd)) {
  450. /* Dont ack the Rx interrupt */
  451. #endif
  452. /*
  453. * Clear specific ethernet port intrerrupt registers by
  454. * acknowleding relevant bits.
  455. */
  456. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num),
  457. ~eth_int_cause);
  458. if (eth_int_cause_ext != 0x0)
  459. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG
  460. (port_num), ~eth_int_cause_ext);
  461. /* UDP change : We may need this */
  462. if ((eth_int_cause_ext & 0x0000ffff) &&
  463. (mv643xx_eth_free_tx_queue(dev, eth_int_cause_ext) == 0) &&
  464. (mp->tx_ring_size > mp->tx_ring_skbs + MAX_DESCS_PER_SKB))
  465. netif_wake_queue(dev);
  466. #ifdef MV643XX_NAPI
  467. } else {
  468. if (netif_rx_schedule_prep(dev)) {
  469. /* Mask all the interrupts */
  470. mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), 0);
  471. mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG
  472. (port_num), 0);
  473. __netif_rx_schedule(dev);
  474. }
  475. #else
  476. if (eth_int_cause & (BIT2 | BIT11))
  477. mv643xx_eth_receive_queue(dev, 0);
  478. /*
  479. * After forwarded received packets to upper layer, add a task
  480. * in an interrupts enabled context that refills the RX ring
  481. * with skb's.
  482. */
  483. #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK
  484. /* Unmask all interrupts on ethernet port */
  485. mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
  486. INT_CAUSE_MASK_ALL);
  487. queue_task(&mp->rx_task, &tq_immediate);
  488. mark_bh(IMMEDIATE_BH);
  489. #else
  490. mp->rx_task.func(dev);
  491. #endif
  492. #endif
  493. }
  494. /* PHY status changed */
  495. if (eth_int_cause_ext & (BIT16 | BIT20)) {
  496. if (eth_port_link_is_up(port_num)) {
  497. netif_carrier_on(dev);
  498. netif_wake_queue(dev);
  499. /* Start TX queue */
  500. mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG
  501. (port_num), 1);
  502. } else {
  503. netif_carrier_off(dev);
  504. netif_stop_queue(dev);
  505. }
  506. }
  507. /*
  508. * If no real interrupt occured, exit.
  509. * This can happen when using gigE interrupt coalescing mechanism.
  510. */
  511. if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
  512. return IRQ_NONE;
  513. return IRQ_HANDLED;
  514. }
  515. #ifdef MV643XX_COAL
  516. /*
  517. * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
  518. *
  519. * DESCRIPTION:
  520. * This routine sets the RX coalescing interrupt mechanism parameter.
  521. * This parameter is a timeout counter, that counts in 64 t_clk
  522. * chunks ; that when timeout event occurs a maskable interrupt
  523. * occurs.
  524. * The parameter is calculated using the tClk of the MV-643xx chip
  525. * , and the required delay of the interrupt in usec.
  526. *
  527. * INPUT:
  528. * unsigned int eth_port_num Ethernet port number
  529. * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
  530. * unsigned int delay Delay in usec
  531. *
  532. * OUTPUT:
  533. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  534. *
  535. * RETURN:
  536. * The interrupt coalescing value set in the gigE port.
  537. *
  538. */
  539. static unsigned int eth_port_set_rx_coal(unsigned int eth_port_num,
  540. unsigned int t_clk, unsigned int delay)
  541. {
  542. unsigned int coal = ((t_clk / 1000000) * delay) / 64;
  543. /* Set RX Coalescing mechanism */
  544. mv_write(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num),
  545. ((coal & 0x3fff) << 8) |
  546. (mv_read(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num))
  547. & 0xffc000ff));
  548. return coal;
  549. }
  550. #endif
  551. /*
  552. * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
  553. *
  554. * DESCRIPTION:
  555. * This routine sets the TX coalescing interrupt mechanism parameter.
  556. * This parameter is a timeout counter, that counts in 64 t_clk
  557. * chunks ; that when timeout event occurs a maskable interrupt
  558. * occurs.
  559. * The parameter is calculated using the t_cLK frequency of the
  560. * MV-643xx chip and the required delay in the interrupt in uSec
  561. *
  562. * INPUT:
  563. * unsigned int eth_port_num Ethernet port number
  564. * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
  565. * unsigned int delay Delay in uSeconds
  566. *
  567. * OUTPUT:
  568. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  569. *
  570. * RETURN:
  571. * The interrupt coalescing value set in the gigE port.
  572. *
  573. */
  574. static unsigned int eth_port_set_tx_coal(unsigned int eth_port_num,
  575. unsigned int t_clk, unsigned int delay)
  576. {
  577. unsigned int coal;
  578. coal = ((t_clk / 1000000) * delay) / 64;
  579. /* Set TX Coalescing mechanism */
  580. mv_write(MV643XX_ETH_TX_FIFO_URGENT_THRESHOLD_REG(eth_port_num),
  581. coal << 4);
  582. return coal;
  583. }
  584. /*
  585. * mv643xx_eth_open
  586. *
  587. * This function is called when openning the network device. The function
  588. * should initialize all the hardware, initialize cyclic Rx/Tx
  589. * descriptors chain and buffers and allocate an IRQ to the network
  590. * device.
  591. *
  592. * Input : a pointer to the network device structure
  593. *
  594. * Output : zero of success , nonzero if fails.
  595. */
  596. static int mv643xx_eth_open(struct net_device *dev)
  597. {
  598. struct mv643xx_private *mp = netdev_priv(dev);
  599. unsigned int port_num = mp->port_num;
  600. int err;
  601. spin_lock_irq(&mp->lock);
  602. err = request_irq(dev->irq, mv643xx_eth_int_handler,
  603. SA_SHIRQ | SA_SAMPLE_RANDOM, dev->name, dev);
  604. if (err) {
  605. printk(KERN_ERR "Can not assign IRQ number to MV643XX_eth%d\n",
  606. port_num);
  607. err = -EAGAIN;
  608. goto out;
  609. }
  610. if (mv643xx_eth_real_open(dev)) {
  611. printk("%s: Error opening interface\n", dev->name);
  612. err = -EBUSY;
  613. goto out_free;
  614. }
  615. spin_unlock_irq(&mp->lock);
  616. return 0;
  617. out_free:
  618. free_irq(dev->irq, dev);
  619. out:
  620. spin_unlock_irq(&mp->lock);
  621. return err;
  622. }
  623. /*
  624. * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
  625. *
  626. * DESCRIPTION:
  627. * This function prepares a Rx chained list of descriptors and packet
  628. * buffers in a form of a ring. The routine must be called after port
  629. * initialization routine and before port start routine.
  630. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  631. * devices in the system (i.e. DRAM). This function uses the ethernet
  632. * struct 'virtual to physical' routine (set by the user) to set the ring
  633. * with physical addresses.
  634. *
  635. * INPUT:
  636. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  637. *
  638. * OUTPUT:
  639. * The routine updates the Ethernet port control struct with information
  640. * regarding the Rx descriptors and buffers.
  641. *
  642. * RETURN:
  643. * None.
  644. */
  645. static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
  646. {
  647. volatile struct eth_rx_desc *p_rx_desc;
  648. int rx_desc_num = mp->rx_ring_size;
  649. int i;
  650. /* initialize the next_desc_ptr links in the Rx descriptors ring */
  651. p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
  652. for (i = 0; i < rx_desc_num; i++) {
  653. p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
  654. ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
  655. }
  656. /* Save Rx desc pointer to driver struct. */
  657. mp->rx_curr_desc_q = 0;
  658. mp->rx_used_desc_q = 0;
  659. mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
  660. /* Add the queue to the list of RX queues of this port */
  661. mp->port_rx_queue_command |= 1;
  662. }
  663. /*
  664. * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
  665. *
  666. * DESCRIPTION:
  667. * This function prepares a Tx chained list of descriptors and packet
  668. * buffers in a form of a ring. The routine must be called after port
  669. * initialization routine and before port start routine.
  670. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  671. * devices in the system (i.e. DRAM). This function uses the ethernet
  672. * struct 'virtual to physical' routine (set by the user) to set the ring
  673. * with physical addresses.
  674. *
  675. * INPUT:
  676. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  677. *
  678. * OUTPUT:
  679. * The routine updates the Ethernet port control struct with information
  680. * regarding the Tx descriptors and buffers.
  681. *
  682. * RETURN:
  683. * None.
  684. */
  685. static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
  686. {
  687. int tx_desc_num = mp->tx_ring_size;
  688. struct eth_tx_desc *p_tx_desc;
  689. int i;
  690. /* Initialize the next_desc_ptr links in the Tx descriptors ring */
  691. p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
  692. for (i = 0; i < tx_desc_num; i++) {
  693. p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
  694. ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
  695. }
  696. mp->tx_curr_desc_q = 0;
  697. mp->tx_used_desc_q = 0;
  698. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  699. mp->tx_first_desc_q = 0;
  700. #endif
  701. mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
  702. /* Add the queue to the list of Tx queues of this port */
  703. mp->port_tx_queue_command |= 1;
  704. }
  705. /* Helper function for mv643xx_eth_open */
  706. static int mv643xx_eth_real_open(struct net_device *dev)
  707. {
  708. struct mv643xx_private *mp = netdev_priv(dev);
  709. unsigned int port_num = mp->port_num;
  710. unsigned int size;
  711. /* Stop RX Queues */
  712. mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), 0x0000ff00);
  713. /* Clear the ethernet port interrupts */
  714. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
  715. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
  716. /* Unmask RX buffer and TX end interrupt */
  717. mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
  718. INT_CAUSE_UNMASK_ALL);
  719. /* Unmask phy and link status changes interrupts */
  720. mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num),
  721. INT_CAUSE_UNMASK_ALL_EXT);
  722. /* Set the MAC Address */
  723. memcpy(mp->port_mac_addr, dev->dev_addr, 6);
  724. eth_port_init(mp);
  725. INIT_WORK(&mp->rx_task, (void (*)(void *))mv643xx_eth_rx_task, dev);
  726. memset(&mp->timeout, 0, sizeof(struct timer_list));
  727. mp->timeout.function = mv643xx_eth_rx_task_timer_wrapper;
  728. mp->timeout.data = (unsigned long)dev;
  729. mp->rx_task_busy = 0;
  730. mp->rx_timer_flag = 0;
  731. /* Allocate RX and TX skb rings */
  732. mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
  733. GFP_KERNEL);
  734. if (!mp->rx_skb) {
  735. printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
  736. return -ENOMEM;
  737. }
  738. mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
  739. GFP_KERNEL);
  740. if (!mp->tx_skb) {
  741. printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
  742. kfree(mp->rx_skb);
  743. return -ENOMEM;
  744. }
  745. /* Allocate TX ring */
  746. mp->tx_ring_skbs = 0;
  747. size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
  748. mp->tx_desc_area_size = size;
  749. if (mp->tx_sram_size) {
  750. mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
  751. mp->tx_sram_size);
  752. mp->tx_desc_dma = mp->tx_sram_addr;
  753. } else
  754. mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
  755. &mp->tx_desc_dma,
  756. GFP_KERNEL);
  757. if (!mp->p_tx_desc_area) {
  758. printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
  759. dev->name, size);
  760. kfree(mp->rx_skb);
  761. kfree(mp->tx_skb);
  762. return -ENOMEM;
  763. }
  764. BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
  765. memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
  766. ether_init_tx_desc_ring(mp);
  767. /* Allocate RX ring */
  768. mp->rx_ring_skbs = 0;
  769. size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
  770. mp->rx_desc_area_size = size;
  771. if (mp->rx_sram_size) {
  772. mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
  773. mp->rx_sram_size);
  774. mp->rx_desc_dma = mp->rx_sram_addr;
  775. } else
  776. mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
  777. &mp->rx_desc_dma,
  778. GFP_KERNEL);
  779. if (!mp->p_rx_desc_area) {
  780. printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
  781. dev->name, size);
  782. printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
  783. dev->name);
  784. if (mp->rx_sram_size)
  785. iounmap(mp->p_rx_desc_area);
  786. else
  787. dma_free_coherent(NULL, mp->tx_desc_area_size,
  788. mp->p_tx_desc_area, mp->tx_desc_dma);
  789. kfree(mp->rx_skb);
  790. kfree(mp->tx_skb);
  791. return -ENOMEM;
  792. }
  793. memset((void *)mp->p_rx_desc_area, 0, size);
  794. ether_init_rx_desc_ring(mp);
  795. mv643xx_eth_rx_task(dev); /* Fill RX ring with skb's */
  796. eth_port_start(mp);
  797. /* Interrupt Coalescing */
  798. #ifdef MV643XX_COAL
  799. mp->rx_int_coal =
  800. eth_port_set_rx_coal(port_num, 133000000, MV643XX_RX_COAL);
  801. #endif
  802. mp->tx_int_coal =
  803. eth_port_set_tx_coal(port_num, 133000000, MV643XX_TX_COAL);
  804. netif_start_queue(dev);
  805. return 0;
  806. }
  807. static void mv643xx_eth_free_tx_rings(struct net_device *dev)
  808. {
  809. struct mv643xx_private *mp = netdev_priv(dev);
  810. unsigned int port_num = mp->port_num;
  811. unsigned int curr;
  812. /* Stop Tx Queues */
  813. mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num), 0x0000ff00);
  814. /* Free outstanding skb's on TX rings */
  815. for (curr = 0; mp->tx_ring_skbs && curr < mp->tx_ring_size; curr++) {
  816. if (mp->tx_skb[curr]) {
  817. dev_kfree_skb(mp->tx_skb[curr]);
  818. mp->tx_ring_skbs--;
  819. }
  820. }
  821. if (mp->tx_ring_skbs)
  822. printk("%s: Error on Tx descriptor free - could not free %d"
  823. " descriptors\n", dev->name, mp->tx_ring_skbs);
  824. /* Free TX ring */
  825. if (mp->tx_sram_size)
  826. iounmap(mp->p_tx_desc_area);
  827. else
  828. dma_free_coherent(NULL, mp->tx_desc_area_size,
  829. mp->p_tx_desc_area, mp->tx_desc_dma);
  830. }
  831. static void mv643xx_eth_free_rx_rings(struct net_device *dev)
  832. {
  833. struct mv643xx_private *mp = netdev_priv(dev);
  834. unsigned int port_num = mp->port_num;
  835. int curr;
  836. /* Stop RX Queues */
  837. mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), 0x0000ff00);
  838. /* Free preallocated skb's on RX rings */
  839. for (curr = 0; mp->rx_ring_skbs && curr < mp->rx_ring_size; curr++) {
  840. if (mp->rx_skb[curr]) {
  841. dev_kfree_skb(mp->rx_skb[curr]);
  842. mp->rx_ring_skbs--;
  843. }
  844. }
  845. if (mp->rx_ring_skbs)
  846. printk(KERN_ERR
  847. "%s: Error in freeing Rx Ring. %d skb's still"
  848. " stuck in RX Ring - ignoring them\n", dev->name,
  849. mp->rx_ring_skbs);
  850. /* Free RX ring */
  851. if (mp->rx_sram_size)
  852. iounmap(mp->p_rx_desc_area);
  853. else
  854. dma_free_coherent(NULL, mp->rx_desc_area_size,
  855. mp->p_rx_desc_area, mp->rx_desc_dma);
  856. }
  857. /*
  858. * mv643xx_eth_stop
  859. *
  860. * This function is used when closing the network device.
  861. * It updates the hardware,
  862. * release all memory that holds buffers and descriptors and release the IRQ.
  863. * Input : a pointer to the device structure
  864. * Output : zero if success , nonzero if fails
  865. */
  866. /* Helper function for mv643xx_eth_stop */
  867. static int mv643xx_eth_real_stop(struct net_device *dev)
  868. {
  869. struct mv643xx_private *mp = netdev_priv(dev);
  870. unsigned int port_num = mp->port_num;
  871. netif_carrier_off(dev);
  872. netif_stop_queue(dev);
  873. mv643xx_eth_free_tx_rings(dev);
  874. mv643xx_eth_free_rx_rings(dev);
  875. eth_port_reset(mp->port_num);
  876. /* Disable ethernet port interrupts */
  877. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
  878. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
  879. /* Mask RX buffer and TX end interrupt */
  880. mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), 0);
  881. /* Mask phy and link status changes interrupts */
  882. mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num), 0);
  883. return 0;
  884. }
  885. static int mv643xx_eth_stop(struct net_device *dev)
  886. {
  887. struct mv643xx_private *mp = netdev_priv(dev);
  888. spin_lock_irq(&mp->lock);
  889. mv643xx_eth_real_stop(dev);
  890. free_irq(dev->irq, dev);
  891. spin_unlock_irq(&mp->lock);
  892. return 0;
  893. }
  894. #ifdef MV643XX_NAPI
  895. static void mv643xx_tx(struct net_device *dev)
  896. {
  897. struct mv643xx_private *mp = netdev_priv(dev);
  898. struct pkt_info pkt_info;
  899. while (eth_tx_return_desc(mp, &pkt_info) == ETH_OK) {
  900. if (pkt_info.return_info) {
  901. if (skb_shinfo(pkt_info.return_info)->nr_frags)
  902. dma_unmap_page(NULL, pkt_info.buf_ptr,
  903. pkt_info.byte_cnt,
  904. DMA_TO_DEVICE);
  905. else
  906. dma_unmap_single(NULL, pkt_info.buf_ptr,
  907. pkt_info.byte_cnt,
  908. DMA_TO_DEVICE);
  909. dev_kfree_skb_irq(pkt_info.return_info);
  910. if (mp->tx_ring_skbs)
  911. mp->tx_ring_skbs--;
  912. } else
  913. dma_unmap_page(NULL, pkt_info.buf_ptr,
  914. pkt_info.byte_cnt, DMA_TO_DEVICE);
  915. }
  916. if (netif_queue_stopped(dev) &&
  917. mp->tx_ring_size > mp->tx_ring_skbs + MAX_DESCS_PER_SKB)
  918. netif_wake_queue(dev);
  919. }
  920. /*
  921. * mv643xx_poll
  922. *
  923. * This function is used in case of NAPI
  924. */
  925. static int mv643xx_poll(struct net_device *dev, int *budget)
  926. {
  927. struct mv643xx_private *mp = netdev_priv(dev);
  928. int done = 1, orig_budget, work_done;
  929. unsigned int port_num = mp->port_num;
  930. unsigned long flags;
  931. #ifdef MV643XX_TX_FAST_REFILL
  932. if (++mp->tx_clean_threshold > 5) {
  933. spin_lock_irqsave(&mp->lock, flags);
  934. mv643xx_tx(dev);
  935. mp->tx_clean_threshold = 0;
  936. spin_unlock_irqrestore(&mp->lock, flags);
  937. }
  938. #endif
  939. if ((mv_read(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num)))
  940. != (u32) mp->rx_used_desc_q) {
  941. orig_budget = *budget;
  942. if (orig_budget > dev->quota)
  943. orig_budget = dev->quota;
  944. work_done = mv643xx_eth_receive_queue(dev, orig_budget);
  945. mp->rx_task.func(dev);
  946. *budget -= work_done;
  947. dev->quota -= work_done;
  948. if (work_done >= orig_budget)
  949. done = 0;
  950. }
  951. if (done) {
  952. spin_lock_irqsave(&mp->lock, flags);
  953. __netif_rx_complete(dev);
  954. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
  955. mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
  956. mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
  957. INT_CAUSE_UNMASK_ALL);
  958. mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num),
  959. INT_CAUSE_UNMASK_ALL_EXT);
  960. spin_unlock_irqrestore(&mp->lock, flags);
  961. }
  962. return done ? 0 : 1;
  963. }
  964. #endif
  965. /*
  966. * mv643xx_eth_start_xmit
  967. *
  968. * This function is queues a packet in the Tx descriptor for
  969. * required port.
  970. *
  971. * Input : skb - a pointer to socket buffer
  972. * dev - a pointer to the required port
  973. *
  974. * Output : zero upon success
  975. */
  976. static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  977. {
  978. struct mv643xx_private *mp = netdev_priv(dev);
  979. struct net_device_stats *stats = &mp->stats;
  980. ETH_FUNC_RET_STATUS status;
  981. unsigned long flags;
  982. struct pkt_info pkt_info;
  983. if (netif_queue_stopped(dev)) {
  984. printk(KERN_ERR
  985. "%s: Tried sending packet when interface is stopped\n",
  986. dev->name);
  987. return 1;
  988. }
  989. /* This is a hard error, log it. */
  990. if ((mp->tx_ring_size - mp->tx_ring_skbs) <=
  991. (skb_shinfo(skb)->nr_frags + 1)) {
  992. netif_stop_queue(dev);
  993. printk(KERN_ERR
  994. "%s: Bug in mv643xx_eth - Trying to transmit when"
  995. " queue full !\n", dev->name);
  996. return 1;
  997. }
  998. /* Paranoid check - this shouldn't happen */
  999. if (skb == NULL) {
  1000. stats->tx_dropped++;
  1001. printk(KERN_ERR "mv64320_eth paranoid check failed\n");
  1002. return 1;
  1003. }
  1004. spin_lock_irqsave(&mp->lock, flags);
  1005. /* Update packet info data structure -- DMA owned, first last */
  1006. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  1007. if (!skb_shinfo(skb)->nr_frags) {
  1008. linear:
  1009. if (skb->ip_summed != CHECKSUM_HW) {
  1010. pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT |
  1011. ETH_TX_FIRST_DESC | ETH_TX_LAST_DESC;
  1012. pkt_info.l4i_chk = 0;
  1013. } else {
  1014. u32 ipheader = skb->nh.iph->ihl << 11;
  1015. pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT |
  1016. ETH_TX_FIRST_DESC | ETH_TX_LAST_DESC |
  1017. ETH_GEN_TCP_UDP_CHECKSUM |
  1018. ETH_GEN_IP_V_4_CHECKSUM | ipheader;
  1019. /* CPU already calculated pseudo header checksum. */
  1020. if (skb->nh.iph->protocol == IPPROTO_UDP) {
  1021. pkt_info.cmd_sts |= ETH_UDP_FRAME;
  1022. pkt_info.l4i_chk = skb->h.uh->check;
  1023. } else if (skb->nh.iph->protocol == IPPROTO_TCP)
  1024. pkt_info.l4i_chk = skb->h.th->check;
  1025. else {
  1026. printk(KERN_ERR
  1027. "%s: chksum proto != TCP or UDP\n",
  1028. dev->name);
  1029. spin_unlock_irqrestore(&mp->lock, flags);
  1030. return 1;
  1031. }
  1032. }
  1033. pkt_info.byte_cnt = skb->len;
  1034. pkt_info.buf_ptr = dma_map_single(NULL, skb->data, skb->len,
  1035. DMA_TO_DEVICE);
  1036. pkt_info.return_info = skb;
  1037. mp->tx_ring_skbs++;
  1038. status = eth_port_send(mp, &pkt_info);
  1039. if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL))
  1040. printk(KERN_ERR "%s: Error on transmitting packet\n",
  1041. dev->name);
  1042. stats->tx_bytes += pkt_info.byte_cnt;
  1043. } else {
  1044. unsigned int frag;
  1045. u32 ipheader;
  1046. /* Since hardware can't handle unaligned fragments smaller
  1047. * than 9 bytes, if we find any, we linearize the skb
  1048. * and start again. When I've seen it, it's always been
  1049. * the first frag (probably near the end of the page),
  1050. * but we check all frags to be safe.
  1051. */
  1052. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  1053. skb_frag_t *fragp;
  1054. fragp = &skb_shinfo(skb)->frags[frag];
  1055. if (fragp->size <= 8 && fragp->page_offset & 0x7) {
  1056. skb_linearize(skb, GFP_ATOMIC);
  1057. printk(KERN_DEBUG "%s: unaligned tiny fragment"
  1058. "%d of %d, fixed\n",
  1059. dev->name, frag,
  1060. skb_shinfo(skb)->nr_frags);
  1061. goto linear;
  1062. }
  1063. }
  1064. /* first frag which is skb header */
  1065. pkt_info.byte_cnt = skb_headlen(skb);
  1066. pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
  1067. skb_headlen(skb),
  1068. DMA_TO_DEVICE);
  1069. pkt_info.l4i_chk = 0;
  1070. pkt_info.return_info = 0;
  1071. pkt_info.cmd_sts = ETH_TX_FIRST_DESC;
  1072. if (skb->ip_summed == CHECKSUM_HW) {
  1073. ipheader = skb->nh.iph->ihl << 11;
  1074. pkt_info.cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
  1075. ETH_GEN_IP_V_4_CHECKSUM | ipheader;
  1076. /* CPU already calculated pseudo header checksum. */
  1077. if (skb->nh.iph->protocol == IPPROTO_UDP) {
  1078. pkt_info.cmd_sts |= ETH_UDP_FRAME;
  1079. pkt_info.l4i_chk = skb->h.uh->check;
  1080. } else if (skb->nh.iph->protocol == IPPROTO_TCP)
  1081. pkt_info.l4i_chk = skb->h.th->check;
  1082. else {
  1083. printk(KERN_ERR
  1084. "%s: chksum proto != TCP or UDP\n",
  1085. dev->name);
  1086. spin_unlock_irqrestore(&mp->lock, flags);
  1087. return 1;
  1088. }
  1089. }
  1090. status = eth_port_send(mp, &pkt_info);
  1091. if (status != ETH_OK) {
  1092. if ((status == ETH_ERROR))
  1093. printk(KERN_ERR
  1094. "%s: Error on transmitting packet\n",
  1095. dev->name);
  1096. if (status == ETH_QUEUE_FULL)
  1097. printk("Error on Queue Full \n");
  1098. if (status == ETH_QUEUE_LAST_RESOURCE)
  1099. printk("Tx resource error \n");
  1100. }
  1101. stats->tx_bytes += pkt_info.byte_cnt;
  1102. /* Check for the remaining frags */
  1103. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  1104. skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
  1105. pkt_info.l4i_chk = 0x0000;
  1106. pkt_info.cmd_sts = 0x00000000;
  1107. /* Last Frag enables interrupt and frees the skb */
  1108. if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
  1109. pkt_info.cmd_sts |= ETH_TX_ENABLE_INTERRUPT |
  1110. ETH_TX_LAST_DESC;
  1111. pkt_info.return_info = skb;
  1112. mp->tx_ring_skbs++;
  1113. } else {
  1114. pkt_info.return_info = 0;
  1115. }
  1116. pkt_info.l4i_chk = 0;
  1117. pkt_info.byte_cnt = this_frag->size;
  1118. pkt_info.buf_ptr = dma_map_page(NULL, this_frag->page,
  1119. this_frag->page_offset,
  1120. this_frag->size,
  1121. DMA_TO_DEVICE);
  1122. status = eth_port_send(mp, &pkt_info);
  1123. if (status != ETH_OK) {
  1124. if ((status == ETH_ERROR))
  1125. printk(KERN_ERR "%s: Error on "
  1126. "transmitting packet\n",
  1127. dev->name);
  1128. if (status == ETH_QUEUE_LAST_RESOURCE)
  1129. printk("Tx resource error \n");
  1130. if (status == ETH_QUEUE_FULL)
  1131. printk("Queue is full \n");
  1132. }
  1133. stats->tx_bytes += pkt_info.byte_cnt;
  1134. }
  1135. }
  1136. #else
  1137. pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT | ETH_TX_FIRST_DESC |
  1138. ETH_TX_LAST_DESC;
  1139. pkt_info.l4i_chk = 0;
  1140. pkt_info.byte_cnt = skb->len;
  1141. pkt_info.buf_ptr = dma_map_single(NULL, skb->data, skb->len,
  1142. DMA_TO_DEVICE);
  1143. pkt_info.return_info = skb;
  1144. mp->tx_ring_skbs++;
  1145. status = eth_port_send(mp, &pkt_info);
  1146. if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL))
  1147. printk(KERN_ERR "%s: Error on transmitting packet\n",
  1148. dev->name);
  1149. stats->tx_bytes += pkt_info.byte_cnt;
  1150. #endif
  1151. /* Check if TX queue can handle another skb. If not, then
  1152. * signal higher layers to stop requesting TX
  1153. */
  1154. if (mp->tx_ring_size <= (mp->tx_ring_skbs + MAX_DESCS_PER_SKB))
  1155. /*
  1156. * Stop getting skb's from upper layers.
  1157. * Getting skb's from upper layers will be enabled again after
  1158. * packets are released.
  1159. */
  1160. netif_stop_queue(dev);
  1161. /* Update statistics and start of transmittion time */
  1162. stats->tx_packets++;
  1163. dev->trans_start = jiffies;
  1164. spin_unlock_irqrestore(&mp->lock, flags);
  1165. return 0; /* success */
  1166. }
  1167. /*
  1168. * mv643xx_eth_get_stats
  1169. *
  1170. * Returns a pointer to the interface statistics.
  1171. *
  1172. * Input : dev - a pointer to the required interface
  1173. *
  1174. * Output : a pointer to the interface's statistics
  1175. */
  1176. static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
  1177. {
  1178. struct mv643xx_private *mp = netdev_priv(dev);
  1179. return &mp->stats;
  1180. }
  1181. /*/
  1182. * mv643xx_eth_probe
  1183. *
  1184. * First function called after registering the network device.
  1185. * It's purpose is to initialize the device as an ethernet device,
  1186. * fill the ethernet device structure with pointers * to functions,
  1187. * and set the MAC address of the interface
  1188. *
  1189. * Input : struct device *
  1190. * Output : -ENOMEM if failed , 0 if success
  1191. */
  1192. static int mv643xx_eth_probe(struct device *ddev)
  1193. {
  1194. struct platform_device *pdev = to_platform_device(ddev);
  1195. struct mv643xx_eth_platform_data *pd;
  1196. int port_num = pdev->id;
  1197. struct mv643xx_private *mp;
  1198. struct net_device *dev;
  1199. u8 *p;
  1200. struct resource *res;
  1201. int err;
  1202. dev = alloc_etherdev(sizeof(struct mv643xx_private));
  1203. if (!dev)
  1204. return -ENOMEM;
  1205. dev_set_drvdata(ddev, dev);
  1206. mp = netdev_priv(dev);
  1207. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1208. BUG_ON(!res);
  1209. dev->irq = res->start;
  1210. mp->port_num = port_num;
  1211. dev->open = mv643xx_eth_open;
  1212. dev->stop = mv643xx_eth_stop;
  1213. dev->hard_start_xmit = mv643xx_eth_start_xmit;
  1214. dev->get_stats = mv643xx_eth_get_stats;
  1215. dev->set_mac_address = mv643xx_eth_set_mac_address;
  1216. dev->set_multicast_list = mv643xx_eth_set_rx_mode;
  1217. /* No need to Tx Timeout */
  1218. dev->tx_timeout = mv643xx_eth_tx_timeout;
  1219. #ifdef MV643XX_NAPI
  1220. dev->poll = mv643xx_poll;
  1221. dev->weight = 64;
  1222. #endif
  1223. dev->watchdog_timeo = 2 * HZ;
  1224. dev->tx_queue_len = mp->tx_ring_size;
  1225. dev->base_addr = 0;
  1226. dev->change_mtu = mv643xx_eth_change_mtu;
  1227. SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
  1228. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  1229. #ifdef MAX_SKB_FRAGS
  1230. /*
  1231. * Zero copy can only work if we use Discovery II memory. Else, we will
  1232. * have to map the buffers to ISA memory which is only 16 MB
  1233. */
  1234. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HW_CSUM;
  1235. #endif
  1236. #endif
  1237. /* Configure the timeout task */
  1238. INIT_WORK(&mp->tx_timeout_task,
  1239. (void (*)(void *))mv643xx_eth_tx_timeout_task, dev);
  1240. spin_lock_init(&mp->lock);
  1241. /* set default config values */
  1242. eth_port_uc_addr_get(dev, dev->dev_addr);
  1243. mp->port_config = MV643XX_ETH_PORT_CONFIG_DEFAULT_VALUE;
  1244. mp->port_config_extend = MV643XX_ETH_PORT_CONFIG_EXTEND_DEFAULT_VALUE;
  1245. mp->port_sdma_config = MV643XX_ETH_PORT_SDMA_CONFIG_DEFAULT_VALUE;
  1246. mp->port_serial_control = MV643XX_ETH_PORT_SERIAL_CONTROL_DEFAULT_VALUE;
  1247. mp->rx_ring_size = MV643XX_ETH_PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
  1248. mp->tx_ring_size = MV643XX_ETH_PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
  1249. pd = pdev->dev.platform_data;
  1250. if (pd) {
  1251. if (pd->mac_addr != NULL)
  1252. memcpy(dev->dev_addr, pd->mac_addr, 6);
  1253. if (pd->phy_addr || pd->force_phy_addr)
  1254. ethernet_phy_set(port_num, pd->phy_addr);
  1255. if (pd->port_config || pd->force_port_config)
  1256. mp->port_config = pd->port_config;
  1257. if (pd->port_config_extend || pd->force_port_config_extend)
  1258. mp->port_config_extend = pd->port_config_extend;
  1259. if (pd->port_sdma_config || pd->force_port_sdma_config)
  1260. mp->port_sdma_config = pd->port_sdma_config;
  1261. if (pd->port_serial_control || pd->force_port_serial_control)
  1262. mp->port_serial_control = pd->port_serial_control;
  1263. if (pd->rx_queue_size)
  1264. mp->rx_ring_size = pd->rx_queue_size;
  1265. if (pd->tx_queue_size)
  1266. mp->tx_ring_size = pd->tx_queue_size;
  1267. if (pd->tx_sram_size) {
  1268. mp->tx_sram_size = pd->tx_sram_size;
  1269. mp->tx_sram_addr = pd->tx_sram_addr;
  1270. }
  1271. if (pd->rx_sram_size) {
  1272. mp->rx_sram_size = pd->rx_sram_size;
  1273. mp->rx_sram_addr = pd->rx_sram_addr;
  1274. }
  1275. }
  1276. err = ethernet_phy_detect(port_num);
  1277. if (err) {
  1278. pr_debug("MV643xx ethernet port %d: "
  1279. "No PHY detected at addr %d\n",
  1280. port_num, ethernet_phy_get(port_num));
  1281. return err;
  1282. }
  1283. err = register_netdev(dev);
  1284. if (err)
  1285. goto out;
  1286. p = dev->dev_addr;
  1287. printk(KERN_NOTICE
  1288. "%s: port %d with MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
  1289. dev->name, port_num, p[0], p[1], p[2], p[3], p[4], p[5]);
  1290. if (dev->features & NETIF_F_SG)
  1291. printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
  1292. if (dev->features & NETIF_F_IP_CSUM)
  1293. printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
  1294. dev->name);
  1295. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  1296. printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
  1297. #endif
  1298. #ifdef MV643XX_COAL
  1299. printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
  1300. dev->name);
  1301. #endif
  1302. #ifdef MV643XX_NAPI
  1303. printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
  1304. #endif
  1305. return 0;
  1306. out:
  1307. free_netdev(dev);
  1308. return err;
  1309. }
  1310. static int mv643xx_eth_remove(struct device *ddev)
  1311. {
  1312. struct net_device *dev = dev_get_drvdata(ddev);
  1313. unregister_netdev(dev);
  1314. flush_scheduled_work();
  1315. free_netdev(dev);
  1316. dev_set_drvdata(ddev, NULL);
  1317. return 0;
  1318. }
  1319. static int mv643xx_eth_shared_probe(struct device *ddev)
  1320. {
  1321. struct platform_device *pdev = to_platform_device(ddev);
  1322. struct resource *res;
  1323. printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
  1324. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1325. if (res == NULL)
  1326. return -ENODEV;
  1327. mv643xx_eth_shared_base = ioremap(res->start,
  1328. MV643XX_ETH_SHARED_REGS_SIZE);
  1329. if (mv643xx_eth_shared_base == NULL)
  1330. return -ENOMEM;
  1331. return 0;
  1332. }
  1333. static int mv643xx_eth_shared_remove(struct device *ddev)
  1334. {
  1335. iounmap(mv643xx_eth_shared_base);
  1336. mv643xx_eth_shared_base = NULL;
  1337. return 0;
  1338. }
  1339. static struct device_driver mv643xx_eth_driver = {
  1340. .name = MV643XX_ETH_NAME,
  1341. .bus = &platform_bus_type,
  1342. .probe = mv643xx_eth_probe,
  1343. .remove = mv643xx_eth_remove,
  1344. };
  1345. static struct device_driver mv643xx_eth_shared_driver = {
  1346. .name = MV643XX_ETH_SHARED_NAME,
  1347. .bus = &platform_bus_type,
  1348. .probe = mv643xx_eth_shared_probe,
  1349. .remove = mv643xx_eth_shared_remove,
  1350. };
  1351. /*
  1352. * mv643xx_init_module
  1353. *
  1354. * Registers the network drivers into the Linux kernel
  1355. *
  1356. * Input : N/A
  1357. *
  1358. * Output : N/A
  1359. */
  1360. static int __init mv643xx_init_module(void)
  1361. {
  1362. int rc;
  1363. rc = driver_register(&mv643xx_eth_shared_driver);
  1364. if (!rc) {
  1365. rc = driver_register(&mv643xx_eth_driver);
  1366. if (rc)
  1367. driver_unregister(&mv643xx_eth_shared_driver);
  1368. }
  1369. return rc;
  1370. }
  1371. /*
  1372. * mv643xx_cleanup_module
  1373. *
  1374. * Registers the network drivers into the Linux kernel
  1375. *
  1376. * Input : N/A
  1377. *
  1378. * Output : N/A
  1379. */
  1380. static void __exit mv643xx_cleanup_module(void)
  1381. {
  1382. driver_unregister(&mv643xx_eth_driver);
  1383. driver_unregister(&mv643xx_eth_shared_driver);
  1384. }
  1385. module_init(mv643xx_init_module);
  1386. module_exit(mv643xx_cleanup_module);
  1387. MODULE_LICENSE("GPL");
  1388. MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
  1389. " and Dale Farnsworth");
  1390. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  1391. /*
  1392. * The second part is the low level driver of the gigE ethernet ports.
  1393. */
  1394. /*
  1395. * Marvell's Gigabit Ethernet controller low level driver
  1396. *
  1397. * DESCRIPTION:
  1398. * This file introduce low level API to Marvell's Gigabit Ethernet
  1399. * controller. This Gigabit Ethernet Controller driver API controls
  1400. * 1) Operations (i.e. port init, start, reset etc').
  1401. * 2) Data flow (i.e. port send, receive etc').
  1402. * Each Gigabit Ethernet port is controlled via
  1403. * struct mv643xx_private.
  1404. * This struct includes user configuration information as well as
  1405. * driver internal data needed for its operations.
  1406. *
  1407. * Supported Features:
  1408. * - This low level driver is OS independent. Allocating memory for
  1409. * the descriptor rings and buffers are not within the scope of
  1410. * this driver.
  1411. * - The user is free from Rx/Tx queue managing.
  1412. * - This low level driver introduce functionality API that enable
  1413. * the to operate Marvell's Gigabit Ethernet Controller in a
  1414. * convenient way.
  1415. * - Simple Gigabit Ethernet port operation API.
  1416. * - Simple Gigabit Ethernet port data flow API.
  1417. * - Data flow and operation API support per queue functionality.
  1418. * - Support cached descriptors for better performance.
  1419. * - Enable access to all four DRAM banks and internal SRAM memory
  1420. * spaces.
  1421. * - PHY access and control API.
  1422. * - Port control register configuration API.
  1423. * - Full control over Unicast and Multicast MAC configurations.
  1424. *
  1425. * Operation flow:
  1426. *
  1427. * Initialization phase
  1428. * This phase complete the initialization of the the
  1429. * mv643xx_private struct.
  1430. * User information regarding port configuration has to be set
  1431. * prior to calling the port initialization routine.
  1432. *
  1433. * In this phase any port Tx/Rx activity is halted, MIB counters
  1434. * are cleared, PHY address is set according to user parameter and
  1435. * access to DRAM and internal SRAM memory spaces.
  1436. *
  1437. * Driver ring initialization
  1438. * Allocating memory for the descriptor rings and buffers is not
  1439. * within the scope of this driver. Thus, the user is required to
  1440. * allocate memory for the descriptors ring and buffers. Those
  1441. * memory parameters are used by the Rx and Tx ring initialization
  1442. * routines in order to curve the descriptor linked list in a form
  1443. * of a ring.
  1444. * Note: Pay special attention to alignment issues when using
  1445. * cached descriptors/buffers. In this phase the driver store
  1446. * information in the mv643xx_private struct regarding each queue
  1447. * ring.
  1448. *
  1449. * Driver start
  1450. * This phase prepares the Ethernet port for Rx and Tx activity.
  1451. * It uses the information stored in the mv643xx_private struct to
  1452. * initialize the various port registers.
  1453. *
  1454. * Data flow:
  1455. * All packet references to/from the driver are done using
  1456. * struct pkt_info.
  1457. * This struct is a unified struct used with Rx and Tx operations.
  1458. * This way the user is not required to be familiar with neither
  1459. * Tx nor Rx descriptors structures.
  1460. * The driver's descriptors rings are management by indexes.
  1461. * Those indexes controls the ring resources and used to indicate
  1462. * a SW resource error:
  1463. * 'current'
  1464. * This index points to the current available resource for use. For
  1465. * example in Rx process this index will point to the descriptor
  1466. * that will be passed to the user upon calling the receive
  1467. * routine. In Tx process, this index will point to the descriptor
  1468. * that will be assigned with the user packet info and transmitted.
  1469. * 'used'
  1470. * This index points to the descriptor that need to restore its
  1471. * resources. For example in Rx process, using the Rx buffer return
  1472. * API will attach the buffer returned in packet info to the
  1473. * descriptor pointed by 'used'. In Tx process, using the Tx
  1474. * descriptor return will merely return the user packet info with
  1475. * the command status of the transmitted buffer pointed by the
  1476. * 'used' index. Nevertheless, it is essential to use this routine
  1477. * to update the 'used' index.
  1478. * 'first'
  1479. * This index supports Tx Scatter-Gather. It points to the first
  1480. * descriptor of a packet assembled of multiple buffers. For
  1481. * example when in middle of Such packet we have a Tx resource
  1482. * error the 'curr' index get the value of 'first' to indicate
  1483. * that the ring returned to its state before trying to transmit
  1484. * this packet.
  1485. *
  1486. * Receive operation:
  1487. * The eth_port_receive API set the packet information struct,
  1488. * passed by the caller, with received information from the
  1489. * 'current' SDMA descriptor.
  1490. * It is the user responsibility to return this resource back
  1491. * to the Rx descriptor ring to enable the reuse of this source.
  1492. * Return Rx resource is done using the eth_rx_return_buff API.
  1493. *
  1494. * Transmit operation:
  1495. * The eth_port_send API supports Scatter-Gather which enables to
  1496. * send a packet spanned over multiple buffers. This means that
  1497. * for each packet info structure given by the user and put into
  1498. * the Tx descriptors ring, will be transmitted only if the 'LAST'
  1499. * bit will be set in the packet info command status field. This
  1500. * API also consider restriction regarding buffer alignments and
  1501. * sizes.
  1502. * The user must return a Tx resource after ensuring the buffer
  1503. * has been transmitted to enable the Tx ring indexes to update.
  1504. *
  1505. * BOARD LAYOUT
  1506. * This device is on-board. No jumper diagram is necessary.
  1507. *
  1508. * EXTERNAL INTERFACE
  1509. *
  1510. * Prior to calling the initialization routine eth_port_init() the user
  1511. * must set the following fields under mv643xx_private struct:
  1512. * port_num User Ethernet port number.
  1513. * port_mac_addr[6] User defined port MAC address.
  1514. * port_config User port configuration value.
  1515. * port_config_extend User port config extend value.
  1516. * port_sdma_config User port SDMA config value.
  1517. * port_serial_control User port serial control value.
  1518. *
  1519. * This driver data flow is done using the struct pkt_info which
  1520. * is a unified struct for Rx and Tx operations:
  1521. *
  1522. * byte_cnt Tx/Rx descriptor buffer byte count.
  1523. * l4i_chk CPU provided TCP Checksum. For Tx operation
  1524. * only.
  1525. * cmd_sts Tx/Rx descriptor command status.
  1526. * buf_ptr Tx/Rx descriptor buffer pointer.
  1527. * return_info Tx/Rx user resource return information.
  1528. */
  1529. /* defines */
  1530. /* SDMA command macros */
  1531. #define ETH_ENABLE_TX_QUEUE(eth_port) \
  1532. mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port), 1)
  1533. /* locals */
  1534. /* PHY routines */
  1535. static int ethernet_phy_get(unsigned int eth_port_num);
  1536. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
  1537. /* Ethernet Port routines */
  1538. static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble,
  1539. int option);
  1540. /*
  1541. * eth_port_init - Initialize the Ethernet port driver
  1542. *
  1543. * DESCRIPTION:
  1544. * This function prepares the ethernet port to start its activity:
  1545. * 1) Completes the ethernet port driver struct initialization toward port
  1546. * start routine.
  1547. * 2) Resets the device to a quiescent state in case of warm reboot.
  1548. * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
  1549. * 4) Clean MAC tables. The reset status of those tables is unknown.
  1550. * 5) Set PHY address.
  1551. * Note: Call this routine prior to eth_port_start routine and after
  1552. * setting user values in the user fields of Ethernet port control
  1553. * struct.
  1554. *
  1555. * INPUT:
  1556. * struct mv643xx_private *mp Ethernet port control struct
  1557. *
  1558. * OUTPUT:
  1559. * See description.
  1560. *
  1561. * RETURN:
  1562. * None.
  1563. */
  1564. static void eth_port_init(struct mv643xx_private *mp)
  1565. {
  1566. mp->port_rx_queue_command = 0;
  1567. mp->port_tx_queue_command = 0;
  1568. mp->rx_resource_err = 0;
  1569. mp->tx_resource_err = 0;
  1570. eth_port_reset(mp->port_num);
  1571. eth_port_init_mac_tables(mp->port_num);
  1572. ethernet_phy_reset(mp->port_num);
  1573. }
  1574. /*
  1575. * eth_port_start - Start the Ethernet port activity.
  1576. *
  1577. * DESCRIPTION:
  1578. * This routine prepares the Ethernet port for Rx and Tx activity:
  1579. * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
  1580. * has been initialized a descriptor's ring (using
  1581. * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
  1582. * 2. Initialize and enable the Ethernet configuration port by writing to
  1583. * the port's configuration and command registers.
  1584. * 3. Initialize and enable the SDMA by writing to the SDMA's
  1585. * configuration and command registers. After completing these steps,
  1586. * the ethernet port SDMA can starts to perform Rx and Tx activities.
  1587. *
  1588. * Note: Each Rx and Tx queue descriptor's list must be initialized prior
  1589. * to calling this function (use ether_init_tx_desc_ring for Tx queues
  1590. * and ether_init_rx_desc_ring for Rx queues).
  1591. *
  1592. * INPUT:
  1593. * struct mv643xx_private *mp Ethernet port control struct
  1594. *
  1595. * OUTPUT:
  1596. * Ethernet port is ready to receive and transmit.
  1597. *
  1598. * RETURN:
  1599. * None.
  1600. */
  1601. static void eth_port_start(struct mv643xx_private *mp)
  1602. {
  1603. unsigned int port_num = mp->port_num;
  1604. int tx_curr_desc, rx_curr_desc;
  1605. /* Assignment of Tx CTRP of given queue */
  1606. tx_curr_desc = mp->tx_curr_desc_q;
  1607. mv_write(MV643XX_ETH_TX_CURRENT_QUEUE_DESC_PTR_0(port_num),
  1608. (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
  1609. /* Assignment of Rx CRDP of given queue */
  1610. rx_curr_desc = mp->rx_curr_desc_q;
  1611. mv_write(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num),
  1612. (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
  1613. /* Add the assigned Ethernet address to the port's address table */
  1614. eth_port_uc_addr_set(port_num, mp->port_mac_addr);
  1615. /* Assign port configuration and command. */
  1616. mv_write(MV643XX_ETH_PORT_CONFIG_REG(port_num), mp->port_config);
  1617. mv_write(MV643XX_ETH_PORT_CONFIG_EXTEND_REG(port_num),
  1618. mp->port_config_extend);
  1619. /* Increase the Rx side buffer size if supporting GigE */
  1620. if (mp->port_serial_control & MV643XX_ETH_SET_GMII_SPEED_TO_1000)
  1621. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
  1622. (mp->port_serial_control & 0xfff1ffff) | (0x5 << 17));
  1623. else
  1624. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
  1625. mp->port_serial_control);
  1626. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
  1627. mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num)) |
  1628. MV643XX_ETH_SERIAL_PORT_ENABLE);
  1629. /* Assign port SDMA configuration */
  1630. mv_write(MV643XX_ETH_SDMA_CONFIG_REG(port_num),
  1631. mp->port_sdma_config);
  1632. /* Enable port Rx. */
  1633. mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num),
  1634. mp->port_rx_queue_command);
  1635. }
  1636. /*
  1637. * eth_port_uc_addr_set - This function Set the port Unicast address.
  1638. *
  1639. * DESCRIPTION:
  1640. * This function Set the port Ethernet MAC address.
  1641. *
  1642. * INPUT:
  1643. * unsigned int eth_port_num Port number.
  1644. * char * p_addr Address to be set
  1645. *
  1646. * OUTPUT:
  1647. * Set MAC address low and high registers. also calls eth_port_uc_addr()
  1648. * To set the unicast table with the proper information.
  1649. *
  1650. * RETURN:
  1651. * N/A.
  1652. *
  1653. */
  1654. static void eth_port_uc_addr_set(unsigned int eth_port_num,
  1655. unsigned char *p_addr)
  1656. {
  1657. unsigned int mac_h;
  1658. unsigned int mac_l;
  1659. mac_l = (p_addr[4] << 8) | (p_addr[5]);
  1660. mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
  1661. (p_addr[3] << 0);
  1662. mv_write(MV643XX_ETH_MAC_ADDR_LOW(eth_port_num), mac_l);
  1663. mv_write(MV643XX_ETH_MAC_ADDR_HIGH(eth_port_num), mac_h);
  1664. /* Accept frames of this address */
  1665. eth_port_uc_addr(eth_port_num, p_addr[5], ACCEPT_MAC_ADDR);
  1666. return;
  1667. }
  1668. /*
  1669. * eth_port_uc_addr_get - This function retrieves the port Unicast address
  1670. * (MAC address) from the ethernet hw registers.
  1671. *
  1672. * DESCRIPTION:
  1673. * This function retrieves the port Ethernet MAC address.
  1674. *
  1675. * INPUT:
  1676. * unsigned int eth_port_num Port number.
  1677. * char *MacAddr pointer where the MAC address is stored
  1678. *
  1679. * OUTPUT:
  1680. * Copy the MAC address to the location pointed to by MacAddr
  1681. *
  1682. * RETURN:
  1683. * N/A.
  1684. *
  1685. */
  1686. static void eth_port_uc_addr_get(struct net_device *dev, unsigned char *p_addr)
  1687. {
  1688. struct mv643xx_private *mp = netdev_priv(dev);
  1689. unsigned int mac_h;
  1690. unsigned int mac_l;
  1691. mac_h = mv_read(MV643XX_ETH_MAC_ADDR_HIGH(mp->port_num));
  1692. mac_l = mv_read(MV643XX_ETH_MAC_ADDR_LOW(mp->port_num));
  1693. p_addr[0] = (mac_h >> 24) & 0xff;
  1694. p_addr[1] = (mac_h >> 16) & 0xff;
  1695. p_addr[2] = (mac_h >> 8) & 0xff;
  1696. p_addr[3] = mac_h & 0xff;
  1697. p_addr[4] = (mac_l >> 8) & 0xff;
  1698. p_addr[5] = mac_l & 0xff;
  1699. }
  1700. /*
  1701. * eth_port_uc_addr - This function Set the port unicast address table
  1702. *
  1703. * DESCRIPTION:
  1704. * This function locates the proper entry in the Unicast table for the
  1705. * specified MAC nibble and sets its properties according to function
  1706. * parameters.
  1707. *
  1708. * INPUT:
  1709. * unsigned int eth_port_num Port number.
  1710. * unsigned char uc_nibble Unicast MAC Address last nibble.
  1711. * int option 0 = Add, 1 = remove address.
  1712. *
  1713. * OUTPUT:
  1714. * This function add/removes MAC addresses from the port unicast address
  1715. * table.
  1716. *
  1717. * RETURN:
  1718. * true is output succeeded.
  1719. * false if option parameter is invalid.
  1720. *
  1721. */
  1722. static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble,
  1723. int option)
  1724. {
  1725. unsigned int unicast_reg;
  1726. unsigned int tbl_offset;
  1727. unsigned int reg_offset;
  1728. /* Locate the Unicast table entry */
  1729. uc_nibble = (0xf & uc_nibble);
  1730. tbl_offset = (uc_nibble / 4) * 4; /* Register offset from unicast table base */
  1731. reg_offset = uc_nibble % 4; /* Entry offset within the above register */
  1732. switch (option) {
  1733. case REJECT_MAC_ADDR:
  1734. /* Clear accepts frame bit at given unicast DA table entry */
  1735. unicast_reg = mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1736. (eth_port_num) + tbl_offset));
  1737. unicast_reg &= (0x0E << (8 * reg_offset));
  1738. mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1739. (eth_port_num) + tbl_offset), unicast_reg);
  1740. break;
  1741. case ACCEPT_MAC_ADDR:
  1742. /* Set accepts frame bit at unicast DA filter table entry */
  1743. unicast_reg =
  1744. mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1745. (eth_port_num) + tbl_offset));
  1746. unicast_reg |= (0x01 << (8 * reg_offset));
  1747. mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1748. (eth_port_num) + tbl_offset), unicast_reg);
  1749. break;
  1750. default:
  1751. return 0;
  1752. }
  1753. return 1;
  1754. }
  1755. /*
  1756. * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
  1757. *
  1758. * DESCRIPTION:
  1759. * Go through all the DA filter tables (Unicast, Special Multicast &
  1760. * Other Multicast) and set each entry to 0.
  1761. *
  1762. * INPUT:
  1763. * unsigned int eth_port_num Ethernet Port number.
  1764. *
  1765. * OUTPUT:
  1766. * Multicast and Unicast packets are rejected.
  1767. *
  1768. * RETURN:
  1769. * None.
  1770. */
  1771. static void eth_port_init_mac_tables(unsigned int eth_port_num)
  1772. {
  1773. int table_index;
  1774. /* Clear DA filter unicast table (Ex_dFUT) */
  1775. for (table_index = 0; table_index <= 0xC; table_index += 4)
  1776. mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1777. (eth_port_num) + table_index), 0);
  1778. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  1779. /* Clear DA filter special multicast table (Ex_dFSMT) */
  1780. mv_write((MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
  1781. (eth_port_num) + table_index), 0);
  1782. /* Clear DA filter other multicast table (Ex_dFOMT) */
  1783. mv_write((MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE
  1784. (eth_port_num) + table_index), 0);
  1785. }
  1786. }
  1787. /*
  1788. * eth_clear_mib_counters - Clear all MIB counters
  1789. *
  1790. * DESCRIPTION:
  1791. * This function clears all MIB counters of a specific ethernet port.
  1792. * A read from the MIB counter will reset the counter.
  1793. *
  1794. * INPUT:
  1795. * unsigned int eth_port_num Ethernet Port number.
  1796. *
  1797. * OUTPUT:
  1798. * After reading all MIB counters, the counters resets.
  1799. *
  1800. * RETURN:
  1801. * MIB counter value.
  1802. *
  1803. */
  1804. static void eth_clear_mib_counters(unsigned int eth_port_num)
  1805. {
  1806. int i;
  1807. /* Perform dummy reads from MIB counters */
  1808. for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
  1809. i += 4)
  1810. mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(eth_port_num) + i);
  1811. }
  1812. static inline u32 read_mib(struct mv643xx_private *mp, int offset)
  1813. {
  1814. return mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(mp->port_num) + offset);
  1815. }
  1816. static void eth_update_mib_counters(struct mv643xx_private *mp)
  1817. {
  1818. struct mv643xx_mib_counters *p = &mp->mib_counters;
  1819. int offset;
  1820. p->good_octets_received +=
  1821. read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
  1822. p->good_octets_received +=
  1823. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
  1824. for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
  1825. offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
  1826. offset += 4)
  1827. *(u32 *)((char *)p + offset) = read_mib(mp, offset);
  1828. p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
  1829. p->good_octets_sent +=
  1830. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
  1831. for (offset = ETH_MIB_GOOD_FRAMES_SENT;
  1832. offset <= ETH_MIB_LATE_COLLISION;
  1833. offset += 4)
  1834. *(u32 *)((char *)p + offset) = read_mib(mp, offset);
  1835. }
  1836. /*
  1837. * ethernet_phy_detect - Detect whether a phy is present
  1838. *
  1839. * DESCRIPTION:
  1840. * This function tests whether there is a PHY present on
  1841. * the specified port.
  1842. *
  1843. * INPUT:
  1844. * unsigned int eth_port_num Ethernet Port number.
  1845. *
  1846. * OUTPUT:
  1847. * None
  1848. *
  1849. * RETURN:
  1850. * 0 on success
  1851. * -ENODEV on failure
  1852. *
  1853. */
  1854. static int ethernet_phy_detect(unsigned int port_num)
  1855. {
  1856. unsigned int phy_reg_data0;
  1857. int auto_neg;
  1858. eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
  1859. auto_neg = phy_reg_data0 & 0x1000;
  1860. phy_reg_data0 ^= 0x1000; /* invert auto_neg */
  1861. eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
  1862. eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
  1863. if ((phy_reg_data0 & 0x1000) == auto_neg)
  1864. return -ENODEV; /* change didn't take */
  1865. phy_reg_data0 ^= 0x1000;
  1866. eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
  1867. return 0;
  1868. }
  1869. /*
  1870. * ethernet_phy_get - Get the ethernet port PHY address.
  1871. *
  1872. * DESCRIPTION:
  1873. * This routine returns the given ethernet port PHY address.
  1874. *
  1875. * INPUT:
  1876. * unsigned int eth_port_num Ethernet Port number.
  1877. *
  1878. * OUTPUT:
  1879. * None.
  1880. *
  1881. * RETURN:
  1882. * PHY address.
  1883. *
  1884. */
  1885. static int ethernet_phy_get(unsigned int eth_port_num)
  1886. {
  1887. unsigned int reg_data;
  1888. reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
  1889. return ((reg_data >> (5 * eth_port_num)) & 0x1f);
  1890. }
  1891. /*
  1892. * ethernet_phy_set - Set the ethernet port PHY address.
  1893. *
  1894. * DESCRIPTION:
  1895. * This routine sets the given ethernet port PHY address.
  1896. *
  1897. * INPUT:
  1898. * unsigned int eth_port_num Ethernet Port number.
  1899. * int phy_addr PHY address.
  1900. *
  1901. * OUTPUT:
  1902. * None.
  1903. *
  1904. * RETURN:
  1905. * None.
  1906. *
  1907. */
  1908. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr)
  1909. {
  1910. u32 reg_data;
  1911. int addr_shift = 5 * eth_port_num;
  1912. reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
  1913. reg_data &= ~(0x1f << addr_shift);
  1914. reg_data |= (phy_addr & 0x1f) << addr_shift;
  1915. mv_write(MV643XX_ETH_PHY_ADDR_REG, reg_data);
  1916. }
  1917. /*
  1918. * ethernet_phy_reset - Reset Ethernet port PHY.
  1919. *
  1920. * DESCRIPTION:
  1921. * This routine utilizes the SMI interface to reset the ethernet port PHY.
  1922. *
  1923. * INPUT:
  1924. * unsigned int eth_port_num Ethernet Port number.
  1925. *
  1926. * OUTPUT:
  1927. * The PHY is reset.
  1928. *
  1929. * RETURN:
  1930. * None.
  1931. *
  1932. */
  1933. static void ethernet_phy_reset(unsigned int eth_port_num)
  1934. {
  1935. unsigned int phy_reg_data;
  1936. /* Reset the PHY */
  1937. eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
  1938. phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
  1939. eth_port_write_smi_reg(eth_port_num, 0, phy_reg_data);
  1940. }
  1941. /*
  1942. * eth_port_reset - Reset Ethernet port
  1943. *
  1944. * DESCRIPTION:
  1945. * This routine resets the chip by aborting any SDMA engine activity and
  1946. * clearing the MIB counters. The Receiver and the Transmit unit are in
  1947. * idle state after this command is performed and the port is disabled.
  1948. *
  1949. * INPUT:
  1950. * unsigned int eth_port_num Ethernet Port number.
  1951. *
  1952. * OUTPUT:
  1953. * Channel activity is halted.
  1954. *
  1955. * RETURN:
  1956. * None.
  1957. *
  1958. */
  1959. static void eth_port_reset(unsigned int port_num)
  1960. {
  1961. unsigned int reg_data;
  1962. /* Stop Tx port activity. Check port Tx activity. */
  1963. reg_data = mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num));
  1964. if (reg_data & 0xFF) {
  1965. /* Issue stop command for active channels only */
  1966. mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num),
  1967. (reg_data << 8));
  1968. /* Wait for all Tx activity to terminate. */
  1969. /* Check port cause register that all Tx queues are stopped */
  1970. while (mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num))
  1971. & 0xFF)
  1972. udelay(10);
  1973. }
  1974. /* Stop Rx port activity. Check port Rx activity. */
  1975. reg_data = mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num));
  1976. if (reg_data & 0xFF) {
  1977. /* Issue stop command for active channels only */
  1978. mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num),
  1979. (reg_data << 8));
  1980. /* Wait for all Rx activity to terminate. */
  1981. /* Check port cause register that all Rx queues are stopped */
  1982. while (mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num))
  1983. & 0xFF)
  1984. udelay(10);
  1985. }
  1986. /* Clear all MIB counters */
  1987. eth_clear_mib_counters(port_num);
  1988. /* Reset the Enable bit in the Configuration Register */
  1989. reg_data = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
  1990. reg_data &= ~MV643XX_ETH_SERIAL_PORT_ENABLE;
  1991. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), reg_data);
  1992. }
  1993. /*
  1994. * ethernet_set_config_reg - Set specified bits in configuration register.
  1995. *
  1996. * DESCRIPTION:
  1997. * This function sets specified bits in the given ethernet
  1998. * configuration register.
  1999. *
  2000. * INPUT:
  2001. * unsigned int eth_port_num Ethernet Port number.
  2002. * unsigned int value 32 bit value.
  2003. *
  2004. * OUTPUT:
  2005. * The set bits in the value parameter are set in the configuration
  2006. * register.
  2007. *
  2008. * RETURN:
  2009. * None.
  2010. *
  2011. */
  2012. static void ethernet_set_config_reg(unsigned int eth_port_num,
  2013. unsigned int value)
  2014. {
  2015. unsigned int eth_config_reg;
  2016. eth_config_reg = mv_read(MV643XX_ETH_PORT_CONFIG_REG(eth_port_num));
  2017. eth_config_reg |= value;
  2018. mv_write(MV643XX_ETH_PORT_CONFIG_REG(eth_port_num), eth_config_reg);
  2019. }
  2020. static int eth_port_autoneg_supported(unsigned int eth_port_num)
  2021. {
  2022. unsigned int phy_reg_data0;
  2023. eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data0);
  2024. return phy_reg_data0 & 0x1000;
  2025. }
  2026. static int eth_port_link_is_up(unsigned int eth_port_num)
  2027. {
  2028. unsigned int phy_reg_data1;
  2029. eth_port_read_smi_reg(eth_port_num, 1, &phy_reg_data1);
  2030. if (eth_port_autoneg_supported(eth_port_num)) {
  2031. if (phy_reg_data1 & 0x20) /* auto-neg complete */
  2032. return 1;
  2033. } else if (phy_reg_data1 & 0x4) /* link up */
  2034. return 1;
  2035. return 0;
  2036. }
  2037. /*
  2038. * ethernet_get_config_reg - Get the port configuration register
  2039. *
  2040. * DESCRIPTION:
  2041. * This function returns the configuration register value of the given
  2042. * ethernet port.
  2043. *
  2044. * INPUT:
  2045. * unsigned int eth_port_num Ethernet Port number.
  2046. *
  2047. * OUTPUT:
  2048. * None.
  2049. *
  2050. * RETURN:
  2051. * Port configuration register value.
  2052. */
  2053. static unsigned int ethernet_get_config_reg(unsigned int eth_port_num)
  2054. {
  2055. unsigned int eth_config_reg;
  2056. eth_config_reg = mv_read(MV643XX_ETH_PORT_CONFIG_EXTEND_REG
  2057. (eth_port_num));
  2058. return eth_config_reg;
  2059. }
  2060. /*
  2061. * eth_port_read_smi_reg - Read PHY registers
  2062. *
  2063. * DESCRIPTION:
  2064. * This routine utilize the SMI interface to interact with the PHY in
  2065. * order to perform PHY register read.
  2066. *
  2067. * INPUT:
  2068. * unsigned int port_num Ethernet Port number.
  2069. * unsigned int phy_reg PHY register address offset.
  2070. * unsigned int *value Register value buffer.
  2071. *
  2072. * OUTPUT:
  2073. * Write the value of a specified PHY register into given buffer.
  2074. *
  2075. * RETURN:
  2076. * false if the PHY is busy or read data is not in valid state.
  2077. * true otherwise.
  2078. *
  2079. */
  2080. static void eth_port_read_smi_reg(unsigned int port_num,
  2081. unsigned int phy_reg, unsigned int *value)
  2082. {
  2083. int phy_addr = ethernet_phy_get(port_num);
  2084. unsigned long flags;
  2085. int i;
  2086. /* the SMI register is a shared resource */
  2087. spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
  2088. /* wait for the SMI register to become available */
  2089. for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
  2090. if (i == PHY_WAIT_ITERATIONS) {
  2091. printk("mv643xx PHY busy timeout, port %d\n", port_num);
  2092. goto out;
  2093. }
  2094. udelay(PHY_WAIT_MICRO_SECONDS);
  2095. }
  2096. mv_write(MV643XX_ETH_SMI_REG,
  2097. (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ);
  2098. /* now wait for the data to be valid */
  2099. for (i = 0; !(mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_READ_VALID); i++) {
  2100. if (i == PHY_WAIT_ITERATIONS) {
  2101. printk("mv643xx PHY read timeout, port %d\n", port_num);
  2102. goto out;
  2103. }
  2104. udelay(PHY_WAIT_MICRO_SECONDS);
  2105. }
  2106. *value = mv_read(MV643XX_ETH_SMI_REG) & 0xffff;
  2107. out:
  2108. spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
  2109. }
  2110. /*
  2111. * eth_port_write_smi_reg - Write to PHY registers
  2112. *
  2113. * DESCRIPTION:
  2114. * This routine utilize the SMI interface to interact with the PHY in
  2115. * order to perform writes to PHY registers.
  2116. *
  2117. * INPUT:
  2118. * unsigned int eth_port_num Ethernet Port number.
  2119. * unsigned int phy_reg PHY register address offset.
  2120. * unsigned int value Register value.
  2121. *
  2122. * OUTPUT:
  2123. * Write the given value to the specified PHY register.
  2124. *
  2125. * RETURN:
  2126. * false if the PHY is busy.
  2127. * true otherwise.
  2128. *
  2129. */
  2130. static void eth_port_write_smi_reg(unsigned int eth_port_num,
  2131. unsigned int phy_reg, unsigned int value)
  2132. {
  2133. int phy_addr;
  2134. int i;
  2135. unsigned long flags;
  2136. phy_addr = ethernet_phy_get(eth_port_num);
  2137. /* the SMI register is a shared resource */
  2138. spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
  2139. /* wait for the SMI register to become available */
  2140. for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
  2141. if (i == PHY_WAIT_ITERATIONS) {
  2142. printk("mv643xx PHY busy timeout, port %d\n",
  2143. eth_port_num);
  2144. goto out;
  2145. }
  2146. udelay(PHY_WAIT_MICRO_SECONDS);
  2147. }
  2148. mv_write(MV643XX_ETH_SMI_REG, (phy_addr << 16) | (phy_reg << 21) |
  2149. ETH_SMI_OPCODE_WRITE | (value & 0xffff));
  2150. out:
  2151. spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
  2152. }
  2153. /*
  2154. * eth_port_send - Send an Ethernet packet
  2155. *
  2156. * DESCRIPTION:
  2157. * This routine send a given packet described by p_pktinfo parameter. It
  2158. * supports transmitting of a packet spaned over multiple buffers. The
  2159. * routine updates 'curr' and 'first' indexes according to the packet
  2160. * segment passed to the routine. In case the packet segment is first,
  2161. * the 'first' index is update. In any case, the 'curr' index is updated.
  2162. * If the routine get into Tx resource error it assigns 'curr' index as
  2163. * 'first'. This way the function can abort Tx process of multiple
  2164. * descriptors per packet.
  2165. *
  2166. * INPUT:
  2167. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2168. * struct pkt_info *p_pkt_info User packet buffer.
  2169. *
  2170. * OUTPUT:
  2171. * Tx ring 'curr' and 'first' indexes are updated.
  2172. *
  2173. * RETURN:
  2174. * ETH_QUEUE_FULL in case of Tx resource error.
  2175. * ETH_ERROR in case the routine can not access Tx desc ring.
  2176. * ETH_QUEUE_LAST_RESOURCE if the routine uses the last Tx resource.
  2177. * ETH_OK otherwise.
  2178. *
  2179. */
  2180. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  2181. /*
  2182. * Modified to include the first descriptor pointer in case of SG
  2183. */
  2184. static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp,
  2185. struct pkt_info *p_pkt_info)
  2186. {
  2187. int tx_desc_curr, tx_desc_used, tx_first_desc, tx_next_desc;
  2188. struct eth_tx_desc *current_descriptor;
  2189. struct eth_tx_desc *first_descriptor;
  2190. u32 command;
  2191. /* Do not process Tx ring in case of Tx ring resource error */
  2192. if (mp->tx_resource_err)
  2193. return ETH_QUEUE_FULL;
  2194. /*
  2195. * The hardware requires that each buffer that is <= 8 bytes
  2196. * in length must be aligned on an 8 byte boundary.
  2197. */
  2198. if (p_pkt_info->byte_cnt <= 8 && p_pkt_info->buf_ptr & 0x7) {
  2199. printk(KERN_ERR
  2200. "mv643xx_eth port %d: packet size <= 8 problem\n",
  2201. mp->port_num);
  2202. return ETH_ERROR;
  2203. }
  2204. /* Get the Tx Desc ring indexes */
  2205. tx_desc_curr = mp->tx_curr_desc_q;
  2206. tx_desc_used = mp->tx_used_desc_q;
  2207. current_descriptor = &mp->p_tx_desc_area[tx_desc_curr];
  2208. tx_next_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
  2209. current_descriptor->buf_ptr = p_pkt_info->buf_ptr;
  2210. current_descriptor->byte_cnt = p_pkt_info->byte_cnt;
  2211. current_descriptor->l4i_chk = p_pkt_info->l4i_chk;
  2212. mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info;
  2213. command = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC |
  2214. ETH_BUFFER_OWNED_BY_DMA;
  2215. if (command & ETH_TX_FIRST_DESC) {
  2216. tx_first_desc = tx_desc_curr;
  2217. mp->tx_first_desc_q = tx_first_desc;
  2218. first_descriptor = current_descriptor;
  2219. mp->tx_first_command = command;
  2220. } else {
  2221. tx_first_desc = mp->tx_first_desc_q;
  2222. first_descriptor = &mp->p_tx_desc_area[tx_first_desc];
  2223. BUG_ON(first_descriptor == NULL);
  2224. current_descriptor->cmd_sts = command;
  2225. }
  2226. if (command & ETH_TX_LAST_DESC) {
  2227. wmb();
  2228. first_descriptor->cmd_sts = mp->tx_first_command;
  2229. wmb();
  2230. ETH_ENABLE_TX_QUEUE(mp->port_num);
  2231. /*
  2232. * Finish Tx packet. Update first desc in case of Tx resource
  2233. * error */
  2234. tx_first_desc = tx_next_desc;
  2235. mp->tx_first_desc_q = tx_first_desc;
  2236. }
  2237. /* Check for ring index overlap in the Tx desc ring */
  2238. if (tx_next_desc == tx_desc_used) {
  2239. mp->tx_resource_err = 1;
  2240. mp->tx_curr_desc_q = tx_first_desc;
  2241. return ETH_QUEUE_LAST_RESOURCE;
  2242. }
  2243. mp->tx_curr_desc_q = tx_next_desc;
  2244. return ETH_OK;
  2245. }
  2246. #else
  2247. static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp,
  2248. struct pkt_info *p_pkt_info)
  2249. {
  2250. int tx_desc_curr;
  2251. int tx_desc_used;
  2252. struct eth_tx_desc *current_descriptor;
  2253. unsigned int command_status;
  2254. /* Do not process Tx ring in case of Tx ring resource error */
  2255. if (mp->tx_resource_err)
  2256. return ETH_QUEUE_FULL;
  2257. /* Get the Tx Desc ring indexes */
  2258. tx_desc_curr = mp->tx_curr_desc_q;
  2259. tx_desc_used = mp->tx_used_desc_q;
  2260. current_descriptor = &mp->p_tx_desc_area[tx_desc_curr];
  2261. command_status = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC;
  2262. current_descriptor->buf_ptr = p_pkt_info->buf_ptr;
  2263. current_descriptor->byte_cnt = p_pkt_info->byte_cnt;
  2264. mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info;
  2265. /* Set last desc with DMA ownership and interrupt enable. */
  2266. wmb();
  2267. current_descriptor->cmd_sts = command_status |
  2268. ETH_BUFFER_OWNED_BY_DMA | ETH_TX_ENABLE_INTERRUPT;
  2269. wmb();
  2270. ETH_ENABLE_TX_QUEUE(mp->port_num);
  2271. /* Finish Tx packet. Update first desc in case of Tx resource error */
  2272. tx_desc_curr = (tx_desc_curr + 1) % mp->tx_ring_size;
  2273. /* Update the current descriptor */
  2274. mp->tx_curr_desc_q = tx_desc_curr;
  2275. /* Check for ring index overlap in the Tx desc ring */
  2276. if (tx_desc_curr == tx_desc_used) {
  2277. mp->tx_resource_err = 1;
  2278. return ETH_QUEUE_LAST_RESOURCE;
  2279. }
  2280. return ETH_OK;
  2281. }
  2282. #endif
  2283. /*
  2284. * eth_tx_return_desc - Free all used Tx descriptors
  2285. *
  2286. * DESCRIPTION:
  2287. * This routine returns the transmitted packet information to the caller.
  2288. * It uses the 'first' index to support Tx desc return in case a transmit
  2289. * of a packet spanned over multiple buffer still in process.
  2290. * In case the Tx queue was in "resource error" condition, where there are
  2291. * no available Tx resources, the function resets the resource error flag.
  2292. *
  2293. * INPUT:
  2294. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2295. * struct pkt_info *p_pkt_info User packet buffer.
  2296. *
  2297. * OUTPUT:
  2298. * Tx ring 'first' and 'used' indexes are updated.
  2299. *
  2300. * RETURN:
  2301. * ETH_ERROR in case the routine can not access Tx desc ring.
  2302. * ETH_RETRY in case there is transmission in process.
  2303. * ETH_END_OF_JOB if the routine has nothing to release.
  2304. * ETH_OK otherwise.
  2305. *
  2306. */
  2307. static ETH_FUNC_RET_STATUS eth_tx_return_desc(struct mv643xx_private *mp,
  2308. struct pkt_info *p_pkt_info)
  2309. {
  2310. int tx_desc_used;
  2311. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  2312. int tx_busy_desc = mp->tx_first_desc_q;
  2313. #else
  2314. int tx_busy_desc = mp->tx_curr_desc_q;
  2315. #endif
  2316. struct eth_tx_desc *p_tx_desc_used;
  2317. unsigned int command_status;
  2318. /* Get the Tx Desc ring indexes */
  2319. tx_desc_used = mp->tx_used_desc_q;
  2320. p_tx_desc_used = &mp->p_tx_desc_area[tx_desc_used];
  2321. /* Sanity check */
  2322. if (p_tx_desc_used == NULL)
  2323. return ETH_ERROR;
  2324. /* Stop release. About to overlap the current available Tx descriptor */
  2325. if (tx_desc_used == tx_busy_desc && !mp->tx_resource_err)
  2326. return ETH_END_OF_JOB;
  2327. command_status = p_tx_desc_used->cmd_sts;
  2328. /* Still transmitting... */
  2329. if (command_status & (ETH_BUFFER_OWNED_BY_DMA))
  2330. return ETH_RETRY;
  2331. /* Pass the packet information to the caller */
  2332. p_pkt_info->cmd_sts = command_status;
  2333. p_pkt_info->return_info = mp->tx_skb[tx_desc_used];
  2334. mp->tx_skb[tx_desc_used] = NULL;
  2335. /* Update the next descriptor to release. */
  2336. mp->tx_used_desc_q = (tx_desc_used + 1) % mp->tx_ring_size;
  2337. /* Any Tx return cancels the Tx resource error status */
  2338. mp->tx_resource_err = 0;
  2339. return ETH_OK;
  2340. }
  2341. /*
  2342. * eth_port_receive - Get received information from Rx ring.
  2343. *
  2344. * DESCRIPTION:
  2345. * This routine returns the received data to the caller. There is no
  2346. * data copying during routine operation. All information is returned
  2347. * using pointer to packet information struct passed from the caller.
  2348. * If the routine exhausts Rx ring resources then the resource error flag
  2349. * is set.
  2350. *
  2351. * INPUT:
  2352. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2353. * struct pkt_info *p_pkt_info User packet buffer.
  2354. *
  2355. * OUTPUT:
  2356. * Rx ring current and used indexes are updated.
  2357. *
  2358. * RETURN:
  2359. * ETH_ERROR in case the routine can not access Rx desc ring.
  2360. * ETH_QUEUE_FULL if Rx ring resources are exhausted.
  2361. * ETH_END_OF_JOB if there is no received data.
  2362. * ETH_OK otherwise.
  2363. */
  2364. static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
  2365. struct pkt_info *p_pkt_info)
  2366. {
  2367. int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
  2368. volatile struct eth_rx_desc *p_rx_desc;
  2369. unsigned int command_status;
  2370. /* Do not process Rx ring in case of Rx ring resource error */
  2371. if (mp->rx_resource_err)
  2372. return ETH_QUEUE_FULL;
  2373. /* Get the Rx Desc ring 'curr and 'used' indexes */
  2374. rx_curr_desc = mp->rx_curr_desc_q;
  2375. rx_used_desc = mp->rx_used_desc_q;
  2376. p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
  2377. /* The following parameters are used to save readings from memory */
  2378. command_status = p_rx_desc->cmd_sts;
  2379. rmb();
  2380. /* Nothing to receive... */
  2381. if (command_status & (ETH_BUFFER_OWNED_BY_DMA))
  2382. return ETH_END_OF_JOB;
  2383. p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
  2384. p_pkt_info->cmd_sts = command_status;
  2385. p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
  2386. p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
  2387. p_pkt_info->l4i_chk = p_rx_desc->buf_size;
  2388. /* Clean the return info field to indicate that the packet has been */
  2389. /* moved to the upper layers */
  2390. mp->rx_skb[rx_curr_desc] = NULL;
  2391. /* Update current index in data structure */
  2392. rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
  2393. mp->rx_curr_desc_q = rx_next_curr_desc;
  2394. /* Rx descriptors exhausted. Set the Rx ring resource error flag */
  2395. if (rx_next_curr_desc == rx_used_desc)
  2396. mp->rx_resource_err = 1;
  2397. return ETH_OK;
  2398. }
  2399. /*
  2400. * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
  2401. *
  2402. * DESCRIPTION:
  2403. * This routine returns a Rx buffer back to the Rx ring. It retrieves the
  2404. * next 'used' descriptor and attached the returned buffer to it.
  2405. * In case the Rx ring was in "resource error" condition, where there are
  2406. * no available Rx resources, the function resets the resource error flag.
  2407. *
  2408. * INPUT:
  2409. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2410. * struct pkt_info *p_pkt_info Information on returned buffer.
  2411. *
  2412. * OUTPUT:
  2413. * New available Rx resource in Rx descriptor ring.
  2414. *
  2415. * RETURN:
  2416. * ETH_ERROR in case the routine can not access Rx desc ring.
  2417. * ETH_OK otherwise.
  2418. */
  2419. static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
  2420. struct pkt_info *p_pkt_info)
  2421. {
  2422. int used_rx_desc; /* Where to return Rx resource */
  2423. volatile struct eth_rx_desc *p_used_rx_desc;
  2424. /* Get 'used' Rx descriptor */
  2425. used_rx_desc = mp->rx_used_desc_q;
  2426. p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
  2427. p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
  2428. p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
  2429. mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
  2430. /* Flush the write pipe */
  2431. /* Return the descriptor to DMA ownership */
  2432. wmb();
  2433. p_used_rx_desc->cmd_sts =
  2434. ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
  2435. wmb();
  2436. /* Move the used descriptor pointer to the next descriptor */
  2437. mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
  2438. /* Any Rx return cancels the Rx resource error status */
  2439. mp->rx_resource_err = 0;
  2440. return ETH_OK;
  2441. }
  2442. /************* Begin ethtool support *************************/
  2443. struct mv643xx_stats {
  2444. char stat_string[ETH_GSTRING_LEN];
  2445. int sizeof_stat;
  2446. int stat_offset;
  2447. };
  2448. #define MV643XX_STAT(m) sizeof(((struct mv643xx_private *)0)->m), \
  2449. offsetof(struct mv643xx_private, m)
  2450. static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
  2451. { "rx_packets", MV643XX_STAT(stats.rx_packets) },
  2452. { "tx_packets", MV643XX_STAT(stats.tx_packets) },
  2453. { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
  2454. { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
  2455. { "rx_errors", MV643XX_STAT(stats.rx_errors) },
  2456. { "tx_errors", MV643XX_STAT(stats.tx_errors) },
  2457. { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
  2458. { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
  2459. { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
  2460. { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
  2461. { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
  2462. { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
  2463. { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
  2464. { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
  2465. { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
  2466. { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
  2467. { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
  2468. { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
  2469. { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
  2470. { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
  2471. { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
  2472. { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
  2473. { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
  2474. { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
  2475. { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
  2476. { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
  2477. { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
  2478. { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
  2479. { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
  2480. { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
  2481. { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
  2482. { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
  2483. { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
  2484. { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
  2485. { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
  2486. { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
  2487. { "collision", MV643XX_STAT(mib_counters.collision) },
  2488. { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
  2489. };
  2490. #define MV643XX_STATS_LEN \
  2491. sizeof(mv643xx_gstrings_stats) / sizeof(struct mv643xx_stats)
  2492. static int
  2493. mv643xx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  2494. {
  2495. struct mv643xx_private *mp = netdev->priv;
  2496. int port_num = mp->port_num;
  2497. int autoneg = eth_port_autoneg_supported(port_num);
  2498. int mode_10_bit;
  2499. int auto_duplex;
  2500. int half_duplex = 0;
  2501. int full_duplex = 0;
  2502. int auto_speed;
  2503. int speed_10 = 0;
  2504. int speed_100 = 0;
  2505. int speed_1000 = 0;
  2506. u32 pcs = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
  2507. u32 psr = mv_read(MV643XX_ETH_PORT_STATUS_REG(port_num));
  2508. mode_10_bit = psr & MV643XX_ETH_PORT_STATUS_MODE_10_BIT;
  2509. if (mode_10_bit) {
  2510. ecmd->supported = SUPPORTED_10baseT_Half;
  2511. } else {
  2512. ecmd->supported = (SUPPORTED_10baseT_Half |
  2513. SUPPORTED_10baseT_Full |
  2514. SUPPORTED_100baseT_Half |
  2515. SUPPORTED_100baseT_Full |
  2516. SUPPORTED_1000baseT_Full |
  2517. (autoneg ? SUPPORTED_Autoneg : 0) |
  2518. SUPPORTED_TP);
  2519. auto_duplex = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_FOR_DUPLX);
  2520. auto_speed = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_SPEED_GMII);
  2521. ecmd->advertising = ADVERTISED_TP;
  2522. if (autoneg) {
  2523. ecmd->advertising |= ADVERTISED_Autoneg;
  2524. if (auto_duplex) {
  2525. half_duplex = 1;
  2526. full_duplex = 1;
  2527. } else {
  2528. if (pcs & MV643XX_ETH_SET_FULL_DUPLEX_MODE)
  2529. full_duplex = 1;
  2530. else
  2531. half_duplex = 1;
  2532. }
  2533. if (auto_speed) {
  2534. speed_10 = 1;
  2535. speed_100 = 1;
  2536. speed_1000 = 1;
  2537. } else {
  2538. if (pcs & MV643XX_ETH_SET_GMII_SPEED_TO_1000)
  2539. speed_1000 = 1;
  2540. else if (pcs & MV643XX_ETH_SET_MII_SPEED_TO_100)
  2541. speed_100 = 1;
  2542. else
  2543. speed_10 = 1;
  2544. }
  2545. if (speed_10 & half_duplex)
  2546. ecmd->advertising |= ADVERTISED_10baseT_Half;
  2547. if (speed_10 & full_duplex)
  2548. ecmd->advertising |= ADVERTISED_10baseT_Full;
  2549. if (speed_100 & half_duplex)
  2550. ecmd->advertising |= ADVERTISED_100baseT_Half;
  2551. if (speed_100 & full_duplex)
  2552. ecmd->advertising |= ADVERTISED_100baseT_Full;
  2553. if (speed_1000)
  2554. ecmd->advertising |= ADVERTISED_1000baseT_Full;
  2555. }
  2556. }
  2557. ecmd->port = PORT_TP;
  2558. ecmd->phy_address = ethernet_phy_get(port_num);
  2559. ecmd->transceiver = XCVR_EXTERNAL;
  2560. if (netif_carrier_ok(netdev)) {
  2561. if (mode_10_bit)
  2562. ecmd->speed = SPEED_10;
  2563. else {
  2564. if (psr & MV643XX_ETH_PORT_STATUS_GMII_1000)
  2565. ecmd->speed = SPEED_1000;
  2566. else if (psr & MV643XX_ETH_PORT_STATUS_MII_100)
  2567. ecmd->speed = SPEED_100;
  2568. else
  2569. ecmd->speed = SPEED_10;
  2570. }
  2571. if (psr & MV643XX_ETH_PORT_STATUS_FULL_DUPLEX)
  2572. ecmd->duplex = DUPLEX_FULL;
  2573. else
  2574. ecmd->duplex = DUPLEX_HALF;
  2575. } else {
  2576. ecmd->speed = -1;
  2577. ecmd->duplex = -1;
  2578. }
  2579. ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  2580. return 0;
  2581. }
  2582. static void
  2583. mv643xx_get_drvinfo(struct net_device *netdev,
  2584. struct ethtool_drvinfo *drvinfo)
  2585. {
  2586. strncpy(drvinfo->driver, mv643xx_driver_name, 32);
  2587. strncpy(drvinfo->version, mv643xx_driver_version, 32);
  2588. strncpy(drvinfo->fw_version, "N/A", 32);
  2589. strncpy(drvinfo->bus_info, "mv643xx", 32);
  2590. drvinfo->n_stats = MV643XX_STATS_LEN;
  2591. }
  2592. static int
  2593. mv643xx_get_stats_count(struct net_device *netdev)
  2594. {
  2595. return MV643XX_STATS_LEN;
  2596. }
  2597. static void
  2598. mv643xx_get_ethtool_stats(struct net_device *netdev,
  2599. struct ethtool_stats *stats, uint64_t *data)
  2600. {
  2601. struct mv643xx_private *mp = netdev->priv;
  2602. int i;
  2603. eth_update_mib_counters(mp);
  2604. for(i = 0; i < MV643XX_STATS_LEN; i++) {
  2605. char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
  2606. data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
  2607. sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
  2608. }
  2609. }
  2610. static void
  2611. mv643xx_get_strings(struct net_device *netdev, uint32_t stringset, uint8_t *data)
  2612. {
  2613. int i;
  2614. switch(stringset) {
  2615. case ETH_SS_STATS:
  2616. for (i=0; i < MV643XX_STATS_LEN; i++) {
  2617. memcpy(data + i * ETH_GSTRING_LEN,
  2618. mv643xx_gstrings_stats[i].stat_string,
  2619. ETH_GSTRING_LEN);
  2620. }
  2621. break;
  2622. }
  2623. }
  2624. static struct ethtool_ops mv643xx_ethtool_ops = {
  2625. .get_settings = mv643xx_get_settings,
  2626. .get_drvinfo = mv643xx_get_drvinfo,
  2627. .get_link = ethtool_op_get_link,
  2628. .get_sg = ethtool_op_get_sg,
  2629. .set_sg = ethtool_op_set_sg,
  2630. .get_strings = mv643xx_get_strings,
  2631. .get_stats_count = mv643xx_get_stats_count,
  2632. .get_ethtool_stats = mv643xx_get_ethtool_stats,
  2633. };
  2634. /************* End ethtool support *************************/