libcxgb_ppm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * libcxgb_ppm.c: Chelsio common library for T3/T4/T5 iSCSI PagePod Manager
  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. #define DRV_NAME "libcxgb"
  37. #define DRV_VERSION "1.0.0-ko"
  38. #define pr_fmt(fmt) DRV_NAME ": " fmt
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/types.h>
  43. #include <linux/debugfs.h>
  44. #include <linux/export.h>
  45. #include <linux/list.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/pci.h>
  48. #include <linux/scatterlist.h>
  49. #include "libcxgb_ppm.h"
  50. /* Direct Data Placement -
  51. * Directly place the iSCSI Data-In or Data-Out PDU's payload into
  52. * pre-posted final destination host-memory buffers based on the
  53. * Initiator Task Tag (ITT) in Data-In or Target Task Tag (TTT)
  54. * in Data-Out PDUs. The host memory address is programmed into
  55. * h/w in the format of pagepod entries. The location of the
  56. * pagepod entry is encoded into ddp tag which is used as the base
  57. * for ITT/TTT.
  58. */
  59. /* Direct-Data Placement page size adjustment
  60. */
  61. int cxgbi_ppm_find_page_index(struct cxgbi_ppm *ppm, unsigned long pgsz)
  62. {
  63. struct cxgbi_tag_format *tformat = &ppm->tformat;
  64. int i;
  65. for (i = 0; i < DDP_PGIDX_MAX; i++) {
  66. if (pgsz == 1UL << (DDP_PGSZ_BASE_SHIFT +
  67. tformat->pgsz_order[i])) {
  68. pr_debug("%s: %s ppm, pgsz %lu -> idx %d.\n",
  69. __func__, ppm->ndev->name, pgsz, i);
  70. return i;
  71. }
  72. }
  73. pr_info("ippm: ddp page size %lu not supported.\n", pgsz);
  74. return DDP_PGIDX_MAX;
  75. }
  76. /* DDP setup & teardown
  77. */
  78. static int ppm_find_unused_entries(unsigned long *bmap,
  79. unsigned int max_ppods,
  80. unsigned int start,
  81. unsigned int nr,
  82. unsigned int align_mask)
  83. {
  84. unsigned long i;
  85. i = bitmap_find_next_zero_area(bmap, max_ppods, start, nr, align_mask);
  86. if (unlikely(i >= max_ppods) && (start > nr))
  87. i = bitmap_find_next_zero_area(bmap, max_ppods, 0, start - 1,
  88. align_mask);
  89. if (unlikely(i >= max_ppods))
  90. return -ENOSPC;
  91. bitmap_set(bmap, i, nr);
  92. return (int)i;
  93. }
  94. static void ppm_mark_entries(struct cxgbi_ppm *ppm, int i, int count,
  95. unsigned long caller_data)
  96. {
  97. struct cxgbi_ppod_data *pdata = ppm->ppod_data + i;
  98. pdata->caller_data = caller_data;
  99. pdata->npods = count;
  100. if (pdata->color == ((1 << PPOD_IDX_SHIFT) - 1))
  101. pdata->color = 0;
  102. else
  103. pdata->color++;
  104. }
  105. static int ppm_get_cpu_entries(struct cxgbi_ppm *ppm, unsigned int count,
  106. unsigned long caller_data)
  107. {
  108. struct cxgbi_ppm_pool *pool;
  109. unsigned int cpu;
  110. int i;
  111. cpu = get_cpu();
  112. pool = per_cpu_ptr(ppm->pool, cpu);
  113. spin_lock_bh(&pool->lock);
  114. put_cpu();
  115. i = ppm_find_unused_entries(pool->bmap, ppm->pool_index_max,
  116. pool->next, count, 0);
  117. if (i < 0) {
  118. pool->next = 0;
  119. spin_unlock_bh(&pool->lock);
  120. return -ENOSPC;
  121. }
  122. pool->next = i + count;
  123. if (pool->next >= ppm->pool_index_max)
  124. pool->next = 0;
  125. spin_unlock_bh(&pool->lock);
  126. pr_debug("%s: cpu %u, idx %d + %d (%d), next %u.\n",
  127. __func__, cpu, i, count, i + cpu * ppm->pool_index_max,
  128. pool->next);
  129. i += cpu * ppm->pool_index_max;
  130. ppm_mark_entries(ppm, i, count, caller_data);
  131. return i;
  132. }
  133. static int ppm_get_entries(struct cxgbi_ppm *ppm, unsigned int count,
  134. unsigned long caller_data)
  135. {
  136. int i;
  137. spin_lock_bh(&ppm->map_lock);
  138. i = ppm_find_unused_entries(ppm->ppod_bmap, ppm->bmap_index_max,
  139. ppm->next, count, 0);
  140. if (i < 0) {
  141. ppm->next = 0;
  142. spin_unlock_bh(&ppm->map_lock);
  143. pr_debug("ippm: NO suitable entries %u available.\n",
  144. count);
  145. return -ENOSPC;
  146. }
  147. ppm->next = i + count;
  148. if (ppm->next >= ppm->bmap_index_max)
  149. ppm->next = 0;
  150. spin_unlock_bh(&ppm->map_lock);
  151. pr_debug("%s: idx %d + %d (%d), next %u, caller_data 0x%lx.\n",
  152. __func__, i, count, i + ppm->pool_rsvd, ppm->next,
  153. caller_data);
  154. i += ppm->pool_rsvd;
  155. ppm_mark_entries(ppm, i, count, caller_data);
  156. return i;
  157. }
  158. static void ppm_unmark_entries(struct cxgbi_ppm *ppm, int i, int count)
  159. {
  160. pr_debug("%s: idx %d + %d.\n", __func__, i, count);
  161. if (i < ppm->pool_rsvd) {
  162. unsigned int cpu;
  163. struct cxgbi_ppm_pool *pool;
  164. cpu = i / ppm->pool_index_max;
  165. i %= ppm->pool_index_max;
  166. pool = per_cpu_ptr(ppm->pool, cpu);
  167. spin_lock_bh(&pool->lock);
  168. bitmap_clear(pool->bmap, i, count);
  169. if (i < pool->next)
  170. pool->next = i;
  171. spin_unlock_bh(&pool->lock);
  172. pr_debug("%s: cpu %u, idx %d, next %u.\n",
  173. __func__, cpu, i, pool->next);
  174. } else {
  175. spin_lock_bh(&ppm->map_lock);
  176. i -= ppm->pool_rsvd;
  177. bitmap_clear(ppm->ppod_bmap, i, count);
  178. if (i < ppm->next)
  179. ppm->next = i;
  180. spin_unlock_bh(&ppm->map_lock);
  181. pr_debug("%s: idx %d, next %u.\n", __func__, i, ppm->next);
  182. }
  183. }
  184. void cxgbi_ppm_ppod_release(struct cxgbi_ppm *ppm, u32 idx)
  185. {
  186. struct cxgbi_ppod_data *pdata;
  187. if (idx >= ppm->ppmax) {
  188. pr_warn("ippm: idx too big %u > %u.\n", idx, ppm->ppmax);
  189. return;
  190. }
  191. pdata = ppm->ppod_data + idx;
  192. if (!pdata->npods) {
  193. pr_warn("ippm: idx %u, npods 0.\n", idx);
  194. return;
  195. }
  196. pr_debug("release idx %u, npods %u.\n", idx, pdata->npods);
  197. ppm_unmark_entries(ppm, idx, pdata->npods);
  198. }
  199. EXPORT_SYMBOL(cxgbi_ppm_ppod_release);
  200. int cxgbi_ppm_ppods_reserve(struct cxgbi_ppm *ppm, unsigned short nr_pages,
  201. u32 per_tag_pg_idx, u32 *ppod_idx,
  202. u32 *ddp_tag, unsigned long caller_data)
  203. {
  204. struct cxgbi_ppod_data *pdata;
  205. unsigned int npods;
  206. int idx = -1;
  207. unsigned int hwidx;
  208. u32 tag;
  209. npods = (nr_pages + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
  210. if (!npods) {
  211. pr_warn("%s: pages %u -> npods %u, full.\n",
  212. __func__, nr_pages, npods);
  213. return -EINVAL;
  214. }
  215. /* grab from cpu pool first */
  216. idx = ppm_get_cpu_entries(ppm, npods, caller_data);
  217. /* try the general pool */
  218. if (idx < 0)
  219. idx = ppm_get_entries(ppm, npods, caller_data);
  220. if (idx < 0) {
  221. pr_debug("ippm: pages %u, nospc %u, nxt %u, 0x%lx.\n",
  222. nr_pages, npods, ppm->next, caller_data);
  223. return idx;
  224. }
  225. pdata = ppm->ppod_data + idx;
  226. hwidx = ppm->base_idx + idx;
  227. tag = cxgbi_ppm_make_ddp_tag(hwidx, pdata->color);
  228. if (per_tag_pg_idx)
  229. tag |= (per_tag_pg_idx << 30) & 0xC0000000;
  230. *ppod_idx = idx;
  231. *ddp_tag = tag;
  232. pr_debug("ippm: sg %u, tag 0x%x(%u,%u), data 0x%lx.\n",
  233. nr_pages, tag, idx, npods, caller_data);
  234. return npods;
  235. }
  236. EXPORT_SYMBOL(cxgbi_ppm_ppods_reserve);
  237. void cxgbi_ppm_make_ppod_hdr(struct cxgbi_ppm *ppm, u32 tag,
  238. unsigned int tid, unsigned int offset,
  239. unsigned int length,
  240. struct cxgbi_pagepod_hdr *hdr)
  241. {
  242. /* The ddp tag in pagepod should be with bit 31:30 set to 0.
  243. * The ddp Tag on the wire should be with non-zero 31:30 to the peer
  244. */
  245. tag &= 0x3FFFFFFF;
  246. hdr->vld_tid = htonl(PPOD_VALID_FLAG | PPOD_TID(tid));
  247. hdr->rsvd = 0;
  248. hdr->pgsz_tag_clr = htonl(tag & ppm->tformat.idx_clr_mask);
  249. hdr->max_offset = htonl(length);
  250. hdr->page_offset = htonl(offset);
  251. pr_debug("ippm: tag 0x%x, tid 0x%x, xfer %u, off %u.\n",
  252. tag, tid, length, offset);
  253. }
  254. EXPORT_SYMBOL(cxgbi_ppm_make_ppod_hdr);
  255. static void ppm_free(struct cxgbi_ppm *ppm)
  256. {
  257. vfree(ppm);
  258. }
  259. static void ppm_destroy(struct kref *kref)
  260. {
  261. struct cxgbi_ppm *ppm = container_of(kref,
  262. struct cxgbi_ppm,
  263. refcnt);
  264. pr_info("ippm: kref 0, destroy %s ppm 0x%p.\n",
  265. ppm->ndev->name, ppm);
  266. *ppm->ppm_pp = NULL;
  267. free_percpu(ppm->pool);
  268. ppm_free(ppm);
  269. }
  270. int cxgbi_ppm_release(struct cxgbi_ppm *ppm)
  271. {
  272. if (ppm) {
  273. int rv;
  274. rv = kref_put(&ppm->refcnt, ppm_destroy);
  275. return rv;
  276. }
  277. return 1;
  278. }
  279. EXPORT_SYMBOL(cxgbi_ppm_release);
  280. static struct cxgbi_ppm_pool *ppm_alloc_cpu_pool(unsigned int *total,
  281. unsigned int *pcpu_ppmax)
  282. {
  283. struct cxgbi_ppm_pool *pools;
  284. unsigned int ppmax = (*total) / num_possible_cpus();
  285. unsigned int max = (PCPU_MIN_UNIT_SIZE - sizeof(*pools)) << 3;
  286. unsigned int bmap;
  287. unsigned int alloc_sz;
  288. unsigned int count = 0;
  289. unsigned int cpu;
  290. /* make sure per cpu pool fits into PCPU_MIN_UNIT_SIZE */
  291. if (ppmax > max)
  292. ppmax = max;
  293. /* pool size must be multiple of unsigned long */
  294. bmap = BITS_TO_LONGS(ppmax);
  295. ppmax = (bmap * sizeof(unsigned long)) << 3;
  296. alloc_sz = sizeof(*pools) + sizeof(unsigned long) * bmap;
  297. pools = __alloc_percpu(alloc_sz, __alignof__(struct cxgbi_ppm_pool));
  298. if (!pools)
  299. return NULL;
  300. for_each_possible_cpu(cpu) {
  301. struct cxgbi_ppm_pool *ppool = per_cpu_ptr(pools, cpu);
  302. memset(ppool, 0, alloc_sz);
  303. spin_lock_init(&ppool->lock);
  304. count += ppmax;
  305. }
  306. *total = count;
  307. *pcpu_ppmax = ppmax;
  308. return pools;
  309. }
  310. int cxgbi_ppm_init(void **ppm_pp, struct net_device *ndev,
  311. struct pci_dev *pdev, void *lldev,
  312. struct cxgbi_tag_format *tformat,
  313. unsigned int ppmax,
  314. unsigned int llimit,
  315. unsigned int start,
  316. unsigned int reserve_factor)
  317. {
  318. struct cxgbi_ppm *ppm = (struct cxgbi_ppm *)(*ppm_pp);
  319. struct cxgbi_ppm_pool *pool = NULL;
  320. unsigned int ppmax_pool = 0;
  321. unsigned int pool_index_max = 0;
  322. unsigned int alloc_sz;
  323. unsigned int ppod_bmap_size;
  324. if (ppm) {
  325. pr_info("ippm: %s, ppm 0x%p,0x%p already initialized, %u/%u.\n",
  326. ndev->name, ppm_pp, ppm, ppm->ppmax, ppmax);
  327. kref_get(&ppm->refcnt);
  328. return 1;
  329. }
  330. if (reserve_factor) {
  331. ppmax_pool = ppmax / reserve_factor;
  332. pool = ppm_alloc_cpu_pool(&ppmax_pool, &pool_index_max);
  333. pr_debug("%s: ppmax %u, cpu total %u, per cpu %u.\n",
  334. ndev->name, ppmax, ppmax_pool, pool_index_max);
  335. }
  336. ppod_bmap_size = BITS_TO_LONGS(ppmax - ppmax_pool);
  337. alloc_sz = sizeof(struct cxgbi_ppm) +
  338. ppmax * (sizeof(struct cxgbi_ppod_data)) +
  339. ppod_bmap_size * sizeof(unsigned long);
  340. ppm = vmalloc(alloc_sz);
  341. if (!ppm)
  342. goto release_ppm_pool;
  343. memset(ppm, 0, alloc_sz);
  344. ppm->ppod_bmap = (unsigned long *)(&ppm->ppod_data[ppmax]);
  345. if ((ppod_bmap_size >> 3) > (ppmax - ppmax_pool)) {
  346. unsigned int start = ppmax - ppmax_pool;
  347. unsigned int end = ppod_bmap_size >> 3;
  348. bitmap_set(ppm->ppod_bmap, ppmax, end - start);
  349. pr_info("%s: %u - %u < %u * 8, mask extra bits %u, %u.\n",
  350. __func__, ppmax, ppmax_pool, ppod_bmap_size, start,
  351. end);
  352. }
  353. spin_lock_init(&ppm->map_lock);
  354. kref_init(&ppm->refcnt);
  355. memcpy(&ppm->tformat, tformat, sizeof(struct cxgbi_tag_format));
  356. ppm->ppm_pp = ppm_pp;
  357. ppm->ndev = ndev;
  358. ppm->pdev = pdev;
  359. ppm->lldev = lldev;
  360. ppm->ppmax = ppmax;
  361. ppm->next = 0;
  362. ppm->llimit = llimit;
  363. ppm->base_idx = start > llimit ?
  364. (start - llimit + 1) >> PPOD_SIZE_SHIFT : 0;
  365. ppm->bmap_index_max = ppmax - ppmax_pool;
  366. ppm->pool = pool;
  367. ppm->pool_rsvd = ppmax_pool;
  368. ppm->pool_index_max = pool_index_max;
  369. /* check one more time */
  370. if (*ppm_pp) {
  371. ppm_free(ppm);
  372. ppm = (struct cxgbi_ppm *)(*ppm_pp);
  373. pr_info("ippm: %s, ppm 0x%p,0x%p already initialized, %u/%u.\n",
  374. ndev->name, ppm_pp, *ppm_pp, ppm->ppmax, ppmax);
  375. kref_get(&ppm->refcnt);
  376. return 1;
  377. }
  378. *ppm_pp = ppm;
  379. ppm->tformat.pgsz_idx_dflt = cxgbi_ppm_find_page_index(ppm, PAGE_SIZE);
  380. pr_info("ippm %s: ppm 0x%p, 0x%p, base %u/%u, pg %lu,%u, rsvd %u,%u.\n",
  381. ndev->name, ppm_pp, ppm, ppm->base_idx, ppm->ppmax, PAGE_SIZE,
  382. ppm->tformat.pgsz_idx_dflt, ppm->pool_rsvd,
  383. ppm->pool_index_max);
  384. return 0;
  385. release_ppm_pool:
  386. free_percpu(pool);
  387. return -ENOMEM;
  388. }
  389. EXPORT_SYMBOL(cxgbi_ppm_init);
  390. unsigned int cxgbi_tagmask_set(unsigned int ppmax)
  391. {
  392. unsigned int bits = fls(ppmax);
  393. if (bits > PPOD_IDX_MAX_SIZE)
  394. bits = PPOD_IDX_MAX_SIZE;
  395. pr_info("ippm: ppmax %u/0x%x -> bits %u, tagmask 0x%x.\n",
  396. ppmax, ppmax, bits, 1 << (bits + PPOD_IDX_SHIFT));
  397. return 1 << (bits + PPOD_IDX_SHIFT);
  398. }
  399. EXPORT_SYMBOL(cxgbi_tagmask_set);
  400. MODULE_AUTHOR("Chelsio Communications");
  401. MODULE_DESCRIPTION("Chelsio common library");
  402. MODULE_VERSION(DRV_VERSION);
  403. MODULE_LICENSE("Dual BSD/GPL");