ql4_nx.c 98.4 KB
Newer Older
1 2
/*
 * QLogic iSCSI HBA Driver
3
 * Copyright (c)  2003-2012 QLogic Corporation
4 5 6 7
 *
 * See LICENSE.qla4xxx for copyright and licensing details.
 */
#include <linux/delay.h>
J
Jiri Slaby 已提交
8
#include <linux/io.h>
9
#include <linux/pci.h>
10
#include <linux/ratelimit.h>
11 12
#include "ql4_def.h"
#include "ql4_glbl.h"
13
#include "ql4_inline.h"
14

15 16
#include <asm-generic/io-64-nonatomic-lo-hi.h>

17 18 19 20 21 22 23 24 25 26 27 28 29 30
#define MASK(n)		DMA_BIT_MASK(n)
#define MN_WIN(addr)	(((addr & 0x1fc0000) >> 1) | ((addr >> 25) & 0x3ff))
#define OCM_WIN(addr)	(((addr & 0x1ff0000) >> 1) | ((addr >> 25) & 0x3ff))
#define MS_WIN(addr)	(addr & 0x0ffc0000)
#define QLA82XX_PCI_MN_2M	(0)
#define QLA82XX_PCI_MS_2M	(0x80000)
#define QLA82XX_PCI_OCM0_2M	(0xc0000)
#define VALID_OCM_ADDR(addr)	(((addr) & 0x3f800) != 0x3f800)
#define GET_MEM_OFFS_2M(addr)	(addr & MASK(18))

/* CRB window related */
#define CRB_BLK(off)	((off >> 20) & 0x3f)
#define CRB_SUBBLK(off)	((off >> 16) & 0xf)
#define CRB_WINDOW_2M	(0x130060)
31
#define CRB_HI(off)	((qla4_82xx_crb_hub_agt[CRB_BLK(off)] << 20) | \
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
			((off) & 0xf0000))
#define QLA82XX_PCI_CAMQM_2M_END	(0x04800800UL)
#define QLA82XX_PCI_CAMQM_2M_BASE	(0x000ff800UL)
#define CRB_INDIRECT_2M			(0x1e0000UL)

static inline void __iomem *
qla4_8xxx_pci_base_offsetfset(struct scsi_qla_host *ha, unsigned long off)
{
	if ((off < ha->first_page_group_end) &&
	    (off >= ha->first_page_group_start))
		return (void __iomem *)(ha->nx_pcibase + off);

	return NULL;
}

#define MAX_CRB_XFORM 60
static unsigned long crb_addr_xform[MAX_CRB_XFORM];
static int qla4_8xxx_crb_table_initialized;

#define qla4_8xxx_crb_addr_transform(name) \
	(crb_addr_xform[QLA82XX_HW_PX_MAP_CRB_##name] = \
	 QLA82XX_HW_CRB_HUB_AGT_ADR_##name << 20)
static void
55
qla4_82xx_crb_addr_transform_setup(void)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
{
	qla4_8xxx_crb_addr_transform(XDMA);
	qla4_8xxx_crb_addr_transform(TIMR);
	qla4_8xxx_crb_addr_transform(SRE);
	qla4_8xxx_crb_addr_transform(SQN3);
	qla4_8xxx_crb_addr_transform(SQN2);
	qla4_8xxx_crb_addr_transform(SQN1);
	qla4_8xxx_crb_addr_transform(SQN0);
	qla4_8xxx_crb_addr_transform(SQS3);
	qla4_8xxx_crb_addr_transform(SQS2);
	qla4_8xxx_crb_addr_transform(SQS1);
	qla4_8xxx_crb_addr_transform(SQS0);
	qla4_8xxx_crb_addr_transform(RPMX7);
	qla4_8xxx_crb_addr_transform(RPMX6);
	qla4_8xxx_crb_addr_transform(RPMX5);
	qla4_8xxx_crb_addr_transform(RPMX4);
	qla4_8xxx_crb_addr_transform(RPMX3);
	qla4_8xxx_crb_addr_transform(RPMX2);
	qla4_8xxx_crb_addr_transform(RPMX1);
	qla4_8xxx_crb_addr_transform(RPMX0);
	qla4_8xxx_crb_addr_transform(ROMUSB);
	qla4_8xxx_crb_addr_transform(SN);
	qla4_8xxx_crb_addr_transform(QMN);
	qla4_8xxx_crb_addr_transform(QMS);
	qla4_8xxx_crb_addr_transform(PGNI);
	qla4_8xxx_crb_addr_transform(PGND);
	qla4_8xxx_crb_addr_transform(PGN3);
	qla4_8xxx_crb_addr_transform(PGN2);
	qla4_8xxx_crb_addr_transform(PGN1);
	qla4_8xxx_crb_addr_transform(PGN0);
	qla4_8xxx_crb_addr_transform(PGSI);
	qla4_8xxx_crb_addr_transform(PGSD);
	qla4_8xxx_crb_addr_transform(PGS3);
	qla4_8xxx_crb_addr_transform(PGS2);
	qla4_8xxx_crb_addr_transform(PGS1);
	qla4_8xxx_crb_addr_transform(PGS0);
	qla4_8xxx_crb_addr_transform(PS);
	qla4_8xxx_crb_addr_transform(PH);
	qla4_8xxx_crb_addr_transform(NIU);
	qla4_8xxx_crb_addr_transform(I2Q);
	qla4_8xxx_crb_addr_transform(EG);
	qla4_8xxx_crb_addr_transform(MN);
	qla4_8xxx_crb_addr_transform(MS);
	qla4_8xxx_crb_addr_transform(CAS2);
	qla4_8xxx_crb_addr_transform(CAS1);
	qla4_8xxx_crb_addr_transform(CAS0);
	qla4_8xxx_crb_addr_transform(CAM);
	qla4_8xxx_crb_addr_transform(C2C1);
	qla4_8xxx_crb_addr_transform(C2C0);
	qla4_8xxx_crb_addr_transform(SMB);
	qla4_8xxx_crb_addr_transform(OCM0);
	qla4_8xxx_crb_addr_transform(I2C0);

	qla4_8xxx_crb_table_initialized = 1;
}

static struct crb_128M_2M_block_map crb_128M_2M_map[64] = {
	{{{0, 0,         0,         0} } },		/* 0: PCI */
	{{{1, 0x0100000, 0x0102000, 0x120000},	/* 1: PCIE */
		{1, 0x0110000, 0x0120000, 0x130000},
		{1, 0x0120000, 0x0122000, 0x124000},
		{1, 0x0130000, 0x0132000, 0x126000},
		{1, 0x0140000, 0x0142000, 0x128000},
		{1, 0x0150000, 0x0152000, 0x12a000},
		{1, 0x0160000, 0x0170000, 0x110000},
		{1, 0x0170000, 0x0172000, 0x12e000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{1, 0x01e0000, 0x01e0800, 0x122000},
		{0, 0x0000000, 0x0000000, 0x000000} } },
	{{{1, 0x0200000, 0x0210000, 0x180000} } },/* 2: MN */
	{{{0, 0,         0,         0} } },	    /* 3: */
	{{{1, 0x0400000, 0x0401000, 0x169000} } },/* 4: P2NR1 */
	{{{1, 0x0500000, 0x0510000, 0x140000} } },/* 5: SRE   */
	{{{1, 0x0600000, 0x0610000, 0x1c0000} } },/* 6: NIU   */
	{{{1, 0x0700000, 0x0704000, 0x1b8000} } },/* 7: QM    */
	{{{1, 0x0800000, 0x0802000, 0x170000},  /* 8: SQM0  */
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{1, 0x08f0000, 0x08f2000, 0x172000} } },
	{{{1, 0x0900000, 0x0902000, 0x174000},	/* 9: SQM1*/
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{1, 0x09f0000, 0x09f2000, 0x176000} } },
	{{{0, 0x0a00000, 0x0a02000, 0x178000},	/* 10: SQM2*/
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{1, 0x0af0000, 0x0af2000, 0x17a000} } },
	{{{0, 0x0b00000, 0x0b02000, 0x17c000},	/* 11: SQM3*/
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{1, 0x0bf0000, 0x0bf2000, 0x17e000} } },
	{{{1, 0x0c00000, 0x0c04000, 0x1d4000} } },/* 12: I2Q */
	{{{1, 0x0d00000, 0x0d04000, 0x1a4000} } },/* 13: TMR */
	{{{1, 0x0e00000, 0x0e04000, 0x1a0000} } },/* 14: ROMUSB */
	{{{1, 0x0f00000, 0x0f01000, 0x164000} } },/* 15: PEG4 */
	{{{0, 0x1000000, 0x1004000, 0x1a8000} } },/* 16: XDMA */
	{{{1, 0x1100000, 0x1101000, 0x160000} } },/* 17: PEG0 */
	{{{1, 0x1200000, 0x1201000, 0x161000} } },/* 18: PEG1 */
	{{{1, 0x1300000, 0x1301000, 0x162000} } },/* 19: PEG2 */
	{{{1, 0x1400000, 0x1401000, 0x163000} } },/* 20: PEG3 */
	{{{1, 0x1500000, 0x1501000, 0x165000} } },/* 21: P2ND */
	{{{1, 0x1600000, 0x1601000, 0x166000} } },/* 22: P2NI */
	{{{0, 0,         0,         0} } },	/* 23: */
	{{{0, 0,         0,         0} } },	/* 24: */
	{{{0, 0,         0,         0} } },	/* 25: */
	{{{0, 0,         0,         0} } },	/* 26: */
	{{{0, 0,         0,         0} } },	/* 27: */
	{{{0, 0,         0,         0} } },	/* 28: */
	{{{1, 0x1d00000, 0x1d10000, 0x190000} } },/* 29: MS */
	{{{1, 0x1e00000, 0x1e01000, 0x16a000} } },/* 30: P2NR2 */
	{{{1, 0x1f00000, 0x1f10000, 0x150000} } },/* 31: EPG */
	{{{0} } },				/* 32: PCI */
	{{{1, 0x2100000, 0x2102000, 0x120000},	/* 33: PCIE */
		{1, 0x2110000, 0x2120000, 0x130000},
		{1, 0x2120000, 0x2122000, 0x124000},
		{1, 0x2130000, 0x2132000, 0x126000},
		{1, 0x2140000, 0x2142000, 0x128000},
		{1, 0x2150000, 0x2152000, 0x12a000},
		{1, 0x2160000, 0x2170000, 0x110000},
		{1, 0x2170000, 0x2172000, 0x12e000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000},
		{0, 0x0000000, 0x0000000, 0x000000} } },
	{{{1, 0x2200000, 0x2204000, 0x1b0000} } },/* 34: CAM */
	{{{0} } },				/* 35: */
	{{{0} } },				/* 36: */
	{{{0} } },				/* 37: */
	{{{0} } },				/* 38: */
	{{{0} } },				/* 39: */
	{{{1, 0x2800000, 0x2804000, 0x1a4000} } },/* 40: TMR */
	{{{1, 0x2900000, 0x2901000, 0x16b000} } },/* 41: P2NR3 */
	{{{1, 0x2a00000, 0x2a00400, 0x1ac400} } },/* 42: RPMX1 */
	{{{1, 0x2b00000, 0x2b00400, 0x1ac800} } },/* 43: RPMX2 */
	{{{1, 0x2c00000, 0x2c00400, 0x1acc00} } },/* 44: RPMX3 */
	{{{1, 0x2d00000, 0x2d00400, 0x1ad000} } },/* 45: RPMX4 */
	{{{1, 0x2e00000, 0x2e00400, 0x1ad400} } },/* 46: RPMX5 */
	{{{1, 0x2f00000, 0x2f00400, 0x1ad800} } },/* 47: RPMX6 */
	{{{1, 0x3000000, 0x3000400, 0x1adc00} } },/* 48: RPMX7 */
	{{{0, 0x3100000, 0x3104000, 0x1a8000} } },/* 49: XDMA */
	{{{1, 0x3200000, 0x3204000, 0x1d4000} } },/* 50: I2Q */
	{{{1, 0x3300000, 0x3304000, 0x1a0000} } },/* 51: ROMUSB */
	{{{0} } },				/* 52: */
	{{{1, 0x3500000, 0x3500400, 0x1ac000} } },/* 53: RPMX0 */
	{{{1, 0x3600000, 0x3600400, 0x1ae000} } },/* 54: RPMX8 */
	{{{1, 0x3700000, 0x3700400, 0x1ae400} } },/* 55: RPMX9 */
	{{{1, 0x3800000, 0x3804000, 0x1d0000} } },/* 56: OCM0 */
	{{{1, 0x3900000, 0x3904000, 0x1b4000} } },/* 57: CRYPTO */
	{{{1, 0x3a00000, 0x3a04000, 0x1d8000} } },/* 58: SMB */
	{{{0} } },				/* 59: I2C0 */
	{{{0} } },				/* 60: I2C1 */
	{{{1, 0x3d00000, 0x3d04000, 0x1dc000} } },/* 61: LPC */
	{{{1, 0x3e00000, 0x3e01000, 0x167000} } },/* 62: P2NC */
	{{{1, 0x3f00000, 0x3f01000, 0x168000} } }	/* 63: P2NR0 */
};

/*
 * top 12 bits of crb internal address (hub, agent)
 */
272
static unsigned qla4_82xx_crb_hub_agt[64] = {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
	QLA82XX_HW_CRB_HUB_AGT_ADR_MN,
	QLA82XX_HW_CRB_HUB_AGT_ADR_MS,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_SRE,
	QLA82XX_HW_CRB_HUB_AGT_ADR_NIU,
	QLA82XX_HW_CRB_HUB_AGT_ADR_QMN,
	QLA82XX_HW_CRB_HUB_AGT_ADR_SQN0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_SQN1,
	QLA82XX_HW_CRB_HUB_AGT_ADR_SQN2,
	QLA82XX_HW_CRB_HUB_AGT_ADR_SQN3,
	QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
	QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
	QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGN4,
	QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGN0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGN1,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGN2,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGN3,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGND,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGNI,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGS0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGS1,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGS2,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGS3,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGSI,
	QLA82XX_HW_CRB_HUB_AGT_ADR_SN,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_EG,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
	QLA82XX_HW_CRB_HUB_AGT_ADR_CAM,
	0,
	0,
	0,
	0,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX1,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX2,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX3,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX4,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX5,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX6,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX7,
	QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
	QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
	QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX8,
	QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX9,
	QLA82XX_HW_CRB_HUB_AGT_ADR_OCM0,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_SMB,
	QLA82XX_HW_CRB_HUB_AGT_ADR_I2C0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_I2C1,
	0,
	QLA82XX_HW_CRB_HUB_AGT_ADR_PGNC,
	0,
};

/* Device states */
static char *qdev_state[] = {
	"Unknown",
	"Cold",
	"Initializing",
	"Ready",
	"Need Reset",
	"Need Quiescent",
	"Failed",
	"Quiescent",
};

/*
 * In: 'off' is offset from CRB space in 128M pci map
 * Out: 'off' is 2M pci map addr
 * side effect: lock crb window
 */
static void
357
qla4_82xx_pci_set_crbwindow_2M(struct scsi_qla_host *ha, ulong *off)
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
{
	u32 win_read;

	ha->crb_win = CRB_HI(*off);
	writel(ha->crb_win,
		(void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));

	/* Read back value to make sure write has gone through before trying
	* to use it. */
	win_read = readl((void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
	if (win_read != ha->crb_win) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
		    "%s: Written crbwin (0x%x) != Read crbwin (0x%x),"
		    " off=0x%lx\n", __func__, ha->crb_win, win_read, *off));
	}
	*off = (*off & MASK(16)) + CRB_INDIRECT_2M + ha->nx_pcibase;
}

void
377
qla4_82xx_wr_32(struct scsi_qla_host *ha, ulong off, u32 data)
378 379 380 381
{
	unsigned long flags = 0;
	int rv;

382
	rv = qla4_82xx_pci_get_crb_addr_2M(ha, &off);
383 384 385 386 387

	BUG_ON(rv == -1);

	if (rv == 1) {
		write_lock_irqsave(&ha->hw_lock, flags);
388 389
		qla4_82xx_crb_win_lock(ha);
		qla4_82xx_pci_set_crbwindow_2M(ha, &off);
390 391 392 393 394
	}

	writel(data, (void __iomem *)off);

	if (rv == 1) {
395
		qla4_82xx_crb_win_unlock(ha);
396 397 398 399
		write_unlock_irqrestore(&ha->hw_lock, flags);
	}
}

400
uint32_t qla4_82xx_rd_32(struct scsi_qla_host *ha, ulong off)
401 402 403 404 405
{
	unsigned long flags = 0;
	int rv;
	u32 data;

406
	rv = qla4_82xx_pci_get_crb_addr_2M(ha, &off);
407 408 409 410 411

	BUG_ON(rv == -1);

	if (rv == 1) {
		write_lock_irqsave(&ha->hw_lock, flags);
412 413
		qla4_82xx_crb_win_lock(ha);
		qla4_82xx_pci_set_crbwindow_2M(ha, &off);
414 415 416 417
	}
	data = readl((void __iomem *)off);

	if (rv == 1) {
418
		qla4_82xx_crb_win_unlock(ha);
419 420 421 422 423
		write_unlock_irqrestore(&ha->hw_lock, flags);
	}
	return data;
}

424
/* Minidump related functions */
425
int qla4_82xx_md_rd_32(struct scsi_qla_host *ha, uint32_t off, uint32_t *data)
426
{
427 428
	uint32_t win_read, off_value;
	int rval = QLA_SUCCESS;
429 430 431 432

	off_value  = off & 0xFFFF0000;
	writel(off_value, (void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));

433 434
	/*
	 * Read back value to make sure write has gone through before trying
435 436 437 438 439 440
	 * to use it.
	 */
	win_read = readl((void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
	if (win_read != off_value) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
				  "%s: Written (0x%x) != Read (0x%x), off=0x%x\n",
441 442 443 444 445 446
				  __func__, off_value, win_read, off));
		rval = QLA_ERROR;
	} else {
		off_value  = off & 0x0000FFFF;
		*data = readl((void __iomem *)(off_value + CRB_INDIRECT_2M +
					       ha->nx_pcibase));
447
	}
448 449
	return rval;
}
450

451 452 453 454
int qla4_82xx_md_wr_32(struct scsi_qla_host *ha, uint32_t off, uint32_t data)
{
	uint32_t win_read, off_value;
	int rval = QLA_SUCCESS;
455

456 457
	off_value  = off & 0xFFFF0000;
	writel(off_value, (void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
458

459 460 461 462 463 464 465 466 467 468 469
	/* Read back value to make sure write has gone through before trying
	 * to use it.
	 */
	win_read = readl((void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
	if (win_read != off_value) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
				  "%s: Written (0x%x) != Read (0x%x), off=0x%x\n",
				  __func__, off_value, win_read, off));
		rval = QLA_ERROR;
	} else {
		off_value  = off & 0x0000FFFF;
470 471
		writel(data, (void __iomem *)(off_value + CRB_INDIRECT_2M +
					      ha->nx_pcibase));
472
	}
473 474 475
	return rval;
}

476 477
#define CRB_WIN_LOCK_TIMEOUT 100000000

478
int qla4_82xx_crb_win_lock(struct scsi_qla_host *ha)
479 480 481 482 483 484
{
	int i;
	int done = 0, timeout = 0;

	while (!done) {
		/* acquire semaphore3 from PCI HW block */
485
		done = qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_LOCK));
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
		if (done == 1)
			break;
		if (timeout >= CRB_WIN_LOCK_TIMEOUT)
			return -1;

		timeout++;

		/* Yield CPU */
		if (!in_interrupt())
			schedule();
		else {
			for (i = 0; i < 20; i++)
				cpu_relax();    /*This a nop instr on i386*/
		}
	}
501
	qla4_82xx_wr_32(ha, QLA82XX_CRB_WIN_LOCK_ID, ha->func_num);
502 503 504
	return 0;
}

505
void qla4_82xx_crb_win_unlock(struct scsi_qla_host *ha)
506
{
507
	qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
508 509 510 511 512
}

#define IDC_LOCK_TIMEOUT 100000000

/**
513
 * qla4_82xx_idc_lock - hw_lock
514 515 516 517 518
 * @ha: pointer to adapter structure
 *
 * General purpose lock used to synchronize access to
 * CRB_DEV_STATE, CRB_DEV_REF_COUNT, etc.
 **/
519
int qla4_82xx_idc_lock(struct scsi_qla_host *ha)
520 521 522 523 524 525
{
	int i;
	int done = 0, timeout = 0;

	while (!done) {
		/* acquire semaphore5 from PCI HW block */
526
		done = qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_LOCK));
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
		if (done == 1)
			break;
		if (timeout >= IDC_LOCK_TIMEOUT)
			return -1;

		timeout++;

		/* Yield CPU */
		if (!in_interrupt())
			schedule();
		else {
			for (i = 0; i < 20; i++)
				cpu_relax();    /*This a nop instr on i386*/
		}
	}
	return 0;
}

545
void qla4_82xx_idc_unlock(struct scsi_qla_host *ha)
546
{
547
	qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_UNLOCK));
548 549 550
}

int
551
qla4_82xx_pci_get_crb_addr_2M(struct scsi_qla_host *ha, ulong *off)
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
{
	struct crb_128M_2M_sub_block_map *m;

	if (*off >= QLA82XX_CRB_MAX)
		return -1;

	if (*off >= QLA82XX_PCI_CAMQM && (*off < QLA82XX_PCI_CAMQM_2M_END)) {
		*off = (*off - QLA82XX_PCI_CAMQM) +
		    QLA82XX_PCI_CAMQM_2M_BASE + ha->nx_pcibase;
		return 0;
	}

	if (*off < QLA82XX_PCI_CRBSPACE)
		return -1;

	*off -= QLA82XX_PCI_CRBSPACE;
	/*
	 * Try direct map
	 */

	m = &crb_128M_2M_map[CRB_BLK(*off)].sub_block[CRB_SUBBLK(*off)];

	if (m->valid && (m->start_128M <= *off) && (m->end_128M > *off)) {
		*off = *off + m->start_2M - m->start_128M + ha->nx_pcibase;
		return 0;
	}

	/*
	 * Not in direct map, use crb window
	 */
	return 1;
}

/*
* check memory access boundary.
* used by test agent. support ddr access only for now
*/
static unsigned long
590
qla4_82xx_pci_mem_bound_check(struct scsi_qla_host *ha,
591 592
		unsigned long long addr, int size)
{
593 594 595 596
	if (!QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_DDR_NET,
	    QLA8XXX_ADDR_DDR_NET_MAX) ||
	    !QLA8XXX_ADDR_IN_RANGE(addr + size - 1,
	    QLA8XXX_ADDR_DDR_NET, QLA8XXX_ADDR_DDR_NET_MAX) ||
597 598 599 600 601 602
	    ((size != 1) && (size != 2) && (size != 4) && (size != 8))) {
		return 0;
	}
	return 1;
}

603
static int qla4_82xx_pci_set_window_warning_count;
604 605

static unsigned long
606
qla4_82xx_pci_set_window(struct scsi_qla_host *ha, unsigned long long addr)
607 608 609 610
{
	int window;
	u32 win_read;

611 612
	if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_DDR_NET,
	    QLA8XXX_ADDR_DDR_NET_MAX)) {
613 614 615
		/* DDR network side */
		window = MN_WIN(addr);
		ha->ddr_mn_window = window;
616
		qla4_82xx_wr_32(ha, ha->mn_win_crb |
617
		    QLA82XX_PCI_CRBSPACE, window);
618
		win_read = qla4_82xx_rd_32(ha, ha->mn_win_crb |
619 620 621 622 623 624 625
		    QLA82XX_PCI_CRBSPACE);
		if ((win_read << 17) != window) {
			ql4_printk(KERN_WARNING, ha,
			"%s: Written MNwin (0x%x) != Read MNwin (0x%x)\n",
			__func__, window, win_read);
		}
		addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_DDR_NET;
626 627
	} else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_OCM0,
				QLA8XXX_ADDR_OCM0_MAX)) {
628 629 630 631 632 633 634 635 636
		unsigned int temp1;
		/* if bits 19:18&17:11 are on */
		if ((addr & 0x00ff800) == 0xff800) {
			printk("%s: QM access not handled.\n", __func__);
			addr = -1UL;
		}

		window = OCM_WIN(addr);
		ha->ddr_mn_window = window;
637
		qla4_82xx_wr_32(ha, ha->mn_win_crb |
638
		    QLA82XX_PCI_CRBSPACE, window);
639
		win_read = qla4_82xx_rd_32(ha, ha->mn_win_crb |
640 641 642 643 644 645 646 647 648
		    QLA82XX_PCI_CRBSPACE);
		temp1 = ((window & 0x1FF) << 7) |
		    ((window & 0x0FFFE0000) >> 17);
		if (win_read != temp1) {
			printk("%s: Written OCMwin (0x%x) != Read"
			    " OCMwin (0x%x)\n", __func__, temp1, win_read);
		}
		addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_OCM0_2M;

649
	} else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_QDR_NET,
650 651 652 653
				QLA82XX_P3_ADDR_QDR_NET_MAX)) {
		/* QDR network side */
		window = MS_WIN(addr);
		ha->qdr_sn_window = window;
654
		qla4_82xx_wr_32(ha, ha->ms_win_crb |
655
		    QLA82XX_PCI_CRBSPACE, window);
656
		win_read = qla4_82xx_rd_32(ha,
657 658 659 660 661 662 663 664 665 666 667 668
		     ha->ms_win_crb | QLA82XX_PCI_CRBSPACE);
		if (win_read != window) {
			printk("%s: Written MSwin (0x%x) != Read "
			    "MSwin (0x%x)\n", __func__, window, win_read);
		}
		addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_QDR_NET;

	} else {
		/*
		 * peg gdb frequently accesses memory that doesn't exist,
		 * this limits the chit chat so debugging isn't slowed down.
		 */
669 670
		if ((qla4_82xx_pci_set_window_warning_count++ < 8) ||
		    (qla4_82xx_pci_set_window_warning_count%64 == 0)) {
671 672 673 674 675 676 677 678 679
			printk("%s: Warning:%s Unknown address range!\n",
			    __func__, DRIVER_NAME);
		}
		addr = -1UL;
	}
	return addr;
}

/* check if address is in the same windows as the previous access */
680
static int qla4_82xx_pci_is_same_window(struct scsi_qla_host *ha,
681 682 683 684 685 686 687
		unsigned long long addr)
{
	int window;
	unsigned long long qdr_max;

	qdr_max = QLA82XX_P3_ADDR_QDR_NET_MAX;

688 689
	if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_DDR_NET,
	    QLA8XXX_ADDR_DDR_NET_MAX)) {
690 691
		/* DDR network side */
		BUG();	/* MN access can not come here */
692 693
	} else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_OCM0,
	     QLA8XXX_ADDR_OCM0_MAX)) {
694
		return 1;
695 696
	} else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_OCM1,
	     QLA8XXX_ADDR_OCM1_MAX)) {
697
		return 1;
698
	} else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_QDR_NET,
699 700
	    qdr_max)) {
		/* QDR network side */
701
		window = ((addr - QLA8XXX_ADDR_QDR_NET) >> 22) & 0x3f;
702 703 704 705 706 707 708
		if (ha->qdr_sn_window == window)
			return 1;
	}

	return 0;
}

709
static int qla4_82xx_pci_mem_read_direct(struct scsi_qla_host *ha,
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
		u64 off, void *data, int size)
{
	unsigned long flags;
	void __iomem *addr;
	int ret = 0;
	u64 start;
	void __iomem *mem_ptr = NULL;
	unsigned long mem_base;
	unsigned long mem_page;

	write_lock_irqsave(&ha->hw_lock, flags);

	/*
	 * If attempting to access unknown address or straddle hw windows,
	 * do not access.
	 */
726
	start = qla4_82xx_pci_set_window(ha, off);
727
	if ((start == -1UL) ||
728
	    (qla4_82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
		write_unlock_irqrestore(&ha->hw_lock, flags);
		printk(KERN_ERR"%s out of bound pci memory access. "
				"offset is 0x%llx\n", DRIVER_NAME, off);
		return -1;
	}

	addr = qla4_8xxx_pci_base_offsetfset(ha, start);
	if (!addr) {
		write_unlock_irqrestore(&ha->hw_lock, flags);
		mem_base = pci_resource_start(ha->pdev, 0);
		mem_page = start & PAGE_MASK;
		/* Map two pages whenever user tries to access addresses in two
		   consecutive pages.
		 */
		if (mem_page != ((start + size - 1) & PAGE_MASK))
			mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE * 2);
		else
			mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);

		if (mem_ptr == NULL) {
			*(u8 *)data = 0;
			return -1;
		}
		addr = mem_ptr;
		addr += start & (PAGE_SIZE - 1);
		write_lock_irqsave(&ha->hw_lock, flags);
	}

	switch (size) {
	case 1:
		*(u8  *)data = readb(addr);
		break;
	case 2:
		*(u16 *)data = readw(addr);
		break;
	case 4:
		*(u32 *)data = readl(addr);
		break;
	case 8:
		*(u64 *)data = readq(addr);
		break;
	default:
		ret = -1;
		break;
	}
	write_unlock_irqrestore(&ha->hw_lock, flags);

	if (mem_ptr)
		iounmap(mem_ptr);
	return ret;
}

static int
782
qla4_82xx_pci_mem_write_direct(struct scsi_qla_host *ha, u64 off,
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		void *data, int size)
{
	unsigned long flags;
	void __iomem *addr;
	int ret = 0;
	u64 start;
	void __iomem *mem_ptr = NULL;
	unsigned long mem_base;
	unsigned long mem_page;

	write_lock_irqsave(&ha->hw_lock, flags);

	/*
	 * If attempting to access unknown address or straddle hw windows,
	 * do not access.
	 */
799
	start = qla4_82xx_pci_set_window(ha, off);
800
	if ((start == -1UL) ||
801
	    (qla4_82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
		write_unlock_irqrestore(&ha->hw_lock, flags);
		printk(KERN_ERR"%s out of bound pci memory access. "
				"offset is 0x%llx\n", DRIVER_NAME, off);
		return -1;
	}

	addr = qla4_8xxx_pci_base_offsetfset(ha, start);
	if (!addr) {
		write_unlock_irqrestore(&ha->hw_lock, flags);
		mem_base = pci_resource_start(ha->pdev, 0);
		mem_page = start & PAGE_MASK;
		/* Map two pages whenever user tries to access addresses in two
		   consecutive pages.
		 */
		if (mem_page != ((start + size - 1) & PAGE_MASK))
			mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE*2);
		else
			mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
		if (mem_ptr == NULL)
			return -1;

		addr = mem_ptr;
		addr += start & (PAGE_SIZE - 1);
		write_lock_irqsave(&ha->hw_lock, flags);
	}

	switch (size) {
	case 1:
		writeb(*(u8 *)data, addr);
		break;
	case 2:
		writew(*(u16 *)data, addr);
		break;
	case 4:
		writel(*(u32 *)data, addr);
		break;
	case 8:
		writeq(*(u64 *)data, addr);
		break;
	default:
		ret = -1;
		break;
	}
	write_unlock_irqrestore(&ha->hw_lock, flags);
	if (mem_ptr)
		iounmap(mem_ptr);
	return ret;
}

#define MTU_FUDGE_FACTOR 100

static unsigned long
854
qla4_82xx_decode_crb_addr(unsigned long addr)
855 856 857 858 859
{
	int i;
	unsigned long base_addr, offset, pci_base;

	if (!qla4_8xxx_crb_table_initialized)
860
		qla4_82xx_crb_addr_transform_setup();
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

	pci_base = ADDR_ERROR;
	base_addr = addr & 0xfff00000;
	offset = addr & 0x000fffff;

	for (i = 0; i < MAX_CRB_XFORM; i++) {
		if (crb_addr_xform[i] == base_addr) {
			pci_base = i << 20;
			break;
		}
	}
	if (pci_base == ADDR_ERROR)
		return pci_base;
	else
		return pci_base + offset;
}

static long rom_max_timeout = 100;
879
static long qla4_82xx_rom_lock_timeout = 100;
880 881

static int
882
qla4_82xx_rom_lock(struct scsi_qla_host *ha)
883 884 885 886 887 888 889
{
	int i;
	int done = 0, timeout = 0;

	while (!done) {
		/* acquire semaphore2 from PCI HW block */

890
		done = qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_LOCK));
891 892
		if (done == 1)
			break;
893
		if (timeout >= qla4_82xx_rom_lock_timeout)
894 895 896 897 898 899 900 901 902 903 904 905
			return -1;

		timeout++;

		/* Yield CPU */
		if (!in_interrupt())
			schedule();
		else {
			for (i = 0; i < 20; i++)
				cpu_relax();    /*This a nop instr on i386*/
		}
	}
906
	qla4_82xx_wr_32(ha, QLA82XX_ROM_LOCK_ID, ROM_LOCK_DRIVER);
907 908 909 910
	return 0;
}

static void
911
qla4_82xx_rom_unlock(struct scsi_qla_host *ha)
912
{
913
	qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_UNLOCK));
914 915 916
}

static int
917
qla4_82xx_wait_rom_done(struct scsi_qla_host *ha)
918 919 920 921 922
{
	long timeout = 0;
	long done = 0 ;

	while (done == 0) {
923
		done = qla4_82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
924 925 926 927 928 929 930 931 932 933 934 935
		done &= 2;
		timeout++;
		if (timeout >= rom_max_timeout) {
			printk("%s: Timeout reached  waiting for rom done",
					DRIVER_NAME);
			return -1;
		}
	}
	return 0;
}

static int
936
qla4_82xx_do_rom_fast_read(struct scsi_qla_host *ha, int addr, int *valp)
937
{
938 939 940 941 942
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0xb);
	if (qla4_82xx_wait_rom_done(ha)) {
943 944 945 946
		printk("%s: Error waiting for rom done\n", DRIVER_NAME);
		return -1;
	}
	/* reset abyte_cnt and dummy_byte_cnt */
947
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
948
	udelay(10);
949
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
950

951
	*valp = qla4_82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
952 953 954 955
	return 0;
}

static int
956
qla4_82xx_rom_fast_read(struct scsi_qla_host *ha, int addr, int *valp)
957 958 959
{
	int ret, loops = 0;

960
	while ((qla4_82xx_rom_lock(ha) != 0) && (loops < 50000)) {
961 962 963 964
		udelay(100);
		loops++;
	}
	if (loops >= 50000) {
965 966
		ql4_printk(KERN_WARNING, ha, "%s: qla4_82xx_rom_lock failed\n",
			   DRIVER_NAME);
967 968
		return -1;
	}
969 970
	ret = qla4_82xx_do_rom_fast_read(ha, addr, valp);
	qla4_82xx_rom_unlock(ha);
971 972 973 974 975 976 977 978
	return ret;
}

