snic_res.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. #include <linux/errno.h>
  4. #include <linux/types.h>
  5. #include <linux/pci.h>
  6. #include "wq_enet_desc.h"
  7. #include "cq_enet_desc.h"
  8. #include "vnic_resource.h"
  9. #include "vnic_dev.h"
  10. #include "vnic_wq.h"
  11. #include "vnic_cq.h"
  12. #include "vnic_intr.h"
  13. #include "vnic_stats.h"
  14. #include "snic.h"
  15. int
  16. snic_get_vnic_config(struct snic *snic)
  17. {
  18. struct vnic_snic_config *c = &snic->config;
  19. int ret;
  20. #define GET_CONFIG(m) \
  21. do { \
  22. ret = svnic_dev_spec(snic->vdev, \
  23. offsetof(struct vnic_snic_config, m), \
  24. sizeof(c->m), \
  25. &c->m); \
  26. if (ret) { \
  27. SNIC_HOST_ERR(snic->shost, \
  28. "Error getting %s, %d\n", #m, ret); \
  29. return ret; \
  30. } \
  31. } while (0)
  32. GET_CONFIG(wq_enet_desc_count);
  33. GET_CONFIG(maxdatafieldsize);
  34. GET_CONFIG(intr_timer);
  35. GET_CONFIG(intr_timer_type);
  36. GET_CONFIG(flags);
  37. GET_CONFIG(io_throttle_count);
  38. GET_CONFIG(port_down_timeout);
  39. GET_CONFIG(port_down_io_retries);
  40. GET_CONFIG(luns_per_tgt);
  41. GET_CONFIG(xpt_type);
  42. GET_CONFIG(hid);
  43. c->wq_enet_desc_count = min_t(u32,
  44. VNIC_SNIC_WQ_DESCS_MAX,
  45. max_t(u32,
  46. VNIC_SNIC_WQ_DESCS_MIN,
  47. c->wq_enet_desc_count));
  48. c->wq_enet_desc_count = ALIGN(c->wq_enet_desc_count, 16);
  49. c->maxdatafieldsize = min_t(u32,
  50. VNIC_SNIC_MAXDATAFIELDSIZE_MAX,
  51. max_t(u32,
  52. VNIC_SNIC_MAXDATAFIELDSIZE_MIN,
  53. c->maxdatafieldsize));
  54. c->io_throttle_count = min_t(u32,
  55. VNIC_SNIC_IO_THROTTLE_COUNT_MAX,
  56. max_t(u32,
  57. VNIC_SNIC_IO_THROTTLE_COUNT_MIN,
  58. c->io_throttle_count));
  59. c->port_down_timeout = min_t(u32,
  60. VNIC_SNIC_PORT_DOWN_TIMEOUT_MAX,
  61. c->port_down_timeout);
  62. c->port_down_io_retries = min_t(u32,
  63. VNIC_SNIC_PORT_DOWN_IO_RETRIES_MAX,
  64. c->port_down_io_retries);
  65. c->luns_per_tgt = min_t(u32,
  66. VNIC_SNIC_LUNS_PER_TARGET_MAX,
  67. max_t(u32,
  68. VNIC_SNIC_LUNS_PER_TARGET_MIN,
  69. c->luns_per_tgt));
  70. c->intr_timer = min_t(u32, VNIC_INTR_TIMER_MAX, c->intr_timer);
  71. SNIC_INFO("vNIC resources wq %d\n", c->wq_enet_desc_count);
  72. SNIC_INFO("vNIC mtu %d intr timer %d\n",
  73. c->maxdatafieldsize,
  74. c->intr_timer);
  75. SNIC_INFO("vNIC flags 0x%x luns per tgt %d\n",
  76. c->flags,
  77. c->luns_per_tgt);
  78. SNIC_INFO("vNIC io throttle count %d\n", c->io_throttle_count);
  79. SNIC_INFO("vNIC port down timeout %d port down io retries %d\n",
  80. c->port_down_timeout,
  81. c->port_down_io_retries);
  82. SNIC_INFO("vNIC back end type = %d\n", c->xpt_type);
  83. SNIC_INFO("vNIC hid = %d\n", c->hid);
  84. return 0;
  85. }
  86. void
  87. snic_get_res_counts(struct snic *snic)
  88. {
  89. snic->wq_count = svnic_dev_get_res_count(snic->vdev, RES_TYPE_WQ);
  90. SNIC_BUG_ON(snic->wq_count == 0);
  91. snic->cq_count = svnic_dev_get_res_count(snic->vdev, RES_TYPE_CQ);
  92. SNIC_BUG_ON(snic->cq_count == 0);
  93. snic->intr_count = svnic_dev_get_res_count(snic->vdev,
  94. RES_TYPE_INTR_CTRL);
  95. SNIC_BUG_ON(snic->intr_count == 0);
  96. }
  97. void
  98. snic_free_vnic_res(struct snic *snic)
  99. {
  100. unsigned int i;
  101. for (i = 0; i < snic->wq_count; i++)
  102. svnic_wq_free(&snic->wq[i]);
  103. for (i = 0; i < snic->cq_count; i++)
  104. svnic_cq_free(&snic->cq[i]);
  105. for (i = 0; i < snic->intr_count; i++)
  106. svnic_intr_free(&snic->intr[i]);
  107. }
  108. int
  109. snic_alloc_vnic_res(struct snic *snic)
  110. {
  111. enum vnic_dev_intr_mode intr_mode;
  112. unsigned int mask_on_assertion;
  113. unsigned int intr_offset;
  114. unsigned int err_intr_enable;
  115. unsigned int err_intr_offset;
  116. unsigned int i;
  117. int ret;
  118. intr_mode = svnic_dev_get_intr_mode(snic->vdev);
  119. SNIC_INFO("vNIC interrupt mode: %s\n",
  120. ((intr_mode == VNIC_DEV_INTR_MODE_INTX) ?
  121. "Legacy PCI INTx" :
  122. ((intr_mode == VNIC_DEV_INTR_MODE_MSI) ?
  123. "MSI" :
  124. ((intr_mode == VNIC_DEV_INTR_MODE_MSIX) ?
  125. "MSI-X" : "Unknown"))));
  126. /* only MSI-X is supported */
  127. SNIC_BUG_ON(intr_mode != VNIC_DEV_INTR_MODE_MSIX);
  128. SNIC_INFO("wq %d cq %d intr %d\n", snic->wq_count,
  129. snic->cq_count,
  130. snic->intr_count);
  131. /* Allocate WQs used for SCSI IOs */
  132. for (i = 0; i < snic->wq_count; i++) {
  133. ret = svnic_wq_alloc(snic->vdev,
  134. &snic->wq[i],
  135. i,
  136. snic->config.wq_enet_desc_count,
  137. sizeof(struct wq_enet_desc));
  138. if (ret)
  139. goto error_cleanup;
  140. }
  141. /* CQ for each WQ */
  142. for (i = 0; i < snic->wq_count; i++) {
  143. ret = svnic_cq_alloc(snic->vdev,
  144. &snic->cq[i],
  145. i,
  146. snic->config.wq_enet_desc_count,
  147. sizeof(struct cq_enet_wq_desc));
  148. if (ret)
  149. goto error_cleanup;
  150. }
  151. SNIC_BUG_ON(snic->cq_count != 2 * snic->wq_count);
  152. /* CQ for FW TO host */
  153. for (i = snic->wq_count; i < snic->cq_count; i++) {
  154. ret = svnic_cq_alloc(snic->vdev,
  155. &snic->cq[i],
  156. i,
  157. (snic->config.wq_enet_desc_count * 3),
  158. sizeof(struct snic_fw_req));
  159. if (ret)
  160. goto error_cleanup;
  161. }
  162. for (i = 0; i < snic->intr_count; i++) {
  163. ret = svnic_intr_alloc(snic->vdev, &snic->intr[i], i);
  164. if (ret)
  165. goto error_cleanup;
  166. }
  167. /*
  168. * Init WQ Resources.
  169. * WQ[0 to n] points to CQ[0 to n-1]
  170. * firmware to host comm points to CQ[n to m+1]
  171. */
  172. err_intr_enable = 1;
  173. err_intr_offset = snic->err_intr_offset;
  174. for (i = 0; i < snic->wq_count; i++) {
  175. svnic_wq_init(&snic->wq[i],
  176. i,
  177. err_intr_enable,
  178. err_intr_offset);
  179. }
  180. for (i = 0; i < snic->cq_count; i++) {
  181. intr_offset = i;
  182. svnic_cq_init(&snic->cq[i],
  183. 0 /* flow_control_enable */,
  184. 1 /* color_enable */,
  185. 0 /* cq_head */,
  186. 0 /* cq_tail */,
  187. 1 /* cq_tail_color */,
  188. 1 /* interrupt_enable */,
  189. 1 /* cq_entry_enable */,
  190. 0 /* cq_message_enable */,
  191. intr_offset,
  192. 0 /* cq_message_addr */);
  193. }
  194. /*
  195. * Init INTR resources
  196. * Assumption : snic is always in MSI-X mode
  197. */
  198. SNIC_BUG_ON(intr_mode != VNIC_DEV_INTR_MODE_MSIX);
  199. mask_on_assertion = 1;
  200. for (i = 0; i < snic->intr_count; i++) {
  201. svnic_intr_init(&snic->intr[i],
  202. snic->config.intr_timer,
  203. snic->config.intr_timer_type,
  204. mask_on_assertion);
  205. }
  206. /* init the stats memory by making the first call here */
  207. ret = svnic_dev_stats_dump(snic->vdev, &snic->stats);
  208. if (ret) {
  209. SNIC_HOST_ERR(snic->shost,
  210. "svnic_dev_stats_dump failed - x%x\n",
  211. ret);
  212. goto error_cleanup;
  213. }
  214. /* Clear LIF stats */
  215. svnic_dev_stats_clear(snic->vdev);
  216. ret = 0;
  217. return ret;
  218. error_cleanup:
  219. snic_free_vnic_res(snic);
  220. return ret;
  221. }
  222. void
  223. snic_log_q_error(struct snic *snic)
  224. {
  225. unsigned int i;
  226. u32 err_status;
  227. for (i = 0; i < snic->wq_count; i++) {
  228. err_status = ioread32(&snic->wq[i].ctrl->error_status);
  229. if (err_status)
  230. SNIC_HOST_ERR(snic->shost,
  231. "WQ[%d] error status %d\n",
  232. i,
  233. err_status);
  234. }
  235. } /* end of snic_log_q_error */