pm80xx_hwi.c 160.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * PMC-Sierra SPCv/ve 8088/8089 SAS/SATA based host adapters driver
 *
 * Copyright (c) 2008-2009 PMC-Sierra, Inc.,
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions, and the following disclaimer,
 * without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 * substantially similar to the "NO WARRANTY" disclaimer below
 * ("Disclaimer") and any redistribution must be conditioned upon
 * including a substantially similar Disclaimer requirement for further
 * binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 * of any contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 */
 #include <linux/slab.h>
 #include "pm8001_sas.h"
 #include "pm80xx_hwi.h"
 #include "pm8001_chips.h"
 #include "pm8001_ctl.h"

#define SMP_DIRECT 1
#define SMP_INDIRECT 2
48 49 50 51 52 53 54 55 56 57 58 59 60


int pm80xx_bar4_shift(struct pm8001_hba_info *pm8001_ha, u32 shift_value)
{
	u32 reg_val;
	unsigned long start;
	pm8001_cw32(pm8001_ha, 0, MEMBASE_II_SHIFT_REGISTER, shift_value);
	/* confirm the setting is written */
	start = jiffies + HZ; /* 1 sec */
	do {
		reg_val = pm8001_cr32(pm8001_ha, 0, MEMBASE_II_SHIFT_REGISTER);
	} while ((reg_val != shift_value) && time_before(jiffies, start));
	if (reg_val != shift_value) {
61 62
		pm8001_dbg(pm8001_ha, FAIL, "TIMEOUT:MEMBASE_II_SHIFT_REGISTER = 0x%x\n",
			   reg_val);
63 64 65 66 67
		return -1;
	}
	return 0;
}

68
static void pm80xx_pci_mem_copy(struct pm8001_hba_info  *pm8001_ha, u32 soffset,
69 70 71 72 73 74 75 76
				const void *destination,
				u32 dw_count, u32 bus_base_number)
{
	u32 index, value, offset;
	u32 *destination1;
	destination1 = (u32 *)destination;

	for (index = 0; index < dw_count; index += 4, destination1++) {
77
		offset = (soffset + index);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		if (offset < (64 * 1024)) {
			value = pm8001_cr32(pm8001_ha, bus_base_number, offset);
			*destination1 =  cpu_to_le32(value);
		}
	}
	return;
}

ssize_t pm80xx_get_fatal_dump(struct device *cdev,
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
	void __iomem *fatal_table_address = pm8001_ha->fatal_tbl_addr;
	u32 accum_len , reg_val, index, *temp;
94
	u32 status = 1;
95 96 97
	unsigned long start;
	u8 *direct_data;
	char *fatal_error_data = buf;
98 99
	u32 length_to_read;
	u32 offset;
100 101 102 103 104 105 106 107 108

	pm8001_ha->forensic_info.data_buf.direct_data = buf;
	if (pm8001_ha->chip_id == chip_8001) {
		pm8001_ha->forensic_info.data_buf.direct_data +=
			sprintf(pm8001_ha->forensic_info.data_buf.direct_data,
			"Not supported for SPC controller");
		return (char *)pm8001_ha->forensic_info.data_buf.direct_data -
			(char *)buf;
	}
109
	/* initialize variables for very first call from host application */
110
	if (pm8001_ha->forensic_info.data_buf.direct_offset == 0) {
111 112
		pm8001_dbg(pm8001_ha, IO,
			   "forensic_info TYPE_NON_FATAL..............\n");
113 114 115
		direct_data = (u8 *)fatal_error_data;
		pm8001_ha->forensic_info.data_type = TYPE_NON_FATAL;
		pm8001_ha->forensic_info.data_buf.direct_len = SYSFS_OFFSET;
116
		pm8001_ha->forensic_info.data_buf.direct_offset = 0;
117
		pm8001_ha->forensic_info.data_buf.read_len = 0;
118
		pm8001_ha->forensic_preserved_accumulated_transfer = 0;
119

120 121 122
		/* Write signature to fatal dump table */
		pm8001_mw32(fatal_table_address,
				MPI_FATAL_EDUMP_TABLE_SIGNATURE, 0x1234abcd);
123

124
		pm8001_ha->forensic_info.data_buf.direct_data = direct_data;
125 126 127 128 129 130 131
		pm8001_dbg(pm8001_ha, IO, "ossaHwCB: status1 %d\n", status);
		pm8001_dbg(pm8001_ha, IO, "ossaHwCB: read_len 0x%x\n",
			   pm8001_ha->forensic_info.data_buf.read_len);
		pm8001_dbg(pm8001_ha, IO, "ossaHwCB: direct_len 0x%x\n",
			   pm8001_ha->forensic_info.data_buf.direct_len);
		pm8001_dbg(pm8001_ha, IO, "ossaHwCB: direct_offset 0x%x\n",
			   pm8001_ha->forensic_info.data_buf.direct_offset);
132 133
	}
	if (pm8001_ha->forensic_info.data_buf.direct_offset == 0) {
134 135 136 137 138 139 140 141
		/* start to get data */
		/* Program the MEMBASE II Shifting Register with 0x00.*/
		pm8001_cw32(pm8001_ha, 0, MEMBASE_II_SHIFT_REGISTER,
				pm8001_ha->fatal_forensic_shift_offset);
		pm8001_ha->forensic_last_offset = 0;
		pm8001_ha->forensic_fatal_step = 0;
		pm8001_ha->fatal_bar_loc = 0;
	}
142

143 144 145
	/* Read until accum_len is retrived */
	accum_len = pm8001_mr32(fatal_table_address,
				MPI_FATAL_EDUMP_TABLE_ACCUM_LEN);
146 147 148 149 150
	/* Determine length of data between previously stored transfer length
	 * and current accumulated transfer length
	 */
	length_to_read =
		accum_len - pm8001_ha->forensic_preserved_accumulated_transfer;
151 152 153 154 155 156 157 158 159 160 161 162
	pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv: accum_len 0x%x\n",
		   accum_len);
	pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv: length_to_read 0x%x\n",
		   length_to_read);
	pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv: last_offset 0x%x\n",
		   pm8001_ha->forensic_last_offset);
	pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv: read_len 0x%x\n",
		   pm8001_ha->forensic_info.data_buf.read_len);
	pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv:: direct_len 0x%x\n",
		   pm8001_ha->forensic_info.data_buf.direct_len);
	pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv:: direct_offset 0x%x\n",
		   pm8001_ha->forensic_info.data_buf.direct_offset);
163 164

	/* If accumulated length failed to read correctly fail the attempt.*/
165
	if (accum_len == 0xFFFFFFFF) {
166 167 168
		pm8001_dbg(pm8001_ha, IO,
			   "Possible PCI issue 0x%x not expected\n",
			   accum_len);
169
		return status;
170
	}
171 172
	/* If accumulated length is zero fail the attempt */
	if (accum_len == 0) {
173 174
		pm8001_ha->forensic_info.data_buf.direct_data +=
			sprintf(pm8001_ha->forensic_info.data_buf.direct_data,
175
			"%08x ", 0xFFFFFFFF);
176 177 178
		return (char *)pm8001_ha->forensic_info.data_buf.direct_data -
			(char *)buf;
	}
179
	/* Accumulated length is good so start capturing the first data */
180 181 182
	temp = (u32 *)pm8001_ha->memoryMap.region[FORENSIC_MEM].virt_ptr;
	if (pm8001_ha->forensic_fatal_step == 0) {
moreData:
183 184 185 186 187 188 189 190 191 192 193 194
		/* If data to read is less than SYSFS_OFFSET then reduce the
		 * length of dataLen
		 */
		if (pm8001_ha->forensic_last_offset + SYSFS_OFFSET
				> length_to_read) {
			pm8001_ha->forensic_info.data_buf.direct_len =
				length_to_read -
				pm8001_ha->forensic_last_offset;
		} else {
			pm8001_ha->forensic_info.data_buf.direct_len =
				SYSFS_OFFSET;
		}
195 196
		if (pm8001_ha->forensic_info.data_buf.direct_data) {
			/* Data is in bar, copy to host memory */
197 198 199 200
			pm80xx_pci_mem_copy(pm8001_ha,
			pm8001_ha->fatal_bar_loc,
			pm8001_ha->memoryMap.region[FORENSIC_MEM].virt_ptr,
			pm8001_ha->forensic_info.data_buf.direct_len, 1);
201 202 203 204 205 206 207 208 209 210
		}
		pm8001_ha->fatal_bar_loc +=
			pm8001_ha->forensic_info.data_buf.direct_len;
		pm8001_ha->forensic_info.data_buf.direct_offset +=
			pm8001_ha->forensic_info.data_buf.direct_len;
		pm8001_ha->forensic_last_offset	+=
			pm8001_ha->forensic_info.data_buf.direct_len;
		pm8001_ha->forensic_info.data_buf.read_len =
			pm8001_ha->forensic_info.data_buf.direct_len;

211
		if (pm8001_ha->forensic_last_offset  >= length_to_read) {
212 213 214
			pm8001_ha->forensic_info.data_buf.direct_data +=
			sprintf(pm8001_ha->forensic_info.data_buf.direct_data,
				"%08x ", 3);
215 216 217
			for (index = 0; index <
				(pm8001_ha->forensic_info.data_buf.direct_len
				 / 4); index++) {
218
				pm8001_ha->forensic_info.data_buf.direct_data +=
219 220 221
				sprintf(
				pm8001_ha->forensic_info.data_buf.direct_data,
				"%08x ", *(temp + index));
222 223 224 225 226 227
			}

			pm8001_ha->fatal_bar_loc = 0;
			pm8001_ha->forensic_fatal_step = 1;
			pm8001_ha->fatal_forensic_shift_offset = 0;
			pm8001_ha->forensic_last_offset	= 0;
228 229 230 231
			status = 0;
			offset = (int)
			((char *)pm8001_ha->forensic_info.data_buf.direct_data
			- (char *)buf);
232 233
			pm8001_dbg(pm8001_ha, IO,
				   "get_fatal_spcv:return1 0x%x\n", offset);
234 235 236 237 238 239 240 241 242
			return (char *)pm8001_ha->
				forensic_info.data_buf.direct_data -
				(char *)buf;
		}
		if (pm8001_ha->fatal_bar_loc < (64 * 1024)) {
			pm8001_ha->forensic_info.data_buf.direct_data +=
				sprintf(pm8001_ha->
					forensic_info.data_buf.direct_data,
					"%08x ", 2);
243 244 245 246 247
			for (index = 0; index <
				(pm8001_ha->forensic_info.data_buf.direct_len
				 / 4); index++) {
				pm8001_ha->forensic_info.data_buf.direct_data
					+= sprintf(pm8001_ha->
248 249 250
					forensic_info.data_buf.direct_data,
					"%08x ", *(temp + index));
			}
251 252 253 254
			status = 0;
			offset = (int)
			((char *)pm8001_ha->forensic_info.data_buf.direct_data
			- (char *)buf);
255 256
			pm8001_dbg(pm8001_ha, IO,
				   "get_fatal_spcv:return2 0x%x\n", offset);
257 258 259 260 261 262 263 264 265
			return (char *)pm8001_ha->
				forensic_info.data_buf.direct_data -
				(char *)buf;
		}

		/* Increment the MEMBASE II Shifting Register value by 0x100.*/
		pm8001_ha->forensic_info.data_buf.direct_data +=
			sprintf(pm8001_ha->forensic_info.data_buf.direct_data,
				"%08x ", 2);
266 267 268
		for (index = 0; index <
			(pm8001_ha->forensic_info.data_buf.direct_len
			 / 4) ; index++) {
269 270
			pm8001_ha->forensic_info.data_buf.direct_data +=
				sprintf(pm8001_ha->
271 272
				forensic_info.data_buf.direct_data,
				"%08x ", *(temp + index));
273 274 275 276 277
		}
		pm8001_ha->fatal_forensic_shift_offset += 0x100;
		pm8001_cw32(pm8001_ha, 0, MEMBASE_II_SHIFT_REGISTER,
			pm8001_ha->fatal_forensic_shift_offset);
		pm8001_ha->fatal_bar_loc = 0;
278 279 280 281
		status = 0;
		offset = (int)
			((char *)pm8001_ha->forensic_info.data_buf.direct_data
			- (char *)buf);
282 283
		pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv: return3 0x%x\n",
			   offset);
284 285 286 287
		return (char *)pm8001_ha->forensic_info.data_buf.direct_data -
			(char *)buf;
	}
	if (pm8001_ha->forensic_fatal_step == 1) {
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
		/* store previous accumulated length before triggering next
		 * accumulated length update
		 */
		pm8001_ha->forensic_preserved_accumulated_transfer =
			pm8001_mr32(fatal_table_address,
			MPI_FATAL_EDUMP_TABLE_ACCUM_LEN);

		/* continue capturing the fatal log until Dump status is 0x3 */
		if (pm8001_mr32(fatal_table_address,
			MPI_FATAL_EDUMP_TABLE_STATUS) <
			MPI_FATAL_EDUMP_TABLE_STAT_NF_SUCCESS_DONE) {

			/* reset fddstat bit by writing to zero*/
			pm8001_mw32(fatal_table_address,
					MPI_FATAL_EDUMP_TABLE_STATUS, 0x0);

			/* set dump control value to '1' so that new data will
			 * be transferred to shared memory
			 */
			pm8001_mw32(fatal_table_address,
				MPI_FATAL_EDUMP_TABLE_HANDSHAKE,
309 310
				MPI_FATAL_EDUMP_HANDSHAKE_RDY);

311 312
			/*Poll FDDHSHK  until clear */
			start = jiffies + (2 * HZ); /* 2 sec */
313

314 315
			do {
				reg_val = pm8001_mr32(fatal_table_address,
316
					MPI_FATAL_EDUMP_TABLE_HANDSHAKE);
317
			} while ((reg_val) && time_before(jiffies, start));
318

319
			if (reg_val != 0) {
320 321 322
				pm8001_dbg(pm8001_ha, FAIL,
					   "TIMEOUT:MPI_FATAL_EDUMP_TABLE_HDSHAKE 0x%x\n",
					   reg_val);
323 324 325 326 327 328 329 330 331 332 333 334 335
			       /* Fail the dump if a timeout occurs */
				pm8001_ha->forensic_info.data_buf.direct_data +=
				sprintf(
				pm8001_ha->forensic_info.data_buf.direct_data,
				"%08x ", 0xFFFFFFFF);
				return((char *)
				pm8001_ha->forensic_info.data_buf.direct_data
				- (char *)buf);
			}
			/* Poll status register until set to 2 or
			 * 3 for up to 2 seconds
			 */
			start = jiffies + (2 * HZ); /* 2 sec */
336

337 338 339
			do {
				reg_val = pm8001_mr32(fatal_table_address,
					MPI_FATAL_EDUMP_TABLE_STATUS);
340
			} while (((reg_val != 2) && (reg_val != 3)) &&
341 342 343
					time_before(jiffies, start));

			if (reg_val < 2) {
344 345 346
				pm8001_dbg(pm8001_ha, FAIL,
					   "TIMEOUT:MPI_FATAL_EDUMP_TABLE_STATUS = 0x%x\n",
					   reg_val);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
				/* Fail the dump if a timeout occurs */
				pm8001_ha->forensic_info.data_buf.direct_data +=
				sprintf(
				pm8001_ha->forensic_info.data_buf.direct_data,
				"%08x ", 0xFFFFFFFF);
				pm8001_cw32(pm8001_ha, 0,
					MEMBASE_II_SHIFT_REGISTER,
					pm8001_ha->fatal_forensic_shift_offset);
			}
			/* Read the next block of the debug data.*/
			length_to_read = pm8001_mr32(fatal_table_address,
			MPI_FATAL_EDUMP_TABLE_ACCUM_LEN) -
			pm8001_ha->forensic_preserved_accumulated_transfer;
			if (length_to_read != 0x0) {
				pm8001_ha->forensic_fatal_step = 0;
				goto moreData;
			} else {
				pm8001_ha->forensic_info.data_buf.direct_data +=
				sprintf(
				pm8001_ha->forensic_info.data_buf.direct_data,
				"%08x ", 4);
				pm8001_ha->forensic_info.data_buf.read_len
								= 0xFFFFFFFF;
				pm8001_ha->forensic_info.data_buf.direct_len
								=  0;
				pm8001_ha->forensic_info.data_buf.direct_offset
								= 0;
				pm8001_ha->forensic_info.data_buf.read_len = 0;
			}
376 377
		}
	}
378 379
	offset = (int)((char *)pm8001_ha->forensic_info.data_buf.direct_data
			- (char *)buf);
380
	pm8001_dbg(pm8001_ha, IO, "get_fatal_spcv: return4 0x%x\n", offset);
381 382 383 384
	return (char *)pm8001_ha->forensic_info.data_buf.direct_data -
		(char *)buf;
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/* pm80xx_get_non_fatal_dump - dump the nonfatal data from the dma
 * location by the firmware.
 */
