mv643xx_eth.c 81 KB

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