fnic_res.c 11 KB

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