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