pm8001_sas.c 36.1 KB
Newer Older
J
jack wang 已提交
1
/*
2
 * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
J
jack wang 已提交
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
 *
 * Copyright (c) 2008-2009 USI Co., Ltd.
 * 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.
 *
 */

41
#include <linux/slab.h>
J
jack wang 已提交
42
#include "pm8001_sas.h"
C
Changyuan Lyu 已提交
43
#include "pm80xx_tracepoints.h"
J
jack wang 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

/**
 * pm8001_find_tag - from sas task to find out  tag that belongs to this task
 * @task: the task sent to the LLDD
 * @tag: the found tag associated with the task
 */
static int pm8001_find_tag(struct sas_task *task, u32 *tag)
{
	if (task->lldd_task) {
		struct pm8001_ccb_info *ccb;
		ccb = task->lldd_task;
		*tag = ccb->ccb_tag;
		return 1;
	}
	return 0;
}

/**
62
  * pm8001_tag_free - free the no more needed tag
J
jack wang 已提交
63 64 65
  * @pm8001_ha: our hba struct
  * @tag: the found tag associated with the task
  */
66
void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
J
jack wang 已提交
67 68
{
	void *bitmap = pm8001_ha->tags;
69
	clear_bit(tag, bitmap);
J
jack wang 已提交
70 71 72 73 74 75 76 77 78
}

/**
  * pm8001_tag_alloc - allocate a empty tag for task used.
  * @pm8001_ha: our hba struct
  * @tag_out: the found empty tag .
  */
inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
{
79
	unsigned int tag;
J
jack wang 已提交
80
	void *bitmap = pm8001_ha->tags;
81
	unsigned long flags;
J
jack wang 已提交
82

83
	spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
84
	tag = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
85 86
	if (tag >= pm8001_ha->tags_num) {
		spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
J
jack wang 已提交
87
		return -SAS_QUEUE_FULL;
88
	}
89
	set_bit(tag, bitmap);
90
	spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
J
jack wang 已提交
91 92 93 94 95 96 97 98
	*tag_out = tag;
	return 0;
}

void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
{
	int i;
	for (i = 0; i < pm8001_ha->tags_num; ++i)
99
		pm8001_tag_free(pm8001_ha, i);
J
jack wang 已提交
100 101
}

102 103 104 105 106 107 108 109 110 111
/**
 * pm8001_mem_alloc - allocate memory for pm8001.
 * @pdev: pci device.
 * @virt_addr: the allocated virtual address
 * @pphys_addr: DMA address for this device
 * @pphys_addr_hi: the physical address high byte address.
 * @pphys_addr_lo: the physical address low byte address.
 * @mem_size: memory size.
 * @align: requested byte alignment
 */
J
jack wang 已提交
112 113 114 115 116 117 118 119 120 121
int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
	dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
	u32 *pphys_addr_lo, u32 mem_size, u32 align)
{
	caddr_t mem_virt_alloc;
	dma_addr_t mem_dma_handle;
	u64 phys_align;
	u64 align_offset = 0;
	if (align)
		align_offset = (dma_addr_t)align - 1;
122 123
	mem_virt_alloc = dma_alloc_coherent(&pdev->dev, mem_size + align,
					    &mem_dma_handle, GFP_KERNEL);
124 125
	if (!mem_virt_alloc)
		return -ENOMEM;
J
jack wang 已提交
126 127 128 129 130 131 132
	*pphys_addr = mem_dma_handle;
	phys_align = (*pphys_addr + align_offset) & ~align_offset;
	*virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
	*pphys_addr_hi = upper_32_bits(phys_align);
	*pphys_addr_lo = lower_32_bits(phys_align);
	return 0;
}
133

J
jack wang 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/**
  * pm8001_find_ha_by_dev - from domain device which come from sas layer to
  * find out our hba struct.
  * @dev: the domain device which from sas layer.
  */
static
struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
{
	struct sas_ha_struct *sha = dev->port->ha;
	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
	return pm8001_ha;
}

/**
  * pm8001_phy_control - this function should be registered to
  * sas_domain_function_template to provide libsas used, note: this is just
  * control the HBA phy rather than other expander phy if you want control
  * other phy, you should use SMP command.
  * @sas_phy: which phy in HBA phys.
  * @func: the operation.
  * @funcdata: always NULL.
  */
int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
	void *funcdata)
{
	int rc = 0, phy_id = sas_phy->id;
	struct pm8001_hba_info *pm8001_ha = NULL;
	struct sas_phy_linkrates *rates;
162
	struct pm8001_phy *phy;
J
jack wang 已提交
163
	DECLARE_COMPLETION_ONSTACK(completion);
164
	unsigned long flags;
J
jack wang 已提交
165
	pm8001_ha = sas_phy->ha->lldd_ha;
166
	phy = &pm8001_ha->phy[phy_id];
J
jack wang 已提交
167 168 169 170 171 172 173 174 175 176 177 178
	pm8001_ha->phy[phy_id].enable_completion = &completion;
	switch (func) {
	case PHY_FUNC_SET_LINK_RATE:
		rates = funcdata;
		if (rates->minimum_linkrate) {
			pm8001_ha->phy[phy_id].minimum_linkrate =
				rates->minimum_linkrate;
		}
		if (rates->maximum_linkrate) {
			pm8001_ha->phy[phy_id].maximum_linkrate =
				rates->maximum_linkrate;
		}
179
		if (pm8001_ha->phy[phy_id].phy_state ==  PHY_LINK_DISABLE) {
J
jack wang 已提交
180 181 182 183 184 185 186
			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
			wait_for_completion(&completion);
		}
		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
					      PHY_LINK_RESET);
		break;
	case PHY_FUNC_HARD_RESET:
187
		if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
J
jack wang 已提交
188 189 190 191 192 193 194
			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
			wait_for_completion(&completion);
		}
		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
					      PHY_HARD_RESET);
		break;
	case PHY_FUNC_LINK_RESET:
195
		if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
