]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/qla2xxx/qla_init.c
[SCSI] qla2xxx: T10 DIF support added.
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / qla2xxx / qla_init.c
CommitLineData
1da177e4 1/*
fa90c54f 2 * QLogic Fibre Channel HBA Driver
01e58d8e 3 * Copyright (c) 2003-2008 QLogic Corporation
1da177e4 4 *
fa90c54f 5 * See LICENSE.qla2xxx for copyright and licensing details.
1da177e4
LT
6 */
7#include "qla_def.h"
73208dfd 8#include "qla_gbl.h"
1da177e4
LT
9
10#include <linux/delay.h>
5a0e3ad6 11#include <linux/slab.h>
0107109e 12#include <linux/vmalloc.h>
1da177e4
LT
13
14#include "qla_devtbl.h"
15
4e08df3f
DM
16#ifdef CONFIG_SPARC
17#include <asm/prom.h>
4e08df3f
DM
18#endif
19
1da177e4
LT
20/*
21* QLogic ISP2x00 Hardware Support Function Prototypes.
22*/
1da177e4 23static int qla2x00_isp_firmware(scsi_qla_host_t *);
1da177e4 24static int qla2x00_setup_chip(scsi_qla_host_t *);
1da177e4
LT
25static int qla2x00_init_rings(scsi_qla_host_t *);
26static int qla2x00_fw_ready(scsi_qla_host_t *);
27static int qla2x00_configure_hba(scsi_qla_host_t *);
1da177e4
LT
28static int qla2x00_configure_loop(scsi_qla_host_t *);
29static int qla2x00_configure_local_loop(scsi_qla_host_t *);
1da177e4
LT
30static int qla2x00_configure_fabric(scsi_qla_host_t *);
31static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
32static int qla2x00_device_resync(scsi_qla_host_t *);
33static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
34 uint16_t *);
1da177e4
LT
35
36static int qla2x00_restart_isp(scsi_qla_host_t *);
1da177e4 37
e315cd28 38static int qla2x00_find_new_loop_id(scsi_qla_host_t *, fc_port_t *);
413975a0 39
4d4df193
HK
40static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
41static int qla84xx_init_chip(scsi_qla_host_t *);
73208dfd 42static int qla25xx_init_queues(struct qla_hw_data *);
4d4df193 43
ac280b67
AV
44/* SRB Extensions ---------------------------------------------------------- */
45
46static void
47qla2x00_ctx_sp_timeout(unsigned long __data)
48{
49 srb_t *sp = (srb_t *)__data;
50 struct srb_ctx *ctx;
4916392b 51 struct srb_iocb *iocb;
ac280b67
AV
52 fc_port_t *fcport = sp->fcport;
53 struct qla_hw_data *ha = fcport->vha->hw;
54 struct req_que *req;
55 unsigned long flags;
56
57 spin_lock_irqsave(&ha->hardware_lock, flags);
58 req = ha->req_q_map[0];
59 req->outstanding_cmds[sp->handle] = NULL;
60 ctx = sp->ctx;
4916392b
MI
61 iocb = ctx->u.iocb_cmd;
62 iocb->timeout(sp);
ac280b67
AV
63 spin_unlock_irqrestore(&ha->hardware_lock, flags);
64
4916392b 65 iocb->free(sp);
ac280b67
AV
66}
67
9a069e19 68void
ac280b67
AV
69qla2x00_ctx_sp_free(srb_t *sp)
70{
71 struct srb_ctx *ctx = sp->ctx;
4916392b 72 struct srb_iocb *iocb = ctx->u.iocb_cmd;
ac280b67 73
4916392b
MI
74 del_timer_sync(&iocb->timer);
75 kfree(iocb);
ac280b67
AV
76 kfree(ctx);
77 mempool_free(sp, sp->fcport->vha->hw->srb_mempool);
78}
79
80inline srb_t *
81qla2x00_get_ctx_sp(scsi_qla_host_t *vha, fc_port_t *fcport, size_t size,
82 unsigned long tmo)
83{
84 srb_t *sp;
85 struct qla_hw_data *ha = vha->hw;
86 struct srb_ctx *ctx;
4916392b 87 struct srb_iocb *iocb;
ac280b67
AV
88
89 sp = mempool_alloc(ha->srb_mempool, GFP_KERNEL);
90 if (!sp)
91 goto done;
92 ctx = kzalloc(size, GFP_KERNEL);
93 if (!ctx) {
94 mempool_free(sp, ha->srb_mempool);
4916392b
MI
95 sp = NULL;
96 goto done;
97 }
98 iocb = kzalloc(sizeof(struct srb_iocb), GFP_KERNEL);
99 if (!iocb) {
100 mempool_free(sp, ha->srb_mempool);
101 sp = NULL;
102 kfree(ctx);
ac280b67
AV
103 goto done;
104 }
105
106 memset(sp, 0, sizeof(*sp));
107 sp->fcport = fcport;
108 sp->ctx = ctx;
4916392b
MI
109 ctx->u.iocb_cmd = iocb;
110 iocb->free = qla2x00_ctx_sp_free;
ac280b67 111
4916392b 112 init_timer(&iocb->timer);
ac280b67
AV
113 if (!tmo)
114 goto done;
4916392b
MI
115 iocb->timer.expires = jiffies + tmo * HZ;
116 iocb->timer.data = (unsigned long)sp;
117 iocb->timer.function = qla2x00_ctx_sp_timeout;
118 add_timer(&iocb->timer);
ac280b67
AV
119done:
120 return sp;
121}
122
123/* Asynchronous Login/Logout Routines -------------------------------------- */
124
125#define ELS_TMO_2_RATOV(ha) ((ha)->r_a_tov / 10 * 2)
126
127static void
3822263e 128qla2x00_async_iocb_timeout(srb_t *sp)
ac280b67
AV
129{
130 fc_port_t *fcport = sp->fcport;
4916392b 131 struct srb_ctx *ctx = sp->ctx;
ac280b67
AV
132
133 DEBUG2(printk(KERN_WARNING
134 "scsi(%ld:%x): Async-%s timeout.\n",
4916392b 135 fcport->vha->host_no, sp->handle, ctx->name));
ac280b67 136
5ff1d584 137 fcport->flags &= ~FCF_ASYNC_SENT;
4916392b 138 if (ctx->type == SRB_LOGIN_CMD)
ac280b67
AV
139 qla2x00_post_async_logout_work(fcport->vha, fcport, NULL);
140}
141
99b0bec7
AV
142static void
143qla2x00_async_login_ctx_done(srb_t *sp)
144{
4916392b
MI
145 struct srb_ctx *ctx = sp->ctx;
146 struct srb_iocb *lio = ctx->u.iocb_cmd;
99b0bec7
AV
147
148 qla2x00_post_async_login_done_work(sp->fcport->vha, sp->fcport,
4916392b
MI
149 lio->u.logio.data);
150 lio->free(sp);
99b0bec7
AV
151}
152
ac280b67
AV
153int
154qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
155 uint16_t *data)
156{
157 struct qla_hw_data *ha = vha->hw;
158 srb_t *sp;
4916392b
MI
159 struct srb_ctx *ctx;
160 struct srb_iocb *lio;
ac280b67
AV
161 int rval;
162
163 rval = QLA_FUNCTION_FAILED;
4916392b 164 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
ac280b67
AV
165 ELS_TMO_2_RATOV(ha) + 2);
166 if (!sp)
167 goto done;
168
4916392b
MI
169 ctx = sp->ctx;
170 ctx->type = SRB_LOGIN_CMD;
171 ctx->name = "login";
172 lio = ctx->u.iocb_cmd;
3822263e 173 lio->timeout = qla2x00_async_iocb_timeout;
4916392b
MI
174 lio->done = qla2x00_async_login_ctx_done;
175 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
ac280b67 176 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
4916392b 177 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
ac280b67
AV
178 rval = qla2x00_start_sp(sp);
179 if (rval != QLA_SUCCESS)
180 goto done_free_sp;
181
182 DEBUG2(printk(KERN_DEBUG
183 "scsi(%ld:%x): Async-login - loop-id=%x portid=%02x%02x%02x "
184 "retries=%d.\n", fcport->vha->host_no, sp->handle, fcport->loop_id,
185 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
186 fcport->login_retry));
187 return rval;
188
189done_free_sp:
4916392b 190 lio->free(sp);
ac280b67
AV
191done:
192 return rval;
193}
194
99b0bec7
AV
195static void
196qla2x00_async_logout_ctx_done(srb_t *sp)
197{
4916392b
MI
198 struct srb_ctx *ctx = sp->ctx;
199 struct srb_iocb *lio = ctx->u.iocb_cmd;
99b0bec7
AV
200
201 qla2x00_post_async_logout_done_work(sp->fcport->vha, sp->fcport,
4916392b
MI
202 lio->u.logio.data);
203 lio->free(sp);
99b0bec7
AV
204}
205
ac280b67
AV
206int
207qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
208{
209 struct qla_hw_data *ha = vha->hw;
210 srb_t *sp;
4916392b
MI
211 struct srb_ctx *ctx;
212 struct srb_iocb *lio;
ac280b67
AV
213 int rval;
214
215 rval = QLA_FUNCTION_FAILED;
4916392b 216 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
ac280b67
AV
217 ELS_TMO_2_RATOV(ha) + 2);
218 if (!sp)
219 goto done;
220
4916392b
MI
221 ctx = sp->ctx;
222 ctx->type = SRB_LOGOUT_CMD;
223 ctx->name = "logout";
224 lio = ctx->u.iocb_cmd;
3822263e 225 lio->timeout = qla2x00_async_iocb_timeout;
4916392b 226 lio->done = qla2x00_async_logout_ctx_done;
ac280b67
AV
227 rval = qla2x00_start_sp(sp);
228 if (rval != QLA_SUCCESS)
229 goto done_free_sp;
230
231 DEBUG2(printk(KERN_DEBUG
232 "scsi(%ld:%x): Async-logout - loop-id=%x portid=%02x%02x%02x.\n",
233 fcport->vha->host_no, sp->handle, fcport->loop_id,
234 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa));
235 return rval;
236
237done_free_sp:
4916392b 238 lio->free(sp);
ac280b67
AV
239done:
240 return rval;
241}
242
5ff1d584
AV
243static void
244qla2x00_async_adisc_ctx_done(srb_t *sp)
245{
4916392b
MI
246 struct srb_ctx *ctx = sp->ctx;
247 struct srb_iocb *lio = ctx->u.iocb_cmd;
5ff1d584
AV
248
249 qla2x00_post_async_adisc_done_work(sp->fcport->vha, sp->fcport,
4916392b
MI
250 lio->u.logio.data);
251 lio->free(sp);
5ff1d584
AV
252}
253
254int
255qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
256 uint16_t *data)
257{
258 struct qla_hw_data *ha = vha->hw;
259 srb_t *sp;
4916392b
MI
260 struct srb_ctx *ctx;
261 struct srb_iocb *lio;
5ff1d584
AV
262 int rval;
263
264 rval = QLA_FUNCTION_FAILED;
4916392b 265 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
5ff1d584
AV
266 ELS_TMO_2_RATOV(ha) + 2);
267 if (!sp)
268 goto done;
269
4916392b
MI
270 ctx = sp->ctx;
271 ctx->type = SRB_ADISC_CMD;
272 ctx->name = "adisc";
273 lio = ctx->u.iocb_cmd;
3822263e 274 lio->timeout = qla2x00_async_iocb_timeout;
4916392b 275 lio->done = qla2x00_async_adisc_ctx_done;
5ff1d584 276 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
4916392b 277 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
5ff1d584
AV
278 rval = qla2x00_start_sp(sp);
279 if (rval != QLA_SUCCESS)
280 goto done_free_sp;
281
282 DEBUG2(printk(KERN_DEBUG
283 "scsi(%ld:%x): Async-adisc - loop-id=%x portid=%02x%02x%02x.\n",
284 fcport->vha->host_no, sp->handle, fcport->loop_id,
285 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa));
286
287 return rval;
288
289done_free_sp:
4916392b 290 lio->free(sp);
5ff1d584
AV
291done:
292 return rval;
293}
294
3822263e
MI
295static void
296qla2x00_async_tm_cmd_ctx_done(srb_t *sp)
297{
298 struct srb_ctx *ctx = sp->ctx;
299 struct srb_iocb *iocb = (struct srb_iocb *)ctx->u.iocb_cmd;
300
301 qla2x00_async_tm_cmd_done(sp->fcport->vha, sp->fcport, iocb);
302 iocb->free(sp);
303}
304
305int
306qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
307 uint32_t tag)
308{
309 struct scsi_qla_host *vha = fcport->vha;
310 struct qla_hw_data *ha = vha->hw;
311 srb_t *sp;
312 struct srb_ctx *ctx;
313 struct srb_iocb *tcf;
314 int rval;
315
316 rval = QLA_FUNCTION_FAILED;
317 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
318 ELS_TMO_2_RATOV(ha) + 2);
319 if (!sp)
320 goto done;
321
322 ctx = sp->ctx;
323 ctx->type = SRB_TM_CMD;
324 ctx->name = "tmf";
325 tcf = ctx->u.iocb_cmd;
326 tcf->u.tmf.flags = flags;
327 tcf->u.tmf.lun = lun;
328 tcf->u.tmf.data = tag;
329 tcf->timeout = qla2x00_async_iocb_timeout;
330 tcf->done = qla2x00_async_tm_cmd_ctx_done;
331
332 rval = qla2x00_start_sp(sp);
333 if (rval != QLA_SUCCESS)
334 goto done_free_sp;
335
336 DEBUG2(printk(KERN_DEBUG
337 "scsi(%ld:%x): Async-tmf - loop-id=%x portid=%02x%02x%02x.\n",
338 fcport->vha->host_no, sp->handle, fcport->loop_id,
339 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa));
340
341 return rval;
342
343done_free_sp:
344 tcf->free(sp);
345done:
346 return rval;
347}
348
349static void
350qla2x00_async_marker_ctx_done(srb_t *sp)
351{
352 struct srb_ctx *ctx = sp->ctx;
353 struct srb_iocb *iocb = (struct srb_iocb *)ctx->u.iocb_cmd;
354
355 qla2x00_async_marker_done(sp->fcport->vha, sp->fcport, iocb);
356 iocb->free(sp);
357}
358
359int
360qla2x00_async_marker(fc_port_t *fcport, uint16_t lun, uint8_t modif)
361{
362 struct scsi_qla_host *vha = fcport->vha;
363 srb_t *sp;
364 struct srb_ctx *ctx;
365 struct srb_iocb *mrk;
366 int rval;
367
368 rval = QLA_FUNCTION_FAILED;
369 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx), 0);
370 if (!sp)
371 goto done;
372
373 ctx = sp->ctx;
374 ctx->type = SRB_MARKER_CMD;
375 ctx->name = "marker";
376 mrk = ctx->u.iocb_cmd;
377 mrk->u.marker.lun = lun;
378 mrk->u.marker.modif = modif;
379 mrk->timeout = qla2x00_async_iocb_timeout;
380 mrk->done = qla2x00_async_marker_ctx_done;
381
382 rval = qla2x00_start_sp(sp);
383 if (rval != QLA_SUCCESS)
384 goto done_free_sp;
385
386 DEBUG2(printk(KERN_DEBUG
387 "scsi(%ld:%x): Async-marker - loop-id=%x "
388 "portid=%02x%02x%02x.\n",
389 fcport->vha->host_no, sp->handle, fcport->loop_id,
390 fcport->d_id.b.domain, fcport->d_id.b.area,
391 fcport->d_id.b.al_pa));
392
393 return rval;
394
395done_free_sp:
396 mrk->free(sp);
397done:
398 return rval;
399}
400
4916392b 401void
ac280b67
AV
402qla2x00_async_login_done(struct scsi_qla_host *vha, fc_port_t *fcport,
403 uint16_t *data)
404{
405 int rval;
ac280b67
AV
406
407 switch (data[0]) {
408 case MBS_COMMAND_COMPLETE:
99b0bec7 409 if (fcport->flags & FCF_FCP2_DEVICE) {
5ff1d584
AV
410 fcport->flags |= FCF_ASYNC_SENT;
411 qla2x00_post_async_adisc_work(vha, fcport, data);
412 break;
99b0bec7
AV
413 }
414 qla2x00_update_fcport(vha, fcport);
ac280b67
AV
415 break;
416 case MBS_COMMAND_ERROR:
5ff1d584 417 fcport->flags &= ~FCF_ASYNC_SENT;
ac280b67
AV
418 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
419 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
420 else
421 qla2x00_mark_device_lost(vha, fcport, 1, 0);
422 break;
423 case MBS_PORT_ID_USED:
424 fcport->loop_id = data[1];
425 qla2x00_post_async_login_work(vha, fcport, NULL);
426 break;
427 case MBS_LOOP_ID_USED:
428 fcport->loop_id++;
429 rval = qla2x00_find_new_loop_id(vha, fcport);
430 if (rval != QLA_SUCCESS) {
5ff1d584 431 fcport->flags &= ~FCF_ASYNC_SENT;
ac280b67
AV
432 qla2x00_mark_device_lost(vha, fcport, 1, 0);
433 break;
434 }
435 qla2x00_post_async_login_work(vha, fcport, NULL);
436 break;
437 }
4916392b 438 return;
ac280b67
AV
439}
440
4916392b 441void
ac280b67
AV
442qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
443 uint16_t *data)
444{
445 qla2x00_mark_device_lost(vha, fcport, 1, 0);
4916392b 446 return;
ac280b67
AV
447}
448
4916392b 449void
5ff1d584
AV
450qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
451 uint16_t *data)
452{
453 if (data[0] == MBS_COMMAND_COMPLETE) {
454 qla2x00_update_fcport(vha, fcport);
455
4916392b 456 return;
5ff1d584
AV
457 }
458
459 /* Retry login. */
460 fcport->flags &= ~FCF_ASYNC_SENT;
461 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
462 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
463 else
464 qla2x00_mark_device_lost(vha, fcport, 1, 0);
465
4916392b 466 return;
5ff1d584
AV
467}
468
3822263e
MI
469void
470qla2x00_async_tm_cmd_done(struct scsi_qla_host *vha, fc_port_t *fcport,
471 struct srb_iocb *iocb)
472{
473 int rval;
474 uint32_t flags;
475 uint16_t lun;
476
477 flags = iocb->u.tmf.flags;
478 lun = (uint16_t)iocb->u.tmf.lun;
479
480 /* Issue Marker IOCB */
481 rval = qla2x00_async_marker(fcport, lun,
482 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
483
484 if ((rval != QLA_SUCCESS) || iocb->u.tmf.data) {
485 DEBUG2_3_11(printk(KERN_WARNING
486 "%s(%ld): TM IOCB failed (%x).\n",
487 __func__, vha->host_no, rval));
488 }
489
490 return;
491}
492
493void
494qla2x00_async_marker_done(struct scsi_qla_host *vha, fc_port_t *fcport,
495 struct srb_iocb *iocb)
496{
497 /*
498 * Currently we dont have any specific post response processing
499 * for this IOCB. We'll just return success or failed
500 * depending on whether the IOCB command succeeded or failed.
501 */
502 if (iocb->u.tmf.data) {
503 DEBUG2_3_11(printk(KERN_WARNING
504 "%s(%ld): Marker IOCB failed (%x).\n",
505 __func__, vha->host_no, iocb->u.tmf.data));
506 }
507
508 return;
509}
510
1da177e4
LT
511/****************************************************************************/
512/* QLogic ISP2x00 Hardware Support Functions. */
513/****************************************************************************/
514
515/*
516* qla2x00_initialize_adapter
517* Initialize board.
518*
519* Input:
520* ha = adapter block pointer.
521*
522* Returns:
523* 0 = success
524*/
525int
e315cd28 526qla2x00_initialize_adapter(scsi_qla_host_t *vha)
1da177e4
LT
527{
528 int rval;
e315cd28 529 struct qla_hw_data *ha = vha->hw;
73208dfd 530 struct req_que *req = ha->req_q_map[0];
2533cf67 531
1da177e4 532 /* Clear adapter flags. */
e315cd28 533 vha->flags.online = 0;
2533cf67 534 ha->flags.chip_reset_done = 0;
e315cd28 535 vha->flags.reset_active = 0;
85880801
AV
536 ha->flags.pci_channel_io_perm_failure = 0;
537 ha->flags.eeh_busy = 0;
e315cd28
AC
538 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
539 atomic_set(&vha->loop_state, LOOP_DOWN);
540 vha->device_flags = DFLG_NO_CABLE;
541 vha->dpc_flags = 0;
542 vha->flags.management_server_logged_in = 0;
543 vha->marker_needed = 0;
1da177e4
LT
544 ha->isp_abort_cnt = 0;
545 ha->beacon_blink_led = 0;
546
73208dfd
AC
547 set_bit(0, ha->req_qid_map);
548 set_bit(0, ha->rsp_qid_map);
549
0107109e 550 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
e315cd28 551 rval = ha->isp_ops->pci_config(vha);
1da177e4 552 if (rval) {
7c98a046 553 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
e315cd28 554 vha->host_no));
1da177e4
LT
555 return (rval);
556 }
557
e315cd28 558 ha->isp_ops->reset_chip(vha);
1da177e4 559
e315cd28 560 rval = qla2xxx_get_flash_info(vha);
c00d8994
AV
561 if (rval) {
562 DEBUG2(printk("scsi(%ld): Unable to validate FLASH data.\n",
e315cd28 563 vha->host_no));
c00d8994
AV
564 return (rval);
565 }
566
73208dfd 567 ha->isp_ops->get_flash_version(vha, req->ring);
30c47662 568
1da177e4 569 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
0107109e 570
e315cd28 571 ha->isp_ops->nvram_config(vha);
1da177e4 572
d4c760c2
AV
573 if (ha->flags.disable_serdes) {
574 /* Mask HBA via NVRAM settings? */
575 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
576 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
e315cd28
AC
577 vha->port_name[0], vha->port_name[1],
578 vha->port_name[2], vha->port_name[3],
579 vha->port_name[4], vha->port_name[5],
580 vha->port_name[6], vha->port_name[7]);
d4c760c2
AV
581 return QLA_FUNCTION_FAILED;
582 }
583
1da177e4
LT
584 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
585
e315cd28
AC
586 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
587 rval = ha->isp_ops->chip_diag(vha);
d19044c3
AV
588 if (rval)
589 return (rval);
e315cd28 590 rval = qla2x00_setup_chip(vha);
d19044c3
AV
591 if (rval)
592 return (rval);
1da177e4 593 }
a9083016 594
4d4df193 595 if (IS_QLA84XX(ha)) {
e315cd28 596 ha->cs84xx = qla84xx_get_chip(vha);
4d4df193
HK
597 if (!ha->cs84xx) {
598 qla_printk(KERN_ERR, ha,
599 "Unable to configure ISP84XX.\n");
600 return QLA_FUNCTION_FAILED;
601 }
602 }
e315cd28 603 rval = qla2x00_init_rings(vha);
2533cf67 604 ha->flags.chip_reset_done = 1;
1da177e4 605
9a069e19 606 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
6c452a45 607 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
9a069e19
GM
608 rval = qla84xx_init_chip(vha);
609 if (rval != QLA_SUCCESS) {
610 qla_printk(KERN_ERR, ha,
611 "Unable to initialize ISP84XX.\n");
612 qla84xx_put_chip(vha);
613 }
614 }
615
09ff701a
SR
616 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha)) {
617 if (qla24xx_read_fcp_prio_cfg(vha))
618 qla_printk(KERN_ERR, ha,
619 "Unable to read FCP priority data.\n");
620 }
621
1da177e4
LT
622 return (rval);
623}
624
625/**
abbd8870 626 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
1da177e4
LT
627 * @ha: HA context
628 *
629 * Returns 0 on success.
630 */
abbd8870 631int
e315cd28 632qla2100_pci_config(scsi_qla_host_t *vha)
1da177e4 633{
a157b101 634 uint16_t w;
abbd8870 635 unsigned long flags;
e315cd28 636 struct qla_hw_data *ha = vha->hw;
3d71644c 637 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 638
1da177e4 639 pci_set_master(ha->pdev);
af6177d8 640 pci_try_set_mwi(ha->pdev);
1da177e4 641
1da177e4 642 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
a157b101 643 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
abbd8870
AV
644 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
645
737faece 646 pci_disable_rom(ha->pdev);
1da177e4
LT
647
648 /* Get PCI bus information. */
649 spin_lock_irqsave(&ha->hardware_lock, flags);
3d71644c 650 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
1da177e4
LT
651 spin_unlock_irqrestore(&ha->hardware_lock, flags);
652
abbd8870
AV
653 return QLA_SUCCESS;
654}
1da177e4 655
abbd8870
AV
656/**
657 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
658 * @ha: HA context
659 *
660 * Returns 0 on success.
661 */
662int
e315cd28 663qla2300_pci_config(scsi_qla_host_t *vha)
abbd8870 664{
a157b101 665 uint16_t w;
abbd8870
AV
666 unsigned long flags = 0;
667 uint32_t cnt;
e315cd28 668 struct qla_hw_data *ha = vha->hw;
3d71644c 669 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 670
abbd8870 671 pci_set_master(ha->pdev);
af6177d8 672 pci_try_set_mwi(ha->pdev);
1da177e4 673
abbd8870 674 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
a157b101 675 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1da177e4 676
abbd8870
AV
677 if (IS_QLA2322(ha) || IS_QLA6322(ha))
678 w &= ~PCI_COMMAND_INTX_DISABLE;
a157b101 679 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1da177e4 680
abbd8870
AV
681 /*
682 * If this is a 2300 card and not 2312, reset the
683 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
684 * the 2310 also reports itself as a 2300 so we need to get the
685 * fb revision level -- a 6 indicates it really is a 2300 and
686 * not a 2310.
687 */
688 if (IS_QLA2300(ha)) {
689 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4 690
abbd8870 691 /* Pause RISC. */
3d71644c 692 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
abbd8870 693 for (cnt = 0; cnt < 30000; cnt++) {
3d71644c 694 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
abbd8870 695 break;
1da177e4 696
abbd8870
AV
697 udelay(10);
698 }
1da177e4 699
abbd8870 700 /* Select FPM registers. */
3d71644c
AV
701 WRT_REG_WORD(&reg->ctrl_status, 0x20);
702 RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
703
704 /* Get the fb rev level */
3d71644c 705 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
abbd8870
AV
706
707 if (ha->fb_rev == FPM_2300)
a157b101 708 pci_clear_mwi(ha->pdev);
abbd8870
AV
709
710 /* Deselect FPM registers. */
3d71644c
AV
711 WRT_REG_WORD(&reg->ctrl_status, 0x0);
712 RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
713
714 /* Release RISC module. */
3d71644c 715 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
abbd8870 716 for (cnt = 0; cnt < 30000; cnt++) {
3d71644c 717 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
abbd8870
AV
718 break;
719
720 udelay(10);
1da177e4 721 }
1da177e4 722
abbd8870
AV
723 spin_unlock_irqrestore(&ha->hardware_lock, flags);
724 }
1da177e4 725
abbd8870
AV
726 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
727
737faece 728 pci_disable_rom(ha->pdev);
1da177e4 729
abbd8870
AV
730 /* Get PCI bus information. */
731 spin_lock_irqsave(&ha->hardware_lock, flags);
3d71644c 732 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
733 spin_unlock_irqrestore(&ha->hardware_lock, flags);
734
735 return QLA_SUCCESS;
1da177e4
LT
736}
737
0107109e
AV
738/**
739 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
740 * @ha: HA context
741 *
742 * Returns 0 on success.
743 */
744int
e315cd28 745qla24xx_pci_config(scsi_qla_host_t *vha)
0107109e 746{
a157b101 747 uint16_t w;
0107109e 748 unsigned long flags = 0;
e315cd28 749 struct qla_hw_data *ha = vha->hw;
0107109e 750 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
0107109e
AV
751
752 pci_set_master(ha->pdev);
af6177d8 753 pci_try_set_mwi(ha->pdev);
0107109e
AV
754
755 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
a157b101 756 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
0107109e
AV
757 w &= ~PCI_COMMAND_INTX_DISABLE;
758 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
759
760 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
761
762 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
f85ec187
AV
763 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
764 pcix_set_mmrbc(ha->pdev, 2048);
0107109e
AV
765
766 /* PCIe -- adjust Maximum Read Request Size (2048). */
f85ec187
AV
767 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
768 pcie_set_readrq(ha->pdev, 2048);
0107109e 769
737faece 770 pci_disable_rom(ha->pdev);
0107109e 771
44c10138 772 ha->chip_revision = ha->pdev->revision;
a8488abe 773
0107109e
AV
774 /* Get PCI bus information. */
775 spin_lock_irqsave(&ha->hardware_lock, flags);
776 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
777 spin_unlock_irqrestore(&ha->hardware_lock, flags);
778
779 return QLA_SUCCESS;
780}
781
c3a2f0df
AV
782/**
783 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
784 * @ha: HA context
785 *
786 * Returns 0 on success.
787 */
788int
e315cd28 789qla25xx_pci_config(scsi_qla_host_t *vha)
c3a2f0df
AV
790{
791 uint16_t w;
e315cd28 792 struct qla_hw_data *ha = vha->hw;
c3a2f0df
AV
793
794 pci_set_master(ha->pdev);
795 pci_try_set_mwi(ha->pdev);
796
797 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
798 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
799 w &= ~PCI_COMMAND_INTX_DISABLE;
800 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
801
802 /* PCIe -- adjust Maximum Read Request Size (2048). */
803 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
804 pcie_set_readrq(ha->pdev, 2048);
805
737faece 806 pci_disable_rom(ha->pdev);
c3a2f0df
AV
807
808 ha->chip_revision = ha->pdev->revision;
809
810 return QLA_SUCCESS;
811}
812
1da177e4
LT
813/**
814 * qla2x00_isp_firmware() - Choose firmware image.
815 * @ha: HA context
816 *
817 * Returns 0 on success.
818 */
819static int
e315cd28 820qla2x00_isp_firmware(scsi_qla_host_t *vha)
1da177e4
LT
821{
822 int rval;
42e421b1
AV
823 uint16_t loop_id, topo, sw_cap;
824 uint8_t domain, area, al_pa;
e315cd28 825 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
826
827 /* Assume loading risc code */
fa2a1ce5 828 rval = QLA_FUNCTION_FAILED;
1da177e4
LT
829
830 if (ha->flags.disable_risc_code_load) {
831 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
e315cd28 832 vha->host_no));
1da177e4
LT
833 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
834
835 /* Verify checksum of loaded RISC code. */
e315cd28 836 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
42e421b1
AV
837 if (rval == QLA_SUCCESS) {
838 /* And, verify we are not in ROM code. */
e315cd28 839 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
42e421b1
AV
840 &area, &domain, &topo, &sw_cap);
841 }
1da177e4
LT
842 }
843
844 if (rval) {
845 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
e315cd28 846 vha->host_no));
1da177e4
LT
847 }
848
849 return (rval);
850}
851
852/**
853 * qla2x00_reset_chip() - Reset ISP chip.
854 * @ha: HA context
855 *
856 * Returns 0 on success.
857 */
abbd8870 858void
e315cd28 859qla2x00_reset_chip(scsi_qla_host_t *vha)
1da177e4
LT
860{
861 unsigned long flags = 0;
e315cd28 862 struct qla_hw_data *ha = vha->hw;
3d71644c 863 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 864 uint32_t cnt;
1da177e4
LT
865 uint16_t cmd;
866
85880801
AV
867 if (unlikely(pci_channel_offline(ha->pdev)))
868 return;
869
fd34f556 870 ha->isp_ops->disable_intrs(ha);
1da177e4
LT
871
872 spin_lock_irqsave(&ha->hardware_lock, flags);
873
874 /* Turn off master enable */
875 cmd = 0;
876 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
877 cmd &= ~PCI_COMMAND_MASTER;
878 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
879
880 if (!IS_QLA2100(ha)) {
881 /* Pause RISC. */
882 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
883 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
884 for (cnt = 0; cnt < 30000; cnt++) {
885 if ((RD_REG_WORD(&reg->hccr) &
886 HCCR_RISC_PAUSE) != 0)
887 break;
888 udelay(100);
889 }
890 } else {
891 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
892 udelay(10);
893 }
894
895 /* Select FPM registers. */
896 WRT_REG_WORD(&reg->ctrl_status, 0x20);
897 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
898
899 /* FPM Soft Reset. */
900 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
901 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
902
903 /* Toggle Fpm Reset. */
904 if (!IS_QLA2200(ha)) {
905 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
906 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
907 }
908
909 /* Select frame buffer registers. */
910 WRT_REG_WORD(&reg->ctrl_status, 0x10);
911 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
912
913 /* Reset frame buffer FIFOs. */
914 if (IS_QLA2200(ha)) {
915 WRT_FB_CMD_REG(ha, reg, 0xa000);
916 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
917 } else {
918 WRT_FB_CMD_REG(ha, reg, 0x00fc);
919
920 /* Read back fb_cmd until zero or 3 seconds max */
921 for (cnt = 0; cnt < 3000; cnt++) {
922 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
923 break;
924 udelay(100);
925 }
926 }
927
928 /* Select RISC module registers. */
929 WRT_REG_WORD(&reg->ctrl_status, 0);
930 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
931
932 /* Reset RISC processor. */
933 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
934 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
935
936 /* Release RISC processor. */
937 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
938 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
939 }
940
941 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
942 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
943
944 /* Reset ISP chip. */
945 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
946
947 /* Wait for RISC to recover from reset. */
948 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
949 /*
950 * It is necessary to for a delay here since the card doesn't
951 * respond to PCI reads during a reset. On some architectures
952 * this will result in an MCA.
953 */
954 udelay(20);
955 for (cnt = 30000; cnt; cnt--) {
956 if ((RD_REG_WORD(&reg->ctrl_status) &
957 CSR_ISP_SOFT_RESET) == 0)
958 break;
959 udelay(100);
960 }
961 } else
962 udelay(10);
963
964 /* Reset RISC processor. */
965 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
966
967 WRT_REG_WORD(&reg->semaphore, 0);
968
969 /* Release RISC processor. */
970 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
971 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
972
973 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
974 for (cnt = 0; cnt < 30000; cnt++) {
ffb39f03 975 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
1da177e4 976 break;
1da177e4
LT
977
978 udelay(100);
979 }
980 } else
981 udelay(100);
982
983 /* Turn on master enable */
984 cmd |= PCI_COMMAND_MASTER;
985 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
986
987 /* Disable RISC pause on FPM parity error. */
988 if (!IS_QLA2100(ha)) {
989 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
990 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
991 }
992
993 spin_unlock_irqrestore(&ha->hardware_lock, flags);
994}
995
0107109e 996/**
88c26663 997 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
0107109e
AV
998 * @ha: HA context
999 *
1000 * Returns 0 on success.
1001 */
88c26663 1002static inline void
e315cd28 1003qla24xx_reset_risc(scsi_qla_host_t *vha)
0107109e
AV
1004{
1005 unsigned long flags = 0;
e315cd28 1006 struct qla_hw_data *ha = vha->hw;
0107109e
AV
1007 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1008 uint32_t cnt, d2;
335a1cc9 1009 uint16_t wd;
0107109e 1010
0107109e
AV
1011 spin_lock_irqsave(&ha->hardware_lock, flags);
1012
1013 /* Reset RISC. */
1014 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
1015 for (cnt = 0; cnt < 30000; cnt++) {
1016 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
1017 break;
1018
1019 udelay(10);
1020 }
1021
1022 WRT_REG_DWORD(&reg->ctrl_status,
1023 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
335a1cc9 1024 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
88c26663 1025
335a1cc9 1026 udelay(100);
88c26663 1027 /* Wait for firmware to complete NVRAM accesses. */
88c26663
AV
1028 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1029 for (cnt = 10000 ; cnt && d2; cnt--) {
1030 udelay(5);
1031 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1032 barrier();
1033 }
1034
335a1cc9 1035 /* Wait for soft-reset to complete. */
0107109e
AV
1036 d2 = RD_REG_DWORD(&reg->ctrl_status);
1037 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
1038 udelay(5);
1039 d2 = RD_REG_DWORD(&reg->ctrl_status);
1040 barrier();
1041 }
1042
1043 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
1044 RD_REG_DWORD(&reg->hccr);
1045
1046 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
1047 RD_REG_DWORD(&reg->hccr);
1048
1049 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
1050 RD_REG_DWORD(&reg->hccr);
1051
1052 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1053 for (cnt = 6000000 ; cnt && d2; cnt--) {
1054 udelay(5);
1055 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1056 barrier();
1057 }
1058
1059 spin_unlock_irqrestore(&ha->hardware_lock, flags);
124f85e6
AV
1060
1061 if (IS_NOPOLLING_TYPE(ha))
1062 ha->isp_ops->enable_intrs(ha);
0107109e
AV
1063}
1064
88c26663
AV
1065/**
1066 * qla24xx_reset_chip() - Reset ISP24xx chip.
1067 * @ha: HA context
1068 *
1069 * Returns 0 on success.
1070 */
1071void
e315cd28 1072qla24xx_reset_chip(scsi_qla_host_t *vha)
88c26663 1073{
e315cd28 1074 struct qla_hw_data *ha = vha->hw;
85880801
AV
1075
1076 if (pci_channel_offline(ha->pdev) &&
1077 ha->flags.pci_channel_io_perm_failure) {
1078 return;
1079 }
1080
fd34f556 1081 ha->isp_ops->disable_intrs(ha);
88c26663
AV
1082
1083 /* Perform RISC reset. */
e315cd28 1084 qla24xx_reset_risc(vha);
88c26663
AV
1085}
1086
1da177e4
LT
1087/**
1088 * qla2x00_chip_diag() - Test chip for proper operation.
1089 * @ha: HA context
1090 *
1091 * Returns 0 on success.
1092 */
abbd8870 1093int
e315cd28 1094qla2x00_chip_diag(scsi_qla_host_t *vha)
1da177e4
LT
1095{
1096 int rval;
e315cd28 1097 struct qla_hw_data *ha = vha->hw;
3d71644c 1098 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
1099 unsigned long flags = 0;
1100 uint16_t data;
1101 uint32_t cnt;
1102 uint16_t mb[5];
73208dfd 1103 struct req_que *req = ha->req_q_map[0];
1da177e4
LT
1104
1105 /* Assume a failed state */
1106 rval = QLA_FUNCTION_FAILED;
1107
1108 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
e315cd28 1109 vha->host_no, (u_long)&reg->flash_address));
1da177e4
LT
1110
1111 spin_lock_irqsave(&ha->hardware_lock, flags);
1112
1113 /* Reset ISP chip. */
1114 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
1115
1116 /*
1117 * We need to have a delay here since the card will not respond while
1118 * in reset causing an MCA on some architectures.
1119 */
1120 udelay(20);
1121 data = qla2x00_debounce_register(&reg->ctrl_status);
1122 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
1123 udelay(5);
1124 data = RD_REG_WORD(&reg->ctrl_status);
1125 barrier();
1126 }
1127
1128 if (!cnt)
1129 goto chip_diag_failed;
1130
1131 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
7640335e 1132 vha->host_no));
1da177e4
LT
1133
1134 /* Reset RISC processor. */
1135 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
1136 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
1137
1138 /* Workaround for QLA2312 PCI parity error */
1139 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
1140 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
1141 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
1142 udelay(5);
1143 data = RD_MAILBOX_REG(ha, reg, 0);
fa2a1ce5 1144 barrier();
1da177e4
LT
1145 }
1146 } else
1147 udelay(10);
1148
1149 if (!cnt)
1150 goto chip_diag_failed;
1151
1152 /* Check product ID of chip */
7640335e 1153 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", vha->host_no));
1da177e4
LT
1154
1155 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
1156 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
1157 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
1158 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
1159 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
1160 mb[3] != PROD_ID_3) {
1161 qla_printk(KERN_WARNING, ha,
1162 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
1163
1164 goto chip_diag_failed;
1165 }
1166 ha->product_id[0] = mb[1];
1167 ha->product_id[1] = mb[2];
1168 ha->product_id[2] = mb[3];
1169 ha->product_id[3] = mb[4];
1170
1171 /* Adjust fw RISC transfer size */
73208dfd 1172 if (req->length > 1024)
1da177e4
LT
1173 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
1174 else
1175 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
73208dfd 1176 req->length;
1da177e4
LT
1177
1178 if (IS_QLA2200(ha) &&
1179 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
1180 /* Limit firmware transfer size with a 2200A */
1181 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
e315cd28 1182 vha->host_no));
1da177e4 1183
ea5b6382 1184 ha->device_type |= DT_ISP2200A;
1da177e4
LT
1185 ha->fw_transfer_size = 128;
1186 }
1187
1188 /* Wrap Incoming Mailboxes Test. */
1189 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1190
e315cd28
AC
1191 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", vha->host_no));
1192 rval = qla2x00_mbx_reg_test(vha);
1da177e4
LT
1193 if (rval) {
1194 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
e315cd28 1195 vha->host_no));
1da177e4
LT
1196 qla_printk(KERN_WARNING, ha,
1197 "Failed mailbox send register test\n");
1198 }
1199 else {
1200 /* Flag a successful rval */
1201 rval = QLA_SUCCESS;
1202 }
1203 spin_lock_irqsave(&ha->hardware_lock, flags);
1204
1205chip_diag_failed:
1206 if (rval)
1207 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
e315cd28 1208 "****\n", vha->host_no));
1da177e4
LT
1209
1210 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1211
1212 return (rval);
1213}
1214
0107109e
AV
1215/**
1216 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
1217 * @ha: HA context
1218 *
1219 * Returns 0 on success.
1220 */
1221int
e315cd28 1222qla24xx_chip_diag(scsi_qla_host_t *vha)
0107109e
AV
1223{
1224 int rval;
e315cd28 1225 struct qla_hw_data *ha = vha->hw;
73208dfd 1226 struct req_que *req = ha->req_q_map[0];
0107109e 1227
a9083016
GM
1228 if (IS_QLA82XX(ha))
1229 return QLA_SUCCESS;
1230
73208dfd 1231 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
0107109e 1232
e315cd28 1233 rval = qla2x00_mbx_reg_test(vha);
0107109e
AV
1234 if (rval) {
1235 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
e315cd28 1236 vha->host_no));
0107109e
AV
1237 qla_printk(KERN_WARNING, ha,
1238 "Failed mailbox send register test\n");
1239 } else {
1240 /* Flag a successful rval */
1241 rval = QLA_SUCCESS;
1242 }
1243
1244 return rval;
1245}
1246
a7a167bf 1247void
e315cd28 1248qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
0107109e 1249{
a7a167bf
AV
1250 int rval;
1251 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
73208dfd 1252 eft_size, fce_size, mq_size;
df613b96
AV
1253 dma_addr_t tc_dma;
1254 void *tc;
e315cd28 1255 struct qla_hw_data *ha = vha->hw;
73208dfd
AC
1256 struct req_que *req = ha->req_q_map[0];
1257 struct rsp_que *rsp = ha->rsp_q_map[0];
a7a167bf
AV
1258
1259 if (ha->fw_dump) {
1260 qla_printk(KERN_WARNING, ha,
1261 "Firmware dump previously allocated.\n");
1262 return;
1263 }
d4e3e04d 1264
0107109e 1265 ha->fw_dumped = 0;
73208dfd 1266 fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
d4e3e04d 1267 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
a7a167bf 1268 fixed_size = sizeof(struct qla2100_fw_dump);
d4e3e04d 1269 } else if (IS_QLA23XX(ha)) {
a7a167bf
AV
1270 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
1271 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
1272 sizeof(uint16_t);
e428924c 1273 } else if (IS_FWI2_CAPABLE(ha)) {
3a03eb79
AV
1274 if (IS_QLA81XX(ha))
1275 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
1276 else if (IS_QLA25XX(ha))
1277 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
1278 else
1279 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
a7a167bf
AV
1280 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
1281 sizeof(uint32_t);
73208dfd
AC
1282 if (ha->mqenable)
1283 mq_size = sizeof(struct qla2xxx_mq_chain);
df613b96 1284 /* Allocate memory for Fibre Channel Event Buffer. */
3a03eb79 1285 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha))
436a7b11 1286 goto try_eft;
df613b96
AV
1287
1288 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
1289 GFP_KERNEL);
1290 if (!tc) {
1291 qla_printk(KERN_WARNING, ha, "Unable to allocate "
1292 "(%d KB) for FCE.\n", FCE_SIZE / 1024);
17d98630 1293 goto try_eft;
df613b96
AV
1294 }
1295
1296 memset(tc, 0, FCE_SIZE);
e315cd28 1297 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
df613b96
AV
1298 ha->fce_mb, &ha->fce_bufs);
1299 if (rval) {
1300 qla_printk(KERN_WARNING, ha, "Unable to initialize "
1301 "FCE (%d).\n", rval);
1302 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
1303 tc_dma);
1304 ha->flags.fce_enabled = 0;
17d98630 1305 goto try_eft;
df613b96
AV
1306 }
1307
1308 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
1309 FCE_SIZE / 1024);
1310
7d9dade3 1311 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
df613b96
AV
1312 ha->flags.fce_enabled = 1;
1313 ha->fce_dma = tc_dma;
1314 ha->fce = tc;
436a7b11
AV
1315try_eft:
1316 /* Allocate memory for Extended Trace Buffer. */
1317 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
1318 GFP_KERNEL);
1319 if (!tc) {
1320 qla_printk(KERN_WARNING, ha, "Unable to allocate "
1321 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
1322 goto cont_alloc;
1323 }
1324
1325 memset(tc, 0, EFT_SIZE);
e315cd28 1326 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
436a7b11
AV
1327 if (rval) {
1328 qla_printk(KERN_WARNING, ha, "Unable to initialize "
1329 "EFT (%d).\n", rval);
1330 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
1331 tc_dma);
1332 goto cont_alloc;
1333 }
1334
1335 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
1336 EFT_SIZE / 1024);
1337
1338 eft_size = EFT_SIZE;
1339 ha->eft_dma = tc_dma;
1340 ha->eft = tc;
d4e3e04d 1341 }
a7a167bf 1342cont_alloc:
73208dfd
AC
1343 req_q_size = req->length * sizeof(request_t);
1344 rsp_q_size = rsp->length * sizeof(response_t);
a7a167bf
AV
1345
1346 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
2afa19a9 1347 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
bb99de67
AV
1348 ha->chain_offset = dump_size;
1349 dump_size += mq_size + fce_size;
d4e3e04d
AV
1350
1351 ha->fw_dump = vmalloc(dump_size);
a7a167bf 1352 if (!ha->fw_dump) {
0107109e 1353 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
d4e3e04d 1354 "firmware dump!!!\n", dump_size / 1024);
a7a167bf
AV
1355
1356 if (ha->eft) {
1357 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
1358 ha->eft_dma);
1359 ha->eft = NULL;
1360 ha->eft_dma = 0;
1361 }
1362 return;
1363 }
a7a167bf
AV
1364 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
1365 dump_size / 1024);
1366
1367 ha->fw_dump_len = dump_size;
1368 ha->fw_dump->signature[0] = 'Q';
1369 ha->fw_dump->signature[1] = 'L';
1370 ha->fw_dump->signature[2] = 'G';
1371 ha->fw_dump->signature[3] = 'C';
1372 ha->fw_dump->version = __constant_htonl(1);
1373
1374 ha->fw_dump->fixed_size = htonl(fixed_size);
1375 ha->fw_dump->mem_size = htonl(mem_size);
1376 ha->fw_dump->req_q_size = htonl(req_q_size);
1377 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
1378
1379 ha->fw_dump->eft_size = htonl(eft_size);
1380 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
1381 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
1382
1383 ha->fw_dump->header_size =
1384 htonl(offsetof(struct qla2xxx_fw_dump, isp));
0107109e
AV
1385}
1386
18e7555a
AV
1387static int
1388qla81xx_mpi_sync(scsi_qla_host_t *vha)
1389{
1390#define MPS_MASK 0xe0
1391 int rval;
1392 uint16_t dc;
1393 uint32_t dw;
1394 struct qla_hw_data *ha = vha->hw;
1395
1396 if (!IS_QLA81XX(vha->hw))
1397 return QLA_SUCCESS;
1398
1399 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
1400 if (rval != QLA_SUCCESS) {
1401 DEBUG2(qla_printk(KERN_WARNING, ha,
1402 "Sync-MPI: Unable to acquire semaphore.\n"));
1403 goto done;
1404 }
1405
1406 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
1407 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
1408 if (rval != QLA_SUCCESS) {
1409 DEBUG2(qla_printk(KERN_WARNING, ha,
1410 "Sync-MPI: Unable to read sync.\n"));
1411 goto done_release;
1412 }
1413
1414 dc &= MPS_MASK;
1415 if (dc == (dw & MPS_MASK))
1416 goto done_release;
1417
1418 dw &= ~MPS_MASK;
1419 dw |= dc;
1420 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
1421 if (rval != QLA_SUCCESS) {
1422 DEBUG2(qla_printk(KERN_WARNING, ha,
1423 "Sync-MPI: Unable to gain sync.\n"));
1424 }
1425
1426done_release:
1427 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
1428 if (rval != QLA_SUCCESS) {
1429 DEBUG2(qla_printk(KERN_WARNING, ha,
1430 "Sync-MPI: Unable to release semaphore.\n"));
1431 }
1432
1433done:
1434 return rval;
1435}
1436
1da177e4
LT
1437/**
1438 * qla2x00_setup_chip() - Load and start RISC firmware.
1439 * @ha: HA context
1440 *
1441 * Returns 0 on success.
1442 */
1443static int
e315cd28 1444qla2x00_setup_chip(scsi_qla_host_t *vha)
1da177e4 1445{
0107109e
AV
1446 int rval;
1447 uint32_t srisc_address = 0;
e315cd28 1448 struct qla_hw_data *ha = vha->hw;
3db0652e
AV
1449 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1450 unsigned long flags;
dda772e8 1451 uint16_t fw_major_version;
3db0652e 1452
a9083016
GM
1453 if (IS_QLA82XX(ha)) {
1454 rval = ha->isp_ops->load_risc(vha, &srisc_address);
1455 if (rval == QLA_SUCCESS)
1456 goto enable_82xx_npiv;
1457 }
1458
3db0652e
AV
1459 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1460 /* Disable SRAM, Instruction RAM and GP RAM parity. */
1461 spin_lock_irqsave(&ha->hardware_lock, flags);
1462 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
1463 RD_REG_WORD(&reg->hccr);
1464 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1465 }
1da177e4 1466
18e7555a
AV
1467 qla81xx_mpi_sync(vha);
1468
1da177e4 1469 /* Load firmware sequences */
e315cd28 1470 rval = ha->isp_ops->load_risc(vha, &srisc_address);
0107109e 1471 if (rval == QLA_SUCCESS) {
1da177e4 1472 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
e315cd28 1473 "code.\n", vha->host_no));
1da177e4 1474
e315cd28 1475 rval = qla2x00_verify_checksum(vha, srisc_address);
1da177e4
LT
1476 if (rval == QLA_SUCCESS) {
1477 /* Start firmware execution. */
1478 DEBUG(printk("scsi(%ld): Checksum OK, start "
e315cd28 1479 "firmware.\n", vha->host_no));
1da177e4 1480
e315cd28 1481 rval = qla2x00_execute_fw(vha, srisc_address);
1da177e4 1482 /* Retrieve firmware information. */
dda772e8 1483 if (rval == QLA_SUCCESS) {
a9083016 1484enable_82xx_npiv:
dda772e8 1485 fw_major_version = ha->fw_major_version;
ca9e9c3e 1486 rval = qla2x00_get_fw_version(vha,
1da177e4
LT
1487 &ha->fw_major_version,
1488 &ha->fw_minor_version,
1489 &ha->fw_subminor_version,
3a03eb79 1490 &ha->fw_attributes, &ha->fw_memory_size,
55a96158
AV
1491 ha->mpi_version, &ha->mpi_capabilities,
1492 ha->phy_version);
ca9e9c3e
AV
1493 if (rval != QLA_SUCCESS)
1494 goto failed;
2c3dfe3f 1495 ha->flags.npiv_supported = 0;
e315cd28 1496 if (IS_QLA2XXX_MIDTYPE(ha) &&
946fb891 1497 (ha->fw_attributes & BIT_2)) {
2c3dfe3f 1498 ha->flags.npiv_supported = 1;
4d0ea247
SJ
1499 if ((!ha->max_npiv_vports) ||
1500 ((ha->max_npiv_vports + 1) %
eb66dc60 1501 MIN_MULTI_ID_FABRIC))
4d0ea247 1502 ha->max_npiv_vports =
eb66dc60 1503 MIN_MULTI_ID_FABRIC - 1;
4d0ea247 1504 }
24a08138
AV
1505 qla2x00_get_resource_cnts(vha, NULL,
1506 &ha->fw_xcb_count, NULL, NULL,
f3a0a77e 1507 &ha->max_npiv_vports, NULL);
d743de66 1508
a9083016
GM
1509 if (!fw_major_version && ql2xallocfwdump) {
1510 if (!IS_QLA82XX(ha))
1511 qla2x00_alloc_fw_dump(vha);
1512 }
1da177e4
LT
1513 }
1514 } else {
1515 DEBUG2(printk(KERN_INFO
1516 "scsi(%ld): ISP Firmware failed checksum.\n",
e315cd28 1517 vha->host_no));
1da177e4
LT
1518 }
1519 }
1520
3db0652e
AV
1521 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1522 /* Enable proper parity. */
1523 spin_lock_irqsave(&ha->hardware_lock, flags);
1524 if (IS_QLA2300(ha))
1525 /* SRAM parity */
1526 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
1527 else
1528 /* SRAM, Instruction RAM and GP RAM parity */
1529 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
1530 RD_REG_WORD(&reg->hccr);
1531 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1532 }
1533
1d2874de
JC
1534 if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
1535 uint32_t size;
1536
1537 rval = qla81xx_fac_get_sector_size(vha, &size);
1538 if (rval == QLA_SUCCESS) {
1539 ha->flags.fac_supported = 1;
1540 ha->fdt_block_size = size << 2;
1541 } else {
1542 qla_printk(KERN_ERR, ha,
1543 "Unsupported FAC firmware (%d.%02d.%02d).\n",
1544 ha->fw_major_version, ha->fw_minor_version,
1545 ha->fw_subminor_version);
1546 }
1547 }
ca9e9c3e 1548failed:
1da177e4
LT
1549 if (rval) {
1550 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
e315cd28 1551 vha->host_no));
1da177e4
LT
1552 }
1553
1554 return (rval);
1555}
1556
1557/**
1558 * qla2x00_init_response_q_entries() - Initializes response queue entries.
1559 * @ha: HA context
1560 *
1561 * Beginning of request ring has initialization control block already built
1562 * by nvram config routine.
1563 *
1564 * Returns 0 on success.
1565 */
73208dfd
AC
1566void
1567qla2x00_init_response_q_entries(struct rsp_que *rsp)
1da177e4
LT
1568{
1569 uint16_t cnt;
1570 response_t *pkt;
1571
2afa19a9
AC
1572 rsp->ring_ptr = rsp->ring;
1573 rsp->ring_index = 0;
1574 rsp->status_srb = NULL;
e315cd28
AC
1575 pkt = rsp->ring_ptr;
1576 for (cnt = 0; cnt < rsp->length; cnt++) {
1da177e4
LT
1577 pkt->signature = RESPONSE_PROCESSED;
1578 pkt++;
1579 }
1da177e4
LT
1580}
1581
1582/**
1583 * qla2x00_update_fw_options() - Read and process firmware options.
1584 * @ha: HA context
1585 *
1586 * Returns 0 on success.
1587 */
abbd8870 1588void
e315cd28 1589qla2x00_update_fw_options(scsi_qla_host_t *vha)
1da177e4
LT
1590{
1591 uint16_t swing, emphasis, tx_sens, rx_sens;
e315cd28 1592 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
1593
1594 memset(ha->fw_options, 0, sizeof(ha->fw_options));
e315cd28 1595 qla2x00_get_fw_options(vha, ha->fw_options);
1da177e4
LT
1596
1597 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1598 return;
1599
1600 /* Serial Link options. */
1601 DEBUG3(printk("scsi(%ld): Serial link options:\n",
e315cd28 1602 vha->host_no));
1da177e4
LT
1603 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1604 sizeof(ha->fw_seriallink_options)));
1605
1606 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1607 if (ha->fw_seriallink_options[3] & BIT_2) {
1608 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1609
1610 /* 1G settings */
1611 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1612 emphasis = (ha->fw_seriallink_options[2] &
1613 (BIT_4 | BIT_3)) >> 3;
1614 tx_sens = ha->fw_seriallink_options[0] &
fa2a1ce5 1615 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1da177e4
LT
1616 rx_sens = (ha->fw_seriallink_options[0] &
1617 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1618 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1619 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1620 if (rx_sens == 0x0)
1621 rx_sens = 0x3;
1622 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1623 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1624 ha->fw_options[10] |= BIT_5 |
1625 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1626 (tx_sens & (BIT_1 | BIT_0));
1627
1628 /* 2G settings */
1629 swing = (ha->fw_seriallink_options[2] &
1630 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1631 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1632 tx_sens = ha->fw_seriallink_options[1] &
fa2a1ce5 1633 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1da177e4
LT
1634 rx_sens = (ha->fw_seriallink_options[1] &
1635 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1636 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1637 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1638 if (rx_sens == 0x0)
1639 rx_sens = 0x3;
1640 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1641 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1642 ha->fw_options[11] |= BIT_5 |
1643 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1644 (tx_sens & (BIT_1 | BIT_0));
1645 }
1646
1647 /* FCP2 options. */
1648 /* Return command IOCBs without waiting for an ABTS to complete. */
1649 ha->fw_options[3] |= BIT_13;
1650
1651 /* LED scheme. */
1652 if (ha->flags.enable_led_scheme)
1653 ha->fw_options[2] |= BIT_12;
1654
48c02fde
AV
1655 /* Detect ISP6312. */
1656 if (IS_QLA6312(ha))
1657 ha->fw_options[2] |= BIT_13;
1658
1da177e4 1659 /* Update firmware options. */
e315cd28 1660 qla2x00_set_fw_options(vha, ha->fw_options);
1da177e4
LT
1661}
1662
0107109e 1663void
e315cd28 1664qla24xx_update_fw_options(scsi_qla_host_t *vha)
0107109e
AV
1665{
1666 int rval;
e315cd28 1667 struct qla_hw_data *ha = vha->hw;
0107109e 1668
a9083016
GM
1669 if (IS_QLA82XX(ha))
1670 return;
1671
0107109e 1672 /* Update Serial Link options. */
f94097ed 1673 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
0107109e
AV
1674 return;
1675
e315cd28 1676 rval = qla2x00_set_serdes_params(vha,
f94097ed
AV
1677 le16_to_cpu(ha->fw_seriallink_options24[1]),
1678 le16_to_cpu(ha->fw_seriallink_options24[2]),
1679 le16_to_cpu(ha->fw_seriallink_options24[3]));
0107109e
AV
1680 if (rval != QLA_SUCCESS) {
1681 qla_printk(KERN_WARNING, ha,
1682 "Unable to update Serial Link options (%x).\n", rval);
1683 }
1684}
1685
abbd8870 1686void
e315cd28 1687qla2x00_config_rings(struct scsi_qla_host *vha)
abbd8870 1688{
e315cd28 1689 struct qla_hw_data *ha = vha->hw;
3d71644c 1690 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
73208dfd
AC
1691 struct req_que *req = ha->req_q_map[0];
1692 struct rsp_que *rsp = ha->rsp_q_map[0];
abbd8870
AV
1693
1694 /* Setup ring parameters in initialization control block. */
1695 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1696 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
e315cd28
AC
1697 ha->init_cb->request_q_length = cpu_to_le16(req->length);
1698 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
1699 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1700 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1701 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1702 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
abbd8870
AV
1703
1704 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1705 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1706 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1707 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1708 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1709}
1710
0107109e 1711void
e315cd28 1712qla24xx_config_rings(struct scsi_qla_host *vha)
0107109e 1713{
e315cd28 1714 struct qla_hw_data *ha = vha->hw;
73208dfd
AC
1715 device_reg_t __iomem *reg = ISP_QUE_REG(ha, 0);
1716 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1717 struct qla_msix_entry *msix;
0107109e 1718 struct init_cb_24xx *icb;
73208dfd
AC
1719 uint16_t rid = 0;
1720 struct req_que *req = ha->req_q_map[0];
1721 struct rsp_que *rsp = ha->rsp_q_map[0];
0107109e 1722
73208dfd 1723/* Setup ring parameters in initialization control block. */
0107109e
AV
1724 icb = (struct init_cb_24xx *)ha->init_cb;
1725 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1726 icb->response_q_inpointer = __constant_cpu_to_le16(0);
e315cd28
AC
1727 icb->request_q_length = cpu_to_le16(req->length);
1728 icb->response_q_length = cpu_to_le16(rsp->length);
1729 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1730 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1731 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1732 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
0107109e 1733
73208dfd
AC
1734 if (ha->mqenable) {
1735 icb->qos = __constant_cpu_to_le16(QLA_DEFAULT_QUE_QOS);
1736 icb->rid = __constant_cpu_to_le16(rid);
1737 if (ha->flags.msix_enabled) {
1738 msix = &ha->msix_entries[1];
1739 DEBUG2_17(printk(KERN_INFO
2afa19a9 1740 "Registering vector 0x%x for base que\n", msix->entry));
73208dfd
AC
1741 icb->msix = cpu_to_le16(msix->entry);
1742 }
1743 /* Use alternate PCI bus number */
1744 if (MSB(rid))
1745 icb->firmware_options_2 |=
1746 __constant_cpu_to_le32(BIT_19);
1747 /* Use alternate PCI devfn */
1748 if (LSB(rid))
1749 icb->firmware_options_2 |=
1750 __constant_cpu_to_le32(BIT_18);
1751
3155754a
AC
1752 /* Use Disable MSIX Handshake mode for capable adapters */
1753 if (IS_MSIX_NACK_CAPABLE(ha)) {
1754 icb->firmware_options_2 &=
1755 __constant_cpu_to_le32(~BIT_22);
1756 ha->flags.disable_msix_handshake = 1;
1757 qla_printk(KERN_INFO, ha,
1758 "MSIX Handshake Disable Mode turned on\n");
1759 } else {
1760 icb->firmware_options_2 |=
1761 __constant_cpu_to_le32(BIT_22);
1762 }
73208dfd 1763 icb->firmware_options_2 |= __constant_cpu_to_le32(BIT_23);
73208dfd
AC
1764
1765 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
1766 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
1767 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
1768 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
1769 } else {
1770 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
1771 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
1772 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
1773 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
1774 }
1775 /* PCI posting */
1776 RD_REG_DWORD(&ioreg->hccr);
0107109e
AV
1777}
1778
1da177e4
LT
1779/**
1780 * qla2x00_init_rings() - Initializes firmware.
1781 * @ha: HA context
1782 *
1783 * Beginning of request ring has initialization control block already built
1784 * by nvram config routine.
1785 *
1786 * Returns 0 on success.
1787 */
1788static int
e315cd28 1789qla2x00_init_rings(scsi_qla_host_t *vha)
1da177e4
LT
1790{
1791 int rval;
1792 unsigned long flags = 0;
29bdccbe 1793 int cnt, que;
e315cd28 1794 struct qla_hw_data *ha = vha->hw;
29bdccbe
AC
1795 struct req_que *req;
1796 struct rsp_que *rsp;
1797 struct scsi_qla_host *vp;
2c3dfe3f
SJ
1798 struct mid_init_cb_24xx *mid_init_cb =
1799 (struct mid_init_cb_24xx *) ha->init_cb;
1da177e4
LT
1800
1801 spin_lock_irqsave(&ha->hardware_lock, flags);
1802
1803 /* Clear outstanding commands array. */
2afa19a9 1804 for (que = 0; que < ha->max_req_queues; que++) {
29bdccbe
AC
1805 req = ha->req_q_map[que];
1806 if (!req)
1807 continue;
2afa19a9 1808 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
29bdccbe 1809 req->outstanding_cmds[cnt] = NULL;
1da177e4 1810
2afa19a9 1811 req->current_outstanding_cmd = 1;
1da177e4 1812
29bdccbe
AC
1813 /* Initialize firmware. */
1814 req->ring_ptr = req->ring;
1815 req->ring_index = 0;
1816 req->cnt = req->length;
1817 }
1da177e4 1818
2afa19a9 1819 for (que = 0; que < ha->max_rsp_queues; que++) {
29bdccbe
AC
1820 rsp = ha->rsp_q_map[que];
1821 if (!rsp)
1822 continue;
29bdccbe
AC
1823 /* Initialize response queue entries */
1824 qla2x00_init_response_q_entries(rsp);
1825 }
1da177e4 1826
29bdccbe
AC
1827 /* Clear RSCN queue. */
1828 list_for_each_entry(vp, &ha->vp_list, list) {
1829 vp->rscn_in_ptr = 0;
1830 vp->rscn_out_ptr = 0;
1831 }
e315cd28 1832 ha->isp_ops->config_rings(vha);
1da177e4
LT
1833
1834 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1835
1836 /* Update any ISP specific firmware options before initialization. */
e315cd28 1837 ha->isp_ops->update_fw_options(vha);
1da177e4 1838
e315cd28 1839 DEBUG(printk("scsi(%ld): Issue init firmware.\n", vha->host_no));
2c3dfe3f 1840
605aa2bc
LC
1841 if (ha->flags.npiv_supported) {
1842 if (ha->operating_mode == LOOP)
1843 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
c48339de 1844 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
605aa2bc
LC
1845 }
1846
24a08138
AV
1847 if (IS_FWI2_CAPABLE(ha)) {
1848 mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
1849 mid_init_cb->init_cb.execution_throttle =
1850 cpu_to_le16(ha->fw_xcb_count);
1851 }
2c3dfe3f 1852
e315cd28 1853 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
1da177e4
LT
1854 if (rval) {
1855 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
e315cd28 1856 vha->host_no));
1da177e4
LT
1857 } else {
1858 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
e315cd28 1859 vha->host_no));
1da177e4
LT
1860 }
1861
1862 return (rval);
1863}
1864
1865/**
1866 * qla2x00_fw_ready() - Waits for firmware ready.
1867 * @ha: HA context
1868 *
1869 * Returns 0 on success.
1870 */
1871static int
e315cd28 1872qla2x00_fw_ready(scsi_qla_host_t *vha)
1da177e4
LT
1873{
1874 int rval;
4d4df193 1875 unsigned long wtime, mtime, cs84xx_time;
1da177e4
LT
1876 uint16_t min_wait; /* Minimum wait time if loop is down */
1877 uint16_t wait_time; /* Wait time if loop is coming ready */
656e8912 1878 uint16_t state[5];
e315cd28 1879 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
1880
1881 rval = QLA_SUCCESS;
1882
1883 /* 20 seconds for loop down. */
fa2a1ce5 1884 min_wait = 20;
1da177e4
LT
1885
1886 /*
1887 * Firmware should take at most one RATOV to login, plus 5 seconds for
1888 * our own processing.
1889 */
1890 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1891 wait_time = min_wait;
1892 }
1893
1894 /* Min wait time if loop down */
1895 mtime = jiffies + (min_wait * HZ);
1896
1897 /* wait time before firmware ready */
1898 wtime = jiffies + (wait_time * HZ);
1899
1900 /* Wait for ISP to finish LIP */
e315cd28 1901 if (!vha->flags.init_done)
1da177e4
LT
1902 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1903
1904 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
e315cd28 1905 vha->host_no));
1da177e4
LT
1906
1907 do {
e315cd28 1908 rval = qla2x00_get_firmware_state(vha, state);
1da177e4 1909 if (rval == QLA_SUCCESS) {
4d4df193 1910 if (state[0] < FSTATE_LOSS_OF_SYNC) {
e315cd28 1911 vha->device_flags &= ~DFLG_NO_CABLE;
1da177e4 1912 }
4d4df193
HK
1913 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
1914 DEBUG16(printk("scsi(%ld): fw_state=%x "
e315cd28 1915 "84xx=%x.\n", vha->host_no, state[0],
4d4df193
HK
1916 state[2]));
1917 if ((state[2] & FSTATE_LOGGED_IN) &&
1918 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
1919 DEBUG16(printk("scsi(%ld): Sending "
e315cd28 1920 "verify iocb.\n", vha->host_no));
4d4df193
HK
1921
1922 cs84xx_time = jiffies;
e315cd28 1923 rval = qla84xx_init_chip(vha);
4d4df193
HK
1924 if (rval != QLA_SUCCESS)
1925 break;
1926
1927 /* Add time taken to initialize. */
1928 cs84xx_time = jiffies - cs84xx_time;
1929 wtime += cs84xx_time;
1930 mtime += cs84xx_time;
1931 DEBUG16(printk("scsi(%ld): Increasing "
1932 "wait time by %ld. New time %ld\n",
e315cd28 1933 vha->host_no, cs84xx_time, wtime));
4d4df193
HK
1934 }
1935 } else if (state[0] == FSTATE_READY) {
1da177e4 1936 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
e315cd28 1937 vha->host_no));
1da177e4 1938
e315cd28 1939 qla2x00_get_retry_cnt(vha, &ha->retry_count,
1da177e4
LT
1940 &ha->login_timeout, &ha->r_a_tov);
1941
1942 rval = QLA_SUCCESS;
1943 break;
1944 }
1945
1946 rval = QLA_FUNCTION_FAILED;
1947
e315cd28 1948 if (atomic_read(&vha->loop_down_timer) &&
4d4df193 1949 state[0] != FSTATE_READY) {
1da177e4 1950 /* Loop down. Timeout on min_wait for states
fa2a1ce5
AV
1951 * other than Wait for Login.
1952 */
1da177e4
LT
1953 if (time_after_eq(jiffies, mtime)) {
1954 qla_printk(KERN_INFO, ha,
1955 "Cable is unplugged...\n");
1956
e315cd28 1957 vha->device_flags |= DFLG_NO_CABLE;
1da177e4
LT
1958 break;
1959 }
1960 }
1961 } else {
1962 /* Mailbox cmd failed. Timeout on min_wait. */
1963 if (time_after_eq(jiffies, mtime))
1964 break;
1965 }
1966
1967 if (time_after_eq(jiffies, wtime))
1968 break;
1969
1970 /* Delay for a while */
1971 msleep(500);
1972
1973 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
e315cd28 1974 vha->host_no, state[0], jiffies));
1da177e4
LT
1975 } while (1);
1976
656e8912
AV
1977 DEBUG(printk("scsi(%ld): fw_state=%x (%x, %x, %x, %x) curr time=%lx.\n",
1978 vha->host_no, state[0], state[1], state[2], state[3], state[4],
1979 jiffies));
1da177e4
LT
1980
1981 if (rval) {
1982 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
e315cd28 1983 vha->host_no));
1da177e4
LT
1984 }
1985
1986 return (rval);
1987}
1988
1989/*
1990* qla2x00_configure_hba
1991* Setup adapter context.
1992*
1993* Input:
1994* ha = adapter state pointer.
1995*
1996* Returns:
1997* 0 = success
1998*
1999* Context:
2000* Kernel context.
2001*/
2002static int
e315cd28 2003qla2x00_configure_hba(scsi_qla_host_t *vha)
1da177e4
LT
2004{
2005 int rval;
2006 uint16_t loop_id;
2007 uint16_t topo;
2c3dfe3f 2008 uint16_t sw_cap;
1da177e4
LT
2009 uint8_t al_pa;
2010 uint8_t area;
2011 uint8_t domain;
2012 char connect_type[22];
e315cd28 2013 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
2014
2015 /* Get host addresses. */
e315cd28 2016 rval = qla2x00_get_adapter_id(vha,
2c3dfe3f 2017 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
1da177e4 2018 if (rval != QLA_SUCCESS) {
e315cd28 2019 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
33135aa2
RA
2020 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
2021 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
e315cd28 2022 __func__, vha->host_no));
33135aa2
RA
2023 } else {
2024 qla_printk(KERN_WARNING, ha,
2025 "ERROR -- Unable to get host loop ID.\n");
e315cd28 2026 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
33135aa2 2027 }
1da177e4
LT
2028 return (rval);
2029 }
2030
2031 if (topo == 4) {
2032 qla_printk(KERN_INFO, ha,
2033 "Cannot get topology - retrying.\n");
2034 return (QLA_FUNCTION_FAILED);
2035 }
2036
e315cd28 2037 vha->loop_id = loop_id;
1da177e4
LT
2038
2039 /* initialize */
2040 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
2041 ha->operating_mode = LOOP;
2c3dfe3f 2042 ha->switch_cap = 0;
1da177e4
LT
2043
2044 switch (topo) {
2045 case 0:
2046 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
e315cd28 2047 vha->host_no));
1da177e4
LT
2048 ha->current_topology = ISP_CFG_NL;
2049 strcpy(connect_type, "(Loop)");
2050 break;
2051
2052 case 1:
2053 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
e315cd28 2054 vha->host_no));
2c3dfe3f 2055 ha->switch_cap = sw_cap;
1da177e4
LT
2056 ha->current_topology = ISP_CFG_FL;
2057 strcpy(connect_type, "(FL_Port)");
2058 break;
2059
2060 case 2:
2061 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
e315cd28 2062 vha->host_no));
1da177e4
LT
2063 ha->operating_mode = P2P;
2064 ha->current_topology = ISP_CFG_N;
2065 strcpy(connect_type, "(N_Port-to-N_Port)");
2066 break;
2067
2068 case 3:
2069 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
e315cd28 2070 vha->host_no));
2c3dfe3f 2071 ha->switch_cap = sw_cap;
1da177e4
LT
2072 ha->operating_mode = P2P;
2073 ha->current_topology = ISP_CFG_F;
2074 strcpy(connect_type, "(F_Port)");
2075 break;
2076
2077 default:
2078 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
2079 "Using NL.\n",
e315cd28 2080 vha->host_no, topo));
1da177e4
LT
2081 ha->current_topology = ISP_CFG_NL;
2082 strcpy(connect_type, "(Loop)");
2083 break;
2084 }
2085
2086 /* Save Host port and loop ID. */
2087 /* byte order - Big Endian */
e315cd28
AC
2088 vha->d_id.b.domain = domain;
2089 vha->d_id.b.area = area;
2090 vha->d_id.b.al_pa = al_pa;
1da177e4 2091
e315cd28 2092 if (!vha->flags.init_done)
1da177e4
LT
2093 qla_printk(KERN_INFO, ha,
2094 "Topology - %s, Host Loop address 0x%x\n",
e315cd28 2095 connect_type, vha->loop_id);
1da177e4
LT
2096
2097 if (rval) {
e315cd28 2098 DEBUG2_3(printk("scsi(%ld): FAILED.\n", vha->host_no));
1da177e4 2099 } else {
e315cd28 2100 DEBUG3(printk("scsi(%ld): exiting normally.\n", vha->host_no));
1da177e4
LT
2101 }
2102
2103 return(rval);
2104}
2105
a9083016 2106inline void
e315cd28
AC
2107qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
2108 char *def)
9bb9fcf2
AV
2109{
2110 char *st, *en;
2111 uint16_t index;
e315cd28 2112 struct qla_hw_data *ha = vha->hw;
ab671149 2113 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
a9083016 2114 !IS_QLA8XXX_TYPE(ha);
9bb9fcf2
AV
2115
2116 if (memcmp(model, BINZERO, len) != 0) {
2117 strncpy(ha->model_number, model, len);
2118 st = en = ha->model_number;
2119 en += len - 1;
2120 while (en > st) {
2121 if (*en != 0x20 && *en != 0x00)
2122 break;
2123 *en-- = '\0';
2124 }
2125
2126 index = (ha->pdev->subsystem_device & 0xff);
7d0dba17
AV
2127 if (use_tbl &&
2128 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
9bb9fcf2 2129 index < QLA_MODEL_NAMES)
1ee27146
JC
2130 strncpy(ha->model_desc,
2131 qla2x00_model_name[index * 2 + 1],
2132 sizeof(ha->model_desc) - 1);
9bb9fcf2
AV
2133 } else {
2134 index = (ha->pdev->subsystem_device & 0xff);
7d0dba17
AV
2135 if (use_tbl &&
2136 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
9bb9fcf2
AV
2137 index < QLA_MODEL_NAMES) {
2138 strcpy(ha->model_number,
2139 qla2x00_model_name[index * 2]);
1ee27146
JC
2140 strncpy(ha->model_desc,
2141 qla2x00_model_name[index * 2 + 1],
2142 sizeof(ha->model_desc) - 1);
9bb9fcf2
AV
2143 } else {
2144 strcpy(ha->model_number, def);
2145 }
2146 }
1ee27146 2147 if (IS_FWI2_CAPABLE(ha))
e315cd28 2148 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
1ee27146 2149 sizeof(ha->model_desc));
9bb9fcf2
AV
2150}
2151
4e08df3f
DM
2152/* On sparc systems, obtain port and node WWN from firmware
2153 * properties.
2154 */
e315cd28 2155static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
4e08df3f
DM
2156{
2157#ifdef CONFIG_SPARC
e315cd28 2158 struct qla_hw_data *ha = vha->hw;
4e08df3f 2159 struct pci_dev *pdev = ha->pdev;
15576bc8
DM
2160 struct device_node *dp = pci_device_to_OF_node(pdev);
2161 const u8 *val;
4e08df3f
DM
2162 int len;
2163
2164 val = of_get_property(dp, "port-wwn", &len);
2165 if (val && len >= WWN_SIZE)
2166 memcpy(nv->port_name, val, WWN_SIZE);
2167
2168 val = of_get_property(dp, "node-wwn", &len);
2169 if (val && len >= WWN_SIZE)
2170 memcpy(nv->node_name, val, WWN_SIZE);
2171#endif
2172}
2173
1da177e4
LT
2174/*
2175* NVRAM configuration for ISP 2xxx
2176*
2177* Input:
2178* ha = adapter block pointer.
2179*
2180* Output:
2181* initialization control block in response_ring
2182* host adapters parameters in host adapter block
2183*
2184* Returns:
2185* 0 = success.
2186*/
abbd8870 2187int
e315cd28 2188qla2x00_nvram_config(scsi_qla_host_t *vha)
1da177e4 2189{
4e08df3f 2190 int rval;
0107109e
AV
2191 uint8_t chksum = 0;
2192 uint16_t cnt;
2193 uint8_t *dptr1, *dptr2;
e315cd28 2194 struct qla_hw_data *ha = vha->hw;
0107109e 2195 init_cb_t *icb = ha->init_cb;
281afe19
SJ
2196 nvram_t *nv = ha->nvram;
2197 uint8_t *ptr = ha->nvram;
3d71644c 2198 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 2199
4e08df3f
DM
2200 rval = QLA_SUCCESS;
2201
1da177e4 2202 /* Determine NVRAM starting address. */
0107109e 2203 ha->nvram_size = sizeof(nvram_t);
1da177e4
LT
2204 ha->nvram_base = 0;
2205 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
2206 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
2207 ha->nvram_base = 0x80;
2208
2209 /* Get NVRAM data and calculate checksum. */
e315cd28 2210 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
0107109e
AV
2211 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
2212 chksum += *ptr++;
1da177e4 2213
e315cd28 2214 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
281afe19 2215 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
1da177e4
LT
2216
2217 /* Bad NVRAM data, set defaults parameters. */
2218 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
2219 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
2220 /* Reset NVRAM data. */
2221 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
2222 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
2223 nv->nvram_version);
4e08df3f
DM
2224 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
2225 "invalid -- WWPN) defaults.\n");
2226
2227 /*
2228 * Set default initialization control block.
2229 */
2230 memset(nv, 0, ha->nvram_size);
2231 nv->parameter_block_version = ICB_VERSION;
2232
2233 if (IS_QLA23XX(ha)) {
2234 nv->firmware_options[0] = BIT_2 | BIT_1;
2235 nv->firmware_options[1] = BIT_7 | BIT_5;
2236 nv->add_firmware_options[0] = BIT_5;
2237 nv->add_firmware_options[1] = BIT_5 | BIT_4;
2238 nv->frame_payload_size = __constant_cpu_to_le16(2048);
2239 nv->special_options[1] = BIT_7;
2240 } else if (IS_QLA2200(ha)) {
2241 nv->firmware_options[0] = BIT_2 | BIT_1;
2242 nv->firmware_options[1] = BIT_7 | BIT_5;
2243 nv->add_firmware_options[0] = BIT_5;
2244 nv->add_firmware_options[1] = BIT_5 | BIT_4;
2245 nv->frame_payload_size = __constant_cpu_to_le16(1024);
2246 } else if (IS_QLA2100(ha)) {
2247 nv->firmware_options[0] = BIT_3 | BIT_1;
2248 nv->firmware_options[1] = BIT_5;
2249 nv->frame_payload_size = __constant_cpu_to_le16(1024);
2250 }
2251
2252 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
2253 nv->execution_throttle = __constant_cpu_to_le16(16);
2254 nv->retry_count = 8;
2255 nv->retry_delay = 1;
2256
2257 nv->port_name[0] = 33;
2258 nv->port_name[3] = 224;
2259 nv->port_name[4] = 139;
2260
e315cd28 2261 qla2xxx_nvram_wwn_from_ofw(vha, nv);
4e08df3f
DM
2262
2263 nv->login_timeout = 4;
2264
2265 /*
2266 * Set default host adapter parameters
2267 */
2268 nv->host_p[1] = BIT_2;
2269 nv->reset_delay = 5;
2270 nv->port_down_retry_count = 8;
2271 nv->max_luns_per_target = __constant_cpu_to_le16(8);
2272 nv->link_down_timeout = 60;
2273
2274 rval = 1;
1da177e4
LT
2275 }
2276
2277#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
2278 /*
2279 * The SN2 does not provide BIOS emulation which means you can't change
2280 * potentially bogus BIOS settings. Force the use of default settings
2281 * for link rate and frame size. Hope that the rest of the settings
2282 * are valid.
2283 */
2284 if (ia64_platform_is("sn2")) {
2285 nv->frame_payload_size = __constant_cpu_to_le16(2048);
2286 if (IS_QLA23XX(ha))
2287 nv->special_options[1] = BIT_7;
2288 }
2289#endif
2290
2291 /* Reset Initialization control block */
0107109e 2292 memset(icb, 0, ha->init_cb_size);
1da177e4
LT
2293
2294 /*
2295 * Setup driver NVRAM options.
2296 */
2297 nv->firmware_options[0] |= (BIT_6 | BIT_1);
2298 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
2299 nv->firmware_options[1] |= (BIT_5 | BIT_0);
2300 nv->firmware_options[1] &= ~BIT_4;
2301
2302 if (IS_QLA23XX(ha)) {
2303 nv->firmware_options[0] |= BIT_2;
2304 nv->firmware_options[0] &= ~BIT_3;
5ff1d584 2305 nv->firmware_options[0] &= ~BIT_6;
0107109e 2306 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1da177e4
LT
2307
2308 if (IS_QLA2300(ha)) {
2309 if (ha->fb_rev == FPM_2310) {
2310 strcpy(ha->model_number, "QLA2310");
2311 } else {
2312 strcpy(ha->model_number, "QLA2300");
2313 }
2314 } else {
e315cd28 2315 qla2x00_set_model_info(vha, nv->model_number,
9bb9fcf2 2316 sizeof(nv->model_number), "QLA23xx");
1da177e4
LT
2317 }
2318 } else if (IS_QLA2200(ha)) {
2319 nv->firmware_options[0] |= BIT_2;
2320 /*
2321 * 'Point-to-point preferred, else loop' is not a safe
2322 * connection mode setting.
2323 */
2324 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
2325 (BIT_5 | BIT_4)) {
2326 /* Force 'loop preferred, else point-to-point'. */
2327 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
2328 nv->add_firmware_options[0] |= BIT_5;
2329 }
2330 strcpy(ha->model_number, "QLA22xx");
2331 } else /*if (IS_QLA2100(ha))*/ {
2332 strcpy(ha->model_number, "QLA2100");
2333 }
2334
2335 /*
2336 * Copy over NVRAM RISC parameter block to initialization control block.
2337 */
2338 dptr1 = (uint8_t *)icb;
2339 dptr2 = (uint8_t *)&nv->parameter_block_version;
2340 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
2341 while (cnt--)
2342 *dptr1++ = *dptr2++;
2343
2344 /* Copy 2nd half. */
2345 dptr1 = (uint8_t *)icb->add_firmware_options;
2346 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
2347 while (cnt--)
2348 *dptr1++ = *dptr2++;
2349
5341e868
AV
2350 /* Use alternate WWN? */
2351 if (nv->host_p[1] & BIT_7) {
2352 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
2353 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
2354 }
2355
1da177e4
LT
2356 /* Prepare nodename */
2357 if ((icb->firmware_options[1] & BIT_6) == 0) {
2358 /*
2359 * Firmware will apply the following mask if the nodename was
2360 * not provided.
2361 */
2362 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
2363 icb->node_name[0] &= 0xF0;
2364 }
2365
2366 /*
2367 * Set host adapter parameters.
2368 */
0181944f 2369 if (nv->host_p[0] & BIT_7)
11010fec 2370 ql2xextended_error_logging = 1;
1da177e4
LT
2371 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
2372 /* Always load RISC code on non ISP2[12]00 chips. */
2373 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2374 ha->flags.disable_risc_code_load = 0;
2375 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
2376 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
2377 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
06c22bd1 2378 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
d4c760c2 2379 ha->flags.disable_serdes = 0;
1da177e4
LT
2380
2381 ha->operating_mode =
2382 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
2383
2384 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
2385 sizeof(ha->fw_seriallink_options));
2386
2387 /* save HBA serial number */
2388 ha->serial0 = icb->port_name[5];
2389 ha->serial1 = icb->port_name[6];
2390 ha->serial2 = icb->port_name[7];
e315cd28
AC
2391 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
2392 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
1da177e4
LT
2393
2394 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
2395
2396 ha->retry_count = nv->retry_count;
2397
2398 /* Set minimum login_timeout to 4 seconds. */
2399 if (nv->login_timeout < ql2xlogintimeout)
2400 nv->login_timeout = ql2xlogintimeout;
2401 if (nv->login_timeout < 4)
2402 nv->login_timeout = 4;
2403 ha->login_timeout = nv->login_timeout;
2404 icb->login_timeout = nv->login_timeout;
2405
00a537b8
AV
2406 /* Set minimum RATOV to 100 tenths of a second. */
2407 ha->r_a_tov = 100;
1da177e4 2408
1da177e4
LT
2409 ha->loop_reset_delay = nv->reset_delay;
2410
1da177e4
LT
2411 /* Link Down Timeout = 0:
2412 *
2413 * When Port Down timer expires we will start returning
2414 * I/O's to OS with "DID_NO_CONNECT".
2415 *
2416 * Link Down Timeout != 0:
2417 *
2418 * The driver waits for the link to come up after link down
2419 * before returning I/Os to OS with "DID_NO_CONNECT".
fa2a1ce5 2420 */
1da177e4
LT
2421 if (nv->link_down_timeout == 0) {
2422 ha->loop_down_abort_time =
354d6b21 2423 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1da177e4
LT
2424 } else {
2425 ha->link_down_timeout = nv->link_down_timeout;
2426 ha->loop_down_abort_time =
2427 (LOOP_DOWN_TIME - ha->link_down_timeout);
fa2a1ce5 2428 }
1da177e4 2429
1da177e4
LT
2430 /*
2431 * Need enough time to try and get the port back.
2432 */
2433 ha->port_down_retry_count = nv->port_down_retry_count;
2434 if (qlport_down_retry)
2435 ha->port_down_retry_count = qlport_down_retry;
2436 /* Set login_retry_count */
2437 ha->login_retry_count = nv->retry_count;
2438 if (ha->port_down_retry_count == nv->port_down_retry_count &&
2439 ha->port_down_retry_count > 3)
2440 ha->login_retry_count = ha->port_down_retry_count;
2441 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
2442 ha->login_retry_count = ha->port_down_retry_count;
2443 if (ql2xloginretrycount)
2444 ha->login_retry_count = ql2xloginretrycount;
2445
1da177e4
LT
2446 icb->lun_enables = __constant_cpu_to_le16(0);
2447 icb->command_resource_count = 0;
2448 icb->immediate_notify_resource_count = 0;
2449 icb->timeout = __constant_cpu_to_le16(0);
2450
2451 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2452 /* Enable RIO */
2453 icb->firmware_options[0] &= ~BIT_3;
2454 icb->add_firmware_options[0] &=
2455 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
2456 icb->add_firmware_options[0] |= BIT_2;
2457 icb->response_accumulation_timer = 3;
2458 icb->interrupt_delay_timer = 5;
2459
e315cd28 2460 vha->flags.process_response_queue = 1;
1da177e4 2461 } else {
4fdfefe5 2462 /* Enable ZIO. */
e315cd28 2463 if (!vha->flags.init_done) {
4fdfefe5
AV
2464 ha->zio_mode = icb->add_firmware_options[0] &
2465 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
2466 ha->zio_timer = icb->interrupt_delay_timer ?
2467 icb->interrupt_delay_timer: 2;
2468 }
1da177e4
LT
2469 icb->add_firmware_options[0] &=
2470 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
e315cd28 2471 vha->flags.process_response_queue = 0;
4fdfefe5 2472 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4a59f71d
AV
2473 ha->zio_mode = QLA_ZIO_MODE_6;
2474
4fdfefe5 2475 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
e315cd28 2476 "delay (%d us).\n", vha->host_no, ha->zio_mode,
4fdfefe5 2477 ha->zio_timer * 100));
1da177e4 2478 qla_printk(KERN_INFO, ha,
4fdfefe5
AV
2479 "ZIO mode %d enabled; timer delay (%d us).\n",
2480 ha->zio_mode, ha->zio_timer * 100);
1da177e4 2481
4fdfefe5
AV
2482 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
2483 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
e315cd28 2484 vha->flags.process_response_queue = 1;
1da177e4
LT
2485 }
2486 }
2487
4e08df3f
DM
2488 if (rval) {
2489 DEBUG2_3(printk(KERN_WARNING
e315cd28 2490 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
4e08df3f
DM
2491 }
2492 return (rval);
1da177e4
LT
2493}
2494
19a7b4ae
JSEC
2495static void
2496qla2x00_rport_del(void *data)
2497{
2498 fc_port_t *fcport = data;
d97994dc 2499 struct fc_rport *rport;
d97994dc 2500
e315cd28 2501 spin_lock_irq(fcport->vha->host->host_lock);
ac280b67 2502 rport = fcport->drport ? fcport->drport: fcport->rport;
d97994dc 2503 fcport->drport = NULL;
e315cd28 2504 spin_unlock_irq(fcport->vha->host->host_lock);
d97994dc
AV
2505 if (rport)
2506 fc_remote_port_delete(rport);
19a7b4ae
JSEC
2507}
2508
1da177e4
LT
2509/**
2510 * qla2x00_alloc_fcport() - Allocate a generic fcport.
2511 * @ha: HA context
2512 * @flags: allocation flags
2513 *
2514 * Returns a pointer to the allocated fcport, or NULL, if none available.
2515 */
9a069e19 2516fc_port_t *
e315cd28 2517qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
1da177e4
LT
2518{
2519 fc_port_t *fcport;
2520
bbfbbbc1
MK
2521 fcport = kzalloc(sizeof(fc_port_t), flags);
2522 if (!fcport)
2523 return NULL;
1da177e4
LT
2524
2525 /* Setup fcport template structure. */
e315cd28
AC
2526 fcport->vha = vha;
2527 fcport->vp_idx = vha->vp_idx;
1da177e4
LT
2528 fcport->port_type = FCT_UNKNOWN;
2529 fcport->loop_id = FC_NO_LOOP_ID;
1da177e4 2530 atomic_set(&fcport->state, FCS_UNCONFIGURED);
ad3e0eda 2531 fcport->supported_classes = FC_COS_UNSPECIFIED;
1da177e4 2532
bbfbbbc1 2533 return fcport;
1da177e4
LT
2534}
2535
2536/*
2537 * qla2x00_configure_loop
2538 * Updates Fibre Channel Device Database with what is actually on loop.
2539 *
2540 * Input:
2541 * ha = adapter block pointer.
2542 *
2543 * Returns:
2544 * 0 = success.
2545 * 1 = error.
2546 * 2 = database was full and device was not configured.
2547 */
2548static int
e315cd28 2549qla2x00_configure_loop(scsi_qla_host_t *vha)
1da177e4
LT
2550{
2551 int rval;
2552 unsigned long flags, save_flags;
e315cd28 2553 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
2554 rval = QLA_SUCCESS;
2555
2556 /* Get Initiator ID */
e315cd28
AC
2557 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
2558 rval = qla2x00_configure_hba(vha);
1da177e4
LT
2559 if (rval != QLA_SUCCESS) {
2560 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
e315cd28 2561 vha->host_no));
1da177e4
LT
2562 return (rval);
2563 }
2564 }
2565
e315cd28 2566 save_flags = flags = vha->dpc_flags;
1da177e4 2567 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
e315cd28 2568 vha->host_no, flags));
1da177e4
LT
2569
2570 /*
2571 * If we have both an RSCN and PORT UPDATE pending then handle them
2572 * both at the same time.
2573 */
e315cd28
AC
2574 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2575 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
1da177e4 2576
3064ff39
MH
2577 qla2x00_get_data_rate(vha);
2578
1da177e4
LT
2579 /* Determine what we need to do */
2580 if (ha->current_topology == ISP_CFG_FL &&
2581 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2582
e315cd28 2583 vha->flags.rscn_queue_overflow = 1;
1da177e4
LT
2584 set_bit(RSCN_UPDATE, &flags);
2585
2586 } else if (ha->current_topology == ISP_CFG_F &&
2587 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2588
e315cd28 2589 vha->flags.rscn_queue_overflow = 1;
1da177e4
LT
2590 set_bit(RSCN_UPDATE, &flags);
2591 clear_bit(LOCAL_LOOP_UPDATE, &flags);
21333b48
AV
2592
2593 } else if (ha->current_topology == ISP_CFG_N) {
2594 clear_bit(RSCN_UPDATE, &flags);
1da177e4 2595
e315cd28 2596 } else if (!vha->flags.online ||
1da177e4
LT
2597 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
2598
e315cd28 2599 vha->flags.rscn_queue_overflow = 1;
1da177e4
LT
2600 set_bit(RSCN_UPDATE, &flags);
2601 set_bit(LOCAL_LOOP_UPDATE, &flags);
2602 }
2603
2604 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
e315cd28 2605 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4 2606 rval = QLA_FUNCTION_FAILED;
e315cd28
AC
2607 else
2608 rval = qla2x00_configure_local_loop(vha);
1da177e4
LT
2609 }
2610
2611 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
e315cd28 2612 if (LOOP_TRANSITION(vha))
1da177e4 2613 rval = QLA_FUNCTION_FAILED;
e315cd28
AC
2614 else
2615 rval = qla2x00_configure_fabric(vha);
1da177e4
LT
2616 }
2617
2618 if (rval == QLA_SUCCESS) {
e315cd28
AC
2619 if (atomic_read(&vha->loop_down_timer) ||
2620 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
1da177e4
LT
2621 rval = QLA_FUNCTION_FAILED;
2622 } else {
e315cd28 2623 atomic_set(&vha->loop_state, LOOP_READY);
1da177e4 2624
e315cd28 2625 DEBUG(printk("scsi(%ld): LOOP READY\n", vha->host_no));
1da177e4
LT
2626 }
2627 }
2628
2629 if (rval) {
2630 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
e315cd28 2631 __func__, vha->host_no));
1da177e4
LT
2632 } else {
2633 DEBUG3(printk("%s: exiting normally\n", __func__));
2634 }
2635
cc3ef7bc 2636 /* Restore state if a resync event occurred during processing */
e315cd28 2637 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
1da177e4 2638 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
e315cd28 2639 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
f4658b6c 2640 if (test_bit(RSCN_UPDATE, &save_flags)) {
e315cd28 2641 set_bit(RSCN_UPDATE, &vha->dpc_flags);
f4658b6c
AV
2642 vha->flags.rscn_queue_overflow = 1;
2643 }
1da177e4
LT
2644 }
2645
2646 return (rval);
2647}
2648
2649
2650
2651/*
2652 * qla2x00_configure_local_loop
2653 * Updates Fibre Channel Device Database with local loop devices.
2654 *
2655 * Input:
2656 * ha = adapter block pointer.
2657 *
2658 * Returns:
2659 * 0 = success.
2660 */
2661static int
e315cd28 2662qla2x00_configure_local_loop(scsi_qla_host_t *vha)
1da177e4
LT
2663{
2664 int rval, rval2;
2665 int found_devs;
2666 int found;
2667 fc_port_t *fcport, *new_fcport;
2668
2669 uint16_t index;
2670 uint16_t entries;
2671 char *id_iter;
2672 uint16_t loop_id;
2673 uint8_t domain, area, al_pa;
e315cd28 2674 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
2675
2676 found_devs = 0;
2677 new_fcport = NULL;
2678 entries = MAX_FIBRE_DEVICES;
2679
e315cd28
AC
2680 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", vha->host_no));
2681 DEBUG3(qla2x00_get_fcal_position_map(vha, NULL));
1da177e4
LT
2682
2683 /* Get list of logged in devices. */
2684 memset(ha->gid_list, 0, GID_LIST_SIZE);
e315cd28 2685 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
1da177e4
LT
2686 &entries);
2687 if (rval != QLA_SUCCESS)
2688 goto cleanup_allocation;
2689
2690 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
7640335e 2691 vha->host_no, entries));
1da177e4
LT
2692 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2693 entries * sizeof(struct gid_list_info)));
2694
2695 /* Allocate temporary fcport for any new fcports discovered. */
e315cd28 2696 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4
LT
2697 if (new_fcport == NULL) {
2698 rval = QLA_MEMORY_ALLOC_FAILED;
2699 goto cleanup_allocation;
2700 }
2701 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2702
2703 /*
2704 * Mark local devices that were present with FCF_DEVICE_LOST for now.
2705 */
e315cd28 2706 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
2707 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2708 fcport->port_type != FCT_BROADCAST &&
2709 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2710
2711 DEBUG(printk("scsi(%ld): Marking port lost, "
2712 "loop_id=0x%04x\n",
e315cd28 2713 vha->host_no, fcport->loop_id));
1da177e4
LT
2714
2715 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1da177e4
LT
2716 }
2717 }
2718
2719 /* Add devices to port list. */
2720 id_iter = (char *)ha->gid_list;
2721 for (index = 0; index < entries; index++) {
2722 domain = ((struct gid_list_info *)id_iter)->domain;
2723 area = ((struct gid_list_info *)id_iter)->area;
2724 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
abbd8870 2725 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1da177e4
LT
2726 loop_id = (uint16_t)
2727 ((struct gid_list_info *)id_iter)->loop_id_2100;
abbd8870 2728 else
1da177e4
LT
2729 loop_id = le16_to_cpu(
2730 ((struct gid_list_info *)id_iter)->loop_id);
abbd8870 2731 id_iter += ha->gid_list_info_size;
1da177e4
LT
2732
2733 /* Bypass reserved domain fields. */
2734 if ((domain & 0xf0) == 0xf0)
2735 continue;
2736
2737 /* Bypass if not same domain and area of adapter. */
f7d289f6 2738 if (area && domain &&
e315cd28 2739 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
1da177e4
LT
2740 continue;
2741
2742 /* Bypass invalid local loop ID. */
2743 if (loop_id > LAST_LOCAL_LOOP_ID)
2744 continue;
2745
2746 /* Fill in member data. */
2747 new_fcport->d_id.b.domain = domain;
2748 new_fcport->d_id.b.area = area;
2749 new_fcport->d_id.b.al_pa = al_pa;
2750 new_fcport->loop_id = loop_id;
e315cd28
AC
2751 new_fcport->vp_idx = vha->vp_idx;
2752 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
1da177e4
LT
2753 if (rval2 != QLA_SUCCESS) {
2754 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2755 "information -- get_port_database=%x, "
2756 "loop_id=0x%04x\n",
e315cd28 2757 vha->host_no, rval2, new_fcport->loop_id));
c9d02acf 2758 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
e315cd28
AC
2759 vha->host_no));
2760 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1da177e4
LT
2761 continue;
2762 }
2763
2764 /* Check for matching device in port list. */
2765 found = 0;
2766 fcport = NULL;
e315cd28 2767 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
2768 if (memcmp(new_fcport->port_name, fcport->port_name,
2769 WWN_SIZE))
2770 continue;
2771
ddb9b126 2772 fcport->flags &= ~FCF_FABRIC_DEVICE;
1da177e4
LT
2773 fcport->loop_id = new_fcport->loop_id;
2774 fcport->port_type = new_fcport->port_type;
2775 fcport->d_id.b24 = new_fcport->d_id.b24;
2776 memcpy(fcport->node_name, new_fcport->node_name,
2777 WWN_SIZE);
2778
2779 found++;
2780 break;
2781 }
2782
2783 if (!found) {
2784 /* New device, add to fcports list. */
e315cd28
AC
2785 if (vha->vp_idx) {
2786 new_fcport->vha = vha;
2787 new_fcport->vp_idx = vha->vp_idx;
2c3dfe3f 2788 }
e315cd28 2789 list_add_tail(&new_fcport->list, &vha->vp_fcports);
1da177e4
LT
2790
2791 /* Allocate a new replacement fcport. */
2792 fcport = new_fcport;
e315cd28 2793 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4
LT
2794 if (new_fcport == NULL) {
2795 rval = QLA_MEMORY_ALLOC_FAILED;
2796 goto cleanup_allocation;
2797 }
2798 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2799 }
2800
d8b45213 2801 /* Base iIDMA settings on HBA port speed. */
a3cbdfad 2802 fcport->fp_speed = ha->link_data_rate;
d8b45213 2803
e315cd28 2804 qla2x00_update_fcport(vha, fcport);
1da177e4
LT
2805
2806 found_devs++;
2807 }
2808
2809cleanup_allocation:
c9475cb0 2810 kfree(new_fcport);
1da177e4
LT
2811
2812 if (rval != QLA_SUCCESS) {
2813 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
e315cd28 2814 "rval=%x\n", vha->host_no, rval));
1da177e4
LT
2815 }
2816
1da177e4
LT
2817 return (rval);
2818}
2819
d8b45213 2820static void
e315cd28 2821qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
d8b45213
AV
2822{
2823#define LS_UNKNOWN 2
9f8fddee
AV
2824 static char *link_speeds[] = { "1", "2", "?", "4", "8", "10" };
2825 char *link_speed;
d8b45213 2826 int rval;
1bb39548 2827 uint16_t mb[4];
e315cd28 2828 struct qla_hw_data *ha = vha->hw;
d8b45213 2829
c76f2c01 2830 if (!IS_IIDMA_CAPABLE(ha))
d8b45213
AV
2831 return;
2832
39bd9622
AV
2833 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2834 fcport->fp_speed > ha->link_data_rate)
d8b45213
AV
2835 return;
2836
e315cd28 2837 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
a3cbdfad 2838 mb);
d8b45213
AV
2839 if (rval != QLA_SUCCESS) {
2840 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2841 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
e315cd28 2842 vha->host_no, fcport->port_name[0], fcport->port_name[1],
d8b45213
AV
2843 fcport->port_name[2], fcport->port_name[3],
2844 fcport->port_name[4], fcport->port_name[5],
2845 fcport->port_name[6], fcport->port_name[7], rval,
a3cbdfad 2846 fcport->fp_speed, mb[0], mb[1]));
d8b45213 2847 } else {
9f8fddee
AV
2848 link_speed = link_speeds[LS_UNKNOWN];
2849 if (fcport->fp_speed < 5)
2850 link_speed = link_speeds[fcport->fp_speed];
2851 else if (fcport->fp_speed == 0x13)
2852 link_speed = link_speeds[5];
d8b45213
AV
2853 DEBUG2(qla_printk(KERN_INFO, ha,
2854 "iIDMA adjusted to %s GB/s on "
2855 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
9f8fddee 2856 link_speed, fcport->port_name[0],
d8b45213
AV
2857 fcport->port_name[1], fcport->port_name[2],
2858 fcport->port_name[3], fcport->port_name[4],
2859 fcport->port_name[5], fcport->port_name[6],
2860 fcport->port_name[7]));
2861 }
2862}
2863
23be331d 2864static void
e315cd28 2865qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e118
AV
2866{
2867 struct fc_rport_identifiers rport_ids;
bdf79621 2868 struct fc_rport *rport;
e315cd28 2869 struct qla_hw_data *ha = vha->hw;
8482e118 2870
ac280b67 2871 qla2x00_rport_del(fcport);
8482e118 2872
f8b02a85
AV
2873 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2874 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e118
AV
2875 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2876 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
77d74143 2877 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
e315cd28 2878 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
77d74143
AV
2879 if (!rport) {
2880 qla_printk(KERN_WARNING, ha,
2881 "Unable to allocate fc remote port!\n");
2882 return;
2883 }
e315cd28 2884 spin_lock_irq(fcport->vha->host->host_lock);
19a7b4ae 2885 *((fc_port_t **)rport->dd_data) = fcport;
e315cd28 2886 spin_unlock_irq(fcport->vha->host->host_lock);
d97994dc 2887
ad3e0eda 2888 rport->supported_classes = fcport->supported_classes;
77d74143 2889
8482e118
AV
2890 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2891 if (fcport->port_type == FCT_INITIATOR)
2892 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2893 if (fcport->port_type == FCT_TARGET)
2894 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
77d74143 2895 fc_remote_port_rolechg(rport, rport_ids.roles);
1da177e4
LT
2896}
2897
23be331d
AB
2898/*
2899 * qla2x00_update_fcport
2900 * Updates device on list.
2901 *
2902 * Input:
2903 * ha = adapter block pointer.
2904 * fcport = port structure pointer.
2905 *
2906 * Return:
2907 * 0 - Success
2908 * BIT_0 - error
2909 *
2910 * Context:
2911 * Kernel context.
2912 */
2913void
e315cd28 2914qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
23be331d 2915{
e315cd28 2916 struct qla_hw_data *ha = vha->hw;
2c3dfe3f 2917
e315cd28 2918 fcport->vha = vha;
23be331d 2919 fcport->login_retry = 0;
e315cd28 2920 fcport->port_login_retry_count = ha->port_down_retry_count *
23be331d 2921 PORT_RETRY_TIME;
e315cd28 2922 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
23be331d 2923 PORT_RETRY_TIME);
5ff1d584 2924 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
23be331d 2925
e315cd28 2926 qla2x00_iidma_fcport(vha, fcport);
23be331d
AB
2927
2928 atomic_set(&fcport->state, FCS_ONLINE);
2929
e315cd28 2930 qla2x00_reg_remote_port(vha, fcport);
23be331d
AB
2931}
2932
1da177e4
LT
2933/*
2934 * qla2x00_configure_fabric
2935 * Setup SNS devices with loop ID's.
2936 *
2937 * Input:
2938 * ha = adapter block pointer.
2939 *
2940 * Returns:
2941 * 0 = success.
2942 * BIT_0 = error
2943 */
2944static int
e315cd28 2945qla2x00_configure_fabric(scsi_qla_host_t *vha)
1da177e4
LT
2946{
2947 int rval, rval2;
2948 fc_port_t *fcport, *fcptemp;
2949 uint16_t next_loopid;
2950 uint16_t mb[MAILBOX_REGISTER_COUNT];
0107109e 2951 uint16_t loop_id;
1da177e4 2952 LIST_HEAD(new_fcports);
e315cd28
AC
2953 struct qla_hw_data *ha = vha->hw;
2954 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
1da177e4
LT
2955
2956 /* If FL port exists, then SNS is present */
e428924c 2957 if (IS_FWI2_CAPABLE(ha))
0107109e
AV
2958 loop_id = NPH_F_PORT;
2959 else
2960 loop_id = SNS_FL_PORT;
e315cd28 2961 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
1da177e4
LT
2962 if (rval != QLA_SUCCESS) {
2963 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
e315cd28 2964 "Port\n", vha->host_no));
1da177e4 2965
e315cd28 2966 vha->device_flags &= ~SWITCH_FOUND;
1da177e4
LT
2967 return (QLA_SUCCESS);
2968 }
e315cd28 2969 vha->device_flags |= SWITCH_FOUND;
1da177e4
LT
2970
2971 /* Mark devices that need re-synchronization. */
e315cd28 2972 rval2 = qla2x00_device_resync(vha);
1da177e4
LT
2973 if (rval2 == QLA_RSCNS_HANDLED) {
2974 /* No point doing the scan, just continue. */
2975 return (QLA_SUCCESS);
2976 }
2977 do {
cca5335c
AV
2978 /* FDMI support. */
2979 if (ql2xfdmienable &&
e315cd28
AC
2980 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
2981 qla2x00_fdmi_register(vha);
cca5335c 2982
1da177e4 2983 /* Ensure we are logged into the SNS. */
e428924c 2984 if (IS_FWI2_CAPABLE(ha))
0107109e
AV
2985 loop_id = NPH_SNS;
2986 else
2987 loop_id = SIMPLE_NAME_SERVER;
e315cd28 2988 ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
abbd8870 2989 0xfc, mb, BIT_1 | BIT_0);
1da177e4
LT
2990 if (mb[0] != MBS_COMMAND_COMPLETE) {
2991 DEBUG2(qla_printk(KERN_INFO, ha,
2992 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
0107109e 2993 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
1da177e4
LT
2994 mb[0], mb[1], mb[2], mb[6], mb[7]));
2995 return (QLA_SUCCESS);
2996 }
2997
e315cd28
AC
2998 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
2999 if (qla2x00_rft_id(vha)) {
1da177e4
LT
3000 /* EMPTY */
3001 DEBUG2(printk("scsi(%ld): Register FC-4 "
e315cd28 3002 "TYPE failed.\n", vha->host_no));
1da177e4 3003 }
e315cd28 3004 if (qla2x00_rff_id(vha)) {
1da177e4
LT
3005 /* EMPTY */
3006 DEBUG2(printk("scsi(%ld): Register FC-4 "
e315cd28 3007 "Features failed.\n", vha->host_no));
1da177e4 3008 }
e315cd28 3009 if (qla2x00_rnn_id(vha)) {
1da177e4
LT
3010 /* EMPTY */
3011 DEBUG2(printk("scsi(%ld): Register Node Name "
e315cd28
AC
3012 "failed.\n", vha->host_no));
3013 } else if (qla2x00_rsnn_nn(vha)) {
1da177e4
LT
3014 /* EMPTY */
3015 DEBUG2(printk("scsi(%ld): Register Symbolic "
e315cd28 3016 "Node Name failed.\n", vha->host_no));
1da177e4
LT
3017 }
3018 }
3019
e315cd28 3020 rval = qla2x00_find_all_fabric_devs(vha, &new_fcports);
1da177e4
LT
3021 if (rval != QLA_SUCCESS)
3022 break;
3023
3024 /*
3025 * Logout all previous fabric devices marked lost, except
f08b7251 3026 * FCP2 devices.
1da177e4 3027 */
e315cd28
AC
3028 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3029 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4
LT
3030 break;
3031
3032 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
3033 continue;
3034
3035 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
e315cd28 3036 qla2x00_mark_device_lost(vha, fcport,
d97994dc 3037 ql2xplogiabsentdevice, 0);
1da177e4 3038 if (fcport->loop_id != FC_NO_LOOP_ID &&
f08b7251 3039 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
1da177e4
LT
3040 fcport->port_type != FCT_INITIATOR &&
3041 fcport->port_type != FCT_BROADCAST) {
e315cd28 3042 ha->isp_ops->fabric_logout(vha,
1c7c6357
AV
3043 fcport->loop_id,
3044 fcport->d_id.b.domain,
3045 fcport->d_id.b.area,
3046 fcport->d_id.b.al_pa);
1da177e4
LT
3047 fcport->loop_id = FC_NO_LOOP_ID;
3048 }
3049 }
3050 }
3051
3052 /* Starting free loop ID. */
e315cd28 3053 next_loopid = ha->min_external_loopid;
1da177e4
LT
3054
3055 /*
3056 * Scan through our port list and login entries that need to be
3057 * logged in.
3058 */
e315cd28
AC
3059 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3060 if (atomic_read(&vha->loop_down_timer) ||
3061 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4
LT
3062 break;
3063
3064 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
3065 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
3066 continue;
3067
3068 if (fcport->loop_id == FC_NO_LOOP_ID) {
3069 fcport->loop_id = next_loopid;
d4486fd6 3070 rval = qla2x00_find_new_loop_id(
e315cd28 3071 base_vha, fcport);
1da177e4
LT
3072 if (rval != QLA_SUCCESS) {
3073 /* Ran out of IDs to use */
3074 break;
3075 }
3076 }
1da177e4 3077 /* Login and update database */
e315cd28 3078 qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
1da177e4
LT
3079 }
3080
3081 /* Exit if out of loop IDs. */
3082 if (rval != QLA_SUCCESS) {
3083 break;
3084 }
3085
3086 /*
3087 * Login and add the new devices to our port list.
3088 */
3089 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
e315cd28
AC
3090 if (atomic_read(&vha->loop_down_timer) ||
3091 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4
LT
3092 break;
3093
3094 /* Find a new loop ID to use. */
3095 fcport->loop_id = next_loopid;
e315cd28 3096 rval = qla2x00_find_new_loop_id(base_vha, fcport);
1da177e4
LT
3097 if (rval != QLA_SUCCESS) {
3098 /* Ran out of IDs to use */
3099 break;
3100 }
3101
bdf79621 3102 /* Login and update database */
e315cd28
AC
3103 qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
3104
3105 if (vha->vp_idx) {
3106 fcport->vha = vha;
3107 fcport->vp_idx = vha->vp_idx;
3108 }
3109 list_move_tail(&fcport->list, &vha->vp_fcports);
1da177e4
LT
3110 }
3111 } while (0);
3112
3113 /* Free all new device structures not processed. */
3114 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
3115 list_del(&fcport->list);
3116 kfree(fcport);
3117 }
3118
3119 if (rval) {
3120 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
e315cd28 3121 "rval=%d\n", vha->host_no, rval));
1da177e4
LT
3122 }
3123
3124 return (rval);
3125}
3126
3127
3128/*
3129 * qla2x00_find_all_fabric_devs
3130 *
3131 * Input:
3132 * ha = adapter block pointer.
3133 * dev = database device entry pointer.
3134 *
3135 * Returns:
3136 * 0 = success.
3137 *
3138 * Context:
3139 * Kernel context.
3140 */
3141static int
e315cd28
AC
3142qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha,
3143 struct list_head *new_fcports)
1da177e4
LT
3144{
3145 int rval;
3146 uint16_t loop_id;
3147 fc_port_t *fcport, *new_fcport, *fcptemp;
3148 int found;
3149
3150 sw_info_t *swl;
3151 int swl_idx;
3152 int first_dev, last_dev;
3153 port_id_t wrap, nxt_d_id;
e315cd28
AC
3154 struct qla_hw_data *ha = vha->hw;
3155 struct scsi_qla_host *vp, *base_vha = pci_get_drvdata(ha->pdev);
ee546b6e 3156 struct scsi_qla_host *tvp;
1da177e4
LT
3157
3158 rval = QLA_SUCCESS;
3159
3160 /* Try GID_PT to get device list, else GAN. */
4b89258c 3161 swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_KERNEL);
bbfbbbc1 3162 if (!swl) {
1da177e4
LT
3163 /*EMPTY*/
3164 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
e315cd28 3165 "on GA_NXT\n", vha->host_no));
1da177e4 3166 } else {
e315cd28 3167 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
1da177e4
LT
3168 kfree(swl);
3169 swl = NULL;
e315cd28 3170 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
1da177e4
LT
3171 kfree(swl);
3172 swl = NULL;
e315cd28 3173 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
1da177e4
LT
3174 kfree(swl);
3175 swl = NULL;
e5896bd5 3176 } else if (ql2xiidmaenable &&
e315cd28
AC
3177 qla2x00_gfpn_id(vha, swl) == QLA_SUCCESS) {
3178 qla2x00_gpsc(vha, swl);
1da177e4
LT
3179 }
3180 }
3181 swl_idx = 0;
3182
3183 /* Allocate temporary fcport for any new fcports discovered. */
e315cd28 3184 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4 3185 if (new_fcport == NULL) {
c9475cb0 3186 kfree(swl);
1da177e4
LT
3187 return (QLA_MEMORY_ALLOC_FAILED);
3188 }
3189 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
1da177e4
LT
3190 /* Set start port ID scan at adapter ID. */
3191 first_dev = 1;
3192 last_dev = 0;
3193
3194 /* Starting free loop ID. */
e315cd28
AC
3195 loop_id = ha->min_external_loopid;
3196 for (; loop_id <= ha->max_loop_id; loop_id++) {
3197 if (qla2x00_is_reserved_id(vha, loop_id))
1da177e4
LT
3198 continue;
3199
bb2d52b2
AV
3200 if (atomic_read(&vha->loop_down_timer) ||
3201 LOOP_TRANSITION(vha)) {
3202 atomic_set(&vha->loop_down_timer, 0);
3203 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3204 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1da177e4 3205 break;
bb2d52b2 3206 }
1da177e4
LT
3207
3208 if (swl != NULL) {
3209 if (last_dev) {
3210 wrap.b24 = new_fcport->d_id.b24;
3211 } else {
3212 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
3213 memcpy(new_fcport->node_name,
3214 swl[swl_idx].node_name, WWN_SIZE);
3215 memcpy(new_fcport->port_name,
3216 swl[swl_idx].port_name, WWN_SIZE);
d8b45213
AV
3217 memcpy(new_fcport->fabric_port_name,
3218 swl[swl_idx].fabric_port_name, WWN_SIZE);
3219 new_fcport->fp_speed = swl[swl_idx].fp_speed;
1da177e4
LT
3220
3221 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
3222 last_dev = 1;
3223 }
3224 swl_idx++;
3225 }
3226 } else {
3227 /* Send GA_NXT to the switch */
e315cd28 3228 rval = qla2x00_ga_nxt(vha, new_fcport);
1da177e4
LT
3229 if (rval != QLA_SUCCESS) {
3230 qla_printk(KERN_WARNING, ha,
3231 "SNS scan failed -- assuming zero-entry "
3232 "result...\n");
3233 list_for_each_entry_safe(fcport, fcptemp,
3234 new_fcports, list) {
3235 list_del(&fcport->list);
3236 kfree(fcport);
3237 }
3238 rval = QLA_SUCCESS;
3239 break;
3240 }
3241 }
3242
3243 /* If wrap on switch device list, exit. */
3244 if (first_dev) {
3245 wrap.b24 = new_fcport->d_id.b24;
3246 first_dev = 0;
3247 } else if (new_fcport->d_id.b24 == wrap.b24) {
3248 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
e315cd28 3249 vha->host_no, new_fcport->d_id.b.domain,
1da177e4
LT
3250 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
3251 break;
3252 }
3253
2c3dfe3f 3254 /* Bypass if same physical adapter. */
e315cd28 3255 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
1da177e4
LT
3256 continue;
3257
2c3dfe3f 3258 /* Bypass virtual ports of the same host. */
e315cd28
AC
3259 found = 0;
3260 if (ha->num_vhosts) {
ee546b6e 3261 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
e315cd28
AC
3262 if (new_fcport->d_id.b24 == vp->d_id.b24) {
3263 found = 1;
2c3dfe3f 3264 break;
e315cd28 3265 }
2c3dfe3f 3266 }
e315cd28 3267 if (found)
2c3dfe3f
SJ
3268 continue;
3269 }
3270
f7d289f6
AV
3271 /* Bypass if same domain and area of adapter. */
3272 if (((new_fcport->d_id.b24 & 0xffff00) ==
e315cd28 3273 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
f7d289f6
AV
3274 ISP_CFG_FL)
3275 continue;
3276
1da177e4
LT
3277 /* Bypass reserved domain fields. */
3278 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
3279 continue;
3280
3281 /* Locate matching device in database. */
3282 found = 0;
e315cd28 3283 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
3284 if (memcmp(new_fcport->port_name, fcport->port_name,
3285 WWN_SIZE))
3286 continue;
3287
3288 found++;
3289
d8b45213
AV
3290 /* Update port state. */
3291 memcpy(fcport->fabric_port_name,
3292 new_fcport->fabric_port_name, WWN_SIZE);
3293 fcport->fp_speed = new_fcport->fp_speed;
3294
1da177e4
LT
3295 /*
3296 * If address the same and state FCS_ONLINE, nothing
3297 * changed.
3298 */
3299 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
3300 atomic_read(&fcport->state) == FCS_ONLINE) {
3301 break;
3302 }
3303
3304 /*
3305 * If device was not a fabric device before.
3306 */
3307 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
3308 fcport->d_id.b24 = new_fcport->d_id.b24;
3309 fcport->loop_id = FC_NO_LOOP_ID;
3310 fcport->flags |= (FCF_FABRIC_DEVICE |
3311 FCF_LOGIN_NEEDED);
1da177e4
LT
3312 break;
3313 }
3314
3315 /*
3316 * Port ID changed or device was marked to be updated;
3317 * Log it out if still logged in and mark it for
3318 * relogin later.
3319 */
3320 fcport->d_id.b24 = new_fcport->d_id.b24;
3321 fcport->flags |= FCF_LOGIN_NEEDED;
3322 if (fcport->loop_id != FC_NO_LOOP_ID &&
f08b7251 3323 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
1da177e4
LT
3324 fcport->port_type != FCT_INITIATOR &&
3325 fcport->port_type != FCT_BROADCAST) {
e315cd28 3326 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
3327 fcport->d_id.b.domain, fcport->d_id.b.area,
3328 fcport->d_id.b.al_pa);
1da177e4
LT
3329 fcport->loop_id = FC_NO_LOOP_ID;
3330 }
3331
3332 break;
3333 }
3334
3335 if (found)
3336 continue;
1da177e4
LT
3337 /* If device was not in our fcports list, then add it. */
3338 list_add_tail(&new_fcport->list, new_fcports);
3339
3340 /* Allocate a new replacement fcport. */
3341 nxt_d_id.b24 = new_fcport->d_id.b24;
e315cd28 3342 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4 3343 if (new_fcport == NULL) {
c9475cb0 3344 kfree(swl);
1da177e4
LT
3345 return (QLA_MEMORY_ALLOC_FAILED);
3346 }
3347 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
3348 new_fcport->d_id.b24 = nxt_d_id.b24;
3349 }
3350
c9475cb0
JJ
3351 kfree(swl);
3352 kfree(new_fcport);
1da177e4 3353
1da177e4
LT
3354 return (rval);
3355}
3356
3357/*
3358 * qla2x00_find_new_loop_id
3359 * Scan through our port list and find a new usable loop ID.
3360 *
3361 * Input:
3362 * ha: adapter state pointer.
3363 * dev: port structure pointer.
3364 *
3365 * Returns:
3366 * qla2x00 local function return status code.
3367 *
3368 * Context:
3369 * Kernel context.
3370 */
413975a0 3371static int
e315cd28 3372qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
1da177e4
LT
3373{
3374 int rval;
3375 int found;
3376 fc_port_t *fcport;
3377 uint16_t first_loop_id;
e315cd28
AC
3378 struct qla_hw_data *ha = vha->hw;
3379 struct scsi_qla_host *vp;
ee546b6e 3380 struct scsi_qla_host *tvp;
1da177e4
LT
3381
3382 rval = QLA_SUCCESS;
3383
3384 /* Save starting loop ID. */
3385 first_loop_id = dev->loop_id;
3386
3387 for (;;) {
3388 /* Skip loop ID if already used by adapter. */
e315cd28 3389 if (dev->loop_id == vha->loop_id)
1da177e4 3390 dev->loop_id++;
1da177e4
LT
3391
3392 /* Skip reserved loop IDs. */
e315cd28 3393 while (qla2x00_is_reserved_id(vha, dev->loop_id))
1da177e4 3394 dev->loop_id++;
1da177e4
LT
3395
3396 /* Reset loop ID if passed the end. */
e315cd28 3397 if (dev->loop_id > ha->max_loop_id) {
1da177e4
LT
3398 /* first loop ID. */
3399 dev->loop_id = ha->min_external_loopid;
3400 }
3401
3402 /* Check for loop ID being already in use. */
3403 found = 0;
3404 fcport = NULL;
ee546b6e 3405 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
e315cd28
AC
3406 list_for_each_entry(fcport, &vp->vp_fcports, list) {
3407 if (fcport->loop_id == dev->loop_id &&
3408 fcport != dev) {
3409 /* ID possibly in use */
3410 found++;
3411 break;
3412 }
1da177e4 3413 }
e315cd28
AC
3414 if (found)
3415 break;
1da177e4
LT
3416 }
3417
3418 /* If not in use then it is free to use. */
3419 if (!found) {
3420 break;
3421 }
3422
3423 /* ID in use. Try next value. */
3424 dev->loop_id++;
3425
3426 /* If wrap around. No free ID to use. */
3427 if (dev->loop_id == first_loop_id) {
3428 dev->loop_id = FC_NO_LOOP_ID;
3429 rval = QLA_FUNCTION_FAILED;
3430 break;
3431 }
3432 }
3433
3434 return (rval);
3435}
3436
3437/*
3438 * qla2x00_device_resync
3439 * Marks devices in the database that needs resynchronization.
3440 *
3441 * Input:
3442 * ha = adapter block pointer.
3443 *
3444 * Context:
3445 * Kernel context.
3446 */
3447static int
e315cd28 3448qla2x00_device_resync(scsi_qla_host_t *vha)
1da177e4
LT
3449{
3450 int rval;
1da177e4
LT
3451 uint32_t mask;
3452 fc_port_t *fcport;
3453 uint32_t rscn_entry;
3454 uint8_t rscn_out_iter;
3455 uint8_t format;
3456 port_id_t d_id;
3457
3458 rval = QLA_RSCNS_HANDLED;
3459
e315cd28
AC
3460 while (vha->rscn_out_ptr != vha->rscn_in_ptr ||
3461 vha->flags.rscn_queue_overflow) {
1da177e4 3462
e315cd28 3463 rscn_entry = vha->rscn_queue[vha->rscn_out_ptr];
1da177e4
LT
3464 format = MSB(MSW(rscn_entry));
3465 d_id.b.domain = LSB(MSW(rscn_entry));
3466 d_id.b.area = MSB(LSW(rscn_entry));
3467 d_id.b.al_pa = LSB(LSW(rscn_entry));
3468
3469 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
3470 "[%02x/%02x%02x%02x].\n",
e315cd28 3471 vha->host_no, vha->rscn_out_ptr, format, d_id.b.domain,
1da177e4
LT
3472 d_id.b.area, d_id.b.al_pa));
3473
e315cd28
AC
3474 vha->rscn_out_ptr++;
3475 if (vha->rscn_out_ptr == MAX_RSCN_COUNT)
3476 vha->rscn_out_ptr = 0;
1da177e4
LT
3477
3478 /* Skip duplicate entries. */
e315cd28
AC
3479 for (rscn_out_iter = vha->rscn_out_ptr;
3480 !vha->flags.rscn_queue_overflow &&
3481 rscn_out_iter != vha->rscn_in_ptr;
1da177e4
LT
3482 rscn_out_iter = (rscn_out_iter ==
3483 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
3484
e315cd28 3485 if (rscn_entry != vha->rscn_queue[rscn_out_iter])
1da177e4
LT
3486 break;
3487
3488 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
e315cd28 3489 "entry found at [%d].\n", vha->host_no,
1da177e4
LT
3490 rscn_out_iter));
3491
e315cd28 3492 vha->rscn_out_ptr = rscn_out_iter;
1da177e4
LT
3493 }
3494
3495 /* Queue overflow, set switch default case. */
e315cd28 3496 if (vha->flags.rscn_queue_overflow) {
1da177e4 3497 DEBUG(printk("scsi(%ld): device_resync: rscn "
e315cd28 3498 "overflow.\n", vha->host_no));
1da177e4
LT
3499
3500 format = 3;
e315cd28 3501 vha->flags.rscn_queue_overflow = 0;
1da177e4
LT
3502 }
3503
3504 switch (format) {
3505 case 0:
1da177e4
LT
3506 mask = 0xffffff;
3507 break;
3508 case 1:
3509 mask = 0xffff00;
3510 break;
3511 case 2:
3512 mask = 0xff0000;
3513 break;
3514 default:
3515 mask = 0x0;
3516 d_id.b24 = 0;
e315cd28 3517 vha->rscn_out_ptr = vha->rscn_in_ptr;
1da177e4
LT
3518 break;
3519 }
3520
3521 rval = QLA_SUCCESS;
3522
e315cd28 3523 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
3524 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
3525 (fcport->d_id.b24 & mask) != d_id.b24 ||
3526 fcport->port_type == FCT_BROADCAST)
3527 continue;
3528
3529 if (atomic_read(&fcport->state) == FCS_ONLINE) {
3530 if (format != 3 ||
3531 fcport->port_type != FCT_INITIATOR) {
e315cd28 3532 qla2x00_mark_device_lost(vha, fcport,
d97994dc 3533 0, 0);
1da177e4
LT
3534 }
3535 }
1da177e4
LT
3536 }
3537 }
3538 return (rval);
3539}
3540
3541/*
3542 * qla2x00_fabric_dev_login
3543 * Login fabric target device and update FC port database.
3544 *
3545 * Input:
3546 * ha: adapter state pointer.
3547 * fcport: port structure list pointer.
3548 * next_loopid: contains value of a new loop ID that can be used
3549 * by the next login attempt.
3550 *
3551 * Returns:
3552 * qla2x00 local function return status code.
3553 *
3554 * Context:
3555 * Kernel context.
3556 */
3557static int
e315cd28 3558qla2x00_fabric_dev_login(scsi_qla_host_t *vha, fc_port_t *fcport,
1da177e4
LT
3559 uint16_t *next_loopid)
3560{
3561 int rval;
3562 int retry;
0107109e 3563 uint8_t opts;
e315cd28 3564 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
3565
3566 rval = QLA_SUCCESS;
3567 retry = 0;
3568
ac280b67 3569 if (IS_ALOGIO_CAPABLE(ha)) {
5ff1d584
AV
3570 if (fcport->flags & FCF_ASYNC_SENT)
3571 return rval;
3572 fcport->flags |= FCF_ASYNC_SENT;
ac280b67
AV
3573 rval = qla2x00_post_async_login_work(vha, fcport, NULL);
3574 if (!rval)
3575 return rval;
3576 }
3577
5ff1d584 3578 fcport->flags &= ~FCF_ASYNC_SENT;
e315cd28 3579 rval = qla2x00_fabric_login(vha, fcport, next_loopid);
1da177e4 3580 if (rval == QLA_SUCCESS) {
f08b7251 3581 /* Send an ADISC to FCP2 devices.*/
0107109e 3582 opts = 0;
f08b7251 3583 if (fcport->flags & FCF_FCP2_DEVICE)
0107109e 3584 opts |= BIT_1;
e315cd28 3585 rval = qla2x00_get_port_database(vha, fcport, opts);
1da177e4 3586 if (rval != QLA_SUCCESS) {
e315cd28 3587 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
3588 fcport->d_id.b.domain, fcport->d_id.b.area,
3589 fcport->d_id.b.al_pa);
e315cd28 3590 qla2x00_mark_device_lost(vha, fcport, 1, 0);
1da177e4 3591 } else {
e315cd28 3592 qla2x00_update_fcport(vha, fcport);
1da177e4
LT
3593 }
3594 }
3595
3596 return (rval);
3597}
3598
3599/*
3600 * qla2x00_fabric_login
3601 * Issue fabric login command.
3602 *
3603 * Input:
3604 * ha = adapter block pointer.
3605 * device = pointer to FC device type structure.
3606 *
3607 * Returns:
3608 * 0 - Login successfully
3609 * 1 - Login failed
3610 * 2 - Initiator device
3611 * 3 - Fatal error
3612 */
3613int
e315cd28 3614qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
1da177e4
LT
3615 uint16_t *next_loopid)
3616{
3617 int rval;
3618 int retry;
3619 uint16_t tmp_loopid;
3620 uint16_t mb[MAILBOX_REGISTER_COUNT];
e315cd28 3621 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
3622
3623 retry = 0;
3624 tmp_loopid = 0;
3625
3626 for (;;) {
3627 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
3628 "for port %02x%02x%02x.\n",
e315cd28 3629 vha->host_no, fcport->loop_id, fcport->d_id.b.domain,
1da177e4
LT
3630 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3631
3632 /* Login fcport on switch. */
e315cd28 3633 ha->isp_ops->fabric_login(vha, fcport->loop_id,
1da177e4
LT
3634 fcport->d_id.b.domain, fcport->d_id.b.area,
3635 fcport->d_id.b.al_pa, mb, BIT_0);
3636 if (mb[0] == MBS_PORT_ID_USED) {
3637 /*
3638 * Device has another loop ID. The firmware team
0107109e
AV
3639 * recommends the driver perform an implicit login with
3640 * the specified ID again. The ID we just used is save
3641 * here so we return with an ID that can be tried by
3642 * the next login.
1da177e4
LT
3643 */
3644 retry++;
3645 tmp_loopid = fcport->loop_id;
3646 fcport->loop_id = mb[1];
3647
3648 DEBUG(printk("Fabric Login: port in use - next "
3649 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3650 fcport->loop_id, fcport->d_id.b.domain,
3651 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3652
3653 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3654 /*
3655 * Login succeeded.
3656 */
3657 if (retry) {
3658 /* A retry occurred before. */
3659 *next_loopid = tmp_loopid;
3660 } else {
3661 /*
3662 * No retry occurred before. Just increment the
3663 * ID value for next login.
3664 */
3665 *next_loopid = (fcport->loop_id + 1);
3666 }
3667
3668 if (mb[1] & BIT_0) {
3669 fcport->port_type = FCT_INITIATOR;
3670 } else {
3671 fcport->port_type = FCT_TARGET;
3672 if (mb[1] & BIT_1) {
8474f3a0 3673 fcport->flags |= FCF_FCP2_DEVICE;
1da177e4
LT
3674 }
3675 }
3676
ad3e0eda
AV
3677 if (mb[10] & BIT_0)
3678 fcport->supported_classes |= FC_COS_CLASS2;
3679 if (mb[10] & BIT_1)
3680 fcport->supported_classes |= FC_COS_CLASS3;
3681
1da177e4
LT
3682 rval = QLA_SUCCESS;
3683 break;
3684 } else if (mb[0] == MBS_LOOP_ID_USED) {
3685 /*
3686 * Loop ID already used, try next loop ID.
3687 */
3688 fcport->loop_id++;
e315cd28 3689 rval = qla2x00_find_new_loop_id(vha, fcport);
1da177e4
LT
3690 if (rval != QLA_SUCCESS) {
3691 /* Ran out of loop IDs to use */
3692 break;
3693 }
3694 } else if (mb[0] == MBS_COMMAND_ERROR) {
3695 /*
3696 * Firmware possibly timed out during login. If NO
3697 * retries are left to do then the device is declared
3698 * dead.
3699 */
3700 *next_loopid = fcport->loop_id;
e315cd28 3701 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
3702 fcport->d_id.b.domain, fcport->d_id.b.area,
3703 fcport->d_id.b.al_pa);
e315cd28 3704 qla2x00_mark_device_lost(vha, fcport, 1, 0);
1da177e4
LT
3705
3706 rval = 1;
3707 break;
3708 } else {
3709 /*
3710 * unrecoverable / not handled error
3711 */
3712 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
fa2a1ce5 3713 "loop_id=%x jiffies=%lx.\n",
e315cd28 3714 __func__, vha->host_no, mb[0],
1da177e4
LT
3715 fcport->d_id.b.domain, fcport->d_id.b.area,
3716 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3717
3718 *next_loopid = fcport->loop_id;
e315cd28 3719 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
3720 fcport->d_id.b.domain, fcport->d_id.b.area,
3721 fcport->d_id.b.al_pa);
1da177e4 3722 fcport->loop_id = FC_NO_LOOP_ID;
0eedfcf0 3723 fcport->login_retry = 0;
1da177e4
LT
3724
3725 rval = 3;
3726 break;
3727 }
3728 }
3729
3730 return (rval);
3731}
3732
3733/*
3734 * qla2x00_local_device_login
3735 * Issue local device login command.
3736 *
3737 * Input:
3738 * ha = adapter block pointer.
3739 * loop_id = loop id of device to login to.
3740 *
3741 * Returns (Where's the #define!!!!):
3742 * 0 - Login successfully
3743 * 1 - Login failed
3744 * 3 - Fatal error
3745 */
3746int
e315cd28 3747qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
1da177e4
LT
3748{
3749 int rval;
3750 uint16_t mb[MAILBOX_REGISTER_COUNT];
3751
3752 memset(mb, 0, sizeof(mb));
e315cd28 3753 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
1da177e4
LT
3754 if (rval == QLA_SUCCESS) {
3755 /* Interrogate mailbox registers for any errors */
3756 if (mb[0] == MBS_COMMAND_ERROR)
3757 rval = 1;
3758 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3759 /* device not in PCB table */
3760 rval = 3;
3761 }
3762
3763 return (rval);
3764}
3765
3766/*
3767 * qla2x00_loop_resync
3768 * Resync with fibre channel devices.
3769 *
3770 * Input:
3771 * ha = adapter block pointer.
3772 *
3773 * Returns:
3774 * 0 = success
3775 */
3776int
e315cd28 3777qla2x00_loop_resync(scsi_qla_host_t *vha)
1da177e4 3778{
73208dfd 3779 int rval = QLA_SUCCESS;
1da177e4 3780 uint32_t wait_time;
67c2e93a
AC
3781 struct req_que *req;
3782 struct rsp_que *rsp;
3783
7163ea81 3784 if (vha->hw->flags.cpu_affinity_enabled)
67c2e93a
AC
3785 req = vha->hw->req_q_map[0];
3786 else
3787 req = vha->req;
3788 rsp = req->rsp;
1da177e4 3789
e315cd28
AC
3790 atomic_set(&vha->loop_state, LOOP_UPDATE);
3791 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3792 if (vha->flags.online) {
3793 if (!(rval = qla2x00_fw_ready(vha))) {
1da177e4
LT
3794 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3795 wait_time = 256;
3796 do {
e315cd28 3797 atomic_set(&vha->loop_state, LOOP_UPDATE);
1da177e4 3798
0107109e 3799 /* Issue a marker after FW becomes ready. */
73208dfd
AC
3800 qla2x00_marker(vha, req, rsp, 0, 0,
3801 MK_SYNC_ALL);
e315cd28 3802 vha->marker_needed = 0;
1da177e4
LT
3803
3804 /* Remap devices on Loop. */
e315cd28 3805 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1da177e4 3806
e315cd28 3807 qla2x00_configure_loop(vha);
1da177e4 3808 wait_time--;
e315cd28
AC
3809 } while (!atomic_read(&vha->loop_down_timer) &&
3810 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
3811 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
3812 &vha->dpc_flags)));
1da177e4 3813 }
1da177e4
LT
3814 }
3815
e315cd28 3816 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
1da177e4 3817 return (QLA_FUNCTION_FAILED);
1da177e4 3818
e315cd28 3819 if (rval)
1da177e4 3820 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
1da177e4
LT
3821
3822 return (rval);
3823}
3824
d97994dc 3825void
67becc00 3826qla2x00_update_fcports(scsi_qla_host_t *base_vha)
d97994dc
AV
3827{
3828 fc_port_t *fcport;
67becc00 3829 struct scsi_qla_host *tvp, *vha;
d97994dc
AV
3830
3831 /* Go with deferred removal of rport references. */
67becc00
AV
3832 list_for_each_entry_safe(vha, tvp, &base_vha->hw->vp_list, list)
3833 list_for_each_entry(fcport, &vha->vp_fcports, list)
3834 if (fcport && fcport->drport &&
3835 atomic_read(&fcport->state) != FCS_UNCONFIGURED)
3836 qla2x00_rport_del(fcport);
d97994dc
AV
3837}
3838
a9083016
GM
3839void
3840qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
3841{
3842 struct qla_hw_data *ha = vha->hw;
3843 struct scsi_qla_host *vp, *base_vha = pci_get_drvdata(ha->pdev);
3844 struct scsi_qla_host *tvp;
3845
3846 vha->flags.online = 0;
3847 ha->flags.chip_reset_done = 0;
3848 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3849 ha->qla_stats.total_isp_aborts++;
3850
3851 qla_printk(KERN_INFO, ha,
3852 "Performing ISP error recovery - ha= %p.\n", ha);
3853
3854 /* Chip reset does not apply to 82XX */
3855 if (!IS_QLA82XX(ha))
3856 ha->isp_ops->reset_chip(vha);
3857
3858 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
3859 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
3860 atomic_set(&vha->loop_state, LOOP_DOWN);
3861 qla2x00_mark_all_devices_lost(vha, 0);
3862 list_for_each_entry_safe(vp, tvp, &base_vha->hw->vp_list, list)
3863 qla2x00_mark_all_devices_lost(vp, 0);
3864 } else {
3865 if (!atomic_read(&vha->loop_down_timer))
3866 atomic_set(&vha->loop_down_timer,
3867 LOOP_DOWN_TIME);
3868 }
3869
3870 /* Make sure for ISP 82XX IO DMA is complete */
3871 if (IS_QLA82XX(ha))
3872 qla82xx_wait_for_pending_commands(vha);
3873
3874 /* Requeue all commands in outstanding command list. */
3875 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
3876}
3877
1da177e4
LT
3878/*
3879* qla2x00_abort_isp
3880* Resets ISP and aborts all outstanding commands.
3881*
3882* Input:
3883* ha = adapter block pointer.
3884*
3885* Returns:
3886* 0 = success
3887*/
3888int
e315cd28 3889qla2x00_abort_isp(scsi_qla_host_t *vha)
1da177e4 3890{
476e8978 3891 int rval;
1da177e4 3892 uint8_t status = 0;
e315cd28
AC
3893 struct qla_hw_data *ha = vha->hw;
3894 struct scsi_qla_host *vp;
ee546b6e 3895 struct scsi_qla_host *tvp;
73208dfd 3896 struct req_que *req = ha->req_q_map[0];
1da177e4 3897
e315cd28 3898 if (vha->flags.online) {
a9083016 3899 qla2x00_abort_isp_cleanup(vha);
1da177e4 3900
85880801
AV
3901 if (unlikely(pci_channel_offline(ha->pdev) &&
3902 ha->flags.pci_channel_io_perm_failure)) {
3903 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3904 status = 0;
3905 return status;
3906 }
3907
73208dfd 3908 ha->isp_ops->get_flash_version(vha, req->ring);
30c47662 3909
e315cd28 3910 ha->isp_ops->nvram_config(vha);
1da177e4 3911
e315cd28
AC
3912 if (!qla2x00_restart_isp(vha)) {
3913 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1da177e4 3914
e315cd28 3915 if (!atomic_read(&vha->loop_down_timer)) {
1da177e4
LT
3916 /*
3917 * Issue marker command only when we are going
3918 * to start the I/O .
3919 */
e315cd28 3920 vha->marker_needed = 1;
1da177e4
LT
3921 }
3922
e315cd28 3923 vha->flags.online = 1;
1da177e4 3924
fd34f556 3925 ha->isp_ops->enable_intrs(ha);
1da177e4 3926
fa2a1ce5 3927 ha->isp_abort_cnt = 0;
e315cd28 3928 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
476e8978 3929
29c5397f
LC
3930 if (IS_QLA81XX(ha))
3931 qla2x00_get_fw_version(vha,
3932 &ha->fw_major_version,
3933 &ha->fw_minor_version,
3934 &ha->fw_subminor_version,
3935 &ha->fw_attributes, &ha->fw_memory_size,
3936 ha->mpi_version, &ha->mpi_capabilities,
3937 ha->phy_version);
3938
df613b96
AV
3939 if (ha->fce) {
3940 ha->flags.fce_enabled = 1;
3941 memset(ha->fce, 0,
3942 fce_calc_size(ha->fce_bufs));
e315cd28 3943 rval = qla2x00_enable_fce_trace(vha,
df613b96
AV
3944 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3945 &ha->fce_bufs);
3946 if (rval) {
3947 qla_printk(KERN_WARNING, ha,
3948 "Unable to reinitialize FCE "
3949 "(%d).\n", rval);
3950 ha->flags.fce_enabled = 0;
3951 }
3952 }
436a7b11
AV
3953
3954 if (ha->eft) {
3955 memset(ha->eft, 0, EFT_SIZE);
e315cd28 3956 rval = qla2x00_enable_eft_trace(vha,
436a7b11
AV
3957 ha->eft_dma, EFT_NUM_BUFFERS);
3958 if (rval) {
3959 qla_printk(KERN_WARNING, ha,
3960 "Unable to reinitialize EFT "
3961 "(%d).\n", rval);
3962 }
3963 }
1da177e4 3964 } else { /* failed the ISP abort */
e315cd28
AC
3965 vha->flags.online = 1;
3966 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1da177e4
LT
3967 if (ha->isp_abort_cnt == 0) {
3968 qla_printk(KERN_WARNING, ha,
3969 "ISP error recovery failed - "
3970 "board disabled\n");
fa2a1ce5 3971 /*
1da177e4
LT
3972 * The next call disables the board
3973 * completely.
3974 */
e315cd28
AC
3975 ha->isp_ops->reset_adapter(vha);
3976 vha->flags.online = 0;
1da177e4 3977 clear_bit(ISP_ABORT_RETRY,
e315cd28 3978 &vha->dpc_flags);
1da177e4
LT
3979 status = 0;
3980 } else { /* schedule another ISP abort */
3981 ha->isp_abort_cnt--;
3982 DEBUG(printk("qla%ld: ISP abort - "
0107109e 3983 "retry remaining %d\n",
e315cd28 3984 vha->host_no, ha->isp_abort_cnt));
1da177e4
LT
3985 status = 1;
3986 }
3987 } else {
3988 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3989 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3990 "- retrying (%d) more times\n",
e315cd28
AC
3991 vha->host_no, ha->isp_abort_cnt));
3992 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
1da177e4
LT
3993 status = 1;
3994 }
3995 }
fa2a1ce5 3996
1da177e4
LT
3997 }
3998
e315cd28
AC
3999 if (!status) {
4000 DEBUG(printk(KERN_INFO
4001 "qla2x00_abort_isp(%ld): succeeded.\n",
4002 vha->host_no));
ee546b6e 4003 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
e315cd28
AC
4004 if (vp->vp_idx)
4005 qla2x00_vp_abort_isp(vp);
4006 }
4007 } else {
1da177e4
LT
4008 qla_printk(KERN_INFO, ha,
4009 "qla2x00_abort_isp: **** FAILED ****\n");
1da177e4
LT
4010 }
4011
4012 return(status);
4013}
4014
4015/*
4016* qla2x00_restart_isp
4017* restarts the ISP after a reset
4018*
4019* Input:
4020* ha = adapter block pointer.
4021*
4022* Returns:
4023* 0 = success
4024*/
4025static int
e315cd28 4026qla2x00_restart_isp(scsi_qla_host_t *vha)
1da177e4 4027{
c6b2fca8 4028 int status = 0;
1da177e4 4029 uint32_t wait_time;
e315cd28 4030 struct qla_hw_data *ha = vha->hw;
73208dfd
AC
4031 struct req_que *req = ha->req_q_map[0];
4032 struct rsp_que *rsp = ha->rsp_q_map[0];
1da177e4
LT
4033
4034 /* If firmware needs to be loaded */
e315cd28
AC
4035 if (qla2x00_isp_firmware(vha)) {
4036 vha->flags.online = 0;
4037 status = ha->isp_ops->chip_diag(vha);
4038 if (!status)
4039 status = qla2x00_setup_chip(vha);
1da177e4
LT
4040 }
4041
e315cd28
AC
4042 if (!status && !(status = qla2x00_init_rings(vha))) {
4043 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
2533cf67 4044 ha->flags.chip_reset_done = 1;
73208dfd
AC
4045 /* Initialize the queues in use */
4046 qla25xx_init_queues(ha);
4047
e315cd28
AC
4048 status = qla2x00_fw_ready(vha);
4049 if (!status) {
1da177e4 4050 DEBUG(printk("%s(): Start configure loop, "
744f11fd 4051 "status = %d\n", __func__, status));
0107109e
AV
4052
4053 /* Issue a marker after FW becomes ready. */
73208dfd 4054 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
0107109e 4055
e315cd28 4056 vha->flags.online = 1;
1da177e4
LT
4057 /* Wait at most MAX_TARGET RSCNs for a stable link. */
4058 wait_time = 256;
4059 do {
e315cd28
AC
4060 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4061 qla2x00_configure_loop(vha);
1da177e4 4062 wait_time--;
e315cd28
AC
4063 } while (!atomic_read(&vha->loop_down_timer) &&
4064 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
4065 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
4066 &vha->dpc_flags)));
1da177e4
LT
4067 }
4068
4069 /* if no cable then assume it's good */
e315cd28 4070 if ((vha->device_flags & DFLG_NO_CABLE))
1da177e4
LT
4071 status = 0;
4072
4073 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
4074 __func__,
744f11fd 4075 status));
1da177e4
LT
4076 }
4077 return (status);
4078}
4079
73208dfd
AC
4080static int
4081qla25xx_init_queues(struct qla_hw_data *ha)
4082{
4083 struct rsp_que *rsp = NULL;
4084 struct req_que *req = NULL;
4085 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
4086 int ret = -1;
4087 int i;
4088
2afa19a9 4089 for (i = 1; i < ha->max_rsp_queues; i++) {
73208dfd
AC
4090 rsp = ha->rsp_q_map[i];
4091 if (rsp) {
4092 rsp->options &= ~BIT_0;
618a7523 4093 ret = qla25xx_init_rsp_que(base_vha, rsp);
73208dfd
AC
4094 if (ret != QLA_SUCCESS)
4095 DEBUG2_17(printk(KERN_WARNING
4096 "%s Rsp que:%d init failed\n", __func__,
4097 rsp->id));
4098 else
4099 DEBUG2_17(printk(KERN_INFO
4100 "%s Rsp que:%d inited\n", __func__,
4101 rsp->id));
4102 }
2afa19a9
AC
4103 }
4104 for (i = 1; i < ha->max_req_queues; i++) {
73208dfd
AC
4105 req = ha->req_q_map[i];
4106 if (req) {
29bdccbe 4107 /* Clear outstanding commands array. */
73208dfd 4108 req->options &= ~BIT_0;
618a7523 4109 ret = qla25xx_init_req_que(base_vha, req);
73208dfd
AC
4110 if (ret != QLA_SUCCESS)
4111 DEBUG2_17(printk(KERN_WARNING
4112 "%s Req que:%d init failed\n", __func__,
4113 req->id));
4114 else
4115 DEBUG2_17(printk(KERN_WARNING
29bdccbe 4116 "%s Req que:%d inited\n", __func__,
73208dfd
AC
4117 req->id));
4118 }
4119 }
4120 return ret;
4121}
4122
1da177e4
LT
4123/*
4124* qla2x00_reset_adapter
4125* Reset adapter.
4126*
4127* Input:
4128* ha = adapter block pointer.
4129*/
abbd8870 4130void
e315cd28 4131qla2x00_reset_adapter(scsi_qla_host_t *vha)
1da177e4
LT
4132{
4133 unsigned long flags = 0;
e315cd28 4134 struct qla_hw_data *ha = vha->hw;
3d71644c 4135 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 4136
e315cd28 4137 vha->flags.online = 0;
fd34f556 4138 ha->isp_ops->disable_intrs(ha);
1da177e4 4139
1da177e4
LT
4140 spin_lock_irqsave(&ha->hardware_lock, flags);
4141 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
4142 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
4143 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
4144 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
4145 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4146}
0107109e
AV
4147
4148void
e315cd28 4149qla24xx_reset_adapter(scsi_qla_host_t *vha)
0107109e
AV
4150{
4151 unsigned long flags = 0;
e315cd28 4152 struct qla_hw_data *ha = vha->hw;
0107109e
AV
4153 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
4154
a9083016
GM
4155 if (IS_QLA82XX(ha))
4156 return;
4157
e315cd28 4158 vha->flags.online = 0;
fd34f556 4159 ha->isp_ops->disable_intrs(ha);
0107109e
AV
4160
4161 spin_lock_irqsave(&ha->hardware_lock, flags);
4162 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
4163 RD_REG_DWORD(&reg->hccr);
4164 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
4165 RD_REG_DWORD(&reg->hccr);
4166 spin_unlock_irqrestore(&ha->hardware_lock, flags);
09ff36d3
AV
4167
4168 if (IS_NOPOLLING_TYPE(ha))
4169 ha->isp_ops->enable_intrs(ha);
0107109e
AV
4170}
4171
4e08df3f
DM
4172/* On sparc systems, obtain port and node WWN from firmware
4173 * properties.
4174 */
e315cd28
AC
4175static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
4176 struct nvram_24xx *nv)
4e08df3f
DM
4177{
4178#ifdef CONFIG_SPARC
e315cd28 4179 struct qla_hw_data *ha = vha->hw;
4e08df3f 4180 struct pci_dev *pdev = ha->pdev;
15576bc8
DM
4181 struct device_node *dp = pci_device_to_OF_node(pdev);
4182 const u8 *val;
4e08df3f
DM
4183 int len;
4184
4185 val = of_get_property(dp, "port-wwn", &len);
4186 if (val && len >= WWN_SIZE)
4187 memcpy(nv->port_name, val, WWN_SIZE);
4188
4189 val = of_get_property(dp, "node-wwn", &len);
4190 if (val && len >= WWN_SIZE)
4191 memcpy(nv->node_name, val, WWN_SIZE);
4192#endif
4193}
4194
0107109e 4195int
e315cd28 4196qla24xx_nvram_config(scsi_qla_host_t *vha)
0107109e 4197{
4e08df3f 4198 int rval;
0107109e
AV
4199 struct init_cb_24xx *icb;
4200 struct nvram_24xx *nv;
4201 uint32_t *dptr;
4202 uint8_t *dptr1, *dptr2;
4203 uint32_t chksum;
4204 uint16_t cnt;
e315cd28 4205 struct qla_hw_data *ha = vha->hw;
0107109e 4206
4e08df3f 4207 rval = QLA_SUCCESS;
0107109e 4208 icb = (struct init_cb_24xx *)ha->init_cb;
281afe19 4209 nv = ha->nvram;
0107109e
AV
4210
4211 /* Determine NVRAM starting address. */
e5b68a61
AC
4212 if (ha->flags.port0) {
4213 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
4214 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
4215 } else {
0107109e 4216 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
6f641790
AV
4217 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
4218 }
e5b68a61
AC
4219 ha->nvram_size = sizeof(struct nvram_24xx);
4220 ha->vpd_size = FA_NVRAM_VPD_SIZE;
a9083016
GM
4221 if (IS_QLA82XX(ha))
4222 ha->vpd_size = FA_VPD_SIZE_82XX;
0107109e 4223
281afe19
SJ
4224 /* Get VPD data into cache */
4225 ha->vpd = ha->nvram + VPD_OFFSET;
e315cd28 4226 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
281afe19
SJ
4227 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
4228
4229 /* Get NVRAM data into cache and calculate checksum. */
0107109e 4230 dptr = (uint32_t *)nv;
e315cd28 4231 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
0107109e
AV
4232 ha->nvram_size);
4233 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
4234 chksum += le32_to_cpu(*dptr++);
4235
7640335e 4236 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
281afe19 4237 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
0107109e
AV
4238
4239 /* Bad NVRAM data, set defaults parameters. */
4240 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
4241 || nv->id[3] != ' ' ||
4242 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
4243 /* Reset NVRAM data. */
4244 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
4245 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
4246 le16_to_cpu(nv->nvram_version));
4e08df3f
DM
4247 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
4248 "invalid -- WWPN) defaults.\n");
4249
4250 /*
4251 * Set default initialization control block.
4252 */
4253 memset(nv, 0, ha->nvram_size);
4254 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
4255 nv->version = __constant_cpu_to_le16(ICB_VERSION);
4256 nv->frame_payload_size = __constant_cpu_to_le16(2048);
4257 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
4258 nv->exchange_count = __constant_cpu_to_le16(0);
4259 nv->hard_address = __constant_cpu_to_le16(124);
4260 nv->port_name[0] = 0x21;
e5b68a61 4261 nv->port_name[1] = 0x00 + ha->port_no;
4e08df3f
DM
4262 nv->port_name[2] = 0x00;
4263 nv->port_name[3] = 0xe0;
4264 nv->port_name[4] = 0x8b;
4265 nv->port_name[5] = 0x1c;
4266 nv->port_name[6] = 0x55;
4267 nv->port_name[7] = 0x86;
4268 nv->node_name[0] = 0x20;
4269 nv->node_name[1] = 0x00;
4270 nv->node_name[2] = 0x00;
4271 nv->node_name[3] = 0xe0;
4272 nv->node_name[4] = 0x8b;
4273 nv->node_name[5] = 0x1c;
4274 nv->node_name[6] = 0x55;
4275 nv->node_name[7] = 0x86;
e315cd28 4276 qla24xx_nvram_wwn_from_ofw(vha, nv);
4e08df3f
DM
4277 nv->login_retry_count = __constant_cpu_to_le16(8);
4278 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
4279 nv->login_timeout = __constant_cpu_to_le16(0);
4280 nv->firmware_options_1 =
4281 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
4282 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
4283 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4284 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
4285 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
4286 nv->efi_parameters = __constant_cpu_to_le32(0);
4287 nv->reset_delay = 5;
4288 nv->max_luns_per_target = __constant_cpu_to_le16(128);
4289 nv->port_down_retry_count = __constant_cpu_to_le16(30);
4290 nv->link_down_timeout = __constant_cpu_to_le16(30);
4291
4292 rval = 1;
0107109e
AV
4293 }
4294
4295 /* Reset Initialization control block */
e315cd28 4296 memset(icb, 0, ha->init_cb_size);
0107109e
AV
4297
4298 /* Copy 1st segment. */
4299 dptr1 = (uint8_t *)icb;
4300 dptr2 = (uint8_t *)&nv->version;
4301 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
4302 while (cnt--)
4303 *dptr1++ = *dptr2++;
4304
4305 icb->login_retry_count = nv->login_retry_count;
3ea66e28 4306 icb->link_down_on_nos = nv->link_down_on_nos;
0107109e
AV
4307
4308 /* Copy 2nd segment. */
4309 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
4310 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
4311 cnt = (uint8_t *)&icb->reserved_3 -
4312 (uint8_t *)&icb->interrupt_delay_timer;
4313 while (cnt--)
4314 *dptr1++ = *dptr2++;
4315
4316 /*
4317 * Setup driver NVRAM options.
4318 */
e315cd28 4319 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
9bb9fcf2 4320 "QLA2462");
0107109e 4321
5341e868
AV
4322 /* Use alternate WWN? */
4323 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
4324 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4325 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4326 }
4327
0107109e 4328 /* Prepare nodename */
fd0e7e4d 4329 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
0107109e
AV
4330 /*
4331 * Firmware will apply the following mask if the nodename was
4332 * not provided.
4333 */
4334 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4335 icb->node_name[0] &= 0xF0;
4336 }
4337
4338 /* Set host adapter parameters. */
4339 ha->flags.disable_risc_code_load = 0;
0c8c39af
AV
4340 ha->flags.enable_lip_reset = 0;
4341 ha->flags.enable_lip_full_login =
4342 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
4343 ha->flags.enable_target_reset =
4344 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
0107109e 4345 ha->flags.enable_led_scheme = 0;
d4c760c2 4346 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
0107109e 4347
fd0e7e4d
AV
4348 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
4349 (BIT_6 | BIT_5 | BIT_4)) >> 4;
0107109e
AV
4350
4351 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
4352 sizeof(ha->fw_seriallink_options24));
4353
4354 /* save HBA serial number */
4355 ha->serial0 = icb->port_name[5];
4356 ha->serial1 = icb->port_name[6];
4357 ha->serial2 = icb->port_name[7];
e315cd28
AC
4358 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4359 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
0107109e 4360
bc8fb3cb
AV
4361 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
4362
0107109e
AV
4363 ha->retry_count = le16_to_cpu(nv->login_retry_count);
4364
4365 /* Set minimum login_timeout to 4 seconds. */
4366 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
4367 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
4368 if (le16_to_cpu(nv->login_timeout) < 4)
4369 nv->login_timeout = __constant_cpu_to_le16(4);
4370 ha->login_timeout = le16_to_cpu(nv->login_timeout);
c6852c4c 4371 icb->login_timeout = nv->login_timeout;
0107109e 4372
00a537b8
AV
4373 /* Set minimum RATOV to 100 tenths of a second. */
4374 ha->r_a_tov = 100;
0107109e
AV
4375
4376 ha->loop_reset_delay = nv->reset_delay;
4377
4378 /* Link Down Timeout = 0:
4379 *
4380 * When Port Down timer expires we will start returning
4381 * I/O's to OS with "DID_NO_CONNECT".
4382 *
4383 * Link Down Timeout != 0:
4384 *
4385 * The driver waits for the link to come up after link down
4386 * before returning I/Os to OS with "DID_NO_CONNECT".
4387 */
4388 if (le16_to_cpu(nv->link_down_timeout) == 0) {
4389 ha->loop_down_abort_time =
4390 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
4391 } else {
4392 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
4393 ha->loop_down_abort_time =
4394 (LOOP_DOWN_TIME - ha->link_down_timeout);
4395 }
4396
4397 /* Need enough time to try and get the port back. */
4398 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
4399 if (qlport_down_retry)
4400 ha->port_down_retry_count = qlport_down_retry;
4401
4402 /* Set login_retry_count */
4403 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
4404 if (ha->port_down_retry_count ==
4405 le16_to_cpu(nv->port_down_retry_count) &&
4406 ha->port_down_retry_count > 3)
4407 ha->login_retry_count = ha->port_down_retry_count;
4408 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4409 ha->login_retry_count = ha->port_down_retry_count;
4410 if (ql2xloginretrycount)
4411 ha->login_retry_count = ql2xloginretrycount;
4412
4fdfefe5 4413 /* Enable ZIO. */
e315cd28 4414 if (!vha->flags.init_done) {
4fdfefe5
AV
4415 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
4416 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4417 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
4418 le16_to_cpu(icb->interrupt_delay_timer): 2;
4419 }
4420 icb->firmware_options_2 &= __constant_cpu_to_le32(
4421 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
e315cd28 4422 vha->flags.process_response_queue = 0;
4fdfefe5 4423 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4a59f71d
AV
4424 ha->zio_mode = QLA_ZIO_MODE_6;
4425
4fdfefe5 4426 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
e315cd28 4427 "(%d us).\n", vha->host_no, ha->zio_mode,
4fdfefe5
AV
4428 ha->zio_timer * 100));
4429 qla_printk(KERN_INFO, ha,
4430 "ZIO mode %d enabled; timer delay (%d us).\n",
4431 ha->zio_mode, ha->zio_timer * 100);
4432
4433 icb->firmware_options_2 |= cpu_to_le32(
4434 (uint32_t)ha->zio_mode);
4435 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
e315cd28 4436 vha->flags.process_response_queue = 1;
4fdfefe5
AV
4437 }
4438
4e08df3f
DM
4439 if (rval) {
4440 DEBUG2_3(printk(KERN_WARNING
e315cd28 4441 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
4e08df3f
DM
4442 }
4443 return (rval);
0107109e
AV
4444}
4445
413975a0 4446static int
cbc8eb67
AV
4447qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
4448 uint32_t faddr)
d1c61909 4449{
73208dfd 4450 int rval = QLA_SUCCESS;
d1c61909 4451 int segments, fragment;
d1c61909
AV
4452 uint32_t *dcode, dlen;
4453 uint32_t risc_addr;
4454 uint32_t risc_size;
4455 uint32_t i;
e315cd28 4456 struct qla_hw_data *ha = vha->hw;
73208dfd 4457 struct req_que *req = ha->req_q_map[0];
eaac30be
AV
4458
4459 qla_printk(KERN_INFO, ha,
cbc8eb67 4460 "FW: Loading from flash (%x)...\n", faddr);
eaac30be 4461
d1c61909
AV
4462 rval = QLA_SUCCESS;
4463
4464 segments = FA_RISC_CODE_SEGMENTS;
73208dfd 4465 dcode = (uint32_t *)req->ring;
d1c61909
AV
4466 *srisc_addr = 0;
4467
4468 /* Validate firmware image by checking version. */
e315cd28 4469 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
d1c61909
AV
4470 for (i = 0; i < 4; i++)
4471 dcode[i] = be32_to_cpu(dcode[i]);
4472 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
4473 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
4474 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
4475 dcode[3] == 0)) {
4476 qla_printk(KERN_WARNING, ha,
4477 "Unable to verify integrity of flash firmware image!\n");
4478 qla_printk(KERN_WARNING, ha,
4479 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
4480 dcode[1], dcode[2], dcode[3]);
4481
4482 return QLA_FUNCTION_FAILED;
4483 }
4484
4485 while (segments && rval == QLA_SUCCESS) {
4486 /* Read segment's load information. */
e315cd28 4487 qla24xx_read_flash_data(vha, dcode, faddr, 4);
d1c61909
AV
4488
4489 risc_addr = be32_to_cpu(dcode[2]);
4490 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
4491 risc_size = be32_to_cpu(dcode[3]);
4492
4493 fragment = 0;
4494 while (risc_size > 0 && rval == QLA_SUCCESS) {
4495 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
4496 if (dlen > risc_size)
4497 dlen = risc_size;
4498
4499 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
4500 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
e315cd28 4501 vha->host_no, risc_addr, dlen, faddr));
d1c61909 4502
e315cd28 4503 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
d1c61909
AV
4504 for (i = 0; i < dlen; i++)
4505 dcode[i] = swab32(dcode[i]);
4506
73208dfd 4507 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
d1c61909
AV
4508 dlen);
4509 if (rval) {
4510 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
e315cd28 4511 "segment %d of firmware\n", vha->host_no,
d1c61909
AV
4512 fragment));
4513 qla_printk(KERN_WARNING, ha,
4514 "[ERROR] Failed to load segment %d of "
4515 "firmware\n", fragment);
4516 break;
4517 }
4518
4519 faddr += dlen;
4520 risc_addr += dlen;
4521 risc_size -= dlen;
4522 fragment++;
4523 }
4524
4525 /* Next segment. */
4526 segments--;
4527 }
4528
4529 return rval;
4530}
4531
d1c61909
AV
4532#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
4533
0107109e 4534int
e315cd28 4535qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
5433383e
AV
4536{
4537 int rval;
4538 int i, fragment;
4539 uint16_t *wcode, *fwcode;
4540 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
4541 struct fw_blob *blob;
e315cd28 4542 struct qla_hw_data *ha = vha->hw;
73208dfd 4543 struct req_que *req = ha->req_q_map[0];
5433383e
AV
4544
4545 /* Load firmware blob. */
e315cd28 4546 blob = qla2x00_request_firmware(vha);
5433383e
AV
4547 if (!blob) {
4548 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
d1c61909
AV
4549 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
4550 "from: " QLA_FW_URL ".\n");
5433383e
AV
4551 return QLA_FUNCTION_FAILED;
4552 }
4553
4554 rval = QLA_SUCCESS;
4555
73208dfd 4556 wcode = (uint16_t *)req->ring;
5433383e
AV
4557 *srisc_addr = 0;
4558 fwcode = (uint16_t *)blob->fw->data;
4559 fwclen = 0;
4560
4561 /* Validate firmware image by checking version. */
4562 if (blob->fw->size < 8 * sizeof(uint16_t)) {
4563 qla_printk(KERN_WARNING, ha,
4564 "Unable to verify integrity of firmware image (%Zd)!\n",
4565 blob->fw->size);
4566 goto fail_fw_integrity;
4567 }
4568 for (i = 0; i < 4; i++)
4569 wcode[i] = be16_to_cpu(fwcode[i + 4]);
4570 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
4571 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
4572 wcode[2] == 0 && wcode[3] == 0)) {
4573 qla_printk(KERN_WARNING, ha,
4574 "Unable to verify integrity of firmware image!\n");
4575 qla_printk(KERN_WARNING, ha,
4576 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
4577 wcode[1], wcode[2], wcode[3]);
4578 goto fail_fw_integrity;
4579 }
4580
4581 seg = blob->segs;
4582 while (*seg && rval == QLA_SUCCESS) {
4583 risc_addr = *seg;
4584 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
4585 risc_size = be16_to_cpu(fwcode[3]);
4586
4587 /* Validate firmware image size. */
4588 fwclen += risc_size * sizeof(uint16_t);
4589 if (blob->fw->size < fwclen) {
4590 qla_printk(KERN_WARNING, ha,
4591 "Unable to verify integrity of firmware image "
4592 "(%Zd)!\n", blob->fw->size);
4593 goto fail_fw_integrity;
4594 }
4595
4596 fragment = 0;
4597 while (risc_size > 0 && rval == QLA_SUCCESS) {
4598 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
4599 if (wlen > risc_size)
4600 wlen = risc_size;
4601
4602 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
e315cd28 4603 "addr %x, number of words 0x%x.\n", vha->host_no,
5433383e
AV
4604 risc_addr, wlen));
4605
4606 for (i = 0; i < wlen; i++)
4607 wcode[i] = swab16(fwcode[i]);
4608
73208dfd 4609 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
5433383e
AV
4610 wlen);
4611 if (rval) {
4612 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
e315cd28 4613 "segment %d of firmware\n", vha->host_no,
5433383e
AV
4614 fragment));
4615 qla_printk(KERN_WARNING, ha,
4616 "[ERROR] Failed to load segment %d of "
4617 "firmware\n", fragment);
4618 break;
4619 }
4620
4621 fwcode += wlen;
4622 risc_addr += wlen;
4623 risc_size -= wlen;
4624 fragment++;
4625 }
4626
4627 /* Next segment. */
4628 seg++;
4629 }
4630 return rval;
4631
4632fail_fw_integrity:
4633 return QLA_FUNCTION_FAILED;
4634}
4635
eaac30be
AV
4636static int
4637qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
0107109e
AV
4638{
4639 int rval;
4640 int segments, fragment;
4641 uint32_t *dcode, dlen;
4642 uint32_t risc_addr;
4643 uint32_t risc_size;
4644 uint32_t i;
5433383e 4645 struct fw_blob *blob;
0107109e 4646 uint32_t *fwcode, fwclen;
e315cd28 4647 struct qla_hw_data *ha = vha->hw;
73208dfd 4648 struct req_que *req = ha->req_q_map[0];
0107109e 4649
5433383e 4650 /* Load firmware blob. */
e315cd28 4651 blob = qla2x00_request_firmware(vha);
5433383e
AV
4652 if (!blob) {
4653 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
d1c61909
AV
4654 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
4655 "from: " QLA_FW_URL ".\n");
4656
eaac30be 4657 return QLA_FUNCTION_FAILED;
0107109e
AV
4658 }
4659
eaac30be
AV
4660 qla_printk(KERN_INFO, ha,
4661 "FW: Loading via request-firmware...\n");
4662
0107109e
AV
4663 rval = QLA_SUCCESS;
4664
4665 segments = FA_RISC_CODE_SEGMENTS;
73208dfd 4666 dcode = (uint32_t *)req->ring;
0107109e 4667 *srisc_addr = 0;
5433383e 4668 fwcode = (uint32_t *)blob->fw->data;
0107109e
AV
4669 fwclen = 0;
4670
4671 /* Validate firmware image by checking version. */
5433383e 4672 if (blob->fw->size < 8 * sizeof(uint32_t)) {
0107109e 4673 qla_printk(KERN_WARNING, ha,
5433383e
AV
4674 "Unable to verify integrity of firmware image (%Zd)!\n",
4675 blob->fw->size);
0107109e
AV
4676 goto fail_fw_integrity;
4677 }
4678 for (i = 0; i < 4; i++)
4679 dcode[i] = be32_to_cpu(fwcode[i + 4]);
4680 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
4681 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
4682 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
4683 dcode[3] == 0)) {
4684 qla_printk(KERN_WARNING, ha,
5433383e 4685 "Unable to verify integrity of firmware image!\n");
0107109e
AV
4686 qla_printk(KERN_WARNING, ha,
4687 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
4688 dcode[1], dcode[2], dcode[3]);
4689 goto fail_fw_integrity;
4690 }
4691
4692 while (segments && rval == QLA_SUCCESS) {
4693 risc_addr = be32_to_cpu(fwcode[2]);
4694 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
4695 risc_size = be32_to_cpu(fwcode[3]);
4696
4697 /* Validate firmware image size. */
4698 fwclen += risc_size * sizeof(uint32_t);
5433383e 4699 if (blob->fw->size < fwclen) {
0107109e 4700 qla_printk(KERN_WARNING, ha,
5433383e
AV
4701 "Unable to verify integrity of firmware image "
4702 "(%Zd)!\n", blob->fw->size);
4703
0107109e
AV
4704 goto fail_fw_integrity;
4705 }
4706
4707 fragment = 0;
4708 while (risc_size > 0 && rval == QLA_SUCCESS) {
4709 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
4710 if (dlen > risc_size)
4711 dlen = risc_size;
4712
4713 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
e315cd28 4714 "addr %x, number of dwords 0x%x.\n", vha->host_no,
0107109e
AV
4715 risc_addr, dlen));
4716
4717 for (i = 0; i < dlen; i++)
4718 dcode[i] = swab32(fwcode[i]);
4719
73208dfd 4720 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
590f98e5 4721 dlen);
0107109e
AV
4722 if (rval) {
4723 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
e315cd28 4724 "segment %d of firmware\n", vha->host_no,
0107109e
AV
4725 fragment));
4726 qla_printk(KERN_WARNING, ha,
4727 "[ERROR] Failed to load segment %d of "
4728 "firmware\n", fragment);
4729 break;
4730 }
4731
4732 fwcode += dlen;
4733 risc_addr += dlen;
4734 risc_size -= dlen;
4735 fragment++;
4736 }
4737
4738 /* Next segment. */
4739 segments--;
4740 }
0107109e
AV
4741 return rval;
4742
4743fail_fw_integrity:
0107109e 4744 return QLA_FUNCTION_FAILED;
0107109e 4745}
18c6c127 4746
eaac30be
AV
4747int
4748qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4749{
4750 int rval;
4751
e337d907
AV
4752 if (ql2xfwloadbin == 1)
4753 return qla81xx_load_risc(vha, srisc_addr);
4754
eaac30be
AV
4755 /*
4756 * FW Load priority:
4757 * 1) Firmware via request-firmware interface (.bin file).
4758 * 2) Firmware residing in flash.
4759 */
4760 rval = qla24xx_load_risc_blob(vha, srisc_addr);
4761 if (rval == QLA_SUCCESS)
4762 return rval;
4763
cbc8eb67
AV
4764 return qla24xx_load_risc_flash(vha, srisc_addr,
4765 vha->hw->flt_region_fw);
eaac30be
AV
4766}
4767
4768int
4769qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4770{
4771 int rval;
cbc8eb67 4772 struct qla_hw_data *ha = vha->hw;
eaac30be 4773
e337d907 4774 if (ql2xfwloadbin == 2)
cbc8eb67 4775 goto try_blob_fw;
e337d907 4776
eaac30be
AV
4777 /*
4778 * FW Load priority:
4779 * 1) Firmware residing in flash.
4780 * 2) Firmware via request-firmware interface (.bin file).
cbc8eb67 4781 * 3) Golden-Firmware residing in flash -- limited operation.
eaac30be 4782 */
cbc8eb67 4783 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
eaac30be
AV
4784 if (rval == QLA_SUCCESS)
4785 return rval;
4786
cbc8eb67
AV
4787try_blob_fw:
4788 rval = qla24xx_load_risc_blob(vha, srisc_addr);
4789 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
4790 return rval;
4791
4792 qla_printk(KERN_ERR, ha,
4793 "FW: Attempting to fallback to golden firmware...\n");
4794 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
4795 if (rval != QLA_SUCCESS)
4796 return rval;
4797
4798 qla_printk(KERN_ERR, ha,
4799 "FW: Please update operational firmware...\n");
4800 ha->flags.running_gold_fw = 1;
4801
4802 return rval;
eaac30be
AV
4803}
4804
18c6c127 4805void
e315cd28 4806qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
18c6c127
AV
4807{
4808 int ret, retries;
e315cd28 4809 struct qla_hw_data *ha = vha->hw;
18c6c127 4810
85880801
AV
4811 if (ha->flags.pci_channel_io_perm_failure)
4812 return;
e428924c 4813 if (!IS_FWI2_CAPABLE(ha))
18c6c127 4814 return;
75edf81d
AV
4815 if (!ha->fw_major_version)
4816 return;
18c6c127 4817
e315cd28 4818 ret = qla2x00_stop_firmware(vha);
7c7f1f29 4819 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
b469a7cb 4820 ret != QLA_INVALID_COMMAND && retries ; retries--) {
e315cd28
AC
4821 ha->isp_ops->reset_chip(vha);
4822 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
18c6c127 4823 continue;
e315cd28 4824 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
18c6c127
AV
4825 continue;
4826 qla_printk(KERN_INFO, ha,
4827 "Attempting retry of stop-firmware command...\n");
e315cd28 4828 ret = qla2x00_stop_firmware(vha);
18c6c127
AV
4829 }
4830}
2c3dfe3f
SJ
4831
4832int
e315cd28 4833qla24xx_configure_vhba(scsi_qla_host_t *vha)
2c3dfe3f
SJ
4834{
4835 int rval = QLA_SUCCESS;
4836 uint16_t mb[MAILBOX_REGISTER_COUNT];
e315cd28
AC
4837 struct qla_hw_data *ha = vha->hw;
4838 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
67c2e93a
AC
4839 struct req_que *req;
4840 struct rsp_que *rsp;
2c3dfe3f 4841
e315cd28 4842 if (!vha->vp_idx)
2c3dfe3f
SJ
4843 return -EINVAL;
4844
e315cd28 4845 rval = qla2x00_fw_ready(base_vha);
7163ea81 4846 if (ha->flags.cpu_affinity_enabled)
67c2e93a
AC
4847 req = ha->req_q_map[0];
4848 else
4849 req = vha->req;
4850 rsp = req->rsp;
4851
2c3dfe3f 4852 if (rval == QLA_SUCCESS) {
e315cd28 4853 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
73208dfd 4854 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
2c3dfe3f
SJ
4855 }
4856
e315cd28 4857 vha->flags.management_server_logged_in = 0;
2c3dfe3f
SJ
4858
4859 /* Login to SNS first */
e315cd28 4860 ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb, BIT_1);
2c3dfe3f
SJ
4861 if (mb[0] != MBS_COMMAND_COMPLETE) {
4862 DEBUG15(qla_printk(KERN_INFO, ha,
4863 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4864 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4865 mb[0], mb[1], mb[2], mb[6], mb[7]));
4866 return (QLA_FUNCTION_FAILED);
4867 }
4868
e315cd28
AC
4869 atomic_set(&vha->loop_down_timer, 0);
4870 atomic_set(&vha->loop_state, LOOP_UP);
4871 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4872 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4873 rval = qla2x00_loop_resync(base_vha);
2c3dfe3f
SJ
4874
4875 return rval;
4876}
4d4df193
HK
4877
4878/* 84XX Support **************************************************************/
4879
4880static LIST_HEAD(qla_cs84xx_list);
4881static DEFINE_MUTEX(qla_cs84xx_mutex);
4882
4883static struct qla_chip_state_84xx *
e315cd28 4884qla84xx_get_chip(struct scsi_qla_host *vha)
4d4df193
HK
4885{
4886 struct qla_chip_state_84xx *cs84xx;
e315cd28 4887 struct qla_hw_data *ha = vha->hw;
4d4df193
HK
4888
4889 mutex_lock(&qla_cs84xx_mutex);
4890
4891 /* Find any shared 84xx chip. */
4892 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
4893 if (cs84xx->bus == ha->pdev->bus) {
4894 kref_get(&cs84xx->kref);
4895 goto done;
4896 }
4897 }
4898
4899 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
4900 if (!cs84xx)
4901 goto done;
4902
4903 kref_init(&cs84xx->kref);
4904 spin_lock_init(&cs84xx->access_lock);
4905 mutex_init(&cs84xx->fw_update_mutex);
4906 cs84xx->bus = ha->pdev->bus;
4907
4908 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
4909done:
4910 mutex_unlock(&qla_cs84xx_mutex);
4911 return cs84xx;
4912}
4913
4914static void
4915__qla84xx_chip_release(struct kref *kref)
4916{
4917 struct qla_chip_state_84xx *cs84xx =
4918 container_of(kref, struct qla_chip_state_84xx, kref);
4919
4920 mutex_lock(&qla_cs84xx_mutex);
4921 list_del(&cs84xx->list);
4922 mutex_unlock(&qla_cs84xx_mutex);
4923 kfree(cs84xx);
4924}
4925
4926void
e315cd28 4927qla84xx_put_chip(struct scsi_qla_host *vha)
4d4df193 4928{
e315cd28 4929 struct qla_hw_data *ha = vha->hw;
4d4df193
HK
4930 if (ha->cs84xx)
4931 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
4932}
4933
4934static int
e315cd28 4935qla84xx_init_chip(scsi_qla_host_t *vha)
4d4df193
HK
4936{
4937 int rval;
4938 uint16_t status[2];
e315cd28 4939 struct qla_hw_data *ha = vha->hw;
4d4df193
HK
4940
4941 mutex_lock(&ha->cs84xx->fw_update_mutex);
4942
e315cd28 4943 rval = qla84xx_verify_chip(vha, status);
4d4df193
HK
4944
4945 mutex_unlock(&ha->cs84xx->fw_update_mutex);
4946
4947 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
4948 QLA_SUCCESS;
4949}
3a03eb79
AV
4950
4951/* 81XX Support **************************************************************/
4952
4953int
4954qla81xx_nvram_config(scsi_qla_host_t *vha)
4955{
4956 int rval;
4957 struct init_cb_81xx *icb;
4958 struct nvram_81xx *nv;
4959 uint32_t *dptr;
4960 uint8_t *dptr1, *dptr2;
4961 uint32_t chksum;
4962 uint16_t cnt;
4963 struct qla_hw_data *ha = vha->hw;
4964
4965 rval = QLA_SUCCESS;
4966 icb = (struct init_cb_81xx *)ha->init_cb;
4967 nv = ha->nvram;
4968
4969 /* Determine NVRAM starting address. */
4970 ha->nvram_size = sizeof(struct nvram_81xx);
3a03eb79 4971 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3a03eb79
AV
4972
4973 /* Get VPD data into cache */
4974 ha->vpd = ha->nvram + VPD_OFFSET;
3d79038f
AV
4975 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
4976 ha->vpd_size);
3a03eb79
AV
4977
4978 /* Get NVRAM data into cache and calculate checksum. */
3d79038f 4979 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
3a03eb79 4980 ha->nvram_size);
3d79038f 4981 dptr = (uint32_t *)nv;
3a03eb79
AV
4982 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
4983 chksum += le32_to_cpu(*dptr++);
4984
7640335e 4985 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
3a03eb79
AV
4986 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
4987
4988 /* Bad NVRAM data, set defaults parameters. */
4989 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
4990 || nv->id[3] != ' ' ||
4991 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
4992 /* Reset NVRAM data. */
4993 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
4994 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
4995 le16_to_cpu(nv->nvram_version));
4996 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
4997 "invalid -- WWPN) defaults.\n");
4998
4999 /*
5000 * Set default initialization control block.
5001 */
5002 memset(nv, 0, ha->nvram_size);
5003 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
5004 nv->version = __constant_cpu_to_le16(ICB_VERSION);
5005 nv->frame_payload_size = __constant_cpu_to_le16(2048);
5006 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
5007 nv->exchange_count = __constant_cpu_to_le16(0);
5008 nv->port_name[0] = 0x21;
e5b68a61 5009 nv->port_name[1] = 0x00 + ha->port_no;
3a03eb79
AV
5010 nv->port_name[2] = 0x00;
5011 nv->port_name[3] = 0xe0;
5012 nv->port_name[4] = 0x8b;
5013 nv->port_name[5] = 0x1c;
5014 nv->port_name[6] = 0x55;
5015 nv->port_name[7] = 0x86;
5016 nv->node_name[0] = 0x20;
5017 nv->node_name[1] = 0x00;
5018 nv->node_name[2] = 0x00;
5019 nv->node_name[3] = 0xe0;
5020 nv->node_name[4] = 0x8b;
5021 nv->node_name[5] = 0x1c;
5022 nv->node_name[6] = 0x55;
5023 nv->node_name[7] = 0x86;
5024 nv->login_retry_count = __constant_cpu_to_le16(8);
5025 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
5026 nv->login_timeout = __constant_cpu_to_le16(0);
5027 nv->firmware_options_1 =
5028 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
5029 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
5030 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
5031 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
5032 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
5033 nv->efi_parameters = __constant_cpu_to_le32(0);
5034 nv->reset_delay = 5;
5035 nv->max_luns_per_target = __constant_cpu_to_le16(128);
5036 nv->port_down_retry_count = __constant_cpu_to_le16(30);
5037 nv->link_down_timeout = __constant_cpu_to_le16(30);
eeebcc92 5038 nv->enode_mac[0] = 0x00;
3a03eb79
AV
5039 nv->enode_mac[1] = 0x02;
5040 nv->enode_mac[2] = 0x03;
5041 nv->enode_mac[3] = 0x04;
5042 nv->enode_mac[4] = 0x05;
e5b68a61 5043 nv->enode_mac[5] = 0x06 + ha->port_no;
3a03eb79
AV
5044
5045 rval = 1;
5046 }
5047
5048 /* Reset Initialization control block */
5049 memset(icb, 0, sizeof(struct init_cb_81xx));
5050
5051 /* Copy 1st segment. */
5052 dptr1 = (uint8_t *)icb;
5053 dptr2 = (uint8_t *)&nv->version;
5054 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
5055 while (cnt--)
5056 *dptr1++ = *dptr2++;
5057
5058 icb->login_retry_count = nv->login_retry_count;
5059
5060 /* Copy 2nd segment. */
5061 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
5062 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
5063 cnt = (uint8_t *)&icb->reserved_5 -
5064 (uint8_t *)&icb->interrupt_delay_timer;
5065 while (cnt--)
5066 *dptr1++ = *dptr2++;
5067
5068 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
5069 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
5070 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
5071 icb->enode_mac[0] = 0x01;
5072 icb->enode_mac[1] = 0x02;
5073 icb->enode_mac[2] = 0x03;
5074 icb->enode_mac[3] = 0x04;
5075 icb->enode_mac[4] = 0x05;
e5b68a61 5076 icb->enode_mac[5] = 0x06 + ha->port_no;
3a03eb79
AV
5077 }
5078
b64b0e8f
AV
5079 /* Use extended-initialization control block. */
5080 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
5081
3a03eb79
AV
5082 /*
5083 * Setup driver NVRAM options.
5084 */
5085 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
a9083016 5086 "QLE8XXX");
3a03eb79
AV
5087
5088 /* Use alternate WWN? */
5089 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
5090 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
5091 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
5092 }
5093
5094 /* Prepare nodename */
5095 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
5096 /*
5097 * Firmware will apply the following mask if the nodename was
5098 * not provided.
5099 */
5100 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
5101 icb->node_name[0] &= 0xF0;
5102 }
5103
5104 /* Set host adapter parameters. */
5105 ha->flags.disable_risc_code_load = 0;
5106 ha->flags.enable_lip_reset = 0;
5107 ha->flags.enable_lip_full_login =
5108 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
5109 ha->flags.enable_target_reset =
5110 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
5111 ha->flags.enable_led_scheme = 0;
5112 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
5113
5114 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
5115 (BIT_6 | BIT_5 | BIT_4)) >> 4;
5116
5117 /* save HBA serial number */
5118 ha->serial0 = icb->port_name[5];
5119 ha->serial1 = icb->port_name[6];
5120 ha->serial2 = icb->port_name[7];
5121 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
5122 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
5123
5124 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
5125
5126 ha->retry_count = le16_to_cpu(nv->login_retry_count);
5127
5128 /* Set minimum login_timeout to 4 seconds. */
5129 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
5130 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
5131 if (le16_to_cpu(nv->login_timeout) < 4)
5132 nv->login_timeout = __constant_cpu_to_le16(4);
5133 ha->login_timeout = le16_to_cpu(nv->login_timeout);
5134 icb->login_timeout = nv->login_timeout;
5135
5136 /* Set minimum RATOV to 100 tenths of a second. */
5137 ha->r_a_tov = 100;
5138
5139 ha->loop_reset_delay = nv->reset_delay;
5140
5141 /* Link Down Timeout = 0:
5142 *
5143 * When Port Down timer expires we will start returning
5144 * I/O's to OS with "DID_NO_CONNECT".
5145 *
5146 * Link Down Timeout != 0:
5147 *
5148 * The driver waits for the link to come up after link down
5149 * before returning I/Os to OS with "DID_NO_CONNECT".
5150 */
5151 if (le16_to_cpu(nv->link_down_timeout) == 0) {
5152 ha->loop_down_abort_time =
5153 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
5154 } else {
5155 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
5156 ha->loop_down_abort_time =
5157 (LOOP_DOWN_TIME - ha->link_down_timeout);
5158 }
5159
5160 /* Need enough time to try and get the port back. */
5161 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
5162 if (qlport_down_retry)
5163 ha->port_down_retry_count = qlport_down_retry;
5164
5165 /* Set login_retry_count */
5166 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
5167 if (ha->port_down_retry_count ==
5168 le16_to_cpu(nv->port_down_retry_count) &&
5169 ha->port_down_retry_count > 3)
5170 ha->login_retry_count = ha->port_down_retry_count;
5171 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
5172 ha->login_retry_count = ha->port_down_retry_count;
5173 if (ql2xloginretrycount)
5174 ha->login_retry_count = ql2xloginretrycount;
5175
5176 /* Enable ZIO. */
5177 if (!vha->flags.init_done) {
5178 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
5179 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
5180 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
5181 le16_to_cpu(icb->interrupt_delay_timer): 2;
5182 }
5183 icb->firmware_options_2 &= __constant_cpu_to_le32(
5184 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
5185 vha->flags.process_response_queue = 0;
5186 if (ha->zio_mode != QLA_ZIO_DISABLED) {
5187 ha->zio_mode = QLA_ZIO_MODE_6;
5188
5189 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
5190 "(%d us).\n", vha->host_no, ha->zio_mode,
5191 ha->zio_timer * 100));
5192 qla_printk(KERN_INFO, ha,
5193 "ZIO mode %d enabled; timer delay (%d us).\n",
5194 ha->zio_mode, ha->zio_timer * 100);
5195
5196 icb->firmware_options_2 |= cpu_to_le32(
5197 (uint32_t)ha->zio_mode);
5198 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
5199 vha->flags.process_response_queue = 1;
5200 }
5201
5202 if (rval) {
5203 DEBUG2_3(printk(KERN_WARNING
5204 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
5205 }
5206 return (rval);
5207}
5208
a9083016
GM
5209int
5210qla82xx_restart_isp(scsi_qla_host_t *vha)
5211{
5212 int status, rval;
5213 uint32_t wait_time;
5214 struct qla_hw_data *ha = vha->hw;
5215 struct req_que *req = ha->req_q_map[0];
5216 struct rsp_que *rsp = ha->rsp_q_map[0];
5217 struct scsi_qla_host *vp;
5218 struct scsi_qla_host *tvp;
5219
5220 status = qla2x00_init_rings(vha);
5221 if (!status) {
5222 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
5223 ha->flags.chip_reset_done = 1;
5224
5225 status = qla2x00_fw_ready(vha);
5226 if (!status) {
5227 qla_printk(KERN_INFO, ha,
5228 "%s(): Start configure loop, "
5229 "status = %d\n", __func__, status);
5230
5231 /* Issue a marker after FW becomes ready. */
5232 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
5233
5234 vha->flags.online = 1;
5235 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5236 wait_time = 256;
5237 do {
5238 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5239 qla2x00_configure_loop(vha);
5240 wait_time--;
5241 } while (!atomic_read(&vha->loop_down_timer) &&
5242 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags)) &&
5243 wait_time &&
5244 (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)));
5245 }
5246
5247 /* if no cable then assume it's good */
5248 if ((vha->device_flags & DFLG_NO_CABLE))
5249 status = 0;
5250
5251 qla_printk(KERN_INFO, ha,
5252 "%s(): Configure loop done, status = 0x%x\n",
5253 __func__, status);
5254 }
5255
5256 if (!status) {
5257 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
5258
5259 if (!atomic_read(&vha->loop_down_timer)) {
5260 /*
5261 * Issue marker command only when we are going
5262 * to start the I/O .
5263 */
5264 vha->marker_needed = 1;
5265 }
5266
5267 vha->flags.online = 1;
5268
5269 ha->isp_ops->enable_intrs(ha);
5270
5271 ha->isp_abort_cnt = 0;
5272 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5273
5274 if (ha->fce) {
5275 ha->flags.fce_enabled = 1;
5276 memset(ha->fce, 0,
5277 fce_calc_size(ha->fce_bufs));
5278 rval = qla2x00_enable_fce_trace(vha,
5279 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
5280 &ha->fce_bufs);
5281 if (rval) {
5282 qla_printk(KERN_WARNING, ha,
5283 "Unable to reinitialize FCE "
5284 "(%d).\n", rval);
5285 ha->flags.fce_enabled = 0;
5286 }
5287 }
5288
5289 if (ha->eft) {
5290 memset(ha->eft, 0, EFT_SIZE);
5291 rval = qla2x00_enable_eft_trace(vha,
5292 ha->eft_dma, EFT_NUM_BUFFERS);
5293 if (rval) {
5294 qla_printk(KERN_WARNING, ha,
5295 "Unable to reinitialize EFT "
5296 "(%d).\n", rval);
5297 }
5298 }
5299 } else { /* failed the ISP abort */
5300 vha->flags.online = 1;
5301 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
5302 if (ha->isp_abort_cnt == 0) {
5303 qla_printk(KERN_WARNING, ha,
5304 "ISP error recovery failed - "
5305 "board disabled\n");
5306 /*
5307 * The next call disables the board
5308 * completely.
5309 */
5310 ha->isp_ops->reset_adapter(vha);
5311 vha->flags.online = 0;
5312 clear_bit(ISP_ABORT_RETRY,
5313 &vha->dpc_flags);
5314 status = 0;
5315 } else { /* schedule another ISP abort */
5316 ha->isp_abort_cnt--;
5317 qla_printk(KERN_INFO, ha,
5318 "qla%ld: ISP abort - "
5319 "retry remaining %d\n",
5320 vha->host_no, ha->isp_abort_cnt);
5321 status = 1;
5322 }
5323 } else {
5324 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
5325 qla_printk(KERN_INFO, ha,
5326 "(%ld): ISP error recovery "
5327 "- retrying (%d) more times\n",
5328 vha->host_no, ha->isp_abort_cnt);
5329 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5330 status = 1;
5331 }
5332 }
5333
5334 if (!status) {
5335 DEBUG(printk(KERN_INFO
5336 "qla82xx_restart_isp(%ld): succeeded.\n",
5337 vha->host_no));
5338 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
5339 if (vp->vp_idx)
5340 qla2x00_vp_abort_isp(vp);
5341 }
5342 } else {
5343 qla_printk(KERN_INFO, ha,
5344 "qla82xx_restart_isp: **** FAILED ****\n");
5345 }
5346
5347 return status;
5348}
5349
3a03eb79 5350void
ae97c91e 5351qla81xx_update_fw_options(scsi_qla_host_t *vha)
3a03eb79 5352{
ae97c91e
AV
5353 struct qla_hw_data *ha = vha->hw;
5354
5355 if (!ql2xetsenable)
5356 return;
5357
5358 /* Enable ETS Burst. */
5359 memset(ha->fw_options, 0, sizeof(ha->fw_options));
5360 ha->fw_options[2] |= BIT_9;
5361 qla2x00_set_fw_options(vha, ha->fw_options);
3a03eb79 5362}
09ff701a
SR
5363
5364/*
5365 * qla24xx_get_fcp_prio
5366 * Gets the fcp cmd priority value for the logged in port.
5367 * Looks for a match of the port descriptors within
5368 * each of the fcp prio config entries. If a match is found,
5369 * the tag (priority) value is returned.
5370 *
5371 * Input:
5372 * ha = adapter block po
5373 * fcport = port structure pointer.
5374 *
5375 * Return:
6c452a45 5376 * non-zero (if found)
09ff701a
SR
5377 * 0 (if not found)
5378 *
5379 * Context:
5380 * Kernel context
5381 */
5382uint8_t
5383qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
5384{
5385 int i, entries;
5386 uint8_t pid_match, wwn_match;
5387 uint8_t priority;
5388 uint32_t pid1, pid2;
5389 uint64_t wwn1, wwn2;
5390 struct qla_fcp_prio_entry *pri_entry;
5391 struct qla_hw_data *ha = vha->hw;
5392
5393 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
5394 return 0;
5395
5396 priority = 0;
5397 entries = ha->fcp_prio_cfg->num_entries;
5398 pri_entry = &ha->fcp_prio_cfg->entry[0];
5399
5400 for (i = 0; i < entries; i++) {
5401 pid_match = wwn_match = 0;
5402
5403 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
5404 pri_entry++;
5405 continue;
5406 }
5407
5408 /* check source pid for a match */
5409 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
5410 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
5411 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
5412 if (pid1 == INVALID_PORT_ID)
5413 pid_match++;
5414 else if (pid1 == pid2)
5415 pid_match++;
5416 }
5417
5418 /* check destination pid for a match */
5419 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
5420 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
5421 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
5422 if (pid1 == INVALID_PORT_ID)
5423 pid_match++;
5424 else if (pid1 == pid2)
5425 pid_match++;
5426 }
5427
5428 /* check source WWN for a match */
5429 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
5430 wwn1 = wwn_to_u64(vha->port_name);
5431 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
5432 if (wwn2 == (uint64_t)-1)
5433 wwn_match++;
5434 else if (wwn1 == wwn2)
5435 wwn_match++;
5436 }
5437
5438 /* check destination WWN for a match */
5439 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
5440 wwn1 = wwn_to_u64(fcport->port_name);
5441 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
5442 if (wwn2 == (uint64_t)-1)
5443 wwn_match++;
5444 else if (wwn1 == wwn2)
5445 wwn_match++;
5446 }
5447
5448 if (pid_match == 2 || wwn_match == 2) {
5449 /* Found a matching entry */
5450 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
5451 priority = pri_entry->tag;
5452 break;
5453 }
5454
5455 pri_entry++;
5456 }
5457
5458 return priority;
5459}
5460
5461/*
5462 * qla24xx_update_fcport_fcp_prio
5463 * Activates fcp priority for the logged in fc port
5464 *
5465 * Input:
5466 * ha = adapter block pointer.
5467 * fcp = port structure pointer.
5468 *
5469 * Return:
5470 * QLA_SUCCESS or QLA_FUNCTION_FAILED
5471 *
5472 * Context:
5473 * Kernel context.
5474 */
5475int
5476qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *ha, fc_port_t *fcport)
5477{
5478 int ret;
5479 uint8_t priority;
5480 uint16_t mb[5];
5481
5482 if (atomic_read(&fcport->state) == FCS_UNCONFIGURED ||
5483 fcport->port_type != FCT_TARGET ||
5484 fcport->loop_id == FC_NO_LOOP_ID)
5485 return QLA_FUNCTION_FAILED;
5486
5487 priority = qla24xx_get_fcp_prio(ha, fcport);
5488 ret = qla24xx_set_fcp_prio(ha, fcport->loop_id, priority, mb);
5489 if (ret == QLA_SUCCESS)
5490 fcport->fcp_prio = priority;
5491 else
5492 DEBUG2(printk(KERN_WARNING
5493 "scsi(%ld): Unable to activate fcp priority, "
5494 " ret=0x%x\n", ha->host_no, ret));
5495
5496 return ret;
5497}
5498
5499/*
5500 * qla24xx_update_all_fcp_prio
5501 * Activates fcp priority for all the logged in ports
5502 *
5503 * Input:
5504 * ha = adapter block pointer.
5505 *
5506 * Return:
5507 * QLA_SUCCESS or QLA_FUNCTION_FAILED
5508 *
5509 * Context:
5510 * Kernel context.
5511 */
5512int
5513qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
5514{
5515 int ret;
5516 fc_port_t *fcport;
5517
5518 ret = QLA_FUNCTION_FAILED;
5519 /* We need to set priority for all logged in ports */
5520 list_for_each_entry(fcport, &vha->vp_fcports, list)
5521 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
5522
5523 return ret;
5524}