cns3xxx_eth.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Cavium CNS3xxx Gigabit driver for Linux
  3. *
  4. * Copyright 2011 Gateworks Corporation
  5. * Chris Lang <clang@gateworks.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License
  9. * as published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/module.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/phy.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/platform_data/cns3xxx.h>
  23. #include <linux/skbuff.h>
  24. #define DRV_NAME "cns3xxx_eth"
  25. #define RX_DESCS 256
  26. #define TX_DESCS 128
  27. #define TX_DESC_RESERVE 20
  28. #define RX_POOL_ALLOC_SIZE (sizeof(struct rx_desc) * RX_DESCS)
  29. #define TX_POOL_ALLOC_SIZE (sizeof(struct tx_desc) * TX_DESCS)
  30. #define REGS_SIZE 336
  31. #define RX_BUFFER_ALIGN 64
  32. #define RX_BUFFER_ALIGN_MASK (~(RX_BUFFER_ALIGN - 1))
  33. #define SKB_HEAD_ALIGN (((PAGE_SIZE - NET_SKB_PAD) % RX_BUFFER_ALIGN) + NET_SKB_PAD + NET_IP_ALIGN)
  34. #define RX_SEGMENT_ALLOC_SIZE 2048
  35. #define RX_SEGMENT_BUFSIZE (SKB_WITH_OVERHEAD(RX_SEGMENT_ALLOC_SIZE))
  36. #define RX_SEGMENT_MRU (((RX_SEGMENT_BUFSIZE - SKB_HEAD_ALIGN) & RX_BUFFER_ALIGN_MASK) - NET_IP_ALIGN)
  37. #define MAX_MTU 9500
  38. #define NAPI_WEIGHT 64
  39. /* MDIO Defines */
  40. #define MDIO_CMD_COMPLETE 0x00008000
  41. #define MDIO_WRITE_COMMAND 0x00002000
  42. #define MDIO_READ_COMMAND 0x00004000
  43. #define MDIO_REG_OFFSET 8
  44. #define MDIO_VALUE_OFFSET 16
  45. /* Descritor Defines */
  46. #define END_OF_RING 0x40000000
  47. #define FIRST_SEGMENT 0x20000000
  48. #define LAST_SEGMENT 0x10000000
  49. #define FORCE_ROUTE 0x04000000
  50. #define UDP_CHECKSUM 0x00020000
  51. #define TCP_CHECKSUM 0x00010000
  52. /* Port Config Defines */
  53. #define PORT_BP_ENABLE 0x00020000
  54. #define PORT_DISABLE 0x00040000
  55. #define PORT_LEARN_DIS 0x00080000
  56. #define PORT_BLOCK_STATE 0x00100000
  57. #define PORT_BLOCK_MODE 0x00200000
  58. #define PROMISC_OFFSET 29
  59. /* Global Config Defines */
  60. #define UNKNOWN_VLAN_TO_CPU 0x02000000
  61. #define ACCEPT_CRC_PACKET 0x00200000
  62. #define CRC_STRIPPING 0x00100000
  63. /* VLAN Config Defines */
  64. #define NIC_MODE 0x00008000
  65. #define VLAN_UNAWARE 0x00000001
  66. /* DMA AUTO Poll Defines */
  67. #define TS_POLL_EN 0x00000020
  68. #define TS_SUSPEND 0x00000010
  69. #define FS_POLL_EN 0x00000002
  70. #define FS_SUSPEND 0x00000001
  71. /* DMA Ring Control Defines */
  72. #define QUEUE_THRESHOLD 0x000000f0
  73. #define CLR_FS_STATE 0x80000000
  74. /* Interrupt Status Defines */
  75. #define MAC0_STATUS_CHANGE 0x00004000
  76. #define MAC1_STATUS_CHANGE 0x00008000
  77. #define MAC2_STATUS_CHANGE 0x00010000
  78. #define MAC0_RX_ERROR 0x00100000
  79. #define MAC1_RX_ERROR 0x00200000
  80. #define MAC2_RX_ERROR 0x00400000
  81. struct tx_desc
  82. {
  83. u32 sdp; /* segment data pointer */
  84. union {
  85. struct {
  86. u32 sdl:16; /* segment data length */
  87. u32 tco:1;
  88. u32 uco:1;
  89. u32 ico:1;
  90. u32 rsv_1:3; /* reserve */
  91. u32 pri:3;
  92. u32 fp:1; /* force priority */
  93. u32 fr:1;
  94. u32 interrupt:1;
  95. u32 lsd:1;
  96. u32 fsd:1;
  97. u32 eor:1;
  98. u32 cown:1;
  99. };
  100. u32 config0;
  101. };
  102. union {
  103. struct {
  104. u32 ctv:1;
  105. u32 stv:1;
  106. u32 sid:4;
  107. u32 inss:1;
  108. u32 dels:1;
  109. u32 rsv_2:9;
  110. u32 pmap:5;
  111. u32 mark:3;
  112. u32 ewan:1;
  113. u32 fewan:1;
  114. u32 rsv_3:5;
  115. };
  116. u32 config1;
  117. };
  118. union {
  119. struct {
  120. u32 c_vid:12;
  121. u32 c_cfs:1;
  122. u32 c_pri:3;
  123. u32 s_vid:12;
  124. u32 s_dei:1;
  125. u32 s_pri:3;
  126. };
  127. u32 config2;
  128. };
  129. u8 alignment[16]; /* for 32 byte */
  130. };
  131. struct rx_desc
  132. {
  133. u32 sdp; /* segment data pointer */
  134. union {
  135. struct {
  136. u32 sdl:16; /* segment data length */
  137. u32 l4f:1;
  138. u32 ipf:1;
  139. u32 prot:4;
  140. u32 hr:6;
  141. u32 lsd:1;
  142. u32 fsd:1;
  143. u32 eor:1;
  144. u32 cown:1;
  145. };
  146. u32 config0;
  147. };
  148. union {
  149. struct {
  150. u32 ctv:1;
  151. u32 stv:1;
  152. u32 unv:1;
  153. u32 iwan:1;
  154. u32 exdv:1;
  155. u32 e_wan:1;
  156. u32 rsv_1:2;
  157. u32 sp:3;
  158. u32 crc_err:1;
  159. u32 un_eth:1;
  160. u32 tc:2;
  161. u32 rsv_2:1;
  162. u32 ip_offset:5;
  163. u32 rsv_3:11;
  164. };
  165. u32 config1;
  166. };
  167. union {
  168. struct {
  169. u32 c_vid:12;
  170. u32 c_cfs:1;
  171. u32 c_pri:3;
  172. u32 s_vid:12;
  173. u32 s_dei:1;
  174. u32 s_pri:3;
  175. };
  176. u32 config2;
  177. };
  178. u8 alignment[16]; /* for 32 byte alignment */
  179. };
  180. struct switch_regs {
  181. u32 phy_control;
  182. u32 phy_auto_addr;
  183. u32 mac_glob_cfg;
  184. u32 mac_cfg[4];
  185. u32 mac_pri_ctrl[5], __res;
  186. u32 etype[2];
  187. u32 udp_range[4];
  188. u32 prio_etype_udp;
  189. u32 prio_ipdscp[8];
  190. u32 tc_ctrl;
  191. u32 rate_ctrl;
  192. u32 fc_glob_thrs;
  193. u32 fc_port_thrs;
  194. u32 mc_fc_glob_thrs;
  195. u32 dc_glob_thrs;
  196. u32 arl_vlan_cmd;
  197. u32 arl_ctrl[3];
  198. u32 vlan_cfg;
  199. u32 pvid[2];
  200. u32 vlan_ctrl[3];
  201. u32 session_id[8];
  202. u32 intr_stat;
  203. u32 intr_mask;
  204. u32 sram_test;
  205. u32 mem_queue;
  206. u32 farl_ctrl;
  207. u32 fc_input_thrs, __res1[2];
  208. u32 clk_skew_ctrl;
  209. u32 mac_glob_cfg_ext, __res2[2];
  210. u32 dma_ring_ctrl;
  211. u32 dma_auto_poll_cfg;
  212. u32 delay_intr_cfg, __res3;
  213. u32 ts_dma_ctrl0;
  214. u32 ts_desc_ptr0;
  215. u32 ts_desc_base_addr0, __res4;
  216. u32 fs_dma_ctrl0;
  217. u32 fs_desc_ptr0;
  218. u32 fs_desc_base_addr0, __res5;
  219. u32 ts_dma_ctrl1;
  220. u32 ts_desc_ptr1;
  221. u32 ts_desc_base_addr1, __res6;
  222. u32 fs_dma_ctrl1;
  223. u32 fs_desc_ptr1;
  224. u32 fs_desc_base_addr1;
  225. u32 __res7[109];
  226. u32 mac_counter0[13];
  227. };
  228. struct _tx_ring {
  229. struct tx_desc *desc;
  230. dma_addr_t phys_addr;
  231. struct tx_desc *cur_addr;
  232. struct sk_buff *buff_tab[TX_DESCS];
  233. unsigned int phys_tab[TX_DESCS];
  234. u32 free_index;
  235. u32 count_index;
  236. u32 cur_index;
  237. int num_used;
  238. int num_count;
  239. bool stopped;
  240. };
  241. struct _rx_ring {
  242. struct rx_desc *desc;
  243. dma_addr_t phys_addr;
  244. struct rx_desc *cur_addr;
  245. void *buff_tab[RX_DESCS];
  246. unsigned int phys_tab[RX_DESCS];
  247. u32 cur_index;
  248. u32 alloc_index;
  249. int alloc_count;
  250. };
  251. struct sw {
  252. struct switch_regs __iomem *regs;
  253. struct napi_struct napi;
  254. struct cns3xxx_plat_info *plat;
  255. struct _tx_ring tx_ring;
  256. struct _rx_ring rx_ring;
  257. struct sk_buff *frag_first;
  258. struct sk_buff *frag_last;
  259. struct device *dev;
  260. int rx_irq;
  261. int stat_irq;
  262. };
  263. struct port {
  264. struct net_device *netdev;
  265. struct phy_device *phydev;
  266. struct sw *sw;
  267. int id; /* logical port ID */
  268. int speed, duplex;
  269. };
  270. static spinlock_t mdio_lock;
  271. static DEFINE_SPINLOCK(tx_lock);
  272. static struct switch_regs __iomem *mdio_regs; /* mdio command and status only */
  273. struct mii_bus *mdio_bus;
  274. static int ports_open;
  275. static struct port *switch_port_tab[4];
  276. struct net_device *napi_dev;
  277. static int cns3xxx_mdio_cmd(struct mii_bus *bus, int phy_id, int location,
  278. int write, u16 cmd)
  279. {
  280. int cycles = 0;
  281. u32 temp = 0;
  282. temp = __raw_readl(&mdio_regs->phy_control);
  283. temp |= MDIO_CMD_COMPLETE;
  284. __raw_writel(temp, &mdio_regs->phy_control);
  285. udelay(10);
  286. if (write) {
  287. temp = (cmd << MDIO_VALUE_OFFSET);
  288. temp |= MDIO_WRITE_COMMAND;
  289. } else {
  290. temp = MDIO_READ_COMMAND;
  291. }
  292. temp |= ((location & 0x1f) << MDIO_REG_OFFSET);
  293. temp |= (phy_id & 0x1f);
  294. __raw_writel(temp, &mdio_regs->phy_control);
  295. while (((__raw_readl(&mdio_regs->phy_control) & MDIO_CMD_COMPLETE) == 0)
  296. && cycles < 5000) {
  297. udelay(1);
  298. cycles++;
  299. }
  300. if (cycles == 5000) {
  301. printk(KERN_ERR "%s #%i: MII transaction failed\n", bus->name,
  302. phy_id);
  303. return -1;
  304. }
  305. temp = __raw_readl(&mdio_regs->phy_control);
  306. temp |= MDIO_CMD_COMPLETE;
  307. __raw_writel(temp, &mdio_regs->phy_control);
  308. if (write)
  309. return 0;
  310. return ((temp >> MDIO_VALUE_OFFSET) & 0xFFFF);
  311. }
  312. static int cns3xxx_mdio_read(struct mii_bus *bus, int phy_id, int location)
  313. {
  314. unsigned long flags;
  315. int ret;
  316. spin_lock_irqsave(&mdio_lock, flags);
  317. ret = cns3xxx_mdio_cmd(bus, phy_id, location, 0, 0);
  318. spin_unlock_irqrestore(&mdio_lock, flags);
  319. return ret;
  320. }
  321. static int cns3xxx_mdio_write(struct mii_bus *bus, int phy_id, int location,
  322. u16 val)
  323. {
  324. unsigned long flags;
  325. int ret;
  326. spin_lock_irqsave(&mdio_lock, flags);
  327. ret = cns3xxx_mdio_cmd(bus, phy_id, location, 1, val);
  328. spin_unlock_irqrestore(&mdio_lock, flags);
  329. return ret;
  330. }
  331. static int cns3xxx_mdio_register(void __iomem *base)
  332. {
  333. int err;
  334. if (!(mdio_bus = mdiobus_alloc()))
  335. return -ENOMEM;
  336. mdio_regs = base;
  337. spin_lock_init(&mdio_lock);
  338. mdio_bus->name = "CNS3xxx MII Bus";
  339. mdio_bus->read = &cns3xxx_mdio_read;
  340. mdio_bus->write = &cns3xxx_mdio_write;
  341. strcpy(mdio_bus->id, "0");
  342. if ((err = mdiobus_register(mdio_bus)))
  343. mdiobus_free(mdio_bus);
  344. return err;
  345. }
  346. static void cns3xxx_mdio_remove(void)
  347. {
  348. mdiobus_unregister(mdio_bus);
  349. mdiobus_free(mdio_bus);
  350. }
  351. static void enable_tx_dma(struct sw *sw)
  352. {
  353. __raw_writel(0x1, &sw->regs->ts_dma_ctrl0);
  354. }
  355. static void enable_rx_dma(struct sw *sw)
  356. {
  357. __raw_writel(0x1, &sw->regs->fs_dma_ctrl0);
  358. }
  359. static void cns3xxx_adjust_link(struct net_device *dev)
  360. {
  361. struct port *port = netdev_priv(dev);
  362. struct phy_device *phydev = port->phydev;
  363. if (!phydev->link) {
  364. if (port->speed) {
  365. port->speed = 0;
  366. printk(KERN_INFO "%s: link down\n", dev->name);
  367. }
  368. return;
  369. }
  370. if (port->speed == phydev->speed && port->duplex == phydev->duplex)
  371. return;
  372. port->speed = phydev->speed;
  373. port->duplex = phydev->duplex;
  374. printk(KERN_INFO "%s: link up, speed %u Mb/s, %s duplex\n",
  375. dev->name, port->speed, port->duplex ? "full" : "half");
  376. }
  377. static void eth_schedule_poll(struct sw *sw)
  378. {
  379. if (unlikely(!napi_schedule_prep(&sw->napi)))
  380. return;
  381. disable_irq_nosync(sw->rx_irq);
  382. __napi_schedule(&sw->napi);
  383. }
  384. irqreturn_t eth_rx_irq(int irq, void *pdev)
  385. {
  386. struct net_device *dev = pdev;
  387. struct sw *sw = netdev_priv(dev);
  388. eth_schedule_poll(sw);
  389. return (IRQ_HANDLED);
  390. }
  391. irqreturn_t eth_stat_irq(int irq, void *pdev)
  392. {
  393. struct net_device *dev = pdev;
  394. struct sw *sw = netdev_priv(dev);
  395. u32 cfg;
  396. u32 stat = __raw_readl(&sw->regs->intr_stat);
  397. __raw_writel(0xffffffff, &sw->regs->intr_stat);
  398. if (stat & MAC2_RX_ERROR)
  399. switch_port_tab[3]->netdev->stats.rx_dropped++;
  400. if (stat & MAC1_RX_ERROR)
  401. switch_port_tab[1]->netdev->stats.rx_dropped++;
  402. if (stat & MAC0_RX_ERROR)
  403. switch_port_tab[0]->netdev->stats.rx_dropped++;
  404. if (stat & MAC0_STATUS_CHANGE) {
  405. cfg = __raw_readl(&sw->regs->mac_cfg[0]);
  406. switch_port_tab[0]->phydev->link = (cfg & 0x1);
  407. switch_port_tab[0]->phydev->duplex = ((cfg >> 4) & 0x1);
  408. if (((cfg >> 2) & 0x3) == 2)
  409. switch_port_tab[0]->phydev->speed = 1000;
  410. else if (((cfg >> 2) & 0x3) == 1)
  411. switch_port_tab[0]->phydev->speed = 100;
  412. else
  413. switch_port_tab[0]->phydev->speed = 10;
  414. cns3xxx_adjust_link(switch_port_tab[0]->netdev);
  415. }
  416. if (stat & MAC1_STATUS_CHANGE) {
  417. cfg = __raw_readl(&sw->regs->mac_cfg[1]);
  418. switch_port_tab[1]->phydev->link = (cfg & 0x1);
  419. switch_port_tab[1]->phydev->duplex = ((cfg >> 4) & 0x1);
  420. if (((cfg >> 2) & 0x3) == 2)
  421. switch_port_tab[1]->phydev->speed = 1000;
  422. else if (((cfg >> 2) & 0x3) == 1)
  423. switch_port_tab[1]->phydev->speed = 100;
  424. else
  425. switch_port_tab[1]->phydev->speed = 10;
  426. cns3xxx_adjust_link(switch_port_tab[1]->netdev);
  427. }
  428. if (stat & MAC2_STATUS_CHANGE) {
  429. cfg = __raw_readl(&sw->regs->mac_cfg[3]);
  430. switch_port_tab[3]->phydev->link = (cfg & 0x1);
  431. switch_port_tab[3]->phydev->duplex = ((cfg >> 4) & 0x1);
  432. if (((cfg >> 2) & 0x3) == 2)
  433. switch_port_tab[3]->phydev->speed = 1000;
  434. else if (((cfg >> 2) & 0x3) == 1)
  435. switch_port_tab[3]->phydev->speed = 100;
  436. else
  437. switch_port_tab[3]->phydev->speed = 10;
  438. cns3xxx_adjust_link(switch_port_tab[3]->netdev);
  439. }
  440. return (IRQ_HANDLED);
  441. }
  442. static void cns3xxx_alloc_rx_buf(struct sw *sw, int received)
  443. {
  444. struct _rx_ring *rx_ring = &sw->rx_ring;
  445. unsigned int i = rx_ring->alloc_index;
  446. struct rx_desc *desc = &(rx_ring)->desc[i];
  447. void *buf;
  448. unsigned int phys;
  449. for (received += rx_ring->alloc_count; received > 0; received--) {
  450. buf = napi_alloc_frag(RX_SEGMENT_ALLOC_SIZE);
  451. if (!buf)
  452. break;
  453. phys = dma_map_single(sw->dev, buf + SKB_HEAD_ALIGN,
  454. RX_SEGMENT_MRU, DMA_FROM_DEVICE);
  455. if (dma_mapping_error(sw->dev, phys)) {
  456. skb_free_frag(buf);
  457. break;
  458. }
  459. desc->sdl = RX_SEGMENT_MRU;
  460. desc->sdp = phys;
  461. wmb();
  462. /* put the new buffer on RX-free queue */
  463. rx_ring->buff_tab[i] = buf;
  464. rx_ring->phys_tab[i] = phys;
  465. if (i == RX_DESCS - 1) {
  466. i = 0;
  467. desc->config0 = END_OF_RING | FIRST_SEGMENT |
  468. LAST_SEGMENT | RX_SEGMENT_MRU;
  469. desc = &(rx_ring)->desc[i];
  470. } else {
  471. desc->config0 = FIRST_SEGMENT | LAST_SEGMENT |
  472. RX_SEGMENT_MRU;
  473. i++;
  474. desc++;
  475. }
  476. }
  477. rx_ring->alloc_count = received;
  478. rx_ring->alloc_index = i;
  479. }
  480. static void eth_check_num_used(struct _tx_ring *tx_ring)
  481. {
  482. bool stop = false;
  483. int i;
  484. if (tx_ring->num_used >= TX_DESCS - TX_DESC_RESERVE)
  485. stop = true;
  486. if (tx_ring->stopped == stop)
  487. return;
  488. tx_ring->stopped = stop;
  489. for (i = 0; i < 4; i++) {
  490. struct port *port = switch_port_tab[i];
  491. struct net_device *dev;
  492. if (!port)
  493. continue;
  494. dev = port->netdev;
  495. if (stop)
  496. netif_stop_queue(dev);
  497. else
  498. netif_wake_queue(dev);
  499. }
  500. }
  501. static void eth_complete_tx(struct sw *sw)
  502. {
  503. struct _tx_ring *tx_ring = &sw->tx_ring;
  504. struct tx_desc *desc;
  505. int i;
  506. int index;
  507. int num_used = tx_ring->num_used;
  508. struct sk_buff *skb;
  509. index = tx_ring->free_index;
  510. desc = &(tx_ring)->desc[index];
  511. for (i = 0; i < num_used; i++) {
  512. if (desc->cown) {
  513. skb = tx_ring->buff_tab[index];
  514. tx_ring->buff_tab[index] = 0;
  515. if (skb)
  516. dev_kfree_skb_any(skb);
  517. dma_unmap_single(sw->dev, tx_ring->phys_tab[index],
  518. desc->sdl, DMA_TO_DEVICE);
  519. if (++index == TX_DESCS) {
  520. index = 0;
  521. desc = &(tx_ring)->desc[index];
  522. } else {
  523. desc++;
  524. }
  525. } else {
  526. break;
  527. }
  528. }
  529. tx_ring->free_index = index;
  530. tx_ring->num_used -= i;
  531. eth_check_num_used(tx_ring);
  532. }
  533. static int eth_poll(struct napi_struct *napi, int budget)
  534. {
  535. struct sw *sw = container_of(napi, struct sw, napi);
  536. struct _rx_ring *rx_ring = &sw->rx_ring;
  537. int received = 0;
  538. unsigned int length;
  539. unsigned int i = rx_ring->cur_index;
  540. struct rx_desc *desc = &(rx_ring)->desc[i];
  541. unsigned int alloc_count = rx_ring->alloc_count;
  542. while (desc->cown && alloc_count + received < RX_DESCS - 1) {
  543. struct sk_buff *skb;
  544. int reserve = SKB_HEAD_ALIGN;
  545. if (received >= budget)
  546. break;
  547. /* process received frame */
  548. dma_unmap_single(sw->dev, rx_ring->phys_tab[i],
  549. RX_SEGMENT_MRU, DMA_FROM_DEVICE);
  550. skb = build_skb(rx_ring->buff_tab[i], RX_SEGMENT_ALLOC_SIZE);
  551. if (!skb)
  552. break;
  553. skb->dev = switch_port_tab[desc->sp]->netdev;
  554. length = desc->sdl;
  555. if (desc->fsd && !desc->lsd)
  556. length = RX_SEGMENT_MRU;
  557. if (!desc->fsd) {
  558. reserve -= NET_IP_ALIGN;
  559. if (!desc->lsd)
  560. length += NET_IP_ALIGN;
  561. }
  562. skb_reserve(skb, reserve);
  563. skb_put(skb, length);
  564. if (!sw->frag_first)
  565. sw->frag_first = skb;
  566. else {
  567. if (sw->frag_first == sw->frag_last)
  568. skb_shinfo(sw->frag_first)->frag_list = skb;
  569. else
  570. sw->frag_last->next = skb;
  571. sw->frag_first->len += skb->len;
  572. sw->frag_first->data_len += skb->len;
  573. sw->frag_first->truesize += skb->truesize;
  574. }
  575. sw->frag_last = skb;
  576. if (desc->lsd) {
  577. struct net_device *dev;
  578. skb = sw->frag_first;
  579. dev = skb->dev;
  580. skb->protocol = eth_type_trans(skb, dev);
  581. dev->stats.rx_packets++;
  582. dev->stats.rx_bytes += skb->len;
  583. /* RX Hardware checksum offload */
  584. skb->ip_summed = CHECKSUM_NONE;
  585. switch (desc->prot) {
  586. case 1:
  587. case 2:
  588. case 5:
  589. case 6:
  590. case 13:
  591. case 14:
  592. if (!desc->l4f) {
  593. skb->ip_summed = CHECKSUM_UNNECESSARY;
  594. napi_gro_receive(napi, skb);
  595. break;
  596. }
  597. /* fall through */
  598. default:
  599. netif_receive_skb(skb);
  600. break;
  601. }
  602. sw->frag_first = NULL;
  603. sw->frag_last = NULL;
  604. }
  605. received++;
  606. if (++i == RX_DESCS) {
  607. i = 0;
  608. desc = &(rx_ring)->desc[i];
  609. } else {
  610. desc++;
  611. }
  612. }
  613. rx_ring->cur_index = i;
  614. if (!received) {
  615. napi_complete(napi);
  616. enable_irq(sw->rx_irq);
  617. budget = 0;
  618. /* If 1 or more frames came in during IRQ enable, re-schedule */
  619. if (rx_ring->desc[i].cown)
  620. eth_schedule_poll(sw);
  621. }
  622. spin_lock_bh(&tx_lock);
  623. eth_complete_tx(sw);
  624. spin_unlock_bh(&tx_lock);
  625. cns3xxx_alloc_rx_buf(sw, received);
  626. wmb();
  627. enable_rx_dma(sw);
  628. return budget;
  629. }
  630. static void eth_set_desc(struct sw *sw, struct _tx_ring *tx_ring, int index,
  631. int index_last, void *data, int len, u32 config0,
  632. u32 pmap)
  633. {
  634. struct tx_desc *tx_desc = &(tx_ring)->desc[index];
  635. unsigned int phys;
  636. phys = dma_map_single(sw->dev, data, len, DMA_TO_DEVICE);
  637. tx_desc->sdp = phys;
  638. tx_desc->pmap = pmap;
  639. tx_ring->phys_tab[index] = phys;
  640. config0 |= len;
  641. if (index == TX_DESCS - 1)
  642. config0 |= END_OF_RING;
  643. if (index == index_last)
  644. config0 |= LAST_SEGMENT;
  645. wmb();
  646. tx_desc->config0 = config0;
  647. }
  648. static int eth_xmit(struct sk_buff *skb, struct net_device *dev)
  649. {
  650. struct port *port = netdev_priv(dev);
  651. struct sw *sw = port->sw;
  652. struct _tx_ring *tx_ring = &sw->tx_ring;
  653. struct sk_buff *skb1;
  654. char pmap = (1 << port->id);
  655. int nr_frags = skb_shinfo(skb)->nr_frags;
  656. int nr_desc = nr_frags;
  657. int index0, index, index_last;
  658. int len0;
  659. unsigned int i;
  660. u32 config0;
  661. if (pmap == 8)
  662. pmap = (1 << 4);
  663. skb_walk_frags(skb, skb1)
  664. nr_desc++;
  665. eth_schedule_poll(sw);
  666. spin_lock_bh(&tx_lock);
  667. if ((tx_ring->num_used + nr_desc + 1) >= TX_DESCS) {
  668. spin_unlock_bh(&tx_lock);
  669. return NETDEV_TX_BUSY;
  670. }
  671. index = index0 = tx_ring->cur_index;
  672. index_last = (index0 + nr_desc) % TX_DESCS;
  673. tx_ring->cur_index = (index_last + 1) % TX_DESCS;
  674. spin_unlock_bh(&tx_lock);
  675. config0 = FORCE_ROUTE;
  676. if (skb->ip_summed == CHECKSUM_PARTIAL)
  677. config0 |= UDP_CHECKSUM | TCP_CHECKSUM;
  678. len0 = skb->len;
  679. /* fragments */
  680. for (i = 0; i < nr_frags; i++) {
  681. struct skb_frag_struct *frag;
  682. void *addr;
  683. index = (index + 1) % TX_DESCS;
  684. frag = &skb_shinfo(skb)->frags[i];
  685. addr = page_address(skb_frag_page(frag)) + frag->page_offset;
  686. eth_set_desc(sw, tx_ring, index, index_last, addr, frag->size,
  687. config0, pmap);
  688. }
  689. if (nr_frags)
  690. len0 = skb->len - skb->data_len;
  691. skb_walk_frags(skb, skb1) {
  692. index = (index + 1) % TX_DESCS;
  693. len0 -= skb1->len;
  694. eth_set_desc(sw, tx_ring, index, index_last, skb1->data,
  695. skb1->len, config0, pmap);
  696. }
  697. tx_ring->buff_tab[index0] = skb;
  698. eth_set_desc(sw, tx_ring, index0, index_last, skb->data, len0,
  699. config0 | FIRST_SEGMENT, pmap);
  700. wmb();
  701. spin_lock(&tx_lock);
  702. tx_ring->num_used += nr_desc + 1;
  703. spin_unlock(&tx_lock);
  704. dev->stats.tx_packets++;
  705. dev->stats.tx_bytes += skb->len;
  706. enable_tx_dma(sw);
  707. return NETDEV_TX_OK;
  708. }
  709. static int eth_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  710. {
  711. struct port *port = netdev_priv(dev);
  712. if (!netif_running(dev))
  713. return -EINVAL;
  714. return phy_mii_ioctl(port->phydev, req, cmd);
  715. }
  716. /* ethtool support */
  717. static void cns3xxx_get_drvinfo(struct net_device *dev,
  718. struct ethtool_drvinfo *info)
  719. {
  720. strcpy(info->driver, DRV_NAME);
  721. strcpy(info->bus_info, "internal");
  722. }
  723. static int cns3xxx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  724. {
  725. struct port *port = netdev_priv(dev);
  726. return phy_ethtool_gset(port->phydev, cmd);
  727. }
  728. static int cns3xxx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  729. {
  730. struct port *port = netdev_priv(dev);
  731. return phy_ethtool_sset(port->phydev, cmd);
  732. }
  733. static int cns3xxx_nway_reset(struct net_device *dev)
  734. {
  735. struct port *port = netdev_priv(dev);
  736. return phy_start_aneg(port->phydev);
  737. }
  738. static struct ethtool_ops cns3xxx_ethtool_ops = {
  739. .get_drvinfo = cns3xxx_get_drvinfo,
  740. .get_settings = cns3xxx_get_settings,
  741. .set_settings = cns3xxx_set_settings,
  742. .nway_reset = cns3xxx_nway_reset,
  743. .get_link = ethtool_op_get_link,
  744. };
  745. static int init_rings(struct sw *sw)
  746. {
  747. int i;
  748. struct _rx_ring *rx_ring = &sw->rx_ring;
  749. struct _tx_ring *tx_ring = &sw->tx_ring;
  750. __raw_writel(0, &sw->regs->fs_dma_ctrl0);
  751. __raw_writel(TS_SUSPEND | FS_SUSPEND, &sw->regs->dma_auto_poll_cfg);
  752. __raw_writel(QUEUE_THRESHOLD, &sw->regs->dma_ring_ctrl);
  753. __raw_writel(CLR_FS_STATE | QUEUE_THRESHOLD, &sw->regs->dma_ring_ctrl);
  754. __raw_writel(QUEUE_THRESHOLD, &sw->regs->dma_ring_ctrl);
  755. rx_ring->desc = dmam_alloc_coherent(sw->dev, RX_POOL_ALLOC_SIZE,
  756. &rx_ring->phys_addr, GFP_KERNEL);
  757. if (!rx_ring->desc)
  758. return -ENOMEM;
  759. /* Setup RX buffers */
  760. memset(rx_ring->desc, 0, RX_POOL_ALLOC_SIZE);
  761. for (i = 0; i < RX_DESCS; i++) {
  762. struct rx_desc *desc = &(rx_ring)->desc[i];
  763. void *buf;
  764. buf = netdev_alloc_frag(RX_SEGMENT_ALLOC_SIZE);
  765. if (!buf)
  766. return -ENOMEM;
  767. desc->sdl = RX_SEGMENT_MRU;
  768. if (i == (RX_DESCS - 1))
  769. desc->eor = 1;
  770. desc->fsd = 1;
  771. desc->lsd = 1;
  772. desc->sdp = dma_map_single(sw->dev, buf + SKB_HEAD_ALIGN,
  773. RX_SEGMENT_MRU, DMA_FROM_DEVICE);
  774. if (dma_mapping_error(sw->dev, desc->sdp))
  775. return -EIO;
  776. rx_ring->buff_tab[i] = buf;
  777. rx_ring->phys_tab[i] = desc->sdp;
  778. desc->cown = 0;
  779. }
  780. __raw_writel(rx_ring->phys_addr, &sw->regs->fs_desc_ptr0);
  781. __raw_writel(rx_ring->phys_addr, &sw->regs->fs_desc_base_addr0);
  782. tx_ring->desc = dmam_alloc_coherent(sw->dev, TX_POOL_ALLOC_SIZE,
  783. &tx_ring->phys_addr, GFP_KERNEL);
  784. if (!tx_ring->desc)
  785. return -ENOMEM;
  786. /* Setup TX buffers */
  787. memset(tx_ring->desc, 0, TX_POOL_ALLOC_SIZE);
  788. for (i = 0; i < TX_DESCS; i++) {
  789. struct tx_desc *desc = &(tx_ring)->desc[i];
  790. tx_ring->buff_tab[i] = 0;
  791. if (i == (TX_DESCS - 1))
  792. desc->eor = 1;
  793. desc->cown = 1;
  794. }
  795. __raw_writel(tx_ring->phys_addr, &sw->regs->ts_desc_ptr0);
  796. __raw_writel(tx_ring->phys_addr, &sw->regs->ts_desc_base_addr0);
  797. return 0;
  798. }
  799. static void destroy_rings(struct sw *sw)
  800. {
  801. int i;
  802. for (i = 0; i < RX_DESCS; i++) {
  803. struct _rx_ring *rx_ring = &sw->rx_ring;
  804. struct rx_desc *desc = &(rx_ring)->desc[i];
  805. void *buf = sw->rx_ring.buff_tab[i];
  806. if (!buf)
  807. continue;
  808. dma_unmap_single(sw->dev, desc->sdp, RX_SEGMENT_MRU,
  809. DMA_FROM_DEVICE);
  810. skb_free_frag(buf);
  811. }
  812. for (i = 0; i < TX_DESCS; i++) {
  813. struct _tx_ring *tx_ring = &sw->tx_ring;
  814. struct tx_desc *desc = &(tx_ring)->desc[i];
  815. struct sk_buff *skb = sw->tx_ring.buff_tab[i];
  816. if (!skb)
  817. continue;
  818. dma_unmap_single(sw->dev, desc->sdp, skb->len, DMA_TO_DEVICE);
  819. dev_kfree_skb(skb);
  820. }
  821. }
  822. static int eth_open(struct net_device *dev)
  823. {
  824. struct port *port = netdev_priv(dev);
  825. struct sw *sw = port->sw;
  826. u32 temp;
  827. port->speed = 0; /* force "link up" message */
  828. phy_start(port->phydev);
  829. netif_start_queue(dev);
  830. if (!ports_open) {
  831. request_irq(sw->rx_irq, eth_rx_irq, IRQF_SHARED, "gig_switch", napi_dev);
  832. request_irq(sw->stat_irq, eth_stat_irq, IRQF_SHARED, "gig_stat", napi_dev);
  833. napi_enable(&sw->napi);
  834. netif_start_queue(napi_dev);
  835. __raw_writel(~(MAC0_STATUS_CHANGE | MAC1_STATUS_CHANGE | MAC2_STATUS_CHANGE |
  836. MAC0_RX_ERROR | MAC1_RX_ERROR | MAC2_RX_ERROR), &sw->regs->intr_mask);
  837. temp = __raw_readl(&sw->regs->mac_cfg[2]);
  838. temp &= ~(PORT_DISABLE);
  839. __raw_writel(temp, &sw->regs->mac_cfg[2]);
  840. temp = __raw_readl(&sw->regs->dma_auto_poll_cfg);
  841. temp &= ~(TS_SUSPEND | FS_SUSPEND);
  842. __raw_writel(temp, &sw->regs->dma_auto_poll_cfg);
  843. enable_rx_dma(sw);
  844. }
  845. temp = __raw_readl(&sw->regs->mac_cfg[port->id]);
  846. temp &= ~(PORT_DISABLE);
  847. __raw_writel(temp, &sw->regs->mac_cfg[port->id]);
  848. ports_open++;
  849. netif_carrier_on(dev);
  850. return 0;
  851. }
  852. static int eth_close(struct net_device *dev)
  853. {
  854. struct port *port = netdev_priv(dev);
  855. struct sw *sw = port->sw;
  856. u32 temp;
  857. ports_open--;
  858. temp = __raw_readl(&sw->regs->mac_cfg[port->id]);
  859. temp |= (PORT_DISABLE);
  860. __raw_writel(temp, &sw->regs->mac_cfg[port->id]);
  861. netif_stop_queue(dev);
  862. phy_stop(port->phydev);
  863. if (!ports_open) {
  864. disable_irq(sw->rx_irq);
  865. free_irq(sw->rx_irq, napi_dev);
  866. disable_irq(sw->stat_irq);
  867. free_irq(sw->stat_irq, napi_dev);
  868. napi_disable(&sw->napi);
  869. netif_stop_queue(napi_dev);
  870. temp = __raw_readl(&sw->regs->mac_cfg[2]);
  871. temp |= (PORT_DISABLE);
  872. __raw_writel(temp, &sw->regs->mac_cfg[2]);
  873. __raw_writel(TS_SUSPEND | FS_SUSPEND,
  874. &sw->regs->dma_auto_poll_cfg);
  875. }
  876. netif_carrier_off(dev);
  877. return 0;
  878. }
  879. static void eth_rx_mode(struct net_device *dev)
  880. {
  881. struct port *port = netdev_priv(dev);
  882. struct sw *sw = port->sw;
  883. u32 temp;
  884. temp = __raw_readl(&sw->regs->mac_glob_cfg);
  885. if (dev->flags & IFF_PROMISC) {
  886. if (port->id == 3)
  887. temp |= ((1 << 2) << PROMISC_OFFSET);
  888. else
  889. temp |= ((1 << port->id) << PROMISC_OFFSET);
  890. } else {
  891. if (port->id == 3)
  892. temp &= ~((1 << 2) << PROMISC_OFFSET);
  893. else
  894. temp &= ~((1 << port->id) << PROMISC_OFFSET);
  895. }
  896. __raw_writel(temp, &sw->regs->mac_glob_cfg);
  897. }
  898. static int eth_set_mac(struct net_device *netdev, void *p)
  899. {
  900. struct port *port = netdev_priv(netdev);
  901. struct sw *sw = port->sw;
  902. struct sockaddr *addr = p;
  903. u32 cycles = 0;
  904. if (!is_valid_ether_addr(addr->sa_data))
  905. return -EADDRNOTAVAIL;
  906. /* Invalidate old ARL Entry */
  907. if (port->id == 3)
  908. __raw_writel((port->id << 16) | (0x4 << 9), &sw->regs->arl_ctrl[0]);
  909. else
  910. __raw_writel(((port->id + 1) << 16) | (0x4 << 9), &sw->regs->arl_ctrl[0]);
  911. __raw_writel( ((netdev->dev_addr[0] << 24) | (netdev->dev_addr[1] << 16) |
  912. (netdev->dev_addr[2] << 8) | (netdev->dev_addr[3])),
  913. &sw->regs->arl_ctrl[1]);
  914. __raw_writel( ((netdev->dev_addr[4] << 24) | (netdev->dev_addr[5] << 16) |
  915. (1 << 1)),
  916. &sw->regs->arl_ctrl[2]);
  917. __raw_writel((1 << 19), &sw->regs->arl_vlan_cmd);
  918. while (((__raw_readl(&sw->regs->arl_vlan_cmd) & (1 << 21)) == 0)
  919. && cycles < 5000) {
  920. udelay(1);
  921. cycles++;
  922. }
  923. cycles = 0;
  924. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  925. if (port->id == 3)
  926. __raw_writel((port->id << 16) | (0x4 << 9), &sw->regs->arl_ctrl[0]);
  927. else
  928. __raw_writel(((port->id + 1) << 16) | (0x4 << 9), &sw->regs->arl_ctrl[0]);
  929. __raw_writel( ((addr->sa_data[0] << 24) | (addr->sa_data[1] << 16) |
  930. (addr->sa_data[2] << 8) | (addr->sa_data[3])),
  931. &sw->regs->arl_ctrl[1]);
  932. __raw_writel( ((addr->sa_data[4] << 24) | (addr->sa_data[5] << 16) |
  933. (7 << 4) | (1 << 1)), &sw->regs->arl_ctrl[2]);
  934. __raw_writel((1 << 19), &sw->regs->arl_vlan_cmd);
  935. while (((__raw_readl(&sw->regs->arl_vlan_cmd) & (1 << 21)) == 0)
  936. && cycles < 5000) {
  937. udelay(1);
  938. cycles++;
  939. }
  940. return 0;
  941. }
  942. static int cns3xxx_change_mtu(struct net_device *dev, int new_mtu)
  943. {
  944. if (new_mtu > MAX_MTU)
  945. return -EINVAL;
  946. dev->mtu = new_mtu;
  947. return 0;
  948. }
  949. static const struct net_device_ops cns3xxx_netdev_ops = {
  950. .ndo_open = eth_open,
  951. .ndo_stop = eth_close,
  952. .ndo_start_xmit = eth_xmit,
  953. .ndo_set_rx_mode = eth_rx_mode,
  954. .ndo_do_ioctl = eth_ioctl,
  955. .ndo_change_mtu = cns3xxx_change_mtu,
  956. .ndo_set_mac_address = eth_set_mac,
  957. .ndo_validate_addr = eth_validate_addr,
  958. };
  959. static int eth_init_one(struct platform_device *pdev)
  960. {
  961. int i;
  962. struct port *port;
  963. struct sw *sw;
  964. struct net_device *dev;
  965. struct cns3xxx_plat_info *plat = pdev->dev.platform_data;
  966. char phy_id[MII_BUS_ID_SIZE + 3];
  967. int err;
  968. u32 temp;
  969. struct resource *res;
  970. void __iomem *regs;
  971. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  972. regs = devm_ioremap_resource(&pdev->dev, res);
  973. if (IS_ERR(regs))
  974. return PTR_ERR(regs);
  975. err = cns3xxx_mdio_register(regs);
  976. if (err)
  977. return err;
  978. if (!(napi_dev = alloc_etherdev(sizeof(struct sw)))) {
  979. err = -ENOMEM;
  980. goto err_remove_mdio;
  981. }
  982. strcpy(napi_dev->name, "switch%d");
  983. napi_dev->features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_FRAGLIST;
  984. SET_NETDEV_DEV(napi_dev, &pdev->dev);
  985. sw = netdev_priv(napi_dev);
  986. memset(sw, 0, sizeof(struct sw));
  987. sw->regs = regs;
  988. sw->dev = &pdev->dev;
  989. sw->rx_irq = platform_get_irq_byname(pdev, "eth_rx");
  990. sw->stat_irq = platform_get_irq_byname(pdev, "eth_stat");
  991. temp = __raw_readl(&sw->regs->phy_auto_addr);
  992. temp |= (3 << 30); /* maximum frame length: 9600 bytes */
  993. __raw_writel(temp, &sw->regs->phy_auto_addr);
  994. for (i = 0; i < 4; i++) {
  995. temp = __raw_readl(&sw->regs->mac_cfg[i]);
  996. temp |= (PORT_DISABLE);
  997. __raw_writel(temp, &sw->regs->mac_cfg[i]);
  998. }
  999. temp = PORT_DISABLE;
  1000. __raw_writel(temp, &sw->regs->mac_cfg[2]);
  1001. temp = __raw_readl(&sw->regs->vlan_cfg);
  1002. temp |= NIC_MODE | VLAN_UNAWARE;
  1003. __raw_writel(temp, &sw->regs->vlan_cfg);
  1004. __raw_writel(UNKNOWN_VLAN_TO_CPU |
  1005. CRC_STRIPPING, &sw->regs->mac_glob_cfg);
  1006. if ((err = init_rings(sw)) != 0) {
  1007. err = -ENOMEM;
  1008. goto err_free;
  1009. }
  1010. platform_set_drvdata(pdev, napi_dev);
  1011. netif_napi_add(napi_dev, &sw->napi, eth_poll, NAPI_WEIGHT);
  1012. for (i = 0; i < 3; i++) {
  1013. if (!(plat->ports & (1 << i))) {
  1014. continue;
  1015. }
  1016. if (!(dev = alloc_etherdev(sizeof(struct port)))) {
  1017. goto free_ports;
  1018. }
  1019. port = netdev_priv(dev);
  1020. port->netdev = dev;
  1021. if (i == 2)
  1022. port->id = 3;
  1023. else
  1024. port->id = i;
  1025. port->sw = sw;
  1026. temp = __raw_readl(&sw->regs->mac_cfg[port->id]);
  1027. temp |= (PORT_DISABLE | PORT_BLOCK_STATE | PORT_LEARN_DIS);
  1028. __raw_writel(temp, &sw->regs->mac_cfg[port->id]);
  1029. SET_NETDEV_DEV(dev, &pdev->dev);
  1030. dev->netdev_ops = &cns3xxx_netdev_ops;
  1031. dev->ethtool_ops = &cns3xxx_ethtool_ops;
  1032. dev->tx_queue_len = 1000;
  1033. dev->features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_FRAGLIST;
  1034. switch_port_tab[port->id] = port;
  1035. memcpy(dev->dev_addr, &plat->hwaddr[i], ETH_ALEN);
  1036. snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, "0", plat->phy[i]);
  1037. port->phydev = phy_connect(dev, phy_id, &cns3xxx_adjust_link,
  1038. PHY_INTERFACE_MODE_RGMII);
  1039. if ((err = IS_ERR(port->phydev))) {
  1040. switch_port_tab[port->id] = 0;
  1041. free_netdev(dev);
  1042. goto free_ports;
  1043. }
  1044. port->phydev->irq = PHY_IGNORE_INTERRUPT;
  1045. if ((err = register_netdev(dev))) {
  1046. phy_disconnect(port->phydev);
  1047. switch_port_tab[port->id] = 0;
  1048. free_netdev(dev);
  1049. goto free_ports;
  1050. }
  1051. printk(KERN_INFO "%s: RGMII PHY %i on cns3xxx Switch\n", dev->name, plat->phy[i]);
  1052. netif_carrier_off(dev);
  1053. dev = 0;
  1054. }
  1055. return 0;
  1056. free_ports:
  1057. err = -ENOMEM;
  1058. for (--i; i >= 0; i--) {
  1059. if (switch_port_tab[i]) {
  1060. port = switch_port_tab[i];
  1061. dev = port->netdev;
  1062. unregister_netdev(dev);
  1063. phy_disconnect(port->phydev);
  1064. switch_port_tab[i] = 0;
  1065. free_netdev(dev);
  1066. }
  1067. }
  1068. err_free:
  1069. free_netdev(napi_dev);
  1070. err_remove_mdio:
  1071. cns3xxx_mdio_remove();
  1072. return err;
  1073. }
  1074. static int eth_remove_one(struct platform_device *pdev)
  1075. {
  1076. struct net_device *dev = platform_get_drvdata(pdev);
  1077. struct sw *sw = netdev_priv(dev);
  1078. int i;
  1079. destroy_rings(sw);
  1080. for (i = 3; i >= 0; i--) {
  1081. if (switch_port_tab[i]) {
  1082. struct port *port = switch_port_tab[i];
  1083. struct net_device *dev = port->netdev;
  1084. unregister_netdev(dev);
  1085. phy_disconnect(port->phydev);
  1086. switch_port_tab[i] = 0;
  1087. free_netdev(dev);
  1088. }
  1089. }
  1090. free_netdev(napi_dev);
  1091. cns3xxx_mdio_remove();
  1092. return 0;
  1093. }
  1094. static struct platform_driver cns3xxx_eth_driver = {
  1095. .driver.name = DRV_NAME,
  1096. .probe = eth_init_one,
  1097. .remove = eth_remove_one,
  1098. };
  1099. static int __init eth_init_module(void)
  1100. {
  1101. return platform_driver_register(&cns3xxx_eth_driver);
  1102. }
  1103. static void __exit eth_cleanup_module(void)
  1104. {
  1105. platform_driver_unregister(&cns3xxx_eth_driver);
  1106. }
  1107. module_init(eth_init_module);
  1108. module_exit(eth_cleanup_module);
  1109. MODULE_AUTHOR("Chris Lang");
  1110. MODULE_DESCRIPTION("Cavium CNS3xxx Ethernet driver");
  1111. MODULE_LICENSE("GPL v2");
  1112. MODULE_ALIAS("platform:cns3xxx_eth");