mv643xx_eth.c 80 KB

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