bnxt_ulp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2016-2018 Broadcom Limited
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pci.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/bitops.h>
  17. #include <linux/irq.h>
  18. #include <asm/byteorder.h>
  19. #include <linux/bitmap.h>
  20. #include "bnxt_hsi.h"
  21. #include "bnxt.h"
  22. #include "bnxt_ulp.h"
  23. static int bnxt_register_dev(struct bnxt_en_dev *edev, int ulp_id,
  24. struct bnxt_ulp_ops *ulp_ops, void *handle)
  25. {
  26. struct net_device *dev = edev->net;
  27. struct bnxt *bp = netdev_priv(dev);
  28. struct bnxt_ulp *ulp;
  29. ASSERT_RTNL();
  30. if (ulp_id >= BNXT_MAX_ULP)
  31. return -EINVAL;
  32. ulp = &edev->ulp_tbl[ulp_id];
  33. if (rcu_access_pointer(ulp->ulp_ops)) {
  34. netdev_err(bp->dev, "ulp id %d already registered\n", ulp_id);
  35. return -EBUSY;
  36. }
  37. if (ulp_id == BNXT_ROCE_ULP) {
  38. unsigned int max_stat_ctxs;
  39. max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
  40. if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS ||
  41. bp->num_stat_ctxs == max_stat_ctxs)
  42. return -ENOMEM;
  43. bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs -
  44. BNXT_MIN_ROCE_STAT_CTXS);
  45. }
  46. atomic_set(&ulp->ref_count, 0);
  47. ulp->handle = handle;
  48. rcu_assign_pointer(ulp->ulp_ops, ulp_ops);
  49. if (ulp_id == BNXT_ROCE_ULP) {
  50. if (test_bit(BNXT_STATE_OPEN, &bp->state))
  51. bnxt_hwrm_vnic_cfg(bp, 0);
  52. }
  53. return 0;
  54. }
  55. static int bnxt_unregister_dev(struct bnxt_en_dev *edev, int ulp_id)
  56. {
  57. struct net_device *dev = edev->net;
  58. struct bnxt *bp = netdev_priv(dev);
  59. struct bnxt_ulp *ulp;
  60. int i = 0;
  61. ASSERT_RTNL();
  62. if (ulp_id >= BNXT_MAX_ULP)
  63. return -EINVAL;
  64. ulp = &edev->ulp_tbl[ulp_id];
  65. if (!rcu_access_pointer(ulp->ulp_ops)) {
  66. netdev_err(bp->dev, "ulp id %d not registered\n", ulp_id);
  67. return -EINVAL;
  68. }
  69. if (ulp_id == BNXT_ROCE_ULP) {
  70. unsigned int max_stat_ctxs;
  71. max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
  72. bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs + 1);
  73. if (ulp->msix_requested)
  74. edev->en_ops->bnxt_free_msix(edev, ulp_id);
  75. }
  76. if (ulp->max_async_event_id)
  77. bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
  78. RCU_INIT_POINTER(ulp->ulp_ops, NULL);
  79. synchronize_rcu();
  80. ulp->max_async_event_id = 0;
  81. ulp->async_events_bmap = NULL;
  82. while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
  83. msleep(100);
  84. i++;
  85. }
  86. return 0;
  87. }
  88. static void bnxt_fill_msix_vecs(struct bnxt *bp, struct bnxt_msix_entry *ent)
  89. {
  90. struct bnxt_en_dev *edev = bp->edev;
  91. int num_msix, idx, i;
  92. num_msix = edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
  93. idx = edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
  94. for (i = 0; i < num_msix; i++) {
  95. ent[i].vector = bp->irq_tbl[idx + i].vector;
  96. ent[i].ring_idx = idx + i;
  97. ent[i].db_offset = (idx + i) * 0x80;
  98. }
  99. }
  100. static int bnxt_req_msix_vecs(struct bnxt_en_dev *edev, int ulp_id,
  101. struct bnxt_msix_entry *ent, int num_msix)
  102. {
  103. struct net_device *dev = edev->net;
  104. struct bnxt *bp = netdev_priv(dev);
  105. int max_idx, max_cp_rings;
  106. int avail_msix, idx;
  107. int rc = 0;
  108. ASSERT_RTNL();
  109. if (ulp_id != BNXT_ROCE_ULP)
  110. return -EINVAL;
  111. if (!(bp->flags & BNXT_FLAG_USING_MSIX))
  112. return -ENODEV;
  113. if (edev->ulp_tbl[ulp_id].msix_requested)
  114. return -EAGAIN;
  115. max_cp_rings = bnxt_get_max_func_cp_rings(bp);
  116. avail_msix = bnxt_get_avail_msix(bp, num_msix);
  117. if (!avail_msix)
  118. return -ENOMEM;
  119. if (avail_msix > num_msix)
  120. avail_msix = num_msix;
  121. if (bp->flags & BNXT_FLAG_NEW_RM) {
  122. idx = bp->cp_nr_rings;
  123. } else {
  124. max_idx = min_t(int, bp->total_irqs, max_cp_rings);
  125. idx = max_idx - avail_msix;
  126. }
  127. edev->ulp_tbl[ulp_id].msix_base = idx;
  128. edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
  129. if (bp->total_irqs < (idx + avail_msix)) {
  130. if (netif_running(dev)) {
  131. bnxt_close_nic(bp, true, false);
  132. rc = bnxt_open_nic(bp, true, false);
  133. } else {
  134. rc = bnxt_reserve_rings(bp);
  135. }
  136. }
  137. if (rc) {
  138. edev->ulp_tbl[ulp_id].msix_requested = 0;
  139. return -EAGAIN;
  140. }
  141. if (bp->flags & BNXT_FLAG_NEW_RM) {
  142. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  143. avail_msix = hw_resc->resv_cp_rings - bp->cp_nr_rings;
  144. edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
  145. }
  146. bnxt_fill_msix_vecs(bp, ent);
  147. bnxt_set_max_func_irqs(bp, bnxt_get_max_func_irqs(bp) - avail_msix);
  148. bnxt_set_max_func_cp_rings(bp, max_cp_rings - avail_msix);
  149. edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED;
  150. return avail_msix;
  151. }
  152. static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id)
  153. {
  154. struct net_device *dev = edev->net;
  155. struct bnxt *bp = netdev_priv(dev);
  156. int max_cp_rings, msix_requested;
  157. ASSERT_RTNL();
  158. if (ulp_id != BNXT_ROCE_ULP)
  159. return -EINVAL;
  160. if (!(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
  161. return 0;
  162. max_cp_rings = bnxt_get_max_func_cp_rings(bp);
  163. msix_requested = edev->ulp_tbl[ulp_id].msix_requested;
  164. bnxt_set_max_func_cp_rings(bp, max_cp_rings + msix_requested);
  165. edev->ulp_tbl[ulp_id].msix_requested = 0;
  166. bnxt_set_max_func_irqs(bp, bnxt_get_max_func_irqs(bp) + msix_requested);
  167. edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED;
  168. if (netif_running(dev)) {
  169. bnxt_close_nic(bp, true, false);
  170. bnxt_open_nic(bp, true, false);
  171. }
  172. return 0;
  173. }
  174. int bnxt_get_ulp_msix_num(struct bnxt *bp)
  175. {
  176. if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
  177. struct bnxt_en_dev *edev = bp->edev;
  178. return edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
  179. }
  180. return 0;
  181. }
  182. int bnxt_get_ulp_msix_base(struct bnxt *bp)
  183. {
  184. if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
  185. struct bnxt_en_dev *edev = bp->edev;
  186. if (edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested)
  187. return edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
  188. }
  189. return 0;
  190. }
  191. void bnxt_subtract_ulp_resources(struct bnxt *bp, int ulp_id)
  192. {
  193. ASSERT_RTNL();
  194. if (bnxt_ulp_registered(bp->edev, ulp_id)) {
  195. struct bnxt_en_dev *edev = bp->edev;
  196. unsigned int msix_req, max;
  197. msix_req = edev->ulp_tbl[ulp_id].msix_requested;
  198. max = bnxt_get_max_func_cp_rings(bp);
  199. bnxt_set_max_func_cp_rings(bp, max - msix_req);
  200. max = bnxt_get_max_func_stat_ctxs(bp);
  201. bnxt_set_max_func_stat_ctxs(bp, max - 1);
  202. }
  203. }
  204. static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id,
  205. struct bnxt_fw_msg *fw_msg)
  206. {
  207. struct net_device *dev = edev->net;
  208. struct bnxt *bp = netdev_priv(dev);
  209. struct input *req;
  210. int rc;
  211. mutex_lock(&bp->hwrm_cmd_lock);
  212. req = fw_msg->msg;
  213. req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
  214. rc = _hwrm_send_message(bp, fw_msg->msg, fw_msg->msg_len,
  215. fw_msg->timeout);
  216. if (!rc) {
  217. struct output *resp = bp->hwrm_cmd_resp_addr;
  218. u32 len = le16_to_cpu(resp->resp_len);
  219. if (fw_msg->resp_max_len < len)
  220. len = fw_msg->resp_max_len;
  221. memcpy(fw_msg->resp, resp, len);
  222. }
  223. mutex_unlock(&bp->hwrm_cmd_lock);
  224. return rc;
  225. }
  226. static void bnxt_ulp_get(struct bnxt_ulp *ulp)
  227. {
  228. atomic_inc(&ulp->ref_count);
  229. }
  230. static void bnxt_ulp_put(struct bnxt_ulp *ulp)
  231. {
  232. atomic_dec(&ulp->ref_count);
  233. }
  234. void bnxt_ulp_stop(struct bnxt *bp)
  235. {
  236. struct bnxt_en_dev *edev = bp->edev;
  237. struct bnxt_ulp_ops *ops;
  238. int i;
  239. if (!edev)
  240. return;
  241. for (i = 0; i < BNXT_MAX_ULP; i++) {
  242. struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
  243. ops = rtnl_dereference(ulp->ulp_ops);
  244. if (!ops || !ops->ulp_stop)
  245. continue;
  246. ops->ulp_stop(ulp->handle);
  247. }
  248. }
  249. void bnxt_ulp_start(struct bnxt *bp)
  250. {
  251. struct bnxt_en_dev *edev = bp->edev;
  252. struct bnxt_ulp_ops *ops;
  253. int i;
  254. if (!edev)
  255. return;
  256. for (i = 0; i < BNXT_MAX_ULP; i++) {
  257. struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
  258. ops = rtnl_dereference(ulp->ulp_ops);
  259. if (!ops || !ops->ulp_start)
  260. continue;
  261. ops->ulp_start(ulp->handle);
  262. }
  263. }
  264. void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs)
  265. {
  266. struct bnxt_en_dev *edev = bp->edev;
  267. struct bnxt_ulp_ops *ops;
  268. int i;
  269. if (!edev)
  270. return;
  271. for (i = 0; i < BNXT_MAX_ULP; i++) {
  272. struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
  273. rcu_read_lock();
  274. ops = rcu_dereference(ulp->ulp_ops);
  275. if (!ops || !ops->ulp_sriov_config) {
  276. rcu_read_unlock();
  277. continue;
  278. }
  279. bnxt_ulp_get(ulp);
  280. rcu_read_unlock();
  281. ops->ulp_sriov_config(ulp->handle, num_vfs);
  282. bnxt_ulp_put(ulp);
  283. }
  284. }
  285. void bnxt_ulp_shutdown(struct bnxt *bp)
  286. {
  287. struct bnxt_en_dev *edev = bp->edev;
  288. struct bnxt_ulp_ops *ops;
  289. int i;
  290. if (!edev)
  291. return;
  292. for (i = 0; i < BNXT_MAX_ULP; i++) {
  293. struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
  294. ops = rtnl_dereference(ulp->ulp_ops);
  295. if (!ops || !ops->ulp_shutdown)
  296. continue;
  297. ops->ulp_shutdown(ulp->handle);
  298. }
  299. }
  300. void bnxt_ulp_irq_stop(struct bnxt *bp)
  301. {
  302. struct bnxt_en_dev *edev = bp->edev;
  303. struct bnxt_ulp_ops *ops;
  304. if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
  305. return;
  306. if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
  307. struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
  308. if (!ulp->msix_requested)
  309. return;
  310. ops = rtnl_dereference(ulp->ulp_ops);
  311. if (!ops || !ops->ulp_irq_stop)
  312. return;
  313. ops->ulp_irq_stop(ulp->handle);
  314. }
  315. }
  316. void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
  317. {
  318. struct bnxt_en_dev *edev = bp->edev;
  319. struct bnxt_ulp_ops *ops;
  320. if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
  321. return;
  322. if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
  323. struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
  324. struct bnxt_msix_entry *ent = NULL;
  325. if (!ulp->msix_requested)
  326. return;
  327. ops = rtnl_dereference(ulp->ulp_ops);
  328. if (!ops || !ops->ulp_irq_restart)
  329. return;
  330. if (!err) {
  331. ent = kcalloc(ulp->msix_requested, sizeof(*ent),
  332. GFP_KERNEL);
  333. if (!ent)
  334. return;
  335. bnxt_fill_msix_vecs(bp, ent);
  336. }
  337. ops->ulp_irq_restart(ulp->handle, ent);
  338. kfree(ent);
  339. }
  340. }
  341. void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
  342. {
  343. u16 event_id = le16_to_cpu(cmpl->event_id);
  344. struct bnxt_en_dev *edev = bp->edev;
  345. struct bnxt_ulp_ops *ops;
  346. int i;
  347. if (!edev)
  348. return;
  349. rcu_read_lock();
  350. for (i = 0; i < BNXT_MAX_ULP; i++) {
  351. struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
  352. ops = rcu_dereference(ulp->ulp_ops);
  353. if (!ops || !ops->ulp_async_notifier)
  354. continue;
  355. if (!ulp->async_events_bmap ||
  356. event_id > ulp->max_async_event_id)
  357. continue;
  358. /* Read max_async_event_id first before testing the bitmap. */
  359. smp_rmb();
  360. if (test_bit(event_id, ulp->async_events_bmap))
  361. ops->ulp_async_notifier(ulp->handle, cmpl);
  362. }
  363. rcu_read_unlock();
  364. }
  365. static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id,
  366. unsigned long *events_bmap, u16 max_id)
  367. {
  368. struct net_device *dev = edev->net;
  369. struct bnxt *bp = netdev_priv(dev);
  370. struct bnxt_ulp *ulp;
  371. if (ulp_id >= BNXT_MAX_ULP)
  372. return -EINVAL;
  373. ulp = &edev->ulp_tbl[ulp_id];
  374. ulp->async_events_bmap = events_bmap;
  375. /* Make sure bnxt_ulp_async_events() sees this order */
  376. smp_wmb();
  377. ulp->max_async_event_id = max_id;
  378. bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1);
  379. return 0;
  380. }
  381. static const struct bnxt_en_ops bnxt_en_ops_tbl = {
  382. .bnxt_register_device = bnxt_register_dev,
  383. .bnxt_unregister_device = bnxt_unregister_dev,
  384. .bnxt_request_msix = bnxt_req_msix_vecs,
  385. .bnxt_free_msix = bnxt_free_msix_vecs,
  386. .bnxt_send_fw_msg = bnxt_send_msg,
  387. .bnxt_register_fw_async_events = bnxt_register_async_events,
  388. };
  389. struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev)
  390. {
  391. struct bnxt *bp = netdev_priv(dev);
  392. struct bnxt_en_dev *edev;
  393. edev = bp->edev;
  394. if (!edev) {
  395. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  396. if (!edev)
  397. return ERR_PTR(-ENOMEM);
  398. edev->en_ops = &bnxt_en_ops_tbl;
  399. if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
  400. edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
  401. if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
  402. edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
  403. edev->net = dev;
  404. edev->pdev = bp->pdev;
  405. bp->edev = edev;
  406. }
  407. return bp->edev;
  408. }