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