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