J
jack wang 已提交
196 197 198 199 200 201 202 203 204 205 206
			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
			wait_for_completion(&completion);
		}
		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
					      PHY_LINK_RESET);
		break;
	case PHY_FUNC_RELEASE_SPINUP_HOLD:
		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
					      PHY_LINK_RESET);
		break;
	case PHY_FUNC_DISABLE:
207 208 209 210
		if (pm8001_ha->chip_id != chip_8001) {
			if (pm8001_ha->phy[phy_id].phy_state ==
				PHY_STATE_LINK_UP_SPCV) {
				sas_phy_disconnected(&phy->sas_phy);
211
				sas_notify_phy_event(&phy->sas_phy,
212
					PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
213 214 215 216 217 218
				phy->phy_attached = 0;
			}
		} else {
			if (pm8001_ha->phy[phy_id].phy_state ==
				PHY_STATE_LINK_UP_SPC) {
				sas_phy_disconnected(&phy->sas_phy);
219
				sas_notify_phy_event(&phy->sas_phy,
220
					PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
221 222 223
				phy->phy_attached = 0;
			}
		}
J
jack wang 已提交
224 225
		PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
		break;
226 227
	case PHY_FUNC_GET_EVENTS:
		spin_lock_irqsave(&pm8001_ha->lock, flags);
228 229
		if (pm8001_ha->chip_id == chip_8001) {
			if (-1 == pm8001_bar4_shift(pm8001_ha,
230
					(phy_id < 4) ? 0x30000 : 0x40000)) {
231 232 233
				spin_unlock_irqrestore(&pm8001_ha->lock, flags);
				return -EINVAL;
			}
234 235 236
		}
		{
			struct sas_phy *phy = sas_phy->phy;
237 238
			u32 __iomem *qp = pm8001_ha->io_mem[2].memvirtaddr
				+ 0x1034 + (0x4000 * (phy_id & 3));
239

240 241 242 243
			phy->invalid_dword_count = readl(qp);
			phy->running_disparity_error_count = readl(&qp[1]);
			phy->loss_of_dword_sync_count = readl(&qp[3]);
			phy->phy_reset_problem_count = readl(&qp[4]);
244
		}
245 246
		if (pm8001_ha->chip_id == chip_8001)
			pm8001_bar4_shift(pm8001_ha, 0);
247 248
		spin_unlock_irqrestore(&pm8001_ha->lock, flags);
		return 0;
J
jack wang 已提交
249
	default:
250
		pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
251
		rc = -EOPNOTSUPP;
J
jack wang 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	}
	msleep(300);
	return rc;
}

/**
  * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
  * command to HBA.
  * @shost: the scsi host data.
  */
void pm8001_scan_start(struct Scsi_Host *shost)
{
	int i;
	struct pm8001_hba_info *pm8001_ha;
	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
267
	DECLARE_COMPLETION_ONSTACK(completion);
J
jack wang 已提交
268
	pm8001_ha = sha->lldd_ha;
269 270 271
	/* SAS_RE_INITIALIZATION not available in SPCv/ve */
	if (pm8001_ha->chip_id == chip_8001)
		PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
272 273
	for (i = 0; i < pm8001_ha->chip->n_phy; ++i) {
		pm8001_ha->phy[i].enable_completion = &completion;
J
jack wang 已提交
274
		PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
275 276 277
		wait_for_completion(&completion);
		msleep(300);
	}
J
jack wang 已提交
278 279 280 281
}

int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
{
282 283
	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);

J
jack wang 已提交
284 285 286 287 288
	/* give the phy enabling interrupt event time to come in (1s
	* is empirically about all it takes) */
	if (time < HZ)
		return 0;
	/* Wait for discovery to finish */
289
	sas_drain_work(ha);
J
jack wang 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	return 1;
}

/**
  * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
  * @pm8001_ha: our hba card information
  * @ccb: the ccb which attached to smp task
  */
static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_ccb_info *ccb)
{
	return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
}

u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
{
	struct ata_queued_cmd *qc = task->uldd_task;
	if (qc) {
		if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
309 310
		    qc->tf.command == ATA_CMD_FPDMA_READ ||
		    qc->tf.command == ATA_CMD_FPDMA_RECV ||
311 312
		    qc->tf.command == ATA_CMD_FPDMA_SEND ||
		    qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
J
jack wang 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
			*tag = qc->tag;
			return 1;
		}
	}
	return 0;
}

/**
  * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
  * @pm8001_ha: our hba card information
  * @ccb: the ccb which attached to sata task
  */
static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_ccb_info *ccb)
{
	return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
}

/**
  * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
  * @pm8001_ha: our hba card information
  * @ccb: the ccb which attached to TM
  * @tmf: the task management IU
  */
static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
338
	struct pm8001_ccb_info *ccb, struct sas_tmf_task *tmf)
J
jack wang 已提交
339 340 341 342 343
{
	return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
}

/**
344
  * pm8001_task_prep_ssp - the dispatcher function, prepare ssp data for ssp task
J
jack wang 已提交
345 346 347 348 349 350 351 352
  * @pm8001_ha: our hba card information
  * @ccb: the ccb which attached to ssp task
  */
static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_ccb_info *ccb)
{
	return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
}
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
 /* Find the local port id that's attached to this device */
static int sas_find_local_port_id(struct domain_device *dev)
{
	struct domain_device *pdev = dev->parent;

	/* Directly attached device */
	if (!pdev)
		return dev->port->id;
	while (pdev) {
		struct domain_device *pdev_p = pdev->parent;
		if (!pdev_p)
			return pdev->port->id;
		pdev = pdev->parent;
	}
	return 0;
}