/**
 * This routine does CRB initialize sequence
 * to put the ISP into operational state
 **/
static int
979
qla4_82xx_pinit_from_rom(struct scsi_qla_host *ha, int verbose)
980 981 982 983 984 985 986 987 988 989 990 991 992
{
	int addr, val;
	int i ;
	struct crb_addr_pair *buf;
	unsigned long off;
	unsigned offset, n;

	struct crb_addr_pair {
		long addr;
		long data;
	};

	/* Halt all the indiviual PEGs and other blocks of the ISP */
993
	qla4_82xx_rom_lock(ha);
994

995
	/* disable all I2Q */
996 997 998 999 1000 1001
	qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x10, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x14, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x18, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x1c, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x20, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x24, 0x0);
1002 1003

	/* disable all niu interrupts */
1004
	qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x40, 0xff);
1005
	/* disable xge rx/tx */
1006
	qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x70000, 0x00);
1007
	/* disable xg1 rx/tx */
1008
	qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x80000, 0x00);
1009
	/* disable sideband mac */
1010
	qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x90000, 0x00);
1011
	/* disable ap0 mac */
1012
	qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xa0000, 0x00);
1013
	/* disable ap1 mac */
1014
	qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xb0000, 0x00);
1015 1016

	/* halt sre */
1017 1018
	val = qla4_82xx_rd_32(ha, QLA82XX_CRB_SRE + 0x1000);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_SRE + 0x1000, val & (~(0x1)));
1019 1020

	/* halt epg */
1021
	qla4_82xx_wr_32(ha, QLA82XX_CRB_EPG + 0x1300, 0x1);
1022 1023

	/* halt timers */
1024 1025 1026 1027 1028 1029
	qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x0, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x8, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x10, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x18, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x100, 0x0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x200, 0x0);
1030 1031

	/* halt pegs */
1032 1033 1034 1035 1036
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x3c, 1);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1 + 0x3c, 1);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2 + 0x3c, 1);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3 + 0x3c, 1);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_4 + 0x3c, 1);
1037
	msleep(5);
1038 1039

	/* big hammer */
1040 1041
	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
		/* don't reset CAM block on reset */
1042
		qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xfeffffff);
1043
	else
1044
		qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xffffffff);
1045

1046
	qla4_82xx_rom_unlock(ha);
1047 1048 1049 1050 1051 1052

	/* Read the signature value from the flash.
	 * Offset 0: Contain signature (0xcafecafe)
	 * Offset 4: Offset and number of addr/value pairs
	 * that present in CRB initialize sequence
	 */
1053 1054
	if (qla4_82xx_rom_fast_read(ha, 0, &n) != 0 || n != 0xcafecafeUL ||
	    qla4_82xx_rom_fast_read(ha, 4, &n) != 0) {
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
		ql4_printk(KERN_WARNING, ha,
			"[ERROR] Reading crb_init area: n: %08x\n", n);
		return -1;
	}

	/* Offset in flash = lower 16 bits
	 * Number of enteries = upper 16 bits
	 */
	offset = n & 0xffffU;
	n = (n >> 16) & 0xffffU;

	/* number of addr/value pair should not exceed 1024 enteries */
	if (n  >= 1024) {
		ql4_printk(KERN_WARNING, ha,
		    "%s: %s:n=0x%x [ERROR] Card flash not initialized.\n",
		    DRIVER_NAME, __func__, n);
		return -1;
	}

	ql4_printk(KERN_INFO, ha,
		"%s: %d CRB init values found in ROM.\n", DRIVER_NAME, n);

	buf = kmalloc(n * sizeof(struct crb_addr_pair), GFP_KERNEL);
	if (buf == NULL) {
		ql4_printk(KERN_WARNING, ha,
		    "%s: [ERROR] Unable to malloc memory.\n", DRIVER_NAME);
		return -1;
	}

	for (i = 0; i < n; i++) {
1085 1086
		if (qla4_82xx_rom_fast_read(ha, 8*i + 4*offset, &val) != 0 ||
		    qla4_82xx_rom_fast_read(ha, 8*i + 4*offset + 4, &addr) !=
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
		    0) {
			kfree(buf);
			return -1;
		}

		buf[i].addr = addr;
		buf[i].data = val;
	}

	for (i = 0; i < n; i++) {
		/* Translate internal CRB initialization
		 * address to PCI bus address
		 */
1100
		off = qla4_82xx_decode_crb_addr((unsigned long)buf[i].addr) +
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
		    QLA82XX_PCI_CRBSPACE;
		/* Not all CRB  addr/value pair to be written,
		 * some of them are skipped
		 */

		/* skip if LS bit is set*/
		if (off & 0x1) {
			DEBUG2(ql4_printk(KERN_WARNING, ha,
			    "Skip CRB init replay for offset = 0x%lx\n", off));
			continue;
		}

		/* skipping cold reboot MAGIC */
		if (off == QLA82XX_CAM_RAM(0x1fc))
			continue;

		/* do not reset PCI */
		if (off == (ROMUSB_GLB + 0xbc))
			continue;

		/* skip core clock, so that firmware can increase the clock */
		if (off == (ROMUSB_GLB + 0xc8))
			continue;

		/* skip the function enable register */
		if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION))
			continue;

		if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION2))
			continue;

		if ((off & 0x0ff00000) == QLA82XX_CRB_SMB)
			continue;

		if ((off & 0x0ff00000) == QLA82XX_CRB_DDR_NET)
			continue;

		if (off == ADDR_ERROR) {
			ql4_printk(KERN_WARNING, ha,
			    "%s: [ERROR] Unknown addr: 0x%08lx\n",
			    DRIVER_NAME, buf[i].addr);
			continue;
		}

1145
		qla4_82xx_wr_32(ha, off, buf[i].data);
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

		/* ISP requires much bigger delay to settle down,
		 * else crb_window returns 0xffffffff
		 */
		if (off == QLA82XX_ROMUSB_GLB_SW_RESET)
			msleep(1000);

		/* ISP requires millisec delay between
		 * successive CRB register updation
		 */
		msleep(1);
	}

	kfree(buf);

	/* Resetting the data and instruction cache */
1162 1163 1164
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0xec, 0x1e);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0x4c, 8);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_I+0x4c, 8);
1165 1166

	/* Clear all protocol processing engines */
1167 1168 1169 1170 1171 1172 1173 1174
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0x8, 0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0xc, 0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0x8, 0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0xc, 0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0x8, 0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0xc, 0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0x8, 0);
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0xc, 0);
1175 1176 1177 1178 1179

	return 0;
}

static int
1180
qla4_82xx_load_from_flash(struct scsi_qla_host *ha, uint32_t image_start)
1181
{
1182
	int  i, rval = 0;
1183 1184 1185 1186 1187 1188
	long size = 0;
	long flashaddr, memaddr;
	u64 data;
	u32 high, low;

	flashaddr = memaddr = ha->hw.flt_region_bootload;
1189
	size = (image_start - flashaddr) / 8;
1190 1191 1192 1193 1194

	DEBUG2(printk("scsi%ld: %s: bootldr=0x%lx, fw_image=0x%x\n",
	    ha->host_no, __func__, flashaddr, image_start));

	for (i = 0; i < size; i++) {
1195 1196
		if ((qla4_82xx_rom_fast_read(ha, flashaddr, (int *)&low)) ||
		    (qla4_82xx_rom_fast_read(ha, flashaddr + 4,
1197
		    (int *)&high))) {
1198 1199
			rval = -1;
			goto exit_load_from_flash;
1200 1201
		}
		data = ((u64)high << 32) | low ;
1202
		rval = qla4_82xx_pci_mem_write_2M(ha, memaddr, &data, 8);
1203 1204 1205
		if (rval)
			goto exit_load_from_flash;

1206 1207 1208
		flashaddr += 8;
		memaddr   += 8;

1209
		if (i % 0x1000 == 0)
1210 1211 1212 1213 1214 1215 1216
			msleep(1);

	}

	udelay(100);

	read_lock(&ha->hw_lock);
1217 1218
	qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
1219 1220
	read_unlock(&ha->hw_lock);

1221 1222
exit_load_from_flash:
	return rval;
1223 1224
}

1225
static int qla4_82xx_load_fw(struct scsi_qla_host *ha, uint32_t image_start)
1226 1227 1228
{
	u32 rst;

1229 1230
	qla4_82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
	if (qla4_82xx_pinit_from_rom(ha, 0) != QLA_SUCCESS) {
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
		printk(KERN_WARNING "%s: Error during CRB Initialization\n",
		    __func__);
		return QLA_ERROR;
	}

	udelay(500);

	/* at this point, QM is in reset. This could be a problem if there are
	 * incoming d* transition queue messages. QM/PCIE could wedge.
	 * To get around this, QM is brought out of reset.
	 */

1243
	rst = qla4_82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET);
1244 1245
	/* unreset qm */
	rst &= ~(1 << 28);
1246
	qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, rst);
1247

1248
	if (qla4_82xx_load_from_flash(ha, image_start)) {
1249 1250 1251 1252 1253 1254 1255 1256
		printk("%s: Error trying to load fw from flash!\n", __func__);
		return QLA_ERROR;
	}

	return QLA_SUCCESS;
}

int
1257
qla4_82xx_pci_mem_read_2M(struct scsi_qla_host *ha,
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
		u64 off, void *data, int size)
{
	int i, j = 0, k, start, end, loop, sz[2], off0[2];
	int shift_amount;
	uint32_t temp;
	uint64_t off8, val, mem_crb, word[2] = {0, 0};

	/*
	 * If not MN, go check for MS or invalid.
	 */

1269
	if (off >= QLA8XXX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
1270 1271 1272
		mem_crb = QLA82XX_CRB_QDR_NET;
	else {
		mem_crb = QLA82XX_CRB_DDR_NET;
1273 1274
		if (qla4_82xx_pci_mem_bound_check(ha, off, size) == 0)
			return qla4_82xx_pci_mem_read_direct(ha,
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
					off, data, size);
	}


	off8 = off & 0xfffffff0;
	off0[0] = off & 0xf;
	sz[0] = (size < (16 - off0[0])) ? size : (16 - off0[0]);
	shift_amount = 4;

	loop = ((off0[0] + size - 1) >> shift_amount) + 1;
	off0[1] = 0;
	sz[1] = size - sz[0];

	for (i = 0; i < loop; i++) {
		temp = off8 + (i << shift_amount);
1290
		qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_LO, temp);
1291
		temp = 0;
1292
		qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_HI, temp);
1293
		temp = MIU_TA_CTL_ENABLE;
1294
		qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1295
		temp = MIU_TA_CTL_START_ENABLE;
1296
		qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1297 1298

		for (j = 0; j < MAX_CTL_CHECK; j++) {
1299
			temp = qla4_82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
1300 1301 1302 1303 1304
			if ((temp & MIU_TA_CTL_BUSY) == 0)
				break;
		}

		if (j >= MAX_CTL_CHECK) {
1305 1306 1307
			printk_ratelimited(KERN_ERR
					   "%s: failed to read through agent\n",
					   __func__);
1308 1309 1310 1311 1312 1313
			break;
		}

		start = off0[i] >> 2;
		end   = (off0[i] + sz[i] - 1) >> 2;
		for (k = start; k <= end; k++) {
1314
			temp = qla4_82xx_rd_32(ha,
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
				mem_crb + MIU_TEST_AGT_RDDATA(k));
			word[i] |= ((uint64_t)temp << (32 * (k & 1)));
		}
	}

	if (j >= MAX_CTL_CHECK)
		return -1;

	if ((off0[0] & 7) == 0) {
		val = word[0];
	} else {
		val = ((word[0] >> (off0[0] * 8)) & (~(~0ULL << (sz[0] * 8)))) |
		((word[1] & (~(~0ULL << (sz[1] * 8)))) << (sz[0] * 8));
	}

	switch (size) {
	case 1:
		*(uint8_t  *)data = val;
		break;
	case 2:
		*(uint16_t *)data = val;
		break;
	case 4:
		*(uint32_t *)data = val;
		break;
	case 8:
		*(uint64_t *)data = val;
		break;
	}
	return 0;
}

int
1348
qla4_82xx_pci_mem_write_2M(struct scsi_qla_host *ha,
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		u64 off, void *data, int size)
{
	int i, j, ret = 0, loop, sz[2], off0;
	int scale, shift_amount, startword;
	uint32_t temp;
	uint64_t off8, mem_crb, tmpw, word[2] = {0, 0};

	/*
	 * If not MN, go check for MS or invalid.
	 */
1359
	if (off >= QLA8XXX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
1360 1361 1362
		mem_crb = QLA82XX_CRB_QDR_NET;
	else {
		mem_crb = QLA82XX_CRB_DDR_NET;
1363 1364
		if (qla4_82xx_pci_mem_bound_check(ha, off, size) == 0)
			return qla4_82xx_pci_mem_write_direct(ha,
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
					off, data, size);
	}

	off0 = off & 0x7;
	sz[0] = (size < (8 - off0)) ? size : (8 - off0);
	sz[1] = size - sz[0];

	off8 = off & 0xfffffff0;
	loop = (((off & 0xf) + size - 1) >> 4) + 1;
	shift_amount = 4;
	scale = 2;
	startword = (off & 0xf)/8;

	for (i = 0; i < loop; i++) {
1379
		if (qla4_82xx_pci_mem_read_2M(ha, off8 +
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
		    (i << shift_amount), &word[i * scale], 8))
			return -1;
	}

	switch (size) {
	case 1:
		tmpw = *((uint8_t *)data);
		break;
	case 2:
		tmpw = *((uint16_t *)data);
		break;
	case 4:
		tmpw = *((uint32_t *)data);
		break;
	case 8:
	default:
		tmpw = *((uint64_t *)data);
		break;
	}

	if (sz[0] == 8)
		word[startword] = tmpw;
	else {
		word[startword] &=
		    ~((~(~0ULL << (sz[0] * 8))) << (off0 * 8));
		word[startword] |= tmpw << (off0 * 8);
	}

	if (sz[1] != 0) {
		word[startword+1] &= ~(~0ULL << (sz[1] * 8));
		word[startword+1] |= tmpw >> (sz[0] * 8);
	}

	for (i = 0; i < loop; i++) {
		temp = off8 + (i << shift_amount);
1415
		qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_LO, temp);
1416
		temp = 0;
1417
		qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_HI, temp);
1418
		temp = word[i * scale] & 0xffffffff;
1419
		qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_LO, temp);
1420
		temp = (word[i * scale] >> 32) & 0xffffffff;
1421
		qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_HI, temp);
1422
		temp = word[i*scale + 1] & 0xffffffff;
1423
		qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_WRDATA_UPPER_LO,
1424 1425
		    temp);
		temp = (word[i*scale + 1] >> 32) & 0xffffffff;
1426
		qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_WRDATA_UPPER_HI,
1427 1428
		    temp);

1429
		temp = MIU_TA_CTL_WRITE_ENABLE;
1430
		qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_CTRL, temp);
1431
		temp = MIU_TA_CTL_WRITE_START;
1432
		qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_CTRL, temp);
1433 1434

		for (j = 0; j < MAX_CTL_CHECK; j++) {
1435
			temp = qla4_82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
1436 1437 1438 1439 1440 1441 1442
			if ((temp & MIU_TA_CTL_BUSY) == 0)
				break;
		}

		if (j >= MAX_CTL_CHECK) {
			if (printk_ratelimit())
				ql4_printk(KERN_ERR, ha,
1443 1444
					   "%s: failed to read through agent\n",
					   __func__);
1445 1446 1447 1448 1449 1450 1451 1452
			ret = -1;
			break;
		}
	}

	return ret;
}

1453
static int qla4_82xx_cmdpeg_ready(struct scsi_qla_host *ha, int pegtune_val)
1454 1455 1456 1457 1458 1459
{
	u32 val = 0;
	int retries = 60;

	if (!pegtune_val) {
		do {
1460
			val = qla4_82xx_rd_32(ha, CRB_CMDPEG_STATE);
1461 1462 1463 1464 1465 1466 1467 1468 1469
			if ((val == PHAN_INITIALIZE_COMPLETE) ||
			    (val == PHAN_INITIALIZE_ACK))
				return 0;
			set_current_state(TASK_UNINTERRUPTIBLE);
			schedule_timeout(500);

		} while (--retries);

		if (!retries) {
1470
			pegtune_val = qla4_82xx_rd_32(ha,
1471 1472 1473 1474 1475 1476 1477 1478 1479
				QLA82XX_ROMUSB_GLB_PEGTUNE_DONE);
			printk(KERN_WARNING "%s: init failed, "
				"pegtune_val = %x\n", __func__, pegtune_val);
			return -1;
		}
	}
	return 0;
}

