blob: 8220e7b9799bb4cc7dd819bd28dc59c11f082a96 [file] [log] [blame]
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001/*
Andrew Vasquez01e58d82008-04-03 13:13:13 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 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;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070033
34 /* Find an empty slot and assign an vp_id */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070035 mutex_lock(&ha->vport_lock);
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080036 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
37 if (vp_id > ha->max_npiv_vports) {
38 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
39 vp_id, ha->max_npiv_vports));
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070040 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070041 return vp_id;
42 }
43
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080044 set_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070045 ha->num_vhosts++;
46 vha->vp_idx = vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080047 list_add_tail(&vha->list, &ha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070048 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070049 return vp_id;
50}
51
52void
53qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
54{
55 uint16_t vp_id;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080056 struct qla_hw_data *ha = vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070057
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070058 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070059 vp_id = vha->vp_idx;
60 ha->num_vhosts--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080061 clear_bit(vp_id, ha->vp_idx_map);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080062 list_del(&vha->list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070063 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070064}
65
Adrian Bunka824ebb2008-01-17 09:02:15 -080066static scsi_qla_host_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080067qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070068{
69 scsi_qla_host_t *vha;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080070 struct scsi_qla_host *tvha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070071
72 /* Locate matching device in database. */
Anirban Chakrabortyee546b62009-03-05 11:07:02 -080073 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070074 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
75 return vha;
76 }
77 return NULL;
78}
79
80/*
81 * qla2x00_mark_vp_devices_dead
82 * Updates fcport state when device goes offline.
83 *
84 * Input:
85 * ha = adapter block pointer.
86 * fcport = port structure pointer.
87 *
88 * Return:
89 * None.
90 *
91 * Context:
92 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -070093static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070094qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
95{
96 fc_port_t *fcport;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070097
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080098 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070099 DEBUG15(printk("scsi(%ld): Marking port dead, "
100 "loop_id=0x%04x :%x\n",
101 vha->host_no, fcport->loop_id, fcport->vp_idx));
102
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800103 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700104 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700105 atomic_set(&fcport->state, FCS_UNCONFIGURED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700106 }
107}
108
109int
110qla24xx_disable_vp(scsi_qla_host_t *vha)
111{
112 int ret;
113
114 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
115 atomic_set(&vha->loop_state, LOOP_DOWN);
116 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
117
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700118 qla2x00_mark_vp_devices_dead(vha);
119 atomic_set(&vha->vp_state, VP_FAILED);
120 vha->flags.management_server_logged_in = 0;
121 if (ret == QLA_SUCCESS) {
122 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
123 } else {
124 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
125 return -1;
126 }
127 return 0;
128}
129
130int
131qla24xx_enable_vp(scsi_qla_host_t *vha)
132{
133 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800134 struct qla_hw_data *ha = vha->hw;
135 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700136
137 /* Check if physical ha port is Up */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800138 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
139 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700140 vha->vp_err_state = VP_ERR_PORTDWN;
141 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
142 goto enable_failed;
143 }
144
145 /* Initialize the new vport unless it is a persistent port */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700146 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700147 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700148 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700149
150 if (ret != QLA_SUCCESS) {
151 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
152 goto enable_failed;
153 }
154
155 DEBUG15(qla_printk(KERN_INFO, ha,
156 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
157 return 0;
158
159enable_failed:
160 DEBUG15(qla_printk(KERN_INFO, ha,
161 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
162 return 1;
163}
164
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700165static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700166qla24xx_configure_vp(scsi_qla_host_t *vha)
167{
168 struct fc_vport *fc_vport;
169 int ret;
170
171 fc_vport = vha->fc_vport;
172
173 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
174 vha->host_no, __func__));
175 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
176 if (ret != QLA_SUCCESS) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800177 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
178 "receiving of RSCN requests: 0x%x\n", ret));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700179 return;
180 } else {
181 /* Corresponds to SCR enabled */
182 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
183 }
184
185 vha->flags.online = 1;
186 if (qla24xx_configure_vhba(vha))
187 return;
188
189 atomic_set(&vha->vp_state, VP_ACTIVE);
190 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
191}
192
193void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800194qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700195{
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800196 scsi_qla_host_t *vha, *tvha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800197 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800198 int i = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700199
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800200 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800201 if (vha->vp_idx) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700202 switch (mb[0]) {
203 case MBA_LIP_OCCURRED:
204 case MBA_LOOP_UP:
205 case MBA_LOOP_DOWN:
206 case MBA_LIP_RESET:
207 case MBA_POINT_TO_POINT:
208 case MBA_CHG_IN_CONNECTION:
209 case MBA_PORT_UPDATE:
210 case MBA_RSCN_UPDATE:
211 DEBUG15(printk("scsi(%ld)%s: Async_event for"
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800212 " VP[%d], mb = 0x%x, vha=%p\n",
213 vha->host_no, __func__, i, *mb, vha));
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800214 qla2x00_async_event(vha, rsp, mb);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700215 break;
216 }
217 }
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800218 i++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700219 }
220}
221
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800222int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700223qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
224{
225 /*
226 * Physical port will do most of the abort and recovery work. We can
227 * just treat it as a loop down
228 */
229 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
230 atomic_set(&vha->loop_state, LOOP_DOWN);
231 qla2x00_mark_all_devices_lost(vha, 0);
232 } else {
233 if (!atomic_read(&vha->loop_down_timer))
234 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
235 }
236
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700237 /*
238 * To exclusively reset vport, we need to log it out first. Note: this
239 * control_vp can fail if ISP reset is already issued, this is
240 * expected, as the vp would be already logged out due to ISP reset.
241 */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800242 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
243 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
244
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700245 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
246 vha->host_no, vha->vp_idx));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800247 return qla24xx_enable_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700248}
249
Adrian Bunka824ebb2008-01-17 09:02:15 -0800250static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700251qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
252{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700253 qla2x00_do_work(vha);
254
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700255 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
256 /* VP acquired. complete port configuration */
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700257 qla24xx_configure_vp(vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700258 return 0;
259 }
260
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800261 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
262 qla2x00_update_fcports(vha);
263 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
264 }
265
266 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
267 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
268 atomic_read(&vha->loop_state) != LOOP_DOWN) {
269
270 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
271 vha->host_no));
272 qla2x00_relogin(vha);
273
274 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
275 vha->host_no));
276 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700277
278 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
279 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
280 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
281 }
282
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800283 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700284 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
285 qla2x00_loop_resync(vha);
286 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
287 }
288 }
289
290 return 0;
291}
292
293void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800294qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700295{
296 int ret;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800297 struct qla_hw_data *ha = vha->hw;
298 scsi_qla_host_t *vp;
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800299 struct scsi_qla_host *tvp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700300
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800301 if (vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700302 return;
303 if (list_empty(&ha->vp_list))
304 return;
305
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800306 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700307
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700308 if (!(ha->current_topology & ISP_CFG_F))
309 return;
310
Anirban Chakrabortyee546b62009-03-05 11:07:02 -0800311 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800312 if (vp->vp_idx)
313 ret = qla2x00_do_dpc_vp(vp);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700314 }
315}
316
317int
318qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
319{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800320 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
321 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700322 scsi_qla_host_t *vha;
323 uint8_t port_name[WWN_SIZE];
324
325 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
326 return VPCERR_UNSUPPORTED;
327
328 /* Check up the F/W and H/W support NPIV */
329 if (!ha->flags.npiv_supported)
330 return VPCERR_UNSUPPORTED;
331
332 /* Check up whether npiv supported switch presented */
333 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
334 return VPCERR_NO_FABRIC_SUPP;
335
336 /* Check up unique WWPN */
337 u64_to_wwn(fc_vport->port_name, port_name);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800338 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
Seokmann Ju50db6b12008-01-17 09:02:14 -0800339 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700340 vha = qla24xx_find_vhost_by_name(ha, port_name);
341 if (vha)
342 return VPCERR_BAD_WWN;
343
344 /* Check up max-npiv-supports */
345 if (ha->num_vhosts > ha->max_npiv_vports) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800346 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800347 "max_npv_vports %ud.\n", base_vha->host_no,
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800348 ha->num_vhosts, ha->max_npiv_vports));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700349 return VPCERR_UNSUPPORTED;
350 }
351 return 0;
352}
353
354scsi_qla_host_t *
355qla24xx_create_vhost(struct fc_vport *fc_vport)
356{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800357 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
358 struct qla_hw_data *ha = base_vha->hw;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700359 scsi_qla_host_t *vha;
Giridhar Malavalia5326f82009-03-24 09:07:56 -0700360 struct scsi_host_template *sht = &qla2xxx_driver_template;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700361 struct Scsi_Host *host;
362
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800363 vha = qla2x00_create_host(sht, ha);
364 if (!vha) {
365 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700366 return(NULL);
367 }
368
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800369 host = vha->host;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700370 fc_vport->dd_data = vha;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700371 /* New host info */
372 u64_to_wwn(fc_vport->node_name, vha->node_name);
373 u64_to_wwn(fc_vport->port_name, vha->port_name);
374
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700375 vha->fc_vport = fc_vport;
376 vha->device_flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700377 vha->vp_idx = qla24xx_allocate_vp_id(vha);
378 if (vha->vp_idx > ha->max_npiv_vports) {
379 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
380 vha->host_no));
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800381 goto create_vhost_failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700382 }
383 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
384
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700385 vha->dpc_flags = 0L;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700386
387 /*
388 * To fix the issue of processing a parent's RSCN for the vport before
389 * its SCR is complete.
390 */
391 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
392 atomic_set(&vha->loop_state, LOOP_DOWN);
393 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
394
395 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
396
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700397 vha->req = base_vha->req;
398 host->can_queue = base_vha->req->length + 128;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700399 host->this_id = 255;
400 host->cmd_per_lun = 3;
401 host->max_cmd_len = MAX_CMDSZ;
402 host->max_channel = MAX_BUSES - 1;
403 host->max_lun = MAX_LUNS;
Seokmann Ju711c1d92008-07-10 16:55:51 -0700404 host->unique_id = host->host_no;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700405 host->max_id = MAX_TARGETS_2200;
406 host->transportt = qla2xxx_transport_vport_template;
407
408 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
409 vha->host_no, vha));
410
411 vha->flags.init_done = 1;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700412
Andrew Vasquez0d6e61b2009-08-25 11:36:19 -0700413 mutex_lock(&ha->vport_lock);
414 set_bit(vha->vp_idx, ha->vp_idx_map);
415 ha->cur_vport_count++;
416 mutex_unlock(&ha->vport_lock);
417
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700418 return vha;
419
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800420create_vhost_failed:
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700421 return NULL;
422}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800423
424static void
425qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
426{
427 struct qla_hw_data *ha = vha->hw;
428 uint16_t que_id = req->id;
429
430 dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
431 sizeof(request_t), req->ring, req->dma);
432 req->ring = NULL;
433 req->dma = 0;
434 if (que_id) {
435 ha->req_q_map[que_id] = NULL;
436 mutex_lock(&ha->vport_lock);
437 clear_bit(que_id, ha->req_qid_map);
438 mutex_unlock(&ha->vport_lock);
439 }
440 kfree(req);
441 req = NULL;
442}
443
444static void
445qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
446{
447 struct qla_hw_data *ha = vha->hw;
448 uint16_t que_id = rsp->id;
449
450 if (rsp->msix && rsp->msix->have_irq) {
451 free_irq(rsp->msix->vector, rsp);
452 rsp->msix->have_irq = 0;
453 rsp->msix->rsp = NULL;
454 }
455 dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
456 sizeof(response_t), rsp->ring, rsp->dma);
457 rsp->ring = NULL;
458 rsp->dma = 0;
459 if (que_id) {
460 ha->rsp_q_map[que_id] = NULL;
461 mutex_lock(&ha->vport_lock);
462 clear_bit(que_id, ha->rsp_qid_map);
463 mutex_unlock(&ha->vport_lock);
464 }
465 kfree(rsp);
466 rsp = NULL;
467}
468
469int
470qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
471{
472 int ret = -1;
473
474 if (req) {
475 req->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800476 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800477 }
478 if (ret == QLA_SUCCESS)
479 qla25xx_free_req_que(vha, req);
480
481 return ret;
482}
483
484int
485qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
486{
487 int ret = -1;
488
489 if (rsp) {
490 rsp->options |= BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800491 ret = qla25xx_init_rsp_que(vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800492 }
493 if (ret == QLA_SUCCESS)
494 qla25xx_free_rsp_que(vha, rsp);
495
496 return ret;
497}
498
499int qla25xx_update_req_que(struct scsi_qla_host *vha, uint8_t que, uint8_t qos)
500{
501 int ret = 0;
502 struct qla_hw_data *ha = vha->hw;
503 struct req_que *req = ha->req_q_map[que];
504
505 req->options |= BIT_3;
506 req->qos = qos;
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800507 ret = qla25xx_init_req_que(vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800508 if (ret != QLA_SUCCESS)
509 DEBUG2_17(printk(KERN_WARNING "%s failed\n", __func__));
510 /* restore options bit */
511 req->options &= ~BIT_3;
512 return ret;
513}
514
515
516/* Delete all queues for a given vhost */
517int
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700518qla25xx_delete_queues(struct scsi_qla_host *vha)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800519{
520 int cnt, ret = 0;
521 struct req_que *req = NULL;
522 struct rsp_que *rsp = NULL;
523 struct qla_hw_data *ha = vha->hw;
524
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700525 /* Delete request queues */
526 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
527 req = ha->req_q_map[cnt];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800528 if (req) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800529 ret = qla25xx_delete_req_que(vha, req);
530 if (ret != QLA_SUCCESS) {
531 qla_printk(KERN_WARNING, ha,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700532 "Couldn't delete req que %d\n",
533 req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800534 return ret;
535 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800536 }
537 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700538
539 /* Delete response queues */
540 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
541 rsp = ha->rsp_q_map[cnt];
542 if (rsp) {
543 ret = qla25xx_delete_rsp_que(vha, rsp);
544 if (ret != QLA_SUCCESS) {
545 qla_printk(KERN_WARNING, ha,
546 "Couldn't delete rsp que %d\n",
547 rsp->id);
548 return ret;
549 }
550 }
551 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800552 return ret;
553}
554
555int
556qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700557 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800558{
559 int ret = 0;
560 struct req_que *req = NULL;
561 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
562 uint16_t que_id = 0;
Andrew Vasquez08029992009-03-24 09:07:55 -0700563 device_reg_t __iomem *reg;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700564 uint32_t cnt;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800565
566 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
567 if (req == NULL) {
568 qla_printk(KERN_WARNING, ha, "could not allocate memory"
569 "for request que\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700570 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800571 }
572
573 req->length = REQUEST_ENTRY_CNT_24XX;
574 req->ring = dma_alloc_coherent(&ha->pdev->dev,
575 (req->length + 1) * sizeof(request_t),
576 &req->dma, GFP_KERNEL);
577 if (req->ring == NULL) {
578 qla_printk(KERN_WARNING, ha,
579 "Memory Allocation failed - request_ring\n");
580 goto que_failed;
581 }
582
583 mutex_lock(&ha->vport_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700584 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
585 if (que_id >= ha->max_req_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800586 mutex_unlock(&ha->vport_lock);
587 qla_printk(KERN_INFO, ha, "No resources to create "
588 "additional request queue\n");
589 goto que_failed;
590 }
591 set_bit(que_id, ha->req_qid_map);
592 ha->req_q_map[que_id] = req;
593 req->rid = rid;
594 req->vp_idx = vp_idx;
595 req->qos = qos;
596
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700597 if (rsp_que < 0)
598 req->rsp = NULL;
599 else
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800600 req->rsp = ha->rsp_q_map[rsp_que];
601 /* Use alternate PCI bus number */
602 if (MSB(req->rid))
603 options |= BIT_4;
604 /* Use alternate PCI devfn */
605 if (LSB(req->rid))
606 options |= BIT_5;
607 req->options = options;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700608
609 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
610 req->outstanding_cmds[cnt] = NULL;
611 req->current_outstanding_cmd = 1;
612
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800613 req->ring_ptr = req->ring;
614 req->ring_index = 0;
615 req->cnt = req->length;
616 req->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700617 reg = ISP_QUE_REG(ha, que_id);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -0800618 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800619 mutex_unlock(&ha->vport_lock);
620
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800621 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800622 if (ret != QLA_SUCCESS) {
623 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
624 mutex_lock(&ha->vport_lock);
625 clear_bit(que_id, ha->req_qid_map);
626 mutex_unlock(&ha->vport_lock);
627 goto que_failed;
628 }
629
630 return req->id;
631
632que_failed:
633 qla25xx_free_req_que(base_vha, req);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700634failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800635 return 0;
636}
637
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700638static void qla_do_work(struct work_struct *work)
639{
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800640 unsigned long flags;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700641 struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
642 struct scsi_qla_host *vha;
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800643 struct qla_hw_data *ha = rsp->hw;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700644
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800645 spin_lock_irqsave(&rsp->hw->hardware_lock, flags);
646 vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700647 qla24xx_process_response_queue(vha, rsp);
Anirban Chakrabortya67093d2010-02-04 14:17:59 -0800648 spin_unlock_irqrestore(&rsp->hw->hardware_lock, flags);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700649}
650
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800651/* create response queue */
652int
653qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700654 uint8_t vp_idx, uint16_t rid, int req)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800655{
656 int ret = 0;
657 struct rsp_que *rsp = NULL;
658 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Andrew Vasquez08029992009-03-24 09:07:55 -0700659 uint16_t que_id = 0;
660 device_reg_t __iomem *reg;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800661
662 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
663 if (rsp == NULL) {
664 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
665 " response que\n");
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700666 goto failed;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800667 }
668
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700669 rsp->length = RESPONSE_ENTRY_CNT_MQ;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800670 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
671 (rsp->length + 1) * sizeof(response_t),
672 &rsp->dma, GFP_KERNEL);
673 if (rsp->ring == NULL) {
674 qla_printk(KERN_WARNING, ha,
675 "Memory Allocation failed - response_ring\n");
676 goto que_failed;
677 }
678
679 mutex_lock(&ha->vport_lock);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700680 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
681 if (que_id >= ha->max_rsp_queues) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800682 mutex_unlock(&ha->vport_lock);
683 qla_printk(KERN_INFO, ha, "No resources to create "
684 "additional response queue\n");
685 goto que_failed;
686 }
687 set_bit(que_id, ha->rsp_qid_map);
688
689 if (ha->flags.msix_enabled)
690 rsp->msix = &ha->msix_entries[que_id + 1];
691 else
692 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
693
694 ha->rsp_q_map[que_id] = rsp;
695 rsp->rid = rid;
696 rsp->vp_idx = vp_idx;
697 rsp->hw = ha;
698 /* Use alternate PCI bus number */
699 if (MSB(rsp->rid))
700 options |= BIT_4;
701 /* Use alternate PCI devfn */
702 if (LSB(rsp->rid))
703 options |= BIT_5;
Anirban Chakraborty31557542009-12-02 10:36:55 -0800704 /* Enable MSIX handshake mode on for uncapable adapters */
705 if (!IS_MSIX_NACK_CAPABLE(ha))
706 options |= BIT_6;
707
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800708 rsp->options = options;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800709 rsp->id = que_id;
Andrew Vasquez08029992009-03-24 09:07:55 -0700710 reg = ISP_QUE_REG(ha, que_id);
711 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
712 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800713 mutex_unlock(&ha->vport_lock);
714
715 ret = qla25xx_request_irq(rsp);
716 if (ret)
717 goto que_failed;
718
Anirban Chakraborty618a7522009-02-08 20:50:11 -0800719 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800720 if (ret != QLA_SUCCESS) {
721 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
722 mutex_lock(&ha->vport_lock);
723 clear_bit(que_id, ha->rsp_qid_map);
724 mutex_unlock(&ha->vport_lock);
725 goto que_failed;
726 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700727 if (req >= 0)
728 rsp->req = ha->req_q_map[req];
729 else
730 rsp->req = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800731
732 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700733 if (rsp->hw->wq)
734 INIT_WORK(&rsp->q_work, qla_do_work);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800735 return rsp->id;
736
737que_failed:
738 qla25xx_free_rsp_que(base_vha, rsp);
Anirban Chakrabortyc7922a92009-09-28 13:52:58 -0700739failed:
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800740 return 0;
741}
742
743int
744qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
745{
746 uint16_t options = 0;
747 uint8_t ret = 0;
748 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700749 struct rsp_que *rsp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800750
751 options |= BIT_1;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700752 ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0, -1);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800753 if (!ret) {
754 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
755 return ret;
756 } else
757 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700758 rsp = ha->rsp_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800759
760 options = 0;
761 if (qos & BIT_7)
762 options |= BIT_8;
763 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
764 qos & ~BIT_7);
765 if (ret) {
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700766 vha->req = ha->req_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800767 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
768 } else
769 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700770 rsp->req = ha->req_q_map[ret];
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800771
772 return ret;
773}