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

#include <linux/moduleparam.h>
#include <linux/vmalloc.h>
12
#include <linux/slab.h>
13 14 15 16 17 18 19 20 21
#include <linux/list.h>

#include <scsi/scsi_tcq.h>
#include <scsi/scsicam.h>
#include <linux/delay.h>

void
qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
{
22
	if (vha->vp_idx && vha->timer_active) {
23 24 25 26 27
		del_timer_sync(&vha->timer);
		vha->timer_active = 0;
	}
}

A
Adrian Bunk 已提交
28
static uint32_t
29 30 31
qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
{
	uint32_t vp_id;
32
	struct qla_hw_data *ha = vha->hw;
33 34

	/* Find an empty slot and assign an vp_id */
35
	mutex_lock(&ha->vport_lock);
36 37 38 39
	vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
	if (vp_id > ha->max_npiv_vports) {
		DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
		    vp_id, ha->max_npiv_vports));
40
		mutex_unlock(&ha->vport_lock);
41 42 43
		return vp_id;
	}

44
	set_bit(vp_id, ha->vp_idx_map);
45 46
	ha->num_vhosts++;
	vha->vp_idx = vp_id;
47
	list_add_tail(&vha->list, &ha->vp_list);
48
	mutex_unlock(&ha->vport_lock);
49 50 51 52 53 54 55
	return vp_id;
}

void
qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
{
	uint16_t vp_id;
56
	struct qla_hw_data *ha = vha->hw;
57

58
	mutex_lock(&ha->vport_lock);
59 60
	vp_id = vha->vp_idx;
	ha->num_vhosts--;
61
	clear_bit(vp_id, ha->vp_idx_map);
62
	list_del(&vha->list);
63
	mutex_unlock(&ha->vport_lock);
64 65
}

A
Adrian Bunk 已提交
66
static scsi_qla_host_t *
67
qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
68 69
{
	scsi_qla_host_t *vha;
70
	struct scsi_qla_host *tvha;
71 72

	/* Locate matching device in database. */
73
	list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		if (!memcmp(port_name, vha->port_name, WWN_SIZE))
			return vha;
	}
	return NULL;
}

/*
 * qla2x00_mark_vp_devices_dead
 *	Updates fcport state when device goes offline.
 *
 * Input:
 *	ha = adapter block pointer.
 *	fcport = port structure pointer.
 *
 * Return:
 *	None.
 *
 * Context:
 */
93
static void
94 95 96 97
qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
{
	fc_port_t *fcport;

98
	list_for_each_entry(fcport, &vha->vp_fcports, list) {
99 100 101 102
		DEBUG15(printk("scsi(%ld): Marking port dead, "
		    "loop_id=0x%04x :%x\n",
		    vha->host_no, fcport->loop_id, fcport->vp_idx));

103
		atomic_set(&fcport->state, FCS_DEVICE_DEAD);
104
		qla2x00_mark_device_lost(vha, fcport, 0, 0);
105
		atomic_set(&fcport->state, FCS_UNCONFIGURED);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	}
}

int
qla24xx_disable_vp(scsi_qla_host_t *vha)
{
	int ret;

	ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
	atomic_set(&vha->loop_state, LOOP_DOWN);
	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);

	qla2x00_mark_vp_devices_dead(vha);
	atomic_set(&vha->vp_state, VP_FAILED);
	vha->flags.management_server_logged_in = 0;
	if (ret == QLA_SUCCESS) {
		fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
	} else {
		fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
		return -1;
	}
	return 0;
}

