xref: /openbmc/linux/drivers/scsi/pm8001/pm8001_sas.c (revision e15a5365)
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 
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  */
49 static 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_free - free the no more needed tag
62   * @pm8001_ha: our hba struct
63   * @tag: the found tag associated with the task
64   */
65 void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
66 {
67 	void *bitmap = pm8001_ha->tags;
68 	clear_bit(tag, bitmap);
69 }
70 
71 /**
72   * pm8001_tag_alloc - allocate a empty tag for task used.
73   * @pm8001_ha: our hba struct
74   * @tag_out: the found empty tag .
75   */
76 inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
77 {
78 	unsigned int tag;
79 	void *bitmap = pm8001_ha->tags;
80 	unsigned long flags;
81 
82 	spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
83 	tag = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
84 	if (tag >= pm8001_ha->tags_num) {
85 		spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
86 		return -SAS_QUEUE_FULL;
87 	}
88 	set_bit(tag, bitmap);
89 	spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
90 	*tag_out = tag;
91 	return 0;
92 }
93 
94 void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
95 {
96 	int i;
97 	for (i = 0; i < pm8001_ha->tags_num; ++i)
98 		pm8001_tag_free(pm8001_ha, i);
99 }
100 
101  /**
102   * pm8001_mem_alloc - allocate memory for pm8001.
103   * @pdev: pci device.
104   * @virt_addr: the allocated virtual address
105   * @pphys_addr_hi: the physical address high byte address.
106   * @pphys_addr_lo: the physical address low byte address.
107   * @mem_size: memory size.
108   */
109 int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
110 	dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
111 	u32 *pphys_addr_lo, u32 mem_size, u32 align)
112 {
113 	caddr_t mem_virt_alloc;
114 	dma_addr_t mem_dma_handle;
115 	u64 phys_align;
116 	u64 align_offset = 0;
117 	if (align)
118 		align_offset = (dma_addr_t)align - 1;
119 	mem_virt_alloc = dma_alloc_coherent(&pdev->dev, mem_size + align,
120 					    &mem_dma_handle, GFP_KERNEL);
121 	if (!mem_virt_alloc) {
122 		pr_err("pm80xx: memory allocation error\n");
123 		return -1;
124 	}
125 	*pphys_addr = mem_dma_handle;
126 	phys_align = (*pphys_addr + align_offset) & ~align_offset;
127 	*virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
128 	*pphys_addr_hi = upper_32_bits(phys_align);
129 	*pphys_addr_lo = lower_32_bits(phys_align);
130 	return 0;
131 }
132 
133 /**
134   * pm8001_find_ha_by_dev - from domain device which come from sas layer to
135   * find out our hba struct.
136   * @dev: the domain device which from sas layer.
137   */
138 static
139 struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
140 {
141 	struct sas_ha_struct *sha = dev->port->ha;
142 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
143 	return pm8001_ha;
144 }
145 
146 /**
147   * pm8001_phy_control - this function should be registered to
148   * sas_domain_function_template to provide libsas used, note: this is just
149   * control the HBA phy rather than other expander phy if you want control
150   * other phy, you should use SMP command.
151   * @sas_phy: which phy in HBA phys.
152   * @func: the operation.
153   * @funcdata: always NULL.
154   */
155 int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
156 	void *funcdata)
157 {
158 	int rc = 0, phy_id = sas_phy->id;
159 	struct pm8001_hba_info *pm8001_ha = NULL;
160 	struct sas_phy_linkrates *rates;
161 	struct sas_ha_struct *sas_ha;
162 	struct pm8001_phy *phy;
163 	DECLARE_COMPLETION_ONSTACK(completion);
164 	unsigned long flags;
165 	pm8001_ha = sas_phy->ha->lldd_ha;
166 	phy = &pm8001_ha->phy[phy_id];
167 	pm8001_ha->phy[phy_id].enable_completion = &completion;
168 	switch (func) {
169 	case PHY_FUNC_SET_LINK_RATE:
170 		rates = funcdata;
171 		if (rates->minimum_linkrate) {
172 			pm8001_ha->phy[phy_id].minimum_linkrate =
173 				rates->minimum_linkrate;
174 		}
175 		if (rates->maximum_linkrate) {
176 			pm8001_ha->phy[phy_id].maximum_linkrate =
177 				rates->maximum_linkrate;
178 		}
179 		if (pm8001_ha->phy[phy_id].phy_state ==  PHY_LINK_DISABLE) {
180 			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
181 			wait_for_completion(&completion);
182 		}
183 		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
184 					      PHY_LINK_RESET);
185 		break;
186 	case PHY_FUNC_HARD_RESET:
187 		if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
188 			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
189 			wait_for_completion(&completion);
190 		}
191 		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
192 					      PHY_HARD_RESET);
193 		break;
194 	case PHY_FUNC_LINK_RESET:
195 		if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
196 			PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
197 			wait_for_completion(&completion);
198 		}
199 		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
200 					      PHY_LINK_RESET);
201 		break;
202 	case PHY_FUNC_RELEASE_SPINUP_HOLD:
203 		PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
204 					      PHY_LINK_RESET);
205 		break;
206 	case PHY_FUNC_DISABLE:
207 		if (pm8001_ha->chip_id != chip_8001) {
208 			if (pm8001_ha->phy[phy_id].phy_state ==
209 				PHY_STATE_LINK_UP_SPCV) {
210 				sas_ha = pm8001_ha->sas;
211 				sas_phy_disconnected(&phy->sas_phy);
212 				sas_ha->notify_phy_event(&phy->sas_phy,
213 					PHYE_LOSS_OF_SIGNAL);
214 				phy->phy_attached = 0;
215 			}
216 		} else {
217 			if (pm8001_ha->phy[phy_id].phy_state ==
218 				PHY_STATE_LINK_UP_SPC) {
219 				sas_ha = pm8001_ha->sas;
220 				sas_phy_disconnected(&phy->sas_phy);
221 				sas_ha->notify_phy_event(&phy->sas_phy,
222 					PHYE_LOSS_OF_SIGNAL);
223 				phy->phy_attached = 0;
224 			}
225 		}
226 		PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
227 		break;
228 	case PHY_FUNC_GET_EVENTS:
229 		spin_lock_irqsave(&pm8001_ha->lock, flags);
230 		if (pm8001_ha->chip_id == chip_8001) {
231 			if (-1 == pm8001_bar4_shift(pm8001_ha,
232 					(phy_id < 4) ? 0x30000 : 0x40000)) {
233 				spin_unlock_irqrestore(&pm8001_ha->lock, flags);
234 				return -EINVAL;
235 			}
236 		}
237 		{
238 			struct sas_phy *phy = sas_phy->phy;
239 			uint32_t *qp = (uint32_t *)(((char *)
240 				pm8001_ha->io_mem[2].memvirtaddr)
241 				+ 0x1034 + (0x4000 * (phy_id & 3)));
242 
243 			phy->invalid_dword_count = qp[0];
244 			phy->running_disparity_error_count = qp[1];
245 			phy->loss_of_dword_sync_count = qp[3];
246 			phy->phy_reset_problem_count = qp[4];
247 		}
248 		if (pm8001_ha->chip_id == chip_8001)
249 			pm8001_bar4_shift(pm8001_ha, 0);
250 		spin_unlock_irqrestore(&pm8001_ha->lock, flags);
251 		return 0;
252 	default:
253 		PM8001_DEVIO_DBG(pm8001_ha,
254 			pm8001_printk("func 0x%x\n", func));
255 		rc = -EOPNOTSUPP;
256 	}
257 	msleep(300);
258 	return rc;
259 }
260 
261 /**
262   * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
263   * command to HBA.
264   * @shost: the scsi host data.
265   */
266 void pm8001_scan_start(struct Scsi_Host *shost)
267 {
268 	int i;
269 	struct pm8001_hba_info *pm8001_ha;
270 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
271 	pm8001_ha = sha->lldd_ha;
272 	/* SAS_RE_INITIALIZATION not available in SPCv/ve */
273 	if (pm8001_ha->chip_id == chip_8001)
274 		PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
275 	for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
276 		PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
277 }
278 
279 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
280 {
281 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
282 
283 	/* give the phy enabling interrupt event time to come in (1s
284 	* is empirically about all it takes) */
285 	if (time < HZ)
286 		return 0;
287 	/* Wait for discovery to finish */
288 	sas_drain_work(ha);
289 	return 1;
290 }
291 
292 /**
293   * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
294   * @pm8001_ha: our hba card information
295   * @ccb: the ccb which attached to smp task
296   */
297 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
298 	struct pm8001_ccb_info *ccb)
299 {
300 	return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
301 }
302 
303 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
304 {
305 	struct ata_queued_cmd *qc = task->uldd_task;
306 	if (qc) {
307 		if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
308 		    qc->tf.command == ATA_CMD_FPDMA_READ ||
309 		    qc->tf.command == ATA_CMD_FPDMA_RECV ||
310 		    qc->tf.command == ATA_CMD_FPDMA_SEND ||
311 		    qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
312 			*tag = qc->tag;
313 			return 1;
314 		}
315 	}
316 	return 0;
317 }
318 
319 /**
320   * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
321   * @pm8001_ha: our hba card information
322   * @ccb: the ccb which attached to sata task
323   */
324 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
325 	struct pm8001_ccb_info *ccb)
326 {
327 	return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
328 }
329 
330 /**
331   * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
332   * @pm8001_ha: our hba card information
333   * @ccb: the ccb which attached to TM
334   * @tmf: the task management IU
335   */
336 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
337 	struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
338 {
339 	return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
340 }
341 
342 /**
343   * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
344   * @pm8001_ha: our hba card information
345   * @ccb: the ccb which attached to ssp task
346   */
347 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
348 	struct pm8001_ccb_info *ccb)
349 {
350 	return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
351 }
352 
353  /* Find the local port id that's attached to this device */
354 static int sas_find_local_port_id(struct domain_device *dev)
355 {
356 	struct domain_device *pdev = dev->parent;
357 
358 	/* Directly attached device */
359 	if (!pdev)
360 		return dev->port->id;
361 	while (pdev) {
362 		struct domain_device *pdev_p = pdev->parent;
363 		if (!pdev_p)
364 			return pdev->port->id;
365 		pdev = pdev->parent;
366 	}
367 	return 0;
368 }
369 
370 #define DEV_IS_GONE(pm8001_dev)	\
371 	((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
372 /**
373   * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
374   * @task: the task to be execute.
375   * @gfp_flags: gfp_flags.
376   * @is_tmf: if it is task management task.
377   * @tmf: the task management IU
378   */
379 static int pm8001_task_exec(struct sas_task *task,
380 	gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
381 {
382 	struct domain_device *dev = task->dev;
383 	struct pm8001_hba_info *pm8001_ha;
384 	struct pm8001_device *pm8001_dev;
385 	struct pm8001_port *port = NULL;
386 	struct sas_task *t = task;
387 	struct pm8001_ccb_info *ccb;
388 	u32 tag = 0xdeadbeef, rc = 0, n_elem = 0;
389 	unsigned long flags = 0;
390 	enum sas_protocol task_proto = t->task_proto;
391 
392 	if (!dev->port) {
393 		struct task_status_struct *tsm = &t->task_status;
394 		tsm->resp = SAS_TASK_UNDELIVERED;
395 		tsm->stat = SAS_PHY_DOWN;
396 		if (dev->dev_type != SAS_SATA_DEV)
397 			t->task_done(t);
398 		return 0;
399 	}
400 	pm8001_ha = pm8001_find_ha_by_dev(task->dev);
401 	if (pm8001_ha->controller_fatal_error) {
402 		struct task_status_struct *ts = &t->task_status;
403 
404 		ts->resp = SAS_TASK_UNDELIVERED;
405 		t->task_done(t);
406 		return 0;
407 	}
408 	PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
409 	spin_lock_irqsave(&pm8001_ha->lock, flags);
410 	do {
411 		dev = t->dev;
412 		pm8001_dev = dev->lldd_dev;
413 		port = &pm8001_ha->port[sas_find_local_port_id(dev)];
414 		if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
415 			if (sas_protocol_ata(task_proto)) {
416 				struct task_status_struct *ts = &t->task_status;
417 				ts->resp = SAS_TASK_UNDELIVERED;
418 				ts->stat = SAS_PHY_DOWN;
419 
420 				spin_unlock_irqrestore(&pm8001_ha->lock, flags);
421 				t->task_done(t);
422 				spin_lock_irqsave(&pm8001_ha->lock, flags);
423 				continue;
424 			} else {
425 				struct task_status_struct *ts = &t->task_status;
426 				ts->resp = SAS_TASK_UNDELIVERED;
427 				ts->stat = SAS_PHY_DOWN;
428 				t->task_done(t);
429 				continue;
430 			}
431 		}
432 		rc = pm8001_tag_alloc(pm8001_ha, &tag);
433 		if (rc)
434 			goto err_out;
435 		ccb = &pm8001_ha->ccb_info[tag];
436 
437 		if (!sas_protocol_ata(task_proto)) {
438 			if (t->num_scatter) {
439 				n_elem = dma_map_sg(pm8001_ha->dev,
440 					t->scatter,
441 					t->num_scatter,
442 					t->data_dir);
443 				if (!n_elem) {
444 					rc = -ENOMEM;
445 					goto err_out_tag;
446 				}
447 			}
448 		} else {
449 			n_elem = t->num_scatter;
450 		}
451 
452 		t->lldd_task = ccb;
453 		ccb->n_elem = n_elem;
454 		ccb->ccb_tag = tag;
455 		ccb->task = t;
456 		ccb->device = pm8001_dev;
457 		switch (task_proto) {
458 		case SAS_PROTOCOL_SMP:
459 			rc = pm8001_task_prep_smp(pm8001_ha, ccb);
460 			break;
461 		case SAS_PROTOCOL_SSP:
462 			if (is_tmf)
463 				rc = pm8001_task_prep_ssp_tm(pm8001_ha,
464 					ccb, tmf);
465 			else
466 				rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
467 			break;
468 		case SAS_PROTOCOL_SATA:
469 		case SAS_PROTOCOL_STP:
470 			rc = pm8001_task_prep_ata(pm8001_ha, ccb);
471 			break;
472 		default:
473 			dev_printk(KERN_ERR, pm8001_ha->dev,
474 				"unknown sas_task proto: 0x%x\n", task_proto);
475 			rc = -EINVAL;
476 			break;
477 		}
478 
479 		if (rc) {
480 			PM8001_IO_DBG(pm8001_ha,
481 				pm8001_printk("rc is %x\n", rc));
482 			goto err_out_tag;
483 		}
484 		/* TODO: select normal or high priority */
485 		spin_lock(&t->task_state_lock);
486 		t->task_state_flags |= SAS_TASK_AT_INITIATOR;
487 		spin_unlock(&t->task_state_lock);
488 		pm8001_dev->running_req++;
489 	} while (0);
490 	rc = 0;
491 	goto out_done;
492 
493 err_out_tag:
494 	pm8001_tag_free(pm8001_ha, tag);
495 err_out:
496 	dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
497 	if (!sas_protocol_ata(task_proto))
498 		if (n_elem)
499 			dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter,
500 				t->data_dir);
501 out_done:
502 	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
503 	return rc;
504 }
505 
506 /**
507   * pm8001_queue_command - register for upper layer used, all IO commands sent
508   * to HBA are from this interface.
509   * @task: the task to be execute.
510   * @gfp_flags: gfp_flags
511   */
512 int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
513 {
514 	return pm8001_task_exec(task, gfp_flags, 0, NULL);
515 }
516 
517 /**
518   * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
519   * @pm8001_ha: our hba card information
520   * @ccb: the ccb which attached to ssp task
521   * @task: the task to be free.
522   * @ccb_idx: ccb index.
523   */
524 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
525 	struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
526 {
527 	if (!ccb->task)
528 		return;
529 	if (!sas_protocol_ata(task->task_proto))
530 		if (ccb->n_elem)
531 			dma_unmap_sg(pm8001_ha->dev, task->scatter,
532 				task->num_scatter, task->data_dir);
533 
534 	switch (task->task_proto) {
535 	case SAS_PROTOCOL_SMP:
536 		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
537 			DMA_FROM_DEVICE);
538 		dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
539 			DMA_TO_DEVICE);
540 		break;
541 
542 	case SAS_PROTOCOL_SATA:
543 	case SAS_PROTOCOL_STP:
544 	case SAS_PROTOCOL_SSP:
545 	default:
546 		/* do nothing */
547 		break;
548 	}
549 	task->lldd_task = NULL;
550 	ccb->task = NULL;
551 	ccb->ccb_tag = 0xFFFFFFFF;
552 	ccb->open_retry = 0;
553 	pm8001_tag_free(pm8001_ha, ccb_idx);
554 }
555 
556  /**
557   * pm8001_alloc_dev - find a empty pm8001_device
558   * @pm8001_ha: our hba card information
559   */
560 static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
561 {
562 	u32 dev;
563 	for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
564 		if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
565 			pm8001_ha->devices[dev].id = dev;
566 			return &pm8001_ha->devices[dev];
567 		}
568 	}
569 	if (dev == PM8001_MAX_DEVICES) {
570 		PM8001_FAIL_DBG(pm8001_ha,
571 			pm8001_printk("max support %d devices, ignore ..\n",
572 			PM8001_MAX_DEVICES));
573 	}
574 	return NULL;
575 }
576 /**
577   * pm8001_find_dev - find a matching pm8001_device
578   * @pm8001_ha: our hba card information
579   * @device_id: device ID to match against
580   */
581 struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
582 					u32 device_id)
583 {
584 	u32 dev;
585 	for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
586 		if (pm8001_ha->devices[dev].device_id == device_id)
587 			return &pm8001_ha->devices[dev];
588 	}
589 	if (dev == PM8001_MAX_DEVICES) {
590 		PM8001_FAIL_DBG(pm8001_ha, pm8001_printk("NO MATCHING "
591 				"DEVICE FOUND !!!\n"));
592 	}
593 	return NULL;
594 }
595 
596 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
597 {
598 	u32 id = pm8001_dev->id;
599 	memset(pm8001_dev, 0, sizeof(*pm8001_dev));
600 	pm8001_dev->id = id;
601 	pm8001_dev->dev_type = SAS_PHY_UNUSED;
602 	pm8001_dev->device_id = PM8001_MAX_DEVICES;
603 	pm8001_dev->sas_device = NULL;
604 }
605 
606 /**
607   * pm8001_dev_found_notify - libsas notify a device is found.
608   * @dev: the device structure which sas layer used.
609   *
610   * when libsas find a sas domain device, it should tell the LLDD that
611   * device is found, and then LLDD register this device to HBA firmware
612   * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
613   * device ID(according to device's sas address) and returned it to LLDD. From
614   * now on, we communicate with HBA FW with the device ID which HBA assigned
615   * rather than sas address. it is the necessary step for our HBA but it is
616   * the optional for other HBA driver.
617   */
618 static int pm8001_dev_found_notify(struct domain_device *dev)
619 {
620 	unsigned long flags = 0;
621 	int res = 0;
622 	struct pm8001_hba_info *pm8001_ha = NULL;
623 	struct domain_device *parent_dev = dev->parent;
624 	struct pm8001_device *pm8001_device;
625 	DECLARE_COMPLETION_ONSTACK(completion);
626 	u32 flag = 0;
627 	pm8001_ha = pm8001_find_ha_by_dev(dev);
628 	spin_lock_irqsave(&pm8001_ha->lock, flags);
629 
630 	pm8001_device = pm8001_alloc_dev(pm8001_ha);
631 	if (!pm8001_device) {
632 		res = -1;
633 		goto found_out;
634 	}
635 	pm8001_device->sas_device = dev;
636 	dev->lldd_dev = pm8001_device;
637 	pm8001_device->dev_type = dev->dev_type;
638 	pm8001_device->dcompletion = &completion;
639 	if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
640 		int phy_id;
641 		struct ex_phy *phy;
642 		for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
643 		phy_id++) {
644 			phy = &parent_dev->ex_dev.ex_phy[phy_id];
645 			if (SAS_ADDR(phy->attached_sas_addr)
646 				== SAS_ADDR(dev->sas_addr)) {
647 				pm8001_device->attached_phy = phy_id;
648 				break;
649 			}
650 		}
651 		if (phy_id == parent_dev->ex_dev.num_phys) {
652 			PM8001_FAIL_DBG(pm8001_ha,
653 			pm8001_printk("Error: no attached dev:%016llx"
654 			" at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
655 				SAS_ADDR(parent_dev->sas_addr)));
656 			res = -1;
657 		}
658 	} else {
659 		if (dev->dev_type == SAS_SATA_DEV) {
660 			pm8001_device->attached_phy =
661 				dev->rphy->identify.phy_identifier;
662 			flag = 1; /* directly sata */
663 		}
664 	} /*register this device to HBA*/
665 	PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
666 	PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
667 	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
668 	wait_for_completion(&completion);
669 	if (dev->dev_type == SAS_END_DEVICE)
670 		msleep(50);
671 	pm8001_ha->flags = PM8001F_RUN_TIME;
672 	return 0;
673 found_out:
674 	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
675 	return res;
676 }
677 
678 int pm8001_dev_found(struct domain_device *dev)
679 {
680 	return pm8001_dev_found_notify(dev);
681 }
682 
683 void pm8001_task_done(struct sas_task *task)
684 {
685 	if (!del_timer(&task->slow_task->timer))
686 		return;
687 	complete(&task->slow_task->completion);
688 }
689 
690 static void pm8001_tmf_timedout(struct timer_list *t)
691 {
692 	struct sas_task_slow *slow = from_timer(slow, t, timer);
693 	struct sas_task *task = slow->task;
694 
695 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
696 	complete(&task->slow_task->completion);
697 }
698 
699 #define PM8001_TASK_TIMEOUT 20
700 /**
701   * pm8001_exec_internal_tmf_task - execute some task management commands.
702   * @dev: the wanted device.
703   * @tmf: which task management wanted to be take.
704   * @para_len: para_len.
705   * @parameter: ssp task parameter.
706   *
707   * when errors or exception happened, we may want to do something, for example
708   * abort the issued task which result in this execption, it is done by calling
709   * this function, note it is also with the task execute interface.
710   */
711 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
712 	void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
713 {
714 	int res, retry;
715 	struct sas_task *task = NULL;
716 	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
717 	struct pm8001_device *pm8001_dev = dev->lldd_dev;
718 	DECLARE_COMPLETION_ONSTACK(completion_setstate);
719 
720 	for (retry = 0; retry < 3; retry++) {
721 		task = sas_alloc_slow_task(GFP_KERNEL);
722 		if (!task)
723 			return -ENOMEM;
724 
725 		task->dev = dev;
726 		task->task_proto = dev->tproto;
727 		memcpy(&task->ssp_task, parameter, para_len);
728 		task->task_done = pm8001_task_done;
729 		task->slow_task->timer.function = pm8001_tmf_timedout;
730 		task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
731 		add_timer(&task->slow_task->timer);
732 
733 		res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf);
734 
735 		if (res) {
736 			del_timer(&task->slow_task->timer);
737 			PM8001_FAIL_DBG(pm8001_ha,
738 				pm8001_printk("Executing internal task "
739 				"failed\n"));
740 			goto ex_err;
741 		}
742 		wait_for_completion(&task->slow_task->completion);
743 		if (pm8001_ha->chip_id != chip_8001) {
744 			pm8001_dev->setds_completion = &completion_setstate;
745 			PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
746 				pm8001_dev, 0x01);
747 			wait_for_completion(&completion_setstate);
748 		}
749 		res = -TMF_RESP_FUNC_FAILED;
750 		/* Even TMF timed out, return direct. */
751 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
752 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
753 				PM8001_FAIL_DBG(pm8001_ha,
754 					pm8001_printk("TMF task[%x]timeout.\n",
755 					tmf->tmf));
756 				goto ex_err;
757 			}
758 		}
759 
760 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
761 			task->task_status.stat == SAM_STAT_GOOD) {
762 			res = TMF_RESP_FUNC_COMPLETE;
763 			break;
764 		}
765 
766 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
767 		task->task_status.stat == SAS_DATA_UNDERRUN) {
768 			/* no error, but return the number of bytes of
769 			* underrun */
770 			res = task->task_status.residual;
771 			break;
772 		}
773 
774 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
775 			task->task_status.stat == SAS_DATA_OVERRUN) {
776 			PM8001_FAIL_DBG(pm8001_ha,
777 				pm8001_printk("Blocked task error.\n"));
778 			res = -EMSGSIZE;
779 			break;
780 		} else {
781 			PM8001_EH_DBG(pm8001_ha,
782 				pm8001_printk(" Task to dev %016llx response:"
783 				"0x%x status 0x%x\n",
784 				SAS_ADDR(dev->sas_addr),
785 				task->task_status.resp,
786 				task->task_status.stat));
787 			sas_free_task(task);
788 			task = NULL;
789 		}
790 	}
791 ex_err:
792 	BUG_ON(retry == 3 && task != NULL);
793 	sas_free_task(task);
794 	return res;
795 }
796 
797 static int
798 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
799 	struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
800 	u32 task_tag)
801 {
802 	int res, retry;
803 	u32 ccb_tag;
804 	struct pm8001_ccb_info *ccb;
805 	struct sas_task *task = NULL;
806 
807 	for (retry = 0; retry < 3; retry++) {
808 		task = sas_alloc_slow_task(GFP_KERNEL);
809 		if (!task)
810 			return -ENOMEM;
811 
812 		task->dev = dev;
813 		task->task_proto = dev->tproto;
814 		task->task_done = pm8001_task_done;
815 		task->slow_task->timer.function = pm8001_tmf_timedout;
816 		task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
817 		add_timer(&task->slow_task->timer);
818 
819 		res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
820 		if (res)
821 			goto ex_err;
822 		ccb = &pm8001_ha->ccb_info[ccb_tag];
823 		ccb->device = pm8001_dev;
824 		ccb->ccb_tag = ccb_tag;
825 		ccb->task = task;
826 		ccb->n_elem = 0;
827 
828 		res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
829 			pm8001_dev, flag, task_tag, ccb_tag);
830 
831 		if (res) {
832 			del_timer(&task->slow_task->timer);
833 			PM8001_FAIL_DBG(pm8001_ha,
834 				pm8001_printk("Executing internal task "
835 				"failed\n"));
836 			goto ex_err;
837 		}
838 		wait_for_completion(&task->slow_task->completion);
839 		res = TMF_RESP_FUNC_FAILED;
840 		/* Even TMF timed out, return direct. */
841 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
842 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
843 				PM8001_FAIL_DBG(pm8001_ha,
844 					pm8001_printk("TMF task timeout.\n"));
845 				goto ex_err;
846 			}
847 		}
848 
849 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
850 			task->task_status.stat == SAM_STAT_GOOD) {
851 			res = TMF_RESP_FUNC_COMPLETE;
852 			break;
853 
854 		} else {
855 			PM8001_EH_DBG(pm8001_ha,
856 				pm8001_printk(" Task to dev %016llx response: "
857 					"0x%x status 0x%x\n",
858 				SAS_ADDR(dev->sas_addr),
859 				task->task_status.resp,
860 				task->task_status.stat));
861 			sas_free_task(task);
862 			task = NULL;
863 		}
864 	}
865 ex_err:
866 	BUG_ON(retry == 3 && task != NULL);
867 	sas_free_task(task);
868 	return res;
869 }
870 
871 /**
872   * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
873   * @dev: the device structure which sas layer used.
874   */
875 static void pm8001_dev_gone_notify(struct domain_device *dev)
876 {
877 	unsigned long flags = 0;
878 	struct pm8001_hba_info *pm8001_ha;
879 	struct pm8001_device *pm8001_dev = dev->lldd_dev;
880 
881 	pm8001_ha = pm8001_find_ha_by_dev(dev);
882 	spin_lock_irqsave(&pm8001_ha->lock, flags);
883 	if (pm8001_dev) {
884 		u32 device_id = pm8001_dev->device_id;
885 
886 		PM8001_DISC_DBG(pm8001_ha,
887 			pm8001_printk("found dev[%d:%x] is gone.\n",
888 			pm8001_dev->device_id, pm8001_dev->dev_type));
889 		if (pm8001_dev->running_req) {
890 			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
891 			pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
892 				dev, 1, 0);
893 			while (pm8001_dev->running_req)
894 				msleep(20);
895 			spin_lock_irqsave(&pm8001_ha->lock, flags);
896 		}
897 		PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
898 		pm8001_free_dev(pm8001_dev);
899 	} else {
900 		PM8001_DISC_DBG(pm8001_ha,
901 			pm8001_printk("Found dev has gone.\n"));
902 	}
903 	dev->lldd_dev = NULL;
904 	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
905 }
906 
907 void pm8001_dev_gone(struct domain_device *dev)
908 {
909 	pm8001_dev_gone_notify(dev);
910 }
911 
912 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
913 	u8 *lun, struct pm8001_tmf_task *tmf)
914 {
915 	struct sas_ssp_task ssp_task;
916 	if (!(dev->tproto & SAS_PROTOCOL_SSP))
917 		return TMF_RESP_FUNC_ESUPP;
918 
919 	strncpy((u8 *)&ssp_task.LUN, lun, 8);
920 	return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
921 		tmf);
922 }
923 
924 /* retry commands by ha, by task and/or by device */
925 void pm8001_open_reject_retry(
926 	struct pm8001_hba_info *pm8001_ha,
927 	struct sas_task *task_to_close,
928 	struct pm8001_device *device_to_close)
929 {
930 	int i;
931 	unsigned long flags;
932 
933 	if (pm8001_ha == NULL)
934 		return;
935 
936 	spin_lock_irqsave(&pm8001_ha->lock, flags);
937 
938 	for (i = 0; i < PM8001_MAX_CCB; i++) {
939 		struct sas_task *task;
940 		struct task_status_struct *ts;
941 		struct pm8001_device *pm8001_dev;
942 		unsigned long flags1;
943 		u32 tag;
944 		struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
945 
946 		pm8001_dev = ccb->device;
947 		if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
948 			continue;
949 		if (!device_to_close) {
950 			uintptr_t d = (uintptr_t)pm8001_dev
951 					- (uintptr_t)&pm8001_ha->devices;
952 			if (((d % sizeof(*pm8001_dev)) != 0)
953 			 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
954 				continue;
955 		} else if (pm8001_dev != device_to_close)
956 			continue;
957 		tag = ccb->ccb_tag;
958 		if (!tag || (tag == 0xFFFFFFFF))
959 			continue;
960 		task = ccb->task;
961 		if (!task || !task->task_done)
962 			continue;
963 		if (task_to_close && (task != task_to_close))
964 			continue;
965 		ts = &task->task_status;
966 		ts->resp = SAS_TASK_COMPLETE;
967 		/* Force the midlayer to retry */
968 		ts->stat = SAS_OPEN_REJECT;
969 		ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
970 		if (pm8001_dev)
971 			pm8001_dev->running_req--;
972 		spin_lock_irqsave(&task->task_state_lock, flags1);
973 		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
974 		task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
975 		task->task_state_flags |= SAS_TASK_STATE_DONE;
976 		if (unlikely((task->task_state_flags
977 				& SAS_TASK_STATE_ABORTED))) {
978 			spin_unlock_irqrestore(&task->task_state_lock,
979 				flags1);
980 			pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
981 		} else {
982 			spin_unlock_irqrestore(&task->task_state_lock,
983 				flags1);
984 			pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
985 			mb();/* in order to force CPU ordering */
986 			spin_unlock_irqrestore(&pm8001_ha->lock, flags);
987 			task->task_done(task);
988 			spin_lock_irqsave(&pm8001_ha->lock, flags);
989 		}
990 	}
991 
992 	spin_unlock_irqrestore(&pm8001_ha->lock, flags);
993 }
994 
995 /**
996   * Standard mandates link reset for ATA  (type 0) and hard reset for
997   * SSP (type 1) , only for RECOVERY
998   * @dev: the device structure for the device to reset.
999   */
1000 int pm8001_I_T_nexus_reset(struct domain_device *dev)
1001 {
1002 	int rc = TMF_RESP_FUNC_FAILED;
1003 	struct pm8001_device *pm8001_dev;
1004 	struct pm8001_hba_info *pm8001_ha;
1005 	struct sas_phy *phy;
1006 
1007 	if (!dev || !dev->lldd_dev)
1008 		return -ENODEV;
1009 
1010 	pm8001_dev = dev->lldd_dev;
1011 	pm8001_ha = pm8001_find_ha_by_dev(dev);
1012 	phy = sas_get_local_phy(dev);
1013 
1014 	if (dev_is_sata(dev)) {
1015 		if (scsi_is_sas_phy_local(phy)) {
1016 			rc = 0;
1017 			goto out;
1018 		}
1019 		rc = sas_phy_reset(phy, 1);
1020 		if (rc) {
1021 			PM8001_EH_DBG(pm8001_ha,
1022 			pm8001_printk("phy reset failed for device %x\n"
1023 			"with rc %d\n", pm8001_dev->device_id, rc));
1024 			rc = TMF_RESP_FUNC_FAILED;
1025 			goto out;
1026 		}
1027 		msleep(2000);
1028 		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1029 			dev, 1, 0);
1030 		if (rc) {
1031 			PM8001_EH_DBG(pm8001_ha,
1032 			pm8001_printk("task abort failed %x\n"
1033 			"with rc %d\n", pm8001_dev->device_id, rc));
1034 			rc = TMF_RESP_FUNC_FAILED;
1035 		}
1036 	} else {
1037 		rc = sas_phy_reset(phy, 1);
1038 		msleep(2000);
1039 	}
1040 	PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
1041 		pm8001_dev->device_id, rc));
1042  out:
1043 	sas_put_local_phy(phy);
1044 	return rc;
1045 }
1046 
1047 /*
1048 * This function handle the IT_NEXUS_XXX event or completion
1049 * status code for SSP/SATA/SMP I/O request.
1050 */
1051 int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
1052 {
1053 	int rc = TMF_RESP_FUNC_FAILED;
1054 	struct pm8001_device *pm8001_dev;
1055 	struct pm8001_hba_info *pm8001_ha;
1056 	struct sas_phy *phy;
1057 
1058 	if (!dev || !dev->lldd_dev)
1059 		return -1;
1060 
1061 	pm8001_dev = dev->lldd_dev;
1062 	pm8001_ha = pm8001_find_ha_by_dev(dev);
1063 
1064 	PM8001_EH_DBG(pm8001_ha,
1065 			pm8001_printk("I_T_Nexus handler invoked !!"));
1066 
1067 	phy = sas_get_local_phy(dev);
1068 
1069 	if (dev_is_sata(dev)) {
1070 		DECLARE_COMPLETION_ONSTACK(completion_setstate);
1071 		if (scsi_is_sas_phy_local(phy)) {
1072 			rc = 0;
1073 			goto out;
1074 		}
1075 		/* send internal ssp/sata/smp abort command to FW */
1076 		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1077 							dev, 1, 0);
1078 		msleep(100);
1079 
1080 		/* deregister the target device */
1081 		pm8001_dev_gone_notify(dev);
1082 		msleep(200);
1083 
1084 		/*send phy reset to hard reset target */
1085 		rc = sas_phy_reset(phy, 1);
1086 		msleep(2000);
1087 		pm8001_dev->setds_completion = &completion_setstate;
1088 
1089 		wait_for_completion(&completion_setstate);
1090 	} else {
1091 		/* send internal ssp/sata/smp abort command to FW */
1092 		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1093 							dev, 1, 0);
1094 		msleep(100);
1095 
1096 		/* deregister the target device */
1097 		pm8001_dev_gone_notify(dev);
1098 		msleep(200);
1099 
1100 		/*send phy reset to hard reset target */
1101 		rc = sas_phy_reset(phy, 1);
1102 		msleep(2000);
1103 	}
1104 	PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
1105 		pm8001_dev->device_id, rc));
1106 out:
1107 	sas_put_local_phy(phy);
1108 
1109 	return rc;
1110 }
1111 /* mandatory SAM-3, the task reset the specified LUN*/
1112 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1113 {
1114 	int rc = TMF_RESP_FUNC_FAILED;
1115 	struct pm8001_tmf_task tmf_task;
1116 	struct pm8001_device *pm8001_dev = dev->lldd_dev;
1117 	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1118 	DECLARE_COMPLETION_ONSTACK(completion_setstate);
1119 	if (dev_is_sata(dev)) {
1120 		struct sas_phy *phy = sas_get_local_phy(dev);
1121 		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1122 			dev, 1, 0);
1123 		rc = sas_phy_reset(phy, 1);
1124 		sas_put_local_phy(phy);
1125 		pm8001_dev->setds_completion = &completion_setstate;
1126 		rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1127 			pm8001_dev, 0x01);
1128 		wait_for_completion(&completion_setstate);
1129 	} else {
1130 		tmf_task.tmf = TMF_LU_RESET;
1131 		rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1132 	}
1133 	/* If failed, fall-through I_T_Nexus reset */
1134 	PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
1135 		pm8001_dev->device_id, rc));
1136 	return rc;
1137 }
1138 
1139 /* optional SAM-3 */
1140 int pm8001_query_task(struct sas_task *task)
1141 {
1142 	u32 tag = 0xdeadbeef;
1143 	int i = 0;
1144 	struct scsi_lun lun;
1145 	struct pm8001_tmf_task tmf_task;
1146 	int rc = TMF_RESP_FUNC_FAILED;
1147 	if (unlikely(!task || !task->lldd_task || !task->dev))
1148 		return rc;
1149 
1150 	if (task->task_proto & SAS_PROTOCOL_SSP) {
1151 		struct scsi_cmnd *cmnd = task->uldd_task;
1152 		struct domain_device *dev = task->dev;
1153 		struct pm8001_hba_info *pm8001_ha =
1154 			pm8001_find_ha_by_dev(dev);
1155 
1156 		int_to_scsilun(cmnd->device->lun, &lun);
1157 		rc = pm8001_find_tag(task, &tag);
1158 		if (rc == 0) {
1159 			rc = TMF_RESP_FUNC_FAILED;
1160 			return rc;
1161 		}
1162 		PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
1163 		for (i = 0; i < 16; i++)
1164 			printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
1165 		printk(KERN_INFO "]\n");
1166 		tmf_task.tmf = 	TMF_QUERY_TASK;
1167 		tmf_task.tag_of_task_to_be_managed = tag;
1168 
1169 		rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1170 		switch (rc) {
1171 		/* The task is still in Lun, release it then */
1172 		case TMF_RESP_FUNC_SUCC:
1173 			PM8001_EH_DBG(pm8001_ha,
1174 				pm8001_printk("The task is still in Lun\n"));
1175 			break;
1176 		/* The task is not in Lun or failed, reset the phy */
1177 		case TMF_RESP_FUNC_FAILED:
1178 		case TMF_RESP_FUNC_COMPLETE:
1179 			PM8001_EH_DBG(pm8001_ha,
1180 			pm8001_printk("The task is not in Lun or failed,"
1181 			" reset the phy\n"));
1182 			break;
1183 		}
1184 	}
1185 	pr_err("pm80xx: rc= %d\n", rc);
1186 	return rc;
1187 }
1188 
1189 /*  mandatory SAM-3, still need free task/ccb info, abort the specified task */
1190 int pm8001_abort_task(struct sas_task *task)
1191 {
1192 	unsigned long flags;
1193 	u32 tag;
1194 	struct domain_device *dev ;
1195 	struct pm8001_hba_info *pm8001_ha;
1196 	struct scsi_lun lun;
1197 	struct pm8001_device *pm8001_dev;
1198 	struct pm8001_tmf_task tmf_task;
1199 	int rc = TMF_RESP_FUNC_FAILED, ret;
1200 	u32 phy_id;
1201 	struct sas_task_slow slow_task;
1202 	if (unlikely(!task || !task->lldd_task || !task->dev))
1203 		return TMF_RESP_FUNC_FAILED;
1204 	dev = task->dev;
1205 	pm8001_dev = dev->lldd_dev;
1206 	pm8001_ha = pm8001_find_ha_by_dev(dev);
1207 	phy_id = pm8001_dev->attached_phy;
1208 	ret = pm8001_find_tag(task, &tag);
1209 	if (ret == 0) {
1210 		pm8001_printk("no tag for task:%p\n", task);
1211 		return TMF_RESP_FUNC_FAILED;
1212 	}
1213 	spin_lock_irqsave(&task->task_state_lock, flags);
1214 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1215 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1216 		return TMF_RESP_FUNC_COMPLETE;
1217 	}
1218 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1219 	if (task->slow_task == NULL) {
1220 		init_completion(&slow_task.completion);
1221 		task->slow_task = &slow_task;
1222 	}
1223 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1224 	if (task->task_proto & SAS_PROTOCOL_SSP) {
1225 		struct scsi_cmnd *cmnd = task->uldd_task;
1226 		int_to_scsilun(cmnd->device->lun, &lun);
1227 		tmf_task.tmf = TMF_ABORT_TASK;
1228 		tmf_task.tag_of_task_to_be_managed = tag;
1229 		rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1230 		pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1231 			pm8001_dev->sas_device, 0, tag);
1232 	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
1233 		task->task_proto & SAS_PROTOCOL_STP) {
1234 		if (pm8001_ha->chip_id == chip_8006) {
1235 			DECLARE_COMPLETION_ONSTACK(completion_reset);
1236 			DECLARE_COMPLETION_ONSTACK(completion);
1237 			struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1238 
1239 			/* 1. Set Device state as Recovery */
1240 			pm8001_dev->setds_completion = &completion;
1241 			PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1242 				pm8001_dev, 0x03);
1243 			wait_for_completion(&completion);
1244 
1245 			/* 2. Send Phy Control Hard Reset */
1246 			reinit_completion(&completion);
1247 			phy->port_reset_status = PORT_RESET_TMO;
1248 			phy->reset_success = false;
1249 			phy->enable_completion = &completion;
1250 			phy->reset_completion = &completion_reset;
1251 			ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
1252 				PHY_HARD_RESET);
1253 			if (ret) {
1254 				phy->enable_completion = NULL;
1255 				phy->reset_completion = NULL;
1256 				goto out;
1257 			}
1258 
1259 			/* In the case of the reset timeout/fail we still
1260 			 * abort the command at the firmware. The assumption
1261 			 * here is that the drive is off doing something so
1262 			 * that it's not processing requests, and we want to
1263 			 * avoid getting a completion for this and either
1264 			 * leaking the task in libsas or losing the race and
1265 			 * getting a double free.
1266 			 */
1267 			PM8001_MSG_DBG(pm8001_ha,
1268 				pm8001_printk("Waiting for local phy ctl\n"));
1269 			ret = wait_for_completion_timeout(&completion,
1270 					PM8001_TASK_TIMEOUT * HZ);
1271 			if (!ret || !phy->reset_success) {
1272 				phy->enable_completion = NULL;
1273 				phy->reset_completion = NULL;
1274 			} else {
1275 				/* 3. Wait for Port Reset complete or
1276 				 * Port reset TMO
1277 				 */
1278 				PM8001_MSG_DBG(pm8001_ha,
1279 				pm8001_printk("Waiting for Port reset\n"));
1280 				ret = wait_for_completion_timeout(
1281 					&completion_reset,
1282 					PM8001_TASK_TIMEOUT * HZ);
1283 				if (!ret)
1284 					phy->reset_completion = NULL;
1285 				WARN_ON(phy->port_reset_status ==
1286 						PORT_RESET_TMO);
1287 				if (phy->port_reset_status == PORT_RESET_TMO) {
1288 					pm8001_dev_gone_notify(dev);
1289 					goto out;
1290 				}
1291 			}
1292 
1293 			/*
1294 			 * 4. SATA Abort ALL
1295 			 * we wait for the task to be aborted so that the task
1296 			 * is removed from the ccb. on success the caller is
1297 			 * going to free the task.
1298 			 */
1299 			ret = pm8001_exec_internal_task_abort(pm8001_ha,
1300 				pm8001_dev, pm8001_dev->sas_device, 1, tag);
1301 			if (ret)
1302 				goto out;
1303 			ret = wait_for_completion_timeout(
1304 				&task->slow_task->completion,
1305 				PM8001_TASK_TIMEOUT * HZ);
1306 			if (!ret)
1307 				goto out;
1308 
1309 			/* 5. Set Device State as Operational */
1310 			reinit_completion(&completion);
1311 			pm8001_dev->setds_completion = &completion;
1312 			PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1313 				pm8001_dev, 0x01);
1314 			wait_for_completion(&completion);
1315 		} else {
1316 			rc = pm8001_exec_internal_task_abort(pm8001_ha,
1317 				pm8001_dev, pm8001_dev->sas_device, 0, tag);
1318 		}
1319 		rc = TMF_RESP_FUNC_COMPLETE;
1320 	} else if (task->task_proto & SAS_PROTOCOL_SMP) {
1321 		/* SMP */
1322 		rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1323 			pm8001_dev->sas_device, 0, tag);
1324 
1325 	}
1326 out:
1327 	spin_lock_irqsave(&task->task_state_lock, flags);
1328 	if (task->slow_task == &slow_task)
1329 		task->slow_task = NULL;
1330 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1331 	if (rc != TMF_RESP_FUNC_COMPLETE)
1332 		pm8001_printk("rc= %d\n", rc);
1333 	return rc;
1334 }
1335 
1336 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1337 {
1338 	struct pm8001_tmf_task tmf_task;
1339 
1340 	tmf_task.tmf = TMF_ABORT_TASK_SET;
1341 	return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1342 }
1343 
1344 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1345 {
1346 	struct pm8001_tmf_task tmf_task;
1347 
1348 	tmf_task.tmf = TMF_CLEAR_ACA;
1349 	return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1350 }
1351 
1352 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1353 {
1354 	struct pm8001_tmf_task tmf_task;
1355 	struct pm8001_device *pm8001_dev = dev->lldd_dev;
1356 	struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1357 
1358 	PM8001_EH_DBG(pm8001_ha,
1359 		pm8001_printk("I_T_L_Q clear task set[%x]\n",
1360 		pm8001_dev->device_id));
1361 	tmf_task.tmf = TMF_CLEAR_TASK_SET;
1362 	return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1363 }
1364 
1365