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