Linux Audio

Check our new training course

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