octeon_network.h 11 KB

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