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