[SCSI] qla2xxx: Correct maximum supported lun and target-id definitions.
[deliverable/linux.git] / drivers / scsi / qla2xxx / qla_init.c
CommitLineData
1da177e4
LT
1/*
2 * QLOGIC LINUX SOFTWARE
3 *
4 * QLogic ISP2x00 device driver for Linux 2.6.x
ae91193c 5 * Copyright (C) 2003-2005 QLogic Corporation
1da177e4
LT
6 * (www.qlogic.com)
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 */
19#include "qla_def.h"
20
21#include <linux/delay.h>
0107109e
AV
22#include <linux/vmalloc.h>
23#include <linux/firmware.h>
8482e118 24#include <scsi/scsi_transport_fc.h>
1da177e4
LT
25
26#include "qla_devtbl.h"
27
28/* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
29#ifndef EXT_IS_LUN_BIT_SET
30#define EXT_IS_LUN_BIT_SET(P,L) \
31 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
32#define EXT_SET_LUN_BIT(P,L) \
33 ((P)->mask[L/8] |= (0x80 >> (L%8)))
34#endif
35
36/*
37* QLogic ISP2x00 Hardware Support Function Prototypes.
38*/
1da177e4 39static int qla2x00_isp_firmware(scsi_qla_host_t *);
1da177e4
LT
40static void qla2x00_resize_request_q(scsi_qla_host_t *);
41static int qla2x00_setup_chip(scsi_qla_host_t *);
42static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
43static int qla2x00_init_rings(scsi_qla_host_t *);
44static int qla2x00_fw_ready(scsi_qla_host_t *);
45static int qla2x00_configure_hba(scsi_qla_host_t *);
1da177e4
LT
46static int qla2x00_configure_loop(scsi_qla_host_t *);
47static int qla2x00_configure_local_loop(scsi_qla_host_t *);
48static void qla2x00_update_fcport(scsi_qla_host_t *, fc_port_t *);
1da177e4
LT
49static int qla2x00_configure_fabric(scsi_qla_host_t *);
50static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
51static int qla2x00_device_resync(scsi_qla_host_t *);
52static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
53 uint16_t *);
1da177e4
LT
54
55static int qla2x00_restart_isp(scsi_qla_host_t *);
1da177e4
LT
56
57/****************************************************************************/
58/* QLogic ISP2x00 Hardware Support Functions. */
59/****************************************************************************/
60
61/*
62* qla2x00_initialize_adapter
63* Initialize board.
64*
65* Input:
66* ha = adapter block pointer.
67*
68* Returns:
69* 0 = success
70*/
71int
72qla2x00_initialize_adapter(scsi_qla_host_t *ha)
73{
74 int rval;
75 uint8_t restart_risc = 0;
76 uint8_t retry;
77 uint32_t wait_time;
78
79 /* Clear adapter flags. */
80 ha->flags.online = 0;
81 ha->flags.reset_active = 0;
82 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
83 atomic_set(&ha->loop_state, LOOP_DOWN);
84 ha->device_flags = 0;
1da177e4 85 ha->dpc_flags = 0;
1da177e4
LT
86 ha->flags.management_server_logged_in = 0;
87 ha->marker_needed = 0;
88 ha->mbx_flags = 0;
89 ha->isp_abort_cnt = 0;
90 ha->beacon_blink_led = 0;
91
0107109e 92 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
abbd8870 93 rval = ha->isp_ops.pci_config(ha);
1da177e4
LT
94 if (rval) {
95 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
96 ha->host_no));
97 return (rval);
98 }
99
abbd8870 100 ha->isp_ops.reset_chip(ha);
1da177e4 101
1da177e4 102 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
0107109e 103
abbd8870 104 ha->isp_ops.nvram_config(ha);
1da177e4
LT
105
106 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
107
108 retry = 10;
109 /*
110 * Try to configure the loop.
111 */
112 do {
113 restart_risc = 0;
114
115 /* If firmware needs to be loaded */
116 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
abbd8870 117 if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
1da177e4
LT
118 rval = qla2x00_setup_chip(ha);
119 }
120 }
121
122 if (rval == QLA_SUCCESS &&
123 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
124check_fw_ready_again:
125 /*
fa2a1ce5 126 * Wait for a successful LIP up to a maximum
1da177e4
LT
127 * of (in seconds): RISC login timeout value,
128 * RISC retry count value, and port down retry
fa2a1ce5 129 * value OR a minimum of 4 seconds OR If no
1da177e4
LT
130 * cable, only 5 seconds.
131 */
132 rval = qla2x00_fw_ready(ha);
133 if (rval == QLA_SUCCESS) {
134 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
135
0107109e
AV
136 /* Issue a marker after FW becomes ready. */
137 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
138
1da177e4
LT
139 /*
140 * Wait at most MAX_TARGET RSCNs for a stable
141 * link.
142 */
143 wait_time = 256;
144 do {
145 clear_bit(LOOP_RESYNC_NEEDED,
146 &ha->dpc_flags);
147 rval = qla2x00_configure_loop(ha);
148
149 if (test_and_clear_bit(ISP_ABORT_NEEDED,
150 &ha->dpc_flags)) {
151 restart_risc = 1;
152 break;
153 }
154
155 /*
156 * If loop state change while we were
157 * discoverying devices then wait for
158 * LIP to complete
159 */
160
161 if (atomic_read(&ha->loop_state) ==
162 LOOP_DOWN && retry--) {
163 goto check_fw_ready_again;
164 }
165 wait_time--;
166 } while (!atomic_read(&ha->loop_down_timer) &&
167 retry &&
168 wait_time &&
169 (test_bit(LOOP_RESYNC_NEEDED,
170 &ha->dpc_flags)));
171
172 if (wait_time == 0)
173 rval = QLA_FUNCTION_FAILED;
1da177e4
LT
174 } else if (ha->device_flags & DFLG_NO_CABLE)
175 /* If no cable, then all is good. */
176 rval = QLA_SUCCESS;
177 }
178 } while (restart_risc && retry--);
179
180 if (rval == QLA_SUCCESS) {
181 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
1da177e4
LT
182 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
183 ha->marker_needed = 0;
184
185 ha->flags.online = 1;
186 } else {
187 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
188 }
189
190 return (rval);
191}
192
193/**
abbd8870 194 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
1da177e4
LT
195 * @ha: HA context
196 *
197 * Returns 0 on success.
198 */
abbd8870
AV
199int
200qla2100_pci_config(scsi_qla_host_t *ha)
1da177e4 201{
abbd8870
AV
202 uint16_t w, mwi;
203 unsigned long flags;
3d71644c 204 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 205
1da177e4
LT
206 pci_set_master(ha->pdev);
207 mwi = 0;
208 if (pci_set_mwi(ha->pdev))
209 mwi = PCI_COMMAND_INVALIDATE;
210 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
211
1da177e4
LT
212 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
213 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
abbd8870
AV
214 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
215
216 /* Reset expansion ROM address decode enable */
217 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
218 w &= ~PCI_ROM_ADDRESS_ENABLE;
219 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
1da177e4
LT
220
221 /* Get PCI bus information. */
222 spin_lock_irqsave(&ha->hardware_lock, flags);
3d71644c 223 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
1da177e4
LT
224 spin_unlock_irqrestore(&ha->hardware_lock, flags);
225
abbd8870
AV
226 return QLA_SUCCESS;
227}
1da177e4 228
abbd8870
AV
229/**
230 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
231 * @ha: HA context
232 *
233 * Returns 0 on success.
234 */
235int
236qla2300_pci_config(scsi_qla_host_t *ha)
237{
238 uint16_t w, mwi;
239 unsigned long flags = 0;
240 uint32_t cnt;
3d71644c 241 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 242
abbd8870
AV
243 pci_set_master(ha->pdev);
244 mwi = 0;
245 if (pci_set_mwi(ha->pdev))
246 mwi = PCI_COMMAND_INVALIDATE;
247 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
1da177e4 248
abbd8870
AV
249 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
250 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1da177e4 251
abbd8870
AV
252 if (IS_QLA2322(ha) || IS_QLA6322(ha))
253 w &= ~PCI_COMMAND_INTX_DISABLE;
1da177e4 254
abbd8870
AV
255 /*
256 * If this is a 2300 card and not 2312, reset the
257 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
258 * the 2310 also reports itself as a 2300 so we need to get the
259 * fb revision level -- a 6 indicates it really is a 2300 and
260 * not a 2310.
261 */
262 if (IS_QLA2300(ha)) {
263 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4 264
abbd8870 265 /* Pause RISC. */
3d71644c 266 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
abbd8870 267 for (cnt = 0; cnt < 30000; cnt++) {
3d71644c 268 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
abbd8870 269 break;
1da177e4 270
abbd8870
AV
271 udelay(10);
272 }
1da177e4 273
abbd8870 274 /* Select FPM registers. */
3d71644c
AV
275 WRT_REG_WORD(&reg->ctrl_status, 0x20);
276 RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
277
278 /* Get the fb rev level */
3d71644c 279 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
abbd8870
AV
280
281 if (ha->fb_rev == FPM_2300)
282 w &= ~PCI_COMMAND_INVALIDATE;
283
284 /* Deselect FPM registers. */
3d71644c
AV
285 WRT_REG_WORD(&reg->ctrl_status, 0x0);
286 RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
287
288 /* Release RISC module. */
3d71644c 289 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
abbd8870 290 for (cnt = 0; cnt < 30000; cnt++) {
3d71644c 291 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
abbd8870
AV
292 break;
293
294 udelay(10);
1da177e4 295 }
1da177e4 296
abbd8870
AV
297 spin_unlock_irqrestore(&ha->hardware_lock, flags);
298 }
1da177e4
LT
299 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
300
abbd8870
AV
301 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
302
1da177e4
LT
303 /* Reset expansion ROM address decode enable */
304 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
305 w &= ~PCI_ROM_ADDRESS_ENABLE;
306 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
307
abbd8870
AV
308 /* Get PCI bus information. */
309 spin_lock_irqsave(&ha->hardware_lock, flags);
3d71644c 310 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
311 spin_unlock_irqrestore(&ha->hardware_lock, flags);
312
313 return QLA_SUCCESS;
1da177e4
LT
314}
315
0107109e
AV
316/**
317 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
318 * @ha: HA context
319 *
320 * Returns 0 on success.
321 */
322int
323qla24xx_pci_config(scsi_qla_host_t *ha)
324{
325 uint16_t w, mwi;
326 unsigned long flags = 0;
327 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
328 int pcix_cmd_reg, pcie_dctl_reg;
329
330 pci_set_master(ha->pdev);
331 mwi = 0;
332 if (pci_set_mwi(ha->pdev))
333 mwi = PCI_COMMAND_INVALIDATE;
334 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
335
336 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
337 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
338 w &= ~PCI_COMMAND_INTX_DISABLE;
339 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
340
341 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
342
343 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
344 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
345 if (pcix_cmd_reg) {
346 uint16_t pcix_cmd;
347
348 pcix_cmd_reg += PCI_X_CMD;
349 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
350 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
351 pcix_cmd |= 0x0008;
352 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
353 }
354
355 /* PCIe -- adjust Maximum Read Request Size (2048). */
356 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
357 if (pcie_dctl_reg) {
358 uint16_t pcie_dctl;
359
360 pcie_dctl_reg += PCI_EXP_DEVCTL;
361 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
362 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
363 pcie_dctl |= 0x4000;
364 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
365 }
366
367 /* Reset expansion ROM address decode enable */
368 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
369 w &= ~PCI_ROM_ADDRESS_ENABLE;
370 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
371
372 /* Get PCI bus information. */
373 spin_lock_irqsave(&ha->hardware_lock, flags);
374 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
375 spin_unlock_irqrestore(&ha->hardware_lock, flags);
376
377 return QLA_SUCCESS;
378}
379
1da177e4
LT
380/**
381 * qla2x00_isp_firmware() - Choose firmware image.
382 * @ha: HA context
383 *
384 * Returns 0 on success.
385 */
386static int
387qla2x00_isp_firmware(scsi_qla_host_t *ha)
388{
389 int rval;
390
391 /* Assume loading risc code */
fa2a1ce5 392 rval = QLA_FUNCTION_FAILED;
1da177e4
LT
393
394 if (ha->flags.disable_risc_code_load) {
395 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
396 ha->host_no));
397 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
398
399 /* Verify checksum of loaded RISC code. */
1c7c6357
AV
400 rval = qla2x00_verify_checksum(ha,
401 IS_QLA24XX(ha) || IS_QLA25XX(ha) ? RISC_SADDRESS :
402 *ha->brd_info->fw_info[0].fwstart);
1da177e4
LT
403 }
404
405 if (rval) {
406 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
407 ha->host_no));
408 }
409
410 return (rval);
411}
412
413/**
414 * qla2x00_reset_chip() - Reset ISP chip.
415 * @ha: HA context
416 *
417 * Returns 0 on success.
418 */
abbd8870 419void
fa2a1ce5 420qla2x00_reset_chip(scsi_qla_host_t *ha)
1da177e4
LT
421{
422 unsigned long flags = 0;
3d71644c 423 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
424 uint32_t cnt;
425 unsigned long mbx_flags = 0;
426 uint16_t cmd;
427
abbd8870 428 ha->isp_ops.disable_intrs(ha);
1da177e4
LT
429
430 spin_lock_irqsave(&ha->hardware_lock, flags);
431
432 /* Turn off master enable */
433 cmd = 0;
434 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
435 cmd &= ~PCI_COMMAND_MASTER;
436 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
437
438 if (!IS_QLA2100(ha)) {
439 /* Pause RISC. */
440 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
441 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
442 for (cnt = 0; cnt < 30000; cnt++) {
443 if ((RD_REG_WORD(&reg->hccr) &
444 HCCR_RISC_PAUSE) != 0)
445 break;
446 udelay(100);
447 }
448 } else {
449 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
450 udelay(10);
451 }
452
453 /* Select FPM registers. */
454 WRT_REG_WORD(&reg->ctrl_status, 0x20);
455 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
456
457 /* FPM Soft Reset. */
458 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
459 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
460
461 /* Toggle Fpm Reset. */
462 if (!IS_QLA2200(ha)) {
463 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
464 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
465 }
466
467 /* Select frame buffer registers. */
468 WRT_REG_WORD(&reg->ctrl_status, 0x10);
469 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
470
471 /* Reset frame buffer FIFOs. */
472 if (IS_QLA2200(ha)) {
473 WRT_FB_CMD_REG(ha, reg, 0xa000);
474 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
475 } else {
476 WRT_FB_CMD_REG(ha, reg, 0x00fc);
477
478 /* Read back fb_cmd until zero or 3 seconds max */
479 for (cnt = 0; cnt < 3000; cnt++) {
480 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
481 break;
482 udelay(100);
483 }
484 }
485
486 /* Select RISC module registers. */
487 WRT_REG_WORD(&reg->ctrl_status, 0);
488 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
489
490 /* Reset RISC processor. */
491 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
492 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
493
494 /* Release RISC processor. */
495 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
496 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
497 }
498
499 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
500 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
501
502 /* Reset ISP chip. */
503 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
504
505 /* Wait for RISC to recover from reset. */
506 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
507 /*
508 * It is necessary to for a delay here since the card doesn't
509 * respond to PCI reads during a reset. On some architectures
510 * this will result in an MCA.
511 */
512 udelay(20);
513 for (cnt = 30000; cnt; cnt--) {
514 if ((RD_REG_WORD(&reg->ctrl_status) &
515 CSR_ISP_SOFT_RESET) == 0)
516 break;
517 udelay(100);
518 }
519 } else
520 udelay(10);
521
522 /* Reset RISC processor. */
523 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
524
525 WRT_REG_WORD(&reg->semaphore, 0);
526
527 /* Release RISC processor. */
528 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
529 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
530
531 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
532 for (cnt = 0; cnt < 30000; cnt++) {
533 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
534 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
535
536 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY) {
537 if (!(test_bit(ABORT_ISP_ACTIVE,
538 &ha->dpc_flags)))
539 spin_unlock_irqrestore(
540 &ha->mbx_reg_lock, mbx_flags);
541 break;
542 }
543
544 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
545 spin_unlock_irqrestore(&ha->mbx_reg_lock,
546 mbx_flags);
547
548 udelay(100);
549 }
550 } else
551 udelay(100);
552
553 /* Turn on master enable */
554 cmd |= PCI_COMMAND_MASTER;
555 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
556
557 /* Disable RISC pause on FPM parity error. */
558 if (!IS_QLA2100(ha)) {
559 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
560 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
561 }
562
563 spin_unlock_irqrestore(&ha->hardware_lock, flags);
564}
565
0107109e
AV
566/**
567 * qla24xx_reset_chip() - Reset ISP24xx chip.
568 * @ha: HA context
569 *
570 * Returns 0 on success.
571 */
572void
573qla24xx_reset_chip(scsi_qla_host_t *ha)
574{
575 unsigned long flags = 0;
576 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
577 uint32_t cnt, d2;
578
579 ha->isp_ops.disable_intrs(ha);
580
581 spin_lock_irqsave(&ha->hardware_lock, flags);
582
583 /* Reset RISC. */
584 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
585 for (cnt = 0; cnt < 30000; cnt++) {
586 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
587 break;
588
589 udelay(10);
590 }
591
592 WRT_REG_DWORD(&reg->ctrl_status,
593 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
594 udelay(20);
595 d2 = RD_REG_DWORD(&reg->ctrl_status);
596 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
597 udelay(5);
598 d2 = RD_REG_DWORD(&reg->ctrl_status);
599 barrier();
600 }
601
602 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
603 RD_REG_DWORD(&reg->hccr);
604
605 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
606 RD_REG_DWORD(&reg->hccr);
607
608 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
609 RD_REG_DWORD(&reg->hccr);
610
611 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
612 for (cnt = 6000000 ; cnt && d2; cnt--) {
613 udelay(5);
614 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
615 barrier();
616 }
617
618 spin_unlock_irqrestore(&ha->hardware_lock, flags);
619}
620
1da177e4
LT
621/**
622 * qla2x00_chip_diag() - Test chip for proper operation.
623 * @ha: HA context
624 *
625 * Returns 0 on success.
626 */
abbd8870 627int
1da177e4
LT
628qla2x00_chip_diag(scsi_qla_host_t *ha)
629{
630 int rval;
3d71644c 631 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
632 unsigned long flags = 0;
633 uint16_t data;
634 uint32_t cnt;
635 uint16_t mb[5];
636
637 /* Assume a failed state */
638 rval = QLA_FUNCTION_FAILED;
639
640 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
641 ha->host_no, (u_long)&reg->flash_address));
642
643 spin_lock_irqsave(&ha->hardware_lock, flags);
644
645 /* Reset ISP chip. */
646 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
647
648 /*
649 * We need to have a delay here since the card will not respond while
650 * in reset causing an MCA on some architectures.
651 */
652 udelay(20);
653 data = qla2x00_debounce_register(&reg->ctrl_status);
654 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
655 udelay(5);
656 data = RD_REG_WORD(&reg->ctrl_status);
657 barrier();
658 }
659
660 if (!cnt)
661 goto chip_diag_failed;
662
663 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
664 ha->host_no));
665
666 /* Reset RISC processor. */
667 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
668 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
669
670 /* Workaround for QLA2312 PCI parity error */
671 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
672 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
673 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
674 udelay(5);
675 data = RD_MAILBOX_REG(ha, reg, 0);
fa2a1ce5 676 barrier();
1da177e4
LT
677 }
678 } else
679 udelay(10);
680
681 if (!cnt)
682 goto chip_diag_failed;
683
684 /* Check product ID of chip */
685 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
686
687 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
688 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
689 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
690 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
691 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
692 mb[3] != PROD_ID_3) {
693 qla_printk(KERN_WARNING, ha,
694 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
695
696 goto chip_diag_failed;
697 }
698 ha->product_id[0] = mb[1];
699 ha->product_id[1] = mb[2];
700 ha->product_id[2] = mb[3];
701 ha->product_id[3] = mb[4];
702
703 /* Adjust fw RISC transfer size */
704 if (ha->request_q_length > 1024)
705 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
706 else
707 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
708 ha->request_q_length;
709
710 if (IS_QLA2200(ha) &&
711 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
712 /* Limit firmware transfer size with a 2200A */
713 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
714 ha->host_no));
715
716 ha->fw_transfer_size = 128;
717 }
718
719 /* Wrap Incoming Mailboxes Test. */
720 spin_unlock_irqrestore(&ha->hardware_lock, flags);
721
722 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
723 rval = qla2x00_mbx_reg_test(ha);
724 if (rval) {
725 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
726 ha->host_no));
727 qla_printk(KERN_WARNING, ha,
728 "Failed mailbox send register test\n");
729 }
730 else {
731 /* Flag a successful rval */
732 rval = QLA_SUCCESS;
733 }
734 spin_lock_irqsave(&ha->hardware_lock, flags);
735
736chip_diag_failed:
737 if (rval)
738 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
739 "****\n", ha->host_no));
740
741 spin_unlock_irqrestore(&ha->hardware_lock, flags);
742
743 return (rval);
744}
745
0107109e
AV
746/**
747 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
748 * @ha: HA context
749 *
750 * Returns 0 on success.
751 */
752int
753qla24xx_chip_diag(scsi_qla_host_t *ha)
754{
755 int rval;
756 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
757 unsigned long flags = 0;
758 uint32_t cnt, d2;
759
760 spin_lock_irqsave(&ha->hardware_lock, flags);
761
762 /* Reset RISC. */
763 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
764 for (cnt = 0; cnt < 30000; cnt++) {
765 if ((RD_REG_DWORD(&reg->ctrl_status) &
766 CSRX_DMA_ACTIVE) == 0)
767 break;
768
769 udelay(10);
770 }
771
772 WRT_REG_DWORD(&reg->ctrl_status,
773 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
774 udelay(20);
775 d2 = RD_REG_DWORD(&reg->ctrl_status);
776 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
777 udelay(5);
778 d2 = RD_REG_DWORD(&reg->ctrl_status);
779 barrier();
780 }
781
782 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
783 RD_REG_DWORD(&reg->hccr);
784
785 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
786 RD_REG_DWORD(&reg->hccr);
787
788 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
789 RD_REG_DWORD(&reg->hccr);
790
791 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
792 for (cnt = 6000000 ; cnt && d2; cnt--) {
793 udelay(5);
794 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
795 barrier();
796 }
797
798 spin_unlock_irqrestore(&ha->hardware_lock, flags);
799
800 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
801
802 rval = qla2x00_mbx_reg_test(ha);
803 if (rval) {
804 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
805 ha->host_no));
806 qla_printk(KERN_WARNING, ha,
807 "Failed mailbox send register test\n");
808 } else {
809 /* Flag a successful rval */
810 rval = QLA_SUCCESS;
811 }
812
813 return rval;
814}
815
816static void
817qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
818{
819 ha->fw_dumped = 0;
820 ha->fw_dump24_len = sizeof(struct qla24xx_fw_dump);
821 ha->fw_dump24_len += (ha->fw_memory_size - 0x100000) * sizeof(uint32_t);
822 ha->fw_dump24 = vmalloc(ha->fw_dump24_len);
823 if (ha->fw_dump24)
824 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware "
825 "dump...\n", ha->fw_dump24_len / 1024);
826 else
827 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
828 "firmware dump!!!\n", ha->fw_dump24_len / 1024);
829}
830
1da177e4
LT
831/**
832 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
833 * @ha: HA context
834 *
835 * Returns 0 on success.
836 */
837static void
838qla2x00_resize_request_q(scsi_qla_host_t *ha)
839{
840 int rval;
841 uint16_t fw_iocb_cnt = 0;
842 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
843 dma_addr_t request_dma;
844 request_t *request_ring;
845
846 /* Valid only on recent ISPs. */
847 if (IS_QLA2100(ha) || IS_QLA2200(ha))
848 return;
849
0107109e
AV
850 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
851 qla2x00_alloc_fw_dump(ha);
852
1da177e4
LT
853 /* Retrieve IOCB counts available to the firmware. */
854 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
855 if (rval)
856 return;
857 /* No point in continuing if current settings are sufficient. */
858 if (fw_iocb_cnt < 1024)
859 return;
860 if (ha->request_q_length >= request_q_length)
861 return;
862
863 /* Attempt to claim larger area for request queue. */
864 request_ring = dma_alloc_coherent(&ha->pdev->dev,
865 (request_q_length + 1) * sizeof(request_t), &request_dma,
866 GFP_KERNEL);
867 if (request_ring == NULL)
868 return;
869
870 /* Resize successful, report extensions. */
871 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
872 (ha->fw_memory_size + 1) / 1024);
873 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
874 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
875
876 /* Clear old allocations. */
877 dma_free_coherent(&ha->pdev->dev,
878 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
879 ha->request_dma);
880
881 /* Begin using larger queue. */
882 ha->request_q_length = request_q_length;
883 ha->request_ring = request_ring;
884 ha->request_dma = request_dma;
885}
886
887/**
888 * qla2x00_setup_chip() - Load and start RISC firmware.
889 * @ha: HA context
890 *
891 * Returns 0 on success.
892 */
893static int
894qla2x00_setup_chip(scsi_qla_host_t *ha)
895{
0107109e
AV
896 int rval;
897 uint32_t srisc_address = 0;
1da177e4
LT
898
899 /* Load firmware sequences */
0107109e
AV
900 rval = ha->isp_ops.load_risc(ha, &srisc_address);
901 if (rval == QLA_SUCCESS) {
1da177e4
LT
902 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
903 "code.\n", ha->host_no));
904
0107109e 905 rval = qla2x00_verify_checksum(ha, srisc_address);
1da177e4
LT
906 if (rval == QLA_SUCCESS) {
907 /* Start firmware execution. */
908 DEBUG(printk("scsi(%ld): Checksum OK, start "
909 "firmware.\n", ha->host_no));
910
0107109e 911 rval = qla2x00_execute_fw(ha, srisc_address);
1da177e4
LT
912 /* Retrieve firmware information. */
913 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
914 qla2x00_get_fw_version(ha,
915 &ha->fw_major_version,
916 &ha->fw_minor_version,
917 &ha->fw_subminor_version,
918 &ha->fw_attributes, &ha->fw_memory_size);
919 qla2x00_resize_request_q(ha);
920 }
921 } else {
922 DEBUG2(printk(KERN_INFO
923 "scsi(%ld): ISP Firmware failed checksum.\n",
924 ha->host_no));
925 }
926 }
927
928 if (rval) {
929 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
930 ha->host_no));
931 }
932
933 return (rval);
934}
935
936/**
937 * qla2x00_init_response_q_entries() - Initializes response queue entries.
938 * @ha: HA context
939 *
940 * Beginning of request ring has initialization control block already built
941 * by nvram config routine.
942 *
943 * Returns 0 on success.
944 */
945static void
946qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
947{
948 uint16_t cnt;
949 response_t *pkt;
950
951 pkt = ha->response_ring_ptr;
952 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
953 pkt->signature = RESPONSE_PROCESSED;
954 pkt++;
955 }
956
957}
958
959/**
960 * qla2x00_update_fw_options() - Read and process firmware options.
961 * @ha: HA context
962 *
963 * Returns 0 on success.
964 */
abbd8870 965void
1da177e4
LT
966qla2x00_update_fw_options(scsi_qla_host_t *ha)
967{
968 uint16_t swing, emphasis, tx_sens, rx_sens;
969
970 memset(ha->fw_options, 0, sizeof(ha->fw_options));
971 qla2x00_get_fw_options(ha, ha->fw_options);
972
973 if (IS_QLA2100(ha) || IS_QLA2200(ha))
974 return;
975
976 /* Serial Link options. */
977 DEBUG3(printk("scsi(%ld): Serial link options:\n",
978 ha->host_no));
979 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
980 sizeof(ha->fw_seriallink_options)));
981
982 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
983 if (ha->fw_seriallink_options[3] & BIT_2) {
984 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
985
986 /* 1G settings */
987 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
988 emphasis = (ha->fw_seriallink_options[2] &
989 (BIT_4 | BIT_3)) >> 3;
990 tx_sens = ha->fw_seriallink_options[0] &
fa2a1ce5 991 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1da177e4
LT
992 rx_sens = (ha->fw_seriallink_options[0] &
993 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
994 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
995 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
996 if (rx_sens == 0x0)
997 rx_sens = 0x3;
998 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
999 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1000 ha->fw_options[10] |= BIT_5 |
1001 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1002 (tx_sens & (BIT_1 | BIT_0));
1003
1004 /* 2G settings */
1005 swing = (ha->fw_seriallink_options[2] &
1006 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1007 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1008 tx_sens = ha->fw_seriallink_options[1] &
fa2a1ce5 1009 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1da177e4
LT
1010 rx_sens = (ha->fw_seriallink_options[1] &
1011 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1012 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1013 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1014 if (rx_sens == 0x0)
1015 rx_sens = 0x3;
1016 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1017 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1018 ha->fw_options[11] |= BIT_5 |
1019 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1020 (tx_sens & (BIT_1 | BIT_0));
1021 }
1022
1023 /* FCP2 options. */
1024 /* Return command IOCBs without waiting for an ABTS to complete. */
1025 ha->fw_options[3] |= BIT_13;
1026
1027 /* LED scheme. */
1028 if (ha->flags.enable_led_scheme)
1029 ha->fw_options[2] |= BIT_12;
1030
1031 /* Update firmware options. */
1032 qla2x00_set_fw_options(ha, ha->fw_options);
1033}
1034
0107109e
AV
1035void
1036qla24xx_update_fw_options(scsi_qla_host_t *ha)
1037{
1038 int rval;
1039
1040 /* Update Serial Link options. */
1041 if ((ha->fw_seriallink_options24[0] & BIT_0) == 0)
1042 return;
1043
1044 rval = qla2x00_set_serdes_params(ha, ha->fw_seriallink_options24[1],
1045 ha->fw_seriallink_options24[2], ha->fw_seriallink_options24[3]);
1046 if (rval != QLA_SUCCESS) {
1047 qla_printk(KERN_WARNING, ha,
1048 "Unable to update Serial Link options (%x).\n", rval);
1049 }
1050}
1051
abbd8870
AV
1052void
1053qla2x00_config_rings(struct scsi_qla_host *ha)
1054{
3d71644c 1055 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
abbd8870
AV
1056
1057 /* Setup ring parameters in initialization control block. */
1058 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1059 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1060 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1061 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1062 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1063 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1064 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1065 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1066
1067 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1068 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1069 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1070 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1071 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1072}
1073
0107109e
AV
1074void
1075qla24xx_config_rings(struct scsi_qla_host *ha)
1076{
1077 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1078 struct init_cb_24xx *icb;
1079
1080 /* Setup ring parameters in initialization control block. */
1081 icb = (struct init_cb_24xx *)ha->init_cb;
1082 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1083 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1084 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1085 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1086 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1087 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1088 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1089 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1090
1091 WRT_REG_DWORD(&reg->req_q_in, 0);
1092 WRT_REG_DWORD(&reg->req_q_out, 0);
1093 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1094 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1095 RD_REG_DWORD(&reg->rsp_q_out);
1096}
1097
1da177e4
LT
1098/**
1099 * qla2x00_init_rings() - Initializes firmware.
1100 * @ha: HA context
1101 *
1102 * Beginning of request ring has initialization control block already built
1103 * by nvram config routine.
1104 *
1105 * Returns 0 on success.
1106 */
1107static int
1108qla2x00_init_rings(scsi_qla_host_t *ha)
1109{
1110 int rval;
1111 unsigned long flags = 0;
1112 int cnt;
1da177e4
LT
1113
1114 spin_lock_irqsave(&ha->hardware_lock, flags);
1115
1116 /* Clear outstanding commands array. */
1117 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1118 ha->outstanding_cmds[cnt] = NULL;
1119
1120 ha->current_outstanding_cmd = 0;
1121
1122 /* Clear RSCN queue. */
1123 ha->rscn_in_ptr = 0;
1124 ha->rscn_out_ptr = 0;
1125
1126 /* Initialize firmware. */
1127 ha->request_ring_ptr = ha->request_ring;
1128 ha->req_ring_index = 0;
1129 ha->req_q_cnt = ha->request_q_length;
1130 ha->response_ring_ptr = ha->response_ring;
1131 ha->rsp_ring_index = 0;
1132
1da177e4
LT
1133 /* Initialize response queue entries */
1134 qla2x00_init_response_q_entries(ha);
1135
abbd8870 1136 ha->isp_ops.config_rings(ha);
1da177e4
LT
1137
1138 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1139
1140 /* Update any ISP specific firmware options before initialization. */
abbd8870 1141 ha->isp_ops.update_fw_options(ha);
1da177e4
LT
1142
1143 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
0107109e 1144 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1da177e4
LT
1145 if (rval) {
1146 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1147 ha->host_no));
1148 } else {
1149 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1150 ha->host_no));
1151 }
1152
1153 return (rval);
1154}
1155
1156/**
1157 * qla2x00_fw_ready() - Waits for firmware ready.
1158 * @ha: HA context
1159 *
1160 * Returns 0 on success.
1161 */
1162static int
1163qla2x00_fw_ready(scsi_qla_host_t *ha)
1164{
1165 int rval;
1166 unsigned long wtime, mtime;
1167 uint16_t min_wait; /* Minimum wait time if loop is down */
1168 uint16_t wait_time; /* Wait time if loop is coming ready */
1169 uint16_t fw_state;
1170
1171 rval = QLA_SUCCESS;
1172
1173 /* 20 seconds for loop down. */
fa2a1ce5 1174 min_wait = 20;
1da177e4
LT
1175
1176 /*
1177 * Firmware should take at most one RATOV to login, plus 5 seconds for
1178 * our own processing.
1179 */
1180 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1181 wait_time = min_wait;
1182 }
1183
1184 /* Min wait time if loop down */
1185 mtime = jiffies + (min_wait * HZ);
1186
1187 /* wait time before firmware ready */
1188 wtime = jiffies + (wait_time * HZ);
1189
1190 /* Wait for ISP to finish LIP */
1191 if (!ha->flags.init_done)
1192 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1193
1194 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1195 ha->host_no));
1196
1197 do {
1198 rval = qla2x00_get_firmware_state(ha, &fw_state);
1199 if (rval == QLA_SUCCESS) {
1200 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1201 ha->device_flags &= ~DFLG_NO_CABLE;
1202 }
1203 if (fw_state == FSTATE_READY) {
1204 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1205 ha->host_no));
1206
1207 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1208 &ha->login_timeout, &ha->r_a_tov);
1209
1210 rval = QLA_SUCCESS;
1211 break;
1212 }
1213
1214 rval = QLA_FUNCTION_FAILED;
1215
1216 if (atomic_read(&ha->loop_down_timer) &&
1217 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1218 fw_state == FSTATE_WAIT_AL_PA)) {
1219 /* Loop down. Timeout on min_wait for states
fa2a1ce5
AV
1220 * other than Wait for Login.
1221 */
1da177e4
LT
1222 if (time_after_eq(jiffies, mtime)) {
1223 qla_printk(KERN_INFO, ha,
1224 "Cable is unplugged...\n");
1225
1226 ha->device_flags |= DFLG_NO_CABLE;
1227 break;
1228 }
1229 }
1230 } else {
1231 /* Mailbox cmd failed. Timeout on min_wait. */
1232 if (time_after_eq(jiffies, mtime))
1233 break;
1234 }
1235
1236 if (time_after_eq(jiffies, wtime))
1237 break;
1238
1239 /* Delay for a while */
1240 msleep(500);
1241
1242 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1243 ha->host_no, fw_state, jiffies));
1244 } while (1);
1245
1246 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1247 ha->host_no, fw_state, jiffies));
1248
1249 if (rval) {
1250 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1251 ha->host_no));
1252 }
1253
1254 return (rval);
1255}
1256
1257/*
1258* qla2x00_configure_hba
1259* Setup adapter context.
1260*
1261* Input:
1262* ha = adapter state pointer.
1263*
1264* Returns:
1265* 0 = success
1266*
1267* Context:
1268* Kernel context.
1269*/
1270static int
1271qla2x00_configure_hba(scsi_qla_host_t *ha)
1272{
1273 int rval;
1274 uint16_t loop_id;
1275 uint16_t topo;
1276 uint8_t al_pa;
1277 uint8_t area;
1278 uint8_t domain;
1279 char connect_type[22];
1280
1281 /* Get host addresses. */
1282 rval = qla2x00_get_adapter_id(ha,
1283 &loop_id, &al_pa, &area, &domain, &topo);
1284 if (rval != QLA_SUCCESS) {
1285 qla_printk(KERN_WARNING, ha,
1286 "ERROR -- Unable to get host loop ID.\n");
1287 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1288 return (rval);
1289 }
1290
1291 if (topo == 4) {
1292 qla_printk(KERN_INFO, ha,
1293 "Cannot get topology - retrying.\n");
1294 return (QLA_FUNCTION_FAILED);
1295 }
1296
1297 ha->loop_id = loop_id;
1298
1299 /* initialize */
1300 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1301 ha->operating_mode = LOOP;
1302
1303 switch (topo) {
1304 case 0:
1305 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1306 ha->host_no));
1307 ha->current_topology = ISP_CFG_NL;
1308 strcpy(connect_type, "(Loop)");
1309 break;
1310
1311 case 1:
1312 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1313 ha->host_no));
1314 ha->current_topology = ISP_CFG_FL;
1315 strcpy(connect_type, "(FL_Port)");
1316 break;
1317
1318 case 2:
1319 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1320 ha->host_no));
1321 ha->operating_mode = P2P;
1322 ha->current_topology = ISP_CFG_N;
1323 strcpy(connect_type, "(N_Port-to-N_Port)");
1324 break;
1325
1326 case 3:
1327 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1328 ha->host_no));
1329 ha->operating_mode = P2P;
1330 ha->current_topology = ISP_CFG_F;
1331 strcpy(connect_type, "(F_Port)");
1332 break;
1333
1334 default:
1335 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1336 "Using NL.\n",
1337 ha->host_no, topo));
1338 ha->current_topology = ISP_CFG_NL;
1339 strcpy(connect_type, "(Loop)");
1340 break;
1341 }
1342
1343 /* Save Host port and loop ID. */
1344 /* byte order - Big Endian */
1345 ha->d_id.b.domain = domain;
1346 ha->d_id.b.area = area;
1347 ha->d_id.b.al_pa = al_pa;
1348
1349 if (!ha->flags.init_done)
1350 qla_printk(KERN_INFO, ha,
1351 "Topology - %s, Host Loop address 0x%x\n",
1352 connect_type, ha->loop_id);
1353
1354 if (rval) {
1355 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1356 } else {
1357 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1358 }
1359
1360 return(rval);
1361}
1362
1363/*
1364* NVRAM configuration for ISP 2xxx
1365*
1366* Input:
1367* ha = adapter block pointer.
1368*
1369* Output:
1370* initialization control block in response_ring
1371* host adapters parameters in host adapter block
1372*
1373* Returns:
1374* 0 = success.
1375*/
abbd8870 1376int
1da177e4
LT
1377qla2x00_nvram_config(scsi_qla_host_t *ha)
1378{
0107109e
AV
1379 int rval;
1380 uint8_t chksum = 0;
1381 uint16_t cnt;
1382 uint8_t *dptr1, *dptr2;
1383 init_cb_t *icb = ha->init_cb;
1384 nvram_t *nv = (nvram_t *)ha->request_ring;
1385 uint8_t *ptr = (uint8_t *)ha->request_ring;
3d71644c 1386 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
0107109e 1387 uint8_t timer_mode;
1da177e4
LT
1388
1389 rval = QLA_SUCCESS;
1390
1391 /* Determine NVRAM starting address. */
0107109e 1392 ha->nvram_size = sizeof(nvram_t);
1da177e4
LT
1393 ha->nvram_base = 0;
1394 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1395 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1396 ha->nvram_base = 0x80;
1397
1398 /* Get NVRAM data and calculate checksum. */
0107109e
AV
1399 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1400 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1401 chksum += *ptr++;
1da177e4
LT
1402
1403 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1404 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
0107109e 1405 ha->nvram_size));
1da177e4
LT
1406
1407 /* Bad NVRAM data, set defaults parameters. */
1408 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1409 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1410 /* Reset NVRAM data. */
1411 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1412 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1413 nv->nvram_version);
1414 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1415 "invalid -- WWPN) defaults.\n");
1416
1417 /*
1418 * Set default initialization control block.
1419 */
0107109e 1420 memset(nv, 0, ha->nvram_size);
1da177e4
LT
1421 nv->parameter_block_version = ICB_VERSION;
1422
1423 if (IS_QLA23XX(ha)) {
1424 nv->firmware_options[0] = BIT_2 | BIT_1;
1425 nv->firmware_options[1] = BIT_7 | BIT_5;
1426 nv->add_firmware_options[0] = BIT_5;
1427 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1428 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1429 nv->special_options[1] = BIT_7;
1430 } else if (IS_QLA2200(ha)) {
1431 nv->firmware_options[0] = BIT_2 | BIT_1;
1432 nv->firmware_options[1] = BIT_7 | BIT_5;
1433 nv->add_firmware_options[0] = BIT_5;
1434 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1435 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1436 } else if (IS_QLA2100(ha)) {
1437 nv->firmware_options[0] = BIT_3 | BIT_1;
1438 nv->firmware_options[1] = BIT_5;
1439 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1440 }
1441
1442 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1443 nv->execution_throttle = __constant_cpu_to_le16(16);
1444 nv->retry_count = 8;
1445 nv->retry_delay = 1;
1446
1447 nv->port_name[0] = 33;
1448 nv->port_name[3] = 224;
1449 nv->port_name[4] = 139;
1450
1451 nv->login_timeout = 4;
1452
1453 /*
1454 * Set default host adapter parameters
1455 */
1456 nv->host_p[1] = BIT_2;
1457 nv->reset_delay = 5;
1458 nv->port_down_retry_count = 8;
1459 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1460 nv->link_down_timeout = 60;
1461
1462 rval = 1;
1463 }
1464
1465#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1466 /*
1467 * The SN2 does not provide BIOS emulation which means you can't change
1468 * potentially bogus BIOS settings. Force the use of default settings
1469 * for link rate and frame size. Hope that the rest of the settings
1470 * are valid.
1471 */
1472 if (ia64_platform_is("sn2")) {
1473 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1474 if (IS_QLA23XX(ha))
1475 nv->special_options[1] = BIT_7;
1476 }
1477#endif
1478
1479 /* Reset Initialization control block */
0107109e 1480 memset(icb, 0, ha->init_cb_size);
1da177e4
LT
1481
1482 /*
1483 * Setup driver NVRAM options.
1484 */
1485 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1486 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1487 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1488 nv->firmware_options[1] &= ~BIT_4;
1489
1490 if (IS_QLA23XX(ha)) {
1491 nv->firmware_options[0] |= BIT_2;
1492 nv->firmware_options[0] &= ~BIT_3;
0107109e 1493 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1da177e4
LT
1494
1495 if (IS_QLA2300(ha)) {
1496 if (ha->fb_rev == FPM_2310) {
1497 strcpy(ha->model_number, "QLA2310");
1498 } else {
1499 strcpy(ha->model_number, "QLA2300");
1500 }
1501 } else {
1502 if (rval == 0 &&
1503 memcmp(nv->model_number, BINZERO,
1504 sizeof(nv->model_number)) != 0) {
1505 char *st, *en;
1506
1507 strncpy(ha->model_number, nv->model_number,
1508 sizeof(nv->model_number));
1509 st = en = ha->model_number;
1510 en += sizeof(nv->model_number) - 1;
1511 while (en > st) {
1512 if (*en != 0x20 && *en != 0x00)
1513 break;
1514 *en-- = '\0';
1515 }
1516 } else {
1517 uint16_t index;
1518
1519 index = (ha->pdev->subsystem_device & 0xff);
1520 if (index < QLA_MODEL_NAMES) {
1521 strcpy(ha->model_number,
1522 qla2x00_model_name[index]);
1523 ha->model_desc =
1524 qla2x00_model_desc[index];
1525 } else {
1526 strcpy(ha->model_number, "QLA23xx");
1527 }
1528 }
1529 }
1530 } else if (IS_QLA2200(ha)) {
1531 nv->firmware_options[0] |= BIT_2;
1532 /*
1533 * 'Point-to-point preferred, else loop' is not a safe
1534 * connection mode setting.
1535 */
1536 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1537 (BIT_5 | BIT_4)) {
1538 /* Force 'loop preferred, else point-to-point'. */
1539 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1540 nv->add_firmware_options[0] |= BIT_5;
1541 }
1542 strcpy(ha->model_number, "QLA22xx");
1543 } else /*if (IS_QLA2100(ha))*/ {
1544 strcpy(ha->model_number, "QLA2100");
1545 }
1546
1547 /*
1548 * Copy over NVRAM RISC parameter block to initialization control block.
1549 */
1550 dptr1 = (uint8_t *)icb;
1551 dptr2 = (uint8_t *)&nv->parameter_block_version;
1552 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1553 while (cnt--)
1554 *dptr1++ = *dptr2++;
1555
1556 /* Copy 2nd half. */
1557 dptr1 = (uint8_t *)icb->add_firmware_options;
1558 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1559 while (cnt--)
1560 *dptr1++ = *dptr2++;
1561
1562 /* Prepare nodename */
1563 if ((icb->firmware_options[1] & BIT_6) == 0) {
1564 /*
1565 * Firmware will apply the following mask if the nodename was
1566 * not provided.
1567 */
1568 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1569 icb->node_name[0] &= 0xF0;
1570 }
1571
1572 /*
1573 * Set host adapter parameters.
1574 */
1da177e4
LT
1575 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1576 /* Always load RISC code on non ISP2[12]00 chips. */
1577 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1578 ha->flags.disable_risc_code_load = 0;
1579 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1580 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1581 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1582 ha->flags.enable_led_scheme = ((nv->efi_parameters & BIT_3) ? 1 : 0);
1583
1584 ha->operating_mode =
1585 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1586
1587 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1588 sizeof(ha->fw_seriallink_options));
1589
1590 /* save HBA serial number */
1591 ha->serial0 = icb->port_name[5];
1592 ha->serial1 = icb->port_name[6];
1593 ha->serial2 = icb->port_name[7];
3d71644c
AV
1594 ha->node_name = icb->node_name;
1595 ha->port_name = icb->port_name;
1da177e4
LT
1596
1597 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1598
1599 ha->retry_count = nv->retry_count;
1600
1601 /* Set minimum login_timeout to 4 seconds. */
1602 if (nv->login_timeout < ql2xlogintimeout)
1603 nv->login_timeout = ql2xlogintimeout;
1604 if (nv->login_timeout < 4)
1605 nv->login_timeout = 4;
1606 ha->login_timeout = nv->login_timeout;
1607 icb->login_timeout = nv->login_timeout;
1608
1609 /* Set minimum RATOV to 200 tenths of a second. */
1610 ha->r_a_tov = 200;
1611
1da177e4
LT
1612 ha->loop_reset_delay = nv->reset_delay;
1613
1da177e4
LT
1614 /* Link Down Timeout = 0:
1615 *
1616 * When Port Down timer expires we will start returning
1617 * I/O's to OS with "DID_NO_CONNECT".
1618 *
1619 * Link Down Timeout != 0:
1620 *
1621 * The driver waits for the link to come up after link down
1622 * before returning I/Os to OS with "DID_NO_CONNECT".
fa2a1ce5 1623 */
1da177e4
LT
1624 if (nv->link_down_timeout == 0) {
1625 ha->loop_down_abort_time =
354d6b21 1626 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1da177e4
LT
1627 } else {
1628 ha->link_down_timeout = nv->link_down_timeout;
1629 ha->loop_down_abort_time =
1630 (LOOP_DOWN_TIME - ha->link_down_timeout);
fa2a1ce5 1631 }
1da177e4 1632
1da177e4
LT
1633 /*
1634 * Need enough time to try and get the port back.
1635 */
1636 ha->port_down_retry_count = nv->port_down_retry_count;
1637 if (qlport_down_retry)
1638 ha->port_down_retry_count = qlport_down_retry;
1639 /* Set login_retry_count */
1640 ha->login_retry_count = nv->retry_count;
1641 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1642 ha->port_down_retry_count > 3)
1643 ha->login_retry_count = ha->port_down_retry_count;
1644 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1645 ha->login_retry_count = ha->port_down_retry_count;
1646 if (ql2xloginretrycount)
1647 ha->login_retry_count = ql2xloginretrycount;
1648
1da177e4
LT
1649 icb->lun_enables = __constant_cpu_to_le16(0);
1650 icb->command_resource_count = 0;
1651 icb->immediate_notify_resource_count = 0;
1652 icb->timeout = __constant_cpu_to_le16(0);
1653
1654 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1655 /* Enable RIO */
1656 icb->firmware_options[0] &= ~BIT_3;
1657 icb->add_firmware_options[0] &=
1658 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1659 icb->add_firmware_options[0] |= BIT_2;
1660 icb->response_accumulation_timer = 3;
1661 icb->interrupt_delay_timer = 5;
1662
1663 ha->flags.process_response_queue = 1;
1664 } else {
1665 /* Enable ZIO -- Support mode 5 only. */
1666 timer_mode = icb->add_firmware_options[0] &
1667 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1668 icb->add_firmware_options[0] &=
1669 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1670 if (ql2xenablezio)
1671 timer_mode = BIT_2 | BIT_0;
1672 if (timer_mode == (BIT_2 | BIT_0)) {
1673 DEBUG2(printk("scsi(%ld): ZIO enabled; timer delay "
1674 "(%d).\n", ha->host_no, ql2xintrdelaytimer));
1675 qla_printk(KERN_INFO, ha,
1676 "ZIO enabled; timer delay (%d).\n",
1677 ql2xintrdelaytimer);
1678
1679 icb->add_firmware_options[0] |= timer_mode;
1680 icb->interrupt_delay_timer = ql2xintrdelaytimer;
1681 ha->flags.process_response_queue = 1;
1682 }
1683 }
1684
1685 if (rval) {
1686 DEBUG2_3(printk(KERN_WARNING
1687 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1688 }
1689 return (rval);
1690}
1691
1da177e4
LT
1692/**
1693 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1694 * @ha: HA context
1695 * @flags: allocation flags
1696 *
1697 * Returns a pointer to the allocated fcport, or NULL, if none available.
1698 */
1699fc_port_t *
1700qla2x00_alloc_fcport(scsi_qla_host_t *ha, int flags)
1701{
1702 fc_port_t *fcport;
1703
1704 fcport = kmalloc(sizeof(fc_port_t), flags);
1705 if (fcport == NULL)
1706 return (fcport);
1707
1708 /* Setup fcport template structure. */
1709 memset(fcport, 0, sizeof (fc_port_t));
1710 fcport->ha = ha;
1711 fcport->port_type = FCT_UNKNOWN;
1712 fcport->loop_id = FC_NO_LOOP_ID;
1713 fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1714 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1715 fcport->flags = FCF_RLC_SUPPORT;
1da177e4
LT
1716
1717 return (fcport);
1718}
1719
1720/*
1721 * qla2x00_configure_loop
1722 * Updates Fibre Channel Device Database with what is actually on loop.
1723 *
1724 * Input:
1725 * ha = adapter block pointer.
1726 *
1727 * Returns:
1728 * 0 = success.
1729 * 1 = error.
1730 * 2 = database was full and device was not configured.
1731 */
1732static int
fa2a1ce5 1733qla2x00_configure_loop(scsi_qla_host_t *ha)
1da177e4
LT
1734{
1735 int rval;
1736 unsigned long flags, save_flags;
1737
1738 rval = QLA_SUCCESS;
1739
1740 /* Get Initiator ID */
1741 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1742 rval = qla2x00_configure_hba(ha);
1743 if (rval != QLA_SUCCESS) {
1744 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1745 ha->host_no));
1746 return (rval);
1747 }
1748 }
1749
1750 save_flags = flags = ha->dpc_flags;
1751 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1752 ha->host_no, flags));
1753
1754 /*
1755 * If we have both an RSCN and PORT UPDATE pending then handle them
1756 * both at the same time.
1757 */
1758 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1759 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1da177e4
LT
1760
1761 /* Determine what we need to do */
1762 if (ha->current_topology == ISP_CFG_FL &&
1763 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1764
1765 ha->flags.rscn_queue_overflow = 1;
1766 set_bit(RSCN_UPDATE, &flags);
1767
1768 } else if (ha->current_topology == ISP_CFG_F &&
1769 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1770
1771 ha->flags.rscn_queue_overflow = 1;
1772 set_bit(RSCN_UPDATE, &flags);
1773 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1774
1775 } else if (!ha->flags.online ||
1776 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1777
1778 ha->flags.rscn_queue_overflow = 1;
1779 set_bit(RSCN_UPDATE, &flags);
1780 set_bit(LOCAL_LOOP_UPDATE, &flags);
1781 }
1782
1783 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1784 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1785 rval = QLA_FUNCTION_FAILED;
1786 } else {
1787 rval = qla2x00_configure_local_loop(ha);
1788 }
1789 }
1790
1791 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1792 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1793 rval = QLA_FUNCTION_FAILED;
1794 } else {
1795 rval = qla2x00_configure_fabric(ha);
1796 }
1797 }
1798
1799 if (rval == QLA_SUCCESS) {
1800 if (atomic_read(&ha->loop_down_timer) ||
1801 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1802 rval = QLA_FUNCTION_FAILED;
1803 } else {
1da177e4
LT
1804 atomic_set(&ha->loop_state, LOOP_READY);
1805
1806 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1807 }
1808 }
1809
1810 if (rval) {
1811 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1812 __func__, ha->host_no));
1813 } else {
1814 DEBUG3(printk("%s: exiting normally\n", __func__));
1815 }
1816
1817 /* Restore state if a resync event occured during processing */
1818 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1819 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1820 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1821 if (test_bit(RSCN_UPDATE, &save_flags))
1822 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1823 }
1824
1825 return (rval);
1826}
1827
1828
1829
1830/*
1831 * qla2x00_configure_local_loop
1832 * Updates Fibre Channel Device Database with local loop devices.
1833 *
1834 * Input:
1835 * ha = adapter block pointer.
1836 *
1837 * Returns:
1838 * 0 = success.
1839 */
1840static int
fa2a1ce5 1841qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1da177e4
LT
1842{
1843 int rval, rval2;
1844 int found_devs;
1845 int found;
1846 fc_port_t *fcport, *new_fcport;
1847
1848 uint16_t index;
1849 uint16_t entries;
1850 char *id_iter;
1851 uint16_t loop_id;
1852 uint8_t domain, area, al_pa;
1853
1854 found_devs = 0;
1855 new_fcport = NULL;
1856 entries = MAX_FIBRE_DEVICES;
1857
1858 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1859 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1860
1861 /* Get list of logged in devices. */
1862 memset(ha->gid_list, 0, GID_LIST_SIZE);
1863 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1864 &entries);
1865 if (rval != QLA_SUCCESS)
1866 goto cleanup_allocation;
1867
1868 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1869 ha->host_no, entries));
1870 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1871 entries * sizeof(struct gid_list_info)));
1872
1873 /* Allocate temporary fcport for any new fcports discovered. */
1874 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1875 if (new_fcport == NULL) {
1876 rval = QLA_MEMORY_ALLOC_FAILED;
1877 goto cleanup_allocation;
1878 }
1879 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1880
1881 /*
1882 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1883 */
1884 list_for_each_entry(fcport, &ha->fcports, list) {
1885 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1886 fcport->port_type != FCT_BROADCAST &&
1887 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1888
1889 DEBUG(printk("scsi(%ld): Marking port lost, "
1890 "loop_id=0x%04x\n",
1891 ha->host_no, fcport->loop_id));
1892
1893 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1894 fcport->flags &= ~FCF_FARP_DONE;
1895 }
1896 }
1897
1898 /* Add devices to port list. */
1899 id_iter = (char *)ha->gid_list;
1900 for (index = 0; index < entries; index++) {
1901 domain = ((struct gid_list_info *)id_iter)->domain;
1902 area = ((struct gid_list_info *)id_iter)->area;
1903 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
abbd8870 1904 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1da177e4
LT
1905 loop_id = (uint16_t)
1906 ((struct gid_list_info *)id_iter)->loop_id_2100;
abbd8870 1907 else
1da177e4
LT
1908 loop_id = le16_to_cpu(
1909 ((struct gid_list_info *)id_iter)->loop_id);
abbd8870 1910 id_iter += ha->gid_list_info_size;
1da177e4
LT
1911
1912 /* Bypass reserved domain fields. */
1913 if ((domain & 0xf0) == 0xf0)
1914 continue;
1915
1916 /* Bypass if not same domain and area of adapter. */
1917 if (area != ha->d_id.b.area || domain != ha->d_id.b.domain)
1918 continue;
1919
1920 /* Bypass invalid local loop ID. */
1921 if (loop_id > LAST_LOCAL_LOOP_ID)
1922 continue;
1923
1924 /* Fill in member data. */
1925 new_fcport->d_id.b.domain = domain;
1926 new_fcport->d_id.b.area = area;
1927 new_fcport->d_id.b.al_pa = al_pa;
1928 new_fcport->loop_id = loop_id;
1929 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1930 if (rval2 != QLA_SUCCESS) {
1931 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1932 "information -- get_port_database=%x, "
1933 "loop_id=0x%04x\n",
1934 ha->host_no, rval2, new_fcport->loop_id));
1935 continue;
1936 }
1937
1938 /* Check for matching device in port list. */
1939 found = 0;
1940 fcport = NULL;
1941 list_for_each_entry(fcport, &ha->fcports, list) {
1942 if (memcmp(new_fcport->port_name, fcport->port_name,
1943 WWN_SIZE))
1944 continue;
1945
1946 fcport->flags &= ~(FCF_FABRIC_DEVICE |
1947 FCF_PERSISTENT_BOUND);
1948 fcport->loop_id = new_fcport->loop_id;
1949 fcport->port_type = new_fcport->port_type;
1950 fcport->d_id.b24 = new_fcport->d_id.b24;
1951 memcpy(fcport->node_name, new_fcport->node_name,
1952 WWN_SIZE);
1953
1954 found++;
1955 break;
1956 }
1957
1958 if (!found) {
1959 /* New device, add to fcports list. */
1960 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1961 list_add_tail(&new_fcport->list, &ha->fcports);
1962
1963 /* Allocate a new replacement fcport. */
1964 fcport = new_fcport;
1965 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1966 if (new_fcport == NULL) {
1967 rval = QLA_MEMORY_ALLOC_FAILED;
1968 goto cleanup_allocation;
1969 }
1970 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1971 }
1972
1973 qla2x00_update_fcport(ha, fcport);
1974
1975 found_devs++;
1976 }
1977
1978cleanup_allocation:
1979 if (new_fcport)
1980 kfree(new_fcport);
1981
1982 if (rval != QLA_SUCCESS) {
1983 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
1984 "rval=%x\n", ha->host_no, rval));
1985 }
1986
1987 if (found_devs) {
1988 ha->device_flags |= DFLG_LOCAL_DEVICES;
1989 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
1990 }
1991
1992 return (rval);
1993}
1994
1995static void
fa2a1ce5 1996qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
1da177e4
LT
1997{
1998 fc_port_t *fcport;
1999
fa2a1ce5 2000 qla2x00_mark_all_devices_lost(ha);
1da177e4
LT
2001 list_for_each_entry(fcport, &ha->fcports, list) {
2002 if (fcport->port_type != FCT_TARGET)
2003 continue;
2004
fa2a1ce5 2005 qla2x00_update_fcport(ha, fcport);
1da177e4
LT
2006 }
2007}
2008
2009/*
2010 * qla2x00_update_fcport
2011 * Updates device on list.
2012 *
2013 * Input:
2014 * ha = adapter block pointer.
2015 * fcport = port structure pointer.
2016 *
2017 * Return:
2018 * 0 - Success
2019 * BIT_0 - error
2020 *
2021 * Context:
2022 * Kernel context.
2023 */
2024static void
2025qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2026{
2027 uint16_t index;
2028 unsigned long flags;
2029 srb_t *sp;
2030
2031 fcport->ha = ha;
2032 fcport->login_retry = 0;
2033 fcport->port_login_retry_count = ha->port_down_retry_count *
2034 PORT_RETRY_TIME;
2035 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2036 PORT_RETRY_TIME);
2037 fcport->flags &= ~FCF_LOGIN_NEEDED;
2038
2039 /*
2040 * Check for outstanding cmd on tape Bypass LUN discovery if active
2041 * command on tape.
2042 */
2043 if (fcport->flags & FCF_TAPE_PRESENT) {
2044 spin_lock_irqsave(&ha->hardware_lock, flags);
2045 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
bdf79621 2046 fc_port_t *sfcp;
2047
1da177e4 2048 if ((sp = ha->outstanding_cmds[index]) != 0) {
bdf79621 2049 sfcp = sp->fcport;
2050 if (sfcp == fcport) {
1da177e4
LT
2051 atomic_set(&fcport->state, FCS_ONLINE);
2052 spin_unlock_irqrestore(
2053 &ha->hardware_lock, flags);
2054 return;
2055 }
2056 }
2057 }
2058 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2059 }
2060
1da177e4 2061 if (fcport->port_type == FCT_INITIATOR ||
bdf79621 2062 fcport->port_type == FCT_BROADCAST)
1da177e4 2063 fcport->device_type = TYPE_PROCESSOR;
bdf79621 2064
1da177e4 2065 atomic_set(&fcport->state, FCS_ONLINE);
bdf79621 2066
8482e118 2067 if (ha->flags.init_done)
2068 qla2x00_reg_remote_port(ha, fcport);
1da177e4
LT
2069}
2070
8482e118 2071void
2072qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2073{
2074 struct fc_rport_identifiers rport_ids;
bdf79621 2075 struct fc_rport *rport;
8482e118 2076
2077 if (fcport->rport) {
2078 fc_remote_port_unblock(fcport->rport);
2079 return;
2080 }
2081
2082 rport_ids.node_name = be64_to_cpu(*(uint64_t *)fcport->node_name);
2083 rport_ids.port_name = be64_to_cpu(*(uint64_t *)fcport->port_name);
2084 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2085 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2086 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2087 if (fcport->port_type == FCT_INITIATOR)
2088 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2089 if (fcport->port_type == FCT_TARGET)
2090 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2091
bdf79621 2092 fcport->rport = rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2093 if (!rport)
8482e118 2094 qla_printk(KERN_WARNING, ha,
2095 "Unable to allocate fc remote port!\n");
1da177e4 2096
cc4731f5
AV
2097 if (rport->scsi_target_id != -1 &&
2098 rport->scsi_target_id < ha->host->max_id)
bdf79621 2099 fcport->os_target_id = rport->scsi_target_id;
1da177e4 2100
bdf79621 2101 rport->dd_data = fcport;
1da177e4
LT
2102}
2103
1da177e4
LT
2104/*
2105 * qla2x00_configure_fabric
2106 * Setup SNS devices with loop ID's.
2107 *
2108 * Input:
2109 * ha = adapter block pointer.
2110 *
2111 * Returns:
2112 * 0 = success.
2113 * BIT_0 = error
2114 */
2115static int
2116qla2x00_configure_fabric(scsi_qla_host_t *ha)
2117{
2118 int rval, rval2;
2119 fc_port_t *fcport, *fcptemp;
2120 uint16_t next_loopid;
2121 uint16_t mb[MAILBOX_REGISTER_COUNT];
0107109e 2122 uint16_t loop_id;
1da177e4
LT
2123 LIST_HEAD(new_fcports);
2124
2125 /* If FL port exists, then SNS is present */
0107109e
AV
2126 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
2127 loop_id = NPH_F_PORT;
2128 else
2129 loop_id = SNS_FL_PORT;
2130 rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
1da177e4
LT
2131 if (rval != QLA_SUCCESS) {
2132 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2133 "Port\n", ha->host_no));
2134
2135 ha->device_flags &= ~SWITCH_FOUND;
2136 return (QLA_SUCCESS);
2137 }
2138
2139 /* Mark devices that need re-synchronization. */
2140 rval2 = qla2x00_device_resync(ha);
2141 if (rval2 == QLA_RSCNS_HANDLED) {
2142 /* No point doing the scan, just continue. */
2143 return (QLA_SUCCESS);
2144 }
2145 do {
2146 /* Ensure we are logged into the SNS. */
0107109e
AV
2147 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
2148 loop_id = NPH_SNS;
2149 else
2150 loop_id = SIMPLE_NAME_SERVER;
2151 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
abbd8870 2152 0xfc, mb, BIT_1 | BIT_0);
1da177e4
LT
2153 if (mb[0] != MBS_COMMAND_COMPLETE) {
2154 DEBUG2(qla_printk(KERN_INFO, ha,
2155 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
0107109e 2156 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
1da177e4
LT
2157 mb[0], mb[1], mb[2], mb[6], mb[7]));
2158 return (QLA_SUCCESS);
2159 }
2160
2161 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2162 if (qla2x00_rft_id(ha)) {
2163 /* EMPTY */
2164 DEBUG2(printk("scsi(%ld): Register FC-4 "
2165 "TYPE failed.\n", ha->host_no));
2166 }
2167 if (qla2x00_rff_id(ha)) {
2168 /* EMPTY */
2169 DEBUG2(printk("scsi(%ld): Register FC-4 "
2170 "Features failed.\n", ha->host_no));
2171 }
2172 if (qla2x00_rnn_id(ha)) {
2173 /* EMPTY */
2174 DEBUG2(printk("scsi(%ld): Register Node Name "
2175 "failed.\n", ha->host_no));
2176 } else if (qla2x00_rsnn_nn(ha)) {
2177 /* EMPTY */
2178 DEBUG2(printk("scsi(%ld): Register Symbolic "
2179 "Node Name failed.\n", ha->host_no));
2180 }
2181 }
2182
2183 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2184 if (rval != QLA_SUCCESS)
2185 break;
2186
2187 /*
2188 * Logout all previous fabric devices marked lost, except
2189 * tape devices.
2190 */
2191 list_for_each_entry(fcport, &ha->fcports, list) {
2192 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2193 break;
2194
2195 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2196 continue;
2197
2198 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2199 qla2x00_mark_device_lost(ha, fcport,
2200 ql2xplogiabsentdevice);
2201 if (fcport->loop_id != FC_NO_LOOP_ID &&
2202 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2203 fcport->port_type != FCT_INITIATOR &&
2204 fcport->port_type != FCT_BROADCAST) {
2205
abbd8870 2206 ha->isp_ops.fabric_logout(ha,
1c7c6357
AV
2207 fcport->loop_id,
2208 fcport->d_id.b.domain,
2209 fcport->d_id.b.area,
2210 fcport->d_id.b.al_pa);
1da177e4
LT
2211 fcport->loop_id = FC_NO_LOOP_ID;
2212 }
2213 }
2214 }
2215
2216 /* Starting free loop ID. */
2217 next_loopid = ha->min_external_loopid;
2218
2219 /*
2220 * Scan through our port list and login entries that need to be
2221 * logged in.
2222 */
2223 list_for_each_entry(fcport, &ha->fcports, list) {
2224 if (atomic_read(&ha->loop_down_timer) ||
2225 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2226 break;
2227
2228 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2229 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2230 continue;
2231
2232 if (fcport->loop_id == FC_NO_LOOP_ID) {
2233 fcport->loop_id = next_loopid;
2234 rval = qla2x00_find_new_loop_id(ha, fcport);
2235 if (rval != QLA_SUCCESS) {
2236 /* Ran out of IDs to use */
2237 break;
2238 }
2239 }
1da177e4
LT
2240 /* Login and update database */
2241 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2242 }
2243
2244 /* Exit if out of loop IDs. */
2245 if (rval != QLA_SUCCESS) {
2246 break;
2247 }
2248
2249 /*
2250 * Login and add the new devices to our port list.
2251 */
2252 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2253 if (atomic_read(&ha->loop_down_timer) ||
2254 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2255 break;
2256
2257 /* Find a new loop ID to use. */
2258 fcport->loop_id = next_loopid;
2259 rval = qla2x00_find_new_loop_id(ha, fcport);
2260 if (rval != QLA_SUCCESS) {
2261 /* Ran out of IDs to use */
2262 break;
2263 }
2264
1da177e4
LT
2265 /* Remove device from the new list and add it to DB */
2266 list_del(&fcport->list);
2267 list_add_tail(&fcport->list, &ha->fcports);
bdf79621 2268
2269 /* Login and update database */
2270 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
1da177e4
LT
2271 }
2272 } while (0);
2273
2274 /* Free all new device structures not processed. */
2275 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2276 list_del(&fcport->list);
2277 kfree(fcport);
2278 }
2279
2280 if (rval) {
2281 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2282 "rval=%d\n", ha->host_no, rval));
2283 }
2284
2285 return (rval);
2286}
2287
2288
2289/*
2290 * qla2x00_find_all_fabric_devs
2291 *
2292 * Input:
2293 * ha = adapter block pointer.
2294 * dev = database device entry pointer.
2295 *
2296 * Returns:
2297 * 0 = success.
2298 *
2299 * Context:
2300 * Kernel context.
2301 */
2302static int
2303qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2304{
2305 int rval;
2306 uint16_t loop_id;
2307 fc_port_t *fcport, *new_fcport, *fcptemp;
2308 int found;
2309
2310 sw_info_t *swl;
2311 int swl_idx;
2312 int first_dev, last_dev;
2313 port_id_t wrap, nxt_d_id;
2314
2315 rval = QLA_SUCCESS;
2316
2317 /* Try GID_PT to get device list, else GAN. */
2318 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2319 if (swl == NULL) {
2320 /*EMPTY*/
2321 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2322 "on GA_NXT\n", ha->host_no));
2323 } else {
2324 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2325 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2326 kfree(swl);
2327 swl = NULL;
2328 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2329 kfree(swl);
2330 swl = NULL;
2331 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2332 kfree(swl);
2333 swl = NULL;
2334 }
2335 }
2336 swl_idx = 0;
2337
2338 /* Allocate temporary fcport for any new fcports discovered. */
2339 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2340 if (new_fcport == NULL) {
2341 if (swl)
2342 kfree(swl);
2343 return (QLA_MEMORY_ALLOC_FAILED);
2344 }
2345 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2346
2347 /* Set start port ID scan at adapter ID. */
2348 first_dev = 1;
2349 last_dev = 0;
2350
2351 /* Starting free loop ID. */
2352 loop_id = ha->min_external_loopid;
1da177e4 2353 for (; loop_id <= ha->last_loop_id; loop_id++) {
3d71644c 2354 if (qla2x00_is_reserved_id(ha, loop_id))
1da177e4
LT
2355 continue;
2356
2357 if (atomic_read(&ha->loop_down_timer) ||
2358 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2359 break;
2360
2361 if (swl != NULL) {
2362 if (last_dev) {
2363 wrap.b24 = new_fcport->d_id.b24;
2364 } else {
2365 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2366 memcpy(new_fcport->node_name,
2367 swl[swl_idx].node_name, WWN_SIZE);
2368 memcpy(new_fcport->port_name,
2369 swl[swl_idx].port_name, WWN_SIZE);
2370
2371 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2372 last_dev = 1;
2373 }
2374 swl_idx++;
2375 }
2376 } else {
2377 /* Send GA_NXT to the switch */
2378 rval = qla2x00_ga_nxt(ha, new_fcport);
2379 if (rval != QLA_SUCCESS) {
2380 qla_printk(KERN_WARNING, ha,
2381 "SNS scan failed -- assuming zero-entry "
2382 "result...\n");
2383 list_for_each_entry_safe(fcport, fcptemp,
2384 new_fcports, list) {
2385 list_del(&fcport->list);
2386 kfree(fcport);
2387 }
2388 rval = QLA_SUCCESS;
2389 break;
2390 }
2391 }
2392
2393 /* If wrap on switch device list, exit. */
2394 if (first_dev) {
2395 wrap.b24 = new_fcport->d_id.b24;
2396 first_dev = 0;
2397 } else if (new_fcport->d_id.b24 == wrap.b24) {
2398 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2399 ha->host_no, new_fcport->d_id.b.domain,
2400 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2401 break;
2402 }
2403
2404 /* Bypass if host adapter. */
2405 if (new_fcport->d_id.b24 == ha->d_id.b24)
2406 continue;
2407
2408 /* Bypass reserved domain fields. */
2409 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2410 continue;
2411
2412 /* Locate matching device in database. */
2413 found = 0;
2414 list_for_each_entry(fcport, &ha->fcports, list) {
2415 if (memcmp(new_fcport->port_name, fcport->port_name,
2416 WWN_SIZE))
2417 continue;
2418
2419 found++;
2420
2421 /*
2422 * If address the same and state FCS_ONLINE, nothing
2423 * changed.
2424 */
2425 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2426 atomic_read(&fcport->state) == FCS_ONLINE) {
2427 break;
2428 }
2429
2430 /*
2431 * If device was not a fabric device before.
2432 */
2433 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2434 fcport->d_id.b24 = new_fcport->d_id.b24;
2435 fcport->loop_id = FC_NO_LOOP_ID;
2436 fcport->flags |= (FCF_FABRIC_DEVICE |
2437 FCF_LOGIN_NEEDED);
2438 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2439 break;
2440 }
2441
2442 /*
2443 * Port ID changed or device was marked to be updated;
2444 * Log it out if still logged in and mark it for
2445 * relogin later.
2446 */
2447 fcport->d_id.b24 = new_fcport->d_id.b24;
2448 fcport->flags |= FCF_LOGIN_NEEDED;
2449 if (fcport->loop_id != FC_NO_LOOP_ID &&
2450 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2451 fcport->port_type != FCT_INITIATOR &&
2452 fcport->port_type != FCT_BROADCAST) {
1c7c6357
AV
2453 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2454 fcport->d_id.b.domain, fcport->d_id.b.area,
2455 fcport->d_id.b.al_pa);
1da177e4
LT
2456 fcport->loop_id = FC_NO_LOOP_ID;
2457 }
2458
2459 break;
2460 }
2461
2462 if (found)
2463 continue;
2464
2465 /* If device was not in our fcports list, then add it. */
2466 list_add_tail(&new_fcport->list, new_fcports);
2467
2468 /* Allocate a new replacement fcport. */
2469 nxt_d_id.b24 = new_fcport->d_id.b24;
2470 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2471 if (new_fcport == NULL) {
2472 if (swl)
2473 kfree(swl);
2474 return (QLA_MEMORY_ALLOC_FAILED);
2475 }
2476 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2477 new_fcport->d_id.b24 = nxt_d_id.b24;
2478 }
2479
2480 if (swl)
2481 kfree(swl);
2482
2483 if (new_fcport)
2484 kfree(new_fcport);
2485
2486 if (!list_empty(new_fcports))
2487 ha->device_flags |= DFLG_FABRIC_DEVICES;
2488
2489 return (rval);
2490}
2491
2492/*
2493 * qla2x00_find_new_loop_id
2494 * Scan through our port list and find a new usable loop ID.
2495 *
2496 * Input:
2497 * ha: adapter state pointer.
2498 * dev: port structure pointer.
2499 *
2500 * Returns:
2501 * qla2x00 local function return status code.
2502 *
2503 * Context:
2504 * Kernel context.
2505 */
2506int
2507qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2508{
2509 int rval;
2510 int found;
2511 fc_port_t *fcport;
2512 uint16_t first_loop_id;
2513
2514 rval = QLA_SUCCESS;
2515
2516 /* Save starting loop ID. */
2517 first_loop_id = dev->loop_id;
2518
2519 for (;;) {
2520 /* Skip loop ID if already used by adapter. */
2521 if (dev->loop_id == ha->loop_id) {
2522 dev->loop_id++;
2523 }
2524
2525 /* Skip reserved loop IDs. */
3d71644c 2526 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
1da177e4
LT
2527 dev->loop_id++;
2528 }
2529
2530 /* Reset loop ID if passed the end. */
2531 if (dev->loop_id > ha->last_loop_id) {
2532 /* first loop ID. */
2533 dev->loop_id = ha->min_external_loopid;
2534 }
2535
2536 /* Check for loop ID being already in use. */
2537 found = 0;
2538 fcport = NULL;
2539 list_for_each_entry(fcport, &ha->fcports, list) {
2540 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2541 /* ID possibly in use */
2542 found++;
2543 break;
2544 }
2545 }
2546
2547 /* If not in use then it is free to use. */
2548 if (!found) {
2549 break;
2550 }
2551
2552 /* ID in use. Try next value. */
2553 dev->loop_id++;
2554
2555 /* If wrap around. No free ID to use. */
2556 if (dev->loop_id == first_loop_id) {
2557 dev->loop_id = FC_NO_LOOP_ID;
2558 rval = QLA_FUNCTION_FAILED;
2559 break;
2560 }
2561 }
2562
2563 return (rval);
2564}
2565
2566/*
2567 * qla2x00_device_resync
2568 * Marks devices in the database that needs resynchronization.
2569 *
2570 * Input:
2571 * ha = adapter block pointer.
2572 *
2573 * Context:
2574 * Kernel context.
2575 */
2576static int
fa2a1ce5 2577qla2x00_device_resync(scsi_qla_host_t *ha)
1da177e4
LT
2578{
2579 int rval;
2580 int rval2;
2581 uint32_t mask;
2582 fc_port_t *fcport;
2583 uint32_t rscn_entry;
2584 uint8_t rscn_out_iter;
2585 uint8_t format;
2586 port_id_t d_id;
2587
2588 rval = QLA_RSCNS_HANDLED;
2589
2590 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2591 ha->flags.rscn_queue_overflow) {
2592
2593 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2594 format = MSB(MSW(rscn_entry));
2595 d_id.b.domain = LSB(MSW(rscn_entry));
2596 d_id.b.area = MSB(LSW(rscn_entry));
2597 d_id.b.al_pa = LSB(LSW(rscn_entry));
2598
2599 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2600 "[%02x/%02x%02x%02x].\n",
2601 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2602 d_id.b.area, d_id.b.al_pa));
2603
2604 ha->rscn_out_ptr++;
2605 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2606 ha->rscn_out_ptr = 0;
2607
2608 /* Skip duplicate entries. */
2609 for (rscn_out_iter = ha->rscn_out_ptr;
2610 !ha->flags.rscn_queue_overflow &&
2611 rscn_out_iter != ha->rscn_in_ptr;
2612 rscn_out_iter = (rscn_out_iter ==
2613 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2614
2615 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2616 break;
2617
2618 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2619 "entry found at [%d].\n", ha->host_no,
2620 rscn_out_iter));
2621
2622 ha->rscn_out_ptr = rscn_out_iter;
2623 }
2624
2625 /* Queue overflow, set switch default case. */
2626 if (ha->flags.rscn_queue_overflow) {
2627 DEBUG(printk("scsi(%ld): device_resync: rscn "
2628 "overflow.\n", ha->host_no));
2629
2630 format = 3;
2631 ha->flags.rscn_queue_overflow = 0;
2632 }
2633
2634 switch (format) {
2635 case 0:
2636 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2637 !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
0107109e 2638 !IS_QLA24XX(ha) && !IS_QLA25XX(ha) &&
1da177e4
LT
2639 ha->flags.init_done) {
2640 /* Handle port RSCN via asyncronous IOCBs */
2641 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2642 NULL, 0);
2643 if (rval2 == QLA_SUCCESS)
2644 continue;
2645 }
2646 mask = 0xffffff;
2647 break;
2648 case 1:
2649 mask = 0xffff00;
2650 break;
2651 case 2:
2652 mask = 0xff0000;
2653 break;
2654 default:
2655 mask = 0x0;
2656 d_id.b24 = 0;
2657 ha->rscn_out_ptr = ha->rscn_in_ptr;
2658 break;
2659 }
2660
2661 rval = QLA_SUCCESS;
2662
2663 /* Abort any outstanding IO descriptors. */
2664 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2665 qla2x00_cancel_io_descriptors(ha);
2666
2667 list_for_each_entry(fcport, &ha->fcports, list) {
2668 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2669 (fcport->d_id.b24 & mask) != d_id.b24 ||
2670 fcport->port_type == FCT_BROADCAST)
2671 continue;
2672
2673 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2674 if (format != 3 ||
2675 fcport->port_type != FCT_INITIATOR) {
8482e118 2676 qla2x00_mark_device_lost(ha, fcport, 0);
1da177e4
LT
2677 }
2678 }
2679 fcport->flags &= ~FCF_FARP_DONE;
2680 }
2681 }
2682 return (rval);
2683}
2684
2685/*
2686 * qla2x00_fabric_dev_login
2687 * Login fabric target device and update FC port database.
2688 *
2689 * Input:
2690 * ha: adapter state pointer.
2691 * fcport: port structure list pointer.
2692 * next_loopid: contains value of a new loop ID that can be used
2693 * by the next login attempt.
2694 *
2695 * Returns:
2696 * qla2x00 local function return status code.
2697 *
2698 * Context:
2699 * Kernel context.
2700 */
2701static int
2702qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2703 uint16_t *next_loopid)
2704{
2705 int rval;
2706 int retry;
0107109e 2707 uint8_t opts;
1da177e4
LT
2708
2709 rval = QLA_SUCCESS;
2710 retry = 0;
2711
2712 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2713 if (rval == QLA_SUCCESS) {
0107109e
AV
2714 /* Send an ADISC to tape devices.*/
2715 opts = 0;
2716 if (fcport->flags & FCF_TAPE_PRESENT)
2717 opts |= BIT_1;
2718 rval = qla2x00_get_port_database(ha, fcport, opts);
1da177e4 2719 if (rval != QLA_SUCCESS) {
1c7c6357
AV
2720 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2721 fcport->d_id.b.domain, fcport->d_id.b.area,
2722 fcport->d_id.b.al_pa);
0107109e
AV
2723 qla2x00_mark_device_lost(ha, fcport, 1);
2724
1da177e4
LT
2725 } else {
2726 qla2x00_update_fcport(ha, fcport);
2727 }
2728 }
2729
2730 return (rval);
2731}
2732
2733/*
2734 * qla2x00_fabric_login
2735 * Issue fabric login command.
2736 *
2737 * Input:
2738 * ha = adapter block pointer.
2739 * device = pointer to FC device type structure.
2740 *
2741 * Returns:
2742 * 0 - Login successfully
2743 * 1 - Login failed
2744 * 2 - Initiator device
2745 * 3 - Fatal error
2746 */
2747int
2748qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2749 uint16_t *next_loopid)
2750{
2751 int rval;
2752 int retry;
2753 uint16_t tmp_loopid;
2754 uint16_t mb[MAILBOX_REGISTER_COUNT];
2755
2756 retry = 0;
2757 tmp_loopid = 0;
2758
2759 for (;;) {
2760 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2761 "for port %02x%02x%02x.\n",
2762 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2763 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2764
2765 /* Login fcport on switch. */
abbd8870 2766 ha->isp_ops.fabric_login(ha, fcport->loop_id,
1da177e4
LT
2767 fcport->d_id.b.domain, fcport->d_id.b.area,
2768 fcport->d_id.b.al_pa, mb, BIT_0);
2769 if (mb[0] == MBS_PORT_ID_USED) {
2770 /*
2771 * Device has another loop ID. The firmware team
0107109e
AV
2772 * recommends the driver perform an implicit login with
2773 * the specified ID again. The ID we just used is save
2774 * here so we return with an ID that can be tried by
2775 * the next login.
1da177e4
LT
2776 */
2777 retry++;
2778 tmp_loopid = fcport->loop_id;
2779 fcport->loop_id = mb[1];
2780
2781 DEBUG(printk("Fabric Login: port in use - next "
2782 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2783 fcport->loop_id, fcport->d_id.b.domain,
2784 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2785
2786 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2787 /*
2788 * Login succeeded.
2789 */
2790 if (retry) {
2791 /* A retry occurred before. */
2792 *next_loopid = tmp_loopid;
2793 } else {
2794 /*
2795 * No retry occurred before. Just increment the
2796 * ID value for next login.
2797 */
2798 *next_loopid = (fcport->loop_id + 1);
2799 }
2800
2801 if (mb[1] & BIT_0) {
2802 fcport->port_type = FCT_INITIATOR;
2803 } else {
2804 fcport->port_type = FCT_TARGET;
2805 if (mb[1] & BIT_1) {
2806 fcport->flags |= FCF_TAPE_PRESENT;
2807 }
2808 }
2809
2810 rval = QLA_SUCCESS;
2811 break;
2812 } else if (mb[0] == MBS_LOOP_ID_USED) {
2813 /*
2814 * Loop ID already used, try next loop ID.
2815 */
2816 fcport->loop_id++;
2817 rval = qla2x00_find_new_loop_id(ha, fcport);
2818 if (rval != QLA_SUCCESS) {
2819 /* Ran out of loop IDs to use */
2820 break;
2821 }
2822 } else if (mb[0] == MBS_COMMAND_ERROR) {
2823 /*
2824 * Firmware possibly timed out during login. If NO
2825 * retries are left to do then the device is declared
2826 * dead.
2827 */
2828 *next_loopid = fcport->loop_id;
1c7c6357
AV
2829 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2830 fcport->d_id.b.domain, fcport->d_id.b.area,
2831 fcport->d_id.b.al_pa);
1da177e4
LT
2832 qla2x00_mark_device_lost(ha, fcport, 1);
2833
2834 rval = 1;
2835 break;
2836 } else {
2837 /*
2838 * unrecoverable / not handled error
2839 */
2840 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
fa2a1ce5
AV
2841 "loop_id=%x jiffies=%lx.\n",
2842 __func__, ha->host_no, mb[0],
1da177e4
LT
2843 fcport->d_id.b.domain, fcport->d_id.b.area,
2844 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2845
2846 *next_loopid = fcport->loop_id;
1c7c6357
AV
2847 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2848 fcport->d_id.b.domain, fcport->d_id.b.area,
2849 fcport->d_id.b.al_pa);
1da177e4
LT
2850 fcport->loop_id = FC_NO_LOOP_ID;
2851 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2852
2853 rval = 3;
2854 break;
2855 }
2856 }
2857
2858 return (rval);
2859}
2860
2861/*
2862 * qla2x00_local_device_login
2863 * Issue local device login command.
2864 *
2865 * Input:
2866 * ha = adapter block pointer.
2867 * loop_id = loop id of device to login to.
2868 *
2869 * Returns (Where's the #define!!!!):
2870 * 0 - Login successfully
2871 * 1 - Login failed
2872 * 3 - Fatal error
2873 */
2874int
2875qla2x00_local_device_login(scsi_qla_host_t *ha, uint16_t loop_id)
2876{
2877 int rval;
2878 uint16_t mb[MAILBOX_REGISTER_COUNT];
2879
2880 memset(mb, 0, sizeof(mb));
2881 rval = qla2x00_login_local_device(ha, loop_id, mb, BIT_0);
2882 if (rval == QLA_SUCCESS) {
2883 /* Interrogate mailbox registers for any errors */
2884 if (mb[0] == MBS_COMMAND_ERROR)
2885 rval = 1;
2886 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2887 /* device not in PCB table */
2888 rval = 3;
2889 }
2890
2891 return (rval);
2892}
2893
2894/*
2895 * qla2x00_loop_resync
2896 * Resync with fibre channel devices.
2897 *
2898 * Input:
2899 * ha = adapter block pointer.
2900 *
2901 * Returns:
2902 * 0 = success
2903 */
2904int
fa2a1ce5 2905qla2x00_loop_resync(scsi_qla_host_t *ha)
1da177e4
LT
2906{
2907 int rval;
2908 uint32_t wait_time;
2909
2910 rval = QLA_SUCCESS;
2911
2912 atomic_set(&ha->loop_state, LOOP_UPDATE);
1da177e4
LT
2913 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2914 if (ha->flags.online) {
2915 if (!(rval = qla2x00_fw_ready(ha))) {
2916 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2917 wait_time = 256;
2918 do {
1da177e4
LT
2919 atomic_set(&ha->loop_state, LOOP_UPDATE);
2920
0107109e
AV
2921 /* Issue a marker after FW becomes ready. */
2922 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2923 ha->marker_needed = 0;
1da177e4
LT
2924
2925 /* Remap devices on Loop. */
2926 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2927
2928 qla2x00_configure_loop(ha);
2929 wait_time--;
2930 } while (!atomic_read(&ha->loop_down_timer) &&
2931 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2932 wait_time &&
2933 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2934 }
1da177e4
LT
2935 }
2936
2937 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2938 return (QLA_FUNCTION_FAILED);
2939 }
2940
2941 if (rval) {
2942 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
2943 }
2944
2945 return (rval);
2946}
2947
1da177e4
LT
2948void
2949qla2x00_rescan_fcports(scsi_qla_host_t *ha)
2950{
2951 int rescan_done;
2952 fc_port_t *fcport;
2953
2954 rescan_done = 0;
2955 list_for_each_entry(fcport, &ha->fcports, list) {
2956 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
2957 continue;
2958
2959 qla2x00_update_fcport(ha, fcport);
2960 fcport->flags &= ~FCF_RESCAN_NEEDED;
2961
2962 rescan_done = 1;
2963 }
fa2a1ce5 2964 qla2x00_probe_for_all_luns(ha);
1da177e4
LT
2965}
2966
2967/*
2968* qla2x00_abort_isp
2969* Resets ISP and aborts all outstanding commands.
2970*
2971* Input:
2972* ha = adapter block pointer.
2973*
2974* Returns:
2975* 0 = success
2976*/
2977int
2978qla2x00_abort_isp(scsi_qla_host_t *ha)
2979{
2980 unsigned long flags = 0;
2981 uint16_t cnt;
2982 srb_t *sp;
2983 uint8_t status = 0;
2984
2985 if (ha->flags.online) {
2986 ha->flags.online = 0;
2987 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1da177e4
LT
2988
2989 qla_printk(KERN_INFO, ha,
2990 "Performing ISP error recovery - ha= %p.\n", ha);
abbd8870 2991 ha->isp_ops.reset_chip(ha);
1da177e4
LT
2992
2993 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
2994 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
2995 atomic_set(&ha->loop_state, LOOP_DOWN);
2996 qla2x00_mark_all_devices_lost(ha);
2997 } else {
2998 if (!atomic_read(&ha->loop_down_timer))
2999 atomic_set(&ha->loop_down_timer,
3000 LOOP_DOWN_TIME);
3001 }
3002
3003 spin_lock_irqsave(&ha->hardware_lock, flags);
3004 /* Requeue all commands in outstanding command list. */
3005 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3006 sp = ha->outstanding_cmds[cnt];
3007 if (sp) {
3008 ha->outstanding_cmds[cnt] = NULL;
1da177e4 3009 sp->flags = 0;
f4f051eb 3010 sp->cmd->result = DID_RESET << 16;
1da177e4 3011 sp->cmd->host_scribble = (unsigned char *)NULL;
f4f051eb 3012 qla2x00_sp_compl(ha, sp);
1da177e4
LT
3013 }
3014 }
3015 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3016
abbd8870 3017 ha->isp_ops.nvram_config(ha);
1da177e4
LT
3018
3019 if (!qla2x00_restart_isp(ha)) {
3020 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3021
3022 if (!atomic_read(&ha->loop_down_timer)) {
3023 /*
3024 * Issue marker command only when we are going
3025 * to start the I/O .
3026 */
3027 ha->marker_needed = 1;
3028 }
3029
3030 ha->flags.online = 1;
3031
abbd8870 3032 ha->isp_ops.enable_intrs(ha);
1da177e4 3033
fa2a1ce5 3034 ha->isp_abort_cnt = 0;
1da177e4
LT
3035 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3036 } else { /* failed the ISP abort */
3037 ha->flags.online = 1;
3038 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3039 if (ha->isp_abort_cnt == 0) {
3040 qla_printk(KERN_WARNING, ha,
3041 "ISP error recovery failed - "
3042 "board disabled\n");
fa2a1ce5 3043 /*
1da177e4
LT
3044 * The next call disables the board
3045 * completely.
3046 */
abbd8870 3047 ha->isp_ops.reset_adapter(ha);
1da177e4
LT
3048 ha->flags.online = 0;
3049 clear_bit(ISP_ABORT_RETRY,
3050 &ha->dpc_flags);
3051 status = 0;
3052 } else { /* schedule another ISP abort */
3053 ha->isp_abort_cnt--;
3054 DEBUG(printk("qla%ld: ISP abort - "
0107109e 3055 "retry remaining %d\n",
1da177e4
LT
3056 ha->host_no, ha->isp_abort_cnt);)
3057 status = 1;
3058 }
3059 } else {
3060 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3061 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3062 "- retrying (%d) more times\n",
3063 ha->host_no, ha->isp_abort_cnt);)
3064 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3065 status = 1;
3066 }
3067 }
fa2a1ce5 3068
1da177e4
LT
3069 }
3070
3071 if (status) {
3072 qla_printk(KERN_INFO, ha,
3073 "qla2x00_abort_isp: **** FAILED ****\n");
3074 } else {
3075 DEBUG(printk(KERN_INFO
3076 "qla2x00_abort_isp(%ld): exiting.\n",
3077 ha->host_no);)
3078 }
3079
3080 return(status);
3081}
3082
3083/*
3084* qla2x00_restart_isp
3085* restarts the ISP after a reset
3086*
3087* Input:
3088* ha = adapter block pointer.
3089*
3090* Returns:
3091* 0 = success
3092*/
3093static int
3094qla2x00_restart_isp(scsi_qla_host_t *ha)
3095{
3096 uint8_t status = 0;
3d71644c 3097 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
3098 unsigned long flags = 0;
3099 uint32_t wait_time;
3100
3101 /* If firmware needs to be loaded */
3102 if (qla2x00_isp_firmware(ha)) {
3103 ha->flags.online = 0;
abbd8870 3104 if (!(status = ha->isp_ops.chip_diag(ha))) {
1da177e4
LT
3105 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3106 status = qla2x00_setup_chip(ha);
3107 goto done;
3108 }
3109
1da177e4
LT
3110 spin_lock_irqsave(&ha->hardware_lock, flags);
3111
0107109e
AV
3112 if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
3113 /*
3114 * Disable SRAM, Instruction RAM and GP RAM
3115 * parity.
3116 */
3117 WRT_REG_WORD(&reg->hccr,
3118 (HCCR_ENABLE_PARITY + 0x0));
3119 RD_REG_WORD(&reg->hccr);
3120 }
1da177e4
LT
3121
3122 spin_unlock_irqrestore(&ha->hardware_lock, flags);
fa2a1ce5 3123
1da177e4
LT
3124 status = qla2x00_setup_chip(ha);
3125
3126 spin_lock_irqsave(&ha->hardware_lock, flags);
fa2a1ce5 3127
0107109e
AV
3128 if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
3129 /* Enable proper parity */
3130 if (IS_QLA2300(ha))
3131 /* SRAM parity */
3132 WRT_REG_WORD(&reg->hccr,
3133 (HCCR_ENABLE_PARITY + 0x1));
3134 else
3135 /*
3136 * SRAM, Instruction RAM and GP RAM
3137 * parity.
3138 */
3139 WRT_REG_WORD(&reg->hccr,
3140 (HCCR_ENABLE_PARITY + 0x7));
3141 RD_REG_WORD(&reg->hccr);
3142 }
1da177e4
LT
3143
3144 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3145 }
3146 }
3147
3148 done:
3149 if (!status && !(status = qla2x00_init_rings(ha))) {
3150 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3151 if (!(status = qla2x00_fw_ready(ha))) {
3152 DEBUG(printk("%s(): Start configure loop, "
0107109e
AV
3153 "status = %d\n", __func__, status);)
3154
3155 /* Issue a marker after FW becomes ready. */
3156 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3157
1da177e4
LT
3158 ha->flags.online = 1;
3159 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3160 wait_time = 256;
3161 do {
3162 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3163 qla2x00_configure_loop(ha);
3164 wait_time--;
3165 } while (!atomic_read(&ha->loop_down_timer) &&
3166 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3167 wait_time &&
3168 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3169 }
3170
3171 /* if no cable then assume it's good */
fa2a1ce5 3172 if ((ha->device_flags & DFLG_NO_CABLE))
1da177e4
LT
3173 status = 0;
3174
3175 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3176 __func__,
3177 status);)
3178 }
3179 return (status);
3180}
3181
3182/*
3183* qla2x00_reset_adapter
3184* Reset adapter.
3185*
3186* Input:
3187* ha = adapter block pointer.
3188*/
abbd8870 3189void
1da177e4
LT
3190qla2x00_reset_adapter(scsi_qla_host_t *ha)
3191{
3192 unsigned long flags = 0;
3d71644c 3193 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
3194
3195 ha->flags.online = 0;
abbd8870 3196 ha->isp_ops.disable_intrs(ha);
1da177e4 3197
1da177e4
LT
3198 spin_lock_irqsave(&ha->hardware_lock, flags);
3199 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3200 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3201 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3202 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3203 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3204}
0107109e
AV
3205
3206void
3207qla24xx_reset_adapter(scsi_qla_host_t *ha)
3208{
3209 unsigned long flags = 0;
3210 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3211
3212 ha->flags.online = 0;
3213 ha->isp_ops.disable_intrs(ha);
3214
3215 spin_lock_irqsave(&ha->hardware_lock, flags);
3216 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3217 RD_REG_DWORD(&reg->hccr);
3218 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3219 RD_REG_DWORD(&reg->hccr);
3220 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3221}
3222
3223int
3224qla24xx_nvram_config(scsi_qla_host_t *ha)
3225{
3226 int rval;
3227 struct init_cb_24xx *icb;
3228 struct nvram_24xx *nv;
3229 uint32_t *dptr;
3230 uint8_t *dptr1, *dptr2;
3231 uint32_t chksum;
3232 uint16_t cnt;
3233
3234 rval = QLA_SUCCESS;
3235 icb = (struct init_cb_24xx *)ha->init_cb;
3236 nv = (struct nvram_24xx *)ha->request_ring;
3237
3238 /* Determine NVRAM starting address. */
3239 ha->nvram_size = sizeof(struct nvram_24xx);
3240 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3241 if (PCI_FUNC(ha->pdev->devfn))
3242 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3243
3244 /* Get NVRAM data and calculate checksum. */
3245 dptr = (uint32_t *)nv;
3246 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3247 ha->nvram_size);
3248 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3249 chksum += le32_to_cpu(*dptr++);
3250
3251 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3252 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3253 ha->nvram_size));
3254
3255 /* Bad NVRAM data, set defaults parameters. */
3256 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3257 || nv->id[3] != ' ' ||
3258 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3259 /* Reset NVRAM data. */
3260 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3261 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3262 le16_to_cpu(nv->nvram_version));
3263 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3264 "invalid -- WWPN) defaults.\n");
3265
3266 /*
3267 * Set default initialization control block.
3268 */
3269 memset(nv, 0, ha->nvram_size);
3270 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3271 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3272 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3273 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3274 nv->exchange_count = __constant_cpu_to_le16(0);
3275 nv->hard_address = __constant_cpu_to_le16(124);
3276 nv->port_name[0] = 0x21;
3277 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3278 nv->port_name[2] = 0x00;
3279 nv->port_name[3] = 0xe0;
3280 nv->port_name[4] = 0x8b;
3281 nv->port_name[5] = 0x1c;
3282 nv->port_name[6] = 0x55;
3283 nv->port_name[7] = 0x86;
3284 nv->node_name[0] = 0x20;
3285 nv->node_name[1] = 0x00;
3286 nv->node_name[2] = 0x00;
3287 nv->node_name[3] = 0xe0;
3288 nv->node_name[4] = 0x8b;
3289 nv->node_name[5] = 0x1c;
3290 nv->node_name[6] = 0x55;
3291 nv->node_name[7] = 0x86;
3292 nv->login_retry_count = __constant_cpu_to_le16(8);
3293 nv->link_down_timeout = __constant_cpu_to_le16(200);
3294 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3295 nv->login_timeout = __constant_cpu_to_le16(0);
3296 nv->firmware_options_1 =
3297 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3298 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3299 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3300 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3301 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3302 nv->efi_parameters = __constant_cpu_to_le32(0);
3303 nv->reset_delay = 5;
3304 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3305 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3306 nv->link_down_timeout = __constant_cpu_to_le16(30);
3307
3308 rval = 1;
3309 }
3310
3311 /* Reset Initialization control block */
3312 memset(icb, 0, sizeof(struct init_cb_24xx));
3313
3314 /* Copy 1st segment. */
3315 dptr1 = (uint8_t *)icb;
3316 dptr2 = (uint8_t *)&nv->version;
3317 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3318 while (cnt--)
3319 *dptr1++ = *dptr2++;
3320
3321 icb->login_retry_count = nv->login_retry_count;
3322 icb->link_down_timeout = nv->link_down_timeout;
3323
3324 /* Copy 2nd segment. */
3325 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3326 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3327 cnt = (uint8_t *)&icb->reserved_3 -
3328 (uint8_t *)&icb->interrupt_delay_timer;
3329 while (cnt--)
3330 *dptr1++ = *dptr2++;
3331
3332 /*
3333 * Setup driver NVRAM options.
3334 */
3335 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3336 char *st, *en;
3337 uint16_t index;
3338
3339 strncpy(ha->model_number, nv->model_name,
3340 sizeof(nv->model_name));
3341 st = en = ha->model_number;
3342 en += sizeof(nv->model_name) - 1;
3343 while (en > st) {
3344 if (*en != 0x20 && *en != 0x00)
3345 break;
3346 *en-- = '\0';
3347 }
3348
3349 index = (ha->pdev->subsystem_device & 0xff);
3350 if (index < QLA_MODEL_NAMES)
3351 ha->model_desc = qla2x00_model_desc[index];
3352 } else
3353 strcpy(ha->model_number, "QLA2462");
3354
3355 /* Prepare nodename */
3356 if ((icb->firmware_options_1 & BIT_14) == 0) {
3357 /*
3358 * Firmware will apply the following mask if the nodename was
3359 * not provided.
3360 */
3361 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3362 icb->node_name[0] &= 0xF0;
3363 }
3364
3365 /* Set host adapter parameters. */
3366 ha->flags.disable_risc_code_load = 0;
3367 ha->flags.enable_lip_reset = 1;
3368 ha->flags.enable_lip_full_login = 1;
3369 ha->flags.enable_target_reset = 1;
3370 ha->flags.enable_led_scheme = 0;
3371
3372 ha->operating_mode =
3373 (icb->firmware_options_2 & (BIT_6 | BIT_5 | BIT_4)) >> 4;
3374
3375 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3376 sizeof(ha->fw_seriallink_options24));
3377
3378 /* save HBA serial number */
3379 ha->serial0 = icb->port_name[5];
3380 ha->serial1 = icb->port_name[6];
3381 ha->serial2 = icb->port_name[7];
3382 ha->node_name = icb->node_name;
3383 ha->port_name = icb->port_name;
3384
3385 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3386
3387 /* Set minimum login_timeout to 4 seconds. */
3388 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3389 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3390 if (le16_to_cpu(nv->login_timeout) < 4)
3391 nv->login_timeout = __constant_cpu_to_le16(4);
3392 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3393 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3394
3395 /* Set minimum RATOV to 200 tenths of a second. */
3396 ha->r_a_tov = 200;
3397
3398 ha->loop_reset_delay = nv->reset_delay;
3399
3400 /* Link Down Timeout = 0:
3401 *
3402 * When Port Down timer expires we will start returning
3403 * I/O's to OS with "DID_NO_CONNECT".
3404 *
3405 * Link Down Timeout != 0:
3406 *
3407 * The driver waits for the link to come up after link down
3408 * before returning I/Os to OS with "DID_NO_CONNECT".
3409 */
3410 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3411 ha->loop_down_abort_time =
3412 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3413 } else {
3414 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3415 ha->loop_down_abort_time =
3416 (LOOP_DOWN_TIME - ha->link_down_timeout);
3417 }
3418
3419 /* Need enough time to try and get the port back. */
3420 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3421 if (qlport_down_retry)
3422 ha->port_down_retry_count = qlport_down_retry;
3423
3424 /* Set login_retry_count */
3425 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3426 if (ha->port_down_retry_count ==
3427 le16_to_cpu(nv->port_down_retry_count) &&
3428 ha->port_down_retry_count > 3)
3429 ha->login_retry_count = ha->port_down_retry_count;
3430 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3431 ha->login_retry_count = ha->port_down_retry_count;
3432 if (ql2xloginretrycount)
3433 ha->login_retry_count = ql2xloginretrycount;
3434
3435 if (rval) {
3436 DEBUG2_3(printk(KERN_WARNING
3437 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3438 }
3439 return (rval);
3440}
3441
3442int
3443qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3444{
3445 int rval;
3446 uint16_t cnt;
3447 uint16_t *risc_code;
3448 unsigned long risc_address;
3449 unsigned long risc_code_size;
3450 int num;
3451 int i;
3452 uint16_t *req_ring;
3453 struct qla_fw_info *fw_iter;
3454
3455 rval = QLA_SUCCESS;
3456
3457 /* Load firmware sequences */
3458 fw_iter = ha->brd_info->fw_info;
3459 *srisc_addr = *ha->brd_info->fw_info->fwstart;
3460 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3461 risc_code = fw_iter->fwcode;
3462 risc_code_size = *fw_iter->fwlen;
3463
3464 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
3465 risc_address = *fw_iter->fwstart;
3466 } else {
3467 /* Extended address */
3468 risc_address = *fw_iter->lfwstart;
3469 }
3470
3471 num = 0;
3472 rval = 0;
3473 while (risc_code_size > 0 && !rval) {
3474 cnt = (uint16_t)(ha->fw_transfer_size >> 1);
3475 if (cnt > risc_code_size)
3476 cnt = risc_code_size;
3477
3478 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3479 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
3480 ha->host_no, risc_code, cnt, risc_address));
3481
3482 req_ring = (uint16_t *)ha->request_ring;
3483 for (i = 0; i < cnt; i++)
3484 req_ring[i] = cpu_to_le16(risc_code[i]);
3485
3486 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
3487 rval = qla2x00_load_ram(ha, ha->request_dma,
3488 risc_address, cnt);
3489 } else {
3490 rval = qla2x00_load_ram_ext(ha,
3491 ha->request_dma, risc_address, cnt);
3492 }
3493 if (rval) {
3494 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3495 "load segment %d of firmware\n",
3496 ha->host_no, num));
3497 qla_printk(KERN_WARNING, ha,
3498 "[ERROR] Failed to load segment %d of "
3499 "firmware\n", num);
3500
3501 qla2x00_dump_regs(ha);
3502 break;
3503 }
3504
3505 risc_code += cnt;
3506 risc_address += cnt;
3507 risc_code_size -= cnt;
3508 num++;
3509 }
3510
3511 /* Next firmware sequence */
3512 fw_iter++;
3513 }
3514
3515 return (rval);
3516}
3517
3518int
3519qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3520{
3521 int rval;
3522 int segments, fragment;
3523 uint32_t faddr;
3524 uint32_t *dcode, dlen;
3525 uint32_t risc_addr;
3526 uint32_t risc_size;
3527 uint32_t i;
3528
3529 rval = QLA_SUCCESS;
3530
3531 segments = FA_RISC_CODE_SEGMENTS;
3532 faddr = FA_RISC_CODE_ADDR;
3533 dcode = (uint32_t *)ha->request_ring;
3534 *srisc_addr = 0;
3535
3536 /* Validate firmware image by checking version. */
3537 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3538 for (i = 0; i < 4; i++)
3539 dcode[i] = be32_to_cpu(dcode[i]);
3540 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3541 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3542 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3543 dcode[3] == 0)) {
3544 qla_printk(KERN_WARNING, ha,
3545 "Unable to verify integrity of flash firmware image!\n");
3546 qla_printk(KERN_WARNING, ha,
3547 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3548 dcode[1], dcode[2], dcode[3]);
3549
3550 return QLA_FUNCTION_FAILED;
3551 }
3552
3553 while (segments && rval == QLA_SUCCESS) {
3554 /* Read segment's load information. */
3555 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3556
3557 risc_addr = be32_to_cpu(dcode[2]);
3558 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3559 risc_size = be32_to_cpu(dcode[3]);
3560
3561 fragment = 0;
3562 while (risc_size > 0 && rval == QLA_SUCCESS) {
3563 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3564 if (dlen > risc_size)
3565 dlen = risc_size;
3566
3567 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3568 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3569 ha->host_no, risc_addr, dlen, faddr));
3570
3571 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3572 for (i = 0; i < dlen; i++)
3573 dcode[i] = swab32(dcode[i]);
3574
3575 rval = qla2x00_load_ram_ext(ha, ha->request_dma,
3576 risc_addr, dlen);
3577 if (rval) {
3578 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3579 "segment %d of firmware\n", ha->host_no,
3580 fragment));
3581 qla_printk(KERN_WARNING, ha,
3582 "[ERROR] Failed to load segment %d of "
3583 "firmware\n", fragment);
3584 break;
3585 }
3586
3587 faddr += dlen;
3588 risc_addr += dlen;
3589 risc_size -= dlen;
3590 fragment++;
3591 }
3592
3593 /* Next segment. */
3594 segments--;
3595 }
3596
3597 return rval;
3598}
3599
3600int
3601qla24xx_load_risc_hotplug(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3602{
3603 int rval;
3604 int segments, fragment;
3605 uint32_t *dcode, dlen;
3606 uint32_t risc_addr;
3607 uint32_t risc_size;
3608 uint32_t i;
3609 const struct firmware *fw_entry;
3610 uint32_t *fwcode, fwclen;
3611
3612 if (request_firmware(&fw_entry, ha->brd_info->fw_fname,
3613 &ha->pdev->dev)) {
3614 qla_printk(KERN_ERR, ha,
3615 "Firmware image file not available: '%s'\n",
3616 ha->brd_info->fw_fname);
3617 return QLA_FUNCTION_FAILED;
3618 }
3619
3620 rval = QLA_SUCCESS;
3621
3622 segments = FA_RISC_CODE_SEGMENTS;
3623 dcode = (uint32_t *)ha->request_ring;
3624 *srisc_addr = 0;
3625 fwcode = (uint32_t *)fw_entry->data;
3626 fwclen = 0;
3627
3628 /* Validate firmware image by checking version. */
3629 if (fw_entry->size < 8 * sizeof(uint32_t)) {
3630 qla_printk(KERN_WARNING, ha,
3631 "Unable to verify integrity of flash firmware image "
3632 "(%Zd)!\n", fw_entry->size);
3633 goto fail_fw_integrity;
3634 }
3635 for (i = 0; i < 4; i++)
3636 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3637 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3638 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3639 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3640 dcode[3] == 0)) {
3641 qla_printk(KERN_WARNING, ha,
3642 "Unable to verify integrity of flash firmware image!\n");
3643 qla_printk(KERN_WARNING, ha,
3644 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3645 dcode[1], dcode[2], dcode[3]);
3646 goto fail_fw_integrity;
3647 }
3648
3649 while (segments && rval == QLA_SUCCESS) {
3650 risc_addr = be32_to_cpu(fwcode[2]);
3651 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3652 risc_size = be32_to_cpu(fwcode[3]);
3653
3654 /* Validate firmware image size. */
3655 fwclen += risc_size * sizeof(uint32_t);
3656 if (fw_entry->size < fwclen) {
3657 qla_printk(KERN_WARNING, ha,
3658 "Unable to verify integrity of flash firmware "
3659 "image (%Zd)!\n", fw_entry->size);
3660 goto fail_fw_integrity;
3661 }
3662
3663 fragment = 0;
3664 while (risc_size > 0 && rval == QLA_SUCCESS) {
3665 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3666 if (dlen > risc_size)
3667 dlen = risc_size;
3668
3669 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3670 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3671 risc_addr, dlen));
3672
3673 for (i = 0; i < dlen; i++)
3674 dcode[i] = swab32(fwcode[i]);
3675
3676 rval = qla2x00_load_ram_ext(ha, ha->request_dma,
3677 risc_addr, dlen);
3678 if (rval) {
3679 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3680 "segment %d of firmware\n", ha->host_no,
3681 fragment));
3682 qla_printk(KERN_WARNING, ha,
3683 "[ERROR] Failed to load segment %d of "
3684 "firmware\n", fragment);
3685 break;
3686 }
3687
3688 fwcode += dlen;
3689 risc_addr += dlen;
3690 risc_size -= dlen;
3691 fragment++;
3692 }
3693
3694 /* Next segment. */
3695 segments--;
3696 }
3697
3698 release_firmware(fw_entry);
3699 return rval;
3700
3701fail_fw_integrity:
3702
3703 release_firmware(fw_entry);
3704 return QLA_FUNCTION_FAILED;
3705
3706}
This page took 0.217009 seconds and 5 git commands to generate.