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