371 372
#define DEV_IS_GONE(pm8001_dev)	\
	((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
J
John Garry 已提交
373

J
jack wang 已提交
374
/**
J
John Garry 已提交
375 376
  * pm8001_queue_command - register for upper layer used, all IO commands sent
  * to HBA are from this interface.
J
jack wang 已提交
377
  * @task: the task to be execute.
J
John Garry 已提交
378
  * @gfp_flags: gfp_flags
J
jack wang 已提交
379
  */
J
John Garry 已提交
380
int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
J
jack wang 已提交
381 382 383 384
{
	struct domain_device *dev = task->dev;
	struct pm8001_hba_info *pm8001_ha;
	struct pm8001_device *pm8001_dev;
385
	struct pm8001_port *port = NULL;
J
jack wang 已提交
386 387
	struct sas_task *t = task;
	struct pm8001_ccb_info *ccb;
388
	u32 tag = 0xdeadbeef, rc = 0, n_elem = 0;
389
	unsigned long flags = 0;
390
	enum sas_protocol task_proto = t->task_proto;
J
John Garry 已提交
391 392
	struct sas_tmf_task *tmf = task->tmf;
	int is_tmf = !!task->tmf;
J
jack wang 已提交
393 394 395 396 397

	if (!dev->port) {
		struct task_status_struct *tsm = &t->task_status;
		tsm->resp = SAS_TASK_UNDELIVERED;
		tsm->stat = SAS_PHY_DOWN;
398
		if (dev->dev_type != SAS_SATA_DEV)
J
jack wang 已提交
399 400 401 402
			t->task_done(t);
		return 0;
	}
	pm8001_ha = pm8001_find_ha_by_dev(task->dev);
403 404 405 406 407 408 409
	if (pm8001_ha->controller_fatal_error) {
		struct task_status_struct *ts = &t->task_status;

		ts->resp = SAS_TASK_UNDELIVERED;
		t->task_done(t);
		return 0;
	}
410
	pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
J
jack wang 已提交
411 412 413 414
	spin_lock_irqsave(&pm8001_ha->lock, flags);
	do {
		dev = t->dev;
		pm8001_dev = dev->lldd_dev;
415
		port = &pm8001_ha->port[sas_find_local_port_id(dev)];
416
		if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
417
			if (sas_protocol_ata(task_proto)) {
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
				struct task_status_struct *ts = &t->task_status;
				ts->resp = SAS_TASK_UNDELIVERED;
				ts->stat = SAS_PHY_DOWN;

				spin_unlock_irqrestore(&pm8001_ha->lock, flags);
				t->task_done(t);
				spin_lock_irqsave(&pm8001_ha->lock, flags);
				continue;
			} else {
				struct task_status_struct *ts = &t->task_status;
				ts->resp = SAS_TASK_UNDELIVERED;
				ts->stat = SAS_PHY_DOWN;
				t->task_done(t);
				continue;
			}
		}
J
jack wang 已提交
434 435 436 437 438
		rc = pm8001_tag_alloc(pm8001_ha, &tag);
		if (rc)
			goto err_out;
		ccb = &pm8001_ha->ccb_info[tag];

439
		if (!sas_protocol_ata(task_proto)) {
J
jack wang 已提交
440 441 442 443 444 445 446
			if (t->num_scatter) {
				n_elem = dma_map_sg(pm8001_ha->dev,
					t->scatter,
					t->num_scatter,
					t->data_dir);
				if (!n_elem) {
					rc = -ENOMEM;
447
					goto err_out_tag;
J
jack wang 已提交
448 449 450 451 452 453
				}
			}
		} else {
			n_elem = t->num_scatter;
		}

454
		t->lldd_task = ccb;
J
jack wang 已提交
455 456 457
		ccb->n_elem = n_elem;
		ccb->ccb_tag = tag;
		ccb->task = t;
458
		ccb->device = pm8001_dev;
459
		switch (task_proto) {
J
jack wang 已提交
460
		case SAS_PROTOCOL_SMP:
V
Viswas G 已提交
461
			atomic_inc(&pm8001_dev->running_req);
J
jack wang 已提交
462 463 464
			rc = pm8001_task_prep_smp(pm8001_ha, ccb);
			break;
		case SAS_PROTOCOL_SSP:
V
Viswas G 已提交
465
			atomic_inc(&pm8001_dev->running_req);
J
jack wang 已提交
466 467 468 469 470 471 472 473
			if (is_tmf)
				rc = pm8001_task_prep_ssp_tm(pm8001_ha,
					ccb, tmf);
			else
				rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
			break;
		case SAS_PROTOCOL_SATA:
		case SAS_PROTOCOL_STP:
V
Viswas G 已提交
474
			atomic_inc(&pm8001_dev->running_req);
J
jack wang 已提交
475 476 477 478
			rc = pm8001_task_prep_ata(pm8001_ha, ccb);
			break;
		default:
			dev_printk(KERN_ERR, pm8001_ha->dev,
479
				"unknown sas_task proto: 0x%x\n", task_proto);
J
jack wang 已提交
480 481 482 483 484
			rc = -EINVAL;
			break;
		}

		if (rc) {
485
			pm8001_dbg(pm8001_ha, IO, "rc is %x\n", rc);
V
Viswas G 已提交
486
			atomic_dec(&pm8001_dev->running_req);
J
jack wang 已提交
487 488 489
			goto err_out_tag;
		}
		/* TODO: select normal or high priority */
490
	} while (0);
J
jack wang 已提交
491 492 493 494 495 496 497
	rc = 0;
	goto out_done;

err_out_tag:
	pm8001_tag_free(pm8001_ha, tag);
err_out:
	dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
498
	if (!sas_protocol_ata(task_proto))
J
jack wang 已提交
499
		if (n_elem)
500
			dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter,
J
jack wang 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
				t->data_dir);
out_done:
	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
	return rc;
}

/**
  * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
  * @pm8001_ha: our hba card information
  * @ccb: the ccb which attached to ssp task
  * @task: the task to be free.
  * @ccb_idx: ccb index.
  */
