mv643xx_eth.c 89 KB

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