mv643xx_eth.c 81 KB

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