mv643xx_eth.c 85 KB

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