Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * PMC-Sierra SPC 8001 SAS/SATA based host adapters driver
   3 *
   4 * Copyright (c) 2008-2009 USI Co., Ltd.
   5 * All rights reserved.
   6 *
   7 * Redistribution and use in source and binary forms, with or without
   8 * modification, are permitted provided that the following conditions
   9 * are met:
  10 * 1. Redistributions of source code must retain the above copyright
  11 *    notice, this list of conditions, and the following disclaimer,
  12 *    without modification.
  13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  14 *    substantially similar to the "NO WARRANTY" disclaimer below
  15 *    ("Disclaimer") and any redistribution must be conditioned upon
  16 *    including a substantially similar Disclaimer requirement for further
  17 *    binary redistribution.
  18 * 3. Neither the names of the above-listed copyright holders nor the names
  19 *    of any contributors may be used to endorse or promote products derived
  20 *    from this software without specific prior written permission.
  21 *
  22 * Alternatively, this software may be distributed under the terms of the
  23 * GNU General Public License ("GPL") version 2 as published by the Free
  24 * Software Foundation.
  25 *
  26 * NO WARRANTY
  27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37 * POSSIBILITY OF SUCH DAMAGES.
  38 *
  39 */
  40
  41#include <linux/slab.h>
  42#include "pm8001_sas.h"
 
  43
  44/**
  45 * pm8001_find_tag - from sas task to find out  tag that belongs to this task
  46 * @task: the task sent to the LLDD
  47 * @tag: the found tag associated with the task
  48 */
  49static int pm8001_find_tag(struct sas_task *task, u32 *tag)
  50{
  51	if (task->lldd_task) {
  52		struct pm8001_ccb_info *ccb;
  53		ccb = task->lldd_task;
  54		*tag = ccb->ccb_tag;
  55		return 1;
  56	}
  57	return 0;
  58}
  59
  60/**
  61  * pm8001_tag_clear - clear the tags bitmap
  62  * @pm8001_ha: our hba struct
  63  * @tag: the found tag associated with the task
  64  */
  65static void pm8001_tag_clear(struct pm8001_hba_info *pm8001_ha, u32 tag)
  66{
  67	void *bitmap = pm8001_ha->tags;
  68	clear_bit(tag, bitmap);
  69}
  70
  71static void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
  72{
  73	pm8001_tag_clear(pm8001_ha, tag);
  74}
  75
  76static void pm8001_tag_set(struct pm8001_hba_info *pm8001_ha, u32 tag)
  77{
  78	void *bitmap = pm8001_ha->tags;
  79	set_bit(tag, bitmap);
  80}
  81
  82/**
  83  * pm8001_tag_alloc - allocate a empty tag for task used.
  84  * @pm8001_ha: our hba struct
  85  * @tag_out: the found empty tag .
  86  */
  87inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
  88{
  89	unsigned int index, tag;
  90	void *bitmap = pm8001_ha->tags;
 
  91
  92	index = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
  93	tag = index;
  94	if (tag >= pm8001_ha->tags_num)
 
  95		return -SAS_QUEUE_FULL;
  96	pm8001_tag_set(pm8001_ha, tag);
 
 
 
 
  97	*tag_out = tag;
  98	return 0;
  99}
 100
 101void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
 102{
 103	int i;
 104	for (i = 0; i < pm8001_ha->tags_num; ++i)
 105		pm8001_tag_clear(pm8001_ha, i);
 106}
 107
 108 /**
 109  * pm8001_mem_alloc - allocate memory for pm8001.
 110  * @pdev: pci device.
 111  * @virt_addr: the allocated virtual address
 112  * @pphys_addr_hi: the physical address high byte address.
 113  * @pphys_addr_lo: the physical address low byte address.
 114  * @mem_size: memory size.
 115  */
 116int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
 117	dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
 118	u32 *pphys_addr_lo, u32 mem_size, u32 align)
 119{
 120	caddr_t mem_virt_alloc;
 121	dma_addr_t mem_dma_handle;
 122	u64 phys_align;
 123	u64 align_offset = 0;
 124	if (align)
 125		align_offset = (dma_addr_t)align - 1;
 126	mem_virt_alloc =
 127		pci_alloc_consistent(pdev, mem_size + align, &mem_dma_handle);
 128	if (!mem_virt_alloc) {
 129		pm8001_printk("memory allocation error\n");
 130		return -1;
 131	}
 132	memset((void *)mem_virt_alloc, 0, mem_size+align);
 133	*pphys_addr = mem_dma_handle;
 134	phys_align = (*pphys_addr + align_offset) & ~align_offset;
 135	*virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
 136	*pphys_addr_hi = upper_32_bits(phys_align);
 137	*pphys_addr_lo = lower_32_bits(phys_align);
 138	return 0;
 139}
 
 140/**
 141  * pm8001_find_ha_by_dev - from domain device which come from sas layer to
 142  * find out our hba struct.
 143  * @dev: the domain device which from sas layer.
 144  */
 145static
 146struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
 147{
 148	struct sas_ha_struct *sha = dev->port->ha;
 149	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
 150	return pm8001_ha;
 151}
 152
 153/**
 154  * pm8001_phy_control - this function should be registered to
 155  * sas_domain_function_template to provide libsas used, note: this is just
 156  * control the HBA phy rather than other expander phy if you want control
 157  * other phy, you should use SMP command.
 158  * @sas_phy: which phy in HBA phys.
 159  * @func: the operation.
 160  * @funcdata: always NULL.
 161  */
 162int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
 163	void *funcdata)
 164{
 165	int rc = 0, phy_id = sas_phy->id;
 166	struct pm8001_hba_info *pm8001_ha = NULL;
 167	struct sas_phy_linkrates *rates;
 
 168	DECLARE_COMPLETION_ONSTACK(completion);
 
 169	pm8001_ha = sas_phy->ha->lldd_ha;
 170	pm8001_ha->phy[phy_id].enable_completion = &completion;
 
 
 
 
 
 
 
 
 
 
 
 171	switch (func) {
 172	case PHY_FUNC_SET_LINK_RATE:
 173		rates = funcdata;
 174		if (rates->minimum_linkrate) {
 175			pm8001_ha->phy[phy_id].minimum_linkrate =
 176				rates->minimum_linkrate;
 177		}
 178		if (rates->maximum_linkrate) {
 179			pm8001_ha->phy[phy_id].maximum_linkrate =
 180				rates->maximum_linkrate;
 181		}
 182		if (pm8001_ha->phy[phy_id].phy_state == 0) {
 
 183			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
 184			wait_for_completion(&completion);
 185		}
 186		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 187					      PHY_LINK_RESET);
 188		break;
 189	case PHY_FUNC_HARD_RESET:
 190		if (pm8001_ha->phy[phy_id].phy_state == 0) {
 
 191			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
 192			wait_for_completion(&completion);
 193		}
 194		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 195					      PHY_HARD_RESET);
 196		break;
 197	case PHY_FUNC_LINK_RESET:
 198		if (pm8001_ha->phy[phy_id].phy_state == 0) {
 
 199			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
 200			wait_for_completion(&completion);
 201		}
 202		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 203					      PHY_LINK_RESET);
 204		break;
 205	case PHY_FUNC_RELEASE_SPINUP_HOLD:
 206		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 207					      PHY_LINK_RESET);
 208		break;
 209	case PHY_FUNC_DISABLE:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 210		PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
 211		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 212	default:
 
 213		rc = -EOPNOTSUPP;
 214	}
 215	msleep(300);
 216	return rc;
 217}
 218
 219int pm8001_slave_alloc(struct scsi_device *scsi_dev)
 220{
 221	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
 222	if (dev_is_sata(dev)) {
 223		/* We don't need to rescan targets
 224		* if REPORT_LUNS request is failed
 225		*/
 226		if (scsi_dev->lun > 0)
 227			return -ENXIO;
 228		scsi_dev->tagged_supported = 1;
 229	}
 230	return sas_slave_alloc(scsi_dev);
 231}
 232
 233/**
 234  * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
 235  * command to HBA.
 236  * @shost: the scsi host data.
 237  */
 238void pm8001_scan_start(struct Scsi_Host *shost)
 239{
 240	int i;
 241	struct pm8001_hba_info *pm8001_ha;
 242	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
 
 243	pm8001_ha = sha->lldd_ha;
 244	PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
 245	for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
 
 
 
 246		PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
 
 
 
 247}
 248
 249int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
 250{
 
 
 251	/* give the phy enabling interrupt event time to come in (1s
 252	* is empirically about all it takes) */
 253	if (time < HZ)
 254		return 0;
 255	/* Wait for discovery to finish */
 256	scsi_flush_work(shost);
 257	return 1;
 258}
 259
 260/**
 261  * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
 262  * @pm8001_ha: our hba card information
 263  * @ccb: the ccb which attached to smp task
 264  */
 265static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
 266	struct pm8001_ccb_info *ccb)
 267{
 268	return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
 269}
 270
 271u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
 272{
 273	struct ata_queued_cmd *qc = task->uldd_task;
 274	if (qc) {
 275		if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
 276			qc->tf.command == ATA_CMD_FPDMA_READ) {
 277			*tag = qc->tag;
 278			return 1;
 279		}
 280	}
 
 281	return 0;
 282}
 283
 284/**
 285  * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
 286  * @pm8001_ha: our hba card information
 287  * @ccb: the ccb which attached to sata task
 288  */
 289static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
 290	struct pm8001_ccb_info *ccb)
 291{
 292	return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
 293}
 294
 295/**
 
 
 
 
 
 
 
 
 
 
 
 
 296  * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
 297  * @pm8001_ha: our hba card information
 298  * @ccb: the ccb which attached to TM
 299  * @tmf: the task management IU
 300  */
 301static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
 302	struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
 303{
 304	return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
 305}
 306
 307/**
 308  * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
 309  * @pm8001_ha: our hba card information
 310  * @ccb: the ccb which attached to ssp task
 311  */
 312static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
 313	struct pm8001_ccb_info *ccb)
 314{
 315	return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
 316}
 317int pm8001_slave_configure(struct scsi_device *sdev)
 318{
 319	struct domain_device *dev = sdev_to_domain_dev(sdev);
 320	int ret = sas_slave_configure(sdev);
 321	if (ret)
 322		return ret;
 323	if (dev_is_sata(dev)) {
 324	#ifdef PM8001_DISABLE_NCQ
 325		struct ata_port *ap = dev->sata_dev.ap;
 326		struct ata_device *adev = ap->link.device;
 327		adev->flags |= ATA_DFLAG_NCQ_OFF;
 328		scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, 1);
 329	#endif
 330	}
 331	return 0;
 332}
 333 /* Find the local port id that's attached to this device */
 334static int sas_find_local_port_id(struct domain_device *dev)
 335{
 336	struct domain_device *pdev = dev->parent;
 337
 338	/* Directly attached device */
 339	if (!pdev)
 340		return dev->port->id;
 341	while (pdev) {
 342		struct domain_device *pdev_p = pdev->parent;
 343		if (!pdev_p)
 344			return pdev->port->id;
 345		pdev = pdev->parent;
 346	}
 347	return 0;
 348}
 349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 350/**
 351  * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
 
 352  * @task: the task to be execute.
 353  * @num: if can_queue great than 1, the task can be queued up. for SMP task,
 354  * we always execute one one time.
 355  * @gfp_flags: gfp_flags.
 356  * @is_tmf: if it is task management task.
 357  * @tmf: the task management IU
 358  */
 359#define DEV_IS_GONE(pm8001_dev)	\
 360	((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
 361static int pm8001_task_exec(struct sas_task *task, const int num,
 362	gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
 363{
 
 
 364	struct domain_device *dev = task->dev;
 
 
 365	struct pm8001_hba_info *pm8001_ha;
 366	struct pm8001_device *pm8001_dev;
 367	struct pm8001_port *port = NULL;
 368	struct sas_task *t = task;
 369	struct pm8001_ccb_info *ccb;
 370	u32 tag = 0xdeadbeef, rc, n_elem = 0;
 371	u32 n = num;
 372	unsigned long flags = 0, flags_libsas = 0;
 373
 374	if (!dev->port) {
 375		struct task_status_struct *tsm = &t->task_status;
 376		tsm->resp = SAS_TASK_UNDELIVERED;
 377		tsm->stat = SAS_PHY_DOWN;
 378		if (dev->dev_type != SATA_DEV)
 379			t->task_done(t);
 
 
 
 
 
 
 380		return 0;
 381	}
 382	pm8001_ha = pm8001_find_ha_by_dev(task->dev);
 383	PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
 
 384	spin_lock_irqsave(&pm8001_ha->lock, flags);
 385	do {
 386		dev = t->dev;
 387		pm8001_dev = dev->lldd_dev;
 388		if (DEV_IS_GONE(pm8001_dev)) {
 389			if (pm8001_dev) {
 390				PM8001_IO_DBG(pm8001_ha,
 391					pm8001_printk("device %d not ready.\n",
 392					pm8001_dev->device_id));
 393			} else {
 394				PM8001_IO_DBG(pm8001_ha,
 395					pm8001_printk("device %016llx not "
 396					"ready.\n", SAS_ADDR(dev->sas_addr)));
 397			}
 398			rc = SAS_PHY_DOWN;
 399			goto out_done;
 400		}
 401		port = &pm8001_ha->port[sas_find_local_port_id(dev)];
 402		if (!port->port_attached) {
 403			if (sas_protocol_ata(t->task_proto)) {
 404				struct task_status_struct *ts = &t->task_status;
 405				ts->resp = SAS_TASK_UNDELIVERED;
 406				ts->stat = SAS_PHY_DOWN;
 407
 408				spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 409				spin_unlock_irqrestore(dev->sata_dev.ap->lock,
 410						flags_libsas);
 411				t->task_done(t);
 412				spin_lock_irqsave(dev->sata_dev.ap->lock,
 413					flags_libsas);
 414				spin_lock_irqsave(&pm8001_ha->lock, flags);
 415				if (n > 1)
 416					t = list_entry(t->list.next,
 417							struct sas_task, list);
 418				continue;
 419			} else {
 420				struct task_status_struct *ts = &t->task_status;
 421				ts->resp = SAS_TASK_UNDELIVERED;
 422				ts->stat = SAS_PHY_DOWN;
 423				t->task_done(t);
 424				if (n > 1)
 425					t = list_entry(t->list.next,
 426							struct sas_task, list);
 427				continue;
 428			}
 429		}
 430		rc = pm8001_tag_alloc(pm8001_ha, &tag);
 431		if (rc)
 432			goto err_out;
 433		ccb = &pm8001_ha->ccb_info[tag];
 434
 435		if (!sas_protocol_ata(t->task_proto)) {
 436			if (t->num_scatter) {
 437				n_elem = dma_map_sg(pm8001_ha->dev,
 438					t->scatter,
 439					t->num_scatter,
 440					t->data_dir);
 441				if (!n_elem) {
 442					rc = -ENOMEM;
 443					goto err_out_tag;
 444				}
 445			}
 446		} else {
 447			n_elem = t->num_scatter;
 448		}
 
 
 
 449
 450		t->lldd_task = ccb;
 451		ccb->n_elem = n_elem;
 452		ccb->ccb_tag = tag;
 453		ccb->task = t;
 454		switch (t->task_proto) {
 455		case SAS_PROTOCOL_SMP:
 456			rc = pm8001_task_prep_smp(pm8001_ha, ccb);
 457			break;
 458		case SAS_PROTOCOL_SSP:
 459			if (is_tmf)
 460				rc = pm8001_task_prep_ssp_tm(pm8001_ha,
 461					ccb, tmf);
 462			else
 463				rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
 464			break;
 465		case SAS_PROTOCOL_SATA:
 466		case SAS_PROTOCOL_STP:
 467		case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
 468			rc = pm8001_task_prep_ata(pm8001_ha, ccb);
 469			break;
 470		default:
 471			dev_printk(KERN_ERR, pm8001_ha->dev,
 472				"unknown sas_task proto: 0x%x\n",
 473				t->task_proto);
 474			rc = -EINVAL;
 475			break;
 476		}
 
 
 
 477
 478		if (rc) {
 479			PM8001_IO_DBG(pm8001_ha,
 480				pm8001_printk("rc is %x\n", rc));
 481			goto err_out_tag;
 482		}
 483		/* TODO: select normal or high priority */
 484		spin_lock(&t->task_state_lock);
 485		t->task_state_flags |= SAS_TASK_AT_INITIATOR;
 486		spin_unlock(&t->task_state_lock);
 487		pm8001_dev->running_req++;
 488		if (n > 1)
 489			t = list_entry(t->list.next, struct sas_task, list);
 490	} while (--n);
 491	rc = 0;
 492	goto out_done;
 493
 494err_out_tag:
 495	pm8001_tag_free(pm8001_ha, tag);
 496err_out:
 497	dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
 498	if (!sas_protocol_ata(t->task_proto))
 499		if (n_elem)
 500			dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
 501				t->data_dir);
 502out_done:
 503	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 504	return rc;
 505}
 506
 507/**
 508  * pm8001_queue_command - register for upper layer used, all IO commands sent
 509  * to HBA are from this interface.
 510  * @task: the task to be execute.
 511  * @num: if can_queue great than 1, the task can be queued up. for SMP task,
 512  * we always execute one one time
 513  * @gfp_flags: gfp_flags
 514  */
 515int pm8001_queue_command(struct sas_task *task, const int num,
 516		gfp_t gfp_flags)
 517{
 518	return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
 519}
 520
 521void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
 522{
 523	pm8001_tag_clear(pm8001_ha, ccb_idx);
 524}
 525
 526/**
 527  * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
 528  * @pm8001_ha: our hba card information
 529  * @ccb: the ccb which attached to ssp task
 530  * @task: the task to be free.
 531  * @ccb_idx: ccb index.
 532  */
 533void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
 534	struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
 535{
 536	if (!ccb->task)
 
 
 
 
 537		return;
 538	if (!sas_protocol_ata(task->task_proto))
 539		if (ccb->n_elem)
 540			dma_unmap_sg(pm8001_ha->dev, task->scatter,
 541				task->num_scatter, task->data_dir);
 542
 543	switch (task->task_proto) {
 544	case SAS_PROTOCOL_SMP:
 545		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
 546			PCI_DMA_FROMDEVICE);
 547		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
 548			PCI_DMA_TODEVICE);
 549		break;
 550
 551	case SAS_PROTOCOL_SATA:
 552	case SAS_PROTOCOL_STP:
 553	case SAS_PROTOCOL_SSP:
 554	default:
 555		/* do nothing */
 556		break;
 557	}
 
 
 
 
 
 
 
 
 
 
 
 
 558	task->lldd_task = NULL;
 559	ccb->task = NULL;
 560	ccb->ccb_tag = 0xFFFFFFFF;
 561	pm8001_ccb_free(pm8001_ha, ccb_idx);
 562}
 563
 564 /**
 565  * pm8001_alloc_dev - find a empty pm8001_device
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 566  * @pm8001_ha: our hba card information
 
 567  */
 568struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
 
 569{
 570	u32 dev;
 571	for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
 572		if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
 573			pm8001_ha->devices[dev].id = dev;
 574			return &pm8001_ha->devices[dev];
 575		}
 576	}
 577	if (dev == PM8001_MAX_DEVICES) {
 578		PM8001_FAIL_DBG(pm8001_ha,
 579			pm8001_printk("max support %d devices, ignore ..\n",
 580			PM8001_MAX_DEVICES));
 581	}
 582	return NULL;
 583}
 584
 585static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
 586{
 587	u32 id = pm8001_dev->id;
 588	memset(pm8001_dev, 0, sizeof(*pm8001_dev));
 589	pm8001_dev->id = id;
 590	pm8001_dev->dev_type = NO_DEVICE;
 591	pm8001_dev->device_id = PM8001_MAX_DEVICES;
 592	pm8001_dev->sas_device = NULL;
 593}
 594
 595/**
 596  * pm8001_dev_found_notify - libsas notify a device is found.
 597  * @dev: the device structure which sas layer used.
 598  *
 599  * when libsas find a sas domain device, it should tell the LLDD that
 600  * device is found, and then LLDD register this device to HBA firmware
 601  * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
 602  * device ID(according to device's sas address) and returned it to LLDD. From
 603  * now on, we communicate with HBA FW with the device ID which HBA assigned
 604  * rather than sas address. it is the necessary step for our HBA but it is
 605  * the optional for other HBA driver.
 606  */
 607static int pm8001_dev_found_notify(struct domain_device *dev)
 608{
 609	unsigned long flags = 0;
 610	int res = 0;
 611	struct pm8001_hba_info *pm8001_ha = NULL;
 612	struct domain_device *parent_dev = dev->parent;
 613	struct pm8001_device *pm8001_device;
 614	DECLARE_COMPLETION_ONSTACK(completion);
 615	u32 flag = 0;
 616	pm8001_ha = pm8001_find_ha_by_dev(dev);
 617	spin_lock_irqsave(&pm8001_ha->lock, flags);
 618
 619	pm8001_device = pm8001_alloc_dev(pm8001_ha);
 620	if (!pm8001_device) {
 621		res = -1;
 622		goto found_out;
 623	}
 624	pm8001_device->sas_device = dev;
 625	dev->lldd_dev = pm8001_device;
 626	pm8001_device->dev_type = dev->dev_type;
 627	pm8001_device->dcompletion = &completion;
 628	if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
 629		int phy_id;
 630		struct ex_phy *phy;
 631		for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
 632		phy_id++) {
 633			phy = &parent_dev->ex_dev.ex_phy[phy_id];
 634			if (SAS_ADDR(phy->attached_sas_addr)
 635				== SAS_ADDR(dev->sas_addr)) {
 636				pm8001_device->attached_phy = phy_id;
 637				break;
 638			}
 639		}
 640		if (phy_id == parent_dev->ex_dev.num_phys) {
 641			PM8001_FAIL_DBG(pm8001_ha,
 642			pm8001_printk("Error: no attached dev:%016llx"
 643			" at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
 644				SAS_ADDR(parent_dev->sas_addr)));
 645			res = -1;
 646		}
 647	} else {
 648		if (dev->dev_type == SATA_DEV) {
 649			pm8001_device->attached_phy =
 650				dev->rphy->identify.phy_identifier;
 651				flag = 1; /* directly sata*/
 652		}
 653	} /*register this device to HBA*/
 654	PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device \n"));
 655	PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
 656	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 657	wait_for_completion(&completion);
 658	if (dev->dev_type == SAS_END_DEV)
 659		msleep(50);
 660	pm8001_ha->flags |= PM8001F_RUN_TIME ;
 661	return 0;
 662found_out:
 663	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 664	return res;
 665}
 666
 667int pm8001_dev_found(struct domain_device *dev)
 668{
 669	return pm8001_dev_found_notify(dev);
 670}
 671
 672/**
 673  * pm8001_alloc_task - allocate a task structure for TMF
 674  */
 675static struct sas_task *pm8001_alloc_task(void)
 676{
 677	struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
 678	if (task) {
 679		INIT_LIST_HEAD(&task->list);
 680		spin_lock_init(&task->task_state_lock);
 681		task->task_state_flags = SAS_TASK_STATE_PENDING;
 682		init_timer(&task->timer);
 683		init_completion(&task->completion);
 684	}
 685	return task;
 686}
 687
 688static void pm8001_free_task(struct sas_task *task)
 689{
 690	if (task) {
 691		BUG_ON(!list_empty(&task->list));
 692		kfree(task);
 693	}
 694}
 695
 696static void pm8001_task_done(struct sas_task *task)
 697{
 698	if (!del_timer(&task->timer))
 699		return;
 700	complete(&task->completion);
 701}
 702
 703static void pm8001_tmf_timedout(unsigned long data)
 704{
 705	struct sas_task *task = (struct sas_task *)data;
 706
 707	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
 708	complete(&task->completion);
 709}
 710
 711#define PM8001_TASK_TIMEOUT 20
 712/**
 713  * pm8001_exec_internal_tmf_task - execute some task management commands.
 714  * @dev: the wanted device.
 715  * @tmf: which task management wanted to be take.
 716  * @para_len: para_len.
 717  * @parameter: ssp task parameter.
 718  *
 719  * when errors or exception happened, we may want to do something, for example
 720  * abort the issued task which result in this execption, it is done by calling
 721  * this function, note it is also with the task execute interface.
 722  */
 723static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
 724	void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
 725{
 726	int res, retry;
 727	struct sas_task *task = NULL;
 728	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
 729
 730	for (retry = 0; retry < 3; retry++) {
 731		task = pm8001_alloc_task();
 732		if (!task)
 733			return -ENOMEM;
 734
 735		task->dev = dev;
 736		task->task_proto = dev->tproto;
 737		memcpy(&task->ssp_task, parameter, para_len);
 738		task->task_done = pm8001_task_done;
 739		task->timer.data = (unsigned long)task;
 740		task->timer.function = pm8001_tmf_timedout;
 741		task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
 742		add_timer(&task->timer);
 743
 744		res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
 745
 746		if (res) {
 747			del_timer(&task->timer);
 748			PM8001_FAIL_DBG(pm8001_ha,
 749				pm8001_printk("Executing internal task "
 750				"failed\n"));
 751			goto ex_err;
 752		}
 753		wait_for_completion(&task->completion);
 754		res = -TMF_RESP_FUNC_FAILED;
 755		/* Even TMF timed out, return direct. */
 756		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
 757			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
 758				PM8001_FAIL_DBG(pm8001_ha,
 759					pm8001_printk("TMF task[%x]timeout.\n",
 760					tmf->tmf));
 761				goto ex_err;
 762			}
 763		}
 764
 765		if (task->task_status.resp == SAS_TASK_COMPLETE &&
 766			task->task_status.stat == SAM_STAT_GOOD) {
 767			res = TMF_RESP_FUNC_COMPLETE;
 768			break;
 769		}
 770
 771		if (task->task_status.resp == SAS_TASK_COMPLETE &&
 772		task->task_status.stat == SAS_DATA_UNDERRUN) {
 773			/* no error, but return the number of bytes of
 774			* underrun */
 775			res = task->task_status.residual;
 776			break;
 777		}
 778
 779		if (task->task_status.resp == SAS_TASK_COMPLETE &&
 780			task->task_status.stat == SAS_DATA_OVERRUN) {
 781			PM8001_FAIL_DBG(pm8001_ha,
 782				pm8001_printk("Blocked task error.\n"));
 783			res = -EMSGSIZE;
 784			break;
 785		} else {
 786			PM8001_EH_DBG(pm8001_ha,
 787				pm8001_printk(" Task to dev %016llx response:"
 788				"0x%x status 0x%x\n",
 789				SAS_ADDR(dev->sas_addr),
 790				task->task_status.resp,
 791				task->task_status.stat));
 792			pm8001_free_task(task);
 793			task = NULL;
 794		}
 795	}
 796ex_err:
 797	BUG_ON(retry == 3 && task != NULL);
 798	if (task != NULL)
 799		pm8001_free_task(task);
 800	return res;
 801}
 802
 803static int
 804pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
 805	struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
 806	u32 task_tag)
 807{
 808	int res, retry;
 809	u32 ccb_tag;
 810	struct pm8001_ccb_info *ccb;
 811	struct sas_task *task = NULL;
 812
 813	for (retry = 0; retry < 3; retry++) {
 814		task = pm8001_alloc_task();
 815		if (!task)
 816			return -ENOMEM;
 817
 818		task->dev = dev;
 819		task->task_proto = dev->tproto;
 820		task->task_done = pm8001_task_done;
 821		task->timer.data = (unsigned long)task;
 822		task->timer.function = pm8001_tmf_timedout;
 823		task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
 824		add_timer(&task->timer);
 825
 826		res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
 827		if (res)
 828			return res;
 829		ccb = &pm8001_ha->ccb_info[ccb_tag];
 830		ccb->device = pm8001_dev;
 831		ccb->ccb_tag = ccb_tag;
 832		ccb->task = task;
 833
 834		res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
 835			pm8001_dev, flag, task_tag, ccb_tag);
 836
 837		if (res) {
 838			del_timer(&task->timer);
 839			PM8001_FAIL_DBG(pm8001_ha,
 840				pm8001_printk("Executing internal task "
 841				"failed\n"));
 842			goto ex_err;
 843		}
 844		wait_for_completion(&task->completion);
 845		res = TMF_RESP_FUNC_FAILED;
 846		/* Even TMF timed out, return direct. */
 847		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
 848			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
 849				PM8001_FAIL_DBG(pm8001_ha,
 850					pm8001_printk("TMF task timeout.\n"));
 851				goto ex_err;
 852			}
 853		}
 854
 855		if (task->task_status.resp == SAS_TASK_COMPLETE &&
 856			task->task_status.stat == SAM_STAT_GOOD) {
 857			res = TMF_RESP_FUNC_COMPLETE;
 858			break;
 859
 860		} else {
 861			PM8001_EH_DBG(pm8001_ha,
 862				pm8001_printk(" Task to dev %016llx response: "
 863					"0x%x status 0x%x\n",
 864				SAS_ADDR(dev->sas_addr),
 865				task->task_status.resp,
 866				task->task_status.stat));
 867			pm8001_free_task(task);
 868			task = NULL;
 869		}
 870	}
 871ex_err:
 872	BUG_ON(retry == 3 && task != NULL);
 873	if (task != NULL)
 874		pm8001_free_task(task);
 875	return res;
 876}
 877
 878/**
 879  * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
 880  * @dev: the device structure which sas layer used.
 881  */
 882static void pm8001_dev_gone_notify(struct domain_device *dev)
 883{
 884	unsigned long flags = 0;
 885	u32 tag;
 886	struct pm8001_hba_info *pm8001_ha;
 887	struct pm8001_device *pm8001_dev = dev->lldd_dev;
 888
 889	pm8001_ha = pm8001_find_ha_by_dev(dev);
 890	spin_lock_irqsave(&pm8001_ha->lock, flags);
 891	pm8001_tag_alloc(pm8001_ha, &tag);
 892	if (pm8001_dev) {
 893		u32 device_id = pm8001_dev->device_id;
 894
 895		PM8001_DISC_DBG(pm8001_ha,
 896			pm8001_printk("found dev[%d:%x] is gone.\n",
 897			pm8001_dev->device_id, pm8001_dev->dev_type));
 898		if (pm8001_dev->running_req) {
 899			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 900			pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
 901				dev, 1, 0);
 
 902			spin_lock_irqsave(&pm8001_ha->lock, flags);
 903		}
 904		PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
 905		pm8001_free_dev(pm8001_dev);
 906	} else {
 907		PM8001_DISC_DBG(pm8001_ha,
 908			pm8001_printk("Found dev has gone.\n"));
 909	}
 910	dev->lldd_dev = NULL;
 911	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 912}
 913
 914void pm8001_dev_gone(struct domain_device *dev)
 915{
 916	pm8001_dev_gone_notify(dev);
 917}
 918
 919static int pm8001_issue_ssp_tmf(struct domain_device *dev,
 920	u8 *lun, struct pm8001_tmf_task *tmf)
 
 
 
 921{
 922	struct sas_ssp_task ssp_task;
 923	if (!(dev->tproto & SAS_PROTOCOL_SSP))
 924		return TMF_RESP_FUNC_ESUPP;
 925
 926	strncpy((u8 *)&ssp_task.LUN, lun, 8);
 927	return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
 928		tmf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 929}
 930
 931/**
 932  * Standard mandates link reset for ATA  (type 0) and hard reset for
 933  * SSP (type 1) , only for RECOVERY
 934  */
 
 
 
 935int pm8001_I_T_nexus_reset(struct domain_device *dev)
 936{
 937	int rc = TMF_RESP_FUNC_FAILED;
 938	struct pm8001_device *pm8001_dev;
 939	struct pm8001_hba_info *pm8001_ha;
 940	struct sas_phy *phy;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 941	if (!dev || !dev->lldd_dev)
 942		return -1;
 943
 944	pm8001_dev = dev->lldd_dev;
 945	pm8001_ha = pm8001_find_ha_by_dev(dev);
 946	phy = sas_find_local_phy(dev);
 
 
 
 947
 948	if (dev_is_sata(dev)) {
 949		DECLARE_COMPLETION_ONSTACK(completion_setstate);
 950		if (scsi_is_sas_phy_local(phy))
 951			return 0;
 
 
 
 
 
 
 
 
 
 
 
 952		rc = sas_phy_reset(phy, 1);
 953		msleep(2000);
 954		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
 955			dev, 1, 0);
 956		pm8001_dev->setds_completion = &completion_setstate;
 957		rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
 958			pm8001_dev, 0x01);
 959		wait_for_completion(&completion_setstate);
 960	} else{
 961	rc = sas_phy_reset(phy, 1);
 962	msleep(2000);
 
 
 
 
 
 
 
 
 
 963	}
 964	PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
 965		pm8001_dev->device_id, rc));
 
 
 
 966	return rc;
 967}
 968
 969/* mandatory SAM-3, the task reset the specified LUN*/
 970int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
 971{
 972	int rc = TMF_RESP_FUNC_FAILED;
 973	struct pm8001_tmf_task tmf_task;
 974	struct pm8001_device *pm8001_dev = dev->lldd_dev;
 975	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 976	if (dev_is_sata(dev)) {
 977		struct sas_phy *phy = sas_find_local_phy(dev);
 978		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
 979			dev, 1, 0);
 980		rc = sas_phy_reset(phy, 1);
 
 
 981		rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
 982			pm8001_dev, 0x01);
 983		msleep(2000);
 984	} else {
 985		tmf_task.tmf = TMF_LU_RESET;
 986		rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
 987	}
 988	/* If failed, fall-through I_T_Nexus reset */
 989	PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
 990		pm8001_dev->device_id, rc));
 991	return rc;
 992}
 993
 994/* optional SAM-3 */
 995int pm8001_query_task(struct sas_task *task)
 996{
 997	u32 tag = 0xdeadbeef;
 998	int i = 0;
 999	struct scsi_lun lun;
1000	struct pm8001_tmf_task tmf_task;
1001	int rc = TMF_RESP_FUNC_FAILED;
1002	if (unlikely(!task || !task->lldd_task || !task->dev))
1003		return rc;
1004
1005	if (task->task_proto & SAS_PROTOCOL_SSP) {
1006		struct scsi_cmnd *cmnd = task->uldd_task;
1007		struct domain_device *dev = task->dev;
1008		struct pm8001_hba_info *pm8001_ha =
1009			pm8001_find_ha_by_dev(dev);
1010
1011		int_to_scsilun(cmnd->device->lun, &lun);
1012		rc = pm8001_find_tag(task, &tag);
1013		if (rc == 0) {
1014			rc = TMF_RESP_FUNC_FAILED;
1015			return rc;
1016		}
1017		PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
1018		for (i = 0; i < 16; i++)
1019			printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
1020		printk(KERN_INFO "]\n");
1021		tmf_task.tmf = 	TMF_QUERY_TASK;
1022		tmf_task.tag_of_task_to_be_managed = tag;
1023
1024		rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1025		switch (rc) {
1026		/* The task is still in Lun, release it then */
1027		case TMF_RESP_FUNC_SUCC:
1028			PM8001_EH_DBG(pm8001_ha,
1029				pm8001_printk("The task is still in Lun \n"));
 
1030		/* The task is not in Lun or failed, reset the phy */
1031		case TMF_RESP_FUNC_FAILED:
1032		case TMF_RESP_FUNC_COMPLETE:
1033			PM8001_EH_DBG(pm8001_ha,
1034			pm8001_printk("The task is not in Lun or failed,"
1035			" reset the phy \n"));
1036			break;
1037		}
1038	}
1039	pm8001_printk(":rc= %d\n", rc);
1040	return rc;
1041}
1042
1043/*  mandatory SAM-3, still need free task/ccb info, abord the specified task */
1044int pm8001_abort_task(struct sas_task *task)
1045{
 
1046	unsigned long flags;
1047	u32 tag = 0xdeadbeef;
1048	u32 device_id;
1049	struct domain_device *dev ;
1050	struct pm8001_hba_info *pm8001_ha = NULL;
1051	struct pm8001_ccb_info *ccb;
1052	struct scsi_lun lun;
1053	struct pm8001_device *pm8001_dev;
1054	struct pm8001_tmf_task tmf_task;
1055	int rc = TMF_RESP_FUNC_FAILED;
1056	if (unlikely(!task || !task->lldd_task || !task->dev))
1057		return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1058	spin_lock_irqsave(&task->task_state_lock, flags);
1059	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1060		spin_unlock_irqrestore(&task->task_state_lock, flags);
1061		rc = TMF_RESP_FUNC_COMPLETE;
1062		goto out;
 
 
 
 
1063	}
1064	spin_unlock_irqrestore(&task->task_state_lock, flags);
1065	if (task->task_proto & SAS_PROTOCOL_SSP) {
1066		struct scsi_cmnd *cmnd = task->uldd_task;
1067		dev = task->dev;
1068		ccb = task->lldd_task;
1069		pm8001_dev = dev->lldd_dev;
1070		pm8001_ha = pm8001_find_ha_by_dev(dev);
1071		int_to_scsilun(cmnd->device->lun, &lun);
1072		rc = pm8001_find_tag(task, &tag);
1073		if (rc == 0) {
1074			printk(KERN_INFO "No such tag in %s\n", __func__);
1075			rc = TMF_RESP_FUNC_FAILED;
1076			return rc;
1077		}
1078		device_id = pm8001_dev->device_id;
1079		PM8001_EH_DBG(pm8001_ha,
1080			pm8001_printk("abort io to deviceid= %d\n", device_id));
1081		tmf_task.tmf = TMF_ABORT_TASK;
1082		tmf_task.tag_of_task_to_be_managed = tag;
1083		rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1084		pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1085			pm8001_dev->sas_device, 0, tag);
1086	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
1087		task->task_proto & SAS_PROTOCOL_STP) {
1088		dev = task->dev;
1089		pm8001_dev = dev->lldd_dev;
1090		pm8001_ha = pm8001_find_ha_by_dev(dev);
1091		rc = pm8001_find_tag(task, &tag);
1092		if (rc == 0) {
1093			printk(KERN_INFO "No such tag in %s\n", __func__);
1094			rc = TMF_RESP_FUNC_FAILED;
1095			return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1096		}
1097		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1098			pm8001_dev->sas_device, 0, tag);
1099	} else if (task->task_proto & SAS_PROTOCOL_SMP) {
1100		/* SMP */
1101		dev = task->dev;
1102		pm8001_dev = dev->lldd_dev;
1103		pm8001_ha = pm8001_find_ha_by_dev(dev);
1104		rc = pm8001_find_tag(task, &tag);
1105		if (rc == 0) {
1106			printk(KERN_INFO "No such tag in %s\n", __func__);
1107			rc = TMF_RESP_FUNC_FAILED;
1108			return rc;
1109		}
1110		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1111			pm8001_dev->sas_device, 0, tag);
1112
1113	}
1114out:
 
 
 
 
1115	if (rc != TMF_RESP_FUNC_COMPLETE)
1116		pm8001_printk("rc= %d\n", rc);
1117	return rc;
1118}
1119
1120int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1121{
1122	int rc = TMF_RESP_FUNC_FAILED;
1123	struct pm8001_tmf_task tmf_task;
1124
1125	tmf_task.tmf = TMF_ABORT_TASK_SET;
1126	rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1127	return rc;
1128}
1129
1130int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1131{
1132	int rc = TMF_RESP_FUNC_FAILED;
1133	struct pm8001_tmf_task tmf_task;
1134
1135	tmf_task.tmf = TMF_CLEAR_ACA;
1136	rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1137
1138	return rc;
 
 
 
 
1139}
1140
1141int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1142{
1143	int rc = TMF_RESP_FUNC_FAILED;
1144	struct pm8001_tmf_task tmf_task;
1145	struct pm8001_device *pm8001_dev = dev->lldd_dev;
1146	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
 
 
1147
1148	PM8001_EH_DBG(pm8001_ha,
1149		pm8001_printk("I_T_L_Q clear task set[%x]\n",
1150		pm8001_dev->device_id));
1151	tmf_task.tmf = TMF_CLEAR_TASK_SET;
1152	rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1153	return rc;
1154}
1155
v6.13.7
   1/*
   2 * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
   3 *
   4 * Copyright (c) 2008-2009 USI Co., Ltd.
   5 * All rights reserved.
   6 *
   7 * Redistribution and use in source and binary forms, with or without
   8 * modification, are permitted provided that the following conditions
   9 * are met:
  10 * 1. Redistributions of source code must retain the above copyright
  11 *    notice, this list of conditions, and the following disclaimer,
  12 *    without modification.
  13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  14 *    substantially similar to the "NO WARRANTY" disclaimer below
  15 *    ("Disclaimer") and any redistribution must be conditioned upon
  16 *    including a substantially similar Disclaimer requirement for further
  17 *    binary redistribution.
  18 * 3. Neither the names of the above-listed copyright holders nor the names
  19 *    of any contributors may be used to endorse or promote products derived
  20 *    from this software without specific prior written permission.
  21 *
  22 * Alternatively, this software may be distributed under the terms of the
  23 * GNU General Public License ("GPL") version 2 as published by the Free
  24 * Software Foundation.
  25 *
  26 * NO WARRANTY
  27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37 * POSSIBILITY OF SUCH DAMAGES.
  38 *
  39 */
  40
  41#include <linux/slab.h>
  42#include "pm8001_sas.h"
  43#include "pm80xx_tracepoints.h"
  44
  45/**
  46 * pm8001_find_tag - from sas task to find out  tag that belongs to this task
  47 * @task: the task sent to the LLDD
  48 * @tag: the found tag associated with the task
  49 */
  50static int pm8001_find_tag(struct sas_task *task, u32 *tag)
  51{
  52	if (task->lldd_task) {
  53		struct pm8001_ccb_info *ccb;
  54		ccb = task->lldd_task;
  55		*tag = ccb->ccb_tag;
  56		return 1;
  57	}
  58	return 0;
  59}
  60
  61/**
  62  * pm8001_tag_free - free the no more needed tag
  63  * @pm8001_ha: our hba struct
  64  * @tag: the found tag associated with the task
  65  */
  66void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
  67{
  68	void *bitmap = pm8001_ha->rsvd_tags;
  69	unsigned long flags;
 
  70
  71	if (tag >= PM8001_RESERVE_SLOT)
  72		return;
 
 
  73
  74	spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
  75	__clear_bit(tag, bitmap);
  76	spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
 
  77}
  78
  79/**
  80  * pm8001_tag_alloc - allocate a empty tag for task used.
  81  * @pm8001_ha: our hba struct
  82  * @tag_out: the found empty tag .
  83  */
  84int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
  85{
  86	void *bitmap = pm8001_ha->rsvd_tags;
  87	unsigned long flags;
  88	unsigned int tag;
  89
  90	spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
  91	tag = find_first_zero_bit(bitmap, PM8001_RESERVE_SLOT);
  92	if (tag >= PM8001_RESERVE_SLOT) {
  93		spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
  94		return -SAS_QUEUE_FULL;
  95	}
  96	__set_bit(tag, bitmap);
  97	spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
  98
  99	/* reserved tags are in the lower region of the tagset */
 100	*tag_out = tag;
 101	return 0;
 102}
 103
 104/**
 105 * pm8001_mem_alloc - allocate memory for pm8001.
 106 * @pdev: pci device.
 107 * @virt_addr: the allocated virtual address
 108 * @pphys_addr: DMA address for this device
 109 * @pphys_addr_hi: the physical address high byte address.
 110 * @pphys_addr_lo: the physical address low byte address.
 111 * @mem_size: memory size.
 112 * @align: requested byte alignment
 113 */
 
 
 
 
 
 114int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
 115	dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
 116	u32 *pphys_addr_lo, u32 mem_size, u32 align)
 117{
 118	caddr_t mem_virt_alloc;
 119	dma_addr_t mem_dma_handle;
 120	u64 phys_align;
 121	u64 align_offset = 0;
 122	if (align)
 123		align_offset = (dma_addr_t)align - 1;
 124	mem_virt_alloc = dma_alloc_coherent(&pdev->dev, mem_size + align,
 125					    &mem_dma_handle, GFP_KERNEL);
 126	if (!mem_virt_alloc)
 127		return -ENOMEM;
 
 
 
 128	*pphys_addr = mem_dma_handle;
 129	phys_align = (*pphys_addr + align_offset) & ~align_offset;
 130	*virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
 131	*pphys_addr_hi = upper_32_bits(phys_align);
 132	*pphys_addr_lo = lower_32_bits(phys_align);
 133	return 0;
 134}
 135
 136/**
 137  * pm8001_find_ha_by_dev - from domain device which come from sas layer to
 138  * find out our hba struct.
 139  * @dev: the domain device which from sas layer.
 140  */
 141static
 142struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
 143{
 144	struct sas_ha_struct *sha = dev->port->ha;
 145	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
 146	return pm8001_ha;
 147}
 148
 149/**
 150  * pm8001_phy_control - this function should be registered to
 151  * sas_domain_function_template to provide libsas used, note: this is just
 152  * control the HBA phy rather than other expander phy if you want control
 153  * other phy, you should use SMP command.
 154  * @sas_phy: which phy in HBA phys.
 155  * @func: the operation.
 156  * @funcdata: always NULL.
 157  */
 158int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
 159	void *funcdata)
 160{
 161	int rc = 0, phy_id = sas_phy->id;
 162	struct pm8001_hba_info *pm8001_ha = NULL;
 163	struct sas_phy_linkrates *rates;
 164	struct pm8001_phy *phy;
 165	DECLARE_COMPLETION_ONSTACK(completion);
 166	unsigned long flags;
 167	pm8001_ha = sas_phy->ha->lldd_ha;
 168	phy = &pm8001_ha->phy[phy_id];
 169
 170	if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
 171		/*
 172		 * If the controller is in fatal error state,
 173		 * we will not get a response from the controller
 174		 */
 175		pm8001_dbg(pm8001_ha, FAIL,
 176			   "Phy control failed due to fatal errors\n");
 177		return -EFAULT;
 178	}
 179
 180	switch (func) {
 181	case PHY_FUNC_SET_LINK_RATE:
 182		rates = funcdata;
 183		if (rates->minimum_linkrate) {
 184			pm8001_ha->phy[phy_id].minimum_linkrate =
 185				rates->minimum_linkrate;
 186		}
 187		if (rates->maximum_linkrate) {
 188			pm8001_ha->phy[phy_id].maximum_linkrate =
 189				rates->maximum_linkrate;
 190		}
 191		if (pm8001_ha->phy[phy_id].phy_state ==  PHY_LINK_DISABLE) {
 192			pm8001_ha->phy[phy_id].enable_completion = &completion;
 193			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
 194			wait_for_completion(&completion);
 195		}
 196		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 197					      PHY_LINK_RESET);
 198		break;
 199	case PHY_FUNC_HARD_RESET:
 200		if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
 201			pm8001_ha->phy[phy_id].enable_completion = &completion;
 202			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
 203			wait_for_completion(&completion);
 204		}
 205		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 206					      PHY_HARD_RESET);
 207		break;
 208	case PHY_FUNC_LINK_RESET:
 209		if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
 210			pm8001_ha->phy[phy_id].enable_completion = &completion;
 211			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
 212			wait_for_completion(&completion);
 213		}
 214		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 215					      PHY_LINK_RESET);
 216		break;
 217	case PHY_FUNC_RELEASE_SPINUP_HOLD:
 218		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
 219					      PHY_LINK_RESET);
 220		break;
 221	case PHY_FUNC_DISABLE:
 222		if (pm8001_ha->chip_id != chip_8001) {
 223			if (pm8001_ha->phy[phy_id].phy_state ==
 224				PHY_STATE_LINK_UP_SPCV) {
 225				sas_phy_disconnected(&phy->sas_phy);
 226				sas_notify_phy_event(&phy->sas_phy,
 227					PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
 228				phy->phy_attached = 0;
 229			}
 230		} else {
 231			if (pm8001_ha->phy[phy_id].phy_state ==
 232				PHY_STATE_LINK_UP_SPC) {
 233				sas_phy_disconnected(&phy->sas_phy);
 234				sas_notify_phy_event(&phy->sas_phy,
 235					PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
 236				phy->phy_attached = 0;
 237			}
 238		}
 239		PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
 240		break;
 241	case PHY_FUNC_GET_EVENTS:
 242		spin_lock_irqsave(&pm8001_ha->lock, flags);
 243		if (pm8001_ha->chip_id == chip_8001) {
 244			if (-1 == pm8001_bar4_shift(pm8001_ha,
 245					(phy_id < 4) ? 0x30000 : 0x40000)) {
 246				spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 247				return -EINVAL;
 248			}
 249		}
 250		{
 251			struct sas_phy *phy = sas_phy->phy;
 252			u32 __iomem *qp = pm8001_ha->io_mem[2].memvirtaddr
 253				+ 0x1034 + (0x4000 * (phy_id & 3));
 254
 255			phy->invalid_dword_count = readl(qp);
 256			phy->running_disparity_error_count = readl(&qp[1]);
 257			phy->loss_of_dword_sync_count = readl(&qp[3]);
 258			phy->phy_reset_problem_count = readl(&qp[4]);
 259		}
 260		if (pm8001_ha->chip_id == chip_8001)
 261			pm8001_bar4_shift(pm8001_ha, 0);
 262		spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 263		return 0;
 264	default:
 265		pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
 266		rc = -EOPNOTSUPP;
 267	}
 268	msleep(300);
 269	return rc;
 270}
 271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 272/**
 273  * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
 274  * command to HBA.
 275  * @shost: the scsi host data.
 276  */
 277void pm8001_scan_start(struct Scsi_Host *shost)
 278{
 279	int i;
 280	struct pm8001_hba_info *pm8001_ha;
 281	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
 282	DECLARE_COMPLETION_ONSTACK(completion);
 283	pm8001_ha = sha->lldd_ha;
 284	/* SAS_RE_INITIALIZATION not available in SPCv/ve */
 285	if (pm8001_ha->chip_id == chip_8001)
 286		PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
 287	for (i = 0; i < pm8001_ha->chip->n_phy; ++i) {
 288		pm8001_ha->phy[i].enable_completion = &completion;
 289		PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
 290		wait_for_completion(&completion);
 291		msleep(300);
 292	}
 293}
 294
 295int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
 296{
 297	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
 298
 299	/* give the phy enabling interrupt event time to come in (1s
 300	* is empirically about all it takes) */
 301	if (time < HZ)
 302		return 0;
 303	/* Wait for discovery to finish */
 304	sas_drain_work(ha);
 305	return 1;
 306}
 307
 308/**
 309  * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
 310  * @pm8001_ha: our hba card information
 311  * @ccb: the ccb which attached to smp task
 312  */
 313static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
 314	struct pm8001_ccb_info *ccb)
 315{
 316	return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
 317}
 318
 319u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
 320{
 321	struct ata_queued_cmd *qc = task->uldd_task;
 322
 323	if (qc && ata_is_ncq(qc->tf.protocol)) {
 324		*tag = qc->tag;
 325		return 1;
 
 
 326	}
 327
 328	return 0;
 329}
 330
 331/**
 332  * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
 333  * @pm8001_ha: our hba card information
 334  * @ccb: the ccb which attached to sata task
 335  */
 336static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
 337	struct pm8001_ccb_info *ccb)
 338{
 339	return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
 340}
 341
 342/**
 343  * pm8001_task_prep_internal_abort - the dispatcher function, prepare data
 344  *				      for internal abort task
 345  * @pm8001_ha: our hba card information
 346  * @ccb: the ccb which attached to sata task
 347  */
 348static int pm8001_task_prep_internal_abort(struct pm8001_hba_info *pm8001_ha,
 349					   struct pm8001_ccb_info *ccb)
 350{
 351	return PM8001_CHIP_DISP->task_abort(pm8001_ha, ccb);
 352}
 353
 354/**
 355  * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
 356  * @pm8001_ha: our hba card information
 357  * @ccb: the ccb which attached to TM
 358  * @tmf: the task management IU
 359  */
 360static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
 361	struct pm8001_ccb_info *ccb, struct sas_tmf_task *tmf)
 362{
 363	return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
 364}
 365
 366/**
 367  * pm8001_task_prep_ssp - the dispatcher function, prepare ssp data for ssp task
 368  * @pm8001_ha: our hba card information
 369  * @ccb: the ccb which attached to ssp task
 370  */
 371static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
 372	struct pm8001_ccb_info *ccb)
 373{
 374	return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
 375}
 376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 377 /* Find the local port id that's attached to this device */
 378static int sas_find_local_port_id(struct domain_device *dev)
 379{
 380	struct domain_device *pdev = dev->parent;
 381
 382	/* Directly attached device */
 383	if (!pdev)
 384		return dev->port->id;
 385	while (pdev) {
 386		struct domain_device *pdev_p = pdev->parent;
 387		if (!pdev_p)
 388			return pdev->port->id;
 389		pdev = pdev->parent;
 390	}
 391	return 0;
 392}
 393
 394#define DEV_IS_GONE(pm8001_dev)	\
 395	((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
 396
 397
 398static int pm8001_deliver_command(struct pm8001_hba_info *pm8001_ha,
 399				  struct pm8001_ccb_info *ccb)
 400{
 401	struct sas_task *task = ccb->task;
 402	enum sas_protocol task_proto = task->task_proto;
 403	struct sas_tmf_task *tmf = task->tmf;
 404	int is_tmf = !!tmf;
 405
 406	switch (task_proto) {
 407	case SAS_PROTOCOL_SMP:
 408		return pm8001_task_prep_smp(pm8001_ha, ccb);
 409	case SAS_PROTOCOL_SSP:
 410		if (is_tmf)
 411			return pm8001_task_prep_ssp_tm(pm8001_ha, ccb, tmf);
 412		return pm8001_task_prep_ssp(pm8001_ha, ccb);
 413	case SAS_PROTOCOL_SATA:
 414	case SAS_PROTOCOL_STP:
 415		return pm8001_task_prep_ata(pm8001_ha, ccb);
 416	case SAS_PROTOCOL_INTERNAL_ABORT:
 417		return pm8001_task_prep_internal_abort(pm8001_ha, ccb);
 418	default:
 419		dev_err(pm8001_ha->dev, "unknown sas_task proto: 0x%x\n",
 420			task_proto);
 421	}
 422
 423	return -EINVAL;
 424}
 425
 426/**
 427  * pm8001_queue_command - register for upper layer used, all IO commands sent
 428  * to HBA are from this interface.
 429  * @task: the task to be execute.
 430  * @gfp_flags: gfp_flags
 
 
 
 
 431  */
 432int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
 
 
 
 433{
 434	struct task_status_struct *ts = &task->task_status;
 435	enum sas_protocol task_proto = task->task_proto;
 436	struct domain_device *dev = task->dev;
 437	struct pm8001_device *pm8001_dev = dev->lldd_dev;
 438	bool internal_abort = sas_is_internal_abort(task);
 439	struct pm8001_hba_info *pm8001_ha;
 
 440	struct pm8001_port *port = NULL;
 
 441	struct pm8001_ccb_info *ccb;
 442	unsigned long flags;
 443	u32 n_elem = 0;
 444	int rc = 0;
 445
 446	if (!internal_abort && !dev->port) {
 447		ts->resp = SAS_TASK_UNDELIVERED;
 448		ts->stat = SAS_PHY_DOWN;
 449		if (dev->dev_type != SAS_SATA_DEV)
 450			task->task_done(task);
 451		return 0;
 452	}
 453
 454	pm8001_ha = pm8001_find_ha_by_dev(dev);
 455	if (pm8001_ha->controller_fatal_error) {
 456		ts->resp = SAS_TASK_UNDELIVERED;
 457		task->task_done(task);
 458		return 0;
 459	}
 460
 461	pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
 462
 463	spin_lock_irqsave(&pm8001_ha->lock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 464
 465	pm8001_dev = dev->lldd_dev;
 466	port = &pm8001_ha->port[sas_find_local_port_id(dev)];
 467
 468	if (!internal_abort &&
 469	    (DEV_IS_GONE(pm8001_dev) || !port->port_attached)) {
 470		ts->resp = SAS_TASK_UNDELIVERED;
 471		ts->stat = SAS_PHY_DOWN;
 472		if (sas_protocol_ata(task_proto)) {
 473			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 474			task->task_done(task);
 475			spin_lock_irqsave(&pm8001_ha->lock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 476		} else {
 477			task->task_done(task);
 478		}
 479		rc = -ENODEV;
 480		goto err_out;
 481	}
 482
 483	ccb = pm8001_ccb_alloc(pm8001_ha, pm8001_dev, task);
 484	if (!ccb) {
 485		rc = -SAS_QUEUE_FULL;
 486		goto err_out;
 487	}
 488
 489	if (!sas_protocol_ata(task_proto)) {
 490		if (task->num_scatter) {
 491			n_elem = dma_map_sg(pm8001_ha->dev, task->scatter,
 492					    task->num_scatter, task->data_dir);
 493			if (!n_elem) {
 494				rc = -ENOMEM;
 495				goto err_out_ccb;
 496			}
 
 
 
 
 
 
 
 
 
 
 
 
 497		}
 498	} else {
 499		n_elem = task->num_scatter;
 500	}
 501
 502	task->lldd_task = ccb;
 503	ccb->n_elem = n_elem;
 504
 505	atomic_inc(&pm8001_dev->running_req);
 506
 507	rc = pm8001_deliver_command(pm8001_ha, ccb);
 508	if (rc) {
 509		atomic_dec(&pm8001_dev->running_req);
 510		if (!sas_protocol_ata(task_proto) && n_elem)
 511			dma_unmap_sg(pm8001_ha->dev, task->scatter,
 512				     task->num_scatter, task->data_dir);
 513err_out_ccb:
 514		pm8001_ccb_free(pm8001_ha, ccb);
 
 
 515
 
 
 516err_out:
 517		pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec failed[%d]!\n", rc);
 518	}
 
 
 
 
 
 
 
 519
 520	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 521
 522	return rc;
 
 
 523}
 524
 525/**
 526  * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
 527  * @pm8001_ha: our hba card information
 528  * @ccb: the ccb which attached to ssp task to free
 
 
 529  */
 530void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
 531			  struct pm8001_ccb_info *ccb)
 532{
 533	struct sas_task *task = ccb->task;
 534	struct ata_queued_cmd *qc;
 535	struct pm8001_device *pm8001_dev;
 536
 537	if (!task)
 538		return;
 539
 540	if (!sas_protocol_ata(task->task_proto) && ccb->n_elem)
 541		dma_unmap_sg(pm8001_ha->dev, task->scatter,
 542			     task->num_scatter, task->data_dir);
 543
 544	switch (task->task_proto) {
 545	case SAS_PROTOCOL_SMP:
 546		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
 547			DMA_FROM_DEVICE);
 548		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
 549			DMA_TO_DEVICE);
 550		break;
 551
 552	case SAS_PROTOCOL_SATA:
 553	case SAS_PROTOCOL_STP:
 554	case SAS_PROTOCOL_SSP:
 555	default:
 556		/* do nothing */
 557		break;
 558	}
 559
 560	if (sas_protocol_ata(task->task_proto)) {
 561		/* For SCSI/ATA commands uldd_task points to ata_queued_cmd */
 562		qc = task->uldd_task;
 563		pm8001_dev = ccb->device;
 564		trace_pm80xx_request_complete(pm8001_ha->id,
 565			pm8001_dev ? pm8001_dev->attached_phy : PM8001_MAX_PHYS,
 566			ccb->ccb_tag, 0 /* ctlr_opcode not known */,
 567			qc ? qc->tf.command : 0, // ata opcode
 568			pm8001_dev ? atomic_read(&pm8001_dev->running_req) : -1);
 569	}
 570
 571	task->lldd_task = NULL;
 572	pm8001_ccb_free(pm8001_ha, ccb);
 
 
 573}
 574
 575static void pm8001_init_dev(struct pm8001_device *pm8001_dev, int id)
 576{
 577	pm8001_dev->id = id;
 578	pm8001_dev->device_id = PM8001_MAX_DEVICES;
 579	atomic_set(&pm8001_dev->running_req, 0);
 580}
 581
 582/**
 583 * pm8001_alloc_dev - find a empty pm8001_device
 584 * @pm8001_ha: our hba card information
 585 */
 586static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
 587{
 588	u32 dev;
 589	for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
 590		struct pm8001_device *pm8001_dev = &pm8001_ha->devices[dev];
 591
 592		if (pm8001_dev->dev_type == SAS_PHY_UNUSED) {
 593			pm8001_init_dev(pm8001_dev, dev);
 594			return pm8001_dev;
 595		}
 596	}
 597	if (dev == PM8001_MAX_DEVICES) {
 598		pm8001_dbg(pm8001_ha, FAIL,
 599			   "max support %d devices, ignore ..\n",
 600			   PM8001_MAX_DEVICES);
 601	}
 602	return NULL;
 603}
 604/**
 605  * pm8001_find_dev - find a matching pm8001_device
 606  * @pm8001_ha: our hba card information
 607  * @device_id: device ID to match against
 608  */
 609struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
 610					u32 device_id)
 611{
 612	u32 dev;
 613	for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
 614		if (pm8001_ha->devices[dev].device_id == device_id)
 
 615			return &pm8001_ha->devices[dev];
 
 616	}
 617	if (dev == PM8001_MAX_DEVICES) {
 618		pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
 
 
 619	}
 620	return NULL;
 621}
 622
 623void pm8001_free_dev(struct pm8001_device *pm8001_dev)
 624{
 
 625	memset(pm8001_dev, 0, sizeof(*pm8001_dev));
 626	pm8001_dev->dev_type = SAS_PHY_UNUSED;
 
 627	pm8001_dev->device_id = PM8001_MAX_DEVICES;
 628	pm8001_dev->sas_device = NULL;
 629}
 630
 631/**
 632  * pm8001_dev_found_notify - libsas notify a device is found.
 633  * @dev: the device structure which sas layer used.
 634  *
 635  * when libsas find a sas domain device, it should tell the LLDD that
 636  * device is found, and then LLDD register this device to HBA firmware
 637  * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
 638  * device ID(according to device's sas address) and returned it to LLDD. From
 639  * now on, we communicate with HBA FW with the device ID which HBA assigned
 640  * rather than sas address. it is the necessary step for our HBA but it is
 641  * the optional for other HBA driver.
 642  */
 643static int pm8001_dev_found_notify(struct domain_device *dev)
 644{
 645	unsigned long flags = 0;
 646	int res = 0;
 647	struct pm8001_hba_info *pm8001_ha = NULL;
 648	struct domain_device *parent_dev = dev->parent;
 649	struct pm8001_device *pm8001_device;
 650	DECLARE_COMPLETION_ONSTACK(completion);
 651	u32 flag = 0;
 652	pm8001_ha = pm8001_find_ha_by_dev(dev);
 653	spin_lock_irqsave(&pm8001_ha->lock, flags);
 654
 655	pm8001_device = pm8001_alloc_dev(pm8001_ha);
 656	if (!pm8001_device) {
 657		res = -1;
 658		goto found_out;
 659	}
 660	pm8001_device->sas_device = dev;
 661	dev->lldd_dev = pm8001_device;
 662	pm8001_device->dev_type = dev->dev_type;
 663	pm8001_device->dcompletion = &completion;
 664	if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
 665		int phy_id;
 666
 667		phy_id = sas_find_attached_phy_id(&parent_dev->ex_dev, dev);
 668		if (phy_id < 0) {
 669			pm8001_dbg(pm8001_ha, FAIL,
 670				   "Error: no attached dev:%016llx at ex:%016llx.\n",
 671				   SAS_ADDR(dev->sas_addr),
 672				   SAS_ADDR(parent_dev->sas_addr));
 673			res = phy_id;
 674		} else {
 675			pm8001_device->attached_phy = phy_id;
 
 
 
 
 
 
 676		}
 677	} else {
 678		if (dev->dev_type == SAS_SATA_DEV) {
 679			pm8001_device->attached_phy =
 680				dev->rphy->identify.phy_identifier;
 681			flag = 1; /* directly sata */
 682		}
 683	} /*register this device to HBA*/
 684	pm8001_dbg(pm8001_ha, DISC, "Found device\n");
 685	PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
 686	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 687	wait_for_completion(&completion);
 688	if (dev->dev_type == SAS_END_DEVICE)
 689		msleep(50);
 690	pm8001_ha->flags = PM8001F_RUN_TIME;
 691	return 0;
 692found_out:
 693	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 694	return res;
 695}
 696
 697int pm8001_dev_found(struct domain_device *dev)
 698{
 699	return pm8001_dev_found_notify(dev);
 700}
 701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 702#define PM8001_TASK_TIMEOUT 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 703
 704/**
 705  * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
 706  * @dev: the device structure which sas layer used.
 707  */
 708static void pm8001_dev_gone_notify(struct domain_device *dev)
 709{
 710	unsigned long flags = 0;
 
 711	struct pm8001_hba_info *pm8001_ha;
 712	struct pm8001_device *pm8001_dev = dev->lldd_dev;
 713
 714	pm8001_ha = pm8001_find_ha_by_dev(dev);
 715	spin_lock_irqsave(&pm8001_ha->lock, flags);
 
 716	if (pm8001_dev) {
 717		u32 device_id = pm8001_dev->device_id;
 718
 719		pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
 720			   pm8001_dev->device_id, pm8001_dev->dev_type);
 721		if (atomic_read(&pm8001_dev->running_req)) {
 
 722			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 723			sas_execute_internal_abort_dev(dev, 0, NULL);
 724			while (atomic_read(&pm8001_dev->running_req))
 725				msleep(20);
 726			spin_lock_irqsave(&pm8001_ha->lock, flags);
 727		}
 728		PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
 729		pm8001_free_dev(pm8001_dev);
 730	} else {
 731		pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
 
 732	}
 733	dev->lldd_dev = NULL;
 734	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 735}
 736
 737void pm8001_dev_gone(struct domain_device *dev)
 738{
 739	pm8001_dev_gone_notify(dev);
 740}
 741
 742/* retry commands by ha, by task and/or by device */
 743void pm8001_open_reject_retry(
 744	struct pm8001_hba_info *pm8001_ha,
 745	struct sas_task *task_to_close,
 746	struct pm8001_device *device_to_close)
 747{
 748	int i;
 749	unsigned long flags;
 750
 751	if (pm8001_ha == NULL)
 752		return;
 753
 754	spin_lock_irqsave(&pm8001_ha->lock, flags);
 755
 756	for (i = 0; i < PM8001_MAX_CCB; i++) {
 757		struct sas_task *task;
 758		struct task_status_struct *ts;
 759		struct pm8001_device *pm8001_dev;
 760		unsigned long flags1;
 761		struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
 762
 763		if (ccb->ccb_tag == PM8001_INVALID_TAG)
 764			continue;
 765
 766		pm8001_dev = ccb->device;
 767		if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
 768			continue;
 769		if (!device_to_close) {
 770			uintptr_t d = (uintptr_t)pm8001_dev
 771					- (uintptr_t)&pm8001_ha->devices;
 772			if (((d % sizeof(*pm8001_dev)) != 0)
 773			 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
 774				continue;
 775		} else if (pm8001_dev != device_to_close)
 776			continue;
 777		task = ccb->task;
 778		if (!task || !task->task_done)
 779			continue;
 780		if (task_to_close && (task != task_to_close))
 781			continue;
 782		ts = &task->task_status;
 783		ts->resp = SAS_TASK_COMPLETE;
 784		/* Force the midlayer to retry */
 785		ts->stat = SAS_OPEN_REJECT;
 786		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
 787		if (pm8001_dev)
 788			atomic_dec(&pm8001_dev->running_req);
 789		spin_lock_irqsave(&task->task_state_lock, flags1);
 790		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
 791		task->task_state_flags |= SAS_TASK_STATE_DONE;
 792		if (unlikely((task->task_state_flags
 793				& SAS_TASK_STATE_ABORTED))) {
 794			spin_unlock_irqrestore(&task->task_state_lock,
 795				flags1);
 796			pm8001_ccb_task_free(pm8001_ha, ccb);
 797		} else {
 798			spin_unlock_irqrestore(&task->task_state_lock,
 799				flags1);
 800			pm8001_ccb_task_free(pm8001_ha, ccb);
 801			mb();/* in order to force CPU ordering */
 802			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 803			task->task_done(task);
 804			spin_lock_irqsave(&pm8001_ha->lock, flags);
 805		}
 806	}
 807
 808	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
 809}
 810
 811/**
 812 * pm8001_I_T_nexus_reset() - reset the initiator/target connection
 813 * @dev: the device structure for the device to reset.
 814 *
 815 * Standard mandates link reset for ATA (type 0) and hard reset for
 816 * SSP (type 1), only for RECOVERY
 817 */
 818int pm8001_I_T_nexus_reset(struct domain_device *dev)
 819{
 820	int rc = TMF_RESP_FUNC_FAILED;
 821	struct pm8001_device *pm8001_dev;
 822	struct pm8001_hba_info *pm8001_ha;
 823	struct sas_phy *phy;
 824
 825	if (!dev || !dev->lldd_dev)
 826		return -ENODEV;
 827
 828	pm8001_dev = dev->lldd_dev;
 829	pm8001_ha = pm8001_find_ha_by_dev(dev);
 830	phy = sas_get_local_phy(dev);
 831
 832	if (dev_is_sata(dev)) {
 833		if (scsi_is_sas_phy_local(phy)) {
 834			rc = 0;
 835			goto out;
 836		}
 837		rc = sas_phy_reset(phy, 1);
 838		if (rc) {
 839			pm8001_dbg(pm8001_ha, EH,
 840				   "phy reset failed for device %x\n"
 841				   "with rc %d\n", pm8001_dev->device_id, rc);
 842			rc = TMF_RESP_FUNC_FAILED;
 843			goto out;
 844		}
 845		msleep(2000);
 846		rc = sas_execute_internal_abort_dev(dev, 0, NULL);
 847		if (rc) {
 848			pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
 849				   "with rc %d\n", pm8001_dev->device_id, rc);
 850			rc = TMF_RESP_FUNC_FAILED;
 851		}
 852	} else {
 853		rc = sas_phy_reset(phy, 1);
 854		msleep(2000);
 855	}
 856	pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
 857		   pm8001_dev->device_id, rc);
 858 out:
 859	sas_put_local_phy(phy);
 860	return rc;
 861}
 862
 863/*
 864* This function handle the IT_NEXUS_XXX event or completion
 865* status code for SSP/SATA/SMP I/O request.
 866*/
 867int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
 868{
 869	int rc = TMF_RESP_FUNC_FAILED;
 870	struct pm8001_device *pm8001_dev;
 871	struct pm8001_hba_info *pm8001_ha;
 872	struct sas_phy *phy;
 873
 874	if (!dev || !dev->lldd_dev)
 875		return -1;
 876
 877	pm8001_dev = dev->lldd_dev;
 878	pm8001_ha = pm8001_find_ha_by_dev(dev);
 879
 880	pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
 881
 882	phy = sas_get_local_phy(dev);
 883
 884	if (dev_is_sata(dev)) {
 885		DECLARE_COMPLETION_ONSTACK(completion_setstate);
 886		if (scsi_is_sas_phy_local(phy)) {
 887			rc = 0;
 888			goto out;
 889		}
 890		/* send internal ssp/sata/smp abort command to FW */
 891		sas_execute_internal_abort_dev(dev, 0, NULL);
 892		msleep(100);
 893
 894		/* deregister the target device */
 895		pm8001_dev_gone_notify(dev);
 896		msleep(200);
 897
 898		/*send phy reset to hard reset target */
 899		rc = sas_phy_reset(phy, 1);
 900		msleep(2000);
 
 
 901		pm8001_dev->setds_completion = &completion_setstate;
 902
 
 903		wait_for_completion(&completion_setstate);
 904	} else {
 905		/* send internal ssp/sata/smp abort command to FW */
 906		sas_execute_internal_abort_dev(dev, 0, NULL);
 907		msleep(100);
 908
 909		/* deregister the target device */
 910		pm8001_dev_gone_notify(dev);
 911		msleep(200);
 912
 913		/*send phy reset to hard reset target */
 914		rc = sas_phy_reset(phy, 1);
 915		msleep(2000);
 916	}
 917	pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
 918		   pm8001_dev->device_id, rc);
 919out:
 920	sas_put_local_phy(phy);
 921
 922	return rc;
 923}
 
 924/* mandatory SAM-3, the task reset the specified LUN*/
 925int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
 926{
 927	int rc = TMF_RESP_FUNC_FAILED;
 
 928	struct pm8001_device *pm8001_dev = dev->lldd_dev;
 929	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
 930	DECLARE_COMPLETION_ONSTACK(completion_setstate);
 931
 932	if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
 933		/*
 934		 * If the controller is in fatal error state,
 935		 * we will not get a response from the controller
 936		 */
 937		pm8001_dbg(pm8001_ha, FAIL,
 938			   "LUN reset failed due to fatal errors\n");
 939		return rc;
 940	}
 941
 942	if (dev_is_sata(dev)) {
 943		struct sas_phy *phy = sas_get_local_phy(dev);
 944		sas_execute_internal_abort_dev(dev, 0, NULL);
 
 945		rc = sas_phy_reset(phy, 1);
 946		sas_put_local_phy(phy);
 947		pm8001_dev->setds_completion = &completion_setstate;
 948		rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
 949			pm8001_dev, DS_OPERATIONAL);
 950		wait_for_completion(&completion_setstate);
 951	} else {
 952		rc = sas_lu_reset(dev, lun);
 
 953	}
 954	/* If failed, fall-through I_T_Nexus reset */
 955	pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
 956		   pm8001_dev->device_id, rc);
 957	return rc;
 958}
 959
 960/* optional SAM-3 */
 961int pm8001_query_task(struct sas_task *task)
 962{
 963	u32 tag = 0xdeadbeef;
 
 
 
 964	int rc = TMF_RESP_FUNC_FAILED;
 965	if (unlikely(!task || !task->lldd_task || !task->dev))
 966		return rc;
 967
 968	if (task->task_proto & SAS_PROTOCOL_SSP) {
 969		struct scsi_cmnd *cmnd = task->uldd_task;
 970		struct domain_device *dev = task->dev;
 971		struct pm8001_hba_info *pm8001_ha =
 972			pm8001_find_ha_by_dev(dev);
 973
 
 974		rc = pm8001_find_tag(task, &tag);
 975		if (rc == 0) {
 976			rc = TMF_RESP_FUNC_FAILED;
 977			return rc;
 978		}
 979		pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
 
 
 
 
 
 980
 981		rc = sas_query_task(task, tag);
 982		switch (rc) {
 983		/* The task is still in Lun, release it then */
 984		case TMF_RESP_FUNC_SUCC:
 985			pm8001_dbg(pm8001_ha, EH,
 986				   "The task is still in Lun\n");
 987			break;
 988		/* The task is not in Lun or failed, reset the phy */
 989		case TMF_RESP_FUNC_FAILED:
 990		case TMF_RESP_FUNC_COMPLETE:
 991			pm8001_dbg(pm8001_ha, EH,
 992				   "The task is not in Lun or failed, reset the phy\n");
 
 993			break;
 994		}
 995	}
 996	pr_err("pm80xx: rc= %d\n", rc);
 997	return rc;
 998}
 999
