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