mv643xx_eth.c 91 KB

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