resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. } else
  244. rdev->stats.pbl.fail++;
  245. mutex_unlock(&rdev->stats.lock);
  246. return (u32)addr;
  247. }
  248. void c4iw_pblpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  249. {
  250. PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
  251. mutex_lock(&rdev->stats.lock);
  252. rdev->stats.pbl.cur -= roundup(size, 1 << MIN_PBL_SHIFT);
  253. mutex_unlock(&rdev->stats.lock);
  254. gen_pool_free(rdev->pbl_pool, (unsigned long)addr, size);
  255. }
  256. int c4iw_pblpool_create(struct c4iw_rdev *rdev)
  257. {
  258. unsigned pbl_start, pbl_chunk, pbl_top;
  259. rdev->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
  260. if (!rdev->pbl_pool)
  261. return -ENOMEM;
  262. pbl_start = rdev->lldi.vr->pbl.start;
  263. pbl_chunk = rdev->lldi.vr->pbl.size;
  264. pbl_top = pbl_start + pbl_chunk;
  265. while (pbl_start < pbl_top) {
  266. pbl_chunk = min(pbl_top - pbl_start + 1, pbl_chunk);
  267. if (gen_pool_add(rdev->pbl_pool, pbl_start, pbl_chunk, -1)) {
  268. PDBG("%s failed to add PBL chunk (%x/%x)\n",
  269. __func__, pbl_start, pbl_chunk);
  270. if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
  271. printk(KERN_WARNING MOD
  272. "Failed to add all PBL chunks (%x/%x)\n",
  273. pbl_start,
  274. pbl_top - pbl_start);
  275. return 0;
  276. }
  277. pbl_chunk >>= 1;
  278. } else {
  279. PDBG("%s added PBL chunk (%x/%x)\n",
  280. __func__, pbl_start, pbl_chunk);
  281. pbl_start += pbl_chunk;
  282. }
  283. }
  284. return 0;
  285. }
  286. void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
  287. {
  288. gen_pool_destroy(rdev->pbl_pool);
  289. }
  290. /*
  291. * RQT Memory Manager. Uses Linux generic allocator.
  292. */
  293. #define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
  294. u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
  295. {
  296. unsigned long addr = gen_pool_alloc(rdev->rqt_pool, size << 6);
  297. PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
  298. if (!addr)
  299. pr_warn_ratelimited(MOD "%s: Out of RQT memory\n",
  300. pci_name(rdev->lldi.pdev));
  301. mutex_lock(&rdev->stats.lock);
  302. if (addr) {
  303. rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
  304. if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
  305. rdev->stats.rqt.max = rdev->stats.rqt.cur;
  306. } else
  307. rdev->stats.rqt.fail++;
  308. mutex_unlock(&rdev->stats.lock);
  309. return (u32)addr;
  310. }
  311. void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  312. {
  313. PDBG("%s addr 0x%x size %d\n", __func__, addr, size << 6);
  314. mutex_lock(&rdev->stats.lock);
  315. rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
  316. mutex_unlock(&rdev->stats.lock);
  317. gen_pool_free(rdev->rqt_pool, (unsigned long)addr, size << 6);
  318. }
  319. int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
  320. {
  321. unsigned rqt_start, rqt_chunk, rqt_top;
  322. rdev->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
  323. if (!rdev->rqt_pool)
  324. return -ENOMEM;
  325. rqt_start = rdev->lldi.vr->rq.start;
  326. rqt_chunk = rdev->lldi.vr->rq.size;
  327. rqt_top = rqt_start + rqt_chunk;
  328. while (rqt_start < rqt_top) {
  329. rqt_chunk = min(rqt_top - rqt_start + 1, rqt_chunk);
  330. if (gen_pool_add(rdev->rqt_pool, rqt_start, rqt_chunk, -1)) {
  331. PDBG("%s failed to add RQT chunk (%x/%x)\n",
  332. __func__, rqt_start, rqt_chunk);
  333. if (rqt_chunk <= 1024 << MIN_RQT_SHIFT) {
  334. printk(KERN_WARNING MOD
  335. "Failed to add all RQT chunks (%x/%x)\n",
  336. rqt_start, rqt_top - rqt_start);
  337. return 0;
  338. }
  339. rqt_chunk >>= 1;
  340. } else {
  341. PDBG("%s added RQT chunk (%x/%x)\n",
  342. __func__, rqt_start, rqt_chunk);
  343. rqt_start += rqt_chunk;
  344. }
  345. }
  346. return 0;
  347. }
  348. void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
  349. {
  350. gen_pool_destroy(rdev->rqt_pool);
  351. }
  352. /*
  353. * On-Chip QP Memory.
  354. */
  355. #define MIN_OCQP_SHIFT 12 /* 4KB == min ocqp size */
  356. u32 c4iw_ocqp_pool_alloc(struct c4iw_rdev *rdev, int size)
  357. {
  358. unsigned long addr = gen_pool_alloc(rdev->ocqp_pool, size);
  359. PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
  360. if (addr) {
  361. mutex_lock(&rdev->stats.lock);
  362. rdev->stats.ocqp.cur += roundup(size, 1 << MIN_OCQP_SHIFT);
  363. if (rdev->stats.ocqp.cur > rdev->stats.ocqp.max)
  364. rdev->stats.ocqp.max = rdev->stats.ocqp.cur;
  365. mutex_unlock(&rdev->stats.lock);
  366. }
  367. return (u32)addr;
  368. }
  369. void c4iw_ocqp_pool_free(struct c4iw_rdev *rdev, u32 addr, int size)
  370. {
  371. PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
  372. mutex_lock(&rdev->stats.lock);
  373. rdev->stats.ocqp.cur -= roundup(size, 1 << MIN_OCQP_SHIFT);
  374. mutex_unlock(&rdev->stats.lock);
  375. gen_pool_free(rdev->ocqp_pool, (unsigned long)addr, size);
  376. }
  377. int c4iw_ocqp_pool_create(struct c4iw_rdev *rdev)
  378. {
  379. unsigned start, chunk, top;
  380. rdev->ocqp_pool = gen_pool_create(MIN_OCQP_SHIFT, -1);
  381. if (!rdev->ocqp_pool)
  382. return -ENOMEM;
  383. start = rdev->lldi.vr->ocq.start;
  384. chunk = rdev->lldi.vr->ocq.size;
  385. top = start + chunk;
  386. while (start < top) {
  387. chunk = min(top - start + 1, chunk);
  388. if (gen_pool_add(rdev->ocqp_pool, start, chunk, -1)) {
  389. PDBG("%s failed to add OCQP chunk (%x/%x)\n",
  390. __func__, start, chunk);
  391. if (chunk <= 1024 << MIN_OCQP_SHIFT) {
  392. printk(KERN_WARNING MOD
  393. "Failed to add all OCQP chunks (%x/%x)\n",
  394. start, top - start);
  395. return 0;
  396. }
  397. chunk >>= 1;
  398. } else {
  399. PDBG("%s added OCQP chunk (%x/%x)\n",
  400. __func__, start, chunk);
  401. start += chunk;
  402. }
  403. }
  404. return 0;
  405. }
  406. void c4iw_ocqp_pool_destroy(struct c4iw_rdev *rdev)
  407. {
  408. gen_pool_destroy(rdev->ocqp_pool);
  409. }