int
qla24xx_enable_vp(scsi_qla_host_t *vha)
{
	int ret;
134 135
	struct qla_hw_data *ha = vha->hw;
	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
136 137

	/* Check if physical ha port is Up */
138 139
	if (atomic_read(&base_vha->loop_state) == LOOP_DOWN  ||
		atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
140 141 142 143 144 145
		vha->vp_err_state =  VP_ERR_PORTDWN;
		fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
		goto enable_failed;
	}

	/* Initialize the new vport unless it is a persistent port */
146
	mutex_lock(&ha->vport_lock);
147
	ret = qla24xx_modify_vp_config(vha);
148
	mutex_unlock(&ha->vport_lock);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

	if (ret != QLA_SUCCESS) {
		fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
		goto enable_failed;
	}

	DEBUG15(qla_printk(KERN_INFO, ha,
	    "Virtual port with id: %d - Enabled\n", vha->vp_idx));
	return 0;

enable_failed:
	DEBUG15(qla_printk(KERN_INFO, ha,
	    "Virtual port with id: %d - Disabled\n", vha->vp_idx));
	return 1;
}

165
static void
166 167 168 169 170 171 172 173 174 175 176
qla24xx_configure_vp(scsi_qla_host_t *vha)
{
	struct fc_vport *fc_vport;
	int ret;

	fc_vport = vha->fc_vport;

	DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
	    vha->host_no, __func__));
	ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
	if (ret != QLA_SUCCESS) {
177 178
		DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
		    "receiving of RSCN requests: 0x%x\n", ret));
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		return;
	} else {
		/* Corresponds to SCR enabled */
		clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
	}

	vha->flags.online = 1;
	if (qla24xx_configure_vhba(vha))
		return;

	atomic_set(&vha->vp_state, VP_ACTIVE);
	fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
}

void
194
qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
195
{
196
	scsi_qla_host_t *vha, *tvha;
197
	struct qla_hw_data *ha = rsp->hw;
198
	int i = 0;
199

200
	list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
201
		if (vha->vp_idx) {
202 203 204 205 206 207 208 209 210 211
			switch (mb[0]) {
			case MBA_LIP_OCCURRED:
			case MBA_LOOP_UP:
			case MBA_LOOP_DOWN:
			case MBA_LIP_RESET:
			case MBA_POINT_TO_POINT:
			case MBA_CHG_IN_CONNECTION:
			case MBA_PORT_UPDATE:
			case MBA_RSCN_UPDATE:
				DEBUG15(printk("scsi(%ld)%s: Async_event for"
212 213
				" VP[%d], mb = 0x%x, vha=%p\n",
				vha->host_no, __func__, i, *mb, vha));
214
				qla2x00_async_event(vha, rsp, mb);
215 216 217
				break;
			}
		}
218
		i++;
219 220 221
	}
}

222
int
223 224 225 226 227 228 229 230 231 232 233 234 235 236
qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
{
	/*
	 * Physical port will do most of the abort and recovery work. We can
	 * just treat it as a loop down
	 */
	if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
		atomic_set(&vha->loop_state, LOOP_DOWN);
		qla2x00_mark_all_devices_lost(vha, 0);
	} else {
		if (!atomic_read(&vha->loop_down_timer))
			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
	}

237 238 239 240 241
	/*
	 * To exclusively reset vport, we need to log it out first.  Note: this
	 * control_vp can fail if ISP reset is already issued, this is
	 * expected, as the vp would be already logged out due to ISP reset.
	 */
242 243 244
	if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
		qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);

245 246
	DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
	    vha->host_no, vha->vp_idx));
247
	return qla24xx_enable_vp(vha);
248 249
}

A
Adrian Bunk 已提交
250
static int
251 252
qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
{
253 254
	qla2x00_do_work(vha);

255 256
	if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
		/* VP acquired. complete port configuration */
257
		qla24xx_configure_vp(vha);
258 259 260
		return 0;
	}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
		qla2x00_update_fcports(vha);
		clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
	}

	if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
		!test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
		atomic_read(&vha->loop_state) != LOOP_DOWN) {

		DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
						vha->host_no));
		qla2x00_relogin(vha);

		DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
							vha->host_no));
	}
