resource.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. PDBG("%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. PDBG("%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. PDBG("%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. PDBG("%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. PDBG("%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. PDBG("%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. PDBG("%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. PDBG("%s failed to add PBL chunk (%x/%x)\n",
  278. __func__, pbl_start, pbl_chunk);
  279. if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
  280. printk(KERN_WARNING MOD
  281. "Failed to add all PBL chunks (%x/%x)\n",
  282. pbl_start,
  283. pbl_top - pbl_start);
  284. return 0;
  285. }
  286. pbl_chunk >>= 1;
  287. } else {
  288. PDBG("%s added PBL chunk (%x/%x)\n",
  289. __func__, pbl_start, pbl_chunk);
  290. pbl_start += pbl_chunk;
  291. }
  292. }
  293. return 0;
  294. }
  295. void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
  296. {
  297. kref_put(&rdev->pbl_kref, destroy_pblpool);
  298. }
  299. /*
  300. * RQT Memory Manager. Uses Linux generic allocator.
  301. */
  302. #define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
  303. u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
  304. {
  305. unsigned long addr = gen_pool_alloc(rdev->rqt_pool, size << 6);
  306. PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
  307. if (!addr)
  308. pr_warn_ratelimited(MOD "%s: Out of RQT memory\n",
  309. pci_name(rdev->lldi.pdev));
  310. mutex_lock(&rdev->stats.lock);
  311. if (addr) {
  312. rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
  313. if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
  314. rdev->stats.rqt.max = rdev->stats.rqt.cur;
  315. kref_get(&rdev->rqt_kref);
  316. } else
  317. rdev->stats.rqt.fail++;
  318. mutex_unlock(&rdev->stats.lock);
  319. return (u32)addr;
  320. }
  321. static void destroy_rqtpool(struct kref *kref)
  322. {
  323. struct c4iw_rdev *rdev;
  324. rdev = container_of(kref, struct c4iw_rdev, rqt_kref);
  325. gen_pool_destroy(rdev->rqt_pool);
  326. complete(&rdev->rqt_compl);
  327. }
  328. void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  329. {
  330. PDBG("%s addr 0x%x size %d\n", __func__, addr, size << 6);
  331. mutex_lock(&rdev->stats.lock);
  332. rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
  333. mutex_unlock(&rdev->stats.lock);
  334. gen_pool_free(rdev->rqt_pool, (unsigned long)addr, size << 6);
  335. kref_put(&rdev->rqt_kref, destroy_rqtpool);
  336. }
  337. int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
  338. {
  339. unsigned rqt_start, rqt_chunk, rqt_top;
  340. rdev->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
  341. if (!rdev->rqt_pool)
  342. return -ENOMEM;
  343. rqt_start = rdev->lldi.vr->rq.start;
  344. rqt_chunk = rdev->lldi.vr->rq.size;
  345. rqt_top = rqt_start + rqt_chunk;
  346. while (rqt_start < rqt_top) {
  347. rqt_chunk = min(rqt_top - rqt_start + 1, rqt_chunk);
  348. if (gen_pool_add(rdev->rqt_pool, rqt_start, rqt_chunk, -1)) {
  349. PDBG("%s failed to add RQT chunk (%x/%x)\n",
  350. __func__, rqt_start, rqt_chunk);
  351. if (rqt_chunk <= 1024 << MIN_RQT_SHIFT) {
  352. printk(KERN_WARNING MOD
  353. "Failed to add all RQT chunks (%x/%x)\n",
  354. rqt_start, rqt_top - rqt_start);
  355. return 0;
  356. }
  357. rqt_chunk >>= 1;
  358. } else {
  359. PDBG("%s added RQT chunk (%x/%x)\n",
  360. __func__, rqt_start, rqt_chunk);
  361. rqt_start += rqt_chunk;
  362. }
  363. }
  364. return 0;
  365. }
  366. void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
  367. {
  368. kref_put(&rdev->rqt_kref, destroy_rqtpool);
  369. }
  370. /*
  371. * On-Chip QP Memory.
  372. */
  373. #define MIN_OCQP_SHIFT 12 /* 4KB == min ocqp size */
  374. u32 c4iw_ocqp_pool_alloc(struct c4iw_rdev *rdev, int size)
  375. {
  376. unsigned long addr = gen_pool_alloc(rdev->ocqp_pool, size);
  377. PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
  378. if (addr) {
  379. mutex_lock(&rdev->stats.lock);
  380. rdev->stats.ocqp.cur += roundup(size, 1 << MIN_OCQP_SHIFT);
  381. if (rdev->stats.ocqp.cur > rdev->stats.ocqp.max)
  382. rdev->stats.ocqp.max = rdev->stats.ocqp.cur;
  383. mutex_unlock(&rdev->stats.lock);
  384. }
  385. return (u32)addr;
  386. }
  387. void c4iw_ocqp_pool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  388. {
  389. PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
  390. mutex_lock(&rdev->stats.lock);
  391. rdev->stats.ocqp.cur -= roundup(size, 1 << MIN_OCQP_SHIFT);
  392. mutex_unlock(&rdev->stats.lock);
  393. gen_pool_free(rdev->ocqp_pool, (unsigned long)addr, size);
  394. }
  395. int c4iw_ocqp_pool_create(struct c4iw_rdev *rdev)
  396. {
  397. unsigned start, chunk, top;
  398. rdev->ocqp_pool = gen_pool_create(MIN_OCQP_SHIFT, -1);
  399. if (!rdev->ocqp_pool)
  400. return -ENOMEM;
  401. start = rdev->lldi.vr->ocq.start;
  402. chunk = rdev->lldi.vr->ocq.size;
  403. top = start + chunk;
  404. while (start < top) {
  405. chunk = min(top - start + 1, chunk);
  406. if (gen_pool_add(rdev->ocqp_pool, start, chunk, -1)) {
  407. PDBG("%s failed to add OCQP chunk (%x/%x)\n",
  408. __func__, start, chunk);
  409. if (chunk <= 1024 << MIN_OCQP_SHIFT) {
  410. printk(KERN_WARNING MOD
  411. "Failed to add all OCQP chunks (%x/%x)\n",
  412. start, top - start);
  413. return 0;
  414. }
  415. chunk >>= 1;
  416. } else {
  417. PDBG("%s added OCQP chunk (%x/%x)\n",
  418. __func__, start, chunk);
  419. start += chunk;
  420. }
  421. }
  422. return 0;
  423. }
  424. void c4iw_ocqp_pool_destroy(struct c4iw_rdev *rdev)
  425. {
  426. gen_pool_destroy(rdev->ocqp_pool);
  427. }