Initial commit
[wrt350n-kernel.git] / drivers / scsi / qla2xxx / qla_mid.c
blobcf784cdafb010c2b4ad3a3af374df991ea1c9283
1 /*
2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2005 QLogic Corporation
6 * (www.qlogic.com)
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
11 * later version.
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.
19 #include "qla_def.h"
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 *);
33 void
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;
42 static uint32_t
43 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
45 uint32_t vp_id;
46 scsi_qla_host_t *ha = vha->parent;
48 /* Find an empty slot and assign an vp_id */
49 down(&ha->vport_sem);
50 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
51 if (vp_id > ha->max_npiv_vports) {
52 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
53 vp_id, ha->max_npiv_vports));
54 up(&ha->vport_sem);
55 return vp_id;
58 set_bit(vp_id, ha->vp_idx_map);
59 ha->num_vhosts++;
60 vha->vp_idx = vp_id;
61 list_add_tail(&vha->vp_list, &ha->vp_list);
62 up(&ha->vport_sem);
63 return vp_id;
66 void
67 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
69 uint16_t vp_id;
70 scsi_qla_host_t *ha = vha->parent;
72 down(&ha->vport_sem);
73 vp_id = vha->vp_idx;
74 ha->num_vhosts--;
75 clear_bit(vp_id, ha->vp_idx_map);
76 list_del(&vha->vp_list);
77 up(&ha->vport_sem);
80 static scsi_qla_host_t *
81 qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
83 scsi_qla_host_t *vha;
85 /* Locate matching device in database. */
86 list_for_each_entry(vha, &ha->vp_list, vp_list) {
87 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
88 return vha;
90 return NULL;
94 * qla2x00_mark_vp_devices_dead
95 * Updates fcport state when device goes offline.
97 * Input:
98 * ha = adapter block pointer.
99 * fcport = port structure pointer.
101 * Return:
102 * None.
104 * Context:
106 static void
107 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
109 fc_port_t *fcport;
110 scsi_qla_host_t *pha = to_qla_parent(vha);
112 list_for_each_entry(fcport, &pha->fcports, list) {
113 if (fcport->vp_idx != vha->vp_idx)
114 continue;
116 DEBUG15(printk("scsi(%ld): Marking port dead, "
117 "loop_id=0x%04x :%x\n",
118 vha->host_no, fcport->loop_id, fcport->vp_idx));
120 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
121 qla2x00_mark_device_lost(vha, fcport, 0, 0);
126 qla24xx_disable_vp(scsi_qla_host_t *vha)
128 int ret;
130 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
131 atomic_set(&vha->loop_state, LOOP_DOWN);
132 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
134 /* Delete all vp's fcports from parent's list */
135 qla2x00_mark_vp_devices_dead(vha);
136 atomic_set(&vha->vp_state, VP_FAILED);
137 vha->flags.management_server_logged_in = 0;
138 if (ret == QLA_SUCCESS) {
139 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
140 } else {
141 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
142 return -1;
144 return 0;
148 qla24xx_enable_vp(scsi_qla_host_t *vha)
150 int ret;
151 scsi_qla_host_t *ha = vha->parent;
153 /* Check if physical ha port is Up */
154 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
155 atomic_read(&ha->loop_state) == LOOP_DEAD ) {
156 vha->vp_err_state = VP_ERR_PORTDWN;
157 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
158 goto enable_failed;
161 /* Initialize the new vport unless it is a persistent port */
162 down(&ha->vport_sem);
163 ret = qla24xx_modify_vp_config(vha);
164 up(&ha->vport_sem);
166 if (ret != QLA_SUCCESS) {
167 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
168 goto enable_failed;
171 DEBUG15(qla_printk(KERN_INFO, ha,
172 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
173 return 0;
175 enable_failed:
176 DEBUG15(qla_printk(KERN_INFO, ha,
177 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
178 return 1;
181 static void
182 qla24xx_configure_vp(scsi_qla_host_t *vha)
184 struct fc_vport *fc_vport;
185 int ret;
187 fc_vport = vha->fc_vport;
189 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
190 vha->host_no, __func__));
191 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
192 if (ret != QLA_SUCCESS) {
193 DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
194 " of RSCN requests: 0x%x\n", ret));
195 return;
196 } else {
197 /* Corresponds to SCR enabled */
198 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
201 vha->flags.online = 1;
202 if (qla24xx_configure_vhba(vha))
203 return;
205 atomic_set(&vha->vp_state, VP_ACTIVE);
206 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
209 void
210 qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
212 int i, vp_idx_matched;
213 scsi_qla_host_t *vha;
215 if (ha->parent)
216 return;
218 for_each_mapped_vp_idx(ha, i) {
219 vp_idx_matched = 0;
221 list_for_each_entry(vha, &ha->vp_list, vp_list) {
222 if (i == vha->vp_idx) {
223 vp_idx_matched = 1;
224 break;
228 if (vp_idx_matched) {
229 switch (mb[0]) {
230 case MBA_LIP_OCCURRED:
231 case MBA_LOOP_UP:
232 case MBA_LOOP_DOWN:
233 case MBA_LIP_RESET:
234 case MBA_POINT_TO_POINT:
235 case MBA_CHG_IN_CONNECTION:
236 case MBA_PORT_UPDATE:
237 case MBA_RSCN_UPDATE:
238 DEBUG15(printk("scsi(%ld)%s: Async_event for"
239 " VP[%d], mb = 0x%x, vha=%p\n",
240 vha->host_no, __func__,i, *mb, vha));
241 qla2x00_async_event(vha, mb);
242 break;
248 void
249 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
252 * Physical port will do most of the abort and recovery work. We can
253 * just treat it as a loop down
255 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
256 atomic_set(&vha->loop_state, LOOP_DOWN);
257 qla2x00_mark_all_devices_lost(vha, 0);
258 } else {
259 if (!atomic_read(&vha->loop_down_timer))
260 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
263 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
264 vha->host_no, vha->vp_idx));
265 qla24xx_enable_vp(vha);
268 static int
269 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
271 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
272 /* VP acquired. complete port configuration */
273 qla24xx_configure_vp(vha);
274 return 0;
277 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
278 qla2x00_vp_abort_isp(vha);
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 (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
286 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
287 qla2x00_loop_resync(vha);
288 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
292 return 0;
295 void
296 qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
298 int ret;
299 int i, vp_idx_matched;
300 scsi_qla_host_t *vha;
302 if (ha->parent)
303 return;
304 if (list_empty(&ha->vp_list))
305 return;
307 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
309 for_each_mapped_vp_idx(ha, i) {
310 vp_idx_matched = 0;
312 list_for_each_entry(vha, &ha->vp_list, vp_list) {
313 if (i == vha->vp_idx) {
314 vp_idx_matched = 1;
315 break;
319 if (vp_idx_matched)
320 ret = qla2x00_do_dpc_vp(vha);
325 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
327 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
328 scsi_qla_host_t *vha;
329 uint8_t port_name[WWN_SIZE];
331 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
332 return VPCERR_UNSUPPORTED;
334 /* Check up the F/W and H/W support NPIV */
335 if (!ha->flags.npiv_supported)
336 return VPCERR_UNSUPPORTED;
338 /* Check up whether npiv supported switch presented */
339 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
340 return VPCERR_NO_FABRIC_SUPP;
342 /* Check up unique WWPN */
343 u64_to_wwn(fc_vport->port_name, port_name);
344 if (!memcmp(port_name, ha->port_name, WWN_SIZE))
345 return VPCERR_BAD_WWN;
346 vha = qla24xx_find_vhost_by_name(ha, port_name);
347 if (vha)
348 return VPCERR_BAD_WWN;
350 /* Check up max-npiv-supports */
351 if (ha->num_vhosts > ha->max_npiv_vports) {
352 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
353 "max_npv_vports %ud.\n", ha->host_no,
354 ha->num_vhosts, ha->max_npiv_vports));
355 return VPCERR_UNSUPPORTED;
357 return 0;
360 scsi_qla_host_t *
361 qla24xx_create_vhost(struct fc_vport *fc_vport)
363 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
364 scsi_qla_host_t *vha;
365 struct Scsi_Host *host;
367 host = scsi_host_alloc(&qla24xx_driver_template,
368 sizeof(scsi_qla_host_t));
369 if (!host) {
370 printk(KERN_WARNING
371 "qla2xxx: scsi_host_alloc() failed for vport\n");
372 return(NULL);
375 vha = shost_priv(host);
377 /* clone the parent hba */
378 memcpy(vha, ha, sizeof (scsi_qla_host_t));
380 fc_vport->dd_data = vha;
382 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
383 if (!vha->node_name)
384 goto create_vhost_failed_1;
386 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
387 if (!vha->port_name)
388 goto create_vhost_failed_2;
390 /* New host info */
391 u64_to_wwn(fc_vport->node_name, vha->node_name);
392 u64_to_wwn(fc_vport->port_name, vha->port_name);
394 vha->host = host;
395 vha->host_no = host->host_no;
396 vha->parent = ha;
397 vha->fc_vport = fc_vport;
398 vha->device_flags = 0;
399 vha->instance = num_hosts;
400 vha->vp_idx = qla24xx_allocate_vp_id(vha);
401 if (vha->vp_idx > ha->max_npiv_vports) {
402 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
403 vha->host_no));
404 goto create_vhost_failed_3;
406 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
408 init_completion(&vha->mbx_cmd_comp);
409 complete(&vha->mbx_cmd_comp);
410 init_completion(&vha->mbx_intr_comp);
412 INIT_LIST_HEAD(&vha->list);
413 INIT_LIST_HEAD(&vha->fcports);
414 INIT_LIST_HEAD(&vha->vp_fcports);
416 vha->dpc_flags = 0L;
417 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
418 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
421 * To fix the issue of processing a parent's RSCN for the vport before
422 * its SCR is complete.
424 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
425 atomic_set(&vha->loop_state, LOOP_DOWN);
426 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
428 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
430 host->can_queue = vha->request_q_length + 128;
431 host->this_id = 255;
432 host->cmd_per_lun = 3;
433 host->max_cmd_len = MAX_CMDSZ;
434 host->max_channel = MAX_BUSES - 1;
435 host->max_lun = MAX_LUNS;
436 host->unique_id = vha->instance;
437 host->max_id = MAX_TARGETS_2200;
438 host->transportt = qla2xxx_transport_vport_template;
440 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
441 vha->host_no, vha));
443 vha->flags.init_done = 1;
444 num_hosts++;
446 down(&ha->vport_sem);
447 set_bit(vha->vp_idx, ha->vp_idx_map);
448 ha->cur_vport_count++;
449 up(&ha->vport_sem);
451 return vha;
453 create_vhost_failed_3:
454 kfree(vha->port_name);
456 create_vhost_failed_2:
457 kfree(vha->node_name);
459 create_vhost_failed_1:
460 return NULL;