1480
static int qla4_82xx_rcvpeg_ready(struct scsi_qla_host *ha)
1481 1482 1483 1484 1485 1486
{
	uint32_t state = 0;
	int loops = 0;

	/* Window 1 call */
	read_lock(&ha->hw_lock);
1487
	state = qla4_82xx_rd_32(ha, CRB_RCVPEG_STATE);
1488 1489 1490 1491 1492 1493
	read_unlock(&ha->hw_lock);

	while ((state != PHAN_PEG_RCV_INITIALIZED) && (loops < 30000)) {
		udelay(100);
		/* Window 1 call */
		read_lock(&ha->hw_lock);
1494
		state = qla4_82xx_rd_32(ha, CRB_RCVPEG_STATE);
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
		read_unlock(&ha->hw_lock);

		loops++;
	}

	if (loops >= 30000) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
		    "Receive Peg initialization not complete: 0x%x.\n", state));
		return QLA_ERROR;
	}

	return QLA_SUCCESS;
}

A
Andrew Morton 已提交
1509
void
1510 1511 1512 1513
qla4_8xxx_set_drv_active(struct scsi_qla_host *ha)
{
	uint32_t drv_active;

1514
	drv_active = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_ACTIVE);
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

	/*
	 * For ISP8324, drv_active register has 1 bit per function,
	 * shift 1 by func_num to set a bit for the function.
	 * For ISP8022, drv_active has 4 bits per function
	 */
	if (is_qla8032(ha))
		drv_active |= (1 << ha->func_num);
	else
		drv_active |= (1 << (ha->func_num * 4));

1526 1527
	ql4_printk(KERN_INFO, ha, "%s(%ld): drv_active: 0x%08x\n",
		   __func__, ha->host_no, drv_active);
1528
	qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_ACTIVE, drv_active);
1529 1530 1531 1532 1533 1534 1535
}

void
qla4_8xxx_clear_drv_active(struct scsi_qla_host *ha)
{
	uint32_t drv_active;

1536
	drv_active = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_ACTIVE);
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

	/*
	 * For ISP8324, drv_active register has 1 bit per function,
	 * shift 1 by func_num to set a bit for the function.
	 * For ISP8022, drv_active has 4 bits per function
	 */
	if (is_qla8032(ha))
		drv_active &= ~(1 << (ha->func_num));
	else
		drv_active &= ~(1 << (ha->func_num * 4));

1548 1549
	ql4_printk(KERN_INFO, ha, "%s(%ld): drv_active: 0x%08x\n",
		   __func__, ha->host_no, drv_active);
1550
	qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_ACTIVE, drv_active);
1551 1552
}

1553
inline int qla4_8xxx_need_reset(struct scsi_qla_host *ha)
1554
{
1555
	uint32_t drv_state, drv_active;
1556 1557
	int rval;

1558 1559
	drv_active = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_ACTIVE);
	drv_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_STATE);
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570

	/*
	 * For ISP8324, drv_active register has 1 bit per function,
	 * shift 1 by func_num to set a bit for the function.
	 * For ISP8022, drv_active has 4 bits per function
	 */
	if (is_qla8032(ha))
		rval = drv_state & (1 << ha->func_num);
	else
		rval = drv_state & (1 << (ha->func_num * 4));

1571 1572 1573
	if ((test_bit(AF_EEH_BUSY, &ha->flags)) && drv_active)
		rval = 1;

1574 1575 1576
	return rval;
}

1577
void qla4_8xxx_set_rst_ready(struct scsi_qla_host *ha)
1578 1579 1580
{
	uint32_t drv_state;

1581
	drv_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_STATE);
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592

	/*
	 * For ISP8324, drv_active register has 1 bit per function,
	 * shift 1 by func_num to set a bit for the function.
	 * For ISP8022, drv_active has 4 bits per function
	 */
	if (is_qla8032(ha))
		drv_state |= (1 << ha->func_num);
	else
		drv_state |= (1 << (ha->func_num * 4));

1593 1594
	ql4_printk(KERN_INFO, ha, "%s(%ld): drv_state: 0x%08x\n",
		   __func__, ha->host_no, drv_state);
1595
	qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, drv_state);
1596 1597
}

1598
void qla4_8xxx_clear_rst_ready(struct scsi_qla_host *ha)
1599 1600 1601
{
	uint32_t drv_state;

1602
	drv_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_STATE);
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613

	/*
	 * For ISP8324, drv_active register has 1 bit per function,
	 * shift 1 by func_num to set a bit for the function.
	 * For ISP8022, drv_active has 4 bits per function
	 */
	if (is_qla8032(ha))
		drv_state &= ~(1 << ha->func_num);
	else
		drv_state &= ~(1 << (ha->func_num * 4));

1614 1615
	ql4_printk(KERN_INFO, ha, "%s(%ld): drv_state: 0x%08x\n",
		   __func__, ha->host_no, drv_state);
1616
	qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, drv_state);
1617 1618 1619 1620 1621 1622 1623
}

static inline void
qla4_8xxx_set_qsnt_ready(struct scsi_qla_host *ha)
{
	uint32_t qsnt_state;

1624
	qsnt_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_STATE);
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635

	/*
	 * For ISP8324, drv_active register has 1 bit per function,
	 * shift 1 by func_num to set a bit for the function.
	 * For ISP8022, drv_active has 4 bits per function.
	 */
	if (is_qla8032(ha))
		qsnt_state |= (1 << ha->func_num);
	else
		qsnt_state |= (2 << (ha->func_num * 4));

1636
	qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, qsnt_state);
1637 1638 1639 1640
}


static int
1641
qla4_82xx_start_firmware(struct scsi_qla_host *ha, uint32_t image_start)
1642 1643 1644 1645
{
	uint16_t lnk;

	/* scrub dma mask expansion register */
1646
	qla4_82xx_wr_32(ha, CRB_DMA_SHIFT, 0x55555555);
1647 1648

	/* Overwrite stale initialization register values */
1649 1650 1651 1652
	qla4_82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
	qla4_82xx_wr_32(ha, CRB_RCVPEG_STATE, 0);
	qla4_82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS1, 0);
	qla4_82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS2, 0);
1653

1654
	if (qla4_82xx_load_fw(ha, image_start) != QLA_SUCCESS) {
1655 1656 1657 1658 1659
		printk("%s: Error trying to start fw!\n", __func__);
		return QLA_ERROR;
	}

	/* Handshake with the card before we register the devices. */
1660
	if (qla4_82xx_cmdpeg_ready(ha, 0) != QLA_SUCCESS) {
1661 1662 1663 1664 1665
		printk("%s: Error during card handshake!\n", __func__);
		return QLA_ERROR;
	}

	/* Negotiated Link width */
1666
	pcie_capability_read_word(ha->pdev, PCI_EXP_LNKSTA, &lnk);
1667 1668 1669
	ha->link_width = (lnk >> 4) & 0x3f;

	/* Synchronize with Receive peg */
1670
	return qla4_82xx_rcvpeg_ready(ha);
1671 1672
}

1673
int qla4_82xx_try_start_fw(struct scsi_qla_host *ha)
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
{
	int rval = QLA_ERROR;

	/*
	 * FW Load priority:
	 * 1) Operational firmware residing in flash.
	 * 2) Fail
	 */

	ql4_printk(KERN_INFO, ha,
	    "FW: Retrieving flash offsets from FLT/FDT ...\n");
	rval = qla4_8xxx_get_flash_info(ha);
	if (rval != QLA_SUCCESS)
		return rval;

	ql4_printk(KERN_INFO, ha,
	    "FW: Attempting to load firmware from flash...\n");
1691
	rval = qla4_82xx_start_firmware(ha, ha->hw.flt_region_fw);
1692

V
Vikas Chaudhary 已提交
1693 1694 1695 1696 1697
	if (rval != QLA_SUCCESS) {
		ql4_printk(KERN_ERR, ha, "FW: Load firmware from flash"
		    " FAILED...\n");
		return rval;
	}
1698 1699 1700 1701

	return rval;
}

1702
void qla4_82xx_rom_lock_recovery(struct scsi_qla_host *ha)
1703
{
1704
	if (qla4_82xx_rom_lock(ha)) {
1705 1706 1707 1708 1709 1710 1711 1712 1713
		/* Someone else is holding the lock. */
		dev_info(&ha->pdev->dev, "Resetting rom_lock\n");
	}

	/*
	 * Either we got the lock, or someone
	 * else died while holding it.
	 * In either case, unlock.
	 */
1714
	qla4_82xx_rom_unlock(ha);
1715 1716
}

1717
static void qla4_8xxx_minidump_process_rdcrb(struct scsi_qla_host *ha,
1718
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
1719 1720 1721
				uint32_t **d_ptr)
{
	uint32_t r_addr, r_stride, loop_cnt, i, r_value;
1722
	struct qla8xxx_minidump_entry_crb *crb_hdr;
1723 1724 1725
	uint32_t *data_ptr = *d_ptr;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
1726
	crb_hdr = (struct qla8xxx_minidump_entry_crb *)entry_hdr;
1727 1728 1729 1730 1731
	r_addr = crb_hdr->addr;
	r_stride = crb_hdr->crb_strd.addr_stride;
	loop_cnt = crb_hdr->op_count;

	for (i = 0; i < loop_cnt; i++) {
1732
		ha->isp_ops->rd_reg_indirect(ha, r_addr, &r_value);
1733 1734 1735 1736 1737 1738 1739 1740
		*data_ptr++ = cpu_to_le32(r_addr);
		*data_ptr++ = cpu_to_le32(r_value);
		r_addr += r_stride;
	}
	*d_ptr = data_ptr;
}

static int qla4_8xxx_minidump_process_l2tag(struct scsi_qla_host *ha,
1741
				 struct qla8xxx_minidump_entry_hdr *entry_hdr,
1742 1743 1744 1745 1746 1747
				 uint32_t **d_ptr)
{
	uint32_t addr, r_addr, c_addr, t_r_addr;
	uint32_t i, k, loop_count, t_value, r_cnt, r_value;
	unsigned long p_wait, w_time, p_mask;
	uint32_t c_value_w, c_value_r;
1748
	struct qla8xxx_minidump_entry_cache *cache_hdr;
1749 1750 1751 1752
	int rval = QLA_ERROR;
	uint32_t *data_ptr = *d_ptr;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
1753
	cache_hdr = (struct qla8xxx_minidump_entry_cache *)entry_hdr;
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766

	loop_count = cache_hdr->op_count;
	r_addr = cache_hdr->read_addr;
	c_addr = cache_hdr->control_addr;
	c_value_w = cache_hdr->cache_ctrl.write_value;

	t_r_addr = cache_hdr->tag_reg_addr;
	t_value = cache_hdr->addr_ctrl.init_tag_value;
	r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
	p_wait = cache_hdr->cache_ctrl.poll_wait;
	p_mask = cache_hdr->cache_ctrl.poll_mask;

	for (i = 0; i < loop_count; i++) {
1767
		ha->isp_ops->wr_reg_indirect(ha, t_r_addr, t_value);
1768 1769

		if (c_value_w)
1770
			ha->isp_ops->wr_reg_indirect(ha, c_addr, c_value_w);
1771 1772 1773 1774

		if (p_mask) {
			w_time = jiffies + p_wait;
			do {
1775 1776
				ha->isp_ops->rd_reg_indirect(ha, c_addr,
							     &c_value_r);
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
				if ((c_value_r & p_mask) == 0) {
					break;
				} else if (time_after_eq(jiffies, w_time)) {
					/* capturing dump failed */
					return rval;
				}
			} while (1);
		}

		addr = r_addr;
		for (k = 0; k < r_cnt; k++) {
1788
			ha->isp_ops->rd_reg_indirect(ha, addr, &r_value);
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
			*data_ptr++ = cpu_to_le32(r_value);
			addr += cache_hdr->read_ctrl.read_addr_stride;
		}

		t_value += cache_hdr->addr_ctrl.tag_value_stride;
	}
	*d_ptr = data_ptr;
	return QLA_SUCCESS;
}

static int qla4_8xxx_minidump_process_control(struct scsi_qla_host *ha,
1800
				struct qla8xxx_minidump_entry_hdr *entry_hdr)
1801
{
1802
	struct qla8xxx_minidump_entry_crb *crb_entry;
1803 1804 1805 1806 1807 1808 1809 1810 1811
	uint32_t read_value, opcode, poll_time, addr, index, rval = QLA_SUCCESS;
	uint32_t crb_addr;
	unsigned long wtime;
	struct qla4_8xxx_minidump_template_hdr *tmplt_hdr;
	int i;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
	tmplt_hdr = (struct qla4_8xxx_minidump_template_hdr *)
						ha->fw_dump_tmplt_hdr;
1812
	crb_entry = (struct qla8xxx_minidump_entry_crb *)entry_hdr;
1813 1814 1815 1816

	crb_addr = crb_entry->addr;
	for (i = 0; i < crb_entry->op_count; i++) {
		opcode = crb_entry->crb_ctrl.opcode;
1817
		if (opcode & QLA8XXX_DBG_OPCODE_WR) {
1818 1819
			ha->isp_ops->wr_reg_indirect(ha, crb_addr,
						     crb_entry->value_1);
1820
			opcode &= ~QLA8XXX_DBG_OPCODE_WR;
1821
		}
1822
		if (opcode & QLA8XXX_DBG_OPCODE_RW) {
1823 1824
			ha->isp_ops->rd_reg_indirect(ha, crb_addr, &read_value);
			ha->isp_ops->wr_reg_indirect(ha, crb_addr, read_value);
1825
			opcode &= ~QLA8XXX_DBG_OPCODE_RW;
1826
		}
1827
		if (opcode & QLA8XXX_DBG_OPCODE_AND) {
1828
			ha->isp_ops->rd_reg_indirect(ha, crb_addr, &read_value);
1829
			read_value &= crb_entry->value_2;
1830 1831
			opcode &= ~QLA8XXX_DBG_OPCODE_AND;
			if (opcode & QLA8XXX_DBG_OPCODE_OR) {
1832
				read_value |= crb_entry->value_3;
1833
				opcode &= ~QLA8XXX_DBG_OPCODE_OR;
1834
			}
1835
			ha->isp_ops->wr_reg_indirect(ha, crb_addr, read_value);
1836
		}
1837
		if (opcode & QLA8XXX_DBG_OPCODE_OR) {
1838
			ha->isp_ops->rd_reg_indirect(ha, crb_addr, &read_value);
1839
			read_value |= crb_entry->value_3;
1840
			ha->isp_ops->wr_reg_indirect(ha, crb_addr, read_value);
1841
			opcode &= ~QLA8XXX_DBG_OPCODE_OR;
1842
		}
1843
		if (opcode & QLA8XXX_DBG_OPCODE_POLL) {
1844 1845
			poll_time = crb_entry->crb_strd.poll_timeout;
			wtime = jiffies + poll_time;
1846
			ha->isp_ops->rd_reg_indirect(ha, crb_addr, &read_value);
1847 1848 1849

			do {
				if ((read_value & crb_entry->value_2) ==
1850
				    crb_entry->value_1) {
1851
					break;
1852
				} else if (time_after_eq(jiffies, wtime)) {
1853 1854 1855
					/* capturing dump failed */
					rval = QLA_ERROR;
					break;
1856 1857 1858 1859
				} else {
					ha->isp_ops->rd_reg_indirect(ha,
							crb_addr, &read_value);
				}
1860
			} while (1);
1861
			opcode &= ~QLA8XXX_DBG_OPCODE_POLL;
1862 1863
		}

1864
		if (opcode & QLA8XXX_DBG_OPCODE_RDSTATE) {
1865 1866 1867 1868 1869 1870 1871
			if (crb_entry->crb_strd.state_index_a) {
				index = crb_entry->crb_strd.state_index_a;
				addr = tmplt_hdr->saved_state_array[index];
			} else {
				addr = crb_addr;
			}

1872
			ha->isp_ops->rd_reg_indirect(ha, addr, &read_value);
1873 1874
			index = crb_entry->crb_ctrl.state_index_v;
			tmplt_hdr->saved_state_array[index] = read_value;
1875
			opcode &= ~QLA8XXX_DBG_OPCODE_RDSTATE;
1876 1877
		}

1878
		if (opcode & QLA8XXX_DBG_OPCODE_WRSTATE) {
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
			if (crb_entry->crb_strd.state_index_a) {
				index = crb_entry->crb_strd.state_index_a;
				addr = tmplt_hdr->saved_state_array[index];
			} else {
				addr = crb_addr;
			}

			if (crb_entry->crb_ctrl.state_index_v) {
				index = crb_entry->crb_ctrl.state_index_v;
				read_value =
					tmplt_hdr->saved_state_array[index];
			} else {
				read_value = crb_entry->value_1;
			}

1894
			ha->isp_ops->wr_reg_indirect(ha, addr, read_value);
1895
			opcode &= ~QLA8XXX_DBG_OPCODE_WRSTATE;
1896 1897
		}

1898
		if (opcode & QLA8XXX_DBG_OPCODE_MDSTATE) {
1899 1900 1901 1902 1903 1904 1905 1906 1907
			index = crb_entry->crb_ctrl.state_index_v;
			read_value = tmplt_hdr->saved_state_array[index];
			read_value <<= crb_entry->crb_ctrl.shl;
			read_value >>= crb_entry->crb_ctrl.shr;
			if (crb_entry->value_2)
				read_value &= crb_entry->value_2;
			read_value |= crb_entry->value_3;
			read_value += crb_entry->value_1;
			tmplt_hdr->saved_state_array[index] = read_value;
1908
			opcode &= ~QLA8XXX_DBG_OPCODE_MDSTATE;
1909 1910 1911 1912 1913 1914 1915 1916
		}
		crb_addr += crb_entry->crb_strd.addr_stride;
	}
	DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s\n", __func__));
	return rval;
}

