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