resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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, u32 nr_pdid)
  53. {
  54. int err = 0;
  55. err = c4iw_id_table_alloc(&rdev->resource.tpt_table, 0, nr_tpt, 1,
  56. C4IW_ID_TABLE_F_RANDOM);
  57. if (err)
  58. goto tpt_err;
  59. err = c4iw_init_qid_table(rdev);
  60. if (err)
  61. goto qid_err;
  62. err = c4iw_id_table_alloc(&rdev->resource.pdid_table, 0,
  63. nr_pdid, 1, 0);
  64. if (err)
  65. goto pdid_err;
  66. return 0;
  67. pdid_err:
  68. c4iw_id_table_free(&rdev->resource.qid_table);
  69. qid_err:
  70. c4iw_id_table_free(&rdev->resource.tpt_table);
  71. tpt_err:
  72. return -ENOMEM;
  73. }
  74. /*
  75. * returns 0 if no resource available
  76. */
  77. u32 c4iw_get_resource(struct c4iw_id_table *id_table)
  78. {
  79. u32 entry;
  80. entry = c4iw_id_alloc(id_table);
  81. if (entry == (u32)(-1))
  82. return 0;
  83. return entry;
  84. }
  85. void c4iw_put_resource(struct c4iw_id_table *id_table, u32 entry)
  86. {
  87. pr_debug("%s entry 0x%x\n", __func__, entry);
  88. c4iw_id_free(id_table, entry);
  89. }
  90. u32 c4iw_get_cqid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
  91. {
  92. struct c4iw_qid_list *entry;
  93. u32 qid;
  94. int i;
  95. mutex_lock(&uctx->lock);
  96. if (!list_empty(&uctx->cqids)) {
  97. entry = list_entry(uctx->cqids.next, struct c4iw_qid_list,
  98. entry);
  99. list_del(&entry->entry);
  100. qid = entry->qid;
  101. kfree(entry);
  102. } else {
  103. qid = c4iw_get_resource(&rdev->resource.qid_table);
  104. if (!qid)
  105. goto out;
  106. mutex_lock(&rdev->stats.lock);
  107. rdev->stats.qid.cur += rdev->qpmask + 1;
  108. mutex_unlock(&rdev->stats.lock);
  109. for (i = qid+1; i & rdev->qpmask; i++) {
  110. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  111. if (!entry)
  112. goto out;
  113. entry->qid = i;
  114. list_add_tail(&entry->entry, &uctx->cqids);
  115. }
  116. /*
  117. * now put the same ids on the qp list since they all
  118. * map to the same db/gts page.
  119. */
  120. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  121. if (!entry)
  122. goto out;
  123. entry->qid = qid;
  124. list_add_tail(&entry->entry, &uctx->qpids);
  125. for (i = qid+1; i & rdev->qpmask; i++) {
  126. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  127. if (!entry)
  128. goto out;
  129. entry->qid = i;
  130. list_add_tail(&entry->entry, &uctx->qpids);
  131. }
  132. }
  133. out:
  134. mutex_unlock(&uctx->lock);
  135. pr_debug("%s qid 0x%x\n", __func__, qid);
  136. mutex_lock(&rdev->stats.lock);
  137. if (rdev->stats.qid.cur > rdev->stats.qid.max)
  138. rdev->stats.qid.max = rdev->stats.qid.cur;
  139. mutex_unlock(&rdev->stats.lock);
  140. return qid;
  141. }
  142. void c4iw_put_cqid(struct c4iw_rdev *rdev, u32 qid,
  143. struct c4iw_dev_ucontext *uctx)
  144. {
  145. struct c4iw_qid_list *entry;
  146. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  147. if (!entry)
  148. return;
  149. pr_debug("%s qid 0x%x\n", __func__, qid);
  150. entry->qid = qid;
  151. mutex_lock(&uctx->lock);
  152. list_add_tail(&entry->entry, &uctx->cqids);
  153. mutex_unlock(&uctx->lock);
  154. }
  155. u32 c4iw_get_qpid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
  156. {
  157. struct c4iw_qid_list *entry;
  158. u32 qid;
  159. int i;
  160. mutex_lock(&uctx->lock);
  161. if (!list_empty(&uctx->qpids)) {
  162. entry = list_entry(uctx->qpids.next, struct c4iw_qid_list,
  163. entry);
  164. list_del(&entry->entry);
  165. qid = entry->qid;
  166. kfree(entry);
  167. } else {
  168. qid = c4iw_get_resource(&rdev->resource.qid_table);
  169. if (!qid) {
  170. mutex_lock(&rdev->stats.lock);
  171. rdev->stats.qid.fail++;
  172. mutex_unlock(&rdev->stats.lock);
  173. goto out;
  174. }
  175. mutex_lock(&rdev->stats.lock);
  176. rdev->stats.qid.cur += rdev->qpmask + 1;
  177. mutex_unlock(&rdev->stats.lock);
  178. for (i = qid+1; i & rdev->qpmask; i++) {
  179. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  180. if (!entry)
  181. goto out;
  182. entry->qid = i;
  183. list_add_tail(&entry->entry, &uctx->qpids);
  184. }
  185. /*
  186. * now put the same ids on the cq list since they all
  187. * map to the same db/gts page.
  188. */
  189. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  190. if (!entry)
  191. goto out;
  192. entry->qid = qid;
  193. list_add_tail(&entry->entry, &uctx->cqids);
  194. for (i = qid; i & rdev->qpmask; i++) {
  195. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  196. if (!entry)
  197. goto out;
  198. entry->qid = i;
  199. list_add_tail(&entry->entry, &uctx->cqids);
  200. }
  201. }
  202. out:
  203. mutex_unlock(&uctx->lock);
  204. pr_debug("%s qid 0x%x\n", __func__, qid);
  205. mutex_lock(&rdev->stats.lock);
  206. if (rdev->stats.qid.cur > rdev->stats.qid.max)
  207. rdev->stats.qid.max = rdev->stats.qid.cur;
  208. mutex_unlock(&rdev->stats.lock);
  209. return qid;
  210. }
  211. void c4iw_put_qpid(struct c4iw_rdev *rdev, u32 qid,
  212. struct c4iw_dev_ucontext *uctx)
  213. {
  214. struct c4iw_qid_list *entry;
  215. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  216. if (!entry)
  217. return;
  218. pr_debug("%s qid 0x%x\n", __func__, qid);
  219. entry->qid = qid;
  220. mutex_lock(&uctx->lock);
  221. list_add_tail(&entry->entry, &uctx->qpids);
  222. mutex_unlock(&uctx->lock);
  223. }
  224. void c4iw_destroy_resource(struct c4iw_resource *rscp)
  225. {
  226. c4iw_id_table_free(&rscp->tpt_table);
  227. c4iw_id_table_free(&rscp->qid_table);
  228. c4iw_id_table_free(&rscp->pdid_table);
  229. }
  230. /*
  231. * PBL Memory Manager. Uses Linux generic allocator.
  232. */
  233. #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
  234. u32 c4iw_pblpool_alloc(struct c4iw_rdev *rdev, int size)
  235. {
  236. unsigned long addr = gen_pool_alloc(rdev->pbl_pool, size);
  237. pr_debug("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
  238. mutex_lock(&rdev->stats.lock);
  239. if (addr) {
  240. rdev->stats.pbl.cur += roundup(size, 1 << MIN_PBL_SHIFT);
  241. if (rdev->stats.pbl.cur > rdev->stats.pbl.max)
  242. rdev->stats.pbl.max = rdev->stats.pbl.cur;
  243. kref_get(&rdev->pbl_kref);
  244. } else
  245. rdev->stats.pbl.fail++;
  246. mutex_unlock(&rdev->stats.lock);
  247. return (u32)addr;
  248. }
  249. static void destroy_pblpool(struct kref *kref)
  250. {
  251. struct c4iw_rdev *rdev;
  252. rdev = container_of(kref, struct c4iw_rdev, pbl_kref);
  253. gen_pool_destroy(rdev->pbl_pool);
  254. complete(&rdev->pbl_compl);
  255. }
  256. void c4iw_pblpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  257. {
  258. pr_debug("%s addr 0x%x size %d\n", __func__, addr, size);
  259. mutex_lock(&rdev->stats.lock);
  260. rdev->stats.pbl.cur -= roundup(size, 1 << MIN_PBL_SHIFT);
  261. mutex_unlock(&rdev->stats.lock);
  262. gen_pool_free(rdev->pbl_pool, (unsigned long)addr, size);
  263. kref_put(&rdev->pbl_kref, destroy_pblpool);
  264. }
  265. int c4iw_pblpool_create(struct c4iw_rdev *rdev)
  266. {
  267. unsigned pbl_start, pbl_chunk, pbl_top;
  268. rdev->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
  269. if (!rdev->pbl_pool)
  270. return -ENOMEM;
  271. pbl_start = rdev->lldi.vr->pbl.start;
  272. pbl_chunk = rdev->lldi.vr->pbl.size;
  273. pbl_top = pbl_start + pbl_chunk;
  274. while (pbl_start < pbl_top) {
  275. pbl_chunk = min(pbl_top - pbl_start + 1, pbl_chunk);
  276. if (gen_pool_add(rdev->pbl_pool, pbl_start, pbl_chunk, -1)) {
  277. pr_debug("%s failed to add PBL chunk (%x/%x)\n",
  278. __func__, pbl_start, pbl_chunk);
  279. if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
  280. pr_warn("Failed to add all PBL chunks (%x/%x)\n",
  281. pbl_start, pbl_top - pbl_start);
  282. return 0;
  283. }
  284. pbl_chunk >>= 1;
  285. } else {
  286. pr_debug("%s added PBL chunk (%x/%x)\n",
  287. __func__, pbl_start, pbl_chunk);
  288. pbl_start += pbl_chunk;
  289. }
  290. }
  291. return 0;
  292. }
  293. void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
  294. {
  295. kref_put(&rdev->pbl_kref, destroy_pblpool);
  296. }
  297. /*
  298. * RQT Memory Manager. Uses Linux generic allocator.
  299. */
  300. #define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
  301. u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
  302. {
  303. unsigned long addr = gen_pool_alloc(rdev->rqt_pool, size << 6);
  304. pr_debug("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
  305. if (!addr)
  306. pr_warn_ratelimited("%s: Out of RQT memory\n",
  307. pci_name(rdev->lldi.pdev));
  308. mutex_lock(&rdev->stats.lock);
  309. if (addr) {
  310. rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
  311. if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
  312. rdev->stats.rqt.max = rdev->stats.rqt.cur;
  313. kref_get(&rdev->rqt_kref);
  314. } else
  315. rdev->stats.rqt.fail++;
  316. mutex_unlock(&rdev->stats.lock);
  317. return (u32)addr;
  318. }
  319. static void destroy_rqtpool(struct kref *kref)
  320. {
  321. struct c4iw_rdev *rdev;
  322. rdev = container_of(kref, struct c4iw_rdev, rqt_kref);
  323. gen_pool_destroy(rdev->rqt_pool);
  324. complete(&rdev->rqt_compl);
  325. }
  326. void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  327. {
  328. pr_debug("%s addr 0x%x size %d\n", __func__, addr, size << 6);
  329. mutex_lock(&rdev->stats.lock);
  330. rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
  331. mutex_unlock(&rdev->stats.lock);
  332. gen_pool_free(rdev->rqt_pool, (unsigned long)addr, size << 6);
  333. kref_put(&rdev->rqt_kref, destroy_rqtpool);
  334. }
  335. int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
  336. {
  337. unsigned rqt_start, rqt_chunk, rqt_top;
  338. rdev->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
  339. if (!rdev->rqt_pool)
  340. return -ENOMEM;
  341. rqt_start = rdev->lldi.vr->rq.start;
  342. rqt_chunk = rdev->lldi.vr->rq.size;
  343. rqt_top = rqt_start + rqt_chunk;
  344. while (rqt_start < rqt_top) {
  345. rqt_chunk = min(rqt_top - rqt_start + 1, rqt_chunk);
  346. if (gen_pool_add(rdev->rqt_pool, rqt_start, rqt_chunk, -1)) {
  347. pr_debug("%s failed to add RQT chunk (%x/%x)\n",
  348. __func__, rqt_start, rqt_chunk);
  349. if (rqt_chunk <= 1024 << MIN_RQT_SHIFT) {
  350. pr_warn("Failed to add all RQT chunks (%x/%x)\n",
  351. rqt_start, rqt_top - rqt_start);
  352. return 0;
  353. }
  354. rqt_chunk >>= 1;
  355. } else {
  356. pr_debug("%s added RQT chunk (%x/%x)\n",
  357. __func__, rqt_start, rqt_chunk);
  358. rqt_start += rqt_chunk;
  359. }
  360. }
  361. return 0;
  362. }
  363. void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
  364. {
  365. kref_put(&rdev->rqt_kref, destroy_rqtpool);
  366. }
  367. /*
  368. * On-Chip QP Memory.
  369. */
  370. #define MIN_OCQP_SHIFT 12 /* 4KB == min ocqp size */
  371. u32 c4iw_ocqp_pool_alloc(struct c4iw_rdev *rdev, int size)
  372. {
  373. unsigned long addr = gen_pool_alloc(rdev->ocqp_pool, size);
  374. pr_debug("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
  375. if (addr) {
  376. mutex_lock(&rdev->stats.lock);
  377. rdev->stats.ocqp.cur += roundup(size, 1 << MIN_OCQP_SHIFT);
  378. if (rdev->stats.ocqp.cur > rdev->stats.ocqp.max)
  379. rdev->stats.ocqp.max = rdev->stats.ocqp.cur;
  380. mutex_unlock(&rdev->stats.lock);
  381. }
  382. return (u32)addr;
  383. }
  384. void c4iw_ocqp_pool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  385. {
  386. pr_debug("%s addr 0x%x size %d\n", __func__, addr, size);
  387. mutex_lock(&rdev->stats.lock);
  388. rdev->stats.ocqp.cur -= roundup(size, 1 << MIN_OCQP_SHIFT);
  389. mutex_unlock(&rdev->stats.lock);
  390. gen_pool_free(rdev->ocqp_pool, (unsigned long)addr, size);
  391. }
  392. int c4iw_ocqp_pool_create(struct c4iw_rdev *rdev)
  393. {
  394. unsigned start, chunk, top;
  395. rdev->ocqp_pool = gen_pool_create(MIN_OCQP_SHIFT, -1);
  396. if (!rdev->ocqp_pool)
  397. return -ENOMEM;
  398. start = rdev->lldi.vr->ocq.start;
  399. chunk = rdev->lldi.vr->ocq.size;
  400. top = start + chunk;
  401. while (start < top) {
  402. chunk = min(top - start + 1, chunk);
  403. if (gen_pool_add(rdev->ocqp_pool, start, chunk, -1)) {
  404. pr_debug("%s failed to add OCQP chunk (%x/%x)\n",
  405. __func__, start, chunk);
  406. if (chunk <= 1024 << MIN_OCQP_SHIFT) {
  407. pr_warn("Failed to add all OCQP chunks (%x/%x)\n",
  408. start, top - start);
  409. return 0;
  410. }
  411. chunk >>= 1;
  412. } else {
  413. pr_debug("%s added OCQP chunk (%x/%x)\n",
  414. __func__, start, chunk);
  415. start += chunk;
  416. }
  417. }
  418. return 0;
  419. }
  420. void c4iw_ocqp_pool_destroy(struct c4iw_rdev *rdev)
  421. {
  422. gen_pool_destroy(rdev->ocqp_pool);
  423. }