static void qla4_8xxx_minidump_process_rdocm(struct scsi_qla_host *ha,
1917
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
1918 1919 1920
				uint32_t **d_ptr)
{
	uint32_t r_addr, r_stride, loop_cnt, i, r_value;
1921
	struct qla8xxx_minidump_entry_rdocm *ocm_hdr;
1922 1923 1924
	uint32_t *data_ptr = *d_ptr;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
1925
	ocm_hdr = (struct qla8xxx_minidump_entry_rdocm *)entry_hdr;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
	r_addr = ocm_hdr->read_addr;
	r_stride = ocm_hdr->read_addr_stride;
	loop_cnt = ocm_hdr->op_count;

	DEBUG2(ql4_printk(KERN_INFO, ha,
			  "[%s]: r_addr: 0x%x, r_stride: 0x%x, loop_cnt: 0x%x\n",
			  __func__, r_addr, r_stride, loop_cnt));

	for (i = 0; i < loop_cnt; i++) {
		r_value = readl((void __iomem *)(r_addr + ha->nx_pcibase));
		*data_ptr++ = cpu_to_le32(r_value);
		r_addr += r_stride;
	}
	DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s datacount: 0x%lx\n",
1940
		__func__, (long unsigned int) (loop_cnt * sizeof(uint32_t))));
1941 1942 1943 1944
	*d_ptr = data_ptr;
}

static void qla4_8xxx_minidump_process_rdmux(struct scsi_qla_host *ha,
1945
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
1946 1947 1948
				uint32_t **d_ptr)
{
	uint32_t r_addr, s_stride, s_addr, s_value, loop_cnt, i, r_value;
1949
	struct qla8xxx_minidump_entry_mux *mux_hdr;
1950 1951 1952
	uint32_t *data_ptr = *d_ptr;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
1953
	mux_hdr = (struct qla8xxx_minidump_entry_mux *)entry_hdr;
1954 1955 1956 1957 1958 1959 1960
	r_addr = mux_hdr->read_addr;
	s_addr = mux_hdr->select_addr;
	s_stride = mux_hdr->select_value_stride;
	s_value = mux_hdr->select_value;
	loop_cnt = mux_hdr->op_count;

	for (i = 0; i < loop_cnt; i++) {
1961 1962
		ha->isp_ops->wr_reg_indirect(ha, s_addr, s_value);
		ha->isp_ops->rd_reg_indirect(ha, r_addr, &r_value);
1963 1964 1965 1966 1967 1968 1969 1970
		*data_ptr++ = cpu_to_le32(s_value);
		*data_ptr++ = cpu_to_le32(r_value);
		s_value += s_stride;
	}
	*d_ptr = data_ptr;
}

static void qla4_8xxx_minidump_process_l1cache(struct scsi_qla_host *ha,
1971
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
1972 1973 1974 1975 1976
				uint32_t **d_ptr)
{
	uint32_t addr, r_addr, c_addr, t_r_addr;
	uint32_t i, k, loop_count, t_value, r_cnt, r_value;
	uint32_t c_value_w;
1977
	struct qla8xxx_minidump_entry_cache *cache_hdr;
1978 1979
	uint32_t *data_ptr = *d_ptr;

1980
	cache_hdr = (struct qla8xxx_minidump_entry_cache *)entry_hdr;
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
	loop_count = cache_hdr->op_count;
	r_addr = cache_hdr->read_addr;
	c_addr = cache_hdr->control_addr;
	c_value_w = cache_hdr->cache_ctrl.write_value;

	t_r_addr = cache_hdr->tag_reg_addr;
	t_value = cache_hdr->addr_ctrl.init_tag_value;
	r_cnt = cache_hdr->read_ctrl.read_addr_cnt;

	for (i = 0; i < loop_count; i++) {
1991 1992
		ha->isp_ops->wr_reg_indirect(ha, t_r_addr, t_value);
		ha->isp_ops->wr_reg_indirect(ha, c_addr, c_value_w);
1993 1994
		addr = r_addr;
		for (k = 0; k < r_cnt; k++) {
1995
			ha->isp_ops->rd_reg_indirect(ha, addr, &r_value);
1996 1997 1998 1999 2000 2001 2002 2003 2004
			*data_ptr++ = cpu_to_le32(r_value);
			addr += cache_hdr->read_ctrl.read_addr_stride;
		}
		t_value += cache_hdr->addr_ctrl.tag_value_stride;
	}
	*d_ptr = data_ptr;
}

static void qla4_8xxx_minidump_process_queue(struct scsi_qla_host *ha,
2005
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
2006 2007 2008 2009 2010
				uint32_t **d_ptr)
{
	uint32_t s_addr, r_addr;
	uint32_t r_stride, r_value, r_cnt, qid = 0;
	uint32_t i, k, loop_cnt;
2011
	struct qla8xxx_minidump_entry_queue *q_hdr;
2012 2013 2014
	uint32_t *data_ptr = *d_ptr;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
2015
	q_hdr = (struct qla8xxx_minidump_entry_queue *)entry_hdr;
2016 2017 2018 2019 2020 2021
	s_addr = q_hdr->select_addr;
	r_cnt = q_hdr->rd_strd.read_addr_cnt;
	r_stride = q_hdr->rd_strd.read_addr_stride;
	loop_cnt = q_hdr->op_count;

	for (i = 0; i < loop_cnt; i++) {
2022
		ha->isp_ops->wr_reg_indirect(ha, s_addr, qid);
2023 2024
		r_addr = q_hdr->read_addr;
		for (k = 0; k < r_cnt; k++) {
2025
			ha->isp_ops->rd_reg_indirect(ha, r_addr, &r_value);
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
			*data_ptr++ = cpu_to_le32(r_value);
			r_addr += r_stride;
		}
		qid += q_hdr->q_strd.queue_id_stride;
	}
	*d_ptr = data_ptr;
}

#define MD_DIRECT_ROM_WINDOW		0x42110030
#define MD_DIRECT_ROM_READ_BASE		0x42150000

2037
static void qla4_82xx_minidump_process_rdrom(struct scsi_qla_host *ha,
2038
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
2039 2040 2041 2042
				uint32_t **d_ptr)
{
	uint32_t r_addr, r_value;
	uint32_t i, loop_cnt;
2043
	struct qla8xxx_minidump_entry_rdrom *rom_hdr;
2044 2045 2046
	uint32_t *data_ptr = *d_ptr;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
2047
	rom_hdr = (struct qla8xxx_minidump_entry_rdrom *)entry_hdr;
2048 2049 2050 2051 2052 2053 2054 2055
	r_addr = rom_hdr->read_addr;
	loop_cnt = rom_hdr->read_data_size/sizeof(uint32_t);

	DEBUG2(ql4_printk(KERN_INFO, ha,
			  "[%s]: flash_addr: 0x%x, read_data_size: 0x%x\n",
			   __func__, r_addr, loop_cnt));

	for (i = 0; i < loop_cnt; i++) {
2056 2057 2058 2059 2060
		ha->isp_ops->wr_reg_indirect(ha, MD_DIRECT_ROM_WINDOW,
					     (r_addr & 0xFFFF0000));
		ha->isp_ops->rd_reg_indirect(ha,
				MD_DIRECT_ROM_READ_BASE + (r_addr & 0x0000FFFF),
				&r_value);
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
		*data_ptr++ = cpu_to_le32(r_value);
		r_addr += sizeof(uint32_t);
	}
	*d_ptr = data_ptr;
}

#define MD_MIU_TEST_AGT_CTRL		0x41000090
#define MD_MIU_TEST_AGT_ADDR_LO		0x41000094
#define MD_MIU_TEST_AGT_ADDR_HI		0x41000098

static int qla4_8xxx_minidump_process_rdmem(struct scsi_qla_host *ha,
2072
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
2073 2074 2075 2076
				uint32_t **d_ptr)
{
	uint32_t r_addr, r_value, r_data;
	uint32_t i, j, loop_cnt;
2077
	struct qla8xxx_minidump_entry_rdmem *m_hdr;
2078 2079 2080 2081
	unsigned long flags;
	uint32_t *data_ptr = *d_ptr;

	DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
2082
	m_hdr = (struct qla8xxx_minidump_entry_rdmem *)entry_hdr;
2083 2084 2085 2086 2087 2088 2089 2090 2091
	r_addr = m_hdr->read_addr;
	loop_cnt = m_hdr->read_data_size/16;

	DEBUG2(ql4_printk(KERN_INFO, ha,
			  "[%s]: Read addr: 0x%x, read_data_size: 0x%x\n",
			  __func__, r_addr, m_hdr->read_data_size));

	if (r_addr & 0xf) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
2092
				  "[%s]: Read addr 0x%x not 16 bytes aligned\n",
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
				  __func__, r_addr));
		return QLA_ERROR;
	}

	if (m_hdr->read_data_size % 16) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
				  "[%s]: Read data[0x%x] not multiple of 16 bytes\n",
				  __func__, m_hdr->read_data_size));
		return QLA_ERROR;
	}

	DEBUG2(ql4_printk(KERN_INFO, ha,
			  "[%s]: rdmem_addr: 0x%x, read_data_size: 0x%x, loop_cnt: 0x%x\n",
			  __func__, r_addr, m_hdr->read_data_size, loop_cnt));

	write_lock_irqsave(&ha->hw_lock, flags);
	for (i = 0; i < loop_cnt; i++) {
2110 2111
		ha->isp_ops->wr_reg_indirect(ha, MD_MIU_TEST_AGT_ADDR_LO,
					     r_addr);
2112
		r_value = 0;
2113 2114
		ha->isp_ops->wr_reg_indirect(ha, MD_MIU_TEST_AGT_ADDR_HI,
					     r_value);
2115
		r_value = MIU_TA_CTL_ENABLE;
2116
		ha->isp_ops->wr_reg_indirect(ha, MD_MIU_TEST_AGT_CTRL, r_value);
2117
		r_value = MIU_TA_CTL_START_ENABLE;
2118
		ha->isp_ops->wr_reg_indirect(ha, MD_MIU_TEST_AGT_CTRL, r_value);
2119 2120

		for (j = 0; j < MAX_CTL_CHECK; j++) {
2121 2122
			ha->isp_ops->rd_reg_indirect(ha, MD_MIU_TEST_AGT_CTRL,
						     &r_value);
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
			if ((r_value & MIU_TA_CTL_BUSY) == 0)
				break;
		}

		if (j >= MAX_CTL_CHECK) {
			printk_ratelimited(KERN_ERR
					   "%s: failed to read through agent\n",
					    __func__);
			write_unlock_irqrestore(&ha->hw_lock, flags);
			return QLA_SUCCESS;
		}

		for (j = 0; j < 4; j++) {
2136 2137 2138
			ha->isp_ops->rd_reg_indirect(ha,
						     MD_MIU_TEST_AGT_RDDATA[j],
						     &r_data);
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
			*data_ptr++ = cpu_to_le32(r_data);
		}

		r_addr += 16;
	}
	write_unlock_irqrestore(&ha->hw_lock, flags);

	DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s datacount: 0x%x\n",
			  __func__, (loop_cnt * 16)));

	*d_ptr = data_ptr;
	return QLA_SUCCESS;
}

2153
static void qla4_8xxx_mark_entry_skipped(struct scsi_qla_host *ha,
2154
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
2155 2156
				int index)
{
2157
	entry_hdr->d_ctrl.driver_flags |= QLA8XXX_DBG_SKIPPED_FLAG;
2158 2159 2160 2161 2162 2163
	DEBUG2(ql4_printk(KERN_INFO, ha,
			  "scsi(%ld): Skipping entry[%d]: ETYPE[0x%x]-ELEVEL[0x%x]\n",
			  ha->host_no, index, entry_hdr->entry_type,
			  entry_hdr->d_ctrl.entry_capture_mask));
}

2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
/* ISP83xx functions to process new minidump entries... */
static uint32_t qla83xx_minidump_process_pollrd(struct scsi_qla_host *ha,
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
				uint32_t **d_ptr)
{
	uint32_t r_addr, s_addr, s_value, r_value, poll_wait, poll_mask;
	uint16_t s_stride, i;
	uint32_t *data_ptr = *d_ptr;
	uint32_t rval = QLA_SUCCESS;
	struct qla83xx_minidump_entry_pollrd *pollrd_hdr;

	pollrd_hdr = (struct qla83xx_minidump_entry_pollrd *)entry_hdr;
	s_addr = le32_to_cpu(pollrd_hdr->select_addr);
	r_addr = le32_to_cpu(pollrd_hdr->read_addr);
	s_value = le32_to_cpu(pollrd_hdr->select_value);
	s_stride = le32_to_cpu(pollrd_hdr->select_value_stride);

	poll_wait = le32_to_cpu(pollrd_hdr->poll_wait);
	poll_mask = le32_to_cpu(pollrd_hdr->poll_mask);

	for (i = 0; i < le32_to_cpu(pollrd_hdr->op_count); i++) {
		ha->isp_ops->wr_reg_indirect(ha, s_addr, s_value);
		poll_wait = le32_to_cpu(pollrd_hdr->poll_wait);
		while (1) {
			ha->isp_ops->rd_reg_indirect(ha, s_addr, &r_value);

			if ((r_value & poll_mask) != 0) {
				break;
			} else {
				msleep(1);
				if (--poll_wait == 0) {
					ql4_printk(KERN_ERR, ha, "%s: TIMEOUT\n",
						   __func__);
					rval = QLA_ERROR;
					goto exit_process_pollrd;
				}
			}
		}
		ha->isp_ops->rd_reg_indirect(ha, r_addr, &r_value);
		*data_ptr++ = cpu_to_le32(s_value);
		*data_ptr++ = cpu_to_le32(r_value);
		s_value += s_stride;
	}

	*d_ptr = data_ptr;

exit_process_pollrd:
	return rval;
}

static void qla83xx_minidump_process_rdmux2(struct scsi_qla_host *ha,
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
				uint32_t **d_ptr)
{
	uint32_t sel_val1, sel_val2, t_sel_val, data, i;
	uint32_t sel_addr1, sel_addr2, sel_val_mask, read_addr;
	struct qla83xx_minidump_entry_rdmux2 *rdmux2_hdr;
	uint32_t *data_ptr = *d_ptr;

	rdmux2_hdr = (struct qla83xx_minidump_entry_rdmux2 *)entry_hdr;
	sel_val1 = le32_to_cpu(rdmux2_hdr->select_value_1);
	sel_val2 = le32_to_cpu(rdmux2_hdr->select_value_2);
	sel_addr1 = le32_to_cpu(rdmux2_hdr->select_addr_1);
	sel_addr2 = le32_to_cpu(rdmux2_hdr->select_addr_2);
	sel_val_mask = le32_to_cpu(rdmux2_hdr->select_value_mask);
	read_addr = le32_to_cpu(rdmux2_hdr->read_addr);

	for (i = 0; i < rdmux2_hdr->op_count; i++) {
		ha->isp_ops->wr_reg_indirect(ha, sel_addr1, sel_val1);
		t_sel_val = sel_val1 & sel_val_mask;
		*data_ptr++ = cpu_to_le32(t_sel_val);

		ha->isp_ops->wr_reg_indirect(ha, sel_addr2, t_sel_val);
		ha->isp_ops->rd_reg_indirect(ha, read_addr, &data);

		*data_ptr++ = cpu_to_le32(data);

		ha->isp_ops->wr_reg_indirect(ha, sel_addr1, sel_val2);
		t_sel_val = sel_val2 & sel_val_mask;
		*data_ptr++ = cpu_to_le32(t_sel_val);

		ha->isp_ops->wr_reg_indirect(ha, sel_addr2, t_sel_val);
		ha->isp_ops->rd_reg_indirect(ha, read_addr, &data);

		*data_ptr++ = cpu_to_le32(data);

		sel_val1 += rdmux2_hdr->select_value_stride;
		sel_val2 += rdmux2_hdr->select_value_stride;
	}

	*d_ptr = data_ptr;
}

static uint32_t qla83xx_minidump_process_pollrdmwr(struct scsi_qla_host *ha,
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
				uint32_t **d_ptr)
{
	uint32_t poll_wait, poll_mask, r_value, data;
	uint32_t addr_1, addr_2, value_1, value_2;
	uint32_t *data_ptr = *d_ptr;
	uint32_t rval = QLA_SUCCESS;
	struct qla83xx_minidump_entry_pollrdmwr *poll_hdr;

	poll_hdr = (struct qla83xx_minidump_entry_pollrdmwr *)entry_hdr;
	addr_1 = le32_to_cpu(poll_hdr->addr_1);
	addr_2 = le32_to_cpu(poll_hdr->addr_2);
	value_1 = le32_to_cpu(poll_hdr->value_1);
	value_2 = le32_to_cpu(poll_hdr->value_2);
	poll_mask = le32_to_cpu(poll_hdr->poll_mask);

	ha->isp_ops->wr_reg_indirect(ha, addr_1, value_1);

	poll_wait = le32_to_cpu(poll_hdr->poll_wait);
	while (1) {
		ha->isp_ops->rd_reg_indirect(ha, addr_1, &r_value);

		if ((r_value & poll_mask) != 0) {
			break;
		} else {
			msleep(1);
			if (--poll_wait == 0) {
				ql4_printk(KERN_ERR, ha, "%s: TIMEOUT_1\n",
					   __func__);
				rval = QLA_ERROR;
				goto exit_process_pollrdmwr;
			}
		}
	}

	ha->isp_ops->rd_reg_indirect(ha, addr_2, &data);
	data &= le32_to_cpu(poll_hdr->modify_mask);
	ha->isp_ops->wr_reg_indirect(ha, addr_2, data);
	ha->isp_ops->wr_reg_indirect(ha, addr_1, value_2);

	poll_wait = le32_to_cpu(poll_hdr->poll_wait);
	while (1) {
		ha->isp_ops->rd_reg_indirect(ha, addr_1, &r_value);

		if ((r_value & poll_mask) != 0) {
			break;
		} else {
			msleep(1);
			if (--poll_wait == 0) {
				ql4_printk(KERN_ERR, ha, "%s: TIMEOUT_2\n",
					   __func__);
				rval = QLA_ERROR;
				goto exit_process_pollrdmwr;
			}
		}
	}

	*data_ptr++ = cpu_to_le32(addr_2);
	*data_ptr++ = cpu_to_le32(data);
	*d_ptr = data_ptr;

exit_process_pollrdmwr:
	return rval;
}

