blob: 50baf6a1d67cbc732eac8ec5578d36ea20b59fba [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++;
Seokmann Ju711c1d92008-07-10 16:55:51 -070046 ha->cur_vport_count++;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070047 vha->vp_idx = vp_id;
48 list_add_tail(&vha->vp_list, &ha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070049 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070050 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
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070059 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070060 vp_id = vha->vp_idx;
61 ha->num_vhosts--;
Seokmann Ju711c1d92008-07-10 16:55:51 -070062 ha->cur_vport_count--;
Andrew Vasquezeb66dc62007-11-12 10:30:58 -080063 clear_bit(vp_id, ha->vp_idx_map);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070064 list_del(&vha->vp_list);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -070065 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070066}
67
Adrian Bunka824ebb2008-01-17 09:02:15 -080068static scsi_qla_host_t *
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070069qla24xx_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 */
Andrew Vasquez26ff7762007-09-20 14:07:47 -070094static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -070095qla2x00_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
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700108 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700109 atomic_set(&fcport->state, FCS_UNCONFIGURED);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700110 }
111}
112
113int
114qla24xx_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
135int
136qla24xx_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 */
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700150 mutex_lock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700151 ret = qla24xx_modify_vp_config(vha);
matthias@kaehlcke.net6c2f5272008-05-12 22:21:11 -0700152 mutex_unlock(&ha->vport_lock);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700153
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
163enable_failed:
164 DEBUG15(qla_printk(KERN_INFO, ha,
165 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
166 return 1;
167}
168
Andrew Vasquez26ff7762007-09-20 14:07:47 -0700169static void
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700170qla24xx_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
197void
198qla2x00_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
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800206 for_each_mapped_vp_idx(ha, i) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700207 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
236void
237qla2x00_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
Adrian Bunka824ebb2008-01-17 09:02:15 -0800256static int
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700257qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
258{
Seokmann Ju221726d2008-04-03 13:13:31 -0700259 scsi_qla_host_t *ha = vha->parent;
260
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700261 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
262 /* VP acquired. complete port configuration */
Seokmann Ju221726d2008-04-03 13:13:31 -0700263 if (atomic_read(&ha->loop_state) == LOOP_READY) {
264 qla24xx_configure_vp(vha);
265 } else {
266 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
267 set_bit(VP_DPC_NEEDED, &ha->dpc_flags);
268 }
269
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700270 return 0;
271 }
272
273 if (test_and_clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
274 qla2x00_vp_abort_isp(vha);
275
276 if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
277 (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
278 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
279 }
280
Seokmann Ju5de1f702008-07-10 16:55:58 -0700281 if (atomic_read(&vha->vp_state) == VP_ACTIVE &&
282 test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700283 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
284 qla2x00_loop_resync(vha);
285 clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
286 }
287 }
288
289 return 0;
290}
291
292void
293qla2x00_do_dpc_all_vps(scsi_qla_host_t *ha)
294{
295 int ret;
296 int i, vp_idx_matched;
297 scsi_qla_host_t *vha;
298
299 if (ha->parent)
300 return;
301 if (list_empty(&ha->vp_list))
302 return;
303
304 clear_bit(VP_DPC_NEEDED, &ha->dpc_flags);
305
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800306 for_each_mapped_vp_idx(ha, i) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700307 vp_idx_matched = 0;
308
309 list_for_each_entry(vha, &ha->vp_list, vp_list) {
310 if (i == vha->vp_idx) {
311 vp_idx_matched = 1;
312 break;
313 }
314 }
315
316 if (vp_idx_matched)
317 ret = qla2x00_do_dpc_vp(vha);
318 }
319}
320
321int
322qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
323{
Andrew Vasquezf363b942007-09-20 14:07:45 -0700324 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700325 scsi_qla_host_t *vha;
326 uint8_t port_name[WWN_SIZE];
327
328 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
329 return VPCERR_UNSUPPORTED;
330
331 /* Check up the F/W and H/W support NPIV */
332 if (!ha->flags.npiv_supported)
333 return VPCERR_UNSUPPORTED;
334
335 /* Check up whether npiv supported switch presented */
336 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
337 return VPCERR_NO_FABRIC_SUPP;
338
339 /* Check up unique WWPN */
340 u64_to_wwn(fc_vport->port_name, port_name);
Seokmann Ju50db6b12008-01-17 09:02:14 -0800341 if (!memcmp(port_name, ha->port_name, WWN_SIZE))
342 return VPCERR_BAD_WWN;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700343 vha = qla24xx_find_vhost_by_name(ha, port_name);
344 if (vha)
345 return VPCERR_BAD_WWN;
346
347 /* Check up max-npiv-supports */
348 if (ha->num_vhosts > ha->max_npiv_vports) {
Andrew Vasquezeb66dc62007-11-12 10:30:58 -0800349 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
350 "max_npv_vports %ud.\n", ha->host_no,
351 ha->num_vhosts, ha->max_npiv_vports));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700352 return VPCERR_UNSUPPORTED;
353 }
354 return 0;
355}
356
357scsi_qla_host_t *
358qla24xx_create_vhost(struct fc_vport *fc_vport)
359{
Andrew Vasquezf363b942007-09-20 14:07:45 -0700360 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700361 scsi_qla_host_t *vha;
362 struct Scsi_Host *host;
363
364 host = scsi_host_alloc(&qla24xx_driver_template,
365 sizeof(scsi_qla_host_t));
366 if (!host) {
367 printk(KERN_WARNING
368 "qla2xxx: scsi_host_alloc() failed for vport\n");
369 return(NULL);
370 }
371
Andrew Vasquezf363b942007-09-20 14:07:45 -0700372 vha = shost_priv(host);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700373
374 /* clone the parent hba */
375 memcpy(vha, ha, sizeof (scsi_qla_host_t));
376
377 fc_vport->dd_data = vha;
378
379 vha->node_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
380 if (!vha->node_name)
381 goto create_vhost_failed_1;
382
383 vha->port_name = kmalloc(WWN_SIZE * sizeof(char), GFP_KERNEL);
384 if (!vha->port_name)
385 goto create_vhost_failed_2;
386
387 /* New host info */
388 u64_to_wwn(fc_vport->node_name, vha->node_name);
389 u64_to_wwn(fc_vport->port_name, vha->port_name);
390
391 vha->host = host;
392 vha->host_no = host->host_no;
393 vha->parent = ha;
394 vha->fc_vport = fc_vport;
395 vha->device_flags = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700396 vha->vp_idx = qla24xx_allocate_vp_id(vha);
397 if (vha->vp_idx > ha->max_npiv_vports) {
398 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
399 vha->host_no));
400 goto create_vhost_failed_3;
401 }
402 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
403
Marcus Barrow0b05a1f2008-01-17 09:02:13 -0800404 init_completion(&vha->mbx_cmd_comp);
405 complete(&vha->mbx_cmd_comp);
406 init_completion(&vha->mbx_intr_comp);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700407
408 INIT_LIST_HEAD(&vha->list);
409 INIT_LIST_HEAD(&vha->fcports);
410 INIT_LIST_HEAD(&vha->vp_fcports);
Seokmann Ju08b95a12008-05-19 14:25:40 -0700411 INIT_LIST_HEAD(&vha->work_list);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700412
413 vha->dpc_flags = 0L;
414 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
415 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
416
417 /*
418 * To fix the issue of processing a parent's RSCN for the vport before
419 * its SCR is complete.
420 */
421 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
422 atomic_set(&vha->loop_state, LOOP_DOWN);
423 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
424
425 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
426
427 host->can_queue = vha->request_q_length + 128;
428 host->this_id = 255;
429 host->cmd_per_lun = 3;
430 host->max_cmd_len = MAX_CMDSZ;
431 host->max_channel = MAX_BUSES - 1;
432 host->max_lun = MAX_LUNS;
Seokmann Ju711c1d92008-07-10 16:55:51 -0700433 host->unique_id = host->host_no;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700434 host->max_id = MAX_TARGETS_2200;
435 host->transportt = qla2xxx_transport_vport_template;
436
437 DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
438 vha->host_no, vha));
439
440 vha->flags.init_done = 1;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700441
442 return vha;
443
444create_vhost_failed_3:
445 kfree(vha->port_name);
446
447create_vhost_failed_2:
448 kfree(vha->node_name);
449
450create_vhost_failed_1:
451 return NULL;
452}