libcxgb_ppm.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * libcxgb_ppm.h: Chelsio common library for T3/T4/T5 iSCSI ddp operation
  3. *
  4. * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * Written by: Karen Xie (kxie@chelsio.com)
  35. */
  36. #ifndef __LIBCXGB_PPM_H__
  37. #define __LIBCXGB_PPM_H__
  38. #include <linux/kernel.h>
  39. #include <linux/errno.h>
  40. #include <linux/types.h>
  41. #include <linux/debugfs.h>
  42. #include <linux/list.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/scatterlist.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/bitmap.h>
  48. struct cxgbi_pagepod_hdr {
  49. u32 vld_tid;
  50. u32 pgsz_tag_clr;
  51. u32 max_offset;
  52. u32 page_offset;
  53. u64 rsvd;
  54. };
  55. #define PPOD_PAGES_MAX 4
  56. struct cxgbi_pagepod {
  57. struct cxgbi_pagepod_hdr hdr;
  58. u64 addr[PPOD_PAGES_MAX + 1];
  59. };
  60. /* ddp tag format
  61. * for a 32-bit tag:
  62. * bit #
  63. * 31 ..... ..... 0
  64. * X Y...Y Z...Z, where
  65. * ^ ^^^^^ ^^^^
  66. * | | |____ when ddp bit = 0: color bits
  67. * | |
  68. * | |____ when ddp bit = 0: idx into the ddp memory region
  69. * |
  70. * |____ ddp bit: 0 - ddp tag, 1 - non-ddp tag
  71. *
  72. * [page selector:2] [sw/free bits] [0] [idx] [color:6]
  73. */
  74. #define DDP_PGIDX_MAX 4
  75. #define DDP_PGSZ_BASE_SHIFT 12 /* base page 4K */
  76. struct cxgbi_task_tag_info {
  77. unsigned char flags;
  78. #define CXGBI_PPOD_INFO_FLAG_VALID 0x1
  79. #define CXGBI_PPOD_INFO_FLAG_MAPPED 0x2
  80. unsigned char cid;
  81. unsigned short pg_shift;
  82. unsigned int npods;
  83. unsigned int idx;
  84. unsigned int tag;
  85. struct cxgbi_pagepod_hdr hdr;
  86. int nents;
  87. int nr_pages;
  88. struct scatterlist *sgl;
  89. };
  90. struct cxgbi_tag_format {
  91. unsigned char pgsz_order[DDP_PGIDX_MAX];
  92. unsigned char pgsz_idx_dflt;
  93. unsigned char free_bits:4;
  94. unsigned char color_bits:4;
  95. unsigned char idx_bits;
  96. unsigned char rsvd_bits;
  97. unsigned int no_ddp_mask;
  98. unsigned int idx_mask;
  99. unsigned int color_mask;
  100. unsigned int idx_clr_mask;
  101. unsigned int rsvd_mask;
  102. };
  103. struct cxgbi_ppod_data {
  104. unsigned char pg_idx:2;
  105. unsigned char color:6;
  106. unsigned char chan_id;
  107. unsigned short npods;
  108. unsigned long caller_data;
  109. };
  110. /* per cpu ppm pool */
  111. struct cxgbi_ppm_pool {
  112. unsigned int base; /* base index */
  113. unsigned int next; /* next possible free index */
  114. spinlock_t lock; /* ppm pool lock */
  115. unsigned long bmap[0];
  116. } ____cacheline_aligned_in_smp;
  117. struct cxgbi_ppm {
  118. struct kref refcnt;
  119. struct net_device *ndev; /* net_device, 1st port */
  120. struct pci_dev *pdev;
  121. void *lldev;
  122. void **ppm_pp;
  123. struct cxgbi_tag_format tformat;
  124. unsigned int ppmax;
  125. unsigned int llimit;
  126. unsigned int base_idx;
  127. unsigned int pool_rsvd;
  128. unsigned int pool_index_max;
  129. struct cxgbi_ppm_pool __percpu *pool;
  130. /* map lock */
  131. spinlock_t map_lock; /* ppm map lock */
  132. unsigned int bmap_index_max;
  133. unsigned int next;
  134. unsigned long *ppod_bmap;
  135. struct cxgbi_ppod_data ppod_data[0];
  136. };
  137. #define DDP_THRESHOLD 512
  138. #define PPOD_PAGES_SHIFT 2 /* 4 pages per pod */
  139. #define IPPOD_SIZE sizeof(struct cxgbi_pagepod) /* 64 */
  140. #define PPOD_SIZE_SHIFT 6
  141. /* page pods are allocated in groups of this size (must be power of 2) */
  142. #define PPOD_CLUSTER_SIZE 16U
  143. #define ULPMEM_DSGL_MAX_NPPODS 16 /* 1024/PPOD_SIZE */
  144. #define ULPMEM_IDATA_MAX_NPPODS 3 /* (PPOD_SIZE * 3 + ulptx hdr) < 256B */
  145. #define PCIE_MEMWIN_MAX_NPPODS 16 /* 1024/PPOD_SIZE */
  146. #define PPOD_COLOR_SHIFT 0
  147. #define PPOD_COLOR(x) ((x) << PPOD_COLOR_SHIFT)
  148. #define PPOD_IDX_SHIFT 6
  149. #define PPOD_IDX_MAX_SIZE 24
  150. #define PPOD_TID_SHIFT 0
  151. #define PPOD_TID(x) ((x) << PPOD_TID_SHIFT)
  152. #define PPOD_TAG_SHIFT 6
  153. #define PPOD_TAG(x) ((x) << PPOD_TAG_SHIFT)
  154. #define PPOD_VALID_SHIFT 24
  155. #define PPOD_VALID(x) ((x) << PPOD_VALID_SHIFT)
  156. #define PPOD_VALID_FLAG PPOD_VALID(1U)
  157. #define PPOD_PI_EXTRACT_CTL_SHIFT 31
  158. #define PPOD_PI_EXTRACT_CTL(x) ((x) << PPOD_PI_EXTRACT_CTL_SHIFT)
  159. #define PPOD_PI_EXTRACT_CTL_FLAG V_PPOD_PI_EXTRACT_CTL(1U)
  160. #define PPOD_PI_TYPE_SHIFT 29
  161. #define PPOD_PI_TYPE_MASK 0x3
  162. #define PPOD_PI_TYPE(x) ((x) << PPOD_PI_TYPE_SHIFT)
  163. #define PPOD_PI_CHECK_CTL_SHIFT 27
  164. #define PPOD_PI_CHECK_CTL_MASK 0x3
  165. #define PPOD_PI_CHECK_CTL(x) ((x) << PPOD_PI_CHECK_CTL_SHIFT)
  166. #define PPOD_PI_REPORT_CTL_SHIFT 25
  167. #define PPOD_PI_REPORT_CTL_MASK 0x3
  168. #define PPOD_PI_REPORT_CTL(x) ((x) << PPOD_PI_REPORT_CTL_SHIFT)
  169. static inline int cxgbi_ppm_is_ddp_tag(struct cxgbi_ppm *ppm, u32 tag)
  170. {
  171. return !(tag & ppm->tformat.no_ddp_mask);
  172. }
  173. static inline int cxgbi_ppm_sw_tag_is_usable(struct cxgbi_ppm *ppm,
  174. u32 tag)
  175. {
  176. /* the sw tag must be using <= 31 bits */
  177. return !(tag & 0x80000000U);
  178. }
  179. static inline int cxgbi_ppm_make_non_ddp_tag(struct cxgbi_ppm *ppm,
  180. u32 sw_tag,
  181. u32 *final_tag)
  182. {
  183. struct cxgbi_tag_format *tformat = &ppm->tformat;
  184. if (!cxgbi_ppm_sw_tag_is_usable(ppm, sw_tag)) {
  185. pr_info("sw_tag 0x%x NOT usable.\n", sw_tag);
  186. return -EINVAL;
  187. }
  188. if (!sw_tag) {
  189. *final_tag = tformat->no_ddp_mask;
  190. } else {
  191. unsigned int shift = tformat->idx_bits + tformat->color_bits;
  192. u32 lower = sw_tag & tformat->idx_clr_mask;
  193. u32 upper = (sw_tag >> shift) << (shift + 1);
  194. *final_tag = upper | tformat->no_ddp_mask | lower;
  195. }
  196. return 0;
  197. }
  198. static inline u32 cxgbi_ppm_decode_non_ddp_tag(struct cxgbi_ppm *ppm,
  199. u32 tag)
  200. {
  201. struct cxgbi_tag_format *tformat = &ppm->tformat;
  202. unsigned int shift = tformat->idx_bits + tformat->color_bits;
  203. u32 lower = tag & tformat->idx_clr_mask;
  204. u32 upper = (tag >> tformat->rsvd_bits) << shift;
  205. return upper | lower;
  206. }
  207. static inline u32 cxgbi_ppm_ddp_tag_get_idx(struct cxgbi_ppm *ppm,
  208. u32 ddp_tag)
  209. {
  210. u32 hw_idx = (ddp_tag >> PPOD_IDX_SHIFT) &
  211. ppm->tformat.idx_mask;
  212. return hw_idx - ppm->base_idx;
  213. }
  214. static inline u32 cxgbi_ppm_make_ddp_tag(unsigned int hw_idx,
  215. unsigned char color)
  216. {
  217. return (hw_idx << PPOD_IDX_SHIFT) | ((u32)color);
  218. }
  219. static inline unsigned long
  220. cxgbi_ppm_get_tag_caller_data(struct cxgbi_ppm *ppm,
  221. u32 ddp_tag)
  222. {
  223. u32 idx = cxgbi_ppm_ddp_tag_get_idx(ppm, ddp_tag);
  224. return ppm->ppod_data[idx].caller_data;
  225. }
  226. /* sw bits are the free bits */
  227. static inline int cxgbi_ppm_ddp_tag_update_sw_bits(struct cxgbi_ppm *ppm,
  228. u32 val, u32 orig_tag,
  229. u32 *final_tag)
  230. {
  231. struct cxgbi_tag_format *tformat = &ppm->tformat;
  232. u32 v = val >> tformat->free_bits;
  233. if (v) {
  234. pr_info("sw_bits 0x%x too large, avail bits %u.\n",
  235. val, tformat->free_bits);
  236. return -EINVAL;
  237. }
  238. if (!cxgbi_ppm_is_ddp_tag(ppm, orig_tag))
  239. return -EINVAL;
  240. *final_tag = (val << tformat->rsvd_bits) |
  241. (orig_tag & ppm->tformat.rsvd_mask);
  242. return 0;
  243. }
  244. static inline void cxgbi_ppm_ppod_clear(struct cxgbi_pagepod *ppod)
  245. {
  246. ppod->hdr.vld_tid = 0U;
  247. }
  248. static inline void cxgbi_tagmask_check(unsigned int tagmask,
  249. struct cxgbi_tag_format *tformat)
  250. {
  251. unsigned int bits = fls(tagmask);
  252. /* reserve top most 2 bits for page selector */
  253. tformat->free_bits = 32 - 2 - bits;
  254. tformat->rsvd_bits = bits;
  255. tformat->color_bits = PPOD_IDX_SHIFT;
  256. tformat->idx_bits = bits - 1 - PPOD_IDX_SHIFT;
  257. tformat->no_ddp_mask = 1 << (bits - 1);
  258. tformat->idx_mask = (1 << tformat->idx_bits) - 1;
  259. tformat->color_mask = (1 << PPOD_IDX_SHIFT) - 1;
  260. tformat->idx_clr_mask = (1 << (bits - 1)) - 1;
  261. tformat->rsvd_mask = (1 << bits) - 1;
  262. pr_info("ippm: tagmask 0x%x, rsvd %u=%u+%u+1, mask 0x%x,0x%x, "
  263. "pg %u,%u,%u,%u.\n",
  264. tagmask, tformat->rsvd_bits, tformat->idx_bits,
  265. tformat->color_bits, tformat->no_ddp_mask, tformat->rsvd_mask,
  266. tformat->pgsz_order[0], tformat->pgsz_order[1],
  267. tformat->pgsz_order[2], tformat->pgsz_order[3]);
  268. }
  269. int cxgbi_ppm_find_page_index(struct cxgbi_ppm *ppm, unsigned long pgsz);
  270. void cxgbi_ppm_make_ppod_hdr(struct cxgbi_ppm *ppm, u32 tag,
  271. unsigned int tid, unsigned int offset,
  272. unsigned int length,
  273. struct cxgbi_pagepod_hdr *hdr);
  274. void cxgbi_ppm_ppod_release(struct cxgbi_ppm *, u32 idx);
  275. int cxgbi_ppm_ppods_reserve(struct cxgbi_ppm *, unsigned short nr_pages,
  276. u32 per_tag_pg_idx, u32 *ppod_idx, u32 *ddp_tag,
  277. unsigned long caller_data);
  278. int cxgbi_ppm_init(void **ppm_pp, struct net_device *, struct pci_dev *,
  279. void *lldev, struct cxgbi_tag_format *,
  280. unsigned int ppmax, unsigned int llimit,
  281. unsigned int start,
  282. unsigned int reserve_factor);
  283. int cxgbi_ppm_release(struct cxgbi_ppm *ppm);
  284. void cxgbi_tagmask_check(unsigned int tagmask, struct cxgbi_tag_format *);
  285. unsigned int cxgbi_tagmask_set(unsigned int ppmax);
  286. #endif /*__LIBCXGB_PPM_H__*/