octeon_network.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. **********************************************************************/
  19. /*! \file octeon_network.h
  20. * \brief Host NIC Driver: Structure and Macro definitions used by NIC Module.
  21. */
  22. #ifndef __OCTEON_NETWORK_H__
  23. #define __OCTEON_NETWORK_H__
  24. #include <linux/ptp_clock_kernel.h>
  25. #define LIO_MAX_MTU_SIZE (OCTNET_MAX_FRM_SIZE - OCTNET_FRM_HEADER_SIZE)
  26. #define LIO_MIN_MTU_SIZE ETH_MIN_MTU
  27. /* Bit mask values for lio->ifstate */
  28. #define LIO_IFSTATE_DROQ_OPS 0x01
  29. #define LIO_IFSTATE_REGISTERED 0x02
  30. #define LIO_IFSTATE_RUNNING 0x04
  31. #define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
  32. #define LIO_IFSTATE_RESETTING 0x10
  33. struct liquidio_if_cfg_resp {
  34. u64 rh;
  35. struct liquidio_if_cfg_info cfg_info;
  36. u64 status;
  37. };
  38. #define LIO_IFCFG_WAIT_TIME 3000 /* In milli seconds */
  39. #define LIQUIDIO_NDEV_STATS_POLL_TIME_MS 200
  40. /* Structure of a node in list of gather components maintained by
  41. * NIC driver for each network device.
  42. */
  43. struct octnic_gather {
  44. /* List manipulation. Next and prev pointers. */
  45. struct list_head list;
  46. /* Size of the gather component at sg in bytes. */
  47. int sg_size;
  48. /* Number of bytes that sg was adjusted to make it 8B-aligned. */
  49. int adjust;
  50. /* Gather component that can accommodate max sized fragment list
  51. * received from the IP layer.
  52. */
  53. struct octeon_sg_entry *sg;
  54. dma_addr_t sg_dma_ptr;
  55. };
  56. struct oct_nic_stats_resp {
  57. u64 rh;
  58. struct oct_link_stats stats;
  59. u64 status;
  60. };
  61. struct oct_nic_vf_stats_resp {
  62. u64 rh;
  63. u64 spoofmac_cnt;
  64. u64 status;
  65. };
  66. struct oct_nic_stats_ctrl {
  67. struct completion complete;
  68. struct net_device *netdev;
  69. };
  70. struct oct_nic_seapi_resp {
  71. u64 rh;
  72. union {
  73. u32 fec_setting;
  74. u32 speed;
  75. };
  76. u64 status;
  77. };
  78. /** LiquidIO per-interface network private data */
  79. struct lio {
  80. /** State of the interface. Rx/Tx happens only in the RUNNING state. */
  81. atomic_t ifstate;
  82. /** Octeon Interface index number. This device will be represented as
  83. * oct<ifidx> in the system.
  84. */
  85. int ifidx;
  86. /** Octeon Input queue to use to transmit for this network interface. */
  87. int txq;
  88. /** Octeon Output queue from which pkts arrive
  89. * for this network interface.
  90. */
  91. int rxq;
  92. /** Guards each glist */
  93. spinlock_t *glist_lock;
  94. /** Array of gather component linked lists */
  95. struct list_head *glist;
  96. void **glists_virt_base;
  97. dma_addr_t *glists_dma_base;
  98. u32 glist_entry_size;
  99. /** Pointer to the NIC properties for the Octeon device this network
  100. * interface is associated with.
  101. */
  102. struct octdev_props *octprops;
  103. /** Pointer to the octeon device structure. */
  104. struct octeon_device *oct_dev;
  105. struct net_device *netdev;
  106. /** Link information sent by the core application for this interface. */
  107. struct oct_link_info linfo;
  108. /** counter of link changes */
  109. u64 link_changes;
  110. /** Size of Tx queue for this octeon device. */
  111. u32 tx_qsize;
  112. /** Size of Rx queue for this octeon device. */
  113. u32 rx_qsize;
  114. /** Size of MTU this octeon device. */
  115. u32 mtu;
  116. /** msg level flag per interface. */
  117. u32 msg_enable;
  118. /** Copy of Interface capabilities: TSO, TSO6, LRO, Chescksums . */
  119. u64 dev_capability;
  120. /* Copy of transmit encapsulation capabilities:
  121. * TSO, TSO6, Checksums for this device for Kernel
  122. * 3.10.0 onwards
  123. */
  124. u64 enc_dev_capability;
  125. /** Copy of beacaon reg in phy */
  126. u32 phy_beacon_val;
  127. /** Copy of ctrl reg in phy */
  128. u32 led_ctrl_val;
  129. /* PTP clock information */
  130. struct ptp_clock_info ptp_info;
  131. struct ptp_clock *ptp_clock;
  132. s64 ptp_adjust;
  133. /* for atomic access to Octeon PTP reg and data struct */
  134. spinlock_t ptp_lock;
  135. /* Interface info */
  136. u32 intf_open;
  137. /* work queue for txq status */
  138. struct cavium_wq txq_status_wq;
  139. /* work queue for rxq oom status */
  140. struct cavium_wq rxq_status_wq[MAX_POSSIBLE_OCTEON_OUTPUT_QUEUES];
  141. /* work queue for link status */
  142. struct cavium_wq link_status_wq;
  143. /* work queue to regularly send local time to octeon firmware */
  144. struct cavium_wq sync_octeon_time_wq;
  145. int netdev_uc_count;
  146. struct cavium_wk stats_wk;
  147. };
  148. #define LIO_SIZE (sizeof(struct lio))
  149. #define GET_LIO(netdev) ((struct lio *)netdev_priv(netdev))
  150. #define LIO_MAX_CORES 16
  151. /**
  152. * \brief Enable or disable feature
  153. * @param netdev pointer to network device
  154. * @param cmd Command that just requires acknowledgment
  155. * @param param1 Parameter to command
  156. */
  157. int liquidio_set_feature(struct net_device *netdev, int cmd, u16 param1);
  158. int setup_rx_oom_poll_fn(struct net_device *netdev);
  159. void cleanup_rx_oom_poll_fn(struct net_device *netdev);
  160. /**
  161. * \brief Link control command completion callback
  162. * @param nctrl_ptr pointer to control packet structure
  163. *
  164. * This routine is called by the callback function when a ctrl pkt sent to
  165. * core app completes. The nctrl_ptr contains a copy of the command type
  166. * and data sent to the core app. This routine is only called if the ctrl
  167. * pkt was sent successfully to the core app.
  168. */
  169. void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr);
  170. int liquidio_setup_io_queues(struct octeon_device *octeon_dev, int ifidx,
  171. u32 num_iqs, u32 num_oqs);
  172. irqreturn_t liquidio_msix_intr_handler(int irq __attribute__((unused)),
  173. void *dev);
  174. int octeon_setup_interrupt(struct octeon_device *oct, u32 num_ioqs);
  175. void lio_fetch_stats(struct work_struct *work);
  176. int lio_wait_for_clean_oq(struct octeon_device *oct);
  177. /**
  178. * \brief Register ethtool operations
  179. * @param netdev pointer to network device
  180. */
  181. void liquidio_set_ethtool_ops(struct net_device *netdev);
  182. void lio_delete_glists(struct lio *lio);
  183. int lio_setup_glists(struct octeon_device *oct, struct lio *lio, int num_qs);
  184. int liquidio_get_speed(struct lio *lio);
  185. int liquidio_set_speed(struct lio *lio, int speed);
  186. int liquidio_get_fec(struct lio *lio);
  187. int liquidio_set_fec(struct lio *lio, int on_off);
  188. /**
  189. * \brief Net device change_mtu
  190. * @param netdev network device
  191. */
  192. int liquidio_change_mtu(struct net_device *netdev, int new_mtu);
  193. #define LIO_CHANGE_MTU_SUCCESS 1
  194. #define LIO_CHANGE_MTU_FAIL 2
  195. #define SKB_ADJ_MASK 0x3F
  196. #define SKB_ADJ (SKB_ADJ_MASK + 1)
  197. #define MIN_SKB_SIZE 256 /* 8 bytes and more - 8 bytes for PTP */
  198. #define LIO_RXBUFFER_SZ 2048
  199. static inline void
  200. *recv_buffer_alloc(struct octeon_device *oct,
  201. struct octeon_skb_page_info *pg_info)
  202. {
  203. struct page *page;
  204. struct sk_buff *skb;
  205. struct octeon_skb_page_info *skb_pg_info;
  206. page = alloc_page(GFP_ATOMIC);
  207. if (unlikely(!page))
  208. return NULL;
  209. skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
  210. if (unlikely(!skb)) {
  211. __free_page(page);
  212. pg_info->page = NULL;
  213. return NULL;
  214. }
  215. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  216. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  217. skb_reserve(skb, r);
  218. }
  219. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  220. /* Get DMA info */
  221. pg_info->dma = dma_map_page(&oct->pci_dev->dev, page, 0,
  222. PAGE_SIZE, DMA_FROM_DEVICE);
  223. /* Mapping failed!! */
  224. if (dma_mapping_error(&oct->pci_dev->dev, pg_info->dma)) {
  225. __free_page(page);
  226. dev_kfree_skb_any((struct sk_buff *)skb);
  227. pg_info->page = NULL;
  228. return NULL;
  229. }
  230. pg_info->page = page;
  231. pg_info->page_offset = 0;
  232. skb_pg_info->page = page;
  233. skb_pg_info->page_offset = 0;
  234. skb_pg_info->dma = pg_info->dma;
  235. return (void *)skb;
  236. }
  237. static inline void
  238. *recv_buffer_fast_alloc(u32 size)
  239. {
  240. struct sk_buff *skb;
  241. struct octeon_skb_page_info *skb_pg_info;
  242. skb = dev_alloc_skb(size + SKB_ADJ);
  243. if (unlikely(!skb))
  244. return NULL;
  245. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  246. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  247. skb_reserve(skb, r);
  248. }
  249. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  250. skb_pg_info->page = NULL;
  251. skb_pg_info->page_offset = 0;
  252. skb_pg_info->dma = 0;
  253. return skb;
  254. }
  255. static inline int
  256. recv_buffer_recycle(struct octeon_device *oct, void *buf)
  257. {
  258. struct octeon_skb_page_info *pg_info = buf;
  259. if (!pg_info->page) {
  260. dev_err(&oct->pci_dev->dev, "%s: pg_info->page NULL\n",
  261. __func__);
  262. return -ENOMEM;
  263. }
  264. if (unlikely(page_count(pg_info->page) != 1) ||
  265. unlikely(page_to_nid(pg_info->page) != numa_node_id())) {
  266. dma_unmap_page(&oct->pci_dev->dev,
  267. pg_info->dma, (PAGE_SIZE << 0),
  268. DMA_FROM_DEVICE);
  269. pg_info->dma = 0;
  270. pg_info->page = NULL;
  271. pg_info->page_offset = 0;
  272. return -ENOMEM;
  273. }
  274. /* Flip to other half of the buffer */
  275. if (pg_info->page_offset == 0)
  276. pg_info->page_offset = LIO_RXBUFFER_SZ;
  277. else
  278. pg_info->page_offset = 0;
  279. page_ref_inc(pg_info->page);
  280. return 0;
  281. }
  282. static inline void
  283. *recv_buffer_reuse(struct octeon_device *oct, void *buf)
  284. {
  285. struct octeon_skb_page_info *pg_info = buf, *skb_pg_info;
  286. struct sk_buff *skb;
  287. skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
  288. if (unlikely(!skb)) {
  289. dma_unmap_page(&oct->pci_dev->dev,
  290. pg_info->dma, (PAGE_SIZE << 0),
  291. DMA_FROM_DEVICE);
  292. return NULL;
  293. }
  294. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  295. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  296. skb_reserve(skb, r);
  297. }
  298. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  299. skb_pg_info->page = pg_info->page;
  300. skb_pg_info->page_offset = pg_info->page_offset;
  301. skb_pg_info->dma = pg_info->dma;
  302. return skb;
  303. }
  304. static inline void
  305. recv_buffer_destroy(void *buffer, struct octeon_skb_page_info *pg_info)
  306. {
  307. struct sk_buff *skb = (struct sk_buff *)buffer;
  308. put_page(pg_info->page);
  309. pg_info->dma = 0;
  310. pg_info->page = NULL;
  311. pg_info->page_offset = 0;
  312. if (skb)
  313. dev_kfree_skb_any(skb);
  314. }
  315. static inline void recv_buffer_free(void *buffer)
  316. {
  317. struct sk_buff *skb = (struct sk_buff *)buffer;
  318. struct octeon_skb_page_info *pg_info;
  319. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  320. if (pg_info->page) {
  321. put_page(pg_info->page);
  322. pg_info->dma = 0;
  323. pg_info->page = NULL;
  324. pg_info->page_offset = 0;
  325. }
  326. dev_kfree_skb_any((struct sk_buff *)buffer);
  327. }
  328. static inline void
  329. recv_buffer_fast_free(void *buffer)
  330. {
  331. dev_kfree_skb_any((struct sk_buff *)buffer);
  332. }
  333. static inline void tx_buffer_free(void *buffer)
  334. {
  335. dev_kfree_skb_any((struct sk_buff *)buffer);
  336. }
  337. #define lio_dma_alloc(oct, size, dma_addr) \
  338. dma_alloc_coherent(&(oct)->pci_dev->dev, size, dma_addr, GFP_KERNEL)
  339. #define lio_dma_free(oct, size, virt_addr, dma_addr) \
  340. dma_free_coherent(&(oct)->pci_dev->dev, size, virt_addr, dma_addr)
  341. static inline
  342. void *get_rbd(struct sk_buff *skb)
  343. {
  344. struct octeon_skb_page_info *pg_info;
  345. unsigned char *va;
  346. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  347. va = page_address(pg_info->page) + pg_info->page_offset;
  348. return va;
  349. }
  350. static inline u64
  351. lio_map_ring(void *buf)
  352. {
  353. dma_addr_t dma_addr;
  354. struct sk_buff *skb = (struct sk_buff *)buf;
  355. struct octeon_skb_page_info *pg_info;
  356. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  357. if (!pg_info->page) {
  358. pr_err("%s: pg_info->page NULL\n", __func__);
  359. WARN_ON(1);
  360. }
  361. /* Get DMA info */
  362. dma_addr = pg_info->dma;
  363. if (!pg_info->dma) {
  364. pr_err("%s: ERROR it should be already available\n",
  365. __func__);
  366. WARN_ON(1);
  367. }
  368. dma_addr += pg_info->page_offset;
  369. return (u64)dma_addr;
  370. }
  371. static inline void
  372. lio_unmap_ring(struct pci_dev *pci_dev,
  373. u64 buf_ptr)
  374. {
  375. dma_unmap_page(&pci_dev->dev,
  376. buf_ptr, (PAGE_SIZE << 0),
  377. DMA_FROM_DEVICE);
  378. }
  379. static inline void *octeon_fast_packet_alloc(u32 size)
  380. {
  381. return recv_buffer_fast_alloc(size);
  382. }
  383. static inline void octeon_fast_packet_next(struct octeon_droq *droq,
  384. struct sk_buff *nicbuf,
  385. int copy_len,
  386. int idx)
  387. {
  388. skb_put_data(nicbuf, get_rbd(droq->recv_buf_list[idx].buffer),
  389. copy_len);
  390. }
  391. /**
  392. * \brief check interface state
  393. * @param lio per-network private data
  394. * @param state_flag flag state to check
  395. */
  396. static inline int ifstate_check(struct lio *lio, int state_flag)
  397. {
  398. return atomic_read(&lio->ifstate) & state_flag;
  399. }
  400. /**
  401. * \brief set interface state
  402. * @param lio per-network private data
  403. * @param state_flag flag state to set
  404. */
  405. static inline void ifstate_set(struct lio *lio, int state_flag)
  406. {
  407. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
  408. }
  409. /**
  410. * \brief clear interface state
  411. * @param lio per-network private data
  412. * @param state_flag flag state to clear
  413. */
  414. static inline void ifstate_reset(struct lio *lio, int state_flag)
  415. {
  416. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
  417. }
  418. /**
  419. * \brief wait for all pending requests to complete
  420. * @param oct Pointer to Octeon device
  421. *
  422. * Called during shutdown sequence
  423. */
  424. static inline int wait_for_pending_requests(struct octeon_device *oct)
  425. {
  426. int i, pcount = 0;
  427. for (i = 0; i < MAX_IO_PENDING_PKT_COUNT; i++) {
  428. pcount = atomic_read(
  429. &oct->response_list[OCTEON_ORDERED_SC_LIST]
  430. .pending_req_count);
  431. if (pcount)
  432. schedule_timeout_uninterruptible(HZ / 10);
  433. else
  434. break;
  435. }
  436. if (pcount)
  437. return 1;
  438. return 0;
  439. }
  440. /**
  441. * \brief Stop Tx queues
  442. * @param netdev network device
  443. */
  444. static inline void stop_txqs(struct net_device *netdev)
  445. {
  446. int i;
  447. for (i = 0; i < netdev->real_num_tx_queues; i++)
  448. netif_stop_subqueue(netdev, i);
  449. }
  450. /**
  451. * \brief Wake Tx queues
  452. * @param netdev network device
  453. */
  454. static inline void wake_txqs(struct net_device *netdev)
  455. {
  456. struct lio *lio = GET_LIO(netdev);
  457. int i, qno;
  458. for (i = 0; i < netdev->real_num_tx_queues; i++) {
  459. qno = lio->linfo.txpciq[i % lio->oct_dev->num_iqs].s.q_no;
  460. if (__netif_subqueue_stopped(netdev, i)) {
  461. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
  462. tx_restart, 1);
  463. netif_wake_subqueue(netdev, i);
  464. }
  465. }
  466. }
  467. /**
  468. * \brief Start Tx queues
  469. * @param netdev network device
  470. */
  471. static inline void start_txqs(struct net_device *netdev)
  472. {
  473. struct lio *lio = GET_LIO(netdev);
  474. int i;
  475. if (lio->linfo.link.s.link_up) {
  476. for (i = 0; i < netdev->real_num_tx_queues; i++)
  477. netif_start_subqueue(netdev, i);
  478. }
  479. }
  480. static inline int skb_iq(struct octeon_device *oct, struct sk_buff *skb)
  481. {
  482. return skb->queue_mapping % oct->num_iqs;
  483. }
  484. /**
  485. * Remove the node at the head of the list. The list would be empty at
  486. * the end of this call if there are no more nodes in the list.
  487. */
  488. static inline struct list_head *lio_list_delete_head(struct list_head *root)
  489. {
  490. struct list_head *node;
  491. if (list_empty_careful(root))
  492. node = NULL;
  493. else
  494. node = root->next;
  495. if (node)
  496. list_del(node);
  497. return node;
  498. }
  499. #endif