qla_bsg.c 68.9 KB
Newer Older
1
	/*
2
 * QLogic Fibre Channel HBA Driver
3
 * Copyright (c)  2003-2014 QLogic Corporation
4 5 6 7 8 9 10 11
 *
 * See LICENSE.qla2xxx for copyright and licensing details.
 */
#include "qla_def.h"

#include <linux/kthread.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
12
#include <linux/bsg-lib.h>
13 14

/* BSG support for ELS/CT pass through */
15
void
16
qla2x00_bsg_job_done(void *ptr, int res)
17
{
18
	srb_t *sp = ptr;
19
	struct bsg_job *bsg_job = sp->u.bsg_job;
20
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
21

22
	bsg_reply->result = res;
J
Johannes Thumshirn 已提交
23
	bsg_job_done(bsg_job, bsg_reply->result,
24
		       bsg_reply->reply_payload_rcv_len);
25
	sp->free(sp);
26 27 28
}

void
29
qla2x00_bsg_sp_free(void *ptr)
30
{
31 32
	srb_t *sp = ptr;
	struct qla_hw_data *ha = sp->vha->hw;
33
	struct bsg_job *bsg_job = sp->u.bsg_job;
34
	struct fc_bsg_request *bsg_request = bsg_job->request;
35
	struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
36

37 38
	if (sp->type == SRB_FXIOCB_BCMD) {
		piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
39
		    &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
			dma_unmap_sg(&ha->pdev->dev,
			    bsg_job->request_payload.sg_list,
			    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);

		if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
			dma_unmap_sg(&ha->pdev->dev,
			    bsg_job->reply_payload.sg_list,
			    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
	} else {
		dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
		    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);

		dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
		    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
	}
57 58

	if (sp->type == SRB_CT_CMD ||
59
	    sp->type == SRB_FXIOCB_BCMD ||
60 61
	    sp->type == SRB_ELS_CMD_HST)
		kfree(sp->fcport);
62
	qla2x00_rel_sp(sp);
63 64
}

S
Sarang Radke 已提交
65
int
66 67
qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
	struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
S
Sarang Radke 已提交
68 69 70 71
{
	int i, ret, num_valid;
	uint8_t *bcode;
	struct qla_fcp_prio_entry *pri_entry;
72
	uint32_t *bcode_val_ptr, bcode_val;
S
Sarang Radke 已提交
73 74 75 76

	ret = 1;
	num_valid = 0;
	bcode = (uint8_t *)pri_cfg;
77 78
	bcode_val_ptr = (uint32_t *)pri_cfg;
	bcode_val = (uint32_t)(*bcode_val_ptr);
S
Sarang Radke 已提交
79

80 81
	if (bcode_val == 0xFFFFFFFF) {
		/* No FCP Priority config data in flash */
82 83
		ql_dbg(ql_dbg_user, vha, 0x7051,
		    "No FCP Priority config data.\n");
84 85 86 87 88 89
		return 0;
	}

	if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
			bcode[3] != 'S') {
		/* Invalid FCP priority data header*/
90 91 92
		ql_dbg(ql_dbg_user, vha, 0x7052,
		    "Invalid FCP Priority data header. bcode=0x%x.\n",
		    bcode_val);
S
Sarang Radke 已提交
93 94 95 96 97 98 99 100 101 102 103 104
		return 0;
	}
	if (flag != 1)
		return ret;

	pri_entry = &pri_cfg->entry[0];
	for (i = 0; i < pri_cfg->num_entries; i++) {
		if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
			num_valid++;
		pri_entry++;
	}

105 106
	if (num_valid == 0) {
		/* No valid FCP priority data entries */
107 108
		ql_dbg(ql_dbg_user, vha, 0x7053,
		    "No valid FCP Priority data entries.\n");
S
Sarang Radke 已提交
109
		ret = 0;
110 111
	} else {
		/* FCP priority data is valid */
112 113 114
		ql_dbg(ql_dbg_user, vha, 0x7054,
		    "Valid FCP priority data. num entries = %d.\n",
		    num_valid);
115
	}
S
Sarang Radke 已提交
116 117 118 119 120

	return ret;
}

static int
121
qla24xx_proc_fcp_prio_cfg_cmd(struct bsg_job *bsg_job)
S
Sarang Radke 已提交
122
{
123
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
124 125
	struct fc_bsg_request *bsg_request = bsg_job->request;
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
S
Sarang Radke 已提交
126 127 128 129 130 131
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int ret = 0;
	uint32_t len;
	uint32_t oper;

132
	if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_P3P_TYPE(ha))) {
133 134 135 136
		ret = -EINVAL;
		goto exit_fcp_prio_cfg;
	}

S
Sarang Radke 已提交
137
	/* Get the sub command */
138
	oper = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
S
Sarang Radke 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151

	/* Only set config is allowed if config memory is not allocated */
	if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
		ret = -EINVAL;
		goto exit_fcp_prio_cfg;
	}
	switch (oper) {
	case QLFC_FCP_PRIO_DISABLE:
		if (ha->flags.fcp_prio_enabled) {
			ha->flags.fcp_prio_enabled = 0;
			ha->fcp_prio_cfg->attributes &=
				~FCP_PRIO_ATTR_ENABLE;
			qla24xx_update_all_fcp_prio(vha);
152
			bsg_reply->result = DID_OK;
S
Sarang Radke 已提交
153 154
		} else {
			ret = -EINVAL;
155
			bsg_reply->result = (DID_ERROR << 16);
S
Sarang Radke 已提交
156 157 158 159 160 161 162 163 164 165 166
			goto exit_fcp_prio_cfg;
		}
		break;

	case QLFC_FCP_PRIO_ENABLE:
		if (!ha->flags.fcp_prio_enabled) {
			if (ha->fcp_prio_cfg) {
				ha->flags.fcp_prio_enabled = 1;
				ha->fcp_prio_cfg->attributes |=
				    FCP_PRIO_ATTR_ENABLE;
				qla24xx_update_all_fcp_prio(vha);
167
				bsg_reply->result = DID_OK;
S
Sarang Radke 已提交
168 169
			} else {
				ret = -EINVAL;
170
				bsg_reply->result = (DID_ERROR << 16);
S
Sarang Radke 已提交
171 172 173 174 175 176 177 178 179
				goto exit_fcp_prio_cfg;
			}
		}
		break;

	case QLFC_FCP_PRIO_GET_CONFIG:
		len = bsg_job->reply_payload.payload_len;
		if (!len || len > FCP_PRIO_CFG_SIZE) {
			ret = -EINVAL;
180
			bsg_reply->result = (DID_ERROR << 16);
S
Sarang Radke 已提交
181 182 183
			goto exit_fcp_prio_cfg;
		}

184 185
		bsg_reply->result = DID_OK;
		bsg_reply->reply_payload_rcv_len =
S
Sarang Radke 已提交
186 187 188 189 190 191 192 193 194 195
			sg_copy_from_buffer(
			bsg_job->reply_payload.sg_list,
			bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
			len);

		break;

	case QLFC_FCP_PRIO_SET_CONFIG:
		len = bsg_job->request_payload.payload_len;
		if (!len || len > FCP_PRIO_CFG_SIZE) {
196
			bsg_reply->result = (DID_ERROR << 16);
S
Sarang Radke 已提交
197 198 199 200 201 202 203
			ret = -EINVAL;
			goto exit_fcp_prio_cfg;
		}

		if (!ha->fcp_prio_cfg) {
			ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
			if (!ha->fcp_prio_cfg) {
204 205 206
				ql_log(ql_log_warn, vha, 0x7050,
				    "Unable to allocate memory for fcp prio "
				    "config data (%x).\n", FCP_PRIO_CFG_SIZE);
207
				bsg_reply->result = (DID_ERROR << 16);
S
Sarang Radke 已提交
208 209 210 211 212 213 214 215 216 217 218
				ret = -ENOMEM;
				goto exit_fcp_prio_cfg;
			}
		}

		memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
		sg_copy_to_buffer(bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
			FCP_PRIO_CFG_SIZE);

		/* validate fcp priority data */
219 220 221

		if (!qla24xx_fcp_prio_cfg_valid(vha,
		    (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
222
			bsg_reply->result = (DID_ERROR << 16);
S
Sarang Radke 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235
			ret = -EINVAL;
			/* If buffer was invalidatic int
			 * fcp_prio_cfg is of no use
			 */
			vfree(ha->fcp_prio_cfg);
			ha->fcp_prio_cfg = NULL;
			goto exit_fcp_prio_cfg;
		}

		ha->flags.fcp_prio_enabled = 0;
		if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
			ha->flags.fcp_prio_enabled = 1;
		qla24xx_update_all_fcp_prio(vha);
236
		bsg_reply->result = DID_OK;
S
Sarang Radke 已提交
237 238 239 240 241 242
		break;
	default:
		ret = -EINVAL;
		break;
	}
exit_fcp_prio_cfg:
243
	if (!ret)
J
Johannes Thumshirn 已提交
244
		bsg_job_done(bsg_job, bsg_reply->result,
245
			       bsg_reply->reply_payload_rcv_len);
S
Sarang Radke 已提交
246 247
	return ret;
}
248

249
static int
250
qla2x00_process_els(struct bsg_job *bsg_job)
251
{
252
	struct fc_bsg_request *bsg_request = bsg_job->request;
253
	struct fc_rport *rport;
254
	fc_port_t *fcport = NULL;
255 256 257 258 259 260 261 262 263
	struct Scsi_Host *host;
	scsi_qla_host_t *vha;
	struct qla_hw_data *ha;
	srb_t *sp;
	const char *type;
	int req_sg_cnt, rsp_sg_cnt;
	int rval =  (DRIVER_ERROR << 16);
	uint16_t nextlid = 0;

264
	if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
265
		rport = fc_bsg_to_rport(bsg_job);
266 267 268 269 270 271
		fcport = *(fc_port_t **) rport->dd_data;
		host = rport_to_shost(rport);
		vha = shost_priv(host);
		ha = vha->hw;
		type = "FC_BSG_RPT_ELS";
	} else {
272
		host = fc_bsg_to_shost(bsg_job);
273 274 275 276 277
		vha = shost_priv(host);
		ha = vha->hw;
		type = "FC_BSG_HST_ELS_NOLOGIN";
	}

278 279 280 281 282 283
	if (!vha->flags.online) {
		ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
		rval = -EIO;
		goto done;
	}

284 285
	/* pass through is supported only for ISP 4Gb or higher */
	if (!IS_FWI2_CAPABLE(ha)) {
286 287
		ql_dbg(ql_dbg_user, vha, 0x7001,
		    "ELS passthru not supported for ISP23xx based adapters.\n");
288 289 290 291
		rval = -EPERM;
		goto done;
	}

292 293 294
	/*  Multiple SG's are not supported for ELS requests */
	if (bsg_job->request_payload.sg_cnt > 1 ||
		bsg_job->reply_payload.sg_cnt > 1) {
295
		ql_dbg(ql_dbg_user, vha, 0x7002,
296
		    "Multiple SG's are not supported for ELS requests, "
297 298 299
		    "request_sg_cnt=%x reply_sg_cnt=%x.\n",
		    bsg_job->request_payload.sg_cnt,
		    bsg_job->reply_payload.sg_cnt);
300 301 302 303 304
		rval = -EPERM;
		goto done;
	}

	/* ELS request for rport */
305
	if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
306 307 308 309
		/* make sure the rport is logged in,
		 * if not perform fabric login
		 */
		if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
310 311 312
			ql_dbg(ql_dbg_user, vha, 0x7003,
			    "Failed to login port %06X for ELS passthru.\n",
			    fcport->d_id.b24);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
			rval = -EIO;
			goto done;
		}
	} else {
		/* Allocate a dummy fcport structure, since functions
		 * preparing the IOCB and mailbox command retrieves port
		 * specific information from fcport structure. For Host based
		 * ELS commands there will be no fcport structure allocated
		 */
		fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
		if (!fcport) {
			rval = -ENOMEM;
			goto done;
		}

		/* Initialize all required  fields of fcport */
		fcport->vha = vha;
		fcport->d_id.b.al_pa =
331
			bsg_request->rqst_data.h_els.port_id[0];
332
		fcport->d_id.b.area =
333
			bsg_request->rqst_data.h_els.port_id[1];
334
		fcport->d_id.b.domain =
335
			bsg_request->rqst_data.h_els.port_id[2];
336 337 338 339 340 341 342 343 344 345 346 347
		fcport->loop_id =
			(fcport->d_id.b.al_pa == 0xFD) ?
			NPH_FABRIC_CONTROLLER : NPH_F_PORT;
	}

	req_sg_cnt =
		dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
	if (!req_sg_cnt) {
		rval = -ENOMEM;
		goto done_free_fcport;
	}
