resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. goto out;
  171. mutex_lock(&rdev->stats.lock);
  172. rdev->stats.qid.cur += rdev->qpmask + 1;
  173. mutex_unlock(&rdev->stats.lock);
  174. for (i = qid+1; i & rdev->qpmask; i++) {
  175. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  176. if (!entry)
  177. goto out;
  178. entry->qid = i;
  179. list_add_tail(&entry->entry, &uctx->qpids);
  180. }
  181. /*
  182. * now put the same ids on the cq list since they all
  183. * map to the same db/gts page.
  184. */
  185. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  186. if (!entry)
  187. goto out;
  188. entry->qid = qid;
  189. list_add_tail(&entry->entry, &uctx->cqids);
  190. for (i = qid; i & rdev->qpmask; i++) {
  191. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  192. if (!entry)
  193. goto out;
  194. entry->qid = i;
  195. list_add_tail(&entry->entry, &uctx->cqids);
  196. }
  197. }
  198. out:
  199. mutex_unlock(&uctx->lock);
  200. PDBG("%s qid 0x%x\n", __func__, qid);
  201. mutex_lock(&rdev->stats.lock);
  202. if (rdev->stats.qid.cur > rdev->stats.qid.max)
  203. rdev->stats.qid.max = rdev->stats.qid.cur;
  204. mutex_unlock(&rdev->stats.lock);
  205. return qid;
  206. }
  207. void c4iw_put_qpid(struct c4iw_rdev *rdev, u32 qid,
  208. struct c4iw_dev_ucontext *uctx)
  209. {
  210. struct c4iw_qid_list *entry;
  211. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  212. if (!entry)
  213. return;
  214. PDBG("%s qid 0x%x\n", __func__, qid);
  215. entry->qid = qid;
  216. mutex_lock(&uctx->lock);
  217. list_add_tail(&entry->entry, &uctx->qpids);
  218. mutex_unlock(&uctx->lock);
  219. }
  220. void c4iw_destroy_resource(struct c4iw_resource *rscp)
  221. {
  222. c4iw_id_table_free(&rscp->tpt_table);
  223. c4iw_id_table_free(&rscp->qid_table);
  224. c4iw_id_table_free(&rscp->pdid_table);
  225. }
  226. /*
  227. * PBL Memory Manager. Uses Linux generic allocator.
  228. */
  229. #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
  230. u32 c4iw_pblpool_alloc(struct c4iw_rdev *rdev, int size)
  231. {
  232. unsigned long addr = gen_pool_alloc(rdev->pbl_pool, size);
  233. PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
  234. mutex_lock(&rdev->stats.lock);
  235. if (addr) {
  236. rdev->stats.pbl.cur += roundup(size, 1 << MIN_PBL_SHIFT);
  237. if (rdev->stats.pbl.cur > rdev->stats.pbl.max)
  238. rdev->stats.pbl.max = rdev->stats.pbl.cur;
  239. } else
  240. rdev->stats.pbl.fail++;
  241. mutex_unlock(&rdev->stats.lock);
  242. return (u32)addr;
  243. }
  244. void c4iw_pblpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  245. {
  246. PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
  247. mutex_lock(&rdev->stats.lock);
  248. rdev->stats.pbl.cur -= roundup(size, 1 << MIN_PBL_SHIFT);
  249. mutex_unlock(&rdev->stats.lock);
  250. gen_pool_free(rdev->pbl_pool, (unsigned long)addr, size);
  251. }
  252. int c4iw_pblpool_create(struct c4iw_rdev *rdev)
  253. {
  254. unsigned pbl_start, pbl_chunk, pbl_top;
  255. rdev->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
  256. if (!rdev->pbl_pool)
  257. return -ENOMEM;
  258. pbl_start = rdev->lldi.vr->pbl.start;
  259. pbl_chunk = rdev->lldi.vr->pbl.size;
  260. pbl_top = pbl_start + pbl_chunk;
  261. while (pbl_start < pbl_top) {
  262. pbl_chunk = min(pbl_top - pbl_start + 1, pbl_chunk);
  263. if (gen_pool_add(rdev->pbl_pool, pbl_start, pbl_chunk, -1)) {
  264. PDBG("%s failed to add PBL chunk (%x/%x)\n",
  265. __func__, pbl_start, pbl_chunk);
  266. if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
  267. printk(KERN_WARNING MOD
  268. "Failed to add all PBL chunks (%x/%x)\n",
  269. pbl_start,
  270. pbl_top - pbl_start);
  271. return 0;
  272. }
  273. pbl_chunk >>= 1;
  274. } else {
  275. PDBG("%s added PBL chunk (%x/%x)\n",
  276. __func__, pbl_start, pbl_chunk);
  277. pbl_start += pbl_chunk;
  278. }
  279. }
  280. return 0;
  281. }
  282. void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
  283. {
  284. gen_pool_destroy(rdev->pbl_pool);
  285. }
  286. /*
  287. * RQT Memory Manager. Uses Linux generic allocator.
  288. */
  289. #define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
  290. u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
  291. {
  292. unsigned long addr = gen_pool_alloc(rdev->rqt_pool, size << 6);
  293. PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
  294. if (!addr)
  295. printk_ratelimited(KERN_WARNING MOD "%s: Out of RQT memory\n",
  296. pci_name(rdev->lldi.pdev));
  297. mutex_lock(&rdev->stats.lock);
  298. if (addr) {
  299. rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
  300. if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
  301. rdev->stats.rqt.max = rdev->stats.rqt.cur;
  302. } else
  303. rdev->stats.rqt.fail++;
  304. mutex_unlock(&rdev->stats.lock);
  305. return (u32)addr;
  306. }
  307. void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  308. {
  309. PDBG("%s addr 0x%x size %d\n", __func__, addr, size << 6);
  310. mutex_lock(&rdev->stats.lock);
  311. rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
  312. mutex_unlock(&rdev->stats.lock);
  313. gen_pool_free(rdev->rqt_pool, (unsigned long)addr, size << 6);
  314. }
  315. int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
  316. {
  317. unsigned rqt_start, rqt_chunk, rqt_top;
  318. rdev->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
  319. if (!rdev->rqt_pool)
  320. return -ENOMEM;
  321. rqt_start = rdev->lldi.vr->rq.start;
  322. rqt_chunk = rdev->lldi.vr->rq.size;
  323. rqt_top = rqt_start + rqt_chunk;
  324. while (rqt_start < rqt_top) {
  325. rqt_chunk = min(rqt_top - rqt_start + 1, rqt_chunk);
  326. if (gen_pool_add(rdev->rqt_pool, rqt_start, rqt_chunk, -1)) {
  327. PDBG("%s failed to add RQT chunk (%x/%x)\n",
  328. __func__, rqt_start, rqt_chunk);
  329. if (rqt_chunk <= 1024 << MIN_RQT_SHIFT) {
  330. printk(KERN_WARNING MOD
  331. "Failed to add all RQT chunks (%x/%x)\n",
  332. rqt_start, rqt_top - rqt_start);
  333. return 0;
  334. }
  335. rqt_chunk >>= 1;
  336. } else {
  337. PDBG("%s added RQT chunk (%x/%x)\n",
  338. __func__, rqt_start, rqt_chunk);
  339. rqt_start += rqt_chunk;
  340. }
  341. }
  342. return 0;
  343. }
  344. void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
  345. {
  346. gen_pool_destroy(rdev->rqt_pool);
  347. }
  348. /*
  349. * On-Chip QP Memory.
  350. */
  351. #define MIN_OCQP_SHIFT 12 /* 4KB == min ocqp size */
  352. u32 c4iw_ocqp_pool_alloc(struct c4iw_rdev *rdev, int size)
  353. {
  354. unsigned long addr = gen_pool_alloc(rdev->ocqp_pool, size);
  355. PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
  356. if (addr) {
  357. mutex_lock(&rdev->stats.lock);
  358. rdev->stats.ocqp.cur += roundup(size, 1 << MIN_OCQP_SHIFT);
  359. if (rdev->stats.ocqp.cur > rdev->stats.ocqp.max)
  360. rdev->stats.ocqp.max = rdev->stats.ocqp.cur;
  361. mutex_unlock(&rdev->stats.lock);
  362. }
  363. return (u32)addr;
  364. }
  365. void c4iw_ocqp_pool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  366. {
  367. PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
  368. mutex_lock(&rdev->stats.lock);
  369. rdev->stats.ocqp.cur -= roundup(size, 1 << MIN_OCQP_SHIFT);
  370. mutex_unlock(&rdev->stats.lock);
  371. gen_pool_free(rdev->ocqp_pool, (unsigned long)addr, size);
  372. }
  373. int c4iw_ocqp_pool_create(struct c4iw_rdev *rdev)
  374. {
  375. unsigned start, chunk, top;
  376. rdev->ocqp_pool = gen_pool_create(MIN_OCQP_SHIFT, -1);
  377. if (!rdev->ocqp_pool)
  378. return -ENOMEM;
  379. start = rdev->lldi.vr->ocq.start;
  380. chunk = rdev->lldi.vr->ocq.size;
  381. top = start + chunk;
  382. while (start < top) {
  383. chunk = min(top - start + 1, chunk);
  384. if (gen_pool_add(rdev->ocqp_pool, start, chunk, -1)) {
  385. PDBG("%s failed to add OCQP chunk (%x/%x)\n",
  386. __func__, start, chunk);
  387. if (chunk <= 1024 << MIN_OCQP_SHIFT) {
  388. printk(KERN_WARNING MOD
  389. "Failed to add all OCQP chunks (%x/%x)\n",
  390. start, top - start);
  391. return 0;
  392. }
  393. chunk >>= 1;
  394. } else {
  395. PDBG("%s added OCQP chunk (%x/%x)\n",
  396. __func__, start, chunk);
  397. start += chunk;
  398. }
  399. }
  400. return 0;
  401. }
  402. void c4iw_ocqp_pool_destroy(struct c4iw_rdev *rdev)
  403. {
  404. gen_pool_destroy(rdev->ocqp_pool);
  405. }