xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_mid.c (revision 7bcae826)
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_gbl.h"
9 #include "qla_target.h"
10 
11 #include <linux/moduleparam.h>
12 #include <linux/vmalloc.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 
16 #include <scsi/scsi_tcq.h>
17 #include <scsi/scsicam.h>
18 #include <linux/delay.h>
19 
20 void
21 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
22 {
23 	if (vha->vp_idx && vha->timer_active) {
24 		del_timer_sync(&vha->timer);
25 		vha->timer_active = 0;
26 	}
27 }
28 
29 static uint32_t
30 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
31 {
32 	uint32_t vp_id;
33 	struct qla_hw_data *ha = vha->hw;
34 	unsigned long flags;
35 
36 	/* Find an empty slot and assign an vp_id */
37 	mutex_lock(&ha->vport_lock);
38 	vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
39 	if (vp_id > ha->max_npiv_vports) {
40 		ql_dbg(ql_dbg_vport, vha, 0xa000,
41 		    "vp_id %d is bigger than max-supported %d.\n",
42 		    vp_id, ha->max_npiv_vports);
43 		mutex_unlock(&ha->vport_lock);
44 		return vp_id;
45 	}
46 
47 	set_bit(vp_id, ha->vp_idx_map);
48 	ha->num_vhosts++;
49 	vha->vp_idx = vp_id;
50 
51 	spin_lock_irqsave(&ha->vport_slock, flags);
52 	list_add_tail(&vha->list, &ha->vp_list);
53 
54 	qlt_update_vp_map(vha, SET_VP_IDX);
55 
56 	spin_unlock_irqrestore(&ha->vport_slock, flags);
57 
58 	mutex_unlock(&ha->vport_lock);
59 	return vp_id;
60 }
61 
62 void
63 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
64 {
65 	uint16_t vp_id;
66 	struct qla_hw_data *ha = vha->hw;
67 	unsigned long flags = 0;
68 
69 	mutex_lock(&ha->vport_lock);
70 	/*
71 	 * Wait for all pending activities to finish before removing vport from
72 	 * the list.
73 	 * Lock needs to be held for safe removal from the list (it
74 	 * ensures no active vp_list traversal while the vport is removed
75 	 * from the queue)
76 	 */
77 	spin_lock_irqsave(&ha->vport_slock, flags);
78 	while (atomic_read(&vha->vref_count)) {
79 		spin_unlock_irqrestore(&ha->vport_slock, flags);
80 
81 		msleep(500);
82 
83 		spin_lock_irqsave(&ha->vport_slock, flags);
84 	}
85 	list_del(&vha->list);
86 	qlt_update_vp_map(vha, RESET_VP_IDX);
87 	spin_unlock_irqrestore(&ha->vport_slock, flags);
88 
89 	vp_id = vha->vp_idx;
90 	ha->num_vhosts--;
91 	clear_bit(vp_id, ha->vp_idx_map);
92 
93 	mutex_unlock(&ha->vport_lock);
94 }
95 
96 static scsi_qla_host_t *
97 qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
98 {
99 	scsi_qla_host_t *vha;
100 	struct scsi_qla_host *tvha;
101 	unsigned long flags;
102 
103 	spin_lock_irqsave(&ha->vport_slock, flags);
104 	/* Locate matching device in database. */
105 	list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
106 		if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
107 			spin_unlock_irqrestore(&ha->vport_slock, flags);
108 			return vha;
109 		}
110 	}
111 	spin_unlock_irqrestore(&ha->vport_slock, flags);
112 	return NULL;
113 }
114 
115 /*
116  * qla2x00_mark_vp_devices_dead
117  *	Updates fcport state when device goes offline.
118  *
119  * Input:
120  *	ha = adapter block pointer.
121  *	fcport = port structure pointer.
122  *
123  * Return:
124  *	None.
125  *
126  * Context:
127  */
128 static void
129 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
130 {
131 	/*
132 	 * !!! NOTE !!!
133 	 * This function, if called in contexts other than vp create, disable
134 	 * or delete, please make sure this is synchronized with the
135 	 * delete thread.
136 	 */
137 	fc_port_t *fcport;
138 
139 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
140 		ql_dbg(ql_dbg_vport, vha, 0xa001,
141 		    "Marking port dead, loop_id=0x%04x : %x.\n",
142 		    fcport->loop_id, fcport->vha->vp_idx);
143 
144 		qla2x00_mark_device_lost(vha, fcport, 0, 0);
145 		qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
146 	}
147 }
148 
149 int
150 qla24xx_disable_vp(scsi_qla_host_t *vha)
151 {
152 	unsigned long flags;
153 	int ret;
154 
155 	ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
156 	atomic_set(&vha->loop_state, LOOP_DOWN);
157 	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
158 
159 	/* Remove port id from vp target map */
160 	spin_lock_irqsave(&vha->hw->vport_slock, flags);
161 	qlt_update_vp_map(vha, RESET_AL_PA);
162 	spin_unlock_irqrestore(&vha->hw->vport_slock, flags);
163 
164 	qla2x00_mark_vp_devices_dead(vha);
165 	atomic_set(&vha->vp_state, VP_FAILED);
166 	vha->flags.management_server_logged_in = 0;
167 	if (ret == QLA_SUCCESS) {
168 		fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
169 	} else {
170 		fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
171 		return -1;
172 	}
173 	return 0;
174 }
175 
176 int
177 qla24xx_enable_vp(scsi_qla_host_t *vha)
178 {
179 	int ret;
180 	struct qla_hw_data *ha = vha->hw;
181 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
182 
183 	/* Check if physical ha port is Up */
184 	if (atomic_read(&base_vha->loop_state) == LOOP_DOWN  ||
185 		atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
186 		!(ha->current_topology & ISP_CFG_F)) {
187 		vha->vp_err_state =  VP_ERR_PORTDWN;
188 		fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
189 		goto enable_failed;
190 	}
191 
192 	/* Initialize the new vport unless it is a persistent port */
193 	mutex_lock(&ha->vport_lock);
194 	ret = qla24xx_modify_vp_config(vha);
195 	mutex_unlock(&ha->vport_lock);
196 
197 	if (ret != QLA_SUCCESS) {
198 		fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
199 		goto enable_failed;
200 	}
201 
202 	ql_dbg(ql_dbg_taskm, vha, 0x801a,
203 	    "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
204 	return 0;
205 
206 enable_failed:
207 	ql_dbg(ql_dbg_taskm, vha, 0x801b,
208 	    "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
209 	return 1;
210 }
211 
212 static void
213 qla24xx_configure_vp(scsi_qla_host_t *vha)
214 {
215 	struct fc_vport *fc_vport;
216 	int ret;
217 
218 	fc_vport = vha->fc_vport;
219 
220 	ql_dbg(ql_dbg_vport, vha, 0xa002,
221 	    "%s: change request #3.\n", __func__);
222 	ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
223 	if (ret != QLA_SUCCESS) {
224 		ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
225 		    "receiving of RSCN requests: 0x%x.\n", ret);
226 		return;
227 	} else {
228 		/* Corresponds to SCR enabled */
229 		clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
230 	}
231 
232 	vha->flags.online = 1;
233 	if (qla24xx_configure_vhba(vha))
234 		return;
235 
236 	atomic_set(&vha->vp_state, VP_ACTIVE);
237 	fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
238 }
239 
240 void
241 qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
242 {
243 	scsi_qla_host_t *vha;
244 	struct qla_hw_data *ha = rsp->hw;
245 	int i = 0;
246 	unsigned long flags;
247 
248 	spin_lock_irqsave(&ha->vport_slock, flags);
249 	list_for_each_entry(vha, &ha->vp_list, list) {
250 		if (vha->vp_idx) {
251 			atomic_inc(&vha->vref_count);
252 			spin_unlock_irqrestore(&ha->vport_slock, flags);
253 
254 			switch (mb[0]) {
255 			case MBA_LIP_OCCURRED:
256 			case MBA_LOOP_UP:
257 			case MBA_LOOP_DOWN:
258 			case MBA_LIP_RESET:
259 			case MBA_POINT_TO_POINT:
260 			case MBA_CHG_IN_CONNECTION:
261 			case MBA_PORT_UPDATE:
262 			case MBA_RSCN_UPDATE:
263 				ql_dbg(ql_dbg_async, vha, 0x5024,
264 				    "Async_event for VP[%d], mb=0x%x vha=%p.\n",
265 				    i, *mb, vha);
266 				qla2x00_async_event(vha, rsp, mb);
267 				break;
268 			}
269 
270 			spin_lock_irqsave(&ha->vport_slock, flags);
271 			atomic_dec(&vha->vref_count);
272 		}
273 		i++;
274 	}
275 	spin_unlock_irqrestore(&ha->vport_slock, flags);
276 }
277 
278 int
279 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
280 {
281 	/*
282 	 * Physical port will do most of the abort and recovery work. We can
283 	 * just treat it as a loop down
284 	 */
285 	if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
286 		atomic_set(&vha->loop_state, LOOP_DOWN);
287 		qla2x00_mark_all_devices_lost(vha, 0);
288 	} else {
289 		if (!atomic_read(&vha->loop_down_timer))
290 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
291 	}
292 
293 	/*
294 	 * To exclusively reset vport, we need to log it out first.  Note: this
295 	 * control_vp can fail if ISP reset is already issued, this is
296 	 * expected, as the vp would be already logged out due to ISP reset.
297 	 */
298 	if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
299 		qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
300 
301 	ql_dbg(ql_dbg_taskm, vha, 0x801d,
302 	    "Scheduling enable of Vport %d.\n", vha->vp_idx);
303 	return qla24xx_enable_vp(vha);
304 }
305 
306 static int
307 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
308 {
309 	struct qla_hw_data *ha = vha->hw;
310 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
311 
312 	ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
313 	    "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
314 
315 	qla2x00_do_work(vha);
316 
317 	/* Check if Fw is ready to configure VP first */
318 	if (test_bit(VP_CONFIG_OK, &base_vha->vp_flags)) {
319 		if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
320 			/* VP acquired. complete port configuration */
321 			ql_dbg(ql_dbg_dpc, vha, 0x4014,
322 			    "Configure VP scheduled.\n");
323 			qla24xx_configure_vp(vha);
324 			ql_dbg(ql_dbg_dpc, vha, 0x4015,
325 			    "Configure VP end.\n");
326 			return 0;
327 		}
328 	}
329 
330 	if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
331 		ql_dbg(ql_dbg_dpc, vha, 0x4016,
332 		    "FCPort update scheduled.\n");
333 		qla2x00_update_fcports(vha);
334 		clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
335 		ql_dbg(ql_dbg_dpc, vha, 0x4017,
336 		    "FCPort update end.\n");
337 	}
338 
339 	if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
340 		!test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
341 		atomic_read(&vha->loop_state) != LOOP_DOWN) {
342 
343 		ql_dbg(ql_dbg_dpc, vha, 0x4018,
344 		    "Relogin needed scheduled.\n");
345 		qla2x00_relogin(vha);
346 		ql_dbg(ql_dbg_dpc, vha, 0x4019,
347 		    "Relogin needed end.\n");
348 	}
349 
350 	if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
351 	    (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
352 		clear_bit(RESET_ACTIVE, &vha->dpc_flags);
353 	}
354 
355 	if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
356 		if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
357 			ql_dbg(ql_dbg_dpc, vha, 0x401a,
358 			    "Loop resync scheduled.\n");
359 			qla2x00_loop_resync(vha);
360 			clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
361 			ql_dbg(ql_dbg_dpc, vha, 0x401b,
362 			    "Loop resync end.\n");
363 		}
364 	}
365 
366 	ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
367 	    "Exiting %s.\n", __func__);
368 	return 0;
369 }
370 
371 void
372 qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
373 {
374 	struct qla_hw_data *ha = vha->hw;
375 	scsi_qla_host_t *vp;
376 	unsigned long flags = 0;
377 
378 	if (vha->vp_idx)
379 		return;
380 	if (list_empty(&ha->vp_list))
381 		return;
382 
383 	clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
384 
385 	if (!(ha->current_topology & ISP_CFG_F))
386 		return;
387 
388 	spin_lock_irqsave(&ha->vport_slock, flags);
389 	list_for_each_entry(vp, &ha->vp_list, list) {
390 		if (vp->vp_idx) {
391 			atomic_inc(&vp->vref_count);
392 			spin_unlock_irqrestore(&ha->vport_slock, flags);
393 
394 			qla2x00_do_dpc_vp(vp);
395 
396 			spin_lock_irqsave(&ha->vport_slock, flags);
397 			atomic_dec(&vp->vref_count);
398 		}
399 	}
400 	spin_unlock_irqrestore(&ha->vport_slock, flags);
401 }
402 
403 int
404 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
405 {
406 	scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
407 	struct qla_hw_data *ha = base_vha->hw;
408 	scsi_qla_host_t *vha;
409 	uint8_t port_name[WWN_SIZE];
410 
411 	if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
412 		return VPCERR_UNSUPPORTED;
413 
414 	/* Check up the F/W and H/W support NPIV */
415 	if (!ha->flags.npiv_supported)
416 		return VPCERR_UNSUPPORTED;
417 
418 	/* Check up whether npiv supported switch presented */
419 	if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
420 		return VPCERR_NO_FABRIC_SUPP;
421 
422 	/* Check up unique WWPN */
423 	u64_to_wwn(fc_vport->port_name, port_name);
424 	if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
425 		return VPCERR_BAD_WWN;
426 	vha = qla24xx_find_vhost_by_name(ha, port_name);
427 	if (vha)
428 		return VPCERR_BAD_WWN;
429 
430 	/* Check up max-npiv-supports */
431 	if (ha->num_vhosts > ha->max_npiv_vports) {
432 		ql_dbg(ql_dbg_vport, vha, 0xa004,
433 		    "num_vhosts %ud is bigger "
434 		    "than max_npiv_vports %ud.\n",
435 		    ha->num_vhosts, ha->max_npiv_vports);
436 		return VPCERR_UNSUPPORTED;
437 	}
438 	return 0;
439 }
440 
441 scsi_qla_host_t *
442 qla24xx_create_vhost(struct fc_vport *fc_vport)
443 {
444 	scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
445 	struct qla_hw_data *ha = base_vha->hw;
446 	scsi_qla_host_t *vha;
447 	struct scsi_host_template *sht = &qla2xxx_driver_template;
448 	struct Scsi_Host *host;
449 
450 	vha = qla2x00_create_host(sht, ha);
451 	if (!vha) {
452 		ql_log(ql_log_warn, vha, 0xa005,
453 		    "scsi_host_alloc() failed for vport.\n");
454 		return(NULL);
455 	}
456 
457 	host = vha->host;
458 	fc_vport->dd_data = vha;
459 	/* New host info */
460 	u64_to_wwn(fc_vport->node_name, vha->node_name);
461 	u64_to_wwn(fc_vport->port_name, vha->port_name);
462 
463 	vha->fc_vport = fc_vport;
464 	vha->device_flags = 0;
465 	vha->vp_idx = qla24xx_allocate_vp_id(vha);
466 	if (vha->vp_idx > ha->max_npiv_vports) {
467 		ql_dbg(ql_dbg_vport, vha, 0xa006,
468 		    "Couldn't allocate vp_id.\n");
469 		goto create_vhost_failed;
470 	}
471 	vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
472 
473 	vha->dpc_flags = 0L;
474 
475 	/*
476 	 * To fix the issue of processing a parent's RSCN for the vport before
477 	 * its SCR is complete.
478 	 */
479 	set_bit(VP_SCR_NEEDED, &vha->vp_flags);
480 	atomic_set(&vha->loop_state, LOOP_DOWN);
481 	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
482 
483 	qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
484 
485 	vha->req = base_vha->req;
486 	host->can_queue = base_vha->req->length + 128;
487 	host->cmd_per_lun = 3;
488 	if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
489 		host->max_cmd_len = 32;
490 	else
491 		host->max_cmd_len = MAX_CMDSZ;
492 	host->max_channel = MAX_BUSES - 1;
493 	host->max_lun = ql2xmaxlun;
494 	host->unique_id = host->host_no;
495 	host->max_id = ha->max_fibre_devices;
496 	host->transportt = qla2xxx_transport_vport_template;
497 
498 	ql_dbg(ql_dbg_vport, vha, 0xa007,
499 	    "Detect vport hba %ld at address = %p.\n",
500 	    vha->host_no, vha);
501 
502 	vha->flags.init_done = 1;
503 
504 	mutex_lock(&ha->vport_lock);
505 	set_bit(vha->vp_idx, ha->vp_idx_map);
506 	ha->cur_vport_count++;
507 	mutex_unlock(&ha->vport_lock);
508 
509 	return vha;
510 
511 create_vhost_failed:
512 	return NULL;
513 }
514 
515 static void
516 qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
517 {
518 	struct qla_hw_data *ha = vha->hw;
519 	uint16_t que_id = req->id;
520 
521 	dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
522 		sizeof(request_t), req->ring, req->dma);
523 	req->ring = NULL;
524 	req->dma = 0;
525 	if (que_id) {
526 		ha->req_q_map[que_id] = NULL;
527 		mutex_lock(&ha->vport_lock);
528 		clear_bit(que_id, ha->req_qid_map);
529 		mutex_unlock(&ha->vport_lock);
530 	}
531 	kfree(req->outstanding_cmds);
532 	kfree(req);
533 	req = NULL;
534 }
535 
536 static void
537 qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
538 {
539 	struct qla_hw_data *ha = vha->hw;
540 	uint16_t que_id = rsp->id;
541 
542 	if (rsp->msix && rsp->msix->have_irq) {
543 		free_irq(rsp->msix->vector, rsp->msix->handle);
544 		rsp->msix->have_irq = 0;
545 		rsp->msix->in_use = 0;
546 		rsp->msix->handle = NULL;
547 	}
548 	dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
549 		sizeof(response_t), rsp->ring, rsp->dma);
550 	rsp->ring = NULL;
551 	rsp->dma = 0;
552 	if (que_id) {
553 		ha->rsp_q_map[que_id] = NULL;
554 		mutex_lock(&ha->vport_lock);
555 		clear_bit(que_id, ha->rsp_qid_map);
556 		mutex_unlock(&ha->vport_lock);
557 	}
558 	kfree(rsp);
559 	rsp = NULL;
560 }
561 
562 int
563 qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
564 {
565 	int ret = -1;
566 
567 	if (req) {
568 		req->options |= BIT_0;
569 		ret = qla25xx_init_req_que(vha, req);
570 	}
571 	if (ret == QLA_SUCCESS)
572 		qla25xx_free_req_que(vha, req);
573 
574 	return ret;
575 }
576 
577 int
578 qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
579 {
580 	int ret = -1;
581 
582 	if (rsp) {
583 		rsp->options |= BIT_0;
584 		ret = qla25xx_init_rsp_que(vha, rsp);
585 	}
586 	if (ret == QLA_SUCCESS)
587 		qla25xx_free_rsp_que(vha, rsp);
588 
589 	return ret;
590 }
591 
592 /* Delete all queues for a given vhost */
593 int
594 qla25xx_delete_queues(struct scsi_qla_host *vha)
595 {
596 	int cnt, ret = 0;
597 	struct req_que *req = NULL;
598 	struct rsp_que *rsp = NULL;
599 	struct qla_hw_data *ha = vha->hw;
600 	struct qla_qpair *qpair, *tqpair;
601 
602 	if (ql2xmqsupport) {
603 		list_for_each_entry_safe(qpair, tqpair, &vha->qp_list,
604 		    qp_list_elem)
605 			qla2xxx_delete_qpair(vha, qpair);
606 	} else {
607 		/* Delete request queues */
608 		for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
609 			req = ha->req_q_map[cnt];
610 			if (req && test_bit(cnt, ha->req_qid_map)) {
611 				ret = qla25xx_delete_req_que(vha, req);
612 				if (ret != QLA_SUCCESS) {
613 					ql_log(ql_log_warn, vha, 0x00ea,
614 					    "Couldn't delete req que %d.\n",
615 					    req->id);
616 					return ret;
617 				}
618 			}
619 		}
620 
621 		/* Delete response queues */
622 		for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
623 			rsp = ha->rsp_q_map[cnt];
624 			if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
625 				ret = qla25xx_delete_rsp_que(vha, rsp);
626 				if (ret != QLA_SUCCESS) {
627 					ql_log(ql_log_warn, vha, 0x00eb,
628 					    "Couldn't delete rsp que %d.\n",
629 					    rsp->id);
630 					return ret;
631 				}
632 			}
633 		}
634 	}
635 
636 	return ret;
637 }
638 
639 int
640 qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
641 	uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
642 {
643 	int ret = 0;
644 	struct req_que *req = NULL;
645 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
646 	uint16_t que_id = 0;
647 	device_reg_t *reg;
648 	uint32_t cnt;
649 
650 	req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
651 	if (req == NULL) {
652 		ql_log(ql_log_fatal, base_vha, 0x00d9,
653 		    "Failed to allocate memory for request queue.\n");
654 		goto failed;
655 	}
656 
657 	req->length = REQUEST_ENTRY_CNT_24XX;
658 	req->ring = dma_alloc_coherent(&ha->pdev->dev,
659 			(req->length + 1) * sizeof(request_t),
660 			&req->dma, GFP_KERNEL);
661 	if (req->ring == NULL) {
662 		ql_log(ql_log_fatal, base_vha, 0x00da,
663 		    "Failed to allocate memory for request_ring.\n");
664 		goto que_failed;
665 	}
666 
667 	ret = qla2x00_alloc_outstanding_cmds(ha, req);
668 	if (ret != QLA_SUCCESS)
669 		goto que_failed;
670 
671 	mutex_lock(&ha->mq_lock);
672 	que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
673 	if (que_id >= ha->max_req_queues) {
674 		mutex_unlock(&ha->mq_lock);
675 		ql_log(ql_log_warn, base_vha, 0x00db,
676 		    "No resources to create additional request queue.\n");
677 		goto que_failed;
678 	}
679 	set_bit(que_id, ha->req_qid_map);
680 	ha->req_q_map[que_id] = req;
681 	req->rid = rid;
682 	req->vp_idx = vp_idx;
683 	req->qos = qos;
684 
685 	ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
686 	    "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
687 	    que_id, req->rid, req->vp_idx, req->qos);
688 	ql_dbg(ql_dbg_init, base_vha, 0x00dc,
689 	    "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
690 	    que_id, req->rid, req->vp_idx, req->qos);
691 	if (rsp_que < 0)
692 		req->rsp = NULL;
693 	else
694 		req->rsp = ha->rsp_q_map[rsp_que];
695 	/* Use alternate PCI bus number */
696 	if (MSB(req->rid))
697 		options |= BIT_4;
698 	/* Use alternate PCI devfn */
699 	if (LSB(req->rid))
700 		options |= BIT_5;
701 	req->options = options;
702 
703 	ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
704 	    "options=0x%x.\n", req->options);
705 	ql_dbg(ql_dbg_init, base_vha, 0x00dd,
706 	    "options=0x%x.\n", req->options);
707 	for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
708 		req->outstanding_cmds[cnt] = NULL;
709 	req->current_outstanding_cmd = 1;
710 
711 	req->ring_ptr = req->ring;
712 	req->ring_index = 0;
713 	req->cnt = req->length;
714 	req->id = que_id;
715 	reg = ISP_QUE_REG(ha, que_id);
716 	req->req_q_in = &reg->isp25mq.req_q_in;
717 	req->req_q_out = &reg->isp25mq.req_q_out;
718 	req->max_q_depth = ha->req_q_map[0]->max_q_depth;
719 	req->out_ptr = (void *)(req->ring + req->length);
720 	mutex_unlock(&ha->mq_lock);
721 	ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
722 	    "ring_ptr=%p ring_index=%d, "
723 	    "cnt=%d id=%d max_q_depth=%d.\n",
724 	    req->ring_ptr, req->ring_index,
725 	    req->cnt, req->id, req->max_q_depth);
726 	ql_dbg(ql_dbg_init, base_vha, 0x00de,
727 	    "ring_ptr=%p ring_index=%d, "
728 	    "cnt=%d id=%d max_q_depth=%d.\n",
729 	    req->ring_ptr, req->ring_index, req->cnt,
730 	    req->id, req->max_q_depth);
731 
732 	ret = qla25xx_init_req_que(base_vha, req);
733 	if (ret != QLA_SUCCESS) {
734 		ql_log(ql_log_fatal, base_vha, 0x00df,
735 		    "%s failed.\n", __func__);
736 		mutex_lock(&ha->mq_lock);
737 		clear_bit(que_id, ha->req_qid_map);
738 		mutex_unlock(&ha->mq_lock);
739 		goto que_failed;
740 	}
741 
742 	return req->id;
743 
744 que_failed:
745 	qla25xx_free_req_que(base_vha, req);
746 failed:
747 	return 0;
748 }
749 
750 static void qla_do_work(struct work_struct *work)
751 {
752 	unsigned long flags;
753 	struct qla_qpair *qpair = container_of(work, struct qla_qpair, q_work);
754 	struct scsi_qla_host *vha;
755 	struct qla_hw_data *ha = qpair->hw;
756 
757 	spin_lock_irqsave(&qpair->qp_lock, flags);
758 	vha = pci_get_drvdata(ha->pdev);
759 	qla24xx_process_response_queue(vha, qpair->rsp);
760 	spin_unlock_irqrestore(&qpair->qp_lock, flags);
761 }
762 
763 /* create response queue */
764 int
765 qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
766 	uint8_t vp_idx, uint16_t rid, struct qla_qpair *qpair)
767 {
768 	int ret = 0;
769 	struct rsp_que *rsp = NULL;
770 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
771 	uint16_t que_id = 0;
772 	device_reg_t *reg;
773 
774 	rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
775 	if (rsp == NULL) {
776 		ql_log(ql_log_warn, base_vha, 0x0066,
777 		    "Failed to allocate memory for response queue.\n");
778 		goto failed;
779 	}
780 
781 	rsp->length = RESPONSE_ENTRY_CNT_MQ;
782 	rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
783 			(rsp->length + 1) * sizeof(response_t),
784 			&rsp->dma, GFP_KERNEL);
785 	if (rsp->ring == NULL) {
786 		ql_log(ql_log_warn, base_vha, 0x00e1,
787 		    "Failed to allocate memory for response ring.\n");
788 		goto que_failed;
789 	}
790 
791 	mutex_lock(&ha->mq_lock);
792 	que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
793 	if (que_id >= ha->max_rsp_queues) {
794 		mutex_unlock(&ha->mq_lock);
795 		ql_log(ql_log_warn, base_vha, 0x00e2,
796 		    "No resources to create additional request queue.\n");
797 		goto que_failed;
798 	}
799 	set_bit(que_id, ha->rsp_qid_map);
800 
801 	rsp->msix = qpair->msix;
802 
803 	ha->rsp_q_map[que_id] = rsp;
804 	rsp->rid = rid;
805 	rsp->vp_idx = vp_idx;
806 	rsp->hw = ha;
807 	ql_dbg(ql_dbg_init, base_vha, 0x00e4,
808 	    "rsp queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
809 	    que_id, rsp->rid, rsp->vp_idx, rsp->hw);
810 	/* Use alternate PCI bus number */
811 	if (MSB(rsp->rid))
812 		options |= BIT_4;
813 	/* Use alternate PCI devfn */
814 	if (LSB(rsp->rid))
815 		options |= BIT_5;
816 	/* Enable MSIX handshake mode on for uncapable adapters */
817 	if (!IS_MSIX_NACK_CAPABLE(ha))
818 		options |= BIT_6;
819 
820 	/* Set option to indicate response queue creation */
821 	options |= BIT_1;
822 
823 	rsp->options = options;
824 	rsp->id = que_id;
825 	reg = ISP_QUE_REG(ha, que_id);
826 	rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
827 	rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
828 	rsp->in_ptr = (void *)(rsp->ring + rsp->length);
829 	mutex_unlock(&ha->mq_lock);
830 	ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
831 	    "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
832 	    rsp->options, rsp->id, rsp->rsp_q_in,
833 	    rsp->rsp_q_out);
834 	ql_dbg(ql_dbg_init, base_vha, 0x00e5,
835 	    "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
836 	    rsp->options, rsp->id, rsp->rsp_q_in,
837 	    rsp->rsp_q_out);
838 
839 	ret = qla25xx_request_irq(ha, qpair, qpair->msix,
840 	    QLA_MSIX_QPAIR_MULTIQ_RSP_Q);
841 	if (ret)
842 		goto que_failed;
843 
844 	ret = qla25xx_init_rsp_que(base_vha, rsp);
845 	if (ret != QLA_SUCCESS) {
846 		ql_log(ql_log_fatal, base_vha, 0x00e7,
847 		    "%s failed.\n", __func__);
848 		mutex_lock(&ha->mq_lock);
849 		clear_bit(que_id, ha->rsp_qid_map);
850 		mutex_unlock(&ha->mq_lock);
851 		goto que_failed;
852 	}
853 	rsp->req = NULL;
854 
855 	qla2x00_init_response_q_entries(rsp);
856 	if (qpair->hw->wq)
857 		INIT_WORK(&qpair->q_work, qla_do_work);
858 	return rsp->id;
859 
860 que_failed:
861 	qla25xx_free_rsp_que(base_vha, rsp);
862 failed:
863 	return 0;
864 }
865