mv643xx_eth.c 85 KB

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