snic_ctl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/pci.h>
  5. #include <linux/slab.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/mempool.h>
  10. #include <scsi/scsi_tcq.h>
  11. #include <linux/ctype.h>
  12. #include "snic_io.h"
  13. #include "snic.h"
  14. #include "cq_enet_desc.h"
  15. #include "snic_fwint.h"
  16. /*
  17. * snic_handle_link : Handles link flaps.
  18. */
  19. void
  20. snic_handle_link(struct work_struct *work)
  21. {
  22. struct snic *snic = container_of(work, struct snic, link_work);
  23. if (snic->config.xpt_type == SNIC_DAS)
  24. return;
  25. snic->link_status = svnic_dev_link_status(snic->vdev);
  26. snic->link_down_cnt = svnic_dev_link_down_cnt(snic->vdev);
  27. SNIC_HOST_INFO(snic->shost, "Link Event: Link %s.\n",
  28. ((snic->link_status) ? "Up" : "Down"));
  29. SNIC_ASSERT_NOT_IMPL(1);
  30. }
  31. /*
  32. * snic_ver_enc : Encodes version str to int
  33. * version string is similar to netmask string
  34. */
  35. static int
  36. snic_ver_enc(const char *s)
  37. {
  38. int v[4] = {0};
  39. int i = 0, x = 0;
  40. char c;
  41. const char *p = s;
  42. /* validate version string */
  43. if ((strlen(s) > 15) || (strlen(s) < 7))
  44. goto end;
  45. while ((c = *p++)) {
  46. if (c == '.') {
  47. i++;
  48. continue;
  49. }
  50. if (i > 3 || !isdigit(c))
  51. goto end;
  52. v[i] = v[i] * 10 + (c - '0');
  53. }
  54. /* validate sub version numbers */
  55. for (i = 3; i >= 0; i--)
  56. if (v[i] > 0xff)
  57. goto end;
  58. x |= (v[0] << 24) | v[1] << 16 | v[2] << 8 | v[3];
  59. end:
  60. if (x == 0) {
  61. SNIC_ERR("Invalid version string [%s].\n", s);
  62. return -1;
  63. }
  64. return x;
  65. } /* end of snic_ver_enc */
  66. /*
  67. * snic_qeueue_exch_ver_req :
  68. *
  69. * Queues Exchange Version Request, to communicate host information
  70. * in return, it gets firmware version details
  71. */
  72. int
  73. snic_queue_exch_ver_req(struct snic *snic)
  74. {
  75. struct snic_req_info *rqi = NULL;
  76. struct snic_host_req *req = NULL;
  77. u32 ver = 0;
  78. int ret = 0;
  79. SNIC_HOST_INFO(snic->shost, "Exch Ver Req Preparing...\n");
  80. rqi = snic_req_init(snic, 0);
  81. if (!rqi) {
  82. SNIC_HOST_ERR(snic->shost, "Init Exch Ver Req failed\n");
  83. ret = -ENOMEM;
  84. goto error;
  85. }
  86. req = rqi_to_req(rqi);
  87. /* Initialize snic_host_req */
  88. snic_io_hdr_enc(&req->hdr, SNIC_REQ_EXCH_VER, 0, SCSI_NO_TAG,
  89. snic->config.hid, 0, (ulong)rqi);
  90. ver = snic_ver_enc(SNIC_DRV_VERSION);
  91. req->u.exch_ver.drvr_ver = cpu_to_le32(ver);
  92. req->u.exch_ver.os_type = cpu_to_le32(SNIC_OS_LINUX);
  93. snic_handle_untagged_req(snic, rqi);
  94. ret = snic_queue_wq_desc(snic, req, sizeof(*req));
  95. if (ret) {
  96. snic_release_untagged_req(snic, rqi);
  97. SNIC_HOST_ERR(snic->shost,
  98. "Queuing Exch Ver Req failed, err = %d\n",
  99. ret);
  100. goto error;
  101. }
  102. SNIC_HOST_INFO(snic->shost, "Exch Ver Req is issued. ret = %d\n", ret);
  103. error:
  104. return ret;
  105. } /* end of snic_queue_exch_ver_req */
  106. /*
  107. * snic_io_exch_ver_cmpl_handler
  108. */
  109. void
  110. snic_io_exch_ver_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
  111. {
  112. struct snic_req_info *rqi = NULL;
  113. struct snic_exch_ver_rsp *exv_cmpl = &fwreq->u.exch_ver_cmpl;
  114. u8 typ, hdr_stat;
  115. u32 cmnd_id, hid, max_sgs;
  116. ulong ctx = 0;
  117. unsigned long flags;
  118. SNIC_HOST_INFO(snic->shost, "Exch Ver Compl Received.\n");
  119. snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
  120. SNIC_BUG_ON(snic->config.hid != hid);
  121. rqi = (struct snic_req_info *) ctx;
  122. if (hdr_stat) {
  123. SNIC_HOST_ERR(snic->shost,
  124. "Exch Ver Completed w/ err status %d\n",
  125. hdr_stat);
  126. goto exch_cmpl_end;
  127. }
  128. spin_lock_irqsave(&snic->snic_lock, flags);
  129. snic->fwinfo.fw_ver = le32_to_cpu(exv_cmpl->version);
  130. snic->fwinfo.hid = le32_to_cpu(exv_cmpl->hid);
  131. snic->fwinfo.max_concur_ios = le32_to_cpu(exv_cmpl->max_concur_ios);
  132. snic->fwinfo.max_sgs_per_cmd = le32_to_cpu(exv_cmpl->max_sgs_per_cmd);
  133. snic->fwinfo.max_io_sz = le32_to_cpu(exv_cmpl->max_io_sz);
  134. snic->fwinfo.max_tgts = le32_to_cpu(exv_cmpl->max_tgts);
  135. snic->fwinfo.io_tmo = le16_to_cpu(exv_cmpl->io_timeout);
  136. SNIC_HOST_INFO(snic->shost,
  137. "vers %u hid %u max_concur_ios %u max_sgs_per_cmd %u max_io_sz %u max_tgts %u fw tmo %u\n",
  138. snic->fwinfo.fw_ver,
  139. snic->fwinfo.hid,
  140. snic->fwinfo.max_concur_ios,
  141. snic->fwinfo.max_sgs_per_cmd,
  142. snic->fwinfo.max_io_sz,
  143. snic->fwinfo.max_tgts,
  144. snic->fwinfo.io_tmo);
  145. SNIC_HOST_INFO(snic->shost,
  146. "HBA Capabilities = 0x%x\n",
  147. le32_to_cpu(exv_cmpl->hba_cap));
  148. /* Updating SGList size */
  149. max_sgs = snic->fwinfo.max_sgs_per_cmd;
  150. if (max_sgs && max_sgs < SNIC_MAX_SG_DESC_CNT) {
  151. snic->shost->sg_tablesize = max_sgs;
  152. SNIC_HOST_INFO(snic->shost, "Max SGs set to %d\n",
  153. snic->shost->sg_tablesize);
  154. } else if (max_sgs > snic->shost->sg_tablesize) {
  155. SNIC_HOST_INFO(snic->shost,
  156. "Target type %d Supports Larger Max SGList %d than driver's Max SG List %d.\n",
  157. snic->config.xpt_type, max_sgs,
  158. snic->shost->sg_tablesize);
  159. }
  160. if (snic->shost->can_queue > snic->fwinfo.max_concur_ios)
  161. snic->shost->can_queue = snic->fwinfo.max_concur_ios;
  162. snic->shost->max_sectors = snic->fwinfo.max_io_sz >> 9;
  163. if (snic->fwinfo.wait)
  164. complete(snic->fwinfo.wait);
  165. spin_unlock_irqrestore(&snic->snic_lock, flags);
  166. exch_cmpl_end:
  167. snic_release_untagged_req(snic, rqi);
  168. SNIC_HOST_INFO(snic->shost, "Exch_cmpl Done, hdr_stat %d.\n", hdr_stat);
  169. } /* end of snic_io_exch_ver_cmpl_handler */
  170. /*
  171. * snic_get_conf
  172. *
  173. * Synchronous call, and Retrieves snic params.
  174. */
  175. int
  176. snic_get_conf(struct snic *snic)
  177. {
  178. DECLARE_COMPLETION_ONSTACK(wait);
  179. unsigned long flags;
  180. int ret;
  181. int nr_retries = 3;
  182. SNIC_HOST_INFO(snic->shost, "Retrieving snic params.\n");
  183. spin_lock_irqsave(&snic->snic_lock, flags);
  184. memset(&snic->fwinfo, 0, sizeof(snic->fwinfo));
  185. snic->fwinfo.wait = &wait;
  186. spin_unlock_irqrestore(&snic->snic_lock, flags);
  187. /* Additional delay to handle HW Resource initialization. */
  188. msleep(50);
  189. /*
  190. * Exch ver req can be ignored by FW, if HW Resource initialization
  191. * is in progress, Hence retry.
  192. */
  193. do {
  194. ret = snic_queue_exch_ver_req(snic);
  195. if (ret)
  196. return ret;
  197. wait_for_completion_timeout(&wait, msecs_to_jiffies(2000));
  198. spin_lock_irqsave(&snic->snic_lock, flags);
  199. ret = (snic->fwinfo.fw_ver != 0) ? 0 : -ETIMEDOUT;
  200. if (ret)
  201. SNIC_HOST_ERR(snic->shost,
  202. "Failed to retrieve snic params,\n");
  203. /* Unset fwinfo.wait, on success or on last retry */
  204. if (ret == 0 || nr_retries == 1)
  205. snic->fwinfo.wait = NULL;
  206. spin_unlock_irqrestore(&snic->snic_lock, flags);
  207. } while (ret && --nr_retries);
  208. return ret;
  209. } /* end of snic_get_info */