iwch.c 7.5 KB

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