octeon_network.h 12 KB

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