mv643xx_eth.c 79 KB

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