scsi: qla2xxx: Enable type checking for the SRB free and done callback functions
[linux-2.6-block.git] / drivers / scsi / qla2xxx / qla_mid.c
CommitLineData
2c3dfe3f 1/*
01e58d8e 2 * QLogic Fibre Channel HBA Driver
bd21eaf9 3 * Copyright (c) 2003-2014 QLogic Corporation
2c3dfe3f 4 *
01e58d8e 5 * See LICENSE.qla2xxx for copyright and licensing details.
2c3dfe3f
SJ
6 */
7#include "qla_def.h"
7b867cf7 8#include "qla_gbl.h"
2d70c103 9#include "qla_target.h"
2c3dfe3f 10
2c3dfe3f
SJ
11#include <linux/moduleparam.h>
12#include <linux/vmalloc.h>
5a0e3ad6 13#include <linux/slab.h>
2c3dfe3f
SJ
14#include <linux/list.h>
15
16#include <scsi/scsi_tcq.h>
17#include <scsi/scsicam.h>
18#include <linux/delay.h>
19
2c3dfe3f
SJ
20void
21qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
22{
7b867cf7 23 if (vha->vp_idx && vha->timer_active) {
2c3dfe3f
SJ
24 del_timer_sync(&vha->timer);
25 vha->timer_active = 0;
26 }
27}
28
a824ebb3 29static uint32_t
2c3dfe3f
SJ
30qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
31{
32 uint32_t vp_id;
7b867cf7 33 struct qla_hw_data *ha = vha->hw;
feafb7b1 34 unsigned long flags;
2c3dfe3f
SJ
35
36 /* Find an empty slot and assign an vp_id */
6c2f527c 37 mutex_lock(&ha->vport_lock);
eb66dc60
AV
38 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
39 if (vp_id > ha->max_npiv_vports) {
7c3df132
SK
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);
6c2f527c 43 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
44 return vp_id;
45 }
46
eb66dc60 47 set_bit(vp_id, ha->vp_idx_map);
2c3dfe3f
SJ
48 ha->num_vhosts++;
49 vha->vp_idx = vp_id;
feafb7b1
AE
50
51 spin_lock_irqsave(&ha->vport_slock, flags);
7b867cf7 52 list_add_tail(&vha->list, &ha->vp_list);
9d1aa4e1 53 spin_unlock_irqrestore(&ha->vport_slock, flags);
2d70c103 54
9d1aa4e1 55 spin_lock_irqsave(&ha->hardware_lock, flags);
2d70c103 56 qlt_update_vp_map(vha, SET_VP_IDX);
9d1aa4e1 57 spin_unlock_irqrestore(&ha->hardware_lock, flags);
feafb7b1 58
6c2f527c 59 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
60 return vp_id;
61}
62
63void
64qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
65{
66 uint16_t vp_id;
7b867cf7 67 struct qla_hw_data *ha = vha->hw;
feafb7b1 68 unsigned long flags = 0;
2c3dfe3f 69
6c2f527c 70 mutex_lock(&ha->vport_lock);
feafb7b1
AE
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 */
6e98095f 78 wait_event_timeout(vha->vref_waitq, !atomic_read(&vha->vref_count),
c4a9b538 79 10*HZ);
feafb7b1 80
c4a9b538
JC
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);
feafb7b1
AE
86 }
87 list_del(&vha->list);
2d70c103 88 qlt_update_vp_map(vha, RESET_VP_IDX);
feafb7b1
AE
89 spin_unlock_irqrestore(&ha->vport_slock, flags);
90
2c3dfe3f
SJ
91 vp_id = vha->vp_idx;
92 ha->num_vhosts--;
eb66dc60 93 clear_bit(vp_id, ha->vp_idx_map);
feafb7b1 94
6c2f527c 95 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
96}
97
a824ebb3 98static scsi_qla_host_t *
7b867cf7 99qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
2c3dfe3f
SJ
100{
101 scsi_qla_host_t *vha;
ee546b6e 102 struct scsi_qla_host *tvha;
feafb7b1 103 unsigned long flags;
2c3dfe3f 104
feafb7b1 105 spin_lock_irqsave(&ha->vport_slock, flags);
2c3dfe3f 106 /* Locate matching device in database. */
ee546b6e 107 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
feafb7b1
AE
108 if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
109 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f 110 return vha;
feafb7b1 111 }
2c3dfe3f 112 }
feafb7b1 113 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f
SJ
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 */
26ff776d 130static void
2c3dfe3f
SJ
131qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
132{
feafb7b1
AE
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 */
2c3dfe3f 139 fc_port_t *fcport;
2c3dfe3f 140
7b867cf7 141 list_for_each_entry(fcport, &vha->vp_fcports, list) {
7c3df132
SK
142 ql_dbg(ql_dbg_vport, vha, 0xa001,
143 "Marking port dead, loop_id=0x%04x : %x.\n",
c6d39e23 144 fcport->loop_id, fcport->vha->vp_idx);
2c3dfe3f 145
2c3dfe3f 146 qla2x00_mark_device_lost(vha, fcport, 0, 0);
ec426e10 147 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
2c3dfe3f
SJ
148 }
149}
150
151int
152qla24xx_disable_vp(scsi_qla_host_t *vha)
153{
29c08cda 154 unsigned long flags;
45235022 155 int ret = QLA_SUCCESS;
efa93f48 156 fc_port_t *fcport;
2c3dfe3f 157
45235022
QT
158 if (vha->hw->flags.fw_started)
159 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
160
2c3dfe3f
SJ
161 atomic_set(&vha->loop_state, LOOP_DOWN);
162 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
efa93f48
QT
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);
2c3dfe3f 167
2d70c103 168 /* Remove port id from vp target map */
9d1aa4e1 169 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2d70c103 170 qlt_update_vp_map(vha, RESET_AL_PA);
9d1aa4e1 171 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
2d70c103 172
2c3dfe3f
SJ
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
185int
186qla24xx_enable_vp(scsi_qla_host_t *vha)
187{
188 int ret;
7b867cf7
AC
189 struct qla_hw_data *ha = vha->hw;
190 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
2c3dfe3f
SJ
191
192 /* Check if physical ha port is Up */
7b867cf7 193 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
3f3b6f98
LC
194 atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
195 !(ha->current_topology & ISP_CFG_F)) {
2c3dfe3f
SJ
196 vha->vp_err_state = VP_ERR_PORTDWN;
197 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
1608cc4a
QT
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
2c3dfe3f
SJ
203 goto enable_failed;
204 }
205
206 /* Initialize the new vport unless it is a persistent port */
6c2f527c 207 mutex_lock(&ha->vport_lock);
2c3dfe3f 208 ret = qla24xx_modify_vp_config(vha);
6c2f527c 209 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
210
211 if (ret != QLA_SUCCESS) {
212 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
213 goto enable_failed;
214 }
215
7c3df132
SK
216 ql_dbg(ql_dbg_taskm, vha, 0x801a,
217 "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
2c3dfe3f
SJ
218 return 0;
219
220enable_failed:
7c3df132
SK
221 ql_dbg(ql_dbg_taskm, vha, 0x801b,
222 "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
2c3dfe3f
SJ
223 return 1;
224}
225
26ff776d 226static void
2c3dfe3f
SJ
227qla24xx_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
7c3df132
SK
234 ql_dbg(ql_dbg_vport, vha, 0xa002,
235 "%s: change request #3.\n", __func__);
2c3dfe3f
SJ
236 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
237 if (ret != QLA_SUCCESS) {
7c3df132
SK
238 ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
239 "receiving of RSCN requests: 0x%x.\n", ret);
2c3dfe3f
SJ
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
254void
73208dfd 255qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
2c3dfe3f 256{
feafb7b1 257 scsi_qla_host_t *vha;
73208dfd 258 struct qla_hw_data *ha = rsp->hw;
7b867cf7 259 int i = 0;
feafb7b1 260 unsigned long flags;
2c3dfe3f 261
feafb7b1
AE
262 spin_lock_irqsave(&ha->vport_slock, flags);
263 list_for_each_entry(vha, &ha->vp_list, list) {
7b867cf7 264 if (vha->vp_idx) {
feafb7b1
AE
265 atomic_inc(&vha->vref_count);
266 spin_unlock_irqrestore(&ha->vport_slock, flags);
267
2c3dfe3f
SJ
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:
7c3df132
SK
275 ql_dbg(ql_dbg_async, vha, 0x5024,
276 "Async_event for VP[%d], mb=0x%x vha=%p.\n",
277 i, *mb, vha);
73208dfd 278 qla2x00_async_event(vha, rsp, mb);
2c3dfe3f 279 break;
75061750
QT
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;
2c3dfe3f 289 }
feafb7b1
AE
290
291 spin_lock_irqsave(&ha->vport_slock, flags);
292 atomic_dec(&vha->vref_count);
c4a9b538 293 wake_up(&vha->vref_waitq);
2c3dfe3f 294 }
7b867cf7 295 i++;
2c3dfe3f 296 }
feafb7b1 297 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f
SJ
298}
299
7b867cf7 300int
2c3dfe3f
SJ
301qla2x00_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
0d6e61bc
AV
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 */
7b867cf7
AC
320 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
321 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
322
7c3df132
SK
323 ql_dbg(ql_dbg_taskm, vha, 0x801d,
324 "Scheduling enable of Vport %d.\n", vha->vp_idx);
7b867cf7 325 return qla24xx_enable_vp(vha);
2c3dfe3f
SJ
326}
327
a824ebb3 328static int
2c3dfe3f
SJ
329qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
330{
ded6411f
SC
331 struct qla_hw_data *ha = vha->hw;
332 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
333
5f28d2d7
SK
334 ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
335 "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
7c3df132 336
ded6411f
SC
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 }
2c3dfe3f
SJ
348 }
349
7b867cf7 350 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
7c3df132
SK
351 ql_dbg(ql_dbg_dpc, vha, 0x4016,
352 "FCPort update scheduled.\n");
7b867cf7
AC
353 qla2x00_update_fcports(vha);
354 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
7c3df132
SK
355 ql_dbg(ql_dbg_dpc, vha, 0x4017,
356 "FCPort update end.\n");
7b867cf7
AC
357 }
358
4005a995
QT
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");
9b3e0f4d 370 qla24xx_post_relogin_work(vha);
4005a995 371 }
7b867cf7 372 }
2c3dfe3f
SJ
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
73208dfd 379 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
2c3dfe3f 380 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
7c3df132
SK
381 ql_dbg(ql_dbg_dpc, vha, 0x401a,
382 "Loop resync scheduled.\n");
2c3dfe3f
SJ
383 qla2x00_loop_resync(vha);
384 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
7c3df132
SK
385 ql_dbg(ql_dbg_dpc, vha, 0x401b,
386 "Loop resync end.\n");
2c3dfe3f
SJ
387 }
388 }
389
5f28d2d7 390 ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
7c3df132 391 "Exiting %s.\n", __func__);
2c3dfe3f
SJ
392 return 0;
393}
394
395void
7b867cf7 396qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
2c3dfe3f 397{
7b867cf7
AC
398 struct qla_hw_data *ha = vha->hw;
399 scsi_qla_host_t *vp;
feafb7b1 400 unsigned long flags = 0;
2c3dfe3f 401
7b867cf7 402 if (vha->vp_idx)
2c3dfe3f
SJ
403 return;
404 if (list_empty(&ha->vp_list))
405 return;
406
7b867cf7 407 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
2c3dfe3f 408
0d6e61bc
AV
409 if (!(ha->current_topology & ISP_CFG_F))
410 return;
411
feafb7b1
AE
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
52c82823 418 qla2x00_do_dpc_vp(vp);
feafb7b1
AE
419
420 spin_lock_irqsave(&ha->vport_slock, flags);
421 atomic_dec(&vp->vref_count);
422 }
2c3dfe3f 423 }
feafb7b1 424 spin_unlock_irqrestore(&ha->vport_slock, flags);
2c3dfe3f
SJ
425}
426
427int
428qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
429{
7b867cf7
AC
430 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
431 struct qla_hw_data *ha = base_vha->hw;
2c3dfe3f
SJ
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);
7b867cf7 448 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
50db6b13 449 return VPCERR_BAD_WWN;
2c3dfe3f
SJ
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) {
7c3df132
SK
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);
2c3dfe3f
SJ
460 return VPCERR_UNSUPPORTED;
461 }
462 return 0;
463}
464
465scsi_qla_host_t *
466qla24xx_create_vhost(struct fc_vport *fc_vport)
467{
7b867cf7
AC
468 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
469 struct qla_hw_data *ha = base_vha->hw;
2c3dfe3f 470 scsi_qla_host_t *vha;
a5326f86 471 struct scsi_host_template *sht = &qla2xxx_driver_template;
2c3dfe3f
SJ
472 struct Scsi_Host *host;
473
7b867cf7
AC
474 vha = qla2x00_create_host(sht, ha);
475 if (!vha) {
7c3df132
SK
476 ql_log(ql_log_warn, vha, 0xa005,
477 "scsi_host_alloc() failed for vport.\n");
2c3dfe3f
SJ
478 return(NULL);
479 }
480
7b867cf7 481 host = vha->host;
2c3dfe3f 482 fc_vport->dd_data = vha;
2c3dfe3f
SJ
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
2c3dfe3f
SJ
487 vha->fc_vport = fc_vport;
488 vha->device_flags = 0;
2c3dfe3f
SJ
489 vha->vp_idx = qla24xx_allocate_vp_id(vha);
490 if (vha->vp_idx > ha->max_npiv_vports) {
7c3df132
SK
491 ql_dbg(ql_dbg_vport, vha, 0xa006,
492 "Couldn't allocate vp_id.\n");
7b867cf7 493 goto create_vhost_failed;
2c3dfe3f 494 }
f6602f3b 495 vha->mgmt_svr_loop_id = qla2x00_reserve_mgmt_server_loop_id(vha);
2c3dfe3f 496
2c3dfe3f 497 vha->dpc_flags = 0L;
2c3dfe3f
SJ
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
8e5f4ba0 507 qla2x00_start_timer(vha, WATCH_INTERVAL);
2c3dfe3f 508
2afa19a9 509 vha->req = base_vha->req;
850f6acd 510 vha->flags.nvme_enabled = base_vha->flags.nvme_enabled;
2afa19a9 511 host->can_queue = base_vha->req->length + 128;
2c3dfe3f 512 host->cmd_per_lun = 3;
e02587d7 513 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
0c470874
AE
514 host->max_cmd_len = 32;
515 else
516 host->max_cmd_len = MAX_CMDSZ;
2c3dfe3f 517 host->max_channel = MAX_BUSES - 1;
82515920 518 host->max_lun = ql2xmaxlun;
711c1d91 519 host->unique_id = host->host_no;
642ef983 520 host->max_id = ha->max_fibre_devices;
2c3dfe3f
SJ
521 host->transportt = qla2xxx_transport_vport_template;
522
7c3df132
SK
523 ql_dbg(ql_dbg_vport, vha, 0xa007,
524 "Detect vport hba %ld at address = %p.\n",
525 vha->host_no, vha);
2c3dfe3f
SJ
526
527 vha->flags.init_done = 1;
2c3dfe3f 528
0d6e61bc
AV
529 mutex_lock(&ha->vport_lock);
530 set_bit(vha->vp_idx, ha->vp_idx_map);
531 ha->cur_vport_count++;
532 mutex_unlock(&ha->vport_lock);
533
2c3dfe3f
SJ
534 return vha;
535
7b867cf7 536create_vhost_failed:
2c3dfe3f
SJ
537 return NULL;
538}
73208dfd
AC
539
540static void
541qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
542{
543 struct qla_hw_data *ha = vha->hw;
544 uint16_t que_id = req->id;
545
546 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
547 sizeof(request_t), req->ring, req->dma);
548 req->ring = NULL;
549 req->dma = 0;
550 if (que_id) {
551 ha->req_q_map[que_id] = NULL;
552 mutex_lock(&ha->vport_lock);
553 clear_bit(que_id, ha->req_qid_map);
554 mutex_unlock(&ha->vport_lock);
555 }
8d93f550 556 kfree(req->outstanding_cmds);
73208dfd
AC
557 kfree(req);
558 req = NULL;
559}
560
561static void
562qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
563{
564 struct qla_hw_data *ha = vha->hw;
565 uint16_t que_id = rsp->id;
566
567 if (rsp->msix && rsp->msix->have_irq) {
d7459527 568 free_irq(rsp->msix->vector, rsp->msix->handle);
73208dfd 569 rsp->msix->have_irq = 0;
d7459527 570 rsp->msix->in_use = 0;
4fa18345 571 rsp->msix->handle = NULL;
73208dfd
AC
572 }
573 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
574 sizeof(response_t), rsp->ring, rsp->dma);
575 rsp->ring = NULL;
576 rsp->dma = 0;
577 if (que_id) {
578 ha->rsp_q_map[que_id] = NULL;
579 mutex_lock(&ha->vport_lock);
580 clear_bit(que_id, ha->rsp_qid_map);
581 mutex_unlock(&ha->vport_lock);
582 }
583 kfree(rsp);
584 rsp = NULL;
585}
586
587int
588qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
589{
7867b98d 590 int ret = QLA_SUCCESS;
73208dfd 591
7867b98d 592 if (req && vha->flags.qpairs_req_created) {
73208dfd 593 req->options |= BIT_0;
618a7523 594 ret = qla25xx_init_req_que(vha, req);
7867b98d 595 if (ret != QLA_SUCCESS)
596 return QLA_FUNCTION_FAILED;
62aa2814
HM
597
598 qla25xx_free_req_que(vha, req);
73208dfd 599 }
73208dfd
AC
600
601 return ret;
602}
603
d7459527 604int
73208dfd
AC
605qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
606{
7867b98d 607 int ret = QLA_SUCCESS;
73208dfd 608
7867b98d 609 if (rsp && vha->flags.qpairs_rsp_created) {
73208dfd 610 rsp->options |= BIT_0;
618a7523 611 ret = qla25xx_init_rsp_que(vha, rsp);
7867b98d 612 if (ret != QLA_SUCCESS)
613 return QLA_FUNCTION_FAILED;
62aa2814
HM
614
615 qla25xx_free_rsp_que(vha, rsp);
73208dfd 616 }
73208dfd
AC
617
618 return ret;
619}
620
73208dfd
AC
621/* Delete all queues for a given vhost */
622int
2afa19a9 623qla25xx_delete_queues(struct scsi_qla_host *vha)
73208dfd
AC
624{
625 int cnt, ret = 0;
626 struct req_que *req = NULL;
627 struct rsp_que *rsp = NULL;
628 struct qla_hw_data *ha = vha->hw;
d7459527 629 struct qla_qpair *qpair, *tqpair;
73208dfd 630
c38d1baf 631 if (ql2xmqsupport || ql2xnvmeenable) {
d7459527
MH
632 list_for_each_entry_safe(qpair, tqpair, &vha->qp_list,
633 qp_list_elem)
634 qla2xxx_delete_qpair(vha, qpair);
635 } else {
636 /* Delete request queues */
637 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
638 req = ha->req_q_map[cnt];
639 if (req && test_bit(cnt, ha->req_qid_map)) {
640 ret = qla25xx_delete_req_que(vha, req);
641 if (ret != QLA_SUCCESS) {
642 ql_log(ql_log_warn, vha, 0x00ea,
643 "Couldn't delete req que %d.\n",
644 req->id);
645 return ret;
646 }
73208dfd 647 }
73208dfd 648 }
2afa19a9 649
d7459527
MH
650 /* Delete response queues */
651 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
652 rsp = ha->rsp_q_map[cnt];
653 if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
654 ret = qla25xx_delete_rsp_que(vha, rsp);
655 if (ret != QLA_SUCCESS) {
656 ql_log(ql_log_warn, vha, 0x00eb,
657 "Couldn't delete rsp que %d.\n",
658 rsp->id);
659 return ret;
660 }
73208dfd
AC
661 }
662 }
663 }
d7459527 664
73208dfd
AC
665 return ret;
666}
667
668int
669qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
82de802a 670 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos, bool startqp)
73208dfd
AC
671{
672 int ret = 0;
673 struct req_que *req = NULL;
674 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
d65237c7 675 struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
73208dfd 676 uint16_t que_id = 0;
f73cb695 677 device_reg_t *reg;
2afa19a9 678 uint32_t cnt;
73208dfd
AC
679
680 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
681 if (req == NULL) {
7c3df132
SK
682 ql_log(ql_log_fatal, base_vha, 0x00d9,
683 "Failed to allocate memory for request queue.\n");
c7922a91 684 goto failed;
73208dfd
AC
685 }
686
687 req->length = REQUEST_ENTRY_CNT_24XX;
688 req->ring = dma_alloc_coherent(&ha->pdev->dev,
689 (req->length + 1) * sizeof(request_t),
690 &req->dma, GFP_KERNEL);
691 if (req->ring == NULL) {
7c3df132 692 ql_log(ql_log_fatal, base_vha, 0x00da,
d6a03581 693 "Failed to allocate memory for request_ring.\n");
73208dfd
AC
694 goto que_failed;
695 }
696
8d93f550
CD
697 ret = qla2x00_alloc_outstanding_cmds(ha, req);
698 if (ret != QLA_SUCCESS)
699 goto que_failed;
700
d7459527 701 mutex_lock(&ha->mq_lock);
2afa19a9
AC
702 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
703 if (que_id >= ha->max_req_queues) {
d7459527 704 mutex_unlock(&ha->mq_lock);
7c3df132
SK
705 ql_log(ql_log_warn, base_vha, 0x00db,
706 "No resources to create additional request queue.\n");
73208dfd
AC
707 goto que_failed;
708 }
709 set_bit(que_id, ha->req_qid_map);
710 ha->req_q_map[que_id] = req;
711 req->rid = rid;
712 req->vp_idx = vp_idx;
713 req->qos = qos;
714
7c3df132
SK
715 ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
716 "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
717 que_id, req->rid, req->vp_idx, req->qos);
718 ql_dbg(ql_dbg_init, base_vha, 0x00dc,
719 "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
720 que_id, req->rid, req->vp_idx, req->qos);
2afa19a9
AC
721 if (rsp_que < 0)
722 req->rsp = NULL;
723 else
73208dfd
AC
724 req->rsp = ha->rsp_q_map[rsp_que];
725 /* Use alternate PCI bus number */
726 if (MSB(req->rid))
727 options |= BIT_4;
728 /* Use alternate PCI devfn */
729 if (LSB(req->rid))
730 options |= BIT_5;
731 req->options = options;
2afa19a9 732
7c3df132
SK
733 ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
734 "options=0x%x.\n", req->options);
735 ql_dbg(ql_dbg_init, base_vha, 0x00dd,
736 "options=0x%x.\n", req->options);
8d93f550 737 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
2afa19a9
AC
738 req->outstanding_cmds[cnt] = NULL;
739 req->current_outstanding_cmd = 1;
740
73208dfd
AC
741 req->ring_ptr = req->ring;
742 req->ring_index = 0;
743 req->cnt = req->length;
744 req->id = que_id;
08029990 745 reg = ISP_QUE_REG(ha, que_id);
da9b1d5c
AV
746 req->req_q_in = &reg->isp25mq.req_q_in;
747 req->req_q_out = &reg->isp25mq.req_q_out;
29bdccbe 748 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
40969530 749 req->out_ptr = (void *)(req->ring + req->length);
d7459527 750 mutex_unlock(&ha->mq_lock);
7c3df132
SK
751 ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
752 "ring_ptr=%p ring_index=%d, "
753 "cnt=%d id=%d max_q_depth=%d.\n",
754 req->ring_ptr, req->ring_index,
755 req->cnt, req->id, req->max_q_depth);
756 ql_dbg(ql_dbg_init, base_vha, 0x00de,
757 "ring_ptr=%p ring_index=%d, "
758 "cnt=%d id=%d max_q_depth=%d.\n",
759 req->ring_ptr, req->ring_index, req->cnt,
760 req->id, req->max_q_depth);
73208dfd 761
82de802a
QT
762 if (startqp) {
763 ret = qla25xx_init_req_que(base_vha, req);
764 if (ret != QLA_SUCCESS) {
765 ql_log(ql_log_fatal, base_vha, 0x00df,
766 "%s failed.\n", __func__);
767 mutex_lock(&ha->mq_lock);
768 clear_bit(que_id, ha->req_qid_map);
769 mutex_unlock(&ha->mq_lock);
770 goto que_failed;
771 }
d65237c7 772 vha->flags.qpairs_req_created = 1;
73208dfd
AC
773 }
774
775 return req->id;
776
777que_failed:
778 qla25xx_free_req_que(base_vha, req);
c7922a91 779failed:
73208dfd
AC
780 return 0;
781}
782
68ca949c
AC
783static void qla_do_work(struct work_struct *work)
784{
a67093d4 785 unsigned long flags;
d7459527 786 struct qla_qpair *qpair = container_of(work, struct qla_qpair, q_work);
68ca949c 787 struct scsi_qla_host *vha;
d7459527 788 struct qla_hw_data *ha = qpair->hw;
68ca949c 789
d7459527 790 spin_lock_irqsave(&qpair->qp_lock, flags);
a67093d4 791 vha = pci_get_drvdata(ha->pdev);
d7459527
MH
792 qla24xx_process_response_queue(vha, qpair->rsp);
793 spin_unlock_irqrestore(&qpair->qp_lock, flags);
cf19c45d 794
68ca949c
AC
795}
796
73208dfd
AC
797/* create response queue */
798int
799qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
82de802a 800 uint8_t vp_idx, uint16_t rid, struct qla_qpair *qpair, bool startqp)
73208dfd
AC
801{
802 int ret = 0;
803 struct rsp_que *rsp = NULL;
804 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
d65237c7 805 struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
08029990 806 uint16_t que_id = 0;
f73cb695 807 device_reg_t *reg;
73208dfd
AC
808
809 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
810 if (rsp == NULL) {
7c3df132
SK
811 ql_log(ql_log_warn, base_vha, 0x0066,
812 "Failed to allocate memory for response queue.\n");
c7922a91 813 goto failed;
73208dfd
AC
814 }
815
2afa19a9 816 rsp->length = RESPONSE_ENTRY_CNT_MQ;
73208dfd
AC
817 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
818 (rsp->length + 1) * sizeof(response_t),
819 &rsp->dma, GFP_KERNEL);
820 if (rsp->ring == NULL) {
7c3df132
SK
821 ql_log(ql_log_warn, base_vha, 0x00e1,
822 "Failed to allocate memory for response ring.\n");
73208dfd
AC
823 goto que_failed;
824 }
825
d7459527 826 mutex_lock(&ha->mq_lock);
2afa19a9
AC
827 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
828 if (que_id >= ha->max_rsp_queues) {
d7459527 829 mutex_unlock(&ha->mq_lock);
7c3df132
SK
830 ql_log(ql_log_warn, base_vha, 0x00e2,
831 "No resources to create additional request queue.\n");
73208dfd
AC
832 goto que_failed;
833 }
834 set_bit(que_id, ha->rsp_qid_map);
835
d7459527 836 rsp->msix = qpair->msix;
73208dfd
AC
837
838 ha->rsp_q_map[que_id] = rsp;
839 rsp->rid = rid;
840 rsp->vp_idx = vp_idx;
841 rsp->hw = ha;
7c3df132 842 ql_dbg(ql_dbg_init, base_vha, 0x00e4,
d7459527 843 "rsp queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
7c3df132 844 que_id, rsp->rid, rsp->vp_idx, rsp->hw);
73208dfd
AC
845 /* Use alternate PCI bus number */
846 if (MSB(rsp->rid))
847 options |= BIT_4;
848 /* Use alternate PCI devfn */
849 if (LSB(rsp->rid))
850 options |= BIT_5;
3155754a
AC
851 /* Enable MSIX handshake mode on for uncapable adapters */
852 if (!IS_MSIX_NACK_CAPABLE(ha))
853 options |= BIT_6;
854
d7459527
MH
855 /* Set option to indicate response queue creation */
856 options |= BIT_1;
857
73208dfd 858 rsp->options = options;
73208dfd 859 rsp->id = que_id;
08029990
AV
860 reg = ISP_QUE_REG(ha, que_id);
861 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
862 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
40969530 863 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
d7459527 864 mutex_unlock(&ha->mq_lock);
7c3df132 865 ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
d7459527 866 "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
7c3df132
SK
867 rsp->options, rsp->id, rsp->rsp_q_in,
868 rsp->rsp_q_out);
869 ql_dbg(ql_dbg_init, base_vha, 0x00e5,
d7459527 870 "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
7c3df132
SK
871 rsp->options, rsp->id, rsp->rsp_q_in,
872 rsp->rsp_q_out);
73208dfd 873
d7459527
MH
874 ret = qla25xx_request_irq(ha, qpair, qpair->msix,
875 QLA_MSIX_QPAIR_MULTIQ_RSP_Q);
73208dfd
AC
876 if (ret)
877 goto que_failed;
878
82de802a
QT
879 if (startqp) {
880 ret = qla25xx_init_rsp_que(base_vha, rsp);
881 if (ret != QLA_SUCCESS) {
882 ql_log(ql_log_fatal, base_vha, 0x00e7,
883 "%s failed.\n", __func__);
884 mutex_lock(&ha->mq_lock);
885 clear_bit(que_id, ha->rsp_qid_map);
886 mutex_unlock(&ha->mq_lock);
887 goto que_failed;
888 }
d65237c7 889 vha->flags.qpairs_rsp_created = 1;
73208dfd 890 }
d7459527 891 rsp->req = NULL;
73208dfd
AC
892
893 qla2x00_init_response_q_entries(rsp);
d7459527
MH
894 if (qpair->hw->wq)
895 INIT_WORK(&qpair->q_work, qla_do_work);
73208dfd
AC
896 return rsp->id;
897
898que_failed:
899 qla25xx_free_rsp_que(base_vha, rsp);
c7922a91 900failed:
73208dfd
AC
901 return 0;
902}
2853192e 903
6c18a43e 904static void qla_ctrlvp_sp_done(srb_t *sp, int res)
2853192e 905{
982cc4be
BVA
906 if (sp->comp)
907 complete(sp->comp);
2853192e
QT
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 */
918int 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);
982cc4be 924 DECLARE_COMPLETION_ONSTACK(comp);
2853192e
QT
925 srb_t *sp;
926
927 ql_dbg(ql_dbg_vport, vha, 0x10c1,
928 "Entered %s cmd %x index %d.\n", __func__, cmd, vp_index);
929
930 if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
931 return QLA_PARAMETER_ERROR;
932
933 sp = qla2x00_get_sp(base_vha, NULL, GFP_KERNEL);
934 if (!sp)
935 goto done;
936
937 sp->type = SRB_CTRL_VP;
938 sp->name = "ctrl_vp";
982cc4be 939 sp->comp = &comp;
2853192e 940 sp->done = qla_ctrlvp_sp_done;
2853192e 941 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
e74e7d95 942 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
2853192e
QT
943 sp->u.iocb_cmd.u.ctrlvp.cmd = cmd;
944 sp->u.iocb_cmd.u.ctrlvp.vp_index = vp_index;
945
946 rval = qla2x00_start_sp(sp);
947 if (rval != QLA_SUCCESS) {
948 ql_dbg(ql_dbg_async, vha, 0xffff,
949 "%s: %s Failed submission. %x.\n",
950 __func__, sp->name, rval);
951 goto done_free_sp;
952 }
953
954 ql_dbg(ql_dbg_vport, vha, 0x113f, "%s hndl %x submitted\n",
955 sp->name, sp->handle);
956
982cc4be
BVA
957 wait_for_completion(&comp);
958 sp->comp = NULL;
959
2853192e
QT
960 rval = sp->rc;
961 switch (rval) {
962 case QLA_FUNCTION_TIMEOUT:
963 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Timeout. %x.\n",
964 __func__, sp->name, rval);
965 break;
966 case QLA_SUCCESS:
967 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s done.\n",
968 __func__, sp->name);
969 goto done_free_sp;
970 default:
971 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Failed. %x.\n",
972 __func__, sp->name, rval);
973 goto done_free_sp;
974 }
975done:
976 return rval;
977
978done_free_sp:
979 sp->free(sp);
980 return rval;
981}