usnic_ib_main.c 20 KB

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