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