ks8695net.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * Micrel KS8695 (Centaur) Ethernet.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * Copyright 2008 Simtec Electronics
  15. * Daniel Silverstone <dsilvers@simtec.co.uk>
  16. * Vincent Sanders <vince@simtec.co.uk>
  17. */
  18. #include <linux/dma-mapping.h>
  19. #include <linux/module.h>
  20. #include <linux/ioport.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/crc32.h>
  27. #include <linux/mii.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/delay.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/irq.h>
  32. #include <linux/io.h>
  33. #include <linux/slab.h>
  34. #include <asm/irq.h>
  35. #include <mach/regs-switch.h>
  36. #include <mach/regs-misc.h>
  37. #include <asm/mach/irq.h>
  38. #include <mach/regs-irq.h>
  39. #include "ks8695net.h"
  40. #define MODULENAME "ks8695_ether"
  41. #define MODULEVERSION "1.02"
  42. /*
  43. * Transmit and device reset timeout, default 5 seconds.
  44. */
  45. static int watchdog = 5000;
  46. /* Hardware structures */
  47. /**
  48. * struct rx_ring_desc - Receive descriptor ring element
  49. * @status: The status of the descriptor element (E.g. who owns it)
  50. * @length: The number of bytes in the block pointed to by data_ptr
  51. * @data_ptr: The physical address of the data block to receive into
  52. * @next_desc: The physical address of the next descriptor element.
  53. */
  54. struct rx_ring_desc {
  55. __le32 status;
  56. __le32 length;
  57. __le32 data_ptr;
  58. __le32 next_desc;
  59. };
  60. /**
  61. * struct tx_ring_desc - Transmit descriptor ring element
  62. * @owner: Who owns the descriptor
  63. * @status: The number of bytes in the block pointed to by data_ptr
  64. * @data_ptr: The physical address of the data block to receive into
  65. * @next_desc: The physical address of the next descriptor element.
  66. */
  67. struct tx_ring_desc {
  68. __le32 owner;
  69. __le32 status;
  70. __le32 data_ptr;
  71. __le32 next_desc;
  72. };
  73. /**
  74. * struct ks8695_skbuff - sk_buff wrapper for rx/tx rings.
  75. * @skb: The buffer in the ring
  76. * @dma_ptr: The mapped DMA pointer of the buffer
  77. * @length: The number of bytes mapped to dma_ptr
  78. */
  79. struct ks8695_skbuff {
  80. struct sk_buff *skb;
  81. dma_addr_t dma_ptr;
  82. u32 length;
  83. };
  84. /* Private device structure */
  85. #define MAX_TX_DESC 8
  86. #define MAX_TX_DESC_MASK 0x7
  87. #define MAX_RX_DESC 16
  88. #define MAX_RX_DESC_MASK 0xf
  89. /*napi_weight have better more than rx DMA buffers*/
  90. #define NAPI_WEIGHT 64
  91. #define MAX_RXBUF_SIZE 0x700
  92. #define TX_RING_DMA_SIZE (sizeof(struct tx_ring_desc) * MAX_TX_DESC)
  93. #define RX_RING_DMA_SIZE (sizeof(struct rx_ring_desc) * MAX_RX_DESC)
  94. #define RING_DMA_SIZE (TX_RING_DMA_SIZE + RX_RING_DMA_SIZE)
  95. /**
  96. * enum ks8695_dtype - Device type
  97. * @KS8695_DTYPE_WAN: This device is a WAN interface
  98. * @KS8695_DTYPE_LAN: This device is a LAN interface
  99. * @KS8695_DTYPE_HPNA: This device is an HPNA interface
  100. */
  101. enum ks8695_dtype {
  102. KS8695_DTYPE_WAN,
  103. KS8695_DTYPE_LAN,
  104. KS8695_DTYPE_HPNA,
  105. };
  106. /**
  107. * struct ks8695_priv - Private data for the KS8695 Ethernet
  108. * @in_suspend: Flag to indicate if we're suspending/resuming
  109. * @ndev: The net_device for this interface
  110. * @dev: The platform device object for this interface
  111. * @dtype: The type of this device
  112. * @io_regs: The ioremapped registers for this interface
  113. * @napi : Add support NAPI for Rx
  114. * @rx_irq_name: The textual name of the RX IRQ from the platform data
  115. * @tx_irq_name: The textual name of the TX IRQ from the platform data
  116. * @link_irq_name: The textual name of the link IRQ from the
  117. * platform data if available
  118. * @rx_irq: The IRQ number for the RX IRQ
  119. * @tx_irq: The IRQ number for the TX IRQ
  120. * @link_irq: The IRQ number for the link IRQ if available
  121. * @regs_req: The resource request for the registers region
  122. * @phyiface_req: The resource request for the phy/switch region
  123. * if available
  124. * @phyiface_regs: The ioremapped registers for the phy/switch if available
  125. * @ring_base: The base pointer of the dma coherent memory for the rings
  126. * @ring_base_dma: The DMA mapped equivalent of ring_base
  127. * @tx_ring: The pointer in ring_base of the TX ring
  128. * @tx_ring_used: The number of slots in the TX ring which are occupied
  129. * @tx_ring_next_slot: The next slot to fill in the TX ring
  130. * @tx_ring_dma: The DMA mapped equivalent of tx_ring
  131. * @tx_buffers: The sk_buff mappings for the TX ring
  132. * @txq_lock: A lock to protect the tx_buffers tx_ring_used etc variables
  133. * @rx_ring: The pointer in ring_base of the RX ring
  134. * @rx_ring_dma: The DMA mapped equivalent of rx_ring
  135. * @rx_buffers: The sk_buff mappings for the RX ring
  136. * @next_rx_desc_read: The next RX descriptor to read from on IRQ
  137. * @rx_lock: A lock to protect Rx irq function
  138. * @msg_enable: The flags for which messages to emit
  139. */
  140. struct ks8695_priv {
  141. int in_suspend;
  142. struct net_device *ndev;
  143. struct device *dev;
  144. enum ks8695_dtype dtype;
  145. void __iomem *io_regs;
  146. struct napi_struct napi;
  147. const char *rx_irq_name, *tx_irq_name, *link_irq_name;
  148. int rx_irq, tx_irq, link_irq;
  149. struct resource *regs_req, *phyiface_req;
  150. void __iomem *phyiface_regs;
  151. void *ring_base;
  152. dma_addr_t ring_base_dma;
  153. struct tx_ring_desc *tx_ring;
  154. int tx_ring_used;
  155. int tx_ring_next_slot;
  156. dma_addr_t tx_ring_dma;
  157. struct ks8695_skbuff tx_buffers[MAX_TX_DESC];
  158. spinlock_t txq_lock;
  159. struct rx_ring_desc *rx_ring;
  160. dma_addr_t rx_ring_dma;
  161. struct ks8695_skbuff rx_buffers[MAX_RX_DESC];
  162. int next_rx_desc_read;
  163. spinlock_t rx_lock;
  164. int msg_enable;
  165. };
  166. /* Register access */
  167. /**
  168. * ks8695_readreg - Read from a KS8695 ethernet register
  169. * @ksp: The device to read from
  170. * @reg: The register to read
  171. */
  172. static inline u32
  173. ks8695_readreg(struct ks8695_priv *ksp, int reg)
  174. {
  175. return readl(ksp->io_regs + reg);
  176. }
  177. /**
  178. * ks8695_writereg - Write to a KS8695 ethernet register
  179. * @ksp: The device to write to
  180. * @reg: The register to write
  181. * @value: The value to write to the register
  182. */
  183. static inline void
  184. ks8695_writereg(struct ks8695_priv *ksp, int reg, u32 value)
  185. {
  186. writel(value, ksp->io_regs + reg);
  187. }
  188. /* Utility functions */
  189. /**
  190. * ks8695_port_type - Retrieve port-type as user-friendly string
  191. * @ksp: The device to return the type for
  192. *
  193. * Returns a string indicating which of the WAN, LAN or HPNA
  194. * ports this device is likely to represent.
  195. */
  196. static const char *
  197. ks8695_port_type(struct ks8695_priv *ksp)
  198. {
  199. switch (ksp->dtype) {
  200. case KS8695_DTYPE_LAN:
  201. return "LAN";
  202. case KS8695_DTYPE_WAN:
  203. return "WAN";
  204. case KS8695_DTYPE_HPNA:
  205. return "HPNA";
  206. }
  207. return "UNKNOWN";
  208. }
  209. /**
  210. * ks8695_update_mac - Update the MAC registers in the device
  211. * @ksp: The device to update
  212. *
  213. * Updates the MAC registers in the KS8695 device from the address in the
  214. * net_device structure associated with this interface.
  215. */
  216. static void
  217. ks8695_update_mac(struct ks8695_priv *ksp)
  218. {
  219. /* Update the HW with the MAC from the net_device */
  220. struct net_device *ndev = ksp->ndev;
  221. u32 machigh, maclow;
  222. maclow = ((ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
  223. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5] << 0));
  224. machigh = ((ndev->dev_addr[0] << 8) | (ndev->dev_addr[1] << 0));
  225. ks8695_writereg(ksp, KS8695_MAL, maclow);
  226. ks8695_writereg(ksp, KS8695_MAH, machigh);
  227. }
  228. /**
  229. * ks8695_refill_rxbuffers - Re-fill the RX buffer ring
  230. * @ksp: The device to refill
  231. *
  232. * Iterates the RX ring of the device looking for empty slots.
  233. * For each empty slot, we allocate and map a new SKB and give it
  234. * to the hardware.
  235. * This can be called from interrupt context safely.
  236. */
  237. static void
  238. ks8695_refill_rxbuffers(struct ks8695_priv *ksp)
  239. {
  240. /* Run around the RX ring, filling in any missing sk_buff's */
  241. int buff_n;
  242. for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
  243. if (!ksp->rx_buffers[buff_n].skb) {
  244. struct sk_buff *skb =
  245. netdev_alloc_skb(ksp->ndev, MAX_RXBUF_SIZE);
  246. dma_addr_t mapping;
  247. ksp->rx_buffers[buff_n].skb = skb;
  248. if (skb == NULL) {
  249. /* Failed to allocate one, perhaps
  250. * we'll try again later.
  251. */
  252. break;
  253. }
  254. mapping = dma_map_single(ksp->dev, skb->data,
  255. MAX_RXBUF_SIZE,
  256. DMA_FROM_DEVICE);
  257. if (unlikely(dma_mapping_error(ksp->dev, mapping))) {
  258. /* Failed to DMA map this SKB, try later */
  259. dev_kfree_skb_irq(skb);
  260. ksp->rx_buffers[buff_n].skb = NULL;
  261. break;
  262. }
  263. ksp->rx_buffers[buff_n].dma_ptr = mapping;
  264. ksp->rx_buffers[buff_n].length = MAX_RXBUF_SIZE;
  265. /* Record this into the DMA ring */
  266. ksp->rx_ring[buff_n].data_ptr = cpu_to_le32(mapping);
  267. ksp->rx_ring[buff_n].length =
  268. cpu_to_le32(MAX_RXBUF_SIZE);
  269. wmb();
  270. /* And give ownership over to the hardware */
  271. ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
  272. }
  273. }
  274. }
  275. /* Maximum number of multicast addresses which the KS8695 HW supports */
  276. #define KS8695_NR_ADDRESSES 16
  277. /**
  278. * ks8695_init_partial_multicast - Init the mcast addr registers
  279. * @ksp: The device to initialise
  280. * @addr: The multicast address list to use
  281. * @nr_addr: The number of addresses in the list
  282. *
  283. * This routine is a helper for ks8695_set_multicast - it writes
  284. * the additional-address registers in the KS8695 ethernet device
  285. * and cleans up any others left behind.
  286. */
  287. static void
  288. ks8695_init_partial_multicast(struct ks8695_priv *ksp,
  289. struct net_device *ndev)
  290. {
  291. u32 low, high;
  292. int i;
  293. struct netdev_hw_addr *ha;
  294. i = 0;
  295. netdev_for_each_mc_addr(ha, ndev) {
  296. /* Ran out of space in chip? */
  297. BUG_ON(i == KS8695_NR_ADDRESSES);
  298. low = (ha->addr[2] << 24) | (ha->addr[3] << 16) |
  299. (ha->addr[4] << 8) | (ha->addr[5]);
  300. high = (ha->addr[0] << 8) | (ha->addr[1]);
  301. ks8695_writereg(ksp, KS8695_AAL_(i), low);
  302. ks8695_writereg(ksp, KS8695_AAH_(i), AAH_E | high);
  303. i++;
  304. }
  305. /* Clear the remaining Additional Station Addresses */
  306. for (; i < KS8695_NR_ADDRESSES; i++) {
  307. ks8695_writereg(ksp, KS8695_AAL_(i), 0);
  308. ks8695_writereg(ksp, KS8695_AAH_(i), 0);
  309. }
  310. }
  311. /* Interrupt handling */
  312. /**
  313. * ks8695_tx_irq - Transmit IRQ handler
  314. * @irq: The IRQ which went off (ignored)
  315. * @dev_id: The net_device for the interrupt
  316. *
  317. * Process the TX ring, clearing out any transmitted slots.
  318. * Allows the net_device to pass us new packets once slots are
  319. * freed.
  320. */
  321. static irqreturn_t
  322. ks8695_tx_irq(int irq, void *dev_id)
  323. {
  324. struct net_device *ndev = (struct net_device *)dev_id;
  325. struct ks8695_priv *ksp = netdev_priv(ndev);
  326. int buff_n;
  327. for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
  328. if (ksp->tx_buffers[buff_n].skb &&
  329. !(ksp->tx_ring[buff_n].owner & cpu_to_le32(TDES_OWN))) {
  330. rmb();
  331. /* An SKB which is not owned by HW is present */
  332. /* Update the stats for the net_device */
  333. ndev->stats.tx_packets++;
  334. ndev->stats.tx_bytes += ksp->tx_buffers[buff_n].length;
  335. /* Free the packet from the ring */
  336. ksp->tx_ring[buff_n].data_ptr = 0;
  337. /* Free the sk_buff */
  338. dma_unmap_single(ksp->dev,
  339. ksp->tx_buffers[buff_n].dma_ptr,
  340. ksp->tx_buffers[buff_n].length,
  341. DMA_TO_DEVICE);
  342. dev_kfree_skb_irq(ksp->tx_buffers[buff_n].skb);
  343. ksp->tx_buffers[buff_n].skb = NULL;
  344. ksp->tx_ring_used--;
  345. }
  346. }
  347. netif_wake_queue(ndev);
  348. return IRQ_HANDLED;
  349. }
  350. /**
  351. * ks8695_get_rx_enable_bit - Get rx interrupt enable/status bit
  352. * @ksp: Private data for the KS8695 Ethernet
  353. *
  354. * For KS8695 document:
  355. * Interrupt Enable Register (offset 0xE204)
  356. * Bit29 : WAN MAC Receive Interrupt Enable
  357. * Bit16 : LAN MAC Receive Interrupt Enable
  358. * Interrupt Status Register (Offset 0xF208)
  359. * Bit29: WAN MAC Receive Status
  360. * Bit16: LAN MAC Receive Status
  361. * So, this Rx interrupt enable/status bit number is equal
  362. * as Rx IRQ number.
  363. */
  364. static inline u32 ks8695_get_rx_enable_bit(struct ks8695_priv *ksp)
  365. {
  366. return ksp->rx_irq;
  367. }
  368. /**
  369. * ks8695_rx_irq - Receive IRQ handler
  370. * @irq: The IRQ which went off (ignored)
  371. * @dev_id: The net_device for the interrupt
  372. *
  373. * Inform NAPI that packet reception needs to be scheduled
  374. */
  375. static irqreturn_t
  376. ks8695_rx_irq(int irq, void *dev_id)
  377. {
  378. struct net_device *ndev = (struct net_device *)dev_id;
  379. struct ks8695_priv *ksp = netdev_priv(ndev);
  380. spin_lock(&ksp->rx_lock);
  381. if (napi_schedule_prep(&ksp->napi)) {
  382. unsigned long status = readl(KS8695_IRQ_VA + KS8695_INTEN);
  383. unsigned long mask_bit = 1 << ks8695_get_rx_enable_bit(ksp);
  384. /*disable rx interrupt*/
  385. status &= ~mask_bit;
  386. writel(status , KS8695_IRQ_VA + KS8695_INTEN);
  387. __napi_schedule(&ksp->napi);
  388. }
  389. spin_unlock(&ksp->rx_lock);
  390. return IRQ_HANDLED;
  391. }
  392. /**
  393. * ks8695_rx - Receive packets called by NAPI poll method
  394. * @ksp: Private data for the KS8695 Ethernet
  395. * @budget: Number of packets allowed to process
  396. */
  397. static int ks8695_rx(struct ks8695_priv *ksp, int budget)
  398. {
  399. struct net_device *ndev = ksp->ndev;
  400. struct sk_buff *skb;
  401. int buff_n;
  402. u32 flags;
  403. int pktlen;
  404. int received = 0;
  405. buff_n = ksp->next_rx_desc_read;
  406. while (received < budget
  407. && ksp->rx_buffers[buff_n].skb
  408. && (!(ksp->rx_ring[buff_n].status &
  409. cpu_to_le32(RDES_OWN)))) {
  410. rmb();
  411. flags = le32_to_cpu(ksp->rx_ring[buff_n].status);
  412. /* Found an SKB which we own, this means we
  413. * received a packet
  414. */
  415. if ((flags & (RDES_FS | RDES_LS)) !=
  416. (RDES_FS | RDES_LS)) {
  417. /* This packet is not the first and
  418. * the last segment. Therefore it is
  419. * a "spanning" packet and we can't
  420. * handle it
  421. */
  422. goto rx_failure;
  423. }
  424. if (flags & (RDES_ES | RDES_RE)) {
  425. /* It's an error packet */
  426. ndev->stats.rx_errors++;
  427. if (flags & RDES_TL)
  428. ndev->stats.rx_length_errors++;
  429. if (flags & RDES_RF)
  430. ndev->stats.rx_length_errors++;
  431. if (flags & RDES_CE)
  432. ndev->stats.rx_crc_errors++;
  433. if (flags & RDES_RE)
  434. ndev->stats.rx_missed_errors++;
  435. goto rx_failure;
  436. }
  437. pktlen = flags & RDES_FLEN;
  438. pktlen -= 4; /* Drop the CRC */
  439. /* Retrieve the sk_buff */
  440. skb = ksp->rx_buffers[buff_n].skb;
  441. /* Clear it from the ring */
  442. ksp->rx_buffers[buff_n].skb = NULL;
  443. ksp->rx_ring[buff_n].data_ptr = 0;
  444. /* Unmap the SKB */
  445. dma_unmap_single(ksp->dev,
  446. ksp->rx_buffers[buff_n].dma_ptr,
  447. ksp->rx_buffers[buff_n].length,
  448. DMA_FROM_DEVICE);
  449. /* Relinquish the SKB to the network layer */
  450. skb_put(skb, pktlen);
  451. skb->protocol = eth_type_trans(skb, ndev);
  452. napi_gro_receive(&ksp->napi, skb);
  453. /* Record stats */
  454. ndev->stats.rx_packets++;
  455. ndev->stats.rx_bytes += pktlen;
  456. goto rx_finished;
  457. rx_failure:
  458. /* This ring entry is an error, but we can
  459. * re-use the skb
  460. */
  461. /* Give the ring entry back to the hardware */
  462. ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
  463. rx_finished:
  464. received++;
  465. buff_n = (buff_n + 1) & MAX_RX_DESC_MASK;
  466. }
  467. /* And note which RX descriptor we last did */
  468. ksp->next_rx_desc_read = buff_n;
  469. /* And refill the buffers */
  470. ks8695_refill_rxbuffers(ksp);
  471. /* Kick the RX DMA engine, in case it became suspended */
  472. ks8695_writereg(ksp, KS8695_DRSC, 0);
  473. return received;
  474. }
  475. /**
  476. * ks8695_poll - Receive packet by NAPI poll method
  477. * @ksp: Private data for the KS8695 Ethernet
  478. * @budget: The remaining number packets for network subsystem
  479. *
  480. * Invoked by the network core when it requests for new
  481. * packets from the driver
  482. */
  483. static int ks8695_poll(struct napi_struct *napi, int budget)
  484. {
  485. struct ks8695_priv *ksp = container_of(napi, struct ks8695_priv, napi);
  486. unsigned long isr = readl(KS8695_IRQ_VA + KS8695_INTEN);
  487. unsigned long mask_bit = 1 << ks8695_get_rx_enable_bit(ksp);
  488. int work_done;
  489. work_done = ks8695_rx(ksp, budget);
  490. if (work_done < budget && napi_complete_done(napi, work_done)) {
  491. unsigned long flags;
  492. spin_lock_irqsave(&ksp->rx_lock, flags);
  493. /* enable rx interrupt */
  494. writel(isr | mask_bit, KS8695_IRQ_VA + KS8695_INTEN);
  495. spin_unlock_irqrestore(&ksp->rx_lock, flags);
  496. }
  497. return work_done;
  498. }
  499. /**
  500. * ks8695_link_irq - Link change IRQ handler
  501. * @irq: The IRQ which went off (ignored)
  502. * @dev_id: The net_device for the interrupt
  503. *
  504. * The WAN interface can generate an IRQ when the link changes,
  505. * report this to the net layer and the user.
  506. */
  507. static irqreturn_t
  508. ks8695_link_irq(int irq, void *dev_id)
  509. {
  510. struct net_device *ndev = (struct net_device *)dev_id;
  511. struct ks8695_priv *ksp = netdev_priv(ndev);
  512. u32 ctrl;
  513. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  514. if (ctrl & WMC_WLS) {
  515. netif_carrier_on(ndev);
  516. if (netif_msg_link(ksp))
  517. dev_info(ksp->dev,
  518. "%s: Link is now up (10%sMbps/%s-duplex)\n",
  519. ndev->name,
  520. (ctrl & WMC_WSS) ? "0" : "",
  521. (ctrl & WMC_WDS) ? "Full" : "Half");
  522. } else {
  523. netif_carrier_off(ndev);
  524. if (netif_msg_link(ksp))
  525. dev_info(ksp->dev, "%s: Link is now down.\n",
  526. ndev->name);
  527. }
  528. return IRQ_HANDLED;
  529. }
  530. /* KS8695 Device functions */
  531. /**
  532. * ks8695_reset - Reset a KS8695 ethernet interface
  533. * @ksp: The interface to reset
  534. *
  535. * Perform an engine reset of the interface and re-program it
  536. * with sensible defaults.
  537. */
  538. static void
  539. ks8695_reset(struct ks8695_priv *ksp)
  540. {
  541. int reset_timeout = watchdog;
  542. /* Issue the reset via the TX DMA control register */
  543. ks8695_writereg(ksp, KS8695_DTXC, DTXC_TRST);
  544. while (reset_timeout--) {
  545. if (!(ks8695_readreg(ksp, KS8695_DTXC) & DTXC_TRST))
  546. break;
  547. msleep(1);
  548. }
  549. if (reset_timeout < 0) {
  550. dev_crit(ksp->dev,
  551. "Timeout waiting for DMA engines to reset\n");
  552. /* And blithely carry on */
  553. }
  554. /* Definitely wait long enough before attempting to program
  555. * the engines
  556. */
  557. msleep(10);
  558. /* RX: unicast and broadcast */
  559. ks8695_writereg(ksp, KS8695_DRXC, DRXC_RU | DRXC_RB);
  560. /* TX: pad and add CRC */
  561. ks8695_writereg(ksp, KS8695_DTXC, DTXC_TEP | DTXC_TAC);
  562. }
  563. /**
  564. * ks8695_shutdown - Shut down a KS8695 ethernet interface
  565. * @ksp: The interface to shut down
  566. *
  567. * This disables packet RX/TX, cleans up IRQs, drains the rings,
  568. * and basically places the interface into a clean shutdown
  569. * state.
  570. */
  571. static void
  572. ks8695_shutdown(struct ks8695_priv *ksp)
  573. {
  574. u32 ctrl;
  575. int buff_n;
  576. /* Disable packet transmission */
  577. ctrl = ks8695_readreg(ksp, KS8695_DTXC);
  578. ks8695_writereg(ksp, KS8695_DTXC, ctrl & ~DTXC_TE);
  579. /* Disable packet reception */
  580. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  581. ks8695_writereg(ksp, KS8695_DRXC, ctrl & ~DRXC_RE);
  582. /* Release the IRQs */
  583. free_irq(ksp->rx_irq, ksp->ndev);
  584. free_irq(ksp->tx_irq, ksp->ndev);
  585. if (ksp->link_irq != -1)
  586. free_irq(ksp->link_irq, ksp->ndev);
  587. /* Throw away any pending TX packets */
  588. for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
  589. if (ksp->tx_buffers[buff_n].skb) {
  590. /* Remove this SKB from the TX ring */
  591. ksp->tx_ring[buff_n].owner = 0;
  592. ksp->tx_ring[buff_n].status = 0;
  593. ksp->tx_ring[buff_n].data_ptr = 0;
  594. /* Unmap and bin this SKB */
  595. dma_unmap_single(ksp->dev,
  596. ksp->tx_buffers[buff_n].dma_ptr,
  597. ksp->tx_buffers[buff_n].length,
  598. DMA_TO_DEVICE);
  599. dev_kfree_skb_irq(ksp->tx_buffers[buff_n].skb);
  600. ksp->tx_buffers[buff_n].skb = NULL;
  601. }
  602. }
  603. /* Purge the RX buffers */
  604. for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
  605. if (ksp->rx_buffers[buff_n].skb) {
  606. /* Remove the SKB from the RX ring */
  607. ksp->rx_ring[buff_n].status = 0;
  608. ksp->rx_ring[buff_n].data_ptr = 0;
  609. /* Unmap and bin the SKB */
  610. dma_unmap_single(ksp->dev,
  611. ksp->rx_buffers[buff_n].dma_ptr,
  612. ksp->rx_buffers[buff_n].length,
  613. DMA_FROM_DEVICE);
  614. dev_kfree_skb_irq(ksp->rx_buffers[buff_n].skb);
  615. ksp->rx_buffers[buff_n].skb = NULL;
  616. }
  617. }
  618. }
  619. /**
  620. * ks8695_setup_irq - IRQ setup helper function
  621. * @irq: The IRQ number to claim
  622. * @irq_name: The name to give the IRQ claimant
  623. * @handler: The function to call to handle the IRQ
  624. * @ndev: The net_device to pass in as the dev_id argument to the handler
  625. *
  626. * Return 0 on success.
  627. */
  628. static int
  629. ks8695_setup_irq(int irq, const char *irq_name,
  630. irq_handler_t handler, struct net_device *ndev)
  631. {
  632. int ret;
  633. ret = request_irq(irq, handler, IRQF_SHARED, irq_name, ndev);
  634. if (ret) {
  635. dev_err(&ndev->dev, "failure to request IRQ %d\n", irq);
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. /**
  641. * ks8695_init_net - Initialise a KS8695 ethernet interface
  642. * @ksp: The interface to initialise
  643. *
  644. * This routine fills the RX ring, initialises the DMA engines,
  645. * allocates the IRQs and then starts the packet TX and RX
  646. * engines.
  647. */
  648. static int
  649. ks8695_init_net(struct ks8695_priv *ksp)
  650. {
  651. int ret;
  652. u32 ctrl;
  653. ks8695_refill_rxbuffers(ksp);
  654. /* Initialise the DMA engines */
  655. ks8695_writereg(ksp, KS8695_RDLB, (u32) ksp->rx_ring_dma);
  656. ks8695_writereg(ksp, KS8695_TDLB, (u32) ksp->tx_ring_dma);
  657. /* Request the IRQs */
  658. ret = ks8695_setup_irq(ksp->rx_irq, ksp->rx_irq_name,
  659. ks8695_rx_irq, ksp->ndev);
  660. if (ret)
  661. return ret;
  662. ret = ks8695_setup_irq(ksp->tx_irq, ksp->tx_irq_name,
  663. ks8695_tx_irq, ksp->ndev);
  664. if (ret)
  665. return ret;
  666. if (ksp->link_irq != -1) {
  667. ret = ks8695_setup_irq(ksp->link_irq, ksp->link_irq_name,
  668. ks8695_link_irq, ksp->ndev);
  669. if (ret)
  670. return ret;
  671. }
  672. /* Set up the ring indices */
  673. ksp->next_rx_desc_read = 0;
  674. ksp->tx_ring_next_slot = 0;
  675. ksp->tx_ring_used = 0;
  676. /* Bring up transmission */
  677. ctrl = ks8695_readreg(ksp, KS8695_DTXC);
  678. /* Enable packet transmission */
  679. ks8695_writereg(ksp, KS8695_DTXC, ctrl | DTXC_TE);
  680. /* Bring up the reception */
  681. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  682. /* Enable packet reception */
  683. ks8695_writereg(ksp, KS8695_DRXC, ctrl | DRXC_RE);
  684. /* And start the DMA engine */
  685. ks8695_writereg(ksp, KS8695_DRSC, 0);
  686. /* All done */
  687. return 0;
  688. }
  689. /**
  690. * ks8695_release_device - HW resource release for KS8695 e-net
  691. * @ksp: The device to be freed
  692. *
  693. * This unallocates io memory regions, dma-coherent regions etc
  694. * which were allocated in ks8695_probe.
  695. */
  696. static void
  697. ks8695_release_device(struct ks8695_priv *ksp)
  698. {
  699. /* Unmap the registers */
  700. iounmap(ksp->io_regs);
  701. if (ksp->phyiface_regs)
  702. iounmap(ksp->phyiface_regs);
  703. /* And release the request */
  704. release_resource(ksp->regs_req);
  705. kfree(ksp->regs_req);
  706. if (ksp->phyiface_req) {
  707. release_resource(ksp->phyiface_req);
  708. kfree(ksp->phyiface_req);
  709. }
  710. /* Free the ring buffers */
  711. dma_free_coherent(ksp->dev, RING_DMA_SIZE,
  712. ksp->ring_base, ksp->ring_base_dma);
  713. }
  714. /* Ethtool support */
  715. /**
  716. * ks8695_get_msglevel - Get the messages enabled for emission
  717. * @ndev: The network device to read from
  718. */
  719. static u32
  720. ks8695_get_msglevel(struct net_device *ndev)
  721. {
  722. struct ks8695_priv *ksp = netdev_priv(ndev);
  723. return ksp->msg_enable;
  724. }
  725. /**
  726. * ks8695_set_msglevel - Set the messages enabled for emission
  727. * @ndev: The network device to configure
  728. * @value: The messages to set for emission
  729. */
  730. static void
  731. ks8695_set_msglevel(struct net_device *ndev, u32 value)
  732. {
  733. struct ks8695_priv *ksp = netdev_priv(ndev);
  734. ksp->msg_enable = value;
  735. }
  736. /**
  737. * ks8695_wan_get_link_ksettings - Get device-specific settings.
  738. * @ndev: The network device to read settings from
  739. * @cmd: The ethtool structure to read into
  740. */
  741. static int
  742. ks8695_wan_get_link_ksettings(struct net_device *ndev,
  743. struct ethtool_link_ksettings *cmd)
  744. {
  745. struct ks8695_priv *ksp = netdev_priv(ndev);
  746. u32 ctrl;
  747. u32 supported, advertising;
  748. /* All ports on the KS8695 support these... */
  749. supported = (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  750. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  751. SUPPORTED_TP | SUPPORTED_MII);
  752. advertising = ADVERTISED_TP | ADVERTISED_MII;
  753. cmd->base.port = PORT_MII;
  754. supported |= (SUPPORTED_Autoneg | SUPPORTED_Pause);
  755. cmd->base.phy_address = 0;
  756. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  757. if ((ctrl & WMC_WAND) == 0) {
  758. /* auto-negotiation is enabled */
  759. advertising |= ADVERTISED_Autoneg;
  760. if (ctrl & WMC_WANA100F)
  761. advertising |= ADVERTISED_100baseT_Full;
  762. if (ctrl & WMC_WANA100H)
  763. advertising |= ADVERTISED_100baseT_Half;
  764. if (ctrl & WMC_WANA10F)
  765. advertising |= ADVERTISED_10baseT_Full;
  766. if (ctrl & WMC_WANA10H)
  767. advertising |= ADVERTISED_10baseT_Half;
  768. if (ctrl & WMC_WANAP)
  769. advertising |= ADVERTISED_Pause;
  770. cmd->base.autoneg = AUTONEG_ENABLE;
  771. cmd->base.speed = (ctrl & WMC_WSS) ? SPEED_100 : SPEED_10;
  772. cmd->base.duplex = (ctrl & WMC_WDS) ?
  773. DUPLEX_FULL : DUPLEX_HALF;
  774. } else {
  775. /* auto-negotiation is disabled */
  776. cmd->base.autoneg = AUTONEG_DISABLE;
  777. cmd->base.speed = (ctrl & WMC_WANF100) ?
  778. SPEED_100 : SPEED_10;
  779. cmd->base.duplex = (ctrl & WMC_WANFF) ?
  780. DUPLEX_FULL : DUPLEX_HALF;
  781. }
  782. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  783. supported);
  784. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  785. advertising);
  786. return 0;
  787. }
  788. /**
  789. * ks8695_wan_set_link_ksettings - Set device-specific settings.
  790. * @ndev: The network device to configure
  791. * @cmd: The settings to configure
  792. */
  793. static int
  794. ks8695_wan_set_link_ksettings(struct net_device *ndev,
  795. const struct ethtool_link_ksettings *cmd)
  796. {
  797. struct ks8695_priv *ksp = netdev_priv(ndev);
  798. u32 ctrl;
  799. u32 advertising;
  800. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  801. cmd->link_modes.advertising);
  802. if ((cmd->base.speed != SPEED_10) && (cmd->base.speed != SPEED_100))
  803. return -EINVAL;
  804. if ((cmd->base.duplex != DUPLEX_HALF) &&
  805. (cmd->base.duplex != DUPLEX_FULL))
  806. return -EINVAL;
  807. if (cmd->base.port != PORT_MII)
  808. return -EINVAL;
  809. if ((cmd->base.autoneg != AUTONEG_DISABLE) &&
  810. (cmd->base.autoneg != AUTONEG_ENABLE))
  811. return -EINVAL;
  812. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  813. if ((advertising & (ADVERTISED_10baseT_Half |
  814. ADVERTISED_10baseT_Full |
  815. ADVERTISED_100baseT_Half |
  816. ADVERTISED_100baseT_Full)) == 0)
  817. return -EINVAL;
  818. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  819. ctrl &= ~(WMC_WAND | WMC_WANA100F | WMC_WANA100H |
  820. WMC_WANA10F | WMC_WANA10H);
  821. if (advertising & ADVERTISED_100baseT_Full)
  822. ctrl |= WMC_WANA100F;
  823. if (advertising & ADVERTISED_100baseT_Half)
  824. ctrl |= WMC_WANA100H;
  825. if (advertising & ADVERTISED_10baseT_Full)
  826. ctrl |= WMC_WANA10F;
  827. if (advertising & ADVERTISED_10baseT_Half)
  828. ctrl |= WMC_WANA10H;
  829. /* force a re-negotiation */
  830. ctrl |= WMC_WANR;
  831. writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
  832. } else {
  833. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  834. /* disable auto-negotiation */
  835. ctrl |= WMC_WAND;
  836. ctrl &= ~(WMC_WANF100 | WMC_WANFF);
  837. if (cmd->base.speed == SPEED_100)
  838. ctrl |= WMC_WANF100;
  839. if (cmd->base.duplex == DUPLEX_FULL)
  840. ctrl |= WMC_WANFF;
  841. writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
  842. }
  843. return 0;
  844. }
  845. /**
  846. * ks8695_wan_nwayreset - Restart the autonegotiation on the port.
  847. * @ndev: The network device to restart autoneotiation on
  848. */
  849. static int
  850. ks8695_wan_nwayreset(struct net_device *ndev)
  851. {
  852. struct ks8695_priv *ksp = netdev_priv(ndev);
  853. u32 ctrl;
  854. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  855. if ((ctrl & WMC_WAND) == 0)
  856. writel(ctrl | WMC_WANR,
  857. ksp->phyiface_regs + KS8695_WMC);
  858. else
  859. /* auto-negotiation not enabled */
  860. return -EINVAL;
  861. return 0;
  862. }
  863. /**
  864. * ks8695_wan_get_pause - Retrieve network pause/flow-control advertising
  865. * @ndev: The device to retrieve settings from
  866. * @param: The structure to fill out with the information
  867. */
  868. static void
  869. ks8695_wan_get_pause(struct net_device *ndev, struct ethtool_pauseparam *param)
  870. {
  871. struct ks8695_priv *ksp = netdev_priv(ndev);
  872. u32 ctrl;
  873. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  874. /* advertise Pause */
  875. param->autoneg = (ctrl & WMC_WANAP);
  876. /* current Rx Flow-control */
  877. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  878. param->rx_pause = (ctrl & DRXC_RFCE);
  879. /* current Tx Flow-control */
  880. ctrl = ks8695_readreg(ksp, KS8695_DTXC);
  881. param->tx_pause = (ctrl & DTXC_TFCE);
  882. }
  883. /**
  884. * ks8695_get_drvinfo - Retrieve driver information
  885. * @ndev: The network device to retrieve info about
  886. * @info: The info structure to fill out.
  887. */
  888. static void
  889. ks8695_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
  890. {
  891. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  892. strlcpy(info->version, MODULEVERSION, sizeof(info->version));
  893. strlcpy(info->bus_info, dev_name(ndev->dev.parent),
  894. sizeof(info->bus_info));
  895. }
  896. static const struct ethtool_ops ks8695_ethtool_ops = {
  897. .get_msglevel = ks8695_get_msglevel,
  898. .set_msglevel = ks8695_set_msglevel,
  899. .get_drvinfo = ks8695_get_drvinfo,
  900. };
  901. static const struct ethtool_ops ks8695_wan_ethtool_ops = {
  902. .get_msglevel = ks8695_get_msglevel,
  903. .set_msglevel = ks8695_set_msglevel,
  904. .nway_reset = ks8695_wan_nwayreset,
  905. .get_link = ethtool_op_get_link,
  906. .get_pauseparam = ks8695_wan_get_pause,
  907. .get_drvinfo = ks8695_get_drvinfo,
  908. .get_link_ksettings = ks8695_wan_get_link_ksettings,
  909. .set_link_ksettings = ks8695_wan_set_link_ksettings,
  910. };
  911. /* Network device interface functions */
  912. /**
  913. * ks8695_set_mac - Update MAC in net dev and HW
  914. * @ndev: The network device to update
  915. * @addr: The new MAC address to set
  916. */
  917. static int
  918. ks8695_set_mac(struct net_device *ndev, void *addr)
  919. {
  920. struct ks8695_priv *ksp = netdev_priv(ndev);
  921. struct sockaddr *address = addr;
  922. if (!is_valid_ether_addr(address->sa_data))
  923. return -EADDRNOTAVAIL;
  924. memcpy(ndev->dev_addr, address->sa_data, ndev->addr_len);
  925. ks8695_update_mac(ksp);
  926. dev_dbg(ksp->dev, "%s: Updated MAC address to %pM\n",
  927. ndev->name, ndev->dev_addr);
  928. return 0;
  929. }
  930. /**
  931. * ks8695_set_multicast - Set up the multicast behaviour of the interface
  932. * @ndev: The net_device to configure
  933. *
  934. * This routine, called by the net layer, configures promiscuity
  935. * and multicast reception behaviour for the interface.
  936. */
  937. static void
  938. ks8695_set_multicast(struct net_device *ndev)
  939. {
  940. struct ks8695_priv *ksp = netdev_priv(ndev);
  941. u32 ctrl;
  942. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  943. if (ndev->flags & IFF_PROMISC) {
  944. /* enable promiscuous mode */
  945. ctrl |= DRXC_RA;
  946. } else if (ndev->flags & ~IFF_PROMISC) {
  947. /* disable promiscuous mode */
  948. ctrl &= ~DRXC_RA;
  949. }
  950. if (ndev->flags & IFF_ALLMULTI) {
  951. /* enable all multicast mode */
  952. ctrl |= DRXC_RM;
  953. } else if (netdev_mc_count(ndev) > KS8695_NR_ADDRESSES) {
  954. /* more specific multicast addresses than can be
  955. * handled in hardware
  956. */
  957. ctrl |= DRXC_RM;
  958. } else {
  959. /* enable specific multicasts */
  960. ctrl &= ~DRXC_RM;
  961. ks8695_init_partial_multicast(ksp, ndev);
  962. }
  963. ks8695_writereg(ksp, KS8695_DRXC, ctrl);
  964. }
  965. /**
  966. * ks8695_timeout - Handle a network tx/rx timeout.
  967. * @ndev: The net_device which timed out.
  968. *
  969. * A network transaction timed out, reset the device.
  970. */
  971. static void
  972. ks8695_timeout(struct net_device *ndev)
  973. {
  974. struct ks8695_priv *ksp = netdev_priv(ndev);
  975. netif_stop_queue(ndev);
  976. ks8695_shutdown(ksp);
  977. ks8695_reset(ksp);
  978. ks8695_update_mac(ksp);
  979. /* We ignore the return from this since it managed to init
  980. * before it probably will be okay to init again.
  981. */
  982. ks8695_init_net(ksp);
  983. /* Reconfigure promiscuity etc */
  984. ks8695_set_multicast(ndev);
  985. /* And start the TX queue once more */
  986. netif_start_queue(ndev);
  987. }
  988. /**
  989. * ks8695_start_xmit - Start a packet transmission
  990. * @skb: The packet to transmit
  991. * @ndev: The network device to send the packet on
  992. *
  993. * This routine, called by the net layer, takes ownership of the
  994. * sk_buff and adds it to the TX ring. It then kicks the TX DMA
  995. * engine to ensure transmission begins.
  996. */
  997. static netdev_tx_t
  998. ks8695_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  999. {
  1000. struct ks8695_priv *ksp = netdev_priv(ndev);
  1001. int buff_n;
  1002. dma_addr_t dmap;
  1003. spin_lock_irq(&ksp->txq_lock);
  1004. if (ksp->tx_ring_used == MAX_TX_DESC) {
  1005. /* Somehow we got entered when we have no room */
  1006. spin_unlock_irq(&ksp->txq_lock);
  1007. return NETDEV_TX_BUSY;
  1008. }
  1009. buff_n = ksp->tx_ring_next_slot;
  1010. BUG_ON(ksp->tx_buffers[buff_n].skb);
  1011. dmap = dma_map_single(ksp->dev, skb->data, skb->len, DMA_TO_DEVICE);
  1012. if (unlikely(dma_mapping_error(ksp->dev, dmap))) {
  1013. /* Failed to DMA map this SKB, give it back for now */
  1014. spin_unlock_irq(&ksp->txq_lock);
  1015. dev_dbg(ksp->dev, "%s: Could not map DMA memory for "\
  1016. "transmission, trying later\n", ndev->name);
  1017. return NETDEV_TX_BUSY;
  1018. }
  1019. ksp->tx_buffers[buff_n].dma_ptr = dmap;
  1020. /* Mapped okay, store the buffer pointer and length for later */
  1021. ksp->tx_buffers[buff_n].skb = skb;
  1022. ksp->tx_buffers[buff_n].length = skb->len;
  1023. /* Fill out the TX descriptor */
  1024. ksp->tx_ring[buff_n].data_ptr =
  1025. cpu_to_le32(ksp->tx_buffers[buff_n].dma_ptr);
  1026. ksp->tx_ring[buff_n].status =
  1027. cpu_to_le32(TDES_IC | TDES_FS | TDES_LS |
  1028. (skb->len & TDES_TBS));
  1029. wmb();
  1030. /* Hand it over to the hardware */
  1031. ksp->tx_ring[buff_n].owner = cpu_to_le32(TDES_OWN);
  1032. if (++ksp->tx_ring_used == MAX_TX_DESC)
  1033. netif_stop_queue(ndev);
  1034. /* Kick the TX DMA in case it decided to go IDLE */
  1035. ks8695_writereg(ksp, KS8695_DTSC, 0);
  1036. /* And update the next ring slot */
  1037. ksp->tx_ring_next_slot = (buff_n + 1) & MAX_TX_DESC_MASK;
  1038. spin_unlock_irq(&ksp->txq_lock);
  1039. return NETDEV_TX_OK;
  1040. }
  1041. /**
  1042. * ks8695_stop - Stop (shutdown) a KS8695 ethernet interface
  1043. * @ndev: The net_device to stop
  1044. *
  1045. * This disables the TX queue and cleans up a KS8695 ethernet
  1046. * device.
  1047. */
  1048. static int
  1049. ks8695_stop(struct net_device *ndev)
  1050. {
  1051. struct ks8695_priv *ksp = netdev_priv(ndev);
  1052. netif_stop_queue(ndev);
  1053. napi_disable(&ksp->napi);
  1054. ks8695_shutdown(ksp);
  1055. return 0;
  1056. }
  1057. /**
  1058. * ks8695_open - Open (bring up) a KS8695 ethernet interface
  1059. * @ndev: The net_device to open
  1060. *
  1061. * This resets, configures the MAC, initialises the RX ring and
  1062. * DMA engines and starts the TX queue for a KS8695 ethernet
  1063. * device.
  1064. */
  1065. static int
  1066. ks8695_open(struct net_device *ndev)
  1067. {
  1068. struct ks8695_priv *ksp = netdev_priv(ndev);
  1069. int ret;
  1070. ks8695_reset(ksp);
  1071. ks8695_update_mac(ksp);
  1072. ret = ks8695_init_net(ksp);
  1073. if (ret) {
  1074. ks8695_shutdown(ksp);
  1075. return ret;
  1076. }
  1077. napi_enable(&ksp->napi);
  1078. netif_start_queue(ndev);
  1079. return 0;
  1080. }
  1081. /* Platform device driver */
  1082. /**
  1083. * ks8695_init_switch - Init LAN switch to known good defaults.
  1084. * @ksp: The device to initialise
  1085. *
  1086. * This initialises the LAN switch in the KS8695 to a known-good
  1087. * set of defaults.
  1088. */
  1089. static void
  1090. ks8695_init_switch(struct ks8695_priv *ksp)
  1091. {
  1092. u32 ctrl;
  1093. /* Default value for SEC0 according to datasheet */
  1094. ctrl = 0x40819e00;
  1095. /* LED0 = Speed LED1 = Link/Activity */
  1096. ctrl &= ~(SEC0_LLED1S | SEC0_LLED0S);
  1097. ctrl |= (LLED0S_LINK | LLED1S_LINK_ACTIVITY);
  1098. /* Enable Switch */
  1099. ctrl |= SEC0_ENABLE;
  1100. writel(ctrl, ksp->phyiface_regs + KS8695_SEC0);
  1101. /* Defaults for SEC1 */
  1102. writel(0x9400100, ksp->phyiface_regs + KS8695_SEC1);
  1103. }
  1104. /**
  1105. * ks8695_init_wan_phy - Initialise the WAN PHY to sensible defaults
  1106. * @ksp: The device to initialise
  1107. *
  1108. * This initialises a KS8695's WAN phy to sensible values for
  1109. * autonegotiation etc.
  1110. */
  1111. static void
  1112. ks8695_init_wan_phy(struct ks8695_priv *ksp)
  1113. {
  1114. u32 ctrl;
  1115. /* Support auto-negotiation */
  1116. ctrl = (WMC_WANAP | WMC_WANA100F | WMC_WANA100H |
  1117. WMC_WANA10F | WMC_WANA10H);
  1118. /* LED0 = Activity , LED1 = Link */
  1119. ctrl |= (WLED0S_ACTIVITY | WLED1S_LINK);
  1120. /* Restart Auto-negotiation */
  1121. ctrl |= WMC_WANR;
  1122. writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
  1123. writel(0, ksp->phyiface_regs + KS8695_WPPM);
  1124. writel(0, ksp->phyiface_regs + KS8695_PPS);
  1125. }
  1126. static const struct net_device_ops ks8695_netdev_ops = {
  1127. .ndo_open = ks8695_open,
  1128. .ndo_stop = ks8695_stop,
  1129. .ndo_start_xmit = ks8695_start_xmit,
  1130. .ndo_tx_timeout = ks8695_timeout,
  1131. .ndo_set_mac_address = ks8695_set_mac,
  1132. .ndo_validate_addr = eth_validate_addr,
  1133. .ndo_set_rx_mode = ks8695_set_multicast,
  1134. };
  1135. /**
  1136. * ks8695_probe - Probe and initialise a KS8695 ethernet interface
  1137. * @pdev: The platform device to probe
  1138. *
  1139. * Initialise a KS8695 ethernet device from platform data.
  1140. *
  1141. * This driver requires at least one IORESOURCE_MEM for the
  1142. * registers and two IORESOURCE_IRQ for the RX and TX IRQs
  1143. * respectively. It can optionally take an additional
  1144. * IORESOURCE_MEM for the switch or phy in the case of the lan or
  1145. * wan ports, and an IORESOURCE_IRQ for the link IRQ for the wan
  1146. * port.
  1147. */
  1148. static int
  1149. ks8695_probe(struct platform_device *pdev)
  1150. {
  1151. struct ks8695_priv *ksp;
  1152. struct net_device *ndev;
  1153. struct resource *regs_res, *phyiface_res;
  1154. struct resource *rxirq_res, *txirq_res, *linkirq_res;
  1155. int ret = 0;
  1156. int buff_n;
  1157. bool inv_mac_addr = false;
  1158. u32 machigh, maclow;
  1159. /* Initialise a net_device */
  1160. ndev = alloc_etherdev(sizeof(struct ks8695_priv));
  1161. if (!ndev)
  1162. return -ENOMEM;
  1163. SET_NETDEV_DEV(ndev, &pdev->dev);
  1164. dev_dbg(&pdev->dev, "ks8695_probe() called\n");
  1165. /* Configure our private structure a little */
  1166. ksp = netdev_priv(ndev);
  1167. ksp->dev = &pdev->dev;
  1168. ksp->ndev = ndev;
  1169. ksp->msg_enable = NETIF_MSG_LINK;
  1170. /* Retrieve resources */
  1171. regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1172. phyiface_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1173. rxirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1174. txirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  1175. linkirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
  1176. if (!(regs_res && rxirq_res && txirq_res)) {
  1177. dev_err(ksp->dev, "insufficient resources\n");
  1178. ret = -ENOENT;
  1179. goto failure;
  1180. }
  1181. ksp->regs_req = request_mem_region(regs_res->start,
  1182. resource_size(regs_res),
  1183. pdev->name);
  1184. if (!ksp->regs_req) {
  1185. dev_err(ksp->dev, "cannot claim register space\n");
  1186. ret = -EIO;
  1187. goto failure;
  1188. }
  1189. ksp->io_regs = ioremap(regs_res->start, resource_size(regs_res));
  1190. if (!ksp->io_regs) {
  1191. dev_err(ksp->dev, "failed to ioremap registers\n");
  1192. ret = -EINVAL;
  1193. goto failure;
  1194. }
  1195. if (phyiface_res) {
  1196. ksp->phyiface_req =
  1197. request_mem_region(phyiface_res->start,
  1198. resource_size(phyiface_res),
  1199. phyiface_res->name);
  1200. if (!ksp->phyiface_req) {
  1201. dev_err(ksp->dev,
  1202. "cannot claim switch register space\n");
  1203. ret = -EIO;
  1204. goto failure;
  1205. }
  1206. ksp->phyiface_regs = ioremap(phyiface_res->start,
  1207. resource_size(phyiface_res));
  1208. if (!ksp->phyiface_regs) {
  1209. dev_err(ksp->dev,
  1210. "failed to ioremap switch registers\n");
  1211. ret = -EINVAL;
  1212. goto failure;
  1213. }
  1214. }
  1215. ksp->rx_irq = rxirq_res->start;
  1216. ksp->rx_irq_name = rxirq_res->name ? rxirq_res->name : "Ethernet RX";
  1217. ksp->tx_irq = txirq_res->start;
  1218. ksp->tx_irq_name = txirq_res->name ? txirq_res->name : "Ethernet TX";
  1219. ksp->link_irq = (linkirq_res ? linkirq_res->start : -1);
  1220. ksp->link_irq_name = (linkirq_res && linkirq_res->name) ?
  1221. linkirq_res->name : "Ethernet Link";
  1222. /* driver system setup */
  1223. ndev->netdev_ops = &ks8695_netdev_ops;
  1224. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1225. netif_napi_add(ndev, &ksp->napi, ks8695_poll, NAPI_WEIGHT);
  1226. /* Retrieve the default MAC addr from the chip. */
  1227. /* The bootloader should have left it in there for us. */
  1228. machigh = ks8695_readreg(ksp, KS8695_MAH);
  1229. maclow = ks8695_readreg(ksp, KS8695_MAL);
  1230. ndev->dev_addr[0] = (machigh >> 8) & 0xFF;
  1231. ndev->dev_addr[1] = machigh & 0xFF;
  1232. ndev->dev_addr[2] = (maclow >> 24) & 0xFF;
  1233. ndev->dev_addr[3] = (maclow >> 16) & 0xFF;
  1234. ndev->dev_addr[4] = (maclow >> 8) & 0xFF;
  1235. ndev->dev_addr[5] = maclow & 0xFF;
  1236. if (!is_valid_ether_addr(ndev->dev_addr))
  1237. inv_mac_addr = true;
  1238. /* In order to be efficient memory-wise, we allocate both
  1239. * rings in one go.
  1240. */
  1241. ksp->ring_base = dma_alloc_coherent(&pdev->dev, RING_DMA_SIZE,
  1242. &ksp->ring_base_dma, GFP_KERNEL);
  1243. if (!ksp->ring_base) {
  1244. ret = -ENOMEM;
  1245. goto failure;
  1246. }
  1247. /* Specify the TX DMA ring buffer */
  1248. ksp->tx_ring = ksp->ring_base;
  1249. ksp->tx_ring_dma = ksp->ring_base_dma;
  1250. /* And initialise the queue's lock */
  1251. spin_lock_init(&ksp->txq_lock);
  1252. spin_lock_init(&ksp->rx_lock);
  1253. /* Specify the RX DMA ring buffer */
  1254. ksp->rx_ring = ksp->ring_base + TX_RING_DMA_SIZE;
  1255. ksp->rx_ring_dma = ksp->ring_base_dma + TX_RING_DMA_SIZE;
  1256. /* Zero the descriptor rings */
  1257. memset(ksp->tx_ring, 0, TX_RING_DMA_SIZE);
  1258. memset(ksp->rx_ring, 0, RX_RING_DMA_SIZE);
  1259. /* Build the rings */
  1260. for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
  1261. ksp->tx_ring[buff_n].next_desc =
  1262. cpu_to_le32(ksp->tx_ring_dma +
  1263. (sizeof(struct tx_ring_desc) *
  1264. ((buff_n + 1) & MAX_TX_DESC_MASK)));
  1265. }
  1266. for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
  1267. ksp->rx_ring[buff_n].next_desc =
  1268. cpu_to_le32(ksp->rx_ring_dma +
  1269. (sizeof(struct rx_ring_desc) *
  1270. ((buff_n + 1) & MAX_RX_DESC_MASK)));
  1271. }
  1272. /* Initialise the port (physically) */
  1273. if (ksp->phyiface_regs && ksp->link_irq == -1) {
  1274. ks8695_init_switch(ksp);
  1275. ksp->dtype = KS8695_DTYPE_LAN;
  1276. ndev->ethtool_ops = &ks8695_ethtool_ops;
  1277. } else if (ksp->phyiface_regs && ksp->link_irq != -1) {
  1278. ks8695_init_wan_phy(ksp);
  1279. ksp->dtype = KS8695_DTYPE_WAN;
  1280. ndev->ethtool_ops = &ks8695_wan_ethtool_ops;
  1281. } else {
  1282. /* No initialisation since HPNA does not have a PHY */
  1283. ksp->dtype = KS8695_DTYPE_HPNA;
  1284. ndev->ethtool_ops = &ks8695_ethtool_ops;
  1285. }
  1286. /* And bring up the net_device with the net core */
  1287. platform_set_drvdata(pdev, ndev);
  1288. ret = register_netdev(ndev);
  1289. if (ret == 0) {
  1290. if (inv_mac_addr)
  1291. dev_warn(ksp->dev, "%s: Invalid ethernet MAC address. Please set using ip\n",
  1292. ndev->name);
  1293. dev_info(ksp->dev, "ks8695 ethernet (%s) MAC: %pM\n",
  1294. ks8695_port_type(ksp), ndev->dev_addr);
  1295. } else {
  1296. /* Report the failure to register the net_device */
  1297. dev_err(ksp->dev, "ks8695net: failed to register netdev.\n");
  1298. goto failure;
  1299. }
  1300. /* All is well */
  1301. return 0;
  1302. /* Error exit path */
  1303. failure:
  1304. ks8695_release_device(ksp);
  1305. free_netdev(ndev);
  1306. return ret;
  1307. }
  1308. /**
  1309. * ks8695_drv_suspend - Suspend a KS8695 ethernet platform device.
  1310. * @pdev: The device to suspend
  1311. * @state: The suspend state
  1312. *
  1313. * This routine detaches and shuts down a KS8695 ethernet device.
  1314. */
  1315. static int
  1316. ks8695_drv_suspend(struct platform_device *pdev, pm_message_t state)
  1317. {
  1318. struct net_device *ndev = platform_get_drvdata(pdev);
  1319. struct ks8695_priv *ksp = netdev_priv(ndev);
  1320. ksp->in_suspend = 1;
  1321. if (netif_running(ndev)) {
  1322. netif_device_detach(ndev);
  1323. ks8695_shutdown(ksp);
  1324. }
  1325. return 0;
  1326. }
  1327. /**
  1328. * ks8695_drv_resume - Resume a KS8695 ethernet platform device.
  1329. * @pdev: The device to resume
  1330. *
  1331. * This routine re-initialises and re-attaches a KS8695 ethernet
  1332. * device.
  1333. */
  1334. static int
  1335. ks8695_drv_resume(struct platform_device *pdev)
  1336. {
  1337. struct net_device *ndev = platform_get_drvdata(pdev);
  1338. struct ks8695_priv *ksp = netdev_priv(ndev);
  1339. if (netif_running(ndev)) {
  1340. ks8695_reset(ksp);
  1341. ks8695_init_net(ksp);
  1342. ks8695_set_multicast(ndev);
  1343. netif_device_attach(ndev);
  1344. }
  1345. ksp->in_suspend = 0;
  1346. return 0;
  1347. }
  1348. /**
  1349. * ks8695_drv_remove - Remove a KS8695 net device on driver unload.
  1350. * @pdev: The platform device to remove
  1351. *
  1352. * This unregisters and releases a KS8695 ethernet device.
  1353. */
  1354. static int
  1355. ks8695_drv_remove(struct platform_device *pdev)
  1356. {
  1357. struct net_device *ndev = platform_get_drvdata(pdev);
  1358. struct ks8695_priv *ksp = netdev_priv(ndev);
  1359. netif_napi_del(&ksp->napi);
  1360. unregister_netdev(ndev);
  1361. ks8695_release_device(ksp);
  1362. free_netdev(ndev);
  1363. dev_dbg(&pdev->dev, "released and freed device\n");
  1364. return 0;
  1365. }
  1366. static struct platform_driver ks8695_driver = {
  1367. .driver = {
  1368. .name = MODULENAME,
  1369. },
  1370. .probe = ks8695_probe,
  1371. .remove = ks8695_drv_remove,
  1372. .suspend = ks8695_drv_suspend,
  1373. .resume = ks8695_drv_resume,
  1374. };
  1375. module_platform_driver(ks8695_driver);
  1376. MODULE_AUTHOR("Simtec Electronics");
  1377. MODULE_DESCRIPTION("Micrel KS8695 (Centaur) Ethernet driver");
  1378. MODULE_LICENSE("GPL");
  1379. MODULE_ALIAS("platform:" MODULENAME);
  1380. module_param(watchdog, int, 0400);
  1381. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");