[SCSI] qla2xxx: Updates to ISP82xx support.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / qla2xxx / qla_init.c
blobab2cc71994c2d45a7c4e3e432a6deffea7c53c07
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
8 #include "qla_gbl.h"
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
14 #include "qla_devtbl.h"
16 #ifdef CONFIG_SPARC
17 #include <asm/prom.h>
18 #endif
21 * QLogic ISP2x00 Hardware Support Function Prototypes.
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 *);
36 static int qla2x00_restart_isp(scsi_qla_host_t *);
38 static int qla2x00_find_new_loop_id(scsi_qla_host_t *, fc_port_t *);
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 *);
44 /* SRB Extensions ---------------------------------------------------------- */
46 static void
47 qla2x00_ctx_sp_timeout(unsigned long __data)
49 srb_t *sp = (srb_t *)__data;
50 struct srb_ctx *ctx;
51 struct srb_iocb *iocb;
52 fc_port_t *fcport = sp->fcport;
53 struct qla_hw_data *ha = fcport->vha->hw;
54 struct req_que *req;
55 unsigned long flags;
57 spin_lock_irqsave(&ha->hardware_lock, flags);
58 req = ha->req_q_map[0];
59 req->outstanding_cmds[sp->handle] = NULL;
60 ctx = sp->ctx;
61 iocb = ctx->u.iocb_cmd;
62 iocb->timeout(sp);
63 spin_unlock_irqrestore(&ha->hardware_lock, flags);
65 iocb->free(sp);
68 void
69 qla2x00_ctx_sp_free(srb_t *sp)
71 struct srb_ctx *ctx = sp->ctx;
72 struct srb_iocb *iocb = ctx->u.iocb_cmd;
74 del_timer_sync(&iocb->timer);
75 kfree(iocb);
76 kfree(ctx);
77 mempool_free(sp, sp->fcport->vha->hw->srb_mempool);
80 inline srb_t *
81 qla2x00_get_ctx_sp(scsi_qla_host_t *vha, fc_port_t *fcport, size_t size,
82 unsigned long tmo)
84 srb_t *sp;
85 struct qla_hw_data *ha = vha->hw;
86 struct srb_ctx *ctx;
87 struct srb_iocb *iocb;
89 sp = mempool_alloc(ha->srb_mempool, GFP_KERNEL);
90 if (!sp)
91 goto done;
92 ctx = kzalloc(size, GFP_KERNEL);
93 if (!ctx) {
94 mempool_free(sp, ha->srb_mempool);
95 sp = NULL;
96 goto done;
98 iocb = kzalloc(sizeof(struct srb_iocb), GFP_KERNEL);
99 if (!iocb) {
100 mempool_free(sp, ha->srb_mempool);
101 sp = NULL;
102 kfree(ctx);
103 goto done;
106 memset(sp, 0, sizeof(*sp));
107 sp->fcport = fcport;
108 sp->ctx = ctx;
109 ctx->u.iocb_cmd = iocb;
110 iocb->free = qla2x00_ctx_sp_free;
112 init_timer(&iocb->timer);
113 if (!tmo)
114 goto done;
115 iocb->timer.expires = jiffies + tmo * HZ;
116 iocb->timer.data = (unsigned long)sp;
117 iocb->timer.function = qla2x00_ctx_sp_timeout;
118 add_timer(&iocb->timer);
119 done:
120 return sp;
123 /* Asynchronous Login/Logout Routines -------------------------------------- */
125 #define ELS_TMO_2_RATOV(ha) ((ha)->r_a_tov / 10 * 2)
127 static void
128 qla2x00_async_iocb_timeout(srb_t *sp)
130 fc_port_t *fcport = sp->fcport;
131 struct srb_ctx *ctx = sp->ctx;
133 DEBUG2(printk(KERN_WARNING
134 "scsi(%ld:%x): Async-%s timeout.\n",
135 fcport->vha->host_no, sp->handle, ctx->name));
137 fcport->flags &= ~FCF_ASYNC_SENT;
138 if (ctx->type == SRB_LOGIN_CMD)
139 qla2x00_post_async_logout_work(fcport->vha, fcport, NULL);
142 static void
143 qla2x00_async_login_ctx_done(srb_t *sp)
145 struct srb_ctx *ctx = sp->ctx;
146 struct srb_iocb *lio = ctx->u.iocb_cmd;
148 qla2x00_post_async_login_done_work(sp->fcport->vha, sp->fcport,
149 lio->u.logio.data);
150 lio->free(sp);
154 qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
155 uint16_t *data)
157 struct qla_hw_data *ha = vha->hw;
158 srb_t *sp;
159 struct srb_ctx *ctx;
160 struct srb_iocb *lio;
161 int rval;
163 rval = QLA_FUNCTION_FAILED;
164 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
165 ELS_TMO_2_RATOV(ha) + 2);
166 if (!sp)
167 goto done;
169 ctx = sp->ctx;
170 ctx->type = SRB_LOGIN_CMD;
171 ctx->name = "login";
172 lio = ctx->u.iocb_cmd;
173 lio->timeout = qla2x00_async_iocb_timeout;
174 lio->done = qla2x00_async_login_ctx_done;
175 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
176 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
177 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
178 rval = qla2x00_start_sp(sp);
179 if (rval != QLA_SUCCESS)
180 goto done_free_sp;
182 DEBUG2(printk(KERN_DEBUG
183 "scsi(%ld:%x): Async-login - loop-id=%x portid=%02x%02x%02x "
184 "retries=%d.\n", fcport->vha->host_no, sp->handle, fcport->loop_id,
185 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
186 fcport->login_retry));
187 return rval;
189 done_free_sp:
190 lio->free(sp);
191 done:
192 return rval;
195 static void
196 qla2x00_async_logout_ctx_done(srb_t *sp)
198 struct srb_ctx *ctx = sp->ctx;
199 struct srb_iocb *lio = ctx->u.iocb_cmd;
201 qla2x00_post_async_logout_done_work(sp->fcport->vha, sp->fcport,
202 lio->u.logio.data);
203 lio->free(sp);
207 qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
209 struct qla_hw_data *ha = vha->hw;
210 srb_t *sp;
211 struct srb_ctx *ctx;
212 struct srb_iocb *lio;
213 int rval;
215 rval = QLA_FUNCTION_FAILED;
216 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
217 ELS_TMO_2_RATOV(ha) + 2);
218 if (!sp)
219 goto done;
221 ctx = sp->ctx;
222 ctx->type = SRB_LOGOUT_CMD;
223 ctx->name = "logout";
224 lio = ctx->u.iocb_cmd;
225 lio->timeout = qla2x00_async_iocb_timeout;
226 lio->done = qla2x00_async_logout_ctx_done;
227 rval = qla2x00_start_sp(sp);
228 if (rval != QLA_SUCCESS)
229 goto done_free_sp;
231 DEBUG2(printk(KERN_DEBUG
232 "scsi(%ld:%x): Async-logout - loop-id=%x portid=%02x%02x%02x.\n",
233 fcport->vha->host_no, sp->handle, fcport->loop_id,
234 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa));
235 return rval;
237 done_free_sp:
238 lio->free(sp);
239 done:
240 return rval;
243 static void
244 qla2x00_async_adisc_ctx_done(srb_t *sp)
246 struct srb_ctx *ctx = sp->ctx;
247 struct srb_iocb *lio = ctx->u.iocb_cmd;
249 qla2x00_post_async_adisc_done_work(sp->fcport->vha, sp->fcport,
250 lio->u.logio.data);
251 lio->free(sp);
255 qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
256 uint16_t *data)
258 struct qla_hw_data *ha = vha->hw;
259 srb_t *sp;
260 struct srb_ctx *ctx;
261 struct srb_iocb *lio;
262 int rval;
264 rval = QLA_FUNCTION_FAILED;
265 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
266 ELS_TMO_2_RATOV(ha) + 2);
267 if (!sp)
268 goto done;
270 ctx = sp->ctx;
271 ctx->type = SRB_ADISC_CMD;
272 ctx->name = "adisc";
273 lio = ctx->u.iocb_cmd;
274 lio->timeout = qla2x00_async_iocb_timeout;
275 lio->done = qla2x00_async_adisc_ctx_done;
276 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
277 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
278 rval = qla2x00_start_sp(sp);
279 if (rval != QLA_SUCCESS)
280 goto done_free_sp;
282 DEBUG2(printk(KERN_DEBUG
283 "scsi(%ld:%x): Async-adisc - loop-id=%x portid=%02x%02x%02x.\n",
284 fcport->vha->host_no, sp->handle, fcport->loop_id,
285 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa));
287 return rval;
289 done_free_sp:
290 lio->free(sp);
291 done:
292 return rval;
295 static void
296 qla2x00_async_tm_cmd_ctx_done(srb_t *sp)
298 struct srb_ctx *ctx = sp->ctx;
299 struct srb_iocb *iocb = (struct srb_iocb *)ctx->u.iocb_cmd;
301 qla2x00_async_tm_cmd_done(sp->fcport->vha, sp->fcport, iocb);
302 iocb->free(sp);
306 qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
307 uint32_t tag)
309 struct scsi_qla_host *vha = fcport->vha;
310 struct qla_hw_data *ha = vha->hw;
311 srb_t *sp;
312 struct srb_ctx *ctx;
313 struct srb_iocb *tcf;
314 int rval;
316 rval = QLA_FUNCTION_FAILED;
317 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx),
318 ELS_TMO_2_RATOV(ha) + 2);
319 if (!sp)
320 goto done;
322 ctx = sp->ctx;
323 ctx->type = SRB_TM_CMD;
324 ctx->name = "tmf";
325 tcf = ctx->u.iocb_cmd;
326 tcf->u.tmf.flags = flags;
327 tcf->u.tmf.lun = lun;
328 tcf->u.tmf.data = tag;
329 tcf->timeout = qla2x00_async_iocb_timeout;
330 tcf->done = qla2x00_async_tm_cmd_ctx_done;
332 rval = qla2x00_start_sp(sp);
333 if (rval != QLA_SUCCESS)
334 goto done_free_sp;
336 DEBUG2(printk(KERN_DEBUG
337 "scsi(%ld:%x): Async-tmf - loop-id=%x portid=%02x%02x%02x.\n",
338 fcport->vha->host_no, sp->handle, fcport->loop_id,
339 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa));
341 return rval;
343 done_free_sp:
344 tcf->free(sp);
345 done:
346 return rval;
349 static void
350 qla2x00_async_marker_ctx_done(srb_t *sp)
352 struct srb_ctx *ctx = sp->ctx;
353 struct srb_iocb *iocb = (struct srb_iocb *)ctx->u.iocb_cmd;
355 qla2x00_async_marker_done(sp->fcport->vha, sp->fcport, iocb);
356 iocb->free(sp);
360 qla2x00_async_marker(fc_port_t *fcport, uint16_t lun, uint8_t modif)
362 struct scsi_qla_host *vha = fcport->vha;
363 srb_t *sp;
364 struct srb_ctx *ctx;
365 struct srb_iocb *mrk;
366 int rval;
368 rval = QLA_FUNCTION_FAILED;
369 sp = qla2x00_get_ctx_sp(vha, fcport, sizeof(struct srb_ctx), 0);
370 if (!sp)
371 goto done;
373 ctx = sp->ctx;
374 ctx->type = SRB_MARKER_CMD;
375 ctx->name = "marker";
376 mrk = ctx->u.iocb_cmd;
377 mrk->u.marker.lun = lun;
378 mrk->u.marker.modif = modif;
379 mrk->timeout = qla2x00_async_iocb_timeout;
380 mrk->done = qla2x00_async_marker_ctx_done;
382 rval = qla2x00_start_sp(sp);
383 if (rval != QLA_SUCCESS)
384 goto done_free_sp;
386 DEBUG2(printk(KERN_DEBUG
387 "scsi(%ld:%x): Async-marker - loop-id=%x "
388 "portid=%02x%02x%02x.\n",
389 fcport->vha->host_no, sp->handle, fcport->loop_id,
390 fcport->d_id.b.domain, fcport->d_id.b.area,
391 fcport->d_id.b.al_pa));
393 return rval;
395 done_free_sp:
396 mrk->free(sp);
397 done:
398 return rval;
401 void
402 qla2x00_async_login_done(struct scsi_qla_host *vha, fc_port_t *fcport,
403 uint16_t *data)
405 int rval;
407 switch (data[0]) {
408 case MBS_COMMAND_COMPLETE:
409 if (fcport->flags & FCF_FCP2_DEVICE) {
410 fcport->flags |= FCF_ASYNC_SENT;
411 qla2x00_post_async_adisc_work(vha, fcport, data);
412 break;
414 qla2x00_update_fcport(vha, fcport);
415 break;
416 case MBS_COMMAND_ERROR:
417 fcport->flags &= ~FCF_ASYNC_SENT;
418 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
419 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
420 else
421 qla2x00_mark_device_lost(vha, fcport, 1, 0);
422 break;
423 case MBS_PORT_ID_USED:
424 fcport->loop_id = data[1];
425 qla2x00_post_async_login_work(vha, fcport, NULL);
426 break;
427 case MBS_LOOP_ID_USED:
428 fcport->loop_id++;
429 rval = qla2x00_find_new_loop_id(vha, fcport);
430 if (rval != QLA_SUCCESS) {
431 fcport->flags &= ~FCF_ASYNC_SENT;
432 qla2x00_mark_device_lost(vha, fcport, 1, 0);
433 break;
435 qla2x00_post_async_login_work(vha, fcport, NULL);
436 break;
438 return;
441 void
442 qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
443 uint16_t *data)
445 qla2x00_mark_device_lost(vha, fcport, 1, 0);
446 return;
449 void
450 qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
451 uint16_t *data)
453 if (data[0] == MBS_COMMAND_COMPLETE) {
454 qla2x00_update_fcport(vha, fcport);
456 return;
459 /* Retry login. */
460 fcport->flags &= ~FCF_ASYNC_SENT;
461 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
462 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
463 else
464 qla2x00_mark_device_lost(vha, fcport, 1, 0);
466 return;
469 void
470 qla2x00_async_tm_cmd_done(struct scsi_qla_host *vha, fc_port_t *fcport,
471 struct srb_iocb *iocb)
473 int rval;
474 uint32_t flags;
475 uint16_t lun;
477 flags = iocb->u.tmf.flags;
478 lun = (uint16_t)iocb->u.tmf.lun;
480 /* Issue Marker IOCB */
481 rval = qla2x00_async_marker(fcport, lun,
482 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
484 if ((rval != QLA_SUCCESS) || iocb->u.tmf.data) {
485 DEBUG2_3_11(printk(KERN_WARNING
486 "%s(%ld): TM IOCB failed (%x).\n",
487 __func__, vha->host_no, rval));
490 return;
493 void
494 qla2x00_async_marker_done(struct scsi_qla_host *vha, fc_port_t *fcport,
495 struct srb_iocb *iocb)
498 * Currently we dont have any specific post response processing
499 * for this IOCB. We'll just return success or failed
500 * depending on whether the IOCB command succeeded or failed.
502 if (iocb->u.tmf.data) {
503 DEBUG2_3_11(printk(KERN_WARNING
504 "%s(%ld): Marker IOCB failed (%x).\n",
505 __func__, vha->host_no, iocb->u.tmf.data));
508 return;
511 /****************************************************************************/
512 /* QLogic ISP2x00 Hardware Support Functions. */
513 /****************************************************************************/
516 * qla2x00_initialize_adapter
517 * Initialize board.
519 * Input:
520 * ha = adapter block pointer.
522 * Returns:
523 * 0 = success
526 qla2x00_initialize_adapter(scsi_qla_host_t *vha)
528 int rval;
529 struct qla_hw_data *ha = vha->hw;
530 struct req_que *req = ha->req_q_map[0];
532 /* Clear adapter flags. */
533 vha->flags.online = 0;
534 ha->flags.chip_reset_done = 0;
535 vha->flags.reset_active = 0;
536 ha->flags.pci_channel_io_perm_failure = 0;
537 ha->flags.eeh_busy = 0;
538 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
539 atomic_set(&vha->loop_state, LOOP_DOWN);
540 vha->device_flags = DFLG_NO_CABLE;
541 vha->dpc_flags = 0;
542 vha->flags.management_server_logged_in = 0;
543 vha->marker_needed = 0;
544 ha->isp_abort_cnt = 0;
545 ha->beacon_blink_led = 0;
547 set_bit(0, ha->req_qid_map);
548 set_bit(0, ha->rsp_qid_map);
550 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
551 rval = ha->isp_ops->pci_config(vha);
552 if (rval) {
553 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
554 vha->host_no));
555 return (rval);
558 ha->isp_ops->reset_chip(vha);
560 rval = qla2xxx_get_flash_info(vha);
561 if (rval) {
562 DEBUG2(printk("scsi(%ld): Unable to validate FLASH data.\n",
563 vha->host_no));
564 return (rval);
567 ha->isp_ops->get_flash_version(vha, req->ring);
569 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
571 ha->isp_ops->nvram_config(vha);
573 if (ha->flags.disable_serdes) {
574 /* Mask HBA via NVRAM settings? */
575 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
576 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
577 vha->port_name[0], vha->port_name[1],
578 vha->port_name[2], vha->port_name[3],
579 vha->port_name[4], vha->port_name[5],
580 vha->port_name[6], vha->port_name[7]);
581 return QLA_FUNCTION_FAILED;
584 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
586 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
587 rval = ha->isp_ops->chip_diag(vha);
588 if (rval)
589 return (rval);
590 rval = qla2x00_setup_chip(vha);
591 if (rval)
592 return (rval);
595 if (IS_QLA84XX(ha)) {
596 ha->cs84xx = qla84xx_get_chip(vha);
597 if (!ha->cs84xx) {
598 qla_printk(KERN_ERR, ha,
599 "Unable to configure ISP84XX.\n");
600 return QLA_FUNCTION_FAILED;
603 rval = qla2x00_init_rings(vha);
604 ha->flags.chip_reset_done = 1;
606 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
607 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
608 rval = qla84xx_init_chip(vha);
609 if (rval != QLA_SUCCESS) {
610 qla_printk(KERN_ERR, ha,
611 "Unable to initialize ISP84XX.\n");
612 qla84xx_put_chip(vha);
616 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha)) {
617 if (qla24xx_read_fcp_prio_cfg(vha))
618 qla_printk(KERN_ERR, ha,
619 "Unable to read FCP priority data.\n");
622 return (rval);
626 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
627 * @ha: HA context
629 * Returns 0 on success.
632 qla2100_pci_config(scsi_qla_host_t *vha)
634 uint16_t w;
635 unsigned long flags;
636 struct qla_hw_data *ha = vha->hw;
637 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
639 pci_set_master(ha->pdev);
640 pci_try_set_mwi(ha->pdev);
642 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
643 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
644 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
646 pci_disable_rom(ha->pdev);
648 /* Get PCI bus information. */
649 spin_lock_irqsave(&ha->hardware_lock, flags);
650 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
651 spin_unlock_irqrestore(&ha->hardware_lock, flags);
653 return QLA_SUCCESS;
657 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
658 * @ha: HA context
660 * Returns 0 on success.
663 qla2300_pci_config(scsi_qla_host_t *vha)
665 uint16_t w;
666 unsigned long flags = 0;
667 uint32_t cnt;
668 struct qla_hw_data *ha = vha->hw;
669 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
671 pci_set_master(ha->pdev);
672 pci_try_set_mwi(ha->pdev);
674 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
675 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
677 if (IS_QLA2322(ha) || IS_QLA6322(ha))
678 w &= ~PCI_COMMAND_INTX_DISABLE;
679 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
682 * If this is a 2300 card and not 2312, reset the
683 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
684 * the 2310 also reports itself as a 2300 so we need to get the
685 * fb revision level -- a 6 indicates it really is a 2300 and
686 * not a 2310.
688 if (IS_QLA2300(ha)) {
689 spin_lock_irqsave(&ha->hardware_lock, flags);
691 /* Pause RISC. */
692 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
693 for (cnt = 0; cnt < 30000; cnt++) {
694 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
695 break;
697 udelay(10);
700 /* Select FPM registers. */
701 WRT_REG_WORD(&reg->ctrl_status, 0x20);
702 RD_REG_WORD(&reg->ctrl_status);
704 /* Get the fb rev level */
705 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
707 if (ha->fb_rev == FPM_2300)
708 pci_clear_mwi(ha->pdev);
710 /* Deselect FPM registers. */
711 WRT_REG_WORD(&reg->ctrl_status, 0x0);
712 RD_REG_WORD(&reg->ctrl_status);
714 /* Release RISC module. */
715 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
716 for (cnt = 0; cnt < 30000; cnt++) {
717 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
718 break;
720 udelay(10);
723 spin_unlock_irqrestore(&ha->hardware_lock, flags);
726 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
728 pci_disable_rom(ha->pdev);
730 /* Get PCI bus information. */
731 spin_lock_irqsave(&ha->hardware_lock, flags);
732 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
733 spin_unlock_irqrestore(&ha->hardware_lock, flags);
735 return QLA_SUCCESS;
739 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
740 * @ha: HA context
742 * Returns 0 on success.
745 qla24xx_pci_config(scsi_qla_host_t *vha)
747 uint16_t w;
748 unsigned long flags = 0;
749 struct qla_hw_data *ha = vha->hw;
750 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
752 pci_set_master(ha->pdev);
753 pci_try_set_mwi(ha->pdev);
755 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
756 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
757 w &= ~PCI_COMMAND_INTX_DISABLE;
758 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
760 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
762 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
763 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
764 pcix_set_mmrbc(ha->pdev, 2048);
766 /* PCIe -- adjust Maximum Read Request Size (2048). */
767 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
768 pcie_set_readrq(ha->pdev, 2048);
770 pci_disable_rom(ha->pdev);
772 ha->chip_revision = ha->pdev->revision;
774 /* Get PCI bus information. */
775 spin_lock_irqsave(&ha->hardware_lock, flags);
776 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
777 spin_unlock_irqrestore(&ha->hardware_lock, flags);
779 return QLA_SUCCESS;
783 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
784 * @ha: HA context
786 * Returns 0 on success.
789 qla25xx_pci_config(scsi_qla_host_t *vha)
791 uint16_t w;
792 struct qla_hw_data *ha = vha->hw;
794 pci_set_master(ha->pdev);
795 pci_try_set_mwi(ha->pdev);
797 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
798 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
799 w &= ~PCI_COMMAND_INTX_DISABLE;
800 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
802 /* PCIe -- adjust Maximum Read Request Size (2048). */
803 if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
804 pcie_set_readrq(ha->pdev, 2048);
806 pci_disable_rom(ha->pdev);
808 ha->chip_revision = ha->pdev->revision;
810 return QLA_SUCCESS;
814 * qla2x00_isp_firmware() - Choose firmware image.
815 * @ha: HA context
817 * Returns 0 on success.
819 static int
820 qla2x00_isp_firmware(scsi_qla_host_t *vha)
822 int rval;
823 uint16_t loop_id, topo, sw_cap;
824 uint8_t domain, area, al_pa;
825 struct qla_hw_data *ha = vha->hw;
827 /* Assume loading risc code */
828 rval = QLA_FUNCTION_FAILED;
830 if (ha->flags.disable_risc_code_load) {
831 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
832 vha->host_no));
833 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
835 /* Verify checksum of loaded RISC code. */
836 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
837 if (rval == QLA_SUCCESS) {
838 /* And, verify we are not in ROM code. */
839 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
840 &area, &domain, &topo, &sw_cap);
844 if (rval) {
845 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
846 vha->host_no));
849 return (rval);
853 * qla2x00_reset_chip() - Reset ISP chip.
854 * @ha: HA context
856 * Returns 0 on success.
858 void
859 qla2x00_reset_chip(scsi_qla_host_t *vha)
861 unsigned long flags = 0;
862 struct qla_hw_data *ha = vha->hw;
863 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
864 uint32_t cnt;
865 uint16_t cmd;
867 if (unlikely(pci_channel_offline(ha->pdev)))
868 return;
870 ha->isp_ops->disable_intrs(ha);
872 spin_lock_irqsave(&ha->hardware_lock, flags);
874 /* Turn off master enable */
875 cmd = 0;
876 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
877 cmd &= ~PCI_COMMAND_MASTER;
878 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
880 if (!IS_QLA2100(ha)) {
881 /* Pause RISC. */
882 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
883 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
884 for (cnt = 0; cnt < 30000; cnt++) {
885 if ((RD_REG_WORD(&reg->hccr) &
886 HCCR_RISC_PAUSE) != 0)
887 break;
888 udelay(100);
890 } else {
891 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
892 udelay(10);
895 /* Select FPM registers. */
896 WRT_REG_WORD(&reg->ctrl_status, 0x20);
897 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
899 /* FPM Soft Reset. */
900 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
901 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
903 /* Toggle Fpm Reset. */
904 if (!IS_QLA2200(ha)) {
905 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
906 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
909 /* Select frame buffer registers. */
910 WRT_REG_WORD(&reg->ctrl_status, 0x10);
911 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
913 /* Reset frame buffer FIFOs. */
914 if (IS_QLA2200(ha)) {
915 WRT_FB_CMD_REG(ha, reg, 0xa000);
916 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
917 } else {
918 WRT_FB_CMD_REG(ha, reg, 0x00fc);
920 /* Read back fb_cmd until zero or 3 seconds max */
921 for (cnt = 0; cnt < 3000; cnt++) {
922 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
923 break;
924 udelay(100);
928 /* Select RISC module registers. */
929 WRT_REG_WORD(&reg->ctrl_status, 0);
930 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
932 /* Reset RISC processor. */
933 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
934 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
936 /* Release RISC processor. */
937 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
938 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
941 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
942 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
944 /* Reset ISP chip. */
945 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
947 /* Wait for RISC to recover from reset. */
948 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
950 * It is necessary to for a delay here since the card doesn't
951 * respond to PCI reads during a reset. On some architectures
952 * this will result in an MCA.
954 udelay(20);
955 for (cnt = 30000; cnt; cnt--) {
956 if ((RD_REG_WORD(&reg->ctrl_status) &
957 CSR_ISP_SOFT_RESET) == 0)
958 break;
959 udelay(100);
961 } else
962 udelay(10);
964 /* Reset RISC processor. */
965 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
967 WRT_REG_WORD(&reg->semaphore, 0);
969 /* Release RISC processor. */
970 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
971 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
973 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
974 for (cnt = 0; cnt < 30000; cnt++) {
975 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
976 break;
978 udelay(100);
980 } else
981 udelay(100);
983 /* Turn on master enable */
984 cmd |= PCI_COMMAND_MASTER;
985 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
987 /* Disable RISC pause on FPM parity error. */
988 if (!IS_QLA2100(ha)) {
989 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
990 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
993 spin_unlock_irqrestore(&ha->hardware_lock, flags);
997 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
998 * @ha: HA context
1000 * Returns 0 on success.
1002 static inline void
1003 qla24xx_reset_risc(scsi_qla_host_t *vha)
1005 unsigned long flags = 0;
1006 struct qla_hw_data *ha = vha->hw;
1007 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1008 uint32_t cnt, d2;
1009 uint16_t wd;
1011 spin_lock_irqsave(&ha->hardware_lock, flags);
1013 /* Reset RISC. */
1014 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
1015 for (cnt = 0; cnt < 30000; cnt++) {
1016 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
1017 break;
1019 udelay(10);
1022 WRT_REG_DWORD(&reg->ctrl_status,
1023 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
1024 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
1026 udelay(100);
1027 /* Wait for firmware to complete NVRAM accesses. */
1028 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1029 for (cnt = 10000 ; cnt && d2; cnt--) {
1030 udelay(5);
1031 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1032 barrier();
1035 /* Wait for soft-reset to complete. */
1036 d2 = RD_REG_DWORD(&reg->ctrl_status);
1037 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
1038 udelay(5);
1039 d2 = RD_REG_DWORD(&reg->ctrl_status);
1040 barrier();
1043 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
1044 RD_REG_DWORD(&reg->hccr);
1046 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
1047 RD_REG_DWORD(&reg->hccr);
1049 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
1050 RD_REG_DWORD(&reg->hccr);
1052 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1053 for (cnt = 6000000 ; cnt && d2; cnt--) {
1054 udelay(5);
1055 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
1056 barrier();
1059 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1061 if (IS_NOPOLLING_TYPE(ha))
1062 ha->isp_ops->enable_intrs(ha);
1066 * qla24xx_reset_chip() - Reset ISP24xx chip.
1067 * @ha: HA context
1069 * Returns 0 on success.
1071 void
1072 qla24xx_reset_chip(scsi_qla_host_t *vha)
1074 struct qla_hw_data *ha = vha->hw;
1076 if (pci_channel_offline(ha->pdev) &&
1077 ha->flags.pci_channel_io_perm_failure) {
1078 return;
1081 ha->isp_ops->disable_intrs(ha);
1083 /* Perform RISC reset. */
1084 qla24xx_reset_risc(vha);
1088 * qla2x00_chip_diag() - Test chip for proper operation.
1089 * @ha: HA context
1091 * Returns 0 on success.
1094 qla2x00_chip_diag(scsi_qla_host_t *vha)
1096 int rval;
1097 struct qla_hw_data *ha = vha->hw;
1098 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1099 unsigned long flags = 0;
1100 uint16_t data;
1101 uint32_t cnt;
1102 uint16_t mb[5];
1103 struct req_que *req = ha->req_q_map[0];
1105 /* Assume a failed state */
1106 rval = QLA_FUNCTION_FAILED;
1108 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
1109 vha->host_no, (u_long)&reg->flash_address));
1111 spin_lock_irqsave(&ha->hardware_lock, flags);
1113 /* Reset ISP chip. */
1114 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
1117 * We need to have a delay here since the card will not respond while
1118 * in reset causing an MCA on some architectures.
1120 udelay(20);
1121 data = qla2x00_debounce_register(&reg->ctrl_status);
1122 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
1123 udelay(5);
1124 data = RD_REG_WORD(&reg->ctrl_status);
1125 barrier();
1128 if (!cnt)
1129 goto chip_diag_failed;
1131 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
1132 vha->host_no));
1134 /* Reset RISC processor. */
1135 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
1136 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
1138 /* Workaround for QLA2312 PCI parity error */
1139 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
1140 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
1141 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
1142 udelay(5);
1143 data = RD_MAILBOX_REG(ha, reg, 0);
1144 barrier();
1146 } else
1147 udelay(10);
1149 if (!cnt)
1150 goto chip_diag_failed;
1152 /* Check product ID of chip */
1153 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", vha->host_no));
1155 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
1156 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
1157 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
1158 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
1159 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
1160 mb[3] != PROD_ID_3) {
1161 qla_printk(KERN_WARNING, ha,
1162 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
1164 goto chip_diag_failed;
1166 ha->product_id[0] = mb[1];
1167 ha->product_id[1] = mb[2];
1168 ha->product_id[2] = mb[3];
1169 ha->product_id[3] = mb[4];
1171 /* Adjust fw RISC transfer size */
1172 if (req->length > 1024)
1173 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
1174 else
1175 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
1176 req->length;
1178 if (IS_QLA2200(ha) &&
1179 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
1180 /* Limit firmware transfer size with a 2200A */
1181 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
1182 vha->host_no));
1184 ha->device_type |= DT_ISP2200A;
1185 ha->fw_transfer_size = 128;
1188 /* Wrap Incoming Mailboxes Test. */
1189 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1191 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", vha->host_no));
1192 rval = qla2x00_mbx_reg_test(vha);
1193 if (rval) {
1194 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
1195 vha->host_no));
1196 qla_printk(KERN_WARNING, ha,
1197 "Failed mailbox send register test\n");
1199 else {
1200 /* Flag a successful rval */
1201 rval = QLA_SUCCESS;
1203 spin_lock_irqsave(&ha->hardware_lock, flags);
1205 chip_diag_failed:
1206 if (rval)
1207 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
1208 "****\n", vha->host_no));
1210 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1212 return (rval);
1216 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
1217 * @ha: HA context
1219 * Returns 0 on success.
1222 qla24xx_chip_diag(scsi_qla_host_t *vha)
1224 int rval;
1225 struct qla_hw_data *ha = vha->hw;
1226 struct req_que *req = ha->req_q_map[0];
1228 if (IS_QLA82XX(ha))
1229 return QLA_SUCCESS;
1231 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
1233 rval = qla2x00_mbx_reg_test(vha);
1234 if (rval) {
1235 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
1236 vha->host_no));
1237 qla_printk(KERN_WARNING, ha,
1238 "Failed mailbox send register test\n");
1239 } else {
1240 /* Flag a successful rval */
1241 rval = QLA_SUCCESS;
1244 return rval;
1247 void
1248 qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
1250 int rval;
1251 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
1252 eft_size, fce_size, mq_size;
1253 dma_addr_t tc_dma;
1254 void *tc;
1255 struct qla_hw_data *ha = vha->hw;
1256 struct req_que *req = ha->req_q_map[0];
1257 struct rsp_que *rsp = ha->rsp_q_map[0];
1259 if (ha->fw_dump) {
1260 qla_printk(KERN_WARNING, ha,
1261 "Firmware dump previously allocated.\n");
1262 return;
1265 ha->fw_dumped = 0;
1266 fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
1267 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1268 fixed_size = sizeof(struct qla2100_fw_dump);
1269 } else if (IS_QLA23XX(ha)) {
1270 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
1271 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
1272 sizeof(uint16_t);
1273 } else if (IS_FWI2_CAPABLE(ha)) {
1274 if (IS_QLA81XX(ha))
1275 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
1276 else if (IS_QLA25XX(ha))
1277 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
1278 else
1279 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
1280 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
1281 sizeof(uint32_t);
1282 if (ha->mqenable)
1283 mq_size = sizeof(struct qla2xxx_mq_chain);
1284 /* Allocate memory for Fibre Channel Event Buffer. */
1285 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha))
1286 goto try_eft;
1288 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
1289 GFP_KERNEL);
1290 if (!tc) {
1291 qla_printk(KERN_WARNING, ha, "Unable to allocate "
1292 "(%d KB) for FCE.\n", FCE_SIZE / 1024);
1293 goto try_eft;
1296 memset(tc, 0, FCE_SIZE);
1297 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
1298 ha->fce_mb, &ha->fce_bufs);
1299 if (rval) {
1300 qla_printk(KERN_WARNING, ha, "Unable to initialize "
1301 "FCE (%d).\n", rval);
1302 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
1303 tc_dma);
1304 ha->flags.fce_enabled = 0;
1305 goto try_eft;
1308 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
1309 FCE_SIZE / 1024);
1311 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
1312 ha->flags.fce_enabled = 1;
1313 ha->fce_dma = tc_dma;
1314 ha->fce = tc;
1315 try_eft:
1316 /* Allocate memory for Extended Trace Buffer. */
1317 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
1318 GFP_KERNEL);
1319 if (!tc) {
1320 qla_printk(KERN_WARNING, ha, "Unable to allocate "
1321 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
1322 goto cont_alloc;
1325 memset(tc, 0, EFT_SIZE);
1326 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
1327 if (rval) {
1328 qla_printk(KERN_WARNING, ha, "Unable to initialize "
1329 "EFT (%d).\n", rval);
1330 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
1331 tc_dma);
1332 goto cont_alloc;
1335 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
1336 EFT_SIZE / 1024);
1338 eft_size = EFT_SIZE;
1339 ha->eft_dma = tc_dma;
1340 ha->eft = tc;
1342 cont_alloc:
1343 req_q_size = req->length * sizeof(request_t);
1344 rsp_q_size = rsp->length * sizeof(response_t);
1346 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
1347 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
1348 ha->chain_offset = dump_size;
1349 dump_size += mq_size + fce_size;
1351 ha->fw_dump = vmalloc(dump_size);
1352 if (!ha->fw_dump) {
1353 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
1354 "firmware dump!!!\n", dump_size / 1024);
1356 if (ha->eft) {
1357 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
1358 ha->eft_dma);
1359 ha->eft = NULL;
1360 ha->eft_dma = 0;
1362 return;
1364 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
1365 dump_size / 1024);
1367 ha->fw_dump_len = dump_size;
1368 ha->fw_dump->signature[0] = 'Q';
1369 ha->fw_dump->signature[1] = 'L';
1370 ha->fw_dump->signature[2] = 'G';
1371 ha->fw_dump->signature[3] = 'C';
1372 ha->fw_dump->version = __constant_htonl(1);
1374 ha->fw_dump->fixed_size = htonl(fixed_size);
1375 ha->fw_dump->mem_size = htonl(mem_size);
1376 ha->fw_dump->req_q_size = htonl(req_q_size);
1377 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
1379 ha->fw_dump->eft_size = htonl(eft_size);
1380 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
1381 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
1383 ha->fw_dump->header_size =
1384 htonl(offsetof(struct qla2xxx_fw_dump, isp));
1387 static int
1388 qla81xx_mpi_sync(scsi_qla_host_t *vha)
1390 #define MPS_MASK 0xe0
1391 int rval;
1392 uint16_t dc;
1393 uint32_t dw;
1394 struct qla_hw_data *ha = vha->hw;
1396 if (!IS_QLA81XX(vha->hw))
1397 return QLA_SUCCESS;
1399 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
1400 if (rval != QLA_SUCCESS) {
1401 DEBUG2(qla_printk(KERN_WARNING, ha,
1402 "Sync-MPI: Unable to acquire semaphore.\n"));
1403 goto done;
1406 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
1407 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
1408 if (rval != QLA_SUCCESS) {
1409 DEBUG2(qla_printk(KERN_WARNING, ha,
1410 "Sync-MPI: Unable to read sync.\n"));
1411 goto done_release;
1414 dc &= MPS_MASK;
1415 if (dc == (dw & MPS_MASK))
1416 goto done_release;
1418 dw &= ~MPS_MASK;
1419 dw |= dc;
1420 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
1421 if (rval != QLA_SUCCESS) {
1422 DEBUG2(qla_printk(KERN_WARNING, ha,
1423 "Sync-MPI: Unable to gain sync.\n"));
1426 done_release:
1427 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
1428 if (rval != QLA_SUCCESS) {
1429 DEBUG2(qla_printk(KERN_WARNING, ha,
1430 "Sync-MPI: Unable to release semaphore.\n"));
1433 done:
1434 return rval;
1438 * qla2x00_setup_chip() - Load and start RISC firmware.
1439 * @ha: HA context
1441 * Returns 0 on success.
1443 static int
1444 qla2x00_setup_chip(scsi_qla_host_t *vha)
1446 int rval;
1447 uint32_t srisc_address = 0;
1448 struct qla_hw_data *ha = vha->hw;
1449 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1450 unsigned long flags;
1451 uint16_t fw_major_version;
1453 if (IS_QLA82XX(ha)) {
1454 rval = ha->isp_ops->load_risc(vha, &srisc_address);
1455 if (rval == QLA_SUCCESS)
1456 goto enable_82xx_npiv;
1459 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1460 /* Disable SRAM, Instruction RAM and GP RAM parity. */
1461 spin_lock_irqsave(&ha->hardware_lock, flags);
1462 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
1463 RD_REG_WORD(&reg->hccr);
1464 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1467 qla81xx_mpi_sync(vha);
1469 /* Load firmware sequences */
1470 rval = ha->isp_ops->load_risc(vha, &srisc_address);
1471 if (rval == QLA_SUCCESS) {
1472 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
1473 "code.\n", vha->host_no));
1475 rval = qla2x00_verify_checksum(vha, srisc_address);
1476 if (rval == QLA_SUCCESS) {
1477 /* Start firmware execution. */
1478 DEBUG(printk("scsi(%ld): Checksum OK, start "
1479 "firmware.\n", vha->host_no));
1481 rval = qla2x00_execute_fw(vha, srisc_address);
1482 /* Retrieve firmware information. */
1483 if (rval == QLA_SUCCESS) {
1484 enable_82xx_npiv:
1485 fw_major_version = ha->fw_major_version;
1486 rval = qla2x00_get_fw_version(vha,
1487 &ha->fw_major_version,
1488 &ha->fw_minor_version,
1489 &ha->fw_subminor_version,
1490 &ha->fw_attributes, &ha->fw_memory_size,
1491 ha->mpi_version, &ha->mpi_capabilities,
1492 ha->phy_version);
1493 if (rval != QLA_SUCCESS)
1494 goto failed;
1495 ha->flags.npiv_supported = 0;
1496 if (IS_QLA2XXX_MIDTYPE(ha) &&
1497 (ha->fw_attributes & BIT_2)) {
1498 ha->flags.npiv_supported = 1;
1499 if ((!ha->max_npiv_vports) ||
1500 ((ha->max_npiv_vports + 1) %
1501 MIN_MULTI_ID_FABRIC))
1502 ha->max_npiv_vports =
1503 MIN_MULTI_ID_FABRIC - 1;
1505 qla2x00_get_resource_cnts(vha, NULL,
1506 &ha->fw_xcb_count, NULL, NULL,
1507 &ha->max_npiv_vports, NULL);
1509 if (!fw_major_version && ql2xallocfwdump) {
1510 if (!IS_QLA82XX(ha))
1511 qla2x00_alloc_fw_dump(vha);
1514 } else {
1515 DEBUG2(printk(KERN_INFO
1516 "scsi(%ld): ISP Firmware failed checksum.\n",
1517 vha->host_no));
1521 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1522 /* Enable proper parity. */
1523 spin_lock_irqsave(&ha->hardware_lock, flags);
1524 if (IS_QLA2300(ha))
1525 /* SRAM parity */
1526 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
1527 else
1528 /* SRAM, Instruction RAM and GP RAM parity */
1529 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
1530 RD_REG_WORD(&reg->hccr);
1531 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1534 if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
1535 uint32_t size;
1537 rval = qla81xx_fac_get_sector_size(vha, &size);
1538 if (rval == QLA_SUCCESS) {
1539 ha->flags.fac_supported = 1;
1540 ha->fdt_block_size = size << 2;
1541 } else {
1542 qla_printk(KERN_ERR, ha,
1543 "Unsupported FAC firmware (%d.%02d.%02d).\n",
1544 ha->fw_major_version, ha->fw_minor_version,
1545 ha->fw_subminor_version);
1548 failed:
1549 if (rval) {
1550 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
1551 vha->host_no));
1554 return (rval);
1558 * qla2x00_init_response_q_entries() - Initializes response queue entries.
1559 * @ha: HA context
1561 * Beginning of request ring has initialization control block already built
1562 * by nvram config routine.
1564 * Returns 0 on success.
1566 void
1567 qla2x00_init_response_q_entries(struct rsp_que *rsp)
1569 uint16_t cnt;
1570 response_t *pkt;
1572 rsp->ring_ptr = rsp->ring;
1573 rsp->ring_index = 0;
1574 rsp->status_srb = NULL;
1575 pkt = rsp->ring_ptr;
1576 for (cnt = 0; cnt < rsp->length; cnt++) {
1577 pkt->signature = RESPONSE_PROCESSED;
1578 pkt++;
1583 * qla2x00_update_fw_options() - Read and process firmware options.
1584 * @ha: HA context
1586 * Returns 0 on success.
1588 void
1589 qla2x00_update_fw_options(scsi_qla_host_t *vha)
1591 uint16_t swing, emphasis, tx_sens, rx_sens;
1592 struct qla_hw_data *ha = vha->hw;
1594 memset(ha->fw_options, 0, sizeof(ha->fw_options));
1595 qla2x00_get_fw_options(vha, ha->fw_options);
1597 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1598 return;
1600 /* Serial Link options. */
1601 DEBUG3(printk("scsi(%ld): Serial link options:\n",
1602 vha->host_no));
1603 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1604 sizeof(ha->fw_seriallink_options)));
1606 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1607 if (ha->fw_seriallink_options[3] & BIT_2) {
1608 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1610 /* 1G settings */
1611 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1612 emphasis = (ha->fw_seriallink_options[2] &
1613 (BIT_4 | BIT_3)) >> 3;
1614 tx_sens = ha->fw_seriallink_options[0] &
1615 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1616 rx_sens = (ha->fw_seriallink_options[0] &
1617 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1618 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1619 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1620 if (rx_sens == 0x0)
1621 rx_sens = 0x3;
1622 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1623 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1624 ha->fw_options[10] |= BIT_5 |
1625 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1626 (tx_sens & (BIT_1 | BIT_0));
1628 /* 2G settings */
1629 swing = (ha->fw_seriallink_options[2] &
1630 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1631 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1632 tx_sens = ha->fw_seriallink_options[1] &
1633 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1634 rx_sens = (ha->fw_seriallink_options[1] &
1635 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1636 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1637 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1638 if (rx_sens == 0x0)
1639 rx_sens = 0x3;
1640 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1641 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1642 ha->fw_options[11] |= BIT_5 |
1643 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1644 (tx_sens & (BIT_1 | BIT_0));
1647 /* FCP2 options. */
1648 /* Return command IOCBs without waiting for an ABTS to complete. */
1649 ha->fw_options[3] |= BIT_13;
1651 /* LED scheme. */
1652 if (ha->flags.enable_led_scheme)
1653 ha->fw_options[2] |= BIT_12;
1655 /* Detect ISP6312. */
1656 if (IS_QLA6312(ha))
1657 ha->fw_options[2] |= BIT_13;
1659 /* Update firmware options. */
1660 qla2x00_set_fw_options(vha, ha->fw_options);
1663 void
1664 qla24xx_update_fw_options(scsi_qla_host_t *vha)
1666 int rval;
1667 struct qla_hw_data *ha = vha->hw;
1669 if (IS_QLA82XX(ha))
1670 return;
1672 /* Update Serial Link options. */
1673 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1674 return;
1676 rval = qla2x00_set_serdes_params(vha,
1677 le16_to_cpu(ha->fw_seriallink_options24[1]),
1678 le16_to_cpu(ha->fw_seriallink_options24[2]),
1679 le16_to_cpu(ha->fw_seriallink_options24[3]));
1680 if (rval != QLA_SUCCESS) {
1681 qla_printk(KERN_WARNING, ha,
1682 "Unable to update Serial Link options (%x).\n", rval);
1686 void
1687 qla2x00_config_rings(struct scsi_qla_host *vha)
1689 struct qla_hw_data *ha = vha->hw;
1690 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1691 struct req_que *req = ha->req_q_map[0];
1692 struct rsp_que *rsp = ha->rsp_q_map[0];
1694 /* Setup ring parameters in initialization control block. */
1695 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1696 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1697 ha->init_cb->request_q_length = cpu_to_le16(req->length);
1698 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
1699 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1700 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1701 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1702 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
1704 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1705 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1706 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1707 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1708 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1711 void
1712 qla24xx_config_rings(struct scsi_qla_host *vha)
1714 struct qla_hw_data *ha = vha->hw;
1715 device_reg_t __iomem *reg = ISP_QUE_REG(ha, 0);
1716 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1717 struct qla_msix_entry *msix;
1718 struct init_cb_24xx *icb;
1719 uint16_t rid = 0;
1720 struct req_que *req = ha->req_q_map[0];
1721 struct rsp_que *rsp = ha->rsp_q_map[0];
1723 /* Setup ring parameters in initialization control block. */
1724 icb = (struct init_cb_24xx *)ha->init_cb;
1725 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1726 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1727 icb->request_q_length = cpu_to_le16(req->length);
1728 icb->response_q_length = cpu_to_le16(rsp->length);
1729 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1730 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1731 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1732 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
1734 if (ha->mqenable) {
1735 icb->qos = __constant_cpu_to_le16(QLA_DEFAULT_QUE_QOS);
1736 icb->rid = __constant_cpu_to_le16(rid);
1737 if (ha->flags.msix_enabled) {
1738 msix = &ha->msix_entries[1];
1739 DEBUG2_17(printk(KERN_INFO
1740 "Registering vector 0x%x for base que\n", msix->entry));
1741 icb->msix = cpu_to_le16(msix->entry);
1743 /* Use alternate PCI bus number */
1744 if (MSB(rid))
1745 icb->firmware_options_2 |=
1746 __constant_cpu_to_le32(BIT_19);
1747 /* Use alternate PCI devfn */
1748 if (LSB(rid))
1749 icb->firmware_options_2 |=
1750 __constant_cpu_to_le32(BIT_18);
1752 /* Use Disable MSIX Handshake mode for capable adapters */
1753 if (IS_MSIX_NACK_CAPABLE(ha)) {
1754 icb->firmware_options_2 &=
1755 __constant_cpu_to_le32(~BIT_22);
1756 ha->flags.disable_msix_handshake = 1;
1757 qla_printk(KERN_INFO, ha,
1758 "MSIX Handshake Disable Mode turned on\n");
1759 } else {
1760 icb->firmware_options_2 |=
1761 __constant_cpu_to_le32(BIT_22);
1763 icb->firmware_options_2 |= __constant_cpu_to_le32(BIT_23);
1765 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
1766 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
1767 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
1768 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
1769 } else {
1770 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
1771 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
1772 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
1773 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
1775 /* PCI posting */
1776 RD_REG_DWORD(&ioreg->hccr);
1780 * qla2x00_init_rings() - Initializes firmware.
1781 * @ha: HA context
1783 * Beginning of request ring has initialization control block already built
1784 * by nvram config routine.
1786 * Returns 0 on success.
1788 static int
1789 qla2x00_init_rings(scsi_qla_host_t *vha)
1791 int rval;
1792 unsigned long flags = 0;
1793 int cnt, que;
1794 struct qla_hw_data *ha = vha->hw;
1795 struct req_que *req;
1796 struct rsp_que *rsp;
1797 struct scsi_qla_host *vp;
1798 struct mid_init_cb_24xx *mid_init_cb =
1799 (struct mid_init_cb_24xx *) ha->init_cb;
1801 spin_lock_irqsave(&ha->hardware_lock, flags);
1803 /* Clear outstanding commands array. */
1804 for (que = 0; que < ha->max_req_queues; que++) {
1805 req = ha->req_q_map[que];
1806 if (!req)
1807 continue;
1808 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1809 req->outstanding_cmds[cnt] = NULL;
1811 req->current_outstanding_cmd = 1;
1813 /* Initialize firmware. */
1814 req->ring_ptr = req->ring;
1815 req->ring_index = 0;
1816 req->cnt = req->length;
1819 for (que = 0; que < ha->max_rsp_queues; que++) {
1820 rsp = ha->rsp_q_map[que];
1821 if (!rsp)
1822 continue;
1823 /* Initialize response queue entries */
1824 qla2x00_init_response_q_entries(rsp);
1827 /* Clear RSCN queue. */
1828 list_for_each_entry(vp, &ha->vp_list, list) {
1829 vp->rscn_in_ptr = 0;
1830 vp->rscn_out_ptr = 0;
1832 ha->isp_ops->config_rings(vha);
1834 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1836 /* Update any ISP specific firmware options before initialization. */
1837 ha->isp_ops->update_fw_options(vha);
1839 DEBUG(printk("scsi(%ld): Issue init firmware.\n", vha->host_no));
1841 if (ha->flags.npiv_supported) {
1842 if (ha->operating_mode == LOOP)
1843 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
1844 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
1847 if (IS_FWI2_CAPABLE(ha)) {
1848 mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
1849 mid_init_cb->init_cb.execution_throttle =
1850 cpu_to_le16(ha->fw_xcb_count);
1853 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
1854 if (rval) {
1855 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1856 vha->host_no));
1857 } else {
1858 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1859 vha->host_no));
1862 return (rval);
1866 * qla2x00_fw_ready() - Waits for firmware ready.
1867 * @ha: HA context
1869 * Returns 0 on success.
1871 static int
1872 qla2x00_fw_ready(scsi_qla_host_t *vha)
1874 int rval;
1875 unsigned long wtime, mtime, cs84xx_time;
1876 uint16_t min_wait; /* Minimum wait time if loop is down */
1877 uint16_t wait_time; /* Wait time if loop is coming ready */
1878 uint16_t state[5];
1879 struct qla_hw_data *ha = vha->hw;
1881 rval = QLA_SUCCESS;
1883 /* 20 seconds for loop down. */
1884 min_wait = 20;
1887 * Firmware should take at most one RATOV to login, plus 5 seconds for
1888 * our own processing.
1890 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1891 wait_time = min_wait;
1894 /* Min wait time if loop down */
1895 mtime = jiffies + (min_wait * HZ);
1897 /* wait time before firmware ready */
1898 wtime = jiffies + (wait_time * HZ);
1900 /* Wait for ISP to finish LIP */
1901 if (!vha->flags.init_done)
1902 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1904 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1905 vha->host_no));
1907 do {
1908 rval = qla2x00_get_firmware_state(vha, state);
1909 if (rval == QLA_SUCCESS) {
1910 if (state[0] < FSTATE_LOSS_OF_SYNC) {
1911 vha->device_flags &= ~DFLG_NO_CABLE;
1913 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
1914 DEBUG16(printk("scsi(%ld): fw_state=%x "
1915 "84xx=%x.\n", vha->host_no, state[0],
1916 state[2]));
1917 if ((state[2] & FSTATE_LOGGED_IN) &&
1918 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
1919 DEBUG16(printk("scsi(%ld): Sending "
1920 "verify iocb.\n", vha->host_no));
1922 cs84xx_time = jiffies;
1923 rval = qla84xx_init_chip(vha);
1924 if (rval != QLA_SUCCESS)
1925 break;
1927 /* Add time taken to initialize. */
1928 cs84xx_time = jiffies - cs84xx_time;
1929 wtime += cs84xx_time;
1930 mtime += cs84xx_time;
1931 DEBUG16(printk("scsi(%ld): Increasing "
1932 "wait time by %ld. New time %ld\n",
1933 vha->host_no, cs84xx_time, wtime));
1935 } else if (state[0] == FSTATE_READY) {
1936 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1937 vha->host_no));
1939 qla2x00_get_retry_cnt(vha, &ha->retry_count,
1940 &ha->login_timeout, &ha->r_a_tov);
1942 rval = QLA_SUCCESS;
1943 break;
1946 rval = QLA_FUNCTION_FAILED;
1948 if (atomic_read(&vha->loop_down_timer) &&
1949 state[0] != FSTATE_READY) {
1950 /* Loop down. Timeout on min_wait for states
1951 * other than Wait for Login.
1953 if (time_after_eq(jiffies, mtime)) {
1954 qla_printk(KERN_INFO, ha,
1955 "Cable is unplugged...\n");
1957 vha->device_flags |= DFLG_NO_CABLE;
1958 break;
1961 } else {
1962 /* Mailbox cmd failed. Timeout on min_wait. */
1963 if (time_after_eq(jiffies, mtime))
1964 break;
1967 if (time_after_eq(jiffies, wtime))
1968 break;
1970 /* Delay for a while */
1971 msleep(500);
1973 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1974 vha->host_no, state[0], jiffies));
1975 } while (1);
1977 DEBUG(printk("scsi(%ld): fw_state=%x (%x, %x, %x, %x) curr time=%lx.\n",
1978 vha->host_no, state[0], state[1], state[2], state[3], state[4],
1979 jiffies));
1981 if (rval) {
1982 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1983 vha->host_no));
1986 return (rval);
1990 * qla2x00_configure_hba
1991 * Setup adapter context.
1993 * Input:
1994 * ha = adapter state pointer.
1996 * Returns:
1997 * 0 = success
1999 * Context:
2000 * Kernel context.
2002 static int
2003 qla2x00_configure_hba(scsi_qla_host_t *vha)
2005 int rval;
2006 uint16_t loop_id;
2007 uint16_t topo;
2008 uint16_t sw_cap;
2009 uint8_t al_pa;
2010 uint8_t area;
2011 uint8_t domain;
2012 char connect_type[22];
2013 struct qla_hw_data *ha = vha->hw;
2015 /* Get host addresses. */
2016 rval = qla2x00_get_adapter_id(vha,
2017 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
2018 if (rval != QLA_SUCCESS) {
2019 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
2020 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
2021 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
2022 __func__, vha->host_no));
2023 } else {
2024 qla_printk(KERN_WARNING, ha,
2025 "ERROR -- Unable to get host loop ID.\n");
2026 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2028 return (rval);
2031 if (topo == 4) {
2032 qla_printk(KERN_INFO, ha,
2033 "Cannot get topology - retrying.\n");
2034 return (QLA_FUNCTION_FAILED);
2037 vha->loop_id = loop_id;
2039 /* initialize */
2040 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
2041 ha->operating_mode = LOOP;
2042 ha->switch_cap = 0;
2044 switch (topo) {
2045 case 0:
2046 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
2047 vha->host_no));
2048 ha->current_topology = ISP_CFG_NL;
2049 strcpy(connect_type, "(Loop)");
2050 break;
2052 case 1:
2053 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
2054 vha->host_no));
2055 ha->switch_cap = sw_cap;
2056 ha->current_topology = ISP_CFG_FL;
2057 strcpy(connect_type, "(FL_Port)");
2058 break;
2060 case 2:
2061 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
2062 vha->host_no));
2063 ha->operating_mode = P2P;
2064 ha->current_topology = ISP_CFG_N;
2065 strcpy(connect_type, "(N_Port-to-N_Port)");
2066 break;
2068 case 3:
2069 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
2070 vha->host_no));
2071 ha->switch_cap = sw_cap;
2072 ha->operating_mode = P2P;
2073 ha->current_topology = ISP_CFG_F;
2074 strcpy(connect_type, "(F_Port)");
2075 break;
2077 default:
2078 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
2079 "Using NL.\n",
2080 vha->host_no, topo));
2081 ha->current_topology = ISP_CFG_NL;
2082 strcpy(connect_type, "(Loop)");
2083 break;
2086 /* Save Host port and loop ID. */
2087 /* byte order - Big Endian */
2088 vha->d_id.b.domain = domain;
2089 vha->d_id.b.area = area;
2090 vha->d_id.b.al_pa = al_pa;
2092 if (!vha->flags.init_done)
2093 qla_printk(KERN_INFO, ha,
2094 "Topology - %s, Host Loop address 0x%x\n",
2095 connect_type, vha->loop_id);
2097 if (rval) {
2098 DEBUG2_3(printk("scsi(%ld): FAILED.\n", vha->host_no));
2099 } else {
2100 DEBUG3(printk("scsi(%ld): exiting normally.\n", vha->host_no));
2103 return(rval);
2106 inline void
2107 qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
2108 char *def)
2110 char *st, *en;
2111 uint16_t index;
2112 struct qla_hw_data *ha = vha->hw;
2113 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
2114 !IS_QLA8XXX_TYPE(ha);
2116 if (memcmp(model, BINZERO, len) != 0) {
2117 strncpy(ha->model_number, model, len);
2118 st = en = ha->model_number;
2119 en += len - 1;
2120 while (en > st) {
2121 if (*en != 0x20 && *en != 0x00)
2122 break;
2123 *en-- = '\0';
2126 index = (ha->pdev->subsystem_device & 0xff);
2127 if (use_tbl &&
2128 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
2129 index < QLA_MODEL_NAMES)
2130 strncpy(ha->model_desc,
2131 qla2x00_model_name[index * 2 + 1],
2132 sizeof(ha->model_desc) - 1);
2133 } else {
2134 index = (ha->pdev->subsystem_device & 0xff);
2135 if (use_tbl &&
2136 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
2137 index < QLA_MODEL_NAMES) {
2138 strcpy(ha->model_number,
2139 qla2x00_model_name[index * 2]);
2140 strncpy(ha->model_desc,
2141 qla2x00_model_name[index * 2 + 1],
2142 sizeof(ha->model_desc) - 1);
2143 } else {
2144 strcpy(ha->model_number, def);
2147 if (IS_FWI2_CAPABLE(ha))
2148 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
2149 sizeof(ha->model_desc));
2152 /* On sparc systems, obtain port and node WWN from firmware
2153 * properties.
2155 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
2157 #ifdef CONFIG_SPARC
2158 struct qla_hw_data *ha = vha->hw;
2159 struct pci_dev *pdev = ha->pdev;
2160 struct device_node *dp = pci_device_to_OF_node(pdev);
2161 const u8 *val;
2162 int len;
2164 val = of_get_property(dp, "port-wwn", &len);
2165 if (val && len >= WWN_SIZE)
2166 memcpy(nv->port_name, val, WWN_SIZE);
2168 val = of_get_property(dp, "node-wwn", &len);
2169 if (val && len >= WWN_SIZE)
2170 memcpy(nv->node_name, val, WWN_SIZE);
2171 #endif
2175 * NVRAM configuration for ISP 2xxx
2177 * Input:
2178 * ha = adapter block pointer.
2180 * Output:
2181 * initialization control block in response_ring
2182 * host adapters parameters in host adapter block
2184 * Returns:
2185 * 0 = success.
2188 qla2x00_nvram_config(scsi_qla_host_t *vha)
2190 int rval;
2191 uint8_t chksum = 0;
2192 uint16_t cnt;
2193 uint8_t *dptr1, *dptr2;
2194 struct qla_hw_data *ha = vha->hw;
2195 init_cb_t *icb = ha->init_cb;
2196 nvram_t *nv = ha->nvram;
2197 uint8_t *ptr = ha->nvram;
2198 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2200 rval = QLA_SUCCESS;
2202 /* Determine NVRAM starting address. */
2203 ha->nvram_size = sizeof(nvram_t);
2204 ha->nvram_base = 0;
2205 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
2206 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
2207 ha->nvram_base = 0x80;
2209 /* Get NVRAM data and calculate checksum. */
2210 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
2211 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
2212 chksum += *ptr++;
2214 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
2215 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
2217 /* Bad NVRAM data, set defaults parameters. */
2218 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
2219 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
2220 /* Reset NVRAM data. */
2221 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
2222 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
2223 nv->nvram_version);
2224 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
2225 "invalid -- WWPN) defaults.\n");
2228 * Set default initialization control block.
2230 memset(nv, 0, ha->nvram_size);
2231 nv->parameter_block_version = ICB_VERSION;
2233 if (IS_QLA23XX(ha)) {
2234 nv->firmware_options[0] = BIT_2 | BIT_1;
2235 nv->firmware_options[1] = BIT_7 | BIT_5;
2236 nv->add_firmware_options[0] = BIT_5;
2237 nv->add_firmware_options[1] = BIT_5 | BIT_4;
2238 nv->frame_payload_size = __constant_cpu_to_le16(2048);
2239 nv->special_options[1] = BIT_7;
2240 } else if (IS_QLA2200(ha)) {
2241 nv->firmware_options[0] = BIT_2 | BIT_1;
2242 nv->firmware_options[1] = BIT_7 | BIT_5;
2243 nv->add_firmware_options[0] = BIT_5;
2244 nv->add_firmware_options[1] = BIT_5 | BIT_4;
2245 nv->frame_payload_size = __constant_cpu_to_le16(1024);
2246 } else if (IS_QLA2100(ha)) {
2247 nv->firmware_options[0] = BIT_3 | BIT_1;
2248 nv->firmware_options[1] = BIT_5;
2249 nv->frame_payload_size = __constant_cpu_to_le16(1024);
2252 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
2253 nv->execution_throttle = __constant_cpu_to_le16(16);
2254 nv->retry_count = 8;
2255 nv->retry_delay = 1;
2257 nv->port_name[0] = 33;
2258 nv->port_name[3] = 224;
2259 nv->port_name[4] = 139;
2261 qla2xxx_nvram_wwn_from_ofw(vha, nv);
2263 nv->login_timeout = 4;
2266 * Set default host adapter parameters
2268 nv->host_p[1] = BIT_2;
2269 nv->reset_delay = 5;
2270 nv->port_down_retry_count = 8;
2271 nv->max_luns_per_target = __constant_cpu_to_le16(8);
2272 nv->link_down_timeout = 60;
2274 rval = 1;
2277 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
2279 * The SN2 does not provide BIOS emulation which means you can't change
2280 * potentially bogus BIOS settings. Force the use of default settings
2281 * for link rate and frame size. Hope that the rest of the settings
2282 * are valid.
2284 if (ia64_platform_is("sn2")) {
2285 nv->frame_payload_size = __constant_cpu_to_le16(2048);
2286 if (IS_QLA23XX(ha))
2287 nv->special_options[1] = BIT_7;
2289 #endif
2291 /* Reset Initialization control block */
2292 memset(icb, 0, ha->init_cb_size);
2295 * Setup driver NVRAM options.
2297 nv->firmware_options[0] |= (BIT_6 | BIT_1);
2298 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
2299 nv->firmware_options[1] |= (BIT_5 | BIT_0);
2300 nv->firmware_options[1] &= ~BIT_4;
2302 if (IS_QLA23XX(ha)) {
2303 nv->firmware_options[0] |= BIT_2;
2304 nv->firmware_options[0] &= ~BIT_3;
2305 nv->firmware_options[0] &= ~BIT_6;
2306 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
2308 if (IS_QLA2300(ha)) {
2309 if (ha->fb_rev == FPM_2310) {
2310 strcpy(ha->model_number, "QLA2310");
2311 } else {
2312 strcpy(ha->model_number, "QLA2300");
2314 } else {
2315 qla2x00_set_model_info(vha, nv->model_number,
2316 sizeof(nv->model_number), "QLA23xx");
2318 } else if (IS_QLA2200(ha)) {
2319 nv->firmware_options[0] |= BIT_2;
2321 * 'Point-to-point preferred, else loop' is not a safe
2322 * connection mode setting.
2324 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
2325 (BIT_5 | BIT_4)) {
2326 /* Force 'loop preferred, else point-to-point'. */
2327 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
2328 nv->add_firmware_options[0] |= BIT_5;
2330 strcpy(ha->model_number, "QLA22xx");
2331 } else /*if (IS_QLA2100(ha))*/ {
2332 strcpy(ha->model_number, "QLA2100");
2336 * Copy over NVRAM RISC parameter block to initialization control block.
2338 dptr1 = (uint8_t *)icb;
2339 dptr2 = (uint8_t *)&nv->parameter_block_version;
2340 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
2341 while (cnt--)
2342 *dptr1++ = *dptr2++;
2344 /* Copy 2nd half. */
2345 dptr1 = (uint8_t *)icb->add_firmware_options;
2346 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
2347 while (cnt--)
2348 *dptr1++ = *dptr2++;
2350 /* Use alternate WWN? */
2351 if (nv->host_p[1] & BIT_7) {
2352 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
2353 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
2356 /* Prepare nodename */
2357 if ((icb->firmware_options[1] & BIT_6) == 0) {
2359 * Firmware will apply the following mask if the nodename was
2360 * not provided.
2362 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
2363 icb->node_name[0] &= 0xF0;
2367 * Set host adapter parameters.
2369 if (nv->host_p[0] & BIT_7)
2370 ql2xextended_error_logging = 1;
2371 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
2372 /* Always load RISC code on non ISP2[12]00 chips. */
2373 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2374 ha->flags.disable_risc_code_load = 0;
2375 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
2376 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
2377 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
2378 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
2379 ha->flags.disable_serdes = 0;
2381 ha->operating_mode =
2382 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
2384 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
2385 sizeof(ha->fw_seriallink_options));
2387 /* save HBA serial number */
2388 ha->serial0 = icb->port_name[5];
2389 ha->serial1 = icb->port_name[6];
2390 ha->serial2 = icb->port_name[7];
2391 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
2392 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
2394 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
2396 ha->retry_count = nv->retry_count;
2398 /* Set minimum login_timeout to 4 seconds. */
2399 if (nv->login_timeout < ql2xlogintimeout)
2400 nv->login_timeout = ql2xlogintimeout;
2401 if (nv->login_timeout < 4)
2402 nv->login_timeout = 4;
2403 ha->login_timeout = nv->login_timeout;
2404 icb->login_timeout = nv->login_timeout;
2406 /* Set minimum RATOV to 100 tenths of a second. */
2407 ha->r_a_tov = 100;
2409 ha->loop_reset_delay = nv->reset_delay;
2411 /* Link Down Timeout = 0:
2413 * When Port Down timer expires we will start returning
2414 * I/O's to OS with "DID_NO_CONNECT".
2416 * Link Down Timeout != 0:
2418 * The driver waits for the link to come up after link down
2419 * before returning I/Os to OS with "DID_NO_CONNECT".
2421 if (nv->link_down_timeout == 0) {
2422 ha->loop_down_abort_time =
2423 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
2424 } else {
2425 ha->link_down_timeout = nv->link_down_timeout;
2426 ha->loop_down_abort_time =
2427 (LOOP_DOWN_TIME - ha->link_down_timeout);
2431 * Need enough time to try and get the port back.
2433 ha->port_down_retry_count = nv->port_down_retry_count;
2434 if (qlport_down_retry)
2435 ha->port_down_retry_count = qlport_down_retry;
2436 /* Set login_retry_count */
2437 ha->login_retry_count = nv->retry_count;
2438 if (ha->port_down_retry_count == nv->port_down_retry_count &&
2439 ha->port_down_retry_count > 3)
2440 ha->login_retry_count = ha->port_down_retry_count;
2441 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
2442 ha->login_retry_count = ha->port_down_retry_count;
2443 if (ql2xloginretrycount)
2444 ha->login_retry_count = ql2xloginretrycount;
2446 icb->lun_enables = __constant_cpu_to_le16(0);
2447 icb->command_resource_count = 0;
2448 icb->immediate_notify_resource_count = 0;
2449 icb->timeout = __constant_cpu_to_le16(0);
2451 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2452 /* Enable RIO */
2453 icb->firmware_options[0] &= ~BIT_3;
2454 icb->add_firmware_options[0] &=
2455 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
2456 icb->add_firmware_options[0] |= BIT_2;
2457 icb->response_accumulation_timer = 3;
2458 icb->interrupt_delay_timer = 5;
2460 vha->flags.process_response_queue = 1;
2461 } else {
2462 /* Enable ZIO. */
2463 if (!vha->flags.init_done) {
2464 ha->zio_mode = icb->add_firmware_options[0] &
2465 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
2466 ha->zio_timer = icb->interrupt_delay_timer ?
2467 icb->interrupt_delay_timer: 2;
2469 icb->add_firmware_options[0] &=
2470 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
2471 vha->flags.process_response_queue = 0;
2472 if (ha->zio_mode != QLA_ZIO_DISABLED) {
2473 ha->zio_mode = QLA_ZIO_MODE_6;
2475 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
2476 "delay (%d us).\n", vha->host_no, ha->zio_mode,
2477 ha->zio_timer * 100));
2478 qla_printk(KERN_INFO, ha,
2479 "ZIO mode %d enabled; timer delay (%d us).\n",
2480 ha->zio_mode, ha->zio_timer * 100);
2482 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
2483 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
2484 vha->flags.process_response_queue = 1;
2488 if (rval) {
2489 DEBUG2_3(printk(KERN_WARNING
2490 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
2492 return (rval);
2495 static void
2496 qla2x00_rport_del(void *data)
2498 fc_port_t *fcport = data;
2499 struct fc_rport *rport;
2501 spin_lock_irq(fcport->vha->host->host_lock);
2502 rport = fcport->drport ? fcport->drport: fcport->rport;
2503 fcport->drport = NULL;
2504 spin_unlock_irq(fcport->vha->host->host_lock);
2505 if (rport)
2506 fc_remote_port_delete(rport);
2510 * qla2x00_alloc_fcport() - Allocate a generic fcport.
2511 * @ha: HA context
2512 * @flags: allocation flags
2514 * Returns a pointer to the allocated fcport, or NULL, if none available.
2516 fc_port_t *
2517 qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
2519 fc_port_t *fcport;
2521 fcport = kzalloc(sizeof(fc_port_t), flags);
2522 if (!fcport)
2523 return NULL;
2525 /* Setup fcport template structure. */
2526 fcport->vha = vha;
2527 fcport->vp_idx = vha->vp_idx;
2528 fcport->port_type = FCT_UNKNOWN;
2529 fcport->loop_id = FC_NO_LOOP_ID;
2530 atomic_set(&fcport->state, FCS_UNCONFIGURED);
2531 fcport->supported_classes = FC_COS_UNSPECIFIED;
2533 return fcport;
2537 * qla2x00_configure_loop
2538 * Updates Fibre Channel Device Database with what is actually on loop.
2540 * Input:
2541 * ha = adapter block pointer.
2543 * Returns:
2544 * 0 = success.
2545 * 1 = error.
2546 * 2 = database was full and device was not configured.
2548 static int
2549 qla2x00_configure_loop(scsi_qla_host_t *vha)
2551 int rval;
2552 unsigned long flags, save_flags;
2553 struct qla_hw_data *ha = vha->hw;
2554 rval = QLA_SUCCESS;
2556 /* Get Initiator ID */
2557 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
2558 rval = qla2x00_configure_hba(vha);
2559 if (rval != QLA_SUCCESS) {
2560 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
2561 vha->host_no));
2562 return (rval);
2566 save_flags = flags = vha->dpc_flags;
2567 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
2568 vha->host_no, flags));
2571 * If we have both an RSCN and PORT UPDATE pending then handle them
2572 * both at the same time.
2574 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2575 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
2577 qla2x00_get_data_rate(vha);
2579 /* Determine what we need to do */
2580 if (ha->current_topology == ISP_CFG_FL &&
2581 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2583 vha->flags.rscn_queue_overflow = 1;
2584 set_bit(RSCN_UPDATE, &flags);
2586 } else if (ha->current_topology == ISP_CFG_F &&
2587 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2589 vha->flags.rscn_queue_overflow = 1;
2590 set_bit(RSCN_UPDATE, &flags);
2591 clear_bit(LOCAL_LOOP_UPDATE, &flags);
2593 } else if (ha->current_topology == ISP_CFG_N) {
2594 clear_bit(RSCN_UPDATE, &flags);
2596 } else if (!vha->flags.online ||
2597 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
2599 vha->flags.rscn_queue_overflow = 1;
2600 set_bit(RSCN_UPDATE, &flags);
2601 set_bit(LOCAL_LOOP_UPDATE, &flags);
2604 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
2605 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2606 rval = QLA_FUNCTION_FAILED;
2607 else
2608 rval = qla2x00_configure_local_loop(vha);
2611 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
2612 if (LOOP_TRANSITION(vha))
2613 rval = QLA_FUNCTION_FAILED;
2614 else
2615 rval = qla2x00_configure_fabric(vha);
2618 if (rval == QLA_SUCCESS) {
2619 if (atomic_read(&vha->loop_down_timer) ||
2620 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
2621 rval = QLA_FUNCTION_FAILED;
2622 } else {
2623 atomic_set(&vha->loop_state, LOOP_READY);
2625 DEBUG(printk("scsi(%ld): LOOP READY\n", vha->host_no));
2629 if (rval) {
2630 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
2631 __func__, vha->host_no));
2632 } else {
2633 DEBUG3(printk("%s: exiting normally\n", __func__));
2636 /* Restore state if a resync event occurred during processing */
2637 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
2638 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
2639 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2640 if (test_bit(RSCN_UPDATE, &save_flags)) {
2641 set_bit(RSCN_UPDATE, &vha->dpc_flags);
2642 vha->flags.rscn_queue_overflow = 1;
2646 return (rval);
2652 * qla2x00_configure_local_loop
2653 * Updates Fibre Channel Device Database with local loop devices.
2655 * Input:
2656 * ha = adapter block pointer.
2658 * Returns:
2659 * 0 = success.
2661 static int
2662 qla2x00_configure_local_loop(scsi_qla_host_t *vha)
2664 int rval, rval2;
2665 int found_devs;
2666 int found;
2667 fc_port_t *fcport, *new_fcport;
2669 uint16_t index;
2670 uint16_t entries;
2671 char *id_iter;
2672 uint16_t loop_id;
2673 uint8_t domain, area, al_pa;
2674 struct qla_hw_data *ha = vha->hw;
2676 found_devs = 0;
2677 new_fcport = NULL;
2678 entries = MAX_FIBRE_DEVICES;
2680 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", vha->host_no));
2681 DEBUG3(qla2x00_get_fcal_position_map(vha, NULL));
2683 /* Get list of logged in devices. */
2684 memset(ha->gid_list, 0, GID_LIST_SIZE);
2685 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
2686 &entries);
2687 if (rval != QLA_SUCCESS)
2688 goto cleanup_allocation;
2690 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2691 vha->host_no, entries));
2692 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2693 entries * sizeof(struct gid_list_info)));
2695 /* Allocate temporary fcport for any new fcports discovered. */
2696 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2697 if (new_fcport == NULL) {
2698 rval = QLA_MEMORY_ALLOC_FAILED;
2699 goto cleanup_allocation;
2701 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2704 * Mark local devices that were present with FCF_DEVICE_LOST for now.
2706 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2707 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2708 fcport->port_type != FCT_BROADCAST &&
2709 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2711 DEBUG(printk("scsi(%ld): Marking port lost, "
2712 "loop_id=0x%04x\n",
2713 vha->host_no, fcport->loop_id));
2715 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2719 /* Add devices to port list. */
2720 id_iter = (char *)ha->gid_list;
2721 for (index = 0; index < entries; index++) {
2722 domain = ((struct gid_list_info *)id_iter)->domain;
2723 area = ((struct gid_list_info *)id_iter)->area;
2724 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
2725 if (IS_QLA2100(ha) || IS_QLA2200(ha))
2726 loop_id = (uint16_t)
2727 ((struct gid_list_info *)id_iter)->loop_id_2100;
2728 else
2729 loop_id = le16_to_cpu(
2730 ((struct gid_list_info *)id_iter)->loop_id);
2731 id_iter += ha->gid_list_info_size;
2733 /* Bypass reserved domain fields. */
2734 if ((domain & 0xf0) == 0xf0)
2735 continue;
2737 /* Bypass if not same domain and area of adapter. */
2738 if (area && domain &&
2739 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
2740 continue;
2742 /* Bypass invalid local loop ID. */
2743 if (loop_id > LAST_LOCAL_LOOP_ID)
2744 continue;
2746 /* Fill in member data. */
2747 new_fcport->d_id.b.domain = domain;
2748 new_fcport->d_id.b.area = area;
2749 new_fcport->d_id.b.al_pa = al_pa;
2750 new_fcport->loop_id = loop_id;
2751 new_fcport->vp_idx = vha->vp_idx;
2752 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
2753 if (rval2 != QLA_SUCCESS) {
2754 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2755 "information -- get_port_database=%x, "
2756 "loop_id=0x%04x\n",
2757 vha->host_no, rval2, new_fcport->loop_id));
2758 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2759 vha->host_no));
2760 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
2761 continue;
2764 /* Check for matching device in port list. */
2765 found = 0;
2766 fcport = NULL;
2767 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2768 if (memcmp(new_fcport->port_name, fcport->port_name,
2769 WWN_SIZE))
2770 continue;
2772 fcport->flags &= ~FCF_FABRIC_DEVICE;
2773 fcport->loop_id = new_fcport->loop_id;
2774 fcport->port_type = new_fcport->port_type;
2775 fcport->d_id.b24 = new_fcport->d_id.b24;
2776 memcpy(fcport->node_name, new_fcport->node_name,
2777 WWN_SIZE);
2779 found++;
2780 break;
2783 if (!found) {
2784 /* New device, add to fcports list. */
2785 if (vha->vp_idx) {
2786 new_fcport->vha = vha;
2787 new_fcport->vp_idx = vha->vp_idx;
2789 list_add_tail(&new_fcport->list, &vha->vp_fcports);
2791 /* Allocate a new replacement fcport. */
2792 fcport = new_fcport;
2793 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2794 if (new_fcport == NULL) {
2795 rval = QLA_MEMORY_ALLOC_FAILED;
2796 goto cleanup_allocation;
2798 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2801 /* Base iIDMA settings on HBA port speed. */
2802 fcport->fp_speed = ha->link_data_rate;
2804 qla2x00_update_fcport(vha, fcport);
2806 found_devs++;
2809 cleanup_allocation:
2810 kfree(new_fcport);
2812 if (rval != QLA_SUCCESS) {
2813 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2814 "rval=%x\n", vha->host_no, rval));
2817 return (rval);
2820 static void
2821 qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
2823 #define LS_UNKNOWN 2
2824 static char *link_speeds[] = { "1", "2", "?", "4", "8", "10" };
2825 char *link_speed;
2826 int rval;
2827 uint16_t mb[4];
2828 struct qla_hw_data *ha = vha->hw;
2830 if (!IS_IIDMA_CAPABLE(ha))
2831 return;
2833 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2834 fcport->fp_speed > ha->link_data_rate)
2835 return;
2837 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
2838 mb);
2839 if (rval != QLA_SUCCESS) {
2840 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2841 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2842 vha->host_no, fcport->port_name[0], fcport->port_name[1],
2843 fcport->port_name[2], fcport->port_name[3],
2844 fcport->port_name[4], fcport->port_name[5],
2845 fcport->port_name[6], fcport->port_name[7], rval,
2846 fcport->fp_speed, mb[0], mb[1]));
2847 } else {
2848 link_speed = link_speeds[LS_UNKNOWN];
2849 if (fcport->fp_speed < 5)
2850 link_speed = link_speeds[fcport->fp_speed];
2851 else if (fcport->fp_speed == 0x13)
2852 link_speed = link_speeds[5];
2853 DEBUG2(qla_printk(KERN_INFO, ha,
2854 "iIDMA adjusted to %s GB/s on "
2855 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2856 link_speed, fcport->port_name[0],
2857 fcport->port_name[1], fcport->port_name[2],
2858 fcport->port_name[3], fcport->port_name[4],
2859 fcport->port_name[5], fcport->port_name[6],
2860 fcport->port_name[7]));
2864 static void
2865 qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
2867 struct fc_rport_identifiers rport_ids;
2868 struct fc_rport *rport;
2869 struct qla_hw_data *ha = vha->hw;
2871 qla2x00_rport_del(fcport);
2873 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2874 rport_ids.port_name = wwn_to_u64(fcport->port_name);
2875 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2876 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2877 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2878 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
2879 if (!rport) {
2880 qla_printk(KERN_WARNING, ha,
2881 "Unable to allocate fc remote port!\n");
2882 return;
2884 spin_lock_irq(fcport->vha->host->host_lock);
2885 *((fc_port_t **)rport->dd_data) = fcport;
2886 spin_unlock_irq(fcport->vha->host->host_lock);
2888 rport->supported_classes = fcport->supported_classes;
2890 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2891 if (fcport->port_type == FCT_INITIATOR)
2892 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2893 if (fcport->port_type == FCT_TARGET)
2894 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2895 fc_remote_port_rolechg(rport, rport_ids.roles);
2899 * qla2x00_update_fcport
2900 * Updates device on list.
2902 * Input:
2903 * ha = adapter block pointer.
2904 * fcport = port structure pointer.
2906 * Return:
2907 * 0 - Success
2908 * BIT_0 - error
2910 * Context:
2911 * Kernel context.
2913 void
2914 qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
2916 struct qla_hw_data *ha = vha->hw;
2918 fcport->vha = vha;
2919 fcport->login_retry = 0;
2920 fcport->port_login_retry_count = ha->port_down_retry_count *
2921 PORT_RETRY_TIME;
2922 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2923 PORT_RETRY_TIME);
2924 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
2926 qla2x00_iidma_fcport(vha, fcport);
2928 atomic_set(&fcport->state, FCS_ONLINE);
2930 qla2x00_reg_remote_port(vha, fcport);
2934 * qla2x00_configure_fabric
2935 * Setup SNS devices with loop ID's.
2937 * Input:
2938 * ha = adapter block pointer.
2940 * Returns:
2941 * 0 = success.
2942 * BIT_0 = error
2944 static int
2945 qla2x00_configure_fabric(scsi_qla_host_t *vha)
2947 int rval, rval2;
2948 fc_port_t *fcport, *fcptemp;
2949 uint16_t next_loopid;
2950 uint16_t mb[MAILBOX_REGISTER_COUNT];
2951 uint16_t loop_id;
2952 LIST_HEAD(new_fcports);
2953 struct qla_hw_data *ha = vha->hw;
2954 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2956 /* If FL port exists, then SNS is present */
2957 if (IS_FWI2_CAPABLE(ha))
2958 loop_id = NPH_F_PORT;
2959 else
2960 loop_id = SNS_FL_PORT;
2961 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
2962 if (rval != QLA_SUCCESS) {
2963 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2964 "Port\n", vha->host_no));
2966 vha->device_flags &= ~SWITCH_FOUND;
2967 return (QLA_SUCCESS);
2969 vha->device_flags |= SWITCH_FOUND;
2971 /* Mark devices that need re-synchronization. */
2972 rval2 = qla2x00_device_resync(vha);
2973 if (rval2 == QLA_RSCNS_HANDLED) {
2974 /* No point doing the scan, just continue. */
2975 return (QLA_SUCCESS);
2977 do {
2978 /* FDMI support. */
2979 if (ql2xfdmienable &&
2980 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
2981 qla2x00_fdmi_register(vha);
2983 /* Ensure we are logged into the SNS. */
2984 if (IS_FWI2_CAPABLE(ha))
2985 loop_id = NPH_SNS;
2986 else
2987 loop_id = SIMPLE_NAME_SERVER;
2988 ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
2989 0xfc, mb, BIT_1 | BIT_0);
2990 if (mb[0] != MBS_COMMAND_COMPLETE) {
2991 DEBUG2(qla_printk(KERN_INFO, ha,
2992 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2993 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2994 mb[0], mb[1], mb[2], mb[6], mb[7]));
2995 return (QLA_SUCCESS);
2998 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
2999 if (qla2x00_rft_id(vha)) {
3000 /* EMPTY */
3001 DEBUG2(printk("scsi(%ld): Register FC-4 "
3002 "TYPE failed.\n", vha->host_no));
3004 if (qla2x00_rff_id(vha)) {
3005 /* EMPTY */
3006 DEBUG2(printk("scsi(%ld): Register FC-4 "
3007 "Features failed.\n", vha->host_no));
3009 if (qla2x00_rnn_id(vha)) {
3010 /* EMPTY */
3011 DEBUG2(printk("scsi(%ld): Register Node Name "
3012 "failed.\n", vha->host_no));
3013 } else if (qla2x00_rsnn_nn(vha)) {
3014 /* EMPTY */
3015 DEBUG2(printk("scsi(%ld): Register Symbolic "
3016 "Node Name failed.\n", vha->host_no));
3020 rval = qla2x00_find_all_fabric_devs(vha, &new_fcports);
3021 if (rval != QLA_SUCCESS)
3022 break;
3025 * Logout all previous fabric devices marked lost, except
3026 * FCP2 devices.
3028 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3029 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
3030 break;
3032 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
3033 continue;
3035 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3036 qla2x00_mark_device_lost(vha, fcport,
3037 ql2xplogiabsentdevice, 0);
3038 if (fcport->loop_id != FC_NO_LOOP_ID &&
3039 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
3040 fcport->port_type != FCT_INITIATOR &&
3041 fcport->port_type != FCT_BROADCAST) {
3042 ha->isp_ops->fabric_logout(vha,
3043 fcport->loop_id,
3044 fcport->d_id.b.domain,
3045 fcport->d_id.b.area,
3046 fcport->d_id.b.al_pa);
3047 fcport->loop_id = FC_NO_LOOP_ID;
3052 /* Starting free loop ID. */
3053 next_loopid = ha->min_external_loopid;
3056 * Scan through our port list and login entries that need to be
3057 * logged in.
3059 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3060 if (atomic_read(&vha->loop_down_timer) ||
3061 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
3062 break;
3064 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
3065 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
3066 continue;
3068 if (fcport->loop_id == FC_NO_LOOP_ID) {
3069 fcport->loop_id = next_loopid;
3070 rval = qla2x00_find_new_loop_id(
3071 base_vha, fcport);
3072 if (rval != QLA_SUCCESS) {
3073 /* Ran out of IDs to use */
3074 break;
3077 /* Login and update database */
3078 qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
3081 /* Exit if out of loop IDs. */
3082 if (rval != QLA_SUCCESS) {
3083 break;
3087 * Login and add the new devices to our port list.
3089 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
3090 if (atomic_read(&vha->loop_down_timer) ||
3091 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
3092 break;
3094 /* Find a new loop ID to use. */
3095 fcport->loop_id = next_loopid;
3096 rval = qla2x00_find_new_loop_id(base_vha, fcport);
3097 if (rval != QLA_SUCCESS) {
3098 /* Ran out of IDs to use */
3099 break;
3102 /* Login and update database */
3103 qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
3105 if (vha->vp_idx) {
3106 fcport->vha = vha;
3107 fcport->vp_idx = vha->vp_idx;
3109 list_move_tail(&fcport->list, &vha->vp_fcports);
3111 } while (0);
3113 /* Free all new device structures not processed. */
3114 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
3115 list_del(&fcport->list);
3116 kfree(fcport);
3119 if (rval) {
3120 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
3121 "rval=%d\n", vha->host_no, rval));
3124 return (rval);
3129 * qla2x00_find_all_fabric_devs
3131 * Input:
3132 * ha = adapter block pointer.
3133 * dev = database device entry pointer.
3135 * Returns:
3136 * 0 = success.
3138 * Context:
3139 * Kernel context.
3141 static int
3142 qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha,
3143 struct list_head *new_fcports)
3145 int rval;
3146 uint16_t loop_id;
3147 fc_port_t *fcport, *new_fcport, *fcptemp;
3148 int found;
3150 sw_info_t *swl;
3151 int swl_idx;
3152 int first_dev, last_dev;
3153 port_id_t wrap = {}, nxt_d_id;
3154 struct qla_hw_data *ha = vha->hw;
3155 struct scsi_qla_host *vp, *base_vha = pci_get_drvdata(ha->pdev);
3156 struct scsi_qla_host *tvp;
3158 rval = QLA_SUCCESS;
3160 /* Try GID_PT to get device list, else GAN. */
3161 swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_KERNEL);
3162 if (!swl) {
3163 /*EMPTY*/
3164 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
3165 "on GA_NXT\n", vha->host_no));
3166 } else {
3167 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
3168 kfree(swl);
3169 swl = NULL;
3170 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
3171 kfree(swl);
3172 swl = NULL;
3173 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
3174 kfree(swl);
3175 swl = NULL;
3176 } else if (ql2xiidmaenable &&
3177 qla2x00_gfpn_id(vha, swl) == QLA_SUCCESS) {
3178 qla2x00_gpsc(vha, swl);
3181 swl_idx = 0;
3183 /* Allocate temporary fcport for any new fcports discovered. */
3184 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
3185 if (new_fcport == NULL) {
3186 kfree(swl);
3187 return (QLA_MEMORY_ALLOC_FAILED);
3189 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
3190 /* Set start port ID scan at adapter ID. */
3191 first_dev = 1;
3192 last_dev = 0;
3194 /* Starting free loop ID. */
3195 loop_id = ha->min_external_loopid;
3196 for (; loop_id <= ha->max_loop_id; loop_id++) {
3197 if (qla2x00_is_reserved_id(vha, loop_id))
3198 continue;
3200 if (atomic_read(&vha->loop_down_timer) ||
3201 LOOP_TRANSITION(vha)) {
3202 atomic_set(&vha->loop_down_timer, 0);
3203 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3204 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3205 break;
3208 if (swl != NULL) {
3209 if (last_dev) {
3210 wrap.b24 = new_fcport->d_id.b24;
3211 } else {
3212 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
3213 memcpy(new_fcport->node_name,
3214 swl[swl_idx].node_name, WWN_SIZE);
3215 memcpy(new_fcport->port_name,
3216 swl[swl_idx].port_name, WWN_SIZE);
3217 memcpy(new_fcport->fabric_port_name,
3218 swl[swl_idx].fabric_port_name, WWN_SIZE);
3219 new_fcport->fp_speed = swl[swl_idx].fp_speed;
3221 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
3222 last_dev = 1;
3224 swl_idx++;
3226 } else {
3227 /* Send GA_NXT to the switch */
3228 rval = qla2x00_ga_nxt(vha, new_fcport);
3229 if (rval != QLA_SUCCESS) {
3230 qla_printk(KERN_WARNING, ha,
3231 "SNS scan failed -- assuming zero-entry "
3232 "result...\n");
3233 list_for_each_entry_safe(fcport, fcptemp,
3234 new_fcports, list) {
3235 list_del(&fcport->list);
3236 kfree(fcport);
3238 rval = QLA_SUCCESS;
3239 break;
3243 /* If wrap on switch device list, exit. */
3244 if (first_dev) {
3245 wrap.b24 = new_fcport->d_id.b24;
3246 first_dev = 0;
3247 } else if (new_fcport->d_id.b24 == wrap.b24) {
3248 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
3249 vha->host_no, new_fcport->d_id.b.domain,
3250 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
3251 break;
3254 /* Bypass if same physical adapter. */
3255 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
3256 continue;
3258 /* Bypass virtual ports of the same host. */
3259 found = 0;
3260 if (ha->num_vhosts) {
3261 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
3262 if (new_fcport->d_id.b24 == vp->d_id.b24) {
3263 found = 1;
3264 break;
3267 if (found)
3268 continue;
3271 /* Bypass if same domain and area of adapter. */
3272 if (((new_fcport->d_id.b24 & 0xffff00) ==
3273 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
3274 ISP_CFG_FL)
3275 continue;
3277 /* Bypass reserved domain fields. */
3278 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
3279 continue;
3281 /* Locate matching device in database. */
3282 found = 0;
3283 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3284 if (memcmp(new_fcport->port_name, fcport->port_name,
3285 WWN_SIZE))
3286 continue;
3288 found++;
3290 /* Update port state. */
3291 memcpy(fcport->fabric_port_name,
3292 new_fcport->fabric_port_name, WWN_SIZE);
3293 fcport->fp_speed = new_fcport->fp_speed;
3296 * If address the same and state FCS_ONLINE, nothing
3297 * changed.
3299 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
3300 atomic_read(&fcport->state) == FCS_ONLINE) {
3301 break;
3305 * If device was not a fabric device before.
3307 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
3308 fcport->d_id.b24 = new_fcport->d_id.b24;
3309 fcport->loop_id = FC_NO_LOOP_ID;
3310 fcport->flags |= (FCF_FABRIC_DEVICE |
3311 FCF_LOGIN_NEEDED);
3312 break;
3316 * Port ID changed or device was marked to be updated;
3317 * Log it out if still logged in and mark it for
3318 * relogin later.
3320 fcport->d_id.b24 = new_fcport->d_id.b24;
3321 fcport->flags |= FCF_LOGIN_NEEDED;
3322 if (fcport->loop_id != FC_NO_LOOP_ID &&
3323 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
3324 fcport->port_type != FCT_INITIATOR &&
3325 fcport->port_type != FCT_BROADCAST) {
3326 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
3327 fcport->d_id.b.domain, fcport->d_id.b.area,
3328 fcport->d_id.b.al_pa);
3329 fcport->loop_id = FC_NO_LOOP_ID;
3332 break;
3335 if (found)
3336 continue;
3337 /* If device was not in our fcports list, then add it. */
3338 list_add_tail(&new_fcport->list, new_fcports);
3340 /* Allocate a new replacement fcport. */
3341 nxt_d_id.b24 = new_fcport->d_id.b24;
3342 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
3343 if (new_fcport == NULL) {
3344 kfree(swl);
3345 return (QLA_MEMORY_ALLOC_FAILED);
3347 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
3348 new_fcport->d_id.b24 = nxt_d_id.b24;
3351 kfree(swl);
3352 kfree(new_fcport);
3354 return (rval);
3358 * qla2x00_find_new_loop_id
3359 * Scan through our port list and find a new usable loop ID.
3361 * Input:
3362 * ha: adapter state pointer.
3363 * dev: port structure pointer.
3365 * Returns:
3366 * qla2x00 local function return status code.
3368 * Context:
3369 * Kernel context.
3371 static int
3372 qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
3374 int rval;
3375 int found;
3376 fc_port_t *fcport;
3377 uint16_t first_loop_id;
3378 struct qla_hw_data *ha = vha->hw;
3379 struct scsi_qla_host *vp;
3380 struct scsi_qla_host *tvp;
3382 rval = QLA_SUCCESS;
3384 /* Save starting loop ID. */
3385 first_loop_id = dev->loop_id;
3387 for (;;) {
3388 /* Skip loop ID if already used by adapter. */
3389 if (dev->loop_id == vha->loop_id)
3390 dev->loop_id++;
3392 /* Skip reserved loop IDs. */
3393 while (qla2x00_is_reserved_id(vha, dev->loop_id))
3394 dev->loop_id++;
3396 /* Reset loop ID if passed the end. */
3397 if (dev->loop_id > ha->max_loop_id) {
3398 /* first loop ID. */
3399 dev->loop_id = ha->min_external_loopid;
3402 /* Check for loop ID being already in use. */
3403 found = 0;
3404 fcport = NULL;
3405 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
3406 list_for_each_entry(fcport, &vp->vp_fcports, list) {
3407 if (fcport->loop_id == dev->loop_id &&
3408 fcport != dev) {
3409 /* ID possibly in use */
3410 found++;
3411 break;
3414 if (found)
3415 break;
3418 /* If not in use then it is free to use. */
3419 if (!found) {
3420 break;
3423 /* ID in use. Try next value. */
3424 dev->loop_id++;
3426 /* If wrap around. No free ID to use. */
3427 if (dev->loop_id == first_loop_id) {
3428 dev->loop_id = FC_NO_LOOP_ID;
3429 rval = QLA_FUNCTION_FAILED;
3430 break;
3434 return (rval);
3438 * qla2x00_device_resync
3439 * Marks devices in the database that needs resynchronization.
3441 * Input:
3442 * ha = adapter block pointer.
3444 * Context:
3445 * Kernel context.
3447 static int
3448 qla2x00_device_resync(scsi_qla_host_t *vha)
3450 int rval;
3451 uint32_t mask;
3452 fc_port_t *fcport;
3453 uint32_t rscn_entry;
3454 uint8_t rscn_out_iter;
3455 uint8_t format;
3456 port_id_t d_id = {};
3458 rval = QLA_RSCNS_HANDLED;
3460 while (vha->rscn_out_ptr != vha->rscn_in_ptr ||
3461 vha->flags.rscn_queue_overflow) {
3463 rscn_entry = vha->rscn_queue[vha->rscn_out_ptr];
3464 format = MSB(MSW(rscn_entry));
3465 d_id.b.domain = LSB(MSW(rscn_entry));
3466 d_id.b.area = MSB(LSW(rscn_entry));
3467 d_id.b.al_pa = LSB(LSW(rscn_entry));
3469 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
3470 "[%02x/%02x%02x%02x].\n",
3471 vha->host_no, vha->rscn_out_ptr, format, d_id.b.domain,
3472 d_id.b.area, d_id.b.al_pa));
3474 vha->rscn_out_ptr++;
3475 if (vha->rscn_out_ptr == MAX_RSCN_COUNT)
3476 vha->rscn_out_ptr = 0;
3478 /* Skip duplicate entries. */
3479 for (rscn_out_iter = vha->rscn_out_ptr;
3480 !vha->flags.rscn_queue_overflow &&
3481 rscn_out_iter != vha->rscn_in_ptr;
3482 rscn_out_iter = (rscn_out_iter ==
3483 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
3485 if (rscn_entry != vha->rscn_queue[rscn_out_iter])
3486 break;
3488 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
3489 "entry found at [%d].\n", vha->host_no,
3490 rscn_out_iter));
3492 vha->rscn_out_ptr = rscn_out_iter;
3495 /* Queue overflow, set switch default case. */
3496 if (vha->flags.rscn_queue_overflow) {
3497 DEBUG(printk("scsi(%ld): device_resync: rscn "
3498 "overflow.\n", vha->host_no));
3500 format = 3;
3501 vha->flags.rscn_queue_overflow = 0;
3504 switch (format) {
3505 case 0:
3506 mask = 0xffffff;
3507 break;
3508 case 1:
3509 mask = 0xffff00;
3510 break;
3511 case 2:
3512 mask = 0xff0000;
3513 break;
3514 default:
3515 mask = 0x0;
3516 d_id.b24 = 0;
3517 vha->rscn_out_ptr = vha->rscn_in_ptr;
3518 break;
3521 rval = QLA_SUCCESS;
3523 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3524 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
3525 (fcport->d_id.b24 & mask) != d_id.b24 ||
3526 fcport->port_type == FCT_BROADCAST)
3527 continue;
3529 if (atomic_read(&fcport->state) == FCS_ONLINE) {
3530 if (format != 3 ||
3531 fcport->port_type != FCT_INITIATOR) {
3532 qla2x00_mark_device_lost(vha, fcport,
3533 0, 0);
3538 return (rval);
3542 * qla2x00_fabric_dev_login
3543 * Login fabric target device and update FC port database.
3545 * Input:
3546 * ha: adapter state pointer.
3547 * fcport: port structure list pointer.
3548 * next_loopid: contains value of a new loop ID that can be used
3549 * by the next login attempt.
3551 * Returns:
3552 * qla2x00 local function return status code.
3554 * Context:
3555 * Kernel context.
3557 static int
3558 qla2x00_fabric_dev_login(scsi_qla_host_t *vha, fc_port_t *fcport,
3559 uint16_t *next_loopid)
3561 int rval;
3562 int retry;
3563 uint8_t opts;
3564 struct qla_hw_data *ha = vha->hw;
3566 rval = QLA_SUCCESS;
3567 retry = 0;
3569 if (IS_ALOGIO_CAPABLE(ha)) {
3570 if (fcport->flags & FCF_ASYNC_SENT)
3571 return rval;
3572 fcport->flags |= FCF_ASYNC_SENT;
3573 rval = qla2x00_post_async_login_work(vha, fcport, NULL);
3574 if (!rval)
3575 return rval;
3578 fcport->flags &= ~FCF_ASYNC_SENT;
3579 rval = qla2x00_fabric_login(vha, fcport, next_loopid);
3580 if (rval == QLA_SUCCESS) {
3581 /* Send an ADISC to FCP2 devices.*/
3582 opts = 0;
3583 if (fcport->flags & FCF_FCP2_DEVICE)
3584 opts |= BIT_1;
3585 rval = qla2x00_get_port_database(vha, fcport, opts);
3586 if (rval != QLA_SUCCESS) {
3587 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
3588 fcport->d_id.b.domain, fcport->d_id.b.area,
3589 fcport->d_id.b.al_pa);
3590 qla2x00_mark_device_lost(vha, fcport, 1, 0);
3591 } else {
3592 qla2x00_update_fcport(vha, fcport);
3596 return (rval);
3600 * qla2x00_fabric_login
3601 * Issue fabric login command.
3603 * Input:
3604 * ha = adapter block pointer.
3605 * device = pointer to FC device type structure.
3607 * Returns:
3608 * 0 - Login successfully
3609 * 1 - Login failed
3610 * 2 - Initiator device
3611 * 3 - Fatal error
3614 qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
3615 uint16_t *next_loopid)
3617 int rval;
3618 int retry;
3619 uint16_t tmp_loopid;
3620 uint16_t mb[MAILBOX_REGISTER_COUNT];
3621 struct qla_hw_data *ha = vha->hw;
3623 retry = 0;
3624 tmp_loopid = 0;
3626 for (;;) {
3627 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
3628 "for port %02x%02x%02x.\n",
3629 vha->host_no, fcport->loop_id, fcport->d_id.b.domain,
3630 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3632 /* Login fcport on switch. */
3633 ha->isp_ops->fabric_login(vha, fcport->loop_id,
3634 fcport->d_id.b.domain, fcport->d_id.b.area,
3635 fcport->d_id.b.al_pa, mb, BIT_0);
3636 if (mb[0] == MBS_PORT_ID_USED) {
3638 * Device has another loop ID. The firmware team
3639 * recommends the driver perform an implicit login with
3640 * the specified ID again. The ID we just used is save
3641 * here so we return with an ID that can be tried by
3642 * the next login.
3644 retry++;
3645 tmp_loopid = fcport->loop_id;
3646 fcport->loop_id = mb[1];
3648 DEBUG(printk("Fabric Login: port in use - next "
3649 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3650 fcport->loop_id, fcport->d_id.b.domain,
3651 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3653 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3655 * Login succeeded.
3657 if (retry) {
3658 /* A retry occurred before. */
3659 *next_loopid = tmp_loopid;
3660 } else {
3662 * No retry occurred before. Just increment the
3663 * ID value for next login.
3665 *next_loopid = (fcport->loop_id + 1);
3668 if (mb[1] & BIT_0) {
3669 fcport->port_type = FCT_INITIATOR;
3670 } else {
3671 fcport->port_type = FCT_TARGET;
3672 if (mb[1] & BIT_1) {
3673 fcport->flags |= FCF_FCP2_DEVICE;
3677 if (mb[10] & BIT_0)
3678 fcport->supported_classes |= FC_COS_CLASS2;
3679 if (mb[10] & BIT_1)
3680 fcport->supported_classes |= FC_COS_CLASS3;
3682 rval = QLA_SUCCESS;
3683 break;
3684 } else if (mb[0] == MBS_LOOP_ID_USED) {
3686 * Loop ID already used, try next loop ID.
3688 fcport->loop_id++;
3689 rval = qla2x00_find_new_loop_id(vha, fcport);
3690 if (rval != QLA_SUCCESS) {
3691 /* Ran out of loop IDs to use */
3692 break;
3694 } else if (mb[0] == MBS_COMMAND_ERROR) {
3696 * Firmware possibly timed out during login. If NO
3697 * retries are left to do then the device is declared
3698 * dead.
3700 *next_loopid = fcport->loop_id;
3701 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
3702 fcport->d_id.b.domain, fcport->d_id.b.area,
3703 fcport->d_id.b.al_pa);
3704 qla2x00_mark_device_lost(vha, fcport, 1, 0);
3706 rval = 1;
3707 break;
3708 } else {
3710 * unrecoverable / not handled error
3712 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
3713 "loop_id=%x jiffies=%lx.\n",
3714 __func__, vha->host_no, mb[0],
3715 fcport->d_id.b.domain, fcport->d_id.b.area,
3716 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3718 *next_loopid = fcport->loop_id;
3719 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
3720 fcport->d_id.b.domain, fcport->d_id.b.area,
3721 fcport->d_id.b.al_pa);
3722 fcport->loop_id = FC_NO_LOOP_ID;
3723 fcport->login_retry = 0;
3725 rval = 3;
3726 break;
3730 return (rval);
3734 * qla2x00_local_device_login
3735 * Issue local device login command.
3737 * Input:
3738 * ha = adapter block pointer.
3739 * loop_id = loop id of device to login to.
3741 * Returns (Where's the #define!!!!):
3742 * 0 - Login successfully
3743 * 1 - Login failed
3744 * 3 - Fatal error
3747 qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
3749 int rval;
3750 uint16_t mb[MAILBOX_REGISTER_COUNT];
3752 memset(mb, 0, sizeof(mb));
3753 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
3754 if (rval == QLA_SUCCESS) {
3755 /* Interrogate mailbox registers for any errors */
3756 if (mb[0] == MBS_COMMAND_ERROR)
3757 rval = 1;
3758 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3759 /* device not in PCB table */
3760 rval = 3;
3763 return (rval);
3767 * qla2x00_loop_resync
3768 * Resync with fibre channel devices.
3770 * Input:
3771 * ha = adapter block pointer.
3773 * Returns:
3774 * 0 = success
3777 qla2x00_loop_resync(scsi_qla_host_t *vha)
3779 int rval = QLA_SUCCESS;
3780 uint32_t wait_time;
3781 struct req_que *req;
3782 struct rsp_que *rsp;
3784 if (vha->hw->flags.cpu_affinity_enabled)
3785 req = vha->hw->req_q_map[0];
3786 else
3787 req = vha->req;
3788 rsp = req->rsp;
3790 atomic_set(&vha->loop_state, LOOP_UPDATE);
3791 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3792 if (vha->flags.online) {
3793 if (!(rval = qla2x00_fw_ready(vha))) {
3794 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3795 wait_time = 256;
3796 do {
3797 atomic_set(&vha->loop_state, LOOP_UPDATE);
3799 /* Issue a marker after FW becomes ready. */
3800 qla2x00_marker(vha, req, rsp, 0, 0,
3801 MK_SYNC_ALL);
3802 vha->marker_needed = 0;
3804 /* Remap devices on Loop. */
3805 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3807 qla2x00_configure_loop(vha);
3808 wait_time--;
3809 } while (!atomic_read(&vha->loop_down_timer) &&
3810 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
3811 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
3812 &vha->dpc_flags)));
3816 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
3817 return (QLA_FUNCTION_FAILED);
3819 if (rval)
3820 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3822 return (rval);
3825 void
3826 qla2x00_update_fcports(scsi_qla_host_t *base_vha)
3828 fc_port_t *fcport;
3829 struct scsi_qla_host *tvp, *vha;
3831 /* Go with deferred removal of rport references. */
3832 list_for_each_entry_safe(vha, tvp, &base_vha->hw->vp_list, list)
3833 list_for_each_entry(fcport, &vha->vp_fcports, list)
3834 if (fcport && fcport->drport &&
3835 atomic_read(&fcport->state) != FCS_UNCONFIGURED)
3836 qla2x00_rport_del(fcport);
3839 void
3840 qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
3842 struct qla_hw_data *ha = vha->hw;
3843 struct scsi_qla_host *vp, *base_vha = pci_get_drvdata(ha->pdev);
3844 struct scsi_qla_host *tvp;
3846 vha->flags.online = 0;
3847 ha->flags.chip_reset_done = 0;
3848 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3849 ha->qla_stats.total_isp_aborts++;
3851 qla_printk(KERN_INFO, ha,
3852 "Performing ISP error recovery - ha= %p.\n", ha);
3854 /* Chip reset does not apply to 82XX */
3855 if (!IS_QLA82XX(ha))
3856 ha->isp_ops->reset_chip(vha);
3858 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
3859 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
3860 atomic_set(&vha->loop_state, LOOP_DOWN);
3861 qla2x00_mark_all_devices_lost(vha, 0);
3862 list_for_each_entry_safe(vp, tvp, &base_vha->hw->vp_list, list)
3863 qla2x00_mark_all_devices_lost(vp, 0);
3864 } else {
3865 if (!atomic_read(&vha->loop_down_timer))
3866 atomic_set(&vha->loop_down_timer,
3867 LOOP_DOWN_TIME);
3870 /* Make sure for ISP 82XX IO DMA is complete */
3871 if (IS_QLA82XX(ha))
3872 qla82xx_wait_for_pending_commands(vha);
3874 /* Requeue all commands in outstanding command list. */
3875 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
3879 * qla2x00_abort_isp
3880 * Resets ISP and aborts all outstanding commands.
3882 * Input:
3883 * ha = adapter block pointer.
3885 * Returns:
3886 * 0 = success
3889 qla2x00_abort_isp(scsi_qla_host_t *vha)
3891 int rval;
3892 uint8_t status = 0;
3893 struct qla_hw_data *ha = vha->hw;
3894 struct scsi_qla_host *vp;
3895 struct scsi_qla_host *tvp;
3896 struct req_que *req = ha->req_q_map[0];
3898 if (vha->flags.online) {
3899 qla2x00_abort_isp_cleanup(vha);
3901 if (unlikely(pci_channel_offline(ha->pdev) &&
3902 ha->flags.pci_channel_io_perm_failure)) {
3903 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3904 status = 0;
3905 return status;
3908 ha->isp_ops->get_flash_version(vha, req->ring);
3910 ha->isp_ops->nvram_config(vha);
3912 if (!qla2x00_restart_isp(vha)) {
3913 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
3915 if (!atomic_read(&vha->loop_down_timer)) {
3917 * Issue marker command only when we are going
3918 * to start the I/O .
3920 vha->marker_needed = 1;
3923 vha->flags.online = 1;
3925 ha->isp_ops->enable_intrs(ha);
3927 ha->isp_abort_cnt = 0;
3928 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3930 if (IS_QLA81XX(ha))
3931 qla2x00_get_fw_version(vha,
3932 &ha->fw_major_version,
3933 &ha->fw_minor_version,
3934 &ha->fw_subminor_version,
3935 &ha->fw_attributes, &ha->fw_memory_size,
3936 ha->mpi_version, &ha->mpi_capabilities,
3937 ha->phy_version);
3939 if (ha->fce) {
3940 ha->flags.fce_enabled = 1;
3941 memset(ha->fce, 0,
3942 fce_calc_size(ha->fce_bufs));
3943 rval = qla2x00_enable_fce_trace(vha,
3944 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3945 &ha->fce_bufs);
3946 if (rval) {
3947 qla_printk(KERN_WARNING, ha,
3948 "Unable to reinitialize FCE "
3949 "(%d).\n", rval);
3950 ha->flags.fce_enabled = 0;
3954 if (ha->eft) {
3955 memset(ha->eft, 0, EFT_SIZE);
3956 rval = qla2x00_enable_eft_trace(vha,
3957 ha->eft_dma, EFT_NUM_BUFFERS);
3958 if (rval) {
3959 qla_printk(KERN_WARNING, ha,
3960 "Unable to reinitialize EFT "
3961 "(%d).\n", rval);
3964 } else { /* failed the ISP abort */
3965 vha->flags.online = 1;
3966 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
3967 if (ha->isp_abort_cnt == 0) {
3968 qla_printk(KERN_WARNING, ha,
3969 "ISP error recovery failed - "
3970 "board disabled\n");
3972 * The next call disables the board
3973 * completely.
3975 ha->isp_ops->reset_adapter(vha);
3976 vha->flags.online = 0;
3977 clear_bit(ISP_ABORT_RETRY,
3978 &vha->dpc_flags);
3979 status = 0;
3980 } else { /* schedule another ISP abort */
3981 ha->isp_abort_cnt--;
3982 DEBUG(printk("qla%ld: ISP abort - "
3983 "retry remaining %d\n",
3984 vha->host_no, ha->isp_abort_cnt));
3985 status = 1;
3987 } else {
3988 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3989 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3990 "- retrying (%d) more times\n",
3991 vha->host_no, ha->isp_abort_cnt));
3992 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3993 status = 1;
3999 if (!status) {
4000 DEBUG(printk(KERN_INFO
4001 "qla2x00_abort_isp(%ld): succeeded.\n",
4002 vha->host_no));
4003 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
4004 if (vp->vp_idx)
4005 qla2x00_vp_abort_isp(vp);
4007 } else {
4008 qla_printk(KERN_INFO, ha,
4009 "qla2x00_abort_isp: **** FAILED ****\n");
4012 return(status);
4016 * qla2x00_restart_isp
4017 * restarts the ISP after a reset
4019 * Input:
4020 * ha = adapter block pointer.
4022 * Returns:
4023 * 0 = success
4025 static int
4026 qla2x00_restart_isp(scsi_qla_host_t *vha)
4028 int status = 0;
4029 uint32_t wait_time;
4030 struct qla_hw_data *ha = vha->hw;
4031 struct req_que *req = ha->req_q_map[0];
4032 struct rsp_que *rsp = ha->rsp_q_map[0];
4034 /* If firmware needs to be loaded */
4035 if (qla2x00_isp_firmware(vha)) {
4036 vha->flags.online = 0;
4037 status = ha->isp_ops->chip_diag(vha);
4038 if (!status)
4039 status = qla2x00_setup_chip(vha);
4042 if (!status && !(status = qla2x00_init_rings(vha))) {
4043 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
4044 ha->flags.chip_reset_done = 1;
4045 /* Initialize the queues in use */
4046 qla25xx_init_queues(ha);
4048 status = qla2x00_fw_ready(vha);
4049 if (!status) {
4050 DEBUG(printk("%s(): Start configure loop, "
4051 "status = %d\n", __func__, status));
4053 /* Issue a marker after FW becomes ready. */
4054 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
4056 vha->flags.online = 1;
4057 /* Wait at most MAX_TARGET RSCNs for a stable link. */
4058 wait_time = 256;
4059 do {
4060 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4061 qla2x00_configure_loop(vha);
4062 wait_time--;
4063 } while (!atomic_read(&vha->loop_down_timer) &&
4064 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
4065 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
4066 &vha->dpc_flags)));
4069 /* if no cable then assume it's good */
4070 if ((vha->device_flags & DFLG_NO_CABLE))
4071 status = 0;
4073 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
4074 __func__,
4075 status));
4077 return (status);
4080 static int
4081 qla25xx_init_queues(struct qla_hw_data *ha)
4083 struct rsp_que *rsp = NULL;
4084 struct req_que *req = NULL;
4085 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
4086 int ret = -1;
4087 int i;
4089 for (i = 1; i < ha->max_rsp_queues; i++) {
4090 rsp = ha->rsp_q_map[i];
4091 if (rsp) {
4092 rsp->options &= ~BIT_0;
4093 ret = qla25xx_init_rsp_que(base_vha, rsp);
4094 if (ret != QLA_SUCCESS)
4095 DEBUG2_17(printk(KERN_WARNING
4096 "%s Rsp que:%d init failed\n", __func__,
4097 rsp->id));
4098 else
4099 DEBUG2_17(printk(KERN_INFO
4100 "%s Rsp que:%d inited\n", __func__,
4101 rsp->id));
4104 for (i = 1; i < ha->max_req_queues; i++) {
4105 req = ha->req_q_map[i];
4106 if (req) {
4107 /* Clear outstanding commands array. */
4108 req->options &= ~BIT_0;
4109 ret = qla25xx_init_req_que(base_vha, req);
4110 if (ret != QLA_SUCCESS)
4111 DEBUG2_17(printk(KERN_WARNING
4112 "%s Req que:%d init failed\n", __func__,
4113 req->id));
4114 else
4115 DEBUG2_17(printk(KERN_WARNING
4116 "%s Req que:%d inited\n", __func__,
4117 req->id));
4120 return ret;
4124 * qla2x00_reset_adapter
4125 * Reset adapter.
4127 * Input:
4128 * ha = adapter block pointer.
4130 void
4131 qla2x00_reset_adapter(scsi_qla_host_t *vha)
4133 unsigned long flags = 0;
4134 struct qla_hw_data *ha = vha->hw;
4135 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4137 vha->flags.online = 0;
4138 ha->isp_ops->disable_intrs(ha);
4140 spin_lock_irqsave(&ha->hardware_lock, flags);
4141 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
4142 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
4143 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
4144 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
4145 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4148 void
4149 qla24xx_reset_adapter(scsi_qla_host_t *vha)
4151 unsigned long flags = 0;
4152 struct qla_hw_data *ha = vha->hw;
4153 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
4155 if (IS_QLA82XX(ha))
4156 return;
4158 vha->flags.online = 0;
4159 ha->isp_ops->disable_intrs(ha);
4161 spin_lock_irqsave(&ha->hardware_lock, flags);
4162 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
4163 RD_REG_DWORD(&reg->hccr);
4164 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
4165 RD_REG_DWORD(&reg->hccr);
4166 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4168 if (IS_NOPOLLING_TYPE(ha))
4169 ha->isp_ops->enable_intrs(ha);
4172 /* On sparc systems, obtain port and node WWN from firmware
4173 * properties.
4175 static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
4176 struct nvram_24xx *nv)
4178 #ifdef CONFIG_SPARC
4179 struct qla_hw_data *ha = vha->hw;
4180 struct pci_dev *pdev = ha->pdev;
4181 struct device_node *dp = pci_device_to_OF_node(pdev);
4182 const u8 *val;
4183 int len;
4185 val = of_get_property(dp, "port-wwn", &len);
4186 if (val && len >= WWN_SIZE)
4187 memcpy(nv->port_name, val, WWN_SIZE);
4189 val = of_get_property(dp, "node-wwn", &len);
4190 if (val && len >= WWN_SIZE)
4191 memcpy(nv->node_name, val, WWN_SIZE);
4192 #endif
4196 qla24xx_nvram_config(scsi_qla_host_t *vha)
4198 int rval;
4199 struct init_cb_24xx *icb;
4200 struct nvram_24xx *nv;
4201 uint32_t *dptr;
4202 uint8_t *dptr1, *dptr2;
4203 uint32_t chksum;
4204 uint16_t cnt;
4205 struct qla_hw_data *ha = vha->hw;
4207 rval = QLA_SUCCESS;
4208 icb = (struct init_cb_24xx *)ha->init_cb;
4209 nv = ha->nvram;
4211 /* Determine NVRAM starting address. */
4212 if (ha->flags.port0) {
4213 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
4214 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
4215 } else {
4216 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
4217 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
4219 ha->nvram_size = sizeof(struct nvram_24xx);
4220 ha->vpd_size = FA_NVRAM_VPD_SIZE;
4221 if (IS_QLA82XX(ha))
4222 ha->vpd_size = FA_VPD_SIZE_82XX;
4224 /* Get VPD data into cache */
4225 ha->vpd = ha->nvram + VPD_OFFSET;
4226 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
4227 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
4229 /* Get NVRAM data into cache and calculate checksum. */
4230 dptr = (uint32_t *)nv;
4231 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
4232 ha->nvram_size);
4233 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
4234 chksum += le32_to_cpu(*dptr++);
4236 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
4237 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
4239 /* Bad NVRAM data, set defaults parameters. */
4240 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
4241 || nv->id[3] != ' ' ||
4242 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
4243 /* Reset NVRAM data. */
4244 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
4245 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
4246 le16_to_cpu(nv->nvram_version));
4247 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
4248 "invalid -- WWPN) defaults.\n");
4251 * Set default initialization control block.
4253 memset(nv, 0, ha->nvram_size);
4254 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
4255 nv->version = __constant_cpu_to_le16(ICB_VERSION);
4256 nv->frame_payload_size = __constant_cpu_to_le16(2048);
4257 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
4258 nv->exchange_count = __constant_cpu_to_le16(0);
4259 nv->hard_address = __constant_cpu_to_le16(124);
4260 nv->port_name[0] = 0x21;
4261 nv->port_name[1] = 0x00 + ha->port_no;
4262 nv->port_name[2] = 0x00;
4263 nv->port_name[3] = 0xe0;
4264 nv->port_name[4] = 0x8b;
4265 nv->port_name[5] = 0x1c;
4266 nv->port_name[6] = 0x55;
4267 nv->port_name[7] = 0x86;
4268 nv->node_name[0] = 0x20;
4269 nv->node_name[1] = 0x00;
4270 nv->node_name[2] = 0x00;
4271 nv->node_name[3] = 0xe0;
4272 nv->node_name[4] = 0x8b;
4273 nv->node_name[5] = 0x1c;
4274 nv->node_name[6] = 0x55;
4275 nv->node_name[7] = 0x86;
4276 qla24xx_nvram_wwn_from_ofw(vha, nv);
4277 nv->login_retry_count = __constant_cpu_to_le16(8);
4278 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
4279 nv->login_timeout = __constant_cpu_to_le16(0);
4280 nv->firmware_options_1 =
4281 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
4282 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
4283 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4284 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
4285 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
4286 nv->efi_parameters = __constant_cpu_to_le32(0);
4287 nv->reset_delay = 5;
4288 nv->max_luns_per_target = __constant_cpu_to_le16(128);
4289 nv->port_down_retry_count = __constant_cpu_to_le16(30);
4290 nv->link_down_timeout = __constant_cpu_to_le16(30);
4292 rval = 1;
4295 /* Reset Initialization control block */
4296 memset(icb, 0, ha->init_cb_size);
4298 /* Copy 1st segment. */
4299 dptr1 = (uint8_t *)icb;
4300 dptr2 = (uint8_t *)&nv->version;
4301 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
4302 while (cnt--)
4303 *dptr1++ = *dptr2++;
4305 icb->login_retry_count = nv->login_retry_count;
4306 icb->link_down_on_nos = nv->link_down_on_nos;
4308 /* Copy 2nd segment. */
4309 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
4310 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
4311 cnt = (uint8_t *)&icb->reserved_3 -
4312 (uint8_t *)&icb->interrupt_delay_timer;
4313 while (cnt--)
4314 *dptr1++ = *dptr2++;
4317 * Setup driver NVRAM options.
4319 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
4320 "QLA2462");
4322 /* Use alternate WWN? */
4323 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
4324 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4325 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4328 /* Prepare nodename */
4329 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
4331 * Firmware will apply the following mask if the nodename was
4332 * not provided.
4334 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4335 icb->node_name[0] &= 0xF0;
4338 /* Set host adapter parameters. */
4339 ha->flags.disable_risc_code_load = 0;
4340 ha->flags.enable_lip_reset = 0;
4341 ha->flags.enable_lip_full_login =
4342 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
4343 ha->flags.enable_target_reset =
4344 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
4345 ha->flags.enable_led_scheme = 0;
4346 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
4348 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
4349 (BIT_6 | BIT_5 | BIT_4)) >> 4;
4351 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
4352 sizeof(ha->fw_seriallink_options24));
4354 /* save HBA serial number */
4355 ha->serial0 = icb->port_name[5];
4356 ha->serial1 = icb->port_name[6];
4357 ha->serial2 = icb->port_name[7];
4358 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4359 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
4361 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
4363 ha->retry_count = le16_to_cpu(nv->login_retry_count);
4365 /* Set minimum login_timeout to 4 seconds. */
4366 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
4367 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
4368 if (le16_to_cpu(nv->login_timeout) < 4)
4369 nv->login_timeout = __constant_cpu_to_le16(4);
4370 ha->login_timeout = le16_to_cpu(nv->login_timeout);
4371 icb->login_timeout = nv->login_timeout;
4373 /* Set minimum RATOV to 100 tenths of a second. */
4374 ha->r_a_tov = 100;
4376 ha->loop_reset_delay = nv->reset_delay;
4378 /* Link Down Timeout = 0:
4380 * When Port Down timer expires we will start returning
4381 * I/O's to OS with "DID_NO_CONNECT".
4383 * Link Down Timeout != 0:
4385 * The driver waits for the link to come up after link down
4386 * before returning I/Os to OS with "DID_NO_CONNECT".
4388 if (le16_to_cpu(nv->link_down_timeout) == 0) {
4389 ha->loop_down_abort_time =
4390 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
4391 } else {
4392 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
4393 ha->loop_down_abort_time =
4394 (LOOP_DOWN_TIME - ha->link_down_timeout);
4397 /* Need enough time to try and get the port back. */
4398 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
4399 if (qlport_down_retry)
4400 ha->port_down_retry_count = qlport_down_retry;
4402 /* Set login_retry_count */
4403 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
4404 if (ha->port_down_retry_count ==
4405 le16_to_cpu(nv->port_down_retry_count) &&
4406 ha->port_down_retry_count > 3)
4407 ha->login_retry_count = ha->port_down_retry_count;
4408 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4409 ha->login_retry_count = ha->port_down_retry_count;
4410 if (ql2xloginretrycount)
4411 ha->login_retry_count = ql2xloginretrycount;
4413 /* Enable ZIO. */
4414 if (!vha->flags.init_done) {
4415 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
4416 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4417 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
4418 le16_to_cpu(icb->interrupt_delay_timer): 2;
4420 icb->firmware_options_2 &= __constant_cpu_to_le32(
4421 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
4422 vha->flags.process_response_queue = 0;
4423 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4424 ha->zio_mode = QLA_ZIO_MODE_6;
4426 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
4427 "(%d us).\n", vha->host_no, ha->zio_mode,
4428 ha->zio_timer * 100));
4429 qla_printk(KERN_INFO, ha,
4430 "ZIO mode %d enabled; timer delay (%d us).\n",
4431 ha->zio_mode, ha->zio_timer * 100);
4433 icb->firmware_options_2 |= cpu_to_le32(
4434 (uint32_t)ha->zio_mode);
4435 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
4436 vha->flags.process_response_queue = 1;
4439 if (rval) {
4440 DEBUG2_3(printk(KERN_WARNING
4441 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
4443 return (rval);
4446 static int
4447 qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
4448 uint32_t faddr)
4450 int rval = QLA_SUCCESS;
4451 int segments, fragment;
4452 uint32_t *dcode, dlen;
4453 uint32_t risc_addr;
4454 uint32_t risc_size;
4455 uint32_t i;
4456 struct qla_hw_data *ha = vha->hw;
4457 struct req_que *req = ha->req_q_map[0];
4459 qla_printk(KERN_INFO, ha,
4460 "FW: Loading from flash (%x)...\n", faddr);
4462 rval = QLA_SUCCESS;
4464 segments = FA_RISC_CODE_SEGMENTS;
4465 dcode = (uint32_t *)req->ring;
4466 *srisc_addr = 0;
4468 /* Validate firmware image by checking version. */
4469 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
4470 for (i = 0; i < 4; i++)
4471 dcode[i] = be32_to_cpu(dcode[i]);
4472 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
4473 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
4474 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
4475 dcode[3] == 0)) {
4476 qla_printk(KERN_WARNING, ha,
4477 "Unable to verify integrity of flash firmware image!\n");
4478 qla_printk(KERN_WARNING, ha,
4479 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
4480 dcode[1], dcode[2], dcode[3]);
4482 return QLA_FUNCTION_FAILED;
4485 while (segments && rval == QLA_SUCCESS) {
4486 /* Read segment's load information. */
4487 qla24xx_read_flash_data(vha, dcode, faddr, 4);
4489 risc_addr = be32_to_cpu(dcode[2]);
4490 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
4491 risc_size = be32_to_cpu(dcode[3]);
4493 fragment = 0;
4494 while (risc_size > 0 && rval == QLA_SUCCESS) {
4495 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
4496 if (dlen > risc_size)
4497 dlen = risc_size;
4499 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
4500 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
4501 vha->host_no, risc_addr, dlen, faddr));
4503 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
4504 for (i = 0; i < dlen; i++)
4505 dcode[i] = swab32(dcode[i]);
4507 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
4508 dlen);
4509 if (rval) {
4510 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
4511 "segment %d of firmware\n", vha->host_no,
4512 fragment));
4513 qla_printk(KERN_WARNING, ha,
4514 "[ERROR] Failed to load segment %d of "
4515 "firmware\n", fragment);
4516 break;
4519 faddr += dlen;
4520 risc_addr += dlen;
4521 risc_size -= dlen;
4522 fragment++;
4525 /* Next segment. */
4526 segments--;
4529 return rval;
4532 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
4535 qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4537 int rval;
4538 int i, fragment;
4539 uint16_t *wcode, *fwcode;
4540 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
4541 struct fw_blob *blob;
4542 struct qla_hw_data *ha = vha->hw;
4543 struct req_que *req = ha->req_q_map[0];
4545 /* Load firmware blob. */
4546 blob = qla2x00_request_firmware(vha);
4547 if (!blob) {
4548 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
4549 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
4550 "from: " QLA_FW_URL ".\n");
4551 return QLA_FUNCTION_FAILED;
4554 rval = QLA_SUCCESS;
4556 wcode = (uint16_t *)req->ring;
4557 *srisc_addr = 0;
4558 fwcode = (uint16_t *)blob->fw->data;
4559 fwclen = 0;
4561 /* Validate firmware image by checking version. */
4562 if (blob->fw->size < 8 * sizeof(uint16_t)) {
4563 qla_printk(KERN_WARNING, ha,
4564 "Unable to verify integrity of firmware image (%Zd)!\n",
4565 blob->fw->size);
4566 goto fail_fw_integrity;
4568 for (i = 0; i < 4; i++)
4569 wcode[i] = be16_to_cpu(fwcode[i + 4]);
4570 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
4571 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
4572 wcode[2] == 0 && wcode[3] == 0)) {
4573 qla_printk(KERN_WARNING, ha,
4574 "Unable to verify integrity of firmware image!\n");
4575 qla_printk(KERN_WARNING, ha,
4576 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
4577 wcode[1], wcode[2], wcode[3]);
4578 goto fail_fw_integrity;
4581 seg = blob->segs;
4582 while (*seg && rval == QLA_SUCCESS) {
4583 risc_addr = *seg;
4584 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
4585 risc_size = be16_to_cpu(fwcode[3]);
4587 /* Validate firmware image size. */
4588 fwclen += risc_size * sizeof(uint16_t);
4589 if (blob->fw->size < fwclen) {
4590 qla_printk(KERN_WARNING, ha,
4591 "Unable to verify integrity of firmware image "
4592 "(%Zd)!\n", blob->fw->size);
4593 goto fail_fw_integrity;
4596 fragment = 0;
4597 while (risc_size > 0 && rval == QLA_SUCCESS) {
4598 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
4599 if (wlen > risc_size)
4600 wlen = risc_size;
4602 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
4603 "addr %x, number of words 0x%x.\n", vha->host_no,
4604 risc_addr, wlen));
4606 for (i = 0; i < wlen; i++)
4607 wcode[i] = swab16(fwcode[i]);
4609 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
4610 wlen);
4611 if (rval) {
4612 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
4613 "segment %d of firmware\n", vha->host_no,
4614 fragment));
4615 qla_printk(KERN_WARNING, ha,
4616 "[ERROR] Failed to load segment %d of "
4617 "firmware\n", fragment);
4618 break;
4621 fwcode += wlen;
4622 risc_addr += wlen;
4623 risc_size -= wlen;
4624 fragment++;
4627 /* Next segment. */
4628 seg++;
4630 return rval;
4632 fail_fw_integrity:
4633 return QLA_FUNCTION_FAILED;
4636 static int
4637 qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4639 int rval;
4640 int segments, fragment;
4641 uint32_t *dcode, dlen;
4642 uint32_t risc_addr;
4643 uint32_t risc_size;
4644 uint32_t i;
4645 struct fw_blob *blob;
4646 uint32_t *fwcode, fwclen;
4647 struct qla_hw_data *ha = vha->hw;
4648 struct req_que *req = ha->req_q_map[0];
4650 /* Load firmware blob. */
4651 blob = qla2x00_request_firmware(vha);
4652 if (!blob) {
4653 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
4654 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
4655 "from: " QLA_FW_URL ".\n");
4657 return QLA_FUNCTION_FAILED;
4660 qla_printk(KERN_INFO, ha,
4661 "FW: Loading via request-firmware...\n");
4663 rval = QLA_SUCCESS;
4665 segments = FA_RISC_CODE_SEGMENTS;
4666 dcode = (uint32_t *)req->ring;
4667 *srisc_addr = 0;
4668 fwcode = (uint32_t *)blob->fw->data;
4669 fwclen = 0;
4671 /* Validate firmware image by checking version. */
4672 if (blob->fw->size < 8 * sizeof(uint32_t)) {
4673 qla_printk(KERN_WARNING, ha,
4674 "Unable to verify integrity of firmware image (%Zd)!\n",
4675 blob->fw->size);
4676 goto fail_fw_integrity;
4678 for (i = 0; i < 4; i++)
4679 dcode[i] = be32_to_cpu(fwcode[i + 4]);
4680 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
4681 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
4682 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
4683 dcode[3] == 0)) {
4684 qla_printk(KERN_WARNING, ha,
4685 "Unable to verify integrity of firmware image!\n");
4686 qla_printk(KERN_WARNING, ha,
4687 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
4688 dcode[1], dcode[2], dcode[3]);
4689 goto fail_fw_integrity;
4692 while (segments && rval == QLA_SUCCESS) {
4693 risc_addr = be32_to_cpu(fwcode[2]);
4694 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
4695 risc_size = be32_to_cpu(fwcode[3]);
4697 /* Validate firmware image size. */
4698 fwclen += risc_size * sizeof(uint32_t);
4699 if (blob->fw->size < fwclen) {
4700 qla_printk(KERN_WARNING, ha,
4701 "Unable to verify integrity of firmware image "
4702 "(%Zd)!\n", blob->fw->size);
4704 goto fail_fw_integrity;
4707 fragment = 0;
4708 while (risc_size > 0 && rval == QLA_SUCCESS) {
4709 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
4710 if (dlen > risc_size)
4711 dlen = risc_size;
4713 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
4714 "addr %x, number of dwords 0x%x.\n", vha->host_no,
4715 risc_addr, dlen));
4717 for (i = 0; i < dlen; i++)
4718 dcode[i] = swab32(fwcode[i]);
4720 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
4721 dlen);
4722 if (rval) {
4723 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
4724 "segment %d of firmware\n", vha->host_no,
4725 fragment));
4726 qla_printk(KERN_WARNING, ha,
4727 "[ERROR] Failed to load segment %d of "
4728 "firmware\n", fragment);
4729 break;
4732 fwcode += dlen;
4733 risc_addr += dlen;
4734 risc_size -= dlen;
4735 fragment++;
4738 /* Next segment. */
4739 segments--;
4741 return rval;
4743 fail_fw_integrity:
4744 return QLA_FUNCTION_FAILED;
4748 qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4750 int rval;
4752 if (ql2xfwloadbin == 1)
4753 return qla81xx_load_risc(vha, srisc_addr);
4756 * FW Load priority:
4757 * 1) Firmware via request-firmware interface (.bin file).
4758 * 2) Firmware residing in flash.
4760 rval = qla24xx_load_risc_blob(vha, srisc_addr);
4761 if (rval == QLA_SUCCESS)
4762 return rval;
4764 return qla24xx_load_risc_flash(vha, srisc_addr,
4765 vha->hw->flt_region_fw);
4769 qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4771 int rval;
4772 struct qla_hw_data *ha = vha->hw;
4774 if (ql2xfwloadbin == 2)
4775 goto try_blob_fw;
4778 * FW Load priority:
4779 * 1) Firmware residing in flash.
4780 * 2) Firmware via request-firmware interface (.bin file).
4781 * 3) Golden-Firmware residing in flash -- limited operation.
4783 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
4784 if (rval == QLA_SUCCESS)
4785 return rval;
4787 try_blob_fw:
4788 rval = qla24xx_load_risc_blob(vha, srisc_addr);
4789 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
4790 return rval;
4792 qla_printk(KERN_ERR, ha,
4793 "FW: Attempting to fallback to golden firmware...\n");
4794 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
4795 if (rval != QLA_SUCCESS)
4796 return rval;
4798 qla_printk(KERN_ERR, ha,
4799 "FW: Please update operational firmware...\n");
4800 ha->flags.running_gold_fw = 1;
4802 return rval;
4805 void
4806 qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
4808 int ret, retries;
4809 struct qla_hw_data *ha = vha->hw;
4811 if (ha->flags.pci_channel_io_perm_failure)
4812 return;
4813 if (!IS_FWI2_CAPABLE(ha))
4814 return;
4815 if (!ha->fw_major_version)
4816 return;
4818 ret = qla2x00_stop_firmware(vha);
4819 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
4820 ret != QLA_INVALID_COMMAND && retries ; retries--) {
4821 ha->isp_ops->reset_chip(vha);
4822 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
4823 continue;
4824 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
4825 continue;
4826 qla_printk(KERN_INFO, ha,
4827 "Attempting retry of stop-firmware command...\n");
4828 ret = qla2x00_stop_firmware(vha);
4833 qla24xx_configure_vhba(scsi_qla_host_t *vha)
4835 int rval = QLA_SUCCESS;
4836 uint16_t mb[MAILBOX_REGISTER_COUNT];
4837 struct qla_hw_data *ha = vha->hw;
4838 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
4839 struct req_que *req;
4840 struct rsp_que *rsp;
4842 if (!vha->vp_idx)
4843 return -EINVAL;
4845 rval = qla2x00_fw_ready(base_vha);
4846 if (ha->flags.cpu_affinity_enabled)
4847 req = ha->req_q_map[0];
4848 else
4849 req = vha->req;
4850 rsp = req->rsp;
4852 if (rval == QLA_SUCCESS) {
4853 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
4854 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
4857 vha->flags.management_server_logged_in = 0;
4859 /* Login to SNS first */
4860 ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb, BIT_1);
4861 if (mb[0] != MBS_COMMAND_COMPLETE) {
4862 DEBUG15(qla_printk(KERN_INFO, ha,
4863 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4864 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4865 mb[0], mb[1], mb[2], mb[6], mb[7]));
4866 return (QLA_FUNCTION_FAILED);
4869 atomic_set(&vha->loop_down_timer, 0);
4870 atomic_set(&vha->loop_state, LOOP_UP);
4871 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4872 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4873 rval = qla2x00_loop_resync(base_vha);
4875 return rval;
4878 /* 84XX Support **************************************************************/
4880 static LIST_HEAD(qla_cs84xx_list);
4881 static DEFINE_MUTEX(qla_cs84xx_mutex);
4883 static struct qla_chip_state_84xx *
4884 qla84xx_get_chip(struct scsi_qla_host *vha)
4886 struct qla_chip_state_84xx *cs84xx;
4887 struct qla_hw_data *ha = vha->hw;
4889 mutex_lock(&qla_cs84xx_mutex);
4891 /* Find any shared 84xx chip. */
4892 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
4893 if (cs84xx->bus == ha->pdev->bus) {
4894 kref_get(&cs84xx->kref);
4895 goto done;
4899 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
4900 if (!cs84xx)
4901 goto done;
4903 kref_init(&cs84xx->kref);
4904 spin_lock_init(&cs84xx->access_lock);
4905 mutex_init(&cs84xx->fw_update_mutex);
4906 cs84xx->bus = ha->pdev->bus;
4908 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
4909 done:
4910 mutex_unlock(&qla_cs84xx_mutex);
4911 return cs84xx;
4914 static void
4915 __qla84xx_chip_release(struct kref *kref)
4917 struct qla_chip_state_84xx *cs84xx =
4918 container_of(kref, struct qla_chip_state_84xx, kref);
4920 mutex_lock(&qla_cs84xx_mutex);
4921 list_del(&cs84xx->list);
4922 mutex_unlock(&qla_cs84xx_mutex);
4923 kfree(cs84xx);
4926 void
4927 qla84xx_put_chip(struct scsi_qla_host *vha)
4929 struct qla_hw_data *ha = vha->hw;
4930 if (ha->cs84xx)
4931 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
4934 static int
4935 qla84xx_init_chip(scsi_qla_host_t *vha)
4937 int rval;
4938 uint16_t status[2];
4939 struct qla_hw_data *ha = vha->hw;
4941 mutex_lock(&ha->cs84xx->fw_update_mutex);
4943 rval = qla84xx_verify_chip(vha, status);
4945 mutex_unlock(&ha->cs84xx->fw_update_mutex);
4947 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
4948 QLA_SUCCESS;
4951 /* 81XX Support **************************************************************/
4954 qla81xx_nvram_config(scsi_qla_host_t *vha)
4956 int rval;
4957 struct init_cb_81xx *icb;
4958 struct nvram_81xx *nv;
4959 uint32_t *dptr;
4960 uint8_t *dptr1, *dptr2;
4961 uint32_t chksum;
4962 uint16_t cnt;
4963 struct qla_hw_data *ha = vha->hw;
4965 rval = QLA_SUCCESS;
4966 icb = (struct init_cb_81xx *)ha->init_cb;
4967 nv = ha->nvram;
4969 /* Determine NVRAM starting address. */
4970 ha->nvram_size = sizeof(struct nvram_81xx);
4971 ha->vpd_size = FA_NVRAM_VPD_SIZE;
4973 /* Get VPD data into cache */
4974 ha->vpd = ha->nvram + VPD_OFFSET;
4975 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
4976 ha->vpd_size);
4978 /* Get NVRAM data into cache and calculate checksum. */
4979 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
4980 ha->nvram_size);
4981 dptr = (uint32_t *)nv;
4982 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
4983 chksum += le32_to_cpu(*dptr++);
4985 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
4986 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
4988 /* Bad NVRAM data, set defaults parameters. */
4989 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
4990 || nv->id[3] != ' ' ||
4991 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
4992 /* Reset NVRAM data. */
4993 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
4994 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
4995 le16_to_cpu(nv->nvram_version));
4996 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
4997 "invalid -- WWPN) defaults.\n");
5000 * Set default initialization control block.
5002 memset(nv, 0, ha->nvram_size);
5003 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
5004 nv->version = __constant_cpu_to_le16(ICB_VERSION);
5005 nv->frame_payload_size = __constant_cpu_to_le16(2048);
5006 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
5007 nv->exchange_count = __constant_cpu_to_le16(0);
5008 nv->port_name[0] = 0x21;
5009 nv->port_name[1] = 0x00 + ha->port_no;
5010 nv->port_name[2] = 0x00;
5011 nv->port_name[3] = 0xe0;
5012 nv->port_name[4] = 0x8b;
5013 nv->port_name[5] = 0x1c;
5014 nv->port_name[6] = 0x55;
5015 nv->port_name[7] = 0x86;
5016 nv->node_name[0] = 0x20;
5017 nv->node_name[1] = 0x00;
5018 nv->node_name[2] = 0x00;
5019 nv->node_name[3] = 0xe0;
5020 nv->node_name[4] = 0x8b;
5021 nv->node_name[5] = 0x1c;
5022 nv->node_name[6] = 0x55;
5023 nv->node_name[7] = 0x86;
5024 nv->login_retry_count = __constant_cpu_to_le16(8);
5025 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
5026 nv->login_timeout = __constant_cpu_to_le16(0);
5027 nv->firmware_options_1 =
5028 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
5029 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
5030 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
5031 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
5032 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
5033 nv->efi_parameters = __constant_cpu_to_le32(0);
5034 nv->reset_delay = 5;
5035 nv->max_luns_per_target = __constant_cpu_to_le16(128);
5036 nv->port_down_retry_count = __constant_cpu_to_le16(30);
5037 nv->link_down_timeout = __constant_cpu_to_le16(30);
5038 nv->enode_mac[0] = 0x00;
5039 nv->enode_mac[1] = 0x02;
5040 nv->enode_mac[2] = 0x03;
5041 nv->enode_mac[3] = 0x04;
5042 nv->enode_mac[4] = 0x05;
5043 nv->enode_mac[5] = 0x06 + ha->port_no;
5045 rval = 1;
5048 /* Reset Initialization control block */
5049 memset(icb, 0, sizeof(struct init_cb_81xx));
5051 /* Copy 1st segment. */
5052 dptr1 = (uint8_t *)icb;
5053 dptr2 = (uint8_t *)&nv->version;
5054 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
5055 while (cnt--)
5056 *dptr1++ = *dptr2++;
5058 icb->login_retry_count = nv->login_retry_count;
5060 /* Copy 2nd segment. */
5061 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
5062 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
5063 cnt = (uint8_t *)&icb->reserved_5 -
5064 (uint8_t *)&icb->interrupt_delay_timer;
5065 while (cnt--)
5066 *dptr1++ = *dptr2++;
5068 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
5069 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
5070 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
5071 icb->enode_mac[0] = 0x01;
5072 icb->enode_mac[1] = 0x02;
5073 icb->enode_mac[2] = 0x03;
5074 icb->enode_mac[3] = 0x04;
5075 icb->enode_mac[4] = 0x05;
5076 icb->enode_mac[5] = 0x06 + ha->port_no;
5079 /* Use extended-initialization control block. */
5080 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
5083 * Setup driver NVRAM options.
5085 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
5086 "QLE8XXX");
5088 /* Use alternate WWN? */
5089 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
5090 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
5091 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
5094 /* Prepare nodename */
5095 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
5097 * Firmware will apply the following mask if the nodename was
5098 * not provided.
5100 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
5101 icb->node_name[0] &= 0xF0;
5104 /* Set host adapter parameters. */
5105 ha->flags.disable_risc_code_load = 0;
5106 ha->flags.enable_lip_reset = 0;
5107 ha->flags.enable_lip_full_login =
5108 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
5109 ha->flags.enable_target_reset =
5110 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
5111 ha->flags.enable_led_scheme = 0;
5112 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
5114 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
5115 (BIT_6 | BIT_5 | BIT_4)) >> 4;
5117 /* save HBA serial number */
5118 ha->serial0 = icb->port_name[5];
5119 ha->serial1 = icb->port_name[6];
5120 ha->serial2 = icb->port_name[7];
5121 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
5122 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
5124 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
5126 ha->retry_count = le16_to_cpu(nv->login_retry_count);
5128 /* Set minimum login_timeout to 4 seconds. */
5129 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
5130 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
5131 if (le16_to_cpu(nv->login_timeout) < 4)
5132 nv->login_timeout = __constant_cpu_to_le16(4);
5133 ha->login_timeout = le16_to_cpu(nv->login_timeout);
5134 icb->login_timeout = nv->login_timeout;
5136 /* Set minimum RATOV to 100 tenths of a second. */
5137 ha->r_a_tov = 100;
5139 ha->loop_reset_delay = nv->reset_delay;
5141 /* Link Down Timeout = 0:
5143 * When Port Down timer expires we will start returning
5144 * I/O's to OS with "DID_NO_CONNECT".
5146 * Link Down Timeout != 0:
5148 * The driver waits for the link to come up after link down
5149 * before returning I/Os to OS with "DID_NO_CONNECT".
5151 if (le16_to_cpu(nv->link_down_timeout) == 0) {
5152 ha->loop_down_abort_time =
5153 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
5154 } else {
5155 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
5156 ha->loop_down_abort_time =
5157 (LOOP_DOWN_TIME - ha->link_down_timeout);
5160 /* Need enough time to try and get the port back. */
5161 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
5162 if (qlport_down_retry)
5163 ha->port_down_retry_count = qlport_down_retry;
5165 /* Set login_retry_count */
5166 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
5167 if (ha->port_down_retry_count ==
5168 le16_to_cpu(nv->port_down_retry_count) &&
5169 ha->port_down_retry_count > 3)
5170 ha->login_retry_count = ha->port_down_retry_count;
5171 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
5172 ha->login_retry_count = ha->port_down_retry_count;
5173 if (ql2xloginretrycount)
5174 ha->login_retry_count = ql2xloginretrycount;
5176 /* Enable ZIO. */
5177 if (!vha->flags.init_done) {
5178 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
5179 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
5180 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
5181 le16_to_cpu(icb->interrupt_delay_timer): 2;
5183 icb->firmware_options_2 &= __constant_cpu_to_le32(
5184 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
5185 vha->flags.process_response_queue = 0;
5186 if (ha->zio_mode != QLA_ZIO_DISABLED) {
5187 ha->zio_mode = QLA_ZIO_MODE_6;
5189 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
5190 "(%d us).\n", vha->host_no, ha->zio_mode,
5191 ha->zio_timer * 100));
5192 qla_printk(KERN_INFO, ha,
5193 "ZIO mode %d enabled; timer delay (%d us).\n",
5194 ha->zio_mode, ha->zio_timer * 100);
5196 icb->firmware_options_2 |= cpu_to_le32(
5197 (uint32_t)ha->zio_mode);
5198 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
5199 vha->flags.process_response_queue = 1;
5202 if (rval) {
5203 DEBUG2_3(printk(KERN_WARNING
5204 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
5206 return (rval);
5210 qla82xx_restart_isp(scsi_qla_host_t *vha)
5212 int status, rval;
5213 uint32_t wait_time;
5214 struct qla_hw_data *ha = vha->hw;
5215 struct req_que *req = ha->req_q_map[0];
5216 struct rsp_que *rsp = ha->rsp_q_map[0];
5217 struct scsi_qla_host *vp;
5218 struct scsi_qla_host *tvp;
5220 status = qla2x00_init_rings(vha);
5221 if (!status) {
5222 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
5223 ha->flags.chip_reset_done = 1;
5225 status = qla2x00_fw_ready(vha);
5226 if (!status) {
5227 qla_printk(KERN_INFO, ha,
5228 "%s(): Start configure loop, "
5229 "status = %d\n", __func__, status);
5231 /* Issue a marker after FW becomes ready. */
5232 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
5234 vha->flags.online = 1;
5235 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5236 wait_time = 256;
5237 do {
5238 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5239 qla2x00_configure_loop(vha);
5240 wait_time--;
5241 } while (!atomic_read(&vha->loop_down_timer) &&
5242 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags)) &&
5243 wait_time &&
5244 (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)));
5247 /* if no cable then assume it's good */
5248 if ((vha->device_flags & DFLG_NO_CABLE))
5249 status = 0;
5251 qla_printk(KERN_INFO, ha,
5252 "%s(): Configure loop done, status = 0x%x\n",
5253 __func__, status);
5256 if (!status) {
5257 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
5259 if (!atomic_read(&vha->loop_down_timer)) {
5261 * Issue marker command only when we are going
5262 * to start the I/O .
5264 vha->marker_needed = 1;
5267 vha->flags.online = 1;
5269 ha->isp_ops->enable_intrs(ha);
5271 ha->isp_abort_cnt = 0;
5272 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5274 if (ha->fce) {
5275 ha->flags.fce_enabled = 1;
5276 memset(ha->fce, 0,
5277 fce_calc_size(ha->fce_bufs));
5278 rval = qla2x00_enable_fce_trace(vha,
5279 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
5280 &ha->fce_bufs);
5281 if (rval) {
5282 qla_printk(KERN_WARNING, ha,
5283 "Unable to reinitialize FCE "
5284 "(%d).\n", rval);
5285 ha->flags.fce_enabled = 0;
5289 if (ha->eft) {
5290 memset(ha->eft, 0, EFT_SIZE);
5291 rval = qla2x00_enable_eft_trace(vha,
5292 ha->eft_dma, EFT_NUM_BUFFERS);
5293 if (rval) {
5294 qla_printk(KERN_WARNING, ha,
5295 "Unable to reinitialize EFT "
5296 "(%d).\n", rval);
5301 if (!status) {
5302 DEBUG(printk(KERN_INFO
5303 "qla82xx_restart_isp(%ld): succeeded.\n",
5304 vha->host_no));
5305 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
5306 if (vp->vp_idx)
5307 qla2x00_vp_abort_isp(vp);
5309 } else {
5310 qla_printk(KERN_INFO, ha,
5311 "qla82xx_restart_isp: **** FAILED ****\n");
5314 return status;
5317 void
5318 qla81xx_update_fw_options(scsi_qla_host_t *vha)
5320 struct qla_hw_data *ha = vha->hw;
5322 if (!ql2xetsenable)
5323 return;
5325 /* Enable ETS Burst. */
5326 memset(ha->fw_options, 0, sizeof(ha->fw_options));
5327 ha->fw_options[2] |= BIT_9;
5328 qla2x00_set_fw_options(vha, ha->fw_options);
5332 * qla24xx_get_fcp_prio
5333 * Gets the fcp cmd priority value for the logged in port.
5334 * Looks for a match of the port descriptors within
5335 * each of the fcp prio config entries. If a match is found,
5336 * the tag (priority) value is returned.
5338 * Input:
5339 * ha = adapter block po
5340 * fcport = port structure pointer.
5342 * Return:
5343 * non-zero (if found)
5344 * 0 (if not found)
5346 * Context:
5347 * Kernel context
5349 uint8_t
5350 qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
5352 int i, entries;
5353 uint8_t pid_match, wwn_match;
5354 uint8_t priority;
5355 uint32_t pid1, pid2;
5356 uint64_t wwn1, wwn2;
5357 struct qla_fcp_prio_entry *pri_entry;
5358 struct qla_hw_data *ha = vha->hw;
5360 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
5361 return 0;
5363 priority = 0;
5364 entries = ha->fcp_prio_cfg->num_entries;
5365 pri_entry = &ha->fcp_prio_cfg->entry[0];
5367 for (i = 0; i < entries; i++) {
5368 pid_match = wwn_match = 0;
5370 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
5371 pri_entry++;
5372 continue;
5375 /* check source pid for a match */
5376 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
5377 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
5378 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
5379 if (pid1 == INVALID_PORT_ID)
5380 pid_match++;
5381 else if (pid1 == pid2)
5382 pid_match++;
5385 /* check destination pid for a match */
5386 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
5387 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
5388 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
5389 if (pid1 == INVALID_PORT_ID)
5390 pid_match++;
5391 else if (pid1 == pid2)
5392 pid_match++;
5395 /* check source WWN for a match */
5396 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
5397 wwn1 = wwn_to_u64(vha->port_name);
5398 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
5399 if (wwn2 == (uint64_t)-1)
5400 wwn_match++;
5401 else if (wwn1 == wwn2)
5402 wwn_match++;
5405 /* check destination WWN for a match */
5406 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
5407 wwn1 = wwn_to_u64(fcport->port_name);
5408 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
5409 if (wwn2 == (uint64_t)-1)
5410 wwn_match++;
5411 else if (wwn1 == wwn2)
5412 wwn_match++;
5415 if (pid_match == 2 || wwn_match == 2) {
5416 /* Found a matching entry */
5417 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
5418 priority = pri_entry->tag;
5419 break;
5422 pri_entry++;
5425 return priority;
5429 * qla24xx_update_fcport_fcp_prio
5430 * Activates fcp priority for the logged in fc port
5432 * Input:
5433 * ha = adapter block pointer.
5434 * fcp = port structure pointer.
5436 * Return:
5437 * QLA_SUCCESS or QLA_FUNCTION_FAILED
5439 * Context:
5440 * Kernel context.
5443 qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *ha, fc_port_t *fcport)
5445 int ret;
5446 uint8_t priority;
5447 uint16_t mb[5];
5449 if (atomic_read(&fcport->state) == FCS_UNCONFIGURED ||
5450 fcport->port_type != FCT_TARGET ||
5451 fcport->loop_id == FC_NO_LOOP_ID)
5452 return QLA_FUNCTION_FAILED;
5454 priority = qla24xx_get_fcp_prio(ha, fcport);
5455 ret = qla24xx_set_fcp_prio(ha, fcport->loop_id, priority, mb);
5456 if (ret == QLA_SUCCESS)
5457 fcport->fcp_prio = priority;
5458 else
5459 DEBUG2(printk(KERN_WARNING
5460 "scsi(%ld): Unable to activate fcp priority, "
5461 " ret=0x%x\n", ha->host_no, ret));
5463 return ret;
5467 * qla24xx_update_all_fcp_prio
5468 * Activates fcp priority for all the logged in ports
5470 * Input:
5471 * ha = adapter block pointer.
5473 * Return:
5474 * QLA_SUCCESS or QLA_FUNCTION_FAILED
5476 * Context:
5477 * Kernel context.
5480 qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
5482 int ret;
5483 fc_port_t *fcport;
5485 ret = QLA_FUNCTION_FAILED;
5486 /* We need to set priority for all logged in ports */
5487 list_for_each_entry(fcport, &vha->vp_fcports, list)
5488 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
5490 return ret;