348 349 350

	rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
		bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
351 352 353 354 355 356
        if (!rsp_sg_cnt) {
		rval = -ENOMEM;
		goto done_free_fcport;
	}

	if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
357
		(rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
358 359 360 361 362
		ql_log(ql_log_warn, vha, 0x7008,
		    "dma mapping resulted in different sg counts, "
		    "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
		    "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
		    req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
363 364 365 366 367
		rval = -EAGAIN;
		goto done_unmap_sg;
	}

	/* Alloc SRB structure */
368
	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
369 370
	if (!sp) {
		rval = -ENOMEM;
371
		goto done_unmap_sg;
372 373
	}

374
	sp->type =
375 376
		(bsg_request->msgcode == FC_BSG_RPT_ELS ?
		 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
377
	sp->name =
378 379
		(bsg_request->msgcode == FC_BSG_RPT_ELS ?
		 "bsg_els_rpt" : "bsg_els_hst");
380 381 382
	sp->u.bsg_job = bsg_job;
	sp->free = qla2x00_bsg_sp_free;
	sp->done = qla2x00_bsg_job_done;
383

384 385 386
	ql_dbg(ql_dbg_user, vha, 0x700a,
	    "bsg rqst type: %s els type: %x - loop-id=%x "
	    "portid=%-2x%02x%02x.\n", type,
387
	    bsg_request->rqst_data.h_els.command_code, fcport->loop_id,
388
	    fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
389 390 391

	rval = qla2x00_start_sp(sp);
	if (rval != QLA_SUCCESS) {
392 393
		ql_log(ql_log_warn, vha, 0x700e,
		    "qla2x00_start_sp failed = %d\n", rval);
394
		qla2x00_rel_sp(sp);
395 396 397 398 399 400 401 402 403 404 405 406 407
		rval = -EIO;
		goto done_unmap_sg;
	}
	return rval;

done_unmap_sg:
	dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
	dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
		bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
	goto done_free_fcport;

done_free_fcport:
408
	if (bsg_request->msgcode == FC_BSG_RPT_ELS)
409 410 411 412 413
		kfree(fcport);
done:
	return rval;
}

414
static inline uint16_t
415 416 417 418 419 420 421 422 423 424 425 426 427
qla24xx_calc_ct_iocbs(uint16_t dsds)
{
	uint16_t iocbs;

	iocbs = 1;
	if (dsds > 2) {
		iocbs += (dsds - 2) / 5;
		if ((dsds - 2) % 5)
			iocbs++;
	}
	return iocbs;
}

428
static int
429
qla2x00_process_ct(struct bsg_job *bsg_job)
430 431
{
	srb_t *sp;
432
	struct fc_bsg_request *bsg_request = bsg_job->request;
433
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
434 435 436 437 438 439 440 441 442 443 444
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = (DRIVER_ERROR << 16);
	int req_sg_cnt, rsp_sg_cnt;
	uint16_t loop_id;
	struct fc_port *fcport;
	char  *type = "FC_BSG_HST_CT";

	req_sg_cnt =
		dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
			bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
445
	if (!req_sg_cnt) {
446 447
		ql_log(ql_log_warn, vha, 0x700f,
		    "dma_map_sg return %d for request\n", req_sg_cnt);
448 449 450 451 452 453 454
		rval = -ENOMEM;
		goto done;
	}

	rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
		bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
	if (!rsp_sg_cnt) {
455 456
		ql_log(ql_log_warn, vha, 0x7010,
		    "dma_map_sg return %d for reply\n", rsp_sg_cnt);
457 458 459 460 461
		rval = -ENOMEM;
		goto done;
	}

	if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
462
	    (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
463 464 465 466
		ql_log(ql_log_warn, vha, 0x7011,
		    "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
		    "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
		    req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
467
		rval = -EAGAIN;
468
		goto done_unmap_sg;
469 470 471
	}

	if (!vha->flags.online) {
472 473
		ql_log(ql_log_warn, vha, 0x7012,
		    "Host is not online.\n");
474 475 476 477 478
		rval = -EIO;
		goto done_unmap_sg;
	}

	loop_id =
479
		(bsg_request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
480 481
			>> 24;
	switch (loop_id) {
482 483 484 485 486 487 488
	case 0xFC:
		loop_id = cpu_to_le16(NPH_SNS);
		break;
	case 0xFA:
		loop_id = vha->mgmt_svr_loop_id;
		break;
	default:
489 490
		ql_dbg(ql_dbg_user, vha, 0x7013,
		    "Unknown loop id: %x.\n", loop_id);
491 492
		rval = -EINVAL;
		goto done_unmap_sg;
493 494 495 496 497 498 499 500
	}

	/* Allocate a dummy fcport structure, since functions preparing the
	 * IOCB and mailbox command retrieves port specific information
	 * from fcport structure. For Host based ELS commands there will be
	 * no fcport structure allocated
	 */
	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
501
	if (!fcport) {
502 503
		ql_log(ql_log_warn, vha, 0x7014,
		    "Failed to allocate fcport.\n");
504
		rval = -ENOMEM;
505
		goto done_unmap_sg;
506 507 508 509
	}

	/* Initialize all required  fields of fcport */
	fcport->vha = vha;
510 511 512
	fcport->d_id.b.al_pa = bsg_request->rqst_data.h_ct.port_id[0];
	fcport->d_id.b.area = bsg_request->rqst_data.h_ct.port_id[1];
	fcport->d_id.b.domain = bsg_request->rqst_data.h_ct.port_id[2];
513 514 515
	fcport->loop_id = loop_id;

	/* Alloc SRB structure */
516
	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
517
	if (!sp) {
518
		ql_log(ql_log_warn, vha, 0x7015,
519
		    "qla2x00_get_sp failed.\n");
520 521 522 523
		rval = -ENOMEM;
		goto done_free_fcport;
	}

524 525 526 527 528 529
	sp->type = SRB_CT_CMD;
	sp->name = "bsg_ct";
	sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
	sp->u.bsg_job = bsg_job;
	sp->free = qla2x00_bsg_sp_free;
	sp->done = qla2x00_bsg_job_done;
530

531 532 533
	ql_dbg(ql_dbg_user, vha, 0x7016,
	    "bsg rqst type: %s else type: %x - "
	    "loop-id=%x portid=%02x%02x%02x.\n", type,
534
	    (bsg_request->rqst_data.h_ct.preamble_word2 >> 16),
535 536
	    fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
	    fcport->d_id.b.al_pa);
537 538 539

	rval = qla2x00_start_sp(sp);
	if (rval != QLA_SUCCESS) {
540 541
		ql_log(ql_log_warn, vha, 0x7017,
		    "qla2x00_start_sp failed=%d.\n", rval);
542
		qla2x00_rel_sp(sp);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
		rval = -EIO;
		goto done_free_fcport;
	}
	return rval;

done_free_fcport:
	kfree(fcport);
done_unmap_sg:
	dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
	dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
		bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
done:
	return rval;
}
558 559 560 561

/* Disable loopback mode */
static inline int
qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
562
			    int wait, int wait2)
563 564 565 566 567 568
{
	int ret = 0;
	int rval = 0;
	uint16_t new_config[4];
	struct qla_hw_data *ha = vha->hw;

569
	if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
570 571 572 573 574 575 576 577 578 579 580 581 582
		goto done_reset_internal;

	memset(new_config, 0 , sizeof(new_config));
	if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
	    ENABLE_INTERNAL_LOOPBACK ||
	    (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
	    ENABLE_EXTERNAL_LOOPBACK) {
		new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
		ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
		    (new_config[0] & INTERNAL_LOOPBACK_MASK));
		memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;

		ha->notify_dcbx_comp = wait;
583 584
		ha->notify_lb_portup_comp = wait2;

585 586 587 588 589
		ret = qla81xx_set_port_config(vha, new_config);
		if (ret != QLA_SUCCESS) {
			ql_log(ql_log_warn, vha, 0x7025,
			    "Set port config failed.\n");
			ha->notify_dcbx_comp = 0;
590
			ha->notify_lb_portup_comp = 0;
591 592 593 594 595 596
			rval = -EINVAL;
			goto done_reset_internal;
		}

		/* Wait for DCBX complete event */
		if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
597
			(DCBX_COMP_TIMEOUT * HZ))) {
598
			ql_dbg(ql_dbg_user, vha, 0x7026,
599
			    "DCBX completion not received.\n");
600
			ha->notify_dcbx_comp = 0;
601
			ha->notify_lb_portup_comp = 0;
602 603 604 605
			rval = -EINVAL;
			goto done_reset_internal;
		} else
			ql_dbg(ql_dbg_user, vha, 0x7027,
606 607 608 609 610 611 612 613 614 615 616 617 618
			    "DCBX completion received.\n");

		if (wait2 &&
		    !wait_for_completion_timeout(&ha->lb_portup_comp,
		    (LB_PORTUP_COMP_TIMEOUT * HZ))) {
			ql_dbg(ql_dbg_user, vha, 0x70c5,
			    "Port up completion not received.\n");
			ha->notify_lb_portup_comp = 0;
			rval = -EINVAL;
			goto done_reset_internal;
		} else
			ql_dbg(ql_dbg_user, vha, 0x70c6,
			    "Port up completion received.\n");
619 620

		ha->notify_dcbx_comp = 0;
621
		ha->notify_lb_portup_comp = 0;
622 623 624 625 626
	}
done_reset_internal:
	return rval;
}