static uint32_t qla4_83xx_minidump_process_rdrom(struct scsi_qla_host *ha,
				struct qla8xxx_minidump_entry_hdr *entry_hdr,
				uint32_t **d_ptr)
{
	uint32_t fl_addr, u32_count, rval;
	struct qla8xxx_minidump_entry_rdrom *rom_hdr;
	uint32_t *data_ptr = *d_ptr;

	rom_hdr = (struct qla8xxx_minidump_entry_rdrom *)entry_hdr;
	fl_addr = le32_to_cpu(rom_hdr->read_addr);
	u32_count = le32_to_cpu(rom_hdr->read_data_size)/sizeof(uint32_t);

	DEBUG2(ql4_printk(KERN_INFO, ha, "[%s]: fl_addr: 0x%x, count: 0x%x\n",
			  __func__, fl_addr, u32_count));

	rval = qla4_83xx_lockless_flash_read_u32(ha, fl_addr,
						 (u8 *)(data_ptr), u32_count);

	if (rval == QLA_ERROR) {
		ql4_printk(KERN_ERR, ha, "%s: Flash Read Error,Count=%d\n",
			   __func__, u32_count);
		goto exit_process_rdrom;
	}

	data_ptr += u32_count;
	*d_ptr = data_ptr;

exit_process_rdrom:
	return rval;
}

2354
/**
2355
 * qla4_8xxx_collect_md_data - Retrieve firmware minidump data.
2356 2357 2358 2359 2360
 * @ha: pointer to adapter structure
 **/
static int qla4_8xxx_collect_md_data(struct scsi_qla_host *ha)
{
	int num_entry_hdr = 0;
2361
	struct qla8xxx_minidump_entry_hdr *entry_hdr;
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
	struct qla4_8xxx_minidump_template_hdr *tmplt_hdr;
	uint32_t *data_ptr;
	uint32_t data_collected = 0;
	int i, rval = QLA_ERROR;
	uint64_t now;
	uint32_t timestamp;

	if (!ha->fw_dump) {
		ql4_printk(KERN_INFO, ha, "%s(%ld) No buffer to dump\n",
			   __func__, ha->host_no);
		return rval;
	}

	tmplt_hdr = (struct qla4_8xxx_minidump_template_hdr *)
						ha->fw_dump_tmplt_hdr;
	data_ptr = (uint32_t *)((uint8_t *)ha->fw_dump +
						ha->fw_dump_tmplt_size);
	data_collected += ha->fw_dump_tmplt_size;

	num_entry_hdr = tmplt_hdr->num_of_entries;
	ql4_printk(KERN_INFO, ha, "[%s]: starting data ptr: %p\n",
		   __func__, data_ptr);
	ql4_printk(KERN_INFO, ha,
		   "[%s]: no of entry headers in Template: 0x%x\n",
		   __func__, num_entry_hdr);
	ql4_printk(KERN_INFO, ha, "[%s]: Capture Mask obtained: 0x%x\n",
		   __func__, ha->fw_dump_capture_mask);
	ql4_printk(KERN_INFO, ha, "[%s]: Total_data_size 0x%x, %d obtained\n",
		   __func__, ha->fw_dump_size, ha->fw_dump_size);

	/* Update current timestamp before taking dump */
	now = get_jiffies_64();
	timestamp = (u32)(jiffies_to_msecs(now) / 1000);
	tmplt_hdr->driver_timestamp = timestamp;

2397
	entry_hdr = (struct qla8xxx_minidump_entry_hdr *)
2398 2399 2400
					(((uint8_t *)ha->fw_dump_tmplt_hdr) +
					 tmplt_hdr->first_entry_offset);

2401 2402 2403 2404
	if (is_qla8032(ha))
		tmplt_hdr->saved_state_array[QLA83XX_SS_OCM_WNDREG_INDEX] =
					tmplt_hdr->ocm_window_reg[ha->func_num];

2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
	/* Walk through the entry headers - validate/perform required action */
	for (i = 0; i < num_entry_hdr; i++) {
		if (data_collected >= ha->fw_dump_size) {
			ql4_printk(KERN_INFO, ha,
				   "Data collected: [0x%x], Total Dump size: [0x%x]\n",
				   data_collected, ha->fw_dump_size);
			return rval;
		}

		if (!(entry_hdr->d_ctrl.entry_capture_mask &
		      ha->fw_dump_capture_mask)) {
			entry_hdr->d_ctrl.driver_flags |=
2417
						QLA8XXX_DBG_SKIPPED_FLAG;
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
			goto skip_nxt_entry;
		}

		DEBUG2(ql4_printk(KERN_INFO, ha,
				  "Data collected: [0x%x], Dump size left:[0x%x]\n",
				  data_collected,
				  (ha->fw_dump_size - data_collected)));

		/* Decode the entry type and take required action to capture
		 * debug data
		 */
		switch (entry_hdr->entry_type) {
2430
		case QLA8XXX_RDEND:
2431
			qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
2432
			break;
2433
		case QLA8XXX_CNTRL:
2434 2435 2436
			rval = qla4_8xxx_minidump_process_control(ha,
								  entry_hdr);
			if (rval != QLA_SUCCESS) {
2437
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
2438 2439 2440
				goto md_failed;
			}
			break;
2441
		case QLA8XXX_RDCRB:
2442 2443 2444
			qla4_8xxx_minidump_process_rdcrb(ha, entry_hdr,
							 &data_ptr);
			break;
2445
		case QLA8XXX_RDMEM:
2446 2447 2448
			rval = qla4_8xxx_minidump_process_rdmem(ha, entry_hdr,
								&data_ptr);
			if (rval != QLA_SUCCESS) {
2449
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
2450 2451 2452
				goto md_failed;
			}
			break;
2453 2454
		case QLA8XXX_BOARD:
		case QLA8XXX_RDROM:
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
			if (is_qla8022(ha)) {
				qla4_82xx_minidump_process_rdrom(ha, entry_hdr,
								 &data_ptr);
			} else if (is_qla8032(ha)) {
				rval = qla4_83xx_minidump_process_rdrom(ha,
								    entry_hdr,
								    &data_ptr);
				if (rval != QLA_SUCCESS)
					qla4_8xxx_mark_entry_skipped(ha,
								     entry_hdr,
								     i);
			}
2467
			break;
2468 2469 2470 2471
		case QLA8XXX_L2DTG:
		case QLA8XXX_L2ITG:
		case QLA8XXX_L2DAT:
		case QLA8XXX_L2INS:
2472 2473 2474
			rval = qla4_8xxx_minidump_process_l2tag(ha, entry_hdr,
								&data_ptr);
			if (rval != QLA_SUCCESS) {
2475
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
2476 2477 2478
				goto md_failed;
			}
			break;
2479 2480
		case QLA8XXX_L1DTG:
		case QLA8XXX_L1ITG:
2481 2482
		case QLA8XXX_L1DAT:
		case QLA8XXX_L1INS:
2483 2484 2485
			qla4_8xxx_minidump_process_l1cache(ha, entry_hdr,
							   &data_ptr);
			break;
2486
		case QLA8XXX_RDOCM:
2487 2488 2489
			qla4_8xxx_minidump_process_rdocm(ha, entry_hdr,
							 &data_ptr);
			break;
2490
		case QLA8XXX_RDMUX:
2491 2492 2493
			qla4_8xxx_minidump_process_rdmux(ha, entry_hdr,
							 &data_ptr);
			break;
2494
		case QLA8XXX_QUEUE:
2495 2496 2497
			qla4_8xxx_minidump_process_queue(ha, entry_hdr,
							 &data_ptr);
			break;
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
		case QLA83XX_POLLRD:
			if (!is_qla8032(ha)) {
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
				break;
			}
			rval = qla83xx_minidump_process_pollrd(ha, entry_hdr,
							       &data_ptr);
			if (rval != QLA_SUCCESS)
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
			break;
		case QLA83XX_RDMUX2:
			if (!is_qla8032(ha)) {
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
				break;
			}
			qla83xx_minidump_process_rdmux2(ha, entry_hdr,
							&data_ptr);
			break;
		case QLA83XX_POLLRDMWR:
			if (!is_qla8032(ha)) {
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
				break;
			}
			rval = qla83xx_minidump_process_pollrdmwr(ha, entry_hdr,
								  &data_ptr);
			if (rval != QLA_SUCCESS)
				qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
			break;
2526
		case QLA8XXX_RDNOP:
2527
		default:
2528
			qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
2529 2530 2531 2532 2533 2534 2535 2536
			break;
		}

		data_collected = (uint8_t *)data_ptr -
				 ((uint8_t *)((uint8_t *)ha->fw_dump +
						ha->fw_dump_tmplt_size));
skip_nxt_entry:
		/*  next entry in the template */
2537
		entry_hdr = (struct qla8xxx_minidump_entry_hdr *)
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
				(((uint8_t *)entry_hdr) +
				 entry_hdr->entry_size);
	}

	if ((data_collected + ha->fw_dump_tmplt_size) != ha->fw_dump_size) {
		ql4_printk(KERN_INFO, ha,
			   "Dump data mismatch: Data collected: [0x%x], total_data_size:[0x%x]\n",
			   data_collected, ha->fw_dump_size);
		goto md_failed;
	}

	DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s Last entry: 0x%x\n",
			  __func__, i));
md_failed:
	return rval;
}

/**
 * qla4_8xxx_uevent_emit - Send uevent when the firmware dump is ready.
 * @ha: pointer to adapter structure
 **/
static void qla4_8xxx_uevent_emit(struct scsi_qla_host *ha, u32 code)
{
	char event_string[40];
	char *envp[] = { event_string, NULL };

	switch (code) {
	case QL4_UEVENT_CODE_FW_DUMP:
		snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
			 ha->host_no);
		break;
	default:
		/*do nothing*/
		break;
	}

	kobject_uevent_env(&(&ha->pdev->dev)->kobj, KOBJ_CHANGE, envp);
}

2577
void qla4_8xxx_get_minidump(struct scsi_qla_host *ha)
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
{
	if (ql4xenablemd && test_bit(AF_FW_RECOVERY, &ha->flags) &&
	    !test_bit(AF_82XX_FW_DUMPED, &ha->flags)) {
		if (!qla4_8xxx_collect_md_data(ha)) {
			qla4_8xxx_uevent_emit(ha, QL4_UEVENT_CODE_FW_DUMP);
			set_bit(AF_82XX_FW_DUMPED, &ha->flags);
		} else {
			ql4_printk(KERN_INFO, ha, "%s: Unable to collect minidump\n",
				   __func__);
		}
	}
}

2591 2592 2593 2594 2595 2596
/**
 * qla4_8xxx_device_bootstrap - Initialize device, set DEV_READY, start fw
 * @ha: pointer to adapter structure
 *
 * Note: IDC lock must be held upon entry
 **/
2597
int qla4_8xxx_device_bootstrap(struct scsi_qla_host *ha)
2598
{
2599 2600
	int rval = QLA_ERROR;
	int i, timeout;
2601
	uint32_t old_count, count, idc_ctrl;
2602
	int need_reset = 0, peg_stuck = 1;
2603

2604 2605
	need_reset = ha->isp_ops->need_reset(ha);
	old_count = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_ALIVE_COUNTER);
2606 2607 2608 2609

	for (i = 0; i < 10; i++) {
		timeout = msleep_interruptible(200);
		if (timeout) {
2610 2611
			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
					    QLA8XXX_DEV_FAILED);
2612
			return rval;
2613 2614
		}

2615
		count = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_ALIVE_COUNTER);
2616
		if (count != old_count)
2617 2618 2619 2620 2621 2622
			peg_stuck = 0;
	}

	if (need_reset) {
		/* We are trying to perform a recovery here. */
		if (peg_stuck)
2623
			ha->isp_ops->rom_lock_recovery(ha);
2624 2625 2626 2627 2628
		goto dev_initialize;
	} else  {
		/* Start of day for this ha context. */
		if (peg_stuck) {
			/* Either we are the first or recovery in progress. */
2629
			ha->isp_ops->rom_lock_recovery(ha);
2630 2631 2632 2633
			goto dev_initialize;
		} else {
			/* Firmware already running. */
			rval = QLA_SUCCESS;
2634
			goto dev_ready;
2635
		}
2636 2637 2638 2639 2640
	}

dev_initialize:
	/* set to DEV_INITIALIZING */
	ql4_printk(KERN_INFO, ha, "HW State: INITIALIZING\n");
2641 2642
	qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
			    QLA8XXX_DEV_INITIALIZING);
2643

2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
	/*
	 * For ISP8324, if IDC_CTRL GRACEFUL_RESET_BIT1 is set, reset it after
	 * device goes to INIT state.
	 */
	if (is_qla8032(ha)) {
		idc_ctrl = qla4_83xx_rd_reg(ha, QLA83XX_IDC_DRV_CTRL);
		if (idc_ctrl & GRACEFUL_RESET_BIT1) {
			qla4_83xx_wr_reg(ha, QLA83XX_IDC_DRV_CTRL,
					 (idc_ctrl & ~GRACEFUL_RESET_BIT1));
			set_bit(AF_83XX_NO_FW_DUMP, &ha->flags);
2654 2655
		}
	}
2656

2657
	ha->isp_ops->idc_unlock(ha);
2658 2659 2660 2661

	if (is_qla8022(ha))
		qla4_8xxx_get_minidump(ha);

2662 2663
	rval = ha->isp_ops->restart_firmware(ha);
	ha->isp_ops->idc_lock(ha);
2664 2665 2666 2667

	if (rval != QLA_SUCCESS) {
		ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
		qla4_8xxx_clear_drv_active(ha);
2668 2669
		qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
				    QLA8XXX_DEV_FAILED);
2670 2671 2672 2673 2674
		return rval;
	}

dev_ready:
	ql4_printk(KERN_INFO, ha, "HW State: READY\n");
2675
	qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE, QLA8XXX_DEV_READY);
2676

2677
	return rval;
2678 2679 2680
}

/**
2681
 * qla4_82xx_need_reset_handler - Code to start reset sequence
2682 2683 2684 2685 2686
 * @ha: pointer to adapter structure
 *
 * Note: IDC lock must be held upon entry
 **/
static void
2687
qla4_82xx_need_reset_handler(struct scsi_qla_host *ha)
2688 2689
{
	uint32_t dev_state, drv_state, drv_active;
2690
	uint32_t active_mask = 0xFFFFFFFF;
2691 2692 2693 2694 2695 2696
	unsigned long reset_timeout;

	ql4_printk(KERN_INFO, ha,
		"Performing ISP error recovery\n");

	if (test_and_clear_bit(AF_ONLINE, &ha->flags)) {
2697
		qla4_82xx_idc_unlock(ha);
2698
		ha->isp_ops->disable_intrs(ha);
2699
		qla4_82xx_idc_lock(ha);
2700 2701
	}

2702
	if (!test_bit(AF_8XXX_RST_OWNER, &ha->flags)) {
2703 2704 2705 2706 2707 2708 2709
		DEBUG2(ql4_printk(KERN_INFO, ha,
				  "%s(%ld): reset acknowledged\n",
				  __func__, ha->host_no));
		qla4_8xxx_set_rst_ready(ha);
	} else {
		active_mask = (~(1 << (ha->func_num * 4)));
	}
2710 2711 2712 2713

	/* wait for 10 seconds for reset ack from all functions */
	reset_timeout = jiffies + (ha->nx_reset_timeout * HZ);

2714 2715
	drv_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
	drv_active = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2716 2717 2718 2719 2720

	ql4_printk(KERN_INFO, ha,
		"%s(%ld): drv_state = 0x%x, drv_active = 0x%x\n",
		__func__, ha->host_no, drv_state, drv_active);

2721
	while (drv_state != (drv_active & active_mask)) {
2722
		if (time_after_eq(jiffies, reset_timeout)) {
2723 2724 2725
			ql4_printk(KERN_INFO, ha,
				   "%s: RESET TIMEOUT! drv_state: 0x%08x, drv_active: 0x%08x\n",
				   DRIVER_NAME, drv_state, drv_active);
2726 2727 2728
			break;
		}

2729 2730 2731 2732
		/*
		 * When reset_owner times out, check which functions
		 * acked/did not ack
		 */
2733
		if (test_bit(AF_8XXX_RST_OWNER, &ha->flags)) {
2734 2735 2736 2737 2738
			ql4_printk(KERN_INFO, ha,
				   "%s(%ld): drv_state = 0x%x, drv_active = 0x%x\n",
				   __func__, ha->host_no, drv_state,
				   drv_active);
		}
2739
		qla4_82xx_idc_unlock(ha);
2740
		msleep(1000);
2741
		qla4_82xx_idc_lock(ha);
2742

2743 2744
		drv_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
		drv_active = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2745 2746
	}

2747
	/* Clear RESET OWNER as we are not going to use it any further */
2748
	clear_bit(AF_8XXX_RST_OWNER, &ha->flags);
2749

2750
	dev_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2751 2752
	ql4_printk(KERN_INFO, ha, "Device state is 0x%x = %s\n", dev_state,
		   dev_state < MAX_STATES ? qdev_state[dev_state] : "Unknown");
2753 2754

	/* Force to DEV_COLD unless someone else is starting a reset */
2755
	if (dev_state != QLA8XXX_DEV_INITIALIZING) {
2756
		ql4_printk(KERN_INFO, ha, "HW State: COLD/RE-INIT\n");
2757
		qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_COLD);
2758
		qla4_8xxx_set_rst_ready(ha);
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
	}
}

