blob: fc55429dc91447bf5ea88b8f779ccb28f7488382 [file] [log] [blame]
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001/*
Andrew Vasquez01e58d82008-04-03 13:13:13 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004 *
Andrew Vasquez01e58d82008-04-03 13:13:13 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006 */
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
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070019void
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
Adrian Bunka824ebb2008-01-17 09:02:15 -080028static uint32_t
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070029qla24xx_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 */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070035 mutex_lock(&ha->vport_lock);
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080036 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));
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070040 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070041 return vp_id;
42 }
43
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080044 set_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070045 ha->num_vhosts++;
46 vha->vp_idx = vp_id;
47 list_add_tail(&vha->vp_list, &ha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070048 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070049 return vp_id;
50}
51
52void
53qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
54{
55 uint16_t vp_id;
56 scsi_qla_host_t *ha = vha->parent;
57
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070058 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070059 vp_id = vha->vp_idx;
60 ha->num_vhosts--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080061 clear_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070062 list_del(&vha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070063 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070064}
65
Adrian Bunka824ebb2008-01-17 09:02:15 -080066static scsi_qla_host_t *
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070067qla24xx_find_vhost_by_name(scsi_qla_host_t *ha, uint8_t *port_name)
68{
69 scsi_qla_host_t *vha;
70
71 /* Locate matching device in database. */
72 list_for_each_entry(vha, &ha->vp_list, vp_list) {
73 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
74 return vha;
75 }
76 return NULL;
77}
78
79/*
80 * qla2x00_mark_vp_devices_dead
81 * Updates fcport state when device goes offline.
82 *
83 * Input:
84 * ha = adapter block pointer.
85 * fcport = port structure pointer.
86 *
87 * Return:
88 * None.
89 *
90 * Context:
91 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -070092static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070093qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
94{
95 fc_port_t *fcport;
96 scsi_qla_host_t *pha = to_qla_parent(vha);
97
98 list_for_each_entry(fcport, &pha->fcports, list) {
99 if (fcport->vp_idx != vha->vp_idx)
100 continue;
101
102 DEBUG15(printk("scsi(%ld): Marking port dead, "
103 "loop_id=0x%04x :%x\n",
104 vha->host_no, fcport->loop_id, fcport->vp_idx));
105
106 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
107 qla2x00_mark_device_lost(vha, fcport, 0, 0);
108 }
109}
110
111int
112qla24xx_disable_vp(scsi_qla_host_t *vha)
113{
114 int ret;
115
116 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
117 atomic_set(&vha->loop_state, LOOP_DOWN);
118 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
119
120 /* Delete all vp's fcports from parent's list */
121 qla2x00_mark_vp_devices_dead(vha);
122 atomic_set(&vha->vp_state, VP_FAILED);
123 vha->flags.management_server_logged_in = 0;
124 if (ret == QLA_SUCCESS) {
125 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
126 } else {
127 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
128 return -1;
129 }
130 return 0;
131}
132
133int
134qla24xx_enable_vp(scsi_qla_host_t *vha)
135{
136 int ret;
137 scsi_qla_host_t *ha = vha->parent;
138
139 /* Check if physical ha port is Up */
140 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
141 atomic_read(&ha->loop_state) == LOOP_DEAD ) {
142 vha->vp_err_state = VP_ERR_PORTDWN;
143 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
144 goto enable_failed;
145 }
146
147 /* Initialize the new vport unless it is a persistent port */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700148 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700149 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700150 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700151
152 if (ret != QLA_SUCCESS) {
153 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
154 goto enable_failed;
155 }
156
157 DEBUG15(qla_printk(KERN_INFO, ha,
158 "Virtual port with id: %d - Enabled\n", vha->vp_idx));
159 return 0;
160
161enable_failed:
162 DEBUG15(qla_printk(KERN_INFO, ha,
163 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
164 return 1;
165}
166
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700167static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700168qla24xx_configure_vp(scsi_qla_host_t *vha)
169{
170 struct fc_vport *fc_vport;
171 int ret;
172
173 fc_vport = vha->fc_vport;
174
175 DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
176 vha->host_no, __func__));
177 ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
178 if (ret != QLA_SUCCESS) {
179 DEBUG15(qla_printk(KERN_ERR, vha, "Failed to enable receiving"
180 " of RSCN requests: 0x%x\n", ret));
181 return;
182 } else {
183 /* Corresponds to SCR enabled */
184 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
185 }
186
187 vha->flags.online = 1;
188 if (qla24xx_configure_vhba(vha))
189 return;
190
191 atomic_set(&vha->vp_state, VP_ACTIVE);
192 fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
193}
194
195void
196qla2x00_alert_all_vps(scsi_qla_host_t *ha, uint16_t *mb)
197{
198 int i, vp_idx_matched;
199 scsi_qla_host_t *vha;
200
201 if (ha->parent)
202 return;
203
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800204 for_each_mapped_vp_idx(ha, i) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700205 vp_idx_matched = 0;
206
207 list_for_each_entry(vha, &ha->vp_list, vp_list) {
208 if (i == vha->vp_idx) {
209 vp_idx_matched = 1;
210 break;
211 }
212 }
213
214 if (vp_idx_matched) {
215 switch (mb[0]) {
216 case MBA_LIP_OCCURRED:
217 case MBA_LOOP_UP:
218 case MBA_LOOP_DOWN:
219 case MBA_LIP_RESET:
220 case MBA_POINT_TO_POINT:
221 case MBA_CHG_IN_CONNECTION:
222 case MBA_PORT_UPDATE:
223 case MBA_RSCN_UPDATE:
224 DEBUG15(printk("scsi(%ld)%s: Async_event for"
225 " VP[%d], mb = 0x%x, vha=%p\n",
226 vha->host_no, __func__,i, *mb, vha));
227 qla2x00_async_event(vha, mb);
228 break;
229 }
230 }
231 }
232}
233
234void
235qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
236{
237 /*
238 * Physical port will do most of the abort and recovery work. We can
239 * just treat it as a loop down
240 */
241 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
242 atomic_set(&vha->loop_state, LOOP_DOWN);
243 qla2x00_mark_all_devices_lost(vha, 0);
244 } else {
245 if (!atomic_read(&vha->loop_down_timer))
246 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
247 }
248
249 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
250 vha->host_no, vha->vp_idx));
251 qla24xx_enable_vp(vha);
252}
253
Adrian Bunka824ebb2008-01-17 09:02:15 -0800254static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700255qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
256{
Seokmann Ju221726d2008-04-03 13:13:31 -0700257 scsi_qla_host_t *ha = vha->parent;
258
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700259 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
260 /* VP acquired. complete port configuration */
Seokmann Ju221726d2008-04-03 13:13:31 -0700261 if (atomic_read(&ha->loop_state) == LOOP_READY) {
262 qla24xx_configure_vp(vha);
263 } else {
264 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
265 set_bit(VP_DPC_NEEDED, &ha->dpc_flags);
266 }
267
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700268 return 0;
269 }
270
271 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
272 qla2x00_vp_abort_isp(vha);
273
274 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
275 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
276 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
277 }
278
279 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
280 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
281 qla2x00_loop_resync(vha);
282 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
283 }
284 }
285
286 return 0;
287}
288
289void
290qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
291{
292 int ret;
293 int i, vp_idx_matched;
294 scsi_qla_host_t *vha;
295
296 if (ha->parent)
297 return;
298 if (list_empty(&ha->vp_list))
299 return;
300
301 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
302
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800303 for_each_mapped_vp_idx(ha, i) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700304 vp_idx_matched = 0;
305
306 list_for_each_entry(vha, &ha->vp_list, vp_list) {
307 if (i == vha->vp_idx) {
308 vp_idx_matched = 1;
309 break;
310 }
311 }
312
313 if (vp_idx_matched)
314 ret = qla2x00_do_dpc_vp(vha);
315 }
316}
317
318int
319qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
320{
Andrew Vasquezf363b942007-09-20 14:07:45 -0700321 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700322 scsi_qla_host_t *vha;
323 uint8_t port_name[WWN_SIZE];
324
325 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
326 return VPCERR_UNSUPPORTED;
327
328 /* Check up the F/W and H/W support NPIV */
329 if (!ha->flags.npiv_supported)
330 return VPCERR_UNSUPPORTED;
331
332 /* Check up whether npiv supported switch presented */
333 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
334 return VPCERR_NO_FABRIC_SUPP;
335
336 /* Check up unique WWPN */
337 u64_to_wwn(fc_vport->port_name, port_name);
Seokmann Ju50db6b12008-01-17 09:02:14 -0800338 if (!memcmp(port_name, ha->port_name, WWN_SIZE))
339 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700340 vha = qla24xx_find_vhost_by_name(ha, port_name);
341 if (vha)
342 return VPCERR_BAD_WWN;
343
344 /* Check up max-npiv-supports */
345 if (ha->num_vhosts > ha->max_npiv_vports) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800346 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
347 "max_npv_vports %ud.\n", ha->host_no,
348 ha->num_vhosts, ha->max_npiv_vports));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700349 return VPCERR_UNSUPPORTED;
350 }
351 return 0;
352}
353
354scsi_qla_host_t *
355qla24xx_create_vhost(struct fc_vport *fc_vport)
356{
Andrew Vasquezf363b942007-09-20 14:07:45 -0700357 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700358 scsi_qla_host_t *vha;
359 struct Scsi_Host *host;
360
361 host = scsi_host_alloc(&qla24xx_driver_template,
362 sizeof(scsi_qla_host_t));
363 if (!host) {
364 printk(KERN_WARNING
365 "qla2xxx: scsi_host_alloc() failed for vport\n");
366 return(NULL);
367 }
368
Andrew Vasquezf363b942007-09-20 14:07:45 -0700369 vha = shost_priv(host);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700370
371 /* clone the parent hba */
372 memcpy(vha, ha, sizeof (scsi_qla_host_t));
373
374 fc_vport->dd_data = vha;
375
376 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
377 if (!vha->node_name)
378 goto create_vhost_failed_1;
379
380 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
381 if (!vha->port_name)
382 goto create_vhost_failed_2;
383
384 /* New host info */
385 u64_to_wwn(fc_vport->node_name, vha->node_name);
386 u64_to_wwn(fc_vport->port_name, vha->port_name);
387
388 vha->host = host;
389 vha->host_no = host->host_no;
390 vha->parent = ha;
391 vha->fc_vport = fc_vport;
392 vha->device_flags = 0;
393 vha->instance = num_hosts;
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
Marcus Barrow0b05a1f2008-01-17 09:02:13 -0800402 init_completion(&vha->mbx_cmd_comp);
403 complete(&vha->mbx_cmd_comp);
404 init_completion(&vha->mbx_intr_comp);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700405
406 INIT_LIST_HEAD(&vha->list);
407 INIT_LIST_HEAD(&vha->fcports);
408 INIT_LIST_HEAD(&vha->vp_fcports);
409
410 vha->dpc_flags = 0L;
411 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
412 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
413
414 /*
415 * To fix the issue of processing a parent's RSCN for the vport before
416 * its SCR is complete.
417 */
418 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
419 atomic_set(&vha->loop_state, LOOP_DOWN);
420 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
421
422 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
423
424 host->can_queue = vha->request_q_length + 128;
425 host->this_id = 255;
426 host->cmd_per_lun = 3;
427 host->max_cmd_len = MAX_CMDSZ;
428 host->max_channel = MAX_BUSES - 1;
429 host->max_lun = MAX_LUNS;
430 host->unique_id = vha->instance;
431 host->max_id = MAX_TARGETS_2200;
432 host->transportt = qla2xxx_transport_vport_template;
433
434 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
435 vha->host_no, vha));
436
437 vha->flags.init_done = 1;
438 num_hosts++;
439
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700440 mutex_lock(&ha->vport_lock);
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800441 set_bit(vha->vp_idx, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700442 ha->cur_vport_count++;
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700443 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700444
445 return vha;
446
447create_vhost_failed_3:
448 kfree(vha->port_name);
449
450create_vhost_failed_2:
451 kfree(vha->node_name);
452
453create_vhost_failed_1:
454 return NULL;
455}