ocrdma_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*******************************************************************
  2. * This file is part of the Emulex RoCE Device Driver for *
  3. * RoCE (RDMA over Converged Ethernet) adapters. *
  4. * Copyright (C) 2008-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *
  20. * Contact Information:
  21. * linux-drivers@emulex.com
  22. *
  23. * Emulex
  24. * 3333 Susan Street
  25. * Costa Mesa, CA 92626
  26. *******************************************************************/
  27. #include <linux/module.h>
  28. #include <linux/idr.h>
  29. #include <rdma/ib_verbs.h>
  30. #include <rdma/ib_user_verbs.h>
  31. #include <rdma/ib_addr.h>
  32. #include <linux/netdevice.h>
  33. #include <net/addrconf.h>
  34. #include "ocrdma.h"
  35. #include "ocrdma_verbs.h"
  36. #include "ocrdma_ah.h"
  37. #include "be_roce.h"
  38. #include "ocrdma_hw.h"
  39. #include "ocrdma_abi.h"
  40. MODULE_VERSION(OCRDMA_ROCE_DEV_VERSION);
  41. MODULE_DESCRIPTION("Emulex RoCE HCA Driver");
  42. MODULE_AUTHOR("Emulex Corporation");
  43. MODULE_LICENSE("GPL");
  44. static LIST_HEAD(ocrdma_dev_list);
  45. static DEFINE_SPINLOCK(ocrdma_devlist_lock);
  46. static DEFINE_IDR(ocrdma_dev_id);
  47. static union ib_gid ocrdma_zero_sgid;
  48. void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
  49. {
  50. u8 mac_addr[6];
  51. memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
  52. guid[0] = mac_addr[0] ^ 2;
  53. guid[1] = mac_addr[1];
  54. guid[2] = mac_addr[2];
  55. guid[3] = 0xff;
  56. guid[4] = 0xfe;
  57. guid[5] = mac_addr[3];
  58. guid[6] = mac_addr[4];
  59. guid[7] = mac_addr[5];
  60. }
  61. static bool ocrdma_add_sgid(struct ocrdma_dev *dev, union ib_gid *new_sgid)
  62. {
  63. int i;
  64. unsigned long flags;
  65. memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
  66. spin_lock_irqsave(&dev->sgid_lock, flags);
  67. for (i = 0; i < OCRDMA_MAX_SGID; i++) {
  68. if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid,
  69. sizeof(union ib_gid))) {
  70. /* found free entry */
  71. memcpy(&dev->sgid_tbl[i], new_sgid,
  72. sizeof(union ib_gid));
  73. spin_unlock_irqrestore(&dev->sgid_lock, flags);
  74. return true;
  75. } else if (!memcmp(&dev->sgid_tbl[i], new_sgid,
  76. sizeof(union ib_gid))) {
  77. /* entry already present, no addition is required. */
  78. spin_unlock_irqrestore(&dev->sgid_lock, flags);
  79. return false;
  80. }
  81. }
  82. spin_unlock_irqrestore(&dev->sgid_lock, flags);
  83. return false;
  84. }
  85. static bool ocrdma_del_sgid(struct ocrdma_dev *dev, union ib_gid *sgid)
  86. {
  87. int found = false;
  88. int i;
  89. unsigned long flags;
  90. spin_lock_irqsave(&dev->sgid_lock, flags);
  91. /* first is default sgid, which cannot be deleted. */
  92. for (i = 1; i < OCRDMA_MAX_SGID; i++) {
  93. if (!memcmp(&dev->sgid_tbl[i], sgid, sizeof(union ib_gid))) {
  94. /* found matching entry */
  95. memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid));
  96. found = true;
  97. break;
  98. }
  99. }
  100. spin_unlock_irqrestore(&dev->sgid_lock, flags);
  101. return found;
  102. }
  103. static int ocrdma_addr_event(unsigned long event, struct net_device *netdev,
  104. union ib_gid *gid)
  105. {
  106. struct ib_event gid_event;
  107. struct ocrdma_dev *dev;
  108. bool found = false;
  109. bool updated = false;
  110. bool is_vlan = false;
  111. is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN;
  112. if (is_vlan)
  113. netdev = rdma_vlan_dev_real_dev(netdev);
  114. rcu_read_lock();
  115. list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
  116. if (dev->nic_info.netdev == netdev) {
  117. found = true;
  118. break;
  119. }
  120. }
  121. rcu_read_unlock();
  122. if (!found)
  123. return NOTIFY_DONE;
  124. mutex_lock(&dev->dev_lock);
  125. switch (event) {
  126. case NETDEV_UP:
  127. updated = ocrdma_add_sgid(dev, gid);
  128. break;
  129. case NETDEV_DOWN:
  130. updated = ocrdma_del_sgid(dev, gid);
  131. break;
  132. default:
  133. break;
  134. }
  135. if (updated) {
  136. /* GID table updated, notify the consumers about it */
  137. gid_event.device = &dev->ibdev;
  138. gid_event.element.port_num = 1;
  139. gid_event.event = IB_EVENT_GID_CHANGE;
  140. ib_dispatch_event(&gid_event);
  141. }
  142. mutex_unlock(&dev->dev_lock);
  143. return NOTIFY_OK;
  144. }
  145. static int ocrdma_inetaddr_event(struct notifier_block *notifier,
  146. unsigned long event, void *ptr)
  147. {
  148. struct in_ifaddr *ifa = ptr;
  149. union ib_gid gid;
  150. struct net_device *netdev = ifa->ifa_dev->dev;
  151. ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
  152. return ocrdma_addr_event(event, netdev, &gid);
  153. }
  154. static struct notifier_block ocrdma_inetaddr_notifier = {
  155. .notifier_call = ocrdma_inetaddr_event
  156. };
  157. #if IS_ENABLED(CONFIG_IPV6)
  158. static int ocrdma_inet6addr_event(struct notifier_block *notifier,
  159. unsigned long event, void *ptr)
  160. {
  161. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  162. union ib_gid *gid = (union ib_gid *)&ifa->addr;
  163. struct net_device *netdev = ifa->idev->dev;
  164. return ocrdma_addr_event(event, netdev, gid);
  165. }
  166. static struct notifier_block ocrdma_inet6addr_notifier = {
  167. .notifier_call = ocrdma_inet6addr_event
  168. };
  169. #endif /* IPV6 and VLAN */
  170. static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
  171. u8 port_num)
  172. {
  173. return IB_LINK_LAYER_ETHERNET;
  174. }
  175. static int ocrdma_register_device(struct ocrdma_dev *dev)
  176. {
  177. strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
  178. ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
  179. memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
  180. sizeof(OCRDMA_NODE_DESC));
  181. dev->ibdev.owner = THIS_MODULE;
  182. dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
  183. dev->ibdev.uverbs_cmd_mask =
  184. OCRDMA_UVERBS(GET_CONTEXT) |
  185. OCRDMA_UVERBS(QUERY_DEVICE) |
  186. OCRDMA_UVERBS(QUERY_PORT) |
  187. OCRDMA_UVERBS(ALLOC_PD) |
  188. OCRDMA_UVERBS(DEALLOC_PD) |
  189. OCRDMA_UVERBS(REG_MR) |
  190. OCRDMA_UVERBS(DEREG_MR) |
  191. OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
  192. OCRDMA_UVERBS(CREATE_CQ) |
  193. OCRDMA_UVERBS(RESIZE_CQ) |
  194. OCRDMA_UVERBS(DESTROY_CQ) |
  195. OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
  196. OCRDMA_UVERBS(CREATE_QP) |
  197. OCRDMA_UVERBS(MODIFY_QP) |
  198. OCRDMA_UVERBS(QUERY_QP) |
  199. OCRDMA_UVERBS(DESTROY_QP) |
  200. OCRDMA_UVERBS(POLL_CQ) |
  201. OCRDMA_UVERBS(POST_SEND) |
  202. OCRDMA_UVERBS(POST_RECV);
  203. dev->ibdev.uverbs_cmd_mask |=
  204. OCRDMA_UVERBS(CREATE_AH) |
  205. OCRDMA_UVERBS(MODIFY_AH) |
  206. OCRDMA_UVERBS(QUERY_AH) |
  207. OCRDMA_UVERBS(DESTROY_AH);
  208. dev->ibdev.node_type = RDMA_NODE_IB_CA;
  209. dev->ibdev.phys_port_cnt = 1;
  210. dev->ibdev.num_comp_vectors = 1;
  211. /* mandatory verbs. */
  212. dev->ibdev.query_device = ocrdma_query_device;
  213. dev->ibdev.query_port = ocrdma_query_port;
  214. dev->ibdev.modify_port = ocrdma_modify_port;
  215. dev->ibdev.query_gid = ocrdma_query_gid;
  216. dev->ibdev.get_link_layer = ocrdma_link_layer;
  217. dev->ibdev.alloc_pd = ocrdma_alloc_pd;
  218. dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
  219. dev->ibdev.create_cq = ocrdma_create_cq;
  220. dev->ibdev.destroy_cq = ocrdma_destroy_cq;
  221. dev->ibdev.resize_cq = ocrdma_resize_cq;
  222. dev->ibdev.create_qp = ocrdma_create_qp;
  223. dev->ibdev.modify_qp = ocrdma_modify_qp;
  224. dev->ibdev.query_qp = ocrdma_query_qp;
  225. dev->ibdev.destroy_qp = ocrdma_destroy_qp;
  226. dev->ibdev.query_pkey = ocrdma_query_pkey;
  227. dev->ibdev.create_ah = ocrdma_create_ah;
  228. dev->ibdev.destroy_ah = ocrdma_destroy_ah;
  229. dev->ibdev.query_ah = ocrdma_query_ah;
  230. dev->ibdev.modify_ah = ocrdma_modify_ah;
  231. dev->ibdev.poll_cq = ocrdma_poll_cq;
  232. dev->ibdev.post_send = ocrdma_post_send;
  233. dev->ibdev.post_recv = ocrdma_post_recv;
  234. dev->ibdev.req_notify_cq = ocrdma_arm_cq;
  235. dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
  236. dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
  237. dev->ibdev.dereg_mr = ocrdma_dereg_mr;
  238. dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
  239. dev->ibdev.alloc_fast_reg_mr = ocrdma_alloc_frmr;
  240. dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list;
  241. dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list;
  242. /* mandatory to support user space verbs consumer. */
  243. dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
  244. dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
  245. dev->ibdev.mmap = ocrdma_mmap;
  246. dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
  247. dev->ibdev.process_mad = ocrdma_process_mad;
  248. if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
  249. dev->ibdev.uverbs_cmd_mask |=
  250. OCRDMA_UVERBS(CREATE_SRQ) |
  251. OCRDMA_UVERBS(MODIFY_SRQ) |
  252. OCRDMA_UVERBS(QUERY_SRQ) |
  253. OCRDMA_UVERBS(DESTROY_SRQ) |
  254. OCRDMA_UVERBS(POST_SRQ_RECV);
  255. dev->ibdev.create_srq = ocrdma_create_srq;
  256. dev->ibdev.modify_srq = ocrdma_modify_srq;
  257. dev->ibdev.query_srq = ocrdma_query_srq;
  258. dev->ibdev.destroy_srq = ocrdma_destroy_srq;
  259. dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
  260. }
  261. return ib_register_device(&dev->ibdev, NULL);
  262. }
  263. static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
  264. {
  265. mutex_init(&dev->dev_lock);
  266. dev->sgid_tbl = kzalloc(sizeof(union ib_gid) *
  267. OCRDMA_MAX_SGID, GFP_KERNEL);
  268. if (!dev->sgid_tbl)
  269. goto alloc_err;
  270. spin_lock_init(&dev->sgid_lock);
  271. dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
  272. OCRDMA_MAX_CQ, GFP_KERNEL);
  273. if (!dev->cq_tbl)
  274. goto alloc_err;
  275. if (dev->attr.max_qp) {
  276. dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
  277. OCRDMA_MAX_QP, GFP_KERNEL);
  278. if (!dev->qp_tbl)
  279. goto alloc_err;
  280. }
  281. spin_lock_init(&dev->av_tbl.lock);
  282. spin_lock_init(&dev->flush_q_lock);
  283. return 0;
  284. alloc_err:
  285. pr_err("%s(%d) error.\n", __func__, dev->id);
  286. return -ENOMEM;
  287. }
  288. static void ocrdma_free_resources(struct ocrdma_dev *dev)
  289. {
  290. kfree(dev->qp_tbl);
  291. kfree(dev->cq_tbl);
  292. kfree(dev->sgid_tbl);
  293. }
  294. static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
  295. {
  296. int status = 0;
  297. struct ocrdma_dev *dev;
  298. dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
  299. if (!dev) {
  300. pr_err("Unable to allocate ib device\n");
  301. return NULL;
  302. }
  303. dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
  304. if (!dev->mbx_cmd)
  305. goto idr_err;
  306. memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
  307. dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
  308. if (dev->id < 0)
  309. goto idr_err;
  310. status = ocrdma_init_hw(dev);
  311. if (status)
  312. goto init_err;
  313. status = ocrdma_alloc_resources(dev);
  314. if (status)
  315. goto alloc_err;
  316. status = ocrdma_register_device(dev);
  317. if (status)
  318. goto alloc_err;
  319. spin_lock(&ocrdma_devlist_lock);
  320. list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
  321. spin_unlock(&ocrdma_devlist_lock);
  322. return dev;
  323. alloc_err:
  324. ocrdma_free_resources(dev);
  325. ocrdma_cleanup_hw(dev);
  326. init_err:
  327. idr_remove(&ocrdma_dev_id, dev->id);
  328. idr_err:
  329. kfree(dev->mbx_cmd);
  330. ib_dealloc_device(&dev->ibdev);
  331. pr_err("%s() leaving. ret=%d\n", __func__, status);
  332. return NULL;
  333. }
  334. static void ocrdma_remove_free(struct rcu_head *rcu)
  335. {
  336. struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
  337. idr_remove(&ocrdma_dev_id, dev->id);
  338. kfree(dev->mbx_cmd);
  339. ib_dealloc_device(&dev->ibdev);
  340. }
  341. static void ocrdma_remove(struct ocrdma_dev *dev)
  342. {
  343. /* first unregister with stack to stop all the active traffic
  344. * of the registered clients.
  345. */
  346. ib_unregister_device(&dev->ibdev);
  347. spin_lock(&ocrdma_devlist_lock);
  348. list_del_rcu(&dev->entry);
  349. spin_unlock(&ocrdma_devlist_lock);
  350. ocrdma_free_resources(dev);
  351. ocrdma_cleanup_hw(dev);
  352. call_rcu(&dev->rcu, ocrdma_remove_free);
  353. }
  354. static int ocrdma_open(struct ocrdma_dev *dev)
  355. {
  356. struct ib_event port_event;
  357. port_event.event = IB_EVENT_PORT_ACTIVE;
  358. port_event.element.port_num = 1;
  359. port_event.device = &dev->ibdev;
  360. ib_dispatch_event(&port_event);
  361. return 0;
  362. }
  363. static int ocrdma_close(struct ocrdma_dev *dev)
  364. {
  365. int i;
  366. struct ocrdma_qp *qp, **cur_qp;
  367. struct ib_event err_event;
  368. struct ib_qp_attr attrs;
  369. int attr_mask = IB_QP_STATE;
  370. attrs.qp_state = IB_QPS_ERR;
  371. mutex_lock(&dev->dev_lock);
  372. if (dev->qp_tbl) {
  373. cur_qp = dev->qp_tbl;
  374. for (i = 0; i < OCRDMA_MAX_QP; i++) {
  375. qp = cur_qp[i];
  376. if (qp) {
  377. /* change the QP state to ERROR */
  378. _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
  379. err_event.event = IB_EVENT_QP_FATAL;
  380. err_event.element.qp = &qp->ibqp;
  381. err_event.device = &dev->ibdev;
  382. ib_dispatch_event(&err_event);
  383. }
  384. }
  385. }
  386. mutex_unlock(&dev->dev_lock);
  387. err_event.event = IB_EVENT_PORT_ERR;
  388. err_event.element.port_num = 1;
  389. err_event.device = &dev->ibdev;
  390. ib_dispatch_event(&err_event);
  391. return 0;
  392. }
  393. /* event handling via NIC driver ensures that all the NIC specific
  394. * initialization done before RoCE driver notifies
  395. * event to stack.
  396. */
  397. static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
  398. {
  399. switch (event) {
  400. case BE_DEV_UP:
  401. ocrdma_open(dev);
  402. break;
  403. case BE_DEV_DOWN:
  404. ocrdma_close(dev);
  405. break;
  406. }
  407. }
  408. static struct ocrdma_driver ocrdma_drv = {
  409. .name = "ocrdma_driver",
  410. .add = ocrdma_add,
  411. .remove = ocrdma_remove,
  412. .state_change_handler = ocrdma_event_handler,
  413. };
  414. static void ocrdma_unregister_inet6addr_notifier(void)
  415. {
  416. #if IS_ENABLED(CONFIG_IPV6)
  417. unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
  418. #endif
  419. }
  420. static int __init ocrdma_init_module(void)
  421. {
  422. int status;
  423. status = register_inetaddr_notifier(&ocrdma_inetaddr_notifier);
  424. if (status)
  425. return status;
  426. #if IS_ENABLED(CONFIG_IPV6)
  427. status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
  428. if (status)
  429. return status;
  430. #endif
  431. status = be_roce_register_driver(&ocrdma_drv);
  432. if (status)
  433. ocrdma_unregister_inet6addr_notifier();
  434. return status;
  435. }
  436. static void __exit ocrdma_exit_module(void)
  437. {
  438. be_roce_unregister_driver(&ocrdma_drv);
  439. ocrdma_unregister_inet6addr_notifier();
  440. }
  441. module_init(ocrdma_init_module);
  442. module_exit(ocrdma_exit_module);