627 628 629
/*
 * Set the port configuration to enable the internal or external loopback
 * depending on the loopback mode.
630 631
 */
static inline int
632 633
qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
	uint16_t *new_config, uint16_t mode)
634 635 636
{
	int ret = 0;
	int rval = 0;
637
	unsigned long rem_tmo = 0, current_tmo = 0;
638 639
	struct qla_hw_data *ha = vha->hw;

640
	if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
641 642
		goto done_set_internal;

643 644 645 646 647 648 649 650
	if (mode == INTERNAL_LOOPBACK)
		new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
	else if (mode == EXTERNAL_LOOPBACK)
		new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
	ql_dbg(ql_dbg_user, vha, 0x70be,
	     "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));

	memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
651 652 653 654

	ha->notify_dcbx_comp = 1;
	ret = qla81xx_set_port_config(vha, new_config);
	if (ret != QLA_SUCCESS) {
655 656
		ql_log(ql_log_warn, vha, 0x7021,
		    "set port config failed.\n");
657 658 659 660 661 662
		ha->notify_dcbx_comp = 0;
		rval = -EINVAL;
		goto done_set_internal;
	}

	/* Wait for DCBX complete event */
663 664 665 666 667 668 669 670 671 672 673 674 675
	current_tmo = DCBX_COMP_TIMEOUT * HZ;
	while (1) {
		rem_tmo = wait_for_completion_timeout(&ha->dcbx_comp,
		    current_tmo);
		if (!ha->idc_extend_tmo || rem_tmo) {
			ha->idc_extend_tmo = 0;
			break;
		}
		current_tmo = ha->idc_extend_tmo * HZ;
		ha->idc_extend_tmo = 0;
	}

	if (!rem_tmo) {
676
		ql_dbg(ql_dbg_user, vha, 0x7022,
677 678
		    "DCBX completion not received.\n");
		ret = qla81xx_reset_loopback_mode(vha, new_config, 0, 0);
679 680 681 682 683 684 685 686
		/*
		 * If the reset of the loopback mode doesn't work take a FCoE
		 * dump and reset the chip.
		 */
		if (ret) {
			ha->isp_ops->fw_dump(vha, 0);
			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
		}
687 688 689 690 691 692 693 694 695
		rval = -EINVAL;
	} else {
		if (ha->flags.idc_compl_status) {
			ql_dbg(ql_dbg_user, vha, 0x70c3,
			    "Bad status in IDC Completion AEN\n");
			rval = -EINVAL;
			ha->flags.idc_compl_status = 0;
		} else
			ql_dbg(ql_dbg_user, vha, 0x7023,
696
			    "DCBX completion received.\n");
697
	}
698 699

	ha->notify_dcbx_comp = 0;
700
	ha->idc_extend_tmo = 0;
701 702 703 704 705

done_set_internal:
	return rval;
}

706
static int
707
qla2x00_process_loopback(struct bsg_job *bsg_job)
708
{
709 710
	struct fc_bsg_request *bsg_request = bsg_job->request;
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
711
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
712 713 714 715 716 717 718
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval;
	uint8_t command_sent;
	char *type;
	struct msg_echo_lb elreq;
	uint16_t response[MAILBOX_REGISTER_COUNT];
719
	uint16_t config[4], new_config[4];
720
	uint8_t *fw_sts_ptr;
721 722 723 724 725 726 727 728
	uint8_t *req_data = NULL;
	dma_addr_t req_data_dma;
	uint32_t req_data_len;
	uint8_t *rsp_data = NULL;
	dma_addr_t rsp_data_dma;
	uint32_t rsp_data_len;

	if (!vha->flags.online) {
729
		ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
730 731 732
		return -EIO;
	}

733 734
	memset(&elreq, 0, sizeof(elreq));

735 736 737 738
	elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
		bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
		DMA_TO_DEVICE);

739 740 741
	if (!elreq.req_sg_cnt) {
		ql_log(ql_log_warn, vha, 0x701a,
		    "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
742
		return -ENOMEM;
743
	}
744 745 746 747 748 749

	elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
		bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
		DMA_FROM_DEVICE);

	if (!elreq.rsp_sg_cnt) {
750 751
		ql_log(ql_log_warn, vha, 0x701b,
		    "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
752 753
		rval = -ENOMEM;
		goto done_unmap_req_sg;
754
	}
755 756 757

	if ((elreq.req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
		(elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
758 759 760 761 762 763
		ql_log(ql_log_warn, vha, 0x701c,
		    "dma mapping resulted in different sg counts, "
		    "request_sg_cnt: %x dma_request_sg_cnt: %x "
		    "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
		    bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
		    bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
764 765 766 767 768 769 770
		rval = -EAGAIN;
		goto done_unmap_sg;
	}
	req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
	req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
		&req_data_dma, GFP_KERNEL);
	if (!req_data) {
771 772
		ql_log(ql_log_warn, vha, 0x701d,
		    "dma alloc failed for req_data.\n");
773 774 775 776 777 778 779
		rval = -ENOMEM;
		goto done_unmap_sg;
	}

	rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
		&rsp_data_dma, GFP_KERNEL);
	if (!rsp_data) {
780 781
		ql_log(ql_log_warn, vha, 0x7004,
		    "dma alloc failed for rsp_data.\n");
782 783 784 785 786 787 788 789 790 791 792 793
		rval = -ENOMEM;
		goto done_free_dma_req;
	}

	/* Copy the request buffer in req_data now */
	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, req_data, req_data_len);

	elreq.send_dma = req_data_dma;
	elreq.rcv_dma = rsp_data_dma;
	elreq.transfer_size = req_data_len;

794
	elreq.options = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
795
	elreq.iteration_count =
796
	    bsg_request->rqst_data.h_vendor.vendor_cmd[2];
797

798 799
	if (atomic_read(&vha->loop_state) == LOOP_READY &&
	    (ha->current_topology == ISP_CFG_F ||
800 801 802
	    (le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE &&
	     req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
	    elreq.options == EXTERNAL_LOOPBACK) {
803
		type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
804 805
		ql_dbg(ql_dbg_user, vha, 0x701e,
		    "BSG request type: %s.\n", type);
806 807 808
		command_sent = INT_DEF_LB_ECHO_CMD;
		rval = qla2x00_echo_test(vha, &elreq, response);
	} else {
809
		if (IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) {
810 811
			memset(config, 0, sizeof(config));
			memset(new_config, 0, sizeof(new_config));
812

813
			if (qla81xx_get_port_config(vha, config)) {
814 815
				ql_log(ql_log_warn, vha, 0x701f,
				    "Get port config failed.\n");
816
				rval = -EPERM;
817
				goto done_free_dma_rsp;
818 819
			}

820 821 822 823 824 825 826 827
			if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
				ql_dbg(ql_dbg_user, vha, 0x70c4,
				    "Loopback operation already in "
				    "progress.\n");
				rval = -EAGAIN;
				goto done_free_dma_rsp;
			}

828 829 830 831
			ql_dbg(ql_dbg_user, vha, 0x70c0,
			    "elreq.options=%04x\n", elreq.options);

			if (elreq.options == EXTERNAL_LOOPBACK)
832
				if (IS_QLA8031(ha) || IS_QLA8044(ha))
833 834 835 836
					rval = qla81xx_set_loopback_mode(vha,
					    config, new_config, elreq.options);
				else
					rval = qla81xx_reset_loopback_mode(vha,
837
					    config, 1, 0);
838 839 840 841 842 843
			else
				rval = qla81xx_set_loopback_mode(vha, config,
				    new_config, elreq.options);

			if (rval) {
				rval = -EPERM;
844
				goto done_free_dma_rsp;
845 846 847
			}

			type = "FC_BSG_HST_VENDOR_LOOPBACK";
848 849
			ql_dbg(ql_dbg_user, vha, 0x7028,
			    "BSG request type: %s.\n", type);
850 851 852 853

			command_sent = INT_DEF_LB_LOOPBACK_CMD;
			rval = qla2x00_loopback_test(vha, &elreq, response);

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
			if (response[0] == MBS_COMMAND_ERROR &&
					response[1] == MBS_LB_RESET) {
				ql_log(ql_log_warn, vha, 0x7029,
				    "MBX command error, Aborting ISP.\n");
				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
				qla2xxx_wake_dpc(vha);
				qla2x00_wait_for_chip_reset(vha);
				/* Also reset the MPI */
				if (IS_QLA81XX(ha)) {
					if (qla81xx_restart_mpi_firmware(vha) !=
					    QLA_SUCCESS) {
						ql_log(ql_log_warn, vha, 0x702a,
						    "MPI reset failed.\n");
					}
				}

				rval = -EIO;
				goto done_free_dma_rsp;
			}

874
			if (new_config[0]) {
875 876
				int ret;

877 878 879
				/* Revert back to original port config
				 * Also clear internal loopback
				 */
880
				ret = qla81xx_reset_loopback_mode(vha,
881
				    new_config, 0, 1);
882 883 884 885 886 887 888 889 890 891 892
				if (ret) {
					/*
					 * If the reset of the loopback mode
					 * doesn't work take FCoE dump and then
					 * reset the chip.
					 */
					ha->isp_ops->fw_dump(vha, 0);
					set_bit(ISP_ABORT_NEEDED,
					    &vha->dpc_flags);
				}

893 894 895 896
			}

		} else {
			type = "FC_BSG_HST_VENDOR_LOOPBACK";
897 898
			ql_dbg(ql_dbg_user, vha, 0x702b,
			    "BSG request type: %s.\n", type);
899 900
			command_sent = INT_DEF_LB_LOOPBACK_CMD;
			rval = qla2x00_loopback_test(vha, &elreq, response);
901 902 903 904
		}
	}

	if (rval) {
905 906
		ql_log(ql_log_warn, vha, 0x702c,
		    "Vendor request %s failed.\n", type);
907 908

		rval = 0;
909 910
		bsg_reply->result = (DID_ERROR << 16);
		bsg_reply->reply_payload_rcv_len = 0;
911
	} else {
912 913
		ql_dbg(ql_dbg_user, vha, 0x702d,
		    "Vendor request %s completed.\n", type);
914
		bsg_reply->result = (DID_OK << 16);
915 916 917 918
		sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
			bsg_job->reply_payload.sg_cnt, rsp_data,
			rsp_data_len);
	}
919 920 921

	bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
	    sizeof(response) + sizeof(uint8_t);
922
	fw_sts_ptr = ((uint8_t *)scsi_req(bsg_job->req)->sense) +
923 924 925 926
	    sizeof(struct fc_bsg_reply);
	memcpy(fw_sts_ptr, response, sizeof(response));
	fw_sts_ptr += sizeof(response);
	*fw_sts_ptr = command_sent;
927

928
done_free_dma_rsp:
929 930 931 932 933 934 935 936 937 938 939 940 941
	dma_free_coherent(&ha->pdev->dev, rsp_data_len,
		rsp_data, rsp_data_dma);
done_free_dma_req:
	dma_free_coherent(&ha->pdev->dev, req_data_len,
		req_data, req_data_dma);
done_unmap_sg:
	dma_unmap_sg(&ha->pdev->dev,
	    bsg_job->reply_payload.sg_list,
	    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
done_unmap_req_sg:
	dma_unmap_sg(&ha->pdev->dev,
	    bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
942
	if (!rval)
J
Johannes Thumshirn 已提交
943
		bsg_job_done(bsg_job, bsg_reply->result,
944
			       bsg_reply->reply_payload_rcv_len);
945
	return rval;
946 947 948
}

static int
949
qla84xx_reset(struct bsg_job *bsg_job)
950
{
951
	struct fc_bsg_request *bsg_request = bsg_job->request;
952
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
953
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
954 955 956 957 958 959
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;
	uint32_t flag;

	if (!IS_QLA84XX(ha)) {
960
		ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
961 962 963
		return -EINVAL;
	}

964
	flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
965 966 967 968

	rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);

	if (rval) {
969 970
		ql_log(ql_log_warn, vha, 0x7030,
		    "Vendor request 84xx reset failed.\n");
971
		rval = (DID_ERROR << 16);
972 973

	} else {
974 975
		ql_dbg(ql_dbg_user, vha, 0x7031,
		    "Vendor request 84xx reset completed.\n");
976
		bsg_reply->result = DID_OK;
J
Johannes Thumshirn 已提交
977
		bsg_job_done(bsg_job, bsg_reply->result,
978
			       bsg_reply->reply_payload_rcv_len);
979 980 981 982 983 984
	}

	return rval;
}

static int
985
qla84xx_updatefw(struct bsg_job *bsg_job)
986
{
987 988
	struct fc_bsg_request *bsg_request = bsg_job->request;
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
989
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	struct verify_chip_entry_84xx *mn = NULL;
	dma_addr_t mn_dma, fw_dma;
	void *fw_buf = NULL;
	int rval = 0;
	uint32_t sg_cnt;
	uint32_t data_len;
	uint16_t options;
	uint32_t flag;
	uint32_t fw_ver;

	if (!IS_QLA84XX(ha)) {
1003 1004
		ql_dbg(ql_dbg_user, vha, 0x7032,
		    "Not 84xx, exiting.\n");
1005 1006 1007 1008 1009
		return -EINVAL;
	}

	sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1010 1011 1012
	if (!sg_cnt) {
		ql_log(ql_log_warn, vha, 0x7033,
		    "dma_map_sg returned %d for request.\n", sg_cnt);
1013
		return -ENOMEM;
1014
	}
1015 1016

	if (sg_cnt != bsg_job->request_payload.sg_cnt) {
1017 1018 1019 1020
		ql_log(ql_log_warn, vha, 0x7034,
		    "DMA mapping resulted in different sg counts, "
		    "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
		    bsg_job->request_payload.sg_cnt, sg_cnt);
1021 1022 1023 1024 1025 1026 1027 1028
		rval = -EAGAIN;
		goto done_unmap_sg;
	}

	data_len = bsg_job->request_payload.payload_len;
	fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
		&fw_dma, GFP_KERNEL);
	if (!fw_buf) {
1029 1030
		ql_log(ql_log_warn, vha, 0x7035,
		    "DMA alloc failed for fw_buf.\n");
1031 1032 1033 1034 1035 1036 1037 1038 1039
		rval = -ENOMEM;
		goto done_unmap_sg;
	}

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, fw_buf, data_len);

	mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
	if (!mn) {
1040 1041
		ql_log(ql_log_warn, vha, 0x7036,
		    "DMA alloc failed for fw buffer.\n");
1042 1043 1044 1045
		rval = -ENOMEM;
		goto done_free_fw_buf;
	}

1046
	flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));

	memset(mn, 0, sizeof(struct access_chip_84xx));
	mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
	mn->entry_count = 1;

	options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
	if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
		options |= VCO_DIAG_FW;

	mn->options = cpu_to_le16(options);
	mn->fw_ver =  cpu_to_le32(fw_ver);
	mn->fw_size =  cpu_to_le32(data_len);
	mn->fw_seq_size =  cpu_to_le32(data_len);
	mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
	mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
	mn->dseg_length = cpu_to_le32(data_len);
	mn->data_seg_cnt = cpu_to_le16(1);

	rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);

	if (rval) {
1069 1070
		ql_log(ql_log_warn, vha, 0x7037,
		    "Vendor request 84xx updatefw failed.\n");
1071

1072
		rval = (DID_ERROR << 16);
1073
	} else {
1074 1075
		ql_dbg(ql_dbg_user, vha, 0x7038,
		    "Vendor request 84xx updatefw completed.\n");
1076 1077

		bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1078
		bsg_reply->result = DID_OK;
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	}

	dma_pool_free(ha->s_dma_pool, mn, mn_dma);