277 278 279 280 281 282

	if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
	    (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
		clear_bit(RESET_ACTIVE, &vha->dpc_flags);
	}

283
	if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
284 285 286 287 288 289 290 291 292 293
		if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
			qla2x00_loop_resync(vha);
			clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
		}
	}

	return 0;
}

void
294
qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
295 296
{
	int ret;
297 298
	struct qla_hw_data *ha = vha->hw;
	scsi_qla_host_t *vp;
299
	struct scsi_qla_host *tvp;
300

301
	if (vha->vp_idx)
302 303 304 305
		return;
	if (list_empty(&ha->vp_list))
		return;

306
	clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
307

308 309 310
	if (!(ha->current_topology & ISP_CFG_F))
		return;

311
	list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
312 313
		if (vp->vp_idx)
			ret = qla2x00_do_dpc_vp(vp);
314 315 316 317 318 319
	}
}

int
qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
{
320 321
	scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
	struct qla_hw_data *ha = base_vha->hw;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	scsi_qla_host_t *vha;
	uint8_t port_name[WWN_SIZE];

	if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
		return VPCERR_UNSUPPORTED;

	/* Check up the F/W and H/W support NPIV */
	if (!ha->flags.npiv_supported)
		return VPCERR_UNSUPPORTED;

	/* Check up whether npiv supported switch presented */
	if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
		return VPCERR_NO_FABRIC_SUPP;

	/* Check up unique WWPN */
	u64_to_wwn(fc_vport->port_name, port_name);
338
	if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
339
		return VPCERR_BAD_WWN;
340 341 342 343 344 345
	vha = qla24xx_find_vhost_by_name(ha, port_name);
	if (vha)
		return VPCERR_BAD_WWN;

	/* Check up max-npiv-supports */
	if (ha->num_vhosts > ha->max_npiv_vports) {
346
		DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
347
		    "max_npv_vports %ud.\n", base_vha->host_no,
348
		    ha->num_vhosts, ha->max_npiv_vports));
349 350 351 352 353 354 355 356
		return VPCERR_UNSUPPORTED;
	}
	return 0;
}

scsi_qla_host_t *
qla24xx_create_vhost(struct fc_vport *fc_vport)
{
357 358
	scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
	struct qla_hw_data *ha = base_vha->hw;
359
	scsi_qla_host_t *vha;
360
	struct scsi_host_template *sht = &qla2xxx_driver_template;
361 362
	struct Scsi_Host *host;

363 364 365
	vha = qla2x00_create_host(sht, ha);
	if (!vha) {
		DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
366 367 368
		return(NULL);
	}

369
	host = vha->host;
370 371 372 373 374 375 376 377 378 379 380
	fc_vport->dd_data = vha;
	/* New host info */
	u64_to_wwn(fc_vport->node_name, vha->node_name);
	u64_to_wwn(fc_vport->port_name, vha->port_name);

	vha->fc_vport = fc_vport;
	vha->device_flags = 0;
	vha->vp_idx = qla24xx_allocate_vp_id(vha);
	if (vha->vp_idx > ha->max_npiv_vports) {
		DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
			vha->host_no));
381
		goto create_vhost_failed;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	}
	vha->mgmt_svr_loop_id = 10 + vha->vp_idx;

	vha->dpc_flags = 0L;

	/*
	 * To fix the issue of processing a parent's RSCN for the vport before
	 * its SCR is complete.
	 */
	set_bit(VP_SCR_NEEDED, &vha->vp_flags);
	atomic_set(&vha->loop_state, LOOP_DOWN);
	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);

	qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);

397 398
	vha->req = base_vha->req;
	host->can_queue = base_vha->req->length + 128;
399 400 401 402 403
	host->this_id = 255;
	host->cmd_per_lun = 3;
	host->max_cmd_len = MAX_CMDSZ;
	host->max_channel = MAX_BUSES - 1;
	host->max_lun = MAX_LUNS;
404
	host->unique_id = host->host_no;