ssize_t pm80xx_get_non_fatal_dump(struct device *cdev,
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
	void __iomem *nonfatal_table_address = pm8001_ha->fatal_tbl_addr;
	u32 accum_len = 0;
	u32 total_len = 0;
	u32 reg_val = 0;
	u32 *temp = NULL;
	u32 index = 0;
	u32 output_length;
	unsigned long start = 0;
	char *buf_copy = buf;

	temp = (u32 *)pm8001_ha->memoryMap.region[FORENSIC_MEM].virt_ptr;
	if (++pm8001_ha->non_fatal_count == 1) {
		if (pm8001_ha->chip_id == chip_8001) {
			snprintf(pm8001_ha->forensic_info.data_buf.direct_data,
				PAGE_SIZE, "Not supported for SPC controller");
			return 0;
		}
411
		pm8001_dbg(pm8001_ha, IO, "forensic_info TYPE_NON_FATAL...\n");
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
		/*
		 * Step 1: Write the host buffer parameters in the MPI Fatal and
		 * Non-Fatal Error Dump Capture Table.This is the buffer
		 * where debug data will be DMAed to.
		 */
		pm8001_mw32(nonfatal_table_address,
		MPI_FATAL_EDUMP_TABLE_LO_OFFSET,
		pm8001_ha->memoryMap.region[FORENSIC_MEM].phys_addr_lo);

		pm8001_mw32(nonfatal_table_address,
		MPI_FATAL_EDUMP_TABLE_HI_OFFSET,
		pm8001_ha->memoryMap.region[FORENSIC_MEM].phys_addr_hi);

		pm8001_mw32(nonfatal_table_address,
		MPI_FATAL_EDUMP_TABLE_LENGTH, SYSFS_OFFSET);

		/* Optionally, set the DUMPCTRL bit to 1 if the host
		 * keeps sending active I/Os while capturing the non-fatal
		 * debug data. Otherwise, leave this bit set to zero
		 */
		pm8001_mw32(nonfatal_table_address,
		MPI_FATAL_EDUMP_TABLE_HANDSHAKE, MPI_FATAL_EDUMP_HANDSHAKE_RDY);

		/*
		 * Step 2: Clear Accumulative Length of Debug Data Transferred
		 * [ACCDDLEN] field in the MPI Fatal and Non-Fatal Error Dump
		 * Capture Table to zero.
		 */
		pm8001_mw32(nonfatal_table_address,
				MPI_FATAL_EDUMP_TABLE_ACCUM_LEN, 0);

		/* initiallize previous accumulated length to 0 */
		pm8001_ha->forensic_preserved_accumulated_transfer = 0;
		pm8001_ha->non_fatal_read_length = 0;
	}

	total_len = pm8001_mr32(nonfatal_table_address,
			MPI_FATAL_EDUMP_TABLE_TOTAL_LEN);
	/*
	 * Step 3:Clear Fatal/Non-Fatal Debug Data Transfer Status [FDDTSTAT]
	 * field and then request that the SPCv controller transfer the debug
	 * data by setting bit 7 of the Inbound Doorbell Set Register.
	 */
	pm8001_mw32(nonfatal_table_address, MPI_FATAL_EDUMP_TABLE_STATUS, 0);
	pm8001_cw32(pm8001_ha, 0, MSGU_IBDB_SET,
			SPCv_MSGU_CFG_TABLE_NONFATAL_DUMP);

	/*
	 * Step 4.1: Read back the Inbound Doorbell Set Register (by polling for
	 * 2 seconds) until register bit 7 is cleared.
	 * This step only indicates the request is accepted by the controller.
	 */
	start = jiffies + (2 * HZ); /* 2 sec */
	do {
		reg_val = pm8001_cr32(pm8001_ha, 0, MSGU_IBDB_SET) &
			SPCv_MSGU_CFG_TABLE_NONFATAL_DUMP;
	} while ((reg_val != 0) && time_before(jiffies, start));

	/* Step 4.2: To check the completion of the transfer, poll the Fatal/Non
	 * Fatal Debug Data Transfer Status [FDDTSTAT] field for 2 seconds in
	 * the MPI Fatal and Non-Fatal Error Dump Capture Table.
	 */
	start = jiffies + (2 * HZ); /* 2 sec */
	do {
		reg_val = pm8001_mr32(nonfatal_table_address,
				MPI_FATAL_EDUMP_TABLE_STATUS);
	} while ((!reg_val) && time_before(jiffies, start));

	if ((reg_val == 0x00) ||
		(reg_val == MPI_FATAL_EDUMP_TABLE_STAT_DMA_FAILED) ||
		(reg_val > MPI_FATAL_EDUMP_TABLE_STAT_NF_SUCCESS_DONE)) {
		pm8001_ha->non_fatal_read_length = 0;
		buf_copy += snprintf(buf_copy, PAGE_SIZE, "%08x ", 0xFFFFFFFF);
		pm8001_ha->non_fatal_count = 0;
		return (buf_copy - buf);
	} else if (reg_val ==
			MPI_FATAL_EDUMP_TABLE_STAT_NF_SUCCESS_MORE_DATA) {
		buf_copy += snprintf(buf_copy, PAGE_SIZE, "%08x ", 2);
	} else if ((reg_val == MPI_FATAL_EDUMP_TABLE_STAT_NF_SUCCESS_DONE) ||
		(pm8001_ha->non_fatal_read_length >= total_len)) {
		pm8001_ha->non_fatal_read_length = 0;
		buf_copy += snprintf(buf_copy, PAGE_SIZE, "%08x ", 4);
		pm8001_ha->non_fatal_count = 0;
	}
	accum_len = pm8001_mr32(nonfatal_table_address,
			MPI_FATAL_EDUMP_TABLE_ACCUM_LEN);
	output_length = accum_len -
		pm8001_ha->forensic_preserved_accumulated_transfer;

	for (index = 0; index < output_length/4; index++)
		buf_copy += snprintf(buf_copy, PAGE_SIZE,
				"%08x ", *(temp+index));

	pm8001_ha->non_fatal_read_length += output_length;

	/* store current accumulated length to use in next iteration as
	 * the previous accumulated length
	 */
	pm8001_ha->forensic_preserved_accumulated_transfer = accum_len;
	return (buf_copy - buf);
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
/**
 * read_main_config_table - read the configure table and save it.
 * @pm8001_ha: our hba card information
 */
static void read_main_config_table(struct pm8001_hba_info *pm8001_ha)
{
	void __iomem *address = pm8001_ha->main_cfg_tbl_addr;

	pm8001_ha->main_cfg_tbl.pm80xx_tbl.signature	=
		pm8001_mr32(address, MAIN_SIGNATURE_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.interface_rev =
		pm8001_mr32(address, MAIN_INTERFACE_REVISION);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev	=
		pm8001_mr32(address, MAIN_FW_REVISION);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_out_io	=
		pm8001_mr32(address, MAIN_MAX_OUTSTANDING_IO_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_sgl	=
		pm8001_mr32(address, MAIN_MAX_SGL_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.ctrl_cap_flag =
		pm8001_mr32(address, MAIN_CNTRL_CAP_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.gst_offset	=
		pm8001_mr32(address, MAIN_GST_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.inbound_queue_offset =
		pm8001_mr32(address, MAIN_IBQ_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.outbound_queue_offset =
		pm8001_mr32(address, MAIN_OBQ_OFFSET);

	/* read Error Dump Offset and Length */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.fatal_err_dump_offset0 =
		pm8001_mr32(address, MAIN_FATAL_ERROR_RDUMP0_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.fatal_err_dump_length0 =
		pm8001_mr32(address, MAIN_FATAL_ERROR_RDUMP0_LENGTH);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.fatal_err_dump_offset1 =
		pm8001_mr32(address, MAIN_FATAL_ERROR_RDUMP1_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.fatal_err_dump_length1 =
		pm8001_mr32(address, MAIN_FATAL_ERROR_RDUMP1_LENGTH);

	/* read GPIO LED settings from the configuration table */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.gpio_led_mapping =
		pm8001_mr32(address, MAIN_GPIO_LED_FLAGS_OFFSET);

	/* read analog Setting offset from the configuration table */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.analog_setup_table_offset =
		pm8001_mr32(address, MAIN_ANALOG_SETUP_OFFSET);

	pm8001_ha->main_cfg_tbl.pm80xx_tbl.int_vec_table_offset =
		pm8001_mr32(address, MAIN_INT_VECTOR_TABLE_OFFSET);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.phy_attr_table_offset =
		pm8001_mr32(address, MAIN_SAS_PHY_ATTR_TABLE_OFFSET);
563 564 565
	/* read port recover and reset timeout */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.port_recovery_timer =
		pm8001_mr32(address, MAIN_PORT_RECOVERY_TIMER);
566 567 568 569 570
	/* read ILA and inactive firmware version */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.ila_version =
		pm8001_mr32(address, MAIN_MPI_ILA_RELEASE_TYPE);
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.inc_fw_version =
		pm8001_mr32(address, MAIN_MPI_INACTIVE_FW_VERSION);
571

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	pm8001_dbg(pm8001_ha, DEV,
		   "Main cfg table: sign:%x interface rev:%x fw_rev:%x\n",
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.signature,
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.interface_rev,
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.firmware_rev);

	pm8001_dbg(pm8001_ha, DEV,
		   "table offset: gst:%x iq:%x oq:%x int vec:%x phy attr:%x\n",
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.gst_offset,
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.inbound_queue_offset,
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.outbound_queue_offset,
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.int_vec_table_offset,
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.phy_attr_table_offset);

	pm8001_dbg(pm8001_ha, DEV,
		   "Main cfg table; ila rev:%x Inactive fw rev:%x\n",
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.ila_version,
		   pm8001_ha->main_cfg_tbl.pm80xx_tbl.inc_fw_version);
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
}

/**
 * read_general_status_table - read the general status table and save it.
 * @pm8001_ha: our hba card information
 */
static void read_general_status_table(struct pm8001_hba_info *pm8001_ha)
{
	void __iomem *address = pm8001_ha->general_stat_tbl_addr;
	pm8001_ha->gs_tbl.pm80xx_tbl.gst_len_mpistate	=
			pm8001_mr32(address, GST_GSTLEN_MPIS_OFFSET);
	pm8001_ha->gs_tbl.pm80xx_tbl.iq_freeze_state0	=
			pm8001_mr32(address, GST_IQ_FREEZE_STATE0_OFFSET);
	pm8001_ha->gs_tbl.pm80xx_tbl.iq_freeze_state1	=
			pm8001_mr32(address, GST_IQ_FREEZE_STATE1_OFFSET);
	pm8001_ha->gs_tbl.pm80xx_tbl.msgu_tcnt		=
			pm8001_mr32(address, GST_MSGUTCNT_OFFSET);
	pm8001_ha->gs_tbl.pm80xx_tbl.iop_tcnt		=
			pm8001_mr32(address, GST_IOPTCNT_OFFSET);
	pm8001_ha->gs_tbl.pm80xx_tbl.gpio_input_val	=
			pm8001_mr32(address, GST_GPIO_INPUT_VAL);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[0] =
			pm8001_mr32(address, GST_RERRINFO_OFFSET0);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[1] =
			pm8001_mr32(address, GST_RERRINFO_OFFSET1);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[2] =
			pm8001_mr32(address, GST_RERRINFO_OFFSET2);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[3] =
			pm8001_mr32(address, GST_RERRINFO_OFFSET3);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[4] =
			pm8001_mr32(address, GST_RERRINFO_OFFSET4);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[5] =
			pm8001_mr32(address, GST_RERRINFO_OFFSET5);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[6] =
			pm8001_mr32(address, GST_RERRINFO_OFFSET6);
	pm8001_ha->gs_tbl.pm80xx_tbl.recover_err_info[7] =
			 pm8001_mr32(address, GST_RERRINFO_OFFSET7);
}
/**
 * read_phy_attr_table - read the phy attribute table and save it.
 * @pm8001_ha: our hba card information
 */
static void read_phy_attr_table(struct pm8001_hba_info *pm8001_ha)
{
	void __iomem *address = pm8001_ha->pspa_q_tbl_addr;
	pm8001_ha->phy_attr_table.phystart1_16[0] =
			pm8001_mr32(address, PSPA_PHYSTATE0_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[1] =
			pm8001_mr32(address, PSPA_PHYSTATE1_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[2] =
			pm8001_mr32(address, PSPA_PHYSTATE2_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[3] =
			pm8001_mr32(address, PSPA_PHYSTATE3_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[4] =
			pm8001_mr32(address, PSPA_PHYSTATE4_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[5] =
			pm8001_mr32(address, PSPA_PHYSTATE5_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[6] =
			pm8001_mr32(address, PSPA_PHYSTATE6_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[7] =
			pm8001_mr32(address, PSPA_PHYSTATE7_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[8] =
			pm8001_mr32(address, PSPA_PHYSTATE8_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[9] =
			pm8001_mr32(address, PSPA_PHYSTATE9_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[10] =
			pm8001_mr32(address, PSPA_PHYSTATE10_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[11] =
			pm8001_mr32(address, PSPA_PHYSTATE11_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[12] =
			pm8001_mr32(address, PSPA_PHYSTATE12_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[13] =
			pm8001_mr32(address, PSPA_PHYSTATE13_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[14] =
			pm8001_mr32(address, PSPA_PHYSTATE14_OFFSET);
	pm8001_ha->phy_attr_table.phystart1_16[15] =
			pm8001_mr32(address, PSPA_PHYSTATE15_OFFSET);

	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[0] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID0_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[1] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID1_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[2] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID2_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[3] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID3_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[4] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID4_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[5] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID5_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[6] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID6_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[7] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID7_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[8] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID8_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[9] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID9_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[10] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID10_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[11] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID11_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[12] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID12_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[13] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID13_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[14] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID14_OFFSET);
	pm8001_ha->phy_attr_table.outbound_hw_event_pid1_16[15] =
			pm8001_mr32(address, PSPA_OB_HW_EVENT_PID15_OFFSET);

}

/**
 * read_inbnd_queue_table - read the inbound queue table and save it.
 * @pm8001_ha: our hba card information
 */
static void read_inbnd_queue_table(struct pm8001_hba_info *pm8001_ha)
{
	int i;
	void __iomem *address = pm8001_ha->inbnd_q_tbl_addr;
711
	for (i = 0; i < PM8001_MAX_INB_NUM; i++) {
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
		u32 offset = i * 0x20;
		pm8001_ha->inbnd_q_tbl[i].pi_pci_bar =
			get_pci_bar_index(pm8001_mr32(address,
				(offset + IB_PIPCI_BAR)));
		pm8001_ha->inbnd_q_tbl[i].pi_offset =
			pm8001_mr32(address, (offset + IB_PIPCI_BAR_OFFSET));
	}
}

/**
 * read_outbnd_queue_table - read the outbound queue table and save it.
 * @pm8001_ha: our hba card information
 */
static void read_outbnd_queue_table(struct pm8001_hba_info *pm8001_ha)
{
	int i;
	void __iomem *address = pm8001_ha->outbnd_q_tbl_addr;
729
	for (i = 0; i < PM8001_MAX_OUTB_NUM; i++) {
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
		u32 offset = i * 0x24;
		pm8001_ha->outbnd_q_tbl[i].ci_pci_bar =
			get_pci_bar_index(pm8001_mr32(address,
				(offset + OB_CIPCI_BAR)));
		pm8001_ha->outbnd_q_tbl[i].ci_offset =
			pm8001_mr32(address, (offset + OB_CIPCI_BAR_OFFSET));
	}
}

/**
 * init_default_table_values - init the default table.
 * @pm8001_ha: our hba card information
 */
static void init_default_table_values(struct pm8001_hba_info *pm8001_ha)
{
	int i;
	u32 offsetib, offsetob;
	void __iomem *addressib = pm8001_ha->inbnd_q_tbl_addr;
	void __iomem *addressob = pm8001_ha->outbnd_q_tbl_addr;
749 750 751 752
	u32 ib_offset = pm8001_ha->ib_offset;
	u32 ob_offset = pm8001_ha->ob_offset;
	u32 ci_offset = pm8001_ha->ci_offset;
	u32 pi_offset = pm8001_ha->pi_offset;
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

	pm8001_ha->main_cfg_tbl.pm80xx_tbl.upper_event_log_addr		=
		pm8001_ha->memoryMap.region[AAP1].phys_addr_hi;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.lower_event_log_addr		=
		pm8001_ha->memoryMap.region[AAP1].phys_addr_lo;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_size		=
							PM8001_EVENT_LOG_SIZE;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_severity		= 0x01;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.upper_pcs_event_log_addr	=
		pm8001_ha->memoryMap.region[IOP].phys_addr_hi;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.lower_pcs_event_log_addr	=
		pm8001_ha->memoryMap.region[IOP].phys_addr_lo;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.pcs_event_log_size		=
							PM8001_EVENT_LOG_SIZE;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.pcs_event_log_severity	= 0x01;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.fatal_err_interrupt		= 0x01;

770 771 772
	/* Disable end to end CRC checking */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.crc_core_dump = (0x1 << 16);

773
	for (i = 0; i < pm8001_ha->max_q_num; i++) {
774
		pm8001_ha->inbnd_q_tbl[i].element_pri_size_cnt	=
775
			PM8001_MPI_QUEUE | (pm8001_ha->iomb_size << 16) | (0x00<<30);
776
		pm8001_ha->inbnd_q_tbl[i].upper_base_addr	=
777
			pm8001_ha->memoryMap.region[ib_offset + i].phys_addr_hi;
778
		pm8001_ha->inbnd_q_tbl[i].lower_base_addr	=
779
		pm8001_ha->memoryMap.region[ib_offset + i].phys_addr_lo;
780
		pm8001_ha->inbnd_q_tbl[i].base_virt		=
781
		  (u8 *)pm8001_ha->memoryMap.region[ib_offset + i].virt_ptr;
782
		pm8001_ha->inbnd_q_tbl[i].total_length		=
783
			pm8001_ha->memoryMap.region[ib_offset + i].total_len;
784
		pm8001_ha->inbnd_q_tbl[i].ci_upper_base_addr	=
785
			pm8001_ha->memoryMap.region[ci_offset + i].phys_addr_hi;
786
		pm8001_ha->inbnd_q_tbl[i].ci_lower_base_addr	=
787
			pm8001_ha->memoryMap.region[ci_offset + i].phys_addr_lo;
788
		pm8001_ha->inbnd_q_tbl[i].ci_virt		=
789
			pm8001_ha->memoryMap.region[ci_offset + i].virt_ptr;
790 791 792 793 794 795 796 797
		offsetib = i * 0x20;
		pm8001_ha->inbnd_q_tbl[i].pi_pci_bar		=
			get_pci_bar_index(pm8001_mr32(addressib,
				(offsetib + 0x14)));
		pm8001_ha->inbnd_q_tbl[i].pi_offset		=
			pm8001_mr32(addressib, (offsetib + 0x18));
		pm8001_ha->inbnd_q_tbl[i].producer_idx		= 0;
		pm8001_ha->inbnd_q_tbl[i].consumer_index	= 0;
798

799 800 801 802
		pm8001_dbg(pm8001_ha, DEV,
			   "IQ %d pi_bar 0x%x pi_offset 0x%x\n", i,
			   pm8001_ha->inbnd_q_tbl[i].pi_pci_bar,
			   pm8001_ha->inbnd_q_tbl[i].pi_offset);
803
	}
804
	for (i = 0; i < pm8001_ha->max_q_num; i++) {
805
		pm8001_ha->outbnd_q_tbl[i].element_size_cnt	=
806
			PM8001_MPI_QUEUE | (pm8001_ha->iomb_size << 16) | (0x01<<30);
807
		pm8001_ha->outbnd_q_tbl[i].upper_base_addr	=
808
			pm8001_ha->memoryMap.region[ob_offset + i].phys_addr_hi;
809
		pm8001_ha->outbnd_q_tbl[i].lower_base_addr	=
810
			pm8001_ha->memoryMap.region[ob_offset + i].phys_addr_lo;
811
		pm8001_ha->outbnd_q_tbl[i].base_virt		=
812
		  (u8 *)pm8001_ha->memoryMap.region[ob_offset + i].virt_ptr;
813
		pm8001_ha->outbnd_q_tbl[i].total_length		=
814
			pm8001_ha->memoryMap.region[ob_offset + i].total_len;
815
		pm8001_ha->outbnd_q_tbl[i].pi_upper_base_addr	=
816
			pm8001_ha->memoryMap.region[pi_offset + i].phys_addr_hi;
817
		pm8001_ha->outbnd_q_tbl[i].pi_lower_base_addr	=
818
			pm8001_ha->memoryMap.region[pi_offset + i].phys_addr_lo;
819 820 821
		/* interrupt vector based on oq */
		pm8001_ha->outbnd_q_tbl[i].interrup_vec_cnt_delay = (i << 24);
		pm8001_ha->outbnd_q_tbl[i].pi_virt		=
822
			pm8001_ha->memoryMap.region[pi_offset + i].virt_ptr;
823 824 825 826 827 828 829 830
		offsetob = i * 0x24;
		pm8001_ha->outbnd_q_tbl[i].ci_pci_bar		=
			get_pci_bar_index(pm8001_mr32(addressob,
			offsetob + 0x14));
		pm8001_ha->outbnd_q_tbl[i].ci_offset		=
			pm8001_mr32(addressob, (offsetob + 0x18));
		pm8001_ha->outbnd_q_tbl[i].consumer_idx		= 0;
		pm8001_ha->outbnd_q_tbl[i].producer_index	= 0;
831

832 833 834 835
		pm8001_dbg(pm8001_ha, DEV,
			   "OQ %d ci_bar 0x%x ci_offset 0x%x\n", i,
			   pm8001_ha->outbnd_q_tbl[i].ci_pci_bar,
			   pm8001_ha->outbnd_q_tbl[i].ci_offset);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
	}
}

/**
 * update_main_config_table - update the main default table to the HBA.
 * @pm8001_ha: our hba card information
 */
static void update_main_config_table(struct pm8001_hba_info *pm8001_ha)
{
	void __iomem *address = pm8001_ha->main_cfg_tbl_addr;
	pm8001_mw32(address, MAIN_IQNPPD_HPPD_OFFSET,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.inbound_q_nppd_hppd);
	pm8001_mw32(address, MAIN_EVENT_LOG_ADDR_HI,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.upper_event_log_addr);
	pm8001_mw32(address, MAIN_EVENT_LOG_ADDR_LO,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.lower_event_log_addr);
	pm8001_mw32(address, MAIN_EVENT_LOG_BUFF_SIZE,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_size);
	pm8001_mw32(address, MAIN_EVENT_LOG_OPTION,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.event_log_severity);
	pm8001_mw32(address, MAIN_PCS_EVENT_LOG_ADDR_HI,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.upper_pcs_event_log_addr);
	pm8001_mw32(address, MAIN_PCS_EVENT_LOG_ADDR_LO,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.lower_pcs_event_log_addr);
	pm8001_mw32(address, MAIN_PCS_EVENT_LOG_BUFF_SIZE,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.pcs_event_log_size);
	pm8001_mw32(address, MAIN_PCS_EVENT_LOG_OPTION,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.pcs_event_log_severity);
864 865
	/* Update Fatal error interrupt vector */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.fatal_err_interrupt |=
866
					((pm8001_ha->max_q_num - 1) << 8);
867 868
	pm8001_mw32(address, MAIN_FATAL_ERROR_INTERRUPT,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.fatal_err_interrupt);
869 870 871
	pm8001_dbg(pm8001_ha, DEV,
		   "Updated Fatal error interrupt vector 0x%x\n",
		   pm8001_mr32(address, MAIN_FATAL_ERROR_INTERRUPT));
872

873 874
	pm8001_mw32(address, MAIN_EVENT_CRC_CHECK,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.crc_core_dump);
875 876 877 878 879 880 881

	/* SPCv specific */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.gpio_led_mapping &= 0xCFFFFFFF;
	/* Set GPIOLED to 0x2 for LED indicator */
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.gpio_led_mapping |= 0x20000000;
	pm8001_mw32(address, MAIN_GPIO_LED_FLAGS_OFFSET,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.gpio_led_mapping);
882 883 884
	pm8001_dbg(pm8001_ha, DEV,
		   "Programming DW 0x21 in main cfg table with 0x%x\n",
		   pm8001_mr32(address, MAIN_GPIO_LED_FLAGS_OFFSET));
885 886 887 888 889

	pm8001_mw32(address, MAIN_PORT_RECOVERY_TIMER,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.port_recovery_timer);
	pm8001_mw32(address, MAIN_INT_REASSERTION_DELAY,
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.interrupt_reassertion_delay);
890 891 892 893

	pm8001_ha->main_cfg_tbl.pm80xx_tbl.port_recovery_timer &= 0xffff0000;
	pm8001_ha->main_cfg_tbl.pm80xx_tbl.port_recovery_timer |=
							PORT_RECOVERY_TIMEOUT;
894 895 896 897
	if (pm8001_ha->chip_id == chip_8006) {
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.port_recovery_timer &=
					0x0000ffff;
		pm8001_ha->main_cfg_tbl.pm80xx_tbl.port_recovery_timer |=
898
					CHIP_8006_PORT_RECOVERY_TIMEOUT;
899
	}
900 901
	pm8001_mw32(address, MAIN_PORT_RECOVERY_TIMER,
			pm8001_ha->main_cfg_tbl.pm80xx_tbl.port_recovery_timer);
902 903 904 905 906
}

/**
 * update_inbnd_queue_table - update the inbound queue table to the HBA.
 * @pm8001_ha: our hba card information
907
 * @number: entry in the queue
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
 */
static void update_inbnd_queue_table(struct pm8001_hba_info *pm8001_ha,
					 int number)
{
	void __iomem *address = pm8001_ha->inbnd_q_tbl_addr;
	u16 offset = number * 0x20;
	pm8001_mw32(address, offset + IB_PROPERITY_OFFSET,
		pm8001_ha->inbnd_q_tbl[number].element_pri_size_cnt);
	pm8001_mw32(address, offset + IB_BASE_ADDR_HI_OFFSET,
		pm8001_ha->inbnd_q_tbl[number].upper_base_addr);
	pm8001_mw32(address, offset + IB_BASE_ADDR_LO_OFFSET,
		pm8001_ha->inbnd_q_tbl[number].lower_base_addr);
	pm8001_mw32(address, offset + IB_CI_BASE_ADDR_HI_OFFSET,
		pm8001_ha->inbnd_q_tbl[number].ci_upper_base_addr);
	pm8001_mw32(address, offset + IB_CI_BASE_ADDR_LO_OFFSET,
		pm8001_ha->inbnd_q_tbl[number].ci_lower_base_addr);
924

925 926 927 928
	pm8001_dbg(pm8001_ha, DEV,
		   "IQ %d: Element pri size 0x%x\n",
		   number,
		   pm8001_ha->inbnd_q_tbl[number].element_pri_size_cnt);
929

930 931 932 933
	pm8001_dbg(pm8001_ha, DEV,
		   "IQ upr base addr 0x%x IQ lwr base addr 0x%x\n",
		   pm8001_ha->inbnd_q_tbl[number].upper_base_addr,
		   pm8001_ha->inbnd_q_tbl[number].lower_base_addr);
934

935 936 937 938
	pm8001_dbg(pm8001_ha, DEV,
		   "CI upper base addr 0x%x CI lower base addr 0x%x\n",
		   pm8001_ha->inbnd_q_tbl[number].ci_upper_base_addr,
		   pm8001_ha->inbnd_q_tbl[number].ci_lower_base_addr);
939 940 941 942 943
}

/**
 * update_outbnd_queue_table - update the outbound queue table to the HBA.
 * @pm8001_ha: our hba card information
944
 * @number: entry in the queue
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
 */
static void update_outbnd_queue_table(struct pm8001_hba_info *pm8001_ha,
						 int number)
{
	void __iomem *address = pm8001_ha->outbnd_q_tbl_addr;
	u16 offset = number * 0x24;
	pm8001_mw32(address, offset + OB_PROPERITY_OFFSET,
		pm8001_ha->outbnd_q_tbl[number].element_size_cnt);
	pm8001_mw32(address, offset + OB_BASE_ADDR_HI_OFFSET,
		pm8001_ha->outbnd_q_tbl[number].upper_base_addr);
	pm8001_mw32(address, offset + OB_BASE_ADDR_LO_OFFSET,
		pm8001_ha->outbnd_q_tbl[number].lower_base_addr);
	pm8001_mw32(address, offset + OB_PI_BASE_ADDR_HI_OFFSET,
		pm8001_ha->outbnd_q_tbl[number].pi_upper_base_addr);
	pm8001_mw32(address, offset + OB_PI_BASE_ADDR_LO_OFFSET,
		pm8001_ha->outbnd_q_tbl[number].pi_lower_base_addr);
	pm8001_mw32(address, offset + OB_INTERRUPT_COALES_OFFSET,
		pm8001_ha->outbnd_q_tbl[number].interrup_vec_cnt_delay);
963

964 965 966 967
	pm8001_dbg(pm8001_ha, DEV,
		   "OQ %d: Element pri size 0x%x\n",
		   number,
		   pm8001_ha->outbnd_q_tbl[number].element_size_cnt);
968

969 970 971 972
	pm8001_dbg(pm8001_ha, DEV,
		   "OQ upr base addr 0x%x OQ lwr base addr 0x%x\n",
		   pm8001_ha->outbnd_q_tbl[number].upper_base_addr,
		   pm8001_ha->outbnd_q_tbl[number].lower_base_addr);
973

974 975 976 977
	pm8001_dbg(pm8001_ha, DEV,
		   "PI upper base addr 0x%x PI lower base addr 0x%x\n",
		   pm8001_ha->outbnd_q_tbl[number].pi_upper_base_addr,
		   pm8001_ha->outbnd_q_tbl[number].pi_lower_base_addr);
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
}

/**
 * mpi_init_check - check firmware initialization status.
 * @pm8001_ha: our hba card information
 */
static int mpi_init_check(struct pm8001_hba_info *pm8001_ha)
{
	u32 max_wait_count;
	u32 value;
	u32 gst_len_mpistate;

	/* Write bit0=1 to Inbound DoorBell Register to tell the SPC FW the
	table is updated */
	pm8001_cw32(pm8001_ha, 0, MSGU_IBDB_SET, SPCv_MSGU_CFG_TABLE_UPDATE);
	/* wait until Inbound DoorBell Clear Register toggled */
994
	if (IS_SPCV_12G(pm8001_ha->pdev)) {
995
		max_wait_count = SPCV_DOORBELL_CLEAR_TIMEOUT;
996
	} else {
997
		max_wait_count = SPC_DOORBELL_CLEAR_TIMEOUT;
998
	}
999 1000 1001 1002 1003 1004
	do {
		udelay(1);
		value = pm8001_cr32(pm8001_ha, 0, MSGU_IBDB_SET);
		value &= SPCv_MSGU_CFG_TABLE_UPDATE;
	} while ((value != 0) && (--max_wait_count));

1005 1006
	if (!max_wait_count) {
		/* additional check */
1007 1008 1009
		pm8001_dbg(pm8001_ha, FAIL,
			   "Inb doorbell clear not toggled[value:%x]\n",
			   value);
1010 1011
		return -EBUSY;
	}
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	/* check the MPI-State for initialization upto 100ms*/
	max_wait_count = 100 * 1000;/* 100 msec */
	do {
		udelay(1);
		gst_len_mpistate =
			pm8001_mr32(pm8001_ha->general_stat_tbl_addr,
					GST_GSTLEN_MPIS_OFFSET);
	} while ((GST_MPI_STATE_INIT !=
		(gst_len_mpistate & GST_MPI_STATE_MASK)) && (--max_wait_count));
	if (!max_wait_count)
1022
		return -EBUSY;
1023 1024 1025 1026

	/* check MPI Initialization error */
	gst_len_mpistate = gst_len_mpistate >> 16;
	if (0x0000 != gst_len_mpistate)
1027
		return -EBUSY;
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059

	return 0;
}

/**
 * check_fw_ready - The LLDD check if the FW is ready, if not, return error.
 * @pm8001_ha: our hba card information
 */
static int check_fw_ready(struct pm8001_hba_info *pm8001_ha)
{
	u32 value;
	u32 max_wait_count;
	u32 max_wait_time;
	int ret = 0;

	/* reset / PCIe ready */
	max_wait_time = max_wait_count = 100 * 1000;	/* 100 milli sec */
	do {
		udelay(1);
		value = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1);
	} while ((value == 0xFFFFFFFF) && (--max_wait_count));

	/* check ila status */
	max_wait_time = max_wait_count = 1000 * 1000;	/* 1000 milli sec */
	do {
		udelay(1);
		value = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1);
	} while (((value & SCRATCH_PAD_ILA_READY) !=
			SCRATCH_PAD_ILA_READY) && (--max_wait_count));
	if (!max_wait_count)
		ret = -1;
	else {
1060 1061 1062
		pm8001_dbg(pm8001_ha, MSG,
			   " ila ready status in %d millisec\n",
			   (max_wait_time - max_wait_count));
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	}

	/* check RAAE status */
	max_wait_time = max_wait_count = 1800 * 1000;	/* 1800 milli sec */
	do {
		udelay(1);
		value = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1);
	} while (((value & SCRATCH_PAD_RAAE_READY) !=
				SCRATCH_PAD_RAAE_READY) && (--max_wait_count));
	if (!max_wait_count)
		ret = -1;
	else {
1075 1076 1077
		pm8001_dbg(pm8001_ha, MSG,
			   " raae ready status in %d millisec\n",
			   (max_wait_time - max_wait_count));
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	}

	/* check iop0 status */
	max_wait_time = max_wait_count = 600 * 1000;	/* 600 milli sec */
	do {
		udelay(1);
		value = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1);
	} while (((value & SCRATCH_PAD_IOP0_READY) != SCRATCH_PAD_IOP0_READY) &&
			(--max_wait_count));
	if (!max_wait_count)
		ret = -1;
	else {
1090 1091 1092
		pm8001_dbg(pm8001_ha, MSG,
			   " iop0 ready status in %d millisec\n",
			   (max_wait_time - max_wait_count));
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	}

	/* check iop1 status only for 16 port controllers */
	if ((pm8001_ha->chip_id != chip_8008) &&
			(pm8001_ha->chip_id != chip_8009)) {
		/* 200 milli sec */
		max_wait_time = max_wait_count = 200 * 1000;
		do {
			udelay(1);
			value = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1);
		} while (((value & SCRATCH_PAD_IOP1_READY) !=
				SCRATCH_PAD_IOP1_READY) && (--max_wait_count));
		if (!max_wait_count)
			ret = -1;
		else {
1108 1109 1110
			pm8001_dbg(pm8001_ha, MSG,
				   "iop1 ready status in %d millisec\n",
				   (max_wait_time - max_wait_count));
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
		}
	}

	return ret;
}

static void init_pci_device_addresses(struct pm8001_hba_info *pm8001_ha)
{
	void __iomem *base_addr;
	u32	value;
	u32	offset;
	u32	pcibar;
	u32	pcilogic;

	value = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_0);
	offset = value & 0x03FFFFFF; /* scratch pad 0 TBL address */

1128 1129
	pm8001_dbg(pm8001_ha, DEV, "Scratchpad 0 Offset: 0x%x value 0x%x\n",
		   offset, value);
1130 1131
	pcilogic = (value & 0xFC000000) >> 26;
	pcibar = get_pci_bar_index(pcilogic);
1132
	pm8001_dbg(pm8001_ha, INIT, "Scratchpad 0 PCI BAR: %d\n", pcibar);
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	pm8001_ha->main_cfg_tbl_addr = base_addr =
		pm8001_ha->io_mem[pcibar].memvirtaddr + offset;
	pm8001_ha->general_stat_tbl_addr =
		base_addr + (pm8001_cr32(pm8001_ha, pcibar, offset + 0x18) &
					0xFFFFFF);
	pm8001_ha->inbnd_q_tbl_addr =
		base_addr + (pm8001_cr32(pm8001_ha, pcibar, offset + 0x1C) &
					0xFFFFFF);
	pm8001_ha->outbnd_q_tbl_addr =
		base_addr + (pm8001_cr32(pm8001_ha, pcibar, offset + 0x20) &
					0xFFFFFF);
	pm8001_ha->ivt_tbl_addr =
		base_addr + (pm8001_cr32(pm8001_ha, pcibar, offset + 0x8C) &
					0xFFFFFF);
	pm8001_ha->pspa_q_tbl_addr =
		base_addr + (pm8001_cr32(pm8001_ha, pcibar, offset + 0x90) &
					0xFFFFFF);
1150 1151 1152
	pm8001_ha->fatal_tbl_addr =
		base_addr + (pm8001_cr32(pm8001_ha, pcibar, offset + 0xA0) &
					0xFFFFFF);
1153

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	pm8001_dbg(pm8001_ha, INIT, "GST OFFSET 0x%x\n",
		   pm8001_cr32(pm8001_ha, pcibar, offset + 0x18));
	pm8001_dbg(pm8001_ha, INIT, "INBND OFFSET 0x%x\n",
		   pm8001_cr32(pm8001_ha, pcibar, offset + 0x1C));
	pm8001_dbg(pm8001_ha, INIT, "OBND OFFSET 0x%x\n",
		   pm8001_cr32(pm8001_ha, pcibar, offset + 0x20));
	pm8001_dbg(pm8001_ha, INIT, "IVT OFFSET 0x%x\n",
		   pm8001_cr32(pm8001_ha, pcibar, offset + 0x8C));
	pm8001_dbg(pm8001_ha, INIT, "PSPA OFFSET 0x%x\n",
		   pm8001_cr32(pm8001_ha, pcibar, offset + 0x90));
	pm8001_dbg(pm8001_ha, INIT, "addr - main cfg %p general status %p\n",
		   pm8001_ha->main_cfg_tbl_addr,
		   pm8001_ha->general_stat_tbl_addr);
	pm8001_dbg(pm8001_ha, INIT, "addr - inbnd %p obnd %p\n",
		   pm8001_ha->inbnd_q_tbl_addr,
		   pm8001_ha->outbnd_q_tbl_addr);
	pm8001_dbg(pm8001_ha, INIT, "addr - pspa %p ivt %p\n",
		   pm8001_ha->pspa_q_tbl_addr,
		   pm8001_ha->ivt_tbl_addr);
1173 1174 1175 1176 1177 1178
}

/**
 * pm80xx_set_thermal_config - support the thermal configuration
 * @pm8001_ha: our hba card information.
 */
1179
int
1180 1181 1182 1183 1184 1185 1186
pm80xx_set_thermal_config(struct pm8001_hba_info *pm8001_ha)
{
	struct set_ctrl_cfg_req payload;
	struct inbound_queue_table *circularQ;
	int rc;
	u32 tag;
	u32 opc = OPC_INB_SET_CONTROLLER_CONFIG;
V
Viswas G 已提交
1187
	u32 page_code;
1188 1189 1190 1191 1192 1193 1194 1195

	memset(&payload, 0, sizeof(struct set_ctrl_cfg_req));
	rc = pm8001_tag_alloc(pm8001_ha, &tag);
	if (rc)
		return -1;

	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	payload.tag = cpu_to_le32(tag);
V
Viswas G 已提交
1196 1197 1198 1199 1200 1201

	if (IS_SPCV_12G(pm8001_ha->pdev))
		page_code = THERMAL_PAGE_CODE_7H;
	else
		page_code = THERMAL_PAGE_CODE_8H;

1202
	payload.cfg_pg[0] = (THERMAL_LOG_ENABLE << 9) |
V
Viswas G 已提交
1203
				(THERMAL_ENABLE << 8) | page_code;
1204 1205
	payload.cfg_pg[1] = (LTEMPHIL << 24) | (RTEMPHIL << 8);

1206 1207 1208
	pm8001_dbg(pm8001_ha, DEV,
		   "Setting up thermal config. cfg_pg 0 0x%x cfg_pg 1 0x%x\n",
		   payload.cfg_pg[0], payload.cfg_pg[1]);
1209

1210 1211
	rc = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
T
Tomas Henzl 已提交
1212 1213
	if (rc)
		pm8001_tag_free(pm8001_ha, tag);
1214 1215 1216 1217
	return rc;

}

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
/**
* pm80xx_set_sas_protocol_timer_config - support the SAS Protocol
* Timer configuration page
* @pm8001_ha: our hba card information.
*/
static int
pm80xx_set_sas_protocol_timer_config(struct pm8001_hba_info *pm8001_ha)
{
	struct set_ctrl_cfg_req payload;
	struct inbound_queue_table *circularQ;
	SASProtocolTimerConfig_t SASConfigPage;
	int rc;
	u32 tag;
	u32 opc = OPC_INB_SET_CONTROLLER_CONFIG;

	memset(&payload, 0, sizeof(struct set_ctrl_cfg_req));
	memset(&SASConfigPage, 0, sizeof(SASProtocolTimerConfig_t));

	rc = pm8001_tag_alloc(pm8001_ha, &tag);

	if (rc)
		return -1;

	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	payload.tag = cpu_to_le32(tag);

	SASConfigPage.pageCode        =  SAS_PROTOCOL_TIMER_CONFIG_PAGE;
	SASConfigPage.MST_MSI         =  3 << 15;
	SASConfigPage.STP_SSP_MCT_TMO =  (STP_MCT_TMO << 16) | SSP_MCT_TMO;
	SASConfigPage.STP_FRM_TMO     = (SAS_MAX_OPEN_TIME << 24) |
				(SMP_MAX_CONN_TIMER << 16) | STP_FRM_TIMER;
	SASConfigPage.STP_IDLE_TMO    =  STP_IDLE_TIME;

	if (SASConfigPage.STP_IDLE_TMO > 0x3FFFFFF)
		SASConfigPage.STP_IDLE_TMO = 0x3FFFFFF;


	SASConfigPage.OPNRJT_RTRY_INTVL =         (SAS_MFD << 16) |
						SAS_OPNRJT_RTRY_INTVL;
	SASConfigPage.Data_Cmd_OPNRJT_RTRY_TMO =  (SAS_DOPNRJT_RTRY_TMO << 16)
						| SAS_COPNRJT_RTRY_TMO;
	SASConfigPage.Data_Cmd_OPNRJT_RTRY_THR =  (SAS_DOPNRJT_RTRY_THR << 16)
						| SAS_COPNRJT_RTRY_THR;
	SASConfigPage.MAX_AIP =  SAS_MAX_AIP;

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.pageCode 0x%08x\n",
		   SASConfigPage.pageCode);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.MST_MSI  0x%08x\n",
		   SASConfigPage.MST_MSI);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.STP_SSP_MCT_TMO  0x%08x\n",
		   SASConfigPage.STP_SSP_MCT_TMO);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.STP_FRM_TMO  0x%08x\n",
		   SASConfigPage.STP_FRM_TMO);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.STP_IDLE_TMO  0x%08x\n",
		   SASConfigPage.STP_IDLE_TMO);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.OPNRJT_RTRY_INTVL  0x%08x\n",
		   SASConfigPage.OPNRJT_RTRY_INTVL);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.Data_Cmd_OPNRJT_RTRY_TMO  0x%08x\n",
		   SASConfigPage.Data_Cmd_OPNRJT_RTRY_TMO);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.Data_Cmd_OPNRJT_RTRY_THR  0x%08x\n",
		   SASConfigPage.Data_Cmd_OPNRJT_RTRY_THR);
	pm8001_dbg(pm8001_ha, INIT, "SASConfigPage.MAX_AIP  0x%08x\n",
		   SASConfigPage.MAX_AIP);
1281 1282 1283 1284

	memcpy(&payload.cfg_pg, &SASConfigPage,
			 sizeof(SASProtocolTimerConfig_t));

1285 1286
	rc = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
T
Tomas Henzl 已提交
1287 1288
	if (rc)
		pm8001_tag_free(pm8001_ha, tag);
1289 1290 1291 1292

	return rc;
}

1293 1294 1295 1296 1297 1298 1299 1300
/**
 * pm80xx_get_encrypt_info - Check for encryption
 * @pm8001_ha: our hba card information.
 */
static int
pm80xx_get_encrypt_info(struct pm8001_hba_info *pm8001_ha)
{
	u32 scratch3_value;
1301
	int ret = -1;
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

	/* Read encryption status from SCRATCH PAD 3 */
	scratch3_value = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_3);

	if ((scratch3_value & SCRATCH_PAD3_ENC_MASK) ==
					SCRATCH_PAD3_ENC_READY) {
		if (scratch3_value & SCRATCH_PAD3_XTS_ENABLED)
			pm8001_ha->encrypt_info.cipher_mode = CIPHER_MODE_XTS;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
						SCRATCH_PAD3_SMF_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMF;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
						SCRATCH_PAD3_SMA_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMA;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
						SCRATCH_PAD3_SMB_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMB;
		pm8001_ha->encrypt_info.status = 0;
1320 1321 1322 1323 1324 1325
		pm8001_dbg(pm8001_ha, INIT,
			   "Encryption: SCRATCH_PAD3_ENC_READY 0x%08X.Cipher mode 0x%x Sec mode 0x%x status 0x%x\n",
			   scratch3_value,
			   pm8001_ha->encrypt_info.cipher_mode,
			   pm8001_ha->encrypt_info.sec_mode,
			   pm8001_ha->encrypt_info.status);
1326 1327 1328
		ret = 0;
	} else if ((scratch3_value & SCRATCH_PAD3_ENC_READY) ==
					SCRATCH_PAD3_ENC_DISABLED) {
1329 1330 1331
		pm8001_dbg(pm8001_ha, INIT,
			   "Encryption: SCRATCH_PAD3_ENC_DISABLED 0x%08X\n",
			   scratch3_value);
1332 1333 1334
		pm8001_ha->encrypt_info.status = 0xFFFFFFFF;
		pm8001_ha->encrypt_info.cipher_mode = 0;
		pm8001_ha->encrypt_info.sec_mode = 0;
1335
		ret = 0;
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	} else if ((scratch3_value & SCRATCH_PAD3_ENC_MASK) ==
				SCRATCH_PAD3_ENC_DIS_ERR) {
		pm8001_ha->encrypt_info.status =
			(scratch3_value & SCRATCH_PAD3_ERR_CODE) >> 16;
		if (scratch3_value & SCRATCH_PAD3_XTS_ENABLED)
			pm8001_ha->encrypt_info.cipher_mode = CIPHER_MODE_XTS;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
					SCRATCH_PAD3_SMF_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMF;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
					SCRATCH_PAD3_SMA_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMA;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
					SCRATCH_PAD3_SMB_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMB;
1351 1352 1353 1354 1355 1356
		pm8001_dbg(pm8001_ha, INIT,
			   "Encryption: SCRATCH_PAD3_DIS_ERR 0x%08X.Cipher mode 0x%x sec mode 0x%x status 0x%x\n",
			   scratch3_value,
			   pm8001_ha->encrypt_info.cipher_mode,
			   pm8001_ha->encrypt_info.sec_mode,
			   pm8001_ha->encrypt_info.status);
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
	} else if ((scratch3_value & SCRATCH_PAD3_ENC_MASK) ==
				 SCRATCH_PAD3_ENC_ENA_ERR) {

		pm8001_ha->encrypt_info.status =
			(scratch3_value & SCRATCH_PAD3_ERR_CODE) >> 16;
		if (scratch3_value & SCRATCH_PAD3_XTS_ENABLED)
			pm8001_ha->encrypt_info.cipher_mode = CIPHER_MODE_XTS;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
					SCRATCH_PAD3_SMF_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMF;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
					SCRATCH_PAD3_SMA_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMA;
		if ((scratch3_value & SCRATCH_PAD3_SM_MASK) ==
					SCRATCH_PAD3_SMB_ENABLED)
			pm8001_ha->encrypt_info.sec_mode = SEC_MODE_SMB;

1374 1375 1376 1377 1378 1379
		pm8001_dbg(pm8001_ha, INIT,
			   "Encryption: SCRATCH_PAD3_ENA_ERR 0x%08X.Cipher mode 0x%x sec mode 0x%x status 0x%x\n",
			   scratch3_value,
			   pm8001_ha->encrypt_info.cipher_mode,
			   pm8001_ha->encrypt_info.sec_mode,
			   pm8001_ha->encrypt_info.status);
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
	}
	return ret;
}

/**
 * pm80xx_encrypt_update - update flash with encryption informtion
 * @pm8001_ha: our hba card information.
 */
static int pm80xx_encrypt_update(struct pm8001_hba_info *pm8001_ha)
{
	struct kek_mgmt_req payload;
	struct inbound_queue_table *circularQ;
	int rc;
	u32 tag;
	u32 opc = OPC_INB_KEK_MANAGEMENT;

	memset(&payload, 0, sizeof(struct kek_mgmt_req));
	rc = pm8001_tag_alloc(pm8001_ha, &tag);
	if (rc)
		return -1;

	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	payload.tag = cpu_to_le32(tag);
	/* Currently only one key is used. New KEK index is 1.
	 * Current KEK index is 1. Store KEK to NVRAM is 1.
	 */
	payload.new_curidx_ksop = ((1 << 24) | (1 << 16) | (1 << 8) |
					KEK_MGMT_SUBOP_KEYCARDUPDATE);

1409 1410 1411
	pm8001_dbg(pm8001_ha, DEV,
		   "Saving Encryption info to flash. payload 0x%x\n",
		   payload.new_curidx_ksop);
1412

1413 1414
	rc = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
T
Tomas Henzl 已提交
1415 1416
	if (rc)
		pm8001_tag_free(pm8001_ha, tag);
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431

	return rc;
}

/**
 * pm8001_chip_init - the main init function that initialize whole PM8001 chip.
 * @pm8001_ha: our hba card information
 */
static int pm80xx_chip_init(struct pm8001_hba_info *pm8001_ha)
{
	int ret;
	u8 i = 0;

	/* check the firmware status */
	if (-1 == check_fw_ready(pm8001_ha)) {
1432
		pm8001_dbg(pm8001_ha, FAIL, "Firmware is not ready!\n");
1433 1434 1435
		return -EBUSY;
	}

1436 1437 1438
	/* Initialize the controller fatal error flag */
	pm8001_ha->controller_fatal_error = false;

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
	/* Initialize pci space address eg: mpi offset */
	init_pci_device_addresses(pm8001_ha);
	init_default_table_values(pm8001_ha);
	read_main_config_table(pm8001_ha);
	read_general_status_table(pm8001_ha);
	read_inbnd_queue_table(pm8001_ha);
	read_outbnd_queue_table(pm8001_ha);
	read_phy_attr_table(pm8001_ha);

	/* update main config table ,inbound table and outbound table */
	update_main_config_table(pm8001_ha);
1450
	for (i = 0; i < pm8001_ha->max_q_num; i++) {
1451 1452
		update_inbnd_queue_table(pm8001_ha, i);
		update_outbnd_queue_table(pm8001_ha, i);
1453
	}
1454 1455
	/* notify firmware update finished and check initialization status */
	if (0 == mpi_init_check(pm8001_ha)) {
1456
		pm8001_dbg(pm8001_ha, INIT, "MPI initialize successful!\n");
1457 1458 1459
	} else
		return -EBUSY;

1460 1461
	/* send SAS protocol timer configuration page to FW */
	ret = pm80xx_set_sas_protocol_timer_config(pm8001_ha);
1462 1463 1464

	/* Check for encryption */
	if (pm8001_ha->chip->encrypt) {
1465
		pm8001_dbg(pm8001_ha, INIT, "Checking for encryption\n");
1466 1467
		ret = pm80xx_get_encrypt_info(pm8001_ha);
		if (ret == -1) {
1468
			pm8001_dbg(pm8001_ha, INIT, "Encryption error !!\n");
1469
			if (pm8001_ha->encrypt_info.status == 0x81) {
1470 1471
				pm8001_dbg(pm8001_ha, INIT,
					   "Encryption enabled with error.Saving encryption key to flash\n");
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
				pm80xx_encrypt_update(pm8001_ha);
			}
		}
	}
	return 0;
}

static int mpi_uninit_check(struct pm8001_hba_info *pm8001_ha)
{
	u32 max_wait_count;
	u32 value;
	u32 gst_len_mpistate;
	init_pci_device_addresses(pm8001_ha);
	/* Write bit1=1 to Inbound DoorBell Register to tell the SPC FW the
	table is stop */
	pm8001_cw32(pm8001_ha, 0, MSGU_IBDB_SET, SPCv_MSGU_CFG_TABLE_RESET);

	/* wait until Inbound DoorBell Clear Register toggled */
1490
	if (IS_SPCV_12G(pm8001_ha->pdev)) {
1491
		max_wait_count = 30 * 1000 * 1000; /* 30 sec */
1492
	} else {
1493
		max_wait_count = 15 * 1000 * 1000; /* 15 sec */
1494
	}
1495 1496 1497 1498 1499 1500 1501
	do {
		udelay(1);
		value = pm8001_cr32(pm8001_ha, 0, MSGU_IBDB_SET);
		value &= SPCv_MSGU_CFG_TABLE_RESET;
	} while ((value != 0) && (--max_wait_count));

	if (!max_wait_count) {
1502
		pm8001_dbg(pm8001_ha, FAIL, "TIMEOUT:IBDB value/=%x\n", value);
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
		return -1;
	}

	/* check the MPI-State for termination in progress */
	/* wait until Inbound DoorBell Clear Register toggled */
	max_wait_count = 2 * 1000 * 1000;	/* 2 sec for spcv/ve */
	do {
		udelay(1);
		gst_len_mpistate =
			pm8001_mr32(pm8001_ha->general_stat_tbl_addr,
			GST_GSTLEN_MPIS_OFFSET);
		if (GST_MPI_STATE_UNINIT ==
			(gst_len_mpistate & GST_MPI_STATE_MASK))
			break;
	} while (--max_wait_count);
	if (!max_wait_count) {
1519 1520
		pm8001_dbg(pm8001_ha, FAIL, " TIME OUT MPI State = 0x%x\n",
			   gst_len_mpistate & GST_MPI_STATE_MASK);
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
		return -1;
	}

	return 0;
}

/**
 * pm8001_chip_soft_rst - soft reset the PM8001 chip, so that the clear all
 * the FW register status to the originated status.
 * @pm8001_ha: our hba card information
 */

static int
pm80xx_chip_soft_rst(struct pm8001_hba_info *pm8001_ha)
{
	u32 regval;
	u32 bootloader_state;
1538
	u32 ibutton0, ibutton1;
1539

1540 1541 1542 1543
	/* Process MPI table uninitialization only if FW is ready */
	if (!pm8001_ha->controller_fatal_error) {
		/* Check if MPI is in ready state to reset */
		if (mpi_uninit_check(pm8001_ha) != 0) {
1544 1545 1546 1547
			u32 r0 = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_0);
			u32 r1 = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1);
			u32 r2 = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_2);
			u32 r3 = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_3);
1548 1549 1550
			pm8001_dbg(pm8001_ha, FAIL,
				   "MPI state is not ready scratch: %x:%x:%x:%x\n",
				   r0, r1, r2, r3);
1551 1552 1553 1554 1555
			/* if things aren't ready but the bootloader is ok then
			 * try the reset anyway.
			 */
			if (r1 & SCRATCH_PAD1_BOOTSTATE_MASK)
				return -1;
1556
		}
1557 1558 1559
	}
	/* checked for reset register normal state; 0x0 */
	regval = pm8001_cr32(pm8001_ha, 0, SPC_REG_SOFT_RESET);
1560 1561
	pm8001_dbg(pm8001_ha, INIT, "reset register before write : 0x%x\n",
		   regval);
1562 1563

	pm8001_cw32(pm8001_ha, 0, SPC_REG_SOFT_RESET, SPCv_NORMAL_RESET_VALUE);
1564
	msleep(500);
1565 1566

	regval = pm8001_cr32(pm8001_ha, 0, SPC_REG_SOFT_RESET);
1567 1568
	pm8001_dbg(pm8001_ha, INIT, "reset register after write 0x%x\n",
		   regval);
1569 1570 1571

	if ((regval & SPCv_SOFT_RESET_READ_MASK) ==
			SPCv_SOFT_RESET_NORMAL_RESET_OCCURED) {
1572 1573 1574
		pm8001_dbg(pm8001_ha, MSG,
			   " soft reset successful [regval: 0x%x]\n",
			   regval);
1575
	} else {
1576 1577 1578
		pm8001_dbg(pm8001_ha, MSG,
			   " soft reset failed [regval: 0x%x]\n",
			   regval);
1579 1580 1581 1582 1583 1584 1585

		/* check bootloader is successfully executed or in HDA mode */
		bootloader_state =
			pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1) &
			SCRATCH_PAD1_BOOTSTATE_MASK;

		if (bootloader_state == SCRATCH_PAD1_BOOTSTATE_HDA_SEEPROM) {
1586 1587
			pm8001_dbg(pm8001_ha, MSG,
				   "Bootloader state - HDA mode SEEPROM\n");
1588 1589
		} else if (bootloader_state ==
				SCRATCH_PAD1_BOOTSTATE_HDA_BOOTSTRAP) {
1590 1591
			pm8001_dbg(pm8001_ha, MSG,
				   "Bootloader state - HDA mode Bootstrap Pin\n");
1592 1593
		} else if (bootloader_state ==
				SCRATCH_PAD1_BOOTSTATE_HDA_SOFTRESET) {
1594 1595
			pm8001_dbg(pm8001_ha, MSG,
				   "Bootloader state - HDA mode soft reset\n");
1596 1597
		} else if (bootloader_state ==
					SCRATCH_PAD1_BOOTSTATE_CRIT_ERROR) {
1598 1599
			pm8001_dbg(pm8001_ha, MSG,
				   "Bootloader state-HDA mode critical error\n");
1600 1601 1602 1603 1604 1605
		}
		return -EBUSY;
	}

	/* check the firmware status after reset */
	if (-1 == check_fw_ready(pm8001_ha)) {
1606
		pm8001_dbg(pm8001_ha, FAIL, "Firmware is not ready!\n");
1607 1608 1609
		/* check iButton feature support for motherboard controller */
		if (pm8001_ha->pdev->subsystem_vendor !=
			PCI_VENDOR_ID_ADAPTEC2 &&
1610 1611
			pm8001_ha->pdev->subsystem_vendor !=
			PCI_VENDOR_ID_ATTO &&
1612 1613 1614 1615 1616 1617
			pm8001_ha->pdev->subsystem_vendor != 0) {
			ibutton0 = pm8001_cr32(pm8001_ha, 0,
					MSGU_HOST_SCRATCH_PAD_6);
			ibutton1 = pm8001_cr32(pm8001_ha, 0,
					MSGU_HOST_SCRATCH_PAD_7);
			if (!ibutton0 && !ibutton1) {
1618 1619
				pm8001_dbg(pm8001_ha, FAIL,
					   "iButton Feature is not Available!!!\n");
1620 1621 1622
				return -EBUSY;
			}
			if (ibutton0 == 0xdeadbeef && ibutton1 == 0xdeadbeef) {
1623 1624
				pm8001_dbg(pm8001_ha, FAIL,
					   "CRC Check for iButton Feature Failed!!!\n");
1625 1626 1627
				return -EBUSY;
			}
		}
1628
	}
1629
	pm8001_dbg(pm8001_ha, INIT, "SPCv soft reset Complete\n");
1630 1631 1632 1633 1634
	return 0;
}

static void pm80xx_hw_chip_rst(struct pm8001_hba_info *pm8001_ha)
{
1635
	u32 i;
1636

1637
	pm8001_dbg(pm8001_ha, INIT, "chip reset start\n");
1638 1639 1640

	/* do SPCv chip reset. */
	pm8001_cw32(pm8001_ha, 0, SPC_REG_SOFT_RESET, 0x11);
1641
	pm8001_dbg(pm8001_ha, INIT, "SPC soft reset Complete\n");
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652

	/* Check this ..whether delay is required or no */
	/* delay 10 usec */
	udelay(10);

	/* wait for 20 msec until the firmware gets reloaded */
	i = 20;
	do {
		mdelay(1);
	} while ((--i) != 0);

1653
	pm8001_dbg(pm8001_ha, INIT, "chip reset finished\n");
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
}

/**
 * pm8001_chip_interrupt_enable - enable PM8001 chip interrupt
 * @pm8001_ha: our hba card information
 */
static void
pm80xx_chip_intx_interrupt_enable(struct pm8001_hba_info *pm8001_ha)
{
	pm8001_cw32(pm8001_ha, 0, MSGU_ODMR, ODMR_CLEAR_ALL);
	pm8001_cw32(pm8001_ha, 0, MSGU_ODCR, ODCR_CLEAR_ALL);
}

/**
 * pm8001_chip_intx_interrupt_disable- disable PM8001 chip interrupt
 * @pm8001_ha: our hba card information
 */
static void
pm80xx_chip_intx_interrupt_disable(struct pm8001_hba_info *pm8001_ha)
{
	pm8001_cw32(pm8001_ha, 0, MSGU_ODMR_CLR, ODMR_MASK_ALL);
}

/**
 * pm8001_chip_interrupt_enable - enable PM8001 chip interrupt
 * @pm8001_ha: our hba card information
1680
 * @vec: interrupt number to enable
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
 */
static void
pm80xx_chip_interrupt_enable(struct pm8001_hba_info *pm8001_ha, u8 vec)
{
#ifdef PM8001_USE_MSIX
	u32 mask;
	mask = (u32)(1 << vec);

	pm8001_cw32(pm8001_ha, 0, MSGU_ODMR_CLR, (u32)(mask & 0xFFFFFFFF));
	return;
#endif
	pm80xx_chip_intx_interrupt_enable(pm8001_ha);

}

/**
 * pm8001_chip_interrupt_disable- disable PM8001 chip interrupt
 * @pm8001_ha: our hba card information
1699
 * @vec: interrupt number to disable
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
 */
static void
pm80xx_chip_interrupt_disable(struct pm8001_hba_info *pm8001_ha, u8 vec)
{
#ifdef PM8001_USE_MSIX
	u32 mask;
	if (vec == 0xFF)
		mask = 0xFFFFFFFF;
	else
		mask = (u32)(1 << vec);
	pm8001_cw32(pm8001_ha, 0, MSGU_ODMR, (u32)(mask & 0xFFFFFFFF));
	return;
#endif
	pm80xx_chip_intx_interrupt_disable(pm8001_ha);
}

1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
static void pm80xx_send_abort_all(struct pm8001_hba_info *pm8001_ha,
		struct pm8001_device *pm8001_ha_dev)
{
	int res;
	u32 ccb_tag;
	struct pm8001_ccb_info *ccb;
	struct sas_task *task = NULL;
	struct task_abort_req task_abort;
	struct inbound_queue_table *circularQ;
	u32 opc = OPC_INB_SATA_ABORT;
	int ret;

	if (!pm8001_ha_dev) {
1729
		pm8001_dbg(pm8001_ha, FAIL, "dev is null\n");
1730 1731 1732 1733 1734 1735
		return;
	}

	task = sas_alloc_slow_task(GFP_ATOMIC);

	if (!task) {
1736
		pm8001_dbg(pm8001_ha, FAIL, "cannot allocate task\n");
1737 1738 1739 1740 1741 1742
		return;
	}

	task->task_done = pm8001_task_done;

	res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
T
Tomas Henzl 已提交
1743 1744
	if (res) {
		sas_free_task(task);
1745
		return;
T
Tomas Henzl 已提交
1746
	}
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759

	ccb = &pm8001_ha->ccb_info[ccb_tag];
	ccb->device = pm8001_ha_dev;
	ccb->ccb_tag = ccb_tag;
	ccb->task = task;

	circularQ = &pm8001_ha->inbnd_q_tbl[0];

	memset(&task_abort, 0, sizeof(task_abort));
	task_abort.abort_all = cpu_to_le32(1);
	task_abort.device_id = cpu_to_le32(pm8001_ha_dev->device_id);
	task_abort.tag = cpu_to_le32(ccb_tag);

1760 1761
	ret = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &task_abort,
			sizeof(task_abort), 0);
1762
	pm8001_dbg(pm8001_ha, FAIL, "Executing abort task end\n");
T
Tomas Henzl 已提交
1763 1764 1765 1766
	if (ret) {
		sas_free_task(task);
		pm8001_tag_free(pm8001_ha, ccb_tag);
	}
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
}

static void pm80xx_send_read_log(struct pm8001_hba_info *pm8001_ha,
		struct pm8001_device *pm8001_ha_dev)
{
	struct sata_start_req sata_cmd;
	int res;
	u32 ccb_tag;
	struct pm8001_ccb_info *ccb;
	struct sas_task *task = NULL;
	struct host_to_dev_fis fis;
	struct domain_device *dev;
	struct inbound_queue_table *circularQ;
	u32 opc = OPC_INB_SATA_HOST_OPSTART;

	task = sas_alloc_slow_task(GFP_ATOMIC);

	if (!task) {
1785
		pm8001_dbg(pm8001_ha, FAIL, "cannot allocate task !!!\n");
1786 1787 1788 1789 1790 1791
		return;
	}
	task->task_done = pm8001_task_done;

	res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
	if (res) {
T
Tomas Henzl 已提交
1792
		sas_free_task(task);
1793
		pm8001_dbg(pm8001_ha, FAIL, "cannot allocate tag !!!\n");
1794 1795 1796 1797 1798 1799 1800 1801
		return;
	}

	/* allocate domain device by ourselves as libsas
	 * is not going to provide any
	*/
	dev = kzalloc(sizeof(struct domain_device), GFP_ATOMIC);
	if (!dev) {
T
Tomas Henzl 已提交
1802 1803
		sas_free_task(task);
		pm8001_tag_free(pm8001_ha, ccb_tag);
1804 1805
		pm8001_dbg(pm8001_ha, FAIL,
			   "Domain device cannot be allocated\n");
1806 1807 1808
		return;
	}

T
Tomas Henzl 已提交
1809 1810 1811
	task->dev = dev;
	task->dev->lldd_dev = pm8001_ha_dev;

1812 1813 1814 1815
	ccb = &pm8001_ha->ccb_info[ccb_tag];
	ccb->device = pm8001_ha_dev;
	ccb->ccb_tag = ccb_tag;
	ccb->task = task;
1816
	ccb->n_elem = 0;
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
	pm8001_ha_dev->id |= NCQ_READ_LOG_FLAG;
	pm8001_ha_dev->id |= NCQ_2ND_RLE_FLAG;

	memset(&sata_cmd, 0, sizeof(sata_cmd));
	circularQ = &pm8001_ha->inbnd_q_tbl[0];

	/* construct read log FIS */
	memset(&fis, 0, sizeof(struct host_to_dev_fis));
	fis.fis_type = 0x27;
	fis.flags = 0x80;
	fis.command = ATA_CMD_READ_LOG_EXT;
	fis.lbal = 0x10;
	fis.sector_count = 0x1;

	sata_cmd.tag = cpu_to_le32(ccb_tag);
	sata_cmd.device_id = cpu_to_le32(pm8001_ha_dev->device_id);
	sata_cmd.ncqtag_atap_dir_m_dad |= ((0x1 << 7) | (0x5 << 9));
	memcpy(&sata_cmd.sata_fis, &fis, sizeof(struct host_to_dev_fis));

1836 1837
	res = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &sata_cmd,
			sizeof(sata_cmd), 0);
1838
	pm8001_dbg(pm8001_ha, FAIL, "Executing read log end\n");
T
Tomas Henzl 已提交
1839 1840 1841 1842 1843
	if (res) {
		sas_free_task(task);
		pm8001_tag_free(pm8001_ha, ccb_tag);
		kfree(dev);
	}
1844 1845
}

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
/**
 * mpi_ssp_completion- process the event that FW response to the SSP request.
 * @pm8001_ha: our hba card information
 * @piomb: the message contents of this outbound message.
 *
 * When FW has completed a ssp request for example a IO request, after it has
 * filled the SG data with the data, it will trigger this event represent
 * that he has finished the job,please check the coresponding buffer.
 * So we will tell the caller who maybe waiting the result to tell upper layer
 * that the task has been finished.
 */
