mv643xx_eth.c 80 KB

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