octeon_network.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. struct oct_nic_stats_resp {
  44. u64 rh;
  45. struct oct_link_stats stats;
  46. u64 status;
  47. };
  48. struct oct_nic_stats_ctrl {
  49. struct completion complete;
  50. struct net_device *netdev;
  51. };
  52. /** LiquidIO per-interface network private data */
  53. struct lio {
  54. /** State of the interface. Rx/Tx happens only in the RUNNING state. */
  55. atomic_t ifstate;
  56. /** Octeon Interface index number. This device will be represented as
  57. * oct<ifidx> in the system.
  58. */
  59. int ifidx;
  60. /** Octeon Input queue to use to transmit for this network interface. */
  61. int txq;
  62. /** Octeon Output queue from which pkts arrive
  63. * for this network interface.
  64. */
  65. int rxq;
  66. /** Guards each glist */
  67. spinlock_t *glist_lock;
  68. /** Array of gather component linked lists */
  69. struct list_head *glist;
  70. void **glists_virt_base;
  71. dma_addr_t *glists_dma_base;
  72. u32 glist_entry_size;
  73. /** Pointer to the NIC properties for the Octeon device this network
  74. * interface is associated with.
  75. */
  76. struct octdev_props *octprops;
  77. /** Pointer to the octeon device structure. */
  78. struct octeon_device *oct_dev;
  79. struct net_device *netdev;
  80. /** Link information sent by the core application for this interface. */
  81. struct oct_link_info linfo;
  82. /** counter of link changes */
  83. u64 link_changes;
  84. /** Size of Tx queue for this octeon device. */
  85. u32 tx_qsize;
  86. /** Size of Rx queue for this octeon device. */
  87. u32 rx_qsize;
  88. /** Size of MTU this octeon device. */
  89. u32 mtu;
  90. /** msg level flag per interface. */
  91. u32 msg_enable;
  92. /** Copy of Interface capabilities: TSO, TSO6, LRO, Chescksums . */
  93. u64 dev_capability;
  94. /* Copy of transmit encapsulation capabilities:
  95. * TSO, TSO6, Checksums for this device for Kernel
  96. * 3.10.0 onwards
  97. */
  98. u64 enc_dev_capability;
  99. /** Copy of beacaon reg in phy */
  100. u32 phy_beacon_val;
  101. /** Copy of ctrl reg in phy */
  102. u32 led_ctrl_val;
  103. /* PTP clock information */
  104. struct ptp_clock_info ptp_info;
  105. struct ptp_clock *ptp_clock;
  106. s64 ptp_adjust;
  107. /* for atomic access to Octeon PTP reg and data struct */
  108. spinlock_t ptp_lock;
  109. /* Interface info */
  110. u32 intf_open;
  111. /* work queue for txq status */
  112. struct cavium_wq txq_status_wq;
  113. /* work queue for rxq oom status */
  114. struct cavium_wq rxq_status_wq;
  115. /* work queue for link status */
  116. struct cavium_wq link_status_wq;
  117. /* work queue to regularly send local time to octeon firmware */
  118. struct cavium_wq sync_octeon_time_wq;
  119. int netdev_uc_count;
  120. };
  121. #define LIO_SIZE (sizeof(struct lio))
  122. #define GET_LIO(netdev) ((struct lio *)netdev_priv(netdev))
  123. #define LIO_MAX_CORES 12
  124. /**
  125. * \brief Enable or disable feature
  126. * @param netdev pointer to network device
  127. * @param cmd Command that just requires acknowledgment
  128. * @param param1 Parameter to command
  129. */
  130. int liquidio_set_feature(struct net_device *netdev, int cmd, u16 param1);
  131. int setup_rx_oom_poll_fn(struct net_device *netdev);
  132. void cleanup_rx_oom_poll_fn(struct net_device *netdev);
  133. /**
  134. * \brief Link control command completion callback
  135. * @param nctrl_ptr pointer to control packet structure
  136. *
  137. * This routine is called by the callback function when a ctrl pkt sent to
  138. * core app completes. The nctrl_ptr contains a copy of the command type
  139. * and data sent to the core app. This routine is only called if the ctrl
  140. * pkt was sent successfully to the core app.
  141. */
  142. void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr);
  143. int liquidio_setup_io_queues(struct octeon_device *octeon_dev, int ifidx,
  144. u32 num_iqs, u32 num_oqs);
  145. irqreturn_t liquidio_msix_intr_handler(int irq __attribute__((unused)),
  146. void *dev);
  147. int octeon_setup_interrupt(struct octeon_device *oct, u32 num_ioqs);
  148. int lio_wait_for_clean_oq(struct octeon_device *oct);
  149. /**
  150. * \brief Register ethtool operations
  151. * @param netdev pointer to network device
  152. */
  153. void liquidio_set_ethtool_ops(struct net_device *netdev);
  154. /**
  155. * \brief Net device change_mtu
  156. * @param netdev network device
  157. */
  158. int liquidio_change_mtu(struct net_device *netdev, int new_mtu);
  159. #define LIO_CHANGE_MTU_SUCCESS 1
  160. #define LIO_CHANGE_MTU_FAIL 2
  161. #define SKB_ADJ_MASK 0x3F
  162. #define SKB_ADJ (SKB_ADJ_MASK + 1)
  163. #define MIN_SKB_SIZE 256 /* 8 bytes and more - 8 bytes for PTP */
  164. #define LIO_RXBUFFER_SZ 2048
  165. static inline void
  166. *recv_buffer_alloc(struct octeon_device *oct,
  167. struct octeon_skb_page_info *pg_info)
  168. {
  169. struct page *page;
  170. struct sk_buff *skb;
  171. struct octeon_skb_page_info *skb_pg_info;
  172. page = alloc_page(GFP_ATOMIC);
  173. if (unlikely(!page))
  174. return NULL;
  175. skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
  176. if (unlikely(!skb)) {
  177. __free_page(page);
  178. pg_info->page = NULL;
  179. return NULL;
  180. }
  181. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  182. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  183. skb_reserve(skb, r);
  184. }
  185. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  186. /* Get DMA info */
  187. pg_info->dma = dma_map_page(&oct->pci_dev->dev, page, 0,
  188. PAGE_SIZE, DMA_FROM_DEVICE);
  189. /* Mapping failed!! */
  190. if (dma_mapping_error(&oct->pci_dev->dev, pg_info->dma)) {
  191. __free_page(page);
  192. dev_kfree_skb_any((struct sk_buff *)skb);
  193. pg_info->page = NULL;
  194. return NULL;
  195. }
  196. pg_info->page = page;
  197. pg_info->page_offset = 0;
  198. skb_pg_info->page = page;
  199. skb_pg_info->page_offset = 0;
  200. skb_pg_info->dma = pg_info->dma;
  201. return (void *)skb;
  202. }
  203. static inline void
  204. *recv_buffer_fast_alloc(u32 size)
  205. {
  206. struct sk_buff *skb;
  207. struct octeon_skb_page_info *skb_pg_info;
  208. skb = dev_alloc_skb(size + SKB_ADJ);
  209. if (unlikely(!skb))
  210. return NULL;
  211. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  212. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  213. skb_reserve(skb, r);
  214. }
  215. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  216. skb_pg_info->page = NULL;
  217. skb_pg_info->page_offset = 0;
  218. skb_pg_info->dma = 0;
  219. return skb;
  220. }
  221. static inline int
  222. recv_buffer_recycle(struct octeon_device *oct, void *buf)
  223. {
  224. struct octeon_skb_page_info *pg_info = buf;
  225. if (!pg_info->page) {
  226. dev_err(&oct->pci_dev->dev, "%s: pg_info->page NULL\n",
  227. __func__);
  228. return -ENOMEM;
  229. }
  230. if (unlikely(page_count(pg_info->page) != 1) ||
  231. unlikely(page_to_nid(pg_info->page) != numa_node_id())) {
  232. dma_unmap_page(&oct->pci_dev->dev,
  233. pg_info->dma, (PAGE_SIZE << 0),
  234. DMA_FROM_DEVICE);
  235. pg_info->dma = 0;
  236. pg_info->page = NULL;
  237. pg_info->page_offset = 0;
  238. return -ENOMEM;
  239. }
  240. /* Flip to other half of the buffer */
  241. if (pg_info->page_offset == 0)
  242. pg_info->page_offset = LIO_RXBUFFER_SZ;
  243. else
  244. pg_info->page_offset = 0;
  245. page_ref_inc(pg_info->page);
  246. return 0;
  247. }
  248. static inline void
  249. *recv_buffer_reuse(struct octeon_device *oct, void *buf)
  250. {
  251. struct octeon_skb_page_info *pg_info = buf, *skb_pg_info;
  252. struct sk_buff *skb;
  253. skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
  254. if (unlikely(!skb)) {
  255. dma_unmap_page(&oct->pci_dev->dev,
  256. pg_info->dma, (PAGE_SIZE << 0),
  257. DMA_FROM_DEVICE);
  258. return NULL;
  259. }
  260. if ((unsigned long)skb->data & SKB_ADJ_MASK) {
  261. u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
  262. skb_reserve(skb, r);
  263. }
  264. skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  265. skb_pg_info->page = pg_info->page;
  266. skb_pg_info->page_offset = pg_info->page_offset;
  267. skb_pg_info->dma = pg_info->dma;
  268. return skb;
  269. }
  270. static inline void
  271. recv_buffer_destroy(void *buffer, struct octeon_skb_page_info *pg_info)
  272. {
  273. struct sk_buff *skb = (struct sk_buff *)buffer;
  274. put_page(pg_info->page);
  275. pg_info->dma = 0;
  276. pg_info->page = NULL;
  277. pg_info->page_offset = 0;
  278. if (skb)
  279. dev_kfree_skb_any(skb);
  280. }
  281. static inline void recv_buffer_free(void *buffer)
  282. {
  283. struct sk_buff *skb = (struct sk_buff *)buffer;
  284. struct octeon_skb_page_info *pg_info;
  285. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  286. if (pg_info->page) {
  287. put_page(pg_info->page);
  288. pg_info->dma = 0;
  289. pg_info->page = NULL;
  290. pg_info->page_offset = 0;
  291. }
  292. dev_kfree_skb_any((struct sk_buff *)buffer);
  293. }
  294. static inline void
  295. recv_buffer_fast_free(void *buffer)
  296. {
  297. dev_kfree_skb_any((struct sk_buff *)buffer);
  298. }
  299. static inline void tx_buffer_free(void *buffer)
  300. {
  301. dev_kfree_skb_any((struct sk_buff *)buffer);
  302. }
  303. #define lio_dma_alloc(oct, size, dma_addr) \
  304. dma_alloc_coherent(&(oct)->pci_dev->dev, size, dma_addr, GFP_KERNEL)
  305. #define lio_dma_free(oct, size, virt_addr, dma_addr) \
  306. dma_free_coherent(&(oct)->pci_dev->dev, size, virt_addr, dma_addr)
  307. static inline
  308. void *get_rbd(struct sk_buff *skb)
  309. {
  310. struct octeon_skb_page_info *pg_info;
  311. unsigned char *va;
  312. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  313. va = page_address(pg_info->page) + pg_info->page_offset;
  314. return va;
  315. }
  316. static inline u64
  317. lio_map_ring(void *buf)
  318. {
  319. dma_addr_t dma_addr;
  320. struct sk_buff *skb = (struct sk_buff *)buf;
  321. struct octeon_skb_page_info *pg_info;
  322. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  323. if (!pg_info->page) {
  324. pr_err("%s: pg_info->page NULL\n", __func__);
  325. WARN_ON(1);
  326. }
  327. /* Get DMA info */
  328. dma_addr = pg_info->dma;
  329. if (!pg_info->dma) {
  330. pr_err("%s: ERROR it should be already available\n",
  331. __func__);
  332. WARN_ON(1);
  333. }
  334. dma_addr += pg_info->page_offset;
  335. return (u64)dma_addr;
  336. }
  337. static inline void
  338. lio_unmap_ring(struct pci_dev *pci_dev,
  339. u64 buf_ptr)
  340. {
  341. dma_unmap_page(&pci_dev->dev,
  342. buf_ptr, (PAGE_SIZE << 0),
  343. DMA_FROM_DEVICE);
  344. }
  345. static inline void *octeon_fast_packet_alloc(u32 size)
  346. {
  347. return recv_buffer_fast_alloc(size);
  348. }
  349. static inline void octeon_fast_packet_next(struct octeon_droq *droq,
  350. struct sk_buff *nicbuf,
  351. int copy_len,
  352. int idx)
  353. {
  354. skb_put_data(nicbuf, get_rbd(droq->recv_buf_list[idx].buffer),
  355. copy_len);
  356. }
  357. /**
  358. * \brief check interface state
  359. * @param lio per-network private data
  360. * @param state_flag flag state to check
  361. */
  362. static inline int ifstate_check(struct lio *lio, int state_flag)
  363. {
  364. return atomic_read(&lio->ifstate) & state_flag;
  365. }
  366. /**
  367. * \brief set interface state
  368. * @param lio per-network private data
  369. * @param state_flag flag state to set
  370. */
  371. static inline void ifstate_set(struct lio *lio, int state_flag)
  372. {
  373. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
  374. }
  375. /**
  376. * \brief clear interface state
  377. * @param lio per-network private data
  378. * @param state_flag flag state to clear
  379. */
  380. static inline void ifstate_reset(struct lio *lio, int state_flag)
  381. {
  382. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
  383. }
  384. /**
  385. * \brief wait for all pending requests to complete
  386. * @param oct Pointer to Octeon device
  387. *
  388. * Called during shutdown sequence
  389. */
  390. static inline int wait_for_pending_requests(struct octeon_device *oct)
  391. {
  392. int i, pcount = 0;
  393. for (i = 0; i < MAX_IO_PENDING_PKT_COUNT; i++) {
  394. pcount = atomic_read(
  395. &oct->response_list[OCTEON_ORDERED_SC_LIST]
  396. .pending_req_count);
  397. if (pcount)
  398. schedule_timeout_uninterruptible(HZ / 10);
  399. else
  400. break;
  401. }
  402. if (pcount)
  403. return 1;
  404. return 0;
  405. }
  406. /**
  407. * \brief Stop Tx queues
  408. * @param netdev network device
  409. */
  410. static inline void stop_txqs(struct net_device *netdev)
  411. {
  412. int i;
  413. for (i = 0; i < netdev->num_tx_queues; i++)
  414. netif_stop_subqueue(netdev, i);
  415. }
  416. /**
  417. * \brief Wake Tx queues
  418. * @param netdev network device
  419. */
  420. static inline void wake_txqs(struct net_device *netdev)
  421. {
  422. struct lio *lio = GET_LIO(netdev);
  423. int i, qno;
  424. for (i = 0; i < netdev->num_tx_queues; i++) {
  425. qno = lio->linfo.txpciq[i % lio->oct_dev->num_iqs].s.q_no;
  426. if (__netif_subqueue_stopped(netdev, i)) {
  427. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
  428. tx_restart, 1);
  429. netif_wake_subqueue(netdev, i);
  430. }
  431. }
  432. }
  433. /**
  434. * \brief Start Tx queues
  435. * @param netdev network device
  436. */
  437. static inline void start_txqs(struct net_device *netdev)
  438. {
  439. struct lio *lio = GET_LIO(netdev);
  440. int i;
  441. if (lio->linfo.link.s.link_up) {
  442. for (i = 0; i < netdev->num_tx_queues; i++)
  443. netif_start_subqueue(netdev, i);
  444. }
  445. }
  446. static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
  447. {
  448. return skb->queue_mapping % lio->linfo.num_txpciq;
  449. }
  450. #endif