void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
	struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
{
C
Changyuan Lyu 已提交
517 518 519
	struct ata_queued_cmd *qc;
	struct pm8001_device *pm8001_dev;

J
jack wang 已提交
520 521 522 523 524 525 526 527 528 529
	if (!ccb->task)
		return;
	if (!sas_protocol_ata(task->task_proto))
		if (ccb->n_elem)
			dma_unmap_sg(pm8001_ha->dev, task->scatter,
				task->num_scatter, task->data_dir);

	switch (task->task_proto) {
	case SAS_PROTOCOL_SMP:
		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
530
			DMA_FROM_DEVICE);
J
jack wang 已提交
531
		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
532
			DMA_TO_DEVICE);
J
jack wang 已提交
533 534 535 536 537 538 539 540 541
		break;

	case SAS_PROTOCOL_SATA:
	case SAS_PROTOCOL_STP:
	case SAS_PROTOCOL_SSP:
	default:
		/* do nothing */
		break;
	}
C
Changyuan Lyu 已提交
542 543 544 545 546 547 548 549 550 551 552 553

	if (sas_protocol_ata(task->task_proto)) {
		// For SCSI/ATA commands uldd_task points to ata_queued_cmd
		qc = task->uldd_task;
		pm8001_dev = ccb->device;
		trace_pm80xx_request_complete(pm8001_ha->id,
			pm8001_dev ? pm8001_dev->attached_phy : PM8001_MAX_PHYS,
			ccb_idx, 0 /* ctlr_opcode not known */,
			qc ? qc->tf.command : 0, // ata opcode
			pm8001_dev ? atomic_read(&pm8001_dev->running_req) : -1);
	}

J
jack wang 已提交
554 555
	task->lldd_task = NULL;
	ccb->task = NULL;
556
	ccb->ccb_tag = PM8001_INVALID_TAG;
557
	ccb->open_retry = 0;
558
	pm8001_tag_free(pm8001_ha, ccb_idx);
J
jack wang 已提交
559 560
}

561 562 563 564
/**
 * pm8001_alloc_dev - find a empty pm8001_device
 * @pm8001_ha: our hba card information
 */
565
static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
J
jack wang 已提交
566 567 568
{
	u32 dev;
	for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
569
		if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
J
jack wang 已提交
570 571 572 573 574
			pm8001_ha->devices[dev].id = dev;
			return &pm8001_ha->devices[dev];
		}
	}
	if (dev == PM8001_MAX_DEVICES) {
575 576 577
		pm8001_dbg(pm8001_ha, FAIL,
			   "max support %d devices, ignore ..\n",
			   PM8001_MAX_DEVICES);
J
jack wang 已提交
578 579 580
	}
	return NULL;
}
581 582 583
/**
  * pm8001_find_dev - find a matching pm8001_device
  * @pm8001_ha: our hba card information
584
  * @device_id: device ID to match against
585 586 587 588 589 590 591 592 593 594
  */
struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
					u32 device_id)
{
	u32 dev;
	for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
		if (pm8001_ha->devices[dev].device_id == device_id)
			return &pm8001_ha->devices[dev];
	}
	if (dev == PM8001_MAX_DEVICES) {
595
		pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
596 597 598
	}
	return NULL;
}
J
jack wang 已提交
599

600
void pm8001_free_dev(struct pm8001_device *pm8001_dev)
J
jack wang 已提交
601 602 603 604
{
	u32 id = pm8001_dev->id;
	memset(pm8001_dev, 0, sizeof(*pm8001_dev));
	pm8001_dev->id = id;
605
	pm8001_dev->dev_type = SAS_PHY_UNUSED;
J
jack wang 已提交
606 607 608 609 610
	pm8001_dev->device_id = PM8001_MAX_DEVICES;
	pm8001_dev->sas_device = NULL;
}

/**
611 612 613 614 615 616 617
  * pm8001_dev_found_notify - libsas notify a device is found.
  * @dev: the device structure which sas layer used.
  *
  * when libsas find a sas domain device, it should tell the LLDD that
  * device is found, and then LLDD register this device to HBA firmware
  * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
  * device ID(according to device's sas address) and returned it to LLDD. From
J
jack wang 已提交
618
  * now on, we communicate with HBA FW with the device ID which HBA assigned
D
Daniel Mack 已提交
619
  * rather than sas address. it is the necessary step for our HBA but it is
J
jack wang 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
  * the optional for other HBA driver.
  */
static int pm8001_dev_found_notify(struct domain_device *dev)
{
	unsigned long flags = 0;
	int res = 0;
	struct pm8001_hba_info *pm8001_ha = NULL;
	struct domain_device *parent_dev = dev->parent;
	struct pm8001_device *pm8001_device;
	DECLARE_COMPLETION_ONSTACK(completion);
	u32 flag = 0;
	pm8001_ha = pm8001_find_ha_by_dev(dev);
	spin_lock_irqsave(&pm8001_ha->lock, flags);

	pm8001_device = pm8001_alloc_dev(pm8001_ha);
	if (!pm8001_device) {
		res = -1;
		goto found_out;
	}
639
	pm8001_device->sas_device = dev;
J
jack wang 已提交
640 641 642
	dev->lldd_dev = pm8001_device;
	pm8001_device->dev_type = dev->dev_type;
	pm8001_device->dcompletion = &completion;
643
	if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
J
jack wang 已提交
644 645 646 647 648 649 650 651 652 653 654 655
		int phy_id;
		struct ex_phy *phy;
		for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
		phy_id++) {
			phy = &parent_dev->ex_dev.ex_phy[phy_id];
			if (SAS_ADDR(phy->attached_sas_addr)
				== SAS_ADDR(dev->sas_addr)) {
				pm8001_device->attached_phy = phy_id;
				break;
			}
		}
		if (phy_id == parent_dev->ex_dev.num_phys) {
656 657 658 659
			pm8001_dbg(pm8001_ha, FAIL,
				   "Error: no attached dev:%016llx at ex:%016llx.\n",
				   SAS_ADDR(dev->sas_addr),
				   SAS_ADDR(parent_dev->sas_addr));
J
jack wang 已提交
660 661 662
			res = -1;
		}
	} else {
663
		if (dev->dev_type == SAS_SATA_DEV) {
J
jack wang 已提交
664 665
			pm8001_device->attached_phy =
				dev->rphy->identify.phy_identifier;
J
Julia Lawall 已提交
666
			flag = 1; /* directly sata */
J
jack wang 已提交
667 668
		}
	} /*register this device to HBA*/
