[SCSI] qla2xxx: Cruft cleanup of functions and structures.
[linux-2.6/btrfs-unstable.git] / drivers / scsi / qla2xxx / qla_mid.c
blob9f48cd46f618dc1d0e4dd134ebb29bf48b032eda
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"
9 #include <linux/version.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->parent && 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 scsi_qla_host_t *ha = vha->parent;
34 /* Find an empty slot and assign an vp_id */
35 down(&ha->vport_sem);
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 up(&ha->vport_sem);
41 return vp_id;
44 set_bit(vp_id, ha->vp_idx_map);
45 ha->num_vhosts++;
46 vha->vp_idx = vp_id;
47 list_add_tail(&vha->vp_list, &ha->vp_list);
48 up(&ha->vport_sem);
49 return vp_id;
52 void
53 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
55 uint16_t vp_id;
56 scsi_qla_host_t *ha = vha->parent;
58 down(&ha->vport_sem);
59 vp_id = vha->vp_idx;
60 ha->num_vhosts--;
61 clear_bit(vp_id, ha->vp_idx_map);
62 list_del(&vha->vp_list);
63 up(&ha->vport_sem);
66 static scsi_qla_host_t *
67 qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
69 scsi_qla_host_t *vha;
71 /* Locate matching device in database. */
72 list_for_each_entry(vha, &ha->vp_list, vp_list) {
73 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
74 return vha;
76 return NULL;
80 * qla2x00_mark_vp_devices_dead
81 * Updates fcport state when device goes offline.
83 * Input:
84 * ha = adapter block pointer.
85 * fcport = port structure pointer.
87 * Return:
88 * None.
90 * Context:
92 static void
93 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
95 fc_port_t *fcport;
96 scsi_qla_host_t *pha = to_qla_parent(vha);
98 list_for_each_entry(fcport, &pha->fcports, list) {
99 if (fcport->vp_idx != vha->vp_idx)
100 continue;
102 DEBUG15(printk("scsi(%ld): Marking port dead, "
103 "loop_id=0x%04x :%x\n",
104 vha->host_no, fcport->loop_id, fcport->vp_idx));
106 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
107 qla2x00_mark_device_lost(vha, fcport, 0, 0);
112 qla24xx_disable_vp(scsi_qla_host_t *vha)
114 int ret;
116 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
117 atomic_set(&vha->loop_state, LOOP_DOWN);
118 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
120 /* Delete all vp's fcports from parent's list */
121 qla2x00_mark_vp_devices_dead(vha);
122 atomic_set(&vha->vp_state, VP_FAILED);
123 vha->flags.management_server_logged_in = 0;
124 if (ret == QLA_SUCCESS) {
125 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
126 } else {
127 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
128 return -1;
130 return 0;
134 qla24xx_enable_vp(scsi_qla_host_t *vha)
136 int ret;
137 scsi_qla_host_t *ha = vha->parent;
139 /* Check if physical ha port is Up */
140 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
141 atomic_read(&ha->loop_state) == LOOP_DEAD ) {
142 vha->vp_err_state = VP_ERR_PORTDWN;
143 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
144 goto enable_failed;
147 /* Initialize the new vport unless it is a persistent port */
148 down(&ha->vport_sem);
149 ret = qla24xx_modify_vp_config(vha);
150 up(&ha->vport_sem);
152 if (ret != QLA_SUCCESS) {
153 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
154 goto enable_failed;
157 DEBUG15(qla_printk(KERN_INFO, ha,
158 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
159 return 0;
161 enable_failed:
162 DEBUG15(qla_printk(KERN_INFO, ha,
163 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
164 return 1;
167 static void
168 qla24xx_configure_vp(scsi_qla_host_t *vha)
170 struct fc_vport *fc_vport;
171 int ret;
173 fc_vport = vha->fc_vport;
175 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
176 vha->host_no, __func__));
177 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
178 if (ret != QLA_SUCCESS) {
179 DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
180 " of RSCN requests: 0x%x\n", ret));
181 return;
182 } else {
183 /* Corresponds to SCR enabled */
184 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
187 vha->flags.online = 1;
188 if (qla24xx_configure_vhba(vha))
189 return;
191 atomic_set(&vha->vp_state, VP_ACTIVE);
192 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
195 void
196 qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
198 int i, vp_idx_matched;
199 scsi_qla_host_t *vha;
201 if (ha->parent)
202 return;
204 for_each_mapped_vp_idx(ha, i) {
205 vp_idx_matched = 0;
207 list_for_each_entry(vha, &ha->vp_list, vp_list) {
208 if (i == vha->vp_idx) {
209 vp_idx_matched = 1;
210 break;
214 if (vp_idx_matched) {
215 switch (mb[0]) {
216 case MBA_LIP_OCCURRED:
217 case MBA_LOOP_UP:
218 case MBA_LOOP_DOWN:
219 case MBA_LIP_RESET:
220 case MBA_POINT_TO_POINT:
221 case MBA_CHG_IN_CONNECTION:
222 case MBA_PORT_UPDATE:
223 case MBA_RSCN_UPDATE:
224 DEBUG15(printk("scsi(%ld)%s: Async_event for"
225 " VP[%d], mb = 0x%x, vha=%p\n",
226 vha->host_no, __func__,i, *mb, vha));
227 qla2x00_async_event(vha, mb);
228 break;
234 void
235 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
238 * Physical port will do most of the abort and recovery work. We can
239 * just treat it as a loop down
241 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
242 atomic_set(&vha->loop_state, LOOP_DOWN);
243 qla2x00_mark_all_devices_lost(vha, 0);
244 } else {
245 if (!atomic_read(&vha->loop_down_timer))
246 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
249 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
250 vha->host_no, vha->vp_idx));
251 qla24xx_enable_vp(vha);
254 static int
255 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
257 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
258 /* VP acquired. complete port configuration */
259 qla24xx_configure_vp(vha);
260 return 0;
263 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
264 qla2x00_vp_abort_isp(vha);
266 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
267 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
268 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
271 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
272 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
273 qla2x00_loop_resync(vha);
274 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
278 return 0;
281 void
282 qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
284 int ret;
285 int i, vp_idx_matched;
286 scsi_qla_host_t *vha;
288 if (ha->parent)
289 return;
290 if (list_empty(&ha->vp_list))
291 return;
293 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
295 for_each_mapped_vp_idx(ha, i) {
296 vp_idx_matched = 0;
298 list_for_each_entry(vha, &ha->vp_list, vp_list) {
299 if (i == vha->vp_idx) {
300 vp_idx_matched = 1;
301 break;
305 if (vp_idx_matched)
306 ret = qla2x00_do_dpc_vp(vha);
311 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
313 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
314 scsi_qla_host_t *vha;
315 uint8_t port_name[WWN_SIZE];
317 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
318 return VPCERR_UNSUPPORTED;
320 /* Check up the F/W and H/W support NPIV */
321 if (!ha->flags.npiv_supported)
322 return VPCERR_UNSUPPORTED;
324 /* Check up whether npiv supported switch presented */
325 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
326 return VPCERR_NO_FABRIC_SUPP;
328 /* Check up unique WWPN */
329 u64_to_wwn(fc_vport->port_name, port_name);
330 if (!memcmp(port_name, ha->port_name, WWN_SIZE))
331 return VPCERR_BAD_WWN;
332 vha = qla24xx_find_vhost_by_name(ha, port_name);
333 if (vha)
334 return VPCERR_BAD_WWN;
336 /* Check up max-npiv-supports */
337 if (ha->num_vhosts > ha->max_npiv_vports) {
338 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
339 "max_npv_vports %ud.\n", ha->host_no,
340 ha->num_vhosts, ha->max_npiv_vports));
341 return VPCERR_UNSUPPORTED;
343 return 0;
346 scsi_qla_host_t *
347 qla24xx_create_vhost(struct fc_vport *fc_vport)
349 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
350 scsi_qla_host_t *vha;
351 struct Scsi_Host *host;
353 host = scsi_host_alloc(&qla24xx_driver_template,
354 sizeof(scsi_qla_host_t));
355 if (!host) {
356 printk(KERN_WARNING
357 "qla2xxx: scsi_host_alloc() failed for vport\n");
358 return(NULL);
361 vha = shost_priv(host);
363 /* clone the parent hba */
364 memcpy(vha, ha, sizeof (scsi_qla_host_t));
366 fc_vport->dd_data = vha;
368 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
369 if (!vha->node_name)
370 goto create_vhost_failed_1;
372 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
373 if (!vha->port_name)
374 goto create_vhost_failed_2;
376 /* New host info */
377 u64_to_wwn(fc_vport->node_name, vha->node_name);
378 u64_to_wwn(fc_vport->port_name, vha->port_name);
380 vha->host = host;
381 vha->host_no = host->host_no;
382 vha->parent = ha;
383 vha->fc_vport = fc_vport;
384 vha->device_flags = 0;
385 vha->instance = num_hosts;
386 vha->vp_idx = qla24xx_allocate_vp_id(vha);
387 if (vha->vp_idx > ha->max_npiv_vports) {
388 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
389 vha->host_no));
390 goto create_vhost_failed_3;
392 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
394 init_completion(&vha->mbx_cmd_comp);
395 complete(&vha->mbx_cmd_comp);
396 init_completion(&vha->mbx_intr_comp);
398 INIT_LIST_HEAD(&vha->list);
399 INIT_LIST_HEAD(&vha->fcports);
400 INIT_LIST_HEAD(&vha->vp_fcports);
402 vha->dpc_flags = 0L;
403 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
404 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
407 * To fix the issue of processing a parent's RSCN for the vport before
408 * its SCR is complete.
410 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
411 atomic_set(&vha->loop_state, LOOP_DOWN);
412 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
414 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
416 host->can_queue = vha->request_q_length + 128;
417 host->this_id = 255;
418 host->cmd_per_lun = 3;
419 host->max_cmd_len = MAX_CMDSZ;
420 host->max_channel = MAX_BUSES - 1;
421 host->max_lun = MAX_LUNS;
422 host->unique_id = vha->instance;
423 host->max_id = MAX_TARGETS_2200;
424 host->transportt = qla2xxx_transport_vport_template;
426 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
427 vha->host_no, vha));
429 vha->flags.init_done = 1;
430 num_hosts++;
432 down(&ha->vport_sem);
433 set_bit(vha->vp_idx, ha->vp_idx_map);
434 ha->cur_vport_count++;
435 up(&ha->vport_sem);
437 return vha;
439 create_vhost_failed_3:
440 kfree(vha->port_name);
442 create_vhost_failed_2:
443 kfree(vha->node_name);
445 create_vhost_failed_1:
446 return NULL;