mv643xx_eth.c 89 KB

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