2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2005 QLogic Corporation
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
21 #include <linux/version.h>
22 #include <linux/moduleparam.h>
23 #include <linux/vmalloc.h>
24 #include <linux/smp_lock.h>
25 #include <linux/list.h>
27 #include <scsi/scsi_tcq.h>
28 #include <scsi/scsicam.h>
29 #include <linux/delay.h>
31 void qla2x00_vp_stop_timer(scsi_qla_host_t *);
34 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
36 if (vha->parent && vha->timer_active) {
37 del_timer_sync(&vha->timer);
38 vha->timer_active = 0;
43 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
46 scsi_qla_host_t *ha = vha->parent;
48 /* Find an empty slot and assign an vp_id */
50 vp_id = find_first_zero_bit((unsigned long *)ha->vp_idx_map,
52 if (vp_id > MAX_MULTI_ID_FABRIC) {
53 DEBUG15(printk ("vp_id %d is bigger than MAX_MULTI_ID_FABRID\n",
59 set_bit(vp_id, (unsigned long *)ha->vp_idx_map);
62 list_add_tail(&vha->vp_list, &ha->vp_list);
68 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
71 scsi_qla_host_t *ha = vha->parent;
76 clear_bit(vp_id, (unsigned long *)ha->vp_idx_map);
77 list_del(&vha->vp_list);
82 qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
86 /* Locate matching device in database. */
87 list_for_each_entry(vha, &ha->vp_list, vp_list) {
88 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
95 * qla2x00_mark_vp_devices_dead
96 * Updates fcport state when device goes offline.
99 * ha = adapter block pointer.
100 * fcport = port structure pointer.
108 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
111 scsi_qla_host_t *pha = to_qla_parent(vha);
113 list_for_each_entry(fcport, &pha->fcports, list) {
114 if (fcport->vp_idx != vha->vp_idx)
117 DEBUG15(printk("scsi(%ld): Marking port dead, "
118 "loop_id=0x%04x :%x\n",
119 vha->host_no, fcport->loop_id, fcport->vp_idx));
121 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
122 qla2x00_mark_device_lost(vha, fcport, 0, 0);
127 qla24xx_disable_vp(scsi_qla_host_t *vha)
131 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
132 atomic_set(&vha->loop_state, LOOP_DOWN);
133 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
135 /* Delete all vp's fcports from parent's list */
136 qla2x00_mark_vp_devices_dead(vha);
137 atomic_set(&vha->vp_state, VP_FAILED);
138 vha->flags.management_server_logged_in = 0;
139 if (ret == QLA_SUCCESS) {
140 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
142 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
149 qla24xx_enable_vp(scsi_qla_host_t *vha)
152 scsi_qla_host_t *ha = vha->parent;
154 /* Check if physical ha port is Up */
155 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
156 atomic_read(&ha->loop_state) == LOOP_DEAD ) {
157 vha->vp_err_state = VP_ERR_PORTDWN;
158 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
162 /* Initialize the new vport unless it is a persistent port */
163 down(&ha->vport_sem);
164 ret = qla24xx_modify_vp_config(vha);
167 if (ret != QLA_SUCCESS) {
168 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
172 DEBUG15(qla_printk(KERN_INFO, ha,
173 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
177 DEBUG15(qla_printk(KERN_INFO, ha,
178 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
183 qla24xx_configure_vp(scsi_qla_host_t *vha)
185 struct fc_vport *fc_vport;
188 fc_vport = vha->fc_vport;
190 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
191 vha->host_no, __func__));
192 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
193 if (ret != QLA_SUCCESS) {
194 DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
195 " of RSCN requests: 0x%x\n", ret));
198 /* Corresponds to SCR enabled */
199 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
202 vha->flags.online = 1;
203 if (qla24xx_configure_vhba(vha))
206 atomic_set(&vha->vp_state, VP_ACTIVE);
207 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
211 qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
213 int i, vp_idx_matched;
214 scsi_qla_host_t *vha;
219 i = find_next_bit((unsigned long *)ha->vp_idx_map,
220 MAX_MULTI_ID_FABRIC + 1, 1);
221 for (;i <= MAX_MULTI_ID_FABRIC;
222 i = find_next_bit((unsigned long *)ha->vp_idx_map,
223 MAX_MULTI_ID_FABRIC + 1, i + 1)) {
226 list_for_each_entry(vha, &ha->vp_list, vp_list) {
227 if (i == vha->vp_idx) {
233 if (vp_idx_matched) {
235 case MBA_LIP_OCCURRED:
239 case MBA_POINT_TO_POINT:
240 case MBA_CHG_IN_CONNECTION:
241 case MBA_PORT_UPDATE:
242 case MBA_RSCN_UPDATE:
243 DEBUG15(printk("scsi(%ld)%s: Async_event for"
244 " VP[%d], mb = 0x%x, vha=%p\n",
245 vha->host_no, __func__,i, *mb, vha));
246 qla2x00_async_event(vha, mb);
254 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
257 * Physical port will do most of the abort and recovery work. We can
258 * just treat it as a loop down
260 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
261 atomic_set(&vha->loop_state, LOOP_DOWN);
262 qla2x00_mark_all_devices_lost(vha, 0);
264 if (!atomic_read(&vha->loop_down_timer))
265 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
268 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
269 vha->host_no, vha->vp_idx));
270 qla24xx_enable_vp(vha);
274 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
276 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
277 /* VP acquired. complete port configuration */
278 qla24xx_configure_vp(vha);
282 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
283 qla2x00_vp_abort_isp(vha);
285 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
286 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
287 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
290 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
291 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
292 qla2x00_loop_resync(vha);
293 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
301 qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
304 int i, vp_idx_matched;
305 scsi_qla_host_t *vha;
309 if (list_empty(&ha->vp_list))
312 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
314 i = find_next_bit((unsigned long *)ha->vp_idx_map,
315 MAX_MULTI_ID_FABRIC + 1, 1);
316 for (;i <= MAX_MULTI_ID_FABRIC;
317 i = find_next_bit((unsigned long *)ha->vp_idx_map,
318 MAX_MULTI_ID_FABRIC + 1, i + 1)) {
321 list_for_each_entry(vha, &ha->vp_list, vp_list) {
322 if (i == vha->vp_idx) {
329 ret = qla2x00_do_dpc_vp(vha);
334 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
336 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
337 scsi_qla_host_t *vha;
338 uint8_t port_name[WWN_SIZE];
340 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
341 return VPCERR_UNSUPPORTED;
343 /* Check up the F/W and H/W support NPIV */
344 if (!ha->flags.npiv_supported)
345 return VPCERR_UNSUPPORTED;
347 /* Check up whether npiv supported switch presented */
348 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
349 return VPCERR_NO_FABRIC_SUPP;
351 /* Check up unique WWPN */
352 u64_to_wwn(fc_vport->port_name, port_name);
353 vha = qla24xx_find_vhost_by_name(ha, port_name);
355 return VPCERR_BAD_WWN;
357 /* Check up max-npiv-supports */
358 if (ha->num_vhosts > ha->max_npiv_vports) {
359 DEBUG15(printk("scsi(%ld): num_vhosts %d is bigger than "
360 "max_npv_vports %d.\n", ha->host_no,
361 (uint16_t) ha->num_vhosts, (int) ha->max_npiv_vports));
362 return VPCERR_UNSUPPORTED;
368 qla24xx_create_vhost(struct fc_vport *fc_vport)
370 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
371 scsi_qla_host_t *vha;
372 struct Scsi_Host *host;
374 host = scsi_host_alloc(&qla24xx_driver_template,
375 sizeof(scsi_qla_host_t));
378 "qla2xxx: scsi_host_alloc() failed for vport\n");
382 vha = shost_priv(host);
384 /* clone the parent hba */
385 memcpy(vha, ha, sizeof (scsi_qla_host_t));
387 fc_vport->dd_data = vha;
389 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
391 goto create_vhost_failed_1;
393 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
395 goto create_vhost_failed_2;
398 u64_to_wwn(fc_vport->node_name, vha->node_name);
399 u64_to_wwn(fc_vport->port_name, vha->port_name);
402 vha->host_no = host->host_no;
404 vha->fc_vport = fc_vport;
405 vha->device_flags = 0;
406 vha->instance = num_hosts;
407 vha->vp_idx = qla24xx_allocate_vp_id(vha);
408 if (vha->vp_idx > ha->max_npiv_vports) {
409 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
411 goto create_vhost_failed_3;
413 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
415 init_MUTEX(&vha->mbx_cmd_sem);
416 init_MUTEX_LOCKED(&vha->mbx_intr_sem);
418 INIT_LIST_HEAD(&vha->list);
419 INIT_LIST_HEAD(&vha->fcports);
420 INIT_LIST_HEAD(&vha->vp_fcports);
423 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
424 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
427 * To fix the issue of processing a parent's RSCN for the vport before
428 * its SCR is complete.
430 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
431 atomic_set(&vha->loop_state, LOOP_DOWN);
432 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
434 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
436 host->can_queue = vha->request_q_length + 128;
438 host->cmd_per_lun = 3;
439 host->max_cmd_len = MAX_CMDSZ;
440 host->max_channel = MAX_BUSES - 1;
441 host->max_lun = MAX_LUNS;
442 host->unique_id = vha->instance;
443 host->max_id = MAX_TARGETS_2200;
444 host->transportt = qla2xxx_transport_vport_template;
446 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
449 vha->flags.init_done = 1;
452 down(&ha->vport_sem);
453 set_bit(vha->vp_idx, (unsigned long *)ha->vp_idx_map);
454 ha->cur_vport_count++;
459 create_vhost_failed_3:
460 kfree(vha->port_name);
462 create_vhost_failed_2:
463 kfree(vha->node_name);
465 create_vhost_failed_1: