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