Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / drivers / scsi / qla2xxx / qla_mid.c
1 /*
2  *                  QLOGIC LINUX SOFTWARE
3  *
4  * QLogic ISP2x00 device driver for Linux 2.6.x
5  * Copyright (C) 2003-2005 QLogic Corporation
6  * (www.qlogic.com)
7  *
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.
12  *
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.
17  *
18  */
19 #include "qla_def.h"
20
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>
26
27 #include <scsi/scsi_tcq.h>
28 #include <scsi/scsicam.h>
29 #include <linux/delay.h>
30
31 void qla2x00_vp_stop_timer(scsi_qla_host_t *);
32
33 void
34 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
35 {
36         if (vha->parent && vha->timer_active) {
37                 del_timer_sync(&vha->timer);
38                 vha->timer_active = 0;
39         }
40 }
41
42 uint32_t
43 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
44 {
45         uint32_t vp_id;
46         scsi_qla_host_t *ha = vha->parent;
47
48         /* Find an empty slot and assign an vp_id */
49         down(&ha->vport_sem);
50         vp_id = find_first_zero_bit((unsigned long *)ha->vp_idx_map,
51                                 MAX_MULTI_ID_FABRIC);
52         if (vp_id > MAX_MULTI_ID_FABRIC) {
53                 DEBUG15(printk ("vp_id %d is bigger than MAX_MULTI_ID_FABRID\n",
54                     vp_id));
55                 up(&ha->vport_sem);
56                 return vp_id;
57         }
58
59         set_bit(vp_id, (unsigned long *)ha->vp_idx_map);
60         ha->num_vhosts++;
61         vha->vp_idx = vp_id;
62         list_add_tail(&vha->vp_list, &ha->vp_list);
63         up(&ha->vport_sem);
64         return vp_id;
65 }
66
67 void
68 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
69 {
70         uint16_t vp_id;
71         scsi_qla_host_t *ha = vha->parent;
72
73         down(&ha->vport_sem);
74         vp_id = vha->vp_idx;
75         ha->num_vhosts--;
76         clear_bit(vp_id, (unsigned long *)ha->vp_idx_map);
77         list_del(&vha->vp_list);
78         up(&ha->vport_sem);
79 }
80
81 scsi_qla_host_t *
82 qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
83 {
84         scsi_qla_host_t *vha;
85
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))
89                         return vha;
90         }
91         return NULL;
92 }
93
94 /*
95  * qla2x00_mark_vp_devices_dead
96  *      Updates fcport state when device goes offline.
97  *
98  * Input:
99  *      ha = adapter block pointer.
100  *      fcport = port structure pointer.
101  *
102  * Return:
103  *      None.
104  *
105  * Context:
106  */
107 static void
108 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
109 {
110         fc_port_t *fcport;
111         scsi_qla_host_t *pha = to_qla_parent(vha);
112
113         list_for_each_entry(fcport, &pha->fcports, list) {
114                 if (fcport->vp_idx != vha->vp_idx)
115                         continue;
116
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));
120
121                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
122                 qla2x00_mark_device_lost(vha, fcport, 0, 0);
123         }
124 }
125
126 int
127 qla24xx_disable_vp(scsi_qla_host_t *vha)
128 {
129         int ret;
130
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);
134
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);
141         } else {
142                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
143                 return -1;
144         }
145         return 0;
146 }
147
148 int
149 qla24xx_enable_vp(scsi_qla_host_t *vha)
150 {
151         int ret;
152         scsi_qla_host_t *ha = vha->parent;
153
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);
159                 goto enable_failed;
160         }
161
162         /* Initialize the new vport unless it is a persistent port */
163         down(&ha->vport_sem);
164         ret = qla24xx_modify_vp_config(vha);
165         up(&ha->vport_sem);
166
167         if (ret != QLA_SUCCESS) {
168                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
169                 goto enable_failed;
170         }
171
172         DEBUG15(qla_printk(KERN_INFO, ha,
173             "Virtual port with id: %d - Enabled\n", vha->vp_idx));
174         return 0;
175
176 enable_failed:
177         DEBUG15(qla_printk(KERN_INFO, ha,
178             "Virtual port with id: %d - Disabled\n", vha->vp_idx));
179         return 1;
180 }
181
182 static void
183 qla24xx_configure_vp(scsi_qla_host_t *vha)
184 {
185         struct fc_vport *fc_vport;
186         int ret;
187
188         fc_vport = vha->fc_vport;
189
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));
196                 return;
197         } else {
198                 /* Corresponds to SCR enabled */
199                 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
200         }
201
202         vha->flags.online = 1;
203         if (qla24xx_configure_vhba(vha))
204                 return;
205
206         atomic_set(&vha->vp_state, VP_ACTIVE);
207         fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
208 }
209
210 void
211 qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
212 {
213         int i, vp_idx_matched;
214         scsi_qla_host_t *vha;
215
216         if (ha->parent)
217                 return;
218
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)) {
224                 vp_idx_matched = 0;
225
226                 list_for_each_entry(vha, &ha->vp_list, vp_list) {
227                         if (i == vha->vp_idx) {
228                                 vp_idx_matched = 1;
229                                 break;
230                         }
231                 }
232
233                 if (vp_idx_matched) {
234                         switch (mb[0]) {
235                         case MBA_LIP_OCCURRED:
236                         case MBA_LOOP_UP:
237                         case MBA_LOOP_DOWN:
238                         case MBA_LIP_RESET:
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);
247                                 break;
248                         }
249                 }
250         }
251 }
252
253 void
254 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
255 {
256         /*
257          * Physical port will do most of the abort and recovery work. We can
258          * just treat it as a loop down
259          */
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);
263         } else {
264                 if (!atomic_read(&vha->loop_down_timer))
265                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
266         }
267
268         DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
269             vha->host_no, vha->vp_idx));
270         qla24xx_enable_vp(vha);
271 }
272
273 int
274 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
275 {
276         if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
277                 /* VP acquired. complete port configuration */
278                 qla24xx_configure_vp(vha);
279                 return 0;
280         }
281
282         if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
283                 qla2x00_vp_abort_isp(vha);
284
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);
288         }
289
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);
294                 }
295         }
296
297         return 0;
298 }
299
300 void
301 qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
302 {
303         int ret;
304         int i, vp_idx_matched;
305         scsi_qla_host_t *vha;
306
307         if (ha->parent)
308                 return;
309         if (list_empty(&ha->vp_list))
310                 return;
311
312         clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
313
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)) {
319                 vp_idx_matched = 0;
320
321                 list_for_each_entry(vha, &ha->vp_list, vp_list) {
322                         if (i == vha->vp_idx) {
323                                 vp_idx_matched = 1;
324                                 break;
325                         }
326                 }
327
328                 if (vp_idx_matched)
329                         ret = qla2x00_do_dpc_vp(vha);
330         }
331 }
332
333 int
334 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
335 {
336         scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
337         scsi_qla_host_t *vha;
338         uint8_t port_name[WWN_SIZE];
339
340         if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
341                 return VPCERR_UNSUPPORTED;
342
343         /* Check up the F/W and H/W support NPIV */
344         if (!ha->flags.npiv_supported)
345                 return VPCERR_UNSUPPORTED;
346
347         /* Check up whether npiv supported switch presented */
348         if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
349                 return VPCERR_NO_FABRIC_SUPP;
350
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);
354         if (vha)
355                 return VPCERR_BAD_WWN;
356
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;
363         }
364         return 0;
365 }
366
367 scsi_qla_host_t *
368 qla24xx_create_vhost(struct fc_vport *fc_vport)
369 {
370         scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
371         scsi_qla_host_t *vha;
372         struct Scsi_Host *host;
373
374         host = scsi_host_alloc(&qla24xx_driver_template,
375             sizeof(scsi_qla_host_t));
376         if (!host) {
377                 printk(KERN_WARNING
378                     "qla2xxx: scsi_host_alloc() failed for vport\n");
379                 return(NULL);
380         }
381
382         vha = shost_priv(host);
383
384         /* clone the parent hba */
385         memcpy(vha, ha, sizeof (scsi_qla_host_t));
386
387         fc_vport->dd_data = vha;
388
389         vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
390         if (!vha->node_name)
391                 goto create_vhost_failed_1;
392
393         vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
394         if (!vha->port_name)
395                 goto create_vhost_failed_2;
396
397         /* New host info */
398         u64_to_wwn(fc_vport->node_name, vha->node_name);
399         u64_to_wwn(fc_vport->port_name, vha->port_name);
400
401         vha->host = host;
402         vha->host_no = host->host_no;
403         vha->parent = ha;
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",
410                         vha->host_no));
411                 goto create_vhost_failed_3;
412         }
413         vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
414
415         init_MUTEX(&vha->mbx_cmd_sem);
416         init_MUTEX_LOCKED(&vha->mbx_intr_sem);
417
418         INIT_LIST_HEAD(&vha->list);
419         INIT_LIST_HEAD(&vha->fcports);
420         INIT_LIST_HEAD(&vha->vp_fcports);
421
422         vha->dpc_flags = 0L;
423         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
424         set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
425
426         /*
427          * To fix the issue of processing a parent's RSCN for the vport before
428          * its SCR is complete.
429          */
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);
433
434         qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
435
436         host->can_queue = vha->request_q_length + 128;
437         host->this_id = 255;
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;
445
446         DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
447             vha->host_no, vha));
448
449         vha->flags.init_done = 1;
450         num_hosts++;
451
452         down(&ha->vport_sem);
453         set_bit(vha->vp_idx, (unsigned long *)ha->vp_idx_map);
454         ha->cur_vport_count++;
455         up(&ha->vport_sem);
456
457         return vha;
458
459 create_vhost_failed_3:
460         kfree(vha->port_name);
461
462 create_vhost_failed_2:
463         kfree(vha->node_name);
464
465 create_vhost_failed_1:
466         return NULL;
467 }