done_free_fw_buf:
	dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);

done_unmap_sg:
	dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);

1090
	if (!rval)
J
Johannes Thumshirn 已提交
1091
		bsg_job_done(bsg_job, bsg_reply->result,
1092
			       bsg_reply->reply_payload_rcv_len);
1093 1094 1095 1096
	return rval;
}

static int
1097
qla84xx_mgmt_cmd(struct bsg_job *bsg_job)
1098
{
1099 1100
	struct fc_bsg_request *bsg_request = bsg_job->request;
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1101
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1102 1103 1104 1105 1106 1107 1108 1109
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	struct access_chip_84xx *mn = NULL;
	dma_addr_t mn_dma, mgmt_dma;
	void *mgmt_b = NULL;
	int rval = 0;
	struct qla_bsg_a84_mgmt *ql84_mgmt;
	uint32_t sg_cnt;
1110
	uint32_t data_len = 0;
1111 1112 1113
	uint32_t dma_direction = DMA_NONE;

	if (!IS_QLA84XX(ha)) {
1114 1115
		ql_log(ql_log_warn, vha, 0x703a,
		    "Not 84xx, exiting.\n");
1116 1117 1118
		return -EINVAL;
	}

1119
	mn = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1120
	if (!mn) {
1121 1122
		ql_log(ql_log_warn, vha, 0x703c,
		    "DMA alloc failed for fw buffer.\n");
1123 1124 1125 1126 1127
		return -ENOMEM;
	}

	mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
	mn->entry_count = 1;
1128
	ql84_mgmt = (void *)bsg_request + sizeof(struct fc_bsg_request);
1129 1130 1131 1132 1133 1134 1135
	switch (ql84_mgmt->mgmt.cmd) {
	case QLA84_MGMT_READ_MEM:
	case QLA84_MGMT_GET_INFO:
		sg_cnt = dma_map_sg(&ha->pdev->dev,
			bsg_job->reply_payload.sg_list,
			bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
		if (!sg_cnt) {
1136 1137
			ql_log(ql_log_warn, vha, 0x703d,
			    "dma_map_sg returned %d for reply.\n", sg_cnt);
1138 1139 1140 1141 1142 1143 1144
			rval = -ENOMEM;
			goto exit_mgmt;
		}

		dma_direction = DMA_FROM_DEVICE;

		if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
1145 1146 1147 1148
			ql_log(ql_log_warn, vha, 0x703e,
			    "DMA mapping resulted in different sg counts, "
			    "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
			    bsg_job->reply_payload.sg_cnt, sg_cnt);
1149 1150 1151 1152 1153 1154 1155 1156 1157
			rval = -EAGAIN;
			goto done_unmap_sg;
		}

		data_len = bsg_job->reply_payload.payload_len;

		mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
		    &mgmt_dma, GFP_KERNEL);
		if (!mgmt_b) {
1158 1159
			ql_log(ql_log_warn, vha, 0x703f,
			    "DMA alloc failed for mgmt_b.\n");
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
			rval = -ENOMEM;
			goto done_unmap_sg;
		}

		if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
			mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
			mn->parameter1 =
				cpu_to_le32(
				ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);

		} else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
			mn->options = cpu_to_le16(ACO_REQUEST_INFO);
			mn->parameter1 =
				cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);

			mn->parameter2 =
				cpu_to_le32(
				ql84_mgmt->mgmt.mgmtp.u.info.context);
		}
		break;

	case QLA84_MGMT_WRITE_MEM:
		sg_cnt = dma_map_sg(&ha->pdev->dev,
			bsg_job->request_payload.sg_list,
			bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);

		if (!sg_cnt) {
1187 1188
			ql_log(ql_log_warn, vha, 0x7040,
			    "dma_map_sg returned %d.\n", sg_cnt);
1189 1190 1191 1192 1193 1194 1195
			rval = -ENOMEM;
			goto exit_mgmt;
		}

		dma_direction = DMA_TO_DEVICE;

		if (sg_cnt != bsg_job->request_payload.sg_cnt) {
1196 1197 1198 1199
			ql_log(ql_log_warn, vha, 0x7041,
			    "DMA mapping resulted in different sg counts, "
			    "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
			    bsg_job->request_payload.sg_cnt, sg_cnt);
1200 1201 1202 1203 1204 1205 1206 1207
			rval = -EAGAIN;
			goto done_unmap_sg;
		}

		data_len = bsg_job->request_payload.payload_len;
		mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
			&mgmt_dma, GFP_KERNEL);
		if (!mgmt_b) {
1208 1209
			ql_log(ql_log_warn, vha, 0x7042,
			    "DMA alloc failed for mgmt_b.\n");
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
			rval = -ENOMEM;
			goto done_unmap_sg;
		}

		sg_copy_to_buffer(bsg_job->request_payload.sg_list,
			bsg_job->request_payload.sg_cnt, mgmt_b, data_len);

		mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
		mn->parameter1 =
			cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
		break;

	case QLA84_MGMT_CHNG_CONFIG:
		mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
		mn->parameter1 =
			cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);

		mn->parameter2 =
			cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);

		mn->parameter3 =
			cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
		break;

	default:
		rval = -EIO;
		goto exit_mgmt;
	}

	if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
		mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
		mn->dseg_count = cpu_to_le16(1);
		mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
		mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
		mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
	}

	rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);

	if (rval) {
1250 1251
		ql_log(ql_log_warn, vha, 0x7043,
		    "Vendor request 84xx mgmt failed.\n");
1252

1253
		rval = (DID_ERROR << 16);
1254 1255

	} else {
1256 1257
		ql_dbg(ql_dbg_user, vha, 0x7044,
		    "Vendor request 84xx mgmt completed.\n");
1258 1259

		bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1260
		bsg_reply->result = DID_OK;
1261 1262 1263

		if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
			(ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1264
			bsg_reply->reply_payload_rcv_len =
1265 1266 1267
				bsg_job->reply_payload.payload_len;

			sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1268 1269
				bsg_job->reply_payload.sg_cnt, mgmt_b,
				data_len);
1270 1271 1272 1273
		}
	}

