mv643xx_eth.c 79 KB

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