669
	pm8001_dbg(pm8001_ha, DISC, "Found device\n");
J
jack wang 已提交
670 671 672
	PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
	wait_for_completion(&completion);
673
	if (dev->dev_type == SAS_END_DEVICE)
J
jack wang 已提交
674
		msleep(50);
675
	pm8001_ha->flags = PM8001F_RUN_TIME;
J
jack wang 已提交
676 677 678 679 680 681 682 683 684 685 686
	return 0;
found_out:
	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
	return res;
}

int pm8001_dev_found(struct domain_device *dev)
{
	return pm8001_dev_found_notify(dev);
}

687
void pm8001_task_done(struct sas_task *task)
J
jack wang 已提交
688
{
689
	del_timer(&task->slow_task->timer);
690
	complete(&task->slow_task->completion);
J
jack wang 已提交
691 692
}

693
static void pm8001_tmf_timedout(struct timer_list *t)
J
jack wang 已提交
694
{
695 696
	struct sas_task_slow *slow = from_timer(slow, t, timer);
	struct sas_task *task = slow->task;
697
	unsigned long flags;
J
jack wang 已提交
698

699 700 701 702 703 704
	spin_lock_irqsave(&task->task_state_lock, flags);
	if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
		complete(&task->slow_task->completion);
	}
	spin_unlock_irqrestore(&task->task_state_lock, flags);
J
jack wang 已提交
705 706 707 708 709 710 711 712 713
}

#define PM8001_TASK_TIMEOUT 20
static int
pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
	struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
	u32 task_tag)
{
	int res, retry;
714
	u32 ccb_tag;
J
jack wang 已提交
715 716 717 718
	struct pm8001_ccb_info *ccb;
	struct sas_task *task = NULL;

	for (retry = 0; retry < 3; retry++) {
719
		task = sas_alloc_slow_task(GFP_KERNEL);
J
jack wang 已提交
720 721 722 723 724 725
		if (!task)
			return -ENOMEM;

		task->dev = dev;
		task->task_proto = dev->tproto;
		task->task_done = pm8001_task_done;
726
		task->slow_task->timer.function = pm8001_tmf_timedout;
727 728
		task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
		add_timer(&task->slow_task->timer);
J
jack wang 已提交
729

730 731
		res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
		if (res)
732
			goto ex_err;
J
jack wang 已提交
733 734 735 736
		ccb = &pm8001_ha->ccb_info[ccb_tag];
		ccb->device = pm8001_dev;
		ccb->ccb_tag = ccb_tag;
		ccb->task = task;
737
		ccb->n_elem = 0;
J
jack wang 已提交
738 739 740 741 742

		res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
			pm8001_dev, flag, task_tag, ccb_tag);

		if (res) {
743
			del_timer(&task->slow_task->timer);
744
			pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
J
jack wang 已提交
745 746
			goto ex_err;
		}
747
		wait_for_completion(&task->slow_task->completion);
J
jack wang 已提交
748 749
		res = TMF_RESP_FUNC_FAILED;
		/* Even TMF timed out, return direct. */
750 751 752
		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
			pm8001_dbg(pm8001_ha, FAIL, "TMF task timeout.\n");
			goto ex_err;
J
jack wang 已提交
753 754 755
		}

		if (task->task_status.resp == SAS_TASK_COMPLETE &&
756
			task->task_status.stat == SAS_SAM_STAT_GOOD) {
J
jack wang 已提交
757 758 759 760
			res = TMF_RESP_FUNC_COMPLETE;
			break;

		} else {
761 762 763 764 765
			pm8001_dbg(pm8001_ha, EH,
				   " Task to dev %016llx response: 0x%x status 0x%x\n",
				   SAS_ADDR(dev->sas_addr),
				   task->task_status.resp,
				   task->task_status.stat);
766
			sas_free_task(task);
J
jack wang 已提交
767 768 769 770 771
			task = NULL;
		}
	}
ex_err:
	BUG_ON(retry == 3 && task != NULL);
772
	sas_free_task(task);
J
jack wang 已提交
773 774 775 776 777 778 779 780 781 782 783 784
	return res;
}

/**
  * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
  * @dev: the device structure which sas layer used.
  */
static void pm8001_dev_gone_notify(struct domain_device *dev)
{
	unsigned long flags = 0;
	struct pm8001_hba_info *pm8001_ha;
	struct pm8001_device *pm8001_dev = dev->lldd_dev;
785

J
jack wang 已提交
786 787 788
	pm8001_ha = pm8001_find_ha_by_dev(dev);
	spin_lock_irqsave(&pm8001_ha->lock, flags);
	if (pm8001_dev) {
789 790
		u32 device_id = pm8001_dev->device_id;

791 792
		pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
			   pm8001_dev->device_id, pm8001_dev->dev_type);
V
Viswas G 已提交
793
		if (atomic_read(&pm8001_dev->running_req)) {
J
jack wang 已提交
794
			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
L
Luo Jiaxing 已提交
795 796
			pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
							dev, 1, 0);
V
Viswas G 已提交
797
			while (atomic_read(&pm8001_dev->running_req))
798
				msleep(20);
J
jack wang 已提交
799 800 801 802 803
			spin_lock_irqsave(&pm8001_ha->lock, flags);
		}
		PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
		pm8001_free_dev(pm8001_dev);
	} else {
804
		pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
J
jack wang 已提交
805 806 807 808 809 810 811 812 813 814
	}
	dev->lldd_dev = NULL;
	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
}

