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