mv643xx_eth.c 84 KB

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