void pm8001_dev_gone(struct domain_device *dev)
{
	pm8001_dev_gone_notify(dev);
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
/* retry commands by ha, by task and/or by device */
void pm8001_open_reject_retry(
	struct pm8001_hba_info *pm8001_ha,
	struct sas_task *task_to_close,
	struct pm8001_device *device_to_close)
{
	int i;
	unsigned long flags;

	if (pm8001_ha == NULL)
		return;

	spin_lock_irqsave(&pm8001_ha->lock, flags);

	for (i = 0; i < PM8001_MAX_CCB; i++) {
		struct sas_task *task;
		struct task_status_struct *ts;
		struct pm8001_device *pm8001_dev;
		unsigned long flags1;
		struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];

836 837 838
		if (ccb->ccb_tag == PM8001_INVALID_TAG)
			continue;

839
		pm8001_dev = ccb->device;
840
		if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
			continue;
		if (!device_to_close) {
			uintptr_t d = (uintptr_t)pm8001_dev
					- (uintptr_t)&pm8001_ha->devices;
			if (((d % sizeof(*pm8001_dev)) != 0)
			 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
				continue;
		} else if (pm8001_dev != device_to_close)
			continue;
		task = ccb->task;
		if (!task || !task->task_done)
			continue;
		if (task_to_close && (task != task_to_close))
			continue;
		ts = &task->task_status;
		ts->resp = SAS_TASK_COMPLETE;
		/* Force the midlayer to retry */
		ts->stat = SAS_OPEN_REJECT;
		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
		if (pm8001_dev)
V
Viswas G 已提交
861
			atomic_dec(&pm8001_dev->running_req);
862 863 864 865 866 867 868
		spin_lock_irqsave(&task->task_state_lock, flags1);
		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
		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,
				flags1);
869
			pm8001_ccb_task_free(pm8001_ha, task, ccb, ccb->ccb_tag);
870 871 872
		} else {
			spin_unlock_irqrestore(&task->task_state_lock,
				flags1);
873
			pm8001_ccb_task_free(pm8001_ha, task, ccb, ccb->ccb_tag);
874 875 876 877 878 879 880 881 882 883
			mb();/* in order to force CPU ordering */
			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
			task->task_done(task);
			spin_lock_irqsave(&pm8001_ha->lock, flags);
		}
	}

	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
}

J
jack wang 已提交
884
/**
885 886 887 888 889 890
 * pm8001_I_T_nexus_reset() - reset the initiator/target connection
 * @dev: the device structure for the device to reset.
 *
 * Standard mandates link reset for ATA (type 0) and hard reset for
 * SSP (type 1), only for RECOVERY
 */
J
jack wang 已提交
891 892 893 894 895 896
int pm8001_I_T_nexus_reset(struct domain_device *dev)
{
	int rc = TMF_RESP_FUNC_FAILED;
	struct pm8001_device *pm8001_dev;
	struct pm8001_hba_info *pm8001_ha;
	struct sas_phy *phy;
897

J
jack wang 已提交
898
	if (!dev || !dev->lldd_dev)
899
		return -ENODEV;
J
jack wang 已提交
900 901 902

	pm8001_dev = dev->lldd_dev;
	pm8001_ha = pm8001_find_ha_by_dev(dev);
903
	phy = sas_get_local_phy(dev);
J
jack wang 已提交
904 905

	if (dev_is_sata(dev)) {
906 907 908 909
		if (scsi_is_sas_phy_local(phy)) {
			rc = 0;
			goto out;
		}
J
jack wang 已提交
910
		rc = sas_phy_reset(phy, 1);
911
		if (rc) {
912 913 914
			pm8001_dbg(pm8001_ha, EH,
				   "phy reset failed for device %x\n"
				   "with rc %d\n", pm8001_dev->device_id, rc);
915 916 917
			rc = TMF_RESP_FUNC_FAILED;
			goto out;
		}
J
jack wang 已提交
918
		msleep(2000);
L
Luo Jiaxing 已提交
919 920
		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
						     dev, 1, 0);
921
		if (rc) {
922 923
			pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
				   "with rc %d\n", pm8001_dev->device_id, rc);
924 925
			rc = TMF_RESP_FUNC_FAILED;
		}
926 927 928
	} else {
		rc = sas_phy_reset(phy, 1);
		msleep(2000);
J
jack wang 已提交
929
	}
930 931
	pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
		   pm8001_dev->device_id, rc);
932 933
 out:
	sas_put_local_phy(phy);
J
jack wang 已提交
934 935 936
	return rc;
}

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
/*
* This function handle the IT_NEXUS_XXX event or completion
* status code for SSP/SATA/SMP I/O request.
*/
int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
{
	int rc = TMF_RESP_FUNC_FAILED;
	struct pm8001_device *pm8001_dev;
	struct pm8001_hba_info *pm8001_ha;
	struct sas_phy *phy;

	if (!dev || !dev->lldd_dev)
		return -1;

	pm8001_dev = dev->lldd_dev;
	pm8001_ha = pm8001_find_ha_by_dev(dev);

954
	pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
955 956 957 958 959 960 961 962 963 964

	phy = sas_get_local_phy(dev);

	if (dev_is_sata(dev)) {
		DECLARE_COMPLETION_ONSTACK(completion_setstate);
		if (scsi_is_sas_phy_local(phy)) {
			rc = 0;
			goto out;
		}
		/* send internal ssp/sata/smp abort command to FW */
L
Luo Jiaxing 已提交
965 966
		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
						     dev, 1, 0);
967 968 969 970 971 972 973 974 975 976 977 978 979 980
		msleep(100);

		/* deregister the target device */
		pm8001_dev_gone_notify(dev);
		msleep(200);

		/*send phy reset to hard reset target */
		rc = sas_phy_reset(phy, 1);
		msleep(2000);
		pm8001_dev->setds_completion = &completion_setstate;

		wait_for_completion(&completion_setstate);
	} else {
		/* send internal ssp/sata/smp abort command to FW */
L
Luo Jiaxing 已提交
981 982
		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
						     dev, 1, 0);
983 984 985 986 987 988 989 990 991 992
		msleep(100);

		/* deregister the target device */
		pm8001_dev_gone_notify(dev);
		msleep(200);

		/*send phy reset to hard reset target */
		rc = sas_phy_reset(phy, 1);
		msleep(2000);
	}
