ks8695net.c 43 KB

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