2 * Copyright 2014 Cisco Systems, Inc. All rights reserved.
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.
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
18 #include <linux/errno.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/workqueue.h>
24 #include <linux/spinlock.h>
25 #include <linux/mempool.h>
26 #include <scsi/scsi_tcq.h>
27 #include <linux/ctype.h>
31 #include "cq_enet_desc.h"
32 #include "snic_fwint.h"
35 * snic_handle_link : Handles link flaps.
38 snic_handle_link(struct work_struct *work)
40 struct snic *snic = container_of(work, struct snic, link_work);
42 if (snic->config.xpt_type == SNIC_DAS)
45 snic->link_status = svnic_dev_link_status(snic->vdev);
46 snic->link_down_cnt = svnic_dev_link_down_cnt(snic->vdev);
47 SNIC_HOST_INFO(snic->shost, "Link Event: Link %s.\n",
48 ((snic->link_status) ? "Up" : "Down"));
50 SNIC_ASSERT_NOT_IMPL(1);
55 * snic_ver_enc : Encodes version str to int
56 * version string is similar to netmask string
59 snic_ver_enc(const char *s)
66 /* validate version string */
67 if ((strlen(s) > 15) || (strlen(s) < 7))
76 if (i > 3 || !isdigit(c))
79 v[i] = v[i] * 10 + (c - '0');
82 /* validate sub version numbers */
83 for (i = 3; i >= 0; i--)
87 x |= (v[0] << 24) | v[1] << 16 | v[2] << 8 | v[3];
91 SNIC_ERR("Invalid version string [%s].\n", s);
97 } /* end of snic_ver_enc */
100 * snic_qeueue_exch_ver_req :
102 * Queues Exchange Version Request, to communicate host information
103 * in return, it gets firmware version details
106 snic_queue_exch_ver_req(struct snic *snic)
108 struct snic_req_info *rqi = NULL;
109 struct snic_host_req *req = NULL;
113 SNIC_HOST_INFO(snic->shost, "Exch Ver Req Preparing...\n");
115 rqi = snic_req_init(snic, 0);
117 SNIC_HOST_ERR(snic->shost,
118 "Queuing Exch Ver Req failed, err = %d\n",
125 req = rqi_to_req(rqi);
127 /* Initialize snic_host_req */
128 snic_io_hdr_enc(&req->hdr, SNIC_REQ_EXCH_VER, 0, SCSI_NO_TAG,
129 snic->config.hid, 0, (ulong)rqi);
130 ver = snic_ver_enc(SNIC_DRV_VERSION);
131 req->u.exch_ver.drvr_ver = cpu_to_le32(ver);
132 req->u.exch_ver.os_type = cpu_to_le32(SNIC_OS_LINUX);
134 snic_handle_untagged_req(snic, rqi);
136 ret = snic_queue_wq_desc(snic, req, sizeof(*req));
138 snic_release_untagged_req(snic, rqi);
139 SNIC_HOST_ERR(snic->shost,
140 "Queuing Exch Ver Req failed, err = %d\n",
145 SNIC_HOST_INFO(snic->shost, "Exch Ver Req is issued. ret = %d\n", ret);
149 } /* end of snic_queue_exch_ver_req */
152 * snic_io_exch_ver_cmpl_handler
155 snic_io_exch_ver_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
157 struct snic_req_info *rqi = NULL;
158 struct snic_exch_ver_rsp *exv_cmpl = &fwreq->u.exch_ver_cmpl;
160 u32 cmnd_id, hid, max_sgs;
164 SNIC_HOST_INFO(snic->shost, "Exch Ver Compl Received.\n");
165 snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
166 SNIC_BUG_ON(snic->config.hid != hid);
167 rqi = (struct snic_req_info *) ctx;
170 SNIC_HOST_ERR(snic->shost,
171 "Exch Ver Completed w/ err status %d\n",
177 spin_lock_irqsave(&snic->snic_lock, flags);
178 snic->fwinfo.fw_ver = le32_to_cpu(exv_cmpl->version);
179 snic->fwinfo.hid = le32_to_cpu(exv_cmpl->hid);
180 snic->fwinfo.max_concur_ios = le32_to_cpu(exv_cmpl->max_concur_ios);
181 snic->fwinfo.max_sgs_per_cmd = le32_to_cpu(exv_cmpl->max_sgs_per_cmd);
182 snic->fwinfo.max_io_sz = le32_to_cpu(exv_cmpl->max_io_sz);
183 snic->fwinfo.max_tgts = le32_to_cpu(exv_cmpl->max_tgts);
184 snic->fwinfo.io_tmo = le16_to_cpu(exv_cmpl->io_timeout);
186 SNIC_HOST_INFO(snic->shost,
187 "vers %u hid %u max_concur_ios %u max_sgs_per_cmd %u max_io_sz %u max_tgts %u fw tmo %u\n",
190 snic->fwinfo.max_concur_ios,
191 snic->fwinfo.max_sgs_per_cmd,
192 snic->fwinfo.max_io_sz,
193 snic->fwinfo.max_tgts,
194 snic->fwinfo.io_tmo);
196 SNIC_HOST_INFO(snic->shost,
197 "HBA Capabilities = 0x%x\n",
198 le32_to_cpu(exv_cmpl->hba_cap));
200 /* Updating SGList size */
201 max_sgs = snic->fwinfo.max_sgs_per_cmd;
202 if (max_sgs && max_sgs < SNIC_MAX_SG_DESC_CNT) {
203 snic->shost->sg_tablesize = max_sgs;
204 SNIC_HOST_INFO(snic->shost, "Max SGs set to %d\n",
205 snic->shost->sg_tablesize);
206 } else if (max_sgs > snic->shost->sg_tablesize) {
207 SNIC_HOST_INFO(snic->shost,
208 "Target type %d Supports Larger Max SGList %d than driver's Max SG List %d.\n",
209 snic->config.xpt_type, max_sgs,
210 snic->shost->sg_tablesize);
213 if (snic->shost->can_queue > snic->fwinfo.max_concur_ios)
214 snic->shost->can_queue = snic->fwinfo.max_concur_ios;
216 snic->shost->max_sectors = snic->fwinfo.max_io_sz >> 9;
217 if (snic->fwinfo.wait)
218 complete(snic->fwinfo.wait);
220 spin_unlock_irqrestore(&snic->snic_lock, flags);
223 snic_release_untagged_req(snic, rqi);
225 SNIC_HOST_INFO(snic->shost, "Exch_cmpl Done, hdr_stat %d.\n", hdr_stat);
226 } /* end of snic_io_exch_ver_cmpl_handler */
231 * Synchronous call, and Retrieves snic params.
234 snic_get_conf(struct snic *snic)
236 DECLARE_COMPLETION_ONSTACK(wait);
241 SNIC_HOST_INFO(snic->shost, "Retrieving snic params.\n");
242 spin_lock_irqsave(&snic->snic_lock, flags);
243 memset(&snic->fwinfo, 0, sizeof(snic->fwinfo));
244 snic->fwinfo.wait = &wait;
245 spin_unlock_irqrestore(&snic->snic_lock, flags);
247 /* Additional delay to handle HW Resource initialization. */
251 * Exch ver req can be ignored by FW, if HW Resource initialization
252 * is in progress, Hence retry.
255 ret = snic_queue_exch_ver_req(snic);
259 wait_for_completion_timeout(&wait, msecs_to_jiffies(2000));
260 spin_lock_irqsave(&snic->snic_lock, flags);
261 ret = (snic->fwinfo.fw_ver != 0) ? 0 : -ETIMEDOUT;
263 SNIC_HOST_ERR(snic->shost,
264 "Failed to retrieve snic params,\n");
266 /* Unset fwinfo.wait, on success or on last retry */
267 if (ret == 0 || nr_retries == 1)
268 snic->fwinfo.wait = NULL;
270 spin_unlock_irqrestore(&snic->snic_lock, flags);
271 } while (ret && --nr_retries);
274 } /* end of snic_get_info */