resource.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. /* Crude resource management */
  33. #include <linux/spinlock.h>
  34. #include <linux/genalloc.h>
  35. #include <linux/ratelimit.h>
  36. #include "iw_cxgb4.h"
  37. static int c4iw_init_qid_table(struct c4iw_rdev *rdev)
  38. {
  39. u32 i;
  40. if (c4iw_id_table_alloc(&rdev->resource.qid_table,
  41. rdev->lldi.vr->qp.start,
  42. rdev->lldi.vr->qp.size,
  43. rdev->lldi.vr->qp.size, 0))
  44. return -ENOMEM;
  45. for (i = rdev->lldi.vr->qp.start;
  46. i < rdev->lldi.vr->qp.start + rdev->lldi.vr->qp.size; i++)
  47. if (!(i & rdev->qpmask))
  48. c4iw_id_free(&rdev->resource.qid_table, i);
  49. return 0;
  50. }
  51. /* nr_* must be power of 2 */
  52. int c4iw_init_resource(struct c4iw_rdev *rdev, u32 nr_tpt,
  53. u32 nr_pdid, u32 nr_srqt)
  54. {
  55. int err = 0;
  56. err = c4iw_id_table_alloc(&rdev->resource.tpt_table, 0, nr_tpt, 1,
  57. C4IW_ID_TABLE_F_RANDOM);
  58. if (err)
  59. goto tpt_err;
  60. err = c4iw_init_qid_table(rdev);
  61. if (err)
  62. goto qid_err;
  63. err = c4iw_id_table_alloc(&rdev->resource.pdid_table, 0,
  64. nr_pdid, 1, 0);
  65. if (err)
  66. goto pdid_err;
  67. if (!nr_srqt)
  68. err = c4iw_id_table_alloc(&rdev->resource.srq_table, 0,
  69. 1, 1, 0);
  70. else
  71. err = c4iw_id_table_alloc(&rdev->resource.srq_table, 0,
  72. nr_srqt, 0, 0);
  73. if (err)
  74. goto srq_err;
  75. return 0;
  76. srq_err:
  77. c4iw_id_table_free(&rdev->resource.pdid_table);
  78. pdid_err:
  79. c4iw_id_table_free(&rdev->resource.qid_table);
  80. qid_err:
  81. c4iw_id_table_free(&rdev->resource.tpt_table);
  82. tpt_err:
  83. return -ENOMEM;
  84. }
  85. /*
  86. * returns 0 if no resource available
  87. */
  88. u32 c4iw_get_resource(struct c4iw_id_table *id_table)
  89. {
  90. u32 entry;
  91. entry = c4iw_id_alloc(id_table);
  92. if (entry == (u32)(-1))
  93. return 0;
  94. return entry;
  95. }
  96. void c4iw_put_resource(struct c4iw_id_table *id_table, u32 entry)
  97. {
  98. pr_debug("entry 0x%x\n", entry);
  99. c4iw_id_free(id_table, entry);
  100. }
  101. u32 c4iw_get_cqid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
  102. {
  103. struct c4iw_qid_list *entry;
  104. u32 qid;
  105. int i;
  106. mutex_lock(&uctx->lock);
  107. if (!list_empty(&uctx->cqids)) {
  108. entry = list_entry(uctx->cqids.next, struct c4iw_qid_list,
  109. entry);
  110. list_del(&entry->entry);
  111. qid = entry->qid;
  112. kfree(entry);
  113. } else {
  114. qid = c4iw_get_resource(&rdev->resource.qid_table);
  115. if (!qid)
  116. goto out;
  117. mutex_lock(&rdev->stats.lock);
  118. rdev->stats.qid.cur += rdev->qpmask + 1;
  119. mutex_unlock(&rdev->stats.lock);
  120. for (i = qid+1; i & rdev->qpmask; i++) {
  121. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  122. if (!entry)
  123. goto out;
  124. entry->qid = i;
  125. list_add_tail(&entry->entry, &uctx->cqids);
  126. }
  127. /*
  128. * now put the same ids on the qp list since they all
  129. * map to the same db/gts page.
  130. */
  131. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  132. if (!entry)
  133. goto out;
  134. entry->qid = qid;
  135. list_add_tail(&entry->entry, &uctx->qpids);
  136. for (i = qid+1; i & rdev->qpmask; i++) {
  137. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  138. if (!entry)
  139. goto out;
  140. entry->qid = i;
  141. list_add_tail(&entry->entry, &uctx->qpids);
  142. }
  143. }
  144. out:
  145. mutex_unlock(&uctx->lock);
  146. pr_debug("qid 0x%x\n", qid);
  147. mutex_lock(&rdev->stats.lock);
  148. if (rdev->stats.qid.cur > rdev->stats.qid.max)
  149. rdev->stats.qid.max = rdev->stats.qid.cur;
  150. mutex_unlock(&rdev->stats.lock);
  151. return qid;
  152. }
  153. void c4iw_put_cqid(struct c4iw_rdev *rdev, u32 qid,
  154. struct c4iw_dev_ucontext *uctx)
  155. {
  156. struct c4iw_qid_list *entry;
  157. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  158. if (!entry)
  159. return;
  160. pr_debug("qid 0x%x\n", qid);
  161. entry->qid = qid;
  162. mutex_lock(&uctx->lock);
  163. list_add_tail(&entry->entry, &uctx->cqids);
  164. mutex_unlock(&uctx->lock);
  165. }
  166. u32 c4iw_get_qpid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
  167. {
  168. struct c4iw_qid_list *entry;
  169. u32 qid;
  170. int i;
  171. mutex_lock(&uctx->lock);
  172. if (!list_empty(&uctx->qpids)) {
  173. entry = list_entry(uctx->qpids.next, struct c4iw_qid_list,
  174. entry);
  175. list_del(&entry->entry);
  176. qid = entry->qid;
  177. kfree(entry);
  178. } else {
  179. qid = c4iw_get_resource(&rdev->resource.qid_table);
  180. if (!qid) {
  181. mutex_lock(&rdev->stats.lock);
  182. rdev->stats.qid.fail++;
  183. mutex_unlock(&rdev->stats.lock);
  184. goto out;
  185. }
  186. mutex_lock(&rdev->stats.lock);
  187. rdev->stats.qid.cur += rdev->qpmask + 1;
  188. mutex_unlock(&rdev->stats.lock);
  189. for (i = qid+1; i & rdev->qpmask; i++) {
  190. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  191. if (!entry)
  192. goto out;
  193. entry->qid = i;
  194. list_add_tail(&entry->entry, &uctx->qpids);
  195. }
  196. /*
  197. * now put the same ids on the cq list since they all
  198. * map to the same db/gts page.
  199. */
  200. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  201. if (!entry)
  202. goto out;
  203. entry->qid = qid;
  204. list_add_tail(&entry->entry, &uctx->cqids);
  205. for (i = qid; i & rdev->qpmask; i++) {
  206. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  207. if (!entry)
  208. goto out;
  209. entry->qid = i;
  210. list_add_tail(&entry->entry, &uctx->cqids);
  211. }
  212. }
  213. out:
  214. mutex_unlock(&uctx->lock);
  215. pr_debug("qid 0x%x\n", qid);
  216. mutex_lock(&rdev->stats.lock);
  217. if (rdev->stats.qid.cur > rdev->stats.qid.max)
  218. rdev->stats.qid.max = rdev->stats.qid.cur;
  219. mutex_unlock(&rdev->stats.lock);
  220. return qid;
  221. }
  222. void c4iw_put_qpid(struct c4iw_rdev *rdev, u32 qid,
  223. struct c4iw_dev_ucontext *uctx)
  224. {
  225. struct c4iw_qid_list *entry;
  226. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  227. if (!entry)
  228. return;
  229. pr_debug("qid 0x%x\n", qid);
  230. entry->qid = qid;
  231. mutex_lock(&uctx->lock);
  232. list_add_tail(&entry->entry, &uctx->qpids);
  233. mutex_unlock(&uctx->lock);
  234. }
  235. void c4iw_destroy_resource(struct c4iw_resource *rscp)
  236. {
  237. c4iw_id_table_free(&rscp->tpt_table);
  238. c4iw_id_table_free(&rscp->qid_table);
  239. c4iw_id_table_free(&rscp->pdid_table);
  240. }
  241. /*
  242. * PBL Memory Manager. Uses Linux generic allocator.
  243. */
  244. #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
  245. u32 c4iw_pblpool_alloc(struct c4iw_rdev *rdev, int size)
  246. {
  247. unsigned long addr = gen_pool_alloc(rdev->pbl_pool, size);
  248. pr_debug("addr 0x%x size %d\n", (u32)addr, size);
  249. mutex_lock(&rdev->stats.lock);
  250. if (addr) {
  251. rdev->stats.pbl.cur += roundup(size, 1 << MIN_PBL_SHIFT);
  252. if (rdev->stats.pbl.cur > rdev->stats.pbl.max)
  253. rdev->stats.pbl.max = rdev->stats.pbl.cur;
  254. kref_get(&rdev->pbl_kref);
  255. } else
  256. rdev->stats.pbl.fail++;
  257. mutex_unlock(&rdev->stats.lock);
  258. return (u32)addr;
  259. }
  260. static void destroy_pblpool(struct kref *kref)
  261. {
  262. struct c4iw_rdev *rdev;
  263. rdev = container_of(kref, struct c4iw_rdev, pbl_kref);
  264. gen_pool_destroy(rdev->pbl_pool);
  265. complete(&rdev->pbl_compl);
  266. }
  267. void c4iw_pblpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  268. {
  269. pr_debug("addr 0x%x size %d\n", addr, size);
  270. mutex_lock(&rdev->stats.lock);
  271. rdev->stats.pbl.cur -= roundup(size, 1 << MIN_PBL_SHIFT);
  272. mutex_unlock(&rdev->stats.lock);
  273. gen_pool_free(rdev->pbl_pool, (unsigned long)addr, size);
  274. kref_put(&rdev->pbl_kref, destroy_pblpool);
  275. }
  276. int c4iw_pblpool_create(struct c4iw_rdev *rdev)
  277. {
  278. unsigned pbl_start, pbl_chunk, pbl_top;
  279. rdev->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
  280. if (!rdev->pbl_pool)
  281. return -ENOMEM;
  282. pbl_start = rdev->lldi.vr->pbl.start;
  283. pbl_chunk = rdev->lldi.vr->pbl.size;
  284. pbl_top = pbl_start + pbl_chunk;
  285. while (pbl_start < pbl_top) {
  286. pbl_chunk = min(pbl_top - pbl_start + 1, pbl_chunk);
  287. if (gen_pool_add(rdev->pbl_pool, pbl_start, pbl_chunk, -1)) {
  288. pr_debug("failed to add PBL chunk (%x/%x)\n",
  289. pbl_start, pbl_chunk);
  290. if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
  291. pr_warn("Failed to add all PBL chunks (%x/%x)\n",
  292. pbl_start, pbl_top - pbl_start);
  293. return 0;
  294. }
  295. pbl_chunk >>= 1;
  296. } else {
  297. pr_debug("added PBL chunk (%x/%x)\n",
  298. pbl_start, pbl_chunk);
  299. pbl_start += pbl_chunk;
  300. }
  301. }
  302. return 0;
  303. }
  304. void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
  305. {
  306. kref_put(&rdev->pbl_kref, destroy_pblpool);
  307. }
  308. /*
  309. * RQT Memory Manager. Uses Linux generic allocator.
  310. */
  311. #define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
  312. u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
  313. {
  314. unsigned long addr = gen_pool_alloc(rdev->rqt_pool, size << 6);
  315. pr_debug("addr 0x%x size %d\n", (u32)addr, size << 6);
  316. if (!addr)
  317. pr_warn_ratelimited("%s: Out of RQT memory\n",
  318. pci_name(rdev->lldi.pdev));
  319. mutex_lock(&rdev->stats.lock);
  320. if (addr) {
  321. rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
  322. if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
  323. rdev->stats.rqt.max = rdev->stats.rqt.cur;
  324. kref_get(&rdev->rqt_kref);
  325. } else
  326. rdev->stats.rqt.fail++;
  327. mutex_unlock(&rdev->stats.lock);
  328. return (u32)addr;
  329. }
  330. static void destroy_rqtpool(struct kref *kref)
  331. {
  332. struct c4iw_rdev *rdev;
  333. rdev = container_of(kref, struct c4iw_rdev, rqt_kref);
  334. gen_pool_destroy(rdev->rqt_pool);
  335. complete(&rdev->rqt_compl);
  336. }
  337. void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  338. {
  339. pr_debug("addr 0x%x size %d\n", addr, size << 6);
  340. mutex_lock(&rdev->stats.lock);
  341. rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
  342. mutex_unlock(&rdev->stats.lock);
  343. gen_pool_free(rdev->rqt_pool, (unsigned long)addr, size << 6);
  344. kref_put(&rdev->rqt_kref, destroy_rqtpool);
  345. }
  346. int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
  347. {
  348. unsigned rqt_start, rqt_chunk, rqt_top;
  349. int skip = 0;
  350. rdev->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
  351. if (!rdev->rqt_pool)
  352. return -ENOMEM;
  353. /*
  354. * If SRQs are supported, then never use the first RQE from
  355. * the RQT region. This is because HW uses RQT index 0 as NULL.
  356. */
  357. if (rdev->lldi.vr->srq.size)
  358. skip = T4_RQT_ENTRY_SIZE;
  359. rqt_start = rdev->lldi.vr->rq.start + skip;
  360. rqt_chunk = rdev->lldi.vr->rq.size - skip;
  361. rqt_top = rqt_start + rqt_chunk;
  362. while (rqt_start < rqt_top) {
  363. rqt_chunk = min(rqt_top - rqt_start + 1, rqt_chunk);
  364. if (gen_pool_add(rdev->rqt_pool, rqt_start, rqt_chunk, -1)) {
  365. pr_debug("failed to add RQT chunk (%x/%x)\n",
  366. rqt_start, rqt_chunk);
  367. if (rqt_chunk <= 1024 << MIN_RQT_SHIFT) {
  368. pr_warn("Failed to add all RQT chunks (%x/%x)\n",
  369. rqt_start, rqt_top - rqt_start);
  370. return 0;
  371. }
  372. rqt_chunk >>= 1;
  373. } else {
  374. pr_debug("added RQT chunk (%x/%x)\n",
  375. rqt_start, rqt_chunk);
  376. rqt_start += rqt_chunk;
  377. }
  378. }
  379. return 0;
  380. }
  381. void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
  382. {
  383. kref_put(&rdev->rqt_kref, destroy_rqtpool);
  384. }
  385. int c4iw_alloc_srq_idx(struct c4iw_rdev *rdev)
  386. {
  387. int idx;
  388. idx = c4iw_id_alloc(&rdev->resource.srq_table);
  389. mutex_lock(&rdev->stats.lock);
  390. if (idx == -1) {
  391. rdev->stats.srqt.fail++;
  392. mutex_unlock(&rdev->stats.lock);
  393. return -ENOMEM;
  394. }
  395. rdev->stats.srqt.cur++;
  396. if (rdev->stats.srqt.cur > rdev->stats.srqt.max)
  397. rdev->stats.srqt.max = rdev->stats.srqt.cur;
  398. mutex_unlock(&rdev->stats.lock);
  399. return idx;
  400. }
  401. void c4iw_free_srq_idx(struct c4iw_rdev *rdev, int idx)
  402. {
  403. c4iw_id_free(&rdev->resource.srq_table, idx);
  404. mutex_lock(&rdev->stats.lock);
  405. rdev->stats.srqt.cur--;
  406. mutex_unlock(&rdev->stats.lock);
  407. }
  408. /*
  409. * On-Chip QP Memory.
  410. */
  411. #define MIN_OCQP_SHIFT 12 /* 4KB == min ocqp size */
  412. u32 c4iw_ocqp_pool_alloc(struct c4iw_rdev *rdev, int size)
  413. {
  414. unsigned long addr = gen_pool_alloc(rdev->ocqp_pool, size);
  415. pr_debug("addr 0x%x size %d\n", (u32)addr, size);
  416. if (addr) {
  417. mutex_lock(&rdev->stats.lock);
  418. rdev->stats.ocqp.cur += roundup(size, 1 << MIN_OCQP_SHIFT);
  419. if (rdev->stats.ocqp.cur > rdev->stats.ocqp.max)
  420. rdev->stats.ocqp.max = rdev->stats.ocqp.cur;
  421. mutex_unlock(&rdev->stats.lock);
  422. }
  423. return (u32)addr;
  424. }
  425. void c4iw_ocqp_pool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  426. {
  427. pr_debug("addr 0x%x size %d\n", addr, size);
  428. mutex_lock(&rdev->stats.lock);
  429. rdev->stats.ocqp.cur -= roundup(size, 1 << MIN_OCQP_SHIFT);
  430. mutex_unlock(&rdev->stats.lock);
  431. gen_pool_free(rdev->ocqp_pool, (unsigned long)addr, size);
  432. }
  433. int c4iw_ocqp_pool_create(struct c4iw_rdev *rdev)
  434. {
  435. unsigned start, chunk, top;
  436. rdev->ocqp_pool = gen_pool_create(MIN_OCQP_SHIFT, -1);
  437. if (!rdev->ocqp_pool)
  438. return -ENOMEM;
  439. start = rdev->lldi.vr->ocq.start;
  440. chunk = rdev->lldi.vr->ocq.size;
  441. top = start + chunk;
  442. while (start < top) {
  443. chunk = min(top - start + 1, chunk);
  444. if (gen_pool_add(rdev->ocqp_pool, start, chunk, -1)) {
  445. pr_debug("failed to add OCQP chunk (%x/%x)\n",
  446. start, chunk);
  447. if (chunk <= 1024 << MIN_OCQP_SHIFT) {
  448. pr_warn("Failed to add all OCQP chunks (%x/%x)\n",
  449. start, top - start);
  450. return 0;
  451. }
  452. chunk >>= 1;
  453. } else {
  454. pr_debug("added OCQP chunk (%x/%x)\n",
  455. start, chunk);
  456. start += chunk;
  457. }
  458. }
  459. return 0;
  460. }
  461. void c4iw_ocqp_pool_destroy(struct c4iw_rdev *rdev)
  462. {
  463. gen_pool_destroy(rdev->ocqp_pool);
  464. }