mv643xx_eth.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  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. if (mp->tx_sram_size > 0)
  1328. printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
  1329. return 0;
  1330. out:
  1331. free_netdev(dev);
  1332. return err;
  1333. }
  1334. static int mv643xx_eth_remove(struct device *ddev)
  1335. {
  1336. struct net_device *dev = dev_get_drvdata(ddev);
  1337. unregister_netdev(dev);
  1338. flush_scheduled_work();
  1339. free_netdev(dev);
  1340. dev_set_drvdata(ddev, NULL);
  1341. return 0;
  1342. }
  1343. static int mv643xx_eth_shared_probe(struct device *ddev)
  1344. {
  1345. struct platform_device *pdev = to_platform_device(ddev);
  1346. struct resource *res;
  1347. printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
  1348. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1349. if (res == NULL)
  1350. return -ENODEV;
  1351. mv643xx_eth_shared_base = ioremap(res->start,
  1352. MV643XX_ETH_SHARED_REGS_SIZE);
  1353. if (mv643xx_eth_shared_base == NULL)
  1354. return -ENOMEM;
  1355. return 0;
  1356. }
  1357. static int mv643xx_eth_shared_remove(struct device *ddev)
  1358. {
  1359. iounmap(mv643xx_eth_shared_base);
  1360. mv643xx_eth_shared_base = NULL;
  1361. return 0;
  1362. }
  1363. static struct device_driver mv643xx_eth_driver = {
  1364. .name = MV643XX_ETH_NAME,
  1365. .bus = &platform_bus_type,
  1366. .probe = mv643xx_eth_probe,
  1367. .remove = mv643xx_eth_remove,
  1368. };
  1369. static struct device_driver mv643xx_eth_shared_driver = {
  1370. .name = MV643XX_ETH_SHARED_NAME,
  1371. .bus = &platform_bus_type,
  1372. .probe = mv643xx_eth_shared_probe,
  1373. .remove = mv643xx_eth_shared_remove,
  1374. };
  1375. /*
  1376. * mv643xx_init_module
  1377. *
  1378. * Registers the network drivers into the Linux kernel
  1379. *
  1380. * Input : N/A
  1381. *
  1382. * Output : N/A
  1383. */
  1384. static int __init mv643xx_init_module(void)
  1385. {
  1386. int rc;
  1387. rc = driver_register(&mv643xx_eth_shared_driver);
  1388. if (!rc) {
  1389. rc = driver_register(&mv643xx_eth_driver);
  1390. if (rc)
  1391. driver_unregister(&mv643xx_eth_shared_driver);
  1392. }
  1393. return rc;
  1394. }
  1395. /*
  1396. * mv643xx_cleanup_module
  1397. *
  1398. * Registers the network drivers into the Linux kernel
  1399. *
  1400. * Input : N/A
  1401. *
  1402. * Output : N/A
  1403. */
  1404. static void __exit mv643xx_cleanup_module(void)
  1405. {
  1406. driver_unregister(&mv643xx_eth_driver);
  1407. driver_unregister(&mv643xx_eth_shared_driver);
  1408. }
  1409. module_init(mv643xx_init_module);
  1410. module_exit(mv643xx_cleanup_module);
  1411. MODULE_LICENSE("GPL");
  1412. MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
  1413. " and Dale Farnsworth");
  1414. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  1415. /*
  1416. * The second part is the low level driver of the gigE ethernet ports.
  1417. */
  1418. /*
  1419. * Marvell's Gigabit Ethernet controller low level driver
  1420. *
  1421. * DESCRIPTION:
  1422. * This file introduce low level API to Marvell's Gigabit Ethernet
  1423. * controller. This Gigabit Ethernet Controller driver API controls
  1424. * 1) Operations (i.e. port init, start, reset etc').
  1425. * 2) Data flow (i.e. port send, receive etc').
  1426. * Each Gigabit Ethernet port is controlled via
  1427. * struct mv643xx_private.
  1428. * This struct includes user configuration information as well as
  1429. * driver internal data needed for its operations.
  1430. *
  1431. * Supported Features:
  1432. * - This low level driver is OS independent. Allocating memory for
  1433. * the descriptor rings and buffers are not within the scope of
  1434. * this driver.
  1435. * - The user is free from Rx/Tx queue managing.
  1436. * - This low level driver introduce functionality API that enable
  1437. * the to operate Marvell's Gigabit Ethernet Controller in a
  1438. * convenient way.
  1439. * - Simple Gigabit Ethernet port operation API.
  1440. * - Simple Gigabit Ethernet port data flow API.
  1441. * - Data flow and operation API support per queue functionality.
  1442. * - Support cached descriptors for better performance.
  1443. * - Enable access to all four DRAM banks and internal SRAM memory
  1444. * spaces.
  1445. * - PHY access and control API.
  1446. * - Port control register configuration API.
  1447. * - Full control over Unicast and Multicast MAC configurations.
  1448. *
  1449. * Operation flow:
  1450. *
  1451. * Initialization phase
  1452. * This phase complete the initialization of the the
  1453. * mv643xx_private struct.
  1454. * User information regarding port configuration has to be set
  1455. * prior to calling the port initialization routine.
  1456. *
  1457. * In this phase any port Tx/Rx activity is halted, MIB counters
  1458. * are cleared, PHY address is set according to user parameter and
  1459. * access to DRAM and internal SRAM memory spaces.
  1460. *
  1461. * Driver ring initialization
  1462. * Allocating memory for the descriptor rings and buffers is not
  1463. * within the scope of this driver. Thus, the user is required to
  1464. * allocate memory for the descriptors ring and buffers. Those
  1465. * memory parameters are used by the Rx and Tx ring initialization
  1466. * routines in order to curve the descriptor linked list in a form
  1467. * of a ring.
  1468. * Note: Pay special attention to alignment issues when using
  1469. * cached descriptors/buffers. In this phase the driver store
  1470. * information in the mv643xx_private struct regarding each queue
  1471. * ring.
  1472. *
  1473. * Driver start
  1474. * This phase prepares the Ethernet port for Rx and Tx activity.
  1475. * It uses the information stored in the mv643xx_private struct to
  1476. * initialize the various port registers.
  1477. *
  1478. * Data flow:
  1479. * All packet references to/from the driver are done using
  1480. * struct pkt_info.
  1481. * This struct is a unified struct used with Rx and Tx operations.
  1482. * This way the user is not required to be familiar with neither
  1483. * Tx nor Rx descriptors structures.
  1484. * The driver's descriptors rings are management by indexes.
  1485. * Those indexes controls the ring resources and used to indicate
  1486. * a SW resource error:
  1487. * 'current'
  1488. * This index points to the current available resource for use. For
  1489. * example in Rx process this index will point to the descriptor
  1490. * that will be passed to the user upon calling the receive
  1491. * routine. In Tx process, this index will point to the descriptor
  1492. * that will be assigned with the user packet info and transmitted.
  1493. * 'used'
  1494. * This index points to the descriptor that need to restore its
  1495. * resources. For example in Rx process, using the Rx buffer return
  1496. * API will attach the buffer returned in packet info to the
  1497. * descriptor pointed by 'used'. In Tx process, using the Tx
  1498. * descriptor return will merely return the user packet info with
  1499. * the command status of the transmitted buffer pointed by the
  1500. * 'used' index. Nevertheless, it is essential to use this routine
  1501. * to update the 'used' index.
  1502. * 'first'
  1503. * This index supports Tx Scatter-Gather. It points to the first
  1504. * descriptor of a packet assembled of multiple buffers. For
  1505. * example when in middle of Such packet we have a Tx resource
  1506. * error the 'curr' index get the value of 'first' to indicate
  1507. * that the ring returned to its state before trying to transmit
  1508. * this packet.
  1509. *
  1510. * Receive operation:
  1511. * The eth_port_receive API set the packet information struct,
  1512. * passed by the caller, with received information from the
  1513. * 'current' SDMA descriptor.
  1514. * It is the user responsibility to return this resource back
  1515. * to the Rx descriptor ring to enable the reuse of this source.
  1516. * Return Rx resource is done using the eth_rx_return_buff API.
  1517. *
  1518. * Transmit operation:
  1519. * The eth_port_send API supports Scatter-Gather which enables to
  1520. * send a packet spanned over multiple buffers. This means that
  1521. * for each packet info structure given by the user and put into
  1522. * the Tx descriptors ring, will be transmitted only if the 'LAST'
  1523. * bit will be set in the packet info command status field. This
  1524. * API also consider restriction regarding buffer alignments and
  1525. * sizes.
  1526. * The user must return a Tx resource after ensuring the buffer
  1527. * has been transmitted to enable the Tx ring indexes to update.
  1528. *
  1529. * BOARD LAYOUT
  1530. * This device is on-board. No jumper diagram is necessary.
  1531. *
  1532. * EXTERNAL INTERFACE
  1533. *
  1534. * Prior to calling the initialization routine eth_port_init() the user
  1535. * must set the following fields under mv643xx_private struct:
  1536. * port_num User Ethernet port number.
  1537. * port_mac_addr[6] User defined port MAC address.
  1538. * port_config User port configuration value.
  1539. * port_config_extend User port config extend value.
  1540. * port_sdma_config User port SDMA config value.
  1541. * port_serial_control User port serial control value.
  1542. *
  1543. * This driver data flow is done using the struct pkt_info which
  1544. * is a unified struct for Rx and Tx operations:
  1545. *
  1546. * byte_cnt Tx/Rx descriptor buffer byte count.
  1547. * l4i_chk CPU provided TCP Checksum. For Tx operation
  1548. * only.
  1549. * cmd_sts Tx/Rx descriptor command status.
  1550. * buf_ptr Tx/Rx descriptor buffer pointer.
  1551. * return_info Tx/Rx user resource return information.
  1552. */
  1553. /* defines */
  1554. /* SDMA command macros */
  1555. #define ETH_ENABLE_TX_QUEUE(eth_port) \
  1556. mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port), 1)
  1557. /* locals */
  1558. /* PHY routines */
  1559. static int ethernet_phy_get(unsigned int eth_port_num);
  1560. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
  1561. /* Ethernet Port routines */
  1562. static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble,
  1563. int option);
  1564. /*
  1565. * eth_port_init - Initialize the Ethernet port driver
  1566. *
  1567. * DESCRIPTION:
  1568. * This function prepares the ethernet port to start its activity:
  1569. * 1) Completes the ethernet port driver struct initialization toward port
  1570. * start routine.
  1571. * 2) Resets the device to a quiescent state in case of warm reboot.
  1572. * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
  1573. * 4) Clean MAC tables. The reset status of those tables is unknown.
  1574. * 5) Set PHY address.
  1575. * Note: Call this routine prior to eth_port_start routine and after
  1576. * setting user values in the user fields of Ethernet port control
  1577. * struct.
  1578. *
  1579. * INPUT:
  1580. * struct mv643xx_private *mp Ethernet port control struct
  1581. *
  1582. * OUTPUT:
  1583. * See description.
  1584. *
  1585. * RETURN:
  1586. * None.
  1587. */
  1588. static void eth_port_init(struct mv643xx_private *mp)
  1589. {
  1590. mp->port_rx_queue_command = 0;
  1591. mp->port_tx_queue_command = 0;
  1592. mp->rx_resource_err = 0;
  1593. mp->tx_resource_err = 0;
  1594. eth_port_reset(mp->port_num);
  1595. eth_port_init_mac_tables(mp->port_num);
  1596. ethernet_phy_reset(mp->port_num);
  1597. }
  1598. /*
  1599. * eth_port_start - Start the Ethernet port activity.
  1600. *
  1601. * DESCRIPTION:
  1602. * This routine prepares the Ethernet port for Rx and Tx activity:
  1603. * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
  1604. * has been initialized a descriptor's ring (using
  1605. * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
  1606. * 2. Initialize and enable the Ethernet configuration port by writing to
  1607. * the port's configuration and command registers.
  1608. * 3. Initialize and enable the SDMA by writing to the SDMA's
  1609. * configuration and command registers. After completing these steps,
  1610. * the ethernet port SDMA can starts to perform Rx and Tx activities.
  1611. *
  1612. * Note: Each Rx and Tx queue descriptor's list must be initialized prior
  1613. * to calling this function (use ether_init_tx_desc_ring for Tx queues
  1614. * and ether_init_rx_desc_ring for Rx queues).
  1615. *
  1616. * INPUT:
  1617. * struct mv643xx_private *mp Ethernet port control struct
  1618. *
  1619. * OUTPUT:
  1620. * Ethernet port is ready to receive and transmit.
  1621. *
  1622. * RETURN:
  1623. * None.
  1624. */
  1625. static void eth_port_start(struct mv643xx_private *mp)
  1626. {
  1627. unsigned int port_num = mp->port_num;
  1628. int tx_curr_desc, rx_curr_desc;
  1629. /* Assignment of Tx CTRP of given queue */
  1630. tx_curr_desc = mp->tx_curr_desc_q;
  1631. mv_write(MV643XX_ETH_TX_CURRENT_QUEUE_DESC_PTR_0(port_num),
  1632. (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
  1633. /* Assignment of Rx CRDP of given queue */
  1634. rx_curr_desc = mp->rx_curr_desc_q;
  1635. mv_write(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num),
  1636. (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
  1637. /* Add the assigned Ethernet address to the port's address table */
  1638. eth_port_uc_addr_set(port_num, mp->port_mac_addr);
  1639. /* Assign port configuration and command. */
  1640. mv_write(MV643XX_ETH_PORT_CONFIG_REG(port_num), mp->port_config);
  1641. mv_write(MV643XX_ETH_PORT_CONFIG_EXTEND_REG(port_num),
  1642. mp->port_config_extend);
  1643. /* Increase the Rx side buffer size if supporting GigE */
  1644. if (mp->port_serial_control & MV643XX_ETH_SET_GMII_SPEED_TO_1000)
  1645. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
  1646. (mp->port_serial_control & 0xfff1ffff) | (0x5 << 17));
  1647. else
  1648. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
  1649. mp->port_serial_control);
  1650. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
  1651. mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num)) |
  1652. MV643XX_ETH_SERIAL_PORT_ENABLE);
  1653. /* Assign port SDMA configuration */
  1654. mv_write(MV643XX_ETH_SDMA_CONFIG_REG(port_num),
  1655. mp->port_sdma_config);
  1656. /* Enable port Rx. */
  1657. mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num),
  1658. mp->port_rx_queue_command);
  1659. /* Disable port bandwidth limits by clearing MTU register */
  1660. mv_write(MV643XX_ETH_MAXIMUM_TRANSMIT_UNIT(port_num), 0);
  1661. }
  1662. /*
  1663. * eth_port_uc_addr_set - This function Set the port Unicast address.
  1664. *
  1665. * DESCRIPTION:
  1666. * This function Set the port Ethernet MAC address.
  1667. *
  1668. * INPUT:
  1669. * unsigned int eth_port_num Port number.
  1670. * char * p_addr Address to be set
  1671. *
  1672. * OUTPUT:
  1673. * Set MAC address low and high registers. also calls eth_port_uc_addr()
  1674. * To set the unicast table with the proper information.
  1675. *
  1676. * RETURN:
  1677. * N/A.
  1678. *
  1679. */
  1680. static void eth_port_uc_addr_set(unsigned int eth_port_num,
  1681. unsigned char *p_addr)
  1682. {
  1683. unsigned int mac_h;
  1684. unsigned int mac_l;
  1685. mac_l = (p_addr[4] << 8) | (p_addr[5]);
  1686. mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
  1687. (p_addr[3] << 0);
  1688. mv_write(MV643XX_ETH_MAC_ADDR_LOW(eth_port_num), mac_l);
  1689. mv_write(MV643XX_ETH_MAC_ADDR_HIGH(eth_port_num), mac_h);
  1690. /* Accept frames of this address */
  1691. eth_port_uc_addr(eth_port_num, p_addr[5], ACCEPT_MAC_ADDR);
  1692. return;
  1693. }
  1694. /*
  1695. * eth_port_uc_addr_get - This function retrieves the port Unicast address
  1696. * (MAC address) from the ethernet hw registers.
  1697. *
  1698. * DESCRIPTION:
  1699. * This function retrieves the port Ethernet MAC address.
  1700. *
  1701. * INPUT:
  1702. * unsigned int eth_port_num Port number.
  1703. * char *MacAddr pointer where the MAC address is stored
  1704. *
  1705. * OUTPUT:
  1706. * Copy the MAC address to the location pointed to by MacAddr
  1707. *
  1708. * RETURN:
  1709. * N/A.
  1710. *
  1711. */
  1712. static void eth_port_uc_addr_get(struct net_device *dev, unsigned char *p_addr)
  1713. {
  1714. struct mv643xx_private *mp = netdev_priv(dev);
  1715. unsigned int mac_h;
  1716. unsigned int mac_l;
  1717. mac_h = mv_read(MV643XX_ETH_MAC_ADDR_HIGH(mp->port_num));
  1718. mac_l = mv_read(MV643XX_ETH_MAC_ADDR_LOW(mp->port_num));
  1719. p_addr[0] = (mac_h >> 24) & 0xff;
  1720. p_addr[1] = (mac_h >> 16) & 0xff;
  1721. p_addr[2] = (mac_h >> 8) & 0xff;
  1722. p_addr[3] = mac_h & 0xff;
  1723. p_addr[4] = (mac_l >> 8) & 0xff;
  1724. p_addr[5] = mac_l & 0xff;
  1725. }
  1726. /*
  1727. * eth_port_uc_addr - This function Set the port unicast address table
  1728. *
  1729. * DESCRIPTION:
  1730. * This function locates the proper entry in the Unicast table for the
  1731. * specified MAC nibble and sets its properties according to function
  1732. * parameters.
  1733. *
  1734. * INPUT:
  1735. * unsigned int eth_port_num Port number.
  1736. * unsigned char uc_nibble Unicast MAC Address last nibble.
  1737. * int option 0 = Add, 1 = remove address.
  1738. *
  1739. * OUTPUT:
  1740. * This function add/removes MAC addresses from the port unicast address
  1741. * table.
  1742. *
  1743. * RETURN:
  1744. * true is output succeeded.
  1745. * false if option parameter is invalid.
  1746. *
  1747. */
  1748. static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble,
  1749. int option)
  1750. {
  1751. unsigned int unicast_reg;
  1752. unsigned int tbl_offset;
  1753. unsigned int reg_offset;
  1754. /* Locate the Unicast table entry */
  1755. uc_nibble = (0xf & uc_nibble);
  1756. tbl_offset = (uc_nibble / 4) * 4; /* Register offset from unicast table base */
  1757. reg_offset = uc_nibble % 4; /* Entry offset within the above register */
  1758. switch (option) {
  1759. case REJECT_MAC_ADDR:
  1760. /* Clear accepts frame bit at given unicast DA table entry */
  1761. unicast_reg = mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1762. (eth_port_num) + tbl_offset));
  1763. unicast_reg &= (0x0E << (8 * reg_offset));
  1764. mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1765. (eth_port_num) + tbl_offset), unicast_reg);
  1766. break;
  1767. case ACCEPT_MAC_ADDR:
  1768. /* Set accepts frame bit at unicast DA filter table entry */
  1769. unicast_reg =
  1770. mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1771. (eth_port_num) + tbl_offset));
  1772. unicast_reg |= (0x01 << (8 * reg_offset));
  1773. mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1774. (eth_port_num) + tbl_offset), unicast_reg);
  1775. break;
  1776. default:
  1777. return 0;
  1778. }
  1779. return 1;
  1780. }
  1781. /*
  1782. * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
  1783. *
  1784. * DESCRIPTION:
  1785. * Go through all the DA filter tables (Unicast, Special Multicast &
  1786. * Other Multicast) and set each entry to 0.
  1787. *
  1788. * INPUT:
  1789. * unsigned int eth_port_num Ethernet Port number.
  1790. *
  1791. * OUTPUT:
  1792. * Multicast and Unicast packets are rejected.
  1793. *
  1794. * RETURN:
  1795. * None.
  1796. */
  1797. static void eth_port_init_mac_tables(unsigned int eth_port_num)
  1798. {
  1799. int table_index;
  1800. /* Clear DA filter unicast table (Ex_dFUT) */
  1801. for (table_index = 0; table_index <= 0xC; table_index += 4)
  1802. mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1803. (eth_port_num) + table_index), 0);
  1804. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  1805. /* Clear DA filter special multicast table (Ex_dFSMT) */
  1806. mv_write((MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
  1807. (eth_port_num) + table_index), 0);
  1808. /* Clear DA filter other multicast table (Ex_dFOMT) */
  1809. mv_write((MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE
  1810. (eth_port_num) + table_index), 0);
  1811. }
  1812. }
  1813. /*
  1814. * eth_clear_mib_counters - Clear all MIB counters
  1815. *
  1816. * DESCRIPTION:
  1817. * This function clears all MIB counters of a specific ethernet port.
  1818. * A read from the MIB counter will reset the counter.
  1819. *
  1820. * INPUT:
  1821. * unsigned int eth_port_num Ethernet Port number.
  1822. *
  1823. * OUTPUT:
  1824. * After reading all MIB counters, the counters resets.
  1825. *
  1826. * RETURN:
  1827. * MIB counter value.
  1828. *
  1829. */
  1830. static void eth_clear_mib_counters(unsigned int eth_port_num)
  1831. {
  1832. int i;
  1833. /* Perform dummy reads from MIB counters */
  1834. for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
  1835. i += 4)
  1836. mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(eth_port_num) + i);
  1837. }
  1838. static inline u32 read_mib(struct mv643xx_private *mp, int offset)
  1839. {
  1840. return mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(mp->port_num) + offset);
  1841. }
  1842. static void eth_update_mib_counters(struct mv643xx_private *mp)
  1843. {
  1844. struct mv643xx_mib_counters *p = &mp->mib_counters;
  1845. int offset;
  1846. p->good_octets_received +=
  1847. read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
  1848. p->good_octets_received +=
  1849. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
  1850. for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
  1851. offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
  1852. offset += 4)
  1853. *(u32 *)((char *)p + offset) = read_mib(mp, offset);
  1854. p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
  1855. p->good_octets_sent +=
  1856. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
  1857. for (offset = ETH_MIB_GOOD_FRAMES_SENT;
  1858. offset <= ETH_MIB_LATE_COLLISION;
  1859. offset += 4)
  1860. *(u32 *)((char *)p + offset) = read_mib(mp, offset);
  1861. }
  1862. /*
  1863. * ethernet_phy_detect - Detect whether a phy is present
  1864. *
  1865. * DESCRIPTION:
  1866. * This function tests whether there is a PHY present on
  1867. * the specified port.
  1868. *
  1869. * INPUT:
  1870. * unsigned int eth_port_num Ethernet Port number.
  1871. *
  1872. * OUTPUT:
  1873. * None
  1874. *
  1875. * RETURN:
  1876. * 0 on success
  1877. * -ENODEV on failure
  1878. *
  1879. */
  1880. static int ethernet_phy_detect(unsigned int port_num)
  1881. {
  1882. unsigned int phy_reg_data0;
  1883. int auto_neg;
  1884. eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
  1885. auto_neg = phy_reg_data0 & 0x1000;
  1886. phy_reg_data0 ^= 0x1000; /* invert auto_neg */
  1887. eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
  1888. eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
  1889. if ((phy_reg_data0 & 0x1000) == auto_neg)
  1890. return -ENODEV; /* change didn't take */
  1891. phy_reg_data0 ^= 0x1000;
  1892. eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
  1893. return 0;
  1894. }
  1895. /*
  1896. * ethernet_phy_get - Get the ethernet port PHY address.
  1897. *
  1898. * DESCRIPTION:
  1899. * This routine returns the given ethernet port PHY address.
  1900. *
  1901. * INPUT:
  1902. * unsigned int eth_port_num Ethernet Port number.
  1903. *
  1904. * OUTPUT:
  1905. * None.
  1906. *
  1907. * RETURN:
  1908. * PHY address.
  1909. *
  1910. */
  1911. static int ethernet_phy_get(unsigned int eth_port_num)
  1912. {
  1913. unsigned int reg_data;
  1914. reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
  1915. return ((reg_data >> (5 * eth_port_num)) & 0x1f);
  1916. }
  1917. /*
  1918. * ethernet_phy_set - Set the ethernet port PHY address.
  1919. *
  1920. * DESCRIPTION:
  1921. * This routine sets the given ethernet port PHY address.
  1922. *
  1923. * INPUT:
  1924. * unsigned int eth_port_num Ethernet Port number.
  1925. * int phy_addr PHY address.
  1926. *
  1927. * OUTPUT:
  1928. * None.
  1929. *
  1930. * RETURN:
  1931. * None.
  1932. *
  1933. */
  1934. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr)
  1935. {
  1936. u32 reg_data;
  1937. int addr_shift = 5 * eth_port_num;
  1938. reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
  1939. reg_data &= ~(0x1f << addr_shift);
  1940. reg_data |= (phy_addr & 0x1f) << addr_shift;
  1941. mv_write(MV643XX_ETH_PHY_ADDR_REG, reg_data);
  1942. }
  1943. /*
  1944. * ethernet_phy_reset - Reset Ethernet port PHY.
  1945. *
  1946. * DESCRIPTION:
  1947. * This routine utilizes the SMI interface to reset the ethernet port PHY.
  1948. *
  1949. * INPUT:
  1950. * unsigned int eth_port_num Ethernet Port number.
  1951. *
  1952. * OUTPUT:
  1953. * The PHY is reset.
  1954. *
  1955. * RETURN:
  1956. * None.
  1957. *
  1958. */
  1959. static void ethernet_phy_reset(unsigned int eth_port_num)
  1960. {
  1961. unsigned int phy_reg_data;
  1962. /* Reset the PHY */
  1963. eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
  1964. phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
  1965. eth_port_write_smi_reg(eth_port_num, 0, phy_reg_data);
  1966. }
  1967. /*
  1968. * eth_port_reset - Reset Ethernet port
  1969. *
  1970. * DESCRIPTION:
  1971. * This routine resets the chip by aborting any SDMA engine activity and
  1972. * clearing the MIB counters. The Receiver and the Transmit unit are in
  1973. * idle state after this command is performed and the port is disabled.
  1974. *
  1975. * INPUT:
  1976. * unsigned int eth_port_num Ethernet Port number.
  1977. *
  1978. * OUTPUT:
  1979. * Channel activity is halted.
  1980. *
  1981. * RETURN:
  1982. * None.
  1983. *
  1984. */
  1985. static void eth_port_reset(unsigned int port_num)
  1986. {
  1987. unsigned int reg_data;
  1988. /* Stop Tx port activity. Check port Tx activity. */
  1989. reg_data = mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num));
  1990. if (reg_data & 0xFF) {
  1991. /* Issue stop command for active channels only */
  1992. mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num),
  1993. (reg_data << 8));
  1994. /* Wait for all Tx activity to terminate. */
  1995. /* Check port cause register that all Tx queues are stopped */
  1996. while (mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num))
  1997. & 0xFF)
  1998. udelay(10);
  1999. }
  2000. /* Stop Rx port activity. Check port Rx activity. */
  2001. reg_data = mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num));
  2002. if (reg_data & 0xFF) {
  2003. /* Issue stop command for active channels only */
  2004. mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num),
  2005. (reg_data << 8));
  2006. /* Wait for all Rx activity to terminate. */
  2007. /* Check port cause register that all Rx queues are stopped */
  2008. while (mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num))
  2009. & 0xFF)
  2010. udelay(10);
  2011. }
  2012. /* Clear all MIB counters */
  2013. eth_clear_mib_counters(port_num);
  2014. /* Reset the Enable bit in the Configuration Register */
  2015. reg_data = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
  2016. reg_data &= ~MV643XX_ETH_SERIAL_PORT_ENABLE;
  2017. mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), reg_data);
  2018. }
  2019. static int eth_port_autoneg_supported(unsigned int eth_port_num)
  2020. {
  2021. unsigned int phy_reg_data0;
  2022. eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data0);
  2023. return phy_reg_data0 & 0x1000;
  2024. }
  2025. static int eth_port_link_is_up(unsigned int eth_port_num)
  2026. {
  2027. unsigned int phy_reg_data1;
  2028. eth_port_read_smi_reg(eth_port_num, 1, &phy_reg_data1);
  2029. if (eth_port_autoneg_supported(eth_port_num)) {
  2030. if (phy_reg_data1 & 0x20) /* auto-neg complete */
  2031. return 1;
  2032. } else if (phy_reg_data1 & 0x4) /* link up */
  2033. return 1;
  2034. return 0;
  2035. }
  2036. /*
  2037. * eth_port_read_smi_reg - Read PHY registers
  2038. *
  2039. * DESCRIPTION:
  2040. * This routine utilize the SMI interface to interact with the PHY in
  2041. * order to perform PHY register read.
  2042. *
  2043. * INPUT:
  2044. * unsigned int port_num Ethernet Port number.
  2045. * unsigned int phy_reg PHY register address offset.
  2046. * unsigned int *value Register value buffer.
  2047. *
  2048. * OUTPUT:
  2049. * Write the value of a specified PHY register into given buffer.
  2050. *
  2051. * RETURN:
  2052. * false if the PHY is busy or read data is not in valid state.
  2053. * true otherwise.
  2054. *
  2055. */
  2056. static void eth_port_read_smi_reg(unsigned int port_num,
  2057. unsigned int phy_reg, unsigned int *value)
  2058. {
  2059. int phy_addr = ethernet_phy_get(port_num);
  2060. unsigned long flags;
  2061. int i;
  2062. /* the SMI register is a shared resource */
  2063. spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
  2064. /* wait for the SMI register to become available */
  2065. for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
  2066. if (i == PHY_WAIT_ITERATIONS) {
  2067. printk("mv643xx PHY busy timeout, port %d\n", port_num);
  2068. goto out;
  2069. }
  2070. udelay(PHY_WAIT_MICRO_SECONDS);
  2071. }
  2072. mv_write(MV643XX_ETH_SMI_REG,
  2073. (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ);
  2074. /* now wait for the data to be valid */
  2075. for (i = 0; !(mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_READ_VALID); i++) {
  2076. if (i == PHY_WAIT_ITERATIONS) {
  2077. printk("mv643xx PHY read timeout, port %d\n", port_num);
  2078. goto out;
  2079. }
  2080. udelay(PHY_WAIT_MICRO_SECONDS);
  2081. }
  2082. *value = mv_read(MV643XX_ETH_SMI_REG) & 0xffff;
  2083. out:
  2084. spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
  2085. }
  2086. /*
  2087. * eth_port_write_smi_reg - Write to PHY registers
  2088. *
  2089. * DESCRIPTION:
  2090. * This routine utilize the SMI interface to interact with the PHY in
  2091. * order to perform writes to PHY registers.
  2092. *
  2093. * INPUT:
  2094. * unsigned int eth_port_num Ethernet Port number.
  2095. * unsigned int phy_reg PHY register address offset.
  2096. * unsigned int value Register value.
  2097. *
  2098. * OUTPUT:
  2099. * Write the given value to the specified PHY register.
  2100. *
  2101. * RETURN:
  2102. * false if the PHY is busy.
  2103. * true otherwise.
  2104. *
  2105. */
  2106. static void eth_port_write_smi_reg(unsigned int eth_port_num,
  2107. unsigned int phy_reg, unsigned int value)
  2108. {
  2109. int phy_addr;
  2110. int i;
  2111. unsigned long flags;
  2112. phy_addr = ethernet_phy_get(eth_port_num);
  2113. /* the SMI register is a shared resource */
  2114. spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
  2115. /* wait for the SMI register to become available */
  2116. for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
  2117. if (i == PHY_WAIT_ITERATIONS) {
  2118. printk("mv643xx PHY busy timeout, port %d\n",
  2119. eth_port_num);
  2120. goto out;
  2121. }
  2122. udelay(PHY_WAIT_MICRO_SECONDS);
  2123. }
  2124. mv_write(MV643XX_ETH_SMI_REG, (phy_addr << 16) | (phy_reg << 21) |
  2125. ETH_SMI_OPCODE_WRITE | (value & 0xffff));
  2126. out:
  2127. spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
  2128. }
  2129. /*
  2130. * eth_port_send - Send an Ethernet packet
  2131. *
  2132. * DESCRIPTION:
  2133. * This routine send a given packet described by p_pktinfo parameter. It
  2134. * supports transmitting of a packet spaned over multiple buffers. The
  2135. * routine updates 'curr' and 'first' indexes according to the packet
  2136. * segment passed to the routine. In case the packet segment is first,
  2137. * the 'first' index is update. In any case, the 'curr' index is updated.
  2138. * If the routine get into Tx resource error it assigns 'curr' index as
  2139. * 'first'. This way the function can abort Tx process of multiple
  2140. * descriptors per packet.
  2141. *
  2142. * INPUT:
  2143. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2144. * struct pkt_info *p_pkt_info User packet buffer.
  2145. *
  2146. * OUTPUT:
  2147. * Tx ring 'curr' and 'first' indexes are updated.
  2148. *
  2149. * RETURN:
  2150. * ETH_QUEUE_FULL in case of Tx resource error.
  2151. * ETH_ERROR in case the routine can not access Tx desc ring.
  2152. * ETH_QUEUE_LAST_RESOURCE if the routine uses the last Tx resource.
  2153. * ETH_OK otherwise.
  2154. *
  2155. */
  2156. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  2157. /*
  2158. * Modified to include the first descriptor pointer in case of SG
  2159. */
  2160. static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp,
  2161. struct pkt_info *p_pkt_info)
  2162. {
  2163. int tx_desc_curr, tx_desc_used, tx_first_desc, tx_next_desc;
  2164. struct eth_tx_desc *current_descriptor;
  2165. struct eth_tx_desc *first_descriptor;
  2166. u32 command;
  2167. /* Do not process Tx ring in case of Tx ring resource error */
  2168. if (mp->tx_resource_err)
  2169. return ETH_QUEUE_FULL;
  2170. /*
  2171. * The hardware requires that each buffer that is <= 8 bytes
  2172. * in length must be aligned on an 8 byte boundary.
  2173. */
  2174. if (p_pkt_info->byte_cnt <= 8 && p_pkt_info->buf_ptr & 0x7) {
  2175. printk(KERN_ERR
  2176. "mv643xx_eth port %d: packet size <= 8 problem\n",
  2177. mp->port_num);
  2178. return ETH_ERROR;
  2179. }
  2180. mp->tx_ring_skbs++;
  2181. BUG_ON(mp->tx_ring_skbs > mp->tx_ring_size);
  2182. /* Get the Tx Desc ring indexes */
  2183. tx_desc_curr = mp->tx_curr_desc_q;
  2184. tx_desc_used = mp->tx_used_desc_q;
  2185. current_descriptor = &mp->p_tx_desc_area[tx_desc_curr];
  2186. tx_next_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
  2187. current_descriptor->buf_ptr = p_pkt_info->buf_ptr;
  2188. current_descriptor->byte_cnt = p_pkt_info->byte_cnt;
  2189. current_descriptor->l4i_chk = p_pkt_info->l4i_chk;
  2190. mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info;
  2191. command = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC |
  2192. ETH_BUFFER_OWNED_BY_DMA;
  2193. if (command & ETH_TX_FIRST_DESC) {
  2194. tx_first_desc = tx_desc_curr;
  2195. mp->tx_first_desc_q = tx_first_desc;
  2196. first_descriptor = current_descriptor;
  2197. mp->tx_first_command = command;
  2198. } else {
  2199. tx_first_desc = mp->tx_first_desc_q;
  2200. first_descriptor = &mp->p_tx_desc_area[tx_first_desc];
  2201. BUG_ON(first_descriptor == NULL);
  2202. current_descriptor->cmd_sts = command;
  2203. }
  2204. if (command & ETH_TX_LAST_DESC) {
  2205. wmb();
  2206. first_descriptor->cmd_sts = mp->tx_first_command;
  2207. wmb();
  2208. ETH_ENABLE_TX_QUEUE(mp->port_num);
  2209. /*
  2210. * Finish Tx packet. Update first desc in case of Tx resource
  2211. * error */
  2212. tx_first_desc = tx_next_desc;
  2213. mp->tx_first_desc_q = tx_first_desc;
  2214. }
  2215. /* Check for ring index overlap in the Tx desc ring */
  2216. if (tx_next_desc == tx_desc_used) {
  2217. mp->tx_resource_err = 1;
  2218. mp->tx_curr_desc_q = tx_first_desc;
  2219. return ETH_QUEUE_LAST_RESOURCE;
  2220. }
  2221. mp->tx_curr_desc_q = tx_next_desc;
  2222. return ETH_OK;
  2223. }
  2224. #else
  2225. static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp,
  2226. struct pkt_info *p_pkt_info)
  2227. {
  2228. int tx_desc_curr;
  2229. int tx_desc_used;
  2230. struct eth_tx_desc *current_descriptor;
  2231. unsigned int command_status;
  2232. /* Do not process Tx ring in case of Tx ring resource error */
  2233. if (mp->tx_resource_err)
  2234. return ETH_QUEUE_FULL;
  2235. mp->tx_ring_skbs++;
  2236. BUG_ON(mp->tx_ring_skbs > mp->tx_ring_size);
  2237. /* Get the Tx Desc ring indexes */
  2238. tx_desc_curr = mp->tx_curr_desc_q;
  2239. tx_desc_used = mp->tx_used_desc_q;
  2240. current_descriptor = &mp->p_tx_desc_area[tx_desc_curr];
  2241. command_status = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC;
  2242. current_descriptor->buf_ptr = p_pkt_info->buf_ptr;
  2243. current_descriptor->byte_cnt = p_pkt_info->byte_cnt;
  2244. mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info;
  2245. /* Set last desc with DMA ownership and interrupt enable. */
  2246. wmb();
  2247. current_descriptor->cmd_sts = command_status |
  2248. ETH_BUFFER_OWNED_BY_DMA | ETH_TX_ENABLE_INTERRUPT;
  2249. wmb();
  2250. ETH_ENABLE_TX_QUEUE(mp->port_num);
  2251. /* Finish Tx packet. Update first desc in case of Tx resource error */
  2252. tx_desc_curr = (tx_desc_curr + 1) % mp->tx_ring_size;
  2253. /* Update the current descriptor */
  2254. mp->tx_curr_desc_q = tx_desc_curr;
  2255. /* Check for ring index overlap in the Tx desc ring */
  2256. if (tx_desc_curr == tx_desc_used) {
  2257. mp->tx_resource_err = 1;
  2258. return ETH_QUEUE_LAST_RESOURCE;
  2259. }
  2260. return ETH_OK;
  2261. }
  2262. #endif
  2263. /*
  2264. * eth_tx_return_desc - Free all used Tx descriptors
  2265. *
  2266. * DESCRIPTION:
  2267. * This routine returns the transmitted packet information to the caller.
  2268. * It uses the 'first' index to support Tx desc return in case a transmit
  2269. * of a packet spanned over multiple buffer still in process.
  2270. * In case the Tx queue was in "resource error" condition, where there are
  2271. * no available Tx resources, the function resets the resource error flag.
  2272. *
  2273. * INPUT:
  2274. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2275. * struct pkt_info *p_pkt_info User packet buffer.
  2276. *
  2277. * OUTPUT:
  2278. * Tx ring 'first' and 'used' indexes are updated.
  2279. *
  2280. * RETURN:
  2281. * ETH_ERROR in case the routine can not access Tx desc ring.
  2282. * ETH_RETRY in case there is transmission in process.
  2283. * ETH_END_OF_JOB if the routine has nothing to release.
  2284. * ETH_OK otherwise.
  2285. *
  2286. */
  2287. static ETH_FUNC_RET_STATUS eth_tx_return_desc(struct mv643xx_private *mp,
  2288. struct pkt_info *p_pkt_info)
  2289. {
  2290. int tx_desc_used;
  2291. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  2292. int tx_busy_desc = mp->tx_first_desc_q;
  2293. #else
  2294. int tx_busy_desc = mp->tx_curr_desc_q;
  2295. #endif
  2296. struct eth_tx_desc *p_tx_desc_used;
  2297. unsigned int command_status;
  2298. /* Get the Tx Desc ring indexes */
  2299. tx_desc_used = mp->tx_used_desc_q;
  2300. p_tx_desc_used = &mp->p_tx_desc_area[tx_desc_used];
  2301. /* Sanity check */
  2302. if (p_tx_desc_used == NULL)
  2303. return ETH_ERROR;
  2304. /* Stop release. About to overlap the current available Tx descriptor */
  2305. if (tx_desc_used == tx_busy_desc && !mp->tx_resource_err)
  2306. return ETH_END_OF_JOB;
  2307. command_status = p_tx_desc_used->cmd_sts;
  2308. /* Still transmitting... */
  2309. if (command_status & (ETH_BUFFER_OWNED_BY_DMA))
  2310. return ETH_RETRY;
  2311. /* Pass the packet information to the caller */
  2312. p_pkt_info->cmd_sts = command_status;
  2313. p_pkt_info->return_info = mp->tx_skb[tx_desc_used];
  2314. mp->tx_skb[tx_desc_used] = NULL;
  2315. /* Update the next descriptor to release. */
  2316. mp->tx_used_desc_q = (tx_desc_used + 1) % mp->tx_ring_size;
  2317. /* Any Tx return cancels the Tx resource error status */
  2318. mp->tx_resource_err = 0;
  2319. BUG_ON(mp->tx_ring_skbs == 0);
  2320. mp->tx_ring_skbs--;
  2321. return ETH_OK;
  2322. }
  2323. /*
  2324. * eth_port_receive - Get received information from Rx ring.
  2325. *
  2326. * DESCRIPTION:
  2327. * This routine returns the received data to the caller. There is no
  2328. * data copying during routine operation. All information is returned
  2329. * using pointer to packet information struct passed from the caller.
  2330. * If the routine exhausts Rx ring resources then the resource error flag
  2331. * is set.
  2332. *
  2333. * INPUT:
  2334. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2335. * struct pkt_info *p_pkt_info User packet buffer.
  2336. *
  2337. * OUTPUT:
  2338. * Rx ring current and used indexes are updated.
  2339. *
  2340. * RETURN:
  2341. * ETH_ERROR in case the routine can not access Rx desc ring.
  2342. * ETH_QUEUE_FULL if Rx ring resources are exhausted.
  2343. * ETH_END_OF_JOB if there is no received data.
  2344. * ETH_OK otherwise.
  2345. */
  2346. static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
  2347. struct pkt_info *p_pkt_info)
  2348. {
  2349. int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
  2350. volatile struct eth_rx_desc *p_rx_desc;
  2351. unsigned int command_status;
  2352. /* Do not process Rx ring in case of Rx ring resource error */
  2353. if (mp->rx_resource_err)
  2354. return ETH_QUEUE_FULL;
  2355. /* Get the Rx Desc ring 'curr and 'used' indexes */
  2356. rx_curr_desc = mp->rx_curr_desc_q;
  2357. rx_used_desc = mp->rx_used_desc_q;
  2358. p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
  2359. /* The following parameters are used to save readings from memory */
  2360. command_status = p_rx_desc->cmd_sts;
  2361. rmb();
  2362. /* Nothing to receive... */
  2363. if (command_status & (ETH_BUFFER_OWNED_BY_DMA))
  2364. return ETH_END_OF_JOB;
  2365. p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
  2366. p_pkt_info->cmd_sts = command_status;
  2367. p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
  2368. p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
  2369. p_pkt_info->l4i_chk = p_rx_desc->buf_size;
  2370. /* Clean the return info field to indicate that the packet has been */
  2371. /* moved to the upper layers */
  2372. mp->rx_skb[rx_curr_desc] = NULL;
  2373. /* Update current index in data structure */
  2374. rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
  2375. mp->rx_curr_desc_q = rx_next_curr_desc;
  2376. /* Rx descriptors exhausted. Set the Rx ring resource error flag */
  2377. if (rx_next_curr_desc == rx_used_desc)
  2378. mp->rx_resource_err = 1;
  2379. return ETH_OK;
  2380. }
  2381. /*
  2382. * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
  2383. *
  2384. * DESCRIPTION:
  2385. * This routine returns a Rx buffer back to the Rx ring. It retrieves the
  2386. * next 'used' descriptor and attached the returned buffer to it.
  2387. * In case the Rx ring was in "resource error" condition, where there are
  2388. * no available Rx resources, the function resets the resource error flag.
  2389. *
  2390. * INPUT:
  2391. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2392. * struct pkt_info *p_pkt_info Information on returned buffer.
  2393. *
  2394. * OUTPUT:
  2395. * New available Rx resource in Rx descriptor ring.
  2396. *
  2397. * RETURN:
  2398. * ETH_ERROR in case the routine can not access Rx desc ring.
  2399. * ETH_OK otherwise.
  2400. */
  2401. static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
  2402. struct pkt_info *p_pkt_info)
  2403. {
  2404. int used_rx_desc; /* Where to return Rx resource */
  2405. volatile struct eth_rx_desc *p_used_rx_desc;
  2406. /* Get 'used' Rx descriptor */
  2407. used_rx_desc = mp->rx_used_desc_q;
  2408. p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
  2409. p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
  2410. p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
  2411. mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
  2412. /* Flush the write pipe */
  2413. /* Return the descriptor to DMA ownership */
  2414. wmb();
  2415. p_used_rx_desc->cmd_sts =
  2416. ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
  2417. wmb();
  2418. /* Move the used descriptor pointer to the next descriptor */
  2419. mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
  2420. /* Any Rx return cancels the Rx resource error status */
  2421. mp->rx_resource_err = 0;
  2422. return ETH_OK;
  2423. }
  2424. /************* Begin ethtool support *************************/
  2425. struct mv643xx_stats {
  2426. char stat_string[ETH_GSTRING_LEN];
  2427. int sizeof_stat;
  2428. int stat_offset;
  2429. };
  2430. #define MV643XX_STAT(m) sizeof(((struct mv643xx_private *)0)->m), \
  2431. offsetof(struct mv643xx_private, m)
  2432. static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
  2433. { "rx_packets", MV643XX_STAT(stats.rx_packets) },
  2434. { "tx_packets", MV643XX_STAT(stats.tx_packets) },
  2435. { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
  2436. { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
  2437. { "rx_errors", MV643XX_STAT(stats.rx_errors) },
  2438. { "tx_errors", MV643XX_STAT(stats.tx_errors) },
  2439. { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
  2440. { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
  2441. { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
  2442. { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
  2443. { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
  2444. { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
  2445. { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
  2446. { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
  2447. { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
  2448. { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
  2449. { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
  2450. { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
  2451. { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
  2452. { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
  2453. { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
  2454. { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
  2455. { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
  2456. { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
  2457. { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
  2458. { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
  2459. { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
  2460. { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
  2461. { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
  2462. { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
  2463. { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
  2464. { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
  2465. { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
  2466. { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
  2467. { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
  2468. { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
  2469. { "collision", MV643XX_STAT(mib_counters.collision) },
  2470. { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
  2471. };
  2472. #define MV643XX_STATS_LEN \
  2473. sizeof(mv643xx_gstrings_stats) / sizeof(struct mv643xx_stats)
  2474. static int
  2475. mv643xx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  2476. {
  2477. struct mv643xx_private *mp = netdev->priv;
  2478. int port_num = mp->port_num;
  2479. int autoneg = eth_port_autoneg_supported(port_num);
  2480. int mode_10_bit;
  2481. int auto_duplex;
  2482. int half_duplex = 0;
  2483. int full_duplex = 0;
  2484. int auto_speed;
  2485. int speed_10 = 0;
  2486. int speed_100 = 0;
  2487. int speed_1000 = 0;
  2488. u32 pcs = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
  2489. u32 psr = mv_read(MV643XX_ETH_PORT_STATUS_REG(port_num));
  2490. mode_10_bit = psr & MV643XX_ETH_PORT_STATUS_MODE_10_BIT;
  2491. if (mode_10_bit) {
  2492. ecmd->supported = SUPPORTED_10baseT_Half;
  2493. } else {
  2494. ecmd->supported = (SUPPORTED_10baseT_Half |
  2495. SUPPORTED_10baseT_Full |
  2496. SUPPORTED_100baseT_Half |
  2497. SUPPORTED_100baseT_Full |
  2498. SUPPORTED_1000baseT_Full |
  2499. (autoneg ? SUPPORTED_Autoneg : 0) |
  2500. SUPPORTED_TP);
  2501. auto_duplex = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_FOR_DUPLX);
  2502. auto_speed = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_SPEED_GMII);
  2503. ecmd->advertising = ADVERTISED_TP;
  2504. if (autoneg) {
  2505. ecmd->advertising |= ADVERTISED_Autoneg;
  2506. if (auto_duplex) {
  2507. half_duplex = 1;
  2508. full_duplex = 1;
  2509. } else {
  2510. if (pcs & MV643XX_ETH_SET_FULL_DUPLEX_MODE)
  2511. full_duplex = 1;
  2512. else
  2513. half_duplex = 1;
  2514. }
  2515. if (auto_speed) {
  2516. speed_10 = 1;
  2517. speed_100 = 1;
  2518. speed_1000 = 1;
  2519. } else {
  2520. if (pcs & MV643XX_ETH_SET_GMII_SPEED_TO_1000)
  2521. speed_1000 = 1;
  2522. else if (pcs & MV643XX_ETH_SET_MII_SPEED_TO_100)
  2523. speed_100 = 1;
  2524. else
  2525. speed_10 = 1;
  2526. }
  2527. if (speed_10 & half_duplex)
  2528. ecmd->advertising |= ADVERTISED_10baseT_Half;
  2529. if (speed_10 & full_duplex)
  2530. ecmd->advertising |= ADVERTISED_10baseT_Full;
  2531. if (speed_100 & half_duplex)
  2532. ecmd->advertising |= ADVERTISED_100baseT_Half;
  2533. if (speed_100 & full_duplex)
  2534. ecmd->advertising |= ADVERTISED_100baseT_Full;
  2535. if (speed_1000)
  2536. ecmd->advertising |= ADVERTISED_1000baseT_Full;
  2537. }
  2538. }
  2539. ecmd->port = PORT_TP;
  2540. ecmd->phy_address = ethernet_phy_get(port_num);
  2541. ecmd->transceiver = XCVR_EXTERNAL;
  2542. if (netif_carrier_ok(netdev)) {
  2543. if (mode_10_bit)
  2544. ecmd->speed = SPEED_10;
  2545. else {
  2546. if (psr & MV643XX_ETH_PORT_STATUS_GMII_1000)
  2547. ecmd->speed = SPEED_1000;
  2548. else if (psr & MV643XX_ETH_PORT_STATUS_MII_100)
  2549. ecmd->speed = SPEED_100;
  2550. else
  2551. ecmd->speed = SPEED_10;
  2552. }
  2553. if (psr & MV643XX_ETH_PORT_STATUS_FULL_DUPLEX)
  2554. ecmd->duplex = DUPLEX_FULL;
  2555. else
  2556. ecmd->duplex = DUPLEX_HALF;
  2557. } else {
  2558. ecmd->speed = -1;
  2559. ecmd->duplex = -1;
  2560. }
  2561. ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  2562. return 0;
  2563. }
  2564. static void
  2565. mv643xx_get_drvinfo(struct net_device *netdev,
  2566. struct ethtool_drvinfo *drvinfo)
  2567. {
  2568. strncpy(drvinfo->driver, mv643xx_driver_name, 32);
  2569. strncpy(drvinfo->version, mv643xx_driver_version, 32);
  2570. strncpy(drvinfo->fw_version, "N/A", 32);
  2571. strncpy(drvinfo->bus_info, "mv643xx", 32);
  2572. drvinfo->n_stats = MV643XX_STATS_LEN;
  2573. }
  2574. static int
  2575. mv643xx_get_stats_count(struct net_device *netdev)
  2576. {
  2577. return MV643XX_STATS_LEN;
  2578. }
  2579. static void
  2580. mv643xx_get_ethtool_stats(struct net_device *netdev,
  2581. struct ethtool_stats *stats, uint64_t *data)
  2582. {
  2583. struct mv643xx_private *mp = netdev->priv;
  2584. int i;
  2585. eth_update_mib_counters(mp);
  2586. for(i = 0; i < MV643XX_STATS_LEN; i++) {
  2587. char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
  2588. data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
  2589. sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
  2590. }
  2591. }
  2592. static void
  2593. mv643xx_get_strings(struct net_device *netdev, uint32_t stringset, uint8_t *data)
  2594. {
  2595. int i;
  2596. switch(stringset) {
  2597. case ETH_SS_STATS:
  2598. for (i=0; i < MV643XX_STATS_LEN; i++) {
  2599. memcpy(data + i * ETH_GSTRING_LEN,
  2600. mv643xx_gstrings_stats[i].stat_string,
  2601. ETH_GSTRING_LEN);
  2602. }
  2603. break;
  2604. }
  2605. }
  2606. static struct ethtool_ops mv643xx_ethtool_ops = {
  2607. .get_settings = mv643xx_get_settings,
  2608. .get_drvinfo = mv643xx_get_drvinfo,
  2609. .get_link = ethtool_op_get_link,
  2610. .get_sg = ethtool_op_get_sg,
  2611. .set_sg = ethtool_op_set_sg,
  2612. .get_strings = mv643xx_get_strings,
  2613. .get_stats_count = mv643xx_get_stats_count,
  2614. .get_ethtool_stats = mv643xx_get_ethtool_stats,
  2615. };
  2616. /************* End ethtool support *************************/