/**
 * qla4_8xxx_need_qsnt_handler - Code to start qsnt
 * @ha: pointer to adapter structure
 **/
void
qla4_8xxx_need_qsnt_handler(struct scsi_qla_host *ha)
{
2769
	ha->isp_ops->idc_lock(ha);
2770
	qla4_8xxx_set_qsnt_ready(ha);
2771
	ha->isp_ops->idc_unlock(ha);
2772 2773
}

2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
static void qla4_82xx_set_idc_ver(struct scsi_qla_host *ha)
{
	int idc_ver;
	uint32_t drv_active;

	drv_active = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_ACTIVE);
	if (drv_active == (1 << (ha->func_num * 4))) {
		qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_IDC_VERSION,
				    QLA82XX_IDC_VERSION);
		ql4_printk(KERN_INFO, ha,
			   "%s: IDC version updated to %d\n", __func__,
			   QLA82XX_IDC_VERSION);
	} else {
		idc_ver = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_IDC_VERSION);
		if (QLA82XX_IDC_VERSION != idc_ver) {
			ql4_printk(KERN_INFO, ha,
				   "%s: qla4xxx driver IDC version %d is not compatible with IDC version %d of other drivers!\n",
				   __func__, QLA82XX_IDC_VERSION, idc_ver);
		}
	}
}

2796
static int qla4_83xx_set_idc_ver(struct scsi_qla_host *ha)
2797
{
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
	int idc_ver;
	uint32_t drv_active;
	int rval = QLA_SUCCESS;

	drv_active = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_ACTIVE);
	if (drv_active == (1 << ha->func_num)) {
		idc_ver = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_IDC_VERSION);
		idc_ver &= (~0xFF);
		idc_ver |= QLA83XX_IDC_VER_MAJ_VALUE;
		qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_IDC_VERSION, idc_ver);
		ql4_printk(KERN_INFO, ha,
			   "%s: IDC version updated to %d\n", __func__,
2810
			   idc_ver);
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
	} else {
		idc_ver = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_IDC_VERSION);
		idc_ver &= 0xFF;
		if (QLA83XX_IDC_VER_MAJ_VALUE != idc_ver) {
			ql4_printk(KERN_INFO, ha,
				   "%s: qla4xxx driver IDC version %d is not compatible with IDC version %d of other drivers!\n",
				   __func__, QLA83XX_IDC_VER_MAJ_VALUE,
				   idc_ver);
			rval = QLA_ERROR;
			goto exit_set_idc_ver;
		}
	}

	/* Update IDC_MINOR_VERSION */
	idc_ver = qla4_83xx_rd_reg(ha, QLA83XX_CRB_IDC_VER_MINOR);
	idc_ver &= ~(0x03 << (ha->func_num * 2));
	idc_ver |= (QLA83XX_IDC_VER_MIN_VALUE << (ha->func_num * 2));
	qla4_83xx_wr_reg(ha, QLA83XX_CRB_IDC_VER_MINOR, idc_ver);

exit_set_idc_ver:
	return rval;
}

2834
int qla4_8xxx_update_idc_reg(struct scsi_qla_host *ha)
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
{
	uint32_t drv_active;
	int rval = QLA_SUCCESS;

	if (test_bit(AF_INIT_DONE, &ha->flags))
		goto exit_update_idc_reg;

	ha->isp_ops->idc_lock(ha);
	qla4_8xxx_set_drv_active(ha);

	/*
	 * If we are the first driver to load and
	 * ql4xdontresethba is not set, clear IDC_CTRL BIT0.
	 */
	if (is_qla8032(ha)) {
		drv_active = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DRV_ACTIVE);
		if ((drv_active == (1 << ha->func_num)) && !ql4xdontresethba)
			qla4_83xx_clear_idc_dontreset(ha);
	}

	if (is_qla8022(ha)) {
2856
		qla4_82xx_set_idc_ver(ha);
2857 2858 2859 2860
	} else if (is_qla8032(ha)) {
		rval = qla4_83xx_set_idc_ver(ha);
		if (rval == QLA_ERROR)
			qla4_8xxx_clear_drv_active(ha);
2861
	}
2862 2863 2864 2865 2866

	ha->isp_ops->idc_unlock(ha);

exit_update_idc_reg:
	return rval;
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
}

/**
 * qla4_8xxx_device_state_handler - Adapter state machine
 * @ha: pointer to host adapter structure.
 *
 * Note: IDC lock must be UNLOCKED upon entry
 **/
int qla4_8xxx_device_state_handler(struct scsi_qla_host *ha)
{
	uint32_t dev_state;
	int rval = QLA_SUCCESS;
	unsigned long dev_init_timeout;

2881 2882 2883
	rval = qla4_8xxx_update_idc_reg(ha);
	if (rval == QLA_ERROR)
		goto exit_state_handler;
2884

2885
	dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
2886 2887 2888
	DEBUG2(ql4_printk(KERN_INFO, ha, "Device state is 0x%x = %s\n",
			  dev_state, dev_state < MAX_STATES ?
			  qdev_state[dev_state] : "Unknown"));
2889 2890 2891 2892

	/* wait for 30 seconds for device to go ready */
	dev_init_timeout = jiffies + (ha->nx_dev_init_timeout * HZ);

2893
	ha->isp_ops->idc_lock(ha);
2894 2895 2896
	while (1) {

		if (time_after_eq(jiffies, dev_init_timeout)) {
2897 2898 2899 2900 2901
			ql4_printk(KERN_WARNING, ha,
				   "%s: Device Init Failed 0x%x = %s\n",
				   DRIVER_NAME,
				   dev_state, dev_state < MAX_STATES ?
				   qdev_state[dev_state] : "Unknown");
2902 2903
			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
					    QLA8XXX_DEV_FAILED);
2904 2905
		}

2906
		dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
2907 2908 2909
		ql4_printk(KERN_INFO, ha, "Device state is 0x%x = %s\n",
			   dev_state, dev_state < MAX_STATES ?
			   qdev_state[dev_state] : "Unknown");
2910 2911 2912

		/* NOTE: Make sure idc unlocked upon exit of switch statement */
		switch (dev_state) {
2913
		case QLA8XXX_DEV_READY:
2914
			goto exit;
2915
		case QLA8XXX_DEV_COLD:
2916 2917
			rval = qla4_8xxx_device_bootstrap(ha);
			goto exit;
2918
		case QLA8XXX_DEV_INITIALIZING:
2919
			ha->isp_ops->idc_unlock(ha);
2920
			msleep(1000);
2921
			ha->isp_ops->idc_lock(ha);
2922
			break;
2923
		case QLA8XXX_DEV_NEED_RESET:
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
			/*
			 * For ISP8324, if NEED_RESET is set by any driver,
			 * it should be honored, irrespective of IDC_CTRL
			 * DONTRESET_BIT0
			 */
			if (is_qla8032(ha)) {
				qla4_83xx_need_reset_handler(ha);
			} else if (is_qla8022(ha)) {
				if (!ql4xdontresethba) {
					qla4_82xx_need_reset_handler(ha);
					/* Update timeout value after need
					 * reset handler */
					dev_init_timeout = jiffies +
						(ha->nx_dev_init_timeout * HZ);
				} else {
					ha->isp_ops->idc_unlock(ha);
					msleep(1000);
					ha->isp_ops->idc_lock(ha);
				}
2943 2944
			}
			break;
2945
		case QLA8XXX_DEV_NEED_QUIESCENT:
2946 2947
			/* idc locked/unlocked in handler */
			qla4_8xxx_need_qsnt_handler(ha);
2948
			break;
2949
		case QLA8XXX_DEV_QUIESCENT:
2950
			ha->isp_ops->idc_unlock(ha);
2951
			msleep(1000);
2952
			ha->isp_ops->idc_lock(ha);
2953
			break;
2954
		case QLA8XXX_DEV_FAILED:
2955
			ha->isp_ops->idc_unlock(ha);
2956 2957
			qla4xxx_dead_adapter_cleanup(ha);
			rval = QLA_ERROR;
2958
			ha->isp_ops->idc_lock(ha);
2959 2960
			goto exit;
		default:
2961
			ha->isp_ops->idc_unlock(ha);
2962 2963
			qla4xxx_dead_adapter_cleanup(ha);
			rval = QLA_ERROR;
2964
			ha->isp_ops->idc_lock(ha);
2965 2966 2967 2968
			goto exit;
		}
	}
exit:
2969
	ha->isp_ops->idc_unlock(ha);
2970
exit_state_handler:
2971 2972 2973 2974 2975 2976
	return rval;
}

int qla4_8xxx_load_risc(struct scsi_qla_host *ha)
{
	int retval;
2977 2978

	/* clear the interrupt */
2979 2980 2981 2982 2983 2984 2985
	if (is_qla8032(ha)) {
		writel(0, &ha->qla4_83xx_reg->risc_intr);
		readl(&ha->qla4_83xx_reg->risc_intr);
	} else if (is_qla8022(ha)) {
		writel(0, &ha->qla4_82xx_reg->host_int);
		readl(&ha->qla4_82xx_reg->host_int);
	}
2986

2987 2988
	retval = qla4_8xxx_device_state_handler(ha);

2989
	if (retval == QLA_SUCCESS && !test_bit(AF_IRQ_ATTACHED, &ha->flags))
2990
		retval = qla4xxx_request_irqs(ha);
V
Vikas Chaudhary 已提交
2991

2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018
	return retval;
}

/*****************************************************************************/
/* Flash Manipulation Routines                                               */
/*****************************************************************************/

#define OPTROM_BURST_SIZE       0x1000
#define OPTROM_BURST_DWORDS     (OPTROM_BURST_SIZE / 4)

#define FARX_DATA_FLAG	BIT_31
#define FARX_ACCESS_FLASH_CONF	0x7FFD0000
#define FARX_ACCESS_FLASH_DATA	0x7FF00000

static inline uint32_t
flash_conf_addr(struct ql82xx_hw_data *hw, uint32_t faddr)
{
	return hw->flash_conf_off | faddr;
}

static inline uint32_t
flash_data_addr(struct ql82xx_hw_data *hw, uint32_t faddr)
{
	return hw->flash_data_off | faddr;
}

static uint32_t *
3019
qla4_82xx_read_flash_data(struct scsi_qla_host *ha, uint32_t *dwptr,
3020 3021 3022 3023 3024
    uint32_t faddr, uint32_t length)
{
	uint32_t i;
	uint32_t val;
	int loops = 0;
3025
	while ((qla4_82xx_rom_lock(ha) != 0) && (loops < 50000)) {
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
		udelay(100);
		cond_resched();
		loops++;
	}
	if (loops >= 50000) {
		ql4_printk(KERN_WARNING, ha, "ROM lock failed\n");
		return dwptr;
	}

	/* Dword reads to flash. */
	for (i = 0; i < length/4; i++, faddr += 4) {
3037
		if (qla4_82xx_do_rom_fast_read(ha, faddr, &val)) {
3038 3039 3040 3041 3042 3043 3044 3045
			ql4_printk(KERN_WARNING, ha,
			    "Do ROM fast read failed\n");
			goto done_read;
		}
		dwptr[i] = __constant_cpu_to_le32(val);
	}

done_read:
3046
	qla4_82xx_rom_unlock(ha);
3047 3048 3049 3050 3051 3052 3053
	return dwptr;
}

/**
 * Address and length are byte address
 **/
static uint8_t *
3054
qla4_82xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
3055 3056
		uint32_t offset, uint32_t length)
{
3057
	qla4_82xx_read_flash_data(ha, (uint32_t *)buf, offset, length);
3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
	return buf;
}

static int
qla4_8xxx_find_flt_start(struct scsi_qla_host *ha, uint32_t *start)
{
	const char *loc, *locations[] = { "DEF", "PCI" };

	/*
	 * FLT-location structure resides after the last PCI region.
	 */

	/* Begin with sane defaults. */
	loc = locations[0];
	*start = FA_FLASH_LAYOUT_ADDR_82;

	DEBUG2(ql4_printk(KERN_INFO, ha, "FLTL[%s] = 0x%x.\n", loc, *start));
	return QLA_SUCCESS;
}

static void
qla4_8xxx_get_flt_info(struct scsi_qla_host *ha, uint32_t flt_addr)
{
	const char *loc, *locations[] = { "DEF", "FLT" };
	uint16_t *wptr;
	uint16_t cnt, chksum;
3084
	uint32_t start, status;
3085 3086 3087 3088 3089 3090 3091 3092
	struct qla_flt_header *flt;
	struct qla_flt_region *region;
	struct ql82xx_hw_data *hw = &ha->hw;

	hw->flt_region_flt = flt_addr;
	wptr = (uint16_t *)ha->request_ring;
	flt = (struct qla_flt_header *)ha->request_ring;
	region = (struct qla_flt_region *)&flt[1];
3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104

	if (is_qla8022(ha)) {
		qla4_82xx_read_optrom_data(ha, (uint8_t *)ha->request_ring,
					   flt_addr << 2, OPTROM_BURST_SIZE);
	} else if (is_qla8032(ha)) {
		status = qla4_83xx_flash_read_u32(ha, flt_addr << 2,
						  (uint8_t *)ha->request_ring,
						  0x400);
		if (status != QLA_SUCCESS)
			goto no_flash_data;
	}

3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
	if (*wptr == __constant_cpu_to_le16(0xffff))
		goto no_flash_data;
	if (flt->version != __constant_cpu_to_le16(1)) {
		DEBUG2(ql4_printk(KERN_INFO, ha, "Unsupported FLT detected: "
			"version=0x%x length=0x%x checksum=0x%x.\n",
			le16_to_cpu(flt->version), le16_to_cpu(flt->length),
			le16_to_cpu(flt->checksum)));
		goto no_flash_data;
	}

	cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
	for (chksum = 0; cnt; cnt--)
		chksum += le16_to_cpu(*wptr++);
	if (chksum) {
		DEBUG2(ql4_printk(KERN_INFO, ha, "Inconsistent FLT detected: "
			"version=0x%x length=0x%x checksum=0x%x.\n",
			le16_to_cpu(flt->version), le16_to_cpu(flt->length),
			chksum));
		goto no_flash_data;
	}

	loc = locations[1];
	cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
	for ( ; cnt; cnt--, region++) {
		/* Store addresses as DWORD offsets. */
		start = le32_to_cpu(region->start) >> 2;

		DEBUG3(ql4_printk(KERN_DEBUG, ha, "FLT[%02x]: start=0x%x "
		    "end=0x%x size=0x%x.\n", le32_to_cpu(region->code), start,
		    le32_to_cpu(region->end) >> 2, le32_to_cpu(region->size)));

		switch (le32_to_cpu(region->code) & 0xff) {
		case FLT_REG_FDT:
			hw->flt_region_fdt = start;
			break;
		case FLT_REG_BOOT_CODE_82:
			hw->flt_region_boot = start;
			break;
		case FLT_REG_FW_82:
3144
		case FLT_REG_FW_82_1:
3145 3146 3147 3148 3149
			hw->flt_region_fw = start;
			break;
		case FLT_REG_BOOTLOAD_82:
			hw->flt_region_bootload = start;
			break;
3150 3151 3152
		case FLT_REG_ISCSI_PARAM:
			hw->flt_iscsi_param =  start;
			break;
3153 3154 3155 3156
		case FLT_REG_ISCSI_CHAP:
			hw->flt_region_chap =  start;
			hw->flt_chap_size =  le32_to_cpu(region->size);
			break;
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
		}
	}
	goto done;

no_flash_data:
	/* Use hardcoded defaults. */
	loc = locations[0];

	hw->flt_region_fdt      = FA_FLASH_DESCR_ADDR_82;
	hw->flt_region_boot     = FA_BOOT_CODE_ADDR_82;
	hw->flt_region_bootload = FA_BOOT_LOAD_ADDR_82;
	hw->flt_region_fw       = FA_RISC_CODE_ADDR_82;
3169 3170 3171
	hw->flt_region_chap	= FA_FLASH_ISCSI_CHAP;
	hw->flt_chap_size	= FA_FLASH_CHAP_SIZE;

3172 3173 3174 3175 3176 3177 3178 3179
done:
	DEBUG2(ql4_printk(KERN_INFO, ha, "FLT[%s]: flt=0x%x fdt=0x%x "
	    "boot=0x%x bootload=0x%x fw=0x%x\n", loc, hw->flt_region_flt,
	    hw->flt_region_fdt,	hw->flt_region_boot, hw->flt_region_bootload,
	    hw->flt_region_fw));
}

