ocrdma_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* This file is part of the Emulex RoCE Device Driver for
  2. * RoCE (RDMA over Converged Ethernet) adapters.
  3. * Copyright (C) 2012-2015 Emulex. All rights reserved.
  4. * EMULEX and SLI are trademarks of Emulex.
  5. * www.emulex.com
  6. *
  7. * This software is available to you under a choice of one of two licenses.
  8. * You may choose to be licensed under the terms of the GNU General Public
  9. * License (GPL) Version 2, available from the file COPYING in the main
  10. * directory of this source tree, or the BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the distribution.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  33. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Contact Information:
  36. * linux-drivers@emulex.com
  37. *
  38. * Emulex
  39. * 3333 Susan Street
  40. * Costa Mesa, CA 92626
  41. */
  42. #include <linux/module.h>
  43. #include <linux/idr.h>
  44. #include <rdma/ib_verbs.h>
  45. #include <rdma/ib_user_verbs.h>
  46. #include <rdma/ib_addr.h>
  47. #include <rdma/ib_mad.h>
  48. #include <linux/netdevice.h>
  49. #include <net/addrconf.h>
  50. #include "ocrdma.h"
  51. #include "ocrdma_verbs.h"
  52. #include "ocrdma_ah.h"
  53. #include "be_roce.h"
  54. #include "ocrdma_hw.h"
  55. #include "ocrdma_stats.h"
  56. #include "ocrdma_abi.h"
  57. MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
  58. MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
  59. MODULE_AUTHOR("Emulex Corporation");
  60. MODULE_LICENSE("Dual BSD/GPL");
  61. static LIST_HEAD(ocrdma_dev_list);
  62. static DEFINE_SPINLOCK(ocrdma_devlist_lock);
  63. static DEFINE_IDR(ocrdma_dev_id);
  64. void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
  65. {
  66. u8 mac_addr[6];
  67. memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
  68. guid[0] = mac_addr[0] ^ 2;
  69. guid[1] = mac_addr[1];
  70. guid[2] = mac_addr[2];
  71. guid[3] = 0xff;
  72. guid[4] = 0xfe;
  73. guid[5] = mac_addr[3];
  74. guid[6] = mac_addr[4];
  75. guid[7] = mac_addr[5];
  76. }
  77. static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
  78. u8 port_num)
  79. {
  80. return IB_LINK_LAYER_ETHERNET;
  81. }
  82. static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
  83. struct ib_port_immutable *immutable)
  84. {
  85. struct ib_port_attr attr;
  86. int err;
  87. err = ocrdma_query_port(ibdev, port_num, &attr);
  88. if (err)
  89. return err;
  90. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  91. immutable->gid_tbl_len = attr.gid_tbl_len;
  92. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
  93. immutable->max_mad_size = IB_MGMT_MAD_SIZE;
  94. return 0;
  95. }
  96. static int ocrdma_register_device(struct ocrdma_dev *dev)
  97. {
  98. strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
  99. ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
  100. memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
  101. sizeof(OCRDMA_NODE_DESC));
  102. dev->ibdev.owner = THIS_MODULE;
  103. dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
  104. dev->ibdev.uverbs_cmd_mask =
  105. OCRDMA_UVERBS(GET_CONTEXT) |
  106. OCRDMA_UVERBS(QUERY_DEVICE) |
  107. OCRDMA_UVERBS(QUERY_PORT) |
  108. OCRDMA_UVERBS(ALLOC_PD) |
  109. OCRDMA_UVERBS(DEALLOC_PD) |
  110. OCRDMA_UVERBS(REG_MR) |
  111. OCRDMA_UVERBS(DEREG_MR) |
  112. OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
  113. OCRDMA_UVERBS(CREATE_CQ) |
  114. OCRDMA_UVERBS(RESIZE_CQ) |
  115. OCRDMA_UVERBS(DESTROY_CQ) |
  116. OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
  117. OCRDMA_UVERBS(CREATE_QP) |
  118. OCRDMA_UVERBS(MODIFY_QP) |
  119. OCRDMA_UVERBS(QUERY_QP) |
  120. OCRDMA_UVERBS(DESTROY_QP) |
  121. OCRDMA_UVERBS(POLL_CQ) |
  122. OCRDMA_UVERBS(POST_SEND) |
  123. OCRDMA_UVERBS(POST_RECV);
  124. dev->ibdev.uverbs_cmd_mask |=
  125. OCRDMA_UVERBS(CREATE_AH) |
  126. OCRDMA_UVERBS(MODIFY_AH) |
  127. OCRDMA_UVERBS(QUERY_AH) |
  128. OCRDMA_UVERBS(DESTROY_AH);
  129. dev->ibdev.node_type = RDMA_NODE_IB_CA;
  130. dev->ibdev.phys_port_cnt = 1;
  131. dev->ibdev.num_comp_vectors = dev->eq_cnt;
  132. /* mandatory verbs. */
  133. dev->ibdev.query_device = ocrdma_query_device;
  134. dev->ibdev.query_port = ocrdma_query_port;
  135. dev->ibdev.modify_port = ocrdma_modify_port;
  136. dev->ibdev.query_gid = ocrdma_query_gid;
  137. dev->ibdev.get_netdev = ocrdma_get_netdev;
  138. dev->ibdev.add_gid = ocrdma_add_gid;
  139. dev->ibdev.del_gid = ocrdma_del_gid;
  140. dev->ibdev.get_link_layer = ocrdma_link_layer;
  141. dev->ibdev.alloc_pd = ocrdma_alloc_pd;
  142. dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
  143. dev->ibdev.create_cq = ocrdma_create_cq;
  144. dev->ibdev.destroy_cq = ocrdma_destroy_cq;
  145. dev->ibdev.resize_cq = ocrdma_resize_cq;
  146. dev->ibdev.create_qp = ocrdma_create_qp;
  147. dev->ibdev.modify_qp = ocrdma_modify_qp;
  148. dev->ibdev.query_qp = ocrdma_query_qp;
  149. dev->ibdev.destroy_qp = ocrdma_destroy_qp;
  150. dev->ibdev.query_pkey = ocrdma_query_pkey;
  151. dev->ibdev.create_ah = ocrdma_create_ah;
  152. dev->ibdev.destroy_ah = ocrdma_destroy_ah;
  153. dev->ibdev.query_ah = ocrdma_query_ah;
  154. dev->ibdev.modify_ah = ocrdma_modify_ah;
  155. dev->ibdev.poll_cq = ocrdma_poll_cq;
  156. dev->ibdev.post_send = ocrdma_post_send;
  157. dev->ibdev.post_recv = ocrdma_post_recv;
  158. dev->ibdev.req_notify_cq = ocrdma_arm_cq;
  159. dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
  160. dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
  161. dev->ibdev.dereg_mr = ocrdma_dereg_mr;
  162. dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
  163. dev->ibdev.alloc_mr = ocrdma_alloc_mr;
  164. dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list;
  165. dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list;
  166. /* mandatory to support user space verbs consumer. */
  167. dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
  168. dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
  169. dev->ibdev.mmap = ocrdma_mmap;
  170. dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
  171. dev->ibdev.process_mad = ocrdma_process_mad;
  172. dev->ibdev.get_port_immutable = ocrdma_port_immutable;
  173. if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
  174. dev->ibdev.uverbs_cmd_mask |=
  175. OCRDMA_UVERBS(CREATE_SRQ) |
  176. OCRDMA_UVERBS(MODIFY_SRQ) |
  177. OCRDMA_UVERBS(QUERY_SRQ) |
  178. OCRDMA_UVERBS(DESTROY_SRQ) |
  179. OCRDMA_UVERBS(POST_SRQ_RECV);
  180. dev->ibdev.create_srq = ocrdma_create_srq;
  181. dev->ibdev.modify_srq = ocrdma_modify_srq;
  182. dev->ibdev.query_srq = ocrdma_query_srq;
  183. dev->ibdev.destroy_srq = ocrdma_destroy_srq;
  184. dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
  185. }
  186. return ib_register_device(&dev->ibdev, NULL);
  187. }
  188. static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
  189. {
  190. mutex_init(&dev->dev_lock);
  191. dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
  192. OCRDMA_MAX_CQ, GFP_KERNEL);
  193. if (!dev->cq_tbl)
  194. goto alloc_err;
  195. if (dev->attr.max_qp) {
  196. dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
  197. OCRDMA_MAX_QP, GFP_KERNEL);
  198. if (!dev->qp_tbl)
  199. goto alloc_err;
  200. }
  201. dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
  202. if (dev->stag_arr == NULL)
  203. goto alloc_err;
  204. ocrdma_alloc_pd_pool(dev);
  205. spin_lock_init(&dev->av_tbl.lock);
  206. spin_lock_init(&dev->flush_q_lock);
  207. return 0;
  208. alloc_err:
  209. pr_err("%s(%d) error.\n", __func__, dev->id);
  210. return -ENOMEM;
  211. }
  212. static void ocrdma_free_resources(struct ocrdma_dev *dev)
  213. {
  214. kfree(dev->stag_arr);
  215. kfree(dev->qp_tbl);
  216. kfree(dev->cq_tbl);
  217. }
  218. /* OCRDMA sysfs interface */
  219. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  220. char *buf)
  221. {
  222. struct ocrdma_dev *dev = dev_get_drvdata(device);
  223. return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
  224. }
  225. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  226. char *buf)
  227. {
  228. struct ocrdma_dev *dev = dev_get_drvdata(device);
  229. return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
  230. }
  231. static ssize_t show_hca_type(struct device *device,
  232. struct device_attribute *attr, char *buf)
  233. {
  234. struct ocrdma_dev *dev = dev_get_drvdata(device);
  235. return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
  236. }
  237. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  238. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  239. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
  240. static struct device_attribute *ocrdma_attributes[] = {
  241. &dev_attr_hw_rev,
  242. &dev_attr_fw_ver,
  243. &dev_attr_hca_type
  244. };
  245. static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
  246. {
  247. int i;
  248. for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
  249. device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
  250. }
  251. static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
  252. {
  253. int status = 0, i;
  254. struct ocrdma_dev *dev;
  255. dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
  256. if (!dev) {
  257. pr_err("Unable to allocate ib device\n");
  258. return NULL;
  259. }
  260. dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
  261. if (!dev->mbx_cmd)
  262. goto idr_err;
  263. memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
  264. dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
  265. if (dev->id < 0)
  266. goto idr_err;
  267. status = ocrdma_init_hw(dev);
  268. if (status)
  269. goto init_err;
  270. status = ocrdma_alloc_resources(dev);
  271. if (status)
  272. goto alloc_err;
  273. ocrdma_init_service_level(dev);
  274. status = ocrdma_register_device(dev);
  275. if (status)
  276. goto alloc_err;
  277. for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
  278. if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
  279. goto sysfs_err;
  280. spin_lock(&ocrdma_devlist_lock);
  281. list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
  282. spin_unlock(&ocrdma_devlist_lock);
  283. /* Init stats */
  284. ocrdma_add_port_stats(dev);
  285. /* Interrupt Moderation */
  286. INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
  287. schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
  288. pr_info("%s %s: %s \"%s\" port %d\n",
  289. dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
  290. port_speed_string(dev), dev->model_number,
  291. dev->hba_port_num);
  292. pr_info("%s ocrdma%d driver loaded successfully\n",
  293. dev_name(&dev->nic_info.pdev->dev), dev->id);
  294. return dev;
  295. sysfs_err:
  296. ocrdma_remove_sysfiles(dev);
  297. alloc_err:
  298. ocrdma_free_resources(dev);
  299. ocrdma_cleanup_hw(dev);
  300. init_err:
  301. idr_remove(&ocrdma_dev_id, dev->id);
  302. idr_err:
  303. kfree(dev->mbx_cmd);
  304. ib_dealloc_device(&dev->ibdev);
  305. pr_err("%s() leaving. ret=%d\n", __func__, status);
  306. return NULL;
  307. }
  308. static void ocrdma_remove_free(struct rcu_head *rcu)
  309. {
  310. struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
  311. idr_remove(&ocrdma_dev_id, dev->id);
  312. kfree(dev->mbx_cmd);
  313. ib_dealloc_device(&dev->ibdev);
  314. }
  315. static void ocrdma_remove(struct ocrdma_dev *dev)
  316. {
  317. /* first unregister with stack to stop all the active traffic
  318. * of the registered clients.
  319. */
  320. cancel_delayed_work_sync(&dev->eqd_work);
  321. ocrdma_remove_sysfiles(dev);
  322. ib_unregister_device(&dev->ibdev);
  323. ocrdma_rem_port_stats(dev);
  324. spin_lock(&ocrdma_devlist_lock);
  325. list_del_rcu(&dev->entry);
  326. spin_unlock(&ocrdma_devlist_lock);
  327. ocrdma_free_resources(dev);
  328. ocrdma_cleanup_hw(dev);
  329. call_rcu(&dev->rcu, ocrdma_remove_free);
  330. }
  331. static int ocrdma_open(struct ocrdma_dev *dev)
  332. {
  333. struct ib_event port_event;
  334. port_event.event = IB_EVENT_PORT_ACTIVE;
  335. port_event.element.port_num = 1;
  336. port_event.device = &dev->ibdev;
  337. ib_dispatch_event(&port_event);
  338. return 0;
  339. }
  340. static int ocrdma_close(struct ocrdma_dev *dev)
  341. {
  342. int i;
  343. struct ocrdma_qp *qp, **cur_qp;
  344. struct ib_event err_event;
  345. struct ib_qp_attr attrs;
  346. int attr_mask = IB_QP_STATE;
  347. attrs.qp_state = IB_QPS_ERR;
  348. mutex_lock(&dev->dev_lock);
  349. if (dev->qp_tbl) {
  350. cur_qp = dev->qp_tbl;
  351. for (i = 0; i < OCRDMA_MAX_QP; i++) {
  352. qp = cur_qp[i];
  353. if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
  354. /* change the QP state to ERROR */
  355. _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
  356. err_event.event = IB_EVENT_QP_FATAL;
  357. err_event.element.qp = &qp->ibqp;
  358. err_event.device = &dev->ibdev;
  359. ib_dispatch_event(&err_event);
  360. }
  361. }
  362. }
  363. mutex_unlock(&dev->dev_lock);
  364. err_event.event = IB_EVENT_PORT_ERR;
  365. err_event.element.port_num = 1;
  366. err_event.device = &dev->ibdev;
  367. ib_dispatch_event(&err_event);
  368. return 0;
  369. }
  370. static void ocrdma_shutdown(struct ocrdma_dev *dev)
  371. {
  372. ocrdma_close(dev);
  373. ocrdma_remove(dev);
  374. }
  375. /* event handling via NIC driver ensures that all the NIC specific
  376. * initialization done before RoCE driver notifies
  377. * event to stack.
  378. */
  379. static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
  380. {
  381. switch (event) {
  382. case BE_DEV_UP:
  383. ocrdma_open(dev);
  384. break;
  385. case BE_DEV_DOWN:
  386. ocrdma_close(dev);
  387. break;
  388. case BE_DEV_SHUTDOWN:
  389. ocrdma_shutdown(dev);
  390. break;
  391. }
  392. }
  393. static struct ocrdma_driver ocrdma_drv = {
  394. .name = "ocrdma_driver",
  395. .add = ocrdma_add,
  396. .remove = ocrdma_remove,
  397. .state_change_handler = ocrdma_event_handler,
  398. .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION,
  399. };
  400. static int __init ocrdma_init_module(void)
  401. {
  402. int status;
  403. ocrdma_init_debugfs();
  404. status = be_roce_register_driver(&ocrdma_drv);
  405. if (status)
  406. goto err_be_reg;
  407. return 0;
  408. err_be_reg:
  409. return status;
  410. }
  411. static void __exit ocrdma_exit_module(void)
  412. {
  413. be_roce_unregister_driver(&ocrdma_drv);
  414. ocrdma_rem_debugfs();
  415. idr_destroy(&ocrdma_dev_id);
  416. }
  417. module_init(ocrdma_init_module);
  418. module_exit(ocrdma_exit_module);