scsi: qla2xxx: Make qla2x00_process_response_queue() easier to read
[linux-2.6-block.git] / drivers / scsi / qla2xxx / qla_bsg.c
CommitLineData
f65c3389 1/*
6e98016c 2 * QLogic Fibre Channel HBA Driver
bd21eaf9 3 * Copyright (c) 2003-2014 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>
75cc8cfc 12#include <linux/bsg-lib.h>
6e98016c
GM
13
14/* BSG support for ELS/CT pass through */
9ba56b95 15void
25ff6af1 16qla2x00_bsg_job_done(void *ptr, int res)
6e98016c 17{
25ff6af1 18 srb_t *sp = ptr;
75cc8cfc 19 struct bsg_job *bsg_job = sp->u.bsg_job;
01e0e15c 20 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
9ba56b95 21
01e0e15c 22 bsg_reply->result = res;
06548160 23 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 24 bsg_reply->reply_payload_rcv_len);
25ff6af1 25 sp->free(sp);
9ba56b95
GM
26}
27
28void
25ff6af1 29qla2x00_bsg_sp_free(void *ptr)
9ba56b95 30{
25ff6af1
JC
31 srb_t *sp = ptr;
32 struct qla_hw_data *ha = sp->vha->hw;
75cc8cfc 33 struct bsg_job *bsg_job = sp->u.bsg_job;
01e0e15c 34 struct fc_bsg_request *bsg_request = bsg_job->request;
8ae6d9c7 35 struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
6e98016c 36
8ae6d9c7
GM
37 if (sp->type == SRB_FXIOCB_BCMD) {
38 piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
01e0e15c 39 &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
6e98016c 40
8ae6d9c7
GM
41 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
42 dma_unmap_sg(&ha->pdev->dev,
43 bsg_job->request_payload.sg_list,
44 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
45
46 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
47 dma_unmap_sg(&ha->pdev->dev,
48 bsg_job->reply_payload.sg_list,
49 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
50 } else {
51 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
52 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
53
54 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
55 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
56 }
9ba56b95
GM
57
58 if (sp->type == SRB_CT_CMD ||
8ae6d9c7 59 sp->type == SRB_FXIOCB_BCMD ||
9ba56b95
GM
60 sp->type == SRB_ELS_CMD_HST)
61 kfree(sp->fcport);
25ff6af1 62 qla2x00_rel_sp(sp);
6e98016c
GM
63}
64
09ff701a 65int
7c3df132
SK
66qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
67 struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
09ff701a
SR
68{
69 int i, ret, num_valid;
70 uint8_t *bcode;
71 struct qla_fcp_prio_entry *pri_entry;
2f0f3f4f 72 uint32_t *bcode_val_ptr, bcode_val;
09ff701a
SR
73
74 ret = 1;
75 num_valid = 0;
76 bcode = (uint8_t *)pri_cfg;
2f0f3f4f
MI
77 bcode_val_ptr = (uint32_t *)pri_cfg;
78 bcode_val = (uint32_t)(*bcode_val_ptr);
09ff701a 79
2f0f3f4f
MI
80 if (bcode_val == 0xFFFFFFFF) {
81 /* No FCP Priority config data in flash */
7c3df132
SK
82 ql_dbg(ql_dbg_user, vha, 0x7051,
83 "No FCP Priority config data.\n");
2f0f3f4f
MI
84 return 0;
85 }
86
a28d9e4e 87 if (memcmp(bcode, "HQOS", 4)) {
2f0f3f4f 88 /* Invalid FCP priority data header*/
7c3df132
SK
89 ql_dbg(ql_dbg_user, vha, 0x7052,
90 "Invalid FCP Priority data header. bcode=0x%x.\n",
91 bcode_val);
09ff701a
SR
92 return 0;
93 }
94 if (flag != 1)
95 return ret;
96
97 pri_entry = &pri_cfg->entry[0];
98 for (i = 0; i < pri_cfg->num_entries; i++) {
99 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
100 num_valid++;
101 pri_entry++;
102 }
103
2f0f3f4f
MI
104 if (num_valid == 0) {
105 /* No valid FCP priority data entries */
7c3df132
SK
106 ql_dbg(ql_dbg_user, vha, 0x7053,
107 "No valid FCP Priority data entries.\n");
09ff701a 108 ret = 0;
2f0f3f4f
MI
109 } else {
110 /* FCP priority data is valid */
7c3df132
SK
111 ql_dbg(ql_dbg_user, vha, 0x7054,
112 "Valid FCP priority data. num entries = %d.\n",
113 num_valid);
2f0f3f4f 114 }
09ff701a
SR
115
116 return ret;
117}
118
119static int
75cc8cfc 120qla24xx_proc_fcp_prio_cfg_cmd(struct bsg_job *bsg_job)
09ff701a 121{
cd21c605 122 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
01e0e15c
JT
123 struct fc_bsg_request *bsg_request = bsg_job->request;
124 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
09ff701a
SR
125 scsi_qla_host_t *vha = shost_priv(host);
126 struct qla_hw_data *ha = vha->hw;
127 int ret = 0;
128 uint32_t len;
129 uint32_t oper;
130
7ec0effd 131 if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_P3P_TYPE(ha))) {
2f0f3f4f
MI
132 ret = -EINVAL;
133 goto exit_fcp_prio_cfg;
134 }
135
09ff701a 136 /* Get the sub command */
01e0e15c 137 oper = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
09ff701a
SR
138
139 /* Only set config is allowed if config memory is not allocated */
140 if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
141 ret = -EINVAL;
142 goto exit_fcp_prio_cfg;
143 }
144 switch (oper) {
145 case QLFC_FCP_PRIO_DISABLE:
146 if (ha->flags.fcp_prio_enabled) {
147 ha->flags.fcp_prio_enabled = 0;
148 ha->fcp_prio_cfg->attributes &=
149 ~FCP_PRIO_ATTR_ENABLE;
150 qla24xx_update_all_fcp_prio(vha);
01e0e15c 151 bsg_reply->result = DID_OK;
09ff701a
SR
152 } else {
153 ret = -EINVAL;
01e0e15c 154 bsg_reply->result = (DID_ERROR << 16);
09ff701a
SR
155 goto exit_fcp_prio_cfg;
156 }
157 break;
158
159 case QLFC_FCP_PRIO_ENABLE:
160 if (!ha->flags.fcp_prio_enabled) {
161 if (ha->fcp_prio_cfg) {
162 ha->flags.fcp_prio_enabled = 1;
163 ha->fcp_prio_cfg->attributes |=
164 FCP_PRIO_ATTR_ENABLE;
165 qla24xx_update_all_fcp_prio(vha);
01e0e15c 166 bsg_reply->result = DID_OK;
09ff701a
SR
167 } else {
168 ret = -EINVAL;
01e0e15c 169 bsg_reply->result = (DID_ERROR << 16);
09ff701a
SR
170 goto exit_fcp_prio_cfg;
171 }
172 }
173 break;
174
175 case QLFC_FCP_PRIO_GET_CONFIG:
176 len = bsg_job->reply_payload.payload_len;
177 if (!len || len > FCP_PRIO_CFG_SIZE) {
178 ret = -EINVAL;
01e0e15c 179 bsg_reply->result = (DID_ERROR << 16);
09ff701a
SR
180 goto exit_fcp_prio_cfg;
181 }
182
01e0e15c
JT
183 bsg_reply->result = DID_OK;
184 bsg_reply->reply_payload_rcv_len =
09ff701a
SR
185 sg_copy_from_buffer(
186 bsg_job->reply_payload.sg_list,
187 bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
188 len);
189
190 break;
191
192 case QLFC_FCP_PRIO_SET_CONFIG:
193 len = bsg_job->request_payload.payload_len;
194 if (!len || len > FCP_PRIO_CFG_SIZE) {
01e0e15c 195 bsg_reply->result = (DID_ERROR << 16);
09ff701a
SR
196 ret = -EINVAL;
197 goto exit_fcp_prio_cfg;
198 }
199
200 if (!ha->fcp_prio_cfg) {
201 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
202 if (!ha->fcp_prio_cfg) {
7c3df132
SK
203 ql_log(ql_log_warn, vha, 0x7050,
204 "Unable to allocate memory for fcp prio "
205 "config data (%x).\n", FCP_PRIO_CFG_SIZE);
01e0e15c 206 bsg_reply->result = (DID_ERROR << 16);
09ff701a
SR
207 ret = -ENOMEM;
208 goto exit_fcp_prio_cfg;
209 }
210 }
211
212 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
213 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
214 bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
215 FCP_PRIO_CFG_SIZE);
216
217 /* validate fcp priority data */
7c3df132
SK
218
219 if (!qla24xx_fcp_prio_cfg_valid(vha,
220 (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
01e0e15c 221 bsg_reply->result = (DID_ERROR << 16);
09ff701a
SR
222 ret = -EINVAL;
223 /* If buffer was invalidatic int
224 * fcp_prio_cfg is of no use
225 */
226 vfree(ha->fcp_prio_cfg);
227 ha->fcp_prio_cfg = NULL;
228 goto exit_fcp_prio_cfg;
229 }
230
231 ha->flags.fcp_prio_enabled = 0;
232 if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
233 ha->flags.fcp_prio_enabled = 1;
234 qla24xx_update_all_fcp_prio(vha);
01e0e15c 235 bsg_reply->result = DID_OK;
09ff701a
SR
236 break;
237 default:
238 ret = -EINVAL;
239 break;
240 }
241exit_fcp_prio_cfg:
63ea923a 242 if (!ret)
06548160 243 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 244 bsg_reply->reply_payload_rcv_len);
09ff701a
SR
245 return ret;
246}
9ba56b95 247
6e98016c 248static int
75cc8cfc 249qla2x00_process_els(struct bsg_job *bsg_job)
6e98016c 250{
01e0e15c 251 struct fc_bsg_request *bsg_request = bsg_job->request;
6e98016c 252 struct fc_rport *rport;
08f71e09 253 fc_port_t *fcport = NULL;
6e98016c
GM
254 struct Scsi_Host *host;
255 scsi_qla_host_t *vha;
256 struct qla_hw_data *ha;
257 srb_t *sp;
258 const char *type;
259 int req_sg_cnt, rsp_sg_cnt;
260 int rval = (DRIVER_ERROR << 16);
261 uint16_t nextlid = 0;
6e98016c 262
01e0e15c 263 if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
1d69b122 264 rport = fc_bsg_to_rport(bsg_job);
08f71e09
HZ
265 fcport = *(fc_port_t **) rport->dd_data;
266 host = rport_to_shost(rport);
267 vha = shost_priv(host);
268 ha = vha->hw;
269 type = "FC_BSG_RPT_ELS";
270 } else {
cd21c605 271 host = fc_bsg_to_shost(bsg_job);
08f71e09
HZ
272 vha = shost_priv(host);
273 ha = vha->hw;
274 type = "FC_BSG_HST_ELS_NOLOGIN";
275 }
276
8c0eb596
BVA
277 if (!vha->flags.online) {
278 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
279 rval = -EIO;
280 goto done;
281 }
282
08f71e09
HZ
283 /* pass through is supported only for ISP 4Gb or higher */
284 if (!IS_FWI2_CAPABLE(ha)) {
7c3df132
SK
285 ql_dbg(ql_dbg_user, vha, 0x7001,
286 "ELS passthru not supported for ISP23xx based adapters.\n");
08f71e09
HZ
287 rval = -EPERM;
288 goto done;
289 }
290
6e98016c
GM
291 /* Multiple SG's are not supported for ELS requests */
292 if (bsg_job->request_payload.sg_cnt > 1 ||
293 bsg_job->reply_payload.sg_cnt > 1) {
7c3df132 294 ql_dbg(ql_dbg_user, vha, 0x7002,
0bf0efa1 295 "Multiple SG's are not supported for ELS requests, "
7c3df132
SK
296 "request_sg_cnt=%x reply_sg_cnt=%x.\n",
297 bsg_job->request_payload.sg_cnt,
298 bsg_job->reply_payload.sg_cnt);
6e98016c
GM
299 rval = -EPERM;
300 goto done;
301 }
302
303 /* ELS request for rport */
01e0e15c 304 if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
6e98016c
GM
305 /* make sure the rport is logged in,
306 * if not perform fabric login
307 */
308 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
7c3df132
SK
309 ql_dbg(ql_dbg_user, vha, 0x7003,
310 "Failed to login port %06X for ELS passthru.\n",
311 fcport->d_id.b24);
6e98016c
GM
312 rval = -EIO;
313 goto done;
314 }
315 } else {
6e98016c
GM
316 /* Allocate a dummy fcport structure, since functions
317 * preparing the IOCB and mailbox command retrieves port
318 * specific information from fcport structure. For Host based
319 * ELS commands there will be no fcport structure allocated
320 */
321 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
322 if (!fcport) {
323 rval = -ENOMEM;
324 goto done;
325 }
326
327 /* Initialize all required fields of fcport */
328 fcport->vha = vha;
6e98016c 329 fcport->d_id.b.al_pa =
01e0e15c 330 bsg_request->rqst_data.h_els.port_id[0];
6e98016c 331 fcport->d_id.b.area =
01e0e15c 332 bsg_request->rqst_data.h_els.port_id[1];
6e98016c 333 fcport->d_id.b.domain =
01e0e15c 334 bsg_request->rqst_data.h_els.port_id[2];
6e98016c
GM
335 fcport->loop_id =
336 (fcport->d_id.b.al_pa == 0xFD) ?
337 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
338 }
339
6e98016c
GM
340 req_sg_cnt =
341 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
342 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
343 if (!req_sg_cnt) {
344 rval = -ENOMEM;
345 goto done_free_fcport;
346 }
6c452a45
AV
347
348 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
349 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
6e98016c
GM
350 if (!rsp_sg_cnt) {
351 rval = -ENOMEM;
352 goto done_free_fcport;
353 }
354
355 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
6c452a45 356 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
357 ql_log(ql_log_warn, vha, 0x7008,
358 "dma mapping resulted in different sg counts, "
359 "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
360 "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
361 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
6e98016c
GM
362 rval = -EAGAIN;
363 goto done_unmap_sg;
364 }
365
366 /* Alloc SRB structure */
9ba56b95 367 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
6e98016c
GM
368 if (!sp) {
369 rval = -ENOMEM;
6c452a45 370 goto done_unmap_sg;
6e98016c
GM
371 }
372
9ba56b95 373 sp->type =
01e0e15c
JT
374 (bsg_request->msgcode == FC_BSG_RPT_ELS ?
375 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
9ba56b95 376 sp->name =
01e0e15c
JT
377 (bsg_request->msgcode == FC_BSG_RPT_ELS ?
378 "bsg_els_rpt" : "bsg_els_hst");
9ba56b95
GM
379 sp->u.bsg_job = bsg_job;
380 sp->free = qla2x00_bsg_sp_free;
381 sp->done = qla2x00_bsg_job_done;
6e98016c 382
7c3df132
SK
383 ql_dbg(ql_dbg_user, vha, 0x700a,
384 "bsg rqst type: %s els type: %x - loop-id=%x "
385 "portid=%-2x%02x%02x.\n", type,
01e0e15c 386 bsg_request->rqst_data.h_els.command_code, fcport->loop_id,
7c3df132 387 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
6e98016c
GM
388
389 rval = qla2x00_start_sp(sp);
390 if (rval != QLA_SUCCESS) {
7c3df132
SK
391 ql_log(ql_log_warn, vha, 0x700e,
392 "qla2x00_start_sp failed = %d\n", rval);
25ff6af1 393 qla2x00_rel_sp(sp);
6e98016c
GM
394 rval = -EIO;
395 goto done_unmap_sg;
396 }
397 return rval;
398
399done_unmap_sg:
400 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
401 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
402 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
403 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
404 goto done_free_fcport;
405
406done_free_fcport:
01e0e15c 407 if (bsg_request->msgcode == FC_BSG_RPT_ELS)
6e98016c
GM
408 kfree(fcport);
409done:
410 return rval;
411}
412
2374dd23 413static inline uint16_t
5780790e
AV
414qla24xx_calc_ct_iocbs(uint16_t dsds)
415{
416 uint16_t iocbs;
417
418 iocbs = 1;
419 if (dsds > 2) {
420 iocbs += (dsds - 2) / 5;
421 if ((dsds - 2) % 5)
422 iocbs++;
423 }
424 return iocbs;
425}
426
6e98016c 427static int
75cc8cfc 428qla2x00_process_ct(struct bsg_job *bsg_job)
6e98016c
GM
429{
430 srb_t *sp;
01e0e15c 431 struct fc_bsg_request *bsg_request = bsg_job->request;
cd21c605 432 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
6e98016c
GM
433 scsi_qla_host_t *vha = shost_priv(host);
434 struct qla_hw_data *ha = vha->hw;
435 int rval = (DRIVER_ERROR << 16);
436 int req_sg_cnt, rsp_sg_cnt;
437 uint16_t loop_id;
438 struct fc_port *fcport;
439 char *type = "FC_BSG_HST_CT";
6e98016c 440
6e98016c
GM
441 req_sg_cnt =
442 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
443 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
6c452a45 444 if (!req_sg_cnt) {
7c3df132
SK
445 ql_log(ql_log_warn, vha, 0x700f,
446 "dma_map_sg return %d for request\n", req_sg_cnt);
6e98016c
GM
447 rval = -ENOMEM;
448 goto done;
449 }
450
451 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
452 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
453 if (!rsp_sg_cnt) {
7c3df132
SK
454 ql_log(ql_log_warn, vha, 0x7010,
455 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
6e98016c
GM
456 rval = -ENOMEM;
457 goto done;
458 }
459
460 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
6c452a45 461 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
462 ql_log(ql_log_warn, vha, 0x7011,
463 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
464 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
465 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
6e98016c 466 rval = -EAGAIN;
6c452a45 467 goto done_unmap_sg;
6e98016c
GM
468 }
469
470 if (!vha->flags.online) {
7c3df132
SK
471 ql_log(ql_log_warn, vha, 0x7012,
472 "Host is not online.\n");
6e98016c
GM
473 rval = -EIO;
474 goto done_unmap_sg;
475 }
476
477 loop_id =
01e0e15c 478 (bsg_request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
6e98016c
GM
479 >> 24;
480 switch (loop_id) {
6c452a45
AV
481 case 0xFC:
482 loop_id = cpu_to_le16(NPH_SNS);
483 break;
484 case 0xFA:
485 loop_id = vha->mgmt_svr_loop_id;
486 break;
487 default:
7c3df132
SK
488 ql_dbg(ql_dbg_user, vha, 0x7013,
489 "Unknown loop id: %x.\n", loop_id);
6c452a45
AV
490 rval = -EINVAL;
491 goto done_unmap_sg;
6e98016c
GM
492 }
493
494 /* Allocate a dummy fcport structure, since functions preparing the
495 * IOCB and mailbox command retrieves port specific information
496 * from fcport structure. For Host based ELS commands there will be
497 * no fcport structure allocated
498 */
499 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6c452a45 500 if (!fcport) {
7c3df132
SK
501 ql_log(ql_log_warn, vha, 0x7014,
502 "Failed to allocate fcport.\n");
6e98016c 503 rval = -ENOMEM;
6c452a45 504 goto done_unmap_sg;
6e98016c
GM
505 }
506
507 /* Initialize all required fields of fcport */
508 fcport->vha = vha;
01e0e15c
JT
509 fcport->d_id.b.al_pa = bsg_request->rqst_data.h_ct.port_id[0];
510 fcport->d_id.b.area = bsg_request->rqst_data.h_ct.port_id[1];
511 fcport->d_id.b.domain = bsg_request->rqst_data.h_ct.port_id[2];
6e98016c
GM
512 fcport->loop_id = loop_id;
513
514 /* Alloc SRB structure */
9ba56b95 515 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
6e98016c 516 if (!sp) {
7c3df132 517 ql_log(ql_log_warn, vha, 0x7015,
9ba56b95 518 "qla2x00_get_sp failed.\n");
6e98016c
GM
519 rval = -ENOMEM;
520 goto done_free_fcport;
521 }
522
9ba56b95
GM
523 sp->type = SRB_CT_CMD;
524 sp->name = "bsg_ct";
525 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
526 sp->u.bsg_job = bsg_job;
527 sp->free = qla2x00_bsg_sp_free;
528 sp->done = qla2x00_bsg_job_done;
6e98016c 529
7c3df132
SK
530 ql_dbg(ql_dbg_user, vha, 0x7016,
531 "bsg rqst type: %s else type: %x - "
532 "loop-id=%x portid=%02x%02x%02x.\n", type,
01e0e15c 533 (bsg_request->rqst_data.h_ct.preamble_word2 >> 16),
7c3df132
SK
534 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
535 fcport->d_id.b.al_pa);
6e98016c
GM
536
537 rval = qla2x00_start_sp(sp);
538 if (rval != QLA_SUCCESS) {
7c3df132
SK
539 ql_log(ql_log_warn, vha, 0x7017,
540 "qla2x00_start_sp failed=%d.\n", rval);
25ff6af1 541 qla2x00_rel_sp(sp);
6e98016c
GM
542 rval = -EIO;
543 goto done_free_fcport;
544 }
545 return rval;
546
547done_free_fcport:
548 kfree(fcport);
549done_unmap_sg:
550 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
551 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
552 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
553 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
554done:
555 return rval;
556}
67b2a31f
CD
557
558/* Disable loopback mode */
559static inline int
560qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
f356bef1 561 int wait, int wait2)
67b2a31f
CD
562{
563 int ret = 0;
564 int rval = 0;
565 uint16_t new_config[4];
566 struct qla_hw_data *ha = vha->hw;
567
7ec0effd 568 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
67b2a31f
CD
569 goto done_reset_internal;
570
571 memset(new_config, 0 , sizeof(new_config));
572 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
573 ENABLE_INTERNAL_LOOPBACK ||
574 (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
575 ENABLE_EXTERNAL_LOOPBACK) {
576 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
577 ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
578 (new_config[0] & INTERNAL_LOOPBACK_MASK));
579 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
580
581 ha->notify_dcbx_comp = wait;
f356bef1
CD
582 ha->notify_lb_portup_comp = wait2;
583
67b2a31f
CD
584 ret = qla81xx_set_port_config(vha, new_config);
585 if (ret != QLA_SUCCESS) {
586 ql_log(ql_log_warn, vha, 0x7025,
587 "Set port config failed.\n");
588 ha->notify_dcbx_comp = 0;
f356bef1 589 ha->notify_lb_portup_comp = 0;
67b2a31f
CD
590 rval = -EINVAL;
591 goto done_reset_internal;
592 }
593
594 /* Wait for DCBX complete event */
595 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
f356bef1 596 (DCBX_COMP_TIMEOUT * HZ))) {
67b2a31f 597 ql_dbg(ql_dbg_user, vha, 0x7026,
f356bef1 598 "DCBX completion not received.\n");
67b2a31f 599 ha->notify_dcbx_comp = 0;
f356bef1 600 ha->notify_lb_portup_comp = 0;
67b2a31f
CD
601 rval = -EINVAL;
602 goto done_reset_internal;
603 } else
604 ql_dbg(ql_dbg_user, vha, 0x7027,
f356bef1
CD
605 "DCBX completion received.\n");
606
607 if (wait2 &&
608 !wait_for_completion_timeout(&ha->lb_portup_comp,
609 (LB_PORTUP_COMP_TIMEOUT * HZ))) {
610 ql_dbg(ql_dbg_user, vha, 0x70c5,
611 "Port up completion not received.\n");
612 ha->notify_lb_portup_comp = 0;
613 rval = -EINVAL;
614 goto done_reset_internal;
615 } else
616 ql_dbg(ql_dbg_user, vha, 0x70c6,
617 "Port up completion received.\n");
67b2a31f
CD
618
619 ha->notify_dcbx_comp = 0;
f356bef1 620 ha->notify_lb_portup_comp = 0;
67b2a31f
CD
621 }
622done_reset_internal:
623 return rval;
624}
625
8fcd6b8b
CD
626/*
627 * Set the port configuration to enable the internal or external loopback
628 * depending on the loopback mode.
23f2ebd1
SR
629 */
630static inline int
8fcd6b8b
CD
631qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
632 uint16_t *new_config, uint16_t mode)
23f2ebd1
SR
633{
634 int ret = 0;
635 int rval = 0;
454073c9 636 unsigned long rem_tmo = 0, current_tmo = 0;
23f2ebd1
SR
637 struct qla_hw_data *ha = vha->hw;
638
7ec0effd 639 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
23f2ebd1
SR
640 goto done_set_internal;
641
8fcd6b8b
CD
642 if (mode == INTERNAL_LOOPBACK)
643 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
644 else if (mode == EXTERNAL_LOOPBACK)
645 new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
646 ql_dbg(ql_dbg_user, vha, 0x70be,
647 "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
648
649 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
23f2ebd1
SR
650
651 ha->notify_dcbx_comp = 1;
652 ret = qla81xx_set_port_config(vha, new_config);
653 if (ret != QLA_SUCCESS) {
7c3df132
SK
654 ql_log(ql_log_warn, vha, 0x7021,
655 "set port config failed.\n");
23f2ebd1
SR
656 ha->notify_dcbx_comp = 0;
657 rval = -EINVAL;
658 goto done_set_internal;
659 }
660
661 /* Wait for DCBX complete event */
454073c9
SV
662 current_tmo = DCBX_COMP_TIMEOUT * HZ;
663 while (1) {
664 rem_tmo = wait_for_completion_timeout(&ha->dcbx_comp,
665 current_tmo);
666 if (!ha->idc_extend_tmo || rem_tmo) {
667 ha->idc_extend_tmo = 0;
668 break;
669 }
670 current_tmo = ha->idc_extend_tmo * HZ;
671 ha->idc_extend_tmo = 0;
672 }
673
674 if (!rem_tmo) {
7c3df132 675 ql_dbg(ql_dbg_user, vha, 0x7022,
f356bef1
CD
676 "DCBX completion not received.\n");
677 ret = qla81xx_reset_loopback_mode(vha, new_config, 0, 0);
67b2a31f
CD
678 /*
679 * If the reset of the loopback mode doesn't work take a FCoE
680 * dump and reset the chip.
681 */
682 if (ret) {
683 ha->isp_ops->fw_dump(vha, 0);
684 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
685 }
bf5b8ad7
CD
686 rval = -EINVAL;
687 } else {
688 if (ha->flags.idc_compl_status) {
689 ql_dbg(ql_dbg_user, vha, 0x70c3,
690 "Bad status in IDC Completion AEN\n");
691 rval = -EINVAL;
692 ha->flags.idc_compl_status = 0;
693 } else
694 ql_dbg(ql_dbg_user, vha, 0x7023,
f356bef1 695 "DCBX completion received.\n");
bf5b8ad7 696 }
23f2ebd1
SR
697
698 ha->notify_dcbx_comp = 0;
454073c9 699 ha->idc_extend_tmo = 0;
23f2ebd1
SR
700
701done_set_internal:
702 return rval;
703}
704
6e98016c 705static int
75cc8cfc 706qla2x00_process_loopback(struct bsg_job *bsg_job)
6e98016c 707{
01e0e15c
JT
708 struct fc_bsg_request *bsg_request = bsg_job->request;
709 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 710 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
6e98016c
GM
711 scsi_qla_host_t *vha = shost_priv(host);
712 struct qla_hw_data *ha = vha->hw;
713 int rval;
714 uint8_t command_sent;
715 char *type;
716 struct msg_echo_lb elreq;
717 uint16_t response[MAILBOX_REGISTER_COUNT];
23f2ebd1 718 uint16_t config[4], new_config[4];
6c452a45 719 uint8_t *fw_sts_ptr;
6e98016c
GM
720 uint8_t *req_data = NULL;
721 dma_addr_t req_data_dma;
722 uint32_t req_data_len;
723 uint8_t *rsp_data = NULL;
724 dma_addr_t rsp_data_dma;
725 uint32_t rsp_data_len;
726
6e98016c 727 if (!vha->flags.online) {
7c3df132 728 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
6e98016c
GM
729 return -EIO;
730 }
731
1d634965
JC
732 memset(&elreq, 0, sizeof(elreq));
733
6e98016c
GM
734 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
735 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
736 DMA_TO_DEVICE);
737
7c3df132
SK
738 if (!elreq.req_sg_cnt) {
739 ql_log(ql_log_warn, vha, 0x701a,
740 "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
6e98016c 741 return -ENOMEM;
7c3df132 742 }
6e98016c
GM
743
744 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
745 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
746 DMA_FROM_DEVICE);
747
748 if (!elreq.rsp_sg_cnt) {
7c3df132
SK
749 ql_log(ql_log_warn, vha, 0x701b,
750 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
6e98016c
GM
751 rval = -ENOMEM;
752 goto done_unmap_req_sg;
6c452a45 753 }
6e98016c
GM
754
755 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
756 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
757 ql_log(ql_log_warn, vha, 0x701c,
758 "dma mapping resulted in different sg counts, "
759 "request_sg_cnt: %x dma_request_sg_cnt: %x "
760 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
761 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
762 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
6e98016c
GM
763 rval = -EAGAIN;
764 goto done_unmap_sg;
765 }
766 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
767 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
768 &req_data_dma, GFP_KERNEL);
769 if (!req_data) {
7c3df132
SK
770 ql_log(ql_log_warn, vha, 0x701d,
771 "dma alloc failed for req_data.\n");
6e98016c
GM
772 rval = -ENOMEM;
773 goto done_unmap_sg;
774 }
775
776 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
777 &rsp_data_dma, GFP_KERNEL);
778 if (!rsp_data) {
7c3df132
SK
779 ql_log(ql_log_warn, vha, 0x7004,
780 "dma alloc failed for rsp_data.\n");
6e98016c
GM
781 rval = -ENOMEM;
782 goto done_free_dma_req;
783 }
784
785 /* Copy the request buffer in req_data now */
786 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
787 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
788
789 elreq.send_dma = req_data_dma;
790 elreq.rcv_dma = rsp_data_dma;
791 elreq.transfer_size = req_data_len;
792
01e0e15c 793 elreq.options = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
1b98b421 794 elreq.iteration_count =
01e0e15c 795 bsg_request->rqst_data.h_vendor.vendor_cmd[2];
6e98016c 796
8fcd6b8b
CD
797 if (atomic_read(&vha->loop_state) == LOOP_READY &&
798 (ha->current_topology == ISP_CFG_F ||
1d634965
JC
799 (le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE &&
800 req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
801 elreq.options == EXTERNAL_LOOPBACK) {
23f2ebd1 802 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
7c3df132
SK
803 ql_dbg(ql_dbg_user, vha, 0x701e,
804 "BSG request type: %s.\n", type);
23f2ebd1
SR
805 command_sent = INT_DEF_LB_ECHO_CMD;
806 rval = qla2x00_echo_test(vha, &elreq, response);
807 } else {
7ec0effd 808 if (IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) {
23f2ebd1
SR
809 memset(config, 0, sizeof(config));
810 memset(new_config, 0, sizeof(new_config));
f356bef1 811
23f2ebd1 812 if (qla81xx_get_port_config(vha, config)) {
7c3df132
SK
813 ql_log(ql_log_warn, vha, 0x701f,
814 "Get port config failed.\n");
23f2ebd1 815 rval = -EPERM;
9bceab4e 816 goto done_free_dma_rsp;
23f2ebd1
SR
817 }
818
1bcc46cb
CD
819 if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
820 ql_dbg(ql_dbg_user, vha, 0x70c4,
821 "Loopback operation already in "
822 "progress.\n");
823 rval = -EAGAIN;
824 goto done_free_dma_rsp;
825 }
826
8fcd6b8b
CD
827 ql_dbg(ql_dbg_user, vha, 0x70c0,
828 "elreq.options=%04x\n", elreq.options);
829
830 if (elreq.options == EXTERNAL_LOOPBACK)
7ec0effd 831 if (IS_QLA8031(ha) || IS_QLA8044(ha))
8fcd6b8b
CD
832 rval = qla81xx_set_loopback_mode(vha,
833 config, new_config, elreq.options);
834 else
835 rval = qla81xx_reset_loopback_mode(vha,
f356bef1 836 config, 1, 0);
8fcd6b8b
CD
837 else
838 rval = qla81xx_set_loopback_mode(vha, config,
839 new_config, elreq.options);
840
841 if (rval) {
8fcd6b8b 842 rval = -EPERM;
9bceab4e 843 goto done_free_dma_rsp;
23f2ebd1
SR
844 }
845
846 type = "FC_BSG_HST_VENDOR_LOOPBACK";
7c3df132
SK
847 ql_dbg(ql_dbg_user, vha, 0x7028,
848 "BSG request type: %s.\n", type);
23f2ebd1
SR
849
850 command_sent = INT_DEF_LB_LOOPBACK_CMD;
851 rval = qla2x00_loopback_test(vha, &elreq, response);
852
992357c6
CD
853 if (response[0] == MBS_COMMAND_ERROR &&
854 response[1] == MBS_LB_RESET) {
855 ql_log(ql_log_warn, vha, 0x7029,
856 "MBX command error, Aborting ISP.\n");
857 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
858 qla2xxx_wake_dpc(vha);
859 qla2x00_wait_for_chip_reset(vha);
860 /* Also reset the MPI */
861 if (IS_QLA81XX(ha)) {
862 if (qla81xx_restart_mpi_firmware(vha) !=
863 QLA_SUCCESS) {
864 ql_log(ql_log_warn, vha, 0x702a,
865 "MPI reset failed.\n");
866 }
867 }
868
869 rval = -EIO;
870 goto done_free_dma_rsp;
871 }
872
4052bd57 873 if (new_config[0]) {
67b2a31f
CD
874 int ret;
875
23f2ebd1
SR
876 /* Revert back to original port config
877 * Also clear internal loopback
878 */
67b2a31f 879 ret = qla81xx_reset_loopback_mode(vha,
f356bef1 880 new_config, 0, 1);
67b2a31f
CD
881 if (ret) {
882 /*
883 * If the reset of the loopback mode
884 * doesn't work take FCoE dump and then
885 * reset the chip.
886 */
887 ha->isp_ops->fw_dump(vha, 0);
888 set_bit(ISP_ABORT_NEEDED,
889 &vha->dpc_flags);
890 }
891
23f2ebd1
SR
892 }
893
23f2ebd1
SR
894 } else {
895 type = "FC_BSG_HST_VENDOR_LOOPBACK";
7c3df132
SK
896 ql_dbg(ql_dbg_user, vha, 0x702b,
897 "BSG request type: %s.\n", type);
23f2ebd1
SR
898 command_sent = INT_DEF_LB_LOOPBACK_CMD;
899 rval = qla2x00_loopback_test(vha, &elreq, response);
6e98016c 900 }
6e98016c
GM
901 }
902
903 if (rval) {
7c3df132
SK
904 ql_log(ql_log_warn, vha, 0x702c,
905 "Vendor request %s failed.\n", type);
6e98016c 906
6e98016c 907 rval = 0;
01e0e15c
JT
908 bsg_reply->result = (DID_ERROR << 16);
909 bsg_reply->reply_payload_rcv_len = 0;
6e98016c 910 } else {
7c3df132
SK
911 ql_dbg(ql_dbg_user, vha, 0x702d,
912 "Vendor request %s completed.\n", type);
01e0e15c 913 bsg_reply->result = (DID_OK << 16);
6e98016c
GM
914 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
915 bsg_job->reply_payload.sg_cnt, rsp_data,
916 rsp_data_len);
917 }
63ea923a
AB
918
919 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
920 sizeof(response) + sizeof(uint8_t);
05231a3b
CH
921 fw_sts_ptr = bsg_job->reply + sizeof(struct fc_bsg_reply);
922 memcpy(bsg_job->reply + sizeof(struct fc_bsg_reply), response,
923 sizeof(response));
63ea923a
AB
924 fw_sts_ptr += sizeof(response);
925 *fw_sts_ptr = command_sent;
6e98016c 926
9bceab4e 927done_free_dma_rsp:
6e98016c
GM
928 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
929 rsp_data, rsp_data_dma);
930done_free_dma_req:
931 dma_free_coherent(&ha->pdev->dev, req_data_len,
932 req_data, req_data_dma);
933done_unmap_sg:
934 dma_unmap_sg(&ha->pdev->dev,
935 bsg_job->reply_payload.sg_list,
936 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
937done_unmap_req_sg:
938 dma_unmap_sg(&ha->pdev->dev,
939 bsg_job->request_payload.sg_list,
940 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
63ea923a 941 if (!rval)
06548160 942 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 943 bsg_reply->reply_payload_rcv_len);
6c452a45 944 return rval;
6e98016c
GM
945}
946
947static int
75cc8cfc 948qla84xx_reset(struct bsg_job *bsg_job)
6e98016c 949{
01e0e15c 950 struct fc_bsg_request *bsg_request = bsg_job->request;
cd21c605 951 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
01e0e15c 952 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
6e98016c
GM
953 scsi_qla_host_t *vha = shost_priv(host);
954 struct qla_hw_data *ha = vha->hw;
955 int rval = 0;
956 uint32_t flag;
957
6e98016c 958 if (!IS_QLA84XX(ha)) {
7c3df132 959 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
6e98016c
GM
960 return -EINVAL;
961 }
962
01e0e15c 963 flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
6e98016c
GM
964
965 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
966
967 if (rval) {
7c3df132
SK
968 ql_log(ql_log_warn, vha, 0x7030,
969 "Vendor request 84xx reset failed.\n");
63ea923a 970 rval = (DID_ERROR << 16);
6e98016c
GM
971
972 } else {
7c3df132
SK
973 ql_dbg(ql_dbg_user, vha, 0x7031,
974 "Vendor request 84xx reset completed.\n");
01e0e15c 975 bsg_reply->result = DID_OK;
06548160 976 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 977 bsg_reply->reply_payload_rcv_len);
6e98016c
GM
978 }
979
6e98016c
GM
980 return rval;
981}
982
983static int
75cc8cfc 984qla84xx_updatefw(struct bsg_job *bsg_job)
6e98016c 985{
01e0e15c
JT
986 struct fc_bsg_request *bsg_request = bsg_job->request;
987 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 988 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
6e98016c
GM
989 scsi_qla_host_t *vha = shost_priv(host);
990 struct qla_hw_data *ha = vha->hw;
991 struct verify_chip_entry_84xx *mn = NULL;
992 dma_addr_t mn_dma, fw_dma;
993 void *fw_buf = NULL;
994 int rval = 0;
995 uint32_t sg_cnt;
996 uint32_t data_len;
997 uint16_t options;
998 uint32_t flag;
999 uint32_t fw_ver;
1000
6e98016c 1001 if (!IS_QLA84XX(ha)) {
7c3df132
SK
1002 ql_dbg(ql_dbg_user, vha, 0x7032,
1003 "Not 84xx, exiting.\n");
6e98016c
GM
1004 return -EINVAL;
1005 }
1006
1007 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1008 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
7c3df132
SK
1009 if (!sg_cnt) {
1010 ql_log(ql_log_warn, vha, 0x7033,
1011 "dma_map_sg returned %d for request.\n", sg_cnt);
6e98016c 1012 return -ENOMEM;
7c3df132 1013 }
6e98016c
GM
1014
1015 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
7c3df132
SK
1016 ql_log(ql_log_warn, vha, 0x7034,
1017 "DMA mapping resulted in different sg counts, "
1018 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1019 bsg_job->request_payload.sg_cnt, sg_cnt);
6e98016c
GM
1020 rval = -EAGAIN;
1021 goto done_unmap_sg;
1022 }
1023
1024 data_len = bsg_job->request_payload.payload_len;
1025 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
1026 &fw_dma, GFP_KERNEL);
1027 if (!fw_buf) {
7c3df132
SK
1028 ql_log(ql_log_warn, vha, 0x7035,
1029 "DMA alloc failed for fw_buf.\n");
6e98016c
GM
1030 rval = -ENOMEM;
1031 goto done_unmap_sg;
1032 }
1033
1034 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1035 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
1036
501017f6 1037 mn = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
6e98016c 1038 if (!mn) {
7c3df132
SK
1039 ql_log(ql_log_warn, vha, 0x7036,
1040 "DMA alloc failed for fw buffer.\n");
6e98016c
GM
1041 rval = -ENOMEM;
1042 goto done_free_fw_buf;
1043 }
1044
01e0e15c 1045 flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
6e98016c
GM
1046 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
1047
6e98016c
GM
1048 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
1049 mn->entry_count = 1;
1050
1051 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
1052 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
1053 options |= VCO_DIAG_FW;
1054
1055 mn->options = cpu_to_le16(options);
1056 mn->fw_ver = cpu_to_le32(fw_ver);
1057 mn->fw_size = cpu_to_le32(data_len);
1058 mn->fw_seq_size = cpu_to_le32(data_len);
1059 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
1060 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
1061 mn->dseg_length = cpu_to_le32(data_len);
1062 mn->data_seg_cnt = cpu_to_le16(1);
1063
1064 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
1065
1066 if (rval) {
7c3df132
SK
1067 ql_log(ql_log_warn, vha, 0x7037,
1068 "Vendor request 84xx updatefw failed.\n");
6e98016c 1069
63ea923a 1070 rval = (DID_ERROR << 16);
6e98016c 1071 } else {
7c3df132
SK
1072 ql_dbg(ql_dbg_user, vha, 0x7038,
1073 "Vendor request 84xx updatefw completed.\n");
6e98016c
GM
1074
1075 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 1076 bsg_reply->result = DID_OK;
6e98016c
GM
1077 }
1078
6e98016c
GM
1079 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1080
1081done_free_fw_buf:
1082 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
1083
1084done_unmap_sg:
1085 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1086 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1087
63ea923a 1088 if (!rval)
06548160 1089 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1090 bsg_reply->reply_payload_rcv_len);
6e98016c
GM
1091 return rval;
1092}
1093
1094static int
75cc8cfc 1095qla84xx_mgmt_cmd(struct bsg_job *bsg_job)
6e98016c 1096{
01e0e15c
JT
1097 struct fc_bsg_request *bsg_request = bsg_job->request;
1098 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1099 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
6e98016c
GM
1100 scsi_qla_host_t *vha = shost_priv(host);
1101 struct qla_hw_data *ha = vha->hw;
1102 struct access_chip_84xx *mn = NULL;
1103 dma_addr_t mn_dma, mgmt_dma;
1104 void *mgmt_b = NULL;
1105 int rval = 0;
1106 struct qla_bsg_a84_mgmt *ql84_mgmt;
1107 uint32_t sg_cnt;
d5459083 1108 uint32_t data_len = 0;
6e98016c
GM
1109 uint32_t dma_direction = DMA_NONE;
1110
6e98016c 1111 if (!IS_QLA84XX(ha)) {
7c3df132
SK
1112 ql_log(ql_log_warn, vha, 0x703a,
1113 "Not 84xx, exiting.\n");
6e98016c
GM
1114 return -EINVAL;
1115 }
1116
08eb7f45 1117 mn = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
6e98016c 1118 if (!mn) {
7c3df132
SK
1119 ql_log(ql_log_warn, vha, 0x703c,
1120 "DMA alloc failed for fw buffer.\n");
6e98016c
GM
1121 return -ENOMEM;
1122 }
1123
6e98016c
GM
1124 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1125 mn->entry_count = 1;
01e0e15c 1126 ql84_mgmt = (void *)bsg_request + sizeof(struct fc_bsg_request);
6e98016c
GM
1127 switch (ql84_mgmt->mgmt.cmd) {
1128 case QLA84_MGMT_READ_MEM:
1129 case QLA84_MGMT_GET_INFO:
1130 sg_cnt = dma_map_sg(&ha->pdev->dev,
1131 bsg_job->reply_payload.sg_list,
1132 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1133 if (!sg_cnt) {
7c3df132
SK
1134 ql_log(ql_log_warn, vha, 0x703d,
1135 "dma_map_sg returned %d for reply.\n", sg_cnt);
6e98016c
GM
1136 rval = -ENOMEM;
1137 goto exit_mgmt;
1138 }
1139
1140 dma_direction = DMA_FROM_DEVICE;
1141
1142 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
7c3df132
SK
1143 ql_log(ql_log_warn, vha, 0x703e,
1144 "DMA mapping resulted in different sg counts, "
1145 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1146 bsg_job->reply_payload.sg_cnt, sg_cnt);
6e98016c
GM
1147 rval = -EAGAIN;
1148 goto done_unmap_sg;
1149 }
1150
1151 data_len = bsg_job->reply_payload.payload_len;
1152
1153 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1154 &mgmt_dma, GFP_KERNEL);
1155 if (!mgmt_b) {
7c3df132
SK
1156 ql_log(ql_log_warn, vha, 0x703f,
1157 "DMA alloc failed for mgmt_b.\n");
6e98016c
GM
1158 rval = -ENOMEM;
1159 goto done_unmap_sg;
1160 }
1161
1162 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1163 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1164 mn->parameter1 =
1165 cpu_to_le32(
1166 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1167
1168 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1169 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1170 mn->parameter1 =
1171 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1172
1173 mn->parameter2 =
1174 cpu_to_le32(
1175 ql84_mgmt->mgmt.mgmtp.u.info.context);
1176 }
1177 break;
1178
1179 case QLA84_MGMT_WRITE_MEM:
1180 sg_cnt = dma_map_sg(&ha->pdev->dev,
1181 bsg_job->request_payload.sg_list,
1182 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1183
1184 if (!sg_cnt) {
7c3df132
SK
1185 ql_log(ql_log_warn, vha, 0x7040,
1186 "dma_map_sg returned %d.\n", sg_cnt);
6e98016c
GM
1187 rval = -ENOMEM;
1188 goto exit_mgmt;
1189 }
1190
1191 dma_direction = DMA_TO_DEVICE;
1192
1193 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
7c3df132
SK
1194 ql_log(ql_log_warn, vha, 0x7041,
1195 "DMA mapping resulted in different sg counts, "
1196 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1197 bsg_job->request_payload.sg_cnt, sg_cnt);
6e98016c
GM
1198 rval = -EAGAIN;
1199 goto done_unmap_sg;
1200 }
1201
1202 data_len = bsg_job->request_payload.payload_len;
1203 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1204 &mgmt_dma, GFP_KERNEL);
1205 if (!mgmt_b) {
7c3df132
SK
1206 ql_log(ql_log_warn, vha, 0x7042,
1207 "DMA alloc failed for mgmt_b.\n");
6e98016c
GM
1208 rval = -ENOMEM;
1209 goto done_unmap_sg;
1210 }
1211
1212 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1213 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1214
1215 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1216 mn->parameter1 =
1217 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1218 break;
1219
1220 case QLA84_MGMT_CHNG_CONFIG:
1221 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1222 mn->parameter1 =
1223 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1224
1225 mn->parameter2 =
1226 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1227
1228 mn->parameter3 =
1229 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1230 break;
1231
1232 default:
1233 rval = -EIO;
1234 goto exit_mgmt;
1235 }
1236
1237 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1238 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1239 mn->dseg_count = cpu_to_le16(1);
1240 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1241 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1242 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1243 }
1244
1245 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1246
1247 if (rval) {
7c3df132
SK
1248 ql_log(ql_log_warn, vha, 0x7043,
1249 "Vendor request 84xx mgmt failed.\n");
6e98016c 1250
63ea923a 1251 rval = (DID_ERROR << 16);
6e98016c
GM
1252
1253 } else {
7c3df132
SK
1254 ql_dbg(ql_dbg_user, vha, 0x7044,
1255 "Vendor request 84xx mgmt completed.\n");
6e98016c
GM
1256
1257 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 1258 bsg_reply->result = DID_OK;
6e98016c
GM
1259
1260 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1261 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
01e0e15c 1262 bsg_reply->reply_payload_rcv_len =
6e98016c
GM
1263 bsg_job->reply_payload.payload_len;
1264
1265 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
6c452a45
AV
1266 bsg_job->reply_payload.sg_cnt, mgmt_b,
1267 data_len);
6e98016c
GM
1268 }
1269 }
1270
6e98016c 1271done_unmap_sg:
d5459083
HZ
1272 if (mgmt_b)
1273 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1274
6e98016c
GM
1275 if (dma_direction == DMA_TO_DEVICE)
1276 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1277 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1278 else if (dma_direction == DMA_FROM_DEVICE)
1279 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1280 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1281
1282exit_mgmt:
1283 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1284
63ea923a 1285 if (!rval)
06548160 1286 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1287 bsg_reply->reply_payload_rcv_len);
6e98016c
GM
1288 return rval;
1289}
1290
1291static int
75cc8cfc 1292qla24xx_iidma(struct bsg_job *bsg_job)
6e98016c 1293{
01e0e15c
JT
1294 struct fc_bsg_request *bsg_request = bsg_job->request;
1295 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1296 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
6e98016c 1297 scsi_qla_host_t *vha = shost_priv(host);
6e98016c
GM
1298 int rval = 0;
1299 struct qla_port_param *port_param = NULL;
1300 fc_port_t *fcport = NULL;
e8b8b8ad 1301 int found = 0;
6e98016c
GM
1302 uint16_t mb[MAILBOX_REGISTER_COUNT];
1303 uint8_t *rsp_ptr = NULL;
1304
6e98016c 1305 if (!IS_IIDMA_CAPABLE(vha->hw)) {
7c3df132 1306 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
6e98016c
GM
1307 return -EINVAL;
1308 }
1309
01e0e15c 1310 port_param = (void *)bsg_request + sizeof(struct fc_bsg_request);
6e98016c 1311 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
7c3df132
SK
1312 ql_log(ql_log_warn, vha, 0x7048,
1313 "Invalid destination type.\n");
6e98016c
GM
1314 return -EINVAL;
1315 }
1316
1317 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1318 if (fcport->port_type != FCT_TARGET)
1319 continue;
1320
1321 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1322 fcport->port_name, sizeof(fcport->port_name)))
1323 continue;
e8b8b8ad
JC
1324
1325 found = 1;
6e98016c
GM
1326 break;
1327 }
1328
e8b8b8ad 1329 if (!found) {
7c3df132
SK
1330 ql_log(ql_log_warn, vha, 0x7049,
1331 "Failed to find port.\n");
6e98016c
GM
1332 return -EINVAL;
1333 }
1334
c9afb9a2 1335 if (atomic_read(&fcport->state) != FCS_ONLINE) {
7c3df132
SK
1336 ql_log(ql_log_warn, vha, 0x704a,
1337 "Port is not online.\n");
17cf2c5d
MI
1338 return -EINVAL;
1339 }
1340
9a15eb4b 1341 if (fcport->flags & FCF_LOGIN_NEEDED) {
7c3df132
SK
1342 ql_log(ql_log_warn, vha, 0x704b,
1343 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
9a15eb4b
MI
1344 return -EINVAL;
1345 }
1346
6e98016c
GM
1347 if (port_param->mode)
1348 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1349 port_param->speed, mb);
1350 else
1351 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1352 &port_param->speed, mb);
1353
1354 if (rval) {
7c3df132 1355 ql_log(ql_log_warn, vha, 0x704c,
62439b48 1356 "iiDMA cmd failed for %8phN -- "
7b833558
OK
1357 "%04x %x %04x %04x.\n", fcport->port_name,
1358 rval, fcport->fp_speed, mb[0], mb[1]);
63ea923a 1359 rval = (DID_ERROR << 16);
6e98016c
GM
1360 } else {
1361 if (!port_param->mode) {
1362 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1363 sizeof(struct qla_port_param);
1364
01e0e15c 1365 rsp_ptr = ((uint8_t *)bsg_reply) +
6e98016c
GM
1366 sizeof(struct fc_bsg_reply);
1367
1368 memcpy(rsp_ptr, port_param,
1369 sizeof(struct qla_port_param));
1370 }
1371
01e0e15c 1372 bsg_reply->result = DID_OK;
06548160 1373 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1374 bsg_reply->reply_payload_rcv_len);
6e98016c
GM
1375 }
1376
6e98016c
GM
1377 return rval;
1378}
1379
f19af163 1380static int
75cc8cfc 1381qla2x00_optrom_setup(struct bsg_job *bsg_job, scsi_qla_host_t *vha,
f19af163
HZ
1382 uint8_t is_update)
1383{
01e0e15c 1384 struct fc_bsg_request *bsg_request = bsg_job->request;
f19af163
HZ
1385 uint32_t start = 0;
1386 int valid = 0;
7c3df132 1387 struct qla_hw_data *ha = vha->hw;
f19af163 1388
f19af163
HZ
1389 if (unlikely(pci_channel_offline(ha->pdev)))
1390 return -EINVAL;
1391
01e0e15c 1392 start = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
7c3df132
SK
1393 if (start > ha->optrom_size) {
1394 ql_log(ql_log_warn, vha, 0x7055,
1395 "start %d > optrom_size %d.\n", start, ha->optrom_size);
f19af163 1396 return -EINVAL;
7c3df132 1397 }
f19af163 1398
7c3df132
SK
1399 if (ha->optrom_state != QLA_SWAITING) {
1400 ql_log(ql_log_info, vha, 0x7056,
1401 "optrom_state %d.\n", ha->optrom_state);
f19af163 1402 return -EBUSY;
7c3df132 1403 }
f19af163
HZ
1404
1405 ha->optrom_region_start = start;
7c3df132 1406 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
f19af163
HZ
1407 if (is_update) {
1408 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1409 valid = 1;
1410 else if (start == (ha->flt_region_boot * 4) ||
1411 start == (ha->flt_region_fw * 4))
1412 valid = 1;
1413 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
ecc89f25
JC
1414 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
1415 IS_QLA28XX(ha))
f19af163
HZ
1416 valid = 1;
1417 if (!valid) {
7c3df132
SK
1418 ql_log(ql_log_warn, vha, 0x7058,
1419 "Invalid start region 0x%x/0x%x.\n", start,
1420 bsg_job->request_payload.payload_len);
f19af163
HZ
1421 return -EINVAL;
1422 }
1423
1424 ha->optrom_region_size = start +
1425 bsg_job->request_payload.payload_len > ha->optrom_size ?
1426 ha->optrom_size - start :
1427 bsg_job->request_payload.payload_len;
1428 ha->optrom_state = QLA_SWRITING;
1429 } else {
1430 ha->optrom_region_size = start +
1431 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1432 ha->optrom_size - start :
1433 bsg_job->reply_payload.payload_len;
1434 ha->optrom_state = QLA_SREADING;
1435 }
1436
05583121 1437 ha->optrom_buffer = vzalloc(ha->optrom_region_size);
f19af163 1438 if (!ha->optrom_buffer) {
7c3df132 1439 ql_log(ql_log_warn, vha, 0x7059,
f19af163 1440 "Read: Unable to allocate memory for optrom retrieval "
7c3df132 1441 "(%x)\n", ha->optrom_region_size);
f19af163
HZ
1442
1443 ha->optrom_state = QLA_SWAITING;
1444 return -ENOMEM;
1445 }
1446
f19af163
HZ
1447 return 0;
1448}
1449
1450static int
75cc8cfc 1451qla2x00_read_optrom(struct bsg_job *bsg_job)
f19af163 1452{
01e0e15c 1453 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1454 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
f19af163
HZ
1455 scsi_qla_host_t *vha = shost_priv(host);
1456 struct qla_hw_data *ha = vha->hw;
1457 int rval = 0;
1458
7d613ac6 1459 if (ha->flags.nic_core_reset_hdlr_active)
a49393f2
GM
1460 return -EBUSY;
1461
7a8ab9c8 1462 mutex_lock(&ha->optrom_mutex);
7c3df132 1463 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
7a8ab9c8
CD
1464 if (rval) {
1465 mutex_unlock(&ha->optrom_mutex);
f19af163 1466 return rval;
7a8ab9c8 1467 }
f19af163
HZ
1468
1469 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1470 ha->optrom_region_start, ha->optrom_region_size);
1471
1472 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1473 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1474 ha->optrom_region_size);
1475
01e0e15c
JT
1476 bsg_reply->reply_payload_rcv_len = ha->optrom_region_size;
1477 bsg_reply->result = DID_OK;
f19af163
HZ
1478 vfree(ha->optrom_buffer);
1479 ha->optrom_buffer = NULL;
1480 ha->optrom_state = QLA_SWAITING;
7a8ab9c8 1481 mutex_unlock(&ha->optrom_mutex);
06548160 1482 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1483 bsg_reply->reply_payload_rcv_len);
f19af163
HZ
1484 return rval;
1485}
1486
1487static int
75cc8cfc 1488qla2x00_update_optrom(struct bsg_job *bsg_job)
f19af163 1489{
01e0e15c 1490 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1491 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
f19af163
HZ
1492 scsi_qla_host_t *vha = shost_priv(host);
1493 struct qla_hw_data *ha = vha->hw;
1494 int rval = 0;
1495
7a8ab9c8 1496 mutex_lock(&ha->optrom_mutex);
7c3df132 1497 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
7a8ab9c8
CD
1498 if (rval) {
1499 mutex_unlock(&ha->optrom_mutex);
f19af163 1500 return rval;
7a8ab9c8 1501 }
f19af163 1502
b6d0d9d5
GM
1503 /* Set the isp82xx_no_md_cap not to capture minidump */
1504 ha->flags.isp82xx_no_md_cap = 1;
1505
f19af163
HZ
1506 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1507 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1508 ha->optrom_region_size);
1509
1510 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1511 ha->optrom_region_start, ha->optrom_region_size);
1512
01e0e15c 1513 bsg_reply->result = DID_OK;
f19af163
HZ
1514 vfree(ha->optrom_buffer);
1515 ha->optrom_buffer = NULL;
1516 ha->optrom_state = QLA_SWAITING;
7a8ab9c8 1517 mutex_unlock(&ha->optrom_mutex);
06548160 1518 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1519 bsg_reply->reply_payload_rcv_len);
f19af163
HZ
1520 return rval;
1521}
1522
697a4bc6 1523static int
75cc8cfc 1524qla2x00_update_fru_versions(struct bsg_job *bsg_job)
697a4bc6 1525{
01e0e15c 1526 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1527 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
697a4bc6
JC
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_image_version_list *list = (void *)bsg;
1533 struct qla_image_version *image;
1534 uint32_t count;
1535 dma_addr_t sfp_dma;
1536 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1537 if (!sfp) {
01e0e15c 1538 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
697a4bc6
JC
1539 EXT_STATUS_NO_MEMORY;
1540 goto done;
1541 }
1542
1543 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1544 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1545
1546 image = list->version;
1547 count = list->count;
1548 while (count--) {
1549 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1550 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1551 image->field_address.device, image->field_address.offset,
1552 sizeof(image->field_info), image->field_address.option);
1553 if (rval) {
01e0e15c 1554 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
697a4bc6
JC
1555 EXT_STATUS_MAILBOX;
1556 goto dealloc;
1557 }
1558 image++;
1559 }
1560
01e0e15c 1561 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
697a4bc6
JC
1562
1563dealloc:
1564 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1565
1566done:
1567 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 1568 bsg_reply->result = DID_OK << 16;
06548160 1569 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1570 bsg_reply->reply_payload_rcv_len);
697a4bc6
JC
1571
1572 return 0;
1573}
1574
1575static int
75cc8cfc 1576qla2x00_read_fru_status(struct bsg_job *bsg_job)
697a4bc6 1577{
01e0e15c 1578 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1579 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
697a4bc6
JC
1580 scsi_qla_host_t *vha = shost_priv(host);
1581 struct qla_hw_data *ha = vha->hw;
1582 int rval = 0;
1583 uint8_t bsg[DMA_POOL_SIZE];
1584 struct qla_status_reg *sr = (void *)bsg;
1585 dma_addr_t sfp_dma;
1586 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1587 if (!sfp) {
01e0e15c 1588 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
697a4bc6
JC
1589 EXT_STATUS_NO_MEMORY;
1590 goto done;
1591 }
1592
1593 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1594 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1595
1596 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1597 sr->field_address.device, sr->field_address.offset,
1598 sizeof(sr->status_reg), sr->field_address.option);
1599 sr->status_reg = *sfp;
1600
1601 if (rval) {
01e0e15c 1602 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
697a4bc6
JC
1603 EXT_STATUS_MAILBOX;
1604 goto dealloc;
1605 }
1606
1607 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1608 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1609
01e0e15c 1610 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
697a4bc6
JC
1611
1612dealloc:
1613 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1614
1615done:
1616 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c
JT
1617 bsg_reply->reply_payload_rcv_len = sizeof(*sr);
1618 bsg_reply->result = DID_OK << 16;
06548160 1619 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1620 bsg_reply->reply_payload_rcv_len);
697a4bc6
JC
1621
1622 return 0;
1623}
1624
1625static int
75cc8cfc 1626qla2x00_write_fru_status(struct bsg_job *bsg_job)
697a4bc6 1627{
01e0e15c 1628 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1629 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
697a4bc6
JC
1630 scsi_qla_host_t *vha = shost_priv(host);
1631 struct qla_hw_data *ha = vha->hw;
1632 int rval = 0;
1633 uint8_t bsg[DMA_POOL_SIZE];
1634 struct qla_status_reg *sr = (void *)bsg;
1635 dma_addr_t sfp_dma;
1636 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1637 if (!sfp) {
01e0e15c 1638 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
697a4bc6
JC
1639 EXT_STATUS_NO_MEMORY;
1640 goto done;
1641 }
1642
1643 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1644 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1645
1646 *sfp = sr->status_reg;
1647 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1648 sr->field_address.device, sr->field_address.offset,
1649 sizeof(sr->status_reg), sr->field_address.option);
1650
1651 if (rval) {
01e0e15c 1652 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
697a4bc6
JC
1653 EXT_STATUS_MAILBOX;
1654 goto dealloc;
1655 }
1656
01e0e15c 1657 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
697a4bc6
JC
1658
1659dealloc:
1660 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1661
1662done:
1663 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 1664 bsg_reply->result = DID_OK << 16;
06548160 1665 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1666 bsg_reply->reply_payload_rcv_len);
697a4bc6
JC
1667
1668 return 0;
1669}
1670
9ebb5d9c 1671static int
75cc8cfc 1672qla2x00_write_i2c(struct bsg_job *bsg_job)
9ebb5d9c 1673{
01e0e15c 1674 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1675 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
9ebb5d9c
JC
1676 scsi_qla_host_t *vha = shost_priv(host);
1677 struct qla_hw_data *ha = vha->hw;
1678 int rval = 0;
1679 uint8_t bsg[DMA_POOL_SIZE];
1680 struct qla_i2c_access *i2c = (void *)bsg;
1681 dma_addr_t sfp_dma;
1682 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1683 if (!sfp) {
01e0e15c 1684 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
9ebb5d9c
JC
1685 EXT_STATUS_NO_MEMORY;
1686 goto done;
1687 }
1688
1689 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1690 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1691
1692 memcpy(sfp, i2c->buffer, i2c->length);
1693 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1694 i2c->device, i2c->offset, i2c->length, i2c->option);
1695
1696 if (rval) {
01e0e15c 1697 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
9ebb5d9c
JC
1698 EXT_STATUS_MAILBOX;
1699 goto dealloc;
1700 }
1701
01e0e15c 1702 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
9ebb5d9c
JC
1703
1704dealloc:
1705 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1706
1707done:
1708 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 1709 bsg_reply->result = DID_OK << 16;
06548160 1710 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1711 bsg_reply->reply_payload_rcv_len);
9ebb5d9c
JC
1712
1713 return 0;
1714}
1715
1716static int
75cc8cfc 1717qla2x00_read_i2c(struct bsg_job *bsg_job)
9ebb5d9c 1718{
01e0e15c 1719 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1720 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
9ebb5d9c
JC
1721 scsi_qla_host_t *vha = shost_priv(host);
1722 struct qla_hw_data *ha = vha->hw;
1723 int rval = 0;
1724 uint8_t bsg[DMA_POOL_SIZE];
1725 struct qla_i2c_access *i2c = (void *)bsg;
1726 dma_addr_t sfp_dma;
1727 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1728 if (!sfp) {
01e0e15c 1729 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
9ebb5d9c
JC
1730 EXT_STATUS_NO_MEMORY;
1731 goto done;
1732 }
1733
1734 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1735 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1736
1737 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1738 i2c->device, i2c->offset, i2c->length, i2c->option);
1739
1740 if (rval) {
01e0e15c 1741 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
9ebb5d9c
JC
1742 EXT_STATUS_MAILBOX;
1743 goto dealloc;
1744 }
1745
1746 memcpy(i2c->buffer, sfp, i2c->length);
1747 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1748 bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1749
01e0e15c 1750 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
9ebb5d9c
JC
1751
1752dealloc:
1753 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1754
1755done:
1756 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c
JT
1757 bsg_reply->reply_payload_rcv_len = sizeof(*i2c);
1758 bsg_reply->result = DID_OK << 16;
06548160 1759 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1760 bsg_reply->reply_payload_rcv_len);
9ebb5d9c
JC
1761
1762 return 0;
1763}
1764
a9b6f722 1765static int
75cc8cfc 1766qla24xx_process_bidir_cmd(struct bsg_job *bsg_job)
a9b6f722 1767{
01e0e15c 1768 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 1769 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
a9b6f722
SK
1770 scsi_qla_host_t *vha = shost_priv(host);
1771 struct qla_hw_data *ha = vha->hw;
a9b6f722
SK
1772 uint32_t rval = EXT_STATUS_OK;
1773 uint16_t req_sg_cnt = 0;
1774 uint16_t rsp_sg_cnt = 0;
1775 uint16_t nextlid = 0;
1776 uint32_t tot_dsds;
1777 srb_t *sp = NULL;
1778 uint32_t req_data_len = 0;
1779 uint32_t rsp_data_len = 0;
1780
1781 /* Check the type of the adapter */
1782 if (!IS_BIDI_CAPABLE(ha)) {
1783 ql_log(ql_log_warn, vha, 0x70a0,
1784 "This adapter is not supported\n");
1785 rval = EXT_STATUS_NOT_SUPPORTED;
1786 goto done;
1787 }
1788
1789 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1790 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1791 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1792 rval = EXT_STATUS_BUSY;
1793 goto done;
1794 }
1795
1796 /* Check if host is online */
1797 if (!vha->flags.online) {
1798 ql_log(ql_log_warn, vha, 0x70a1,
1799 "Host is not online\n");
1800 rval = EXT_STATUS_DEVICE_OFFLINE;
1801 goto done;
1802 }
1803
1804 /* Check if cable is plugged in or not */
1805 if (vha->device_flags & DFLG_NO_CABLE) {
1806 ql_log(ql_log_warn, vha, 0x70a2,
1807 "Cable is unplugged...\n");
1808 rval = EXT_STATUS_INVALID_CFG;
1809 goto done;
1810 }
1811
1812 /* Check if the switch is connected or not */
1813 if (ha->current_topology != ISP_CFG_F) {
1814 ql_log(ql_log_warn, vha, 0x70a3,
1815 "Host is not connected to the switch\n");
1816 rval = EXT_STATUS_INVALID_CFG;
1817 goto done;
1818 }
1819
1820 /* Check if operating mode is P2P */
1821 if (ha->operating_mode != P2P) {
1822 ql_log(ql_log_warn, vha, 0x70a4,
5a68a1c2 1823 "Host operating mode is not P2p\n");
a9b6f722
SK
1824 rval = EXT_STATUS_INVALID_CFG;
1825 goto done;
1826 }
1827
a9b6f722
SK
1828 mutex_lock(&ha->selflogin_lock);
1829 if (vha->self_login_loop_id == 0) {
1830 /* Initialize all required fields of fcport */
1831 vha->bidir_fcport.vha = vha;
1832 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1833 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1834 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1835 vha->bidir_fcport.loop_id = vha->loop_id;
1836
1837 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1838 ql_log(ql_log_warn, vha, 0x70a7,
1839 "Failed to login port %06X for bidirectional IOCB\n",
1840 vha->bidir_fcport.d_id.b24);
1841 mutex_unlock(&ha->selflogin_lock);
1842 rval = EXT_STATUS_MAILBOX;
1843 goto done;
1844 }
1845 vha->self_login_loop_id = nextlid - 1;
1846
1847 }
1848 /* Assign the self login loop id to fcport */
1849 mutex_unlock(&ha->selflogin_lock);
1850
1851 vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1852
1853 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1854 bsg_job->request_payload.sg_list,
1855 bsg_job->request_payload.sg_cnt,
1856 DMA_TO_DEVICE);
1857
1858 if (!req_sg_cnt) {
1859 rval = EXT_STATUS_NO_MEMORY;
1860 goto done;
1861 }
1862
1863 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1864 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1865 DMA_FROM_DEVICE);
1866
1867 if (!rsp_sg_cnt) {
1868 rval = EXT_STATUS_NO_MEMORY;
1869 goto done_unmap_req_sg;
1870 }
1871
1872 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
1873 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1874 ql_dbg(ql_dbg_user, vha, 0x70a9,
1875 "Dma mapping resulted in different sg counts "
1876 "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1877 "%x dma_reply_sg_cnt: %x]\n",
1878 bsg_job->request_payload.sg_cnt, req_sg_cnt,
1879 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1880 rval = EXT_STATUS_NO_MEMORY;
1881 goto done_unmap_sg;
1882 }
1883
1884 if (req_data_len != rsp_data_len) {
1885 rval = EXT_STATUS_BUSY;
1886 ql_log(ql_log_warn, vha, 0x70aa,
1887 "req_data_len != rsp_data_len\n");
1888 goto done_unmap_sg;
1889 }
1890
1891 req_data_len = bsg_job->request_payload.payload_len;
1892 rsp_data_len = bsg_job->reply_payload.payload_len;
1893
1894
1895 /* Alloc SRB structure */
1896 sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1897 if (!sp) {
1898 ql_dbg(ql_dbg_user, vha, 0x70ac,
1899 "Alloc SRB structure failed\n");
1900 rval = EXT_STATUS_NO_MEMORY;
1901 goto done_unmap_sg;
1902 }
1903
1904 /*Populate srb->ctx with bidir ctx*/
1905 sp->u.bsg_job = bsg_job;
1906 sp->free = qla2x00_bsg_sp_free;
1907 sp->type = SRB_BIDI_CMD;
1908 sp->done = qla2x00_bsg_job_done;
1909
1910 /* Add the read and write sg count */
1911 tot_dsds = rsp_sg_cnt + req_sg_cnt;
1912
1913 rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1914 if (rval != EXT_STATUS_OK)
1915 goto done_free_srb;
1916 /* the bsg request will be completed in the interrupt handler */
1917 return rval;
1918
1919done_free_srb:
1920 mempool_free(sp, ha->srb_mempool);
1921done_unmap_sg:
1922 dma_unmap_sg(&ha->pdev->dev,
1923 bsg_job->reply_payload.sg_list,
1924 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1925done_unmap_req_sg:
1926 dma_unmap_sg(&ha->pdev->dev,
1927 bsg_job->request_payload.sg_list,
1928 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1929done:
1930
1931 /* Return an error vendor specific response
1932 * and complete the bsg request
1933 */
01e0e15c 1934 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
a9b6f722 1935 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c
JT
1936 bsg_reply->reply_payload_rcv_len = 0;
1937 bsg_reply->result = (DID_OK) << 16;
06548160 1938 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 1939 bsg_reply->reply_payload_rcv_len);
9e03aa2f 1940 /* Always return success, vendor rsp carries correct status */
a9b6f722
SK
1941 return 0;
1942}
1943
8ae6d9c7 1944static int
75cc8cfc 1945qlafx00_mgmt_cmd(struct bsg_job *bsg_job)
8ae6d9c7 1946{
01e0e15c 1947 struct fc_bsg_request *bsg_request = bsg_job->request;
cd21c605 1948 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
8ae6d9c7
GM
1949 scsi_qla_host_t *vha = shost_priv(host);
1950 struct qla_hw_data *ha = vha->hw;
1951 int rval = (DRIVER_ERROR << 16);
1952 struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
1953 srb_t *sp;
1954 int req_sg_cnt = 0, rsp_sg_cnt = 0;
1955 struct fc_port *fcport;
1956 char *type = "FC_BSG_HST_FX_MGMT";
1957
1958 /* Copy the IOCB specific information */
1959 piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
01e0e15c 1960 &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
8ae6d9c7
GM
1961
1962 /* Dump the vendor information */
1963 ql_dump_buffer(ql_dbg_user + ql_dbg_verbose , vha, 0x70cf,
f8f97b0c 1964 piocb_rqst, sizeof(*piocb_rqst));
8ae6d9c7
GM
1965
1966 if (!vha->flags.online) {
1967 ql_log(ql_log_warn, vha, 0x70d0,
1968 "Host is not online.\n");
1969 rval = -EIO;
1970 goto done;
1971 }
1972
1973 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID) {
1974 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1975 bsg_job->request_payload.sg_list,
1976 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1977 if (!req_sg_cnt) {
1978 ql_log(ql_log_warn, vha, 0x70c7,
1979 "dma_map_sg return %d for request\n", req_sg_cnt);
1980 rval = -ENOMEM;
1981 goto done;
1982 }
1983 }
1984
1985 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID) {
1986 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1987 bsg_job->reply_payload.sg_list,
1988 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1989 if (!rsp_sg_cnt) {
1990 ql_log(ql_log_warn, vha, 0x70c8,
1991 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
1992 rval = -ENOMEM;
1993 goto done_unmap_req_sg;
1994 }
1995 }
1996
1997 ql_dbg(ql_dbg_user, vha, 0x70c9,
1998 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
1999 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
2000 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
2001
2002 /* Allocate a dummy fcport structure, since functions preparing the
2003 * IOCB and mailbox command retrieves port specific information
2004 * from fcport structure. For Host based ELS commands there will be
2005 * no fcport structure allocated
2006 */
2007 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2008 if (!fcport) {
2009 ql_log(ql_log_warn, vha, 0x70ca,
2010 "Failed to allocate fcport.\n");
2011 rval = -ENOMEM;
2012 goto done_unmap_rsp_sg;
2013 }
2014
2015 /* Alloc SRB structure */
2016 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
2017 if (!sp) {
2018 ql_log(ql_log_warn, vha, 0x70cb,
2019 "qla2x00_get_sp failed.\n");
2020 rval = -ENOMEM;
2021 goto done_free_fcport;
2022 }
2023
2024 /* Initialize all required fields of fcport */
2025 fcport->vha = vha;
2026 fcport->loop_id = piocb_rqst->dataword;
2027
2028 sp->type = SRB_FXIOCB_BCMD;
2029 sp->name = "bsg_fx_mgmt";
2030 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
2031 sp->u.bsg_job = bsg_job;
2032 sp->free = qla2x00_bsg_sp_free;
2033 sp->done = qla2x00_bsg_job_done;
2034
2035 ql_dbg(ql_dbg_user, vha, 0x70cc,
2036 "bsg rqst type: %s fx_mgmt_type: %x id=%x\n",
2037 type, piocb_rqst->func_type, fcport->loop_id);
2038
2039 rval = qla2x00_start_sp(sp);
2040 if (rval != QLA_SUCCESS) {
2041 ql_log(ql_log_warn, vha, 0x70cd,
2042 "qla2x00_start_sp failed=%d.\n", rval);
2043 mempool_free(sp, ha->srb_mempool);
2044 rval = -EIO;
2045 goto done_free_fcport;
2046 }
2047 return rval;
2048
2049done_free_fcport:
2050 kfree(fcport);
2051
2052done_unmap_rsp_sg:
2053 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
2054 dma_unmap_sg(&ha->pdev->dev,
2055 bsg_job->reply_payload.sg_list,
2056 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2057done_unmap_req_sg:
2058 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
2059 dma_unmap_sg(&ha->pdev->dev,
2060 bsg_job->request_payload.sg_list,
2061 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2062
2063done:
2064 return rval;
2065}
2066
db64e930 2067static int
75cc8cfc 2068qla26xx_serdes_op(struct bsg_job *bsg_job)
db64e930 2069{
01e0e15c 2070 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2071 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
db64e930
JC
2072 scsi_qla_host_t *vha = shost_priv(host);
2073 int rval = 0;
2074 struct qla_serdes_reg sr;
2075
2076 memset(&sr, 0, sizeof(sr));
2077
2078 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2079 bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
2080
2081 switch (sr.cmd) {
2082 case INT_SC_SERDES_WRITE_REG:
2083 rval = qla2x00_write_serdes_word(vha, sr.addr, sr.val);
01e0e15c 2084 bsg_reply->reply_payload_rcv_len = 0;
db64e930
JC
2085 break;
2086 case INT_SC_SERDES_READ_REG:
2087 rval = qla2x00_read_serdes_word(vha, sr.addr, &sr.val);
2088 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2089 bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
01e0e15c 2090 bsg_reply->reply_payload_rcv_len = sizeof(sr);
db64e930
JC
2091 break;
2092 default:
e8887c51 2093 ql_dbg(ql_dbg_user, vha, 0x708c,
db64e930 2094 "Unknown serdes cmd %x.\n", sr.cmd);
e8887c51
JC
2095 rval = -EINVAL;
2096 break;
2097 }
2098
01e0e15c 2099 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
e8887c51
JC
2100 rval ? EXT_STATUS_MAILBOX : 0;
2101
2102 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 2103 bsg_reply->result = DID_OK << 16;
06548160 2104 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 2105 bsg_reply->reply_payload_rcv_len);
e8887c51
JC
2106 return 0;
2107}
2108
2109static int
75cc8cfc 2110qla8044_serdes_op(struct bsg_job *bsg_job)
e8887c51 2111{
01e0e15c 2112 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2113 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
e8887c51
JC
2114 scsi_qla_host_t *vha = shost_priv(host);
2115 int rval = 0;
2116 struct qla_serdes_reg_ex sr;
2117
2118 memset(&sr, 0, sizeof(sr));
2119
2120 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2121 bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
2122
2123 switch (sr.cmd) {
2124 case INT_SC_SERDES_WRITE_REG:
2125 rval = qla8044_write_serdes_word(vha, sr.addr, sr.val);
01e0e15c 2126 bsg_reply->reply_payload_rcv_len = 0;
e8887c51
JC
2127 break;
2128 case INT_SC_SERDES_READ_REG:
2129 rval = qla8044_read_serdes_word(vha, sr.addr, &sr.val);
2130 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2131 bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
01e0e15c 2132 bsg_reply->reply_payload_rcv_len = sizeof(sr);
e8887c51
JC
2133 break;
2134 default:
83548fe2 2135 ql_dbg(ql_dbg_user, vha, 0x7020,
e8887c51
JC
2136 "Unknown serdes cmd %x.\n", sr.cmd);
2137 rval = -EINVAL;
db64e930
JC
2138 break;
2139 }
2140
01e0e15c 2141 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
db64e930
JC
2142 rval ? EXT_STATUS_MAILBOX : 0;
2143
2144 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 2145 bsg_reply->result = DID_OK << 16;
06548160 2146 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 2147 bsg_reply->reply_payload_rcv_len);
db64e930
JC
2148 return 0;
2149}
2150
4243c115 2151static int
75cc8cfc 2152qla27xx_get_flash_upd_cap(struct bsg_job *bsg_job)
4243c115 2153{
01e0e15c 2154 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2155 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
4243c115
SC
2156 scsi_qla_host_t *vha = shost_priv(host);
2157 struct qla_hw_data *ha = vha->hw;
2158 struct qla_flash_update_caps cap;
2159
ecc89f25 2160 if (!(IS_QLA27XX(ha)) && !IS_QLA28XX(ha))
4243c115
SC
2161 return -EPERM;
2162
2163 memset(&cap, 0, sizeof(cap));
2164 cap.capabilities = (uint64_t)ha->fw_attributes_ext[1] << 48 |
2165 (uint64_t)ha->fw_attributes_ext[0] << 32 |
2166 (uint64_t)ha->fw_attributes_h << 16 |
2167 (uint64_t)ha->fw_attributes;
2168
2169 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2170 bsg_job->reply_payload.sg_cnt, &cap, sizeof(cap));
01e0e15c 2171 bsg_reply->reply_payload_rcv_len = sizeof(cap);
4243c115 2172
01e0e15c 2173 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
4243c115
SC
2174 EXT_STATUS_OK;
2175
2176 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 2177 bsg_reply->result = DID_OK << 16;
06548160 2178 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 2179 bsg_reply->reply_payload_rcv_len);
4243c115
SC
2180 return 0;
2181}
2182
2183static int
75cc8cfc 2184qla27xx_set_flash_upd_cap(struct bsg_job *bsg_job)
4243c115 2185{
01e0e15c 2186 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2187 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
4243c115
SC
2188 scsi_qla_host_t *vha = shost_priv(host);
2189 struct qla_hw_data *ha = vha->hw;
2190 uint64_t online_fw_attr = 0;
2191 struct qla_flash_update_caps cap;
2192
ecc89f25 2193 if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
4243c115
SC
2194 return -EPERM;
2195
2196 memset(&cap, 0, sizeof(cap));
2197 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2198 bsg_job->request_payload.sg_cnt, &cap, sizeof(cap));
2199
2200 online_fw_attr = (uint64_t)ha->fw_attributes_ext[1] << 48 |
2201 (uint64_t)ha->fw_attributes_ext[0] << 32 |
2202 (uint64_t)ha->fw_attributes_h << 16 |
2203 (uint64_t)ha->fw_attributes;
2204
2205 if (online_fw_attr != cap.capabilities) {
01e0e15c 2206 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
4243c115
SC
2207 EXT_STATUS_INVALID_PARAM;
2208 return -EINVAL;
2209 }
2210
2211 if (cap.outage_duration < MAX_LOOP_TIMEOUT) {
01e0e15c 2212 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
4243c115
SC
2213 EXT_STATUS_INVALID_PARAM;
2214 return -EINVAL;
2215 }
2216
01e0e15c 2217 bsg_reply->reply_payload_rcv_len = 0;
4243c115 2218
01e0e15c 2219 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
4243c115
SC
2220 EXT_STATUS_OK;
2221
2222 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 2223 bsg_reply->result = DID_OK << 16;
06548160 2224 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 2225 bsg_reply->reply_payload_rcv_len);
4243c115
SC
2226 return 0;
2227}
2228
969a6199 2229static int
75cc8cfc 2230qla27xx_get_bbcr_data(struct bsg_job *bsg_job)
969a6199 2231{
01e0e15c 2232 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2233 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
969a6199
SC
2234 scsi_qla_host_t *vha = shost_priv(host);
2235 struct qla_hw_data *ha = vha->hw;
2236 struct qla_bbcr_data bbcr;
2237 uint16_t loop_id, topo, sw_cap;
2238 uint8_t domain, area, al_pa, state;
2239 int rval;
2240
ecc89f25 2241 if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
969a6199
SC
2242 return -EPERM;
2243
2244 memset(&bbcr, 0, sizeof(bbcr));
2245
2246 if (vha->flags.bbcr_enable)
2247 bbcr.status = QLA_BBCR_STATUS_ENABLED;
2248 else
2249 bbcr.status = QLA_BBCR_STATUS_DISABLED;
2250
2251 if (bbcr.status == QLA_BBCR_STATUS_ENABLED) {
2252 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
2253 &area, &domain, &topo, &sw_cap);
c73191b8
HZ
2254 if (rval != QLA_SUCCESS) {
2255 bbcr.status = QLA_BBCR_STATUS_UNKNOWN;
2256 bbcr.state = QLA_BBCR_STATE_OFFLINE;
2257 bbcr.mbx1 = loop_id;
2258 goto done;
2259 }
969a6199
SC
2260
2261 state = (vha->bbcr >> 12) & 0x1;
2262
2263 if (state) {
2264 bbcr.state = QLA_BBCR_STATE_OFFLINE;
2265 bbcr.offline_reason_code = QLA_BBCR_REASON_LOGIN_REJECT;
2266 } else {
2267 bbcr.state = QLA_BBCR_STATE_ONLINE;
2268 bbcr.negotiated_bbscn = (vha->bbcr >> 8) & 0xf;
2269 }
2270
2271 bbcr.configured_bbscn = vha->bbcr & 0xf;
2272 }
2273
c73191b8 2274done:
969a6199
SC
2275 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2276 bsg_job->reply_payload.sg_cnt, &bbcr, sizeof(bbcr));
01e0e15c 2277 bsg_reply->reply_payload_rcv_len = sizeof(bbcr);
969a6199 2278
01e0e15c 2279 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = EXT_STATUS_OK;
969a6199
SC
2280
2281 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
01e0e15c 2282 bsg_reply->result = DID_OK << 16;
06548160 2283 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 2284 bsg_reply->reply_payload_rcv_len);
969a6199
SC
2285 return 0;
2286}
2287
243de676 2288static int
75cc8cfc 2289qla2x00_get_priv_stats(struct bsg_job *bsg_job)
243de676 2290{
01e0e15c
JT
2291 struct fc_bsg_request *bsg_request = bsg_job->request;
2292 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2293 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
243de676
HZ
2294 scsi_qla_host_t *vha = shost_priv(host);
2295 struct qla_hw_data *ha = vha->hw;
2296 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2297 struct link_statistics *stats = NULL;
2298 dma_addr_t stats_dma;
8437dda0 2299 int rval;
01e0e15c 2300 uint32_t *cmd = bsg_request->rqst_data.h_vendor.vendor_cmd;
8437dda0 2301 uint options = cmd[0] == QL_VND_GET_PRIV_STATS_EX ? cmd[1] : 0;
243de676
HZ
2302
2303 if (test_bit(UNLOADING, &vha->dpc_flags))
8437dda0 2304 return -ENODEV;
243de676
HZ
2305
2306 if (unlikely(pci_channel_offline(ha->pdev)))
8437dda0 2307 return -ENODEV;
243de676
HZ
2308
2309 if (qla2x00_reset_active(vha))
8437dda0 2310 return -EBUSY;
243de676
HZ
2311
2312 if (!IS_FWI2_CAPABLE(ha))
8437dda0 2313 return -EPERM;
243de676 2314
750afb08
LC
2315 stats = dma_alloc_coherent(&ha->pdev->dev, sizeof(*stats), &stats_dma,
2316 GFP_KERNEL);
243de676
HZ
2317 if (!stats) {
2318 ql_log(ql_log_warn, vha, 0x70e2,
8437dda0
SC
2319 "Failed to allocate memory for stats.\n");
2320 return -ENOMEM;
243de676
HZ
2321 }
2322
8437dda0 2323 rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, options);
243de676 2324
8437dda0 2325 if (rval == QLA_SUCCESS) {
f8f97b0c
JC
2326 ql_dump_buffer(ql_dbg_user + ql_dbg_verbose, vha, 0x70e5,
2327 stats, sizeof(*stats));
8437dda0
SC
2328 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2329 bsg_job->reply_payload.sg_cnt, stats, sizeof(*stats));
2330 }
243de676 2331
01e0e15c
JT
2332 bsg_reply->reply_payload_rcv_len = sizeof(*stats);
2333 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
8437dda0 2334 rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
243de676 2335
01e0e15c
JT
2336 bsg_job->reply_len = sizeof(*bsg_reply);
2337 bsg_reply->result = DID_OK << 16;
06548160 2338 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 2339 bsg_reply->reply_payload_rcv_len);
243de676 2340
8437dda0 2341 dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
243de676 2342 stats, stats_dma);
8437dda0
SC
2343
2344 return 0;
243de676
HZ
2345}
2346
ec891462 2347static int
75cc8cfc 2348qla2x00_do_dport_diagnostics(struct bsg_job *bsg_job)
ec891462 2349{
01e0e15c 2350 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2351 struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
ec891462
JC
2352 scsi_qla_host_t *vha = shost_priv(host);
2353 int rval;
2354 struct qla_dport_diag *dd;
2355
ecc89f25
JC
2356 if (!IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw) &&
2357 !IS_QLA28XX(vha->hw))
ec891462
JC
2358 return -EPERM;
2359
2360 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
2361 if (!dd) {
2362 ql_log(ql_log_warn, vha, 0x70db,
2363 "Failed to allocate memory for dport.\n");
2364 return -ENOMEM;
2365 }
2366
2367 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2368 bsg_job->request_payload.sg_cnt, dd, sizeof(*dd));
2369
2370 rval = qla26xx_dport_diagnostics(
2371 vha, dd->buf, sizeof(dd->buf), dd->options);
2372 if (rval == QLA_SUCCESS) {
2373 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2374 bsg_job->reply_payload.sg_cnt, dd, sizeof(*dd));
2375 }
2376
01e0e15c
JT
2377 bsg_reply->reply_payload_rcv_len = sizeof(*dd);
2378 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
ec891462
JC
2379 rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
2380
01e0e15c
JT
2381 bsg_job->reply_len = sizeof(*bsg_reply);
2382 bsg_reply->result = DID_OK << 16;
06548160 2383 bsg_job_done(bsg_job, bsg_reply->result,
1abaede7 2384 bsg_reply->reply_payload_rcv_len);
ec891462
JC
2385
2386 kfree(dd);
2387
2388 return 0;
2389}
2390
5fa8774c
JC
2391static int
2392qla2x00_get_flash_image_status(struct bsg_job *bsg_job)
2393{
2394 scsi_qla_host_t *vha = shost_priv(fc_bsg_to_shost(bsg_job));
2395 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2396 struct qla_hw_data *ha = vha->hw;
2397 struct qla_active_regions regions = { };
2398 struct active_regions active_regions = { };
2399
2400 qla28xx_get_aux_images(vha, &active_regions);
2401 regions.global_image = active_regions.global;
2402
2403 if (IS_QLA28XX(ha)) {
2404 qla27xx_get_active_image(vha, &active_regions);
2405 regions.board_config = active_regions.aux.board_config;
2406 regions.vpd_nvram = active_regions.aux.vpd_nvram;
2407 regions.npiv_config_0_1 = active_regions.aux.npiv_config_0_1;
2408 regions.npiv_config_2_3 = active_regions.aux.npiv_config_2_3;
2409 }
2410
2411 ql_dbg(ql_dbg_user, vha, 0x70e1,
2412 "%s(%lu): FW=%u BCFG=%u VPDNVR=%u NPIV01=%u NPIV02=%u\n",
2413 __func__, vha->host_no, regions.global_image,
2414 regions.board_config, regions.vpd_nvram,
2415 regions.npiv_config_0_1, regions.npiv_config_2_3);
2416
2417 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2418 bsg_job->reply_payload.sg_cnt, &regions, sizeof(regions));
2419
2420 bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = EXT_STATUS_OK;
2421 bsg_reply->reply_payload_rcv_len = sizeof(regions);
2422 bsg_reply->result = DID_OK << 16;
2423 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2424 bsg_job_done(bsg_job, bsg_reply->result,
2425 bsg_reply->reply_payload_rcv_len);
2426
2427 return 0;
2428}
2429
6e98016c 2430static int
75cc8cfc 2431qla2x00_process_vendor_specific(struct bsg_job *bsg_job)
6e98016c 2432{
01e0e15c
JT
2433 struct fc_bsg_request *bsg_request = bsg_job->request;
2434
2435 switch (bsg_request->rqst_data.h_vendor.vendor_cmd[0]) {
6e98016c
GM
2436 case QL_VND_LOOPBACK:
2437 return qla2x00_process_loopback(bsg_job);
2438
2439 case QL_VND_A84_RESET:
2440 return qla84xx_reset(bsg_job);
2441
2442 case QL_VND_A84_UPDATE_FW:
2443 return qla84xx_updatefw(bsg_job);
2444
2445 case QL_VND_A84_MGMT_CMD:
2446 return qla84xx_mgmt_cmd(bsg_job);
2447
2448 case QL_VND_IIDMA:
2449 return qla24xx_iidma(bsg_job);
2450
09ff701a
SR
2451 case QL_VND_FCP_PRIO_CFG_CMD:
2452 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
2453
f19af163
HZ
2454 case QL_VND_READ_FLASH:
2455 return qla2x00_read_optrom(bsg_job);
2456
2457 case QL_VND_UPDATE_FLASH:
2458 return qla2x00_update_optrom(bsg_job);
2459
697a4bc6
JC
2460 case QL_VND_SET_FRU_VERSION:
2461 return qla2x00_update_fru_versions(bsg_job);
2462
2463 case QL_VND_READ_FRU_STATUS:
2464 return qla2x00_read_fru_status(bsg_job);
2465
2466 case QL_VND_WRITE_FRU_STATUS:
2467 return qla2x00_write_fru_status(bsg_job);
2468
9ebb5d9c
JC
2469 case QL_VND_WRITE_I2C:
2470 return qla2x00_write_i2c(bsg_job);
2471
2472 case QL_VND_READ_I2C:
2473 return qla2x00_read_i2c(bsg_job);
2474
a9b6f722
SK
2475 case QL_VND_DIAG_IO_CMD:
2476 return qla24xx_process_bidir_cmd(bsg_job);
2477
8ae6d9c7
GM
2478 case QL_VND_FX00_MGMT_CMD:
2479 return qlafx00_mgmt_cmd(bsg_job);
db64e930
JC
2480
2481 case QL_VND_SERDES_OP:
2482 return qla26xx_serdes_op(bsg_job);
2483
e8887c51
JC
2484 case QL_VND_SERDES_OP_EX:
2485 return qla8044_serdes_op(bsg_job);
2486
4243c115
SC
2487 case QL_VND_GET_FLASH_UPDATE_CAPS:
2488 return qla27xx_get_flash_upd_cap(bsg_job);
2489
2490 case QL_VND_SET_FLASH_UPDATE_CAPS:
2491 return qla27xx_set_flash_upd_cap(bsg_job);
2492
969a6199
SC
2493 case QL_VND_GET_BBCR_DATA:
2494 return qla27xx_get_bbcr_data(bsg_job);
2495
243de676 2496 case QL_VND_GET_PRIV_STATS:
8437dda0 2497 case QL_VND_GET_PRIV_STATS_EX:
243de676
HZ
2498 return qla2x00_get_priv_stats(bsg_job);
2499
ec891462
JC
2500 case QL_VND_DPORT_DIAGNOSTICS:
2501 return qla2x00_do_dport_diagnostics(bsg_job);
2502
5fa8774c
JC
2503 case QL_VND_SS_GET_FLASH_IMAGE_STATUS:
2504 return qla2x00_get_flash_image_status(bsg_job);
2505
6e98016c 2506 default:
6e98016c
GM
2507 return -ENOSYS;
2508 }
2509}
2510
2511int
75cc8cfc 2512qla24xx_bsg_request(struct bsg_job *bsg_job)
6e98016c 2513{
01e0e15c
JT
2514 struct fc_bsg_request *bsg_request = bsg_job->request;
2515 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
6e98016c 2516 int ret = -EINVAL;
7c3df132 2517 struct fc_rport *rport;
7c3df132
SK
2518 struct Scsi_Host *host;
2519 scsi_qla_host_t *vha;
2520
b7bfbe12 2521 /* In case no data transferred. */
01e0e15c 2522 bsg_reply->reply_payload_rcv_len = 0;
b7bfbe12 2523
01e0e15c 2524 if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
1d69b122 2525 rport = fc_bsg_to_rport(bsg_job);
7c3df132
SK
2526 host = rport_to_shost(rport);
2527 vha = shost_priv(host);
2528 } else {
cd21c605 2529 host = fc_bsg_to_shost(bsg_job);
7c3df132
SK
2530 vha = shost_priv(host);
2531 }
2532
56d942de 2533 if (qla2x00_chip_is_down(vha)) {
d051a5aa
AV
2534 ql_dbg(ql_dbg_user, vha, 0x709f,
2535 "BSG: ISP abort active/needed -- cmd=%d.\n",
01e0e15c 2536 bsg_request->msgcode);
d051a5aa
AV
2537 return -EBUSY;
2538 }
2539
7c3df132 2540 ql_dbg(ql_dbg_user, vha, 0x7000,
01e0e15c 2541 "Entered %s msgcode=0x%x.\n", __func__, bsg_request->msgcode);
6e98016c 2542
01e0e15c 2543 switch (bsg_request->msgcode) {
6e98016c
GM
2544 case FC_BSG_RPT_ELS:
2545 case FC_BSG_HST_ELS_NOLOGIN:
2546 ret = qla2x00_process_els(bsg_job);
2547 break;
2548 case FC_BSG_HST_CT:
2549 ret = qla2x00_process_ct(bsg_job);
2550 break;
2551 case FC_BSG_HST_VENDOR:
2552 ret = qla2x00_process_vendor_specific(bsg_job);
2553 break;
2554 case FC_BSG_HST_ADD_RPORT:
2555 case FC_BSG_HST_DEL_RPORT:
2556 case FC_BSG_RPT_CT:
2557 default:
7c3df132 2558 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
6e98016c 2559 break;
6c452a45 2560 }
6e98016c
GM
2561 return ret;
2562}
2563
2564int
75cc8cfc 2565qla24xx_bsg_timeout(struct bsg_job *bsg_job)
6e98016c 2566{
01e0e15c 2567 struct fc_bsg_reply *bsg_reply = bsg_job->reply;
cd21c605 2568 scsi_qla_host_t *vha = shost_priv(fc_bsg_to_shost(bsg_job));
6e98016c
GM
2569 struct qla_hw_data *ha = vha->hw;
2570 srb_t *sp;
2571 int cnt, que;
2572 unsigned long flags;
2573 struct req_que *req;
6e98016c
GM
2574
2575 /* find the bsg job from the active list of commands */
2576 spin_lock_irqsave(&ha->hardware_lock, flags);
2577 for (que = 0; que < ha->max_req_queues; que++) {
2578 req = ha->req_q_map[que];
2579 if (!req)
2580 continue;
2581
8d93f550 2582 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
6e98016c 2583 sp = req->outstanding_cmds[cnt];
6e98016c 2584 if (sp) {
9ba56b95 2585 if (((sp->type == SRB_CT_CMD) ||
8ae6d9c7
GM
2586 (sp->type == SRB_ELS_CMD_HST) ||
2587 (sp->type == SRB_FXIOCB_BCMD))
9ba56b95 2588 && (sp->u.bsg_job == bsg_job)) {
8edf3edd 2589 req->outstanding_cmds[cnt] = NULL;
900a36e3 2590 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6e98016c 2591 if (ha->isp_ops->abort_command(sp)) {
7c3df132
SK
2592 ql_log(ql_log_warn, vha, 0x7089,
2593 "mbx abort_command "
2594 "failed.\n");
01e0e15c 2595 bsg_reply->result = -EIO;
6e98016c 2596 } else {
7c3df132
SK
2597 ql_dbg(ql_dbg_user, vha, 0x708a,
2598 "mbx abort_command "
2599 "success.\n");
01e0e15c 2600 bsg_reply->result = 0;
6e98016c 2601 }
900a36e3 2602 spin_lock_irqsave(&ha->hardware_lock, flags);
6e98016c
GM
2603 goto done;
2604 }
2605 }
2606 }
2607 }
2608 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7c3df132 2609 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
05231a3b 2610 bsg_reply->result = -ENXIO;
6e98016c
GM
2611 return 0;
2612
2613done:
2614 spin_unlock_irqrestore(&ha->hardware_lock, flags);
25ff6af1 2615 sp->free(sp);
6e98016c
GM
2616 return 0;
2617}