/drivers/net/ethernet/toshiba/ps3_gelic_net.c

http://github.com/mirrors/linux · C · 1873 lines · 1246 code · 238 blank · 389 comment · 162 complexity · ab36ed0c92d3f9e22fe6a65a16ffb15f MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PS3 gelic network driver.
  4. *
  5. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  6. * Copyright 2006, 2007 Sony Corporation
  7. *
  8. * This file is based on: spider_net.c
  9. *
  10. * (C) Copyright IBM Corp. 2005
  11. *
  12. * Authors : Utz Bacher <utz.bacher@de.ibm.com>
  13. * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
  14. */
  15. #undef DEBUG
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/tcp.h>
  26. #include <linux/dma-mapping.h>
  27. #include <net/checksum.h>
  28. #include <asm/firmware.h>
  29. #include <asm/ps3.h>
  30. #include <asm/lv1call.h>
  31. #include "ps3_gelic_net.h"
  32. #include "ps3_gelic_wireless.h"
  33. #define DRV_NAME "Gelic Network Driver"
  34. #define DRV_VERSION "2.0"
  35. MODULE_AUTHOR("SCE Inc.");
  36. MODULE_DESCRIPTION("Gelic Network driver");
  37. MODULE_LICENSE("GPL");
  38. /* set irq_mask */
  39. int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
  40. {
  41. int status;
  42. status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
  43. mask, 0);
  44. if (status)
  45. dev_info(ctodev(card),
  46. "%s failed %d\n", __func__, status);
  47. return status;
  48. }
  49. static void gelic_card_rx_irq_on(struct gelic_card *card)
  50. {
  51. card->irq_mask |= GELIC_CARD_RXINT;
  52. gelic_card_set_irq_mask(card, card->irq_mask);
  53. }
  54. static void gelic_card_rx_irq_off(struct gelic_card *card)
  55. {
  56. card->irq_mask &= ~GELIC_CARD_RXINT;
  57. gelic_card_set_irq_mask(card, card->irq_mask);
  58. }
  59. static void gelic_card_get_ether_port_status(struct gelic_card *card,
  60. int inform)
  61. {
  62. u64 v2;
  63. struct net_device *ether_netdev;
  64. lv1_net_control(bus_id(card), dev_id(card),
  65. GELIC_LV1_GET_ETH_PORT_STATUS,
  66. GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
  67. &card->ether_port_status, &v2);
  68. if (inform) {
  69. ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
  70. if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
  71. netif_carrier_on(ether_netdev);
  72. else
  73. netif_carrier_off(ether_netdev);
  74. }
  75. }
  76. /**
  77. * gelic_descr_get_status -- returns the status of a descriptor
  78. * @descr: descriptor to look at
  79. *
  80. * returns the status as in the dmac_cmd_status field of the descriptor
  81. */
  82. static enum gelic_descr_dma_status
  83. gelic_descr_get_status(struct gelic_descr *descr)
  84. {
  85. return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
  86. }
  87. static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
  88. {
  89. int status;
  90. u64 v1, v2;
  91. status = lv1_net_control(bus_id(card), dev_id(card),
  92. GELIC_LV1_SET_NEGOTIATION_MODE,
  93. GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
  94. if (status) {
  95. pr_info("%s: failed setting negotiation mode %d\n", __func__,
  96. status);
  97. return -EBUSY;
  98. }
  99. card->link_mode = mode;
  100. return 0;
  101. }
  102. /**
  103. * gelic_card_disable_txdmac - disables the transmit DMA controller
  104. * @card: card structure
  105. *
  106. * gelic_card_disable_txdmac terminates processing on the DMA controller by
  107. * turing off DMA and issuing a force end
  108. */
  109. static void gelic_card_disable_txdmac(struct gelic_card *card)
  110. {
  111. int status;
  112. /* this hvc blocks until the DMA in progress really stopped */
  113. status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card));
  114. if (status)
  115. dev_err(ctodev(card),
  116. "lv1_net_stop_tx_dma failed, status=%d\n", status);
  117. }
  118. /**
  119. * gelic_card_enable_rxdmac - enables the receive DMA controller
  120. * @card: card structure
  121. *
  122. * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
  123. * in the GDADMACCNTR register
  124. */
  125. static void gelic_card_enable_rxdmac(struct gelic_card *card)
  126. {
  127. int status;
  128. #ifdef DEBUG
  129. if (gelic_descr_get_status(card->rx_chain.head) !=
  130. GELIC_DESCR_DMA_CARDOWNED) {
  131. printk(KERN_ERR "%s: status=%x\n", __func__,
  132. be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
  133. printk(KERN_ERR "%s: nextphy=%x\n", __func__,
  134. be32_to_cpu(card->rx_chain.head->next_descr_addr));
  135. printk(KERN_ERR "%s: head=%p\n", __func__,
  136. card->rx_chain.head);
  137. }
  138. #endif
  139. status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
  140. card->rx_chain.head->bus_addr, 0);
  141. if (status)
  142. dev_info(ctodev(card),
  143. "lv1_net_start_rx_dma failed, status=%d\n", status);
  144. }
  145. /**
  146. * gelic_card_disable_rxdmac - disables the receive DMA controller
  147. * @card: card structure
  148. *
  149. * gelic_card_disable_rxdmac terminates processing on the DMA controller by
  150. * turing off DMA and issuing a force end
  151. */
  152. static void gelic_card_disable_rxdmac(struct gelic_card *card)
  153. {
  154. int status;
  155. /* this hvc blocks until the DMA in progress really stopped */
  156. status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card));
  157. if (status)
  158. dev_err(ctodev(card),
  159. "lv1_net_stop_rx_dma failed, %d\n", status);
  160. }
  161. /**
  162. * gelic_descr_set_status -- sets the status of a descriptor
  163. * @descr: descriptor to change
  164. * @status: status to set in the descriptor
  165. *
  166. * changes the status to the specified value. Doesn't change other bits
  167. * in the status
  168. */
  169. static void gelic_descr_set_status(struct gelic_descr *descr,
  170. enum gelic_descr_dma_status status)
  171. {
  172. descr->dmac_cmd_status = cpu_to_be32(status |
  173. (be32_to_cpu(descr->dmac_cmd_status) &
  174. ~GELIC_DESCR_DMA_STAT_MASK));
  175. /*
  176. * dma_cmd_status field is used to indicate whether the descriptor
  177. * is valid or not.
  178. * Usually caller of this function wants to inform that to the
  179. * hardware, so we assure here the hardware sees the change.
  180. */
  181. wmb();
  182. }
  183. /**
  184. * gelic_card_reset_chain - reset status of a descriptor chain
  185. * @card: card structure
  186. * @chain: address of chain
  187. * @start_descr: address of descriptor array
  188. *
  189. * Reset the status of dma descriptors to ready state
  190. * and re-initialize the hardware chain for later use
  191. */
  192. static void gelic_card_reset_chain(struct gelic_card *card,
  193. struct gelic_descr_chain *chain,
  194. struct gelic_descr *start_descr)
  195. {
  196. struct gelic_descr *descr;
  197. for (descr = start_descr; start_descr != descr->next; descr++) {
  198. gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
  199. descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
  200. }
  201. chain->head = start_descr;
  202. chain->tail = (descr - 1);
  203. (descr - 1)->next_descr_addr = 0;
  204. }
  205. void gelic_card_up(struct gelic_card *card)
  206. {
  207. pr_debug("%s: called\n", __func__);
  208. mutex_lock(&card->updown_lock);
  209. if (atomic_inc_return(&card->users) == 1) {
  210. pr_debug("%s: real do\n", __func__);
  211. /* enable irq */
  212. gelic_card_set_irq_mask(card, card->irq_mask);
  213. /* start rx */
  214. gelic_card_enable_rxdmac(card);
  215. napi_enable(&card->napi);
  216. }
  217. mutex_unlock(&card->updown_lock);
  218. pr_debug("%s: done\n", __func__);
  219. }
  220. void gelic_card_down(struct gelic_card *card)
  221. {
  222. u64 mask;
  223. pr_debug("%s: called\n", __func__);
  224. mutex_lock(&card->updown_lock);
  225. if (atomic_dec_if_positive(&card->users) == 0) {
  226. pr_debug("%s: real do\n", __func__);
  227. napi_disable(&card->napi);
  228. /*
  229. * Disable irq. Wireless interrupts will
  230. * be disabled later if any
  231. */
  232. mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
  233. GELIC_CARD_WLAN_COMMAND_COMPLETED);
  234. gelic_card_set_irq_mask(card, mask);
  235. /* stop rx */
  236. gelic_card_disable_rxdmac(card);
  237. gelic_card_reset_chain(card, &card->rx_chain,
  238. card->descr + GELIC_NET_TX_DESCRIPTORS);
  239. /* stop tx */
  240. gelic_card_disable_txdmac(card);
  241. }
  242. mutex_unlock(&card->updown_lock);
  243. pr_debug("%s: done\n", __func__);
  244. }
  245. /**
  246. * gelic_card_free_chain - free descriptor chain
  247. * @card: card structure
  248. * @descr_in: address of desc
  249. */
  250. static void gelic_card_free_chain(struct gelic_card *card,
  251. struct gelic_descr *descr_in)
  252. {
  253. struct gelic_descr *descr;
  254. for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
  255. dma_unmap_single(ctodev(card), descr->bus_addr,
  256. GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
  257. descr->bus_addr = 0;
  258. }
  259. }
  260. /**
  261. * gelic_card_init_chain - links descriptor chain
  262. * @card: card structure
  263. * @chain: address of chain
  264. * @start_descr: address of descriptor array
  265. * @no: number of descriptors
  266. *
  267. * we manage a circular list that mirrors the hardware structure,
  268. * except that the hardware uses bus addresses.
  269. *
  270. * returns 0 on success, <0 on failure
  271. */
  272. static int gelic_card_init_chain(struct gelic_card *card,
  273. struct gelic_descr_chain *chain,
  274. struct gelic_descr *start_descr, int no)
  275. {
  276. int i;
  277. struct gelic_descr *descr;
  278. descr = start_descr;
  279. memset(descr, 0, sizeof(*descr) * no);
  280. /* set up the hardware pointers in each descriptor */
  281. for (i = 0; i < no; i++, descr++) {
  282. gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
  283. descr->bus_addr =
  284. dma_map_single(ctodev(card), descr,
  285. GELIC_DESCR_SIZE,
  286. DMA_BIDIRECTIONAL);
  287. if (!descr->bus_addr)
  288. goto iommu_error;
  289. descr->next = descr + 1;
  290. descr->prev = descr - 1;
  291. }
  292. /* make them as ring */
  293. (descr - 1)->next = start_descr;
  294. start_descr->prev = (descr - 1);
  295. /* chain bus addr of hw descriptor */
  296. descr = start_descr;
  297. for (i = 0; i < no; i++, descr++) {
  298. descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
  299. }
  300. chain->head = start_descr;
  301. chain->tail = start_descr;
  302. /* do not chain last hw descriptor */
  303. (descr - 1)->next_descr_addr = 0;
  304. return 0;
  305. iommu_error:
  306. for (i--, descr--; 0 <= i; i--, descr--)
  307. if (descr->bus_addr)
  308. dma_unmap_single(ctodev(card), descr->bus_addr,
  309. GELIC_DESCR_SIZE,
  310. DMA_BIDIRECTIONAL);
  311. return -ENOMEM;
  312. }
  313. /**
  314. * gelic_descr_prepare_rx - reinitializes a rx descriptor
  315. * @card: card structure
  316. * @descr: descriptor to re-init
  317. *
  318. * return 0 on success, <0 on failure
  319. *
  320. * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
  321. * Activate the descriptor state-wise
  322. */
  323. static int gelic_descr_prepare_rx(struct gelic_card *card,
  324. struct gelic_descr *descr)
  325. {
  326. int offset;
  327. unsigned int bufsize;
  328. if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
  329. dev_info(ctodev(card), "%s: ERROR status\n", __func__);
  330. /* we need to round up the buffer size to a multiple of 128 */
  331. bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
  332. /* and we need to have it 128 byte aligned, therefore we allocate a
  333. * bit more */
  334. descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
  335. if (!descr->skb) {
  336. descr->buf_addr = 0; /* tell DMAC don't touch memory */
  337. dev_info(ctodev(card),
  338. "%s:allocate skb failed !!\n", __func__);
  339. return -ENOMEM;
  340. }
  341. descr->buf_size = cpu_to_be32(bufsize);
  342. descr->dmac_cmd_status = 0;
  343. descr->result_size = 0;
  344. descr->valid_size = 0;
  345. descr->data_error = 0;
  346. offset = ((unsigned long)descr->skb->data) &
  347. (GELIC_NET_RXBUF_ALIGN - 1);
  348. if (offset)
  349. skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
  350. /* io-mmu-map the skb */
  351. descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
  352. descr->skb->data,
  353. GELIC_NET_MAX_MTU,
  354. DMA_FROM_DEVICE));
  355. if (!descr->buf_addr) {
  356. dev_kfree_skb_any(descr->skb);
  357. descr->skb = NULL;
  358. dev_info(ctodev(card),
  359. "%s:Could not iommu-map rx buffer\n", __func__);
  360. gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
  361. return -ENOMEM;
  362. } else {
  363. gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
  364. return 0;
  365. }
  366. }
  367. /**
  368. * gelic_card_release_rx_chain - free all skb of rx descr
  369. * @card: card structure
  370. *
  371. */
  372. static void gelic_card_release_rx_chain(struct gelic_card *card)
  373. {
  374. struct gelic_descr *descr = card->rx_chain.head;
  375. do {
  376. if (descr->skb) {
  377. dma_unmap_single(ctodev(card),
  378. be32_to_cpu(descr->buf_addr),
  379. descr->skb->len,
  380. DMA_FROM_DEVICE);
  381. descr->buf_addr = 0;
  382. dev_kfree_skb_any(descr->skb);
  383. descr->skb = NULL;
  384. gelic_descr_set_status(descr,
  385. GELIC_DESCR_DMA_NOT_IN_USE);
  386. }
  387. descr = descr->next;
  388. } while (descr != card->rx_chain.head);
  389. }
  390. /**
  391. * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
  392. * @card: card structure
  393. *
  394. * fills all descriptors in the rx chain: allocates skbs
  395. * and iommu-maps them.
  396. * returns 0 on success, < 0 on failure
  397. */
  398. static int gelic_card_fill_rx_chain(struct gelic_card *card)
  399. {
  400. struct gelic_descr *descr = card->rx_chain.head;
  401. int ret;
  402. do {
  403. if (!descr->skb) {
  404. ret = gelic_descr_prepare_rx(card, descr);
  405. if (ret)
  406. goto rewind;
  407. }
  408. descr = descr->next;
  409. } while (descr != card->rx_chain.head);
  410. return 0;
  411. rewind:
  412. gelic_card_release_rx_chain(card);
  413. return ret;
  414. }
  415. /**
  416. * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
  417. * @card: card structure
  418. *
  419. * returns 0 on success, < 0 on failure
  420. */
  421. static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
  422. {
  423. struct gelic_descr_chain *chain;
  424. int ret;
  425. chain = &card->rx_chain;
  426. ret = gelic_card_fill_rx_chain(card);
  427. chain->tail = card->rx_top->prev; /* point to the last */
  428. return ret;
  429. }
  430. /**
  431. * gelic_descr_release_tx - processes a used tx descriptor
  432. * @card: card structure
  433. * @descr: descriptor to release
  434. *
  435. * releases a used tx descriptor (unmapping, freeing of skb)
  436. */
  437. static void gelic_descr_release_tx(struct gelic_card *card,
  438. struct gelic_descr *descr)
  439. {
  440. struct sk_buff *skb = descr->skb;
  441. BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
  442. dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
  443. DMA_TO_DEVICE);
  444. dev_kfree_skb_any(skb);
  445. descr->buf_addr = 0;
  446. descr->buf_size = 0;
  447. descr->next_descr_addr = 0;
  448. descr->result_size = 0;
  449. descr->valid_size = 0;
  450. descr->data_status = 0;
  451. descr->data_error = 0;
  452. descr->skb = NULL;
  453. /* set descr status */
  454. gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
  455. }
  456. static void gelic_card_stop_queues(struct gelic_card *card)
  457. {
  458. netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
  459. if (card->netdev[GELIC_PORT_WIRELESS])
  460. netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
  461. }
  462. static void gelic_card_wake_queues(struct gelic_card *card)
  463. {
  464. netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
  465. if (card->netdev[GELIC_PORT_WIRELESS])
  466. netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
  467. }
  468. /**
  469. * gelic_card_release_tx_chain - processes sent tx descriptors
  470. * @card: adapter structure
  471. * @stop: net_stop sequence
  472. *
  473. * releases the tx descriptors that gelic has finished with
  474. */
  475. static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
  476. {
  477. struct gelic_descr_chain *tx_chain;
  478. enum gelic_descr_dma_status status;
  479. struct net_device *netdev;
  480. int release = 0;
  481. for (tx_chain = &card->tx_chain;
  482. tx_chain->head != tx_chain->tail && tx_chain->tail;
  483. tx_chain->tail = tx_chain->tail->next) {
  484. status = gelic_descr_get_status(tx_chain->tail);
  485. netdev = tx_chain->tail->skb->dev;
  486. switch (status) {
  487. case GELIC_DESCR_DMA_RESPONSE_ERROR:
  488. case GELIC_DESCR_DMA_PROTECTION_ERROR:
  489. case GELIC_DESCR_DMA_FORCE_END:
  490. if (printk_ratelimit())
  491. dev_info(ctodev(card),
  492. "%s: forcing end of tx descriptor " \
  493. "with status %x\n",
  494. __func__, status);
  495. netdev->stats.tx_dropped++;
  496. break;
  497. case GELIC_DESCR_DMA_COMPLETE:
  498. if (tx_chain->tail->skb) {
  499. netdev->stats.tx_packets++;
  500. netdev->stats.tx_bytes +=
  501. tx_chain->tail->skb->len;
  502. }
  503. break;
  504. case GELIC_DESCR_DMA_CARDOWNED:
  505. /* pending tx request */
  506. default:
  507. /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
  508. if (!stop)
  509. goto out;
  510. }
  511. gelic_descr_release_tx(card, tx_chain->tail);
  512. release ++;
  513. }
  514. out:
  515. if (!stop && release)
  516. gelic_card_wake_queues(card);
  517. }
  518. /**
  519. * gelic_net_set_multi - sets multicast addresses and promisc flags
  520. * @netdev: interface device structure
  521. *
  522. * gelic_net_set_multi configures multicast addresses as needed for the
  523. * netdev interface. It also sets up multicast, allmulti and promisc
  524. * flags appropriately
  525. */
  526. void gelic_net_set_multi(struct net_device *netdev)
  527. {
  528. struct gelic_card *card = netdev_card(netdev);
  529. struct netdev_hw_addr *ha;
  530. unsigned int i;
  531. uint8_t *p;
  532. u64 addr;
  533. int status;
  534. /* clear all multicast address */
  535. status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
  536. 0, 1);
  537. if (status)
  538. dev_err(ctodev(card),
  539. "lv1_net_remove_multicast_address failed %d\n",
  540. status);
  541. /* set broadcast address */
  542. status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
  543. GELIC_NET_BROADCAST_ADDR, 0);
  544. if (status)
  545. dev_err(ctodev(card),
  546. "lv1_net_add_multicast_address failed, %d\n",
  547. status);
  548. if ((netdev->flags & IFF_ALLMULTI) ||
  549. (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
  550. status = lv1_net_add_multicast_address(bus_id(card),
  551. dev_id(card),
  552. 0, 1);
  553. if (status)
  554. dev_err(ctodev(card),
  555. "lv1_net_add_multicast_address failed, %d\n",
  556. status);
  557. return;
  558. }
  559. /* set multicast addresses */
  560. netdev_for_each_mc_addr(ha, netdev) {
  561. addr = 0;
  562. p = ha->addr;
  563. for (i = 0; i < ETH_ALEN; i++) {
  564. addr <<= 8;
  565. addr |= *p++;
  566. }
  567. status = lv1_net_add_multicast_address(bus_id(card),
  568. dev_id(card),
  569. addr, 0);
  570. if (status)
  571. dev_err(ctodev(card),
  572. "lv1_net_add_multicast_address failed, %d\n",
  573. status);
  574. }
  575. }
  576. /**
  577. * gelic_net_stop - called upon ifconfig down
  578. * @netdev: interface device structure
  579. *
  580. * always returns 0
  581. */
  582. int gelic_net_stop(struct net_device *netdev)
  583. {
  584. struct gelic_card *card;
  585. pr_debug("%s: start\n", __func__);
  586. netif_stop_queue(netdev);
  587. netif_carrier_off(netdev);
  588. card = netdev_card(netdev);
  589. gelic_card_down(card);
  590. pr_debug("%s: done\n", __func__);
  591. return 0;
  592. }
  593. /**
  594. * gelic_card_get_next_tx_descr - returns the next available tx descriptor
  595. * @card: device structure to get descriptor from
  596. *
  597. * returns the address of the next descriptor, or NULL if not available.
  598. */
  599. static struct gelic_descr *
  600. gelic_card_get_next_tx_descr(struct gelic_card *card)
  601. {
  602. if (!card->tx_chain.head)
  603. return NULL;
  604. /* see if the next descriptor is free */
  605. if (card->tx_chain.tail != card->tx_chain.head->next &&
  606. gelic_descr_get_status(card->tx_chain.head) ==
  607. GELIC_DESCR_DMA_NOT_IN_USE)
  608. return card->tx_chain.head;
  609. else
  610. return NULL;
  611. }
  612. /**
  613. * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
  614. * @descr: descriptor structure to fill out
  615. * @skb: packet to consider
  616. *
  617. * fills out the command and status field of the descriptor structure,
  618. * depending on hardware checksum settings. This function assumes a wmb()
  619. * has executed before.
  620. */
  621. static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
  622. struct sk_buff *skb)
  623. {
  624. if (skb->ip_summed != CHECKSUM_PARTIAL)
  625. descr->dmac_cmd_status =
  626. cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
  627. GELIC_DESCR_TX_DMA_FRAME_TAIL);
  628. else {
  629. /* is packet ip?
  630. * if yes: tcp? udp? */
  631. if (skb->protocol == htons(ETH_P_IP)) {
  632. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  633. descr->dmac_cmd_status =
  634. cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
  635. GELIC_DESCR_TX_DMA_FRAME_TAIL);
  636. else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  637. descr->dmac_cmd_status =
  638. cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
  639. GELIC_DESCR_TX_DMA_FRAME_TAIL);
  640. else /*
  641. * the stack should checksum non-tcp and non-udp
  642. * packets on his own: NETIF_F_IP_CSUM
  643. */
  644. descr->dmac_cmd_status =
  645. cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
  646. GELIC_DESCR_TX_DMA_FRAME_TAIL);
  647. }
  648. }
  649. }
  650. static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
  651. unsigned short tag)
  652. {
  653. struct vlan_ethhdr *veth;
  654. static unsigned int c;
  655. if (skb_headroom(skb) < VLAN_HLEN) {
  656. struct sk_buff *sk_tmp = skb;
  657. pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
  658. skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
  659. if (!skb)
  660. return NULL;
  661. dev_kfree_skb_any(sk_tmp);
  662. }
  663. veth = skb_push(skb, VLAN_HLEN);
  664. /* Move the mac addresses to the top of buffer */
  665. memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
  666. veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
  667. veth->h_vlan_TCI = htons(tag);
  668. return skb;
  669. }
  670. /**
  671. * gelic_descr_prepare_tx - setup a descriptor for sending packets
  672. * @card: card structure
  673. * @descr: descriptor structure
  674. * @skb: packet to use
  675. *
  676. * returns 0 on success, <0 on failure.
  677. *
  678. */
  679. static int gelic_descr_prepare_tx(struct gelic_card *card,
  680. struct gelic_descr *descr,
  681. struct sk_buff *skb)
  682. {
  683. dma_addr_t buf;
  684. if (card->vlan_required) {
  685. struct sk_buff *skb_tmp;
  686. enum gelic_port_type type;
  687. type = netdev_port(skb->dev)->type;
  688. skb_tmp = gelic_put_vlan_tag(skb,
  689. card->vlan[type].tx);
  690. if (!skb_tmp)
  691. return -ENOMEM;
  692. skb = skb_tmp;
  693. }
  694. buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
  695. if (!buf) {
  696. dev_err(ctodev(card),
  697. "dma map 2 failed (%p, %i). Dropping packet\n",
  698. skb->data, skb->len);
  699. return -ENOMEM;
  700. }
  701. descr->buf_addr = cpu_to_be32(buf);
  702. descr->buf_size = cpu_to_be32(skb->len);
  703. descr->skb = skb;
  704. descr->data_status = 0;
  705. descr->next_descr_addr = 0; /* terminate hw descr */
  706. gelic_descr_set_tx_cmdstat(descr, skb);
  707. /* bump free descriptor pointer */
  708. card->tx_chain.head = descr->next;
  709. return 0;
  710. }
  711. /**
  712. * gelic_card_kick_txdma - enables TX DMA processing
  713. * @card: card structure
  714. * @descr: descriptor address to enable TX processing at
  715. *
  716. */
  717. static int gelic_card_kick_txdma(struct gelic_card *card,
  718. struct gelic_descr *descr)
  719. {
  720. int status = 0;
  721. if (card->tx_dma_progress)
  722. return 0;
  723. if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
  724. card->tx_dma_progress = 1;
  725. status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
  726. descr->bus_addr, 0);
  727. if (status) {
  728. card->tx_dma_progress = 0;
  729. dev_info(ctodev(card), "lv1_net_start_txdma failed," \
  730. "status=%d\n", status);
  731. }
  732. }
  733. return status;
  734. }
  735. /**
  736. * gelic_net_xmit - transmits a frame over the device
  737. * @skb: packet to send out
  738. * @netdev: interface device structure
  739. *
  740. * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
  741. */
  742. netdev_tx_t gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
  743. {
  744. struct gelic_card *card = netdev_card(netdev);
  745. struct gelic_descr *descr;
  746. int result;
  747. unsigned long flags;
  748. spin_lock_irqsave(&card->tx_lock, flags);
  749. gelic_card_release_tx_chain(card, 0);
  750. descr = gelic_card_get_next_tx_descr(card);
  751. if (!descr) {
  752. /*
  753. * no more descriptors free
  754. */
  755. gelic_card_stop_queues(card);
  756. spin_unlock_irqrestore(&card->tx_lock, flags);
  757. return NETDEV_TX_BUSY;
  758. }
  759. result = gelic_descr_prepare_tx(card, descr, skb);
  760. if (result) {
  761. /*
  762. * DMA map failed. As chances are that failure
  763. * would continue, just release skb and return
  764. */
  765. netdev->stats.tx_dropped++;
  766. dev_kfree_skb_any(skb);
  767. spin_unlock_irqrestore(&card->tx_lock, flags);
  768. return NETDEV_TX_OK;
  769. }
  770. /*
  771. * link this prepared descriptor to previous one
  772. * to achieve high performance
  773. */
  774. descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
  775. /*
  776. * as hardware descriptor is modified in the above lines,
  777. * ensure that the hardware sees it
  778. */
  779. wmb();
  780. if (gelic_card_kick_txdma(card, descr)) {
  781. /*
  782. * kick failed.
  783. * release descriptor which was just prepared
  784. */
  785. netdev->stats.tx_dropped++;
  786. /* don't trigger BUG_ON() in gelic_descr_release_tx */
  787. descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
  788. gelic_descr_release_tx(card, descr);
  789. /* reset head */
  790. card->tx_chain.head = descr;
  791. /* reset hw termination */
  792. descr->prev->next_descr_addr = 0;
  793. dev_info(ctodev(card), "%s: kick failure\n", __func__);
  794. }
  795. spin_unlock_irqrestore(&card->tx_lock, flags);
  796. return NETDEV_TX_OK;
  797. }
  798. /**
  799. * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
  800. * @descr: descriptor to process
  801. * @card: card structure
  802. * @netdev: net_device structure to be passed packet
  803. *
  804. * iommu-unmaps the skb, fills out skb structure and passes the data to the
  805. * stack. The descriptor state is not changed.
  806. */
  807. static void gelic_net_pass_skb_up(struct gelic_descr *descr,
  808. struct gelic_card *card,
  809. struct net_device *netdev)
  810. {
  811. struct sk_buff *skb = descr->skb;
  812. u32 data_status, data_error;
  813. data_status = be32_to_cpu(descr->data_status);
  814. data_error = be32_to_cpu(descr->data_error);
  815. /* unmap skb buffer */
  816. dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
  817. GELIC_NET_MAX_MTU,
  818. DMA_FROM_DEVICE);
  819. skb_put(skb, be32_to_cpu(descr->valid_size)?
  820. be32_to_cpu(descr->valid_size) :
  821. be32_to_cpu(descr->result_size));
  822. if (!descr->valid_size)
  823. dev_info(ctodev(card), "buffer full %x %x %x\n",
  824. be32_to_cpu(descr->result_size),
  825. be32_to_cpu(descr->buf_size),
  826. be32_to_cpu(descr->dmac_cmd_status));
  827. descr->skb = NULL;
  828. /*
  829. * the card put 2 bytes vlan tag in front
  830. * of the ethernet frame
  831. */
  832. skb_pull(skb, 2);
  833. skb->protocol = eth_type_trans(skb, netdev);
  834. /* checksum offload */
  835. if (netdev->features & NETIF_F_RXCSUM) {
  836. if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
  837. (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
  838. skb->ip_summed = CHECKSUM_UNNECESSARY;
  839. else
  840. skb_checksum_none_assert(skb);
  841. } else
  842. skb_checksum_none_assert(skb);
  843. /* update netdevice statistics */
  844. netdev->stats.rx_packets++;
  845. netdev->stats.rx_bytes += skb->len;
  846. /* pass skb up to stack */
  847. netif_receive_skb(skb);
  848. }
  849. /**
  850. * gelic_card_decode_one_descr - processes an rx descriptor
  851. * @card: card structure
  852. *
  853. * returns 1 if a packet has been sent to the stack, otherwise 0
  854. *
  855. * processes an rx descriptor by iommu-unmapping the data buffer and passing
  856. * the packet up to the stack
  857. */
  858. static int gelic_card_decode_one_descr(struct gelic_card *card)
  859. {
  860. enum gelic_descr_dma_status status;
  861. struct gelic_descr_chain *chain = &card->rx_chain;
  862. struct gelic_descr *descr = chain->head;
  863. struct net_device *netdev = NULL;
  864. int dmac_chain_ended;
  865. status = gelic_descr_get_status(descr);
  866. if (status == GELIC_DESCR_DMA_CARDOWNED)
  867. return 0;
  868. if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
  869. dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
  870. return 0;
  871. }
  872. /* netdevice select */
  873. if (card->vlan_required) {
  874. unsigned int i;
  875. u16 vid;
  876. vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
  877. for (i = 0; i < GELIC_PORT_MAX; i++) {
  878. if (card->vlan[i].rx == vid) {
  879. netdev = card->netdev[i];
  880. break;
  881. }
  882. }
  883. if (GELIC_PORT_MAX <= i) {
  884. pr_info("%s: unknown packet vid=%x\n", __func__, vid);
  885. goto refill;
  886. }
  887. } else
  888. netdev = card->netdev[GELIC_PORT_ETHERNET_0];
  889. if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
  890. (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
  891. (status == GELIC_DESCR_DMA_FORCE_END)) {
  892. dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
  893. status);
  894. netdev->stats.rx_dropped++;
  895. goto refill;
  896. }
  897. if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
  898. /*
  899. * Buffer full would occur if and only if
  900. * the frame length was longer than the size of this
  901. * descriptor's buffer. If the frame length was equal
  902. * to or shorter than buffer'size, FRAME_END condition
  903. * would occur.
  904. * Anyway this frame was longer than the MTU,
  905. * just drop it.
  906. */
  907. dev_info(ctodev(card), "overlength frame\n");
  908. goto refill;
  909. }
  910. /*
  911. * descriptors any other than FRAME_END here should
  912. * be treated as error.
  913. */
  914. if (status != GELIC_DESCR_DMA_FRAME_END) {
  915. dev_dbg(ctodev(card), "RX descriptor with state %x\n",
  916. status);
  917. goto refill;
  918. }
  919. /* ok, we've got a packet in descr */
  920. gelic_net_pass_skb_up(descr, card, netdev);
  921. refill:
  922. /* is the current descriptor terminated with next_descr == NULL? */
  923. dmac_chain_ended =
  924. be32_to_cpu(descr->dmac_cmd_status) &
  925. GELIC_DESCR_RX_DMA_CHAIN_END;
  926. /*
  927. * So that always DMAC can see the end
  928. * of the descriptor chain to avoid
  929. * from unwanted DMAC overrun.
  930. */
  931. descr->next_descr_addr = 0;
  932. /* change the descriptor state: */
  933. gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
  934. /*
  935. * this call can fail, but for now, just leave this
  936. * descriptor without skb
  937. */
  938. gelic_descr_prepare_rx(card, descr);
  939. chain->tail = descr;
  940. chain->head = descr->next;
  941. /*
  942. * Set this descriptor the end of the chain.
  943. */
  944. descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
  945. /*
  946. * If dmac chain was met, DMAC stopped.
  947. * thus re-enable it
  948. */
  949. if (dmac_chain_ended)
  950. gelic_card_enable_rxdmac(card);
  951. return 1;
  952. }
  953. /**
  954. * gelic_net_poll - NAPI poll function called by the stack to return packets
  955. * @napi: napi structure
  956. * @budget: number of packets we can pass to the stack at most
  957. *
  958. * returns the number of the processed packets
  959. *
  960. */
  961. static int gelic_net_poll(struct napi_struct *napi, int budget)
  962. {
  963. struct gelic_card *card = container_of(napi, struct gelic_card, napi);
  964. int packets_done = 0;
  965. while (packets_done < budget) {
  966. if (!gelic_card_decode_one_descr(card))
  967. break;
  968. packets_done++;
  969. }
  970. if (packets_done < budget) {
  971. napi_complete_done(napi, packets_done);
  972. gelic_card_rx_irq_on(card);
  973. }
  974. return packets_done;
  975. }
  976. /**
  977. * gelic_card_interrupt - event handler for gelic_net
  978. */
  979. static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
  980. {
  981. unsigned long flags;
  982. struct gelic_card *card = ptr;
  983. u64 status;
  984. status = card->irq_status;
  985. if (!status)
  986. return IRQ_NONE;
  987. status &= card->irq_mask;
  988. if (status & GELIC_CARD_RXINT) {
  989. gelic_card_rx_irq_off(card);
  990. napi_schedule(&card->napi);
  991. }
  992. if (status & GELIC_CARD_TXINT) {
  993. spin_lock_irqsave(&card->tx_lock, flags);
  994. card->tx_dma_progress = 0;
  995. gelic_card_release_tx_chain(card, 0);
  996. /* kick outstanding tx descriptor if any */
  997. gelic_card_kick_txdma(card, card->tx_chain.tail);
  998. spin_unlock_irqrestore(&card->tx_lock, flags);
  999. }
  1000. /* ether port status changed */
  1001. if (status & GELIC_CARD_PORT_STATUS_CHANGED)
  1002. gelic_card_get_ether_port_status(card, 1);
  1003. #ifdef CONFIG_GELIC_WIRELESS
  1004. if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
  1005. GELIC_CARD_WLAN_COMMAND_COMPLETED))
  1006. gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
  1007. #endif
  1008. return IRQ_HANDLED;
  1009. }
  1010. #ifdef CONFIG_NET_POLL_CONTROLLER
  1011. /**
  1012. * gelic_net_poll_controller - artificial interrupt for netconsole etc.
  1013. * @netdev: interface device structure
  1014. *
  1015. * see Documentation/networking/netconsole.txt
  1016. */
  1017. void gelic_net_poll_controller(struct net_device *netdev)
  1018. {
  1019. struct gelic_card *card = netdev_card(netdev);
  1020. gelic_card_set_irq_mask(card, 0);
  1021. gelic_card_interrupt(netdev->irq, netdev);
  1022. gelic_card_set_irq_mask(card, card->irq_mask);
  1023. }
  1024. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1025. /**
  1026. * gelic_net_open - called upon ifconfig up
  1027. * @netdev: interface device structure
  1028. *
  1029. * returns 0 on success, <0 on failure
  1030. *
  1031. * gelic_net_open allocates all the descriptors and memory needed for
  1032. * operation, sets up multicast list and enables interrupts
  1033. */
  1034. int gelic_net_open(struct net_device *netdev)
  1035. {
  1036. struct gelic_card *card = netdev_card(netdev);
  1037. dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
  1038. gelic_card_up(card);
  1039. netif_start_queue(netdev);
  1040. gelic_card_get_ether_port_status(card, 1);
  1041. dev_dbg(ctodev(card), " <- %s\n", __func__);
  1042. return 0;
  1043. }
  1044. void gelic_net_get_drvinfo(struct net_device *netdev,
  1045. struct ethtool_drvinfo *info)
  1046. {
  1047. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1048. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1049. }
  1050. static int gelic_ether_get_link_ksettings(struct net_device *netdev,
  1051. struct ethtool_link_ksettings *cmd)
  1052. {
  1053. struct gelic_card *card = netdev_card(netdev);
  1054. u32 supported, advertising;
  1055. gelic_card_get_ether_port_status(card, 0);
  1056. if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
  1057. cmd->base.duplex = DUPLEX_FULL;
  1058. else
  1059. cmd->base.duplex = DUPLEX_HALF;
  1060. switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
  1061. case GELIC_LV1_ETHER_SPEED_10:
  1062. cmd->base.speed = SPEED_10;
  1063. break;
  1064. case GELIC_LV1_ETHER_SPEED_100:
  1065. cmd->base.speed = SPEED_100;
  1066. break;
  1067. case GELIC_LV1_ETHER_SPEED_1000:
  1068. cmd->base.speed = SPEED_1000;
  1069. break;
  1070. default:
  1071. pr_info("%s: speed unknown\n", __func__);
  1072. cmd->base.speed = SPEED_10;
  1073. break;
  1074. }
  1075. supported = SUPPORTED_TP | SUPPORTED_Autoneg |
  1076. SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  1077. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  1078. SUPPORTED_1000baseT_Full;
  1079. advertising = supported;
  1080. if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
  1081. cmd->base.autoneg = AUTONEG_ENABLE;
  1082. } else {
  1083. cmd->base.autoneg = AUTONEG_DISABLE;
  1084. advertising &= ~ADVERTISED_Autoneg;
  1085. }
  1086. cmd->base.port = PORT_TP;
  1087. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  1088. supported);
  1089. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  1090. advertising);
  1091. return 0;
  1092. }
  1093. static int
  1094. gelic_ether_set_link_ksettings(struct net_device *netdev,
  1095. const struct ethtool_link_ksettings *cmd)
  1096. {
  1097. struct gelic_card *card = netdev_card(netdev);
  1098. u64 mode;
  1099. int ret;
  1100. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  1101. mode = GELIC_LV1_ETHER_AUTO_NEG;
  1102. } else {
  1103. switch (cmd->base.speed) {
  1104. case SPEED_10:
  1105. mode = GELIC_LV1_ETHER_SPEED_10;
  1106. break;
  1107. case SPEED_100:
  1108. mode = GELIC_LV1_ETHER_SPEED_100;
  1109. break;
  1110. case SPEED_1000:
  1111. mode = GELIC_LV1_ETHER_SPEED_1000;
  1112. break;
  1113. default:
  1114. return -EINVAL;
  1115. }
  1116. if (cmd->base.duplex == DUPLEX_FULL) {
  1117. mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
  1118. } else if (cmd->base.speed == SPEED_1000) {
  1119. pr_info("1000 half duplex is not supported.\n");
  1120. return -EINVAL;
  1121. }
  1122. }
  1123. ret = gelic_card_set_link_mode(card, mode);
  1124. if (ret)
  1125. return ret;
  1126. return 0;
  1127. }
  1128. static void gelic_net_get_wol(struct net_device *netdev,
  1129. struct ethtool_wolinfo *wol)
  1130. {
  1131. if (0 <= ps3_compare_firmware_version(2, 2, 0))
  1132. wol->supported = WAKE_MAGIC;
  1133. else
  1134. wol->supported = 0;
  1135. wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
  1136. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1137. }
  1138. static int gelic_net_set_wol(struct net_device *netdev,
  1139. struct ethtool_wolinfo *wol)
  1140. {
  1141. int status;
  1142. struct gelic_card *card;
  1143. u64 v1, v2;
  1144. if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
  1145. !capable(CAP_NET_ADMIN))
  1146. return -EPERM;
  1147. if (wol->wolopts & ~WAKE_MAGIC)
  1148. return -EINVAL;
  1149. card = netdev_card(netdev);
  1150. if (wol->wolopts & WAKE_MAGIC) {
  1151. status = lv1_net_control(bus_id(card), dev_id(card),
  1152. GELIC_LV1_SET_WOL,
  1153. GELIC_LV1_WOL_MAGIC_PACKET,
  1154. 0, GELIC_LV1_WOL_MP_ENABLE,
  1155. &v1, &v2);
  1156. if (status) {
  1157. pr_info("%s: enabling WOL failed %d\n", __func__,
  1158. status);
  1159. status = -EIO;
  1160. goto done;
  1161. }
  1162. status = lv1_net_control(bus_id(card), dev_id(card),
  1163. GELIC_LV1_SET_WOL,
  1164. GELIC_LV1_WOL_ADD_MATCH_ADDR,
  1165. 0, GELIC_LV1_WOL_MATCH_ALL,
  1166. &v1, &v2);
  1167. if (!status)
  1168. ps3_sys_manager_set_wol(1);
  1169. else {
  1170. pr_info("%s: enabling WOL filter failed %d\n",
  1171. __func__, status);
  1172. status = -EIO;
  1173. }
  1174. } else {
  1175. status = lv1_net_control(bus_id(card), dev_id(card),
  1176. GELIC_LV1_SET_WOL,
  1177. GELIC_LV1_WOL_MAGIC_PACKET,
  1178. 0, GELIC_LV1_WOL_MP_DISABLE,
  1179. &v1, &v2);
  1180. if (status) {
  1181. pr_info("%s: disabling WOL failed %d\n", __func__,
  1182. status);
  1183. status = -EIO;
  1184. goto done;
  1185. }
  1186. status = lv1_net_control(bus_id(card), dev_id(card),
  1187. GELIC_LV1_SET_WOL,
  1188. GELIC_LV1_WOL_DELETE_MATCH_ADDR,
  1189. 0, GELIC_LV1_WOL_MATCH_ALL,
  1190. &v1, &v2);
  1191. if (!status)
  1192. ps3_sys_manager_set_wol(0);
  1193. else {
  1194. pr_info("%s: removing WOL filter failed %d\n",
  1195. __func__, status);
  1196. status = -EIO;
  1197. }
  1198. }
  1199. done:
  1200. return status;
  1201. }
  1202. static const struct ethtool_ops gelic_ether_ethtool_ops = {
  1203. .get_drvinfo = gelic_net_get_drvinfo,
  1204. .get_link = ethtool_op_get_link,
  1205. .get_wol = gelic_net_get_wol,
  1206. .set_wol = gelic_net_set_wol,
  1207. .get_link_ksettings = gelic_ether_get_link_ksettings,
  1208. .set_link_ksettings = gelic_ether_set_link_ksettings,
  1209. };
  1210. /**
  1211. * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
  1212. * function (to be called not under interrupt status)
  1213. * @work: work is context of tx timout task
  1214. *
  1215. * called as task when tx hangs, resets interface (if interface is up)
  1216. */
  1217. static void gelic_net_tx_timeout_task(struct work_struct *work)
  1218. {
  1219. struct gelic_card *card =
  1220. container_of(work, struct gelic_card, tx_timeout_task);
  1221. struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
  1222. dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
  1223. if (!(netdev->flags & IFF_UP))
  1224. goto out;
  1225. netif_device_detach(netdev);
  1226. gelic_net_stop(netdev);
  1227. gelic_net_open(netdev);
  1228. netif_device_attach(netdev);
  1229. out:
  1230. atomic_dec(&card->tx_timeout_task_counter);
  1231. }
  1232. /**
  1233. * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
  1234. * @netdev: interface device structure
  1235. *
  1236. * called, if tx hangs. Schedules a task that resets the interface
  1237. */
  1238. void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  1239. {
  1240. struct gelic_card *card;
  1241. card = netdev_card(netdev);
  1242. atomic_inc(&card->tx_timeout_task_counter);
  1243. if (netdev->flags & IFF_UP)
  1244. schedule_work(&card->tx_timeout_task);
  1245. else
  1246. atomic_dec(&card->tx_timeout_task_counter);
  1247. }
  1248. static const struct net_device_ops gelic_netdevice_ops = {
  1249. .ndo_open = gelic_net_open,
  1250. .ndo_stop = gelic_net_stop,
  1251. .ndo_start_xmit = gelic_net_xmit,
  1252. .ndo_set_rx_mode = gelic_net_set_multi,
  1253. .ndo_tx_timeout = gelic_net_tx_timeout,
  1254. .ndo_set_mac_address = eth_mac_addr,
  1255. .ndo_validate_addr = eth_validate_addr,
  1256. #ifdef CONFIG_NET_POLL_CONTROLLER
  1257. .ndo_poll_controller = gelic_net_poll_controller,
  1258. #endif
  1259. };
  1260. /**
  1261. * gelic_ether_setup_netdev_ops - initialization of net_device operations
  1262. * @netdev: net_device structure
  1263. *
  1264. * fills out function pointers in the net_device structure
  1265. */
  1266. static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
  1267. struct napi_struct *napi)
  1268. {
  1269. netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
  1270. /* NAPI */
  1271. netif_napi_add(netdev, napi, gelic_net_poll, NAPI_POLL_WEIGHT);
  1272. netdev->ethtool_ops = &gelic_ether_ethtool_ops;
  1273. netdev->netdev_ops = &gelic_netdevice_ops;
  1274. }
  1275. /**
  1276. * gelic_ether_setup_netdev - initialization of net_device
  1277. * @netdev: net_device structure
  1278. * @card: card structure
  1279. *
  1280. * Returns 0 on success or <0 on failure
  1281. *
  1282. * gelic_ether_setup_netdev initializes the net_device structure
  1283. * and register it.
  1284. **/
  1285. int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
  1286. {
  1287. int status;
  1288. u64 v1, v2;
  1289. netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  1290. netdev->features = NETIF_F_IP_CSUM;
  1291. if (GELIC_CARD_RX_CSUM_DEFAULT)
  1292. netdev->features |= NETIF_F_RXCSUM;
  1293. status = lv1_net_control(bus_id(card), dev_id(card),
  1294. GELIC_LV1_GET_MAC_ADDRESS,
  1295. 0, 0, 0, &v1, &v2);
  1296. v1 <<= 16;
  1297. if (status || !is_valid_ether_addr((u8 *)&v1)) {
  1298. dev_info(ctodev(card),
  1299. "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
  1300. __func__, status);
  1301. return -EINVAL;
  1302. }
  1303. memcpy(netdev->dev_addr, &v1, ETH_ALEN);
  1304. if (card->vlan_required) {
  1305. netdev->hard_header_len += VLAN_HLEN;
  1306. /*
  1307. * As vlan is internally used,
  1308. * we can not receive vlan packets
  1309. */
  1310. netdev->features |= NETIF_F_VLAN_CHALLENGED;
  1311. }
  1312. /* MTU range: 64 - 1518 */
  1313. netdev->min_mtu = GELIC_NET_MIN_MTU;
  1314. netdev->max_mtu = GELIC_NET_MAX_MTU;
  1315. status = register_netdev(netdev);
  1316. if (status) {
  1317. dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
  1318. __func__, netdev->name, status);
  1319. return status;
  1320. }
  1321. dev_info(ctodev(card), "%s: MAC addr %pM\n",
  1322. netdev->name, netdev->dev_addr);
  1323. return 0;
  1324. }
  1325. /**
  1326. * gelic_alloc_card_net - allocates net_device and card structure
  1327. *
  1328. * returns the card structure or NULL in case of errors
  1329. *
  1330. * the card and net_device structures are linked to each other
  1331. */
  1332. #define GELIC_ALIGN (32)
  1333. static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
  1334. {
  1335. struct gelic_card *card;
  1336. struct gelic_port *port;
  1337. void *p;
  1338. size_t alloc_size;
  1339. /*
  1340. * gelic requires dma descriptor is 32 bytes aligned and
  1341. * the hypervisor requires irq_status is 8 bytes aligned.
  1342. */
  1343. BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
  1344. BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
  1345. alloc_size =
  1346. sizeof(struct gelic_card) +
  1347. sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
  1348. sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
  1349. GELIC_ALIGN - 1;
  1350. p = kzalloc(alloc_size, GFP_KERNEL);
  1351. if (!p)
  1352. return NULL;
  1353. card = PTR_ALIGN(p, GELIC_ALIGN);
  1354. card->unalign = p;
  1355. /*
  1356. * alloc netdev
  1357. */
  1358. *netdev = alloc_etherdev(sizeof(struct gelic_port));
  1359. if (!*netdev) {
  1360. kfree(card->unalign);
  1361. return NULL;
  1362. }
  1363. port = netdev_priv(*netdev);
  1364. /* gelic_port */
  1365. port->netdev = *netdev;
  1366. port->card = card;
  1367. port->type = GELIC_PORT_ETHERNET_0;
  1368. /* gelic_card */
  1369. card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
  1370. INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
  1371. init_waitqueue_head(&card->waitq);
  1372. atomic_set(&card->tx_timeout_task_counter, 0);
  1373. mutex_init(&card->updown_lock);
  1374. atomic_set(&card->users, 0);
  1375. return card;
  1376. }
  1377. static void gelic_card_get_vlan_info(struct gelic_card *card)
  1378. {
  1379. u64 v1, v2;
  1380. int status;
  1381. unsigned int i;
  1382. struct {
  1383. int tx;
  1384. int rx;
  1385. } vlan_id_ix[2] = {
  1386. [GELIC_PORT_ETHERNET_0] = {
  1387. .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
  1388. .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
  1389. },
  1390. [GELIC_PORT_WIRELESS] = {
  1391. .tx = GELIC_LV1_VLAN_TX_WIRELESS,
  1392. .rx = GELIC_LV1_VLAN_RX_WIRELESS
  1393. }
  1394. };
  1395. for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
  1396. /* tx tag */
  1397. status = lv1_net_control(bus_id(card), dev_id(card),
  1398. GELIC_LV1_GET_VLAN_ID,
  1399. vlan_id_ix[i].tx,
  1400. 0, 0, &v1, &v2);
  1401. if (status || !v1) {
  1402. if (status != LV1_NO_ENTRY)
  1403. dev_dbg(ctodev(card),
  1404. "get vlan id for tx(%d) failed(%d)\n",
  1405. vlan_id_ix[i].tx, status);
  1406. card->vlan[i].tx = 0;
  1407. card->vlan[i].rx = 0;
  1408. continue;
  1409. }
  1410. card->vlan[i].tx = (u16)v1;
  1411. /* rx tag */
  1412. status = lv1_net_control(bus_id(card), dev_id(card),
  1413. GELIC_LV1_GET_VLAN_ID,
  1414. vlan_id_ix[i].rx,
  1415. 0, 0, &v1, &v2);
  1416. if (status || !v1) {
  1417. if (status != LV1_NO_ENTRY)
  1418. dev_info(ctodev(card),
  1419. "get vlan id for rx(%d) failed(%d)\n",
  1420. vlan_id_ix[i].rx, status);
  1421. card->vlan[i].tx = 0;
  1422. card->vlan[i].rx = 0;
  1423. continue;
  1424. }
  1425. card->vlan[i].rx = (u16)v1;
  1426. dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
  1427. i, card->vlan[i].tx, card->vlan[i].rx);
  1428. }
  1429. if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
  1430. BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
  1431. card->vlan_required = 1;
  1432. } else
  1433. card->vlan_required = 0;
  1434. /* check wirelss capable firmware */
  1435. if (ps3_compare_firmware_version(1, 6, 0) < 0) {
  1436. card->vlan[GELIC_PORT_WIRELESS].tx = 0;
  1437. card->vlan[GELIC_PORT_WIRELESS].rx = 0;
  1438. }
  1439. dev_info(ctodev(card), "internal vlan %s\n",
  1440. card->vlan_required? "enabled" : "disabled");
  1441. }
  1442. /**
  1443. * ps3_gelic_driver_probe - add a device to the control of this driver
  1444. */
  1445. static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
  1446. {
  1447. struct gelic_card *card;
  1448. struct net_device *netdev;
  1449. int result;
  1450. pr_debug("%s: called\n", __func__);
  1451. udbg_shutdown_ps3gelic();
  1452. result = ps3_open_hv_device(dev);
  1453. if (result) {
  1454. dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
  1455. __func__);
  1456. goto fail_open;
  1457. }
  1458. result = ps3_dma_region_create(dev->d_region);
  1459. if (result) {
  1460. dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
  1461. __func__, result);
  1462. BUG_ON("check region type");
  1463. goto fail_dma_region;
  1464. }
  1465. /* alloc card/netdevice */
  1466. card = gelic_alloc_card_net(&netdev);
  1467. if (!card) {
  1468. dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
  1469. __func__);
  1470. result = -ENOMEM;
  1471. goto fail_alloc_card;
  1472. }
  1473. ps3_system_bus_set_drvdata(dev, card);
  1474. card->dev = dev;
  1475. /* get internal vlan info */
  1476. gelic_card_get_vlan_info(card);
  1477. card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
  1478. /* setup interrupt */
  1479. result = lv1_net_set_interrupt_status_indicator(bus_id(card),
  1480. dev_id(card),
  1481. ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
  1482. 0);
  1483. if (result) {
  1484. dev_dbg(&dev->core,
  1485. "%s:set_interrupt_status_indicator failed: %s\n",
  1486. __func__, ps3_result(result));
  1487. result = -EIO;
  1488. goto fail_status_indicator;
  1489. }
  1490. result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
  1491. &card->irq);
  1492. if (result) {
  1493. dev_info(ctodev(card),
  1494. "%s:gelic_net_open_device failed (%d)\n",
  1495. __func__, result);
  1496. result = -EPERM;
  1497. goto fail_alloc_irq;
  1498. }
  1499. result = request_irq(card->irq, gelic_card_interrupt,
  1500. 0, netdev->name, card);
  1501. if (result) {
  1502. dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
  1503. __func__, result);
  1504. goto fail_request_irq;
  1505. }
  1506. /* setup card structure */
  1507. card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
  1508. GELIC_CARD_PORT_STATUS_CHANGED;
  1509. result = gelic_card_init_chain(card, &card->tx_chain,
  1510. card->descr, GELIC_NET_TX_DESCRIPTORS);
  1511. if (result)
  1512. goto fail_alloc_tx;
  1513. result = gelic_card_init_chain(card, &card->rx_chain,
  1514. card->descr + GELIC_NET_TX_DESCRIPTORS,
  1515. GELIC_NET_RX_DESCRIPTORS);
  1516. if (result)
  1517. goto fail_alloc_rx;
  1518. /* head of chain */
  1519. card->tx_top = card->tx_chain.head;
  1520. card->rx_top = card->rx_chain.head;
  1521. dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
  1522. card->rx_top, card->tx_top, sizeof(struct gelic_descr),
  1523. GELIC_NET_RX_DESCRIPTORS);
  1524. /* allocate rx skbs */
  1525. result = gelic_card_alloc_rx_skbs(card);
  1526. if (result)
  1527. goto fail_alloc_skbs;
  1528. spin_lock_init(&card->tx_lock);
  1529. card->tx_dma_progress = 0;
  1530. /* setup net_device structure */
  1531. netdev->irq = card->irq;
  1532. SET_NETDEV_DEV(netdev, &card->dev->core);
  1533. gelic_ether_setup_netdev_ops(netdev, &card->napi);
  1534. result = gelic_net_setup_netdev(netdev, card);
  1535. if (result) {
  1536. dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
  1537. __func__, result);
  1538. goto fail_setup_netdev;
  1539. }
  1540. #ifdef CONFIG_GELIC_WIRELESS
  1541. result = gelic_wl_driver_probe(card);
  1542. if (result) {
  1543. dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
  1544. goto fail_setup_netdev;
  1545. }
  1546. #endif
  1547. pr_debug("%s: done\n", __func__);
  1548. return 0;
  1549. fail_setup_netdev:
  1550. fail_alloc_skbs:
  1551. gelic_card_free_chain(card, card->rx_chain.head);
  1552. fail_alloc_rx:
  1553. gelic_card_free_chain(card, card->tx_chain.head);
  1554. fail_alloc_tx:
  1555. free_irq(card->irq, card);
  1556. netdev->irq = 0;
  1557. fail_request_irq:
  1558. ps3_sb_event_receive_port_destroy(dev, card->irq);
  1559. fail_alloc_irq:
  1560. lv1_net_set_interrupt_status_indicator(bus_id(card),
  1561. bus_id(card),
  1562. 0, 0);
  1563. fail_status_indicator:
  1564. ps3_system_bus_set_drvdata(dev, NULL);
  1565. kfree(netdev_card(netdev)->unalign);
  1566. free_netdev(netdev);
  1567. fail_alloc_card:
  1568. ps3_dma_region_free(dev->d_region);
  1569. fail_dma_region:
  1570. ps3_close_hv_device(dev);
  1571. fail_open:
  1572. return result;
  1573. }
  1574. /**
  1575. * ps3_gelic_driver_remove - remove a device from the control of this driver
  1576. */
  1577. static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
  1578. {
  1579. struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
  1580. struct net_device *netdev0;
  1581. pr_debug("%s: called\n", __func__);
  1582. /* set auto-negotiation */
  1583. gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
  1584. #ifdef CONFIG_GELIC_WIRELESS
  1585. gelic_wl_driver_remove(card);
  1586. #endif
  1587. /* stop interrupt */
  1588. gelic_card_set_irq_mask(card, 0);
  1589. /* turn off DMA, force end */
  1590. gelic_card_disable_rxdmac(card);
  1591. gelic_card_disable_txdmac(card);
  1592. /* release chains */
  1593. gelic_card_release_tx_chain(card, 1);
  1594. gelic_card_release_rx_chain(card);
  1595. gelic_card_free_chain(card, card->tx_top);
  1596. gelic_card_free_chain(card, card->rx_top);
  1597. netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
  1598. /* disconnect event port */
  1599. free_irq(card->irq, card);
  1600. netdev0->irq = 0;
  1601. ps3_sb_event_receive_port_destroy(card->dev, card->irq);
  1602. wait_event(card->waitq,
  1603. atomic_read(&card->tx_timeout_task_counter) == 0);
  1604. lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
  1605. 0 , 0);
  1606. unregister_netdev(netdev0);
  1607. kfree(netdev_card(netdev0)->unalign);
  1608. free_netdev(netdev0);
  1609. ps3_system_bus_set_drvdata(dev, NULL);
  1610. ps3_dma_region_free(dev->d_region);
  1611. ps3_close_hv_device(dev);
  1612. pr_debug("%s: done\n", __func__);
  1613. return 0;
  1614. }
  1615. static struct ps3_system_bus_driver ps3_gelic_driver = {
  1616. .match_id = PS3_MATCH_ID_GELIC,
  1617. .probe = ps3_gelic_driver_probe,
  1618. .remove = ps3_gelic_driver_remove,
  1619. .shutdown = ps3_gelic_driver_remove,
  1620. .core.name = "ps3_gelic_driver",
  1621. .core.owner = THIS_MODULE,
  1622. };
  1623. static int __init ps3_gelic_driver_init (void)
  1624. {
  1625. return firmware_has_feature(FW_FEATURE_PS3_LV1)
  1626. ? ps3_system_bus_driver_register(&ps3_gelic_driver)
  1627. : -ENODEV;
  1628. }
  1629. static void __exit ps3_gelic_driver_exit (void)
  1630. {
  1631. ps3_system_bus_driver_unregister(&ps3_gelic_driver);
  1632. }
  1633. module_init(ps3_gelic_driver_init);
  1634. module_exit(ps3_gelic_driver_exit);
  1635. MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);