mv643xx_eth.c 91 KB

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