mv643xx_eth.c 91 KB

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