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