mv643xx_eth.c 80 KB

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