done_unmap_sg:
1274 1275 1276
	if (mgmt_b)
		dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	if (dma_direction == DMA_TO_DEVICE)
		dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
			bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
	else if (dma_direction == DMA_FROM_DEVICE)
		dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
			bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);

exit_mgmt:
	dma_pool_free(ha->s_dma_pool, mn, mn_dma);

1287
	if (!rval)
J
Johannes Thumshirn 已提交
1288
		bsg_job_done(bsg_job, bsg_reply->result,
1289
			       bsg_reply->reply_payload_rcv_len);
1290 1291 1292 1293
	return rval;
}

static int
1294
qla24xx_iidma(struct bsg_job *bsg_job)
1295
{
1296 1297
	struct fc_bsg_request *bsg_request = bsg_job->request;
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1298
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1299 1300 1301 1302
	scsi_qla_host_t *vha = shost_priv(host);
	int rval = 0;
	struct qla_port_param *port_param = NULL;
	fc_port_t *fcport = NULL;
1303
	int found = 0;
1304 1305 1306 1307
	uint16_t mb[MAILBOX_REGISTER_COUNT];
	uint8_t *rsp_ptr = NULL;

	if (!IS_IIDMA_CAPABLE(vha->hw)) {
1308
		ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
1309 1310 1311
		return -EINVAL;
	}

1312
	port_param = (void *)bsg_request + sizeof(struct fc_bsg_request);
1313
	if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
1314 1315
		ql_log(ql_log_warn, vha, 0x7048,
		    "Invalid destination type.\n");
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
		return -EINVAL;
	}

	list_for_each_entry(fcport, &vha->vp_fcports, list) {
		if (fcport->port_type != FCT_TARGET)
			continue;

		if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
			fcport->port_name, sizeof(fcport->port_name)))
			continue;
1326 1327

		found = 1;
1328 1329 1330
		break;
	}

1331
	if (!found) {
1332 1333
		ql_log(ql_log_warn, vha, 0x7049,
		    "Failed to find port.\n");
1334 1335 1336
		return -EINVAL;
	}

1337
	if (atomic_read(&fcport->state) != FCS_ONLINE) {
1338 1339
		ql_log(ql_log_warn, vha, 0x704a,
		    "Port is not online.\n");
1340 1341 1342
		return -EINVAL;
	}

1343
	if (fcport->flags & FCF_LOGIN_NEEDED) {
1344 1345
		ql_log(ql_log_warn, vha, 0x704b,
		    "Remote port not logged in flags = 0x%x.\n", fcport->flags);
1346 1347 1348
		return -EINVAL;
	}

1349 1350 1351 1352 1353 1354 1355 1356
	if (port_param->mode)
		rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
			port_param->speed, mb);
	else
		rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
			&port_param->speed, mb);

	if (rval) {
1357
		ql_log(ql_log_warn, vha, 0x704c,
1358 1359 1360
		    "iIDMA cmd failed for %8phN -- "
		    "%04x %x %04x %04x.\n", fcport->port_name,
		    rval, fcport->fp_speed, mb[0], mb[1]);
1361
		rval = (DID_ERROR << 16);
1362 1363 1364 1365 1366
	} else {
		if (!port_param->mode) {
			bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
				sizeof(struct qla_port_param);

1367
			rsp_ptr = ((uint8_t *)bsg_reply) +
1368 1369 1370 1371 1372 1373
				sizeof(struct fc_bsg_reply);

			memcpy(rsp_ptr, port_param,
				sizeof(struct qla_port_param));
		}

1374
		bsg_reply->result = DID_OK;
J
Johannes Thumshirn 已提交
1375
		bsg_job_done(bsg_job, bsg_reply->result,
1376
			       bsg_reply->reply_payload_rcv_len);
1377 1378 1379 1380 1381
	}

	return rval;
}

1382
static int
1383
qla2x00_optrom_setup(struct bsg_job *bsg_job, scsi_qla_host_t *vha,
1384 1385
	uint8_t is_update)
{
1386
	struct fc_bsg_request *bsg_request = bsg_job->request;
1387 1388
	uint32_t start = 0;
	int valid = 0;
1389
	struct qla_hw_data *ha = vha->hw;
1390 1391 1392 1393

	if (unlikely(pci_channel_offline(ha->pdev)))
		return -EINVAL;

1394
	start = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
1395 1396 1397
	if (start > ha->optrom_size) {
		ql_log(ql_log_warn, vha, 0x7055,
		    "start %d > optrom_size %d.\n", start, ha->optrom_size);
1398
		return -EINVAL;
1399
	}
1400

1401 1402 1403
	if (ha->optrom_state != QLA_SWAITING) {
		ql_log(ql_log_info, vha, 0x7056,
		    "optrom_state %d.\n", ha->optrom_state);
1404
		return -EBUSY;
1405
	}
1406 1407

	ha->optrom_region_start = start;
1408
	ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
1409 1410 1411 1412 1413 1414 1415
	if (is_update) {
		if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
			valid = 1;
		else if (start == (ha->flt_region_boot * 4) ||
		    start == (ha->flt_region_fw * 4))
			valid = 1;
		else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
1416
		    IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha))
1417 1418
			valid = 1;
		if (!valid) {
1419 1420 1421
			ql_log(ql_log_warn, vha, 0x7058,
			    "Invalid start region 0x%x/0x%x.\n", start,
			    bsg_job->request_payload.payload_len);
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
			return -EINVAL;
		}

		ha->optrom_region_size = start +
		    bsg_job->request_payload.payload_len > ha->optrom_size ?
		    ha->optrom_size - start :
		    bsg_job->request_payload.payload_len;
		ha->optrom_state = QLA_SWRITING;
	} else {
		ha->optrom_region_size = start +
		    bsg_job->reply_payload.payload_len > ha->optrom_size ?
		    ha->optrom_size - start :
		    bsg_job->reply_payload.payload_len;
		ha->optrom_state = QLA_SREADING;
	}

	ha->optrom_buffer = vmalloc(ha->optrom_region_size);
	if (!ha->optrom_buffer) {
1440
		ql_log(ql_log_warn, vha, 0x7059,
1441
		    "Read: Unable to allocate memory for optrom retrieval "
1442
		    "(%x)\n", ha->optrom_region_size);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

		ha->optrom_state = QLA_SWAITING;
		return -ENOMEM;
	}

	memset(ha->optrom_buffer, 0, ha->optrom_region_size);
	return 0;
}

static int
1453
qla2x00_read_optrom(struct bsg_job *bsg_job)
1454
{
1455
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1456
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1457 1458 1459 1460
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;

1461
	if (ha->flags.nic_core_reset_hdlr_active)
1462 1463
		return -EBUSY;

1464
	mutex_lock(&ha->optrom_mutex);
1465
	rval = qla2x00_optrom_setup(bsg_job, vha, 0);
1466 1467
	if (rval) {
		mutex_unlock(&ha->optrom_mutex);
1468
		return rval;
1469
	}
1470 1471 1472 1473 1474 1475 1476 1477

	ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
	    ha->optrom_region_start, ha->optrom_region_size);

	sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
	    bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
	    ha->optrom_region_size);

1478 1479
	bsg_reply->reply_payload_rcv_len = ha->optrom_region_size;
	bsg_reply->result = DID_OK;
1480 1481 1482
	vfree(ha->optrom_buffer);
	ha->optrom_buffer = NULL;
	ha->optrom_state = QLA_SWAITING;
1483
	mutex_unlock(&ha->optrom_mutex);
J
Johannes Thumshirn 已提交
1484
	bsg_job_done(bsg_job, bsg_reply->result,
1485
		       bsg_reply->reply_payload_rcv_len);
1486 1487 1488 1489
	return rval;
}

static int
1490
qla2x00_update_optrom(struct bsg_job *bsg_job)
1491
{
1492
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1493
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1494 1495 1496 1497
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;

1498
	mutex_lock(&ha->optrom_mutex);
1499
	rval = qla2x00_optrom_setup(bsg_job, vha, 1);
1500 1501
	if (rval) {
		mutex_unlock(&ha->optrom_mutex);
1502
		return rval;
1503
	}
1504

1505 1506 1507
	/* Set the isp82xx_no_md_cap not to capture minidump */
	ha->flags.isp82xx_no_md_cap = 1;

1508 1509 1510 1511 1512 1513 1514
	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
	    ha->optrom_region_size);

	ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
	    ha->optrom_region_start, ha->optrom_region_size);

1515
	bsg_reply->result = DID_OK;
1516 1517 1518
	vfree(ha->optrom_buffer);
	ha->optrom_buffer = NULL;
	ha->optrom_state = QLA_SWAITING;
1519
	mutex_unlock(&ha->optrom_mutex);
J
Johannes Thumshirn 已提交
1520
	bsg_job_done(bsg_job, bsg_reply->result,
1521
		       bsg_reply->reply_payload_rcv_len);
1522 1523 1524
	return rval;
}

1525
static int
1526
qla2x00_update_fru_versions(struct bsg_job *bsg_job)
1527
{
1528
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1529
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;
	uint8_t bsg[DMA_POOL_SIZE];
	struct qla_image_version_list *list = (void *)bsg;
	struct qla_image_version *image;
	uint32_t count;
	dma_addr_t sfp_dma;
	void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
	if (!sfp) {
1540
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
		    EXT_STATUS_NO_MEMORY;
		goto done;
	}

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, list, sizeof(bsg));

	image = list->version;
	count = list->count;
	while (count--) {
		memcpy(sfp, &image->field_info, sizeof(image->field_info));
		rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
		    image->field_address.device, image->field_address.offset,
		    sizeof(image->field_info), image->field_address.option);
		if (rval) {
1556
			bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1557 1558 1559 1560 1561 1562
			    EXT_STATUS_MAILBOX;
			goto dealloc;
		}
		image++;
	}

1563
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1564 1565 1566 1567 1568 1569

dealloc:
	dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);

