iwch.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2006 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. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <rdma/ib_verbs.h>
  35. #include "cxgb3_offload.h"
  36. #include "iwch_provider.h"
  37. #include <rdma/cxgb3-abi.h>
  38. #include "iwch.h"
  39. #include "iwch_cm.h"
  40. #define DRV_VERSION "1.1"
  41. MODULE_AUTHOR("Boyd Faulkner, Steve Wise");
  42. MODULE_DESCRIPTION("Chelsio T3 RDMA Driver");
  43. MODULE_LICENSE("Dual BSD/GPL");
  44. static void open_rnic_dev(struct t3cdev *);
  45. static void close_rnic_dev(struct t3cdev *);
  46. static void iwch_event_handler(struct t3cdev *, u32, u32);
  47. struct cxgb3_client t3c_client = {
  48. .name = "iw_cxgb3",
  49. .add = open_rnic_dev,
  50. .remove = close_rnic_dev,
  51. .handlers = t3c_handlers,
  52. .redirect = iwch_ep_redirect,
  53. .event_handler = iwch_event_handler
  54. };
  55. static LIST_HEAD(dev_list);
  56. static DEFINE_MUTEX(dev_mutex);
  57. static int disable_qp_db(int id, void *p, void *data)
  58. {
  59. struct iwch_qp *qhp = p;
  60. cxio_disable_wq_db(&qhp->wq);
  61. return 0;
  62. }
  63. static int enable_qp_db(int id, void *p, void *data)
  64. {
  65. struct iwch_qp *qhp = p;
  66. if (data)
  67. ring_doorbell(qhp->rhp->rdev.ctrl_qp.doorbell, qhp->wq.qpid);
  68. cxio_enable_wq_db(&qhp->wq);
  69. return 0;
  70. }
  71. static void disable_dbs(struct iwch_dev *rnicp)
  72. {
  73. spin_lock_irq(&rnicp->lock);
  74. idr_for_each(&rnicp->qpidr, disable_qp_db, NULL);
  75. spin_unlock_irq(&rnicp->lock);
  76. }
  77. static void enable_dbs(struct iwch_dev *rnicp, int ring_db)
  78. {
  79. spin_lock_irq(&rnicp->lock);
  80. idr_for_each(&rnicp->qpidr, enable_qp_db,
  81. (void *)(unsigned long)ring_db);
  82. spin_unlock_irq(&rnicp->lock);
  83. }
  84. static void iwch_db_drop_task(struct work_struct *work)
  85. {
  86. struct iwch_dev *rnicp = container_of(work, struct iwch_dev,
  87. db_drop_task.work);
  88. enable_dbs(rnicp, 1);
  89. }
  90. static void rnic_init(struct iwch_dev *rnicp)
  91. {
  92. pr_debug("%s iwch_dev %p\n", __func__, rnicp);
  93. idr_init(&rnicp->cqidr);
  94. idr_init(&rnicp->qpidr);
  95. idr_init(&rnicp->mmidr);
  96. spin_lock_init(&rnicp->lock);
  97. INIT_DELAYED_WORK(&rnicp->db_drop_task, iwch_db_drop_task);
  98. rnicp->attr.max_qps = T3_MAX_NUM_QP - 32;
  99. rnicp->attr.max_wrs = T3_MAX_QP_DEPTH;
  100. rnicp->attr.max_sge_per_wr = T3_MAX_SGE;
  101. rnicp->attr.max_sge_per_rdma_write_wr = T3_MAX_SGE;
  102. rnicp->attr.max_cqs = T3_MAX_NUM_CQ - 1;
  103. rnicp->attr.max_cqes_per_cq = T3_MAX_CQ_DEPTH;
  104. rnicp->attr.max_mem_regs = cxio_num_stags(&rnicp->rdev);
  105. rnicp->attr.max_phys_buf_entries = T3_MAX_PBL_SIZE;
  106. rnicp->attr.max_pds = T3_MAX_NUM_PD - 1;
  107. rnicp->attr.mem_pgsizes_bitmask = T3_PAGESIZE_MASK;
  108. rnicp->attr.max_mr_size = T3_MAX_MR_SIZE;
  109. rnicp->attr.can_resize_wq = 0;
  110. rnicp->attr.max_rdma_reads_per_qp = 8;
  111. rnicp->attr.max_rdma_read_resources =
  112. rnicp->attr.max_rdma_reads_per_qp * rnicp->attr.max_qps;
  113. rnicp->attr.max_rdma_read_qp_depth = 8; /* IRD */
  114. rnicp->attr.max_rdma_read_depth =
  115. rnicp->attr.max_rdma_read_qp_depth * rnicp->attr.max_qps;
  116. rnicp->attr.rq_overflow_handled = 0;
  117. rnicp->attr.can_modify_ird = 0;
  118. rnicp->attr.can_modify_ord = 0;
  119. rnicp->attr.max_mem_windows = rnicp->attr.max_mem_regs - 1;
  120. rnicp->attr.stag0_value = 1;
  121. rnicp->attr.zbva_support = 1;
  122. rnicp->attr.local_invalidate_fence = 1;
  123. rnicp->attr.cq_overflow_detection = 1;
  124. return;
  125. }
  126. static void open_rnic_dev(struct t3cdev *tdev)
  127. {
  128. struct iwch_dev *rnicp;
  129. pr_debug("%s t3cdev %p\n", __func__, tdev);
  130. pr_info_once("Chelsio T3 RDMA Driver - version %s\n", DRV_VERSION);
  131. rnicp = (struct iwch_dev *)ib_alloc_device(sizeof(*rnicp));
  132. if (!rnicp) {
  133. pr_err("Cannot allocate ib device\n");
  134. return;
  135. }
  136. rnicp->rdev.ulp = rnicp;
  137. rnicp->rdev.t3cdev_p = tdev;
  138. mutex_lock(&dev_mutex);
  139. if (cxio_rdev_open(&rnicp->rdev)) {
  140. mutex_unlock(&dev_mutex);
  141. pr_err("Unable to open CXIO rdev\n");
  142. ib_dealloc_device(&rnicp->ibdev);
  143. return;
  144. }
  145. rnic_init(rnicp);
  146. list_add_tail(&rnicp->entry, &dev_list);
  147. mutex_unlock(&dev_mutex);
  148. if (iwch_register_device(rnicp)) {
  149. pr_err("Unable to register device\n");
  150. close_rnic_dev(tdev);
  151. }
  152. pr_info("Initialized device %s\n",
  153. pci_name(rnicp->rdev.rnic_info.pdev));
  154. return;
  155. }
  156. static void close_rnic_dev(struct t3cdev *tdev)
  157. {
  158. struct iwch_dev *dev, *tmp;
  159. pr_debug("%s t3cdev %p\n", __func__, tdev);
  160. mutex_lock(&dev_mutex);
  161. list_for_each_entry_safe(dev, tmp, &dev_list, entry) {
  162. if (dev->rdev.t3cdev_p == tdev) {
  163. dev->rdev.flags = CXIO_ERROR_FATAL;
  164. synchronize_net();
  165. cancel_delayed_work_sync(&dev->db_drop_task);
  166. list_del(&dev->entry);
  167. iwch_unregister_device(dev);
  168. cxio_rdev_close(&dev->rdev);
  169. idr_destroy(&dev->cqidr);
  170. idr_destroy(&dev->qpidr);
  171. idr_destroy(&dev->mmidr);
  172. ib_dealloc_device(&dev->ibdev);
  173. break;
  174. }
  175. }
  176. mutex_unlock(&dev_mutex);
  177. }
  178. static void iwch_event_handler(struct t3cdev *tdev, u32 evt, u32 port_id)
  179. {
  180. struct cxio_rdev *rdev = tdev->ulp;
  181. struct iwch_dev *rnicp;
  182. struct ib_event event;
  183. u32 portnum = port_id + 1;
  184. int dispatch = 0;
  185. if (!rdev)
  186. return;
  187. rnicp = rdev_to_iwch_dev(rdev);
  188. switch (evt) {
  189. case OFFLOAD_STATUS_DOWN: {
  190. rdev->flags = CXIO_ERROR_FATAL;
  191. synchronize_net();
  192. event.event = IB_EVENT_DEVICE_FATAL;
  193. dispatch = 1;
  194. break;
  195. }
  196. case OFFLOAD_PORT_DOWN: {
  197. event.event = IB_EVENT_PORT_ERR;
  198. dispatch = 1;
  199. break;
  200. }
  201. case OFFLOAD_PORT_UP: {
  202. event.event = IB_EVENT_PORT_ACTIVE;
  203. dispatch = 1;
  204. break;
  205. }
  206. case OFFLOAD_DB_FULL: {
  207. disable_dbs(rnicp);
  208. break;
  209. }
  210. case OFFLOAD_DB_EMPTY: {
  211. enable_dbs(rnicp, 1);
  212. break;
  213. }
  214. case OFFLOAD_DB_DROP: {
  215. unsigned long delay = 1000;
  216. unsigned short r;
  217. disable_dbs(rnicp);
  218. get_random_bytes(&r, 2);
  219. delay += r & 1023;
  220. /*
  221. * delay is between 1000-2023 usecs.
  222. */
  223. schedule_delayed_work(&rnicp->db_drop_task,
  224. usecs_to_jiffies(delay));
  225. break;
  226. }
  227. }
  228. if (dispatch) {
  229. event.device = &rnicp->ibdev;
  230. event.element.port_num = portnum;
  231. ib_dispatch_event(&event);
  232. }
  233. return;
  234. }
  235. static int __init iwch_init_module(void)
  236. {
  237. int err;
  238. err = cxio_hal_init();
  239. if (err)
  240. return err;
  241. err = iwch_cm_init();
  242. if (err)
  243. return err;
  244. cxio_register_ev_cb(iwch_ev_dispatch);
  245. cxgb3_register_client(&t3c_client);
  246. return 0;
  247. }
  248. static void __exit iwch_exit_module(void)
  249. {
  250. cxgb3_unregister_client(&t3c_client);
  251. cxio_unregister_ev_cb(iwch_ev_dispatch);
  252. iwch_cm_term();
  253. cxio_hal_exit();
  254. }
  255. module_init(iwch_init_module);
  256. module_exit(iwch_exit_module);