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