mv643xx_eth.c 85 KB

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