mv643xx_eth.c 92 KB

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