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_modify_vport() - Modifies the virtual fabric port's configuration
185 * @vp: pointer to buffer of virtual port parameters.
186 * @ret_code: return error code:
188 * Returns the virtual port id, or MAX_VSAN_ID, if couldn't create.
191 qla24xx_modify_vhba(scsi_qla_host_t *ha, vport_params_t *vp, uint32_t *vp_id)
193 scsi_qla_host_t *vha;
195 vha = qla24xx_find_vhost_by_name(ha, vp->port_name);
197 *vp_id = MAX_NUM_VPORT_LOOP;
198 return VP_RET_CODE_WWPN;
201 if (qla24xx_enable_vp(vha)) {
202 scsi_host_put(vha->host);
203 qla2x00_mem_free(vha);
204 *vp_id = MAX_NUM_VPORT_LOOP;
205 return VP_RET_CODE_RESOURCES;
208 *vp_id = vha->vp_idx;
209 return VP_RET_CODE_OK;
213 qla24xx_configure_vp(scsi_qla_host_t *vha)
215 struct fc_vport *fc_vport;
218 fc_vport = vha->fc_vport;
220 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
221 vha->host_no, __func__));
222 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
223 if (ret != QLA_SUCCESS) {
224 DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
225 " of RSCN requests: 0x%x\n", ret));
228 /* Corresponds to SCR enabled */
229 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
232 vha->flags.online = 1;
233 if (qla24xx_configure_vhba(vha))
236 atomic_set(&vha->vp_state, VP_ACTIVE);
237 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
241 qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
243 int i, vp_idx_matched;
244 scsi_qla_host_t *vha;
249 i = find_next_bit((unsigned long *)ha->vp_idx_map,
250 MAX_MULTI_ID_FABRIC + 1, 1);
251 for (;i <= MAX_MULTI_ID_FABRIC;
252 i = find_next_bit((unsigned long *)ha->vp_idx_map,
253 MAX_MULTI_ID_FABRIC + 1, i + 1)) {
256 list_for_each_entry(vha, &ha->vp_list, vp_list) {
257 if (i == vha->vp_idx) {
263 if (vp_idx_matched) {
265 case MBA_LIP_OCCURRED:
269 case MBA_POINT_TO_POINT:
270 case MBA_CHG_IN_CONNECTION:
271 case MBA_PORT_UPDATE:
272 case MBA_RSCN_UPDATE:
273 DEBUG15(printk("scsi(%ld)%s: Async_event for"
274 " VP[%d], mb = 0x%x, vha=%p\n",
275 vha->host_no, __func__,i, *mb, vha));
276 qla2x00_async_event(vha, mb);
284 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
287 * Physical port will do most of the abort and recovery work. We can
288 * just treat it as a loop down
290 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
291 atomic_set(&vha->loop_state, LOOP_DOWN);
292 qla2x00_mark_all_devices_lost(vha, 0);
294 if (!atomic_read(&vha->loop_down_timer))
295 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
298 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
299 vha->host_no, vha->vp_idx));
300 qla24xx_enable_vp(vha);
304 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
306 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
307 /* VP acquired. complete port configuration */
308 qla24xx_configure_vp(vha);
312 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
313 qla2x00_vp_abort_isp(vha);
315 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
316 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
317 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
320 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
321 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
322 qla2x00_loop_resync(vha);
323 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
331 qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
334 int i, vp_idx_matched;
335 scsi_qla_host_t *vha;
339 if (list_empty(&ha->vp_list))
342 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
344 i = find_next_bit((unsigned long *)ha->vp_idx_map,
345 MAX_MULTI_ID_FABRIC + 1, 1);
346 for (;i <= MAX_MULTI_ID_FABRIC;
347 i = find_next_bit((unsigned long *)ha->vp_idx_map,
348 MAX_MULTI_ID_FABRIC + 1, i + 1)) {
351 list_for_each_entry(vha, &ha->vp_list, vp_list) {
352 if (i == vha->vp_idx) {
359 ret = qla2x00_do_dpc_vp(vha);
364 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
366 scsi_qla_host_t *ha = (scsi_qla_host_t *) fc_vport->shost->hostdata;
367 scsi_qla_host_t *vha;
368 uint8_t port_name[WWN_SIZE];
370 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
371 return VPCERR_UNSUPPORTED;
373 /* Check up the F/W and H/W support NPIV */
374 if (!ha->flags.npiv_supported)
375 return VPCERR_UNSUPPORTED;
377 /* Check up whether npiv supported switch presented */
378 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
379 return VPCERR_NO_FABRIC_SUPP;
381 /* Check up unique WWPN */
382 u64_to_wwn(fc_vport->port_name, port_name);
383 vha = qla24xx_find_vhost_by_name(ha, port_name);
385 return VPCERR_BAD_WWN;
387 /* Check up max-npiv-supports */
388 if (ha->num_vhosts > ha->max_npiv_vports) {
389 DEBUG15(printk("scsi(%ld): num_vhosts %d is bigger than "
390 "max_npv_vports %d.\n", ha->host_no,
391 (uint16_t) ha->num_vhosts, (int) ha->max_npiv_vports));
392 return VPCERR_UNSUPPORTED;
398 qla24xx_create_vhost(struct fc_vport *fc_vport)
400 scsi_qla_host_t *ha = (scsi_qla_host_t *) fc_vport->shost->hostdata;
401 scsi_qla_host_t *vha;
402 struct Scsi_Host *host;
404 host = scsi_host_alloc(&qla24xx_driver_template,
405 sizeof(scsi_qla_host_t));
408 "qla2xxx: scsi_host_alloc() failed for vport\n");
412 vha = (scsi_qla_host_t *)host->hostdata;
414 /* clone the parent hba */
415 memcpy(vha, ha, sizeof (scsi_qla_host_t));
417 fc_vport->dd_data = vha;
419 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
421 goto create_vhost_failed_1;
423 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
425 goto create_vhost_failed_2;
428 u64_to_wwn(fc_vport->node_name, vha->node_name);
429 u64_to_wwn(fc_vport->port_name, vha->port_name);
432 vha->host_no = host->host_no;
434 vha->fc_vport = fc_vport;
435 vha->device_flags = 0;
436 vha->instance = num_hosts;
437 vha->vp_idx = qla24xx_allocate_vp_id(vha);
438 if (vha->vp_idx > ha->max_npiv_vports) {
439 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
441 goto create_vhost_failed_3;
443 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
445 init_MUTEX(&vha->mbx_cmd_sem);
446 init_MUTEX_LOCKED(&vha->mbx_intr_sem);
448 INIT_LIST_HEAD(&vha->list);
449 INIT_LIST_HEAD(&vha->fcports);
450 INIT_LIST_HEAD(&vha->vp_fcports);
453 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
454 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
457 * To fix the issue of processing a parent's RSCN for the vport before
458 * its SCR is complete.
460 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
461 atomic_set(&vha->loop_state, LOOP_DOWN);
462 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
464 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
466 host->can_queue = vha->request_q_length + 128;
468 host->cmd_per_lun = 3;
469 host->max_cmd_len = MAX_CMDSZ;
470 host->max_channel = MAX_BUSES - 1;
471 host->max_lun = MAX_LUNS;
472 host->unique_id = vha->instance;
473 host->max_id = MAX_TARGETS_2200;
474 host->transportt = qla2xxx_transport_vport_template;
476 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
479 vha->flags.init_done = 1;
482 down(&ha->vport_sem);
483 set_bit(vha->vp_idx, (unsigned long *)ha->vp_idx_map);
484 ha->cur_vport_count++;
489 create_vhost_failed_3:
490 kfree(vha->port_name);
492 create_vhost_failed_2:
493 kfree(vha->node_name);
495 create_vhost_failed_1: