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