[SCSI] qla2xxx: Update copyright banner.
[safe/jmp/linux-2.6] / drivers / scsi / qla2xxx / qla_mid.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
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>
14
15 #include <scsi/scsi_tcq.h>
16 #include <scsi/scsicam.h>
17 #include <linux/delay.h>
18
19 void qla2x00_vp_stop_timer(scsi_qla_host_t *);
20
21 void
22 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
23 {
24         if (vha->parent && vha->timer_active) {
25                 del_timer_sync(&vha->timer);
26                 vha->timer_active = 0;
27         }
28 }
29
30 static uint32_t
31 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
32 {
33         uint32_t vp_id;
34         scsi_qla_host_t *ha = vha->parent;
35
36         /* Find an empty slot and assign an vp_id */
37         down(&ha->vport_sem);
38         vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
39         if (vp_id > ha->max_npiv_vports) {
40                 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
41                     vp_id, ha->max_npiv_vports));
42                 up(&ha->vport_sem);
43                 return vp_id;
44         }
45
46         set_bit(vp_id, ha->vp_idx_map);
47         ha->num_vhosts++;
48         vha->vp_idx = vp_id;
49         list_add_tail(&vha->vp_list, &ha->vp_list);
50         up(&ha->vport_sem);
51         return vp_id;
52 }
53
54 void
55 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
56 {
57         uint16_t vp_id;
58         scsi_qla_host_t *ha = vha->parent;
59
60         down(&ha->vport_sem);
61         vp_id = vha->vp_idx;
62         ha->num_vhosts--;
63         clear_bit(vp_id, ha->vp_idx_map);
64         list_del(&vha->vp_list);
65         up(&ha->vport_sem);
66 }
67
68 static scsi_qla_host_t *
69 qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
70 {
71         scsi_qla_host_t *vha;
72
73         /* Locate matching device in database. */
74         list_for_each_entry(vha, &ha->vp_list, vp_list) {
75                 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
76                         return vha;
77         }
78         return NULL;
79 }
80
81 /*
82  * qla2x00_mark_vp_devices_dead
83  *      Updates fcport state when device goes offline.
84  *
85  * Input:
86  *      ha = adapter block pointer.
87  *      fcport = port structure pointer.
88  *
89  * Return:
90  *      None.
91  *
92  * Context:
93  */
94 static void
95 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
96 {
97         fc_port_t *fcport;
98         scsi_qla_host_t *pha = to_qla_parent(vha);
99
100         list_for_each_entry(fcport, &pha->fcports, list) {
101                 if (fcport->vp_idx != vha->vp_idx)
102                         continue;
103
104                 DEBUG15(printk("scsi(%ld): Marking port dead, "
105                     "loop_id=0x%04x :%x\n",
106                     vha->host_no, fcport->loop_id, fcport->vp_idx));
107
108                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
109                 qla2x00_mark_device_lost(vha, fcport, 0, 0);
110         }
111 }
112
113 int
114 qla24xx_disable_vp(scsi_qla_host_t *vha)
115 {
116         int ret;
117
118         ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
119         atomic_set(&vha->loop_state, LOOP_DOWN);
120         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
121
122         /* Delete all vp's fcports from parent's list */
123         qla2x00_mark_vp_devices_dead(vha);
124         atomic_set(&vha->vp_state, VP_FAILED);
125         vha->flags.management_server_logged_in = 0;
126         if (ret == QLA_SUCCESS) {
127                 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
128         } else {
129                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
130                 return -1;
131         }
132         return 0;
133 }
134
135 int
136 qla24xx_enable_vp(scsi_qla_host_t *vha)
137 {
138         int ret;
139         scsi_qla_host_t *ha = vha->parent;
140
141         /* Check if physical ha port is Up */
142         if (atomic_read(&ha->loop_state) == LOOP_DOWN  ||
143                 atomic_read(&ha->loop_state) == LOOP_DEAD ) {
144                 vha->vp_err_state =  VP_ERR_PORTDWN;
145                 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
146                 goto enable_failed;
147         }
148
149         /* Initialize the new vport unless it is a persistent port */
150         down(&ha->vport_sem);
151         ret = qla24xx_modify_vp_config(vha);
152         up(&ha->vport_sem);
153
154         if (ret != QLA_SUCCESS) {
155                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
156                 goto enable_failed;
157         }
158
159         DEBUG15(qla_printk(KERN_INFO, ha,
160             "Virtual port with id: %d - Enabled\n", vha->vp_idx));
161         return 0;
162
163 enable_failed:
164         DEBUG15(qla_printk(KERN_INFO, ha,
165             "Virtual port with id: %d - Disabled\n", vha->vp_idx));
166         return 1;
167 }
168
169 static void
170 qla24xx_configure_vp(scsi_qla_host_t *vha)
171 {
172         struct fc_vport *fc_vport;
173         int ret;
174
175         fc_vport = vha->fc_vport;
176
177         DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
178             vha->host_no, __func__));
179         ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
180         if (ret != QLA_SUCCESS) {
181                 DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
182                     " of RSCN requests: 0x%x\n", ret));
183                 return;
184         } else {
185                 /* Corresponds to SCR enabled */
186                 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
187         }
188
189         vha->flags.online = 1;
190         if (qla24xx_configure_vhba(vha))
191                 return;
192
193         atomic_set(&vha->vp_state, VP_ACTIVE);
194         fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
195 }
196
197 void
198 qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
199 {
200         int i, vp_idx_matched;
201         scsi_qla_host_t *vha;
202
203         if (ha->parent)
204                 return;
205
206         for_each_mapped_vp_idx(ha, i) {
207                 vp_idx_matched = 0;
208
209                 list_for_each_entry(vha, &ha->vp_list, vp_list) {
210                         if (i == vha->vp_idx) {
211                                 vp_idx_matched = 1;
212                                 break;
213                         }
214                 }
215
216                 if (vp_idx_matched) {
217                         switch (mb[0]) {
218                         case MBA_LIP_OCCURRED:
219                         case MBA_LOOP_UP:
220                         case MBA_LOOP_DOWN:
221                         case MBA_LIP_RESET:
222                         case MBA_POINT_TO_POINT:
223                         case MBA_CHG_IN_CONNECTION:
224                         case MBA_PORT_UPDATE:
225                         case MBA_RSCN_UPDATE:
226                                 DEBUG15(printk("scsi(%ld)%s: Async_event for"
227                                     " VP[%d], mb = 0x%x, vha=%p\n",
228                                     vha->host_no, __func__,i, *mb, vha));
229                                 qla2x00_async_event(vha, mb);
230                                 break;
231                         }
232                 }
233         }
234 }
235
236 void
237 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
238 {
239         /*
240          * Physical port will do most of the abort and recovery work. We can
241          * just treat it as a loop down
242          */
243         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
244                 atomic_set(&vha->loop_state, LOOP_DOWN);
245                 qla2x00_mark_all_devices_lost(vha, 0);
246         } else {
247                 if (!atomic_read(&vha->loop_down_timer))
248                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
249         }
250
251         DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
252             vha->host_no, vha->vp_idx));
253         qla24xx_enable_vp(vha);
254 }
255
256 static int
257 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
258 {
259         if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
260                 /* VP acquired. complete port configuration */
261                 qla24xx_configure_vp(vha);
262                 return 0;
263         }
264
265         if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
266                 qla2x00_vp_abort_isp(vha);
267
268         if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
269             (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
270                 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
271         }
272
273         if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
274                 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
275                         qla2x00_loop_resync(vha);
276                         clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
277                 }
278         }
279
280         return 0;
281 }
282
283 void
284 qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
285 {
286         int ret;
287         int i, vp_idx_matched;
288         scsi_qla_host_t *vha;
289
290         if (ha->parent)
291                 return;
292         if (list_empty(&ha->vp_list))
293                 return;
294
295         clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
296
297         for_each_mapped_vp_idx(ha, i) {
298                 vp_idx_matched = 0;
299
300                 list_for_each_entry(vha, &ha->vp_list, vp_list) {
301                         if (i == vha->vp_idx) {
302                                 vp_idx_matched = 1;
303                                 break;
304                         }
305                 }
306
307                 if (vp_idx_matched)
308                         ret = qla2x00_do_dpc_vp(vha);
309         }
310 }
311
312 int
313 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
314 {
315         scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
316         scsi_qla_host_t *vha;
317         uint8_t port_name[WWN_SIZE];
318
319         if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
320                 return VPCERR_UNSUPPORTED;
321
322         /* Check up the F/W and H/W support NPIV */
323         if (!ha->flags.npiv_supported)
324                 return VPCERR_UNSUPPORTED;
325
326         /* Check up whether npiv supported switch presented */
327         if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
328                 return VPCERR_NO_FABRIC_SUPP;
329
330         /* Check up unique WWPN */
331         u64_to_wwn(fc_vport->port_name, port_name);
332         if (!memcmp(port_name, ha->port_name, WWN_SIZE))
333                 return VPCERR_BAD_WWN;
334         vha = qla24xx_find_vhost_by_name(ha, port_name);
335         if (vha)
336                 return VPCERR_BAD_WWN;
337
338         /* Check up max-npiv-supports */
339         if (ha->num_vhosts > ha->max_npiv_vports) {
340                 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
341                     "max_npv_vports %ud.\n", ha->host_no,
342                     ha->num_vhosts, ha->max_npiv_vports));
343                 return VPCERR_UNSUPPORTED;
344         }
345         return 0;
346 }
347
348 scsi_qla_host_t *
349 qla24xx_create_vhost(struct fc_vport *fc_vport)
350 {
351         scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
352         scsi_qla_host_t *vha;
353         struct Scsi_Host *host;
354
355         host = scsi_host_alloc(&qla24xx_driver_template,
356             sizeof(scsi_qla_host_t));
357         if (!host) {
358                 printk(KERN_WARNING
359                     "qla2xxx: scsi_host_alloc() failed for vport\n");
360                 return(NULL);
361         }
362
363         vha = shost_priv(host);
364
365         /* clone the parent hba */
366         memcpy(vha, ha, sizeof (scsi_qla_host_t));
367
368         fc_vport->dd_data = vha;
369
370         vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
371         if (!vha->node_name)
372                 goto create_vhost_failed_1;
373
374         vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
375         if (!vha->port_name)
376                 goto create_vhost_failed_2;
377
378         /* New host info */
379         u64_to_wwn(fc_vport->node_name, vha->node_name);
380         u64_to_wwn(fc_vport->port_name, vha->port_name);
381
382         vha->host = host;
383         vha->host_no = host->host_no;
384         vha->parent = ha;
385         vha->fc_vport = fc_vport;
386         vha->device_flags = 0;
387         vha->instance = num_hosts;
388         vha->vp_idx = qla24xx_allocate_vp_id(vha);
389         if (vha->vp_idx > ha->max_npiv_vports) {
390                 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
391                         vha->host_no));
392                 goto create_vhost_failed_3;
393         }
394         vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
395
396         init_completion(&vha->mbx_cmd_comp);
397         complete(&vha->mbx_cmd_comp);
398         init_completion(&vha->mbx_intr_comp);
399
400         INIT_LIST_HEAD(&vha->list);
401         INIT_LIST_HEAD(&vha->fcports);
402         INIT_LIST_HEAD(&vha->vp_fcports);
403
404         vha->dpc_flags = 0L;
405         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
406         set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
407
408         /*
409          * To fix the issue of processing a parent's RSCN for the vport before
410          * its SCR is complete.
411          */
412         set_bit(VP_SCR_NEEDED, &vha->vp_flags);
413         atomic_set(&vha->loop_state, LOOP_DOWN);
414         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
415
416         qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
417
418         host->can_queue = vha->request_q_length + 128;
419         host->this_id = 255;
420         host->cmd_per_lun = 3;
421         host->max_cmd_len = MAX_CMDSZ;
422         host->max_channel = MAX_BUSES - 1;
423         host->max_lun = MAX_LUNS;
424         host->unique_id = vha->instance;
425         host->max_id = MAX_TARGETS_2200;
426         host->transportt = qla2xxx_transport_vport_template;
427
428         DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
429             vha->host_no, vha));
430
431         vha->flags.init_done = 1;
432         num_hosts++;
433
434         down(&ha->vport_sem);
435         set_bit(vha->vp_idx, ha->vp_idx_map);
436         ha->cur_vport_count++;
437         up(&ha->vport_sem);
438
439         return vha;
440
441 create_vhost_failed_3:
442         kfree(vha->port_name);
443
444 create_vhost_failed_2:
445         kfree(vha->node_name);
446
447 create_vhost_failed_1:
448         return NULL;
449 }