1000/*  mandatory SAM-3, still need free task/ccb info, abort the specified task */
1001int pm8001_abort_task(struct sas_task *task)
1002{
1003	struct pm8001_ccb_info *ccb = task->lldd_task;
1004	unsigned long flags;
1005	u32 tag;
 
1006	struct domain_device *dev ;
1007	struct pm8001_hba_info *pm8001_ha;
 
 
1008	struct pm8001_device *pm8001_dev;
1009	int rc = TMF_RESP_FUNC_FAILED, ret;
1010	u32 phy_id, port_id;
1011	struct sas_task_slow slow_task;
1012
1013	if (!task->lldd_task || !task->dev)
1014		return TMF_RESP_FUNC_FAILED;
1015
1016	dev = task->dev;
1017	pm8001_dev = dev->lldd_dev;
1018	pm8001_ha = pm8001_find_ha_by_dev(dev);
1019	phy_id = pm8001_dev->attached_phy;
1020
1021	if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
1022		// If the controller is seeing fatal errors
1023		// abort task will not get a response from the controller
1024		return TMF_RESP_FUNC_FAILED;
1025	}
1026
1027	ret = pm8001_find_tag(task, &tag);
1028	if (ret == 0) {
1029		pm8001_info(pm8001_ha, "no tag for task:%p\n", task);
1030		return TMF_RESP_FUNC_FAILED;
1031	}
1032	spin_lock_irqsave(&task->task_state_lock, flags);
1033	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1034		spin_unlock_irqrestore(&task->task_state_lock, flags);
1035		return TMF_RESP_FUNC_COMPLETE;
1036	}
1037	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1038	if (task->slow_task == NULL) {
1039		init_completion(&slow_task.completion);
1040		task->slow_task = &slow_task;
1041	}
1042	spin_unlock_irqrestore(&task->task_state_lock, flags);
1043	if (task->task_proto & SAS_PROTOCOL_SSP) {
1044		rc = sas_abort_task(task, tag);
1045		sas_execute_internal_abort_single(dev, tag, 0, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1046	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
1047		task->task_proto & SAS_PROTOCOL_STP) {
1048		if (pm8001_ha->chip_id == chip_8006) {
1049			DECLARE_COMPLETION_ONSTACK(completion_reset);
1050			DECLARE_COMPLETION_ONSTACK(completion);
1051			struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1052			port_id = phy->port->port_id;
1053
1054			/* 1. Set Device state as Recovery */
1055			pm8001_dev->setds_completion = &completion;
1056			PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1057				pm8001_dev, DS_IN_RECOVERY);
1058			wait_for_completion(&completion);
1059
1060			/* 2. Send Phy Control Hard Reset */
1061			reinit_completion(&completion);
1062			phy->port_reset_status = PORT_RESET_TMO;
1063			phy->reset_success = false;
1064			phy->enable_completion = &completion;
1065			phy->reset_completion = &completion_reset;
1066			ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
1067				PHY_HARD_RESET);
1068			if (ret) {
1069				phy->enable_completion = NULL;
1070				phy->reset_completion = NULL;
1071				goto out;
1072			}
1073
1074			/* In the case of the reset timeout/fail we still
1075			 * abort the command at the firmware. The assumption
1076			 * here is that the drive is off doing something so
1077			 * that it's not processing requests, and we want to
1078			 * avoid getting a completion for this and either
1079			 * leaking the task in libsas or losing the race and
1080			 * getting a double free.
1081			 */
1082			pm8001_dbg(pm8001_ha, MSG,
1083				   "Waiting for local phy ctl\n");
1084			ret = wait_for_completion_timeout(&completion,
1085					PM8001_TASK_TIMEOUT * HZ);
1086			if (!ret || !phy->reset_success) {
1087				phy->enable_completion = NULL;
1088				phy->reset_completion = NULL;
1089			} else {
1090				/* 3. Wait for Port Reset complete or
1091				 * Port reset TMO
1092				 */
1093				pm8001_dbg(pm8001_ha, MSG,
1094					   "Waiting for Port reset\n");
1095				ret = wait_for_completion_timeout(
1096					&completion_reset,
1097					PM8001_TASK_TIMEOUT * HZ);
1098				if (!ret)
1099					phy->reset_completion = NULL;
1100				WARN_ON(phy->port_reset_status ==
1101						PORT_RESET_TMO);
1102				if (phy->port_reset_status == PORT_RESET_TMO) {
1103					pm8001_dev_gone_notify(dev);
1104					PM8001_CHIP_DISP->hw_event_ack_req(
1105						pm8001_ha, 0,
1106						0x07, /*HW_EVENT_PHY_DOWN ack*/
1107						port_id, phy_id, 0, 0);
1108					goto out;
1109				}
1110			}
1111
1112			/*
1113			 * 4. SATA Abort ALL
1114			 * we wait for the task to be aborted so that the task
1115			 * is removed from the ccb. on success the caller is
1116			 * going to free the task.
1117			 */
1118			ret = sas_execute_internal_abort_dev(dev, 0, NULL);
1119			if (ret)
1120				goto out;
1121			ret = wait_for_completion_timeout(
1122				&task->slow_task->completion,
1123				PM8001_TASK_TIMEOUT * HZ);
1124			if (!ret)
1125				goto out;
1126
1127			/* 5. Set Device State as Operational */
1128			reinit_completion(&completion);
1129			pm8001_dev->setds_completion = &completion;
1130			PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1131				pm8001_dev, DS_OPERATIONAL);
1132			wait_for_completion(&completion);
1133		} else {
1134			/*
1135			 * Ensure that if we see a completion for the ccb
1136			 * associated with the task which we are trying to
1137			 * abort then we should not touch the sas_task as it
1138			 * may race with libsas freeing it when return here.
1139			 */
1140			ccb->task = NULL;
1141			ret = sas_execute_internal_abort_single(dev, tag, 0, NULL);
1142		}
1143		rc = TMF_RESP_FUNC_COMPLETE;
 
