]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/qla2xxx/qla_mid.c
[SCSI] qla2xxx: Correct vport management of MBA_PORT_UPDATE.
[net-next-2.6.git] / drivers / scsi / qla2xxx / qla_mid.c
CommitLineData
2c3dfe3f 1/*
01e58d8e
AV
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
2c3dfe3f 4 *
01e58d8e 5 * See LICENSE.qla2xxx for copyright and licensing details.
2c3dfe3f
SJ
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
2c3dfe3f
SJ
19void
20qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
21{
22 if (vha->parent && vha->timer_active) {
23 del_timer_sync(&vha->timer);
24 vha->timer_active = 0;
25 }
26}
27
a824ebb3 28static uint32_t
2c3dfe3f
SJ
29qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
30{
31 uint32_t vp_id;
32 scsi_qla_host_t *ha = vha->parent;
33
34 /* Find an empty slot and assign an vp_id */
6c2f527c 35 mutex_lock(&ha->vport_lock);
eb66dc60
AV
36 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
37 if (vp_id > ha->max_npiv_vports) {
38 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
39 vp_id, ha->max_npiv_vports));
6c2f527c 40 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
41 return vp_id;
42 }
43
eb66dc60 44 set_bit(vp_id, ha->vp_idx_map);
2c3dfe3f 45 ha->num_vhosts++;
711c1d91 46 ha->cur_vport_count++;
2c3dfe3f
SJ
47 vha->vp_idx = vp_id;
48 list_add_tail(&vha->vp_list, &ha->vp_list);
6c2f527c 49 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
50 return vp_id;
51}
52
53void
54qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
55{
56 uint16_t vp_id;
57 scsi_qla_host_t *ha = vha->parent;
58
6c2f527c 59 mutex_lock(&ha->vport_lock);
2c3dfe3f
SJ
60 vp_id = vha->vp_idx;
61 ha->num_vhosts--;
711c1d91 62 ha->cur_vport_count--;
eb66dc60 63 clear_bit(vp_id, ha->vp_idx_map);
2c3dfe3f 64 list_del(&vha->vp_list);
6c2f527c 65 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
66}
67
a824ebb3 68static scsi_qla_host_t *
2c3dfe3f
SJ
69qla24xx_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 */
26ff776d 94static void
2c3dfe3f
SJ
95qla2x00_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
2c3dfe3f
SJ
108 qla2x00_mark_device_lost(vha, fcport, 0, 0);
109 }
110}
111
112int
113qla24xx_disable_vp(scsi_qla_host_t *vha)
114{
115 int ret;
116
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);
120
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);
127 } else {
128 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
129 return -1;
130 }
131 return 0;
132}
133
134int
135qla24xx_enable_vp(scsi_qla_host_t *vha)
136{
137 int ret;
138 scsi_qla_host_t *ha = vha->parent;
139
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);
145 goto enable_failed;
146 }
147
148 /* Initialize the new vport unless it is a persistent port */
6c2f527c 149 mutex_lock(&ha->vport_lock);
2c3dfe3f 150 ret = qla24xx_modify_vp_config(vha);
6c2f527c 151 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
152
153 if (ret != QLA_SUCCESS) {
154 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
155 goto enable_failed;
156 }
157
158 DEBUG15(qla_printk(KERN_INFO, ha,
159 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
160 return 0;
161
162enable_failed:
163 DEBUG15(qla_printk(KERN_INFO, ha,
164 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
165 return 1;
166}
167
26ff776d 168static void
2c3dfe3f
SJ
169qla24xx_configure_vp(scsi_qla_host_t *vha)
170{
171 struct fc_vport *fc_vport;
172 int ret;
173
174 fc_vport = vha->fc_vport;
175
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));
182 return;
183 } else {
184 /* Corresponds to SCR enabled */
185 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
186 }
187
188 vha->flags.online = 1;
189 if (qla24xx_configure_vhba(vha))
190 return;
191
192 atomic_set(&vha->vp_state, VP_ACTIVE);
193 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
194}
195
196void
197qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
198{
199 int i, vp_idx_matched;
200 scsi_qla_host_t *vha;
201
202 if (ha->parent)
203 return;
204
eb66dc60 205 for_each_mapped_vp_idx(ha, i) {
2c3dfe3f
SJ
206 vp_idx_matched = 0;
207
208 list_for_each_entry(vha, &ha->vp_list, vp_list) {
209 if (i == vha->vp_idx) {
210 vp_idx_matched = 1;
211 break;
212 }
213 }
214
215 if (vp_idx_matched) {
216 switch (mb[0]) {
217 case MBA_LIP_OCCURRED:
218 case MBA_LOOP_UP:
219 case MBA_LOOP_DOWN:
220 case MBA_LIP_RESET:
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);
229 break;
230 }
231 }
232 }
233}
234
235void
236qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
237{
238 /*
239 * Physical port will do most of the abort and recovery work. We can
240 * just treat it as a loop down
241 */
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);
245 } else {
246 if (!atomic_read(&vha->loop_down_timer))
247 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
248 }
249
250 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
251 vha->host_no, vha->vp_idx));
252 qla24xx_enable_vp(vha);
253}
254
a824ebb3 255static int
2c3dfe3f
SJ
256qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
257{
221726d4
SJ
258 scsi_qla_host_t *ha = vha->parent;
259
2c3dfe3f
SJ
260 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
261 /* VP acquired. complete port configuration */
221726d4
SJ
262 if (atomic_read(&ha->loop_state) == LOOP_READY) {
263 qla24xx_configure_vp(vha);
264 } else {
265 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
266 set_bit(VP_DPC_NEEDED, &ha->dpc_flags);
267 }
268
2c3dfe3f
SJ
269 return 0;
270 }
271
272 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
273 qla2x00_vp_abort_isp(vha);
274
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);
278 }
279
280 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
281 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
282 qla2x00_loop_resync(vha);
283 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
284 }
285 }
286
287 return 0;
288}
289
290void
291qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
292{
293 int ret;
294 int i, vp_idx_matched;
295 scsi_qla_host_t *vha;
296
297 if (ha->parent)
298 return;
299 if (list_empty(&ha->vp_list))
300 return;
301
302 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
303
eb66dc60 304 for_each_mapped_vp_idx(ha, i) {
2c3dfe3f
SJ
305 vp_idx_matched = 0;
306
307 list_for_each_entry(vha, &ha->vp_list, vp_list) {
308 if (i == vha->vp_idx) {
309 vp_idx_matched = 1;
310 break;
311 }
312 }
313
314 if (vp_idx_matched)
315 ret = qla2x00_do_dpc_vp(vha);
316 }
317}
318
319int
320qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
321{
f363b943 322 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
2c3dfe3f
SJ
323 scsi_qla_host_t *vha;
324 uint8_t port_name[WWN_SIZE];
325
326 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
327 return VPCERR_UNSUPPORTED;
328
329 /* Check up the F/W and H/W support NPIV */
330 if (!ha->flags.npiv_supported)
331 return VPCERR_UNSUPPORTED;
332
333 /* Check up whether npiv supported switch presented */
334 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
335 return VPCERR_NO_FABRIC_SUPP;
336
337 /* Check up unique WWPN */
338 u64_to_wwn(fc_vport->port_name, port_name);
50db6b13
SJ
339 if (!memcmp(port_name, ha->port_name, WWN_SIZE))
340 return VPCERR_BAD_WWN;
2c3dfe3f
SJ
341 vha = qla24xx_find_vhost_by_name(ha, port_name);
342 if (vha)
343 return VPCERR_BAD_WWN;
344
345 /* Check up max-npiv-supports */
346 if (ha->num_vhosts > ha->max_npiv_vports) {
eb66dc60
AV
347 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
348 "max_npv_vports %ud.\n", ha->host_no,
349 ha->num_vhosts, ha->max_npiv_vports));
2c3dfe3f
SJ
350 return VPCERR_UNSUPPORTED;
351 }
352 return 0;
353}
354
355scsi_qla_host_t *
356qla24xx_create_vhost(struct fc_vport *fc_vport)
357{
f363b943 358 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
2c3dfe3f
SJ
359 scsi_qla_host_t *vha;
360 struct Scsi_Host *host;
361
362 host = scsi_host_alloc(&qla24xx_driver_template,
363 sizeof(scsi_qla_host_t));
364 if (!host) {
365 printk(KERN_WARNING
366 "qla2xxx: scsi_host_alloc() failed for vport\n");
367 return(NULL);
368 }
369
f363b943 370 vha = shost_priv(host);
2c3dfe3f
SJ
371
372 /* clone the parent hba */
373 memcpy(vha, ha, sizeof (scsi_qla_host_t));
374
375 fc_vport->dd_data = vha;
376
377 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
378 if (!vha->node_name)
379 goto create_vhost_failed_1;
380
381 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
382 if (!vha->port_name)
383 goto create_vhost_failed_2;
384
385 /* New host info */
386 u64_to_wwn(fc_vport->node_name, vha->node_name);
387 u64_to_wwn(fc_vport->port_name, vha->port_name);
388
389 vha->host = host;
390 vha->host_no = host->host_no;
391 vha->parent = ha;
392 vha->fc_vport = fc_vport;
393 vha->device_flags = 0;
2c3dfe3f
SJ
394 vha->vp_idx = qla24xx_allocate_vp_id(vha);
395 if (vha->vp_idx > ha->max_npiv_vports) {
396 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
397 vha->host_no));
398 goto create_vhost_failed_3;
399 }
400 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
401
0b05a1f0
MB
402 init_completion(&vha->mbx_cmd_comp);
403 complete(&vha->mbx_cmd_comp);
404 init_completion(&vha->mbx_intr_comp);
2c3dfe3f
SJ
405
406 INIT_LIST_HEAD(&vha->list);
407 INIT_LIST_HEAD(&vha->fcports);
408 INIT_LIST_HEAD(&vha->vp_fcports);
08b95a12 409 INIT_LIST_HEAD(&vha->work_list);
2c3dfe3f
SJ
410
411 vha->dpc_flags = 0L;
412 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
413 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
414
415 /*
416 * To fix the issue of processing a parent's RSCN for the vport before
417 * its SCR is complete.
418 */
419 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
420 atomic_set(&vha->loop_state, LOOP_DOWN);
421 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
422
423 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
424
425 host->can_queue = vha->request_q_length + 128;
426 host->this_id = 255;
427 host->cmd_per_lun = 3;
428 host->max_cmd_len = MAX_CMDSZ;
429 host->max_channel = MAX_BUSES - 1;
430 host->max_lun = MAX_LUNS;
711c1d91 431 host->unique_id = host->host_no;
2c3dfe3f
SJ
432 host->max_id = MAX_TARGETS_2200;
433 host->transportt = qla2xxx_transport_vport_template;
434
435 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
436 vha->host_no, vha));
437
438 vha->flags.init_done = 1;
2c3dfe3f
SJ
439
440 return vha;
441
442create_vhost_failed_3:
443 kfree(vha->port_name);
444
445create_vhost_failed_2:
446 kfree(vha->node_name);
447
448create_vhost_failed_1:
449 return NULL;
450}