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