done:
	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1570
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
1571
	bsg_job_done(bsg_job, bsg_reply->result,
1572
		       bsg_reply->reply_payload_rcv_len);
1573 1574 1575 1576 1577

	return 0;
}

static int
1578
qla2x00_read_fru_status(struct bsg_job *bsg_job)
1579
{
1580
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1581
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1582 1583 1584 1585 1586 1587 1588 1589
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;
	uint8_t bsg[DMA_POOL_SIZE];
	struct qla_status_reg *sr = (void *)bsg;
	dma_addr_t sfp_dma;
	uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
	if (!sfp) {
1590
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
		    EXT_STATUS_NO_MEMORY;
		goto done;
	}

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));

	rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
	    sr->field_address.device, sr->field_address.offset,
	    sizeof(sr->status_reg), sr->field_address.option);
	sr->status_reg = *sfp;

	if (rval) {
1604
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1605 1606 1607 1608 1609 1610 1611
		    EXT_STATUS_MAILBOX;
		goto dealloc;
	}

	sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
	    bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));

1612
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1613 1614 1615 1616 1617 1618

dealloc:
	dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);

done:
	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1619 1620
	bsg_reply->reply_payload_rcv_len = sizeof(*sr);
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
1621
	bsg_job_done(bsg_job, bsg_reply->result,
1622
		       bsg_reply->reply_payload_rcv_len);
1623 1624 1625 1626 1627

	return 0;
}

static int
1628
qla2x00_write_fru_status(struct bsg_job *bsg_job)
1629
{
1630
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1631
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1632 1633 1634 1635 1636 1637 1638 1639
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;
	uint8_t bsg[DMA_POOL_SIZE];
	struct qla_status_reg *sr = (void *)bsg;
	dma_addr_t sfp_dma;
	uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
	if (!sfp) {
1640
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
		    EXT_STATUS_NO_MEMORY;
		goto done;
	}

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));

	*sfp = sr->status_reg;
	rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
	    sr->field_address.device, sr->field_address.offset,
	    sizeof(sr->status_reg), sr->field_address.option);

	if (rval) {
1654
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1655 1656 1657 1658
		    EXT_STATUS_MAILBOX;
		goto dealloc;
	}

1659
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1660 1661 1662 1663 1664 1665

dealloc:
	dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);

done:
	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1666
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
1667
	bsg_job_done(bsg_job, bsg_reply->result,
1668
		       bsg_reply->reply_payload_rcv_len);
1669 1670 1671 1672

	return 0;
}

1673
static int
1674
qla2x00_write_i2c(struct bsg_job *bsg_job)
1675
{
1676
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1677
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1678 1679 1680 1681 1682 1683 1684 1685
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;
	uint8_t bsg[DMA_POOL_SIZE];
	struct qla_i2c_access *i2c = (void *)bsg;
	dma_addr_t sfp_dma;
	uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
	if (!sfp) {
1686
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
		    EXT_STATUS_NO_MEMORY;
		goto done;
	}

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));

	memcpy(sfp, i2c->buffer, i2c->length);
	rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
	    i2c->device, i2c->offset, i2c->length, i2c->option);

	if (rval) {
1699
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1700 1701 1702 1703
		    EXT_STATUS_MAILBOX;
		goto dealloc;
	}

1704
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1705 1706 1707 1708 1709 1710

dealloc:
	dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);

done:
	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1711
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
1712
	bsg_job_done(bsg_job, bsg_reply->result,
1713
		       bsg_reply->reply_payload_rcv_len);
1714 1715 1716 1717 1718

	return 0;
}

static int
1719
qla2x00_read_i2c(struct bsg_job *bsg_job)
1720
{
1721
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1722
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1723 1724 1725 1726 1727 1728 1729 1730
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = 0;
	uint8_t bsg[DMA_POOL_SIZE];
	struct qla_i2c_access *i2c = (void *)bsg;
	dma_addr_t sfp_dma;
	uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
	if (!sfp) {
1731
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
		    EXT_STATUS_NO_MEMORY;
		goto done;
	}

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));

	rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
		i2c->device, i2c->offset, i2c->length, i2c->option);

	if (rval) {
1743
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
1744 1745 1746 1747 1748 1749 1750 1751
		    EXT_STATUS_MAILBOX;
		goto dealloc;
	}

	memcpy(i2c->buffer, sfp, i2c->length);
	sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
	    bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));

1752
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1753 1754 1755 1756 1757 1758

dealloc:
	dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);

done:
	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1759 1760
	bsg_reply->reply_payload_rcv_len = sizeof(*i2c);
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
1761
	bsg_job_done(bsg_job, bsg_reply->result,
1762
		       bsg_reply->reply_payload_rcv_len);
1763 1764 1765 1766

	return 0;
}

1767
static int
1768
qla24xx_process_bidir_cmd(struct bsg_job *bsg_job)
1769
{
1770
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1771
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	uint32_t rval = EXT_STATUS_OK;
	uint16_t req_sg_cnt = 0;
	uint16_t rsp_sg_cnt = 0;
	uint16_t nextlid = 0;
	uint32_t tot_dsds;
	srb_t *sp = NULL;
	uint32_t req_data_len = 0;
	uint32_t rsp_data_len = 0;

	/* Check the type of the adapter */
	if (!IS_BIDI_CAPABLE(ha)) {
		ql_log(ql_log_warn, vha, 0x70a0,
			"This adapter is not supported\n");
		rval = EXT_STATUS_NOT_SUPPORTED;
		goto done;
	}

	if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
		test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
		test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
		rval =  EXT_STATUS_BUSY;
		goto done;
	}

	/* Check if host is online */
	if (!vha->flags.online) {
		ql_log(ql_log_warn, vha, 0x70a1,
			"Host is not online\n");
		rval = EXT_STATUS_DEVICE_OFFLINE;
		goto done;
	}

	/* Check if cable is plugged in or not */
	if (vha->device_flags & DFLG_NO_CABLE) {
		ql_log(ql_log_warn, vha, 0x70a2,
			"Cable is unplugged...\n");
		rval = EXT_STATUS_INVALID_CFG;
		goto done;
	}

	/* Check if the switch is connected or not */
	if (ha->current_topology != ISP_CFG_F) {
		ql_log(ql_log_warn, vha, 0x70a3,
			"Host is not connected to the switch\n");
		rval = EXT_STATUS_INVALID_CFG;
		goto done;
	}

	/* Check if operating mode is P2P */
	if (ha->operating_mode != P2P) {
		ql_log(ql_log_warn, vha, 0x70a4,
1825
		    "Host operating mode is not P2p\n");
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
		rval = EXT_STATUS_INVALID_CFG;
		goto done;
	}

	mutex_lock(&ha->selflogin_lock);
	if (vha->self_login_loop_id == 0) {
		/* Initialize all required  fields of fcport */
		vha->bidir_fcport.vha = vha;
		vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
		vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
		vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
		vha->bidir_fcport.loop_id = vha->loop_id;

		if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
			ql_log(ql_log_warn, vha, 0x70a7,
			    "Failed to login port %06X for bidirectional IOCB\n",
			    vha->bidir_fcport.d_id.b24);
			mutex_unlock(&ha->selflogin_lock);
			rval = EXT_STATUS_MAILBOX;
			goto done;
		}
		vha->self_login_loop_id = nextlid - 1;

	}
	/* Assign the self login loop id to fcport */
	mutex_unlock(&ha->selflogin_lock);

	vha->bidir_fcport.loop_id = vha->self_login_loop_id;

	req_sg_cnt = dma_map_sg(&ha->pdev->dev,
		bsg_job->request_payload.sg_list,
		bsg_job->request_payload.sg_cnt,
		DMA_TO_DEVICE);

	if (!req_sg_cnt) {
		rval = EXT_STATUS_NO_MEMORY;
		goto done;
	}

	rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
		bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
		DMA_FROM_DEVICE);

	if (!rsp_sg_cnt) {
		rval = EXT_STATUS_NO_MEMORY;
		goto done_unmap_req_sg;
	}

	if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
		(rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
		ql_dbg(ql_dbg_user, vha, 0x70a9,
		    "Dma mapping resulted in different sg counts "
		    "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
		    "%x dma_reply_sg_cnt: %x]\n",
		    bsg_job->request_payload.sg_cnt, req_sg_cnt,
		    bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
		rval = EXT_STATUS_NO_MEMORY;
		goto done_unmap_sg;
	}

	if (req_data_len != rsp_data_len) {
		rval = EXT_STATUS_BUSY;
		ql_log(ql_log_warn, vha, 0x70aa,
		    "req_data_len != rsp_data_len\n");
		goto done_unmap_sg;
	}

	req_data_len = bsg_job->request_payload.payload_len;
	rsp_data_len = bsg_job->reply_payload.payload_len;


	/* Alloc SRB structure */
	sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
	if (!sp) {
		ql_dbg(ql_dbg_user, vha, 0x70ac,
		    "Alloc SRB structure failed\n");
		rval = EXT_STATUS_NO_MEMORY;
		goto done_unmap_sg;
	}

	/*Populate srb->ctx with bidir ctx*/
	sp->u.bsg_job = bsg_job;
	sp->free = qla2x00_bsg_sp_free;
	sp->type = SRB_BIDI_CMD;
	sp->done = qla2x00_bsg_job_done;

	/* Add the read and write sg count */
	tot_dsds = rsp_sg_cnt + req_sg_cnt;

	rval = qla2x00_start_bidir(sp, vha, tot_dsds);
	if (rval != EXT_STATUS_OK)
		goto done_free_srb;
	/* the bsg request  will be completed in the interrupt handler */
	return rval;

done_free_srb:
	mempool_free(sp, ha->srb_mempool);
done_unmap_sg:
	dma_unmap_sg(&ha->pdev->dev,
	    bsg_job->reply_payload.sg_list,
	    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
done_unmap_req_sg:
	dma_unmap_sg(&ha->pdev->dev,
	    bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
done:

	/* Return an error vendor specific response
	 * and complete the bsg request
	 */
1936
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1937
	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1938 1939
	bsg_reply->reply_payload_rcv_len = 0;
	bsg_reply->result = (DID_OK) << 16;
J
Johannes Thumshirn 已提交
1940
	bsg_job_done(bsg_job, bsg_reply->result,
1941
		       bsg_reply->reply_payload_rcv_len);
1942
	/* Always return success, vendor rsp carries correct status */
1943 1944 1945
	return 0;
}

1946
static int
1947
qlafx00_mgmt_cmd(struct bsg_job *bsg_job)
1948
{
1949
	struct fc_bsg_request *bsg_request = bsg_job->request;
1950
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	int rval = (DRIVER_ERROR << 16);
	struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
	srb_t *sp;
	int req_sg_cnt = 0, rsp_sg_cnt = 0;
	struct fc_port *fcport;
	char  *type = "FC_BSG_HST_FX_MGMT";

	/* Copy the IOCB specific information */
	piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
1962
	    &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068

	/* Dump the vendor information */
	ql_dump_buffer(ql_dbg_user + ql_dbg_verbose , vha, 0x70cf,
	    (uint8_t *)piocb_rqst, sizeof(struct qla_mt_iocb_rqst_fx00));

	if (!vha->flags.online) {
		ql_log(ql_log_warn, vha, 0x70d0,
		    "Host is not online.\n");
		rval = -EIO;
		goto done;
	}

	if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID) {
		req_sg_cnt = dma_map_sg(&ha->pdev->dev,
		    bsg_job->request_payload.sg_list,
		    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
		if (!req_sg_cnt) {
			ql_log(ql_log_warn, vha, 0x70c7,
			    "dma_map_sg return %d for request\n", req_sg_cnt);
			rval = -ENOMEM;
			goto done;
		}
	}

	if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID) {
		rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
		    bsg_job->reply_payload.sg_list,
		    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
		if (!rsp_sg_cnt) {
			ql_log(ql_log_warn, vha, 0x70c8,
			    "dma_map_sg return %d for reply\n", rsp_sg_cnt);
			rval = -ENOMEM;
			goto done_unmap_req_sg;
		}
	}

	ql_dbg(ql_dbg_user, vha, 0x70c9,
	    "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
	    "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
	    req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);

	/* Allocate a dummy fcport structure, since functions preparing the
	 * IOCB and mailbox command retrieves port specific information
	 * from fcport structure. For Host based ELS commands there will be
	 * no fcport structure allocated
	 */
	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
	if (!fcport) {
		ql_log(ql_log_warn, vha, 0x70ca,
		    "Failed to allocate fcport.\n");
		rval = -ENOMEM;
		goto done_unmap_rsp_sg;
	}

	/* Alloc SRB structure */
	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
	if (!sp) {
		ql_log(ql_log_warn, vha, 0x70cb,
		    "qla2x00_get_sp failed.\n");
		rval = -ENOMEM;
		goto done_free_fcport;
	}

	/* Initialize all required  fields of fcport */
	fcport->vha = vha;
	fcport->loop_id = piocb_rqst->dataword;

	sp->type = SRB_FXIOCB_BCMD;
	sp->name = "bsg_fx_mgmt";
	sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
	sp->u.bsg_job = bsg_job;
	sp->free = qla2x00_bsg_sp_free;
	sp->done = qla2x00_bsg_job_done;

	ql_dbg(ql_dbg_user, vha, 0x70cc,
	    "bsg rqst type: %s fx_mgmt_type: %x id=%x\n",
	    type, piocb_rqst->func_type, fcport->loop_id);

	rval = qla2x00_start_sp(sp);
	if (rval != QLA_SUCCESS) {
		ql_log(ql_log_warn, vha, 0x70cd,
		    "qla2x00_start_sp failed=%d.\n", rval);
		mempool_free(sp, ha->srb_mempool);
		rval = -EIO;
		goto done_free_fcport;
	}
	return rval;

done_free_fcport:
	kfree(fcport);

done_unmap_rsp_sg:
	if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
		dma_unmap_sg(&ha->pdev->dev,
		    bsg_job->reply_payload.sg_list,
		    bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
done_unmap_req_sg:
	if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
		dma_unmap_sg(&ha->pdev->dev,
		    bsg_job->request_payload.sg_list,
		    bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);

done:
	return rval;
}

2069
static int
2070
qla26xx_serdes_op(struct bsg_job *bsg_job)
2071
{
2072
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2073
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
	scsi_qla_host_t *vha = shost_priv(host);
	int rval = 0;
	struct qla_serdes_reg sr;

	memset(&sr, 0, sizeof(sr));

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));

	switch (sr.cmd) {
	case INT_SC_SERDES_WRITE_REG:
		rval = qla2x00_write_serdes_word(vha, sr.addr, sr.val);
2086
		bsg_reply->reply_payload_rcv_len = 0;
2087 2088 2089 2090 2091
		break;
	case INT_SC_SERDES_READ_REG:
		rval = qla2x00_read_serdes_word(vha, sr.addr, &sr.val);
		sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
		    bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
2092
		bsg_reply->reply_payload_rcv_len = sizeof(sr);
2093 2094
		break;
	default:
2095
		ql_dbg(ql_dbg_user, vha, 0x708c,
2096
		    "Unknown serdes cmd %x.\n", sr.cmd);
2097 2098 2099 2100
		rval = -EINVAL;
		break;
	}

2101
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2102 2103 2104
	    rval ? EXT_STATUS_MAILBOX : 0;

	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2105
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
2106
	bsg_job_done(bsg_job, bsg_reply->result,
2107
		       bsg_reply->reply_payload_rcv_len);
2108 2109 2110 2111
	return 0;
}