static void
3180
qla4_82xx_get_fdt_info(struct scsi_qla_host *ha)
3181 3182 3183 3184 3185 3186 3187 3188
{
#define FLASH_BLK_SIZE_4K       0x1000
#define FLASH_BLK_SIZE_32K      0x8000
#define FLASH_BLK_SIZE_64K      0x10000
	const char *loc, *locations[] = { "MID", "FDT" };
	uint16_t cnt, chksum;
	uint16_t *wptr;
	struct qla_fdt_layout *fdt;
3189 3190
	uint16_t mid = 0;
	uint16_t fid = 0;
3191 3192 3193 3194 3195 3196 3197
	struct ql82xx_hw_data *hw = &ha->hw;

	hw->flash_conf_off = FARX_ACCESS_FLASH_CONF;
	hw->flash_data_off = FARX_ACCESS_FLASH_DATA;

	wptr = (uint16_t *)ha->request_ring;
	fdt = (struct qla_fdt_layout *)ha->request_ring;
3198
	qla4_82xx_read_optrom_data(ha, (uint8_t *)ha->request_ring,
3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246
	    hw->flt_region_fdt << 2, OPTROM_BURST_SIZE);

	if (*wptr == __constant_cpu_to_le16(0xffff))
		goto no_flash_data;

	if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
	    fdt->sig[3] != 'D')
		goto no_flash_data;

	for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
	    cnt++)
		chksum += le16_to_cpu(*wptr++);

	if (chksum) {
		DEBUG2(ql4_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
		    "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
		    le16_to_cpu(fdt->version)));
		goto no_flash_data;
	}

	loc = locations[1];
	mid = le16_to_cpu(fdt->man_id);
	fid = le16_to_cpu(fdt->id);
	hw->fdt_wrt_disable = fdt->wrt_disable_bits;
	hw->fdt_erase_cmd = flash_conf_addr(hw, 0x0300 | fdt->erase_cmd);
	hw->fdt_block_size = le32_to_cpu(fdt->block_size);

	if (fdt->unprotect_sec_cmd) {
		hw->fdt_unprotect_sec_cmd = flash_conf_addr(hw, 0x0300 |
		    fdt->unprotect_sec_cmd);
		hw->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
		    flash_conf_addr(hw, 0x0300 | fdt->protect_sec_cmd) :
		    flash_conf_addr(hw, 0x0336);
	}
	goto done;

no_flash_data:
	loc = locations[0];
	hw->fdt_block_size = FLASH_BLK_SIZE_64K;
done:
	DEBUG2(ql4_printk(KERN_INFO, ha, "FDT[%s]: (0x%x/0x%x) erase=0x%x "
		"pro=%x upro=%x wrtd=0x%x blk=0x%x.\n", loc, mid, fid,
		hw->fdt_erase_cmd, hw->fdt_protect_sec_cmd,
		hw->fdt_unprotect_sec_cmd, hw->fdt_wrt_disable,
		hw->fdt_block_size));
}

static void
3247
qla4_82xx_get_idc_param(struct scsi_qla_host *ha)
3248 3249 3250 3251 3252 3253 3254
{
#define QLA82XX_IDC_PARAM_ADDR      0x003e885c
	uint32_t *wptr;

	if (!is_qla8022(ha))
		return;
	wptr = (uint32_t *)ha->request_ring;
3255
	qla4_82xx_read_optrom_data(ha, (uint8_t *)ha->request_ring,
3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272
			QLA82XX_IDC_PARAM_ADDR , 8);

	if (*wptr == __constant_cpu_to_le32(0xffffffff)) {
		ha->nx_dev_init_timeout = ROM_DEV_INIT_TIMEOUT;
		ha->nx_reset_timeout = ROM_DRV_RESET_ACK_TIMEOUT;
	} else {
		ha->nx_dev_init_timeout = le32_to_cpu(*wptr++);
		ha->nx_reset_timeout = le32_to_cpu(*wptr);
	}

	DEBUG2(ql4_printk(KERN_DEBUG, ha,
		"ha->nx_dev_init_timeout = %d\n", ha->nx_dev_init_timeout));
	DEBUG2(ql4_printk(KERN_DEBUG, ha,
		"ha->nx_reset_timeout = %d\n", ha->nx_reset_timeout));
	return;
}

3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303
void qla4_82xx_queue_mbox_cmd(struct scsi_qla_host *ha, uint32_t *mbx_cmd,
			      int in_count)
{
	int i;

	/* Load all mailbox registers, except mailbox 0. */
	for (i = 1; i < in_count; i++)
		writel(mbx_cmd[i], &ha->qla4_82xx_reg->mailbox_in[i]);

	/* Wakeup firmware  */
	writel(mbx_cmd[0], &ha->qla4_82xx_reg->mailbox_in[0]);
	readl(&ha->qla4_82xx_reg->mailbox_in[0]);
	writel(HINT_MBX_INT_PENDING, &ha->qla4_82xx_reg->hint);
	readl(&ha->qla4_82xx_reg->hint);
}

void qla4_82xx_process_mbox_intr(struct scsi_qla_host *ha, int out_count)
{
	int intr_status;

	intr_status = readl(&ha->qla4_82xx_reg->host_int);
	if (intr_status & ISRX_82XX_RISC_INT) {
		ha->mbox_status_count = out_count;
		intr_status = readl(&ha->qla4_82xx_reg->host_status);
		ha->isp_ops->interrupt_service_routine(ha, intr_status);

		if (test_bit(AF_INTERRUPTS_ON, &ha->flags) &&
		    test_bit(AF_INTx_ENABLED, &ha->flags))
			qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg,
					0xfbff);
	}
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316
}

int
qla4_8xxx_get_flash_info(struct scsi_qla_host *ha)
{
	int ret;
	uint32_t flt_addr;

	ret = qla4_8xxx_find_flt_start(ha, &flt_addr);
	if (ret != QLA_SUCCESS)
		return ret;

	qla4_8xxx_get_flt_info(ha, flt_addr);
3317 3318 3319 3320 3321 3322
	if (is_qla8022(ha)) {
		qla4_82xx_get_fdt_info(ha);
		qla4_82xx_get_idc_param(ha);
	} else if (is_qla8032(ha)) {
		qla4_83xx_get_idc_param(ha);
	}
3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355

	return QLA_SUCCESS;
}

/**
 * qla4_8xxx_stop_firmware - stops firmware on specified adapter instance
 * @ha: pointer to host adapter structure.
 *
 * Remarks:
 * For iSCSI, throws away all I/O and AENs into bit bucket, so they will
 * not be available after successful return.  Driver must cleanup potential
 * outstanding I/O's after calling this funcion.
 **/
int
qla4_8xxx_stop_firmware(struct scsi_qla_host *ha)
{
	int status;
	uint32_t mbox_cmd[MBOX_REG_COUNT];
	uint32_t mbox_sts[MBOX_REG_COUNT];

	memset(&mbox_cmd, 0, sizeof(mbox_cmd));
	memset(&mbox_sts, 0, sizeof(mbox_sts));

	mbox_cmd[0] = MBOX_CMD_STOP_FW;
	status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1,
	    &mbox_cmd[0], &mbox_sts[0]);

	DEBUG2(printk("scsi%ld: %s: status = %d\n", ha->host_no,
	    __func__, status));
	return status;
}

/**
3356
 * qla4_82xx_isp_reset - Resets ISP and aborts all outstanding commands.
3357 3358 3359
 * @ha: pointer to host adapter structure.
 **/
int
3360
qla4_82xx_isp_reset(struct scsi_qla_host *ha)
3361 3362 3363 3364
{
	int rval;
	uint32_t dev_state;

3365 3366
	qla4_82xx_idc_lock(ha);
	dev_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
3367

3368
	if (dev_state == QLA8XXX_DEV_READY) {
3369
		ql4_printk(KERN_INFO, ha, "HW State: NEED RESET\n");
3370
		qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3371 3372
		    QLA8XXX_DEV_NEED_RESET);
		set_bit(AF_8XXX_RST_OWNER, &ha->flags);
3373 3374 3375
	} else
		ql4_printk(KERN_INFO, ha, "HW State: DEVICE INITIALIZING\n");

3376
	qla4_82xx_idc_unlock(ha);
3377 3378 3379

	rval = qla4_8xxx_device_state_handler(ha);

3380
	qla4_82xx_idc_lock(ha);
3381
	qla4_8xxx_clear_rst_ready(ha);
3382
	qla4_82xx_idc_unlock(ha);
3383

3384
	if (rval == QLA_SUCCESS) {
3385
		ql4_printk(KERN_INFO, ha, "Clearing AF_RECOVERY in qla4_82xx_isp_reset\n");
3386
		clear_bit(AF_FW_RECOVERY, &ha->flags);
3387
	}
3388

3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
	return rval;
}

/**
 * qla4_8xxx_get_sys_info - get adapter MAC address(es) and serial number
 * @ha: pointer to host adapter structure.
 *
 **/
int qla4_8xxx_get_sys_info(struct scsi_qla_host *ha)
{
	uint32_t mbox_cmd[MBOX_REG_COUNT];
	uint32_t mbox_sts[MBOX_REG_COUNT];
	struct mbx_sys_info *sys_info;
	dma_addr_t sys_info_dma;
	int status = QLA_ERROR;

	sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
				      &sys_info_dma, GFP_KERNEL);
	if (sys_info == NULL) {
		DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
		    ha->host_no, __func__));
		return status;
	}

	memset(sys_info, 0, sizeof(*sys_info));
	memset(&mbox_cmd, 0, sizeof(mbox_cmd));
	memset(&mbox_sts, 0, sizeof(mbox_sts));

	mbox_cmd[0] = MBOX_CMD_GET_SYS_INFO;
	mbox_cmd[1] = LSDW(sys_info_dma);
	mbox_cmd[2] = MSDW(sys_info_dma);
	mbox_cmd[4] = sizeof(*sys_info);

	if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 6, &mbox_cmd[0],
	    &mbox_sts[0]) != QLA_SUCCESS) {
		DEBUG2(printk("scsi%ld: %s: GET_SYS_INFO failed\n",
		    ha->host_no, __func__));
		goto exit_validate_mac82;
	}

3429
	/* Make sure we receive the minimum required data to cache internally */
3430 3431
	if ((is_qla8032(ha) ? mbox_sts[3] : mbox_sts[4]) <
	    offsetof(struct mbx_sys_info, reserved)) {
3432 3433 3434 3435 3436 3437
		DEBUG2(printk("scsi%ld: %s: GET_SYS_INFO data receive"
		    " error (%x)\n", ha->host_no, __func__, mbox_sts[4]));
		goto exit_validate_mac82;
	}

	/* Save M.A.C. address & serial_number */
3438
	ha->port_num = sys_info->port_num;
3439 3440 3441 3442
	memcpy(ha->my_mac, &sys_info->mac_addr[0],
	    min(sizeof(ha->my_mac), sizeof(sys_info->mac_addr)));
	memcpy(ha->serial_number, &sys_info->serial_number,
	    min(sizeof(ha->serial_number), sizeof(sys_info->serial_number)));
3443 3444 3445 3446 3447
	memcpy(ha->model_name, &sys_info->board_id_str,
	       min(sizeof(ha->model_name), sizeof(sys_info->board_id_str)));
	ha->phy_port_cnt = sys_info->phys_port_cnt;
	ha->phy_port_num = sys_info->port_num;
	ha->iscsi_pci_func_cnt = sys_info->iscsi_pci_func_cnt;
3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465

	DEBUG2(printk("scsi%ld: %s: "
	    "mac %02x:%02x:%02x:%02x:%02x:%02x "
	    "serial %s\n", ha->host_no, __func__,
	    ha->my_mac[0], ha->my_mac[1], ha->my_mac[2],
	    ha->my_mac[3], ha->my_mac[4], ha->my_mac[5],
	    ha->serial_number));

	status = QLA_SUCCESS;

exit_validate_mac82:
	dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
			  sys_info_dma);
	return status;
}

/* Interrupt handling helpers. */

3466
int qla4_8xxx_intr_enable(struct scsi_qla_host *ha)
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
{
	uint32_t mbox_cmd[MBOX_REG_COUNT];
	uint32_t mbox_sts[MBOX_REG_COUNT];

	DEBUG2(ql4_printk(KERN_INFO, ha, "%s\n", __func__));

	memset(&mbox_cmd, 0, sizeof(mbox_cmd));
	memset(&mbox_sts, 0, sizeof(mbox_sts));
	mbox_cmd[0] = MBOX_CMD_ENABLE_INTRS;
	mbox_cmd[1] = INTR_ENABLE;
	if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
		&mbox_sts[0]) != QLA_SUCCESS) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
		    "%s: MBOX_CMD_ENABLE_INTRS failed (0x%04x)\n",
		    __func__, mbox_sts[0]));
		return QLA_ERROR;
	}
	return QLA_SUCCESS;
}

3487
int qla4_8xxx_intr_disable(struct scsi_qla_host *ha)
3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509
{
	uint32_t mbox_cmd[MBOX_REG_COUNT];
	uint32_t mbox_sts[MBOX_REG_COUNT];

	DEBUG2(ql4_printk(KERN_INFO, ha, "%s\n", __func__));

	memset(&mbox_cmd, 0, sizeof(mbox_cmd));
	memset(&mbox_sts, 0, sizeof(mbox_sts));
	mbox_cmd[0] = MBOX_CMD_ENABLE_INTRS;
	mbox_cmd[1] = INTR_DISABLE;
	if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
	    &mbox_sts[0]) != QLA_SUCCESS) {
		DEBUG2(ql4_printk(KERN_INFO, ha,
			"%s: MBOX_CMD_ENABLE_INTRS failed (0x%04x)\n",
			__func__, mbox_sts[0]));
		return QLA_ERROR;
	}

	return QLA_SUCCESS;
}

void
3510
qla4_82xx_enable_intrs(struct scsi_qla_host *ha)
3511
{
3512
	qla4_8xxx_intr_enable(ha);
3513 3514 3515

	spin_lock_irq(&ha->hardware_lock);
	/* BIT 10 - reset */
3516
	qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
3517 3518 3519 3520 3521
	spin_unlock_irq(&ha->hardware_lock);
	set_bit(AF_INTERRUPTS_ON, &ha->flags);
}

void
3522
qla4_82xx_disable_intrs(struct scsi_qla_host *ha)
3523
{
3524
	if (test_and_clear_bit(AF_INTERRUPTS_ON, &ha->flags))
3525
		qla4_8xxx_intr_disable(ha);
3526 3527 3528

	spin_lock_irq(&ha->hardware_lock);
	/* BIT 10 - set */
3529
	qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0x0400);
3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606
	spin_unlock_irq(&ha->hardware_lock);
}

struct ql4_init_msix_entry {
	uint16_t entry;
	uint16_t index;
	const char *name;
	irq_handler_t handler;
};

static struct ql4_init_msix_entry qla4_8xxx_msix_entries[QLA_MSIX_ENTRIES] = {
	{ QLA_MSIX_DEFAULT, QLA_MIDX_DEFAULT,
	    "qla4xxx (default)",
	    (irq_handler_t)qla4_8xxx_default_intr_handler },
	{ QLA_MSIX_RSP_Q, QLA_MIDX_RSP_Q,
	    "qla4xxx (rsp_q)", (irq_handler_t)qla4_8xxx_msix_rsp_q },
};

void
qla4_8xxx_disable_msix(struct scsi_qla_host *ha)
{
	int i;
	struct ql4_msix_entry *qentry;

	for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
		qentry = &ha->msix_entries[qla4_8xxx_msix_entries[i].index];
		if (qentry->have_irq) {
			free_irq(qentry->msix_vector, ha);
			DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %s\n",
				__func__, qla4_8xxx_msix_entries[i].name));
		}
	}
	pci_disable_msix(ha->pdev);
	clear_bit(AF_MSIX_ENABLED, &ha->flags);
}

int
qla4_8xxx_enable_msix(struct scsi_qla_host *ha)
{
	int i, ret;
	struct msix_entry entries[QLA_MSIX_ENTRIES];
	struct ql4_msix_entry *qentry;

	for (i = 0; i < QLA_MSIX_ENTRIES; i++)
		entries[i].entry = qla4_8xxx_msix_entries[i].entry;

	ret = pci_enable_msix(ha->pdev, entries, ARRAY_SIZE(entries));
	if (ret) {
		ql4_printk(KERN_WARNING, ha,
		    "MSI-X: Failed to enable support -- %d/%d\n",
		    QLA_MSIX_ENTRIES, ret);
		goto msix_out;
	}
	set_bit(AF_MSIX_ENABLED, &ha->flags);

	for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
		qentry = &ha->msix_entries[qla4_8xxx_msix_entries[i].index];
		qentry->msix_vector = entries[i].vector;
		qentry->msix_entry = entries[i].entry;
		qentry->have_irq = 0;
		ret = request_irq(qentry->msix_vector,
		    qla4_8xxx_msix_entries[i].handler, 0,
		    qla4_8xxx_msix_entries[i].name, ha);
		if (ret) {
			ql4_printk(KERN_WARNING, ha,
			    "MSI-X: Unable to register handler -- %x/%d.\n",
			    qla4_8xxx_msix_entries[i].index, ret);
			qla4_8xxx_disable_msix(ha);
			goto msix_out;
		}
		qentry->have_irq = 1;
		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %s\n",
			__func__, qla4_8xxx_msix_entries[i].name));
	}
msix_out:
	return ret;
}