[SCSI] qla2xxx: Add clarifying printk to thermal access fail cases.
[linux-2.6-block.git] / drivers / scsi / qla2xxx / qla_bsg.c
CommitLineData
6e98016c
GM
1/*
2 * QLogic Fibre Channel HBA Driver
46152ceb 3 * Copyright (c) 2003-2012 QLogic Corporation
6e98016c
GM
4 *
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 */
9ba56b95
GM
14void
15qla2x00_bsg_job_done(void *data, void *ptr, int res)
6e98016c 16{
9ba56b95
GM
17 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;
b00ee7d7 30 struct scsi_qla_host *vha = sp->fcport->vha;
9ba56b95 31 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
6e98016c 32 struct qla_hw_data *ha = vha->hw;
8ae6d9c7 33 struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
6e98016c 34
8ae6d9c7
GM
35 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];
6e98016c 38
8ae6d9c7
GM
39 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 }
9ba56b95
GM
55
56 if (sp->type == SRB_CT_CMD ||
8ae6d9c7 57 sp->type == SRB_FXIOCB_BCMD ||
9ba56b95
GM
58 sp->type == SRB_ELS_CMD_HST)
59 kfree(sp->fcport);
b00ee7d7 60 qla2x00_rel_sp(vha, sp);
6e98016c
GM
61}
62
09ff701a 63int
7c3df132
SK
64qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
65 struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
09ff701a
SR
66{
67 int i, ret, num_valid;
68 uint8_t *bcode;
69 struct qla_fcp_prio_entry *pri_entry;
2f0f3f4f 70 uint32_t *bcode_val_ptr, bcode_val;
09ff701a
SR
71
72 ret = 1;
73 num_valid = 0;
74 bcode = (uint8_t *)pri_cfg;
2f0f3f4f
MI
75 bcode_val_ptr = (uint32_t *)pri_cfg;
76 bcode_val = (uint32_t)(*bcode_val_ptr);
09ff701a 77
2f0f3f4f
MI
78 if (bcode_val == 0xFFFFFFFF) {
79 /* No FCP Priority config data in flash */
7c3df132
SK
80 ql_dbg(ql_dbg_user, vha, 0x7051,
81 "No FCP Priority config data.\n");
2f0f3f4f
MI
82 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*/
7c3df132
SK
88 ql_dbg(ql_dbg_user, vha, 0x7052,
89 "Invalid FCP Priority data header. bcode=0x%x.\n",
90 bcode_val);
09ff701a
SR
91 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
2f0f3f4f
MI
103 if (num_valid == 0) {
104 /* No valid FCP priority data entries */
7c3df132
SK
105 ql_dbg(ql_dbg_user, vha, 0x7053,
106 "No valid FCP Priority data entries.\n");
09ff701a 107 ret = 0;
2f0f3f4f
MI
108 } else {
109 /* FCP priority data is valid */
7c3df132
SK
110 ql_dbg(ql_dbg_user, vha, 0x7054,
111 "Valid FCP priority data. num entries = %d.\n",
112 num_valid);
2f0f3f4f 113 }
09ff701a
SR
114
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
a00f6296 128 if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_QLA82XX(ha))) {
2f0f3f4f
MI
129 ret = -EINVAL;
130 goto exit_fcp_prio_cfg;
131 }
132
09ff701a
SR
133 /* 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) {
7c3df132
SK
200 ql_log(ql_log_warn, vha, 0x7050,
201 "Unable to allocate memory for fcp prio "
202 "config data (%x).\n", FCP_PRIO_CFG_SIZE);
09ff701a
SR
203 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 */
7c3df132
SK
215
216 if (!qla24xx_fcp_prio_cfg_valid(vha,
217 (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
09ff701a
SR
218 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:
63ea923a
AB
239 if (!ret)
240 bsg_job->job_done(bsg_job);
09ff701a
SR
241 return ret;
242}
9ba56b95 243
6e98016c
GM
244static int
245qla2x00_process_els(struct fc_bsg_job *bsg_job)
246{
247 struct fc_rport *rport;
08f71e09 248 fc_port_t *fcport = NULL;
6e98016c
GM
249 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;
6e98016c 257
08f71e09
HZ
258 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
272 /* pass through is supported only for ISP 4Gb or higher */
273 if (!IS_FWI2_CAPABLE(ha)) {
7c3df132
SK
274 ql_dbg(ql_dbg_user, vha, 0x7001,
275 "ELS passthru not supported for ISP23xx based adapters.\n");
08f71e09
HZ
276 rval = -EPERM;
277 goto done;
278 }
279
6e98016c
GM
280 /* Multiple SG's are not supported for ELS requests */
281 if (bsg_job->request_payload.sg_cnt > 1 ||
282 bsg_job->reply_payload.sg_cnt > 1) {
7c3df132
SK
283 ql_dbg(ql_dbg_user, vha, 0x7002,
284 "Multiple SG's are not suppored for ELS requests, "
285 "request_sg_cnt=%x reply_sg_cnt=%x.\n",
286 bsg_job->request_payload.sg_cnt,
287 bsg_job->reply_payload.sg_cnt);
6e98016c
GM
288 rval = -EPERM;
289 goto done;
290 }
291
292 /* ELS request for rport */
293 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
6e98016c
GM
294 /* make sure the rport is logged in,
295 * if not perform fabric login
296 */
297 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
7c3df132
SK
298 ql_dbg(ql_dbg_user, vha, 0x7003,
299 "Failed to login port %06X for ELS passthru.\n",
300 fcport->d_id.b24);
6e98016c
GM
301 rval = -EIO;
302 goto done;
303 }
304 } else {
6e98016c
GM
305 /* Allocate a dummy fcport structure, since functions
306 * preparing the IOCB and mailbox command retrieves port
307 * specific information from fcport structure. For Host based
308 * ELS commands there will be no fcport structure allocated
309 */
310 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
311 if (!fcport) {
312 rval = -ENOMEM;
313 goto done;
314 }
315
316 /* Initialize all required fields of fcport */
317 fcport->vha = vha;
6e98016c
GM
318 fcport->d_id.b.al_pa =
319 bsg_job->request->rqst_data.h_els.port_id[0];
320 fcport->d_id.b.area =
321 bsg_job->request->rqst_data.h_els.port_id[1];
322 fcport->d_id.b.domain =
323 bsg_job->request->rqst_data.h_els.port_id[2];
324 fcport->loop_id =
325 (fcport->d_id.b.al_pa == 0xFD) ?
326 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
327 }
328
329 if (!vha->flags.online) {
7c3df132 330 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
6e98016c
GM
331 rval = -EIO;
332 goto done;
333 }
334
335 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 }
6c452a45
AV
342
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);
6e98016c
GM
345 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) ||
6c452a45 351 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
352 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);
6e98016c
GM
357 rval = -EAGAIN;
358 goto done_unmap_sg;
359 }
360
361 /* Alloc SRB structure */
9ba56b95 362 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
6e98016c
GM
363 if (!sp) {
364 rval = -ENOMEM;
6c452a45 365 goto done_unmap_sg;
6e98016c
GM
366 }
367
9ba56b95 368 sp->type =
6e98016c
GM
369 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
370 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
9ba56b95 371 sp->name =
3822263e
MI
372 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
373 "bsg_els_rpt" : "bsg_els_hst");
9ba56b95
GM
374 sp->u.bsg_job = bsg_job;
375 sp->free = qla2x00_bsg_sp_free;
376 sp->done = qla2x00_bsg_job_done;
6e98016c 377
7c3df132
SK
378 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);
6e98016c
GM
383
384 rval = qla2x00_start_sp(sp);
385 if (rval != QLA_SUCCESS) {
7c3df132
SK
386 ql_log(ql_log_warn, vha, 0x700e,
387 "qla2x00_start_sp failed = %d\n", rval);
b00ee7d7 388 qla2x00_rel_sp(vha, sp);
6e98016c
GM
389 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:
402 if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
403 kfree(fcport);
404done:
405 return rval;
406}
407
5780790e
AV
408inline 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
6e98016c
GM
422static 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";
6e98016c 434
6e98016c
GM
435 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);
6c452a45 438 if (!req_sg_cnt) {
7c3df132
SK
439 ql_log(ql_log_warn, vha, 0x700f,
440 "dma_map_sg return %d for request\n", req_sg_cnt);
6e98016c
GM
441 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) {
7c3df132
SK
448 ql_log(ql_log_warn, vha, 0x7010,
449 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
6e98016c
GM
450 rval = -ENOMEM;
451 goto done;
452 }
453
454 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
6c452a45 455 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
456 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);
6e98016c 460 rval = -EAGAIN;
6c452a45 461 goto done_unmap_sg;
6e98016c
GM
462 }
463
464 if (!vha->flags.online) {
7c3df132
SK
465 ql_log(ql_log_warn, vha, 0x7012,
466 "Host is not online.\n");
6e98016c
GM
467 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) {
6c452a45
AV
475 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:
7c3df132
SK
482 ql_dbg(ql_dbg_user, vha, 0x7013,
483 "Unknown loop id: %x.\n", loop_id);
6c452a45
AV
484 rval = -EINVAL;
485 goto done_unmap_sg;
6e98016c
GM
486 }
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);
6c452a45 494 if (!fcport) {
7c3df132
SK
495 ql_log(ql_log_warn, vha, 0x7014,
496 "Failed to allocate fcport.\n");
6e98016c 497 rval = -ENOMEM;
6c452a45 498 goto done_unmap_sg;
6e98016c
GM
499 }
500
501 /* Initialize all required fields of fcport */
502 fcport->vha = vha;
6e98016c
GM
503 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 */
9ba56b95 509 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
6e98016c 510 if (!sp) {
7c3df132 511 ql_log(ql_log_warn, vha, 0x7015,
9ba56b95 512 "qla2x00_get_sp failed.\n");
6e98016c
GM
513 rval = -ENOMEM;
514 goto done_free_fcport;
515 }
516
9ba56b95
GM
517 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;
6e98016c 523
7c3df132
SK
524 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);
6e98016c
GM
530
531 rval = qla2x00_start_sp(sp);
532 if (rval != QLA_SUCCESS) {
7c3df132
SK
533 ql_log(ql_log_warn, vha, 0x7017,
534 "qla2x00_start_sp failed=%d.\n", rval);
b00ee7d7 535 qla2x00_rel_sp(vha, sp);
6e98016c
GM
536 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}
67b2a31f
CD
551
552/* Disable loopback mode */
553static inline int
554qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
f356bef1 555 int wait, int wait2)
67b2a31f
CD
556{
557 int ret = 0;
558 int rval = 0;
559 uint16_t new_config[4];
560 struct qla_hw_data *ha = vha->hw;
561
562 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
563 goto done_reset_internal;
564
565 memset(new_config, 0 , sizeof(new_config));
566 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
567 ENABLE_INTERNAL_LOOPBACK ||
568 (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
569 ENABLE_EXTERNAL_LOOPBACK) {
570 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
571 ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
572 (new_config[0] & INTERNAL_LOOPBACK_MASK));
573 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
574
575 ha->notify_dcbx_comp = wait;
f356bef1
CD
576 ha->notify_lb_portup_comp = wait2;
577
67b2a31f
CD
578 ret = qla81xx_set_port_config(vha, new_config);
579 if (ret != QLA_SUCCESS) {
580 ql_log(ql_log_warn, vha, 0x7025,
581 "Set port config failed.\n");
582 ha->notify_dcbx_comp = 0;
f356bef1 583 ha->notify_lb_portup_comp = 0;
67b2a31f
CD
584 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,
f356bef1 590 (DCBX_COMP_TIMEOUT * HZ))) {
67b2a31f 591 ql_dbg(ql_dbg_user, vha, 0x7026,
f356bef1 592 "DCBX completion not received.\n");
67b2a31f 593 ha->notify_dcbx_comp = 0;
f356bef1 594 ha->notify_lb_portup_comp = 0;
67b2a31f
CD
595 rval = -EINVAL;
596 goto done_reset_internal;
597 } else
598 ql_dbg(ql_dbg_user, vha, 0x7027,
f356bef1
CD
599 "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");
67b2a31f
CD
612
613 ha->notify_dcbx_comp = 0;
f356bef1 614 ha->notify_lb_portup_comp = 0;
67b2a31f
CD
615 }
616done_reset_internal:
617 return rval;
618}
619
8fcd6b8b
CD
620/*
621 * Set the port configuration to enable the internal or external loopback
622 * depending on the loopback mode.
23f2ebd1
SR
623 */
624static inline int
8fcd6b8b
CD
625qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
626 uint16_t *new_config, uint16_t mode)
23f2ebd1
SR
627{
628 int ret = 0;
629 int rval = 0;
630 struct qla_hw_data *ha = vha->hw;
631
f863f603 632 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
23f2ebd1
SR
633 goto done_set_internal;
634
8fcd6b8b
CD
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);
23f2ebd1
SR
643
644 ha->notify_dcbx_comp = 1;
645 ret = qla81xx_set_port_config(vha, new_config);
646 if (ret != QLA_SUCCESS) {
7c3df132
SK
647 ql_log(ql_log_warn, vha, 0x7021,
648 "set port config failed.\n");
23f2ebd1
SR
649 ha->notify_dcbx_comp = 0;
650 rval = -EINVAL;
651 goto done_set_internal;
652 }
653
654 /* Wait for DCBX complete event */
f356bef1
CD
655 if (!wait_for_completion_timeout(&ha->dcbx_comp,
656 (DCBX_COMP_TIMEOUT * HZ))) {
7c3df132 657 ql_dbg(ql_dbg_user, vha, 0x7022,
f356bef1
CD
658 "DCBX completion not received.\n");
659 ret = qla81xx_reset_loopback_mode(vha, new_config, 0, 0);
67b2a31f
CD
660 /*
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 }
bf5b8ad7
CD
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,
f356bef1 677 "DCBX completion received.\n");
bf5b8ad7 678 }
23f2ebd1
SR
679
680 ha->notify_dcbx_comp = 0;
681
682done_set_internal:
683 return rval;
684}
685
6e98016c
GM
686static 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];
23f2ebd1 697 uint16_t config[4], new_config[4];
6c452a45 698 uint8_t *fw_sts_ptr;
6e98016c
GM
699 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
6e98016c 706 if (!vha->flags.online) {
7c3df132 707 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
6e98016c
GM
708 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
7c3df132
SK
715 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);
6e98016c 718 return -ENOMEM;
7c3df132 719 }
6e98016c
GM
720
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) {
7c3df132
SK
726 ql_log(ql_log_warn, vha, 0x701b,
727 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
6e98016c
GM
728 rval = -ENOMEM;
729 goto done_unmap_req_sg;
6c452a45 730 }
6e98016c
GM
731
732 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
733 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
734 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);
6e98016c
GM
740 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) {
7c3df132
SK
747 ql_log(ql_log_warn, vha, 0x701d,
748 "dma alloc failed for req_data.\n");
6e98016c
GM
749 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) {
7c3df132
SK
756 ql_log(ql_log_warn, vha, 0x7004,
757 "dma alloc failed for rsp_data.\n");
6e98016c
GM
758 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];
771
8fcd6b8b
CD
772 if (atomic_read(&vha->loop_state) == LOOP_READY &&
773 (ha->current_topology == ISP_CFG_F ||
f863f603 774 ((IS_QLA81XX(ha) || IS_QLA8031(ha)) &&
23f2ebd1
SR
775 le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
776 && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
777 elreq.options == EXTERNAL_LOOPBACK) {
778 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
7c3df132
SK
779 ql_dbg(ql_dbg_user, vha, 0x701e,
780 "BSG request type: %s.\n", type);
23f2ebd1
SR
781 command_sent = INT_DEF_LB_ECHO_CMD;
782 rval = qla2x00_echo_test(vha, &elreq, response);
783 } else {
6246b8a1 784 if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
23f2ebd1
SR
785 memset(config, 0, sizeof(config));
786 memset(new_config, 0, sizeof(new_config));
f356bef1 787
23f2ebd1 788 if (qla81xx_get_port_config(vha, config)) {
7c3df132
SK
789 ql_log(ql_log_warn, vha, 0x701f,
790 "Get port config failed.\n");
23f2ebd1 791 rval = -EPERM;
9bceab4e 792 goto done_free_dma_rsp;
23f2ebd1
SR
793 }
794
1bcc46cb
CD
795 if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
796 ql_dbg(ql_dbg_user, vha, 0x70c4,
797 "Loopback operation already in "
798 "progress.\n");
799 rval = -EAGAIN;
800 goto done_free_dma_rsp;
801 }
802
8fcd6b8b
CD
803 ql_dbg(ql_dbg_user, vha, 0x70c0,
804 "elreq.options=%04x\n", elreq.options);
805
806 if (elreq.options == EXTERNAL_LOOPBACK)
807 if (IS_QLA8031(ha))
808 rval = qla81xx_set_loopback_mode(vha,
809 config, new_config, elreq.options);
810 else
811 rval = qla81xx_reset_loopback_mode(vha,
f356bef1 812 config, 1, 0);
8fcd6b8b
CD
813 else
814 rval = qla81xx_set_loopback_mode(vha, config,
815 new_config, elreq.options);
816
817 if (rval) {
8fcd6b8b 818 rval = -EPERM;
9bceab4e 819 goto done_free_dma_rsp;
23f2ebd1
SR
820 }
821
822 type = "FC_BSG_HST_VENDOR_LOOPBACK";
7c3df132
SK
823 ql_dbg(ql_dbg_user, vha, 0x7028,
824 "BSG request type: %s.\n", type);
23f2ebd1
SR
825
826 command_sent = INT_DEF_LB_LOOPBACK_CMD;
827 rval = qla2x00_loopback_test(vha, &elreq, response);
828
992357c6
CD
829 if (response[0] == MBS_COMMAND_ERROR &&
830 response[1] == MBS_LB_RESET) {
831 ql_log(ql_log_warn, vha, 0x7029,
832 "MBX command error, Aborting ISP.\n");
833 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
834 qla2xxx_wake_dpc(vha);
835 qla2x00_wait_for_chip_reset(vha);
836 /* Also reset the MPI */
837 if (IS_QLA81XX(ha)) {
838 if (qla81xx_restart_mpi_firmware(vha) !=
839 QLA_SUCCESS) {
840 ql_log(ql_log_warn, vha, 0x702a,
841 "MPI reset failed.\n");
842 }
843 }
844
845 rval = -EIO;
846 goto done_free_dma_rsp;
847 }
848
4052bd57 849 if (new_config[0]) {
67b2a31f
CD
850 int ret;
851
23f2ebd1
SR
852 /* Revert back to original port config
853 * Also clear internal loopback
854 */
67b2a31f 855 ret = qla81xx_reset_loopback_mode(vha,
f356bef1 856 new_config, 0, 1);
67b2a31f
CD
857 if (ret) {
858 /*
859 * If the reset of the loopback mode
860 * doesn't work take FCoE dump and then
861 * reset the chip.
862 */
863 ha->isp_ops->fw_dump(vha, 0);
864 set_bit(ISP_ABORT_NEEDED,
865 &vha->dpc_flags);
866 }
867
23f2ebd1
SR
868 }
869
23f2ebd1
SR
870 } else {
871 type = "FC_BSG_HST_VENDOR_LOOPBACK";
7c3df132
SK
872 ql_dbg(ql_dbg_user, vha, 0x702b,
873 "BSG request type: %s.\n", type);
23f2ebd1
SR
874 command_sent = INT_DEF_LB_LOOPBACK_CMD;
875 rval = qla2x00_loopback_test(vha, &elreq, response);
6e98016c 876 }
6e98016c
GM
877 }
878
879 if (rval) {
7c3df132
SK
880 ql_log(ql_log_warn, vha, 0x702c,
881 "Vendor request %s failed.\n", type);
6e98016c 882
6e98016c 883 rval = 0;
6e98016c 884 bsg_job->reply->result = (DID_ERROR << 16);
63ea923a 885 bsg_job->reply->reply_payload_rcv_len = 0;
6e98016c 886 } else {
7c3df132
SK
887 ql_dbg(ql_dbg_user, vha, 0x702d,
888 "Vendor request %s completed.\n", type);
63ea923a 889 bsg_job->reply->result = (DID_OK << 16);
6e98016c
GM
890 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
891 bsg_job->reply_payload.sg_cnt, rsp_data,
892 rsp_data_len);
893 }
63ea923a
AB
894
895 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
896 sizeof(response) + sizeof(uint8_t);
897 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
898 sizeof(struct fc_bsg_reply);
899 memcpy(fw_sts_ptr, response, sizeof(response));
900 fw_sts_ptr += sizeof(response);
901 *fw_sts_ptr = command_sent;
6e98016c 902
9bceab4e 903done_free_dma_rsp:
6e98016c
GM
904 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
905 rsp_data, rsp_data_dma);
906done_free_dma_req:
907 dma_free_coherent(&ha->pdev->dev, req_data_len,
908 req_data, req_data_dma);
909done_unmap_sg:
910 dma_unmap_sg(&ha->pdev->dev,
911 bsg_job->reply_payload.sg_list,
912 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
913done_unmap_req_sg:
914 dma_unmap_sg(&ha->pdev->dev,
915 bsg_job->request_payload.sg_list,
916 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
63ea923a
AB
917 if (!rval)
918 bsg_job->job_done(bsg_job);
6c452a45 919 return rval;
6e98016c
GM
920}
921
922static int
923qla84xx_reset(struct fc_bsg_job *bsg_job)
924{
925 struct Scsi_Host *host = bsg_job->shost;
926 scsi_qla_host_t *vha = shost_priv(host);
927 struct qla_hw_data *ha = vha->hw;
928 int rval = 0;
929 uint32_t flag;
930
6e98016c 931 if (!IS_QLA84XX(ha)) {
7c3df132 932 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
6e98016c
GM
933 return -EINVAL;
934 }
935
936 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
937
938 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
939
940 if (rval) {
7c3df132
SK
941 ql_log(ql_log_warn, vha, 0x7030,
942 "Vendor request 84xx reset failed.\n");
63ea923a 943 rval = (DID_ERROR << 16);
6e98016c
GM
944
945 } else {
7c3df132
SK
946 ql_dbg(ql_dbg_user, vha, 0x7031,
947 "Vendor request 84xx reset completed.\n");
6e98016c 948 bsg_job->reply->result = DID_OK;
63ea923a 949 bsg_job->job_done(bsg_job);
6e98016c
GM
950 }
951
6e98016c
GM
952 return rval;
953}
954
955static int
956qla84xx_updatefw(struct fc_bsg_job *bsg_job)
957{
958 struct Scsi_Host *host = bsg_job->shost;
959 scsi_qla_host_t *vha = shost_priv(host);
960 struct qla_hw_data *ha = vha->hw;
961 struct verify_chip_entry_84xx *mn = NULL;
962 dma_addr_t mn_dma, fw_dma;
963 void *fw_buf = NULL;
964 int rval = 0;
965 uint32_t sg_cnt;
966 uint32_t data_len;
967 uint16_t options;
968 uint32_t flag;
969 uint32_t fw_ver;
970
6e98016c 971 if (!IS_QLA84XX(ha)) {
7c3df132
SK
972 ql_dbg(ql_dbg_user, vha, 0x7032,
973 "Not 84xx, exiting.\n");
6e98016c
GM
974 return -EINVAL;
975 }
976
977 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
978 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
7c3df132
SK
979 if (!sg_cnt) {
980 ql_log(ql_log_warn, vha, 0x7033,
981 "dma_map_sg returned %d for request.\n", sg_cnt);
6e98016c 982 return -ENOMEM;
7c3df132 983 }
6e98016c
GM
984
985 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
7c3df132
SK
986 ql_log(ql_log_warn, vha, 0x7034,
987 "DMA mapping resulted in different sg counts, "
988 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
989 bsg_job->request_payload.sg_cnt, sg_cnt);
6e98016c
GM
990 rval = -EAGAIN;
991 goto done_unmap_sg;
992 }
993
994 data_len = bsg_job->request_payload.payload_len;
995 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
996 &fw_dma, GFP_KERNEL);
997 if (!fw_buf) {
7c3df132
SK
998 ql_log(ql_log_warn, vha, 0x7035,
999 "DMA alloc failed for fw_buf.\n");
6e98016c
GM
1000 rval = -ENOMEM;
1001 goto done_unmap_sg;
1002 }
1003
1004 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1005 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
1006
1007 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1008 if (!mn) {
7c3df132
SK
1009 ql_log(ql_log_warn, vha, 0x7036,
1010 "DMA alloc failed for fw buffer.\n");
6e98016c
GM
1011 rval = -ENOMEM;
1012 goto done_free_fw_buf;
1013 }
1014
1015 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1016 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
1017
1018 memset(mn, 0, sizeof(struct access_chip_84xx));
1019 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
1020 mn->entry_count = 1;
1021
1022 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
1023 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
1024 options |= VCO_DIAG_FW;
1025
1026 mn->options = cpu_to_le16(options);
1027 mn->fw_ver = cpu_to_le32(fw_ver);
1028 mn->fw_size = cpu_to_le32(data_len);
1029 mn->fw_seq_size = cpu_to_le32(data_len);
1030 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
1031 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
1032 mn->dseg_length = cpu_to_le32(data_len);
1033 mn->data_seg_cnt = cpu_to_le16(1);
1034
1035 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
1036
1037 if (rval) {
7c3df132
SK
1038 ql_log(ql_log_warn, vha, 0x7037,
1039 "Vendor request 84xx updatefw failed.\n");
6e98016c 1040
63ea923a 1041 rval = (DID_ERROR << 16);
6e98016c 1042 } else {
7c3df132
SK
1043 ql_dbg(ql_dbg_user, vha, 0x7038,
1044 "Vendor request 84xx updatefw completed.\n");
6e98016c
GM
1045
1046 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1047 bsg_job->reply->result = DID_OK;
1048 }
1049
6e98016c
GM
1050 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1051
1052done_free_fw_buf:
1053 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
1054
1055done_unmap_sg:
1056 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1057 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1058
63ea923a
AB
1059 if (!rval)
1060 bsg_job->job_done(bsg_job);
6e98016c
GM
1061 return rval;
1062}
1063
1064static int
1065qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
1066{
1067 struct Scsi_Host *host = bsg_job->shost;
1068 scsi_qla_host_t *vha = shost_priv(host);
1069 struct qla_hw_data *ha = vha->hw;
1070 struct access_chip_84xx *mn = NULL;
1071 dma_addr_t mn_dma, mgmt_dma;
1072 void *mgmt_b = NULL;
1073 int rval = 0;
1074 struct qla_bsg_a84_mgmt *ql84_mgmt;
1075 uint32_t sg_cnt;
d5459083 1076 uint32_t data_len = 0;
6e98016c
GM
1077 uint32_t dma_direction = DMA_NONE;
1078
6e98016c 1079 if (!IS_QLA84XX(ha)) {
7c3df132
SK
1080 ql_log(ql_log_warn, vha, 0x703a,
1081 "Not 84xx, exiting.\n");
6e98016c
GM
1082 return -EINVAL;
1083 }
1084
1085 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
1086 sizeof(struct fc_bsg_request));
1087 if (!ql84_mgmt) {
7c3df132
SK
1088 ql_log(ql_log_warn, vha, 0x703b,
1089 "MGMT header not provided, exiting.\n");
6e98016c
GM
1090 return -EINVAL;
1091 }
1092
1093 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1094 if (!mn) {
7c3df132
SK
1095 ql_log(ql_log_warn, vha, 0x703c,
1096 "DMA alloc failed for fw buffer.\n");
6e98016c
GM
1097 return -ENOMEM;
1098 }
1099
1100 memset(mn, 0, sizeof(struct access_chip_84xx));
1101 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1102 mn->entry_count = 1;
1103
1104 switch (ql84_mgmt->mgmt.cmd) {
1105 case QLA84_MGMT_READ_MEM:
1106 case QLA84_MGMT_GET_INFO:
1107 sg_cnt = dma_map_sg(&ha->pdev->dev,
1108 bsg_job->reply_payload.sg_list,
1109 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1110 if (!sg_cnt) {
7c3df132
SK
1111 ql_log(ql_log_warn, vha, 0x703d,
1112 "dma_map_sg returned %d for reply.\n", sg_cnt);
6e98016c
GM
1113 rval = -ENOMEM;
1114 goto exit_mgmt;
1115 }
1116
1117 dma_direction = DMA_FROM_DEVICE;
1118
1119 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
7c3df132
SK
1120 ql_log(ql_log_warn, vha, 0x703e,
1121 "DMA mapping resulted in different sg counts, "
1122 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1123 bsg_job->reply_payload.sg_cnt, sg_cnt);
6e98016c
GM
1124 rval = -EAGAIN;
1125 goto done_unmap_sg;
1126 }
1127
1128 data_len = bsg_job->reply_payload.payload_len;
1129
1130 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1131 &mgmt_dma, GFP_KERNEL);
1132 if (!mgmt_b) {
7c3df132
SK
1133 ql_log(ql_log_warn, vha, 0x703f,
1134 "DMA alloc failed for mgmt_b.\n");
6e98016c
GM
1135 rval = -ENOMEM;
1136 goto done_unmap_sg;
1137 }
1138
1139 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1140 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1141 mn->parameter1 =
1142 cpu_to_le32(
1143 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1144
1145 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1146 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1147 mn->parameter1 =
1148 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1149
1150 mn->parameter2 =
1151 cpu_to_le32(
1152 ql84_mgmt->mgmt.mgmtp.u.info.context);
1153 }
1154 break;
1155
1156 case QLA84_MGMT_WRITE_MEM:
1157 sg_cnt = dma_map_sg(&ha->pdev->dev,
1158 bsg_job->request_payload.sg_list,
1159 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1160
1161 if (!sg_cnt) {
7c3df132
SK
1162 ql_log(ql_log_warn, vha, 0x7040,
1163 "dma_map_sg returned %d.\n", sg_cnt);
6e98016c
GM
1164 rval = -ENOMEM;
1165 goto exit_mgmt;
1166 }
1167
1168 dma_direction = DMA_TO_DEVICE;
1169
1170 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
7c3df132
SK
1171 ql_log(ql_log_warn, vha, 0x7041,
1172 "DMA mapping resulted in different sg counts, "
1173 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1174 bsg_job->request_payload.sg_cnt, sg_cnt);
6e98016c
GM
1175 rval = -EAGAIN;
1176 goto done_unmap_sg;
1177 }
1178
1179 data_len = bsg_job->request_payload.payload_len;
1180 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1181 &mgmt_dma, GFP_KERNEL);
1182 if (!mgmt_b) {
7c3df132
SK
1183 ql_log(ql_log_warn, vha, 0x7042,
1184 "DMA alloc failed for mgmt_b.\n");
6e98016c
GM
1185 rval = -ENOMEM;
1186 goto done_unmap_sg;
1187 }
1188
1189 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1190 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1191
1192 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1193 mn->parameter1 =
1194 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1195 break;
1196
1197 case QLA84_MGMT_CHNG_CONFIG:
1198 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1199 mn->parameter1 =
1200 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1201
1202 mn->parameter2 =
1203 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1204
1205 mn->parameter3 =
1206 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1207 break;
1208
1209 default:
1210 rval = -EIO;
1211 goto exit_mgmt;
1212 }
1213
1214 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1215 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1216 mn->dseg_count = cpu_to_le16(1);
1217 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1218 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1219 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1220 }
1221
1222 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1223
1224 if (rval) {
7c3df132
SK
1225 ql_log(ql_log_warn, vha, 0x7043,
1226 "Vendor request 84xx mgmt failed.\n");
6e98016c 1227
63ea923a 1228 rval = (DID_ERROR << 16);
6e98016c
GM
1229
1230 } else {
7c3df132
SK
1231 ql_dbg(ql_dbg_user, vha, 0x7044,
1232 "Vendor request 84xx mgmt completed.\n");
6e98016c
GM
1233
1234 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1235 bsg_job->reply->result = DID_OK;
1236
1237 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1238 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1239 bsg_job->reply->reply_payload_rcv_len =
1240 bsg_job->reply_payload.payload_len;
1241
1242 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
6c452a45
AV
1243 bsg_job->reply_payload.sg_cnt, mgmt_b,
1244 data_len);
6e98016c
GM
1245 }
1246 }
1247
6e98016c 1248done_unmap_sg:
d5459083
HZ
1249 if (mgmt_b)
1250 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1251
6e98016c
GM
1252 if (dma_direction == DMA_TO_DEVICE)
1253 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1254 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1255 else if (dma_direction == DMA_FROM_DEVICE)
1256 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1257 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1258
1259exit_mgmt:
1260 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1261
63ea923a
AB
1262 if (!rval)
1263 bsg_job->job_done(bsg_job);
6e98016c
GM
1264 return rval;
1265}
1266
1267static int
1268qla24xx_iidma(struct fc_bsg_job *bsg_job)
1269{
1270 struct Scsi_Host *host = bsg_job->shost;
1271 scsi_qla_host_t *vha = shost_priv(host);
6e98016c
GM
1272 int rval = 0;
1273 struct qla_port_param *port_param = NULL;
1274 fc_port_t *fcport = NULL;
1275 uint16_t mb[MAILBOX_REGISTER_COUNT];
1276 uint8_t *rsp_ptr = NULL;
1277
6e98016c 1278 if (!IS_IIDMA_CAPABLE(vha->hw)) {
7c3df132 1279 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
6e98016c
GM
1280 return -EINVAL;
1281 }
1282
1283 port_param = (struct qla_port_param *)((char *)bsg_job->request +
1284 sizeof(struct fc_bsg_request));
1285 if (!port_param) {
7c3df132
SK
1286 ql_log(ql_log_warn, vha, 0x7047,
1287 "port_param header not provided.\n");
6e98016c
GM
1288 return -EINVAL;
1289 }
1290
1291 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
7c3df132
SK
1292 ql_log(ql_log_warn, vha, 0x7048,
1293 "Invalid destination type.\n");
6e98016c
GM
1294 return -EINVAL;
1295 }
1296
1297 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1298 if (fcport->port_type != FCT_TARGET)
1299 continue;
1300
1301 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1302 fcport->port_name, sizeof(fcport->port_name)))
1303 continue;
1304 break;
1305 }
1306
1307 if (!fcport) {
7c3df132
SK
1308 ql_log(ql_log_warn, vha, 0x7049,
1309 "Failed to find port.\n");
6e98016c
GM
1310 return -EINVAL;
1311 }
1312
c9afb9a2 1313 if (atomic_read(&fcport->state) != FCS_ONLINE) {
7c3df132
SK
1314 ql_log(ql_log_warn, vha, 0x704a,
1315 "Port is not online.\n");
17cf2c5d
MI
1316 return -EINVAL;
1317 }
1318
9a15eb4b 1319 if (fcport->flags & FCF_LOGIN_NEEDED) {
7c3df132
SK
1320 ql_log(ql_log_warn, vha, 0x704b,
1321 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
9a15eb4b
MI
1322 return -EINVAL;
1323 }
1324
6e98016c
GM
1325 if (port_param->mode)
1326 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1327 port_param->speed, mb);
1328 else
1329 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1330 &port_param->speed, mb);
1331
1332 if (rval) {
7c3df132
SK
1333 ql_log(ql_log_warn, vha, 0x704c,
1334 "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
1335 "%04x %x %04x %04x.\n", fcport->port_name[0],
1336 fcport->port_name[1], fcport->port_name[2],
1337 fcport->port_name[3], fcport->port_name[4],
1338 fcport->port_name[5], fcport->port_name[6],
1339 fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
63ea923a 1340 rval = (DID_ERROR << 16);
6e98016c
GM
1341 } else {
1342 if (!port_param->mode) {
1343 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1344 sizeof(struct qla_port_param);
1345
1346 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1347 sizeof(struct fc_bsg_reply);
1348
1349 memcpy(rsp_ptr, port_param,
1350 sizeof(struct qla_port_param));
1351 }
1352
1353 bsg_job->reply->result = DID_OK;
63ea923a 1354 bsg_job->job_done(bsg_job);
6e98016c
GM
1355 }
1356
6e98016c
GM
1357 return rval;
1358}
1359
f19af163 1360static int
7c3df132 1361qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
f19af163
HZ
1362 uint8_t is_update)
1363{
1364 uint32_t start = 0;
1365 int valid = 0;
7c3df132 1366 struct qla_hw_data *ha = vha->hw;
f19af163 1367
f19af163
HZ
1368 if (unlikely(pci_channel_offline(ha->pdev)))
1369 return -EINVAL;
1370
1371 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
7c3df132
SK
1372 if (start > ha->optrom_size) {
1373 ql_log(ql_log_warn, vha, 0x7055,
1374 "start %d > optrom_size %d.\n", start, ha->optrom_size);
f19af163 1375 return -EINVAL;
7c3df132 1376 }
f19af163 1377
7c3df132
SK
1378 if (ha->optrom_state != QLA_SWAITING) {
1379 ql_log(ql_log_info, vha, 0x7056,
1380 "optrom_state %d.\n", ha->optrom_state);
f19af163 1381 return -EBUSY;
7c3df132 1382 }
f19af163
HZ
1383
1384 ha->optrom_region_start = start;
7c3df132 1385 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
f19af163
HZ
1386 if (is_update) {
1387 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1388 valid = 1;
1389 else if (start == (ha->flt_region_boot * 4) ||
1390 start == (ha->flt_region_fw * 4))
1391 valid = 1;
1392 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
6246b8a1 1393 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
f19af163
HZ
1394 valid = 1;
1395 if (!valid) {
7c3df132
SK
1396 ql_log(ql_log_warn, vha, 0x7058,
1397 "Invalid start region 0x%x/0x%x.\n", start,
1398 bsg_job->request_payload.payload_len);
f19af163
HZ
1399 return -EINVAL;
1400 }
1401
1402 ha->optrom_region_size = start +
1403 bsg_job->request_payload.payload_len > ha->optrom_size ?
1404 ha->optrom_size - start :
1405 bsg_job->request_payload.payload_len;
1406 ha->optrom_state = QLA_SWRITING;
1407 } else {
1408 ha->optrom_region_size = start +
1409 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1410 ha->optrom_size - start :
1411 bsg_job->reply_payload.payload_len;
1412 ha->optrom_state = QLA_SREADING;
1413 }
1414
1415 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1416 if (!ha->optrom_buffer) {
7c3df132 1417 ql_log(ql_log_warn, vha, 0x7059,
f19af163 1418 "Read: Unable to allocate memory for optrom retrieval "
7c3df132 1419 "(%x)\n", ha->optrom_region_size);
f19af163
HZ
1420
1421 ha->optrom_state = QLA_SWAITING;
1422 return -ENOMEM;
1423 }
1424
1425 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1426 return 0;
1427}
1428
1429static int
1430qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1431{
1432 struct Scsi_Host *host = bsg_job->shost;
1433 scsi_qla_host_t *vha = shost_priv(host);
1434 struct qla_hw_data *ha = vha->hw;
1435 int rval = 0;
1436
7d613ac6 1437 if (ha->flags.nic_core_reset_hdlr_active)
a49393f2
GM
1438 return -EBUSY;
1439
7c3df132 1440 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
f19af163
HZ
1441 if (rval)
1442 return rval;
1443
1444 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1445 ha->optrom_region_start, ha->optrom_region_size);
1446
1447 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1448 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1449 ha->optrom_region_size);
1450
1451 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1452 bsg_job->reply->result = DID_OK;
1453 vfree(ha->optrom_buffer);
1454 ha->optrom_buffer = NULL;
1455 ha->optrom_state = QLA_SWAITING;
1456 bsg_job->job_done(bsg_job);
1457 return rval;
1458}
1459
1460static int
1461qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1462{
1463 struct Scsi_Host *host = bsg_job->shost;
1464 scsi_qla_host_t *vha = shost_priv(host);
1465 struct qla_hw_data *ha = vha->hw;
1466 int rval = 0;
1467
7c3df132 1468 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
f19af163
HZ
1469 if (rval)
1470 return rval;
1471
b6d0d9d5
GM
1472 /* Set the isp82xx_no_md_cap not to capture minidump */
1473 ha->flags.isp82xx_no_md_cap = 1;
1474
f19af163
HZ
1475 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1476 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1477 ha->optrom_region_size);
1478
1479 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1480 ha->optrom_region_start, ha->optrom_region_size);
1481
1482 bsg_job->reply->result = DID_OK;
1483 vfree(ha->optrom_buffer);
1484 ha->optrom_buffer = NULL;
1485 ha->optrom_state = QLA_SWAITING;
1486 bsg_job->job_done(bsg_job);
1487 return rval;
1488}
1489
697a4bc6
JC
1490static int
1491qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1492{
1493 struct Scsi_Host *host = bsg_job->shost;
1494 scsi_qla_host_t *vha = shost_priv(host);
1495 struct qla_hw_data *ha = vha->hw;
1496 int rval = 0;
1497 uint8_t bsg[DMA_POOL_SIZE];
1498 struct qla_image_version_list *list = (void *)bsg;
1499 struct qla_image_version *image;
1500 uint32_t count;
1501 dma_addr_t sfp_dma;
1502 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1503 if (!sfp) {
1504 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1505 EXT_STATUS_NO_MEMORY;
1506 goto done;
1507 }
1508
1509 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1510 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1511
1512 image = list->version;
1513 count = list->count;
1514 while (count--) {
1515 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1516 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1517 image->field_address.device, image->field_address.offset,
1518 sizeof(image->field_info), image->field_address.option);
1519 if (rval) {
1520 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1521 EXT_STATUS_MAILBOX;
1522 goto dealloc;
1523 }
1524 image++;
1525 }
1526
1527 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1528
1529dealloc:
1530 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1531
1532done:
1533 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1534 bsg_job->reply->result = DID_OK << 16;
1535 bsg_job->job_done(bsg_job);
1536
1537 return 0;
1538}
1539
1540static int
1541qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1542{
1543 struct Scsi_Host *host = bsg_job->shost;
1544 scsi_qla_host_t *vha = shost_priv(host);
1545 struct qla_hw_data *ha = vha->hw;
1546 int rval = 0;
1547 uint8_t bsg[DMA_POOL_SIZE];
1548 struct qla_status_reg *sr = (void *)bsg;
1549 dma_addr_t sfp_dma;
1550 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1551 if (!sfp) {
1552 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1553 EXT_STATUS_NO_MEMORY;
1554 goto done;
1555 }
1556
1557 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1558 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1559
1560 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1561 sr->field_address.device, sr->field_address.offset,
1562 sizeof(sr->status_reg), sr->field_address.option);
1563 sr->status_reg = *sfp;
1564
1565 if (rval) {
1566 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1567 EXT_STATUS_MAILBOX;
1568 goto dealloc;
1569 }
1570
1571 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1572 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1573
1574 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1575
1576dealloc:
1577 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1578
1579done:
1580 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1581 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1582 bsg_job->reply->result = DID_OK << 16;
1583 bsg_job->job_done(bsg_job);
1584
1585 return 0;
1586}
1587
1588static int
1589qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1590{
1591 struct Scsi_Host *host = bsg_job->shost;
1592 scsi_qla_host_t *vha = shost_priv(host);
1593 struct qla_hw_data *ha = vha->hw;
1594 int rval = 0;
1595 uint8_t bsg[DMA_POOL_SIZE];
1596 struct qla_status_reg *sr = (void *)bsg;
1597 dma_addr_t sfp_dma;
1598 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1599 if (!sfp) {
1600 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1601 EXT_STATUS_NO_MEMORY;
1602 goto done;
1603 }
1604
1605 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1606 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1607
1608 *sfp = sr->status_reg;
1609 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1610 sr->field_address.device, sr->field_address.offset,
1611 sizeof(sr->status_reg), sr->field_address.option);
1612
1613 if (rval) {
1614 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1615 EXT_STATUS_MAILBOX;
1616 goto dealloc;
1617 }
1618
1619 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1620
1621dealloc:
1622 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1623
1624done:
1625 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1626 bsg_job->reply->result = DID_OK << 16;
1627 bsg_job->job_done(bsg_job);
1628
1629 return 0;
1630}
1631
9ebb5d9c
JC
1632static int
1633qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
1634{
1635 struct Scsi_Host *host = bsg_job->shost;
1636 scsi_qla_host_t *vha = shost_priv(host);
1637 struct qla_hw_data *ha = vha->hw;
1638 int rval = 0;
1639 uint8_t bsg[DMA_POOL_SIZE];
1640 struct qla_i2c_access *i2c = (void *)bsg;
1641 dma_addr_t sfp_dma;
1642 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1643 if (!sfp) {
1644 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1645 EXT_STATUS_NO_MEMORY;
1646 goto done;
1647 }
1648
1649 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1650 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1651
1652 memcpy(sfp, i2c->buffer, i2c->length);
1653 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1654 i2c->device, i2c->offset, i2c->length, i2c->option);
1655
1656 if (rval) {
1657 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1658 EXT_STATUS_MAILBOX;
1659 goto dealloc;
1660 }
1661
1662 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1663
1664dealloc:
1665 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1666
1667done:
1668 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1669 bsg_job->reply->result = DID_OK << 16;
1670 bsg_job->job_done(bsg_job);
1671
1672 return 0;
1673}
1674
1675static int
1676qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
1677{
1678 struct Scsi_Host *host = bsg_job->shost;
1679 scsi_qla_host_t *vha = shost_priv(host);
1680 struct qla_hw_data *ha = vha->hw;
1681 int rval = 0;
1682 uint8_t bsg[DMA_POOL_SIZE];
1683 struct qla_i2c_access *i2c = (void *)bsg;
1684 dma_addr_t sfp_dma;
1685 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1686 if (!sfp) {
1687 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1688 EXT_STATUS_NO_MEMORY;
1689 goto done;
1690 }
1691
1692 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1693 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1694
1695 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1696 i2c->device, i2c->offset, i2c->length, i2c->option);
1697
1698 if (rval) {
1699 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1700 EXT_STATUS_MAILBOX;
1701 goto dealloc;
1702 }
1703
1704 memcpy(i2c->buffer, sfp, i2c->length);
1705 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1706 bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1707
1708 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1709
1710dealloc:
1711 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1712
1713done:
1714 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1715 bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
1716 bsg_job->reply->result = DID_OK << 16;
1717 bsg_job->job_done(bsg_job);
1718
1719 return 0;
1720}
1721
a9b6f722
SK
1722static int
1723qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
1724{
1725 struct Scsi_Host *host = bsg_job->shost;
1726 scsi_qla_host_t *vha = shost_priv(host);
1727 struct qla_hw_data *ha = vha->hw;
1728 uint16_t thread_id;
1729 uint32_t rval = EXT_STATUS_OK;
1730 uint16_t req_sg_cnt = 0;
1731 uint16_t rsp_sg_cnt = 0;
1732 uint16_t nextlid = 0;
1733 uint32_t tot_dsds;
1734 srb_t *sp = NULL;
1735 uint32_t req_data_len = 0;
1736 uint32_t rsp_data_len = 0;
1737
1738 /* Check the type of the adapter */
1739 if (!IS_BIDI_CAPABLE(ha)) {
1740 ql_log(ql_log_warn, vha, 0x70a0,
1741 "This adapter is not supported\n");
1742 rval = EXT_STATUS_NOT_SUPPORTED;
1743 goto done;
1744 }
1745
1746 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1747 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1748 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1749 rval = EXT_STATUS_BUSY;
1750 goto done;
1751 }
1752
1753 /* Check if host is online */
1754 if (!vha->flags.online) {
1755 ql_log(ql_log_warn, vha, 0x70a1,
1756 "Host is not online\n");
1757 rval = EXT_STATUS_DEVICE_OFFLINE;
1758 goto done;
1759 }
1760
1761 /* Check if cable is plugged in or not */
1762 if (vha->device_flags & DFLG_NO_CABLE) {
1763 ql_log(ql_log_warn, vha, 0x70a2,
1764 "Cable is unplugged...\n");
1765 rval = EXT_STATUS_INVALID_CFG;
1766 goto done;
1767 }
1768
1769 /* Check if the switch is connected or not */
1770 if (ha->current_topology != ISP_CFG_F) {
1771 ql_log(ql_log_warn, vha, 0x70a3,
1772 "Host is not connected to the switch\n");
1773 rval = EXT_STATUS_INVALID_CFG;
1774 goto done;
1775 }
1776
1777 /* Check if operating mode is P2P */
1778 if (ha->operating_mode != P2P) {
1779 ql_log(ql_log_warn, vha, 0x70a4,
1780 "Host is operating mode is not P2p\n");
1781 rval = EXT_STATUS_INVALID_CFG;
1782 goto done;
1783 }
1784
1785 thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1786
1787 mutex_lock(&ha->selflogin_lock);
1788 if (vha->self_login_loop_id == 0) {
1789 /* Initialize all required fields of fcport */
1790 vha->bidir_fcport.vha = vha;
1791 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1792 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1793 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1794 vha->bidir_fcport.loop_id = vha->loop_id;
1795
1796 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1797 ql_log(ql_log_warn, vha, 0x70a7,
1798 "Failed to login port %06X for bidirectional IOCB\n",
1799 vha->bidir_fcport.d_id.b24);
1800 mutex_unlock(&ha->selflogin_lock);
1801 rval = EXT_STATUS_MAILBOX;
1802 goto done;
1803 }
1804 vha->self_login_loop_id = nextlid - 1;
1805
1806 }
1807 /* Assign the self login loop id to fcport */
1808 mutex_unlock(&ha->selflogin_lock);
1809
1810 vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1811
1812 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1813 bsg_job->request_payload.sg_list,
1814 bsg_job->request_payload.sg_cnt,
1815 DMA_TO_DEVICE);
1816
1817 if (!req_sg_cnt) {
1818 rval = EXT_STATUS_NO_MEMORY;
1819 goto done;
1820 }
1821
1822 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1823 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1824 DMA_FROM_DEVICE);
1825
1826 if (!rsp_sg_cnt) {
1827 rval = EXT_STATUS_NO_MEMORY;
1828 goto done_unmap_req_sg;
1829 }
1830
1831 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
1832 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1833 ql_dbg(ql_dbg_user, vha, 0x70a9,
1834 "Dma mapping resulted in different sg counts "
1835 "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1836 "%x dma_reply_sg_cnt: %x]\n",
1837 bsg_job->request_payload.sg_cnt, req_sg_cnt,
1838 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1839 rval = EXT_STATUS_NO_MEMORY;
1840 goto done_unmap_sg;
1841 }
1842
1843 if (req_data_len != rsp_data_len) {
1844 rval = EXT_STATUS_BUSY;
1845 ql_log(ql_log_warn, vha, 0x70aa,
1846 "req_data_len != rsp_data_len\n");
1847 goto done_unmap_sg;
1848 }
1849
1850 req_data_len = bsg_job->request_payload.payload_len;
1851 rsp_data_len = bsg_job->reply_payload.payload_len;
1852
1853
1854 /* Alloc SRB structure */
1855 sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1856 if (!sp) {
1857 ql_dbg(ql_dbg_user, vha, 0x70ac,
1858 "Alloc SRB structure failed\n");
1859 rval = EXT_STATUS_NO_MEMORY;
1860 goto done_unmap_sg;
1861 }
1862
1863 /*Populate srb->ctx with bidir ctx*/
1864 sp->u.bsg_job = bsg_job;
1865 sp->free = qla2x00_bsg_sp_free;
1866 sp->type = SRB_BIDI_CMD;
1867 sp->done = qla2x00_bsg_job_done;
1868
1869 /* Add the read and write sg count */
1870 tot_dsds = rsp_sg_cnt + req_sg_cnt;
1871
1872 rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1873 if (rval != EXT_STATUS_OK)
1874 goto done_free_srb;
1875 /* the bsg request will be completed in the interrupt handler */
1876 return rval;
1877
1878done_free_srb:
1879 mempool_free(sp, ha->srb_mempool);
1880done_unmap_sg:
1881 dma_unmap_sg(&ha->pdev->dev,
1882 bsg_job->reply_payload.sg_list,
1883 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1884done_unmap_req_sg:
1885 dma_unmap_sg(&ha->pdev->dev,
1886 bsg_job->request_payload.sg_list,
1887 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1888done:
1889
1890 /* Return an error vendor specific response
1891 * and complete the bsg request
1892 */
1893 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1894 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1895 bsg_job->reply->reply_payload_rcv_len = 0;
1896 bsg_job->reply->result = (DID_OK) << 16;
1897 bsg_job->job_done(bsg_job);
1898 /* Always retrun success, vendor rsp carries correct status */
1899 return 0;
1900}
1901
8ae6d9c7
GM
1902static int
1903qlafx00_mgmt_cmd(struct fc_bsg_job *bsg_job)
1904{
1905 struct Scsi_Host *host = bsg_job->shost;
1906 scsi_qla_host_t *vha = shost_priv(host);
1907 struct qla_hw_data *ha = vha->hw;
1908 int rval = (DRIVER_ERROR << 16);
1909 struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
1910 srb_t *sp;
1911 int req_sg_cnt = 0, rsp_sg_cnt = 0;
1912 struct fc_port *fcport;
1913 char *type = "FC_BSG_HST_FX_MGMT";
1914
1915 /* Copy the IOCB specific information */
1916 piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
1917 &bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1918
1919 /* Dump the vendor information */
1920 ql_dump_buffer(ql_dbg_user + ql_dbg_verbose , vha, 0x70cf,
1921 (uint8_t *)piocb_rqst, sizeof(struct qla_mt_iocb_rqst_fx00));
1922
1923 if (!vha->flags.online) {
1924 ql_log(ql_log_warn, vha, 0x70d0,
1925 "Host is not online.\n");
1926 rval = -EIO;
1927 goto done;
1928 }
1929
1930 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID) {
1931 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1932 bsg_job->request_payload.sg_list,
1933 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1934 if (!req_sg_cnt) {
1935 ql_log(ql_log_warn, vha, 0x70c7,
1936 "dma_map_sg return %d for request\n", req_sg_cnt);
1937 rval = -ENOMEM;
1938 goto done;
1939 }
1940 }
1941
1942 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID) {
1943 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1944 bsg_job->reply_payload.sg_list,
1945 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1946 if (!rsp_sg_cnt) {
1947 ql_log(ql_log_warn, vha, 0x70c8,
1948 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
1949 rval = -ENOMEM;
1950 goto done_unmap_req_sg;
1951 }
1952 }
1953
1954 ql_dbg(ql_dbg_user, vha, 0x70c9,
1955 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
1956 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
1957 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1958
1959 /* Allocate a dummy fcport structure, since functions preparing the
1960 * IOCB and mailbox command retrieves port specific information
1961 * from fcport structure. For Host based ELS commands there will be
1962 * no fcport structure allocated
1963 */
1964 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1965 if (!fcport) {
1966 ql_log(ql_log_warn, vha, 0x70ca,
1967 "Failed to allocate fcport.\n");
1968 rval = -ENOMEM;
1969 goto done_unmap_rsp_sg;
1970 }
1971
1972 /* Alloc SRB structure */
1973 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1974 if (!sp) {
1975 ql_log(ql_log_warn, vha, 0x70cb,
1976 "qla2x00_get_sp failed.\n");
1977 rval = -ENOMEM;
1978 goto done_free_fcport;
1979 }
1980
1981 /* Initialize all required fields of fcport */
1982 fcport->vha = vha;
1983 fcport->loop_id = piocb_rqst->dataword;
1984
1985 sp->type = SRB_FXIOCB_BCMD;
1986 sp->name = "bsg_fx_mgmt";
1987 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
1988 sp->u.bsg_job = bsg_job;
1989 sp->free = qla2x00_bsg_sp_free;
1990 sp->done = qla2x00_bsg_job_done;
1991
1992 ql_dbg(ql_dbg_user, vha, 0x70cc,
1993 "bsg rqst type: %s fx_mgmt_type: %x id=%x\n",
1994 type, piocb_rqst->func_type, fcport->loop_id);
1995
1996 rval = qla2x00_start_sp(sp);
1997 if (rval != QLA_SUCCESS) {
1998 ql_log(ql_log_warn, vha, 0x70cd,
1999 "qla2x00_start_sp failed=%d.\n", rval);
2000 mempool_free(sp, ha->srb_mempool);
2001 rval = -EIO;
2002 goto done_free_fcport;
2003 }
2004 return rval;
2005
2006done_free_fcport:
2007 kfree(fcport);
2008
2009done_unmap_rsp_sg:
2010 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
2011 dma_unmap_sg(&ha->pdev->dev,
2012 bsg_job->reply_payload.sg_list,
2013 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2014done_unmap_req_sg:
2015 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
2016 dma_unmap_sg(&ha->pdev->dev,
2017 bsg_job->request_payload.sg_list,
2018 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2019
2020done:
2021 return rval;
2022}
2023
6e98016c
GM
2024static int
2025qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
2026{
2027 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
2028 case QL_VND_LOOPBACK:
2029 return qla2x00_process_loopback(bsg_job);
2030
2031 case QL_VND_A84_RESET:
2032 return qla84xx_reset(bsg_job);
2033
2034 case QL_VND_A84_UPDATE_FW:
2035 return qla84xx_updatefw(bsg_job);
2036
2037 case QL_VND_A84_MGMT_CMD:
2038 return qla84xx_mgmt_cmd(bsg_job);
2039
2040 case QL_VND_IIDMA:
2041 return qla24xx_iidma(bsg_job);
2042
09ff701a
SR
2043 case QL_VND_FCP_PRIO_CFG_CMD:
2044 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
2045
f19af163
HZ
2046 case QL_VND_READ_FLASH:
2047 return qla2x00_read_optrom(bsg_job);
2048
2049 case QL_VND_UPDATE_FLASH:
2050 return qla2x00_update_optrom(bsg_job);
2051
697a4bc6
JC
2052 case QL_VND_SET_FRU_VERSION:
2053 return qla2x00_update_fru_versions(bsg_job);
2054
2055 case QL_VND_READ_FRU_STATUS:
2056 return qla2x00_read_fru_status(bsg_job);
2057
2058 case QL_VND_WRITE_FRU_STATUS:
2059 return qla2x00_write_fru_status(bsg_job);
2060
9ebb5d9c
JC
2061 case QL_VND_WRITE_I2C:
2062 return qla2x00_write_i2c(bsg_job);
2063
2064 case QL_VND_READ_I2C:
2065 return qla2x00_read_i2c(bsg_job);
2066
a9b6f722
SK
2067 case QL_VND_DIAG_IO_CMD:
2068 return qla24xx_process_bidir_cmd(bsg_job);
2069
8ae6d9c7
GM
2070 case QL_VND_FX00_MGMT_CMD:
2071 return qlafx00_mgmt_cmd(bsg_job);
6e98016c 2072 default:
6e98016c
GM
2073 return -ENOSYS;
2074 }
2075}
2076
2077int
2078qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
2079{
2080 int ret = -EINVAL;
7c3df132
SK
2081 struct fc_rport *rport;
2082 fc_port_t *fcport = NULL;
2083 struct Scsi_Host *host;
2084 scsi_qla_host_t *vha;
2085
b7bfbe12
AV
2086 /* In case no data transferred. */
2087 bsg_job->reply->reply_payload_rcv_len = 0;
2088
7c3df132
SK
2089 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
2090 rport = bsg_job->rport;
2091 fcport = *(fc_port_t **) rport->dd_data;
2092 host = rport_to_shost(rport);
2093 vha = shost_priv(host);
2094 } else {
2095 host = bsg_job->shost;
2096 vha = shost_priv(host);
2097 }
2098
d051a5aa
AV
2099 if (qla2x00_reset_active(vha)) {
2100 ql_dbg(ql_dbg_user, vha, 0x709f,
2101 "BSG: ISP abort active/needed -- cmd=%d.\n",
2102 bsg_job->request->msgcode);
d051a5aa
AV
2103 return -EBUSY;
2104 }
2105
7c3df132 2106 ql_dbg(ql_dbg_user, vha, 0x7000,
cfb0919c 2107 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
6e98016c
GM
2108
2109 switch (bsg_job->request->msgcode) {
2110 case FC_BSG_RPT_ELS:
2111 case FC_BSG_HST_ELS_NOLOGIN:
2112 ret = qla2x00_process_els(bsg_job);
2113 break;
2114 case FC_BSG_HST_CT:
2115 ret = qla2x00_process_ct(bsg_job);
2116 break;
2117 case FC_BSG_HST_VENDOR:
2118 ret = qla2x00_process_vendor_specific(bsg_job);
2119 break;
2120 case FC_BSG_HST_ADD_RPORT:
2121 case FC_BSG_HST_DEL_RPORT:
2122 case FC_BSG_RPT_CT:
2123 default:
7c3df132 2124 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
6e98016c 2125 break;
6c452a45 2126 }
6e98016c
GM
2127 return ret;
2128}
2129
2130int
2131qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
2132{
2133 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
2134 struct qla_hw_data *ha = vha->hw;
2135 srb_t *sp;
2136 int cnt, que;
2137 unsigned long flags;
2138 struct req_que *req;
6e98016c
GM
2139
2140 /* find the bsg job from the active list of commands */
2141 spin_lock_irqsave(&ha->hardware_lock, flags);
2142 for (que = 0; que < ha->max_req_queues; que++) {
2143 req = ha->req_q_map[que];
2144 if (!req)
2145 continue;
2146
8d93f550 2147 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
6e98016c 2148 sp = req->outstanding_cmds[cnt];
6e98016c 2149 if (sp) {
9ba56b95 2150 if (((sp->type == SRB_CT_CMD) ||
8ae6d9c7
GM
2151 (sp->type == SRB_ELS_CMD_HST) ||
2152 (sp->type == SRB_FXIOCB_BCMD))
9ba56b95 2153 && (sp->u.bsg_job == bsg_job)) {
900a36e3 2154 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6e98016c 2155 if (ha->isp_ops->abort_command(sp)) {
7c3df132
SK
2156 ql_log(ql_log_warn, vha, 0x7089,
2157 "mbx abort_command "
2158 "failed.\n");
6e98016c
GM
2159 bsg_job->req->errors =
2160 bsg_job->reply->result = -EIO;
2161 } else {
7c3df132
SK
2162 ql_dbg(ql_dbg_user, vha, 0x708a,
2163 "mbx abort_command "
2164 "success.\n");
6e98016c
GM
2165 bsg_job->req->errors =
2166 bsg_job->reply->result = 0;
2167 }
900a36e3 2168 spin_lock_irqsave(&ha->hardware_lock, flags);
6e98016c
GM
2169 goto done;
2170 }
2171 }
2172 }
2173 }
2174 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7c3df132 2175 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
6e98016c
GM
2176 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
2177 return 0;
2178
2179done:
2180 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2181 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
2182 kfree(sp->fcport);
b00ee7d7 2183 qla2x00_rel_sp(vha, sp);
6e98016c
GM
2184 return 0;
2185}