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