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