fnic_res.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  4. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/types.h>
  8. #include <linux/pci.h>
  9. #include "wq_enet_desc.h"
  10. #include "rq_enet_desc.h"
  11. #include "cq_enet_desc.h"
  12. #include "vnic_resource.h"
  13. #include "vnic_dev.h"
  14. #include "vnic_wq.h"
  15. #include "vnic_rq.h"
  16. #include "vnic_cq.h"
  17. #include "vnic_intr.h"
  18. #include "vnic_stats.h"
  19. #include "vnic_nic.h"
  20. #include "fnic.h"
  21. int fnic_get_vnic_config(struct fnic *fnic)
  22. {
  23. struct vnic_fc_config *c = &fnic->config;
  24. int err;
  25. #define GET_CONFIG(m) \
  26. do { \
  27. err = vnic_dev_spec(fnic->vdev, \
  28. offsetof(struct vnic_fc_config, m), \
  29. sizeof(c->m), &c->m); \
  30. if (err) { \
  31. shost_printk(KERN_ERR, fnic->lport->host, \
  32. "Error getting %s, %d\n", #m, \
  33. err); \
  34. return err; \
  35. } \
  36. } while (0);
  37. GET_CONFIG(node_wwn);
  38. GET_CONFIG(port_wwn);
  39. GET_CONFIG(wq_enet_desc_count);
  40. GET_CONFIG(wq_copy_desc_count);
  41. GET_CONFIG(rq_desc_count);
  42. GET_CONFIG(maxdatafieldsize);
  43. GET_CONFIG(ed_tov);
  44. GET_CONFIG(ra_tov);
  45. GET_CONFIG(intr_timer);
  46. GET_CONFIG(intr_timer_type);
  47. GET_CONFIG(flags);
  48. GET_CONFIG(flogi_retries);
  49. GET_CONFIG(flogi_timeout);
  50. GET_CONFIG(plogi_retries);
  51. GET_CONFIG(plogi_timeout);
  52. GET_CONFIG(io_throttle_count);
  53. GET_CONFIG(link_down_timeout);
  54. GET_CONFIG(port_down_timeout);
  55. GET_CONFIG(port_down_io_retries);
  56. GET_CONFIG(luns_per_tgt);
  57. GET_CONFIG(intr_mode);
  58. GET_CONFIG(wq_copy_count);
  59. c->wq_enet_desc_count =
  60. min_t(u32, VNIC_FNIC_WQ_DESCS_MAX,
  61. max_t(u32, VNIC_FNIC_WQ_DESCS_MIN,
  62. c->wq_enet_desc_count));
  63. c->wq_enet_desc_count = ALIGN(c->wq_enet_desc_count, 16);
  64. c->wq_copy_desc_count =
  65. min_t(u32, VNIC_FNIC_WQ_COPY_DESCS_MAX,
  66. max_t(u32, VNIC_FNIC_WQ_COPY_DESCS_MIN,
  67. c->wq_copy_desc_count));
  68. c->wq_copy_desc_count = ALIGN(c->wq_copy_desc_count, 16);
  69. c->rq_desc_count =
  70. min_t(u32, VNIC_FNIC_RQ_DESCS_MAX,
  71. max_t(u32, VNIC_FNIC_RQ_DESCS_MIN,
  72. c->rq_desc_count));
  73. c->rq_desc_count = ALIGN(c->rq_desc_count, 16);
  74. c->maxdatafieldsize =
  75. min_t(u16, VNIC_FNIC_MAXDATAFIELDSIZE_MAX,
  76. max_t(u16, VNIC_FNIC_MAXDATAFIELDSIZE_MIN,
  77. c->maxdatafieldsize));
  78. c->ed_tov =
  79. min_t(u32, VNIC_FNIC_EDTOV_MAX,
  80. max_t(u32, VNIC_FNIC_EDTOV_MIN,
  81. c->ed_tov));
  82. c->ra_tov =
  83. min_t(u32, VNIC_FNIC_RATOV_MAX,
  84. max_t(u32, VNIC_FNIC_RATOV_MIN,
  85. c->ra_tov));
  86. c->flogi_retries =
  87. min_t(u32, VNIC_FNIC_FLOGI_RETRIES_MAX, c->flogi_retries);
  88. c->flogi_timeout =
  89. min_t(u32, VNIC_FNIC_FLOGI_TIMEOUT_MAX,
  90. max_t(u32, VNIC_FNIC_FLOGI_TIMEOUT_MIN,
  91. c->flogi_timeout));
  92. c->plogi_retries =
  93. min_t(u32, VNIC_FNIC_PLOGI_RETRIES_MAX, c->plogi_retries);
  94. c->plogi_timeout =
  95. min_t(u32, VNIC_FNIC_PLOGI_TIMEOUT_MAX,
  96. max_t(u32, VNIC_FNIC_PLOGI_TIMEOUT_MIN,
  97. c->plogi_timeout));
  98. c->io_throttle_count =
  99. min_t(u32, VNIC_FNIC_IO_THROTTLE_COUNT_MAX,
  100. max_t(u32, VNIC_FNIC_IO_THROTTLE_COUNT_MIN,
  101. c->io_throttle_count));
  102. c->link_down_timeout =
  103. min_t(u32, VNIC_FNIC_LINK_DOWN_TIMEOUT_MAX,
  104. c->link_down_timeout);
  105. c->port_down_timeout =
  106. min_t(u32, VNIC_FNIC_PORT_DOWN_TIMEOUT_MAX,
  107. c->port_down_timeout);
  108. c->port_down_io_retries =
  109. min_t(u32, VNIC_FNIC_PORT_DOWN_IO_RETRIES_MAX,
  110. c->port_down_io_retries);
  111. c->luns_per_tgt =
  112. min_t(u32, VNIC_FNIC_LUNS_PER_TARGET_MAX,
  113. max_t(u32, VNIC_FNIC_LUNS_PER_TARGET_MIN,
  114. c->luns_per_tgt));
  115. c->intr_timer = min_t(u16, VNIC_INTR_TIMER_MAX, c->intr_timer);
  116. c->intr_timer_type = c->intr_timer_type;
  117. /* for older firmware, GET_CONFIG will not return anything */
  118. if (c->wq_copy_count == 0)
  119. c->wq_copy_count = 1;
  120. c->wq_copy_count = min_t(u16, FNIC_WQ_COPY_MAX, c->wq_copy_count);
  121. shost_printk(KERN_INFO, fnic->lport->host,
  122. "vNIC MAC addr %pM "
  123. "wq/wq_copy/rq %d/%d/%d\n",
  124. fnic->ctlr.ctl_src_addr,
  125. c->wq_enet_desc_count, c->wq_copy_desc_count,
  126. c->rq_desc_count);
  127. shost_printk(KERN_INFO, fnic->lport->host,
  128. "vNIC node wwn %llx port wwn %llx\n",
  129. c->node_wwn, c->port_wwn);
  130. shost_printk(KERN_INFO, fnic->lport->host,
  131. "vNIC ed_tov %d ra_tov %d\n",
  132. c->ed_tov, c->ra_tov);
  133. shost_printk(KERN_INFO, fnic->lport->host,
  134. "vNIC mtu %d intr timer %d\n",
  135. c->maxdatafieldsize, c->intr_timer);
  136. shost_printk(KERN_INFO, fnic->lport->host,
  137. "vNIC flags 0x%x luns per tgt %d\n",
  138. c->flags, c->luns_per_tgt);
  139. shost_printk(KERN_INFO, fnic->lport->host,
  140. "vNIC flogi_retries %d flogi timeout %d\n",
  141. c->flogi_retries, c->flogi_timeout);
  142. shost_printk(KERN_INFO, fnic->lport->host,
  143. "vNIC plogi retries %d plogi timeout %d\n",
  144. c->plogi_retries, c->plogi_timeout);
  145. shost_printk(KERN_INFO, fnic->lport->host,
  146. "vNIC io throttle count %d link dn timeout %d\n",
  147. c->io_throttle_count, c->link_down_timeout);
  148. shost_printk(KERN_INFO, fnic->lport->host,
  149. "vNIC port dn io retries %d port dn timeout %d\n",
  150. c->port_down_io_retries, c->port_down_timeout);
  151. shost_printk(KERN_INFO, fnic->lport->host,
  152. "vNIC wq_copy_count: %d\n", c->wq_copy_count);
  153. shost_printk(KERN_INFO, fnic->lport->host,
  154. "vNIC intr mode: %d\n", c->intr_mode);
  155. return 0;
  156. }
  157. int fnic_set_nic_config(struct fnic *fnic, u8 rss_default_cpu,
  158. u8 rss_hash_type,
  159. u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable,
  160. u8 tso_ipid_split_en, u8 ig_vlan_strip_en)
  161. {
  162. u64 a0, a1;
  163. u32 nic_cfg;
  164. int wait = 1000;
  165. vnic_set_nic_cfg(&nic_cfg, rss_default_cpu,
  166. rss_hash_type, rss_hash_bits, rss_base_cpu,
  167. rss_enable, tso_ipid_split_en, ig_vlan_strip_en);
  168. a0 = nic_cfg;
  169. a1 = 0;
  170. return vnic_dev_cmd(fnic->vdev, CMD_NIC_CFG, &a0, &a1, wait);
  171. }
  172. void fnic_get_res_counts(struct fnic *fnic)
  173. {
  174. fnic->wq_count = vnic_dev_get_res_count(fnic->vdev, RES_TYPE_WQ);
  175. fnic->raw_wq_count = 1;
  176. fnic->wq_copy_count = fnic->config.wq_copy_count;
  177. fnic->rq_count = vnic_dev_get_res_count(fnic->vdev, RES_TYPE_RQ);
  178. fnic->cq_count = vnic_dev_get_res_count(fnic->vdev, RES_TYPE_CQ);
  179. fnic->intr_count = vnic_dev_get_res_count(fnic->vdev,
  180. RES_TYPE_INTR_CTRL);
  181. shost_printk(KERN_INFO, fnic->lport->host,
  182. "vNIC fw resources wq_count: %d\n", fnic->wq_count);
  183. shost_printk(KERN_INFO, fnic->lport->host,
  184. "vNIC fw resources raw_wq_count: %d\n", fnic->raw_wq_count);
  185. shost_printk(KERN_INFO, fnic->lport->host,
  186. "vNIC fw resources wq_copy_count: %d\n", fnic->wq_copy_count);
  187. shost_printk(KERN_INFO, fnic->lport->host,
  188. "vNIC fw resources rq_count: %d\n", fnic->rq_count);
  189. shost_printk(KERN_INFO, fnic->lport->host,
  190. "vNIC fw resources cq_count: %d\n", fnic->cq_count);
  191. shost_printk(KERN_INFO, fnic->lport->host,
  192. "vNIC fw resources intr_count: %d\n", fnic->intr_count);
  193. }
  194. void fnic_free_vnic_resources(struct fnic *fnic)
  195. {
  196. unsigned int i;
  197. for (i = 0; i < fnic->raw_wq_count; i++)
  198. vnic_wq_free(&fnic->wq[i]);
  199. for (i = 0; i < fnic->wq_copy_count; i++)
  200. vnic_wq_copy_free(&fnic->hw_copy_wq[i]);
  201. for (i = 0; i < fnic->rq_count; i++)
  202. vnic_rq_free(&fnic->rq[i]);
  203. for (i = 0; i < fnic->cq_count; i++)
  204. vnic_cq_free(&fnic->cq[i]);
  205. for (i = 0; i < fnic->intr_count; i++)
  206. vnic_intr_free(&fnic->intr[i]);
  207. }
  208. int fnic_alloc_vnic_resources(struct fnic *fnic)
  209. {
  210. enum vnic_dev_intr_mode intr_mode;
  211. unsigned int mask_on_assertion;
  212. unsigned int interrupt_offset;
  213. unsigned int error_interrupt_enable;
  214. unsigned int error_interrupt_offset;
  215. unsigned int i, cq_index;
  216. unsigned int wq_copy_cq_desc_count;
  217. int err;
  218. intr_mode = vnic_dev_get_intr_mode(fnic->vdev);
  219. shost_printk(KERN_INFO, fnic->lport->host, "vNIC interrupt mode: %s\n",
  220. intr_mode == VNIC_DEV_INTR_MODE_INTX ? "legacy PCI INTx" :
  221. intr_mode == VNIC_DEV_INTR_MODE_MSI ? "MSI" :
  222. intr_mode == VNIC_DEV_INTR_MODE_MSIX ?
  223. "MSI-X" : "unknown");
  224. shost_printk(KERN_INFO, fnic->lport->host,
  225. "vNIC resources avail: wq %d cp_wq %d raw_wq %d rq %d",
  226. fnic->wq_count, fnic->wq_copy_count,
  227. fnic->raw_wq_count, fnic->rq_count);
  228. shost_printk(KERN_INFO, fnic->lport->host,
  229. "vNIC resources avail: cq %d intr %d cpy-wq desc count %d\n",
  230. fnic->cq_count, fnic->intr_count,
  231. fnic->config.wq_copy_desc_count);
  232. /* Allocate Raw WQ used for FCS frames */
  233. for (i = 0; i < fnic->raw_wq_count; i++) {
  234. err = vnic_wq_alloc(fnic->vdev, &fnic->wq[i], i,
  235. fnic->config.wq_enet_desc_count,
  236. sizeof(struct wq_enet_desc));
  237. if (err)
  238. goto err_out_cleanup;
  239. }
  240. /* Allocate Copy WQs used for SCSI IOs */
  241. for (i = 0; i < fnic->wq_copy_count; i++) {
  242. err = vnic_wq_copy_alloc(fnic->vdev, &fnic->hw_copy_wq[i],
  243. (fnic->raw_wq_count + i),
  244. fnic->config.wq_copy_desc_count,
  245. sizeof(struct fcpio_host_req));
  246. if (err)
  247. goto err_out_cleanup;
  248. }
  249. /* RQ for receiving FCS frames */
  250. for (i = 0; i < fnic->rq_count; i++) {
  251. err = vnic_rq_alloc(fnic->vdev, &fnic->rq[i], i,
  252. fnic->config.rq_desc_count,
  253. sizeof(struct rq_enet_desc));
  254. if (err)
  255. goto err_out_cleanup;
  256. }
  257. /* CQ for each RQ */
  258. for (i = 0; i < fnic->rq_count; i++) {
  259. cq_index = i;
  260. err = vnic_cq_alloc(fnic->vdev,
  261. &fnic->cq[cq_index], cq_index,
  262. fnic->config.rq_desc_count,
  263. sizeof(struct cq_enet_rq_desc));
  264. if (err)
  265. goto err_out_cleanup;
  266. }
  267. /* CQ for each WQ */
  268. for (i = 0; i < fnic->raw_wq_count; i++) {
  269. cq_index = fnic->rq_count + i;
  270. err = vnic_cq_alloc(fnic->vdev, &fnic->cq[cq_index], cq_index,
  271. fnic->config.wq_enet_desc_count,
  272. sizeof(struct cq_enet_wq_desc));
  273. if (err)
  274. goto err_out_cleanup;
  275. }
  276. /* CQ for each COPY WQ */
  277. wq_copy_cq_desc_count = (fnic->config.wq_copy_desc_count * 3);
  278. for (i = 0; i < fnic->wq_copy_count; i++) {
  279. cq_index = fnic->raw_wq_count + fnic->rq_count + i;
  280. err = vnic_cq_alloc(fnic->vdev, &fnic->cq[cq_index],
  281. cq_index,
  282. wq_copy_cq_desc_count,
  283. sizeof(struct fcpio_fw_req));
  284. if (err)
  285. goto err_out_cleanup;
  286. }
  287. for (i = 0; i < fnic->intr_count; i++) {
  288. err = vnic_intr_alloc(fnic->vdev, &fnic->intr[i], i);
  289. if (err)
  290. goto err_out_cleanup;
  291. }
  292. fnic->legacy_pba = vnic_dev_get_res(fnic->vdev,
  293. RES_TYPE_INTR_PBA_LEGACY, 0);
  294. if (!fnic->legacy_pba && intr_mode == VNIC_DEV_INTR_MODE_INTX) {
  295. shost_printk(KERN_ERR, fnic->lport->host,
  296. "Failed to hook legacy pba resource\n");
  297. err = -ENODEV;
  298. goto err_out_cleanup;
  299. }
  300. /*
  301. * Init RQ/WQ resources.
  302. *
  303. * RQ[0 to n-1] point to CQ[0 to n-1]
  304. * WQ[0 to m-1] point to CQ[n to n+m-1]
  305. * WQ_COPY[0 to k-1] points to CQ[n+m to n+m+k-1]
  306. *
  307. * Note for copy wq we always initialize with cq_index = 0
  308. *
  309. * Error interrupt is not enabled for MSI.
  310. */
  311. switch (intr_mode) {
  312. case VNIC_DEV_INTR_MODE_INTX:
  313. case VNIC_DEV_INTR_MODE_MSIX:
  314. error_interrupt_enable = 1;
  315. error_interrupt_offset = fnic->err_intr_offset;
  316. break;
  317. default:
  318. error_interrupt_enable = 0;
  319. error_interrupt_offset = 0;
  320. break;
  321. }
  322. for (i = 0; i < fnic->rq_count; i++) {
  323. cq_index = i;
  324. vnic_rq_init(&fnic->rq[i],
  325. cq_index,
  326. error_interrupt_enable,
  327. error_interrupt_offset);
  328. }
  329. for (i = 0; i < fnic->raw_wq_count; i++) {
  330. cq_index = i + fnic->rq_count;
  331. vnic_wq_init(&fnic->wq[i],
  332. cq_index,
  333. error_interrupt_enable,
  334. error_interrupt_offset);
  335. }
  336. for (i = 0; i < fnic->wq_copy_count; i++) {
  337. vnic_wq_copy_init(&fnic->hw_copy_wq[i],
  338. 0 /* cq_index 0 - always */,
  339. error_interrupt_enable,
  340. error_interrupt_offset);
  341. }
  342. for (i = 0; i < fnic->cq_count; i++) {
  343. switch (intr_mode) {
  344. case VNIC_DEV_INTR_MODE_MSIX:
  345. interrupt_offset = i;
  346. break;
  347. default:
  348. interrupt_offset = 0;
  349. break;
  350. }
  351. vnic_cq_init(&fnic->cq[i],
  352. 0 /* flow_control_enable */,
  353. 1 /* color_enable */,
  354. 0 /* cq_head */,
  355. 0 /* cq_tail */,
  356. 1 /* cq_tail_color */,
  357. 1 /* interrupt_enable */,
  358. 1 /* cq_entry_enable */,
  359. 0 /* cq_message_enable */,
  360. interrupt_offset,
  361. 0 /* cq_message_addr */);
  362. }
  363. /*
  364. * Init INTR resources
  365. *
  366. * mask_on_assertion is not used for INTx due to the level-
  367. * triggered nature of INTx
  368. */
  369. switch (intr_mode) {
  370. case VNIC_DEV_INTR_MODE_MSI:
  371. case VNIC_DEV_INTR_MODE_MSIX:
  372. mask_on_assertion = 1;
  373. break;
  374. default:
  375. mask_on_assertion = 0;
  376. break;
  377. }
  378. for (i = 0; i < fnic->intr_count; i++) {
  379. vnic_intr_init(&fnic->intr[i],
  380. fnic->config.intr_timer,
  381. fnic->config.intr_timer_type,
  382. mask_on_assertion);
  383. }
  384. /* init the stats memory by making the first call here */
  385. err = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
  386. if (err) {
  387. shost_printk(KERN_ERR, fnic->lport->host,
  388. "vnic_dev_stats_dump failed - x%x\n", err);
  389. goto err_out_cleanup;
  390. }
  391. /* Clear LIF stats */
  392. vnic_dev_stats_clear(fnic->vdev);
  393. return 0;
  394. err_out_cleanup:
  395. fnic_free_vnic_resources(fnic);
  396. return err;
  397. }