405 406 407 408 409 410 411 412
	host->max_id = MAX_TARGETS_2200;
	host->transportt = qla2xxx_transport_vport_template;

	DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
	    vha->host_no, vha));

	vha->flags.init_done = 1;

413 414 415 416 417
	mutex_lock(&ha->vport_lock);
	set_bit(vha->vp_idx, ha->vp_idx_map);
	ha->cur_vport_count++;
	mutex_unlock(&ha->vport_lock);

418 419
	return vha;

420
create_vhost_failed:
421 422
	return NULL;
}
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

static void
qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
{
	struct qla_hw_data *ha = vha->hw;
	uint16_t que_id = req->id;

	dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
		sizeof(request_t), req->ring, req->dma);
	req->ring = NULL;
	req->dma = 0;
	if (que_id) {
		ha->req_q_map[que_id] = NULL;
		mutex_lock(&ha->vport_lock);
		clear_bit(que_id, ha->req_qid_map);
		mutex_unlock(&ha->vport_lock);
	}
	kfree(req);
	req = NULL;
}

static void
qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
{
	struct qla_hw_data *ha = vha->hw;
	uint16_t que_id = rsp->id;

	if (rsp->msix && rsp->msix->have_irq) {
		free_irq(rsp->msix->vector, rsp);
		rsp->msix->have_irq = 0;
		rsp->msix->rsp = NULL;
	}
	dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
		sizeof(response_t), rsp->ring, rsp->dma);
	rsp->ring = NULL;
	rsp->dma = 0;
	if (que_id) {
		ha->rsp_q_map[que_id] = NULL;
		mutex_lock(&ha->vport_lock);
		clear_bit(que_id, ha->rsp_qid_map);
		mutex_unlock(&ha->vport_lock);
	}
	kfree(rsp);
	rsp = NULL;
}

int
qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
{
	int ret = -1;

	if (req) {
		req->options |= BIT_0;
476
		ret = qla25xx_init_req_que(vha, req);
477 478 479 480 481 482 483 484 485 486 487 488 489 490
	}
	if (ret == QLA_SUCCESS)
		qla25xx_free_req_que(vha, req);

	return ret;
}

int
qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
{
	int ret = -1;

	if (rsp) {
		rsp->options |= BIT_0;
491
		ret = qla25xx_init_rsp_que(vha, rsp);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	}
	if (ret == QLA_SUCCESS)
		qla25xx_free_rsp_que(vha, rsp);

	return ret;
}

int qla25xx_update_req_que(struct scsi_qla_host *vha, uint8_t que, uint8_t qos)
{
	int ret = 0;
	struct qla_hw_data *ha = vha->hw;
	struct req_que *req = ha->req_q_map[que];

	req->options |= BIT_3;
	req->qos = qos;
507
	ret = qla25xx_init_req_que(vha, req);
508 509 510 511 512 513 514 515 516 517
	if (ret != QLA_SUCCESS)
		DEBUG2_17(printk(KERN_WARNING "%s failed\n", __func__));
	/* restore options bit */
	req->options &= ~BIT_3;
	return ret;
}


/* Delete all queues for a given vhost */
int
518
qla25xx_delete_queues(struct scsi_qla_host *vha)
519 520 521 522 523 524
{
	int cnt, ret = 0;
	struct req_que *req = NULL;
	struct rsp_que *rsp = NULL;
	struct qla_hw_data *ha = vha->hw;

525 526 527
	/* Delete request queues */
	for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
		req = ha->req_q_map[cnt];
528 529 530 531
		if (req) {
			ret = qla25xx_delete_req_que(vha, req);
			if (ret != QLA_SUCCESS) {
				qla_printk(KERN_WARNING, ha,
532 533
				"Couldn't delete req que %d\n",
				req->id);
534 535 536
				return ret;
			}
		}
537 538 539 540 541 542 543 544 545 546 547 548
	}

	/* Delete response queues */
	for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
		rsp = ha->rsp_q_map[cnt];
		if (rsp) {
			ret = qla25xx_delete_rsp_que(vha, rsp);
			if (ret != QLA_SUCCESS) {
				qla_printk(KERN_WARNING, ha,
				"Couldn't delete rsp que %d\n",
				rsp->id);
				return ret;
549 550 551 552 553 554 555 556
			}
		}
	}
	return ret;
}

