snic_ctl.c 7.1 KB

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