fnic_res.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 %02x:%02x:%02x:%02x:%02x:%02x "
  129. "wq/wq_copy/rq %d/%d/%d\n",
  130. fnic->mac_addr[0], fnic->mac_addr[1], fnic->mac_addr[2],
  131. fnic->mac_addr[3], fnic->mac_addr[4], fnic->mac_addr[5],
  132. c->wq_enet_desc_count, c->wq_copy_desc_count,
  133. c->rq_desc_count);
  134. shost_printk(KERN_INFO, fnic->lport->host,
  135. "vNIC node wwn %llx port wwn %llx\n",
  136. c->node_wwn, c->port_wwn);
  137. shost_printk(KERN_INFO, fnic->lport->host,
  138. "vNIC ed_tov %d ra_tov %d\n",
  139. c->ed_tov, c->ra_tov);
  140. shost_printk(KERN_INFO, fnic->lport->host,
  141. "vNIC mtu %d intr timer %d\n",
  142. c->maxdatafieldsize, c->intr_timer);
  143. shost_printk(KERN_INFO, fnic->lport->host,
  144. "vNIC flags 0x%x luns per tgt %d\n",
  145. c->flags, c->luns_per_tgt);
  146. shost_printk(KERN_INFO, fnic->lport->host,
  147. "vNIC flogi_retries %d flogi timeout %d\n",
  148. c->flogi_retries, c->flogi_timeout);
  149. shost_printk(KERN_INFO, fnic->lport->host,
  150. "vNIC plogi retries %d plogi timeout %d\n",
  151. c->plogi_retries, c->plogi_timeout);
  152. shost_printk(KERN_INFO, fnic->lport->host,
  153. "vNIC io throttle count %d link dn timeout %d\n",
  154. c->io_throttle_count, c->link_down_timeout);
  155. shost_printk(KERN_INFO, fnic->lport->host,
  156. "vNIC port dn io retries %d port dn timeout %d\n",
  157. c->port_down_io_retries, c->port_down_timeout);
  158. return 0;
  159. }
  160. int fnic_set_nic_config(struct fnic *fnic, u8 rss_default_cpu,
  161. u8 rss_hash_type,
  162. u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable,
  163. u8 tso_ipid_split_en, u8 ig_vlan_strip_en)
  164. {
  165. u64 a0, a1;
  166. u32 nic_cfg;
  167. int wait = 1000;
  168. vnic_set_nic_cfg(&nic_cfg, rss_default_cpu,
  169. rss_hash_type, rss_hash_bits, rss_base_cpu,
  170. rss_enable, tso_ipid_split_en, ig_vlan_strip_en);
  171. a0 = nic_cfg;
  172. a1 = 0;
  173. return vnic_dev_cmd(fnic->vdev, CMD_NIC_CFG, &a0, &a1, wait);
  174. }
  175. void fnic_get_res_counts(struct fnic *fnic)
  176. {
  177. fnic->wq_count = vnic_dev_get_res_count(fnic->vdev, RES_TYPE_WQ);
  178. fnic->raw_wq_count = fnic->wq_count - 1;
  179. fnic->wq_copy_count = fnic->wq_count - fnic->raw_wq_count;
  180. fnic->rq_count = vnic_dev_get_res_count(fnic->vdev, RES_TYPE_RQ);
  181. fnic->cq_count = vnic_dev_get_res_count(fnic->vdev, RES_TYPE_CQ);
  182. fnic->intr_count = vnic_dev_get_res_count(fnic->vdev,
  183. RES_TYPE_INTR_CTRL);
  184. }
  185. void fnic_free_vnic_resources(struct fnic *fnic)
  186. {
  187. unsigned int i;
  188. for (i = 0; i < fnic->raw_wq_count; i++)
  189. vnic_wq_free(&fnic->wq[i]);
  190. for (i = 0; i < fnic->wq_copy_count; i++)
  191. vnic_wq_copy_free(&fnic->wq_copy[i]);
  192. for (i = 0; i < fnic->rq_count; i++)
  193. vnic_rq_free(&fnic->rq[i]);
  194. for (i = 0; i < fnic->cq_count; i++)
  195. vnic_cq_free(&fnic->cq[i]);
  196. for (i = 0; i < fnic->intr_count; i++)
  197. vnic_intr_free(&fnic->intr[i]);
  198. }
  199. int fnic_alloc_vnic_resources(struct fnic *fnic)
  200. {
  201. enum vnic_dev_intr_mode intr_mode;
  202. unsigned int mask_on_assertion;
  203. unsigned int interrupt_offset;
  204. unsigned int error_interrupt_enable;
  205. unsigned int error_interrupt_offset;
  206. unsigned int i, cq_index;
  207. unsigned int wq_copy_cq_desc_count;
  208. int err;
  209. intr_mode = vnic_dev_get_intr_mode(fnic->vdev);
  210. shost_printk(KERN_INFO, fnic->lport->host, "vNIC interrupt mode: %s\n",
  211. intr_mode == VNIC_DEV_INTR_MODE_INTX ? "legacy PCI INTx" :
  212. intr_mode == VNIC_DEV_INTR_MODE_MSI ? "MSI" :
  213. intr_mode == VNIC_DEV_INTR_MODE_MSIX ?
  214. "MSI-X" : "unknown");
  215. shost_printk(KERN_INFO, fnic->lport->host, "vNIC resources avail: "
  216. "wq %d cp_wq %d raw_wq %d rq %d cq %d intr %d\n",
  217. fnic->wq_count, fnic->wq_copy_count, fnic->raw_wq_count,
  218. fnic->rq_count, fnic->cq_count, fnic->intr_count);
  219. /* Allocate Raw WQ used for FCS frames */
  220. for (i = 0; i < fnic->raw_wq_count; i++) {
  221. err = vnic_wq_alloc(fnic->vdev, &fnic->wq[i], i,
  222. fnic->config.wq_enet_desc_count,
  223. sizeof(struct wq_enet_desc));
  224. if (err)
  225. goto err_out_cleanup;
  226. }
  227. /* Allocate Copy WQs used for SCSI IOs */
  228. for (i = 0; i < fnic->wq_copy_count; i++) {
  229. err = vnic_wq_copy_alloc(fnic->vdev, &fnic->wq_copy[i],
  230. (fnic->raw_wq_count + i),
  231. fnic->config.wq_copy_desc_count,
  232. sizeof(struct fcpio_host_req));
  233. if (err)
  234. goto err_out_cleanup;
  235. }
  236. /* RQ for receiving FCS frames */
  237. for (i = 0; i < fnic->rq_count; i++) {
  238. err = vnic_rq_alloc(fnic->vdev, &fnic->rq[i], i,
  239. fnic->config.rq_desc_count,
  240. sizeof(struct rq_enet_desc));
  241. if (err)
  242. goto err_out_cleanup;
  243. }
  244. /* CQ for each RQ */
  245. for (i = 0; i < fnic->rq_count; i++) {
  246. cq_index = i;
  247. err = vnic_cq_alloc(fnic->vdev,
  248. &fnic->cq[cq_index], cq_index,
  249. fnic->config.rq_desc_count,
  250. sizeof(struct cq_enet_rq_desc));
  251. if (err)
  252. goto err_out_cleanup;
  253. }
  254. /* CQ for each WQ */
  255. for (i = 0; i < fnic->raw_wq_count; i++) {
  256. cq_index = fnic->rq_count + i;
  257. err = vnic_cq_alloc(fnic->vdev, &fnic->cq[cq_index], cq_index,
  258. fnic->config.wq_enet_desc_count,
  259. sizeof(struct cq_enet_wq_desc));
  260. if (err)
  261. goto err_out_cleanup;
  262. }
  263. /* CQ for each COPY WQ */
  264. wq_copy_cq_desc_count = (fnic->config.wq_copy_desc_count * 3);
  265. for (i = 0; i < fnic->wq_copy_count; i++) {
  266. cq_index = fnic->raw_wq_count + fnic->rq_count + i;
  267. err = vnic_cq_alloc(fnic->vdev, &fnic->cq[cq_index],
  268. cq_index,
  269. wq_copy_cq_desc_count,
  270. sizeof(struct fcpio_fw_req));
  271. if (err)
  272. goto err_out_cleanup;
  273. }
  274. for (i = 0; i < fnic->intr_count; i++) {
  275. err = vnic_intr_alloc(fnic->vdev, &fnic->intr[i], i);
  276. if (err)
  277. goto err_out_cleanup;
  278. }
  279. fnic->legacy_pba = vnic_dev_get_res(fnic->vdev,
  280. RES_TYPE_INTR_PBA_LEGACY, 0);
  281. if (!fnic->legacy_pba && intr_mode == VNIC_DEV_INTR_MODE_INTX) {
  282. shost_printk(KERN_ERR, fnic->lport->host,
  283. "Failed to hook legacy pba resource\n");
  284. err = -ENODEV;
  285. goto err_out_cleanup;
  286. }
  287. /*
  288. * Init RQ/WQ resources.
  289. *
  290. * RQ[0 to n-1] point to CQ[0 to n-1]
  291. * WQ[0 to m-1] point to CQ[n to n+m-1]
  292. * WQ_COPY[0 to k-1] points to CQ[n+m to n+m+k-1]
  293. *
  294. * Note for copy wq we always initialize with cq_index = 0
  295. *
  296. * Error interrupt is not enabled for MSI.
  297. */
  298. switch (intr_mode) {
  299. case VNIC_DEV_INTR_MODE_INTX:
  300. case VNIC_DEV_INTR_MODE_MSIX:
  301. error_interrupt_enable = 1;
  302. error_interrupt_offset = fnic->err_intr_offset;
  303. break;
  304. default:
  305. error_interrupt_enable = 0;
  306. error_interrupt_offset = 0;
  307. break;
  308. }
  309. for (i = 0; i < fnic->rq_count; i++) {
  310. cq_index = i;
  311. vnic_rq_init(&fnic->rq[i],
  312. cq_index,
  313. error_interrupt_enable,
  314. error_interrupt_offset);
  315. }
  316. for (i = 0; i < fnic->raw_wq_count; i++) {
  317. cq_index = i + fnic->rq_count;
  318. vnic_wq_init(&fnic->wq[i],
  319. cq_index,
  320. error_interrupt_enable,
  321. error_interrupt_offset);
  322. }
  323. for (i = 0; i < fnic->wq_copy_count; i++) {
  324. vnic_wq_copy_init(&fnic->wq_copy[i],
  325. 0 /* cq_index 0 - always */,
  326. error_interrupt_enable,
  327. error_interrupt_offset);
  328. }
  329. for (i = 0; i < fnic->cq_count; i++) {
  330. switch (intr_mode) {
  331. case VNIC_DEV_INTR_MODE_MSIX:
  332. interrupt_offset = i;
  333. break;
  334. default:
  335. interrupt_offset = 0;
  336. break;
  337. }
  338. vnic_cq_init(&fnic->cq[i],
  339. 0 /* flow_control_enable */,
  340. 1 /* color_enable */,
  341. 0 /* cq_head */,
  342. 0 /* cq_tail */,
  343. 1 /* cq_tail_color */,
  344. 1 /* interrupt_enable */,
  345. 1 /* cq_entry_enable */,
  346. 0 /* cq_message_enable */,
  347. interrupt_offset,
  348. 0 /* cq_message_addr */);
  349. }
  350. /*
  351. * Init INTR resources
  352. *
  353. * mask_on_assertion is not used for INTx due to the level-
  354. * triggered nature of INTx
  355. */
  356. switch (intr_mode) {
  357. case VNIC_DEV_INTR_MODE_MSI:
  358. case VNIC_DEV_INTR_MODE_MSIX:
  359. mask_on_assertion = 1;
  360. break;
  361. default:
  362. mask_on_assertion = 0;
  363. break;
  364. }
  365. for (i = 0; i < fnic->intr_count; i++) {
  366. vnic_intr_init(&fnic->intr[i],
  367. fnic->config.intr_timer,
  368. fnic->config.intr_timer_type,
  369. mask_on_assertion);
  370. }
  371. /* init the stats memory by making the first call here */
  372. err = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
  373. if (err) {
  374. shost_printk(KERN_ERR, fnic->lport->host,
  375. "vnic_dev_stats_dump failed - x%x\n", err);
  376. goto err_out_cleanup;
  377. }
  378. /* Clear LIF stats */
  379. vnic_dev_stats_clear(fnic->vdev);
  380. return 0;
  381. err_out_cleanup:
  382. fnic_free_vnic_resources(fnic);
  383. return err;
  384. }