mv643xx_eth.c 92 KB

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