mv643xx_eth.c 92 KB

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