static void
mpi_ssp_completion(struct pm8001_hba_info *pm8001_ha , void *piomb)
{
	struct sas_task *t;
	struct pm8001_ccb_info *ccb;
	unsigned long flags;
	u32 status;
	u32 param;
	u32 tag;
	struct ssp_completion_resp *psspPayload;
	struct task_status_struct *ts;
	struct ssp_response_iu *iu;
	struct pm8001_device *pm8001_dev;
	psspPayload = (struct ssp_completion_resp *)(piomb + 4);
	status = le32_to_cpu(psspPayload->status);
	tag = le32_to_cpu(psspPayload->tag);
	ccb = &pm8001_ha->ccb_info[tag];
	if ((status == IO_ABORTED) && ccb->open_retry) {
		/* Being completed by another */
		ccb->open_retry = 0;
		return;
	}
	pm8001_dev = ccb->device;
	param = le32_to_cpu(psspPayload->param);
	t = ccb->task;

	if (status && status != IO_UNDERFLOW)
1884
		pm8001_dbg(pm8001_ha, FAIL, "sas IO status 0x%x\n", status);
1885 1886 1887
	if (unlikely(!t || !t->lldd_task || !t->dev))
		return;
	ts = &t->task_status;
1888

1889 1890
	pm8001_dbg(pm8001_ha, DEV,
		   "tag::0x%x, status::0x%x task::0x%p\n", tag, status, t);
1891

1892 1893 1894
	/* Print sas address of IO failed device */
	if ((status != IO_SUCCESS) && (status != IO_OVERFLOW) &&
		(status != IO_UNDERFLOW))
1895 1896
		pm8001_dbg(pm8001_ha, FAIL, "SAS Address of IO Failure Drive:%016llx\n",
			   SAS_ADDR(t->dev->sas_addr));
1897

1898 1899
	switch (status) {
	case IO_SUCCESS:
1900 1901
		pm8001_dbg(pm8001_ha, IO, "IO_SUCCESS ,param = 0x%x\n",
			   param);
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
		if (param == 0) {
			ts->resp = SAS_TASK_COMPLETE;
			ts->stat = SAM_STAT_GOOD;
		} else {
			ts->resp = SAS_TASK_COMPLETE;
			ts->stat = SAS_PROTO_RESPONSE;
			ts->residual = param;
			iu = &psspPayload->ssp_resp_iu;
			sas_ssp_task_response(pm8001_ha->dev, t, iu);
		}
		if (pm8001_dev)
V
Viswas G 已提交
1913
			atomic_dec(&pm8001_dev->running_req);
1914 1915
		break;
	case IO_ABORTED:
1916
		pm8001_dbg(pm8001_ha, IO, "IO_ABORTED IOMB Tag\n");
1917 1918
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_ABORTED_TASK;
V
Viswas G 已提交
1919 1920
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1921 1922 1923
		break;
	case IO_UNDERFLOW:
		/* SSP Completion with error */
1924 1925
		pm8001_dbg(pm8001_ha, IO, "IO_UNDERFLOW ,param = 0x%x\n",
			   param);
1926 1927 1928 1929
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_UNDERRUN;
		ts->residual = param;
		if (pm8001_dev)
V
Viswas G 已提交
1930
			atomic_dec(&pm8001_dev->running_req);
1931 1932
		break;
	case IO_NO_DEVICE:
1933
		pm8001_dbg(pm8001_ha, IO, "IO_NO_DEVICE\n");
1934 1935
		ts->resp = SAS_TASK_UNDELIVERED;
		ts->stat = SAS_PHY_DOWN;
V
Viswas G 已提交
1936 1937
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1938 1939
		break;
	case IO_XFER_ERROR_BREAK:
1940
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_BREAK\n");
1941 1942 1943 1944
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		/* Force the midlayer to retry */
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
1945 1946
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1947 1948
		break;
	case IO_XFER_ERROR_PHY_NOT_READY:
1949
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_PHY_NOT_READY\n");
1950
		ts->resp = SAS_TASK_COMPLETE;
1951 1952
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
1953 1954
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1955 1956
		break;
	case IO_XFER_ERROR_INVALID_SSP_RSP_FRAME:
1957 1958
		pm8001_dbg(pm8001_ha, IO,
			   "IO_XFER_ERROR_INVALID_SSP_RSP_FRAME\n");
1959
		ts->resp = SAS_TASK_COMPLETE;
1960 1961
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
1962 1963
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1964 1965
		break;
	case IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED:
1966 1967
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED\n");
1968 1969 1970
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_EPROTO;
V
Viswas G 已提交
1971 1972
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1973 1974
		break;
	case IO_OPEN_CNX_ERROR_ZONE_VIOLATION:
1975 1976
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_ZONE_VIOLATION\n");
1977 1978 1979
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
V
Viswas G 已提交
1980 1981
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1982 1983
		break;
	case IO_OPEN_CNX_ERROR_BREAK:
1984
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_BREAK\n");
1985 1986 1987
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
1988 1989
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
1990 1991
		break;
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
1992 1993 1994 1995 1996
	case IO_XFER_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED:
1997
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS\n");
1998 1999 2000 2001 2002 2003 2004 2005 2006
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
		if (!t->uldd_task)
			pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS);
		break;
	case IO_OPEN_CNX_ERROR_BAD_DESTINATION:
2007 2008
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_BAD_DESTINATION\n");
2009 2010 2011
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_BAD_DEST;
V
Viswas G 已提交
2012 2013
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2014 2015
		break;
	case IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED:
2016 2017
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED\n");
2018 2019 2020
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_CONN_RATE;
V
Viswas G 已提交
2021 2022
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2023 2024
		break;
	case IO_OPEN_CNX_ERROR_WRONG_DESTINATION:
2025 2026
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_WRONG_DESTINATION\n");
2027 2028 2029
		ts->resp = SAS_TASK_UNDELIVERED;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
V
Viswas G 已提交
2030 2031
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2032 2033
		break;
	case IO_XFER_ERROR_NAK_RECEIVED:
2034
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_NAK_RECEIVED\n");
2035 2036 2037
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
2038 2039
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2040 2041
		break;
	case IO_XFER_ERROR_ACK_NAK_TIMEOUT:
2042
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_ACK_NAK_TIMEOUT\n");
2043 2044
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_NAK_R_ERR;
V
Viswas G 已提交
2045 2046
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2047 2048
		break;
	case IO_XFER_ERROR_DMA:
2049
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_DMA\n");
2050 2051
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
V
Viswas G 已提交
2052 2053
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2054 2055
		break;
	case IO_XFER_OPEN_RETRY_TIMEOUT:
2056
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_OPEN_RETRY_TIMEOUT\n");
2057 2058 2059
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
2060 2061
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2062 2063
		break;
	case IO_XFER_ERROR_OFFSET_MISMATCH:
2064
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_OFFSET_MISMATCH\n");
2065 2066
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
V
Viswas G 已提交
2067 2068
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2069 2070
		break;
	case IO_PORT_IN_RESET:
2071
		pm8001_dbg(pm8001_ha, IO, "IO_PORT_IN_RESET\n");
2072 2073
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
V
Viswas G 已提交
2074 2075
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2076 2077
		break;
	case IO_DS_NON_OPERATIONAL:
2078
		pm8001_dbg(pm8001_ha, IO, "IO_DS_NON_OPERATIONAL\n");
2079 2080 2081 2082 2083 2084 2085 2086
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		if (!t->uldd_task)
			pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_DS_NON_OPERATIONAL);
		break;
	case IO_DS_IN_RECOVERY:
