octeon_network.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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_context {
  34. u32 octeon_id;
  35. wait_queue_head_t wc;
  36. int cond;
  37. };
  38. struct liquidio_if_cfg_resp {
  39. u64 rh;
  40. struct liquidio_if_cfg_info cfg_info;
  41. u64 status;
  42. };
  43. #define LIO_IFCFG_WAIT_TIME 3000 /* In milli seconds */
  44. /* Structure of a node in list of gather components maintained by
  45. * NIC driver for each network device.
  46. */
  47. struct octnic_gather {
  48. /* List manipulation. Next and prev pointers. */
  49. struct list_head list;
  50. /* Size of the gather component at sg in bytes. */
  51. int sg_size;
  52. /* Number of bytes that sg was adjusted to make it 8B-aligned. */
  53. int adjust;
  54. /* Gather component that can accommodate max sized fragment list
  55. * received from the IP layer.
  56. */
  57. struct octeon_sg_entry *sg;
  58. dma_addr_t sg_dma_ptr;
  59. };
  60. struct oct_nic_stats_resp {
  61. u64 rh;
  62. struct oct_link_stats stats;
  63. u64 status;
  64. };
  65. struct oct_nic_stats_ctrl {
  66. struct completion complete;
  67. struct net_device *netdev;
  68. };
  69. struct oct_nic_seapi_resp {
  70. u64 rh;
  71. u32 speed;
  72. u64 status;
  73. };
  74. struct liquidio_nic_seapi_ctl_context {
  75. int octeon_id;
  76. u32 status;
  77. struct completion complete;
  78. };
  79. /** LiquidIO per-interface network private data */
  80. struct lio {
  81. /** State of the interface. Rx/Tx happens only in the RUNNING state. */
  82. atomic_t ifstate;
  83. /** Octeon Interface index number. This device will be represented as
  84. * oct<ifidx> in the system.
  85. */
  86. int ifidx;
  87. /** Octeon Input queue to use to transmit for this network interface. */
  88. int txq;
  89. /** Octeon Output queue from which pkts arrive
  90. * for this network interface.
  91. */
  92. int rxq;
  93. /** Guards each glist */
  94. spinlock_t *glist_lock;
  95. /** Array of gather component linked lists */
  96. struct list_head *glist;
  97. void **glists_virt_base;
  98. dma_addr_t *glists_dma_base;
  99. u32 glist_entry_size;
  100. /** Pointer to the NIC properties for the Octeon device this network
  101. * interface is associated with.
  102. */
  103. struct octdev_props *octprops;
  104. /** Pointer to the octeon device structure. */
  105. struct octeon_device *oct_dev;
  106. struct net_device *netdev;
  107. /** Link information sent by the core application for this interface. */
  108. struct oct_link_info linfo;
  109. /** counter of link changes */
  110. u64 link_changes;
  111. /** Size of Tx queue for this octeon device. */
  112. u32 tx_qsize;
  113. /** Size of Rx queue for this octeon device. */
  114. u32 rx_qsize;
  115. /** Size of MTU this octeon device. */
  116. u32 mtu;
  117. /** msg level flag per interface. */
  118. u32 msg_enable;
  119. /** Copy of Interface capabilities: TSO, TSO6, LRO, Chescksums . */
  120. u64 dev_capability;
  121. /* Copy of transmit encapsulation capabilities:
  122. * TSO, TSO6, Checksums for this device for Kernel
  123. * 3.10.0 onwards
  124. */
  125. u64 enc_dev_capability;
  126. /** Copy of beacaon reg in phy */
  127. u32 phy_beacon_val;
  128. /** Copy of ctrl reg in phy */
  129. u32 led_ctrl_val;
  130. /* PTP clock information */
  131. struct ptp_clock_info ptp_info;
  132. struct ptp_clock *ptp_clock;
  133. s64 ptp_adjust;
  134. /* for atomic access to Octeon PTP reg and data struct */
  135. spinlock_t ptp_lock;
  136. /* Interface info */
  137. u32 intf_open;
  138. /* work queue for txq status */
  139. struct cavium_wq txq_status_wq;
  140. /* work queue for rxq oom status */
  141. struct cavium_wq rxq_status_wq;
  142. /* work queue for link status */
  143. struct cavium_wq link_status_wq;
  144. /* work queue to regularly send local time to octeon firmware */
  145. struct cavium_wq sync_octeon_time_wq;
  146. int netdev_uc_count;
  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. int octnet_get_link_stats(struct net_device *netdev);
  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_if_cfg_callback(struct octeon_device *oct,
  183. u32 status __attribute__((unused)),
  184. void *buf);
  185. void lio_delete_glists(struct lio *lio);
  186. int lio_setup_glists(struct octeon_device *oct, struct lio *lio, int num_qs);
  187. int liquidio_get_speed(struct lio *lio);
  188. int liquidio_set_speed(struct lio *lio, int speed);
  189. /**
  190. * \brief Net device change_mtu
  191. * @param netdev network device
  192. */
  193. int liquidio_change_mtu(struct net_device *netdev, int new_mtu);
  194. #define LIO_CHANGE_MTU_SUCCESS 1
  195. #define LIO_CHANGE_MTU_FAIL 2
  196. #define SKB_ADJ_MASK 0x3F
  197. #define SKB_ADJ (SKB_ADJ_MASK + 1)
  198. #define MIN_SKB_SIZE 256 /* 8 bytes and more - 8 bytes for PTP */
  199. #define LIO_RXBUFFER_SZ 2048
  200. static inline void
  201. *recv_buffer_alloc(struct octeon_device *oct,
  202. struct octeon_skb_page_info *pg_info)
  203. {
  204. struct page *page;
  205. struct sk_buff *skb;
  206. struct octeon_skb_page_info *skb_pg_info;
  207. page = alloc_page(GFP_ATOMIC);
  208. if (unlikely(!page))
  209. return NULL;
  210. skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
  211. if (unlikely(!skb)) {
  212. __free_page(page);
  213. pg_info->page = NULL;
  214. return NULL;
  215. }
  216. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  217. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  218. skb_reserve(skb, r);
  219. }
  220. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  221. /* Get DMA info */
  222. pg_info->dma = dma_map_page(&oct->pci_dev->dev, page, 0,
  223. PAGE_SIZE, DMA_FROM_DEVICE);
  224. /* Mapping failed!! */
  225. if (dma_mapping_error(&oct->pci_dev->dev, pg_info->dma)) {
  226. __free_page(page);
  227. dev_kfree_skb_any((struct sk_buff *)skb);
  228. pg_info->page = NULL;
  229. return NULL;
  230. }
  231. pg_info->page = page;
  232. pg_info->page_offset = 0;
  233. skb_pg_info->page = page;
  234. skb_pg_info->page_offset = 0;
  235. skb_pg_info->dma = pg_info->dma;
  236. return (void *)skb;
  237. }
  238. static inline void
  239. *recv_buffer_fast_alloc(u32 size)
  240. {
  241. struct sk_buff *skb;
  242. struct octeon_skb_page_info *skb_pg_info;
  243. skb = dev_alloc_skb(size + SKB_ADJ);
  244. if (unlikely(!skb))
  245. return NULL;
  246. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  247. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  248. skb_reserve(skb, r);
  249. }
  250. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  251. skb_pg_info->page = NULL;
  252. skb_pg_info->page_offset = 0;
  253. skb_pg_info->dma = 0;
  254. return skb;
  255. }
  256. static inline int
  257. recv_buffer_recycle(struct octeon_device *oct, void *buf)
  258. {
  259. struct octeon_skb_page_info *pg_info = buf;
  260. if (!pg_info->page) {
  261. dev_err(&oct->pci_dev->dev, "%s: pg_info->page NULL\n",
  262. __func__);
  263. return -ENOMEM;
  264. }
  265. if (unlikely(page_count(pg_info->page) != 1) ||
  266. unlikely(page_to_nid(pg_info->page) != numa_node_id())) {
  267. dma_unmap_page(&oct->pci_dev->dev,
  268. pg_info->dma, (PAGE_SIZE << 0),
  269. DMA_FROM_DEVICE);
  270. pg_info->dma = 0;
  271. pg_info->page = NULL;
  272. pg_info->page_offset = 0;
  273. return -ENOMEM;
  274. }
  275. /* Flip to other half of the buffer */
  276. if (pg_info->page_offset == 0)
  277. pg_info->page_offset = LIO_RXBUFFER_SZ;
  278. else
  279. pg_info->page_offset = 0;
  280. page_ref_inc(pg_info->page);
  281. return 0;
  282. }
  283. static inline void
  284. *recv_buffer_reuse(struct octeon_device *oct, void *buf)
  285. {
  286. struct octeon_skb_page_info *pg_info = buf, *skb_pg_info;
  287. struct sk_buff *skb;
  288. skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
  289. if (unlikely(!skb)) {
  290. dma_unmap_page(&oct->pci_dev->dev,
  291. pg_info->dma, (PAGE_SIZE << 0),
  292. DMA_FROM_DEVICE);
  293. return NULL;
  294. }
  295. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  296. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  297. skb_reserve(skb, r);
  298. }
  299. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  300. skb_pg_info->page = pg_info->page;
  301. skb_pg_info->page_offset = pg_info->page_offset;
  302. skb_pg_info->dma = pg_info->dma;
  303. return skb;
  304. }
  305. static inline void
  306. recv_buffer_destroy(void *buffer, struct octeon_skb_page_info *pg_info)
  307. {
  308. struct sk_buff *skb = (struct sk_buff *)buffer;
  309. put_page(pg_info->page);
  310. pg_info->dma = 0;
  311. pg_info->page = NULL;
  312. pg_info->page_offset = 0;
  313. if (skb)
  314. dev_kfree_skb_any(skb);
  315. }
  316. static inline void recv_buffer_free(void *buffer)
  317. {
  318. struct sk_buff *skb = (struct sk_buff *)buffer;
  319. struct octeon_skb_page_info *pg_info;
  320. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  321. if (pg_info->page) {
  322. put_page(pg_info->page);
  323. pg_info->dma = 0;
  324. pg_info->page = NULL;
  325. pg_info->page_offset = 0;
  326. }
  327. dev_kfree_skb_any((struct sk_buff *)buffer);
  328. }
  329. static inline void
  330. recv_buffer_fast_free(void *buffer)
  331. {
  332. dev_kfree_skb_any((struct sk_buff *)buffer);
  333. }
  334. static inline void tx_buffer_free(void *buffer)
  335. {
  336. dev_kfree_skb_any((struct sk_buff *)buffer);
  337. }
  338. #define lio_dma_alloc(oct, size, dma_addr) \
  339. dma_alloc_coherent(&(oct)->pci_dev->dev, size, dma_addr, GFP_KERNEL)
  340. #define lio_dma_free(oct, size, virt_addr, dma_addr) \
  341. dma_free_coherent(&(oct)->pci_dev->dev, size, virt_addr, dma_addr)
  342. static inline
  343. void *get_rbd(struct sk_buff *skb)
  344. {
  345. struct octeon_skb_page_info *pg_info;
  346. unsigned char *va;
  347. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  348. va = page_address(pg_info->page) + pg_info->page_offset;
  349. return va;
  350. }
  351. static inline u64
  352. lio_map_ring(void *buf)
  353. {
  354. dma_addr_t dma_addr;
  355. struct sk_buff *skb = (struct sk_buff *)buf;
  356. struct octeon_skb_page_info *pg_info;
  357. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  358. if (!pg_info->page) {
  359. pr_err("%s: pg_info->page NULL\n", __func__);
  360. WARN_ON(1);
  361. }
  362. /* Get DMA info */
  363. dma_addr = pg_info->dma;
  364. if (!pg_info->dma) {
  365. pr_err("%s: ERROR it should be already available\n",
  366. __func__);
  367. WARN_ON(1);
  368. }
  369. dma_addr += pg_info->page_offset;
  370. return (u64)dma_addr;
  371. }
  372. static inline void
  373. lio_unmap_ring(struct pci_dev *pci_dev,
  374. u64 buf_ptr)
  375. {
  376. dma_unmap_page(&pci_dev->dev,
  377. buf_ptr, (PAGE_SIZE << 0),
  378. DMA_FROM_DEVICE);
  379. }
  380. static inline void *octeon_fast_packet_alloc(u32 size)
  381. {
  382. return recv_buffer_fast_alloc(size);
  383. }
  384. static inline void octeon_fast_packet_next(struct octeon_droq *droq,
  385. struct sk_buff *nicbuf,
  386. int copy_len,
  387. int idx)
  388. {
  389. skb_put_data(nicbuf, get_rbd(droq->recv_buf_list[idx].buffer),
  390. copy_len);
  391. }
  392. /**
  393. * \brief check interface state
  394. * @param lio per-network private data
  395. * @param state_flag flag state to check
  396. */
  397. static inline int ifstate_check(struct lio *lio, int state_flag)
  398. {
  399. return atomic_read(&lio->ifstate) & state_flag;
  400. }
  401. /**
  402. * \brief set interface state
  403. * @param lio per-network private data
  404. * @param state_flag flag state to set
  405. */
  406. static inline void ifstate_set(struct lio *lio, int state_flag)
  407. {
  408. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
  409. }
  410. /**
  411. * \brief clear interface state
  412. * @param lio per-network private data
  413. * @param state_flag flag state to clear
  414. */
  415. static inline void ifstate_reset(struct lio *lio, int state_flag)
  416. {
  417. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
  418. }
  419. /**
  420. * \brief wait for all pending requests to complete
  421. * @param oct Pointer to Octeon device
  422. *
  423. * Called during shutdown sequence
  424. */
  425. static inline int wait_for_pending_requests(struct octeon_device *oct)
  426. {
  427. int i, pcount = 0;
  428. for (i = 0; i < MAX_IO_PENDING_PKT_COUNT; i++) {
  429. pcount = atomic_read(
  430. &oct->response_list[OCTEON_ORDERED_SC_LIST]
  431. .pending_req_count);
  432. if (pcount)
  433. schedule_timeout_uninterruptible(HZ / 10);
  434. else
  435. break;
  436. }
  437. if (pcount)
  438. return 1;
  439. return 0;
  440. }
  441. /**
  442. * \brief Stop Tx queues
  443. * @param netdev network device
  444. */
  445. static inline void stop_txqs(struct net_device *netdev)
  446. {
  447. int i;
  448. for (i = 0; i < netdev->real_num_tx_queues; i++)
  449. netif_stop_subqueue(netdev, i);
  450. }
  451. /**
  452. * \brief Wake Tx queues
  453. * @param netdev network device
  454. */
  455. static inline void wake_txqs(struct net_device *netdev)
  456. {
  457. struct lio *lio = GET_LIO(netdev);
  458. int i, qno;
  459. for (i = 0; i < netdev->real_num_tx_queues; i++) {
  460. qno = lio->linfo.txpciq[i % lio->oct_dev->num_iqs].s.q_no;
  461. if (__netif_subqueue_stopped(netdev, i)) {
  462. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
  463. tx_restart, 1);
  464. netif_wake_subqueue(netdev, i);
  465. }
  466. }
  467. }
  468. /**
  469. * \brief Start Tx queues
  470. * @param netdev network device
  471. */
  472. static inline void start_txqs(struct net_device *netdev)
  473. {
  474. struct lio *lio = GET_LIO(netdev);
  475. int i;
  476. if (lio->linfo.link.s.link_up) {
  477. for (i = 0; i < netdev->real_num_tx_queues; i++)
  478. netif_start_subqueue(netdev, i);
  479. }
  480. }
  481. static inline int skb_iq(struct octeon_device *oct, struct sk_buff *skb)
  482. {
  483. return skb->queue_mapping % oct->num_iqs;
  484. }
  485. /**
  486. * Remove the node at the head of the list. The list would be empty at
  487. * the end of this call if there are no more nodes in the list.
  488. */
  489. static inline struct list_head *lio_list_delete_head(struct list_head *root)
  490. {
  491. struct list_head *node;
  492. if (root->prev == root && root->next == root)
  493. node = NULL;
  494. else
  495. node = root->next;
  496. if (node)
  497. list_del(node);
  498. return node;
  499. }
  500. #endif