2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
9 #include <linux/moduleparam.h>
10 #include <linux/vmalloc.h>
11 #include <linux/smp_lock.h>
12 #include <linux/list.h>
14 #include <scsi/scsi_tcq.h>
15 #include <scsi/scsicam.h>
16 #include <linux/delay.h>
19 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
21 if (vha->parent && vha->timer_active) {
22 del_timer_sync(&vha->timer);
23 vha->timer_active = 0;
28 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
31 scsi_qla_host_t *ha = vha->parent;
33 /* Find an empty slot and assign an vp_id */
34 mutex_lock(&ha->vport_lock);
35 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
36 if (vp_id > ha->max_npiv_vports) {
37 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
38 vp_id, ha->max_npiv_vports));
39 mutex_unlock(&ha->vport_lock);
43 set_bit(vp_id, ha->vp_idx_map);
45 ha->cur_vport_count++;
47 list_add_tail(&vha->vp_list, &ha->vp_list);
48 mutex_unlock(&ha->vport_lock);
53 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
56 scsi_qla_host_t *ha = vha->parent;
58 mutex_lock(&ha->vport_lock);
61 ha->cur_vport_count--;
62 clear_bit(vp_id, ha->vp_idx_map);
63 list_del(&vha->vp_list);
64 mutex_unlock(&ha->vport_lock);
67 static scsi_qla_host_t *
68 qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
72 /* Locate matching device in database. */
73 list_for_each_entry(vha, &ha->vp_list, vp_list) {
74 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
81 * qla2x00_mark_vp_devices_dead
82 * Updates fcport state when device goes offline.
85 * ha = adapter block pointer.
86 * fcport = port structure pointer.
94 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
97 scsi_qla_host_t *pha = to_qla_parent(vha);
99 list_for_each_entry(fcport, &pha->fcports, list) {
100 if (fcport->vp_idx != vha->vp_idx)
103 DEBUG15(printk("scsi(%ld): Marking port dead, "
104 "loop_id=0x%04x :%x\n",
105 vha->host_no, fcport->loop_id, fcport->vp_idx));
107 qla2x00_mark_device_lost(vha, fcport, 0, 0);
108 atomic_set(&fcport->state, FCS_UNCONFIGURED);
113 qla24xx_disable_vp(scsi_qla_host_t *vha)
117 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
118 atomic_set(&vha->loop_state, LOOP_DOWN);
119 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
121 /* Delete all vp's fcports from parent's list */
122 qla2x00_mark_vp_devices_dead(vha);
123 atomic_set(&vha->vp_state, VP_FAILED);
124 vha->flags.management_server_logged_in = 0;
125 if (ret == QLA_SUCCESS) {
126 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
128 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
135 qla24xx_enable_vp(scsi_qla_host_t *vha)
138 scsi_qla_host_t *ha = vha->parent;
140 /* Check if physical ha port is Up */
141 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
142 atomic_read(&ha->loop_state) == LOOP_DEAD ) {
143 vha->vp_err_state = VP_ERR_PORTDWN;
144 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
148 /* Initialize the new vport unless it is a persistent port */
149 mutex_lock(&ha->vport_lock);
150 ret = qla24xx_modify_vp_config(vha);
151 mutex_unlock(&ha->vport_lock);
153 if (ret != QLA_SUCCESS) {
154 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
158 DEBUG15(qla_printk(KERN_INFO, ha,
159 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
163 DEBUG15(qla_printk(KERN_INFO, ha,
164 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
169 qla24xx_configure_vp(scsi_qla_host_t *vha)
171 struct fc_vport *fc_vport;
174 fc_vport = vha->fc_vport;
176 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
177 vha->host_no, __func__));
178 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
179 if (ret != QLA_SUCCESS) {
180 DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
181 " of RSCN requests: 0x%x\n", ret));
184 /* Corresponds to SCR enabled */
185 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
188 vha->flags.online = 1;
189 if (qla24xx_configure_vhba(vha))
192 atomic_set(&vha->vp_state, VP_ACTIVE);
193 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
197 qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
199 int i, vp_idx_matched;
200 scsi_qla_host_t *vha;
205 for_each_mapped_vp_idx(ha, i) {
208 list_for_each_entry(vha, &ha->vp_list, vp_list) {
209 if (i == vha->vp_idx) {
215 if (vp_idx_matched) {
217 case MBA_LIP_OCCURRED:
221 case MBA_POINT_TO_POINT:
222 case MBA_CHG_IN_CONNECTION:
223 case MBA_PORT_UPDATE:
224 case MBA_RSCN_UPDATE:
225 DEBUG15(printk("scsi(%ld)%s: Async_event for"
226 " VP[%d], mb = 0x%x, vha=%p\n",
227 vha->host_no, __func__,i, *mb, vha));
228 qla2x00_async_event(vha, mb);
236 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
239 * Physical port will do most of the abort and recovery work. We can
240 * just treat it as a loop down
242 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
243 atomic_set(&vha->loop_state, LOOP_DOWN);
244 qla2x00_mark_all_devices_lost(vha, 0);
246 if (!atomic_read(&vha->loop_down_timer))
247 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
250 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
251 vha->host_no, vha->vp_idx));
252 qla24xx_enable_vp(vha);
256 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
258 scsi_qla_host_t *ha = vha->parent;
260 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
261 /* VP acquired. complete port configuration */
262 if (atomic_read(&ha->loop_state) == LOOP_READY) {
263 qla24xx_configure_vp(vha);
265 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
266 set_bit(VP_DPC_NEEDED, &ha->dpc_flags);
272 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
273 qla2x00_vp_abort_isp(vha);
275 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
276 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
277 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
280 if (atomic_read(&vha->vp_state) == VP_ACTIVE &&
281 test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
282 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
283 qla2x00_loop_resync(vha);
284 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
292 qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
295 int i, vp_idx_matched;
296 scsi_qla_host_t *vha;
300 if (list_empty(&ha->vp_list))
303 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
305 for_each_mapped_vp_idx(ha, i) {
308 list_for_each_entry(vha, &ha->vp_list, vp_list) {
309 if (i == vha->vp_idx) {
316 ret = qla2x00_do_dpc_vp(vha);
321 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
323 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
324 scsi_qla_host_t *vha;
325 uint8_t port_name[WWN_SIZE];
327 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
328 return VPCERR_UNSUPPORTED;
330 /* Check up the F/W and H/W support NPIV */
331 if (!ha->flags.npiv_supported)
332 return VPCERR_UNSUPPORTED;
334 /* Check up whether npiv supported switch presented */
335 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
336 return VPCERR_NO_FABRIC_SUPP;
338 /* Check up unique WWPN */
339 u64_to_wwn(fc_vport->port_name, port_name);
340 if (!memcmp(port_name, ha->port_name, WWN_SIZE))
341 return VPCERR_BAD_WWN;
342 vha = qla24xx_find_vhost_by_name(ha, port_name);
344 return VPCERR_BAD_WWN;
346 /* Check up max-npiv-supports */
347 if (ha->num_vhosts > ha->max_npiv_vports) {
348 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
349 "max_npv_vports %ud.\n", ha->host_no,
350 ha->num_vhosts, ha->max_npiv_vports));
351 return VPCERR_UNSUPPORTED;
357 qla24xx_create_vhost(struct fc_vport *fc_vport)
359 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
360 scsi_qla_host_t *vha;
361 struct Scsi_Host *host;
363 host = scsi_host_alloc(&qla24xx_driver_template,
364 sizeof(scsi_qla_host_t));
367 "qla2xxx: scsi_host_alloc() failed for vport\n");
371 vha = shost_priv(host);
373 /* clone the parent hba */
374 memcpy(vha, ha, sizeof (scsi_qla_host_t));
376 fc_vport->dd_data = vha;
378 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
380 goto create_vhost_failed_1;
382 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
384 goto create_vhost_failed_2;
387 u64_to_wwn(fc_vport->node_name, vha->node_name);
388 u64_to_wwn(fc_vport->port_name, vha->port_name);
391 vha->host_no = host->host_no;
393 vha->fc_vport = fc_vport;
394 vha->device_flags = 0;
395 vha->vp_idx = qla24xx_allocate_vp_id(vha);
396 if (vha->vp_idx > ha->max_npiv_vports) {
397 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
399 goto create_vhost_failed_3;
401 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
403 init_completion(&vha->mbx_cmd_comp);
404 complete(&vha->mbx_cmd_comp);
405 init_completion(&vha->mbx_intr_comp);
407 INIT_LIST_HEAD(&vha->list);
408 INIT_LIST_HEAD(&vha->fcports);
409 INIT_LIST_HEAD(&vha->vp_fcports);
410 INIT_LIST_HEAD(&vha->work_list);
413 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
414 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
417 * To fix the issue of processing a parent's RSCN for the vport before
418 * its SCR is complete.
420 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
421 atomic_set(&vha->loop_state, LOOP_DOWN);
422 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
424 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
426 host->can_queue = vha->request_q_length + 128;
428 host->cmd_per_lun = 3;
429 host->max_cmd_len = MAX_CMDSZ;
430 host->max_channel = MAX_BUSES - 1;
431 host->max_lun = MAX_LUNS;
432 host->unique_id = host->host_no;
433 host->max_id = MAX_TARGETS_2200;
434 host->transportt = qla2xxx_transport_vport_template;
436 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
439 vha->flags.init_done = 1;
443 create_vhost_failed_3:
444 kfree(vha->port_name);
446 create_vhost_failed_2:
447 kfree(vha->node_name);
449 create_vhost_failed_1: