octeon_network.h 12 KB

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