[SCSI] qla2xxx: Move loopback mode reset after chip reset check.
[GitHub/mt8127/android_kernel_alcatel_ttab.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;
6e98016c 33
9ba56b95
GM
34 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
35 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
6e98016c 36
9ba56b95
GM
37 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
38 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
39
40 if (sp->type == SRB_CT_CMD ||
41 sp->type == SRB_ELS_CMD_HST)
42 kfree(sp->fcport);
b00ee7d7 43 qla2x00_rel_sp(vha, sp);
6e98016c
GM
44}
45
09ff701a 46int
7c3df132
SK
47qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
48 struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
09ff701a
SR
49{
50 int i, ret, num_valid;
51 uint8_t *bcode;
52 struct qla_fcp_prio_entry *pri_entry;
2f0f3f4f 53 uint32_t *bcode_val_ptr, bcode_val;
09ff701a
SR
54
55 ret = 1;
56 num_valid = 0;
57 bcode = (uint8_t *)pri_cfg;
2f0f3f4f
MI
58 bcode_val_ptr = (uint32_t *)pri_cfg;
59 bcode_val = (uint32_t)(*bcode_val_ptr);
09ff701a 60
2f0f3f4f
MI
61 if (bcode_val == 0xFFFFFFFF) {
62 /* No FCP Priority config data in flash */
7c3df132
SK
63 ql_dbg(ql_dbg_user, vha, 0x7051,
64 "No FCP Priority config data.\n");
2f0f3f4f
MI
65 return 0;
66 }
67
68 if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
69 bcode[3] != 'S') {
70 /* Invalid FCP priority data header*/
7c3df132
SK
71 ql_dbg(ql_dbg_user, vha, 0x7052,
72 "Invalid FCP Priority data header. bcode=0x%x.\n",
73 bcode_val);
09ff701a
SR
74 return 0;
75 }
76 if (flag != 1)
77 return ret;
78
79 pri_entry = &pri_cfg->entry[0];
80 for (i = 0; i < pri_cfg->num_entries; i++) {
81 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
82 num_valid++;
83 pri_entry++;
84 }
85
2f0f3f4f
MI
86 if (num_valid == 0) {
87 /* No valid FCP priority data entries */
7c3df132
SK
88 ql_dbg(ql_dbg_user, vha, 0x7053,
89 "No valid FCP Priority data entries.\n");
09ff701a 90 ret = 0;
2f0f3f4f
MI
91 } else {
92 /* FCP priority data is valid */
7c3df132
SK
93 ql_dbg(ql_dbg_user, vha, 0x7054,
94 "Valid FCP priority data. num entries = %d.\n",
95 num_valid);
2f0f3f4f 96 }
09ff701a
SR
97
98 return ret;
99}
100
101static int
102qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
103{
104 struct Scsi_Host *host = bsg_job->shost;
105 scsi_qla_host_t *vha = shost_priv(host);
106 struct qla_hw_data *ha = vha->hw;
107 int ret = 0;
108 uint32_t len;
109 uint32_t oper;
110
a00f6296 111 if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_QLA82XX(ha))) {
2f0f3f4f
MI
112 ret = -EINVAL;
113 goto exit_fcp_prio_cfg;
114 }
115
09ff701a
SR
116 /* Get the sub command */
117 oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
118
119 /* Only set config is allowed if config memory is not allocated */
120 if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
121 ret = -EINVAL;
122 goto exit_fcp_prio_cfg;
123 }
124 switch (oper) {
125 case QLFC_FCP_PRIO_DISABLE:
126 if (ha->flags.fcp_prio_enabled) {
127 ha->flags.fcp_prio_enabled = 0;
128 ha->fcp_prio_cfg->attributes &=
129 ~FCP_PRIO_ATTR_ENABLE;
130 qla24xx_update_all_fcp_prio(vha);
131 bsg_job->reply->result = DID_OK;
132 } else {
133 ret = -EINVAL;
134 bsg_job->reply->result = (DID_ERROR << 16);
135 goto exit_fcp_prio_cfg;
136 }
137 break;
138
139 case QLFC_FCP_PRIO_ENABLE:
140 if (!ha->flags.fcp_prio_enabled) {
141 if (ha->fcp_prio_cfg) {
142 ha->flags.fcp_prio_enabled = 1;
143 ha->fcp_prio_cfg->attributes |=
144 FCP_PRIO_ATTR_ENABLE;
145 qla24xx_update_all_fcp_prio(vha);
146 bsg_job->reply->result = DID_OK;
147 } else {
148 ret = -EINVAL;
149 bsg_job->reply->result = (DID_ERROR << 16);
150 goto exit_fcp_prio_cfg;
151 }
152 }
153 break;
154
155 case QLFC_FCP_PRIO_GET_CONFIG:
156 len = bsg_job->reply_payload.payload_len;
157 if (!len || len > FCP_PRIO_CFG_SIZE) {
158 ret = -EINVAL;
159 bsg_job->reply->result = (DID_ERROR << 16);
160 goto exit_fcp_prio_cfg;
161 }
162
163 bsg_job->reply->result = DID_OK;
164 bsg_job->reply->reply_payload_rcv_len =
165 sg_copy_from_buffer(
166 bsg_job->reply_payload.sg_list,
167 bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
168 len);
169
170 break;
171
172 case QLFC_FCP_PRIO_SET_CONFIG:
173 len = bsg_job->request_payload.payload_len;
174 if (!len || len > FCP_PRIO_CFG_SIZE) {
175 bsg_job->reply->result = (DID_ERROR << 16);
176 ret = -EINVAL;
177 goto exit_fcp_prio_cfg;
178 }
179
180 if (!ha->fcp_prio_cfg) {
181 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
182 if (!ha->fcp_prio_cfg) {
7c3df132
SK
183 ql_log(ql_log_warn, vha, 0x7050,
184 "Unable to allocate memory for fcp prio "
185 "config data (%x).\n", FCP_PRIO_CFG_SIZE);
09ff701a
SR
186 bsg_job->reply->result = (DID_ERROR << 16);
187 ret = -ENOMEM;
188 goto exit_fcp_prio_cfg;
189 }
190 }
191
192 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
193 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
194 bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
195 FCP_PRIO_CFG_SIZE);
196
197 /* validate fcp priority data */
7c3df132
SK
198
199 if (!qla24xx_fcp_prio_cfg_valid(vha,
200 (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
09ff701a
SR
201 bsg_job->reply->result = (DID_ERROR << 16);
202 ret = -EINVAL;
203 /* If buffer was invalidatic int
204 * fcp_prio_cfg is of no use
205 */
206 vfree(ha->fcp_prio_cfg);
207 ha->fcp_prio_cfg = NULL;
208 goto exit_fcp_prio_cfg;
209 }
210
211 ha->flags.fcp_prio_enabled = 0;
212 if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
213 ha->flags.fcp_prio_enabled = 1;
214 qla24xx_update_all_fcp_prio(vha);
215 bsg_job->reply->result = DID_OK;
216 break;
217 default:
218 ret = -EINVAL;
219 break;
220 }
221exit_fcp_prio_cfg:
63ea923a
AB
222 if (!ret)
223 bsg_job->job_done(bsg_job);
09ff701a
SR
224 return ret;
225}
9ba56b95 226
6e98016c
GM
227static int
228qla2x00_process_els(struct fc_bsg_job *bsg_job)
229{
230 struct fc_rport *rport;
08f71e09 231 fc_port_t *fcport = NULL;
6e98016c
GM
232 struct Scsi_Host *host;
233 scsi_qla_host_t *vha;
234 struct qla_hw_data *ha;
235 srb_t *sp;
236 const char *type;
237 int req_sg_cnt, rsp_sg_cnt;
238 int rval = (DRIVER_ERROR << 16);
239 uint16_t nextlid = 0;
6e98016c 240
08f71e09
HZ
241 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
242 rport = bsg_job->rport;
243 fcport = *(fc_port_t **) rport->dd_data;
244 host = rport_to_shost(rport);
245 vha = shost_priv(host);
246 ha = vha->hw;
247 type = "FC_BSG_RPT_ELS";
248 } else {
249 host = bsg_job->shost;
250 vha = shost_priv(host);
251 ha = vha->hw;
252 type = "FC_BSG_HST_ELS_NOLOGIN";
253 }
254
255 /* pass through is supported only for ISP 4Gb or higher */
256 if (!IS_FWI2_CAPABLE(ha)) {
7c3df132
SK
257 ql_dbg(ql_dbg_user, vha, 0x7001,
258 "ELS passthru not supported for ISP23xx based adapters.\n");
08f71e09
HZ
259 rval = -EPERM;
260 goto done;
261 }
262
6e98016c
GM
263 /* Multiple SG's are not supported for ELS requests */
264 if (bsg_job->request_payload.sg_cnt > 1 ||
265 bsg_job->reply_payload.sg_cnt > 1) {
7c3df132
SK
266 ql_dbg(ql_dbg_user, vha, 0x7002,
267 "Multiple SG's are not suppored for ELS requests, "
268 "request_sg_cnt=%x reply_sg_cnt=%x.\n",
269 bsg_job->request_payload.sg_cnt,
270 bsg_job->reply_payload.sg_cnt);
6e98016c
GM
271 rval = -EPERM;
272 goto done;
273 }
274
275 /* ELS request for rport */
276 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
6e98016c
GM
277 /* make sure the rport is logged in,
278 * if not perform fabric login
279 */
280 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
7c3df132
SK
281 ql_dbg(ql_dbg_user, vha, 0x7003,
282 "Failed to login port %06X for ELS passthru.\n",
283 fcport->d_id.b24);
6e98016c
GM
284 rval = -EIO;
285 goto done;
286 }
287 } else {
6e98016c
GM
288 /* Allocate a dummy fcport structure, since functions
289 * preparing the IOCB and mailbox command retrieves port
290 * specific information from fcport structure. For Host based
291 * ELS commands there will be no fcport structure allocated
292 */
293 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
294 if (!fcport) {
295 rval = -ENOMEM;
296 goto done;
297 }
298
299 /* Initialize all required fields of fcport */
300 fcport->vha = vha;
6e98016c
GM
301 fcport->d_id.b.al_pa =
302 bsg_job->request->rqst_data.h_els.port_id[0];
303 fcport->d_id.b.area =
304 bsg_job->request->rqst_data.h_els.port_id[1];
305 fcport->d_id.b.domain =
306 bsg_job->request->rqst_data.h_els.port_id[2];
307 fcport->loop_id =
308 (fcport->d_id.b.al_pa == 0xFD) ?
309 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
310 }
311
312 if (!vha->flags.online) {
7c3df132 313 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
6e98016c
GM
314 rval = -EIO;
315 goto done;
316 }
317
318 req_sg_cnt =
319 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
320 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
321 if (!req_sg_cnt) {
322 rval = -ENOMEM;
323 goto done_free_fcport;
324 }
6c452a45
AV
325
326 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
327 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
6e98016c
GM
328 if (!rsp_sg_cnt) {
329 rval = -ENOMEM;
330 goto done_free_fcport;
331 }
332
333 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
6c452a45 334 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
335 ql_log(ql_log_warn, vha, 0x7008,
336 "dma mapping resulted in different sg counts, "
337 "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
338 "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
339 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
6e98016c
GM
340 rval = -EAGAIN;
341 goto done_unmap_sg;
342 }
343
344 /* Alloc SRB structure */
9ba56b95 345 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
6e98016c
GM
346 if (!sp) {
347 rval = -ENOMEM;
6c452a45 348 goto done_unmap_sg;
6e98016c
GM
349 }
350
9ba56b95 351 sp->type =
6e98016c
GM
352 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
353 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
9ba56b95 354 sp->name =
3822263e
MI
355 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
356 "bsg_els_rpt" : "bsg_els_hst");
9ba56b95
GM
357 sp->u.bsg_job = bsg_job;
358 sp->free = qla2x00_bsg_sp_free;
359 sp->done = qla2x00_bsg_job_done;
6e98016c 360
7c3df132
SK
361 ql_dbg(ql_dbg_user, vha, 0x700a,
362 "bsg rqst type: %s els type: %x - loop-id=%x "
363 "portid=%-2x%02x%02x.\n", type,
364 bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
365 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
6e98016c
GM
366
367 rval = qla2x00_start_sp(sp);
368 if (rval != QLA_SUCCESS) {
7c3df132
SK
369 ql_log(ql_log_warn, vha, 0x700e,
370 "qla2x00_start_sp failed = %d\n", rval);
b00ee7d7 371 qla2x00_rel_sp(vha, sp);
6e98016c
GM
372 rval = -EIO;
373 goto done_unmap_sg;
374 }
375 return rval;
376
377done_unmap_sg:
378 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
379 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
380 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
381 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
382 goto done_free_fcport;
383
384done_free_fcport:
385 if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
386 kfree(fcport);
387done:
388 return rval;
389}
390
5780790e
AV
391inline uint16_t
392qla24xx_calc_ct_iocbs(uint16_t dsds)
393{
394 uint16_t iocbs;
395
396 iocbs = 1;
397 if (dsds > 2) {
398 iocbs += (dsds - 2) / 5;
399 if ((dsds - 2) % 5)
400 iocbs++;
401 }
402 return iocbs;
403}
404
6e98016c
GM
405static int
406qla2x00_process_ct(struct fc_bsg_job *bsg_job)
407{
408 srb_t *sp;
409 struct Scsi_Host *host = bsg_job->shost;
410 scsi_qla_host_t *vha = shost_priv(host);
411 struct qla_hw_data *ha = vha->hw;
412 int rval = (DRIVER_ERROR << 16);
413 int req_sg_cnt, rsp_sg_cnt;
414 uint16_t loop_id;
415 struct fc_port *fcport;
416 char *type = "FC_BSG_HST_CT";
6e98016c 417
6e98016c
GM
418 req_sg_cnt =
419 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
420 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
6c452a45 421 if (!req_sg_cnt) {
7c3df132
SK
422 ql_log(ql_log_warn, vha, 0x700f,
423 "dma_map_sg return %d for request\n", req_sg_cnt);
6e98016c
GM
424 rval = -ENOMEM;
425 goto done;
426 }
427
428 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
429 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
430 if (!rsp_sg_cnt) {
7c3df132
SK
431 ql_log(ql_log_warn, vha, 0x7010,
432 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
6e98016c
GM
433 rval = -ENOMEM;
434 goto done;
435 }
436
437 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
6c452a45 438 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
439 ql_log(ql_log_warn, vha, 0x7011,
440 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
441 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
442 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
6e98016c 443 rval = -EAGAIN;
6c452a45 444 goto done_unmap_sg;
6e98016c
GM
445 }
446
447 if (!vha->flags.online) {
7c3df132
SK
448 ql_log(ql_log_warn, vha, 0x7012,
449 "Host is not online.\n");
6e98016c
GM
450 rval = -EIO;
451 goto done_unmap_sg;
452 }
453
454 loop_id =
455 (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
456 >> 24;
457 switch (loop_id) {
6c452a45
AV
458 case 0xFC:
459 loop_id = cpu_to_le16(NPH_SNS);
460 break;
461 case 0xFA:
462 loop_id = vha->mgmt_svr_loop_id;
463 break;
464 default:
7c3df132
SK
465 ql_dbg(ql_dbg_user, vha, 0x7013,
466 "Unknown loop id: %x.\n", loop_id);
6c452a45
AV
467 rval = -EINVAL;
468 goto done_unmap_sg;
6e98016c
GM
469 }
470
471 /* Allocate a dummy fcport structure, since functions preparing the
472 * IOCB and mailbox command retrieves port specific information
473 * from fcport structure. For Host based ELS commands there will be
474 * no fcport structure allocated
475 */
476 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6c452a45 477 if (!fcport) {
7c3df132
SK
478 ql_log(ql_log_warn, vha, 0x7014,
479 "Failed to allocate fcport.\n");
6e98016c 480 rval = -ENOMEM;
6c452a45 481 goto done_unmap_sg;
6e98016c
GM
482 }
483
484 /* Initialize all required fields of fcport */
485 fcport->vha = vha;
6e98016c
GM
486 fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
487 fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
488 fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
489 fcport->loop_id = loop_id;
490
491 /* Alloc SRB structure */
9ba56b95 492 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
6e98016c 493 if (!sp) {
7c3df132 494 ql_log(ql_log_warn, vha, 0x7015,
9ba56b95 495 "qla2x00_get_sp failed.\n");
6e98016c
GM
496 rval = -ENOMEM;
497 goto done_free_fcport;
498 }
499
9ba56b95
GM
500 sp->type = SRB_CT_CMD;
501 sp->name = "bsg_ct";
502 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
503 sp->u.bsg_job = bsg_job;
504 sp->free = qla2x00_bsg_sp_free;
505 sp->done = qla2x00_bsg_job_done;
6e98016c 506
7c3df132
SK
507 ql_dbg(ql_dbg_user, vha, 0x7016,
508 "bsg rqst type: %s else type: %x - "
509 "loop-id=%x portid=%02x%02x%02x.\n", type,
510 (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
511 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
512 fcport->d_id.b.al_pa);
6e98016c
GM
513
514 rval = qla2x00_start_sp(sp);
515 if (rval != QLA_SUCCESS) {
7c3df132
SK
516 ql_log(ql_log_warn, vha, 0x7017,
517 "qla2x00_start_sp failed=%d.\n", rval);
b00ee7d7 518 qla2x00_rel_sp(vha, sp);
6e98016c
GM
519 rval = -EIO;
520 goto done_free_fcport;
521 }
522 return rval;
523
524done_free_fcport:
525 kfree(fcport);
526done_unmap_sg:
527 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
528 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
529 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
530 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
531done:
532 return rval;
533}
67b2a31f
CD
534
535/* Disable loopback mode */
536static inline int
537qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
538 int wait)
539{
540 int ret = 0;
541 int rval = 0;
542 uint16_t new_config[4];
543 struct qla_hw_data *ha = vha->hw;
544
545 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
546 goto done_reset_internal;
547
548 memset(new_config, 0 , sizeof(new_config));
549 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
550 ENABLE_INTERNAL_LOOPBACK ||
551 (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
552 ENABLE_EXTERNAL_LOOPBACK) {
553 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
554 ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
555 (new_config[0] & INTERNAL_LOOPBACK_MASK));
556 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
557
558 ha->notify_dcbx_comp = wait;
559 ret = qla81xx_set_port_config(vha, new_config);
560 if (ret != QLA_SUCCESS) {
561 ql_log(ql_log_warn, vha, 0x7025,
562 "Set port config failed.\n");
563 ha->notify_dcbx_comp = 0;
564 rval = -EINVAL;
565 goto done_reset_internal;
566 }
567
568 /* Wait for DCBX complete event */
569 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
570 (20 * HZ))) {
571 ql_dbg(ql_dbg_user, vha, 0x7026,
572 "State change notification not received.\n");
573 ha->notify_dcbx_comp = 0;
574 rval = -EINVAL;
575 goto done_reset_internal;
576 } else
577 ql_dbg(ql_dbg_user, vha, 0x7027,
578 "State change received.\n");
579
580 ha->notify_dcbx_comp = 0;
581 }
582done_reset_internal:
583 return rval;
584}
585
8fcd6b8b
CD
586/*
587 * Set the port configuration to enable the internal or external loopback
588 * depending on the loopback mode.
23f2ebd1
SR
589 */
590static inline int
8fcd6b8b
CD
591qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
592 uint16_t *new_config, uint16_t mode)
23f2ebd1
SR
593{
594 int ret = 0;
595 int rval = 0;
596 struct qla_hw_data *ha = vha->hw;
597
f863f603 598 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
23f2ebd1
SR
599 goto done_set_internal;
600
8fcd6b8b
CD
601 if (mode == INTERNAL_LOOPBACK)
602 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
603 else if (mode == EXTERNAL_LOOPBACK)
604 new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
605 ql_dbg(ql_dbg_user, vha, 0x70be,
606 "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
607
608 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
23f2ebd1
SR
609
610 ha->notify_dcbx_comp = 1;
611 ret = qla81xx_set_port_config(vha, new_config);
612 if (ret != QLA_SUCCESS) {
7c3df132
SK
613 ql_log(ql_log_warn, vha, 0x7021,
614 "set port config failed.\n");
23f2ebd1
SR
615 ha->notify_dcbx_comp = 0;
616 rval = -EINVAL;
617 goto done_set_internal;
618 }
619
620 /* Wait for DCBX complete event */
621 if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
7c3df132
SK
622 ql_dbg(ql_dbg_user, vha, 0x7022,
623 "State change notification not received.\n");
67b2a31f
CD
624 ret = qla81xx_reset_loopback_mode(vha, new_config, 0);
625 /*
626 * If the reset of the loopback mode doesn't work take a FCoE
627 * dump and reset the chip.
628 */
629 if (ret) {
630 ha->isp_ops->fw_dump(vha, 0);
631 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
632 }
bf5b8ad7
CD
633 rval = -EINVAL;
634 } else {
635 if (ha->flags.idc_compl_status) {
636 ql_dbg(ql_dbg_user, vha, 0x70c3,
637 "Bad status in IDC Completion AEN\n");
638 rval = -EINVAL;
639 ha->flags.idc_compl_status = 0;
640 } else
641 ql_dbg(ql_dbg_user, vha, 0x7023,
642 "State change received.\n");
643 }
23f2ebd1
SR
644
645 ha->notify_dcbx_comp = 0;
646
647done_set_internal:
648 return rval;
649}
650
6e98016c
GM
651static int
652qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
653{
654 struct Scsi_Host *host = bsg_job->shost;
655 scsi_qla_host_t *vha = shost_priv(host);
656 struct qla_hw_data *ha = vha->hw;
657 int rval;
658 uint8_t command_sent;
659 char *type;
660 struct msg_echo_lb elreq;
661 uint16_t response[MAILBOX_REGISTER_COUNT];
23f2ebd1 662 uint16_t config[4], new_config[4];
6c452a45 663 uint8_t *fw_sts_ptr;
6e98016c
GM
664 uint8_t *req_data = NULL;
665 dma_addr_t req_data_dma;
666 uint32_t req_data_len;
667 uint8_t *rsp_data = NULL;
668 dma_addr_t rsp_data_dma;
669 uint32_t rsp_data_len;
670
6e98016c 671 if (!vha->flags.online) {
7c3df132 672 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
6e98016c
GM
673 return -EIO;
674 }
675
676 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
677 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
678 DMA_TO_DEVICE);
679
7c3df132
SK
680 if (!elreq.req_sg_cnt) {
681 ql_log(ql_log_warn, vha, 0x701a,
682 "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
6e98016c 683 return -ENOMEM;
7c3df132 684 }
6e98016c
GM
685
686 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
687 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
688 DMA_FROM_DEVICE);
689
690 if (!elreq.rsp_sg_cnt) {
7c3df132
SK
691 ql_log(ql_log_warn, vha, 0x701b,
692 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
6e98016c
GM
693 rval = -ENOMEM;
694 goto done_unmap_req_sg;
6c452a45 695 }
6e98016c
GM
696
697 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
698 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
7c3df132
SK
699 ql_log(ql_log_warn, vha, 0x701c,
700 "dma mapping resulted in different sg counts, "
701 "request_sg_cnt: %x dma_request_sg_cnt: %x "
702 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
703 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
704 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
6e98016c
GM
705 rval = -EAGAIN;
706 goto done_unmap_sg;
707 }
708 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
709 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
710 &req_data_dma, GFP_KERNEL);
711 if (!req_data) {
7c3df132
SK
712 ql_log(ql_log_warn, vha, 0x701d,
713 "dma alloc failed for req_data.\n");
6e98016c
GM
714 rval = -ENOMEM;
715 goto done_unmap_sg;
716 }
717
718 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
719 &rsp_data_dma, GFP_KERNEL);
720 if (!rsp_data) {
7c3df132
SK
721 ql_log(ql_log_warn, vha, 0x7004,
722 "dma alloc failed for rsp_data.\n");
6e98016c
GM
723 rval = -ENOMEM;
724 goto done_free_dma_req;
725 }
726
727 /* Copy the request buffer in req_data now */
728 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
729 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
730
731 elreq.send_dma = req_data_dma;
732 elreq.rcv_dma = rsp_data_dma;
733 elreq.transfer_size = req_data_len;
734
735 elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
736
8fcd6b8b
CD
737 if (atomic_read(&vha->loop_state) == LOOP_READY &&
738 (ha->current_topology == ISP_CFG_F ||
f863f603 739 ((IS_QLA81XX(ha) || IS_QLA8031(ha)) &&
23f2ebd1
SR
740 le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
741 && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
742 elreq.options == EXTERNAL_LOOPBACK) {
743 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
7c3df132
SK
744 ql_dbg(ql_dbg_user, vha, 0x701e,
745 "BSG request type: %s.\n", type);
23f2ebd1
SR
746 command_sent = INT_DEF_LB_ECHO_CMD;
747 rval = qla2x00_echo_test(vha, &elreq, response);
748 } else {
6246b8a1 749 if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
23f2ebd1
SR
750 memset(config, 0, sizeof(config));
751 memset(new_config, 0, sizeof(new_config));
752 if (qla81xx_get_port_config(vha, config)) {
7c3df132
SK
753 ql_log(ql_log_warn, vha, 0x701f,
754 "Get port config failed.\n");
23f2ebd1 755 rval = -EPERM;
9bceab4e 756 goto done_free_dma_rsp;
23f2ebd1
SR
757 }
758
1bcc46cb
CD
759 if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
760 ql_dbg(ql_dbg_user, vha, 0x70c4,
761 "Loopback operation already in "
762 "progress.\n");
763 rval = -EAGAIN;
764 goto done_free_dma_rsp;
765 }
766
8fcd6b8b
CD
767 ql_dbg(ql_dbg_user, vha, 0x70c0,
768 "elreq.options=%04x\n", elreq.options);
769
770 if (elreq.options == EXTERNAL_LOOPBACK)
771 if (IS_QLA8031(ha))
772 rval = qla81xx_set_loopback_mode(vha,
773 config, new_config, elreq.options);
774 else
775 rval = qla81xx_reset_loopback_mode(vha,
776 config, 1);
777 else
778 rval = qla81xx_set_loopback_mode(vha, config,
779 new_config, elreq.options);
780
781 if (rval) {
8fcd6b8b 782 rval = -EPERM;
9bceab4e 783 goto done_free_dma_rsp;
23f2ebd1
SR
784 }
785
786 type = "FC_BSG_HST_VENDOR_LOOPBACK";
7c3df132
SK
787 ql_dbg(ql_dbg_user, vha, 0x7028,
788 "BSG request type: %s.\n", type);
23f2ebd1
SR
789
790 command_sent = INT_DEF_LB_LOOPBACK_CMD;
791 rval = qla2x00_loopback_test(vha, &elreq, response);
792
992357c6
CD
793 if (response[0] == MBS_COMMAND_ERROR &&
794 response[1] == MBS_LB_RESET) {
795 ql_log(ql_log_warn, vha, 0x7029,
796 "MBX command error, Aborting ISP.\n");
797 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
798 qla2xxx_wake_dpc(vha);
799 qla2x00_wait_for_chip_reset(vha);
800 /* Also reset the MPI */
801 if (IS_QLA81XX(ha)) {
802 if (qla81xx_restart_mpi_firmware(vha) !=
803 QLA_SUCCESS) {
804 ql_log(ql_log_warn, vha, 0x702a,
805 "MPI reset failed.\n");
806 }
807 }
808
809 rval = -EIO;
810 goto done_free_dma_rsp;
811 }
812
4052bd57 813 if (new_config[0]) {
67b2a31f
CD
814 int ret;
815
23f2ebd1
SR
816 /* Revert back to original port config
817 * Also clear internal loopback
818 */
67b2a31f 819 ret = qla81xx_reset_loopback_mode(vha,
23f2ebd1 820 new_config, 0);
67b2a31f
CD
821 if (ret) {
822 /*
823 * If the reset of the loopback mode
824 * doesn't work take FCoE dump and then
825 * reset the chip.
826 */
827 ha->isp_ops->fw_dump(vha, 0);
828 set_bit(ISP_ABORT_NEEDED,
829 &vha->dpc_flags);
830 }
831
23f2ebd1
SR
832 }
833
23f2ebd1
SR
834 } else {
835 type = "FC_BSG_HST_VENDOR_LOOPBACK";
7c3df132
SK
836 ql_dbg(ql_dbg_user, vha, 0x702b,
837 "BSG request type: %s.\n", type);
23f2ebd1
SR
838 command_sent = INT_DEF_LB_LOOPBACK_CMD;
839 rval = qla2x00_loopback_test(vha, &elreq, response);
6e98016c 840 }
6e98016c
GM
841 }
842
843 if (rval) {
7c3df132
SK
844 ql_log(ql_log_warn, vha, 0x702c,
845 "Vendor request %s failed.\n", type);
6e98016c 846
6e98016c 847 rval = 0;
6e98016c 848 bsg_job->reply->result = (DID_ERROR << 16);
63ea923a 849 bsg_job->reply->reply_payload_rcv_len = 0;
6e98016c 850 } else {
7c3df132
SK
851 ql_dbg(ql_dbg_user, vha, 0x702d,
852 "Vendor request %s completed.\n", type);
63ea923a 853 bsg_job->reply->result = (DID_OK << 16);
6e98016c
GM
854 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
855 bsg_job->reply_payload.sg_cnt, rsp_data,
856 rsp_data_len);
857 }
63ea923a
AB
858
859 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
860 sizeof(response) + sizeof(uint8_t);
861 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
862 sizeof(struct fc_bsg_reply);
863 memcpy(fw_sts_ptr, response, sizeof(response));
864 fw_sts_ptr += sizeof(response);
865 *fw_sts_ptr = command_sent;
6e98016c 866
9bceab4e 867done_free_dma_rsp:
6e98016c
GM
868 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
869 rsp_data, rsp_data_dma);
870done_free_dma_req:
871 dma_free_coherent(&ha->pdev->dev, req_data_len,
872 req_data, req_data_dma);
873done_unmap_sg:
874 dma_unmap_sg(&ha->pdev->dev,
875 bsg_job->reply_payload.sg_list,
876 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
877done_unmap_req_sg:
878 dma_unmap_sg(&ha->pdev->dev,
879 bsg_job->request_payload.sg_list,
880 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
63ea923a
AB
881 if (!rval)
882 bsg_job->job_done(bsg_job);
6c452a45 883 return rval;
6e98016c
GM
884}
885
886static int
887qla84xx_reset(struct fc_bsg_job *bsg_job)
888{
889 struct Scsi_Host *host = bsg_job->shost;
890 scsi_qla_host_t *vha = shost_priv(host);
891 struct qla_hw_data *ha = vha->hw;
892 int rval = 0;
893 uint32_t flag;
894
6e98016c 895 if (!IS_QLA84XX(ha)) {
7c3df132 896 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
6e98016c
GM
897 return -EINVAL;
898 }
899
900 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
901
902 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
903
904 if (rval) {
7c3df132
SK
905 ql_log(ql_log_warn, vha, 0x7030,
906 "Vendor request 84xx reset failed.\n");
63ea923a 907 rval = (DID_ERROR << 16);
6e98016c
GM
908
909 } else {
7c3df132
SK
910 ql_dbg(ql_dbg_user, vha, 0x7031,
911 "Vendor request 84xx reset completed.\n");
6e98016c 912 bsg_job->reply->result = DID_OK;
63ea923a 913 bsg_job->job_done(bsg_job);
6e98016c
GM
914 }
915
6e98016c
GM
916 return rval;
917}
918
919static int
920qla84xx_updatefw(struct fc_bsg_job *bsg_job)
921{
922 struct Scsi_Host *host = bsg_job->shost;
923 scsi_qla_host_t *vha = shost_priv(host);
924 struct qla_hw_data *ha = vha->hw;
925 struct verify_chip_entry_84xx *mn = NULL;
926 dma_addr_t mn_dma, fw_dma;
927 void *fw_buf = NULL;
928 int rval = 0;
929 uint32_t sg_cnt;
930 uint32_t data_len;
931 uint16_t options;
932 uint32_t flag;
933 uint32_t fw_ver;
934
6e98016c 935 if (!IS_QLA84XX(ha)) {
7c3df132
SK
936 ql_dbg(ql_dbg_user, vha, 0x7032,
937 "Not 84xx, exiting.\n");
6e98016c
GM
938 return -EINVAL;
939 }
940
941 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
942 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
7c3df132
SK
943 if (!sg_cnt) {
944 ql_log(ql_log_warn, vha, 0x7033,
945 "dma_map_sg returned %d for request.\n", sg_cnt);
6e98016c 946 return -ENOMEM;
7c3df132 947 }
6e98016c
GM
948
949 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
7c3df132
SK
950 ql_log(ql_log_warn, vha, 0x7034,
951 "DMA mapping resulted in different sg counts, "
952 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
953 bsg_job->request_payload.sg_cnt, sg_cnt);
6e98016c
GM
954 rval = -EAGAIN;
955 goto done_unmap_sg;
956 }
957
958 data_len = bsg_job->request_payload.payload_len;
959 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
960 &fw_dma, GFP_KERNEL);
961 if (!fw_buf) {
7c3df132
SK
962 ql_log(ql_log_warn, vha, 0x7035,
963 "DMA alloc failed for fw_buf.\n");
6e98016c
GM
964 rval = -ENOMEM;
965 goto done_unmap_sg;
966 }
967
968 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
969 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
970
971 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
972 if (!mn) {
7c3df132
SK
973 ql_log(ql_log_warn, vha, 0x7036,
974 "DMA alloc failed for fw buffer.\n");
6e98016c
GM
975 rval = -ENOMEM;
976 goto done_free_fw_buf;
977 }
978
979 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
980 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
981
982 memset(mn, 0, sizeof(struct access_chip_84xx));
983 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
984 mn->entry_count = 1;
985
986 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
987 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
988 options |= VCO_DIAG_FW;
989
990 mn->options = cpu_to_le16(options);
991 mn->fw_ver = cpu_to_le32(fw_ver);
992 mn->fw_size = cpu_to_le32(data_len);
993 mn->fw_seq_size = cpu_to_le32(data_len);
994 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
995 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
996 mn->dseg_length = cpu_to_le32(data_len);
997 mn->data_seg_cnt = cpu_to_le16(1);
998
999 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
1000
1001 if (rval) {
7c3df132
SK
1002 ql_log(ql_log_warn, vha, 0x7037,
1003 "Vendor request 84xx updatefw failed.\n");
6e98016c 1004
63ea923a 1005 rval = (DID_ERROR << 16);
6e98016c 1006 } else {
7c3df132
SK
1007 ql_dbg(ql_dbg_user, vha, 0x7038,
1008 "Vendor request 84xx updatefw completed.\n");
6e98016c
GM
1009
1010 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1011 bsg_job->reply->result = DID_OK;
1012 }
1013
6e98016c
GM
1014 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1015
1016done_free_fw_buf:
1017 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
1018
1019done_unmap_sg:
1020 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1021 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1022
63ea923a
AB
1023 if (!rval)
1024 bsg_job->job_done(bsg_job);
6e98016c
GM
1025 return rval;
1026}
1027
1028static int
1029qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
1030{
1031 struct Scsi_Host *host = bsg_job->shost;
1032 scsi_qla_host_t *vha = shost_priv(host);
1033 struct qla_hw_data *ha = vha->hw;
1034 struct access_chip_84xx *mn = NULL;
1035 dma_addr_t mn_dma, mgmt_dma;
1036 void *mgmt_b = NULL;
1037 int rval = 0;
1038 struct qla_bsg_a84_mgmt *ql84_mgmt;
1039 uint32_t sg_cnt;
d5459083 1040 uint32_t data_len = 0;
6e98016c
GM
1041 uint32_t dma_direction = DMA_NONE;
1042
6e98016c 1043 if (!IS_QLA84XX(ha)) {
7c3df132
SK
1044 ql_log(ql_log_warn, vha, 0x703a,
1045 "Not 84xx, exiting.\n");
6e98016c
GM
1046 return -EINVAL;
1047 }
1048
1049 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
1050 sizeof(struct fc_bsg_request));
1051 if (!ql84_mgmt) {
7c3df132
SK
1052 ql_log(ql_log_warn, vha, 0x703b,
1053 "MGMT header not provided, exiting.\n");
6e98016c
GM
1054 return -EINVAL;
1055 }
1056
1057 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1058 if (!mn) {
7c3df132
SK
1059 ql_log(ql_log_warn, vha, 0x703c,
1060 "DMA alloc failed for fw buffer.\n");
6e98016c
GM
1061 return -ENOMEM;
1062 }
1063
1064 memset(mn, 0, sizeof(struct access_chip_84xx));
1065 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1066 mn->entry_count = 1;
1067
1068 switch (ql84_mgmt->mgmt.cmd) {
1069 case QLA84_MGMT_READ_MEM:
1070 case QLA84_MGMT_GET_INFO:
1071 sg_cnt = dma_map_sg(&ha->pdev->dev,
1072 bsg_job->reply_payload.sg_list,
1073 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1074 if (!sg_cnt) {
7c3df132
SK
1075 ql_log(ql_log_warn, vha, 0x703d,
1076 "dma_map_sg returned %d for reply.\n", sg_cnt);
6e98016c
GM
1077 rval = -ENOMEM;
1078 goto exit_mgmt;
1079 }
1080
1081 dma_direction = DMA_FROM_DEVICE;
1082
1083 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
7c3df132
SK
1084 ql_log(ql_log_warn, vha, 0x703e,
1085 "DMA mapping resulted in different sg counts, "
1086 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1087 bsg_job->reply_payload.sg_cnt, sg_cnt);
6e98016c
GM
1088 rval = -EAGAIN;
1089 goto done_unmap_sg;
1090 }
1091
1092 data_len = bsg_job->reply_payload.payload_len;
1093
1094 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1095 &mgmt_dma, GFP_KERNEL);
1096 if (!mgmt_b) {
7c3df132
SK
1097 ql_log(ql_log_warn, vha, 0x703f,
1098 "DMA alloc failed for mgmt_b.\n");
6e98016c
GM
1099 rval = -ENOMEM;
1100 goto done_unmap_sg;
1101 }
1102
1103 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1104 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1105 mn->parameter1 =
1106 cpu_to_le32(
1107 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1108
1109 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1110 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1111 mn->parameter1 =
1112 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1113
1114 mn->parameter2 =
1115 cpu_to_le32(
1116 ql84_mgmt->mgmt.mgmtp.u.info.context);
1117 }
1118 break;
1119
1120 case QLA84_MGMT_WRITE_MEM:
1121 sg_cnt = dma_map_sg(&ha->pdev->dev,
1122 bsg_job->request_payload.sg_list,
1123 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1124
1125 if (!sg_cnt) {
7c3df132
SK
1126 ql_log(ql_log_warn, vha, 0x7040,
1127 "dma_map_sg returned %d.\n", sg_cnt);
6e98016c
GM
1128 rval = -ENOMEM;
1129 goto exit_mgmt;
1130 }
1131
1132 dma_direction = DMA_TO_DEVICE;
1133
1134 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
7c3df132
SK
1135 ql_log(ql_log_warn, vha, 0x7041,
1136 "DMA mapping resulted in different sg counts, "
1137 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1138 bsg_job->request_payload.sg_cnt, sg_cnt);
6e98016c
GM
1139 rval = -EAGAIN;
1140 goto done_unmap_sg;
1141 }
1142
1143 data_len = bsg_job->request_payload.payload_len;
1144 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1145 &mgmt_dma, GFP_KERNEL);
1146 if (!mgmt_b) {
7c3df132
SK
1147 ql_log(ql_log_warn, vha, 0x7042,
1148 "DMA alloc failed for mgmt_b.\n");
6e98016c
GM
1149 rval = -ENOMEM;
1150 goto done_unmap_sg;
1151 }
1152
1153 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1154 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1155
1156 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1157 mn->parameter1 =
1158 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1159 break;
1160
1161 case QLA84_MGMT_CHNG_CONFIG:
1162 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1163 mn->parameter1 =
1164 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1165
1166 mn->parameter2 =
1167 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1168
1169 mn->parameter3 =
1170 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1171 break;
1172
1173 default:
1174 rval = -EIO;
1175 goto exit_mgmt;
1176 }
1177
1178 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1179 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1180 mn->dseg_count = cpu_to_le16(1);
1181 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1182 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1183 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1184 }
1185
1186 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1187
1188 if (rval) {
7c3df132
SK
1189 ql_log(ql_log_warn, vha, 0x7043,
1190 "Vendor request 84xx mgmt failed.\n");
6e98016c 1191
63ea923a 1192 rval = (DID_ERROR << 16);
6e98016c
GM
1193
1194 } else {
7c3df132
SK
1195 ql_dbg(ql_dbg_user, vha, 0x7044,
1196 "Vendor request 84xx mgmt completed.\n");
6e98016c
GM
1197
1198 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1199 bsg_job->reply->result = DID_OK;
1200
1201 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1202 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1203 bsg_job->reply->reply_payload_rcv_len =
1204 bsg_job->reply_payload.payload_len;
1205
1206 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
6c452a45
AV
1207 bsg_job->reply_payload.sg_cnt, mgmt_b,
1208 data_len);
6e98016c
GM
1209 }
1210 }
1211
6e98016c 1212done_unmap_sg:
d5459083
HZ
1213 if (mgmt_b)
1214 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1215
6e98016c
GM
1216 if (dma_direction == DMA_TO_DEVICE)
1217 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1218 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1219 else if (dma_direction == DMA_FROM_DEVICE)
1220 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1221 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1222
1223exit_mgmt:
1224 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1225
63ea923a
AB
1226 if (!rval)
1227 bsg_job->job_done(bsg_job);
6e98016c
GM
1228 return rval;
1229}
1230
1231static int
1232qla24xx_iidma(struct fc_bsg_job *bsg_job)
1233{
1234 struct Scsi_Host *host = bsg_job->shost;
1235 scsi_qla_host_t *vha = shost_priv(host);
6e98016c
GM
1236 int rval = 0;
1237 struct qla_port_param *port_param = NULL;
1238 fc_port_t *fcport = NULL;
1239 uint16_t mb[MAILBOX_REGISTER_COUNT];
1240 uint8_t *rsp_ptr = NULL;
1241
6e98016c 1242 if (!IS_IIDMA_CAPABLE(vha->hw)) {
7c3df132 1243 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
6e98016c
GM
1244 return -EINVAL;
1245 }
1246
1247 port_param = (struct qla_port_param *)((char *)bsg_job->request +
1248 sizeof(struct fc_bsg_request));
1249 if (!port_param) {
7c3df132
SK
1250 ql_log(ql_log_warn, vha, 0x7047,
1251 "port_param header not provided.\n");
6e98016c
GM
1252 return -EINVAL;
1253 }
1254
1255 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
7c3df132
SK
1256 ql_log(ql_log_warn, vha, 0x7048,
1257 "Invalid destination type.\n");
6e98016c
GM
1258 return -EINVAL;
1259 }
1260
1261 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1262 if (fcport->port_type != FCT_TARGET)
1263 continue;
1264
1265 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1266 fcport->port_name, sizeof(fcport->port_name)))
1267 continue;
1268 break;
1269 }
1270
1271 if (!fcport) {
7c3df132
SK
1272 ql_log(ql_log_warn, vha, 0x7049,
1273 "Failed to find port.\n");
6e98016c
GM
1274 return -EINVAL;
1275 }
1276
c9afb9a2 1277 if (atomic_read(&fcport->state) != FCS_ONLINE) {
7c3df132
SK
1278 ql_log(ql_log_warn, vha, 0x704a,
1279 "Port is not online.\n");
17cf2c5d
MI
1280 return -EINVAL;
1281 }
1282
9a15eb4b 1283 if (fcport->flags & FCF_LOGIN_NEEDED) {
7c3df132
SK
1284 ql_log(ql_log_warn, vha, 0x704b,
1285 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
9a15eb4b
MI
1286 return -EINVAL;
1287 }
1288
6e98016c
GM
1289 if (port_param->mode)
1290 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1291 port_param->speed, mb);
1292 else
1293 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1294 &port_param->speed, mb);
1295
1296 if (rval) {
7c3df132
SK
1297 ql_log(ql_log_warn, vha, 0x704c,
1298 "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
1299 "%04x %x %04x %04x.\n", fcport->port_name[0],
1300 fcport->port_name[1], fcport->port_name[2],
1301 fcport->port_name[3], fcport->port_name[4],
1302 fcport->port_name[5], fcport->port_name[6],
1303 fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
63ea923a 1304 rval = (DID_ERROR << 16);
6e98016c
GM
1305 } else {
1306 if (!port_param->mode) {
1307 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1308 sizeof(struct qla_port_param);
1309
1310 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1311 sizeof(struct fc_bsg_reply);
1312
1313 memcpy(rsp_ptr, port_param,
1314 sizeof(struct qla_port_param));
1315 }
1316
1317 bsg_job->reply->result = DID_OK;
63ea923a 1318 bsg_job->job_done(bsg_job);
6e98016c
GM
1319 }
1320
6e98016c
GM
1321 return rval;
1322}
1323
f19af163 1324static int
7c3df132 1325qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
f19af163
HZ
1326 uint8_t is_update)
1327{
1328 uint32_t start = 0;
1329 int valid = 0;
7c3df132 1330 struct qla_hw_data *ha = vha->hw;
f19af163 1331
f19af163
HZ
1332 if (unlikely(pci_channel_offline(ha->pdev)))
1333 return -EINVAL;
1334
1335 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
7c3df132
SK
1336 if (start > ha->optrom_size) {
1337 ql_log(ql_log_warn, vha, 0x7055,
1338 "start %d > optrom_size %d.\n", start, ha->optrom_size);
f19af163 1339 return -EINVAL;
7c3df132 1340 }
f19af163 1341
7c3df132
SK
1342 if (ha->optrom_state != QLA_SWAITING) {
1343 ql_log(ql_log_info, vha, 0x7056,
1344 "optrom_state %d.\n", ha->optrom_state);
f19af163 1345 return -EBUSY;
7c3df132 1346 }
f19af163
HZ
1347
1348 ha->optrom_region_start = start;
7c3df132 1349 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
f19af163
HZ
1350 if (is_update) {
1351 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1352 valid = 1;
1353 else if (start == (ha->flt_region_boot * 4) ||
1354 start == (ha->flt_region_fw * 4))
1355 valid = 1;
1356 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
6246b8a1 1357 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
f19af163
HZ
1358 valid = 1;
1359 if (!valid) {
7c3df132
SK
1360 ql_log(ql_log_warn, vha, 0x7058,
1361 "Invalid start region 0x%x/0x%x.\n", start,
1362 bsg_job->request_payload.payload_len);
f19af163
HZ
1363 return -EINVAL;
1364 }
1365
1366 ha->optrom_region_size = start +
1367 bsg_job->request_payload.payload_len > ha->optrom_size ?
1368 ha->optrom_size - start :
1369 bsg_job->request_payload.payload_len;
1370 ha->optrom_state = QLA_SWRITING;
1371 } else {
1372 ha->optrom_region_size = start +
1373 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1374 ha->optrom_size - start :
1375 bsg_job->reply_payload.payload_len;
1376 ha->optrom_state = QLA_SREADING;
1377 }
1378
1379 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1380 if (!ha->optrom_buffer) {
7c3df132 1381 ql_log(ql_log_warn, vha, 0x7059,
f19af163 1382 "Read: Unable to allocate memory for optrom retrieval "
7c3df132 1383 "(%x)\n", ha->optrom_region_size);
f19af163
HZ
1384
1385 ha->optrom_state = QLA_SWAITING;
1386 return -ENOMEM;
1387 }
1388
1389 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1390 return 0;
1391}
1392
1393static int
1394qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1395{
1396 struct Scsi_Host *host = bsg_job->shost;
1397 scsi_qla_host_t *vha = shost_priv(host);
1398 struct qla_hw_data *ha = vha->hw;
1399 int rval = 0;
1400
7d613ac6 1401 if (ha->flags.nic_core_reset_hdlr_active)
a49393f2
GM
1402 return -EBUSY;
1403
7c3df132 1404 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
f19af163
HZ
1405 if (rval)
1406 return rval;
1407
1408 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1409 ha->optrom_region_start, ha->optrom_region_size);
1410
1411 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1412 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1413 ha->optrom_region_size);
1414
1415 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1416 bsg_job->reply->result = DID_OK;
1417 vfree(ha->optrom_buffer);
1418 ha->optrom_buffer = NULL;
1419 ha->optrom_state = QLA_SWAITING;
1420 bsg_job->job_done(bsg_job);
1421 return rval;
1422}
1423
1424static int
1425qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1426{
1427 struct Scsi_Host *host = bsg_job->shost;
1428 scsi_qla_host_t *vha = shost_priv(host);
1429 struct qla_hw_data *ha = vha->hw;
1430 int rval = 0;
1431
7c3df132 1432 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
f19af163
HZ
1433 if (rval)
1434 return rval;
1435
b6d0d9d5
GM
1436 /* Set the isp82xx_no_md_cap not to capture minidump */
1437 ha->flags.isp82xx_no_md_cap = 1;
1438
f19af163
HZ
1439 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1440 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1441 ha->optrom_region_size);
1442
1443 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1444 ha->optrom_region_start, ha->optrom_region_size);
1445
1446 bsg_job->reply->result = DID_OK;
1447 vfree(ha->optrom_buffer);
1448 ha->optrom_buffer = NULL;
1449 ha->optrom_state = QLA_SWAITING;
1450 bsg_job->job_done(bsg_job);
1451 return rval;
1452}
1453
697a4bc6
JC
1454static int
1455qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1456{
1457 struct Scsi_Host *host = bsg_job->shost;
1458 scsi_qla_host_t *vha = shost_priv(host);
1459 struct qla_hw_data *ha = vha->hw;
1460 int rval = 0;
1461 uint8_t bsg[DMA_POOL_SIZE];
1462 struct qla_image_version_list *list = (void *)bsg;
1463 struct qla_image_version *image;
1464 uint32_t count;
1465 dma_addr_t sfp_dma;
1466 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1467 if (!sfp) {
1468 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1469 EXT_STATUS_NO_MEMORY;
1470 goto done;
1471 }
1472
1473 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1474 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1475
1476 image = list->version;
1477 count = list->count;
1478 while (count--) {
1479 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1480 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1481 image->field_address.device, image->field_address.offset,
1482 sizeof(image->field_info), image->field_address.option);
1483 if (rval) {
1484 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1485 EXT_STATUS_MAILBOX;
1486 goto dealloc;
1487 }
1488 image++;
1489 }
1490
1491 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1492
1493dealloc:
1494 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1495
1496done:
1497 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1498 bsg_job->reply->result = DID_OK << 16;
1499 bsg_job->job_done(bsg_job);
1500
1501 return 0;
1502}
1503
1504static int
1505qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1506{
1507 struct Scsi_Host *host = bsg_job->shost;
1508 scsi_qla_host_t *vha = shost_priv(host);
1509 struct qla_hw_data *ha = vha->hw;
1510 int rval = 0;
1511 uint8_t bsg[DMA_POOL_SIZE];
1512 struct qla_status_reg *sr = (void *)bsg;
1513 dma_addr_t sfp_dma;
1514 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1515 if (!sfp) {
1516 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1517 EXT_STATUS_NO_MEMORY;
1518 goto done;
1519 }
1520
1521 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1522 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1523
1524 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1525 sr->field_address.device, sr->field_address.offset,
1526 sizeof(sr->status_reg), sr->field_address.option);
1527 sr->status_reg = *sfp;
1528
1529 if (rval) {
1530 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1531 EXT_STATUS_MAILBOX;
1532 goto dealloc;
1533 }
1534
1535 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1536 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1537
1538 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1539
1540dealloc:
1541 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1542
1543done:
1544 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1545 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1546 bsg_job->reply->result = DID_OK << 16;
1547 bsg_job->job_done(bsg_job);
1548
1549 return 0;
1550}
1551
1552static int
1553qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1554{
1555 struct Scsi_Host *host = bsg_job->shost;
1556 scsi_qla_host_t *vha = shost_priv(host);
1557 struct qla_hw_data *ha = vha->hw;
1558 int rval = 0;
1559 uint8_t bsg[DMA_POOL_SIZE];
1560 struct qla_status_reg *sr = (void *)bsg;
1561 dma_addr_t sfp_dma;
1562 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1563 if (!sfp) {
1564 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1565 EXT_STATUS_NO_MEMORY;
1566 goto done;
1567 }
1568
1569 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1570 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1571
1572 *sfp = sr->status_reg;
1573 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1574 sr->field_address.device, sr->field_address.offset,
1575 sizeof(sr->status_reg), sr->field_address.option);
1576
1577 if (rval) {
1578 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1579 EXT_STATUS_MAILBOX;
1580 goto dealloc;
1581 }
1582
1583 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1584
1585dealloc:
1586 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1587
1588done:
1589 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1590 bsg_job->reply->result = DID_OK << 16;
1591 bsg_job->job_done(bsg_job);
1592
1593 return 0;
1594}
1595
9ebb5d9c
JC
1596static int
1597qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
1598{
1599 struct Scsi_Host *host = bsg_job->shost;
1600 scsi_qla_host_t *vha = shost_priv(host);
1601 struct qla_hw_data *ha = vha->hw;
1602 int rval = 0;
1603 uint8_t bsg[DMA_POOL_SIZE];
1604 struct qla_i2c_access *i2c = (void *)bsg;
1605 dma_addr_t sfp_dma;
1606 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1607 if (!sfp) {
1608 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1609 EXT_STATUS_NO_MEMORY;
1610 goto done;
1611 }
1612
1613 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1614 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1615
1616 memcpy(sfp, i2c->buffer, i2c->length);
1617 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1618 i2c->device, i2c->offset, i2c->length, i2c->option);
1619
1620 if (rval) {
1621 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1622 EXT_STATUS_MAILBOX;
1623 goto dealloc;
1624 }
1625
1626 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1627
1628dealloc:
1629 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1630
1631done:
1632 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1633 bsg_job->reply->result = DID_OK << 16;
1634 bsg_job->job_done(bsg_job);
1635
1636 return 0;
1637}
1638
1639static int
1640qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
1641{
1642 struct Scsi_Host *host = bsg_job->shost;
1643 scsi_qla_host_t *vha = shost_priv(host);
1644 struct qla_hw_data *ha = vha->hw;
1645 int rval = 0;
1646 uint8_t bsg[DMA_POOL_SIZE];
1647 struct qla_i2c_access *i2c = (void *)bsg;
1648 dma_addr_t sfp_dma;
1649 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1650 if (!sfp) {
1651 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1652 EXT_STATUS_NO_MEMORY;
1653 goto done;
1654 }
1655
1656 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1657 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1658
1659 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1660 i2c->device, i2c->offset, i2c->length, i2c->option);
1661
1662 if (rval) {
1663 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1664 EXT_STATUS_MAILBOX;
1665 goto dealloc;
1666 }
1667
1668 memcpy(i2c->buffer, sfp, i2c->length);
1669 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1670 bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1671
1672 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1673
1674dealloc:
1675 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1676
1677done:
1678 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1679 bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
1680 bsg_job->reply->result = DID_OK << 16;
1681 bsg_job->job_done(bsg_job);
1682
1683 return 0;
1684}
1685
a9b6f722
SK
1686static int
1687qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
1688{
1689 struct Scsi_Host *host = bsg_job->shost;
1690 scsi_qla_host_t *vha = shost_priv(host);
1691 struct qla_hw_data *ha = vha->hw;
1692 uint16_t thread_id;
1693 uint32_t rval = EXT_STATUS_OK;
1694 uint16_t req_sg_cnt = 0;
1695 uint16_t rsp_sg_cnt = 0;
1696 uint16_t nextlid = 0;
1697 uint32_t tot_dsds;
1698 srb_t *sp = NULL;
1699 uint32_t req_data_len = 0;
1700 uint32_t rsp_data_len = 0;
1701
1702 /* Check the type of the adapter */
1703 if (!IS_BIDI_CAPABLE(ha)) {
1704 ql_log(ql_log_warn, vha, 0x70a0,
1705 "This adapter is not supported\n");
1706 rval = EXT_STATUS_NOT_SUPPORTED;
1707 goto done;
1708 }
1709
1710 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1711 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1712 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1713 rval = EXT_STATUS_BUSY;
1714 goto done;
1715 }
1716
1717 /* Check if host is online */
1718 if (!vha->flags.online) {
1719 ql_log(ql_log_warn, vha, 0x70a1,
1720 "Host is not online\n");
1721 rval = EXT_STATUS_DEVICE_OFFLINE;
1722 goto done;
1723 }
1724
1725 /* Check if cable is plugged in or not */
1726 if (vha->device_flags & DFLG_NO_CABLE) {
1727 ql_log(ql_log_warn, vha, 0x70a2,
1728 "Cable is unplugged...\n");
1729 rval = EXT_STATUS_INVALID_CFG;
1730 goto done;
1731 }
1732
1733 /* Check if the switch is connected or not */
1734 if (ha->current_topology != ISP_CFG_F) {
1735 ql_log(ql_log_warn, vha, 0x70a3,
1736 "Host is not connected to the switch\n");
1737 rval = EXT_STATUS_INVALID_CFG;
1738 goto done;
1739 }
1740
1741 /* Check if operating mode is P2P */
1742 if (ha->operating_mode != P2P) {
1743 ql_log(ql_log_warn, vha, 0x70a4,
1744 "Host is operating mode is not P2p\n");
1745 rval = EXT_STATUS_INVALID_CFG;
1746 goto done;
1747 }
1748
1749 thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1750
1751 mutex_lock(&ha->selflogin_lock);
1752 if (vha->self_login_loop_id == 0) {
1753 /* Initialize all required fields of fcport */
1754 vha->bidir_fcport.vha = vha;
1755 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1756 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1757 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1758 vha->bidir_fcport.loop_id = vha->loop_id;
1759
1760 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1761 ql_log(ql_log_warn, vha, 0x70a7,
1762 "Failed to login port %06X for bidirectional IOCB\n",
1763 vha->bidir_fcport.d_id.b24);
1764 mutex_unlock(&ha->selflogin_lock);
1765 rval = EXT_STATUS_MAILBOX;
1766 goto done;
1767 }
1768 vha->self_login_loop_id = nextlid - 1;
1769
1770 }
1771 /* Assign the self login loop id to fcport */
1772 mutex_unlock(&ha->selflogin_lock);
1773
1774 vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1775
1776 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1777 bsg_job->request_payload.sg_list,
1778 bsg_job->request_payload.sg_cnt,
1779 DMA_TO_DEVICE);
1780
1781 if (!req_sg_cnt) {
1782 rval = EXT_STATUS_NO_MEMORY;
1783 goto done;
1784 }
1785
1786 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1787 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1788 DMA_FROM_DEVICE);
1789
1790 if (!rsp_sg_cnt) {
1791 rval = EXT_STATUS_NO_MEMORY;
1792 goto done_unmap_req_sg;
1793 }
1794
1795 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
1796 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1797 ql_dbg(ql_dbg_user, vha, 0x70a9,
1798 "Dma mapping resulted in different sg counts "
1799 "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1800 "%x dma_reply_sg_cnt: %x]\n",
1801 bsg_job->request_payload.sg_cnt, req_sg_cnt,
1802 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1803 rval = EXT_STATUS_NO_MEMORY;
1804 goto done_unmap_sg;
1805 }
1806
1807 if (req_data_len != rsp_data_len) {
1808 rval = EXT_STATUS_BUSY;
1809 ql_log(ql_log_warn, vha, 0x70aa,
1810 "req_data_len != rsp_data_len\n");
1811 goto done_unmap_sg;
1812 }
1813
1814 req_data_len = bsg_job->request_payload.payload_len;
1815 rsp_data_len = bsg_job->reply_payload.payload_len;
1816
1817
1818 /* Alloc SRB structure */
1819 sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1820 if (!sp) {
1821 ql_dbg(ql_dbg_user, vha, 0x70ac,
1822 "Alloc SRB structure failed\n");
1823 rval = EXT_STATUS_NO_MEMORY;
1824 goto done_unmap_sg;
1825 }
1826
1827 /*Populate srb->ctx with bidir ctx*/
1828 sp->u.bsg_job = bsg_job;
1829 sp->free = qla2x00_bsg_sp_free;
1830 sp->type = SRB_BIDI_CMD;
1831 sp->done = qla2x00_bsg_job_done;
1832
1833 /* Add the read and write sg count */
1834 tot_dsds = rsp_sg_cnt + req_sg_cnt;
1835
1836 rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1837 if (rval != EXT_STATUS_OK)
1838 goto done_free_srb;
1839 /* the bsg request will be completed in the interrupt handler */
1840 return rval;
1841
1842done_free_srb:
1843 mempool_free(sp, ha->srb_mempool);
1844done_unmap_sg:
1845 dma_unmap_sg(&ha->pdev->dev,
1846 bsg_job->reply_payload.sg_list,
1847 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1848done_unmap_req_sg:
1849 dma_unmap_sg(&ha->pdev->dev,
1850 bsg_job->request_payload.sg_list,
1851 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1852done:
1853
1854 /* Return an error vendor specific response
1855 * and complete the bsg request
1856 */
1857 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1858 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1859 bsg_job->reply->reply_payload_rcv_len = 0;
1860 bsg_job->reply->result = (DID_OK) << 16;
1861 bsg_job->job_done(bsg_job);
1862 /* Always retrun success, vendor rsp carries correct status */
1863 return 0;
1864}
1865
6e98016c
GM
1866static int
1867qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
1868{
1869 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
1870 case QL_VND_LOOPBACK:
1871 return qla2x00_process_loopback(bsg_job);
1872
1873 case QL_VND_A84_RESET:
1874 return qla84xx_reset(bsg_job);
1875
1876 case QL_VND_A84_UPDATE_FW:
1877 return qla84xx_updatefw(bsg_job);
1878
1879 case QL_VND_A84_MGMT_CMD:
1880 return qla84xx_mgmt_cmd(bsg_job);
1881
1882 case QL_VND_IIDMA:
1883 return qla24xx_iidma(bsg_job);
1884
09ff701a
SR
1885 case QL_VND_FCP_PRIO_CFG_CMD:
1886 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
1887
f19af163
HZ
1888 case QL_VND_READ_FLASH:
1889 return qla2x00_read_optrom(bsg_job);
1890
1891 case QL_VND_UPDATE_FLASH:
1892 return qla2x00_update_optrom(bsg_job);
1893
697a4bc6
JC
1894 case QL_VND_SET_FRU_VERSION:
1895 return qla2x00_update_fru_versions(bsg_job);
1896
1897 case QL_VND_READ_FRU_STATUS:
1898 return qla2x00_read_fru_status(bsg_job);
1899
1900 case QL_VND_WRITE_FRU_STATUS:
1901 return qla2x00_write_fru_status(bsg_job);
1902
9ebb5d9c
JC
1903 case QL_VND_WRITE_I2C:
1904 return qla2x00_write_i2c(bsg_job);
1905
1906 case QL_VND_READ_I2C:
1907 return qla2x00_read_i2c(bsg_job);
1908
a9b6f722
SK
1909 case QL_VND_DIAG_IO_CMD:
1910 return qla24xx_process_bidir_cmd(bsg_job);
1911
6e98016c 1912 default:
6e98016c
GM
1913 return -ENOSYS;
1914 }
1915}
1916
1917int
1918qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
1919{
1920 int ret = -EINVAL;
7c3df132
SK
1921 struct fc_rport *rport;
1922 fc_port_t *fcport = NULL;
1923 struct Scsi_Host *host;
1924 scsi_qla_host_t *vha;
1925
b7bfbe12
AV
1926 /* In case no data transferred. */
1927 bsg_job->reply->reply_payload_rcv_len = 0;
1928
7c3df132
SK
1929 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
1930 rport = bsg_job->rport;
1931 fcport = *(fc_port_t **) rport->dd_data;
1932 host = rport_to_shost(rport);
1933 vha = shost_priv(host);
1934 } else {
1935 host = bsg_job->shost;
1936 vha = shost_priv(host);
1937 }
1938
d051a5aa
AV
1939 if (qla2x00_reset_active(vha)) {
1940 ql_dbg(ql_dbg_user, vha, 0x709f,
1941 "BSG: ISP abort active/needed -- cmd=%d.\n",
1942 bsg_job->request->msgcode);
d051a5aa
AV
1943 return -EBUSY;
1944 }
1945
7c3df132 1946 ql_dbg(ql_dbg_user, vha, 0x7000,
cfb0919c 1947 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
6e98016c
GM
1948
1949 switch (bsg_job->request->msgcode) {
1950 case FC_BSG_RPT_ELS:
1951 case FC_BSG_HST_ELS_NOLOGIN:
1952 ret = qla2x00_process_els(bsg_job);
1953 break;
1954 case FC_BSG_HST_CT:
1955 ret = qla2x00_process_ct(bsg_job);
1956 break;
1957 case FC_BSG_HST_VENDOR:
1958 ret = qla2x00_process_vendor_specific(bsg_job);
1959 break;
1960 case FC_BSG_HST_ADD_RPORT:
1961 case FC_BSG_HST_DEL_RPORT:
1962 case FC_BSG_RPT_CT:
1963 default:
7c3df132 1964 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
6e98016c 1965 break;
6c452a45 1966 }
6e98016c
GM
1967 return ret;
1968}
1969
1970int
1971qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
1972{
1973 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
1974 struct qla_hw_data *ha = vha->hw;
1975 srb_t *sp;
1976 int cnt, que;
1977 unsigned long flags;
1978 struct req_que *req;
6e98016c
GM
1979
1980 /* find the bsg job from the active list of commands */
1981 spin_lock_irqsave(&ha->hardware_lock, flags);
1982 for (que = 0; que < ha->max_req_queues; que++) {
1983 req = ha->req_q_map[que];
1984 if (!req)
1985 continue;
1986
8d93f550 1987 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
6e98016c 1988 sp = req->outstanding_cmds[cnt];
6e98016c 1989 if (sp) {
9ba56b95
GM
1990 if (((sp->type == SRB_CT_CMD) ||
1991 (sp->type == SRB_ELS_CMD_HST))
1992 && (sp->u.bsg_job == bsg_job)) {
900a36e3 1993 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6e98016c 1994 if (ha->isp_ops->abort_command(sp)) {
7c3df132
SK
1995 ql_log(ql_log_warn, vha, 0x7089,
1996 "mbx abort_command "
1997 "failed.\n");
6e98016c
GM
1998 bsg_job->req->errors =
1999 bsg_job->reply->result = -EIO;
2000 } else {
7c3df132
SK
2001 ql_dbg(ql_dbg_user, vha, 0x708a,
2002 "mbx abort_command "
2003 "success.\n");
6e98016c
GM
2004 bsg_job->req->errors =
2005 bsg_job->reply->result = 0;
2006 }
900a36e3 2007 spin_lock_irqsave(&ha->hardware_lock, flags);
6e98016c
GM
2008 goto done;
2009 }
2010 }
2011 }
2012 }
2013 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7c3df132 2014 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
6e98016c
GM
2015 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
2016 return 0;
2017
2018done:
2019 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2020 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
2021 kfree(sp->fcport);
b00ee7d7 2022 qla2x00_rel_sp(vha, sp);
6e98016c
GM
2023 return 0;
2024}