blob: 5e343919acad791ccc8879b4b98cce37e92021d8 [file] [log] [blame]
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001/*
Andrew Vasquez01e58d82008-04-03 13:13:13 -07002 * QLogic Fibre Channel HBA Driver
Andrew Vasquez07e264b2011-03-30 11:46:23 -07003 * Copyright (c) 2003-2011 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"
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07009
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070010#include <linux/moduleparam.h>
11#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070013#include <linux/list.h>
14
15#include <scsi/scsi_tcq.h>
16#include <scsi/scsicam.h>
17#include <linux/delay.h>
18
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070019void
20qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
21{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080022 if (vha->vp_idx && vha->timer_active) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070023 del_timer_sync(&vha->timer);
24 vha->timer_active = 0;
25 }
26}
27
Adrian Bunka824ebb2008-01-17 09:02:15 -080028static uint32_t
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070029qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
30{
31 uint32_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080032 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -070033 unsigned long flags;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070034
35 /* Find an empty slot and assign an vp_id */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070036 mutex_lock(&ha->vport_lock);
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080037 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
38 if (vp_id > ha->max_npiv_vports) {
39 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
40 vp_id, ha->max_npiv_vports));
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070041 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070042 return vp_id;
43 }
44
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080045 set_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070046 ha->num_vhosts++;
47 vha->vp_idx = vp_id;
Arun Easifeafb7b2010-09-03 14:57:00 -070048
49 spin_lock_irqsave(&ha->vport_slock, flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080050 list_add_tail(&vha->list, &ha->vp_list);
Arun Easifeafb7b2010-09-03 14:57:00 -070051 spin_unlock_irqrestore(&ha->vport_slock, flags);
52
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070053 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070054 return vp_id;
55}
56
57void
58qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
59{
60 uint16_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080061 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -070062 unsigned long flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070063
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070064 mutex_lock(&ha->vport_lock);
Arun Easifeafb7b2010-09-03 14:57:00 -070065 /*
66 * Wait for all pending activities to finish before removing vport from
67 * the list.
68 * Lock needs to be held for safe removal from the list (it
69 * ensures no active vp_list traversal while the vport is removed
70 * from the queue)
71 */
72 spin_lock_irqsave(&ha->vport_slock, flags);
73 while (atomic_read(&vha->vref_count)) {
74 spin_unlock_irqrestore(&ha->vport_slock, flags);
75
76 msleep(500);
77
78 spin_lock_irqsave(&ha->vport_slock, flags);
79 }
80 list_del(&vha->list);
81 spin_unlock_irqrestore(&ha->vport_slock, flags);
82
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070083 vp_id = vha->vp_idx;
84 ha->num_vhosts--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080085 clear_bit(vp_id, ha->vp_idx_map);
Arun Easifeafb7b2010-09-03 14:57:00 -070086
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070087 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070088}
89
Adrian Bunka824ebb2008-01-17 09:02:15 -080090static scsi_qla_host_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080091qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070092{
93 scsi_qla_host_t *vha;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080094 struct scsi_qla_host *tvha;
Arun Easifeafb7b2010-09-03 14:57:00 -070095 unsigned long flags;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070096
Arun Easifeafb7b2010-09-03 14:57:00 -070097 spin_lock_irqsave(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070098 /* Locate matching device in database. */
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080099 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -0700100 if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
101 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700102 return vha;
Arun Easifeafb7b2010-09-03 14:57:00 -0700103 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700104 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700105 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700106 return NULL;
107}
108
109/*
110 * qla2x00_mark_vp_devices_dead
111 * Updates fcport state when device goes offline.
112 *
113 * Input:
114 * ha = adapter block pointer.
115 * fcport = port structure pointer.
116 *
117 * Return:
118 * None.
119 *
120 * Context:
121 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700122static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700123qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
124{
Arun Easifeafb7b2010-09-03 14:57:00 -0700125 /*
126 * !!! NOTE !!!
127 * This function, if called in contexts other than vp create, disable
128 * or delete, please make sure this is synchronized with the
129 * delete thread.
130 */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700131 fc_port_t *fcport;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700132
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800133 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700134 DEBUG15(printk("scsi(%ld): Marking port dead, "
135 "loop_id=0x%04x :%x\n",
136 vha->host_no, fcport->loop_id, fcport->vp_idx));
137
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700138 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Chad Dupuisec426e12011-03-30 11:46:32 -0700139 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700140 }
141}
142
143int
144qla24xx_disable_vp(scsi_qla_host_t *vha)
145{
146 int ret;
147
148 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
149 atomic_set(&vha->loop_state, LOOP_DOWN);
150 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
151
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700152 qla2x00_mark_vp_devices_dead(vha);
153 atomic_set(&vha->vp_state, VP_FAILED);
154 vha->flags.management_server_logged_in = 0;
155 if (ret == QLA_SUCCESS) {
156 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
157 } else {
158 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
159 return -1;
160 }
161 return 0;
162}
163
164int
165qla24xx_enable_vp(scsi_qla_host_t *vha)
166{
167 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800168 struct qla_hw_data *ha = vha->hw;
169 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700170
171 /* Check if physical ha port is Up */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800172 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
Lalit Chandivade3f3b6f92010-05-28 15:08:24 -0700173 atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
174 !(ha->current_topology & ISP_CFG_F)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700175 vha->vp_err_state = VP_ERR_PORTDWN;
176 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
177 goto enable_failed;
178 }
179
180 /* Initialize the new vport unless it is a persistent port */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700181 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700182 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700183 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700184
185 if (ret != QLA_SUCCESS) {
186 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
187 goto enable_failed;
188 }
189
190 DEBUG15(qla_printk(KERN_INFO, ha,
191 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
192 return 0;
193
194enable_failed:
195 DEBUG15(qla_printk(KERN_INFO, ha,
196 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
197 return 1;
198}
199
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700200static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700201qla24xx_configure_vp(scsi_qla_host_t *vha)
202{
203 struct fc_vport *fc_vport;
204 int ret;
205
206 fc_vport = vha->fc_vport;
207
208 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
209 vha->host_no, __func__));
210 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
211 if (ret != QLA_SUCCESS) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800212 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
213 "receiving of RSCN requests: 0x%x\n", ret));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700214 return;
215 } else {
216 /* Corresponds to SCR enabled */
217 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
218 }
219
220 vha->flags.online = 1;
221 if (qla24xx_configure_vhba(vha))
222 return;
223
224 atomic_set(&vha->vp_state, VP_ACTIVE);
225 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
226}
227
228void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800229qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700230{
Arun Easifeafb7b2010-09-03 14:57:00 -0700231 scsi_qla_host_t *vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800232 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800233 int i = 0;
Arun Easifeafb7b2010-09-03 14:57:00 -0700234 unsigned long flags;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700235
Arun Easifeafb7b2010-09-03 14:57:00 -0700236 spin_lock_irqsave(&ha->vport_slock, flags);
237 list_for_each_entry(vha, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800238 if (vha->vp_idx) {
Arun Easifeafb7b2010-09-03 14:57:00 -0700239 atomic_inc(&vha->vref_count);
240 spin_unlock_irqrestore(&ha->vport_slock, flags);
241
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700242 switch (mb[0]) {
243 case MBA_LIP_OCCURRED:
244 case MBA_LOOP_UP:
245 case MBA_LOOP_DOWN:
246 case MBA_LIP_RESET:
247 case MBA_POINT_TO_POINT:
248 case MBA_CHG_IN_CONNECTION:
249 case MBA_PORT_UPDATE:
250 case MBA_RSCN_UPDATE:
251 DEBUG15(printk("scsi(%ld)%s: Async_event for"
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800252 " VP[%d], mb = 0x%x, vha=%p\n",
253 vha->host_no, __func__, i, *mb, vha));
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800254 qla2x00_async_event(vha, rsp, mb);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700255 break;
256 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700257
258 spin_lock_irqsave(&ha->vport_slock, flags);
259 atomic_dec(&vha->vref_count);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700260 }
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800261 i++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700262 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700263 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700264}
265
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800266int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700267qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
268{
269 /*
270 * Physical port will do most of the abort and recovery work. We can
271 * just treat it as a loop down
272 */
273 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
274 atomic_set(&vha->loop_state, LOOP_DOWN);
275 qla2x00_mark_all_devices_lost(vha, 0);
276 } else {
277 if (!atomic_read(&vha->loop_down_timer))
278 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
279 }
280
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700281 /*
282 * To exclusively reset vport, we need to log it out first. Note: this
283 * control_vp can fail if ISP reset is already issued, this is
284 * expected, as the vp would be already logged out due to ISP reset.
285 */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800286 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
287 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
288
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700289 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
290 vha->host_no, vha->vp_idx));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800291 return qla24xx_enable_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700292}
293
Adrian Bunka824ebb2008-01-17 09:02:15 -0800294static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700295qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
296{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700297 qla2x00_do_work(vha);
298
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700299 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
300 /* VP acquired. complete port configuration */
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700301 qla24xx_configure_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700302 return 0;
303 }
304
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800305 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
306 qla2x00_update_fcports(vha);
307 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
308 }
309
310 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
311 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
312 atomic_read(&vha->loop_state) != LOOP_DOWN) {
313
314 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
315 vha->host_no));
316 qla2x00_relogin(vha);
317
318 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
319 vha->host_no));
320 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700321
322 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
323 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
324 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
325 }
326
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800327 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700328 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
329 qla2x00_loop_resync(vha);
330 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
331 }
332 }
333
334 return 0;
335}
336
337void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800338qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700339{
340 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800341 struct qla_hw_data *ha = vha->hw;
342 scsi_qla_host_t *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -0700343 unsigned long flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700344
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800345 if (vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700346 return;
347 if (list_empty(&ha->vp_list))
348 return;
349
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800350 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700351
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700352 if (!(ha->current_topology & ISP_CFG_F))
353 return;
354
Arun Easifeafb7b2010-09-03 14:57:00 -0700355 spin_lock_irqsave(&ha->vport_slock, flags);
356 list_for_each_entry(vp, &ha->vp_list, list) {
357 if (vp->vp_idx) {
358 atomic_inc(&vp->vref_count);
359 spin_unlock_irqrestore(&ha->vport_slock, flags);
360
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800361 ret = qla2x00_do_dpc_vp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -0700362
363 spin_lock_irqsave(&ha->vport_slock, flags);
364 atomic_dec(&vp->vref_count);
365 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700366 }
Arun Easifeafb7b2010-09-03 14:57:00 -0700367 spin_unlock_irqrestore(&ha->vport_slock, flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700368}
369
370int
371qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
372{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800373 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
374 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700375 scsi_qla_host_t *vha;
376 uint8_t port_name[WWN_SIZE];
377
378 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
379 return VPCERR_UNSUPPORTED;
380
381 /* Check up the F/W and H/W support NPIV */
382 if (!ha->flags.npiv_supported)
383 return VPCERR_UNSUPPORTED;
384
385 /* Check up whether npiv supported switch presented */
386 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
387 return VPCERR_NO_FABRIC_SUPP;
388
389 /* Check up unique WWPN */
390 u64_to_wwn(fc_vport->port_name, port_name);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800391 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
Seokmann Ju50db6b12008-01-17 09:02:14 -0800392 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700393 vha = qla24xx_find_vhost_by_name(ha, port_name);
394 if (vha)
395 return VPCERR_BAD_WWN;
396
397 /* Check up max-npiv-supports */
398 if (ha->num_vhosts > ha->max_npiv_vports) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800399 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800400 "max_npv_vports %ud.\n", base_vha->host_no,
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800401 ha->num_vhosts, ha->max_npiv_vports));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700402 return VPCERR_UNSUPPORTED;
403 }
404 return 0;
405}
406
407scsi_qla_host_t *
408qla24xx_create_vhost(struct fc_vport *fc_vport)
409{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800410 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
411 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700412 scsi_qla_host_t *vha;
Giridhar Malavalia5326f82009-03-24 09:07:56 -0700413 struct scsi_host_template *sht = &qla2xxx_driver_template;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700414 struct Scsi_Host *host;
415
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800416 vha = qla2x00_create_host(sht, ha);
417 if (!vha) {
418 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700419 return(NULL);
420 }
421
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800422 host = vha->host;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700423 fc_vport->dd_data = vha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700424 /* New host info */
425 u64_to_wwn(fc_vport->node_name, vha->node_name);
426 u64_to_wwn(fc_vport->port_name, vha->port_name);
427
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700428 vha->fc_vport = fc_vport;
429 vha->device_flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700430 vha->vp_idx = qla24xx_allocate_vp_id(vha);
431 if (vha->vp_idx > ha->max_npiv_vports) {
432 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
433 vha->host_no));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800434 goto create_vhost_failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700435 }
436 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
437
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700438 vha->dpc_flags = 0L;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700439
440 /*
441 * To fix the issue of processing a parent's RSCN for the vport before
442 * its SCR is complete.
443 */
444 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
445 atomic_set(&vha->loop_state, LOOP_DOWN);
446 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
447
448 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
449
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700450 vha->req = base_vha->req;
451 host->can_queue = base_vha->req->length + 128;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700452 host->this_id = 255;
453 host->cmd_per_lun = 3;
Arun Easi0c470872010-07-23 15:28:38 +0500454 if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && ql2xenabledif)
455 host->max_cmd_len = 32;
456 else
457 host->max_cmd_len = MAX_CMDSZ;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700458 host->max_channel = MAX_BUSES - 1;
Andrew Vasquez82515922011-05-10 11:30:13 -0700459 host->max_lun = ql2xmaxlun;
Seokmann Ju711c1d92008-07-10 16:55:51 -0700460 host->unique_id = host->host_no;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700461 host->max_id = MAX_TARGETS_2200;
462 host->transportt = qla2xxx_transport_vport_template;
463
464 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
465 vha->host_no, vha));
466
467 vha->flags.init_done = 1;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700468
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700469 mutex_lock(&ha->vport_lock);
470 set_bit(vha->vp_idx, ha->vp_idx_map);
471 ha->cur_vport_count++;
472 mutex_unlock(&ha->vport_lock);
473
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700474 return vha;
475
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800476create_vhost_failed:
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700477 return NULL;
478}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800479
480static void
481qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
482{
483 struct qla_hw_data *ha = vha->hw;
484 uint16_t que_id = req->id;
485
486 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
487 sizeof(request_t), req->ring, req->dma);
488 req->ring = NULL;
489 req->dma = 0;
490 if (que_id) {
491 ha->req_q_map[que_id] = NULL;
492 mutex_lock(&ha->vport_lock);
493 clear_bit(que_id, ha->req_qid_map);
494 mutex_unlock(&ha->vport_lock);
495 }
496 kfree(req);
497 req = NULL;
498}
499
500static void
501qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
502{
503 struct qla_hw_data *ha = vha->hw;
504 uint16_t que_id = rsp->id;
505
506 if (rsp->msix && rsp->msix->have_irq) {
507 free_irq(rsp->msix->vector, rsp);
508 rsp->msix->have_irq = 0;
509 rsp->msix->rsp = NULL;
510 }
511 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
512 sizeof(response_t), rsp->ring, rsp->dma);
513 rsp->ring = NULL;
514 rsp->dma = 0;
515 if (que_id) {
516 ha->rsp_q_map[que_id] = NULL;
517 mutex_lock(&ha->vport_lock);
518 clear_bit(que_id, ha->rsp_qid_map);
519 mutex_unlock(&ha->vport_lock);
520 }
521 kfree(rsp);
522 rsp = NULL;
523}
524
525int
526qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
527{
528 int ret = -1;
529
530 if (req) {
531 req->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800532 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800533 }
534 if (ret == QLA_SUCCESS)
535 qla25xx_free_req_que(vha, req);
536
537 return ret;
538}
539
Andrew Vasquez3dbe7562010-07-23 15:28:37 +0500540static int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800541qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
542{
543 int ret = -1;
544
545 if (rsp) {
546 rsp->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800547 ret = qla25xx_init_rsp_que(vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800548 }
549 if (ret == QLA_SUCCESS)
550 qla25xx_free_rsp_que(vha, rsp);
551
552 return ret;
553}
554
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800555/* Delete all queues for a given vhost */
556int
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700557qla25xx_delete_queues(struct scsi_qla_host *vha)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800558{
559 int cnt, ret = 0;
560 struct req_que *req = NULL;
561 struct rsp_que *rsp = NULL;
562 struct qla_hw_data *ha = vha->hw;
563
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700564 /* Delete request queues */
565 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
566 req = ha->req_q_map[cnt];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800567 if (req) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800568 ret = qla25xx_delete_req_que(vha, req);
569 if (ret != QLA_SUCCESS) {
570 qla_printk(KERN_WARNING, ha,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700571 "Couldn't delete req que %d\n",
572 req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800573 return ret;
574 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800575 }
576 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700577
578 /* Delete response queues */
579 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
580 rsp = ha->rsp_q_map[cnt];
581 if (rsp) {
582 ret = qla25xx_delete_rsp_que(vha, rsp);
583 if (ret != QLA_SUCCESS) {
584 qla_printk(KERN_WARNING, ha,
585 "Couldn't delete rsp que %d\n",
586 rsp->id);
587 return ret;
588 }
589 }
590 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800591 return ret;
592}
593
594int
595qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700596 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800597{
598 int ret = 0;
599 struct req_que *req = NULL;
600 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
601 uint16_t que_id = 0;
Andrew Vasquez08029992009-03-24 09:07:55 -0700602 device_reg_t __iomem *reg;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700603 uint32_t cnt;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800604
605 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
606 if (req == NULL) {
607 qla_printk(KERN_WARNING, ha, "could not allocate memory"
608 "for request que\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700609 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800610 }
611
612 req->length = REQUEST_ENTRY_CNT_24XX;
613 req->ring = dma_alloc_coherent(&ha->pdev->dev,
614 (req->length + 1) * sizeof(request_t),
615 &req->dma, GFP_KERNEL);
616 if (req->ring == NULL) {
617 qla_printk(KERN_WARNING, ha,
618 "Memory Allocation failed - request_ring\n");
619 goto que_failed;
620 }
621
622 mutex_lock(&ha->vport_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700623 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
624 if (que_id >= ha->max_req_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800625 mutex_unlock(&ha->vport_lock);
626 qla_printk(KERN_INFO, ha, "No resources to create "
627 "additional request queue\n");
628 goto que_failed;
629 }
630 set_bit(que_id, ha->req_qid_map);
631 ha->req_q_map[que_id] = req;
632 req->rid = rid;
633 req->vp_idx = vp_idx;
634 req->qos = qos;
635
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700636 if (rsp_que < 0)
637 req->rsp = NULL;
638 else
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800639 req->rsp = ha->rsp_q_map[rsp_que];
640 /* Use alternate PCI bus number */
641 if (MSB(req->rid))
642 options |= BIT_4;
643 /* Use alternate PCI devfn */
644 if (LSB(req->rid))
645 options |= BIT_5;
646 req->options = options;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700647
648 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
649 req->outstanding_cmds[cnt] = NULL;
650 req->current_outstanding_cmd = 1;
651
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800652 req->ring_ptr = req->ring;
653 req->ring_index = 0;
654 req->cnt = req->length;
655 req->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700656 reg = ISP_QUE_REG(ha, que_id);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -0800657 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800658 mutex_unlock(&ha->vport_lock);
659
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800660 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800661 if (ret != QLA_SUCCESS) {
662 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
663 mutex_lock(&ha->vport_lock);
664 clear_bit(que_id, ha->req_qid_map);
665 mutex_unlock(&ha->vport_lock);
666 goto que_failed;
667 }
668
669 return req->id;
670
671que_failed:
672 qla25xx_free_req_que(base_vha, req);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700673failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800674 return 0;
675}
676
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700677static void qla_do_work(struct work_struct *work)
678{
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800679 unsigned long flags;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700680 struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
681 struct scsi_qla_host *vha;
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800682 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700683
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800684 spin_lock_irqsave(&rsp->hw->hardware_lock, flags);
685 vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700686 qla24xx_process_response_queue(vha, rsp);
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800687 spin_unlock_irqrestore(&rsp->hw->hardware_lock, flags);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700688}
689
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800690/* create response queue */
691int
692qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700693 uint8_t vp_idx, uint16_t rid, int req)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800694{
695 int ret = 0;
696 struct rsp_que *rsp = NULL;
697 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Andrew Vasquez08029992009-03-24 09:07:55 -0700698 uint16_t que_id = 0;
699 device_reg_t __iomem *reg;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800700
701 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
702 if (rsp == NULL) {
703 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
704 " response que\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700705 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800706 }
707
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700708 rsp->length = RESPONSE_ENTRY_CNT_MQ;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800709 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
710 (rsp->length + 1) * sizeof(response_t),
711 &rsp->dma, GFP_KERNEL);
712 if (rsp->ring == NULL) {
713 qla_printk(KERN_WARNING, ha,
714 "Memory Allocation failed - response_ring\n");
715 goto que_failed;
716 }
717
718 mutex_lock(&ha->vport_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700719 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
720 if (que_id >= ha->max_rsp_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800721 mutex_unlock(&ha->vport_lock);
722 qla_printk(KERN_INFO, ha, "No resources to create "
723 "additional response queue\n");
724 goto que_failed;
725 }
726 set_bit(que_id, ha->rsp_qid_map);
727
728 if (ha->flags.msix_enabled)
729 rsp->msix = &ha->msix_entries[que_id + 1];
730 else
731 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
732
733 ha->rsp_q_map[que_id] = rsp;
734 rsp->rid = rid;
735 rsp->vp_idx = vp_idx;
736 rsp->hw = ha;
737 /* Use alternate PCI bus number */
738 if (MSB(rsp->rid))
739 options |= BIT_4;
740 /* Use alternate PCI devfn */
741 if (LSB(rsp->rid))
742 options |= BIT_5;
Anirban Chakraborty31557542009-12-02 10:36:55 -0800743 /* Enable MSIX handshake mode on for uncapable adapters */
744 if (!IS_MSIX_NACK_CAPABLE(ha))
745 options |= BIT_6;
746
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800747 rsp->options = options;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800748 rsp->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700749 reg = ISP_QUE_REG(ha, que_id);
750 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
751 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800752 mutex_unlock(&ha->vport_lock);
753
754 ret = qla25xx_request_irq(rsp);
755 if (ret)
756 goto que_failed;
757
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800758 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800759 if (ret != QLA_SUCCESS) {
760 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
761 mutex_lock(&ha->vport_lock);
762 clear_bit(que_id, ha->rsp_qid_map);
763 mutex_unlock(&ha->vport_lock);
764 goto que_failed;
765 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700766 if (req >= 0)
767 rsp->req = ha->req_q_map[req];
768 else
769 rsp->req = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800770
771 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700772 if (rsp->hw->wq)
773 INIT_WORK(&rsp->q_work, qla_do_work);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800774 return rsp->id;
775
776que_failed:
777 qla25xx_free_rsp_que(base_vha, rsp);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700778failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800779 return 0;
780}