fnic_res.c 12 KB

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