993 994
	pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
		   pm8001_dev->device_id, rc);
995 996 997 998 999
out:
	sas_put_local_phy(phy);

	return rc;
}
J
jack wang 已提交
1000 1001 1002 1003 1004 1005
/* mandatory SAM-3, the task reset the specified LUN*/
int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
{
	int rc = TMF_RESP_FUNC_FAILED;
	struct pm8001_device *pm8001_dev = dev->lldd_dev;
	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1006
	DECLARE_COMPLETION_ONSTACK(completion_setstate);
J
jack wang 已提交
1007
	if (dev_is_sata(dev)) {
1008
		struct sas_phy *phy = sas_get_local_phy(dev);
L
Luo Jiaxing 已提交
1009 1010
		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
						     dev, 1, 0);
J
jack wang 已提交
1011
		rc = sas_phy_reset(phy, 1);
1012
		sas_put_local_phy(phy);
1013
		pm8001_dev->setds_completion = &completion_setstate;
J
jack wang 已提交
1014
		rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1015
			pm8001_dev, DS_OPERATIONAL);
1016
		wait_for_completion(&completion_setstate);
J
jack wang 已提交
1017
	} else {
J
John Garry 已提交
1018
		rc = sas_lu_reset(dev, lun);
J
jack wang 已提交
1019 1020
	}
	/* If failed, fall-through I_T_Nexus reset */
1021 1022
	pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
		   pm8001_dev->device_id, rc);
J
jack wang 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	return rc;
}

/* optional SAM-3 */
int pm8001_query_task(struct sas_task *task)
{
	u32 tag = 0xdeadbeef;
	int rc = TMF_RESP_FUNC_FAILED;
	if (unlikely(!task || !task->lldd_task || !task->dev))
		return rc;

	if (task->task_proto & SAS_PROTOCOL_SSP) {
		struct scsi_cmnd *cmnd = task->uldd_task;
		struct domain_device *dev = task->dev;
		struct pm8001_hba_info *pm8001_ha =
			pm8001_find_ha_by_dev(dev);

		rc = pm8001_find_tag(task, &tag);
		if (rc == 0) {
			rc = TMF_RESP_FUNC_FAILED;
			return rc;
		}
1045
		pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
J
jack wang 已提交
1046

J
John Garry 已提交
1047
		rc = sas_query_task(task, tag);
J
jack wang 已提交
1048 1049 1050
		switch (rc) {
		/* The task is still in Lun, release it then */
		case TMF_RESP_FUNC_SUCC:
1051 1052
			pm8001_dbg(pm8001_ha, EH,
				   "The task is still in Lun\n");
1053
			break;
J
jack wang 已提交
1054 1055 1056
		/* The task is not in Lun or failed, reset the phy */
		case TMF_RESP_FUNC_FAILED:
		case TMF_RESP_FUNC_COMPLETE:
1057 1058
			pm8001_dbg(pm8001_ha, EH,
				   "The task is not in Lun or failed, reset the phy\n");
J
jack wang 已提交
1059 1060 1061
			break;
		}
	}
1062
	pr_err("pm80xx: rc= %d\n", rc);
J
jack wang 已提交
1063 1064 1065
	return rc;
}

1066
/*  mandatory SAM-3, still need free task/ccb info, abort the specified task */
J
jack wang 已提交
1067 1068 1069
int pm8001_abort_task(struct sas_task *task)
{
	unsigned long flags;
1070
	u32 tag;
J
jack wang 已提交
1071
	struct domain_device *dev ;
1072
	struct pm8001_hba_info *pm8001_ha;
J
jack wang 已提交
1073
	struct pm8001_device *pm8001_dev;
1074
	int rc = TMF_RESP_FUNC_FAILED, ret;
1075
	u32 phy_id, port_id;
1076
	struct sas_task_slow slow_task;
A
akshatzen 已提交
1077

J
jack wang 已提交
1078
	if (unlikely(!task || !task->lldd_task || !task->dev))
1079
		return TMF_RESP_FUNC_FAILED;
A
akshatzen 已提交
1080

1081 1082 1083
	dev = task->dev;
	pm8001_dev = dev->lldd_dev;
	pm8001_ha = pm8001_find_ha_by_dev(dev);
1084
	phy_id = pm8001_dev->attached_phy;
A
akshatzen 已提交
1085 1086 1087 1088 1089 1090 1091

	if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
		// If the controller is seeing fatal errors
		// abort task will not get a response from the controller
		return TMF_RESP_FUNC_FAILED;
	}

1092 1093
	ret = pm8001_find_tag(task, &tag);
	if (ret == 0) {
1094
		pm8001_info(pm8001_ha, "no tag for task:%p\n", task);
1095 1096
		return TMF_RESP_FUNC_FAILED;
	}
J
jack wang 已提交
1097 1098 1099
	spin_lock_irqsave(&task->task_state_lock, flags);
	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
		spin_unlock_irqrestore(&task->task_state_lock, flags);
1100
		return TMF_RESP_FUNC_COMPLETE;
J
jack wang 已提交
1101
	}
1102 1103 1104 1105
	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
	if (task->slow_task == NULL) {
		init_completion(&slow_task.completion);
		task->slow_task = &slow_task;
J
jack wang 已提交
1106 1107 1108
	}
	spin_unlock_irqrestore(&task->task_state_lock, flags);
	if (task->task_proto & SAS_PROTOCOL_SSP) {
J
John Garry 已提交
1109
		rc = sas_abort_task(task, tag);
1110
		pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
J
jack wang 已提交
1111 1112 1113
			pm8001_dev->sas_device, 0, tag);
	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
		task->task_proto & SAS_PROTOCOL_STP) {
1114 1115 1116 1117
		if (pm8001_ha->chip_id == chip_8006) {
			DECLARE_COMPLETION_ONSTACK(completion_reset);
			DECLARE_COMPLETION_ONSTACK(completion);
			struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1118
			port_id = phy->port->port_id;
1119 1120 1121 1122

			/* 1. Set Device state as Recovery */
			pm8001_dev->setds_completion = &completion;
			PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1123
				pm8001_dev, DS_IN_RECOVERY);
1124 1125 1126 1127
			wait_for_completion(&completion);

			/* 2. Send Phy Control Hard Reset */
			reinit_completion(&completion);
1128
			phy->port_reset_status = PORT_RESET_TMO;
1129 1130 1131 1132 1133
			phy->reset_success = false;
			phy->enable_completion = &completion;
			phy->reset_completion = &completion_reset;
			ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
				PHY_HARD_RESET);
