snic_ctl.c 7.2 KB

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