mv643xx_eth.c 92 KB

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