cxio_resource.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
  3. * Copyright (c) 2006 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /* Crude resource management */
  34. #include <linux/kernel.h>
  35. #include <linux/random.h>
  36. #include <linux/slab.h>
  37. #include <linux/kfifo.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/errno.h>
  40. #include "cxio_resource.h"
  41. #include "cxio_hal.h"
  42. static struct kfifo *rhdl_fifo;
  43. static spinlock_t rhdl_fifo_lock;
  44. #define RANDOM_SIZE 16
  45. static int __cxio_init_resource_fifo(struct kfifo **fifo,
  46. spinlock_t *fifo_lock,
  47. u32 nr, u32 skip_low,
  48. u32 skip_high,
  49. int random)
  50. {
  51. u32 i, j, entry = 0, idx;
  52. u32 random_bytes;
  53. u32 rarray[16];
  54. spin_lock_init(fifo_lock);
  55. *fifo = kfifo_alloc(nr * sizeof(u32), GFP_KERNEL, fifo_lock);
  56. if (IS_ERR(*fifo))
  57. return -ENOMEM;
  58. for (i = 0; i < skip_low + skip_high; i++)
  59. __kfifo_put(*fifo, (unsigned char *) &entry, sizeof(u32));
  60. if (random) {
  61. j = 0;
  62. random_bytes = random32();
  63. for (i = 0; i < RANDOM_SIZE; i++)
  64. rarray[i] = i + skip_low;
  65. for (i = skip_low + RANDOM_SIZE; i < nr - skip_high; i++) {
  66. if (j >= RANDOM_SIZE) {
  67. j = 0;
  68. random_bytes = random32();
  69. }
  70. idx = (random_bytes >> (j * 2)) & 0xF;
  71. __kfifo_put(*fifo,
  72. (unsigned char *) &rarray[idx],
  73. sizeof(u32));
  74. rarray[idx] = i;
  75. j++;
  76. }
  77. for (i = 0; i < RANDOM_SIZE; i++)
  78. __kfifo_put(*fifo,
  79. (unsigned char *) &rarray[i],
  80. sizeof(u32));
  81. } else
  82. for (i = skip_low; i < nr - skip_high; i++)
  83. __kfifo_put(*fifo, (unsigned char *) &i, sizeof(u32));
  84. for (i = 0; i < skip_low + skip_high; i++)
  85. kfifo_get(*fifo, (unsigned char *) &entry, sizeof(u32));
  86. return 0;
  87. }
  88. static int cxio_init_resource_fifo(struct kfifo **fifo, spinlock_t * fifo_lock,
  89. u32 nr, u32 skip_low, u32 skip_high)
  90. {
  91. return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
  92. skip_high, 0));
  93. }
  94. static int cxio_init_resource_fifo_random(struct kfifo **fifo,
  95. spinlock_t * fifo_lock,
  96. u32 nr, u32 skip_low, u32 skip_high)
  97. {
  98. return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
  99. skip_high, 1));
  100. }
  101. static int cxio_init_qpid_fifo(struct cxio_rdev *rdev_p)
  102. {
  103. u32 i;
  104. spin_lock_init(&rdev_p->rscp->qpid_fifo_lock);
  105. rdev_p->rscp->qpid_fifo = kfifo_alloc(T3_MAX_NUM_QP * sizeof(u32),
  106. GFP_KERNEL,
  107. &rdev_p->rscp->qpid_fifo_lock);
  108. if (IS_ERR(rdev_p->rscp->qpid_fifo))
  109. return -ENOMEM;
  110. for (i = 16; i < T3_MAX_NUM_QP; i++)
  111. if (!(i & rdev_p->qpmask))
  112. __kfifo_put(rdev_p->rscp->qpid_fifo,
  113. (unsigned char *) &i, sizeof(u32));
  114. return 0;
  115. }
  116. int cxio_hal_init_rhdl_resource(u32 nr_rhdl)
  117. {
  118. return cxio_init_resource_fifo(&rhdl_fifo, &rhdl_fifo_lock, nr_rhdl, 1,
  119. 0);
  120. }
  121. void cxio_hal_destroy_rhdl_resource(void)
  122. {
  123. kfifo_free(rhdl_fifo);
  124. }
  125. /* nr_* must be power of 2 */
  126. int cxio_hal_init_resource(struct cxio_rdev *rdev_p,
  127. u32 nr_tpt, u32 nr_pbl,
  128. u32 nr_rqt, u32 nr_qpid, u32 nr_cqid, u32 nr_pdid)
  129. {
  130. int err = 0;
  131. struct cxio_hal_resource *rscp;
  132. rscp = kmalloc(sizeof(*rscp), GFP_KERNEL);
  133. if (!rscp)
  134. return -ENOMEM;
  135. rdev_p->rscp = rscp;
  136. err = cxio_init_resource_fifo_random(&rscp->tpt_fifo,
  137. &rscp->tpt_fifo_lock,
  138. nr_tpt, 1, 0);
  139. if (err)
  140. goto tpt_err;
  141. err = cxio_init_qpid_fifo(rdev_p);
  142. if (err)
  143. goto qpid_err;
  144. err = cxio_init_resource_fifo(&rscp->cqid_fifo, &rscp->cqid_fifo_lock,
  145. nr_cqid, 1, 0);
  146. if (err)
  147. goto cqid_err;
  148. err = cxio_init_resource_fifo(&rscp->pdid_fifo, &rscp->pdid_fifo_lock,
  149. nr_pdid, 1, 0);
  150. if (err)
  151. goto pdid_err;
  152. return 0;
  153. pdid_err:
  154. kfifo_free(rscp->cqid_fifo);
  155. cqid_err:
  156. kfifo_free(rscp->qpid_fifo);
  157. qpid_err:
  158. kfifo_free(rscp->tpt_fifo);
  159. tpt_err:
  160. return -ENOMEM;
  161. }
  162. /*
  163. * returns 0 if no resource available
  164. */
  165. static inline u32 cxio_hal_get_resource(struct kfifo *fifo)
  166. {
  167. u32 entry;
  168. if (kfifo_get(fifo, (unsigned char *) &entry, sizeof(u32)))
  169. return entry;
  170. else
  171. return 0; /* fifo emptry */
  172. }
  173. static inline void cxio_hal_put_resource(struct kfifo *fifo, u32 entry)
  174. {
  175. BUG_ON(kfifo_put(fifo, (unsigned char *) &entry, sizeof(u32)) == 0);
  176. }
  177. u32 cxio_hal_get_rhdl(void)
  178. {
  179. return cxio_hal_get_resource(rhdl_fifo);
  180. }
  181. void cxio_hal_put_rhdl(u32 rhdl)
  182. {
  183. cxio_hal_put_resource(rhdl_fifo, rhdl);
  184. }
  185. u32 cxio_hal_get_stag(struct cxio_hal_resource *rscp)
  186. {
  187. return cxio_hal_get_resource(rscp->tpt_fifo);
  188. }
  189. void cxio_hal_put_stag(struct cxio_hal_resource *rscp, u32 stag)
  190. {
  191. cxio_hal_put_resource(rscp->tpt_fifo, stag);
  192. }
  193. u32 cxio_hal_get_qpid(struct cxio_hal_resource *rscp)
  194. {
  195. u32 qpid = cxio_hal_get_resource(rscp->qpid_fifo);
  196. PDBG("%s qpid 0x%x\n", __FUNCTION__, qpid);
  197. return qpid;
  198. }
  199. void cxio_hal_put_qpid(struct cxio_hal_resource *rscp, u32 qpid)
  200. {
  201. PDBG("%s qpid 0x%x\n", __FUNCTION__, qpid);
  202. cxio_hal_put_resource(rscp->qpid_fifo, qpid);
  203. }
  204. u32 cxio_hal_get_cqid(struct cxio_hal_resource *rscp)
  205. {
  206. return cxio_hal_get_resource(rscp->cqid_fifo);
  207. }
  208. void cxio_hal_put_cqid(struct cxio_hal_resource *rscp, u32 cqid)
  209. {
  210. cxio_hal_put_resource(rscp->cqid_fifo, cqid);
  211. }
  212. u32 cxio_hal_get_pdid(struct cxio_hal_resource *rscp)
  213. {
  214. return cxio_hal_get_resource(rscp->pdid_fifo);
  215. }
  216. void cxio_hal_put_pdid(struct cxio_hal_resource *rscp, u32 pdid)
  217. {
  218. cxio_hal_put_resource(rscp->pdid_fifo, pdid);
  219. }
  220. void cxio_hal_destroy_resource(struct cxio_hal_resource *rscp)
  221. {
  222. kfifo_free(rscp->tpt_fifo);
  223. kfifo_free(rscp->cqid_fifo);
  224. kfifo_free(rscp->qpid_fifo);
  225. kfifo_free(rscp->pdid_fifo);
  226. kfree(rscp);
  227. }
  228. /*
  229. * PBL Memory Manager. Uses Linux generic allocator.
  230. */
  231. #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
  232. #define PBL_CHUNK 2*1024*1024
  233. u32 cxio_hal_pblpool_alloc(struct cxio_rdev *rdev_p, int size)
  234. {
  235. unsigned long addr = gen_pool_alloc(rdev_p->pbl_pool, size);
  236. PDBG("%s addr 0x%x size %d\n", __FUNCTION__, (u32)addr, size);
  237. return (u32)addr;
  238. }
  239. void cxio_hal_pblpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
  240. {
  241. PDBG("%s addr 0x%x size %d\n", __FUNCTION__, addr, size);
  242. gen_pool_free(rdev_p->pbl_pool, (unsigned long)addr, size);
  243. }
  244. int cxio_hal_pblpool_create(struct cxio_rdev *rdev_p)
  245. {
  246. unsigned long i;
  247. rdev_p->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
  248. if (rdev_p->pbl_pool)
  249. for (i = rdev_p->rnic_info.pbl_base;
  250. i <= rdev_p->rnic_info.pbl_top - PBL_CHUNK + 1;
  251. i += PBL_CHUNK)
  252. gen_pool_add(rdev_p->pbl_pool, i, PBL_CHUNK, -1);
  253. return rdev_p->pbl_pool ? 0 : -ENOMEM;
  254. }
  255. void cxio_hal_pblpool_destroy(struct cxio_rdev *rdev_p)
  256. {
  257. gen_pool_destroy(rdev_p->pbl_pool);
  258. }
  259. /*
  260. * RQT Memory Manager. Uses Linux generic allocator.
  261. */
  262. #define MIN_RQT_SHIFT 10 /* 1KB == mini RQT size (16 entries) */
  263. #define RQT_CHUNK 2*1024*1024
  264. u32 cxio_hal_rqtpool_alloc(struct cxio_rdev *rdev_p, int size)
  265. {
  266. unsigned long addr = gen_pool_alloc(rdev_p->rqt_pool, size << 6);
  267. PDBG("%s addr 0x%x size %d\n", __FUNCTION__, (u32)addr, size << 6);
  268. return (u32)addr;
  269. }
  270. void cxio_hal_rqtpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
  271. {
  272. PDBG("%s addr 0x%x size %d\n", __FUNCTION__, addr, size << 6);
  273. gen_pool_free(rdev_p->rqt_pool, (unsigned long)addr, size << 6);
  274. }
  275. int cxio_hal_rqtpool_create(struct cxio_rdev *rdev_p)
  276. {
  277. unsigned long i;
  278. rdev_p->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
  279. if (rdev_p->rqt_pool)
  280. for (i = rdev_p->rnic_info.rqt_base;
  281. i <= rdev_p->rnic_info.rqt_top - RQT_CHUNK + 1;
  282. i += RQT_CHUNK)
  283. gen_pool_add(rdev_p->rqt_pool, i, RQT_CHUNK, -1);
  284. return rdev_p->rqt_pool ? 0 : -ENOMEM;
  285. }
  286. void cxio_hal_rqtpool_destroy(struct cxio_rdev *rdev_p)
  287. {
  288. gen_pool_destroy(rdev_p->rqt_pool);
  289. }