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