blob: 2c1aaf9b7a00c041e1a29a074ea75e14a3114a42 [file] [log] [blame]
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001/*
Andrew Vasquez01e58d82008-04-03 13:13:13 -07002 * QLogic Fibre Channel HBA Driver
Armen Baloyanbd21eaf2014-04-11 16:54:24 -04003 * Copyright (c) 2003-2014 QLogic Corporation
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004 *
Andrew Vasquez01e58d82008-04-03 13:13:13 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006 */
7#include "qla_def.h"
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08008#include "qla_gbl.h"
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04009#include "qla_target.h"
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070010
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070011#include <linux/moduleparam.h>
12#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070014#include <linux/list.h>
15
16#include <scsi/scsi_tcq.h>
17#include <scsi/scsicam.h>
18#include <linux/delay.h>
19
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070020void
21qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
22{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080023 if (vha->vp_idx && vha->timer_active) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070024 del_timer_sync(&vha->timer);
25 vha->timer_active = 0;
26 }
27}
28
Adrian Bunka824ebb2008-01-17 09:02:15 -080029static uint32_t
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070030qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
31{
32 uint32_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080033 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -070034 unsigned long flags;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070035
36 /* Find an empty slot and assign an vp_id */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070037 mutex_lock(&ha->vport_lock);
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080038 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
39 if (vp_id > ha->max_npiv_vports) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -070040 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);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070043 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070044 return vp_id;
45 }
46
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080047 set_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070048 ha->num_vhosts++;
49 vha->vp_idx = vp_id;
Arun Easifeafb7b2010-09-03 14:57:00 -070050
51 spin_lock_irqsave(&ha->vport_slock, flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080052 list_add_tail(&vha->list, &ha->vp_list);
Arun Easifeafb7b2010-09-03 14:57:00 -070053 spin_unlock_irqrestore(&ha->vport_slock, flags);
54
Quinn Tran9d1aa4e2017-12-28 12:33:27 -080055 spin_lock_irqsave(&ha->hardware_lock, flags);
56 qlt_update_vp_map(vha, SET_VP_IDX);
57 spin_unlock_irqrestore(&ha->hardware_lock, flags);
58
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070059 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070060 return vp_id;
61}
62
63void
64qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
65{
66 uint16_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080067 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -070068 unsigned long flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070069
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070070 mutex_lock(&ha->vport_lock);
Arun Easifeafb7b2010-09-03 14:57:00 -070071 /*
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 */
Joe Carnuccio6e980952017-08-23 15:04:55 -070078 wait_event_timeout(vha->vref_waitq, !atomic_read(&vha->vref_count),
Joe Carnuccioc4a9b532017-03-15 09:48:43 -070079 10*HZ);
80
Arun Easifeafb7b2010-09-03 14:57:00 -070081 spin_lock_irqsave(&ha->vport_slock, flags);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -070082 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);
Arun Easifeafb7b2010-09-03 14:57:00 -070086 }
87 list_del(&vha->list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040088 qlt_update_vp_map(vha, RESET_VP_IDX);
Arun Easifeafb7b2010-09-03 14:57:00 -070089 spin_unlock_irqrestore(&ha->vport_slock, flags);
90
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070091 vp_id = vha->vp_idx;
92 ha->num_vhosts--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080093 clear_bit(vp_id, ha->vp_idx_map);
Arun Easifeafb7b2010-09-03 14:57:00 -070094
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070095 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070096}
97
Adrian Bunka824ebb2008-01-17 09:02:15 -080098static scsi_qla_host_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080099qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700100{
101 scsi_qla_host_t *vha;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800102 struct scsi_qla_host *tvha;
Arun Easifeafb7b2010-09-03 14:57:00 -0700103 unsigned long flags;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700104
Arun Easifeafb7b2010-09-03 14:57:00 -0700105 spin_lock_irqsave(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700106 /* Locate matching device in database. */
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800107 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -0700108 if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
109 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700110 return vha;
Arun Easifeafb7b2010-09-03 14:57:00 -0700111 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700112 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700113 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700114 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 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700130static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700131qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
132{
Arun Easifeafb7b2010-09-03 14:57:00 -0700133 /*
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 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700139 fc_port_t *fcport;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700140
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800141 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700142 ql_dbg(ql_dbg_vport, vha, 0xa001,
143 "Marking port dead, loop_id=0x%04x : %x.\n",
Joe Carnuccioc6d39e22012-05-15 14:34:20 -0400144 fcport->loop_id, fcport->vha->vp_idx);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700145
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700146 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Chad Dupuisec426e12011-03-30 11:46:32 -0700147 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700148 }
149}
150
151int
152qla24xx_disable_vp(scsi_qla_host_t *vha)
153{
Nicholas Bellinger29c08cd2012-10-09 12:12:14 -0700154 unsigned long flags;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700155 int ret;
Quinn Tranefa93f42018-07-18 14:29:52 -0700156 fc_port_t *fcport;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700157
158 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
159 atomic_set(&vha->loop_state, LOOP_DOWN);
160 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
Quinn Tranefa93f42018-07-18 14:29:52 -0700161 list_for_each_entry(fcport, &vha->vp_fcports, list)
162 fcport->logout_on_delete = 0;
163
164 qla2x00_mark_all_devices_lost(vha, 0);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700165
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400166 /* Remove port id from vp target map */
Quinn Tran9d1aa4e2017-12-28 12:33:27 -0800167 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400168 qlt_update_vp_map(vha, RESET_AL_PA);
Quinn Tran9d1aa4e2017-12-28 12:33:27 -0800169 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400170
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700171 qla2x00_mark_vp_devices_dead(vha);
172 atomic_set(&vha->vp_state, VP_FAILED);
173 vha->flags.management_server_logged_in = 0;
174 if (ret == QLA_SUCCESS) {
175 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
176 } else {
177 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
178 return -1;
179 }
180 return 0;
181}
182
183int
184qla24xx_enable_vp(scsi_qla_host_t *vha)
185{
186 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800187 struct qla_hw_data *ha = vha->hw;
188 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700189
190 /* Check if physical ha port is Up */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800191 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
Lalit Chandivade3f3b6f92010-05-28 15:08:24 -0700192 atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
193 !(ha->current_topology & ISP_CFG_F)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700194 vha->vp_err_state = VP_ERR_PORTDWN;
195 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
Quinn Tran1608cc42017-08-23 15:05:03 -0700196 ql_dbg(ql_dbg_taskm, vha, 0x800b,
197 "%s skip enable. loop_state %x topo %x\n",
198 __func__, base_vha->loop_state.counter,
199 ha->current_topology);
200
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700201 goto enable_failed;
202 }
203
204 /* Initialize the new vport unless it is a persistent port */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700205 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700206 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700207 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700208
209 if (ret != QLA_SUCCESS) {
210 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
211 goto enable_failed;
212 }
213
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700214 ql_dbg(ql_dbg_taskm, vha, 0x801a,
215 "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700216 return 0;
217
218enable_failed:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700219 ql_dbg(ql_dbg_taskm, vha, 0x801b,
220 "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700221 return 1;
222}
223
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700224static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700225qla24xx_configure_vp(scsi_qla_host_t *vha)
226{
227 struct fc_vport *fc_vport;
228 int ret;
229
230 fc_vport = vha->fc_vport;
231
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700232 ql_dbg(ql_dbg_vport, vha, 0xa002,
233 "%s: change request #3.\n", __func__);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700234 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
235 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700236 ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
237 "receiving of RSCN requests: 0x%x.\n", ret);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700238 return;
239 } else {
240 /* Corresponds to SCR enabled */
241 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
242 }
243
244 vha->flags.online = 1;
245 if (qla24xx_configure_vhba(vha))
246 return;
247
248 atomic_set(&vha->vp_state, VP_ACTIVE);
249 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
250}
251
252void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800253qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700254{
Arun Easifeafb7b2010-09-03 14:57:00 -0700255 scsi_qla_host_t *vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800256 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800257 int i = 0;
Arun Easifeafb7b2010-09-03 14:57:00 -0700258 unsigned long flags;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700259
Arun Easifeafb7b2010-09-03 14:57:00 -0700260 spin_lock_irqsave(&ha->vport_slock, flags);
261 list_for_each_entry(vha, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800262 if (vha->vp_idx) {
Arun Easifeafb7b2010-09-03 14:57:00 -0700263 atomic_inc(&vha->vref_count);
264 spin_unlock_irqrestore(&ha->vport_slock, flags);
265
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700266 switch (mb[0]) {
267 case MBA_LIP_OCCURRED:
268 case MBA_LOOP_UP:
269 case MBA_LOOP_DOWN:
270 case MBA_LIP_RESET:
271 case MBA_POINT_TO_POINT:
272 case MBA_CHG_IN_CONNECTION:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700273 ql_dbg(ql_dbg_async, vha, 0x5024,
274 "Async_event for VP[%d], mb=0x%x vha=%p.\n",
275 i, *mb, vha);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800276 qla2x00_async_event(vha, rsp, mb);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700277 break;
Quinn Tran75061752017-12-28 12:33:29 -0800278 case MBA_PORT_UPDATE:
279 case MBA_RSCN_UPDATE:
280 if ((mb[3] & 0xff) == vha->vp_idx) {
281 ql_dbg(ql_dbg_async, vha, 0x5024,
282 "Async_event for VP[%d], mb=0x%x vha=%p\n",
283 i, *mb, vha);
284 qla2x00_async_event(vha, rsp, mb);
285 }
286 break;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700287 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700288
289 spin_lock_irqsave(&ha->vport_slock, flags);
290 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -0700291 wake_up(&vha->vref_waitq);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700292 }
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800293 i++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700294 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700295 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700296}
297
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800298int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700299qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
300{
301 /*
302 * Physical port will do most of the abort and recovery work. We can
303 * just treat it as a loop down
304 */
305 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
306 atomic_set(&vha->loop_state, LOOP_DOWN);
307 qla2x00_mark_all_devices_lost(vha, 0);
308 } else {
309 if (!atomic_read(&vha->loop_down_timer))
310 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
311 }
312
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700313 /*
314 * To exclusively reset vport, we need to log it out first. Note: this
315 * control_vp can fail if ISP reset is already issued, this is
316 * expected, as the vp would be already logged out due to ISP reset.
317 */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800318 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
319 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
320
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700321 ql_dbg(ql_dbg_taskm, vha, 0x801d,
322 "Scheduling enable of Vport %d.\n", vha->vp_idx);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800323 return qla24xx_enable_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700324}
325
Adrian Bunka824ebb2008-01-17 09:02:15 -0800326static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700327qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
328{
Sawan Chandakded64112015-04-09 15:00:06 -0400329 struct qla_hw_data *ha = vha->hw;
330 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
331
Saurav Kashyap5f28d2d2012-05-15 14:34:15 -0400332 ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
333 "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700334
Sawan Chandakded64112015-04-09 15:00:06 -0400335 /* Check if Fw is ready to configure VP first */
336 if (test_bit(VP_CONFIG_OK, &base_vha->vp_flags)) {
337 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
338 /* VP acquired. complete port configuration */
339 ql_dbg(ql_dbg_dpc, vha, 0x4014,
340 "Configure VP scheduled.\n");
341 qla24xx_configure_vp(vha);
342 ql_dbg(ql_dbg_dpc, vha, 0x4015,
343 "Configure VP end.\n");
344 return 0;
345 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700346 }
347
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800348 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700349 ql_dbg(ql_dbg_dpc, vha, 0x4016,
350 "FCPort update scheduled.\n");
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800351 qla2x00_update_fcports(vha);
352 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700353 ql_dbg(ql_dbg_dpc, vha, 0x4017,
354 "FCPort update end.\n");
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800355 }
356
Quinn Tran4005a992017-12-04 14:45:06 -0800357 if (test_bit(RELOGIN_NEEDED, &vha->dpc_flags) &&
358 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
359 atomic_read(&vha->loop_state) != LOOP_DOWN) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800360
Quinn Tran4005a992017-12-04 14:45:06 -0800361 if (!vha->relogin_jif ||
362 time_after_eq(jiffies, vha->relogin_jif)) {
363 vha->relogin_jif = jiffies + HZ;
364 clear_bit(RELOGIN_NEEDED, &vha->dpc_flags);
365
366 ql_dbg(ql_dbg_dpc, vha, 0x4018,
367 "Relogin needed scheduled.\n");
Quinn Tran9b3e0f42017-12-28 12:33:16 -0800368 qla24xx_post_relogin_work(vha);
Quinn Tran4005a992017-12-04 14:45:06 -0800369 }
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800370 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700371
372 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
373 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
374 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
375 }
376
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800377 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700378 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700379 ql_dbg(ql_dbg_dpc, vha, 0x401a,
380 "Loop resync scheduled.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700381 qla2x00_loop_resync(vha);
382 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700383 ql_dbg(ql_dbg_dpc, vha, 0x401b,
384 "Loop resync end.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700385 }
386 }
387
Saurav Kashyap5f28d2d2012-05-15 14:34:15 -0400388 ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700389 "Exiting %s.\n", __func__);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700390 return 0;
391}
392
393void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800394qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700395{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800396 struct qla_hw_data *ha = vha->hw;
397 scsi_qla_host_t *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -0700398 unsigned long flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700399
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800400 if (vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700401 return;
402 if (list_empty(&ha->vp_list))
403 return;
404
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800405 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700406
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700407 if (!(ha->current_topology & ISP_CFG_F))
408 return;
409
Arun Easifeafb7b2010-09-03 14:57:00 -0700410 spin_lock_irqsave(&ha->vport_slock, flags);
411 list_for_each_entry(vp, &ha->vp_list, list) {
412 if (vp->vp_idx) {
413 atomic_inc(&vp->vref_count);
414 spin_unlock_irqrestore(&ha->vport_slock, flags);
415
Bart Van Assche52c82822015-07-09 07:23:26 -0700416 qla2x00_do_dpc_vp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -0700417
418 spin_lock_irqsave(&ha->vport_slock, flags);
419 atomic_dec(&vp->vref_count);
420 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700421 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700422 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700423}
424
425int
426qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
427{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800428 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
429 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700430 scsi_qla_host_t *vha;
431 uint8_t port_name[WWN_SIZE];
432
433 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
434 return VPCERR_UNSUPPORTED;
435
436 /* Check up the F/W and H/W support NPIV */
437 if (!ha->flags.npiv_supported)
438 return VPCERR_UNSUPPORTED;
439
440 /* Check up whether npiv supported switch presented */
441 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
442 return VPCERR_NO_FABRIC_SUPP;
443
444 /* Check up unique WWPN */
445 u64_to_wwn(fc_vport->port_name, port_name);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800446 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
Seokmann Ju50db6b12008-01-17 09:02:14 -0800447 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700448 vha = qla24xx_find_vhost_by_name(ha, port_name);
449 if (vha)
450 return VPCERR_BAD_WWN;
451
452 /* Check up max-npiv-supports */
453 if (ha->num_vhosts > ha->max_npiv_vports) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700454 ql_dbg(ql_dbg_vport, vha, 0xa004,
455 "num_vhosts %ud is bigger "
456 "than max_npiv_vports %ud.\n",
457 ha->num_vhosts, ha->max_npiv_vports);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700458 return VPCERR_UNSUPPORTED;
459 }
460 return 0;
461}
462
463scsi_qla_host_t *
464qla24xx_create_vhost(struct fc_vport *fc_vport)
465{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800466 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
467 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700468 scsi_qla_host_t *vha;
Giridhar Malavalia5326f82009-03-24 09:07:56 -0700469 struct scsi_host_template *sht = &qla2xxx_driver_template;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700470 struct Scsi_Host *host;
471
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800472 vha = qla2x00_create_host(sht, ha);
473 if (!vha) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700474 ql_log(ql_log_warn, vha, 0xa005,
475 "scsi_host_alloc() failed for vport.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700476 return(NULL);
477 }
478
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800479 host = vha->host;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700480 fc_vport->dd_data = vha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700481 /* New host info */
482 u64_to_wwn(fc_vport->node_name, vha->node_name);
483 u64_to_wwn(fc_vport->port_name, vha->port_name);
484
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700485 vha->fc_vport = fc_vport;
486 vha->device_flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700487 vha->vp_idx = qla24xx_allocate_vp_id(vha);
488 if (vha->vp_idx > ha->max_npiv_vports) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700489 ql_dbg(ql_dbg_vport, vha, 0xa006,
490 "Couldn't allocate vp_id.\n");
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800491 goto create_vhost_failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700492 }
himanshu.madhani@cavium.com1429f042017-12-28 12:33:25 -0800493 vha->mgmt_svr_loop_id = NPH_MGMT_SERVER;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700494
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700495 vha->dpc_flags = 0L;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700496
497 /*
498 * To fix the issue of processing a parent's RSCN for the vport before
499 * its SCR is complete.
500 */
501 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
502 atomic_set(&vha->loop_state, LOOP_DOWN);
503 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
504
Kees Cook8e5f4ba2017-09-03 13:23:32 -0700505 qla2x00_start_timer(vha, WATCH_INTERVAL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700506
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700507 vha->req = base_vha->req;
508 host->can_queue = base_vha->req->length + 128;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700509 host->cmd_per_lun = 3;
Arun Easie02587d2011-08-16 11:29:23 -0700510 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
Arun Easi0c470872010-07-23 15:28:38 +0500511 host->max_cmd_len = 32;
512 else
513 host->max_cmd_len = MAX_CMDSZ;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700514 host->max_channel = MAX_BUSES - 1;
Andrew Vasquez82515922011-05-10 11:30:13 -0700515 host->max_lun = ql2xmaxlun;
Seokmann Ju711c1d92008-07-10 16:55:51 -0700516 host->unique_id = host->host_no;
Chad Dupuis642ef982012-02-09 11:15:57 -0800517 host->max_id = ha->max_fibre_devices;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700518 host->transportt = qla2xxx_transport_vport_template;
519
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700520 ql_dbg(ql_dbg_vport, vha, 0xa007,
521 "Detect vport hba %ld at address = %p.\n",
522 vha->host_no, vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700523
524 vha->flags.init_done = 1;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700525
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700526 mutex_lock(&ha->vport_lock);
527 set_bit(vha->vp_idx, ha->vp_idx_map);
528 ha->cur_vport_count++;
529 mutex_unlock(&ha->vport_lock);
530
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700531 return vha;
532
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800533create_vhost_failed:
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700534 return NULL;
535}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800536
537static void
538qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
539{
540 struct qla_hw_data *ha = vha->hw;
541 uint16_t que_id = req->id;
542
543 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
544 sizeof(request_t), req->ring, req->dma);
545 req->ring = NULL;
546 req->dma = 0;
547 if (que_id) {
548 ha->req_q_map[que_id] = NULL;
549 mutex_lock(&ha->vport_lock);
550 clear_bit(que_id, ha->req_qid_map);
551 mutex_unlock(&ha->vport_lock);
552 }
Chad Dupuis8d93f552013-01-30 03:34:37 -0500553 kfree(req->outstanding_cmds);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800554 kfree(req);
555 req = NULL;
556}
557
558static void
559qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
560{
561 struct qla_hw_data *ha = vha->hw;
562 uint16_t que_id = rsp->id;
563
564 if (rsp->msix && rsp->msix->have_irq) {
Michael Hernandezd7459522016-12-12 14:40:07 -0800565 free_irq(rsp->msix->vector, rsp->msix->handle);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800566 rsp->msix->have_irq = 0;
Michael Hernandezd7459522016-12-12 14:40:07 -0800567 rsp->msix->in_use = 0;
Michael Hernandez4fa18342016-12-12 14:40:06 -0800568 rsp->msix->handle = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800569 }
570 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
571 sizeof(response_t), rsp->ring, rsp->dma);
572 rsp->ring = NULL;
573 rsp->dma = 0;
574 if (que_id) {
575 ha->rsp_q_map[que_id] = NULL;
576 mutex_lock(&ha->vport_lock);
577 clear_bit(que_id, ha->rsp_qid_map);
578 mutex_unlock(&ha->vport_lock);
579 }
580 kfree(rsp);
581 rsp = NULL;
582}
583
584int
585qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
586{
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -0800587 int ret = QLA_SUCCESS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800588
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -0800589 if (req && vha->flags.qpairs_req_created) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800590 req->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800591 ret = qla25xx_init_req_que(vha, req);
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -0800592 if (ret != QLA_SUCCESS)
593 return QLA_FUNCTION_FAILED;
Himanshu Madhani62aa2812017-12-16 16:05:09 -0800594
595 qla25xx_free_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800596 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800597
598 return ret;
599}
600
Michael Hernandezd7459522016-12-12 14:40:07 -0800601int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800602qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
603{
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -0800604 int ret = QLA_SUCCESS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800605
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -0800606 if (rsp && vha->flags.qpairs_rsp_created) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800607 rsp->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800608 ret = qla25xx_init_rsp_que(vha, rsp);
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -0800609 if (ret != QLA_SUCCESS)
610 return QLA_FUNCTION_FAILED;
Himanshu Madhani62aa2812017-12-16 16:05:09 -0800611
612 qla25xx_free_rsp_que(vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800613 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800614
615 return ret;
616}
617
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800618/* Delete all queues for a given vhost */
619int
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700620qla25xx_delete_queues(struct scsi_qla_host *vha)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800621{
622 int cnt, ret = 0;
623 struct req_que *req = NULL;
624 struct rsp_que *rsp = NULL;
625 struct qla_hw_data *ha = vha->hw;
Michael Hernandezd7459522016-12-12 14:40:07 -0800626 struct qla_qpair *qpair, *tqpair;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800627
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -0700628 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -0800629 list_for_each_entry_safe(qpair, tqpair, &vha->qp_list,
630 qp_list_elem)
631 qla2xxx_delete_qpair(vha, qpair);
632 } else {
633 /* Delete request queues */
634 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
635 req = ha->req_q_map[cnt];
636 if (req && test_bit(cnt, ha->req_qid_map)) {
637 ret = qla25xx_delete_req_que(vha, req);
638 if (ret != QLA_SUCCESS) {
639 ql_log(ql_log_warn, vha, 0x00ea,
640 "Couldn't delete req que %d.\n",
641 req->id);
642 return ret;
643 }
644 }
645 }
646
647 /* Delete response queues */
648 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
649 rsp = ha->rsp_q_map[cnt];
650 if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
651 ret = qla25xx_delete_rsp_que(vha, rsp);
652 if (ret != QLA_SUCCESS) {
653 ql_log(ql_log_warn, vha, 0x00eb,
654 "Couldn't delete rsp que %d.\n",
655 rsp->id);
656 return ret;
657 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800658 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800659 }
660 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700661
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800662 return ret;
663}
664
665int
666qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
Quinn Tran82de8022017-06-13 20:47:17 -0700667 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos, bool startqp)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800668{
669 int ret = 0;
670 struct req_que *req = NULL;
671 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Sawan Chandakd65237c2017-06-13 20:47:19 -0700672 struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800673 uint16_t que_id = 0;
Chad Dupuisf73cb692014-02-26 04:15:06 -0500674 device_reg_t *reg;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700675 uint32_t cnt;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800676
677 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
678 if (req == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700679 ql_log(ql_log_fatal, base_vha, 0x00d9,
680 "Failed to allocate memory for request queue.\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700681 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800682 }
683
684 req->length = REQUEST_ENTRY_CNT_24XX;
685 req->ring = dma_alloc_coherent(&ha->pdev->dev,
686 (req->length + 1) * sizeof(request_t),
687 &req->dma, GFP_KERNEL);
688 if (req->ring == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700689 ql_log(ql_log_fatal, base_vha, 0x00da,
Masanari Iidad6a03582012-08-22 14:20:58 -0400690 "Failed to allocate memory for request_ring.\n");
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800691 goto que_failed;
692 }
693
Chad Dupuis8d93f552013-01-30 03:34:37 -0500694 ret = qla2x00_alloc_outstanding_cmds(ha, req);
695 if (ret != QLA_SUCCESS)
696 goto que_failed;
697
Michael Hernandezd7459522016-12-12 14:40:07 -0800698 mutex_lock(&ha->mq_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700699 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
700 if (que_id >= ha->max_req_queues) {
Michael Hernandezd7459522016-12-12 14:40:07 -0800701 mutex_unlock(&ha->mq_lock);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700702 ql_log(ql_log_warn, base_vha, 0x00db,
703 "No resources to create additional request queue.\n");
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800704 goto que_failed;
705 }
706 set_bit(que_id, ha->req_qid_map);
707 ha->req_q_map[que_id] = req;
708 req->rid = rid;
709 req->vp_idx = vp_idx;
710 req->qos = qos;
711
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700712 ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
713 "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
714 que_id, req->rid, req->vp_idx, req->qos);
715 ql_dbg(ql_dbg_init, base_vha, 0x00dc,
716 "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
717 que_id, req->rid, req->vp_idx, req->qos);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700718 if (rsp_que < 0)
719 req->rsp = NULL;
720 else
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800721 req->rsp = ha->rsp_q_map[rsp_que];
722 /* Use alternate PCI bus number */
723 if (MSB(req->rid))
724 options |= BIT_4;
725 /* Use alternate PCI devfn */
726 if (LSB(req->rid))
727 options |= BIT_5;
728 req->options = options;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700729
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700730 ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
731 "options=0x%x.\n", req->options);
732 ql_dbg(ql_dbg_init, base_vha, 0x00dd,
733 "options=0x%x.\n", req->options);
Chad Dupuis8d93f552013-01-30 03:34:37 -0500734 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700735 req->outstanding_cmds[cnt] = NULL;
736 req->current_outstanding_cmd = 1;
737
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800738 req->ring_ptr = req->ring;
739 req->ring_index = 0;
740 req->cnt = req->length;
741 req->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700742 reg = ISP_QUE_REG(ha, que_id);
Andrew Vasquezda9b1d52013-08-27 01:37:30 -0400743 req->req_q_in = &reg->isp25mq.req_q_in;
744 req->req_q_out = &reg->isp25mq.req_q_out;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -0800745 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
Joe Carnuccio40969532014-09-25 05:16:39 -0400746 req->out_ptr = (void *)(req->ring + req->length);
Michael Hernandezd7459522016-12-12 14:40:07 -0800747 mutex_unlock(&ha->mq_lock);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700748 ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
749 "ring_ptr=%p ring_index=%d, "
750 "cnt=%d id=%d max_q_depth=%d.\n",
751 req->ring_ptr, req->ring_index,
752 req->cnt, req->id, req->max_q_depth);
753 ql_dbg(ql_dbg_init, base_vha, 0x00de,
754 "ring_ptr=%p ring_index=%d, "
755 "cnt=%d id=%d max_q_depth=%d.\n",
756 req->ring_ptr, req->ring_index, req->cnt,
757 req->id, req->max_q_depth);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800758
Quinn Tran82de8022017-06-13 20:47:17 -0700759 if (startqp) {
760 ret = qla25xx_init_req_que(base_vha, req);
761 if (ret != QLA_SUCCESS) {
762 ql_log(ql_log_fatal, base_vha, 0x00df,
763 "%s failed.\n", __func__);
764 mutex_lock(&ha->mq_lock);
765 clear_bit(que_id, ha->req_qid_map);
766 mutex_unlock(&ha->mq_lock);
767 goto que_failed;
768 }
Sawan Chandakd65237c2017-06-13 20:47:19 -0700769 vha->flags.qpairs_req_created = 1;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800770 }
771
772 return req->id;
773
774que_failed:
775 qla25xx_free_req_que(base_vha, req);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700776failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800777 return 0;
778}
779
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700780static void qla_do_work(struct work_struct *work)
781{
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800782 unsigned long flags;
Michael Hernandezd7459522016-12-12 14:40:07 -0800783 struct qla_qpair *qpair = container_of(work, struct qla_qpair, q_work);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700784 struct scsi_qla_host *vha;
Michael Hernandezd7459522016-12-12 14:40:07 -0800785 struct qla_hw_data *ha = qpair->hw;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700786
Michael Hernandezd7459522016-12-12 14:40:07 -0800787 spin_lock_irqsave(&qpair->qp_lock, flags);
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800788 vha = pci_get_drvdata(ha->pdev);
Michael Hernandezd7459522016-12-12 14:40:07 -0800789 qla24xx_process_response_queue(vha, qpair->rsp);
790 spin_unlock_irqrestore(&qpair->qp_lock, flags);
Duane Grigsbycf19c452017-08-23 15:04:58 -0700791
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700792}
793
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800794/* create response queue */
795int
796qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
Quinn Tran82de8022017-06-13 20:47:17 -0700797 uint8_t vp_idx, uint16_t rid, struct qla_qpair *qpair, bool startqp)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800798{
799 int ret = 0;
800 struct rsp_que *rsp = NULL;
801 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Sawan Chandakd65237c2017-06-13 20:47:19 -0700802 struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
Andrew Vasquez08029992009-03-24 09:07:55 -0700803 uint16_t que_id = 0;
Chad Dupuisf73cb692014-02-26 04:15:06 -0500804 device_reg_t *reg;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800805
806 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
807 if (rsp == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700808 ql_log(ql_log_warn, base_vha, 0x0066,
809 "Failed to allocate memory for response queue.\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700810 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800811 }
812
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700813 rsp->length = RESPONSE_ENTRY_CNT_MQ;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800814 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
815 (rsp->length + 1) * sizeof(response_t),
816 &rsp->dma, GFP_KERNEL);
817 if (rsp->ring == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700818 ql_log(ql_log_warn, base_vha, 0x00e1,
819 "Failed to allocate memory for response ring.\n");
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800820 goto que_failed;
821 }
822
Michael Hernandezd7459522016-12-12 14:40:07 -0800823 mutex_lock(&ha->mq_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700824 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
825 if (que_id >= ha->max_rsp_queues) {
Michael Hernandezd7459522016-12-12 14:40:07 -0800826 mutex_unlock(&ha->mq_lock);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700827 ql_log(ql_log_warn, base_vha, 0x00e2,
828 "No resources to create additional request queue.\n");
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800829 goto que_failed;
830 }
831 set_bit(que_id, ha->rsp_qid_map);
832
Michael Hernandezd7459522016-12-12 14:40:07 -0800833 rsp->msix = qpair->msix;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800834
835 ha->rsp_q_map[que_id] = rsp;
836 rsp->rid = rid;
837 rsp->vp_idx = vp_idx;
838 rsp->hw = ha;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700839 ql_dbg(ql_dbg_init, base_vha, 0x00e4,
Michael Hernandezd7459522016-12-12 14:40:07 -0800840 "rsp queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700841 que_id, rsp->rid, rsp->vp_idx, rsp->hw);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800842 /* Use alternate PCI bus number */
843 if (MSB(rsp->rid))
844 options |= BIT_4;
845 /* Use alternate PCI devfn */
846 if (LSB(rsp->rid))
847 options |= BIT_5;
Anirban Chakraborty31557542009-12-02 10:36:55 -0800848 /* Enable MSIX handshake mode on for uncapable adapters */
849 if (!IS_MSIX_NACK_CAPABLE(ha))
850 options |= BIT_6;
851
Michael Hernandezd7459522016-12-12 14:40:07 -0800852 /* Set option to indicate response queue creation */
853 options |= BIT_1;
854
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800855 rsp->options = options;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800856 rsp->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700857 reg = ISP_QUE_REG(ha, que_id);
858 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
859 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
Joe Carnuccio40969532014-09-25 05:16:39 -0400860 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
Michael Hernandezd7459522016-12-12 14:40:07 -0800861 mutex_unlock(&ha->mq_lock);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700862 ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
Michael Hernandezd7459522016-12-12 14:40:07 -0800863 "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700864 rsp->options, rsp->id, rsp->rsp_q_in,
865 rsp->rsp_q_out);
866 ql_dbg(ql_dbg_init, base_vha, 0x00e5,
Michael Hernandezd7459522016-12-12 14:40:07 -0800867 "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700868 rsp->options, rsp->id, rsp->rsp_q_in,
869 rsp->rsp_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800870
Michael Hernandezd7459522016-12-12 14:40:07 -0800871 ret = qla25xx_request_irq(ha, qpair, qpair->msix,
872 QLA_MSIX_QPAIR_MULTIQ_RSP_Q);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800873 if (ret)
874 goto que_failed;
875
Quinn Tran82de8022017-06-13 20:47:17 -0700876 if (startqp) {
877 ret = qla25xx_init_rsp_que(base_vha, rsp);
878 if (ret != QLA_SUCCESS) {
879 ql_log(ql_log_fatal, base_vha, 0x00e7,
880 "%s failed.\n", __func__);
881 mutex_lock(&ha->mq_lock);
882 clear_bit(que_id, ha->rsp_qid_map);
883 mutex_unlock(&ha->mq_lock);
884 goto que_failed;
885 }
Sawan Chandakd65237c2017-06-13 20:47:19 -0700886 vha->flags.qpairs_rsp_created = 1;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800887 }
Michael Hernandezd7459522016-12-12 14:40:07 -0800888 rsp->req = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800889
890 qla2x00_init_response_q_entries(rsp);
Michael Hernandezd7459522016-12-12 14:40:07 -0800891 if (qpair->hw->wq)
892 INIT_WORK(&qpair->q_work, qla_do_work);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800893 return rsp->id;
894
895que_failed:
896 qla25xx_free_rsp_que(base_vha, rsp);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700897failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800898 return 0;
899}
Quinn Tran28531922017-12-28 12:33:10 -0800900
901static void qla_ctrlvp_sp_done(void *s, int res)
902{
903 struct srb *sp = s;
904
905 complete(&sp->comp);
906 /* don't free sp here. Let the caller do the free */
907}
908
909/**
910 * qla24xx_control_vp() - Enable a virtual port for given host
911 * @vha: adapter block pointer
912 * @cmd: command type to be sent for enable virtual port
913 *
914 * Return: qla2xxx local function return status code.
915 */
916int qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
917{
918 int rval = QLA_MEMORY_ALLOC_FAILED;
919 struct qla_hw_data *ha = vha->hw;
920 int vp_index = vha->vp_idx;
921 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
922 srb_t *sp;
923
924 ql_dbg(ql_dbg_vport, vha, 0x10c1,
925 "Entered %s cmd %x index %d.\n", __func__, cmd, vp_index);
926
927 if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
928 return QLA_PARAMETER_ERROR;
929
930 sp = qla2x00_get_sp(base_vha, NULL, GFP_KERNEL);
931 if (!sp)
932 goto done;
933
934 sp->type = SRB_CTRL_VP;
935 sp->name = "ctrl_vp";
936 sp->done = qla_ctrlvp_sp_done;
Quinn Tran28531922017-12-28 12:33:10 -0800937 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000938 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
Quinn Tran28531922017-12-28 12:33:10 -0800939 sp->u.iocb_cmd.u.ctrlvp.cmd = cmd;
940 sp->u.iocb_cmd.u.ctrlvp.vp_index = vp_index;
941
942 rval = qla2x00_start_sp(sp);
943 if (rval != QLA_SUCCESS) {
944 ql_dbg(ql_dbg_async, vha, 0xffff,
945 "%s: %s Failed submission. %x.\n",
946 __func__, sp->name, rval);
947 goto done_free_sp;
948 }
949
950 ql_dbg(ql_dbg_vport, vha, 0x113f, "%s hndl %x submitted\n",
951 sp->name, sp->handle);
952
953 wait_for_completion(&sp->comp);
954 rval = sp->rc;
955 switch (rval) {
956 case QLA_FUNCTION_TIMEOUT:
957 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Timeout. %x.\n",
958 __func__, sp->name, rval);
959 break;
960 case QLA_SUCCESS:
961 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s done.\n",
962 __func__, sp->name);
963 goto done_free_sp;
964 default:
965 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Failed. %x.\n",
966 __func__, sp->name, rval);
967 goto done_free_sp;
968 }
969done:
970 return rval;
971
972done_free_sp:
973 sp->free(sp);
974 return rval;
975}