usnic_ib_main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Copyright (c) 2013, Cisco Systems, 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. * 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. * Author: Upinder Malhi <umalhi@cisco.com>
  33. * Author: Anant Deepak <anadeepa@cisco.com>
  34. * Author: Cesare Cantu' <cantuc@cisco.com>
  35. * Author: Jeff Squyres <jsquyres@cisco.com>
  36. * Author: Kiran Thirumalai <kithirum@cisco.com>
  37. * Author: Xuyang Wang <xuywang@cisco.com>
  38. * Author: Reese Faucette <rfaucett@cisco.com>
  39. *
  40. */
  41. #include <linux/module.h>
  42. #include <linux/inetdevice.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <linux/errno.h>
  46. #include <linux/pci.h>
  47. #include <linux/netdevice.h>
  48. #include <rdma/ib_user_verbs.h>
  49. #include <rdma/ib_addr.h>
  50. #include "usnic_abi.h"
  51. #include "usnic_common_util.h"
  52. #include "usnic_ib.h"
  53. #include "usnic_ib_qp_grp.h"
  54. #include "usnic_log.h"
  55. #include "usnic_fwd.h"
  56. #include "usnic_debugfs.h"
  57. #include "usnic_ib_verbs.h"
  58. #include "usnic_transport.h"
  59. #include "usnic_uiom.h"
  60. #include "usnic_ib_sysfs.h"
  61. unsigned int usnic_log_lvl = USNIC_LOG_LVL_ERR;
  62. unsigned int usnic_ib_share_vf = 1;
  63. static const char usnic_version[] =
  64. DRV_NAME ": Cisco VIC (USNIC) Verbs Driver v"
  65. DRV_VERSION " (" DRV_RELDATE ")\n";
  66. static DEFINE_MUTEX(usnic_ib_ibdev_list_lock);
  67. static LIST_HEAD(usnic_ib_ibdev_list);
  68. /* Callback dump funcs */
  69. static int usnic_ib_dump_vf_hdr(void *obj, char *buf, int buf_sz)
  70. {
  71. struct usnic_ib_vf *vf = obj;
  72. return scnprintf(buf, buf_sz, "PF: %s ", vf->pf->ib_dev.name);
  73. }
  74. /* End callback dump funcs */
  75. static void usnic_ib_dump_vf(struct usnic_ib_vf *vf, char *buf, int buf_sz)
  76. {
  77. usnic_vnic_dump(vf->vnic, buf, buf_sz, vf,
  78. usnic_ib_dump_vf_hdr,
  79. usnic_ib_qp_grp_dump_hdr, usnic_ib_qp_grp_dump_rows);
  80. }
  81. void usnic_ib_log_vf(struct usnic_ib_vf *vf)
  82. {
  83. char buf[1000];
  84. usnic_ib_dump_vf(vf, buf, sizeof(buf));
  85. usnic_dbg("%s\n", buf);
  86. }
  87. /* Start of netdev section */
  88. static void usnic_ib_qp_grp_modify_active_to_err(struct usnic_ib_dev *us_ibdev)
  89. {
  90. struct usnic_ib_ucontext *ctx;
  91. struct usnic_ib_qp_grp *qp_grp;
  92. enum ib_qp_state cur_state;
  93. int status;
  94. BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
  95. list_for_each_entry(ctx, &us_ibdev->ctx_list, link) {
  96. list_for_each_entry(qp_grp, &ctx->qp_grp_list, link) {
  97. cur_state = qp_grp->state;
  98. if (cur_state == IB_QPS_INIT ||
  99. cur_state == IB_QPS_RTR ||
  100. cur_state == IB_QPS_RTS) {
  101. status = usnic_ib_qp_grp_modify(qp_grp,
  102. IB_QPS_ERR,
  103. NULL);
  104. if (status) {
  105. usnic_err("Failed to transistion qp grp %u from %s to %s\n",
  106. qp_grp->grp_id,
  107. usnic_ib_qp_grp_state_to_string
  108. (cur_state),
  109. usnic_ib_qp_grp_state_to_string
  110. (IB_QPS_ERR));
  111. }
  112. }
  113. }
  114. }
  115. }
  116. static void usnic_ib_handle_usdev_event(struct usnic_ib_dev *us_ibdev,
  117. unsigned long event)
  118. {
  119. struct net_device *netdev;
  120. struct ib_event ib_event;
  121. memset(&ib_event, 0, sizeof(ib_event));
  122. mutex_lock(&us_ibdev->usdev_lock);
  123. netdev = us_ibdev->netdev;
  124. switch (event) {
  125. case NETDEV_REBOOT:
  126. usnic_info("PF Reset on %s\n", us_ibdev->ib_dev.name);
  127. usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
  128. ib_event.event = IB_EVENT_PORT_ERR;
  129. ib_event.device = &us_ibdev->ib_dev;
  130. ib_event.element.port_num = 1;
  131. ib_dispatch_event(&ib_event);
  132. break;
  133. case NETDEV_UP:
  134. case NETDEV_DOWN:
  135. case NETDEV_CHANGE:
  136. if (!us_ibdev->ufdev->link_up &&
  137. netif_carrier_ok(netdev)) {
  138. usnic_fwd_carrier_up(us_ibdev->ufdev);
  139. usnic_info("Link UP on %s\n", us_ibdev->ib_dev.name);
  140. ib_event.event = IB_EVENT_PORT_ACTIVE;
  141. ib_event.device = &us_ibdev->ib_dev;
  142. ib_event.element.port_num = 1;
  143. ib_dispatch_event(&ib_event);
  144. } else if (us_ibdev->ufdev->link_up &&
  145. !netif_carrier_ok(netdev)) {
  146. usnic_fwd_carrier_down(us_ibdev->ufdev);
  147. usnic_info("Link DOWN on %s\n", us_ibdev->ib_dev.name);
  148. usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
  149. ib_event.event = IB_EVENT_PORT_ERR;
  150. ib_event.device = &us_ibdev->ib_dev;
  151. ib_event.element.port_num = 1;
  152. ib_dispatch_event(&ib_event);
  153. } else {
  154. usnic_dbg("Ignoring %s on %s\n",
  155. netdev_cmd_to_name(event),
  156. us_ibdev->ib_dev.name);
  157. }
  158. break;
  159. case NETDEV_CHANGEADDR:
  160. if (!memcmp(us_ibdev->ufdev->mac, netdev->dev_addr,
  161. sizeof(us_ibdev->ufdev->mac))) {
  162. usnic_dbg("Ignoring addr change on %s\n",
  163. us_ibdev->ib_dev.name);
  164. } else {
  165. usnic_info(" %s old mac: %pM new mac: %pM\n",
  166. us_ibdev->ib_dev.name,
  167. us_ibdev->ufdev->mac,
  168. netdev->dev_addr);
  169. usnic_fwd_set_mac(us_ibdev->ufdev, netdev->dev_addr);
  170. usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
  171. ib_event.event = IB_EVENT_GID_CHANGE;
  172. ib_event.device = &us_ibdev->ib_dev;
  173. ib_event.element.port_num = 1;
  174. ib_dispatch_event(&ib_event);
  175. }
  176. break;
  177. case NETDEV_CHANGEMTU:
  178. if (us_ibdev->ufdev->mtu != netdev->mtu) {
  179. usnic_info("MTU Change on %s old: %u new: %u\n",
  180. us_ibdev->ib_dev.name,
  181. us_ibdev->ufdev->mtu, netdev->mtu);
  182. usnic_fwd_set_mtu(us_ibdev->ufdev, netdev->mtu);
  183. usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
  184. } else {
  185. usnic_dbg("Ignoring MTU change on %s\n",
  186. us_ibdev->ib_dev.name);
  187. }
  188. break;
  189. default:
  190. usnic_dbg("Ignoring event %s on %s",
  191. netdev_cmd_to_name(event),
  192. us_ibdev->ib_dev.name);
  193. }
  194. mutex_unlock(&us_ibdev->usdev_lock);
  195. }
  196. static int usnic_ib_netdevice_event(struct notifier_block *notifier,
  197. unsigned long event, void *ptr)
  198. {
  199. struct usnic_ib_dev *us_ibdev;
  200. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  201. mutex_lock(&usnic_ib_ibdev_list_lock);
  202. list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
  203. if (us_ibdev->netdev == netdev) {
  204. usnic_ib_handle_usdev_event(us_ibdev, event);
  205. break;
  206. }
  207. }
  208. mutex_unlock(&usnic_ib_ibdev_list_lock);
  209. return NOTIFY_DONE;
  210. }
  211. static struct notifier_block usnic_ib_netdevice_notifier = {
  212. .notifier_call = usnic_ib_netdevice_event
  213. };
  214. /* End of netdev section */
  215. /* Start of inet section */
  216. static int usnic_ib_handle_inet_event(struct usnic_ib_dev *us_ibdev,
  217. unsigned long event, void *ptr)
  218. {
  219. struct in_ifaddr *ifa = ptr;
  220. struct ib_event ib_event;
  221. mutex_lock(&us_ibdev->usdev_lock);
  222. switch (event) {
  223. case NETDEV_DOWN:
  224. usnic_info("%s via ip notifiers",
  225. netdev_cmd_to_name(event));
  226. usnic_fwd_del_ipaddr(us_ibdev->ufdev);
  227. usnic_ib_qp_grp_modify_active_to_err(us_ibdev);
  228. ib_event.event = IB_EVENT_GID_CHANGE;
  229. ib_event.device = &us_ibdev->ib_dev;
  230. ib_event.element.port_num = 1;
  231. ib_dispatch_event(&ib_event);
  232. break;
  233. case NETDEV_UP:
  234. usnic_fwd_add_ipaddr(us_ibdev->ufdev, ifa->ifa_address);
  235. usnic_info("%s via ip notifiers: ip %pI4",
  236. netdev_cmd_to_name(event),
  237. &us_ibdev->ufdev->inaddr);
  238. ib_event.event = IB_EVENT_GID_CHANGE;
  239. ib_event.device = &us_ibdev->ib_dev;
  240. ib_event.element.port_num = 1;
  241. ib_dispatch_event(&ib_event);
  242. break;
  243. default:
  244. usnic_info("Ignoring event %s on %s",
  245. netdev_cmd_to_name(event),
  246. us_ibdev->ib_dev.name);
  247. }
  248. mutex_unlock(&us_ibdev->usdev_lock);
  249. return NOTIFY_DONE;
  250. }
  251. static int usnic_ib_inetaddr_event(struct notifier_block *notifier,
  252. unsigned long event, void *ptr)
  253. {
  254. struct usnic_ib_dev *us_ibdev;
  255. struct in_ifaddr *ifa = ptr;
  256. struct net_device *netdev = ifa->ifa_dev->dev;
  257. mutex_lock(&usnic_ib_ibdev_list_lock);
  258. list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
  259. if (us_ibdev->netdev == netdev) {
  260. usnic_ib_handle_inet_event(us_ibdev, event, ptr);
  261. break;
  262. }
  263. }
  264. mutex_unlock(&usnic_ib_ibdev_list_lock);
  265. return NOTIFY_DONE;
  266. }
  267. static struct notifier_block usnic_ib_inetaddr_notifier = {
  268. .notifier_call = usnic_ib_inetaddr_event
  269. };
  270. /* End of inet section*/
  271. static int usnic_port_immutable(struct ib_device *ibdev, u8 port_num,
  272. struct ib_port_immutable *immutable)
  273. {
  274. struct ib_port_attr attr;
  275. int err;
  276. immutable->core_cap_flags = RDMA_CORE_PORT_USNIC;
  277. err = ib_query_port(ibdev, port_num, &attr);
  278. if (err)
  279. return err;
  280. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  281. immutable->gid_tbl_len = attr.gid_tbl_len;
  282. return 0;
  283. }
  284. static void usnic_get_dev_fw_str(struct ib_device *device, char *str)
  285. {
  286. struct usnic_ib_dev *us_ibdev =
  287. container_of(device, struct usnic_ib_dev, ib_dev);
  288. struct ethtool_drvinfo info;
  289. mutex_lock(&us_ibdev->usdev_lock);
  290. us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
  291. mutex_unlock(&us_ibdev->usdev_lock);
  292. snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", info.fw_version);
  293. }
  294. /* Start of PF discovery section */
  295. static void *usnic_ib_device_add(struct pci_dev *dev)
  296. {
  297. struct usnic_ib_dev *us_ibdev;
  298. union ib_gid gid;
  299. struct in_device *ind;
  300. struct net_device *netdev;
  301. usnic_dbg("\n");
  302. netdev = pci_get_drvdata(dev);
  303. us_ibdev = (struct usnic_ib_dev *)ib_alloc_device(sizeof(*us_ibdev));
  304. if (!us_ibdev) {
  305. usnic_err("Device %s context alloc failed\n",
  306. netdev_name(pci_get_drvdata(dev)));
  307. return ERR_PTR(-EFAULT);
  308. }
  309. us_ibdev->ufdev = usnic_fwd_dev_alloc(dev);
  310. if (!us_ibdev->ufdev) {
  311. usnic_err("Failed to alloc ufdev for %s\n", pci_name(dev));
  312. goto err_dealloc;
  313. }
  314. mutex_init(&us_ibdev->usdev_lock);
  315. INIT_LIST_HEAD(&us_ibdev->vf_dev_list);
  316. INIT_LIST_HEAD(&us_ibdev->ctx_list);
  317. us_ibdev->pdev = dev;
  318. us_ibdev->netdev = pci_get_drvdata(dev);
  319. us_ibdev->ib_dev.owner = THIS_MODULE;
  320. us_ibdev->ib_dev.node_type = RDMA_NODE_USNIC_UDP;
  321. us_ibdev->ib_dev.phys_port_cnt = USNIC_IB_PORT_CNT;
  322. us_ibdev->ib_dev.num_comp_vectors = USNIC_IB_NUM_COMP_VECTORS;
  323. us_ibdev->ib_dev.dev.parent = &dev->dev;
  324. us_ibdev->ib_dev.uverbs_abi_ver = USNIC_UVERBS_ABI_VERSION;
  325. strlcpy(us_ibdev->ib_dev.name, "usnic_%d", IB_DEVICE_NAME_MAX);
  326. us_ibdev->ib_dev.uverbs_cmd_mask =
  327. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  328. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  329. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  330. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  331. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  332. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  333. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  334. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  335. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  336. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  337. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  338. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  339. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  340. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  341. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  342. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  343. (1ull << IB_USER_VERBS_CMD_OPEN_QP);
  344. us_ibdev->ib_dev.query_device = usnic_ib_query_device;
  345. us_ibdev->ib_dev.query_port = usnic_ib_query_port;
  346. us_ibdev->ib_dev.query_pkey = usnic_ib_query_pkey;
  347. us_ibdev->ib_dev.query_gid = usnic_ib_query_gid;
  348. us_ibdev->ib_dev.get_netdev = usnic_get_netdev;
  349. us_ibdev->ib_dev.get_link_layer = usnic_ib_port_link_layer;
  350. us_ibdev->ib_dev.alloc_pd = usnic_ib_alloc_pd;
  351. us_ibdev->ib_dev.dealloc_pd = usnic_ib_dealloc_pd;
  352. us_ibdev->ib_dev.create_qp = usnic_ib_create_qp;
  353. us_ibdev->ib_dev.modify_qp = usnic_ib_modify_qp;
  354. us_ibdev->ib_dev.query_qp = usnic_ib_query_qp;
  355. us_ibdev->ib_dev.destroy_qp = usnic_ib_destroy_qp;
  356. us_ibdev->ib_dev.create_cq = usnic_ib_create_cq;
  357. us_ibdev->ib_dev.destroy_cq = usnic_ib_destroy_cq;
  358. us_ibdev->ib_dev.reg_user_mr = usnic_ib_reg_mr;
  359. us_ibdev->ib_dev.dereg_mr = usnic_ib_dereg_mr;
  360. us_ibdev->ib_dev.alloc_ucontext = usnic_ib_alloc_ucontext;
  361. us_ibdev->ib_dev.dealloc_ucontext = usnic_ib_dealloc_ucontext;
  362. us_ibdev->ib_dev.mmap = usnic_ib_mmap;
  363. us_ibdev->ib_dev.create_ah = usnic_ib_create_ah;
  364. us_ibdev->ib_dev.destroy_ah = usnic_ib_destroy_ah;
  365. us_ibdev->ib_dev.post_send = usnic_ib_post_send;
  366. us_ibdev->ib_dev.post_recv = usnic_ib_post_recv;
  367. us_ibdev->ib_dev.poll_cq = usnic_ib_poll_cq;
  368. us_ibdev->ib_dev.req_notify_cq = usnic_ib_req_notify_cq;
  369. us_ibdev->ib_dev.get_dma_mr = usnic_ib_get_dma_mr;
  370. us_ibdev->ib_dev.get_port_immutable = usnic_port_immutable;
  371. us_ibdev->ib_dev.get_dev_fw_str = usnic_get_dev_fw_str;
  372. us_ibdev->ib_dev.driver_id = RDMA_DRIVER_USNIC;
  373. if (ib_register_device(&us_ibdev->ib_dev, NULL))
  374. goto err_fwd_dealloc;
  375. usnic_fwd_set_mtu(us_ibdev->ufdev, us_ibdev->netdev->mtu);
  376. usnic_fwd_set_mac(us_ibdev->ufdev, us_ibdev->netdev->dev_addr);
  377. if (netif_carrier_ok(us_ibdev->netdev))
  378. usnic_fwd_carrier_up(us_ibdev->ufdev);
  379. ind = in_dev_get(netdev);
  380. if (ind->ifa_list)
  381. usnic_fwd_add_ipaddr(us_ibdev->ufdev,
  382. ind->ifa_list->ifa_address);
  383. in_dev_put(ind);
  384. usnic_mac_ip_to_gid(us_ibdev->netdev->perm_addr,
  385. us_ibdev->ufdev->inaddr, &gid.raw[0]);
  386. memcpy(&us_ibdev->ib_dev.node_guid, &gid.global.interface_id,
  387. sizeof(gid.global.interface_id));
  388. kref_init(&us_ibdev->vf_cnt);
  389. usnic_info("Added ibdev: %s netdev: %s with mac %pM Link: %u MTU: %u\n",
  390. us_ibdev->ib_dev.name, netdev_name(us_ibdev->netdev),
  391. us_ibdev->ufdev->mac, us_ibdev->ufdev->link_up,
  392. us_ibdev->ufdev->mtu);
  393. return us_ibdev;
  394. err_fwd_dealloc:
  395. usnic_fwd_dev_free(us_ibdev->ufdev);
  396. err_dealloc:
  397. usnic_err("failed -- deallocing device\n");
  398. ib_dealloc_device(&us_ibdev->ib_dev);
  399. return NULL;
  400. }
  401. static void usnic_ib_device_remove(struct usnic_ib_dev *us_ibdev)
  402. {
  403. usnic_info("Unregistering %s\n", us_ibdev->ib_dev.name);
  404. usnic_ib_sysfs_unregister_usdev(us_ibdev);
  405. usnic_fwd_dev_free(us_ibdev->ufdev);
  406. ib_unregister_device(&us_ibdev->ib_dev);
  407. ib_dealloc_device(&us_ibdev->ib_dev);
  408. }
  409. static void usnic_ib_undiscover_pf(struct kref *kref)
  410. {
  411. struct usnic_ib_dev *us_ibdev, *tmp;
  412. struct pci_dev *dev;
  413. bool found = false;
  414. dev = container_of(kref, struct usnic_ib_dev, vf_cnt)->pdev;
  415. mutex_lock(&usnic_ib_ibdev_list_lock);
  416. list_for_each_entry_safe(us_ibdev, tmp,
  417. &usnic_ib_ibdev_list, ib_dev_link) {
  418. if (us_ibdev->pdev == dev) {
  419. list_del(&us_ibdev->ib_dev_link);
  420. usnic_ib_device_remove(us_ibdev);
  421. found = true;
  422. break;
  423. }
  424. }
  425. WARN(!found, "Failed to remove PF %s\n", pci_name(dev));
  426. mutex_unlock(&usnic_ib_ibdev_list_lock);
  427. }
  428. static struct usnic_ib_dev *usnic_ib_discover_pf(struct usnic_vnic *vnic)
  429. {
  430. struct usnic_ib_dev *us_ibdev;
  431. struct pci_dev *parent_pci, *vf_pci;
  432. int err;
  433. vf_pci = usnic_vnic_get_pdev(vnic);
  434. parent_pci = pci_physfn(vf_pci);
  435. BUG_ON(!parent_pci);
  436. mutex_lock(&usnic_ib_ibdev_list_lock);
  437. list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) {
  438. if (us_ibdev->pdev == parent_pci) {
  439. kref_get(&us_ibdev->vf_cnt);
  440. goto out;
  441. }
  442. }
  443. us_ibdev = usnic_ib_device_add(parent_pci);
  444. if (IS_ERR_OR_NULL(us_ibdev)) {
  445. us_ibdev = us_ibdev ? us_ibdev : ERR_PTR(-EFAULT);
  446. goto out;
  447. }
  448. err = usnic_ib_sysfs_register_usdev(us_ibdev);
  449. if (err) {
  450. usnic_ib_device_remove(us_ibdev);
  451. us_ibdev = ERR_PTR(err);
  452. goto out;
  453. }
  454. list_add(&us_ibdev->ib_dev_link, &usnic_ib_ibdev_list);
  455. out:
  456. mutex_unlock(&usnic_ib_ibdev_list_lock);
  457. return us_ibdev;
  458. }
  459. /* End of PF discovery section */
  460. /* Start of PCI section */
  461. static const struct pci_device_id usnic_ib_pci_ids[] = {
  462. {PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC)},
  463. {0,}
  464. };
  465. static int usnic_ib_pci_probe(struct pci_dev *pdev,
  466. const struct pci_device_id *id)
  467. {
  468. int err;
  469. struct usnic_ib_dev *pf;
  470. struct usnic_ib_vf *vf;
  471. enum usnic_vnic_res_type res_type;
  472. vf = kzalloc(sizeof(*vf), GFP_KERNEL);
  473. if (!vf)
  474. return -ENOMEM;
  475. err = pci_enable_device(pdev);
  476. if (err) {
  477. usnic_err("Failed to enable %s with err %d\n",
  478. pci_name(pdev), err);
  479. goto out_clean_vf;
  480. }
  481. err = pci_request_regions(pdev, DRV_NAME);
  482. if (err) {
  483. usnic_err("Failed to request region for %s with err %d\n",
  484. pci_name(pdev), err);
  485. goto out_disable_device;
  486. }
  487. pci_set_master(pdev);
  488. pci_set_drvdata(pdev, vf);
  489. vf->vnic = usnic_vnic_alloc(pdev);
  490. if (IS_ERR_OR_NULL(vf->vnic)) {
  491. err = vf->vnic ? PTR_ERR(vf->vnic) : -ENOMEM;
  492. usnic_err("Failed to alloc vnic for %s with err %d\n",
  493. pci_name(pdev), err);
  494. goto out_release_regions;
  495. }
  496. pf = usnic_ib_discover_pf(vf->vnic);
  497. if (IS_ERR_OR_NULL(pf)) {
  498. usnic_err("Failed to discover pf of vnic %s with err%ld\n",
  499. pci_name(pdev), PTR_ERR(pf));
  500. err = pf ? PTR_ERR(pf) : -EFAULT;
  501. goto out_clean_vnic;
  502. }
  503. vf->pf = pf;
  504. spin_lock_init(&vf->lock);
  505. mutex_lock(&pf->usdev_lock);
  506. list_add_tail(&vf->link, &pf->vf_dev_list);
  507. /*
  508. * Save max settings (will be same for each VF, easier to re-write than
  509. * to say "if (!set) { set_values(); set=1; }
  510. */
  511. for (res_type = USNIC_VNIC_RES_TYPE_EOL+1;
  512. res_type < USNIC_VNIC_RES_TYPE_MAX;
  513. res_type++) {
  514. pf->vf_res_cnt[res_type] = usnic_vnic_res_cnt(vf->vnic,
  515. res_type);
  516. }
  517. mutex_unlock(&pf->usdev_lock);
  518. usnic_info("Registering usnic VF %s into PF %s\n", pci_name(pdev),
  519. pf->ib_dev.name);
  520. usnic_ib_log_vf(vf);
  521. return 0;
  522. out_clean_vnic:
  523. usnic_vnic_free(vf->vnic);
  524. out_release_regions:
  525. pci_set_drvdata(pdev, NULL);
  526. pci_clear_master(pdev);
  527. pci_release_regions(pdev);
  528. out_disable_device:
  529. pci_disable_device(pdev);
  530. out_clean_vf:
  531. kfree(vf);
  532. return err;
  533. }
  534. static void usnic_ib_pci_remove(struct pci_dev *pdev)
  535. {
  536. struct usnic_ib_vf *vf = pci_get_drvdata(pdev);
  537. struct usnic_ib_dev *pf = vf->pf;
  538. mutex_lock(&pf->usdev_lock);
  539. list_del(&vf->link);
  540. mutex_unlock(&pf->usdev_lock);
  541. kref_put(&pf->vf_cnt, usnic_ib_undiscover_pf);
  542. usnic_vnic_free(vf->vnic);
  543. pci_set_drvdata(pdev, NULL);
  544. pci_clear_master(pdev);
  545. pci_release_regions(pdev);
  546. pci_disable_device(pdev);
  547. kfree(vf);
  548. usnic_info("Removed VF %s\n", pci_name(pdev));
  549. }
  550. /* PCI driver entry points */
  551. static struct pci_driver usnic_ib_pci_driver = {
  552. .name = DRV_NAME,
  553. .id_table = usnic_ib_pci_ids,
  554. .probe = usnic_ib_pci_probe,
  555. .remove = usnic_ib_pci_remove,
  556. };
  557. /* End of PCI section */
  558. /* Start of module section */
  559. static int __init usnic_ib_init(void)
  560. {
  561. int err;
  562. printk_once(KERN_INFO "%s", usnic_version);
  563. err = usnic_uiom_init(DRV_NAME);
  564. if (err) {
  565. usnic_err("Unable to initalize umem with err %d\n", err);
  566. return err;
  567. }
  568. err = pci_register_driver(&usnic_ib_pci_driver);
  569. if (err) {
  570. usnic_err("Unable to register with PCI\n");
  571. goto out_umem_fini;
  572. }
  573. err = register_netdevice_notifier(&usnic_ib_netdevice_notifier);
  574. if (err) {
  575. usnic_err("Failed to register netdev notifier\n");
  576. goto out_pci_unreg;
  577. }
  578. err = register_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
  579. if (err) {
  580. usnic_err("Failed to register inet addr notifier\n");
  581. goto out_unreg_netdev_notifier;
  582. }
  583. err = usnic_transport_init();
  584. if (err) {
  585. usnic_err("Failed to initialize transport\n");
  586. goto out_unreg_inetaddr_notifier;
  587. }
  588. usnic_debugfs_init();
  589. return 0;
  590. out_unreg_inetaddr_notifier:
  591. unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
  592. out_unreg_netdev_notifier:
  593. unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
  594. out_pci_unreg:
  595. pci_unregister_driver(&usnic_ib_pci_driver);
  596. out_umem_fini:
  597. usnic_uiom_fini();
  598. return err;
  599. }
  600. static void __exit usnic_ib_destroy(void)
  601. {
  602. usnic_dbg("\n");
  603. usnic_debugfs_exit();
  604. usnic_transport_fini();
  605. unregister_inetaddr_notifier(&usnic_ib_inetaddr_notifier);
  606. unregister_netdevice_notifier(&usnic_ib_netdevice_notifier);
  607. pci_unregister_driver(&usnic_ib_pci_driver);
  608. usnic_uiom_fini();
  609. }
  610. MODULE_DESCRIPTION("Cisco VIC (usNIC) Verbs Driver");
  611. MODULE_AUTHOR("Upinder Malhi <umalhi@cisco.com>");
  612. MODULE_LICENSE("Dual BSD/GPL");
  613. module_param(usnic_log_lvl, uint, S_IRUGO | S_IWUSR);
  614. module_param(usnic_ib_share_vf, uint, S_IRUGO | S_IWUSR);
  615. MODULE_PARM_DESC(usnic_log_lvl, " Off=0, Err=1, Info=2, Debug=3");
  616. MODULE_PARM_DESC(usnic_ib_share_vf, "Off=0, On=1 VF sharing amongst QPs");
  617. MODULE_DEVICE_TABLE(pci, usnic_ib_pci_ids);
  618. module_init(usnic_ib_init);
  619. module_exit(usnic_ib_destroy);
  620. /* End of module section */