static int
2112
qla8044_serdes_op(struct bsg_job *bsg_job)
2113
{
2114
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2115
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	scsi_qla_host_t *vha = shost_priv(host);
	int rval = 0;
	struct qla_serdes_reg_ex sr;

	memset(&sr, 0, sizeof(sr));

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));

	switch (sr.cmd) {
	case INT_SC_SERDES_WRITE_REG:
		rval = qla8044_write_serdes_word(vha, sr.addr, sr.val);
2128
		bsg_reply->reply_payload_rcv_len = 0;
2129 2130 2131 2132 2133
		break;
	case INT_SC_SERDES_READ_REG:
		rval = qla8044_read_serdes_word(vha, sr.addr, &sr.val);
		sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
		    bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
2134
		bsg_reply->reply_payload_rcv_len = sizeof(sr);
2135 2136
		break;
	default:
2137
		ql_dbg(ql_dbg_user, vha, 0x7020,
2138 2139
		    "Unknown serdes cmd %x.\n", sr.cmd);
		rval = -EINVAL;
2140 2141 2142
		break;
	}

2143
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2144 2145 2146
	    rval ? EXT_STATUS_MAILBOX : 0;

	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2147
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
2148
	bsg_job_done(bsg_job, bsg_reply->result,
2149
		       bsg_reply->reply_payload_rcv_len);
2150 2151 2152
	return 0;
}

2153
static int
2154
qla27xx_get_flash_upd_cap(struct bsg_job *bsg_job)
2155
{
2156
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2157
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	struct qla_flash_update_caps cap;

	if (!(IS_QLA27XX(ha)))
		return -EPERM;

	memset(&cap, 0, sizeof(cap));
	cap.capabilities = (uint64_t)ha->fw_attributes_ext[1] << 48 |
			   (uint64_t)ha->fw_attributes_ext[0] << 32 |
			   (uint64_t)ha->fw_attributes_h << 16 |
			   (uint64_t)ha->fw_attributes;

	sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
	    bsg_job->reply_payload.sg_cnt, &cap, sizeof(cap));
2173
	bsg_reply->reply_payload_rcv_len = sizeof(cap);
2174

2175
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2176 2177 2178
	    EXT_STATUS_OK;

	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2179
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
2180
	bsg_job_done(bsg_job, bsg_reply->result,
2181
		       bsg_reply->reply_payload_rcv_len);
2182 2183 2184 2185
	return 0;
}

static int
2186
qla27xx_set_flash_upd_cap(struct bsg_job *bsg_job)
2187
{
2188
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2189
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	uint64_t online_fw_attr = 0;
	struct qla_flash_update_caps cap;

	if (!(IS_QLA27XX(ha)))
		return -EPERM;

	memset(&cap, 0, sizeof(cap));
	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, &cap, sizeof(cap));

	online_fw_attr = (uint64_t)ha->fw_attributes_ext[1] << 48 |
			 (uint64_t)ha->fw_attributes_ext[0] << 32 |
			 (uint64_t)ha->fw_attributes_h << 16 |
			 (uint64_t)ha->fw_attributes;

	if (online_fw_attr != cap.capabilities) {
2208
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2209 2210 2211 2212 2213
		    EXT_STATUS_INVALID_PARAM;
		return -EINVAL;
	}

	if (cap.outage_duration < MAX_LOOP_TIMEOUT)  {
2214
		bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2215 2216 2217 2218
		    EXT_STATUS_INVALID_PARAM;
		return -EINVAL;
	}

2219
	bsg_reply->reply_payload_rcv_len = 0;
2220

2221
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2222 2223 2224
	    EXT_STATUS_OK;

	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2225
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
2226
	bsg_job_done(bsg_job, bsg_reply->result,
2227
		       bsg_reply->reply_payload_rcv_len);
2228 2229 2230
	return 0;
}

2231
static int
2232
qla27xx_get_bbcr_data(struct bsg_job *bsg_job)
2233
{
2234
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2235
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	struct qla_bbcr_data bbcr;
	uint16_t loop_id, topo, sw_cap;
	uint8_t domain, area, al_pa, state;
	int rval;

	if (!(IS_QLA27XX(ha)))
		return -EPERM;

	memset(&bbcr, 0, sizeof(bbcr));

	if (vha->flags.bbcr_enable)
		bbcr.status = QLA_BBCR_STATUS_ENABLED;
	else
		bbcr.status = QLA_BBCR_STATUS_DISABLED;

	if (bbcr.status == QLA_BBCR_STATUS_ENABLED) {
		rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
			&area, &domain, &topo, &sw_cap);
2256 2257 2258 2259 2260 2261
		if (rval != QLA_SUCCESS) {
			bbcr.status = QLA_BBCR_STATUS_UNKNOWN;
			bbcr.state = QLA_BBCR_STATE_OFFLINE;
			bbcr.mbx1 = loop_id;
			goto done;
		}
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275

		state = (vha->bbcr >> 12) & 0x1;

		if (state) {
			bbcr.state = QLA_BBCR_STATE_OFFLINE;
			bbcr.offline_reason_code = QLA_BBCR_REASON_LOGIN_REJECT;
		} else {
			bbcr.state = QLA_BBCR_STATE_ONLINE;
			bbcr.negotiated_bbscn = (vha->bbcr >> 8) & 0xf;
		}

		bbcr.configured_bbscn = vha->bbcr & 0xf;
	}

2276
done:
2277 2278
	sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
		bsg_job->reply_payload.sg_cnt, &bbcr, sizeof(bbcr));
2279
	bsg_reply->reply_payload_rcv_len = sizeof(bbcr);
2280

2281
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = EXT_STATUS_OK;
2282 2283

	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2284
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
2285
	bsg_job_done(bsg_job, bsg_reply->result,
2286
		       bsg_reply->reply_payload_rcv_len);
2287 2288 2289
	return 0;
}

