blob: ffcad26af96894bd36e74b3fa81bf8c6441de45d [file] [log] [blame]
Giridhar Malavali6e980162010-03-19 17:03:58 -07001/*
2 * QLogic Fibre Channel HBA Driver
Chad Dupuis46152ce2012-08-22 14:21:08 -04003 * Copyright (c) 2003-2012 QLogic Corporation
Giridhar Malavali6e980162010-03-19 17:03:58 -07004 *
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7#include "qla_def.h"
8
9#include <linux/kthread.h>
10#include <linux/vmalloc.h>
11#include <linux/delay.h>
12
13/* BSG support for ELS/CT pass through */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080014void
15qla2x00_bsg_job_done(void *data, void *ptr, int res)
Giridhar Malavali6e980162010-03-19 17:03:58 -070016{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080017 srb_t *sp = (srb_t *)ptr;
18 struct scsi_qla_host *vha = (scsi_qla_host_t *)data;
19 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
20
21 bsg_job->reply->result = res;
22 bsg_job->job_done(bsg_job);
23 sp->free(vha, sp);
24}
25
26void
27qla2x00_bsg_sp_free(void *data, void *ptr)
28{
29 srb_t *sp = (srb_t *)ptr;
Chad Dupuisb00ee7d2013-02-08 01:57:50 -050030 struct scsi_qla_host *vha = sp->fcport->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080031 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali6e980162010-03-19 17:03:58 -070032 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040033 struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
Giridhar Malavali6e980162010-03-19 17:03:58 -070034
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040035 if (sp->type == SRB_FXIOCB_BCMD) {
36 piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
37 &bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Giridhar Malavali6e980162010-03-19 17:03:58 -070038
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040039 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
40 dma_unmap_sg(&ha->pdev->dev,
41 bsg_job->request_payload.sg_list,
42 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
43
44 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
45 dma_unmap_sg(&ha->pdev->dev,
46 bsg_job->reply_payload.sg_list,
47 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
48 } else {
49 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
50 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
51
52 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
53 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
54 }
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080055
56 if (sp->type == SRB_CT_CMD ||
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040057 sp->type == SRB_FXIOCB_BCMD ||
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080058 sp->type == SRB_ELS_CMD_HST)
59 kfree(sp->fcport);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -050060 qla2x00_rel_sp(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -070061}
62
Sarang Radke09ff7012010-03-19 17:03:59 -070063int
Saurav Kashyap7c3df132011-07-14 12:00:13 -070064qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
65 struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
Sarang Radke09ff7012010-03-19 17:03:59 -070066{
67 int i, ret, num_valid;
68 uint8_t *bcode;
69 struct qla_fcp_prio_entry *pri_entry;
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050070 uint32_t *bcode_val_ptr, bcode_val;
Sarang Radke09ff7012010-03-19 17:03:59 -070071
72 ret = 1;
73 num_valid = 0;
74 bcode = (uint8_t *)pri_cfg;
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050075 bcode_val_ptr = (uint32_t *)pri_cfg;
76 bcode_val = (uint32_t)(*bcode_val_ptr);
Sarang Radke09ff7012010-03-19 17:03:59 -070077
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050078 if (bcode_val == 0xFFFFFFFF) {
79 /* No FCP Priority config data in flash */
Saurav Kashyap7c3df132011-07-14 12:00:13 -070080 ql_dbg(ql_dbg_user, vha, 0x7051,
81 "No FCP Priority config data.\n");
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050082 return 0;
83 }
84
85 if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
86 bcode[3] != 'S') {
87 /* Invalid FCP priority data header*/
Saurav Kashyap7c3df132011-07-14 12:00:13 -070088 ql_dbg(ql_dbg_user, vha, 0x7052,
89 "Invalid FCP Priority data header. bcode=0x%x.\n",
90 bcode_val);
Sarang Radke09ff7012010-03-19 17:03:59 -070091 return 0;
92 }
93 if (flag != 1)
94 return ret;
95
96 pri_entry = &pri_cfg->entry[0];
97 for (i = 0; i < pri_cfg->num_entries; i++) {
98 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
99 num_valid++;
100 pri_entry++;
101 }
102
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500103 if (num_valid == 0) {
104 /* No valid FCP priority data entries */
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700105 ql_dbg(ql_dbg_user, vha, 0x7053,
106 "No valid FCP Priority data entries.\n");
Sarang Radke09ff7012010-03-19 17:03:59 -0700107 ret = 0;
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500108 } else {
109 /* FCP priority data is valid */
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700110 ql_dbg(ql_dbg_user, vha, 0x7054,
111 "Valid FCP priority data. num entries = %d.\n",
112 num_valid);
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500113 }
Sarang Radke09ff7012010-03-19 17:03:59 -0700114
115 return ret;
116}
117
118static int
119qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
120{
121 struct Scsi_Host *host = bsg_job->shost;
122 scsi_qla_host_t *vha = shost_priv(host);
123 struct qla_hw_data *ha = vha->hw;
124 int ret = 0;
125 uint32_t len;
126 uint32_t oper;
127
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -0400128 if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_P3P_TYPE(ha))) {
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500129 ret = -EINVAL;
130 goto exit_fcp_prio_cfg;
131 }
132
Sarang Radke09ff7012010-03-19 17:03:59 -0700133 /* Get the sub command */
134 oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
135
136 /* Only set config is allowed if config memory is not allocated */
137 if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
138 ret = -EINVAL;
139 goto exit_fcp_prio_cfg;
140 }
141 switch (oper) {
142 case QLFC_FCP_PRIO_DISABLE:
143 if (ha->flags.fcp_prio_enabled) {
144 ha->flags.fcp_prio_enabled = 0;
145 ha->fcp_prio_cfg->attributes &=
146 ~FCP_PRIO_ATTR_ENABLE;
147 qla24xx_update_all_fcp_prio(vha);
148 bsg_job->reply->result = DID_OK;
149 } else {
150 ret = -EINVAL;
151 bsg_job->reply->result = (DID_ERROR << 16);
152 goto exit_fcp_prio_cfg;
153 }
154 break;
155
156 case QLFC_FCP_PRIO_ENABLE:
157 if (!ha->flags.fcp_prio_enabled) {
158 if (ha->fcp_prio_cfg) {
159 ha->flags.fcp_prio_enabled = 1;
160 ha->fcp_prio_cfg->attributes |=
161 FCP_PRIO_ATTR_ENABLE;
162 qla24xx_update_all_fcp_prio(vha);
163 bsg_job->reply->result = DID_OK;
164 } else {
165 ret = -EINVAL;
166 bsg_job->reply->result = (DID_ERROR << 16);
167 goto exit_fcp_prio_cfg;
168 }
169 }
170 break;
171
172 case QLFC_FCP_PRIO_GET_CONFIG:
173 len = bsg_job->reply_payload.payload_len;
174 if (!len || len > FCP_PRIO_CFG_SIZE) {
175 ret = -EINVAL;
176 bsg_job->reply->result = (DID_ERROR << 16);
177 goto exit_fcp_prio_cfg;
178 }
179
180 bsg_job->reply->result = DID_OK;
181 bsg_job->reply->reply_payload_rcv_len =
182 sg_copy_from_buffer(
183 bsg_job->reply_payload.sg_list,
184 bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
185 len);
186
187 break;
188
189 case QLFC_FCP_PRIO_SET_CONFIG:
190 len = bsg_job->request_payload.payload_len;
191 if (!len || len > FCP_PRIO_CFG_SIZE) {
192 bsg_job->reply->result = (DID_ERROR << 16);
193 ret = -EINVAL;
194 goto exit_fcp_prio_cfg;
195 }
196
197 if (!ha->fcp_prio_cfg) {
198 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
199 if (!ha->fcp_prio_cfg) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700200 ql_log(ql_log_warn, vha, 0x7050,
201 "Unable to allocate memory for fcp prio "
202 "config data (%x).\n", FCP_PRIO_CFG_SIZE);
Sarang Radke09ff7012010-03-19 17:03:59 -0700203 bsg_job->reply->result = (DID_ERROR << 16);
204 ret = -ENOMEM;
205 goto exit_fcp_prio_cfg;
206 }
207 }
208
209 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
210 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
211 bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
212 FCP_PRIO_CFG_SIZE);
213
214 /* validate fcp priority data */
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700215
216 if (!qla24xx_fcp_prio_cfg_valid(vha,
217 (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
Sarang Radke09ff7012010-03-19 17:03:59 -0700218 bsg_job->reply->result = (DID_ERROR << 16);
219 ret = -EINVAL;
220 /* If buffer was invalidatic int
221 * fcp_prio_cfg is of no use
222 */
223 vfree(ha->fcp_prio_cfg);
224 ha->fcp_prio_cfg = NULL;
225 goto exit_fcp_prio_cfg;
226 }
227
228 ha->flags.fcp_prio_enabled = 0;
229 if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
230 ha->flags.fcp_prio_enabled = 1;
231 qla24xx_update_all_fcp_prio(vha);
232 bsg_job->reply->result = DID_OK;
233 break;
234 default:
235 ret = -EINVAL;
236 break;
237 }
238exit_fcp_prio_cfg:
Armen Baloyan63ea9232012-11-21 02:39:53 -0500239 if (!ret)
240 bsg_job->job_done(bsg_job);
Sarang Radke09ff7012010-03-19 17:03:59 -0700241 return ret;
242}
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800243
Giridhar Malavali6e980162010-03-19 17:03:58 -0700244static int
245qla2x00_process_els(struct fc_bsg_job *bsg_job)
246{
247 struct fc_rport *rport;
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500248 fc_port_t *fcport = NULL;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700249 struct Scsi_Host *host;
250 scsi_qla_host_t *vha;
251 struct qla_hw_data *ha;
252 srb_t *sp;
253 const char *type;
254 int req_sg_cnt, rsp_sg_cnt;
255 int rval = (DRIVER_ERROR << 16);
256 uint16_t nextlid = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700257
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500258 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
259 rport = bsg_job->rport;
260 fcport = *(fc_port_t **) rport->dd_data;
261 host = rport_to_shost(rport);
262 vha = shost_priv(host);
263 ha = vha->hw;
264 type = "FC_BSG_RPT_ELS";
265 } else {
266 host = bsg_job->shost;
267 vha = shost_priv(host);
268 ha = vha->hw;
269 type = "FC_BSG_HST_ELS_NOLOGIN";
270 }
271
Bart Van Assche8c0eb592013-06-25 11:27:31 -0400272 if (!vha->flags.online) {
273 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
274 rval = -EIO;
275 goto done;
276 }
277
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500278 /* pass through is supported only for ISP 4Gb or higher */
279 if (!IS_FWI2_CAPABLE(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700280 ql_dbg(ql_dbg_user, vha, 0x7001,
281 "ELS passthru not supported for ISP23xx based adapters.\n");
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500282 rval = -EPERM;
283 goto done;
284 }
285
Giridhar Malavali6e980162010-03-19 17:03:58 -0700286 /* Multiple SG's are not supported for ELS requests */
287 if (bsg_job->request_payload.sg_cnt > 1 ||
288 bsg_job->reply_payload.sg_cnt > 1) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700289 ql_dbg(ql_dbg_user, vha, 0x7002,
290 "Multiple SG's are not suppored for ELS requests, "
291 "request_sg_cnt=%x reply_sg_cnt=%x.\n",
292 bsg_job->request_payload.sg_cnt,
293 bsg_job->reply_payload.sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700294 rval = -EPERM;
295 goto done;
296 }
297
298 /* ELS request for rport */
299 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700300 /* make sure the rport is logged in,
301 * if not perform fabric login
302 */
303 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700304 ql_dbg(ql_dbg_user, vha, 0x7003,
305 "Failed to login port %06X for ELS passthru.\n",
306 fcport->d_id.b24);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700307 rval = -EIO;
308 goto done;
309 }
310 } else {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700311 /* Allocate a dummy fcport structure, since functions
312 * preparing the IOCB and mailbox command retrieves port
313 * specific information from fcport structure. For Host based
314 * ELS commands there will be no fcport structure allocated
315 */
316 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
317 if (!fcport) {
318 rval = -ENOMEM;
319 goto done;
320 }
321
322 /* Initialize all required fields of fcport */
323 fcport->vha = vha;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700324 fcport->d_id.b.al_pa =
325 bsg_job->request->rqst_data.h_els.port_id[0];
326 fcport->d_id.b.area =
327 bsg_job->request->rqst_data.h_els.port_id[1];
328 fcport->d_id.b.domain =
329 bsg_job->request->rqst_data.h_els.port_id[2];
330 fcport->loop_id =
331 (fcport->d_id.b.al_pa == 0xFD) ?
332 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
333 }
334
Giridhar Malavali6e980162010-03-19 17:03:58 -0700335 req_sg_cnt =
336 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
337 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
338 if (!req_sg_cnt) {
339 rval = -ENOMEM;
340 goto done_free_fcport;
341 }
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700342
343 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
344 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700345 if (!rsp_sg_cnt) {
346 rval = -ENOMEM;
347 goto done_free_fcport;
348 }
349
350 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700351 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700352 ql_log(ql_log_warn, vha, 0x7008,
353 "dma mapping resulted in different sg counts, "
354 "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
355 "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
356 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700357 rval = -EAGAIN;
358 goto done_unmap_sg;
359 }
360
361 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800362 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700363 if (!sp) {
364 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700365 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700366 }
367
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800368 sp->type =
Giridhar Malavali6e980162010-03-19 17:03:58 -0700369 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
370 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800371 sp->name =
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700372 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
373 "bsg_els_rpt" : "bsg_els_hst");
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800374 sp->u.bsg_job = bsg_job;
375 sp->free = qla2x00_bsg_sp_free;
376 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700377
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700378 ql_dbg(ql_dbg_user, vha, 0x700a,
379 "bsg rqst type: %s els type: %x - loop-id=%x "
380 "portid=%-2x%02x%02x.\n", type,
381 bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
382 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700383
384 rval = qla2x00_start_sp(sp);
385 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700386 ql_log(ql_log_warn, vha, 0x700e,
387 "qla2x00_start_sp failed = %d\n", rval);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -0500388 qla2x00_rel_sp(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700389 rval = -EIO;
390 goto done_unmap_sg;
391 }
392 return rval;
393
394done_unmap_sg:
395 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
396 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
397 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
398 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
399 goto done_free_fcport;
400
401done_free_fcport:
Bart Van Assche8c0eb592013-06-25 11:27:31 -0400402 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS)
Giridhar Malavali6e980162010-03-19 17:03:58 -0700403 kfree(fcport);
404done:
405 return rval;
406}
407
Andrew Vasquez57807902011-11-18 09:03:20 -0800408inline uint16_t
409qla24xx_calc_ct_iocbs(uint16_t dsds)
410{
411 uint16_t iocbs;
412
413 iocbs = 1;
414 if (dsds > 2) {
415 iocbs += (dsds - 2) / 5;
416 if ((dsds - 2) % 5)
417 iocbs++;
418 }
419 return iocbs;
420}
421
Giridhar Malavali6e980162010-03-19 17:03:58 -0700422static int
423qla2x00_process_ct(struct fc_bsg_job *bsg_job)
424{
425 srb_t *sp;
426 struct Scsi_Host *host = bsg_job->shost;
427 scsi_qla_host_t *vha = shost_priv(host);
428 struct qla_hw_data *ha = vha->hw;
429 int rval = (DRIVER_ERROR << 16);
430 int req_sg_cnt, rsp_sg_cnt;
431 uint16_t loop_id;
432 struct fc_port *fcport;
433 char *type = "FC_BSG_HST_CT";
Giridhar Malavali6e980162010-03-19 17:03:58 -0700434
Giridhar Malavali6e980162010-03-19 17:03:58 -0700435 req_sg_cnt =
436 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
437 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700438 if (!req_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700439 ql_log(ql_log_warn, vha, 0x700f,
440 "dma_map_sg return %d for request\n", req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700441 rval = -ENOMEM;
442 goto done;
443 }
444
445 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
446 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
447 if (!rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700448 ql_log(ql_log_warn, vha, 0x7010,
449 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700450 rval = -ENOMEM;
451 goto done;
452 }
453
454 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700455 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700456 ql_log(ql_log_warn, vha, 0x7011,
457 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
458 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
459 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700460 rval = -EAGAIN;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700461 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700462 }
463
464 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700465 ql_log(ql_log_warn, vha, 0x7012,
466 "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700467 rval = -EIO;
468 goto done_unmap_sg;
469 }
470
471 loop_id =
472 (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
473 >> 24;
474 switch (loop_id) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700475 case 0xFC:
476 loop_id = cpu_to_le16(NPH_SNS);
477 break;
478 case 0xFA:
479 loop_id = vha->mgmt_svr_loop_id;
480 break;
481 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700482 ql_dbg(ql_dbg_user, vha, 0x7013,
483 "Unknown loop id: %x.\n", loop_id);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700484 rval = -EINVAL;
485 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700486 }
487
488 /* Allocate a dummy fcport structure, since functions preparing the
489 * IOCB and mailbox command retrieves port specific information
490 * from fcport structure. For Host based ELS commands there will be
491 * no fcport structure allocated
492 */
493 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700494 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700495 ql_log(ql_log_warn, vha, 0x7014,
496 "Failed to allocate fcport.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700497 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700498 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700499 }
500
501 /* Initialize all required fields of fcport */
502 fcport->vha = vha;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700503 fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
504 fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
505 fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
506 fcport->loop_id = loop_id;
507
508 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800509 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700510 if (!sp) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700511 ql_log(ql_log_warn, vha, 0x7015,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800512 "qla2x00_get_sp failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700513 rval = -ENOMEM;
514 goto done_free_fcport;
515 }
516
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800517 sp->type = SRB_CT_CMD;
518 sp->name = "bsg_ct";
519 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
520 sp->u.bsg_job = bsg_job;
521 sp->free = qla2x00_bsg_sp_free;
522 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700523
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700524 ql_dbg(ql_dbg_user, vha, 0x7016,
525 "bsg rqst type: %s else type: %x - "
526 "loop-id=%x portid=%02x%02x%02x.\n", type,
527 (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
528 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
529 fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700530
531 rval = qla2x00_start_sp(sp);
532 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700533 ql_log(ql_log_warn, vha, 0x7017,
534 "qla2x00_start_sp failed=%d.\n", rval);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -0500535 qla2x00_rel_sp(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700536 rval = -EIO;
537 goto done_free_fcport;
538 }
539 return rval;
540
541done_free_fcport:
542 kfree(fcport);
543done_unmap_sg:
544 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
545 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
546 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
547 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
548done:
549 return rval;
550}
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700551
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400552/* Disable loopback mode */
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700553static inline int
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400554qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500555 int wait, int wait2)
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700556{
557 int ret = 0;
558 int rval = 0;
559 uint16_t new_config[4];
560 struct qla_hw_data *ha = vha->hw;
561
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -0400562 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700563 goto done_reset_internal;
564
565 memset(new_config, 0 , sizeof(new_config));
566 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400567 ENABLE_INTERNAL_LOOPBACK ||
568 (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
569 ENABLE_EXTERNAL_LOOPBACK) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700570 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400571 ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
572 (new_config[0] & INTERNAL_LOOPBACK_MASK));
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700573 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
574
575 ha->notify_dcbx_comp = wait;
Chad Dupuisf356bef2013-02-08 01:58:04 -0500576 ha->notify_lb_portup_comp = wait2;
577
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700578 ret = qla81xx_set_port_config(vha, new_config);
579 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700580 ql_log(ql_log_warn, vha, 0x7025,
581 "Set port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700582 ha->notify_dcbx_comp = 0;
Chad Dupuisf356bef2013-02-08 01:58:04 -0500583 ha->notify_lb_portup_comp = 0;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700584 rval = -EINVAL;
585 goto done_reset_internal;
586 }
587
588 /* Wait for DCBX complete event */
589 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500590 (DCBX_COMP_TIMEOUT * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700591 ql_dbg(ql_dbg_user, vha, 0x7026,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500592 "DCBX completion not received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700593 ha->notify_dcbx_comp = 0;
Chad Dupuisf356bef2013-02-08 01:58:04 -0500594 ha->notify_lb_portup_comp = 0;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700595 rval = -EINVAL;
596 goto done_reset_internal;
597 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700598 ql_dbg(ql_dbg_user, vha, 0x7027,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500599 "DCBX completion received.\n");
600
601 if (wait2 &&
602 !wait_for_completion_timeout(&ha->lb_portup_comp,
603 (LB_PORTUP_COMP_TIMEOUT * HZ))) {
604 ql_dbg(ql_dbg_user, vha, 0x70c5,
605 "Port up completion not received.\n");
606 ha->notify_lb_portup_comp = 0;
607 rval = -EINVAL;
608 goto done_reset_internal;
609 } else
610 ql_dbg(ql_dbg_user, vha, 0x70c6,
611 "Port up completion received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700612
613 ha->notify_dcbx_comp = 0;
Chad Dupuisf356bef2013-02-08 01:58:04 -0500614 ha->notify_lb_portup_comp = 0;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700615 }
616done_reset_internal:
617 return rval;
618}
619
Chad Dupuis67b2a312013-02-08 01:57:51 -0500620/*
621 * Set the port configuration to enable the internal or external loopback
622 * depending on the loopback mode.
623 */
624static inline int
625qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
626 uint16_t *new_config, uint16_t mode)
627{
628 int ret = 0;
629 int rval = 0;
630 struct qla_hw_data *ha = vha->hw;
631
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -0400632 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
Chad Dupuis67b2a312013-02-08 01:57:51 -0500633 goto done_set_internal;
634
635 if (mode == INTERNAL_LOOPBACK)
636 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
637 else if (mode == EXTERNAL_LOOPBACK)
638 new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
639 ql_dbg(ql_dbg_user, vha, 0x70be,
640 "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
641
642 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
643
644 ha->notify_dcbx_comp = 1;
645 ret = qla81xx_set_port_config(vha, new_config);
646 if (ret != QLA_SUCCESS) {
647 ql_log(ql_log_warn, vha, 0x7021,
648 "set port config failed.\n");
649 ha->notify_dcbx_comp = 0;
650 rval = -EINVAL;
651 goto done_set_internal;
652 }
653
654 /* Wait for DCBX complete event */
Chad Dupuisf356bef2013-02-08 01:58:04 -0500655 if (!wait_for_completion_timeout(&ha->dcbx_comp,
656 (DCBX_COMP_TIMEOUT * HZ))) {
Chad Dupuis67b2a312013-02-08 01:57:51 -0500657 ql_dbg(ql_dbg_user, vha, 0x7022,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500658 "DCBX completion not received.\n");
659 ret = qla81xx_reset_loopback_mode(vha, new_config, 0, 0);
Chad Dupuis67b2a312013-02-08 01:57:51 -0500660 /*
661 * If the reset of the loopback mode doesn't work take a FCoE
662 * dump and reset the chip.
663 */
664 if (ret) {
665 ha->isp_ops->fw_dump(vha, 0);
666 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
667 }
668 rval = -EINVAL;
669 } else {
670 if (ha->flags.idc_compl_status) {
671 ql_dbg(ql_dbg_user, vha, 0x70c3,
672 "Bad status in IDC Completion AEN\n");
673 rval = -EINVAL;
674 ha->flags.idc_compl_status = 0;
675 } else
676 ql_dbg(ql_dbg_user, vha, 0x7023,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500677 "DCBX completion received.\n");
Chad Dupuis67b2a312013-02-08 01:57:51 -0500678 }
679
680 ha->notify_dcbx_comp = 0;
681
682done_set_internal:
683 return rval;
684}
685
Giridhar Malavali6e980162010-03-19 17:03:58 -0700686static int
687qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
688{
689 struct Scsi_Host *host = bsg_job->shost;
690 scsi_qla_host_t *vha = shost_priv(host);
691 struct qla_hw_data *ha = vha->hw;
692 int rval;
693 uint8_t command_sent;
694 char *type;
695 struct msg_echo_lb elreq;
696 uint16_t response[MAILBOX_REGISTER_COUNT];
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700697 uint16_t config[4], new_config[4];
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700698 uint8_t *fw_sts_ptr;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700699 uint8_t *req_data = NULL;
700 dma_addr_t req_data_dma;
701 uint32_t req_data_len;
702 uint8_t *rsp_data = NULL;
703 dma_addr_t rsp_data_dma;
704 uint32_t rsp_data_len;
705
Giridhar Malavali6e980162010-03-19 17:03:58 -0700706 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700707 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700708 return -EIO;
709 }
710
711 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
712 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
713 DMA_TO_DEVICE);
714
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700715 if (!elreq.req_sg_cnt) {
716 ql_log(ql_log_warn, vha, 0x701a,
717 "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700718 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700719 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700720
721 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
722 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
723 DMA_FROM_DEVICE);
724
725 if (!elreq.rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700726 ql_log(ql_log_warn, vha, 0x701b,
727 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700728 rval = -ENOMEM;
729 goto done_unmap_req_sg;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700730 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700731
732 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
733 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700734 ql_log(ql_log_warn, vha, 0x701c,
735 "dma mapping resulted in different sg counts, "
736 "request_sg_cnt: %x dma_request_sg_cnt: %x "
737 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
738 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
739 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700740 rval = -EAGAIN;
741 goto done_unmap_sg;
742 }
743 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
744 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
745 &req_data_dma, GFP_KERNEL);
746 if (!req_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700747 ql_log(ql_log_warn, vha, 0x701d,
748 "dma alloc failed for req_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700749 rval = -ENOMEM;
750 goto done_unmap_sg;
751 }
752
753 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
754 &rsp_data_dma, GFP_KERNEL);
755 if (!rsp_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700756 ql_log(ql_log_warn, vha, 0x7004,
757 "dma alloc failed for rsp_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700758 rval = -ENOMEM;
759 goto done_free_dma_req;
760 }
761
762 /* Copy the request buffer in req_data now */
763 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
764 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
765
766 elreq.send_dma = req_data_dma;
767 elreq.rcv_dma = rsp_data_dma;
768 elreq.transfer_size = req_data_len;
769
770 elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Joe Carnuccio1b98b422013-03-28 08:21:26 -0400771 elreq.iteration_count =
772 bsg_job->request->rqst_data.h_vendor.vendor_cmd[2];
Giridhar Malavali6e980162010-03-19 17:03:58 -0700773
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400774 if (atomic_read(&vha->loop_state) == LOOP_READY &&
775 (ha->current_topology == ISP_CFG_F ||
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -0400776 ((IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) &&
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700777 le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
778 && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
779 elreq.options == EXTERNAL_LOOPBACK) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700780 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700781 ql_dbg(ql_dbg_user, vha, 0x701e,
782 "BSG request type: %s.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700783 command_sent = INT_DEF_LB_ECHO_CMD;
784 rval = qla2x00_echo_test(vha, &elreq, response);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700785 } else {
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -0400786 if (IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700787 memset(config, 0, sizeof(config));
788 memset(new_config, 0, sizeof(new_config));
Chad Dupuisf356bef2013-02-08 01:58:04 -0500789
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700790 if (qla81xx_get_port_config(vha, config)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700791 ql_log(ql_log_warn, vha, 0x701f,
792 "Get port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700793 rval = -EPERM;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500794 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700795 }
796
Chad Dupuis1bcc46c2013-02-08 01:57:46 -0500797 if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
798 ql_dbg(ql_dbg_user, vha, 0x70c4,
799 "Loopback operation already in "
800 "progress.\n");
801 rval = -EAGAIN;
802 goto done_free_dma_rsp;
803 }
804
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400805 ql_dbg(ql_dbg_user, vha, 0x70c0,
806 "elreq.options=%04x\n", elreq.options);
807
808 if (elreq.options == EXTERNAL_LOOPBACK)
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -0400809 if (IS_QLA8031(ha) || IS_QLA8044(ha))
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400810 rval = qla81xx_set_loopback_mode(vha,
811 config, new_config, elreq.options);
812 else
813 rval = qla81xx_reset_loopback_mode(vha,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500814 config, 1, 0);
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400815 else
816 rval = qla81xx_set_loopback_mode(vha, config,
817 new_config, elreq.options);
818
819 if (rval) {
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400820 rval = -EPERM;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500821 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700822 }
823
824 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700825 ql_dbg(ql_dbg_user, vha, 0x7028,
826 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700827
828 command_sent = INT_DEF_LB_LOOPBACK_CMD;
829 rval = qla2x00_loopback_test(vha, &elreq, response);
830
Chad Dupuis992357c2013-02-08 01:57:52 -0500831 if (response[0] == MBS_COMMAND_ERROR &&
832 response[1] == MBS_LB_RESET) {
833 ql_log(ql_log_warn, vha, 0x7029,
834 "MBX command error, Aborting ISP.\n");
835 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
836 qla2xxx_wake_dpc(vha);
837 qla2x00_wait_for_chip_reset(vha);
838 /* Also reset the MPI */
839 if (IS_QLA81XX(ha)) {
840 if (qla81xx_restart_mpi_firmware(vha) !=
841 QLA_SUCCESS) {
842 ql_log(ql_log_warn, vha, 0x702a,
843 "MPI reset failed.\n");
844 }
845 }
846
847 rval = -EIO;
848 goto done_free_dma_rsp;
849 }
850
Joe Carnuccio4052bd52010-12-21 16:00:17 -0800851 if (new_config[0]) {
Chad Dupuis67b2a312013-02-08 01:57:51 -0500852 int ret;
853
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700854 /* Revert back to original port config
855 * Also clear internal loopback
856 */
Chad Dupuis67b2a312013-02-08 01:57:51 -0500857 ret = qla81xx_reset_loopback_mode(vha,
Chad Dupuisf356bef2013-02-08 01:58:04 -0500858 new_config, 0, 1);
Chad Dupuis67b2a312013-02-08 01:57:51 -0500859 if (ret) {
860 /*
861 * If the reset of the loopback mode
862 * doesn't work take FCoE dump and then
863 * reset the chip.
864 */
865 ha->isp_ops->fw_dump(vha, 0);
866 set_bit(ISP_ABORT_NEEDED,
867 &vha->dpc_flags);
868 }
869
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700870 }
871
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700872 } else {
873 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700874 ql_dbg(ql_dbg_user, vha, 0x702b,
875 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700876 command_sent = INT_DEF_LB_LOOPBACK_CMD;
877 rval = qla2x00_loopback_test(vha, &elreq, response);
878 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700879 }
880
881 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700882 ql_log(ql_log_warn, vha, 0x702c,
883 "Vendor request %s failed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700884
Giridhar Malavali6e980162010-03-19 17:03:58 -0700885 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700886 bsg_job->reply->result = (DID_ERROR << 16);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500887 bsg_job->reply->reply_payload_rcv_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700888 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700889 ql_dbg(ql_dbg_user, vha, 0x702d,
890 "Vendor request %s completed.\n", type);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500891 bsg_job->reply->result = (DID_OK << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700892 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
893 bsg_job->reply_payload.sg_cnt, rsp_data,
894 rsp_data_len);
895 }
Armen Baloyan63ea9232012-11-21 02:39:53 -0500896
897 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
898 sizeof(response) + sizeof(uint8_t);
899 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
900 sizeof(struct fc_bsg_reply);
901 memcpy(fw_sts_ptr, response, sizeof(response));
902 fw_sts_ptr += sizeof(response);
903 *fw_sts_ptr = command_sent;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700904
Steve Hodgson9bceab42012-11-21 02:39:56 -0500905done_free_dma_rsp:
Giridhar Malavali6e980162010-03-19 17:03:58 -0700906 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
907 rsp_data, rsp_data_dma);
908done_free_dma_req:
909 dma_free_coherent(&ha->pdev->dev, req_data_len,
910 req_data, req_data_dma);
911done_unmap_sg:
912 dma_unmap_sg(&ha->pdev->dev,
913 bsg_job->reply_payload.sg_list,
914 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
915done_unmap_req_sg:
916 dma_unmap_sg(&ha->pdev->dev,
917 bsg_job->request_payload.sg_list,
918 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500919 if (!rval)
920 bsg_job->job_done(bsg_job);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700921 return rval;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700922}
923
924static int
925qla84xx_reset(struct fc_bsg_job *bsg_job)
926{
927 struct Scsi_Host *host = bsg_job->shost;
928 scsi_qla_host_t *vha = shost_priv(host);
929 struct qla_hw_data *ha = vha->hw;
930 int rval = 0;
931 uint32_t flag;
932
Giridhar Malavali6e980162010-03-19 17:03:58 -0700933 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700934 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700935 return -EINVAL;
936 }
937
938 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
939
940 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
941
942 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700943 ql_log(ql_log_warn, vha, 0x7030,
944 "Vendor request 84xx reset failed.\n");
Armen Baloyan63ea9232012-11-21 02:39:53 -0500945 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700946
947 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700948 ql_dbg(ql_dbg_user, vha, 0x7031,
949 "Vendor request 84xx reset completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700950 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -0500951 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700952 }
953
Giridhar Malavali6e980162010-03-19 17:03:58 -0700954 return rval;
955}
956
957static int
958qla84xx_updatefw(struct fc_bsg_job *bsg_job)
959{
960 struct Scsi_Host *host = bsg_job->shost;
961 scsi_qla_host_t *vha = shost_priv(host);
962 struct qla_hw_data *ha = vha->hw;
963 struct verify_chip_entry_84xx *mn = NULL;
964 dma_addr_t mn_dma, fw_dma;
965 void *fw_buf = NULL;
966 int rval = 0;
967 uint32_t sg_cnt;
968 uint32_t data_len;
969 uint16_t options;
970 uint32_t flag;
971 uint32_t fw_ver;
972
Giridhar Malavali6e980162010-03-19 17:03:58 -0700973 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700974 ql_dbg(ql_dbg_user, vha, 0x7032,
975 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700976 return -EINVAL;
977 }
978
979 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
980 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700981 if (!sg_cnt) {
982 ql_log(ql_log_warn, vha, 0x7033,
983 "dma_map_sg returned %d for request.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700984 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700985 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700986
987 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700988 ql_log(ql_log_warn, vha, 0x7034,
989 "DMA mapping resulted in different sg counts, "
990 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
991 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700992 rval = -EAGAIN;
993 goto done_unmap_sg;
994 }
995
996 data_len = bsg_job->request_payload.payload_len;
997 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
998 &fw_dma, GFP_KERNEL);
999 if (!fw_buf) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001000 ql_log(ql_log_warn, vha, 0x7035,
1001 "DMA alloc failed for fw_buf.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001002 rval = -ENOMEM;
1003 goto done_unmap_sg;
1004 }
1005
1006 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1007 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
1008
1009 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1010 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001011 ql_log(ql_log_warn, vha, 0x7036,
1012 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001013 rval = -ENOMEM;
1014 goto done_free_fw_buf;
1015 }
1016
1017 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1018 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
1019
1020 memset(mn, 0, sizeof(struct access_chip_84xx));
1021 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
1022 mn->entry_count = 1;
1023
1024 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
1025 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
1026 options |= VCO_DIAG_FW;
1027
1028 mn->options = cpu_to_le16(options);
1029 mn->fw_ver = cpu_to_le32(fw_ver);
1030 mn->fw_size = cpu_to_le32(data_len);
1031 mn->fw_seq_size = cpu_to_le32(data_len);
1032 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
1033 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
1034 mn->dseg_length = cpu_to_le32(data_len);
1035 mn->data_seg_cnt = cpu_to_le16(1);
1036
1037 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
1038
1039 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001040 ql_log(ql_log_warn, vha, 0x7037,
1041 "Vendor request 84xx updatefw failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001042
Armen Baloyan63ea9232012-11-21 02:39:53 -05001043 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001044 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001045 ql_dbg(ql_dbg_user, vha, 0x7038,
1046 "Vendor request 84xx updatefw completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001047
1048 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1049 bsg_job->reply->result = DID_OK;
1050 }
1051
Giridhar Malavali6e980162010-03-19 17:03:58 -07001052 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1053
1054done_free_fw_buf:
1055 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
1056
1057done_unmap_sg:
1058 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1059 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1060
Armen Baloyan63ea9232012-11-21 02:39:53 -05001061 if (!rval)
1062 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001063 return rval;
1064}
1065
1066static int
1067qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
1068{
1069 struct Scsi_Host *host = bsg_job->shost;
1070 scsi_qla_host_t *vha = shost_priv(host);
1071 struct qla_hw_data *ha = vha->hw;
1072 struct access_chip_84xx *mn = NULL;
1073 dma_addr_t mn_dma, mgmt_dma;
1074 void *mgmt_b = NULL;
1075 int rval = 0;
1076 struct qla_bsg_a84_mgmt *ql84_mgmt;
1077 uint32_t sg_cnt;
Harish Zunjarraod5459082010-03-19 17:04:00 -07001078 uint32_t data_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001079 uint32_t dma_direction = DMA_NONE;
1080
Giridhar Malavali6e980162010-03-19 17:03:58 -07001081 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001082 ql_log(ql_log_warn, vha, 0x703a,
1083 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001084 return -EINVAL;
1085 }
1086
Giridhar Malavali6e980162010-03-19 17:03:58 -07001087 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1088 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001089 ql_log(ql_log_warn, vha, 0x703c,
1090 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001091 return -ENOMEM;
1092 }
1093
1094 memset(mn, 0, sizeof(struct access_chip_84xx));
1095 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1096 mn->entry_count = 1;
Bart Van Asschea44c72f2013-06-25 11:27:23 -04001097 ql84_mgmt = (void *)bsg_job->request + sizeof(struct fc_bsg_request);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001098 switch (ql84_mgmt->mgmt.cmd) {
1099 case QLA84_MGMT_READ_MEM:
1100 case QLA84_MGMT_GET_INFO:
1101 sg_cnt = dma_map_sg(&ha->pdev->dev,
1102 bsg_job->reply_payload.sg_list,
1103 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1104 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001105 ql_log(ql_log_warn, vha, 0x703d,
1106 "dma_map_sg returned %d for reply.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001107 rval = -ENOMEM;
1108 goto exit_mgmt;
1109 }
1110
1111 dma_direction = DMA_FROM_DEVICE;
1112
1113 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001114 ql_log(ql_log_warn, vha, 0x703e,
1115 "DMA mapping resulted in different sg counts, "
1116 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1117 bsg_job->reply_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001118 rval = -EAGAIN;
1119 goto done_unmap_sg;
1120 }
1121
1122 data_len = bsg_job->reply_payload.payload_len;
1123
1124 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1125 &mgmt_dma, GFP_KERNEL);
1126 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001127 ql_log(ql_log_warn, vha, 0x703f,
1128 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001129 rval = -ENOMEM;
1130 goto done_unmap_sg;
1131 }
1132
1133 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1134 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1135 mn->parameter1 =
1136 cpu_to_le32(
1137 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1138
1139 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1140 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1141 mn->parameter1 =
1142 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1143
1144 mn->parameter2 =
1145 cpu_to_le32(
1146 ql84_mgmt->mgmt.mgmtp.u.info.context);
1147 }
1148 break;
1149
1150 case QLA84_MGMT_WRITE_MEM:
1151 sg_cnt = dma_map_sg(&ha->pdev->dev,
1152 bsg_job->request_payload.sg_list,
1153 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1154
1155 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001156 ql_log(ql_log_warn, vha, 0x7040,
1157 "dma_map_sg returned %d.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001158 rval = -ENOMEM;
1159 goto exit_mgmt;
1160 }
1161
1162 dma_direction = DMA_TO_DEVICE;
1163
1164 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001165 ql_log(ql_log_warn, vha, 0x7041,
1166 "DMA mapping resulted in different sg counts, "
1167 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1168 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001169 rval = -EAGAIN;
1170 goto done_unmap_sg;
1171 }
1172
1173 data_len = bsg_job->request_payload.payload_len;
1174 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1175 &mgmt_dma, GFP_KERNEL);
1176 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001177 ql_log(ql_log_warn, vha, 0x7042,
1178 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001179 rval = -ENOMEM;
1180 goto done_unmap_sg;
1181 }
1182
1183 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1184 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1185
1186 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1187 mn->parameter1 =
1188 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1189 break;
1190
1191 case QLA84_MGMT_CHNG_CONFIG:
1192 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1193 mn->parameter1 =
1194 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1195
1196 mn->parameter2 =
1197 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1198
1199 mn->parameter3 =
1200 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1201 break;
1202
1203 default:
1204 rval = -EIO;
1205 goto exit_mgmt;
1206 }
1207
1208 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1209 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1210 mn->dseg_count = cpu_to_le16(1);
1211 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1212 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1213 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1214 }
1215
1216 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1217
1218 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001219 ql_log(ql_log_warn, vha, 0x7043,
1220 "Vendor request 84xx mgmt failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001221
Armen Baloyan63ea9232012-11-21 02:39:53 -05001222 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001223
1224 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001225 ql_dbg(ql_dbg_user, vha, 0x7044,
1226 "Vendor request 84xx mgmt completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001227
1228 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1229 bsg_job->reply->result = DID_OK;
1230
1231 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1232 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1233 bsg_job->reply->reply_payload_rcv_len =
1234 bsg_job->reply_payload.payload_len;
1235
1236 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001237 bsg_job->reply_payload.sg_cnt, mgmt_b,
1238 data_len);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001239 }
1240 }
1241
Giridhar Malavali6e980162010-03-19 17:03:58 -07001242done_unmap_sg:
Harish Zunjarraod5459082010-03-19 17:04:00 -07001243 if (mgmt_b)
1244 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1245
Giridhar Malavali6e980162010-03-19 17:03:58 -07001246 if (dma_direction == DMA_TO_DEVICE)
1247 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1248 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1249 else if (dma_direction == DMA_FROM_DEVICE)
1250 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1251 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1252
1253exit_mgmt:
1254 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1255
Armen Baloyan63ea9232012-11-21 02:39:53 -05001256 if (!rval)
1257 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001258 return rval;
1259}
1260
1261static int
1262qla24xx_iidma(struct fc_bsg_job *bsg_job)
1263{
1264 struct Scsi_Host *host = bsg_job->shost;
1265 scsi_qla_host_t *vha = shost_priv(host);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001266 int rval = 0;
1267 struct qla_port_param *port_param = NULL;
1268 fc_port_t *fcport = NULL;
1269 uint16_t mb[MAILBOX_REGISTER_COUNT];
1270 uint8_t *rsp_ptr = NULL;
1271
Giridhar Malavali6e980162010-03-19 17:03:58 -07001272 if (!IS_IIDMA_CAPABLE(vha->hw)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001273 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001274 return -EINVAL;
1275 }
1276
Bart Van Assche090fc2e2013-06-25 11:27:22 -04001277 port_param = (void *)bsg_job->request + sizeof(struct fc_bsg_request);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001278 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001279 ql_log(ql_log_warn, vha, 0x7048,
1280 "Invalid destination type.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001281 return -EINVAL;
1282 }
1283
1284 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1285 if (fcport->port_type != FCT_TARGET)
1286 continue;
1287
1288 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1289 fcport->port_name, sizeof(fcport->port_name)))
1290 continue;
1291 break;
1292 }
1293
1294 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001295 ql_log(ql_log_warn, vha, 0x7049,
1296 "Failed to find port.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001297 return -EINVAL;
1298 }
1299
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07001300 if (atomic_read(&fcport->state) != FCS_ONLINE) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001301 ql_log(ql_log_warn, vha, 0x704a,
1302 "Port is not online.\n");
Madhuranath Iyengar17cf2c52010-07-23 15:28:22 +05001303 return -EINVAL;
1304 }
1305
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001306 if (fcport->flags & FCF_LOGIN_NEEDED) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001307 ql_log(ql_log_warn, vha, 0x704b,
1308 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001309 return -EINVAL;
1310 }
1311
Giridhar Malavali6e980162010-03-19 17:03:58 -07001312 if (port_param->mode)
1313 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1314 port_param->speed, mb);
1315 else
1316 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1317 &port_param->speed, mb);
1318
1319 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001320 ql_log(ql_log_warn, vha, 0x704c,
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -04001321 "iIDMA cmd failed for %8phN -- "
1322 "%04x %x %04x %04x.\n", fcport->port_name,
1323 rval, fcport->fp_speed, mb[0], mb[1]);
Armen Baloyan63ea9232012-11-21 02:39:53 -05001324 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001325 } else {
1326 if (!port_param->mode) {
1327 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1328 sizeof(struct qla_port_param);
1329
1330 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1331 sizeof(struct fc_bsg_reply);
1332
1333 memcpy(rsp_ptr, port_param,
1334 sizeof(struct qla_port_param));
1335 }
1336
1337 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -05001338 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001339 }
1340
Giridhar Malavali6e980162010-03-19 17:03:58 -07001341 return rval;
1342}
1343
1344static int
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001345qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001346 uint8_t is_update)
1347{
1348 uint32_t start = 0;
1349 int valid = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001350 struct qla_hw_data *ha = vha->hw;
Harish Zunjarraof19af162010-10-15 11:27:43 -07001351
Harish Zunjarraof19af162010-10-15 11:27:43 -07001352 if (unlikely(pci_channel_offline(ha->pdev)))
1353 return -EINVAL;
1354
1355 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001356 if (start > ha->optrom_size) {
1357 ql_log(ql_log_warn, vha, 0x7055,
1358 "start %d > optrom_size %d.\n", start, ha->optrom_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001359 return -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001360 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001361
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001362 if (ha->optrom_state != QLA_SWAITING) {
1363 ql_log(ql_log_info, vha, 0x7056,
1364 "optrom_state %d.\n", ha->optrom_state);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001365 return -EBUSY;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001366 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001367
1368 ha->optrom_region_start = start;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001369 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001370 if (is_update) {
1371 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1372 valid = 1;
1373 else if (start == (ha->flt_region_boot * 4) ||
1374 start == (ha->flt_region_fw * 4))
1375 valid = 1;
1376 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001377 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
Harish Zunjarraof19af162010-10-15 11:27:43 -07001378 valid = 1;
1379 if (!valid) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001380 ql_log(ql_log_warn, vha, 0x7058,
1381 "Invalid start region 0x%x/0x%x.\n", start,
1382 bsg_job->request_payload.payload_len);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001383 return -EINVAL;
1384 }
1385
1386 ha->optrom_region_size = start +
1387 bsg_job->request_payload.payload_len > ha->optrom_size ?
1388 ha->optrom_size - start :
1389 bsg_job->request_payload.payload_len;
1390 ha->optrom_state = QLA_SWRITING;
1391 } else {
1392 ha->optrom_region_size = start +
1393 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1394 ha->optrom_size - start :
1395 bsg_job->reply_payload.payload_len;
1396 ha->optrom_state = QLA_SREADING;
1397 }
1398
1399 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1400 if (!ha->optrom_buffer) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001401 ql_log(ql_log_warn, vha, 0x7059,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001402 "Read: Unable to allocate memory for optrom retrieval "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001403 "(%x)\n", ha->optrom_region_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001404
1405 ha->optrom_state = QLA_SWAITING;
1406 return -ENOMEM;
1407 }
1408
1409 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1410 return 0;
1411}
1412
1413static int
1414qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1415{
1416 struct Scsi_Host *host = bsg_job->shost;
1417 scsi_qla_host_t *vha = shost_priv(host);
1418 struct qla_hw_data *ha = vha->hw;
1419 int rval = 0;
1420
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001421 if (ha->flags.nic_core_reset_hdlr_active)
Giridhar Malavalia49393f2012-04-25 07:26:14 -07001422 return -EBUSY;
1423
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001424 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001425 if (rval)
1426 return rval;
1427
1428 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1429 ha->optrom_region_start, ha->optrom_region_size);
1430
1431 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1432 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1433 ha->optrom_region_size);
1434
1435 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1436 bsg_job->reply->result = DID_OK;
1437 vfree(ha->optrom_buffer);
1438 ha->optrom_buffer = NULL;
1439 ha->optrom_state = QLA_SWAITING;
1440 bsg_job->job_done(bsg_job);
1441 return rval;
1442}
1443
1444static int
1445qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1446{
1447 struct Scsi_Host *host = bsg_job->shost;
1448 scsi_qla_host_t *vha = shost_priv(host);
1449 struct qla_hw_data *ha = vha->hw;
1450 int rval = 0;
1451
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001452 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001453 if (rval)
1454 return rval;
1455
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04001456 /* Set the isp82xx_no_md_cap not to capture minidump */
1457 ha->flags.isp82xx_no_md_cap = 1;
1458
Harish Zunjarraof19af162010-10-15 11:27:43 -07001459 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1460 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1461 ha->optrom_region_size);
1462
1463 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1464 ha->optrom_region_start, ha->optrom_region_size);
1465
1466 bsg_job->reply->result = DID_OK;
1467 vfree(ha->optrom_buffer);
1468 ha->optrom_buffer = NULL;
1469 ha->optrom_state = QLA_SWAITING;
1470 bsg_job->job_done(bsg_job);
1471 return rval;
1472}
1473
1474static int
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001475qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1476{
1477 struct Scsi_Host *host = bsg_job->shost;
1478 scsi_qla_host_t *vha = shost_priv(host);
1479 struct qla_hw_data *ha = vha->hw;
1480 int rval = 0;
1481 uint8_t bsg[DMA_POOL_SIZE];
1482 struct qla_image_version_list *list = (void *)bsg;
1483 struct qla_image_version *image;
1484 uint32_t count;
1485 dma_addr_t sfp_dma;
1486 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1487 if (!sfp) {
1488 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1489 EXT_STATUS_NO_MEMORY;
1490 goto done;
1491 }
1492
1493 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1494 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1495
1496 image = list->version;
1497 count = list->count;
1498 while (count--) {
1499 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1500 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1501 image->field_address.device, image->field_address.offset,
1502 sizeof(image->field_info), image->field_address.option);
1503 if (rval) {
1504 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1505 EXT_STATUS_MAILBOX;
1506 goto dealloc;
1507 }
1508 image++;
1509 }
1510
1511 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1512
1513dealloc:
1514 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1515
1516done:
1517 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1518 bsg_job->reply->result = DID_OK << 16;
1519 bsg_job->job_done(bsg_job);
1520
1521 return 0;
1522}
1523
1524static int
1525qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1526{
1527 struct Scsi_Host *host = bsg_job->shost;
1528 scsi_qla_host_t *vha = shost_priv(host);
1529 struct qla_hw_data *ha = vha->hw;
1530 int rval = 0;
1531 uint8_t bsg[DMA_POOL_SIZE];
1532 struct qla_status_reg *sr = (void *)bsg;
1533 dma_addr_t sfp_dma;
1534 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1535 if (!sfp) {
1536 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1537 EXT_STATUS_NO_MEMORY;
1538 goto done;
1539 }
1540
1541 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1542 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1543
1544 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1545 sr->field_address.device, sr->field_address.offset,
1546 sizeof(sr->status_reg), sr->field_address.option);
1547 sr->status_reg = *sfp;
1548
1549 if (rval) {
1550 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1551 EXT_STATUS_MAILBOX;
1552 goto dealloc;
1553 }
1554
1555 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1556 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1557
1558 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1559
1560dealloc:
1561 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1562
1563done:
1564 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1565 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1566 bsg_job->reply->result = DID_OK << 16;
1567 bsg_job->job_done(bsg_job);
1568
1569 return 0;
1570}
1571
1572static int
1573qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1574{
1575 struct Scsi_Host *host = bsg_job->shost;
1576 scsi_qla_host_t *vha = shost_priv(host);
1577 struct qla_hw_data *ha = vha->hw;
1578 int rval = 0;
1579 uint8_t bsg[DMA_POOL_SIZE];
1580 struct qla_status_reg *sr = (void *)bsg;
1581 dma_addr_t sfp_dma;
1582 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1583 if (!sfp) {
1584 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1585 EXT_STATUS_NO_MEMORY;
1586 goto done;
1587 }
1588
1589 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1590 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1591
1592 *sfp = sr->status_reg;
1593 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1594 sr->field_address.device, sr->field_address.offset,
1595 sizeof(sr->status_reg), sr->field_address.option);
1596
1597 if (rval) {
1598 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1599 EXT_STATUS_MAILBOX;
1600 goto dealloc;
1601 }
1602
1603 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1604
1605dealloc:
1606 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1607
1608done:
1609 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1610 bsg_job->reply->result = DID_OK << 16;
1611 bsg_job->job_done(bsg_job);
1612
1613 return 0;
1614}
1615
1616static int
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04001617qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
1618{
1619 struct Scsi_Host *host = bsg_job->shost;
1620 scsi_qla_host_t *vha = shost_priv(host);
1621 struct qla_hw_data *ha = vha->hw;
1622 int rval = 0;
1623 uint8_t bsg[DMA_POOL_SIZE];
1624 struct qla_i2c_access *i2c = (void *)bsg;
1625 dma_addr_t sfp_dma;
1626 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1627 if (!sfp) {
1628 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1629 EXT_STATUS_NO_MEMORY;
1630 goto done;
1631 }
1632
1633 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1634 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1635
1636 memcpy(sfp, i2c->buffer, i2c->length);
1637 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1638 i2c->device, i2c->offset, i2c->length, i2c->option);
1639
1640 if (rval) {
1641 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1642 EXT_STATUS_MAILBOX;
1643 goto dealloc;
1644 }
1645
1646 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1647
1648dealloc:
1649 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1650
1651done:
1652 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1653 bsg_job->reply->result = DID_OK << 16;
1654 bsg_job->job_done(bsg_job);
1655
1656 return 0;
1657}
1658
1659static int
1660qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
1661{
1662 struct Scsi_Host *host = bsg_job->shost;
1663 scsi_qla_host_t *vha = shost_priv(host);
1664 struct qla_hw_data *ha = vha->hw;
1665 int rval = 0;
1666 uint8_t bsg[DMA_POOL_SIZE];
1667 struct qla_i2c_access *i2c = (void *)bsg;
1668 dma_addr_t sfp_dma;
1669 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1670 if (!sfp) {
1671 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1672 EXT_STATUS_NO_MEMORY;
1673 goto done;
1674 }
1675
1676 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1677 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1678
1679 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1680 i2c->device, i2c->offset, i2c->length, i2c->option);
1681
1682 if (rval) {
1683 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1684 EXT_STATUS_MAILBOX;
1685 goto dealloc;
1686 }
1687
1688 memcpy(i2c->buffer, sfp, i2c->length);
1689 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1690 bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1691
1692 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1693
1694dealloc:
1695 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1696
1697done:
1698 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1699 bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
1700 bsg_job->reply->result = DID_OK << 16;
1701 bsg_job->job_done(bsg_job);
1702
1703 return 0;
1704}
1705
1706static int
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04001707qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
1708{
1709 struct Scsi_Host *host = bsg_job->shost;
1710 scsi_qla_host_t *vha = shost_priv(host);
1711 struct qla_hw_data *ha = vha->hw;
1712 uint16_t thread_id;
1713 uint32_t rval = EXT_STATUS_OK;
1714 uint16_t req_sg_cnt = 0;
1715 uint16_t rsp_sg_cnt = 0;
1716 uint16_t nextlid = 0;
1717 uint32_t tot_dsds;
1718 srb_t *sp = NULL;
1719 uint32_t req_data_len = 0;
1720 uint32_t rsp_data_len = 0;
1721
1722 /* Check the type of the adapter */
1723 if (!IS_BIDI_CAPABLE(ha)) {
1724 ql_log(ql_log_warn, vha, 0x70a0,
1725 "This adapter is not supported\n");
1726 rval = EXT_STATUS_NOT_SUPPORTED;
1727 goto done;
1728 }
1729
1730 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1731 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1732 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1733 rval = EXT_STATUS_BUSY;
1734 goto done;
1735 }
1736
1737 /* Check if host is online */
1738 if (!vha->flags.online) {
1739 ql_log(ql_log_warn, vha, 0x70a1,
1740 "Host is not online\n");
1741 rval = EXT_STATUS_DEVICE_OFFLINE;
1742 goto done;
1743 }
1744
1745 /* Check if cable is plugged in or not */
1746 if (vha->device_flags & DFLG_NO_CABLE) {
1747 ql_log(ql_log_warn, vha, 0x70a2,
1748 "Cable is unplugged...\n");
1749 rval = EXT_STATUS_INVALID_CFG;
1750 goto done;
1751 }
1752
1753 /* Check if the switch is connected or not */
1754 if (ha->current_topology != ISP_CFG_F) {
1755 ql_log(ql_log_warn, vha, 0x70a3,
1756 "Host is not connected to the switch\n");
1757 rval = EXT_STATUS_INVALID_CFG;
1758 goto done;
1759 }
1760
1761 /* Check if operating mode is P2P */
1762 if (ha->operating_mode != P2P) {
1763 ql_log(ql_log_warn, vha, 0x70a4,
1764 "Host is operating mode is not P2p\n");
1765 rval = EXT_STATUS_INVALID_CFG;
1766 goto done;
1767 }
1768
1769 thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1770
1771 mutex_lock(&ha->selflogin_lock);
1772 if (vha->self_login_loop_id == 0) {
1773 /* Initialize all required fields of fcport */
1774 vha->bidir_fcport.vha = vha;
1775 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1776 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1777 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1778 vha->bidir_fcport.loop_id = vha->loop_id;
1779
1780 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1781 ql_log(ql_log_warn, vha, 0x70a7,
1782 "Failed to login port %06X for bidirectional IOCB\n",
1783 vha->bidir_fcport.d_id.b24);
1784 mutex_unlock(&ha->selflogin_lock);
1785 rval = EXT_STATUS_MAILBOX;
1786 goto done;
1787 }
1788 vha->self_login_loop_id = nextlid - 1;
1789
1790 }
1791 /* Assign the self login loop id to fcport */
1792 mutex_unlock(&ha->selflogin_lock);
1793
1794 vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1795
1796 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1797 bsg_job->request_payload.sg_list,
1798 bsg_job->request_payload.sg_cnt,
1799 DMA_TO_DEVICE);
1800
1801 if (!req_sg_cnt) {
1802 rval = EXT_STATUS_NO_MEMORY;
1803 goto done;
1804 }
1805
1806 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1807 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1808 DMA_FROM_DEVICE);
1809
1810 if (!rsp_sg_cnt) {
1811 rval = EXT_STATUS_NO_MEMORY;
1812 goto done_unmap_req_sg;
1813 }
1814
1815 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
1816 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1817 ql_dbg(ql_dbg_user, vha, 0x70a9,
1818 "Dma mapping resulted in different sg counts "
1819 "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1820 "%x dma_reply_sg_cnt: %x]\n",
1821 bsg_job->request_payload.sg_cnt, req_sg_cnt,
1822 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1823 rval = EXT_STATUS_NO_MEMORY;
1824 goto done_unmap_sg;
1825 }
1826
1827 if (req_data_len != rsp_data_len) {
1828 rval = EXT_STATUS_BUSY;
1829 ql_log(ql_log_warn, vha, 0x70aa,
1830 "req_data_len != rsp_data_len\n");
1831 goto done_unmap_sg;
1832 }
1833
1834 req_data_len = bsg_job->request_payload.payload_len;
1835 rsp_data_len = bsg_job->reply_payload.payload_len;
1836
1837
1838 /* Alloc SRB structure */
1839 sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1840 if (!sp) {
1841 ql_dbg(ql_dbg_user, vha, 0x70ac,
1842 "Alloc SRB structure failed\n");
1843 rval = EXT_STATUS_NO_MEMORY;
1844 goto done_unmap_sg;
1845 }
1846
1847 /*Populate srb->ctx with bidir ctx*/
1848 sp->u.bsg_job = bsg_job;
1849 sp->free = qla2x00_bsg_sp_free;
1850 sp->type = SRB_BIDI_CMD;
1851 sp->done = qla2x00_bsg_job_done;
1852
1853 /* Add the read and write sg count */
1854 tot_dsds = rsp_sg_cnt + req_sg_cnt;
1855
1856 rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1857 if (rval != EXT_STATUS_OK)
1858 goto done_free_srb;
1859 /* the bsg request will be completed in the interrupt handler */
1860 return rval;
1861
1862done_free_srb:
1863 mempool_free(sp, ha->srb_mempool);
1864done_unmap_sg:
1865 dma_unmap_sg(&ha->pdev->dev,
1866 bsg_job->reply_payload.sg_list,
1867 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1868done_unmap_req_sg:
1869 dma_unmap_sg(&ha->pdev->dev,
1870 bsg_job->request_payload.sg_list,
1871 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1872done:
1873
1874 /* Return an error vendor specific response
1875 * and complete the bsg request
1876 */
1877 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1878 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1879 bsg_job->reply->reply_payload_rcv_len = 0;
1880 bsg_job->reply->result = (DID_OK) << 16;
1881 bsg_job->job_done(bsg_job);
1882 /* Always retrun success, vendor rsp carries correct status */
1883 return 0;
1884}
1885
1886static int
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04001887qlafx00_mgmt_cmd(struct fc_bsg_job *bsg_job)
1888{
1889 struct Scsi_Host *host = bsg_job->shost;
1890 scsi_qla_host_t *vha = shost_priv(host);
1891 struct qla_hw_data *ha = vha->hw;
1892 int rval = (DRIVER_ERROR << 16);
1893 struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
1894 srb_t *sp;
1895 int req_sg_cnt = 0, rsp_sg_cnt = 0;
1896 struct fc_port *fcport;
1897 char *type = "FC_BSG_HST_FX_MGMT";
1898
1899 /* Copy the IOCB specific information */
1900 piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
1901 &bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1902
1903 /* Dump the vendor information */
1904 ql_dump_buffer(ql_dbg_user + ql_dbg_verbose , vha, 0x70cf,
1905 (uint8_t *)piocb_rqst, sizeof(struct qla_mt_iocb_rqst_fx00));
1906
1907 if (!vha->flags.online) {
1908 ql_log(ql_log_warn, vha, 0x70d0,
1909 "Host is not online.\n");
1910 rval = -EIO;
1911 goto done;
1912 }
1913
1914 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID) {
1915 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1916 bsg_job->request_payload.sg_list,
1917 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1918 if (!req_sg_cnt) {
1919 ql_log(ql_log_warn, vha, 0x70c7,
1920 "dma_map_sg return %d for request\n", req_sg_cnt);
1921 rval = -ENOMEM;
1922 goto done;
1923 }
1924 }
1925
1926 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID) {
1927 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1928 bsg_job->reply_payload.sg_list,
1929 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1930 if (!rsp_sg_cnt) {
1931 ql_log(ql_log_warn, vha, 0x70c8,
1932 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
1933 rval = -ENOMEM;
1934 goto done_unmap_req_sg;
1935 }
1936 }
1937
1938 ql_dbg(ql_dbg_user, vha, 0x70c9,
1939 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
1940 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
1941 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1942
1943 /* Allocate a dummy fcport structure, since functions preparing the
1944 * IOCB and mailbox command retrieves port specific information
1945 * from fcport structure. For Host based ELS commands there will be
1946 * no fcport structure allocated
1947 */
1948 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1949 if (!fcport) {
1950 ql_log(ql_log_warn, vha, 0x70ca,
1951 "Failed to allocate fcport.\n");
1952 rval = -ENOMEM;
1953 goto done_unmap_rsp_sg;
1954 }
1955
1956 /* Alloc SRB structure */
1957 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1958 if (!sp) {
1959 ql_log(ql_log_warn, vha, 0x70cb,
1960 "qla2x00_get_sp failed.\n");
1961 rval = -ENOMEM;
1962 goto done_free_fcport;
1963 }
1964
1965 /* Initialize all required fields of fcport */
1966 fcport->vha = vha;
1967 fcport->loop_id = piocb_rqst->dataword;
1968
1969 sp->type = SRB_FXIOCB_BCMD;
1970 sp->name = "bsg_fx_mgmt";
1971 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
1972 sp->u.bsg_job = bsg_job;
1973 sp->free = qla2x00_bsg_sp_free;
1974 sp->done = qla2x00_bsg_job_done;
1975
1976 ql_dbg(ql_dbg_user, vha, 0x70cc,
1977 "bsg rqst type: %s fx_mgmt_type: %x id=%x\n",
1978 type, piocb_rqst->func_type, fcport->loop_id);
1979
1980 rval = qla2x00_start_sp(sp);
1981 if (rval != QLA_SUCCESS) {
1982 ql_log(ql_log_warn, vha, 0x70cd,
1983 "qla2x00_start_sp failed=%d.\n", rval);
1984 mempool_free(sp, ha->srb_mempool);
1985 rval = -EIO;
1986 goto done_free_fcport;
1987 }
1988 return rval;
1989
1990done_free_fcport:
1991 kfree(fcport);
1992
1993done_unmap_rsp_sg:
1994 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
1995 dma_unmap_sg(&ha->pdev->dev,
1996 bsg_job->reply_payload.sg_list,
1997 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1998done_unmap_req_sg:
1999 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
2000 dma_unmap_sg(&ha->pdev->dev,
2001 bsg_job->request_payload.sg_list,
2002 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2003
2004done:
2005 return rval;
2006}
2007
2008static int
Giridhar Malavali6e980162010-03-19 17:03:58 -07002009qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
2010{
2011 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
2012 case QL_VND_LOOPBACK:
2013 return qla2x00_process_loopback(bsg_job);
2014
2015 case QL_VND_A84_RESET:
2016 return qla84xx_reset(bsg_job);
2017
2018 case QL_VND_A84_UPDATE_FW:
2019 return qla84xx_updatefw(bsg_job);
2020
2021 case QL_VND_A84_MGMT_CMD:
2022 return qla84xx_mgmt_cmd(bsg_job);
2023
2024 case QL_VND_IIDMA:
2025 return qla24xx_iidma(bsg_job);
2026
Sarang Radke09ff7012010-03-19 17:03:59 -07002027 case QL_VND_FCP_PRIO_CFG_CMD:
2028 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
2029
Harish Zunjarraof19af162010-10-15 11:27:43 -07002030 case QL_VND_READ_FLASH:
2031 return qla2x00_read_optrom(bsg_job);
2032
2033 case QL_VND_UPDATE_FLASH:
2034 return qla2x00_update_optrom(bsg_job);
2035
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07002036 case QL_VND_SET_FRU_VERSION:
2037 return qla2x00_update_fru_versions(bsg_job);
2038
2039 case QL_VND_READ_FRU_STATUS:
2040 return qla2x00_read_fru_status(bsg_job);
2041
2042 case QL_VND_WRITE_FRU_STATUS:
2043 return qla2x00_write_fru_status(bsg_job);
2044
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04002045 case QL_VND_WRITE_I2C:
2046 return qla2x00_write_i2c(bsg_job);
2047
2048 case QL_VND_READ_I2C:
2049 return qla2x00_read_i2c(bsg_job);
2050
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04002051 case QL_VND_DIAG_IO_CMD:
2052 return qla24xx_process_bidir_cmd(bsg_job);
2053
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04002054 case QL_VND_FX00_MGMT_CMD:
2055 return qlafx00_mgmt_cmd(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07002056 default:
Giridhar Malavali6e980162010-03-19 17:03:58 -07002057 return -ENOSYS;
2058 }
2059}
2060
2061int
2062qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
2063{
2064 int ret = -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002065 struct fc_rport *rport;
2066 fc_port_t *fcport = NULL;
2067 struct Scsi_Host *host;
2068 scsi_qla_host_t *vha;
2069
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -08002070 /* In case no data transferred. */
2071 bsg_job->reply->reply_payload_rcv_len = 0;
2072
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002073 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
2074 rport = bsg_job->rport;
2075 fcport = *(fc_port_t **) rport->dd_data;
2076 host = rport_to_shost(rport);
2077 vha = shost_priv(host);
2078 } else {
2079 host = bsg_job->shost;
2080 vha = shost_priv(host);
2081 }
2082
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08002083 if (qla2x00_reset_active(vha)) {
2084 ql_dbg(ql_dbg_user, vha, 0x709f,
2085 "BSG: ISP abort active/needed -- cmd=%d.\n",
2086 bsg_job->request->msgcode);
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08002087 return -EBUSY;
2088 }
2089
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002090 ql_dbg(ql_dbg_user, vha, 0x7000,
Chad Dupuiscfb09192011-11-18 09:03:07 -08002091 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
Giridhar Malavali6e980162010-03-19 17:03:58 -07002092
2093 switch (bsg_job->request->msgcode) {
2094 case FC_BSG_RPT_ELS:
2095 case FC_BSG_HST_ELS_NOLOGIN:
2096 ret = qla2x00_process_els(bsg_job);
2097 break;
2098 case FC_BSG_HST_CT:
2099 ret = qla2x00_process_ct(bsg_job);
2100 break;
2101 case FC_BSG_HST_VENDOR:
2102 ret = qla2x00_process_vendor_specific(bsg_job);
2103 break;
2104 case FC_BSG_HST_ADD_RPORT:
2105 case FC_BSG_HST_DEL_RPORT:
2106 case FC_BSG_RPT_CT:
2107 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002108 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07002109 break;
Andrew Vasquez6c452a42010-03-19 17:04:02 -07002110 }
Giridhar Malavali6e980162010-03-19 17:03:58 -07002111 return ret;
2112}
2113
2114int
2115qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
2116{
2117 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
2118 struct qla_hw_data *ha = vha->hw;
2119 srb_t *sp;
2120 int cnt, que;
2121 unsigned long flags;
2122 struct req_que *req;
Giridhar Malavali6e980162010-03-19 17:03:58 -07002123
2124 /* find the bsg job from the active list of commands */
2125 spin_lock_irqsave(&ha->hardware_lock, flags);
2126 for (que = 0; que < ha->max_req_queues; que++) {
2127 req = ha->req_q_map[que];
2128 if (!req)
2129 continue;
2130
Chad Dupuis8d93f552013-01-30 03:34:37 -05002131 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
Giridhar Malavali6e980162010-03-19 17:03:58 -07002132 sp = req->outstanding_cmds[cnt];
Giridhar Malavali6e980162010-03-19 17:03:58 -07002133 if (sp) {
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002134 if (((sp->type == SRB_CT_CMD) ||
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04002135 (sp->type == SRB_ELS_CMD_HST) ||
2136 (sp->type == SRB_FXIOCB_BCMD))
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002137 && (sp->u.bsg_job == bsg_job)) {
Giridhar Malavali8edf3ed2013-06-25 11:27:17 -04002138 req->outstanding_cmds[cnt] = NULL;
Giridhar Malavali900a36e2010-12-21 16:00:26 -08002139 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07002140 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002141 ql_log(ql_log_warn, vha, 0x7089,
2142 "mbx abort_command "
2143 "failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07002144 bsg_job->req->errors =
2145 bsg_job->reply->result = -EIO;
2146 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002147 ql_dbg(ql_dbg_user, vha, 0x708a,
2148 "mbx abort_command "
2149 "success.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07002150 bsg_job->req->errors =
2151 bsg_job->reply->result = 0;
2152 }
Giridhar Malavali900a36e2010-12-21 16:00:26 -08002153 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07002154 goto done;
2155 }
2156 }
2157 }
2158 }
2159 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002160 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07002161 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
2162 return 0;
2163
2164done:
2165 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavali8edf3ed2013-06-25 11:27:17 -04002166 sp->free(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -07002167 return 0;
2168}