mv643xx_eth.c 91 KB

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