[SCSI] qla2xxx: Refactor qla data structures
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / qla2xxx / qla_mid.c
blobda3db3abb82c7fef4c1e728818279f823f4056e0
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
8 #include "qla_gbl.h"
10 #include <linux/moduleparam.h>
11 #include <linux/vmalloc.h>
12 #include <linux/smp_lock.h>
13 #include <linux/list.h>
15 #include <scsi/scsi_tcq.h>
16 #include <scsi/scsicam.h>
17 #include <linux/delay.h>
19 void
20 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
22 if (vha->vp_idx && vha->timer_active) {
23 del_timer_sync(&vha->timer);
24 vha->timer_active = 0;
28 static uint32_t
29 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
31 uint32_t vp_id;
32 struct qla_hw_data *ha = vha->hw;
34 /* Find an empty slot and assign an vp_id */
35 mutex_lock(&ha->vport_lock);
36 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));
40 mutex_unlock(&ha->vport_lock);
41 return vp_id;
44 set_bit(vp_id, ha->vp_idx_map);
45 ha->num_vhosts++;
46 ha->cur_vport_count++;
47 vha->vp_idx = vp_id;
48 list_add_tail(&vha->list, &ha->vp_list);
49 mutex_unlock(&ha->vport_lock);
50 return vp_id;
53 void
54 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
56 uint16_t vp_id;
57 struct qla_hw_data *ha = vha->hw;
59 mutex_lock(&ha->vport_lock);
60 vp_id = vha->vp_idx;
61 ha->num_vhosts--;
62 ha->cur_vport_count--;
63 clear_bit(vp_id, ha->vp_idx_map);
64 list_del(&vha->list);
65 mutex_unlock(&ha->vport_lock);
68 static scsi_qla_host_t *
69 qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
71 scsi_qla_host_t *vha;
73 /* Locate matching device in database. */
74 list_for_each_entry(vha, &ha->vp_list, list) {
75 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
76 return vha;
78 return NULL;
82 * qla2x00_mark_vp_devices_dead
83 * Updates fcport state when device goes offline.
85 * Input:
86 * ha = adapter block pointer.
87 * fcport = port structure pointer.
89 * Return:
90 * None.
92 * Context:
94 static void
95 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
97 fc_port_t *fcport;
99 list_for_each_entry(fcport, &vha->vp_fcports, list) {
100 DEBUG15(printk("scsi(%ld): Marking port dead, "
101 "loop_id=0x%04x :%x\n",
102 vha->host_no, fcport->loop_id, fcport->vp_idx));
104 qla2x00_mark_device_lost(vha, fcport, 0, 0);
105 atomic_set(&fcport->state, FCS_UNCONFIGURED);
110 qla24xx_disable_vp(scsi_qla_host_t *vha)
112 int ret;
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);
118 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;
127 return 0;
131 qla24xx_enable_vp(scsi_qla_host_t *vha)
133 int ret;
134 struct qla_hw_data *ha = vha->hw;
135 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
137 /* Check if physical ha port is Up */
138 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
139 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
140 vha->vp_err_state = VP_ERR_PORTDWN;
141 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
142 goto enable_failed;
145 /* Initialize the new vport unless it is a persistent port */
146 mutex_lock(&ha->vport_lock);
147 ret = qla24xx_modify_vp_config(vha);
148 mutex_unlock(&ha->vport_lock);
150 if (ret != QLA_SUCCESS) {
151 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
152 goto enable_failed;
155 DEBUG15(qla_printk(KERN_INFO, ha,
156 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
157 return 0;
159 enable_failed:
160 DEBUG15(qla_printk(KERN_INFO, ha,
161 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
162 return 1;
165 static void
166 qla24xx_configure_vp(scsi_qla_host_t *vha)
168 struct fc_vport *fc_vport;
169 int ret;
171 fc_vport = vha->fc_vport;
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) {
177 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
178 "receiving of RSCN requests: 0x%x\n", ret));
179 return;
180 } else {
181 /* Corresponds to SCR enabled */
182 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
185 vha->flags.online = 1;
186 if (qla24xx_configure_vhba(vha))
187 return;
189 atomic_set(&vha->vp_state, VP_ACTIVE);
190 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
193 void
194 qla2x00_alert_all_vps(struct qla_hw_data *ha, uint16_t *mb)
196 scsi_qla_host_t *vha;
197 int i = 0;
199 list_for_each_entry(vha, &ha->vp_list, list) {
200 if (vha->vp_idx) {
201 switch (mb[0]) {
202 case MBA_LIP_OCCURRED:
203 case MBA_LOOP_UP:
204 case MBA_LOOP_DOWN:
205 case MBA_LIP_RESET:
206 case MBA_POINT_TO_POINT:
207 case MBA_CHG_IN_CONNECTION:
208 case MBA_PORT_UPDATE:
209 case MBA_RSCN_UPDATE:
210 DEBUG15(printk("scsi(%ld)%s: Async_event for"
211 " VP[%d], mb = 0x%x, vha=%p\n",
212 vha->host_no, __func__, i, *mb, vha));
213 qla2x00_async_event(vha, mb);
214 break;
217 i++;
222 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
225 * Physical port will do most of the abort and recovery work. We can
226 * just treat it as a loop down
228 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
229 atomic_set(&vha->loop_state, LOOP_DOWN);
230 qla2x00_mark_all_devices_lost(vha, 0);
231 } else {
232 if (!atomic_read(&vha->loop_down_timer))
233 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
236 /* To exclusively reset vport, we need to log it out first.*/
237 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
238 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
240 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
241 vha->host_no, vha->vp_idx));
242 return qla24xx_enable_vp(vha);
245 static int
246 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
248 struct qla_hw_data *ha = vha->hw;
249 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
251 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
252 /* VP acquired. complete port configuration */
253 if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
254 qla24xx_configure_vp(vha);
255 } else {
256 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
257 set_bit(VP_DPC_NEEDED, &base_vha->dpc_flags);
260 return 0;
263 if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
264 qla2x00_update_fcports(vha);
265 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
268 if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
269 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
270 atomic_read(&vha->loop_state) != LOOP_DOWN) {
272 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
273 vha->host_no));
274 qla2x00_relogin(vha);
276 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
277 vha->host_no));
280 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
281 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
282 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
285 if (atomic_read(&vha->vp_state) == VP_ACTIVE &&
286 test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
287 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
288 qla2x00_loop_resync(vha);
289 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
293 return 0;
296 void
297 qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
299 int ret;
300 struct qla_hw_data *ha = vha->hw;
301 scsi_qla_host_t *vp;
303 if (vha->vp_idx)
304 return;
305 if (list_empty(&ha->vp_list))
306 return;
308 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
310 list_for_each_entry(vp, &ha->vp_list, list) {
311 if (vp->vp_idx)
312 ret = qla2x00_do_dpc_vp(vp);
317 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
319 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
320 struct qla_hw_data *ha = base_vha->hw;
321 scsi_qla_host_t *vha;
322 uint8_t port_name[WWN_SIZE];
324 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
325 return VPCERR_UNSUPPORTED;
327 /* Check up the F/W and H/W support NPIV */
328 if (!ha->flags.npiv_supported)
329 return VPCERR_UNSUPPORTED;
331 /* Check up whether npiv supported switch presented */
332 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
333 return VPCERR_NO_FABRIC_SUPP;
335 /* Check up unique WWPN */
336 u64_to_wwn(fc_vport->port_name, port_name);
337 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
338 return VPCERR_BAD_WWN;
339 vha = qla24xx_find_vhost_by_name(ha, port_name);
340 if (vha)
341 return VPCERR_BAD_WWN;
343 /* Check up max-npiv-supports */
344 if (ha->num_vhosts > ha->max_npiv_vports) {
345 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
346 "max_npv_vports %ud.\n", base_vha->host_no,
347 ha->num_vhosts, ha->max_npiv_vports));
348 return VPCERR_UNSUPPORTED;
350 return 0;
353 scsi_qla_host_t *
354 qla24xx_create_vhost(struct fc_vport *fc_vport)
356 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
357 struct qla_hw_data *ha = base_vha->hw;
358 scsi_qla_host_t *vha;
359 struct scsi_host_template *sht = &qla24xx_driver_template;
360 struct Scsi_Host *host;
362 vha = qla2x00_create_host(sht, ha);
363 if (!vha) {
364 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
365 return(NULL);
368 host = vha->host;
369 fc_vport->dd_data = vha;
371 /* 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);
375 vha->fc_vport = fc_vport;
376 vha->device_flags = 0;
377 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));
381 goto create_vhost_failed;
383 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
385 vha->dpc_flags = 0L;
386 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
387 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
390 * To fix the issue of processing a parent's RSCN for the vport before
391 * its SCR is complete.
393 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
394 atomic_set(&vha->loop_state, LOOP_DOWN);
395 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
397 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
399 host->can_queue = ha->req->length + 128;
400 host->this_id = 255;
401 host->cmd_per_lun = 3;
402 host->max_cmd_len = MAX_CMDSZ;
403 host->max_channel = MAX_BUSES - 1;
404 host->max_lun = MAX_LUNS;
405 host->unique_id = host->host_no;
406 host->max_id = MAX_TARGETS_2200;
407 host->transportt = qla2xxx_transport_vport_template;
409 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
410 vha->host_no, vha));
412 vha->flags.init_done = 1;
414 return vha;
416 create_vhost_failed:
417 return NULL;