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