2087
		pm8001_dbg(pm8001_ha, IO, "IO_DS_IN_RECOVERY\n");
2088 2089
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
V
Viswas G 已提交
2090 2091
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2092 2093
		break;
	case IO_TM_TAG_NOT_FOUND:
2094
		pm8001_dbg(pm8001_ha, IO, "IO_TM_TAG_NOT_FOUND\n");
2095 2096
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
V
Viswas G 已提交
2097 2098
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2099 2100
		break;
	case IO_SSP_EXT_IU_ZERO_LEN_ERROR:
2101
		pm8001_dbg(pm8001_ha, IO, "IO_SSP_EXT_IU_ZERO_LEN_ERROR\n");
2102 2103
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
V
Viswas G 已提交
2104 2105
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2106 2107
		break;
	case IO_OPEN_CNX_ERROR_HW_RESOURCE_BUSY:
2108 2109
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_HW_RESOURCE_BUSY\n");
2110 2111 2112
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
2113 2114
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2115 2116
		break;
	default:
2117
		pm8001_dbg(pm8001_ha, DEVIO, "Unknown status 0x%x\n", status);
2118 2119 2120
		/* not allowed case. Therefore, return failed status */
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
V
Viswas G 已提交
2121 2122
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2123 2124
		break;
	}
2125 2126
	pm8001_dbg(pm8001_ha, IO, "scsi_status = 0x%x\n ",
		   psspPayload->ssp_resp_iu.status);
2127 2128 2129 2130 2131 2132
	spin_lock_irqsave(&t->task_state_lock, flags);
	t->task_state_flags &= ~SAS_TASK_STATE_PENDING;
	t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
	t->task_state_flags |= SAS_TASK_STATE_DONE;
	if (unlikely((t->task_state_flags & SAS_TASK_STATE_ABORTED))) {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
2133 2134 2135
		pm8001_dbg(pm8001_ha, FAIL,
			   "task 0x%p done with io_status 0x%x resp 0x%x stat 0x%x but aborted by upper layer!\n",
			   t, status, ts->resp, ts->stat);
2136 2137
		if (t->slow_task)
			complete(&t->slow_task->completion);
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
	} else {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
		mb();/* in order to force CPU ordering */
		t->task_done(t);
	}
}

/*See the comments for mpi_ssp_completion */
static void mpi_ssp_event(struct pm8001_hba_info *pm8001_ha , void *piomb)
{
	struct sas_task *t;
	unsigned long flags;
	struct task_status_struct *ts;
	struct pm8001_ccb_info *ccb;
	struct pm8001_device *pm8001_dev;
	struct ssp_event_resp *psspPayload =
		(struct ssp_event_resp *)(piomb + 4);
	u32 event = le32_to_cpu(psspPayload->event);
	u32 tag = le32_to_cpu(psspPayload->tag);
	u32 port_id = le32_to_cpu(psspPayload->port_id);

	ccb = &pm8001_ha->ccb_info[tag];
	t = ccb->task;
	pm8001_dev = ccb->device;
	if (event)
2165
		pm8001_dbg(pm8001_ha, FAIL, "sas IO status 0x%x\n", event);
2166 2167 2168
	if (unlikely(!t || !t->lldd_task || !t->dev))
		return;
	ts = &t->task_status;
2169 2170
	pm8001_dbg(pm8001_ha, IOERR, "port_id:0x%x, tag:0x%x, event:0x%x\n",
		   port_id, tag, event);
2171 2172
	switch (event) {
	case IO_OVERFLOW:
2173
		pm8001_dbg(pm8001_ha, IO, "IO_UNDERFLOW\n");
2174 2175 2176 2177
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		ts->residual = 0;
		if (pm8001_dev)
V
Viswas G 已提交
2178
			atomic_dec(&pm8001_dev->running_req);
2179 2180
		break;
	case IO_XFER_ERROR_BREAK:
2181
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_BREAK\n");
2182 2183 2184
		pm8001_handle_event(pm8001_ha, t, IO_XFER_ERROR_BREAK);
		return;
	case IO_XFER_ERROR_PHY_NOT_READY:
2185
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_PHY_NOT_READY\n");
2186 2187 2188 2189 2190
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	case IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED:
2191 2192
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED\n");
2193 2194 2195 2196 2197
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_EPROTO;
		break;
	case IO_OPEN_CNX_ERROR_ZONE_VIOLATION:
2198 2199
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_ZONE_VIOLATION\n");
2200 2201 2202 2203 2204
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
		break;
	case IO_OPEN_CNX_ERROR_BREAK:
2205
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_BREAK\n");
2206 2207 2208 2209 2210
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
2211 2212 2213 2214 2215
	case IO_XFER_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED:
2216
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS\n");
2217 2218 2219 2220 2221 2222 2223 2224 2225
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
		if (!t->uldd_task)
			pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS);
		break;
	case IO_OPEN_CNX_ERROR_BAD_DESTINATION:
2226 2227
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_BAD_DESTINATION\n");
2228 2229 2230 2231 2232
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_BAD_DEST;
		break;
	case IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED:
2233 2234
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED\n");
2235 2236 2237 2238 2239
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_CONN_RATE;
		break;
	case IO_OPEN_CNX_ERROR_WRONG_DESTINATION:
2240 2241
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_WRONG_DESTINATION\n");
2242 2243 2244 2245 2246
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
		break;
	case IO_XFER_ERROR_NAK_RECEIVED:
2247
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_NAK_RECEIVED\n");
2248 2249 2250 2251 2252
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	case IO_XFER_ERROR_ACK_NAK_TIMEOUT:
2253
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_ACK_NAK_TIMEOUT\n");
2254 2255 2256 2257
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_NAK_R_ERR;
		break;
	case IO_XFER_OPEN_RETRY_TIMEOUT:
2258
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_OPEN_RETRY_TIMEOUT\n");
2259 2260 2261
		pm8001_handle_event(pm8001_ha, t, IO_XFER_OPEN_RETRY_TIMEOUT);
		return;
	case IO_XFER_ERROR_UNEXPECTED_PHASE:
2262
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_UNEXPECTED_PHASE\n");
2263 2264 2265 2266
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
	case IO_XFER_ERROR_XFER_RDY_OVERRUN:
2267
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_XFER_RDY_OVERRUN\n");
2268 2269 2270 2271
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
	case IO_XFER_ERROR_XFER_RDY_NOT_EXPECTED:
2272 2273
		pm8001_dbg(pm8001_ha, IO,
			   "IO_XFER_ERROR_XFER_RDY_NOT_EXPECTED\n");
2274 2275 2276 2277
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
	case IO_XFER_ERROR_CMD_ISSUE_ACK_NAK_TIMEOUT:
2278 2279
		pm8001_dbg(pm8001_ha, IO,
			   "IO_XFER_ERROR_CMD_ISSUE_ACK_NAK_TIMEOUT\n");
2280 2281 2282 2283
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
	case IO_XFER_ERROR_OFFSET_MISMATCH:
2284
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_OFFSET_MISMATCH\n");
2285 2286 2287 2288
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
	case IO_XFER_ERROR_XFER_ZERO_DATA_LEN:
2289 2290
		pm8001_dbg(pm8001_ha, IO,
			   "IO_XFER_ERROR_XFER_ZERO_DATA_LEN\n");
2291 2292 2293
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
2294
	case IO_XFER_ERROR_INTERNAL_CRC_ERROR:
2295 2296
		pm8001_dbg(pm8001_ha, IOERR,
			   "IO_XFR_ERROR_INTERNAL_CRC_ERROR\n");
2297 2298 2299 2300
		/* TBC: used default set values */
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
2301
	case IO_XFER_CMD_FRAME_ISSUED:
2302
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_CMD_FRAME_ISSUED\n");
2303 2304
		return;
	default:
2305
		pm8001_dbg(pm8001_ha, DEVIO, "Unknown status 0x%x\n", event);
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
		/* not allowed case. Therefore, return failed status */
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		break;
	}
	spin_lock_irqsave(&t->task_state_lock, flags);
	t->task_state_flags &= ~SAS_TASK_STATE_PENDING;
	t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
	t->task_state_flags |= SAS_TASK_STATE_DONE;
	if (unlikely((t->task_state_flags & SAS_TASK_STATE_ABORTED))) {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
2317 2318 2319
		pm8001_dbg(pm8001_ha, FAIL,
			   "task 0x%p done with event 0x%x resp 0x%x stat 0x%x but aborted by upper layer!\n",
			   t, event, ts->resp, ts->stat);
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
	} else {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
		mb();/* in order to force CPU ordering */
		t->task_done(t);
	}
}

/*See the comments for mpi_ssp_completion */
static void
mpi_sata_completion(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	struct sas_task *t;
	struct pm8001_ccb_info *ccb;
	u32 param;
	u32 status;
	u32 tag;
2338 2339 2340 2341
	int i, j;
	u8 sata_addr_low[4];
	u32 temp_sata_addr_low, temp_sata_addr_hi;
	u8 sata_addr_hi[4];
2342 2343 2344 2345 2346
	struct sata_completion_resp *psataPayload;
	struct task_status_struct *ts;
	struct ata_task_resp *resp ;
	u32 *sata_resp;
	struct pm8001_device *pm8001_dev;
2347
	unsigned long flags;
2348 2349 2350 2351 2352

	psataPayload = (struct sata_completion_resp *)(piomb + 4);
	status = le32_to_cpu(psataPayload->status);
	tag = le32_to_cpu(psataPayload->tag);

2353
	if (!tag) {
2354
		pm8001_dbg(pm8001_ha, FAIL, "tag null\n");
2355 2356
		return;
	}
2357 2358
	ccb = &pm8001_ha->ccb_info[tag];
	param = le32_to_cpu(psataPayload->param);
2359 2360 2361 2362
	if (ccb) {
		t = ccb->task;
		pm8001_dev = ccb->device;
	} else {
2363
		pm8001_dbg(pm8001_ha, FAIL, "ccb null\n");
2364 2365 2366 2367 2368 2369 2370
		return;
	}

	if (t) {
		if (t->dev && (t->dev->lldd_dev))
			pm8001_dev = t->dev->lldd_dev;
	} else {
2371
		pm8001_dbg(pm8001_ha, FAIL, "task null\n");
2372 2373 2374 2375 2376
		return;
	}

	if ((pm8001_dev && !(pm8001_dev->id & NCQ_READ_LOG_FLAG))
		&& unlikely(!t || !t->lldd_task || !t->dev)) {
2377
		pm8001_dbg(pm8001_ha, FAIL, "task or dev null\n");
2378 2379 2380
		return;
	}

2381
	ts = &t->task_status;
2382
	if (!ts) {
2383
		pm8001_dbg(pm8001_ha, FAIL, "ts null\n");
2384
		return;
2385
	}
2386 2387

	if (unlikely(status))
2388 2389 2390
		pm8001_dbg(pm8001_ha, IOERR,
			   "status:0x%x, tag:0x%x, task::0x%p\n",
			   status, tag, t);
2391

2392 2393 2394 2395
	/* Print sas address of IO failed device */
	if ((status != IO_SUCCESS) && (status != IO_OVERFLOW) &&
		(status != IO_UNDERFLOW)) {
		if (!((t->dev->parent) &&
2396
			(dev_is_expander(t->dev->parent->dev_type)))) {
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
			for (i = 0 , j = 4; i <= 3 && j <= 7; i++ , j++)
				sata_addr_low[i] = pm8001_ha->sas_addr[j];
			for (i = 0 , j = 0; i <= 3 && j <= 3; i++ , j++)
				sata_addr_hi[i] = pm8001_ha->sas_addr[j];
			memcpy(&temp_sata_addr_low, sata_addr_low,
				sizeof(sata_addr_low));
			memcpy(&temp_sata_addr_hi, sata_addr_hi,
				sizeof(sata_addr_hi));
			temp_sata_addr_hi = (((temp_sata_addr_hi >> 24) & 0xff)
						|((temp_sata_addr_hi << 8) &
						0xff0000) |
						((temp_sata_addr_hi >> 8)
						& 0xff00) |
						((temp_sata_addr_hi << 24) &
						0xff000000));
			temp_sata_addr_low = ((((temp_sata_addr_low >> 24)
						& 0xff) |
						((temp_sata_addr_low << 8)
						& 0xff0000) |
						((temp_sata_addr_low >> 8)
						& 0xff00) |
						((temp_sata_addr_low << 24)
						& 0xff000000)) +
						pm8001_dev->attached_phy +
						0x10);
2422 2423 2424 2425
			pm8001_dbg(pm8001_ha, FAIL,
				   "SAS Address of IO Failure Drive:%08x%08x\n",
				   temp_sata_addr_hi,
				   temp_sata_addr_low);
2426

2427
		} else {
2428 2429 2430
			pm8001_dbg(pm8001_ha, FAIL,
				   "SAS Address of IO Failure Drive:%016llx\n",
				   SAS_ADDR(t->dev->sas_addr));
2431 2432
		}
	}
2433 2434
	switch (status) {
	case IO_SUCCESS:
2435
		pm8001_dbg(pm8001_ha, IO, "IO_SUCCESS\n");
2436 2437 2438
		if (param == 0) {
			ts->resp = SAS_TASK_COMPLETE;
			ts->stat = SAM_STAT_GOOD;
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
			/* check if response is for SEND READ LOG */
			if (pm8001_dev &&
				(pm8001_dev->id & NCQ_READ_LOG_FLAG)) {
				/* set new bit for abort_all */
				pm8001_dev->id |= NCQ_ABORT_ALL_FLAG;
				/* clear bit for read log */
				pm8001_dev->id = pm8001_dev->id & 0x7FFFFFFF;
				pm80xx_send_abort_all(pm8001_ha, pm8001_dev);
				/* Free the tag */
				pm8001_tag_free(pm8001_ha, tag);
				sas_free_task(t);
				return;
			}
2452 2453 2454 2455 2456
		} else {
			u8 len;
			ts->resp = SAS_TASK_COMPLETE;
			ts->stat = SAS_PROTO_RESPONSE;
			ts->residual = param;
2457 2458 2459
			pm8001_dbg(pm8001_ha, IO,
				   "SAS_PROTO_RESPONSE len = %d\n",
				   param);
2460 2461 2462
			sata_resp = &psataPayload->sata_resp[0];
			resp = (struct ata_task_resp *)ts->buf;
			if (t->ata_task.dma_xfer == 0 &&
2463
			    t->data_dir == DMA_FROM_DEVICE) {
2464
				len = sizeof(struct pio_setup_fis);
2465 2466
				pm8001_dbg(pm8001_ha, IO,
					   "PIO read len = %d\n", len);
2467 2468
			} else if (t->ata_task.use_ncq) {
				len = sizeof(struct set_dev_bits_fis);
2469 2470
				pm8001_dbg(pm8001_ha, IO, "FPDMA len = %d\n",
					   len);
2471 2472
			} else {
				len = sizeof(struct dev_to_host_fis);
2473 2474
				pm8001_dbg(pm8001_ha, IO, "other len = %d\n",
					   len);
2475 2476 2477 2478 2479 2480
			}
			if (SAS_STATUS_BUF_SIZE >= sizeof(*resp)) {
				resp->frame_len = len;
				memcpy(&resp->ending_fis[0], sata_resp, len);
				ts->buf_valid_size = sizeof(*resp);
			} else
2481 2482
				pm8001_dbg(pm8001_ha, IO,
					   "response too large\n");
2483 2484
		}
		if (pm8001_dev)
V
Viswas G 已提交
2485
			atomic_dec(&pm8001_dev->running_req);
2486 2487
		break;
	case IO_ABORTED:
2488
		pm8001_dbg(pm8001_ha, IO, "IO_ABORTED IOMB Tag\n");
2489 2490 2491
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_ABORTED_TASK;
		if (pm8001_dev)
V
Viswas G 已提交
2492
			atomic_dec(&pm8001_dev->running_req);
2493 2494 2495 2496
		break;
		/* following cases are to do cases */
	case IO_UNDERFLOW:
		/* SATA Completion with error */
2497
		pm8001_dbg(pm8001_ha, IO, "IO_UNDERFLOW param = %d\n", param);
2498 2499 2500 2501
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_UNDERRUN;
		ts->residual = param;
		if (pm8001_dev)
V
Viswas G 已提交
2502
			atomic_dec(&pm8001_dev->running_req);
2503 2504
		break;
	case IO_NO_DEVICE:
2505
		pm8001_dbg(pm8001_ha, IO, "IO_NO_DEVICE\n");
2506 2507
		ts->resp = SAS_TASK_UNDELIVERED;
		ts->stat = SAS_PHY_DOWN;
V
Viswas G 已提交
2508 2509
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2510 2511
		break;
	case IO_XFER_ERROR_BREAK:
2512
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_BREAK\n");
2513 2514
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_INTERRUPTED;
V
Viswas G 已提交
2515 2516
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2517 2518
		break;
	case IO_XFER_ERROR_PHY_NOT_READY:
2519
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_PHY_NOT_READY\n");
2520 2521 2522
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
2523 2524
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2525 2526
		break;
	case IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED:
2527 2528
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED\n");
2529 2530 2531
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_EPROTO;
V
Viswas G 已提交
2532 2533
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2534 2535
		break;
	case IO_OPEN_CNX_ERROR_ZONE_VIOLATION:
2536 2537
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_ZONE_VIOLATION\n");
2538 2539 2540
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
V
Viswas G 已提交
2541 2542
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2543 2544
		break;
	case IO_OPEN_CNX_ERROR_BREAK:
2545
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_BREAK\n");
2546 2547 2548
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_CONT0;
V
Viswas G 已提交
2549 2550
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2551 2552
		break;
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
2553 2554 2555 2556 2557
	case IO_XFER_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED:
2558
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS\n");
2559 2560 2561 2562 2563 2564 2565 2566
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
		if (!t->uldd_task) {
			pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS);
			ts->resp = SAS_TASK_UNDELIVERED;
			ts->stat = SAS_QUEUE_FULL;
S
Suresh Thiagarajan 已提交
2567
			pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2568 2569 2570 2571
			return;
		}
		break;
	case IO_OPEN_CNX_ERROR_BAD_DESTINATION:
2572 2573
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_BAD_DESTINATION\n");
2574 2575 2576 2577 2578 2579 2580 2581 2582
		ts->resp = SAS_TASK_UNDELIVERED;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_BAD_DEST;
		if (!t->uldd_task) {
			pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS);
			ts->resp = SAS_TASK_UNDELIVERED;
			ts->stat = SAS_QUEUE_FULL;
S
Suresh Thiagarajan 已提交
2583
			pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2584 2585 2586 2587
			return;
		}
		break;
	case IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED:
2588 2589
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED\n");
2590 2591 2592
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_CONN_RATE;
V
Viswas G 已提交
2593 2594
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2595 2596
		break;
	case IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY:
2597 2598
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY\n");
2599 2600 2601 2602 2603 2604 2605 2606
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
		if (!t->uldd_task) {
			pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_OPEN_CNX_ERROR_STP_RESOURCES_BUSY);
			ts->resp = SAS_TASK_UNDELIVERED;
			ts->stat = SAS_QUEUE_FULL;
S
Suresh Thiagarajan 已提交
2607
			pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2608 2609 2610 2611
			return;
		}
		break;
	case IO_OPEN_CNX_ERROR_WRONG_DESTINATION:
2612 2613
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_WRONG_DESTINATION\n");
2614 2615 2616
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
V
Viswas G 已提交
2617 2618
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2619 2620
		break;
	case IO_XFER_ERROR_NAK_RECEIVED:
2621
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_NAK_RECEIVED\n");
2622 2623
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_NAK_R_ERR;
V
Viswas G 已提交
2624 2625
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2626 2627
		break;
	case IO_XFER_ERROR_ACK_NAK_TIMEOUT:
2628
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_ACK_NAK_TIMEOUT\n");
2629 2630
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_NAK_R_ERR;
V
Viswas G 已提交
2631 2632
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2633 2634
		break;
	case IO_XFER_ERROR_DMA:
2635
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_DMA\n");
2636 2637
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_ABORTED_TASK;
V
Viswas G 已提交
2638 2639
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2640 2641
		break;
	case IO_XFER_ERROR_SATA_LINK_TIMEOUT:
2642
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_SATA_LINK_TIMEOUT\n");
2643 2644
		ts->resp = SAS_TASK_UNDELIVERED;
		ts->stat = SAS_DEV_NO_RESPONSE;
V
Viswas G 已提交
2645 2646
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2647 2648
		break;
	case IO_XFER_ERROR_REJECTED_NCQ_MODE:
2649
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_REJECTED_NCQ_MODE\n");
2650 2651
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_UNDERRUN;
V
Viswas G 已提交
2652 2653
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2654 2655
		break;
	case IO_XFER_OPEN_RETRY_TIMEOUT:
2656
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_OPEN_RETRY_TIMEOUT\n");
2657 2658
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
V
Viswas G 已提交
2659 2660
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2661 2662
		break;
	case IO_PORT_IN_RESET:
2663
		pm8001_dbg(pm8001_ha, IO, "IO_PORT_IN_RESET\n");
2664 2665
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
V
Viswas G 已提交
2666 2667
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2668 2669
		break;
	case IO_DS_NON_OPERATIONAL:
2670
		pm8001_dbg(pm8001_ha, IO, "IO_DS_NON_OPERATIONAL\n");
2671 2672 2673 2674 2675 2676 2677
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
		if (!t->uldd_task) {
			pm8001_handle_event(pm8001_ha, pm8001_dev,
					IO_DS_NON_OPERATIONAL);
			ts->resp = SAS_TASK_UNDELIVERED;
			ts->stat = SAS_QUEUE_FULL;
S
Suresh Thiagarajan 已提交
2678
			pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2679 2680 2681 2682
			return;
		}
		break;
	case IO_DS_IN_RECOVERY:
2683
		pm8001_dbg(pm8001_ha, IO, "IO_DS_IN_RECOVERY\n");
2684 2685
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
V
Viswas G 已提交
2686 2687
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2688 2689
		break;
	case IO_DS_IN_ERROR:
2690
		pm8001_dbg(pm8001_ha, IO, "IO_DS_IN_ERROR\n");
2691 2692 2693 2694 2695 2696 2697
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
		if (!t->uldd_task) {
			pm8001_handle_event(pm8001_ha, pm8001_dev,
					IO_DS_IN_ERROR);
			ts->resp = SAS_TASK_UNDELIVERED;
			ts->stat = SAS_QUEUE_FULL;
S
Suresh Thiagarajan 已提交
2698
			pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2699 2700 2701 2702
			return;
		}
		break;
	case IO_OPEN_CNX_ERROR_HW_RESOURCE_BUSY:
2703 2704
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_HW_RESOURCE_BUSY\n");
2705 2706 2707
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
V
Viswas G 已提交
2708 2709
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2710
		break;
2711
	default:
2712
		pm8001_dbg(pm8001_ha, DEVIO, "Unknown status 0x%x\n", status);
2713 2714 2715
		/* not allowed case. Therefore, return failed status */
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
V
Viswas G 已提交
2716 2717
		if (pm8001_dev)
			atomic_dec(&pm8001_dev->running_req);
2718 2719 2720 2721 2722 2723 2724 2725
		break;
	}
	spin_lock_irqsave(&t->task_state_lock, flags);
	t->task_state_flags &= ~SAS_TASK_STATE_PENDING;
	t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
	t->task_state_flags |= SAS_TASK_STATE_DONE;
	if (unlikely((t->task_state_flags & SAS_TASK_STATE_ABORTED))) {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
2726 2727 2728
		pm8001_dbg(pm8001_ha, FAIL,
			   "task 0x%p done with io_status 0x%x resp 0x%x stat 0x%x but aborted by upper layer!\n",
			   t, status, ts->resp, ts->stat);
2729 2730
		if (t->slow_task)
			complete(&t->slow_task->completion);
2731
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
S
Suresh Thiagarajan 已提交
2732
	} else {
2733
		spin_unlock_irqrestore(&t->task_state_lock, flags);
S
Suresh Thiagarajan 已提交
2734
		pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
	}
}

/*See the comments for mpi_ssp_completion */
static void mpi_sata_event(struct pm8001_hba_info *pm8001_ha , void *piomb)
{
	struct sas_task *t;
	struct task_status_struct *ts;
	struct pm8001_ccb_info *ccb;
	struct pm8001_device *pm8001_dev;
	struct sata_event_resp *psataPayload =
		(struct sata_event_resp *)(piomb + 4);
	u32 event = le32_to_cpu(psataPayload->event);
	u32 tag = le32_to_cpu(psataPayload->tag);
	u32 port_id = le32_to_cpu(psataPayload->port_id);
2750 2751
	u32 dev_id = le32_to_cpu(psataPayload->device_id);
	unsigned long flags;
2752 2753

	ccb = &pm8001_ha->ccb_info[tag];
2754 2755 2756 2757 2758

	if (ccb) {
		t = ccb->task;
		pm8001_dev = ccb->device;
	} else {
2759
		pm8001_dbg(pm8001_ha, FAIL, "No CCB !!!. returning\n");
2760 2761
		return;
	}
2762
	if (event)
2763
		pm8001_dbg(pm8001_ha, FAIL, "SATA EVENT 0x%x\n", event);
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775

	/* Check if this is NCQ error */
	if (event == IO_XFER_ERROR_ABORTED_NCQ_MODE) {
		/* find device using device id */
		pm8001_dev = pm8001_find_dev(pm8001_ha, dev_id);
		/* send read log extension */
		if (pm8001_dev)
			pm80xx_send_read_log(pm8001_ha, pm8001_dev);
		return;
	}

	if (unlikely(!t || !t->lldd_task || !t->dev)) {
2776
		pm8001_dbg(pm8001_ha, FAIL, "task or dev null\n");
2777
		return;
2778 2779
	}

2780
	ts = &t->task_status;
2781 2782
	pm8001_dbg(pm8001_ha, IOERR, "port_id:0x%x, tag:0x%x, event:0x%x\n",
		   port_id, tag, event);
2783 2784
	switch (event) {
	case IO_OVERFLOW:
2785
		pm8001_dbg(pm8001_ha, IO, "IO_UNDERFLOW\n");
2786 2787 2788 2789
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		ts->residual = 0;
		if (pm8001_dev)
V
Viswas G 已提交
2790
			atomic_dec(&pm8001_dev->running_req);
2791 2792
		break;
	case IO_XFER_ERROR_BREAK:
2793
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_BREAK\n");
2794 2795 2796 2797
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_INTERRUPTED;
		break;
	case IO_XFER_ERROR_PHY_NOT_READY:
2798
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_PHY_NOT_READY\n");
2799 2800 2801 2802 2803
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	case IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED:
2804 2805
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED\n");
2806 2807 2808 2809 2810
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_EPROTO;
		break;
	case IO_OPEN_CNX_ERROR_ZONE_VIOLATION:
2811 2812
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_ZONE_VIOLATION\n");
2813 2814 2815 2816 2817
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
		break;
	case IO_OPEN_CNX_ERROR_BREAK:
2818
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_BREAK\n");
2819 2820 2821 2822 2823
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_CONT0;
		break;
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
2824 2825 2826 2827 2828
	case IO_XFER_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED:
2829 2830
		pm8001_dbg(pm8001_ha, FAIL,
			   "IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS\n");
2831 2832 2833 2834 2835 2836 2837 2838
		ts->resp = SAS_TASK_UNDELIVERED;
		ts->stat = SAS_DEV_NO_RESPONSE;
		if (!t->uldd_task) {
			pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS);
			ts->resp = SAS_TASK_COMPLETE;
			ts->stat = SAS_QUEUE_FULL;
S
Suresh Thiagarajan 已提交
2839
			pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2840 2841 2842 2843
			return;
		}
		break;
	case IO_OPEN_CNX_ERROR_BAD_DESTINATION:
2844 2845
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_BAD_DESTINATION\n");
2846 2847 2848 2849 2850
		ts->resp = SAS_TASK_UNDELIVERED;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_BAD_DEST;
		break;
	case IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED:
2851 2852
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED\n");
2853 2854 2855 2856 2857
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_CONN_RATE;
		break;
	case IO_OPEN_CNX_ERROR_WRONG_DESTINATION:
2858 2859
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_WRONG_DESTINATION\n");
2860 2861 2862 2863 2864
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
		break;
	case IO_XFER_ERROR_NAK_RECEIVED:
2865
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_NAK_RECEIVED\n");
2866 2867 2868 2869
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_NAK_R_ERR;
		break;
	case IO_XFER_ERROR_PEER_ABORTED:
2870
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_PEER_ABORTED\n");
2871 2872 2873 2874
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_NAK_R_ERR;
		break;
	case IO_XFER_ERROR_REJECTED_NCQ_MODE:
2875
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_REJECTED_NCQ_MODE\n");
2876 2877 2878 2879
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_UNDERRUN;
		break;
	case IO_XFER_OPEN_RETRY_TIMEOUT:
2880
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_OPEN_RETRY_TIMEOUT\n");
2881 2882 2883 2884
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	case IO_XFER_ERROR_UNEXPECTED_PHASE:
2885
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_UNEXPECTED_PHASE\n");
2886 2887 2888 2889
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	case IO_XFER_ERROR_XFER_RDY_OVERRUN:
2890
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_XFER_RDY_OVERRUN\n");
2891 2892 2893 2894
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	case IO_XFER_ERROR_XFER_RDY_NOT_EXPECTED:
2895 2896
		pm8001_dbg(pm8001_ha, IO,
			   "IO_XFER_ERROR_XFER_RDY_NOT_EXPECTED\n");
2897 2898 2899 2900
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	case IO_XFER_ERROR_OFFSET_MISMATCH:
2901
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_OFFSET_MISMATCH\n");
2902 2903 2904 2905
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	case IO_XFER_ERROR_XFER_ZERO_DATA_LEN:
2906 2907
		pm8001_dbg(pm8001_ha, IO,
			   "IO_XFER_ERROR_XFER_ZERO_DATA_LEN\n");
2908 2909 2910 2911
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	case IO_XFER_CMD_FRAME_ISSUED:
2912
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_CMD_FRAME_ISSUED\n");
2913 2914
		break;
	case IO_XFER_PIO_SETUP_ERROR:
2915
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_PIO_SETUP_ERROR\n");
2916 2917 2918
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
2919
	case IO_XFER_ERROR_INTERNAL_CRC_ERROR:
2920 2921
		pm8001_dbg(pm8001_ha, FAIL,
			   "IO_XFR_ERROR_INTERNAL_CRC_ERROR\n");
2922 2923 2924 2925 2926
		/* TBC: used default set values */
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	case IO_XFER_DMA_ACTIVATE_TIMEOUT:
2927
		pm8001_dbg(pm8001_ha, FAIL, "IO_XFR_DMA_ACTIVATE_TIMEOUT\n");
2928 2929 2930 2931
		/* TBC: used default set values */
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
2932
	default:
2933
		pm8001_dbg(pm8001_ha, IO, "Unknown status 0x%x\n", event);
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
		/* not allowed case. Therefore, return failed status */
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_TO;
		break;
	}
	spin_lock_irqsave(&t->task_state_lock, flags);
	t->task_state_flags &= ~SAS_TASK_STATE_PENDING;
	t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
	t->task_state_flags |= SAS_TASK_STATE_DONE;
	if (unlikely((t->task_state_flags & SAS_TASK_STATE_ABORTED))) {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
2945 2946 2947
		pm8001_dbg(pm8001_ha, FAIL,
			   "task 0x%p done with io_status 0x%x resp 0x%x stat 0x%x but aborted by upper layer!\n",
			   t, event, ts->resp, ts->stat);
2948
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
S
Suresh Thiagarajan 已提交
2949
	} else {
2950
		spin_unlock_irqrestore(&t->task_state_lock, flags);
S
Suresh Thiagarajan 已提交
2951
		pm8001_ccb_task_free_done(pm8001_ha, t, ccb, tag);
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
	}
}

/*See the comments for mpi_ssp_completion */
static void
mpi_smp_completion(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	u32 param, i;
	struct sas_task *t;
	struct pm8001_ccb_info *ccb;
	unsigned long flags;
	u32 status;
	u32 tag;
	struct smp_completion_resp *psmpPayload;
	struct task_status_struct *ts;
	struct pm8001_device *pm8001_dev;
	char *pdma_respaddr = NULL;

	psmpPayload = (struct smp_completion_resp *)(piomb + 4);
	status = le32_to_cpu(psmpPayload->status);
	tag = le32_to_cpu(psmpPayload->tag);

	ccb = &pm8001_ha->ccb_info[tag];
	param = le32_to_cpu(psmpPayload->param);
	t = ccb->task;
	ts = &t->task_status;
	pm8001_dev = ccb->device;
	if (status)
2980
		pm8001_dbg(pm8001_ha, FAIL, "smp IO status 0x%x\n", status);
2981 2982 2983
	if (unlikely(!t || !t->lldd_task || !t->dev))
		return;

2984
	pm8001_dbg(pm8001_ha, DEV, "tag::0x%x status::0x%x\n", tag, status);
2985

2986 2987 2988
	switch (status) {

	case IO_SUCCESS:
2989
		pm8001_dbg(pm8001_ha, IO, "IO_SUCCESS\n");
2990 2991 2992
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAM_STAT_GOOD;
		if (pm8001_dev)
V
Viswas G 已提交
2993
			atomic_dec(&pm8001_dev->running_req);
2994
		if (pm8001_ha->smp_exp_mode == SMP_DIRECT) {
2995 2996 2997
			pm8001_dbg(pm8001_ha, IO,
				   "DIRECT RESPONSE Length:%d\n",
				   param);
2998 2999 3000 3001 3002
			pdma_respaddr = (char *)(phys_to_virt(cpu_to_le64
						((u64)sg_dma_address
						(&t->smp_task.smp_resp))));
			for (i = 0; i < param; i++) {
				*(pdma_respaddr+i) = psmpPayload->_r_a[i];
3003 3004 3005 3006
				pm8001_dbg(pm8001_ha, IO,
					   "SMP Byte%d DMA data 0x%x psmp 0x%x\n",
					   i, *(pdma_respaddr + i),
					   psmpPayload->_r_a[i]);
3007 3008 3009 3010
			}
		}
		break;
	case IO_ABORTED:
3011
		pm8001_dbg(pm8001_ha, IO, "IO_ABORTED IOMB\n");
3012 3013 3014
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_ABORTED_TASK;
		if (pm8001_dev)
V
Viswas G 已提交
3015
			atomic_dec(&pm8001_dev->running_req);
3016 3017
		break;
	case IO_OVERFLOW:
3018
		pm8001_dbg(pm8001_ha, IO, "IO_UNDERFLOW\n");
3019 3020 3021 3022
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DATA_OVERRUN;
		ts->residual = 0;
		if (pm8001_dev)
V
Viswas G 已提交
3023
			atomic_dec(&pm8001_dev->running_req);
3024 3025
		break;
	case IO_NO_DEVICE:
3026
		pm8001_dbg(pm8001_ha, IO, "IO_NO_DEVICE\n");
3027 3028 3029 3030
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_PHY_DOWN;
		break;
	case IO_ERROR_HW_TIMEOUT:
3031
		pm8001_dbg(pm8001_ha, IO, "IO_ERROR_HW_TIMEOUT\n");
3032 3033 3034 3035
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAM_STAT_BUSY;
		break;
	case IO_XFER_ERROR_BREAK:
3036
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_BREAK\n");
3037 3038 3039 3040
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAM_STAT_BUSY;
		break;
	case IO_XFER_ERROR_PHY_NOT_READY:
3041
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_PHY_NOT_READY\n");
3042 3043 3044 3045
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAM_STAT_BUSY;
		break;
	case IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED:
3046 3047
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_PROTOCOL_NOT_SUPPORTED\n");
3048 3049 3050 3051 3052
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
		break;
	case IO_OPEN_CNX_ERROR_ZONE_VIOLATION:
3053 3054
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_ZONE_VIOLATION\n");
3055 3056 3057 3058 3059
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
		break;
	case IO_OPEN_CNX_ERROR_BREAK:
3060
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_BREAK\n");
3061 3062 3063 3064 3065
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_CONT0;
		break;
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
3066 3067 3068 3069 3070
	case IO_XFER_OPEN_RETRY_BACKOFF_THRESHOLD_REACHED:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_TMO:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_NO_DEST:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_OPEN_COLLIDE:
	case IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS_PATHWAY_BLOCKED:
3071
		pm8001_dbg(pm8001_ha, IO, "IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS\n");
3072 3073 3074 3075 3076 3077 3078 3079
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_UNKNOWN;
		pm8001_handle_event(pm8001_ha,
				pm8001_dev,
				IO_OPEN_CNX_ERROR_IT_NEXUS_LOSS);
		break;
	case IO_OPEN_CNX_ERROR_BAD_DESTINATION:
3080 3081
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_BAD_DESTINATION\n");
3082 3083 3084 3085 3086
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_BAD_DEST;
		break;
	case IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED:
3087 3088
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED\n");
3089 3090 3091 3092 3093
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_CONN_RATE;
		break;
	case IO_OPEN_CNX_ERROR_WRONG_DESTINATION:
3094 3095
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_WRONG_DESTINATION\n");
3096 3097 3098 3099 3100
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
		break;
	case IO_XFER_ERROR_RX_FRAME:
3101
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_ERROR_RX_FRAME\n");
3102 3103 3104 3105
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
		break;
	case IO_XFER_OPEN_RETRY_TIMEOUT:
3106
		pm8001_dbg(pm8001_ha, IO, "IO_XFER_OPEN_RETRY_TIMEOUT\n");
3107 3108 3109 3110 3111
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	case IO_ERROR_INTERNAL_SMP_RESOURCE:
3112
		pm8001_dbg(pm8001_ha, IO, "IO_ERROR_INTERNAL_SMP_RESOURCE\n");
3113 3114 3115 3116
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_QUEUE_FULL;
		break;
	case IO_PORT_IN_RESET:
3117
		pm8001_dbg(pm8001_ha, IO, "IO_PORT_IN_RESET\n");
3118 3119 3120 3121 3122
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	case IO_DS_NON_OPERATIONAL:
3123
		pm8001_dbg(pm8001_ha, IO, "IO_DS_NON_OPERATIONAL\n");
3124 3125 3126 3127
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
		break;
	case IO_DS_IN_RECOVERY:
3128
		pm8001_dbg(pm8001_ha, IO, "IO_DS_IN_RECOVERY\n");
3129 3130 3131 3132 3133
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	case IO_OPEN_CNX_ERROR_HW_RESOURCE_BUSY:
3134 3135
		pm8001_dbg(pm8001_ha, IO,
			   "IO_OPEN_CNX_ERROR_HW_RESOURCE_BUSY\n");
3136 3137 3138 3139 3140
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		break;
	default:
3141
		pm8001_dbg(pm8001_ha, DEVIO, "Unknown status 0x%x\n", status);
3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152
		ts->resp = SAS_TASK_COMPLETE;
		ts->stat = SAS_DEV_NO_RESPONSE;
		/* not allowed case. Therefore, return failed status */
		break;
	}
	spin_lock_irqsave(&t->task_state_lock, flags);
	t->task_state_flags &= ~SAS_TASK_STATE_PENDING;
	t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
	t->task_state_flags |= SAS_TASK_STATE_DONE;
	if (unlikely((t->task_state_flags & SAS_TASK_STATE_ABORTED))) {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
3153 3154 3155
		pm8001_dbg(pm8001_ha, FAIL,
			   "task 0x%p done with io_status 0x%x resp 0x%xstat 0x%x but aborted by upper layer!\n",
			   t, status, ts->resp, ts->stat);
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
	} else {
		spin_unlock_irqrestore(&t->task_state_lock, flags);
		pm8001_ccb_task_free(pm8001_ha, t, ccb, tag);
		mb();/* in order to force CPU ordering */
		t->task_done(t);
	}
}

/**
 * pm80xx_hw_event_ack_req- For PM8001,some events need to acknowage to FW.
 * @pm8001_ha: our hba card information
 * @Qnum: the outbound queue message number.
 * @SEA: source of event to ack
 * @port_id: port id.
 * @phyId: phy id.
 * @param0: parameter 0.
 * @param1: parameter 1.
 */
static void pm80xx_hw_event_ack_req(struct pm8001_hba_info *pm8001_ha,
	u32 Qnum, u32 SEA, u32 port_id, u32 phyId, u32 param0, u32 param1)
{
	struct hw_event_ack_req	 payload;
	u32 opc = OPC_INB_SAS_HW_EVENT_ACK;

	struct inbound_queue_table *circularQ;

	memset((u8 *)&payload, 0, sizeof(payload));
	circularQ = &pm8001_ha->inbnd_q_tbl[Qnum];
	payload.tag = cpu_to_le32(1);
	payload.phyid_sea_portid = cpu_to_le32(((SEA & 0xFFFF) << 8) |
		((phyId & 0xFF) << 24) | (port_id & 0xFF));
	payload.param0 = cpu_to_le32(param0);
	payload.param1 = cpu_to_le32(param1);
3190 3191
	pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
3192 3193 3194 3195 3196
}

static int pm80xx_chip_phy_ctl_req(struct pm8001_hba_info *pm8001_ha,
	u32 phyId, u32 phy_op);

3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
static void hw_event_port_recover(struct pm8001_hba_info *pm8001_ha,
					void *piomb)
{
	struct hw_event_resp *pPayload = (struct hw_event_resp *)(piomb + 4);
	u32 phyid_npip_portstate = le32_to_cpu(pPayload->phyid_npip_portstate);
	u8 phy_id = (u8)((phyid_npip_portstate & 0xFF0000) >> 16);
	u32 lr_status_evt_portid =
		le32_to_cpu(pPayload->lr_status_evt_portid);
	u8 deviceType = pPayload->sas_identify.dev_type;
	u8 link_rate = (u8)((lr_status_evt_portid & 0xF0000000) >> 28);
	struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
	u8 port_id = (u8)(lr_status_evt_portid & 0x000000FF);
	struct pm8001_port *port = &pm8001_ha->port[port_id];

	if (deviceType == SAS_END_DEVICE) {
		pm80xx_chip_phy_ctl_req(pm8001_ha, phy_id,
					PHY_NOTIFY_ENABLE_SPINUP);
	}

	port->wide_port_phymap |= (1U << phy_id);
	pm8001_get_lrate_mode(phy, link_rate);
	phy->sas_phy.oob_mode = SAS_OOB_MODE;
	phy->phy_state = PHY_STATE_LINK_UP_SPCV;
	phy->phy_attached = 1;
}

3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
/**
 * hw_event_sas_phy_up -FW tells me a SAS phy up event.
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static void
hw_event_sas_phy_up(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	struct hw_event_resp *pPayload =
		(struct hw_event_resp *)(piomb + 4);
	u32 lr_status_evt_portid =
		le32_to_cpu(pPayload->lr_status_evt_portid);
	u32 phyid_npip_portstate = le32_to_cpu(pPayload->phyid_npip_portstate);

	u8 link_rate =
		(u8)((lr_status_evt_portid & 0xF0000000) >> 28);
	u8 port_id = (u8)(lr_status_evt_portid & 0x000000FF);
	u8 phy_id =
		(u8)((phyid_npip_portstate & 0xFF0000) >> 16);
	u8 portstate = (u8)(phyid_npip_portstate & 0x0000000F);

	struct pm8001_port *port = &pm8001_ha->port[port_id];
	struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
	unsigned long flags;
	u8 deviceType = pPayload->sas_identify.dev_type;
	port->port_state = portstate;
3249
	port->wide_port_phymap |= (1U << phy_id);
3250
	phy->phy_state = PHY_STATE_LINK_UP_SPCV;
3251 3252 3253
	pm8001_dbg(pm8001_ha, MSG,
		   "portid:%d; phyid:%d; linkrate:%d; portstate:%x; devicetype:%x\n",
		   port_id, phy_id, link_rate, portstate, deviceType);
3254 3255 3256

	switch (deviceType) {
	case SAS_PHY_UNUSED:
3257
		pm8001_dbg(pm8001_ha, MSG, "device type no device.\n");
3258 3259
		break;
	case SAS_END_DEVICE:
3260
		pm8001_dbg(pm8001_ha, MSG, "end device.\n");
3261 3262 3263 3264 3265 3266
		pm80xx_chip_phy_ctl_req(pm8001_ha, phy_id,
			PHY_NOTIFY_ENABLE_SPINUP);
		port->port_attached = 1;
		pm8001_get_lrate_mode(phy, link_rate);
		break;
	case SAS_EDGE_EXPANDER_DEVICE:
3267
		pm8001_dbg(pm8001_ha, MSG, "expander device.\n");
3268 3269 3270 3271
		port->port_attached = 1;
		pm8001_get_lrate_mode(phy, link_rate);
		break;
	case SAS_FANOUT_EXPANDER_DEVICE:
3272
		pm8001_dbg(pm8001_ha, MSG, "fanout expander device.\n");
3273 3274 3275 3276
		port->port_attached = 1;
		pm8001_get_lrate_mode(phy, link_rate);
		break;
	default:
3277 3278
		pm8001_dbg(pm8001_ha, DEVIO, "unknown device type(%x)\n",
			   deviceType);
3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
		break;
	}
	phy->phy_type |= PORT_TYPE_SAS;
	phy->identify.device_type = deviceType;
	phy->phy_attached = 1;
	if (phy->identify.device_type == SAS_END_DEVICE)
		phy->identify.target_port_protocols = SAS_PROTOCOL_SSP;
	else if (phy->identify.device_type != SAS_PHY_UNUSED)
		phy->identify.target_port_protocols = SAS_PROTOCOL_SMP;
	phy->sas_phy.oob_mode = SAS_OOB_MODE;
3289
	sas_notify_phy_event_gfp(&phy->sas_phy, PHYE_OOB_DONE, GFP_ATOMIC);
3290 3291 3292 3293 3294 3295 3296
	spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
	memcpy(phy->frame_rcvd, &pPayload->sas_identify,
		sizeof(struct sas_identify_frame)-4);
	phy->frame_rcvd_size = sizeof(struct sas_identify_frame) - 4;
	pm8001_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
	spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
	if (pm8001_ha->flags == PM8001F_RUN_TIME)
3297
		mdelay(200); /* delay a moment to wait for disk to spin up */
3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
	pm8001_bytes_dmaed(pm8001_ha, phy_id);
}

/**
 * hw_event_sata_phy_up -FW tells me a SATA phy up event.
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static void
hw_event_sata_phy_up(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	struct hw_event_resp *pPayload =
		(struct hw_event_resp *)(piomb + 4);
	u32 phyid_npip_portstate = le32_to_cpu(pPayload->phyid_npip_portstate);
	u32 lr_status_evt_portid =
		le32_to_cpu(pPayload->lr_status_evt_portid);
	u8 link_rate =
		(u8)((lr_status_evt_portid & 0xF0000000) >> 28);
	u8 port_id = (u8)(lr_status_evt_portid & 0x000000FF);
	u8 phy_id =
		(u8)((phyid_npip_portstate & 0xFF0000) >> 16);

	u8 portstate = (u8)(phyid_npip_portstate & 0x0000000F);

	struct pm8001_port *port = &pm8001_ha->port[port_id];
	struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
	unsigned long flags;
3325 3326 3327
	pm8001_dbg(pm8001_ha, DEVIO,
		   "port id %d, phy id %d link_rate %d portstate 0x%x\n",
		   port_id, phy_id, link_rate, portstate);
3328 3329

	port->port_state = portstate;
3330
	phy->phy_state = PHY_STATE_LINK_UP_SPCV;
3331 3332 3333 3334 3335
	port->port_attached = 1;
	pm8001_get_lrate_mode(phy, link_rate);
	phy->phy_type |= PORT_TYPE_SATA;
	phy->phy_attached = 1;
	phy->sas_phy.oob_mode = SATA_OOB_MODE;
3336
	sas_notify_phy_event_gfp(&phy->sas_phy, PHYE_OOB_DONE, GFP_ATOMIC);
3337 3338 3339 3340 3341
	spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
	memcpy(phy->frame_rcvd, ((u8 *)&pPayload->sata_fis - 4),
		sizeof(struct dev_to_host_fis));
	phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
	phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
3342
	phy->identify.device_type = SAS_SATA_DEV;
3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
	pm8001_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
	spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
	pm8001_bytes_dmaed(pm8001_ha, phy_id);
}

/**
 * hw_event_phy_down -we should notify the libsas the phy is down.
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static void
hw_event_phy_down(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	struct hw_event_resp *pPayload =
		(struct hw_event_resp *)(piomb + 4);

	u32 lr_status_evt_portid =
		le32_to_cpu(pPayload->lr_status_evt_portid);
	u8 port_id = (u8)(lr_status_evt_portid & 0x000000FF);
	u32 phyid_npip_portstate = le32_to_cpu(pPayload->phyid_npip_portstate);
	u8 phy_id =
		(u8)((phyid_npip_portstate & 0xFF0000) >> 16);
	u8 portstate = (u8)(phyid_npip_portstate & 0x0000000F);

	struct pm8001_port *port = &pm8001_ha->port[port_id];
	struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
3369
	u32 port_sata = (phy->phy_type & PORT_TYPE_SATA);
3370 3371 3372 3373 3374 3375 3376
	port->port_state = portstate;
	phy->identify.device_type = 0;
	phy->phy_attached = 0;
	switch (portstate) {
	case PORT_VALID:
		break;
	case PORT_INVALID:
3377 3378 3379 3380
		pm8001_dbg(pm8001_ha, MSG, " PortInvalid portID %d\n",
			   port_id);
		pm8001_dbg(pm8001_ha, MSG,
			   " Last phy Down and port invalid\n");
3381
		if (port_sata) {
3382 3383 3384 3385 3386 3387
			phy->phy_type = 0;
			port->port_attached = 0;
			pm80xx_hw_event_ack_req(pm8001_ha, 0, HW_EVENT_PHY_DOWN,
					port_id, phy_id, 0, 0);
		}
		sas_phy_disconnected(&phy->sas_phy);
3388 3389
		break;
	case PORT_IN_RESET:
3390 3391
		pm8001_dbg(pm8001_ha, MSG, " Port In Reset portID %d\n",
			   port_id);
3392 3393
		break;
	case PORT_NOT_ESTABLISHED:
3394 3395
		pm8001_dbg(pm8001_ha, MSG,
			   " Phy Down and PORT_NOT_ESTABLISHED\n");
3396 3397 3398
		port->port_attached = 0;
		break;
	case PORT_LOSTCOMM:
3399 3400 3401
		pm8001_dbg(pm8001_ha, MSG, " Phy Down and PORT_LOSTCOMM\n");
		pm8001_dbg(pm8001_ha, MSG,
			   " Last phy Down and port invalid\n");
3402
		if (port_sata) {
3403 3404 3405 3406 3407 3408
			port->port_attached = 0;
			phy->phy_type = 0;
			pm80xx_hw_event_ack_req(pm8001_ha, 0, HW_EVENT_PHY_DOWN,
					port_id, phy_id, 0, 0);
		}
		sas_phy_disconnected(&phy->sas_phy);
3409 3410 3411
		break;
	default:
		port->port_attached = 0;
3412 3413 3414
		pm8001_dbg(pm8001_ha, DEVIO,
			   " Phy Down and(default) = 0x%x\n",
			   portstate);
3415 3416 3417
		break;

	}
3418
	if (port_sata && (portstate != PORT_IN_RESET))
3419 3420
		sas_notify_phy_event_gfp(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL,
					GFP_ATOMIC);
3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432
}

static int mpi_phy_start_resp(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	struct phy_start_resp *pPayload =
		(struct phy_start_resp *)(piomb + 4);
	u32 status =
		le32_to_cpu(pPayload->status);
	u32 phy_id =
		le32_to_cpu(pPayload->phyid);
	struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];

3433 3434 3435
	pm8001_dbg(pm8001_ha, INIT,
		   "phy start resp status:0x%x, phyid:0x%x\n",
		   status, phy_id);
3436
	if (status == 0)
3437
		phy->phy_state = PHY_LINK_DOWN;
3438 3439 3440 3441 3442

	if (pm8001_ha->flags == PM8001F_RUN_TIME &&
			phy->enable_completion != NULL) {
		complete(phy->enable_completion);
		phy->enable_completion = NULL;
3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461
	}
	return 0;

}

/**
 * mpi_thermal_hw_event -The hw event has come.
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_thermal_hw_event(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	struct thermal_hw_event *pPayload =
		(struct thermal_hw_event *)(piomb + 4);

	u32 thermal_event = le32_to_cpu(pPayload->thermal_event);
	u32 rht_lht = le32_to_cpu(pPayload->rht_lht);

	if (thermal_event & 0x40) {
3462 3463 3464 3465 3466
		pm8001_dbg(pm8001_ha, IO,
			   "Thermal Event: Local high temperature violated!\n");
		pm8001_dbg(pm8001_ha, IO,
			   "Thermal Event: Measured local high temperature %d\n",
			   ((rht_lht & 0xFF00) >> 8));
3467 3468
	}
	if (thermal_event & 0x10) {
3469 3470 3471 3472 3473
		pm8001_dbg(pm8001_ha, IO,
			   "Thermal Event: Remote high temperature violated!\n");
		pm8001_dbg(pm8001_ha, IO,
			   "Thermal Event: Measured remote high temperature %d\n",
			   ((rht_lht & 0xFF000000) >> 24));
3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
	}
	return 0;
}

/**
 * mpi_hw_event -The hw event has come.
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_hw_event(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
3485
	unsigned long flags, i;
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
	struct hw_event_resp *pPayload =
		(struct hw_event_resp *)(piomb + 4);
	u32 lr_status_evt_portid =
		le32_to_cpu(pPayload->lr_status_evt_portid);
	u32 phyid_npip_portstate = le32_to_cpu(pPayload->phyid_npip_portstate);
	u8 port_id = (u8)(lr_status_evt_portid & 0x000000FF);
	u8 phy_id =
		(u8)((phyid_npip_portstate & 0xFF0000) >> 16);
	u16 eventType =
		(u16)((lr_status_evt_portid & 0x00FFFF00) >> 8);
	u8 status =
		(u8)((lr_status_evt_portid & 0x0F000000) >> 24);
	struct sas_ha_struct *sas_ha = pm8001_ha->sas;
	struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
3500
	struct pm8001_port *port = &pm8001_ha->port[port_id];
3501
	struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
3502 3503 3504
	pm8001_dbg(pm8001_ha, DEV,
		   "portid:%d phyid:%d event:0x%x status:0x%x\n",
		   port_id, phy_id, eventType, status);
3505 3506 3507 3508

	switch (eventType) {

	case HW_EVENT_SAS_PHY_UP:
3509
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_PHY_START_STATUS\n");
3510 3511 3512
		hw_event_sas_phy_up(pm8001_ha, piomb);
		break;
	case HW_EVENT_SATA_PHY_UP:
3513
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_SATA_PHY_UP\n");
3514 3515 3516
		hw_event_sata_phy_up(pm8001_ha, piomb);
		break;
	case HW_EVENT_SATA_SPINUP_HOLD:
3517
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_SATA_SPINUP_HOLD\n");
3518 3519
		sas_notify_phy_event_gfp(&phy->sas_phy, PHYE_SPINUP_HOLD,
			GFP_ATOMIC);
3520 3521
		break;
	case HW_EVENT_PHY_DOWN:
3522
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_PHY_DOWN\n");
3523 3524
		hw_event_phy_down(pm8001_ha, piomb);
		if (pm8001_ha->reset_in_progress) {
3525
			pm8001_dbg(pm8001_ha, MSG, "Reset in progress\n");
3526 3527
			return 0;
		}
3528
		phy->phy_attached = 0;
3529
		phy->phy_state = PHY_LINK_DISABLE;
3530 3531
		break;
	case HW_EVENT_PORT_INVALID:
3532
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_PORT_INVALID\n");
3533 3534
		sas_phy_disconnected(sas_phy);
		phy->phy_attached = 0;
3535 3536
		sas_notify_port_event_gfp(sas_phy, PORTE_LINK_RESET_ERR,
			GFP_ATOMIC);
3537 3538 3539 3540
		break;
	/* the broadcast change primitive received, tell the LIBSAS this event
	to revalidate the sas domain*/
	case HW_EVENT_BROADCAST_CHANGE:
3541
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_BROADCAST_CHANGE\n");
3542 3543 3544 3545 3546
		pm80xx_hw_event_ack_req(pm8001_ha, 0, HW_EVENT_BROADCAST_CHANGE,
			port_id, phy_id, 1, 0);
		spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
		sas_phy->sas_prim = HW_EVENT_BROADCAST_CHANGE;
		spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
3547 3548
		sas_notify_port_event_gfp(sas_phy, PORTE_BROADCAST_RCVD,
			GFP_ATOMIC);
3549 3550
		break;
	case HW_EVENT_PHY_ERROR:
3551
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_PHY_ERROR\n");
3552 3553
		sas_phy_disconnected(&phy->sas_phy);
		phy->phy_attached = 0;
3554 3555
		sas_notify_phy_event_gfp(&phy->sas_phy, PHYE_OOB_ERROR,
			GFP_ATOMIC);
3556 3557
		break;
	case HW_EVENT_BROADCAST_EXP:
3558
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_BROADCAST_EXP\n");
3559 3560 3561
		spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
		sas_phy->sas_prim = HW_EVENT_BROADCAST_EXP;
		spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
3562 3563
		sas_notify_port_event_gfp(sas_phy, PORTE_BROADCAST_RCVD,
			GFP_ATOMIC);
3564 3565
		break;
	case HW_EVENT_LINK_ERR_INVALID_DWORD:
3566 3567
		pm8001_dbg(pm8001_ha, MSG,
			   "HW_EVENT_LINK_ERR_INVALID_DWORD\n");
3568 3569 3570 3571
		pm80xx_hw_event_ack_req(pm8001_ha, 0,
			HW_EVENT_LINK_ERR_INVALID_DWORD, port_id, phy_id, 0, 0);
		break;
	case HW_EVENT_LINK_ERR_DISPARITY_ERROR:
3572 3573
		pm8001_dbg(pm8001_ha, MSG,
			   "HW_EVENT_LINK_ERR_DISPARITY_ERROR\n");
3574 3575 3576 3577 3578
		pm80xx_hw_event_ack_req(pm8001_ha, 0,
			HW_EVENT_LINK_ERR_DISPARITY_ERROR,
			port_id, phy_id, 0, 0);
		break;
	case HW_EVENT_LINK_ERR_CODE_VIOLATION:
3579 3580
		pm8001_dbg(pm8001_ha, MSG,
			   "HW_EVENT_LINK_ERR_CODE_VIOLATION\n");
3581 3582 3583 3584 3585
		pm80xx_hw_event_ack_req(pm8001_ha, 0,
			HW_EVENT_LINK_ERR_CODE_VIOLATION,
			port_id, phy_id, 0, 0);
		break;
	case HW_EVENT_LINK_ERR_LOSS_OF_DWORD_SYNCH:
3586 3587
		pm8001_dbg(pm8001_ha, MSG,
			   "HW_EVENT_LINK_ERR_LOSS_OF_DWORD_SYNCH\n");
3588 3589 3590 3591 3592
		pm80xx_hw_event_ack_req(pm8001_ha, 0,
			HW_EVENT_LINK_ERR_LOSS_OF_DWORD_SYNCH,
			port_id, phy_id, 0, 0);
		break;
	case HW_EVENT_MALFUNCTION:
3593
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_MALFUNCTION\n");
3594 3595
		break;
	case HW_EVENT_BROADCAST_SES:
3596
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_BROADCAST_SES\n");
3597 3598 3599
		spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
		sas_phy->sas_prim = HW_EVENT_BROADCAST_SES;
		spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
3600 3601
		sas_notify_port_event_gfp(sas_phy, PORTE_BROADCAST_RCVD,
			GFP_ATOMIC);
3602 3603
		break;
	case HW_EVENT_INBOUND_CRC_ERROR:
3604
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_INBOUND_CRC_ERROR\n");
3605 3606 3607 3608 3609
		pm80xx_hw_event_ack_req(pm8001_ha, 0,
			HW_EVENT_INBOUND_CRC_ERROR,
			port_id, phy_id, 0, 0);
		break;
	case HW_EVENT_HARD_RESET_RECEIVED:
3610
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_HARD_RESET_RECEIVED\n");
3611 3612
		sas_notify_port_event_gfp(sas_phy, PORTE_HARD_RESET,
			GFP_ATOMIC);
3613 3614
		break;
	case HW_EVENT_ID_FRAME_TIMEOUT:
3615
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_ID_FRAME_TIMEOUT\n");
3616 3617
		sas_phy_disconnected(sas_phy);
		phy->phy_attached = 0;
3618 3619
		sas_notify_port_event_gfp(sas_phy, PORTE_LINK_RESET_ERR,
			GFP_ATOMIC);
3620 3621
		break;
	case HW_EVENT_LINK_ERR_PHY_RESET_FAILED:
3622 3623
		pm8001_dbg(pm8001_ha, MSG,
			   "HW_EVENT_LINK_ERR_PHY_RESET_FAILED\n");
3624 3625 3626 3627 3628
		pm80xx_hw_event_ack_req(pm8001_ha, 0,
			HW_EVENT_LINK_ERR_PHY_RESET_FAILED,
			port_id, phy_id, 0, 0);
		sas_phy_disconnected(sas_phy);
		phy->phy_attached = 0;
3629 3630
		sas_notify_port_event_gfp(sas_phy, PORTE_LINK_RESET_ERR,
			GFP_ATOMIC);
3631 3632
		break;
	case HW_EVENT_PORT_RESET_TIMER_TMO:
3633
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_PORT_RESET_TIMER_TMO\n");
3634 3635
		pm80xx_hw_event_ack_req(pm8001_ha, 0, HW_EVENT_PHY_DOWN,
			port_id, phy_id, 0, 0);
3636 3637
		sas_phy_disconnected(sas_phy);
		phy->phy_attached = 0;
3638 3639
		sas_notify_port_event_gfp(sas_phy, PORTE_LINK_RESET_ERR,
			GFP_ATOMIC);
3640 3641 3642 3643 3644 3645
		if (pm8001_ha->phy[phy_id].reset_completion) {
			pm8001_ha->phy[phy_id].port_reset_status =
					PORT_RESET_TMO;
			complete(pm8001_ha->phy[phy_id].reset_completion);
			pm8001_ha->phy[phy_id].reset_completion = NULL;
		}
3646 3647
		break;
	case HW_EVENT_PORT_RECOVERY_TIMER_TMO:
3648 3649
		pm8001_dbg(pm8001_ha, MSG,
			   "HW_EVENT_PORT_RECOVERY_TIMER_TMO\n");
3650 3651 3652
		pm80xx_hw_event_ack_req(pm8001_ha, 0,
			HW_EVENT_PORT_RECOVERY_TIMER_TMO,
			port_id, phy_id, 0, 0);
3653 3654 3655
		for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
			if (port->wide_port_phymap & (1 << i)) {
				phy = &pm8001_ha->phy[i];
3656 3657
				sas_notify_phy_event_gfp(&phy->sas_phy,
					PHYE_LOSS_OF_SIGNAL, GFP_ATOMIC);
3658 3659 3660
				port->wide_port_phymap &= ~(1 << i);
			}
		}
3661 3662
		break;
	case HW_EVENT_PORT_RECOVER:
3663
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_PORT_RECOVER\n");
3664
		hw_event_port_recover(pm8001_ha, piomb);
3665 3666
		break;
	case HW_EVENT_PORT_RESET_COMPLETE:
3667
		pm8001_dbg(pm8001_ha, MSG, "HW_EVENT_PORT_RESET_COMPLETE\n");
3668 3669 3670 3671 3672 3673
		if (pm8001_ha->phy[phy_id].reset_completion) {
			pm8001_ha->phy[phy_id].port_reset_status =
					PORT_RESET_SUCCESS;
			complete(pm8001_ha->phy[phy_id].reset_completion);
			pm8001_ha->phy[phy_id].reset_completion = NULL;
		}
3674 3675
		break;
	case EVENT_BROADCAST_ASYNCH_EVENT:
3676
		pm8001_dbg(pm8001_ha, MSG, "EVENT_BROADCAST_ASYNCH_EVENT\n");
3677 3678
		break;
	default:
3679 3680
		pm8001_dbg(pm8001_ha, DEVIO, "Unknown event type 0x%x\n",
			   eventType);
3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697
		break;
	}
	return 0;
}

/**
 * mpi_phy_stop_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_phy_stop_resp(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	struct phy_stop_resp *pPayload =
		(struct phy_stop_resp *)(piomb + 4);
	u32 status =
		le32_to_cpu(pPayload->status);
	u32 phyid =
3698
		le32_to_cpu(pPayload->phyid) & 0xFF;
3699
	struct pm8001_phy *phy = &pm8001_ha->phy[phyid];
3700 3701
	pm8001_dbg(pm8001_ha, MSG, "phy:0x%x status:0x%x\n",
		   phyid, status);
3702 3703 3704
	if (status == PHY_STOP_SUCCESS ||
		status == PHY_STOP_ERR_DEVICE_ATTACHED)
		phy->phy_state = PHY_LINK_DISABLE;
3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
	return 0;
}

/**
 * mpi_set_controller_config_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_set_controller_config_resp(struct pm8001_hba_info *pm8001_ha,
			void *piomb)
{
	struct set_ctrl_cfg_resp *pPayload =
			(struct set_ctrl_cfg_resp *)(piomb + 4);
	u32 status = le32_to_cpu(pPayload->status);
	u32 err_qlfr_pgcd = le32_to_cpu(pPayload->err_qlfr_pgcd);

3721 3722 3723
	pm8001_dbg(pm8001_ha, MSG,
		   "SET CONTROLLER RESP: status 0x%x qlfr_pgcd 0x%x\n",
		   status, err_qlfr_pgcd);
3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735

	return 0;
}

/**
 * mpi_get_controller_config_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_get_controller_config_resp(struct pm8001_hba_info *pm8001_ha,
			void *piomb)
{
3736
	pm8001_dbg(pm8001_ha, MSG, " pm80xx_addition_functionality\n");
3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748

	return 0;
}

/**
 * mpi_get_phy_profile_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_get_phy_profile_resp(struct pm8001_hba_info *pm8001_ha,
			void *piomb)
{
3749
	pm8001_dbg(pm8001_ha, MSG, " pm80xx_addition_functionality\n");
3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760

	return 0;
}

/**
 * mpi_flash_op_ext_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_flash_op_ext_resp(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
3761
	pm8001_dbg(pm8001_ha, MSG, " pm80xx_addition_functionality\n");
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773

	return 0;
}

/**
 * mpi_set_phy_profile_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_set_phy_profile_resp(struct pm8001_hba_info *pm8001_ha,
			void *piomb)
{
3774
	u32 tag;
3775
	u8 page_code;
3776
	int rc = 0;
3777 3778 3779 3780
	struct set_phy_profile_resp *pPayload =
		(struct set_phy_profile_resp *)(piomb + 4);
	u32 ppc_phyid = le32_to_cpu(pPayload->ppc_phyid);
	u32 status = le32_to_cpu(pPayload->status);
3781

3782
	tag = le32_to_cpu(pPayload->tag);
3783 3784 3785
	page_code = (u8)((ppc_phyid & 0xFF00) >> 8);
	if (status) {
		/* status is FAILED */
3786 3787 3788
		pm8001_dbg(pm8001_ha, FAIL,
			   "PhyProfile command failed  with status 0x%08X\n",
			   status);
3789
		rc = -1;
3790 3791
	} else {
		if (page_code != SAS_PHY_ANALOG_SETTINGS_PAGE) {
3792 3793
			pm8001_dbg(pm8001_ha, FAIL, "Invalid page code 0x%X\n",
				   page_code);
3794
			rc = -1;
3795 3796
		}
	}
3797 3798
	pm8001_tag_free(pm8001_ha, tag);
	return rc;
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814
}

/**
 * mpi_kek_management_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_kek_management_resp(struct pm8001_hba_info *pm8001_ha,
			void *piomb)
{
	struct kek_mgmt_resp *pPayload = (struct kek_mgmt_resp *)(piomb + 4);

	u32 status = le32_to_cpu(pPayload->status);
	u32 kidx_new_curr_ksop = le32_to_cpu(pPayload->kidx_new_curr_ksop);
	u32 err_qlfr = le32_to_cpu(pPayload->err_qlfr);

3815 3816 3817
	pm8001_dbg(pm8001_ha, MSG,
		   "KEK MGMT RESP. Status 0x%x idx_ksop 0x%x err_qlfr 0x%x\n",
		   status, kidx_new_curr_ksop, err_qlfr);
3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829

	return 0;
}

/**
 * mpi_dek_management_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int mpi_dek_management_resp(struct pm8001_hba_info *pm8001_ha,
			void *piomb)
{
3830
	pm8001_dbg(pm8001_ha, MSG, " pm80xx_addition_functionality\n");
3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842

	return 0;
}

/**
 * ssp_coalesced_comp_resp - SPCv specific
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static int ssp_coalesced_comp_resp(struct pm8001_hba_info *pm8001_ha,
			void *piomb)
{
3843
	pm8001_dbg(pm8001_ha, MSG, " pm80xx_addition_functionality\n");
3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859

	return 0;
}

/**
 * process_one_iomb - process one outbound Queue memory block
 * @pm8001_ha: our hba card information
 * @piomb: IO message buffer
 */
static void process_one_iomb(struct pm8001_hba_info *pm8001_ha, void *piomb)
{
	__le32 pHeader = *(__le32 *)piomb;
	u32 opc = (u32)((le32_to_cpu(pHeader)) & 0xFFF);

	switch (opc) {
	case OPC_OUB_ECHO:
3860
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_ECHO\n");
3861 3862
		break;
	case OPC_OUB_HW_EVENT:
3863
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_HW_EVENT\n");
3864 3865 3866
		mpi_hw_event(pm8001_ha, piomb);
		break;
	case OPC_OUB_THERM_HW_EVENT:
3867
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_THERMAL_EVENT\n");
3868 3869 3870
		mpi_thermal_hw_event(pm8001_ha, piomb);
		break;
	case OPC_OUB_SSP_COMP:
3871
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SSP_COMP\n");
3872 3873 3874
		mpi_ssp_completion(pm8001_ha, piomb);
		break;
	case OPC_OUB_SMP_COMP:
3875
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SMP_COMP\n");
3876 3877 3878
		mpi_smp_completion(pm8001_ha, piomb);
		break;
	case OPC_OUB_LOCAL_PHY_CNTRL:
3879
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_LOCAL_PHY_CNTRL\n");
3880 3881 3882
		pm8001_mpi_local_phy_ctl(pm8001_ha, piomb);
		break;
	case OPC_OUB_DEV_REGIST:
3883
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_DEV_REGIST\n");
3884 3885 3886
		pm8001_mpi_reg_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_DEREG_DEV:
3887
		pm8001_dbg(pm8001_ha, MSG, "unregister the device\n");
3888 3889 3890
		pm8001_mpi_dereg_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_GET_DEV_HANDLE:
3891
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_GET_DEV_HANDLE\n");
3892 3893
		break;
	case OPC_OUB_SATA_COMP:
3894
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SATA_COMP\n");
3895 3896 3897
		mpi_sata_completion(pm8001_ha, piomb);
		break;
	case OPC_OUB_SATA_EVENT:
3898
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SATA_EVENT\n");
3899 3900 3901
		mpi_sata_event(pm8001_ha, piomb);
		break;
	case OPC_OUB_SSP_EVENT:
3902
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SSP_EVENT\n");
3903 3904 3905
		mpi_ssp_event(pm8001_ha, piomb);
		break;
	case OPC_OUB_DEV_HANDLE_ARRIV:
3906
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_DEV_HANDLE_ARRIV\n");
3907 3908 3909
		/*This is for target*/
		break;
	case OPC_OUB_SSP_RECV_EVENT:
3910
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SSP_RECV_EVENT\n");
3911 3912 3913
		/*This is for target*/
		break;
	case OPC_OUB_FW_FLASH_UPDATE:
3914
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_FW_FLASH_UPDATE\n");
3915 3916 3917
		pm8001_mpi_fw_flash_update_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_GPIO_RESPONSE:
3918
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_GPIO_RESPONSE\n");
3919 3920
		break;
	case OPC_OUB_GPIO_EVENT:
3921
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_GPIO_EVENT\n");
3922 3923
		break;
	case OPC_OUB_GENERAL_EVENT:
3924
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_GENERAL_EVENT\n");
3925 3926 3927
		pm8001_mpi_general_event(pm8001_ha, piomb);
		break;
	case OPC_OUB_SSP_ABORT_RSP:
3928
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SSP_ABORT_RSP\n");
3929 3930 3931
		pm8001_mpi_task_abort_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_SATA_ABORT_RSP:
3932
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SATA_ABORT_RSP\n");
3933 3934 3935
		pm8001_mpi_task_abort_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_SAS_DIAG_MODE_START_END:
3936 3937
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_SAS_DIAG_MODE_START_END\n");
3938 3939
		break;
	case OPC_OUB_SAS_DIAG_EXECUTE:
3940
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SAS_DIAG_EXECUTE\n");
3941 3942
		break;
	case OPC_OUB_GET_TIME_STAMP:
3943
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_GET_TIME_STAMP\n");
3944 3945
		break;
	case OPC_OUB_SAS_HW_EVENT_ACK:
3946
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SAS_HW_EVENT_ACK\n");
3947 3948
		break;
	case OPC_OUB_PORT_CONTROL:
3949
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_PORT_CONTROL\n");
3950 3951
		break;
	case OPC_OUB_SMP_ABORT_RSP:
3952
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SMP_ABORT_RSP\n");
3953 3954 3955
		pm8001_mpi_task_abort_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_GET_NVMD_DATA:
3956
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_GET_NVMD_DATA\n");
3957 3958 3959
		pm8001_mpi_get_nvmd_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_SET_NVMD_DATA:
3960
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SET_NVMD_DATA\n");
3961 3962 3963
		pm8001_mpi_set_nvmd_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_DEVICE_HANDLE_REMOVAL:
3964
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_DEVICE_HANDLE_REMOVAL\n");
3965 3966
		break;
	case OPC_OUB_SET_DEVICE_STATE:
3967
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SET_DEVICE_STATE\n");
3968 3969 3970
		pm8001_mpi_set_dev_state_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_GET_DEVICE_STATE:
3971
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_GET_DEVICE_STATE\n");
3972 3973
		break;
	case OPC_OUB_SET_DEV_INFO:
3974
		pm8001_dbg(pm8001_ha, MSG, "OPC_OUB_SET_DEV_INFO\n");
3975 3976 3977
		break;
	/* spcv specifc commands */
	case OPC_OUB_PHY_START_RESP:
3978 3979
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_PHY_START_RESP opcode:%x\n", opc);
3980 3981 3982
		mpi_phy_start_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_PHY_STOP_RESP:
3983 3984
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_PHY_STOP_RESP opcode:%x\n", opc);
3985 3986 3987
		mpi_phy_stop_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_SET_CONTROLLER_CONFIG:
3988 3989
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_SET_CONTROLLER_CONFIG opcode:%x\n", opc);
3990 3991 3992
		mpi_set_controller_config_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_GET_CONTROLLER_CONFIG:
3993 3994
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_GET_CONTROLLER_CONFIG opcode:%x\n", opc);
3995 3996 3997
		mpi_get_controller_config_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_GET_PHY_PROFILE:
3998 3999
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_GET_PHY_PROFILE opcode:%x\n", opc);
4000 4001 4002
		mpi_get_phy_profile_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_FLASH_OP_EXT:
4003 4004
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_FLASH_OP_EXT opcode:%x\n", opc);
4005 4006 4007
		mpi_flash_op_ext_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_SET_PHY_PROFILE:
4008 4009
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_SET_PHY_PROFILE opcode:%x\n", opc);
4010 4011 4012
		mpi_set_phy_profile_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_KEK_MANAGEMENT_RESP:
4013 4014
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_KEK_MANAGEMENT_RESP opcode:%x\n", opc);
4015 4016 4017
		mpi_kek_management_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_DEK_MANAGEMENT_RESP:
4018 4019
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_DEK_MANAGEMENT_RESP opcode:%x\n", opc);
4020 4021 4022
		mpi_dek_management_resp(pm8001_ha, piomb);
		break;
	case OPC_OUB_SSP_COALESCED_COMP_RESP:
4023 4024
		pm8001_dbg(pm8001_ha, MSG,
			   "OPC_OUB_SSP_COALESCED_COMP_RESP opcode:%x\n", opc);
4025 4026 4027
		ssp_coalesced_comp_resp(pm8001_ha, piomb);
		break;
	default:
4028 4029
		pm8001_dbg(pm8001_ha, DEVIO,
			   "Unknown outbound Queue IOMB OPC = 0x%x\n", opc);
4030 4031 4032 4033
		break;
	}
}

4034 4035
static void print_scratchpad_registers(struct pm8001_hba_info *pm8001_ha)
{
4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_SCRATCH_PAD_0: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_0));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_SCRATCH_PAD_1:0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_SCRATCH_PAD_2: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_2));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_SCRATCH_PAD_3: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_3));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_HOST_SCRATCH_PAD_0: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_0));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_HOST_SCRATCH_PAD_1: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_1));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_HOST_SCRATCH_PAD_2: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_2));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_HOST_SCRATCH_PAD_3: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_3));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_HOST_SCRATCH_PAD_4: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_4));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_HOST_SCRATCH_PAD_5: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_5));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_RSVD_SCRATCH_PAD_0: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_6));
	pm8001_dbg(pm8001_ha, FAIL, "MSGU_RSVD_SCRATCH_PAD_1: 0x%x\n",
		   pm8001_cr32(pm8001_ha, 0, MSGU_HOST_SCRATCH_PAD_7));
4060 4061
}

4062 4063 4064 4065
static int process_oq(struct pm8001_hba_info *pm8001_ha, u8 vec)
{
	struct outbound_queue_table *circularQ;
	void *pMsg1 = NULL;
4066
	u8 bc;
4067 4068
	u32 ret = MPI_IO_STATUS_FAIL;
	unsigned long flags;
4069
	u32 regval;
4070

4071
	if (vec == (pm8001_ha->max_q_num - 1)) {
4072 4073 4074 4075
		regval = pm8001_cr32(pm8001_ha, 0, MSGU_SCRATCH_PAD_1);
		if ((regval & SCRATCH_PAD_MIPSALL_READY) !=
					SCRATCH_PAD_MIPSALL_READY) {
			pm8001_ha->controller_fatal_error = true;
4076 4077 4078
			pm8001_dbg(pm8001_ha, FAIL,
				   "Firmware Fatal error! Regval:0x%x\n",
				   regval);
4079 4080 4081 4082
			print_scratchpad_registers(pm8001_ha);
			return ret;
		}
	}
4083 4084 4085
	spin_lock_irqsave(&pm8001_ha->lock, flags);
	circularQ = &pm8001_ha->outbnd_q_tbl[vec];
	do {
4086 4087 4088 4089 4090 4091
		/* spurious interrupt during setup if kexec-ing and
		 * driver doing a doorbell access w/ the pre-kexec oq
		 * interrupt setup.
		 */
		if (!circularQ->pi_virt)
			break;
4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113
		ret = pm8001_mpi_msg_consume(pm8001_ha, circularQ, &pMsg1, &bc);
		if (MPI_IO_STATUS_SUCCESS == ret) {
			/* process the outbound message */
			process_one_iomb(pm8001_ha, (void *)(pMsg1 - 4));
			/* free the message from the outbound circular buffer */
			pm8001_mpi_msg_free_set(pm8001_ha, pMsg1,
							circularQ, bc);
		}
		if (MPI_IO_STATUS_BUSY == ret) {
			/* Update the producer index from SPC */
			circularQ->producer_index =
				cpu_to_le32(pm8001_read_32(circularQ->pi_virt));
			if (le32_to_cpu(circularQ->producer_index) ==
				circularQ->consumer_idx)
				/* OQ is empty */
				break;
		}
	} while (1);
	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
	return ret;
}

4114
/* DMA_... to our direction translation. */
4115
static const u8 data_dir_flags[] = {
4116 4117 4118 4119
	[DMA_BIDIRECTIONAL]	= DATA_DIR_BYRECIPIENT,	/* UNSPECIFIED */
	[DMA_TO_DEVICE]		= DATA_DIR_OUT,		/* OUTBOUND */
	[DMA_FROM_DEVICE]	= DATA_DIR_IN,		/* INBOUND */
	[DMA_NONE]		= DATA_DIR_NONE,	/* NO TRANSFER */
4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160
};

static void build_smp_cmd(u32 deviceID, __le32 hTag,
			struct smp_req *psmp_cmd, int mode, int length)
{
	psmp_cmd->tag = hTag;
	psmp_cmd->device_id = cpu_to_le32(deviceID);
	if (mode == SMP_DIRECT) {
		length = length - 4; /* subtract crc */
		psmp_cmd->len_ip_ir = cpu_to_le32(length << 16);
	} else {
		psmp_cmd->len_ip_ir = cpu_to_le32(1|(1 << 1));
	}
}

/**
 * pm8001_chip_smp_req - send a SMP task to FW
 * @pm8001_ha: our hba card information.
 * @ccb: the ccb information this request used.
 */
static int pm80xx_chip_smp_req(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_ccb_info *ccb)
{
	int elem, rc;
	struct sas_task *task = ccb->task;
	struct domain_device *dev = task->dev;
	struct pm8001_device *pm8001_dev = dev->lldd_dev;
	struct scatterlist *sg_req, *sg_resp;
	u32 req_len, resp_len;
	struct smp_req smp_cmd;
	u32 opc;
	struct inbound_queue_table *circularQ;
	char *preq_dma_addr = NULL;
	__le64 tmp_addr;
	u32 i, length;

	memset(&smp_cmd, 0, sizeof(smp_cmd));
	/*
	 * DMA-map SMP request, response buffers
	 */
	sg_req = &task->smp_task.smp_req;
4161
	elem = dma_map_sg(pm8001_ha->dev, sg_req, 1, DMA_TO_DEVICE);
4162 4163 4164 4165 4166
	if (!elem)
		return -ENOMEM;
	req_len = sg_dma_len(sg_req);

	sg_resp = &task->smp_task.smp_resp;
4167
	elem = dma_map_sg(pm8001_ha->dev, sg_resp, 1, DMA_FROM_DEVICE);
4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183
	if (!elem) {
		rc = -ENOMEM;
		goto err_out;
	}
	resp_len = sg_dma_len(sg_resp);
	/* must be in dwords */
	if ((req_len & 0x3) || (resp_len & 0x3)) {
		rc = -EINVAL;
		goto err_out_2;
	}

	opc = OPC_INB_SMP_REQUEST;
	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	smp_cmd.tag = cpu_to_le32(ccb->ccb_tag);

	length = sg_req->length;
4184
	pm8001_dbg(pm8001_ha, IO, "SMP Frame Length %d\n", sg_req->length);
4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195
	if (!(length - 8))
		pm8001_ha->smp_exp_mode = SMP_DIRECT;
	else
		pm8001_ha->smp_exp_mode = SMP_INDIRECT;


	tmp_addr = cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_req));
	preq_dma_addr = (char *)phys_to_virt(tmp_addr);

	/* INDIRECT MODE command settings. Use DMA */
	if (pm8001_ha->smp_exp_mode == SMP_INDIRECT) {
4196
		pm8001_dbg(pm8001_ha, IO, "SMP REQUEST INDIRECT MODE\n");
4197 4198 4199 4200 4201 4202 4203
		/* for SPCv indirect mode. Place the top 4 bytes of
		 * SMP Request header here. */
		for (i = 0; i < 4; i++)
			smp_cmd.smp_req16[i] = *(preq_dma_addr + i);
		/* exclude top 4 bytes for SMP req header */
		smp_cmd.long_smp_req.long_req_addr =
			cpu_to_le64((u64)sg_dma_address
4204
				(&task->smp_task.smp_req) + 4);
4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227
		/* exclude 4 bytes for SMP req header and CRC */
		smp_cmd.long_smp_req.long_req_size =
			cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_req)-8);
		smp_cmd.long_smp_req.long_resp_addr =
				cpu_to_le64((u64)sg_dma_address
					(&task->smp_task.smp_resp));
		smp_cmd.long_smp_req.long_resp_size =
				cpu_to_le32((u32)sg_dma_len
					(&task->smp_task.smp_resp)-4);
	} else { /* DIRECT MODE */
		smp_cmd.long_smp_req.long_req_addr =
			cpu_to_le64((u64)sg_dma_address
					(&task->smp_task.smp_req));
		smp_cmd.long_smp_req.long_req_size =
			cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_req)-4);
		smp_cmd.long_smp_req.long_resp_addr =
			cpu_to_le64((u64)sg_dma_address
				(&task->smp_task.smp_resp));
		smp_cmd.long_smp_req.long_resp_size =
			cpu_to_le32
			((u32)sg_dma_len(&task->smp_task.smp_resp)-4);
	}
	if (pm8001_ha->smp_exp_mode == SMP_DIRECT) {
4228
		pm8001_dbg(pm8001_ha, IO, "SMP REQUEST DIRECT MODE\n");
4229 4230 4231
		for (i = 0; i < length; i++)
			if (i < 16) {
				smp_cmd.smp_req16[i] = *(preq_dma_addr+i);
4232 4233 4234 4235
				pm8001_dbg(pm8001_ha, IO,
					   "Byte[%d]:%x (DMA data:%x)\n",
					   i, smp_cmd.smp_req16[i],
					   *(preq_dma_addr));
4236 4237
			} else {
				smp_cmd.smp_req[i] = *(preq_dma_addr+i);
4238 4239 4240 4241
				pm8001_dbg(pm8001_ha, IO,
					   "Byte[%d]:%x (DMA data:%x)\n",
					   i, smp_cmd.smp_req[i],
					   *(preq_dma_addr));
4242 4243 4244 4245 4246
			}
	}

	build_smp_cmd(pm8001_dev->device_id, smp_cmd.tag,
				&smp_cmd, pm8001_ha->smp_exp_mode, length);
4247 4248
	rc = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &smp_cmd,
			sizeof(smp_cmd), 0);
T
Tomas Henzl 已提交
4249 4250
	if (rc)
		goto err_out_2;
4251 4252 4253 4254
	return 0;

err_out_2:
	dma_unmap_sg(pm8001_ha->dev, &ccb->task->smp_task.smp_resp, 1,
4255
			DMA_FROM_DEVICE);
4256 4257
err_out:
	dma_unmap_sg(pm8001_ha->dev, &ccb->task->smp_task.smp_req, 1,
4258
			DMA_TO_DEVICE);
4259 4260 4261 4262 4263
	return rc;
}

static int check_enc_sas_cmd(struct sas_task *task)
{
4264 4265 4266
	u8 cmd = task->ssp_task.cmd->cmnd[0];

	if (cmd == READ_10 || cmd == WRITE_10 || cmd == WRITE_VERIFY)
4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308
		return 1;
	else
		return 0;
}

static int check_enc_sat_cmd(struct sas_task *task)
{
	int ret = 0;
	switch (task->ata_task.fis.command) {
	case ATA_CMD_FPDMA_READ:
	case ATA_CMD_READ_EXT:
	case ATA_CMD_READ:
	case ATA_CMD_FPDMA_WRITE:
	case ATA_CMD_WRITE_EXT:
	case ATA_CMD_WRITE:
	case ATA_CMD_PIO_READ:
	case ATA_CMD_PIO_READ_EXT:
	case ATA_CMD_PIO_WRITE:
	case ATA_CMD_PIO_WRITE_EXT:
		ret = 1;
		break;
	default:
		ret = 0;
		break;
	}
	return ret;
}

/**
 * pm80xx_chip_ssp_io_req - send a SSP task to FW
 * @pm8001_ha: our hba card information.
 * @ccb: the ccb information this request used.
 */
static int pm80xx_chip_ssp_io_req(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_ccb_info *ccb)
{
	struct sas_task *task = ccb->task;
	struct domain_device *dev = task->dev;
	struct pm8001_device *pm8001_dev = dev->lldd_dev;
	struct ssp_ini_io_start_req ssp_cmd;
	u32 tag = ccb->ccb_tag;
	int ret;
4309 4310
	u64 phys_addr, start_addr, end_addr;
	u32 end_addr_high, end_addr_low;
4311
	struct inbound_queue_table *circularQ;
4312
	u32 q_index, cpu_id;
4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328
	u32 opc = OPC_INB_SSPINIIOSTART;
	memset(&ssp_cmd, 0, sizeof(ssp_cmd));
	memcpy(ssp_cmd.ssp_iu.lun, task->ssp_task.LUN, 8);
	/* data address domain added for spcv; set to 0 by host,
	 * used internally by controller
	 * 0 for SAS 1.1 and SAS 2.0 compatible TLR
	 */
	ssp_cmd.dad_dir_m_tlr =
		cpu_to_le32(data_dir_flags[task->data_dir] << 8 | 0x0);
	ssp_cmd.data_len = cpu_to_le32(task->total_xfer_len);
	ssp_cmd.device_id = cpu_to_le32(pm8001_dev->device_id);
	ssp_cmd.tag = cpu_to_le32(tag);
	if (task->ssp_task.enable_first_burst)
		ssp_cmd.ssp_iu.efb_prio_attr |= 0x80;
	ssp_cmd.ssp_iu.efb_prio_attr |= (task->ssp_task.task_prio << 3);
	ssp_cmd.ssp_iu.efb_prio_attr |= (task->ssp_task.task_attr & 7);
4329 4330
	memcpy(ssp_cmd.ssp_iu.cdb, task->ssp_task.cmd->cmnd,
		       task->ssp_task.cmd->cmd_len);
4331 4332
	cpu_id = smp_processor_id();
	q_index = (u32) (cpu_id) % (pm8001_ha->max_q_num);
4333
	circularQ = &pm8001_ha->inbnd_q_tbl[q_index];
4334 4335 4336 4337

	/* Check if encryption is set */
	if (pm8001_ha->chip->encrypt &&
		!(pm8001_ha->encrypt_info.status) && check_enc_sas_cmd(task)) {
4338 4339 4340
		pm8001_dbg(pm8001_ha, IO,
			   "Encryption enabled.Sending Encrypt SAS command 0x%x\n",
			   task->ssp_task.cmd->cmnd[0]);
4341 4342 4343 4344 4345 4346 4347 4348 4349
		opc = OPC_INB_SSP_INI_DIF_ENC_IO;
		/* enable encryption. 0 for SAS 1.1 and SAS 2.0 compatible TLR*/
		ssp_cmd.dad_dir_m_tlr =	cpu_to_le32
			((data_dir_flags[task->data_dir] << 8) | 0x20 | 0x0);

		/* fill in PRD (scatter/gather) table, if any */
		if (task->num_scatter > 1) {
			pm8001_chip_make_sg(task->scatter,
						ccb->n_elem, ccb->buf_prd);
4350
			phys_addr = ccb->ccb_dma_handle;
4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363
			ssp_cmd.enc_addr_low =
				cpu_to_le32(lower_32_bits(phys_addr));
			ssp_cmd.enc_addr_high =
				cpu_to_le32(upper_32_bits(phys_addr));
			ssp_cmd.enc_esgl = cpu_to_le32(1<<31);
		} else if (task->num_scatter == 1) {
			u64 dma_addr = sg_dma_address(task->scatter);
			ssp_cmd.enc_addr_low =
				cpu_to_le32(lower_32_bits(dma_addr));
			ssp_cmd.enc_addr_high =
				cpu_to_le32(upper_32_bits(dma_addr));
			ssp_cmd.enc_len = cpu_to_le32(task->total_xfer_len);
			ssp_cmd.enc_esgl = 0;
4364 4365 4366 4367 4368 4369
			/* Check 4G Boundary */
			start_addr = cpu_to_le64(dma_addr);
			end_addr = (start_addr + ssp_cmd.enc_len) - 1;
			end_addr_low = cpu_to_le32(lower_32_bits(end_addr));
			end_addr_high = cpu_to_le32(upper_32_bits(end_addr));
			if (end_addr_high != ssp_cmd.enc_addr_high) {
4370 4371 4372 4373
				pm8001_dbg(pm8001_ha, FAIL,
					   "The sg list address start_addr=0x%016llx data_len=0x%x end_addr_high=0x%08x end_addr_low=0x%08x has crossed 4G boundary\n",
					   start_addr, ssp_cmd.enc_len,
					   end_addr_high, end_addr_low);
4374 4375
				pm8001_chip_make_sg(task->scatter, 1,
					ccb->buf_prd);
4376
				phys_addr = ccb->ccb_dma_handle;
4377 4378 4379 4380 4381 4382
				ssp_cmd.enc_addr_low =
					cpu_to_le32(lower_32_bits(phys_addr));
				ssp_cmd.enc_addr_high =
					cpu_to_le32(upper_32_bits(phys_addr));
				ssp_cmd.enc_esgl = cpu_to_le32(1<<31);
			}
4383 4384 4385 4386 4387 4388 4389 4390 4391
		} else if (task->num_scatter == 0) {
			ssp_cmd.enc_addr_low = 0;
			ssp_cmd.enc_addr_high = 0;
			ssp_cmd.enc_len = cpu_to_le32(task->total_xfer_len);
			ssp_cmd.enc_esgl = 0;
		}
		/* XTS mode. All other fields are 0 */
		ssp_cmd.key_cmode = 0x6 << 4;
		/* set tweak values. Should be the start lba */
4392 4393 4394 4395
		ssp_cmd.twk_val0 = cpu_to_le32((task->ssp_task.cmd->cmnd[2] << 24) |
						(task->ssp_task.cmd->cmnd[3] << 16) |
						(task->ssp_task.cmd->cmnd[4] << 8) |
						(task->ssp_task.cmd->cmnd[5]));
4396
	} else {
4397 4398 4399
		pm8001_dbg(pm8001_ha, IO,
			   "Sending Normal SAS command 0x%x inb q %x\n",
			   task->ssp_task.cmd->cmnd[0], q_index);
4400 4401 4402 4403
		/* fill in PRD (scatter/gather) table, if any */
		if (task->num_scatter > 1) {
			pm8001_chip_make_sg(task->scatter, ccb->n_elem,
					ccb->buf_prd);
4404
			phys_addr = ccb->ccb_dma_handle;
4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
			ssp_cmd.addr_low =
				cpu_to_le32(lower_32_bits(phys_addr));
			ssp_cmd.addr_high =
				cpu_to_le32(upper_32_bits(phys_addr));
			ssp_cmd.esgl = cpu_to_le32(1<<31);
		} else if (task->num_scatter == 1) {
			u64 dma_addr = sg_dma_address(task->scatter);
			ssp_cmd.addr_low = cpu_to_le32(lower_32_bits(dma_addr));
			ssp_cmd.addr_high =
				cpu_to_le32(upper_32_bits(dma_addr));
			ssp_cmd.len = cpu_to_le32(task->total_xfer_len);
			ssp_cmd.esgl = 0;
4417 4418 4419 4420 4421 4422
			/* Check 4G Boundary */
			start_addr = cpu_to_le64(dma_addr);
			end_addr = (start_addr + ssp_cmd.len) - 1;
			end_addr_low = cpu_to_le32(lower_32_bits(end_addr));
			end_addr_high = cpu_to_le32(upper_32_bits(end_addr));
			if (end_addr_high != ssp_cmd.addr_high) {
4423 4424 4425 4426
				pm8001_dbg(pm8001_ha, FAIL,
					   "The sg list address start_addr=0x%016llx data_len=0x%x end_addr_high=0x%08x end_addr_low=0x%08x has crossed 4G boundary\n",
					   start_addr, ssp_cmd.len,
					   end_addr_high, end_addr_low);
4427 4428
				pm8001_chip_make_sg(task->scatter, 1,
					ccb->buf_prd);
4429
				phys_addr = ccb->ccb_dma_handle;
4430 4431 4432 4433 4434 4435
				ssp_cmd.addr_low =
					cpu_to_le32(lower_32_bits(phys_addr));
				ssp_cmd.addr_high =
					cpu_to_le32(upper_32_bits(phys_addr));
				ssp_cmd.esgl = cpu_to_le32(1<<31);
			}
4436 4437 4438 4439 4440 4441 4442
		} else if (task->num_scatter == 0) {
			ssp_cmd.addr_low = 0;
			ssp_cmd.addr_high = 0;
			ssp_cmd.len = cpu_to_le32(task->total_xfer_len);
			ssp_cmd.esgl = 0;
		}
	}
4443
	ret = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc,
4444
			&ssp_cmd, sizeof(ssp_cmd), q_index);
4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455
	return ret;
}

static int pm80xx_chip_sata_req(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_ccb_info *ccb)
{
	struct sas_task *task = ccb->task;
	struct domain_device *dev = task->dev;
	struct pm8001_device *pm8001_ha_dev = dev->lldd_dev;
	u32 tag = ccb->ccb_tag;
	int ret;
4456
	u32 q_index, cpu_id;
4457 4458
	struct sata_start_req sata_cmd;
	u32 hdr_tag, ncg_tag = 0;
4459 4460
	u64 phys_addr, start_addr, end_addr;
	u32 end_addr_high, end_addr_low;
4461 4462 4463
	u32 ATAP = 0x0;
	u32 dir;
	struct inbound_queue_table *circularQ;
4464
	unsigned long flags;
4465 4466
	u32 opc = OPC_INB_SATA_HOST_OPSTART;
	memset(&sata_cmd, 0, sizeof(sata_cmd));
4467 4468
	cpu_id = smp_processor_id();
	q_index = (u32) (cpu_id) % (pm8001_ha->max_q_num);
4469
	circularQ = &pm8001_ha->inbnd_q_tbl[q_index];
4470

4471
	if (task->data_dir == DMA_NONE) {
4472
		ATAP = 0x04; /* no data*/
4473
		pm8001_dbg(pm8001_ha, IO, "no data\n");
4474 4475 4476
	} else if (likely(!task->ata_task.device_control_reg_update)) {
		if (task->ata_task.dma_xfer) {
			ATAP = 0x06; /* DMA */
4477
			pm8001_dbg(pm8001_ha, IO, "DMA\n");
4478 4479
		} else {
			ATAP = 0x05; /* PIO*/
4480
			pm8001_dbg(pm8001_ha, IO, "PIO\n");
4481 4482
		}
		if (task->ata_task.use_ncq &&
H
Hannes Reinecke 已提交
4483
		    dev->sata_dev.class != ATA_DEV_ATAPI) {
4484
			ATAP = 0x07; /* FPDMA */
4485
			pm8001_dbg(pm8001_ha, IO, "FPDMA\n");
4486 4487
		}
	}
4488 4489
	if (task->ata_task.use_ncq && pm8001_get_ncq_tag(task, &hdr_tag)) {
		task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
4490
		ncg_tag = hdr_tag;
4491
	}
4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504
	dir = data_dir_flags[task->data_dir] << 8;
	sata_cmd.tag = cpu_to_le32(tag);
	sata_cmd.device_id = cpu_to_le32(pm8001_ha_dev->device_id);
	sata_cmd.data_len = cpu_to_le32(task->total_xfer_len);

	sata_cmd.sata_fis = task->ata_task.fis;
	if (likely(!task->ata_task.device_control_reg_update))
		sata_cmd.sata_fis.flags |= 0x80;/* C=1: update ATA cmd reg */
	sata_cmd.sata_fis.flags &= 0xF0;/* PM_PORT field shall be 0 */

	/* Check if encryption is set */
	if (pm8001_ha->chip->encrypt &&
		!(pm8001_ha->encrypt_info.status) && check_enc_sat_cmd(task)) {
4505 4506 4507
		pm8001_dbg(pm8001_ha, IO,
			   "Encryption enabled.Sending Encrypt SATA cmd 0x%x\n",
			   sata_cmd.sata_fis.command);
4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518
		opc = OPC_INB_SATA_DIF_ENC_IO;

		/* set encryption bit */
		sata_cmd.ncqtag_atap_dir_m_dad =
			cpu_to_le32(((ncg_tag & 0xff)<<16)|
				((ATAP & 0x3f) << 10) | 0x20 | dir);
							/* dad (bit 0-1) is 0 */
		/* fill in PRD (scatter/gather) table, if any */
		if (task->num_scatter > 1) {
			pm8001_chip_make_sg(task->scatter,
						ccb->n_elem, ccb->buf_prd);
4519
			phys_addr = ccb->ccb_dma_handle;
4520 4521 4522 4523 4524 4525 4526 4527 4528
			sata_cmd.enc_addr_low = lower_32_bits(phys_addr);
			sata_cmd.enc_addr_high = upper_32_bits(phys_addr);
			sata_cmd.enc_esgl = cpu_to_le32(1 << 31);
		} else if (task->num_scatter == 1) {
			u64 dma_addr = sg_dma_address(task->scatter);
			sata_cmd.enc_addr_low = lower_32_bits(dma_addr);
			sata_cmd.enc_addr_high = upper_32_bits(dma_addr);
			sata_cmd.enc_len = cpu_to_le32(task->total_xfer_len);
			sata_cmd.enc_esgl = 0;
4529 4530 4531 4532 4533 4534
			/* Check 4G Boundary */
			start_addr = cpu_to_le64(dma_addr);
			end_addr = (start_addr + sata_cmd.enc_len) - 1;
			end_addr_low = cpu_to_le32(lower_32_bits(end_addr));
			end_addr_high = cpu_to_le32(upper_32_bits(end_addr));
			if (end_addr_high != sata_cmd.enc_addr_high) {
4535 4536 4537 4538
				pm8001_dbg(pm8001_ha, FAIL,
					   "The sg list address start_addr=0x%016llx data_len=0x%x end_addr_high=0x%08x end_addr_low=0x%08x has crossed 4G boundary\n",
					   start_addr, sata_cmd.enc_len,
					   end_addr_high, end_addr_low);
4539 4540
				pm8001_chip_make_sg(task->scatter, 1,
					ccb->buf_prd);
4541
				phys_addr = ccb->ccb_dma_handle;
4542 4543 4544 4545 4546 4547 4548
				sata_cmd.enc_addr_low =
					lower_32_bits(phys_addr);
				sata_cmd.enc_addr_high =
					upper_32_bits(phys_addr);
				sata_cmd.enc_esgl =
					cpu_to_le32(1 << 31);
			}
4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566
		} else if (task->num_scatter == 0) {
			sata_cmd.enc_addr_low = 0;
			sata_cmd.enc_addr_high = 0;
			sata_cmd.enc_len = cpu_to_le32(task->total_xfer_len);
			sata_cmd.enc_esgl = 0;
		}
		/* XTS mode. All other fields are 0 */
		sata_cmd.key_index_mode = 0x6 << 4;
		/* set tweak values. Should be the start lba */
		sata_cmd.twk_val0 =
			cpu_to_le32((sata_cmd.sata_fis.lbal_exp << 24) |
					(sata_cmd.sata_fis.lbah << 16) |
					(sata_cmd.sata_fis.lbam << 8) |
					(sata_cmd.sata_fis.lbal));
		sata_cmd.twk_val1 =
			cpu_to_le32((sata_cmd.sata_fis.lbah_exp << 8) |
					 (sata_cmd.sata_fis.lbam_exp));
	} else {
4567 4568 4569
		pm8001_dbg(pm8001_ha, IO,
			   "Sending Normal SATA command 0x%x inb %x\n",
			   sata_cmd.sata_fis.command, q_index);
4570 4571 4572 4573 4574 4575 4576 4577 4578
		/* dad (bit 0-1) is 0 */
		sata_cmd.ncqtag_atap_dir_m_dad =
			cpu_to_le32(((ncg_tag & 0xff)<<16) |
					((ATAP & 0x3f) << 10) | dir);

		/* fill in PRD (scatter/gather) table, if any */
		if (task->num_scatter > 1) {
			pm8001_chip_make_sg(task->scatter,
					ccb->n_elem, ccb->buf_prd);
4579
			phys_addr = ccb->ccb_dma_handle;
4580 4581 4582 4583 4584 4585 4586 4587 4588
			sata_cmd.addr_low = lower_32_bits(phys_addr);
			sata_cmd.addr_high = upper_32_bits(phys_addr);
			sata_cmd.esgl = cpu_to_le32(1 << 31);
		} else if (task->num_scatter == 1) {
			u64 dma_addr = sg_dma_address(task->scatter);
			sata_cmd.addr_low = lower_32_bits(dma_addr);
			sata_cmd.addr_high = upper_32_bits(dma_addr);
			sata_cmd.len = cpu_to_le32(task->total_xfer_len);
			sata_cmd.esgl = 0;
4589 4590 4591 4592 4593 4594
			/* Check 4G Boundary */
			start_addr = cpu_to_le64(dma_addr);
			end_addr = (start_addr + sata_cmd.len) - 1;
			end_addr_low = cpu_to_le32(lower_32_bits(end_addr));
			end_addr_high = cpu_to_le32(upper_32_bits(end_addr));
			if (end_addr_high != sata_cmd.addr_high) {
4595 4596 4597 4598
				pm8001_dbg(pm8001_ha, FAIL,
					   "The sg list address start_addr=0x%016llx data_len=0x%xend_addr_high=0x%08x end_addr_low=0x%08x has crossed 4G boundary\n",
					   start_addr, sata_cmd.len,
					   end_addr_high, end_addr_low);
4599 4600
				pm8001_chip_make_sg(task->scatter, 1,
					ccb->buf_prd);
4601
				phys_addr = ccb->ccb_dma_handle;
4602 4603 4604 4605 4606 4607
				sata_cmd.addr_low =
					lower_32_bits(phys_addr);
				sata_cmd.addr_high =
					upper_32_bits(phys_addr);
				sata_cmd.esgl = cpu_to_le32(1 << 31);
			}
4608 4609 4610 4611 4612 4613
		} else if (task->num_scatter == 0) {
			sata_cmd.addr_low = 0;
			sata_cmd.addr_high = 0;
			sata_cmd.len = cpu_to_le32(task->total_xfer_len);
			sata_cmd.esgl = 0;
		}
4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634
		/* scsi cdb */
		sata_cmd.atapi_scsi_cdb[0] =
			cpu_to_le32(((task->ata_task.atapi_packet[0]) |
			(task->ata_task.atapi_packet[1] << 8) |
			(task->ata_task.atapi_packet[2] << 16) |
			(task->ata_task.atapi_packet[3] << 24)));
		sata_cmd.atapi_scsi_cdb[1] =
			cpu_to_le32(((task->ata_task.atapi_packet[4]) |
			(task->ata_task.atapi_packet[5] << 8) |
			(task->ata_task.atapi_packet[6] << 16) |
			(task->ata_task.atapi_packet[7] << 24)));
		sata_cmd.atapi_scsi_cdb[2] =
			cpu_to_le32(((task->ata_task.atapi_packet[8]) |
			(task->ata_task.atapi_packet[9] << 8) |
			(task->ata_task.atapi_packet[10] << 16) |
			(task->ata_task.atapi_packet[11] << 24)));
		sata_cmd.atapi_scsi_cdb[3] =
			cpu_to_le32(((task->ata_task.atapi_packet[12]) |
			(task->ata_task.atapi_packet[13] << 8) |
			(task->ata_task.atapi_packet[14] << 16) |
			(task->ata_task.atapi_packet[15] << 24)));
4635
	}
4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656

	/* Check for read log for failed drive and return */
	if (sata_cmd.sata_fis.command == 0x2f) {
		if (pm8001_ha_dev && ((pm8001_ha_dev->id & NCQ_READ_LOG_FLAG) ||
			(pm8001_ha_dev->id & NCQ_ABORT_ALL_FLAG) ||
			(pm8001_ha_dev->id & NCQ_2ND_RLE_FLAG))) {
			struct task_status_struct *ts;

			pm8001_ha_dev->id &= 0xDFFFFFFF;
			ts = &task->task_status;

			spin_lock_irqsave(&task->task_state_lock, flags);
			ts->resp = SAS_TASK_COMPLETE;
			ts->stat = SAM_STAT_GOOD;
			task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
			task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
			task->task_state_flags |= SAS_TASK_STATE_DONE;
			if (unlikely((task->task_state_flags &
					SAS_TASK_STATE_ABORTED))) {
				spin_unlock_irqrestore(&task->task_state_lock,
							flags);
4657 4658 4659 4660
				pm8001_dbg(pm8001_ha, FAIL,
					   "task 0x%p resp 0x%x  stat 0x%x but aborted by upper layer\n",
					   task, ts->resp,
					   ts->stat);
4661 4662
				pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
				return 0;
S
Suresh Thiagarajan 已提交
4663
			} else {
4664 4665
				spin_unlock_irqrestore(&task->task_state_lock,
							flags);
S
Suresh Thiagarajan 已提交
4666 4667
				pm8001_ccb_task_free_done(pm8001_ha, task,
								ccb, tag);
V
Viswas G 已提交
4668
				atomic_dec(&pm8001_ha_dev->running_req);
4669 4670 4671 4672
				return 0;
			}
		}
	}
4673
	ret = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc,
4674
			&sata_cmd, sizeof(sata_cmd), q_index);
4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694
	return ret;
}

/**
 * pm80xx_chip_phy_start_req - start phy via PHY_START COMMAND
 * @pm8001_ha: our hba card information.
 * @phy_id: the phy id which we wanted to start up.
 */
static int
pm80xx_chip_phy_start_req(struct pm8001_hba_info *pm8001_ha, u8 phy_id)
{
	struct phy_start_req payload;
	struct inbound_queue_table *circularQ;
	int ret;
	u32 tag = 0x01;
	u32 opcode = OPC_INB_PHYSTART;
	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	memset(&payload, 0, sizeof(payload));
	payload.tag = cpu_to_le32(tag);

4695
	pm8001_dbg(pm8001_ha, INIT, "PHY START REQ for phy_id %d\n", phy_id);
4696

4697 4698
	payload.ase_sh_lm_slr_phyid = cpu_to_le32(SPINHOLD_DISABLE |
			LINKMODE_AUTO | pm8001_ha->link_rate | phy_id);
4699 4700 4701 4702 4703 4704 4705 4706 4707
	/* SSC Disable and SAS Analog ST configuration */
	/**
	payload.ase_sh_lm_slr_phyid =
		cpu_to_le32(SSC_DISABLE_30 | SAS_ASE | SPINHOLD_DISABLE |
		LINKMODE_AUTO | LINKRATE_15 | LINKRATE_30 | LINKRATE_60 |
		phy_id);
	Have to add "SAS PHY Analog Setup SPASTI 1 Byte" Based on need
	**/

4708
	payload.sas_identify.dev_type = SAS_END_DEVICE;
4709 4710
	payload.sas_identify.initiator_bits = SAS_PROTOCOL_ALL;
	memcpy(payload.sas_identify.sas_addr,
4711
	  &pm8001_ha->sas_addr, SAS_ADDR_SIZE);
4712
	payload.sas_identify.phy_id = phy_id;
4713 4714
	ret = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opcode, &payload,
			sizeof(payload), 0);
4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
	return ret;
}

/**
 * pm8001_chip_phy_stop_req - start phy via PHY_STOP COMMAND
 * @pm8001_ha: our hba card information.
 * @phy_id: the phy id which we wanted to start up.
 */
static int pm80xx_chip_phy_stop_req(struct pm8001_hba_info *pm8001_ha,
	u8 phy_id)
{
	struct phy_stop_req payload;
	struct inbound_queue_table *circularQ;
	int ret;
	u32 tag = 0x01;
	u32 opcode = OPC_INB_PHYSTOP;
	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	memset(&payload, 0, sizeof(payload));
	payload.tag = cpu_to_le32(tag);
	payload.phy_id = cpu_to_le32(phy_id);
4735 4736
	ret = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opcode, &payload,
			sizeof(payload), 0);
4737 4738 4739
	return ret;
}

4740
/*
4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
 * see comments on pm8001_mpi_reg_resp.
 */
static int pm80xx_chip_reg_dev_req(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_device *pm8001_dev, u32 flag)
{
	struct reg_dev_req payload;
	u32	opc;
	u32 stp_sspsmp_sata = 0x4;
	struct inbound_queue_table *circularQ;
	u32 linkrate, phy_id;
	int rc, tag = 0xdeadbeef;
	struct pm8001_ccb_info *ccb;
	u8 retryFlag = 0x1;
	u16 firstBurstSize = 0;
	u16 ITNT = 2000;
	struct domain_device *dev = pm8001_dev->sas_device;
	struct domain_device *parent_dev = dev->parent;
	circularQ = &pm8001_ha->inbnd_q_tbl[0];

	memset(&payload, 0, sizeof(payload));
	rc = pm8001_tag_alloc(pm8001_ha, &tag);
	if (rc)
		return rc;
	ccb = &pm8001_ha->ccb_info[tag];
	ccb->device = pm8001_dev;
	ccb->ccb_tag = tag;
	payload.tag = cpu_to_le32(tag);

	if (flag == 1) {
		stp_sspsmp_sata = 0x02; /*direct attached sata */
	} else {
4772
		if (pm8001_dev->dev_type == SAS_SATA_DEV)
4773
			stp_sspsmp_sata = 0x00; /* stp*/
4774 4775 4776
		else if (pm8001_dev->dev_type == SAS_END_DEVICE ||
			pm8001_dev->dev_type == SAS_EDGE_EXPANDER_DEVICE ||
			pm8001_dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE)
4777 4778
			stp_sspsmp_sata = 0x01; /*ssp or smp*/
	}
4779
	if (parent_dev && dev_is_expander(parent_dev->dev_type))
4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801
		phy_id = parent_dev->ex_dev.ex_phy->phy_id;
	else
		phy_id = pm8001_dev->attached_phy;

	opc = OPC_INB_REG_DEV;

	linkrate = (pm8001_dev->sas_device->linkrate < dev->port->linkrate) ?
			pm8001_dev->sas_device->linkrate : dev->port->linkrate;

	payload.phyid_portid =
		cpu_to_le32(((pm8001_dev->sas_device->port->id) & 0xFF) |
		((phy_id & 0xFF) << 8));

	payload.dtype_dlr_mcn_ir_retry = cpu_to_le32((retryFlag & 0x01) |
		((linkrate & 0x0F) << 24) |
		((stp_sspsmp_sata & 0x03) << 28));
	payload.firstburstsize_ITNexustimeout =
		cpu_to_le32(ITNT | (firstBurstSize * 0x10000));

	memcpy(payload.sas_addr, pm8001_dev->sas_device->sas_addr,
		SAS_ADDR_SIZE);

4802 4803
	rc = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
T
Tomas Henzl 已提交
4804 4805
	if (rc)
		pm8001_tag_free(pm8001_ha, tag);
4806 4807 4808 4809 4810 4811 4812

	return rc;
}

/**
 * pm80xx_chip_phy_ctl_req - support the local phy operation
 * @pm8001_ha: our hba card information.
4813 4814
 * @phyId: the phy id which we wanted to operate
 * @phy_op: phy operation to request
4815 4816 4817 4818
 */
static int pm80xx_chip_phy_ctl_req(struct pm8001_hba_info *pm8001_ha,
	u32 phyId, u32 phy_op)
{
4819 4820
	u32 tag;
	int rc;
4821 4822 4823 4824
	struct local_phy_ctl_req payload;
	struct inbound_queue_table *circularQ;
	u32 opc = OPC_INB_LOCAL_PHY_CONTROL;
	memset(&payload, 0, sizeof(payload));
4825 4826 4827
	rc = pm8001_tag_alloc(pm8001_ha, &tag);
	if (rc)
		return rc;
4828
	circularQ = &pm8001_ha->inbnd_q_tbl[0];
4829
	payload.tag = cpu_to_le32(tag);
4830 4831
	payload.phyop_phyid =
		cpu_to_le32(((phy_op & 0xFF) << 8) | (phyId & 0xFF));
4832 4833
	return pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
4834 4835
}

4836
static u32 pm80xx_chip_is_our_interrupt(struct pm8001_hba_info *pm8001_ha)
4837 4838 4839
{
#ifdef PM8001_USE_MSIX
	return 1;
4840 4841 4842
#else
	u32 value;

4843 4844 4845 4846
	value = pm8001_cr32(pm8001_ha, 0, MSGU_ODR);
	if (value)
		return 1;
	return 0;
4847
#endif
4848 4849 4850 4851 4852
}

/**
 * pm8001_chip_isr - PM8001 isr handler.
 * @pm8001_ha: our hba card information.
4853
 * @vec: irq number.
4854 4855 4856 4857 4858
 */
static irqreturn_t
pm80xx_chip_isr(struct pm8001_hba_info *pm8001_ha, u8 vec)
{
	pm80xx_chip_interrupt_disable(pm8001_ha, vec);
4859 4860 4861
	pm8001_dbg(pm8001_ha, DEVIO,
		   "irq vec %d, ODMR:0x%x\n",
		   vec, pm8001_cr32(pm8001_ha, 0, 0x30));
4862 4863 4864 4865 4866
	process_oq(pm8001_ha, vec);
	pm80xx_chip_interrupt_enable(pm8001_ha, vec);
	return IRQ_HANDLED;
}

4867 4868 4869
static void mpi_set_phy_profile_req(struct pm8001_hba_info *pm8001_ha,
				    u32 operation, u32 phyid,
				    u32 length, u32 *buf)
4870 4871 4872 4873 4874 4875 4876 4877 4878 4879
{
	u32 tag , i, j = 0;
	int rc;
	struct set_phy_profile_req payload;
	struct inbound_queue_table *circularQ;
	u32 opc = OPC_INB_SET_PHY_PROFILE;

	memset(&payload, 0, sizeof(payload));
	rc = pm8001_tag_alloc(pm8001_ha, &tag);
	if (rc)
4880
		pm8001_dbg(pm8001_ha, FAIL, "Invalid tag\n");
4881 4882 4883
	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	payload.tag = cpu_to_le32(tag);
	payload.ppc_phyid = (((operation & 0xF) << 8) | (phyid  & 0xFF));
4884 4885 4886
	pm8001_dbg(pm8001_ha, INIT,
		   " phy profile command for phy %x ,length is %d\n",
		   payload.ppc_phyid, length);
4887 4888 4889 4890
	for (i = length; i < (length + PHY_DWORD_LENGTH - 1); i++) {
		payload.reserved[j] =  cpu_to_le32(*((u32 *)buf + i));
		j++;
	}
4891 4892
	rc = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
T
Tomas Henzl 已提交
4893 4894
	if (rc)
		pm8001_tag_free(pm8001_ha, tag);
4895 4896 4897 4898 4899
}

void pm8001_set_phy_profile(struct pm8001_hba_info *pm8001_ha,
	u32 length, u8 *buf)
{
4900
	u32 i;
4901 4902 4903 4904 4905 4906

	for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
		mpi_set_phy_profile_req(pm8001_ha,
			SAS_PHY_ANALOG_SETTINGS_PAGE, i, length, (u32 *)buf);
		length = length + PHY_DWORD_LENGTH;
	}
4907
	pm8001_dbg(pm8001_ha, INIT, "phy settings completed\n");
4908
}
4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921

void pm8001_set_phy_profile_single(struct pm8001_hba_info *pm8001_ha,
		u32 phy, u32 length, u32 *buf)
{
	u32 tag, opc;
	int rc, i;
	struct set_phy_profile_req payload;
	struct inbound_queue_table *circularQ;

	memset(&payload, 0, sizeof(payload));

	rc = pm8001_tag_alloc(pm8001_ha, &tag);
	if (rc)
4922
		pm8001_dbg(pm8001_ha, INIT, "Invalid tag\n");
4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933

	circularQ = &pm8001_ha->inbnd_q_tbl[0];
	opc = OPC_INB_SET_PHY_PROFILE;

	payload.tag = cpu_to_le32(tag);
	payload.ppc_phyid = (((SAS_PHY_ANALOG_SETTINGS_PAGE & 0xF) << 8)
				| (phy & 0xFF));

	for (i = 0; i < length; i++)
		payload.reserved[i] = cpu_to_le32(*(buf + i));

4934 4935
	rc = pm8001_mpi_build_cmd(pm8001_ha, circularQ, opc, &payload,
			sizeof(payload), 0);
4936 4937 4938
	if (rc)
		pm8001_tag_free(pm8001_ha, tag);

4939
	pm8001_dbg(pm8001_ha, INIT, "PHY %d settings applied\n", phy);
4940
}
4941 4942 4943 4944 4945 4946 4947
const struct pm8001_dispatch pm8001_80xx_dispatch = {
	.name			= "pmc80xx",
	.chip_init		= pm80xx_chip_init,
	.chip_soft_rst		= pm80xx_chip_soft_rst,
	.chip_rst		= pm80xx_hw_chip_rst,
	.chip_iounmap		= pm8001_chip_iounmap,
	.isr			= pm80xx_chip_isr,
4948
	.is_our_interrupt	= pm80xx_chip_is_our_interrupt,
4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967
	.isr_process_oq		= process_oq,
	.interrupt_enable	= pm80xx_chip_interrupt_enable,
	.interrupt_disable	= pm80xx_chip_interrupt_disable,
	.make_prd		= pm8001_chip_make_sg,
	.smp_req		= pm80xx_chip_smp_req,
	.ssp_io_req		= pm80xx_chip_ssp_io_req,
	.sata_req		= pm80xx_chip_sata_req,
	.phy_start_req		= pm80xx_chip_phy_start_req,
	.phy_stop_req		= pm80xx_chip_phy_stop_req,
	.reg_dev_req		= pm80xx_chip_reg_dev_req,
	.dereg_dev_req		= pm8001_chip_dereg_dev_req,
	.phy_ctl_req		= pm80xx_chip_phy_ctl_req,
	.task_abort		= pm8001_chip_abort_task,
	.ssp_tm_req		= pm8001_chip_ssp_tm_req,
	.get_nvmd_req		= pm8001_chip_get_nvmd_req,
	.set_nvmd_req		= pm8001_chip_set_nvmd_req,
	.fw_flash_update_req	= pm8001_chip_fw_flash_update_req,
	.set_dev_state_req	= pm8001_chip_set_dev_state_req,
};