ks8695net.c 45 KB

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