1134 1135 1136
			if (ret) {
				phy->enable_completion = NULL;
				phy->reset_completion = NULL;
1137
				goto out;
1138
			}
1139

1140 1141 1142 1143 1144 1145 1146 1147
			/* In the case of the reset timeout/fail we still
			 * abort the command at the firmware. The assumption
			 * here is that the drive is off doing something so
			 * that it's not processing requests, and we want to
			 * avoid getting a completion for this and either
			 * leaking the task in libsas or losing the race and
			 * getting a double free.
			 */
1148 1149
			pm8001_dbg(pm8001_ha, MSG,
				   "Waiting for local phy ctl\n");
1150 1151 1152 1153 1154 1155 1156 1157 1158
			ret = wait_for_completion_timeout(&completion,
					PM8001_TASK_TIMEOUT * HZ);
			if (!ret || !phy->reset_success) {
				phy->enable_completion = NULL;
				phy->reset_completion = NULL;
			} else {
				/* 3. Wait for Port Reset complete or
				 * Port reset TMO
				 */
1159 1160
				pm8001_dbg(pm8001_ha, MSG,
					   "Waiting for Port reset\n");
1161 1162 1163 1164 1165 1166 1167 1168 1169
				ret = wait_for_completion_timeout(
					&completion_reset,
					PM8001_TASK_TIMEOUT * HZ);
				if (!ret)
					phy->reset_completion = NULL;
				WARN_ON(phy->port_reset_status ==
						PORT_RESET_TMO);
				if (phy->port_reset_status == PORT_RESET_TMO) {
					pm8001_dev_gone_notify(dev);
1170 1171 1172 1173
					PM8001_CHIP_DISP->hw_event_ack_req(
						pm8001_ha, 0,
						0x07, /*HW_EVENT_PHY_DOWN ack*/
						port_id, phy_id, 0, 0);
1174 1175
					goto out;
				}
1176
			}
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

			/*
			 * 4. SATA Abort ALL
			 * we wait for the task to be aborted so that the task
			 * is removed from the ccb. on success the caller is
			 * going to free the task.
			 */
			ret = pm8001_exec_internal_task_abort(pm8001_ha,
				pm8001_dev, pm8001_dev->sas_device, 1, tag);
			if (ret)
				goto out;
			ret = wait_for_completion_timeout(
				&task->slow_task->completion,
				PM8001_TASK_TIMEOUT * HZ);
			if (!ret)
				goto out;

			/* 5. Set Device State as Operational */
			reinit_completion(&completion);
			pm8001_dev->setds_completion = &completion;
			PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1198
				pm8001_dev, DS_OPERATIONAL);
1199 1200 1201 1202
			wait_for_completion(&completion);
		} else {
			rc = pm8001_exec_internal_task_abort(pm8001_ha,
				pm8001_dev, pm8001_dev->sas_device, 0, tag);
J
jack wang 已提交
1203
		}
1204
		rc = TMF_RESP_FUNC_COMPLETE;
J
jack wang 已提交
1205 1206 1207 1208 1209 1210 1211
	} else if (task->task_proto & SAS_PROTOCOL_SMP) {
		/* SMP */
		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
			pm8001_dev->sas_device, 0, tag);

	}
out:
1212 1213 1214 1215
	spin_lock_irqsave(&task->task_state_lock, flags);
	if (task->slow_task == &slow_task)
		task->slow_task = NULL;
	spin_unlock_irqrestore(&task->task_state_lock, flags);
J
jack wang 已提交
1216
	if (rc != TMF_RESP_FUNC_COMPLETE)
1217
		pm8001_info(pm8001_ha, "rc= %d\n", rc);
J
jack wang 已提交
1218 1219 1220 1221 1222 1223 1224 1225
	return rc;
}

int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
{
	struct pm8001_device *pm8001_dev = dev->lldd_dev;
	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);

1226 1227
	pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
		   pm8001_dev->device_id);
1228
	return sas_clear_task_set(dev, lun);
J
jack wang 已提交
1229
}
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244

void pm8001_port_formed(struct asd_sas_phy *sas_phy)
{
	struct sas_ha_struct *sas_ha = sas_phy->ha;
	struct pm8001_hba_info *pm8001_ha = sas_ha->lldd_ha;
	struct pm8001_phy *phy = sas_phy->lldd_phy;
	struct asd_sas_port *sas_port = sas_phy->port;
	struct pm8001_port *port = phy->port;

	if (!sas_port) {
		pm8001_dbg(pm8001_ha, FAIL, "Received null port\n");
		return;
	}
	sas_port->lldd_port = port;
}
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258

void pm8001_setds_completion(struct domain_device *dev)
{
	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
	struct pm8001_device *pm8001_dev = dev->lldd_dev;
	DECLARE_COMPLETION_ONSTACK(completion_setstate);

	if (pm8001_ha->chip_id != chip_8001) {
		pm8001_dev->setds_completion = &completion_setstate;
		PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
			pm8001_dev, DS_OPERATIONAL);
		wait_for_completion(&completion_setstate);
	}
}
1259 1260 1261 1262 1263 1264 1265 1266

void pm8001_tmf_aborted(struct sas_task *task)
{
	struct pm8001_ccb_info *ccb = task->lldd_task;

	if (ccb)
		ccb->task = NULL;
}