int
qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
557
	uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
558 559 560 561 562
{
	int ret = 0;
	struct req_que *req = NULL;
	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
	uint16_t que_id = 0;
563
	device_reg_t __iomem *reg;
564
	uint32_t cnt;
565 566 567 568 569

	req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
	if (req == NULL) {
		qla_printk(KERN_WARNING, ha, "could not allocate memory"
			"for request que\n");
570
		goto failed;
571 572 573 574 575 576 577 578 579 580 581 582 583
	}

	req->length = REQUEST_ENTRY_CNT_24XX;
	req->ring = dma_alloc_coherent(&ha->pdev->dev,
			(req->length + 1) * sizeof(request_t),
			&req->dma, GFP_KERNEL);
	if (req->ring == NULL) {
		qla_printk(KERN_WARNING, ha,
		"Memory Allocation failed - request_ring\n");
		goto que_failed;
	}

	mutex_lock(&ha->vport_lock);
584 585
	que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
	if (que_id >= ha->max_req_queues) {
586 587 588 589 590 591 592 593 594 595 596
		mutex_unlock(&ha->vport_lock);
		qla_printk(KERN_INFO, ha, "No resources to create "
			 "additional request queue\n");
		goto que_failed;
	}
	set_bit(que_id, ha->req_qid_map);
	ha->req_q_map[que_id] = req;
	req->rid = rid;
	req->vp_idx = vp_idx;
	req->qos = qos;

597 598 599
	if (rsp_que < 0)
		req->rsp = NULL;
	else
600 601 602 603 604 605 606 607
		req->rsp = ha->rsp_q_map[rsp_que];
	/* Use alternate PCI bus number */
	if (MSB(req->rid))
		options |= BIT_4;
	/* Use alternate PCI devfn */
	if (LSB(req->rid))
		options |= BIT_5;
	req->options = options;
608 609 610 611 612

	for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
		req->outstanding_cmds[cnt] = NULL;
	req->current_outstanding_cmd = 1;

613 614 615 616
	req->ring_ptr = req->ring;
	req->ring_index = 0;
	req->cnt = req->length;
	req->id = que_id;
617
	reg = ISP_QUE_REG(ha, que_id);
618
	req->max_q_depth = ha->req_q_map[0]->max_q_depth;
619 620
	mutex_unlock(&ha->vport_lock);

621
	ret = qla25xx_init_req_que(base_vha, req);
622 623 624 625 626 627 628 629 630 631 632 633
	if (ret != QLA_SUCCESS) {
		qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
		mutex_lock(&ha->vport_lock);
		clear_bit(que_id, ha->req_qid_map);
		mutex_unlock(&ha->vport_lock);
		goto que_failed;
	}

	return req->id;

que_failed:
	qla25xx_free_req_que(base_vha, req);
634
failed:
635 636 637
	return 0;
}

638 639
static void qla_do_work(struct work_struct *work)
{
640
	unsigned long flags;
641 642
	struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
	struct scsi_qla_host *vha;
643
	struct qla_hw_data *ha = rsp->hw;
644

645 646
	spin_lock_irqsave(&rsp->hw->hardware_lock, flags);
	vha = pci_get_drvdata(ha->pdev);
647
	qla24xx_process_response_queue(vha, rsp);
648
	spin_unlock_irqrestore(&rsp->hw->hardware_lock, flags);
649 650
}

651 652 653
/* create response queue */
int
qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
654
	uint8_t vp_idx, uint16_t rid, int req)