1144	} else if (task->task_proto & SAS_PROTOCOL_SMP) {
1145		/* SMP */
1146		rc = sas_execute_internal_abort_single(dev, tag, 0, NULL);
 
 
 
 
 
 
 
 
 
 
1147
1148	}
1149out:
1150	spin_lock_irqsave(&task->task_state_lock, flags);
1151	if (task->slow_task == &slow_task)
1152		task->slow_task = NULL;
1153	spin_unlock_irqrestore(&task->task_state_lock, flags);
1154	if (rc != TMF_RESP_FUNC_COMPLETE)
1155		pm8001_info(pm8001_ha, "rc= %d\n", rc);
1156	return rc;
1157}
1158
1159int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1160{
1161	struct pm8001_device *pm8001_dev = dev->lldd_dev;
1162	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1163
1164	pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
1165		   pm8001_dev->device_id);
1166	return sas_clear_task_set(dev, lun);
1167}
1168
1169void pm8001_port_formed(struct asd_sas_phy *sas_phy)
1170{
1171	struct sas_ha_struct *sas_ha = sas_phy->ha;
1172	struct pm8001_hba_info *pm8001_ha = sas_ha->lldd_ha;
1173	struct pm8001_phy *phy = sas_phy->lldd_phy;
1174	struct asd_sas_port *sas_port = sas_phy->port;
1175	struct pm8001_port *port = phy->port;
1176
1177	if (!sas_port) {
1178		pm8001_dbg(pm8001_ha, FAIL, "Received null port\n");
1179		return;
1180	}
1181	sas_port->lldd_port = port;
1182}
1183
1184void pm8001_setds_completion(struct domain_device *dev)
1185{
 
 
 
1186	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1187	struct pm8001_device *pm8001_dev = dev->lldd_dev;
1188	DECLARE_COMPLETION_ONSTACK(completion_setstate);
1189
1190	if (pm8001_ha->chip_id != chip_8001) {
1191		pm8001_dev->setds_completion = &completion_setstate;
1192		PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1193			pm8001_dev, DS_OPERATIONAL);
1194		wait_for_completion(&completion_setstate);
1195	}
1196}
1197
1198void pm8001_tmf_aborted(struct sas_task *task)
1199{
1200	struct pm8001_ccb_info *ccb = task->lldd_task;
1201
1202	if (ccb)
1203		ccb->task = NULL;
1204}