2290
static int
2291
qla2x00_get_priv_stats(struct bsg_job *bsg_job)
2292
{
2293 2294
	struct fc_bsg_request *bsg_request = bsg_job->request;
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2295
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
2296 2297 2298 2299 2300
	scsi_qla_host_t *vha = shost_priv(host);
	struct qla_hw_data *ha = vha->hw;
	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
	struct link_statistics *stats = NULL;
	dma_addr_t stats_dma;
2301
	int rval;
2302
	uint32_t *cmd = bsg_request->rqst_data.h_vendor.vendor_cmd;
2303
	uint options = cmd[0] == QL_VND_GET_PRIV_STATS_EX ? cmd[1] : 0;
2304 2305

	if (test_bit(UNLOADING, &vha->dpc_flags))
2306
		return -ENODEV;
2307 2308

	if (unlikely(pci_channel_offline(ha->pdev)))
2309
		return -ENODEV;
2310 2311

	if (qla2x00_reset_active(vha))
2312
		return -EBUSY;
2313 2314

	if (!IS_FWI2_CAPABLE(ha))
2315
		return -EPERM;
2316 2317

	stats = dma_alloc_coherent(&ha->pdev->dev,
2318
		sizeof(*stats), &stats_dma, GFP_KERNEL);
2319 2320
	if (!stats) {
		ql_log(ql_log_warn, vha, 0x70e2,
2321 2322
		    "Failed to allocate memory for stats.\n");
		return -ENOMEM;
2323 2324
	}

2325
	memset(stats, 0, sizeof(*stats));
2326

2327
	rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, options);
2328

2329 2330 2331 2332 2333 2334
	if (rval == QLA_SUCCESS) {
		ql_dump_buffer(ql_dbg_user + ql_dbg_verbose, vha, 0x70e3,
		    (uint8_t *)stats, sizeof(*stats));
		sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
			bsg_job->reply_payload.sg_cnt, stats, sizeof(*stats));
	}
2335

2336 2337
	bsg_reply->reply_payload_rcv_len = sizeof(*stats);
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2338
	    rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
2339

2340 2341
	bsg_job->reply_len = sizeof(*bsg_reply);
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
2342
	bsg_job_done(bsg_job, bsg_reply->result,
2343
		       bsg_reply->reply_payload_rcv_len);
2344

2345
	dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
2346
		stats, stats_dma);
2347 2348

	return 0;
2349 2350
}

2351
static int
2352
qla2x00_do_dport_diagnostics(struct bsg_job *bsg_job)
2353
{
2354
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2355
	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
	scsi_qla_host_t *vha = shost_priv(host);
	int rval;
	struct qla_dport_diag *dd;

	if (!IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw))
		return -EPERM;

	dd = kmalloc(sizeof(*dd), GFP_KERNEL);
	if (!dd) {
		ql_log(ql_log_warn, vha, 0x70db,
		    "Failed to allocate memory for dport.\n");
		return -ENOMEM;
	}

	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
	    bsg_job->request_payload.sg_cnt, dd, sizeof(*dd));

	rval = qla26xx_dport_diagnostics(
	    vha, dd->buf, sizeof(dd->buf), dd->options);
	if (rval == QLA_SUCCESS) {
		sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
		    bsg_job->reply_payload.sg_cnt, dd, sizeof(*dd));
	}

2380 2381
	bsg_reply->reply_payload_rcv_len = sizeof(*dd);
	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
2382 2383
	    rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;

2384 2385
	bsg_job->reply_len = sizeof(*bsg_reply);
	bsg_reply->result = DID_OK << 16;
J
Johannes Thumshirn 已提交
2386
	bsg_job_done(bsg_job, bsg_reply->result,
2387
		       bsg_reply->reply_payload_rcv_len);
2388 2389 2390 2391 2392 2393

	kfree(dd);

	return 0;
}

2394
static int
2395
qla2x00_process_vendor_specific(struct bsg_job *bsg_job)
2396
{
2397 2398 2399
	struct fc_bsg_request *bsg_request = bsg_job->request;

	switch (bsg_request->rqst_data.h_vendor.vendor_cmd[0]) {
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
	case QL_VND_LOOPBACK:
		return qla2x00_process_loopback(bsg_job);

	case QL_VND_A84_RESET:
		return qla84xx_reset(bsg_job);

	case QL_VND_A84_UPDATE_FW:
		return qla84xx_updatefw(bsg_job);

	case QL_VND_A84_MGMT_CMD:
		return qla84xx_mgmt_cmd(bsg_job);

	case QL_VND_IIDMA:
		return qla24xx_iidma(bsg_job);

S
Sarang Radke 已提交
2415 2416 2417
	case QL_VND_FCP_PRIO_CFG_CMD:
		return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);

2418 2419 2420 2421 2422 2423
	case QL_VND_READ_FLASH:
		return qla2x00_read_optrom(bsg_job);

	case QL_VND_UPDATE_FLASH:
		return qla2x00_update_optrom(bsg_job);

2424 2425 2426 2427 2428 2429 2430 2431 2432
	case QL_VND_SET_FRU_VERSION:
		return qla2x00_update_fru_versions(bsg_job);

	case QL_VND_READ_FRU_STATUS:
		return qla2x00_read_fru_status(bsg_job);

	case QL_VND_WRITE_FRU_STATUS:
		return qla2x00_write_fru_status(bsg_job);

2433 2434 2435 2436 2437 2438
	case QL_VND_WRITE_I2C:
		return qla2x00_write_i2c(bsg_job);

	case QL_VND_READ_I2C:
		return qla2x00_read_i2c(bsg_job);

2439 2440 2441
	case QL_VND_DIAG_IO_CMD:
		return qla24xx_process_bidir_cmd(bsg_job);

2442 2443
	case QL_VND_FX00_MGMT_CMD:
		return qlafx00_mgmt_cmd(bsg_job);
2444 2445 2446 2447

	case QL_VND_SERDES_OP:
		return qla26xx_serdes_op(bsg_job);

2448 2449 2450
	case QL_VND_SERDES_OP_EX:
		return qla8044_serdes_op(bsg_job);

2451 2452 2453 2454 2455 2456
	case QL_VND_GET_FLASH_UPDATE_CAPS:
		return qla27xx_get_flash_upd_cap(bsg_job);

	case QL_VND_SET_FLASH_UPDATE_CAPS:
		return qla27xx_set_flash_upd_cap(bsg_job);

2457 2458 2459
	case QL_VND_GET_BBCR_DATA:
		return qla27xx_get_bbcr_data(bsg_job);

2460
	case QL_VND_GET_PRIV_STATS:
2461
	case QL_VND_GET_PRIV_STATS_EX:
2462 2463
		return qla2x00_get_priv_stats(bsg_job);

2464 2465 2466
	case QL_VND_DPORT_DIAGNOSTICS:
		return qla2x00_do_dport_diagnostics(bsg_job);

2467 2468 2469 2470 2471 2472
	default:
		return -ENOSYS;
	}
}

int
2473
qla24xx_bsg_request(struct bsg_job *bsg_job)
2474
{
2475 2476
	struct fc_bsg_request *bsg_request = bsg_job->request;
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2477
	int ret = -EINVAL;
2478 2479 2480 2481
	struct fc_rport *rport;
	struct Scsi_Host *host;
	scsi_qla_host_t *vha;

2482
	/* In case no data transferred. */
2483
	bsg_reply->reply_payload_rcv_len = 0;
2484

2485
	if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
2486
		rport = fc_bsg_to_rport(bsg_job);
2487 2488 2489
		host = rport_to_shost(rport);
		vha = shost_priv(host);
	} else {
2490
		host = fc_bsg_to_shost(bsg_job);
2491 2492 2493
		vha = shost_priv(host);
	}

2494 2495 2496
	if (qla2x00_reset_active(vha)) {
		ql_dbg(ql_dbg_user, vha, 0x709f,
		    "BSG: ISP abort active/needed -- cmd=%d.\n",
2497
		    bsg_request->msgcode);
2498 2499 2500
		return -EBUSY;
	}

2501
	ql_dbg(ql_dbg_user, vha, 0x7000,
2502
	    "Entered %s msgcode=0x%x.\n", __func__, bsg_request->msgcode);
2503

2504
	switch (bsg_request->msgcode) {
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
	case FC_BSG_RPT_ELS:
	case FC_BSG_HST_ELS_NOLOGIN:
		ret = qla2x00_process_els(bsg_job);
		break;
	case FC_BSG_HST_CT:
		ret = qla2x00_process_ct(bsg_job);
		break;
	case FC_BSG_HST_VENDOR:
		ret = qla2x00_process_vendor_specific(bsg_job);
		break;
	case FC_BSG_HST_ADD_RPORT:
	case FC_BSG_HST_DEL_RPORT:
	case FC_BSG_RPT_CT:
	default:
2519
		ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
2520
		break;
2521
	}
2522 2523 2524 2525
	return ret;
}

int
2526
qla24xx_bsg_timeout(struct bsg_job *bsg_job)
2527
{
2528
	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
2529
	scsi_qla_host_t *vha = shost_priv(fc_bsg_to_shost(bsg_job));
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
	struct qla_hw_data *ha = vha->hw;
	srb_t *sp;
	int cnt, que;
	unsigned long flags;
	struct req_que *req;

	/* find the bsg job from the active list of commands */
	spin_lock_irqsave(&ha->hardware_lock, flags);
	for (que = 0; que < ha->max_req_queues; que++) {
		req = ha->req_q_map[que];
		if (!req)
			continue;

2543
		for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
2544 2545
			sp = req->outstanding_cmds[cnt];
			if (sp) {
2546
				if (((sp->type == SRB_CT_CMD) ||
2547 2548
					(sp->type == SRB_ELS_CMD_HST) ||
					(sp->type == SRB_FXIOCB_BCMD))
2549
					&& (sp->u.bsg_job == bsg_job)) {
2550
					req->outstanding_cmds[cnt] = NULL;
2551
					spin_unlock_irqrestore(&ha->hardware_lock, flags);
2552
					if (ha->isp_ops->abort_command(sp)) {
2553 2554 2555
						ql_log(ql_log_warn, vha, 0x7089,
						    "mbx abort_command "
						    "failed.\n");
2556
						scsi_req(bsg_job->req)->result =
2557
						bsg_reply->result = -EIO;
2558
					} else {
2559 2560 2561
						ql_dbg(ql_dbg_user, vha, 0x708a,
						    "mbx abort_command "
						    "success.\n");
2562
						scsi_req(bsg_job->req)->result =
2563
						bsg_reply->result = 0;
2564
					}
2565
					spin_lock_irqsave(&ha->hardware_lock, flags);
2566 2567 2568 2569 2570 2571
					goto done;
				}
			}
		}
	}
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2572
	ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
2573
	scsi_req(bsg_job->req)->result = bsg_reply->result = -ENXIO;
2574 2575 2576 2577
	return 0;

done:
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2578
	sp->free(sp);
2579 2580
	return 0;
}