655 656 657 658
{
	int ret = 0;
	struct rsp_que *rsp = NULL;
	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
659 660
	uint16_t que_id = 0;
	device_reg_t __iomem *reg;
661 662 663 664 665

	rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
	if (rsp == NULL) {
		qla_printk(KERN_WARNING, ha, "could not allocate memory for"
				" response que\n");
666
		goto failed;
667 668
	}

669
	rsp->length = RESPONSE_ENTRY_CNT_MQ;
670 671 672 673 674 675 676 677 678 679
	rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
			(rsp->length + 1) * sizeof(response_t),
			&rsp->dma, GFP_KERNEL);
	if (rsp->ring == NULL) {
		qla_printk(KERN_WARNING, ha,
		"Memory Allocation failed - response_ring\n");
		goto que_failed;
	}

	mutex_lock(&ha->vport_lock);
680 681
	que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
	if (que_id >= ha->max_rsp_queues) {
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
		mutex_unlock(&ha->vport_lock);
		qla_printk(KERN_INFO, ha, "No resources to create "
			 "additional response queue\n");
		goto que_failed;
	}
	set_bit(que_id, ha->rsp_qid_map);

	if (ha->flags.msix_enabled)
		rsp->msix = &ha->msix_entries[que_id + 1];
	else
		qla_printk(KERN_WARNING, ha, "msix not enabled\n");

	ha->rsp_q_map[que_id] = rsp;
	rsp->rid = rid;
	rsp->vp_idx = vp_idx;
	rsp->hw = ha;
	/* Use alternate PCI bus number */
	if (MSB(rsp->rid))
		options |= BIT_4;
	/* Use alternate PCI devfn */
	if (LSB(rsp->rid))
		options |= BIT_5;
704 705 706 707
	/* Enable MSIX handshake mode on for uncapable adapters */
	if (!IS_MSIX_NACK_CAPABLE(ha))
		options |= BIT_6;

708 709
	rsp->options = options;
	rsp->id = que_id;
710 711 712
	reg = ISP_QUE_REG(ha, que_id);
	rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
	rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
713 714 715 716 717 718
	mutex_unlock(&ha->vport_lock);

	ret = qla25xx_request_irq(rsp);
	if (ret)
		goto que_failed;

719
	ret = qla25xx_init_rsp_que(base_vha, rsp);
720 721 722 723 724 725 726
	if (ret != QLA_SUCCESS) {
		qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
		mutex_lock(&ha->vport_lock);
		clear_bit(que_id, ha->rsp_qid_map);
		mutex_unlock(&ha->vport_lock);
		goto que_failed;
	}
727 728 729 730
	if (req >= 0)
		rsp->req = ha->req_q_map[req];
	else
		rsp->req = NULL;
731 732

	qla2x00_init_response_q_entries(rsp);
733 734
	if (rsp->hw->wq)
		INIT_WORK(&rsp->q_work, qla_do_work);
735 736 737 738
	return rsp->id;

que_failed:
	qla25xx_free_rsp_que(base_vha, rsp);
739
failed:
740 741 742 743 744 745 746 747 748
	return 0;
}

int
qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
{
	uint16_t options = 0;
	uint8_t ret = 0;
	struct qla_hw_data *ha = vha->hw;
749
	struct rsp_que *rsp;
750 751

	options |= BIT_1;
752
	ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0, -1);
753 754 755 756 757
	if (!ret) {
		qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
		return ret;
	} else
		qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
758
	rsp = ha->rsp_q_map[ret];
759 760 761 762 763 764 765

	options = 0;
	if (qos & BIT_7)
		options |= BIT_8;
	ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
					qos & ~BIT_7);
	if (ret) {
766
		vha->req = ha->req_q_map[ret];
767 768 769
		qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
	} else
		qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
770
	rsp->req = ha->req_q_map[ret];
771 772 773

	return ret;
}