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