mv643xx_eth.c 91 KB

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