mv643xx_eth.c 80 KB

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