blob: 505a885b4c77a20114dd28342931d3202ddcc4a1 [file] [log] [blame]
jack wangdbf9bfe2009-10-14 16:19:21 +08001/*
Sakthivel Kf5860992013-04-17 16:37:02 +05302 * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
jack wangdbf9bfe2009-10-14 16:19:21 +08003 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
jack wangdbf9bfe2009-10-14 16:19:21 +080042#include "pm8001_sas.h"
43
44/**
45 * pm8001_find_tag - from sas task to find out tag that belongs to this task
46 * @task: the task sent to the LLDD
47 * @tag: the found tag associated with the task
48 */
49static int pm8001_find_tag(struct sas_task *task, u32 *tag)
50{
51 if (task->lldd_task) {
52 struct pm8001_ccb_info *ccb;
53 ccb = task->lldd_task;
54 *tag = ccb->ccb_tag;
55 return 1;
56 }
57 return 0;
58}
59
60/**
Tomas Henzlef300542014-07-09 17:20:40 +053061 * pm8001_tag_free - free the no more needed tag
jack wangdbf9bfe2009-10-14 16:19:21 +080062 * @pm8001_ha: our hba struct
63 * @tag: the found tag associated with the task
64 */
Tomas Henzlef300542014-07-09 17:20:40 +053065void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
jack wangdbf9bfe2009-10-14 16:19:21 +080066{
67 void *bitmap = pm8001_ha->tags;
68 clear_bit(tag, bitmap);
69}
70
jack wangdbf9bfe2009-10-14 16:19:21 +080071/**
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 */
76inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
77{
Tomas Henzlef300542014-07-09 17:20:40 +053078 unsigned int tag;
jack wangdbf9bfe2009-10-14 16:19:21 +080079 void *bitmap = pm8001_ha->tags;
Tomas Henzl646cdf02014-07-09 17:21:01 +053080 unsigned long flags;
jack wangdbf9bfe2009-10-14 16:19:21 +080081
Tomas Henzl646cdf02014-07-09 17:21:01 +053082 spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
Tomas Henzlef300542014-07-09 17:20:40 +053083 tag = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
Tomas Henzl646cdf02014-07-09 17:21:01 +053084 if (tag >= pm8001_ha->tags_num) {
85 spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
jack wangdbf9bfe2009-10-14 16:19:21 +080086 return -SAS_QUEUE_FULL;
Tomas Henzl646cdf02014-07-09 17:21:01 +053087 }
Tomas Henzlef300542014-07-09 17:20:40 +053088 set_bit(tag, bitmap);
Tomas Henzl646cdf02014-07-09 17:21:01 +053089 spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
jack wangdbf9bfe2009-10-14 16:19:21 +080090 *tag_out = tag;
91 return 0;
92}
93
94void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
95{
96 int i;
97 for (i = 0; i < pm8001_ha->tags_num; ++i)
Tomas Henzlef300542014-07-09 17:20:40 +053098 pm8001_tag_free(pm8001_ha, i);
jack wangdbf9bfe2009-10-14 16:19:21 +080099}
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 */
109int 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;
Luis Chamberlain750afb02019-01-04 09:23:09 +0100119 mem_virt_alloc = dma_alloc_coherent(&pdev->dev, mem_size + align,
120 &mem_dma_handle, GFP_KERNEL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800121 if (!mem_virt_alloc) {
peter chang73706722019-11-14 15:39:02 +0530122 pr_err("pm80xx: memory allocation error\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800123 return -1;
124 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800125 *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}
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100132
jack wangdbf9bfe2009-10-14 16:19:21 +0800133/**
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 */
138static
139struct 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 */
155int 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;
Deepak Ukeycd135752018-09-11 14:18:02 +0530161 struct sas_ha_struct *sas_ha;
162 struct pm8001_phy *phy;
jack wangdbf9bfe2009-10-14 16:19:21 +0800163 DECLARE_COMPLETION_ONSTACK(completion);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500164 unsigned long flags;
jack wangdbf9bfe2009-10-14 16:19:21 +0800165 pm8001_ha = sas_phy->ha->lldd_ha;
Deepak Ukeycd135752018-09-11 14:18:02 +0530166 phy = &pm8001_ha->phy[phy_id];
jack wangdbf9bfe2009-10-14 16:19:21 +0800167 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 }
Deepak Ukeycd135752018-09-11 14:18:02 +0530179 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800180 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:
Deepak Ukeycd135752018-09-11 14:18:02 +0530187 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800188 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:
Deepak Ukeycd135752018-09-11 14:18:02 +0530195 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800196 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:
Deepak Ukeycd135752018-09-11 14:18:02 +0530207 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 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800226 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
227 break;
Mark Salyzynd95d0002012-01-17 09:18:57 -0500228 case PHY_FUNC_GET_EVENTS:
229 spin_lock_irqsave(&pm8001_ha->lock, flags);
Sakthivel Kf5860992013-04-17 16:37:02 +0530230 if (pm8001_ha->chip_id == chip_8001) {
231 if (-1 == pm8001_bar4_shift(pm8001_ha,
Mark Salyzynd95d0002012-01-17 09:18:57 -0500232 (phy_id < 4) ? 0x30000 : 0x40000)) {
Sakthivel Kf5860992013-04-17 16:37:02 +0530233 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
234 return -EINVAL;
235 }
Mark Salyzynd95d0002012-01-17 09:18:57 -0500236 }
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 }
Sakthivel Kf5860992013-04-17 16:37:02 +0530248 if (pm8001_ha->chip_id == chip_8001)
249 pm8001_bar4_shift(pm8001_ha, 0);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500250 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
251 return 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800252 default:
Joe Perches1b5d2792020-11-20 15:16:09 -0800253 pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500254 rc = -EOPNOTSUPP;
jack wangdbf9bfe2009-10-14 16:19:21 +0800255 }
256 msleep(300);
257 return rc;
258}
259
jack wangdbf9bfe2009-10-14 16:19:21 +0800260/**
261 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
262 * command to HBA.
263 * @shost: the scsi host data.
264 */
265void pm8001_scan_start(struct Scsi_Host *shost)
266{
267 int i;
268 struct pm8001_hba_info *pm8001_ha;
269 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
270 pm8001_ha = sha->lldd_ha;
Sakthivel Kf5860992013-04-17 16:37:02 +0530271 /* SAS_RE_INITIALIZATION not available in SPCv/ve */
272 if (pm8001_ha->chip_id == chip_8001)
273 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800274 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
275 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
276}
277
278int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
279{
Dan Williamsb1124cd2011-12-19 16:42:34 -0800280 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
281
jack wangdbf9bfe2009-10-14 16:19:21 +0800282 /* give the phy enabling interrupt event time to come in (1s
283 * is empirically about all it takes) */
284 if (time < HZ)
285 return 0;
286 /* Wait for discovery to finish */
Dan Williamsb1124cd2011-12-19 16:42:34 -0800287 sas_drain_work(ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800288 return 1;
289}
290
291/**
292 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
293 * @pm8001_ha: our hba card information
294 * @ccb: the ccb which attached to smp task
295 */
296static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
297 struct pm8001_ccb_info *ccb)
298{
299 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
300}
301
302u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
303{
304 struct ata_queued_cmd *qc = task->uldd_task;
305 if (qc) {
306 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
Hannes Reineckeef026b12016-04-25 12:45:44 +0200307 qc->tf.command == ATA_CMD_FPDMA_READ ||
308 qc->tf.command == ATA_CMD_FPDMA_RECV ||
Hannes Reinecke661ce1f2016-04-25 12:45:45 +0200309 qc->tf.command == ATA_CMD_FPDMA_SEND ||
310 qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800311 *tag = qc->tag;
312 return 1;
313 }
314 }
315 return 0;
316}
317
318/**
319 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
320 * @pm8001_ha: our hba card information
321 * @ccb: the ccb which attached to sata task
322 */
323static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
324 struct pm8001_ccb_info *ccb)
325{
326 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
327}
328
329/**
330 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
331 * @pm8001_ha: our hba card information
332 * @ccb: the ccb which attached to TM
333 * @tmf: the task management IU
334 */
335static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
336 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
337{
338 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
339}
340
341/**
342 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
343 * @pm8001_ha: our hba card information
344 * @ccb: the ccb which attached to ssp task
345 */
346static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
347 struct pm8001_ccb_info *ccb)
348{
349 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
350}
Dan Williams11e16362011-09-20 15:11:03 -0700351
jack wang1cc943a2009-12-07 17:22:42 +0800352 /* Find the local port id that's attached to this device */
353static int sas_find_local_port_id(struct domain_device *dev)
354{
355 struct domain_device *pdev = dev->parent;
356
357 /* Directly attached device */
358 if (!pdev)
359 return dev->port->id;
360 while (pdev) {
361 struct domain_device *pdev_p = pdev->parent;
362 if (!pdev_p)
363 return pdev->port->id;
364 pdev = pdev->parent;
365 }
366 return 0;
367}
368
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100369#define DEV_IS_GONE(pm8001_dev) \
370 ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
jack wangdbf9bfe2009-10-14 16:19:21 +0800371/**
jack_wang97ee2082009-11-05 22:33:51 +0800372 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
jack wangdbf9bfe2009-10-14 16:19:21 +0800373 * @task: the task to be execute.
jack wangdbf9bfe2009-10-14 16:19:21 +0800374 * @gfp_flags: gfp_flags.
jack_wang97ee2082009-11-05 22:33:51 +0800375 * @is_tmf: if it is task management task.
jack wangdbf9bfe2009-10-14 16:19:21 +0800376 * @tmf: the task management IU
377 */
Christoph Hellwig79855d12014-11-05 10:36:28 +0100378static int pm8001_task_exec(struct sas_task *task,
jack wangdbf9bfe2009-10-14 16:19:21 +0800379 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
380{
381 struct domain_device *dev = task->dev;
382 struct pm8001_hba_info *pm8001_ha;
383 struct pm8001_device *pm8001_dev;
jack wang1cc943a2009-12-07 17:22:42 +0800384 struct pm8001_port *port = NULL;
jack wangdbf9bfe2009-10-14 16:19:21 +0800385 struct sas_task *t = task;
386 struct pm8001_ccb_info *ccb;
John Sperbeckcef15382019-11-14 15:39:00 +0530387 u32 tag = 0xdeadbeef, rc = 0, n_elem = 0;
Dan Williams312d3e52011-11-17 17:59:50 -0800388 unsigned long flags = 0;
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530389 enum sas_protocol task_proto = t->task_proto;
jack wangdbf9bfe2009-10-14 16:19:21 +0800390
391 if (!dev->port) {
392 struct task_status_struct *tsm = &t->task_status;
393 tsm->resp = SAS_TASK_UNDELIVERED;
394 tsm->stat = SAS_PHY_DOWN;
James Bottomleyaa9f8322013-05-07 14:44:06 -0700395 if (dev->dev_type != SAS_SATA_DEV)
jack wangdbf9bfe2009-10-14 16:19:21 +0800396 t->task_done(t);
397 return 0;
398 }
399 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
Deepak Ukey72349b62018-09-11 14:18:04 +0530400 if (pm8001_ha->controller_fatal_error) {
401 struct task_status_struct *ts = &t->task_status;
402
403 ts->resp = SAS_TASK_UNDELIVERED;
404 t->task_done(t);
405 return 0;
406 }
Joe Perches1b5d2792020-11-20 15:16:09 -0800407 pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800408 spin_lock_irqsave(&pm8001_ha->lock, flags);
409 do {
410 dev = t->dev;
411 pm8001_dev = dev->lldd_dev;
jack wang1cc943a2009-12-07 17:22:42 +0800412 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
Mark Salyzynb90b3782011-09-22 08:50:01 -0700413 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530414 if (sas_protocol_ata(task_proto)) {
jack wang1cc943a2009-12-07 17:22:42 +0800415 struct task_status_struct *ts = &t->task_status;
416 ts->resp = SAS_TASK_UNDELIVERED;
417 ts->stat = SAS_PHY_DOWN;
418
419 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
jack wang1cc943a2009-12-07 17:22:42 +0800420 t->task_done(t);
jack wang1cc943a2009-12-07 17:22:42 +0800421 spin_lock_irqsave(&pm8001_ha->lock, flags);
jack wang1cc943a2009-12-07 17:22:42 +0800422 continue;
423 } else {
424 struct task_status_struct *ts = &t->task_status;
425 ts->resp = SAS_TASK_UNDELIVERED;
426 ts->stat = SAS_PHY_DOWN;
427 t->task_done(t);
jack wang1cc943a2009-12-07 17:22:42 +0800428 continue;
429 }
430 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800431 rc = pm8001_tag_alloc(pm8001_ha, &tag);
432 if (rc)
433 goto err_out;
434 ccb = &pm8001_ha->ccb_info[tag];
435
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530436 if (!sas_protocol_ata(task_proto)) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800437 if (t->num_scatter) {
438 n_elem = dma_map_sg(pm8001_ha->dev,
439 t->scatter,
440 t->num_scatter,
441 t->data_dir);
442 if (!n_elem) {
443 rc = -ENOMEM;
jack_wang97ee2082009-11-05 22:33:51 +0800444 goto err_out_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800445 }
446 }
447 } else {
448 n_elem = t->num_scatter;
449 }
450
jack_wang97ee2082009-11-05 22:33:51 +0800451 t->lldd_task = ccb;
jack wangdbf9bfe2009-10-14 16:19:21 +0800452 ccb->n_elem = n_elem;
453 ccb->ccb_tag = tag;
454 ccb->task = t;
XinHong Zhuaed97b82014-02-14 16:01:28 +0800455 ccb->device = pm8001_dev;
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530456 switch (task_proto) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800457 case SAS_PROTOCOL_SMP:
Viswas G4a2efd42020-11-02 22:25:26 +0530458 atomic_inc(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800459 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
460 break;
461 case SAS_PROTOCOL_SSP:
Viswas G4a2efd42020-11-02 22:25:26 +0530462 atomic_inc(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800463 if (is_tmf)
464 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
465 ccb, tmf);
466 else
467 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
468 break;
469 case SAS_PROTOCOL_SATA:
470 case SAS_PROTOCOL_STP:
Viswas G4a2efd42020-11-02 22:25:26 +0530471 atomic_inc(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800472 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
473 break;
474 default:
475 dev_printk(KERN_ERR, pm8001_ha->dev,
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530476 "unknown sas_task proto: 0x%x\n", task_proto);
jack wangdbf9bfe2009-10-14 16:19:21 +0800477 rc = -EINVAL;
478 break;
479 }
480
481 if (rc) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800482 pm8001_dbg(pm8001_ha, IO, "rc is %x\n", rc);
Viswas G4a2efd42020-11-02 22:25:26 +0530483 atomic_dec(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800484 goto err_out_tag;
485 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800486 /* TODO: select normal or high priority */
487 spin_lock(&t->task_state_lock);
488 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
489 spin_unlock(&t->task_state_lock);
Christoph Hellwig79855d12014-11-05 10:36:28 +0100490 } while (0);
jack wangdbf9bfe2009-10-14 16:19:21 +0800491 rc = 0;
492 goto out_done;
493
494err_out_tag:
495 pm8001_tag_free(pm8001_ha, tag);
496err_out:
497 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530498 if (!sas_protocol_ata(task_proto))
jack wangdbf9bfe2009-10-14 16:19:21 +0800499 if (n_elem)
Deepak Ukey76cb25b2018-09-11 14:18:03 +0530500 dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter,
jack wangdbf9bfe2009-10-14 16:19:21 +0800501 t->data_dir);
502out_done:
503 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
504 return rc;
505}
506
507/**
508 * pm8001_queue_command - register for upper layer used, all IO commands sent
509 * to HBA are from this interface.
510 * @task: the task to be execute.
jack wangdbf9bfe2009-10-14 16:19:21 +0800511 * @gfp_flags: gfp_flags
512 */
Christoph Hellwig79855d12014-11-05 10:36:28 +0100513int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
jack wangdbf9bfe2009-10-14 16:19:21 +0800514{
Christoph Hellwig79855d12014-11-05 10:36:28 +0100515 return pm8001_task_exec(task, gfp_flags, 0, NULL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800516}
517
jack wangdbf9bfe2009-10-14 16:19:21 +0800518/**
519 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
520 * @pm8001_ha: our hba card information
521 * @ccb: the ccb which attached to ssp task
522 * @task: the task to be free.
523 * @ccb_idx: ccb index.
524 */
525void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
526 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
527{
528 if (!ccb->task)
529 return;
530 if (!sas_protocol_ata(task->task_proto))
531 if (ccb->n_elem)
532 dma_unmap_sg(pm8001_ha->dev, task->scatter,
533 task->num_scatter, task->data_dir);
534
535 switch (task->task_proto) {
536 case SAS_PROTOCOL_SMP:
537 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
Christoph Hellwigf73bdeb2018-10-10 19:59:50 +0200538 DMA_FROM_DEVICE);
jack wangdbf9bfe2009-10-14 16:19:21 +0800539 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
Christoph Hellwigf73bdeb2018-10-10 19:59:50 +0200540 DMA_TO_DEVICE);
jack wangdbf9bfe2009-10-14 16:19:21 +0800541 break;
542
543 case SAS_PROTOCOL_SATA:
544 case SAS_PROTOCOL_STP:
545 case SAS_PROTOCOL_SSP:
546 default:
547 /* do nothing */
548 break;
549 }
550 task->lldd_task = NULL;
551 ccb->task = NULL;
552 ccb->ccb_tag = 0xFFFFFFFF;
Mark Salyzyn5954d732012-01-17 11:52:24 -0500553 ccb->open_retry = 0;
Tomas Henzlef300542014-07-09 17:20:40 +0530554 pm8001_tag_free(pm8001_ha, ccb_idx);
jack wangdbf9bfe2009-10-14 16:19:21 +0800555}
556
557 /**
jack_wang97ee2082009-11-05 22:33:51 +0800558 * pm8001_alloc_dev - find a empty pm8001_device
jack wangdbf9bfe2009-10-14 16:19:21 +0800559 * @pm8001_ha: our hba card information
560 */
Baoyou Xie7efa59e2016-09-23 21:54:22 +0800561static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
jack wangdbf9bfe2009-10-14 16:19:21 +0800562{
563 u32 dev;
564 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
James Bottomleyaa9f8322013-05-07 14:44:06 -0700565 if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800566 pm8001_ha->devices[dev].id = dev;
567 return &pm8001_ha->devices[dev];
568 }
569 }
570 if (dev == PM8001_MAX_DEVICES) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800571 pm8001_dbg(pm8001_ha, FAIL,
572 "max support %d devices, ignore ..\n",
573 PM8001_MAX_DEVICES);
jack wangdbf9bfe2009-10-14 16:19:21 +0800574 }
575 return NULL;
576}
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530577/**
578 * pm8001_find_dev - find a matching pm8001_device
579 * @pm8001_ha: our hba card information
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100580 * @device_id: device ID to match against
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530581 */
582struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
583 u32 device_id)
584{
585 u32 dev;
586 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
587 if (pm8001_ha->devices[dev].device_id == device_id)
588 return &pm8001_ha->devices[dev];
589 }
590 if (dev == PM8001_MAX_DEVICES) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800591 pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530592 }
593 return NULL;
594}
jack wangdbf9bfe2009-10-14 16:19:21 +0800595
596static 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;
James Bottomleyaa9f8322013-05-07 14:44:06 -0700601 pm8001_dev->dev_type = SAS_PHY_UNUSED;
jack wangdbf9bfe2009-10-14 16:19:21 +0800602 pm8001_dev->device_id = PM8001_MAX_DEVICES;
603 pm8001_dev->sas_device = NULL;
604}
605
606/**
jack_wang97ee2082009-11-05 22:33:51 +0800607 * 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
jack wangdbf9bfe2009-10-14 16:19:21 +0800614 * now on, we communicate with HBA FW with the device ID which HBA assigned
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800615 * rather than sas address. it is the necessary step for our HBA but it is
jack wangdbf9bfe2009-10-14 16:19:21 +0800616 * the optional for other HBA driver.
jack wangdbf9bfe2009-10-14 16:19:21 +0800617 */
618static 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);
jack wangdbf9bfe2009-10-14 16:19:21 +0800631 if (!pm8001_device) {
632 res = -1;
633 goto found_out;
634 }
jack wangf01f4e62009-12-07 17:22:55 +0800635 pm8001_device->sas_device = dev;
jack wangdbf9bfe2009-10-14 16:19:21 +0800636 dev->lldd_dev = pm8001_device;
637 pm8001_device->dev_type = dev->dev_type;
638 pm8001_device->dcompletion = &completion;
John Garry924a3542019-06-10 20:41:41 +0800639 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800640 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) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800652 pm8001_dbg(pm8001_ha, FAIL,
653 "Error: no attached dev:%016llx at ex:%016llx.\n",
654 SAS_ADDR(dev->sas_addr),
655 SAS_ADDR(parent_dev->sas_addr));
jack wangdbf9bfe2009-10-14 16:19:21 +0800656 res = -1;
657 }
658 } else {
James Bottomleyaa9f8322013-05-07 14:44:06 -0700659 if (dev->dev_type == SAS_SATA_DEV) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800660 pm8001_device->attached_phy =
661 dev->rphy->identify.phy_identifier;
Julia Lawall251f24e2018-12-30 16:53:13 +0100662 flag = 1; /* directly sata */
jack wangdbf9bfe2009-10-14 16:19:21 +0800663 }
664 } /*register this device to HBA*/
Joe Perches1b5d2792020-11-20 15:16:09 -0800665 pm8001_dbg(pm8001_ha, DISC, "Found device\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800666 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
667 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
668 wait_for_completion(&completion);
James Bottomleyaa9f8322013-05-07 14:44:06 -0700669 if (dev->dev_type == SAS_END_DEVICE)
jack wangdbf9bfe2009-10-14 16:19:21 +0800670 msleep(50);
Mark Salyzyn5c4fb762012-01-17 12:56:45 -0500671 pm8001_ha->flags = PM8001F_RUN_TIME;
jack wangdbf9bfe2009-10-14 16:19:21 +0800672 return 0;
673found_out:
674 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
675 return res;
676}
677
678int pm8001_dev_found(struct domain_device *dev)
679{
680 return pm8001_dev_found_notify(dev);
681}
682
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530683void pm8001_task_done(struct sas_task *task)
jack wangdbf9bfe2009-10-14 16:19:21 +0800684{
Dan Williamsf0bf7502012-06-21 23:36:30 -0700685 if (!del_timer(&task->slow_task->timer))
jack wangdbf9bfe2009-10-14 16:19:21 +0800686 return;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700687 complete(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800688}
689
Kees Cook77570ee2017-08-22 16:05:14 -0700690static void pm8001_tmf_timedout(struct timer_list *t)
jack wangdbf9bfe2009-10-14 16:19:21 +0800691{
Kees Cook77570ee2017-08-22 16:05:14 -0700692 struct sas_task_slow *slow = from_timer(slow, t, timer);
693 struct sas_task *task = slow->task;
jack wangdbf9bfe2009-10-14 16:19:21 +0800694
695 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700696 complete(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800697}
698
699#define PM8001_TASK_TIMEOUT 20
700/**
jack_wang97ee2082009-11-05 22:33:51 +0800701 * pm8001_exec_internal_tmf_task - execute some task management commands.
jack wangdbf9bfe2009-10-14 16:19:21 +0800702 * @dev: the wanted device.
703 * @tmf: which task management wanted to be take.
704 * @para_len: para_len.
705 * @parameter: ssp task parameter.
jack_wang97ee2082009-11-05 22:33:51 +0800706 *
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.
jack wangdbf9bfe2009-10-14 16:19:21 +0800710 */
711static 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);
Anand Kumar Santhaname912457b2013-09-17 16:58:10 +0530717 struct pm8001_device *pm8001_dev = dev->lldd_dev;
718 DECLARE_COMPLETION_ONSTACK(completion_setstate);
jack wangdbf9bfe2009-10-14 16:19:21 +0800719
720 for (retry = 0; retry < 3; retry++) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700721 task = sas_alloc_slow_task(GFP_KERNEL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800722 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;
Kees Cook841b86f2017-10-23 09:40:42 +0200729 task->slow_task->timer.function = pm8001_tmf_timedout;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700730 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
731 add_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800732
Christoph Hellwig79855d12014-11-05 10:36:28 +0100733 res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf);
jack wangdbf9bfe2009-10-14 16:19:21 +0800734
735 if (res) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700736 del_timer(&task->slow_task->timer);
Joe Perches1b5d2792020-11-20 15:16:09 -0800737 pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800738 goto ex_err;
739 }
Dan Williamsf0bf7502012-06-21 23:36:30 -0700740 wait_for_completion(&task->slow_task->completion);
Anand Kumar Santhaname912457b2013-09-17 16:58:10 +0530741 if (pm8001_ha->chip_id != chip_8001) {
742 pm8001_dev->setds_completion = &completion_setstate;
Colin Ian King9e2a07e2019-03-17 18:15:32 +0000743 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
744 pm8001_dev, 0x01);
Anand Kumar Santhaname912457b2013-09-17 16:58:10 +0530745 wait_for_completion(&completion_setstate);
746 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800747 res = -TMF_RESP_FUNC_FAILED;
748 /* Even TMF timed out, return direct. */
749 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
750 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800751 pm8001_dbg(pm8001_ha, FAIL,
752 "TMF task[%x]timeout.\n",
753 tmf->tmf);
jack wangdbf9bfe2009-10-14 16:19:21 +0800754 goto ex_err;
755 }
756 }
757
758 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500759 task->task_status.stat == SAM_STAT_GOOD) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800760 res = TMF_RESP_FUNC_COMPLETE;
761 break;
762 }
763
764 if (task->task_status.resp == SAS_TASK_COMPLETE &&
765 task->task_status.stat == SAS_DATA_UNDERRUN) {
766 /* no error, but return the number of bytes of
767 * underrun */
768 res = task->task_status.residual;
769 break;
770 }
771
772 if (task->task_status.resp == SAS_TASK_COMPLETE &&
773 task->task_status.stat == SAS_DATA_OVERRUN) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800774 pm8001_dbg(pm8001_ha, FAIL, "Blocked task error.\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800775 res = -EMSGSIZE;
776 break;
777 } else {
Joe Perches1b5d2792020-11-20 15:16:09 -0800778 pm8001_dbg(pm8001_ha, EH,
779 " Task to dev %016llx response:0x%x status 0x%x\n",
780 SAS_ADDR(dev->sas_addr),
781 task->task_status.resp,
782 task->task_status.stat);
Dan Williams4fcf8122011-07-29 17:26:39 -0700783 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800784 task = NULL;
785 }
786 }
787ex_err:
788 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -0700789 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800790 return res;
791}
792
793static int
794pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
795 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
796 u32 task_tag)
797{
798 int res, retry;
jack_wang97ee2082009-11-05 22:33:51 +0800799 u32 ccb_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800800 struct pm8001_ccb_info *ccb;
801 struct sas_task *task = NULL;
802
803 for (retry = 0; retry < 3; retry++) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700804 task = sas_alloc_slow_task(GFP_KERNEL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800805 if (!task)
806 return -ENOMEM;
807
808 task->dev = dev;
809 task->task_proto = dev->tproto;
810 task->task_done = pm8001_task_done;
Kees Cook841b86f2017-10-23 09:40:42 +0200811 task->slow_task->timer.function = pm8001_tmf_timedout;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700812 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
813 add_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800814
jack_wang97ee2082009-11-05 22:33:51 +0800815 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
816 if (res)
Dinghao Liuea403fd2020-08-23 17:14:53 +0800817 goto ex_err;
jack wangdbf9bfe2009-10-14 16:19:21 +0800818 ccb = &pm8001_ha->ccb_info[ccb_tag];
819 ccb->device = pm8001_dev;
820 ccb->ccb_tag = ccb_tag;
821 ccb->task = task;
Viswas G3b700e32015-08-11 15:06:28 +0530822 ccb->n_elem = 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800823
824 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
825 pm8001_dev, flag, task_tag, ccb_tag);
826
827 if (res) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700828 del_timer(&task->slow_task->timer);
Joe Perches1b5d2792020-11-20 15:16:09 -0800829 pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800830 goto ex_err;
831 }
Dan Williamsf0bf7502012-06-21 23:36:30 -0700832 wait_for_completion(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800833 res = TMF_RESP_FUNC_FAILED;
834 /* Even TMF timed out, return direct. */
835 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
836 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800837 pm8001_dbg(pm8001_ha, FAIL,
838 "TMF task timeout.\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800839 goto ex_err;
840 }
841 }
842
843 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500844 task->task_status.stat == SAM_STAT_GOOD) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800845 res = TMF_RESP_FUNC_COMPLETE;
846 break;
847
848 } else {
Joe Perches1b5d2792020-11-20 15:16:09 -0800849 pm8001_dbg(pm8001_ha, EH,
850 " Task to dev %016llx response: 0x%x status 0x%x\n",
851 SAS_ADDR(dev->sas_addr),
852 task->task_status.resp,
853 task->task_status.stat);
Dan Williams4fcf8122011-07-29 17:26:39 -0700854 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800855 task = NULL;
856 }
857 }
858ex_err:
859 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -0700860 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800861 return res;
862}
863
864/**
865 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
866 * @dev: the device structure which sas layer used.
867 */
868static void pm8001_dev_gone_notify(struct domain_device *dev)
869{
870 unsigned long flags = 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800871 struct pm8001_hba_info *pm8001_ha;
872 struct pm8001_device *pm8001_dev = dev->lldd_dev;
Dan Carpenter2471b892010-04-23 14:01:04 +0200873
jack wangdbf9bfe2009-10-14 16:19:21 +0800874 pm8001_ha = pm8001_find_ha_by_dev(dev);
875 spin_lock_irqsave(&pm8001_ha->lock, flags);
jack wangdbf9bfe2009-10-14 16:19:21 +0800876 if (pm8001_dev) {
Dan Carpenter2471b892010-04-23 14:01:04 +0200877 u32 device_id = pm8001_dev->device_id;
878
Joe Perches1b5d2792020-11-20 15:16:09 -0800879 pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
880 pm8001_dev->device_id, pm8001_dev->dev_type);
Viswas G4a2efd42020-11-02 22:25:26 +0530881 if (atomic_read(&pm8001_dev->running_req)) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800882 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
883 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
884 dev, 1, 0);
Viswas G4a2efd42020-11-02 22:25:26 +0530885 while (atomic_read(&pm8001_dev->running_req))
Deepak Ukey196ba662019-07-09 15:30:48 +0530886 msleep(20);
jack wangdbf9bfe2009-10-14 16:19:21 +0800887 spin_lock_irqsave(&pm8001_ha->lock, flags);
888 }
889 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
890 pm8001_free_dev(pm8001_dev);
891 } else {
Joe Perches1b5d2792020-11-20 15:16:09 -0800892 pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800893 }
894 dev->lldd_dev = NULL;
895 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
896}
897
898void pm8001_dev_gone(struct domain_device *dev)
899{
900 pm8001_dev_gone_notify(dev);
901}
902
903static int pm8001_issue_ssp_tmf(struct domain_device *dev,
904 u8 *lun, struct pm8001_tmf_task *tmf)
905{
906 struct sas_ssp_task ssp_task;
907 if (!(dev->tproto & SAS_PROTOCOL_SSP))
908 return TMF_RESP_FUNC_ESUPP;
909
Lee Jones412b51f2020-11-02 10:25:43 +0000910 memcpy((u8 *)&ssp_task.LUN, lun, 8);
jack wangdbf9bfe2009-10-14 16:19:21 +0800911 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
912 tmf);
913}
914
Mark Salyzyn5954d732012-01-17 11:52:24 -0500915/* retry commands by ha, by task and/or by device */
916void pm8001_open_reject_retry(
917 struct pm8001_hba_info *pm8001_ha,
918 struct sas_task *task_to_close,
919 struct pm8001_device *device_to_close)
920{
921 int i;
922 unsigned long flags;
923
924 if (pm8001_ha == NULL)
925 return;
926
927 spin_lock_irqsave(&pm8001_ha->lock, flags);
928
929 for (i = 0; i < PM8001_MAX_CCB; i++) {
930 struct sas_task *task;
931 struct task_status_struct *ts;
932 struct pm8001_device *pm8001_dev;
933 unsigned long flags1;
934 u32 tag;
935 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
936
937 pm8001_dev = ccb->device;
James Bottomleyaa9f8322013-05-07 14:44:06 -0700938 if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
Mark Salyzyn5954d732012-01-17 11:52:24 -0500939 continue;
940 if (!device_to_close) {
941 uintptr_t d = (uintptr_t)pm8001_dev
942 - (uintptr_t)&pm8001_ha->devices;
943 if (((d % sizeof(*pm8001_dev)) != 0)
944 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
945 continue;
946 } else if (pm8001_dev != device_to_close)
947 continue;
948 tag = ccb->ccb_tag;
949 if (!tag || (tag == 0xFFFFFFFF))
950 continue;
951 task = ccb->task;
952 if (!task || !task->task_done)
953 continue;
954 if (task_to_close && (task != task_to_close))
955 continue;
956 ts = &task->task_status;
957 ts->resp = SAS_TASK_COMPLETE;
958 /* Force the midlayer to retry */
959 ts->stat = SAS_OPEN_REJECT;
960 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
961 if (pm8001_dev)
Viswas G4a2efd42020-11-02 22:25:26 +0530962 atomic_dec(&pm8001_dev->running_req);
Mark Salyzyn5954d732012-01-17 11:52:24 -0500963 spin_lock_irqsave(&task->task_state_lock, flags1);
964 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
965 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
966 task->task_state_flags |= SAS_TASK_STATE_DONE;
967 if (unlikely((task->task_state_flags
968 & SAS_TASK_STATE_ABORTED))) {
969 spin_unlock_irqrestore(&task->task_state_lock,
970 flags1);
971 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
972 } else {
973 spin_unlock_irqrestore(&task->task_state_lock,
974 flags1);
975 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
976 mb();/* in order to force CPU ordering */
977 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
978 task->task_done(task);
979 spin_lock_irqsave(&pm8001_ha->lock, flags);
980 }
981 }
982
983 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
984}
985
jack wangdbf9bfe2009-10-14 16:19:21 +0800986/**
987 * Standard mandates link reset for ATA (type 0) and hard reset for
988 * SSP (type 1) , only for RECOVERY
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100989 * @dev: the device structure for the device to reset.
jack wangdbf9bfe2009-10-14 16:19:21 +0800990 */
991int pm8001_I_T_nexus_reset(struct domain_device *dev)
992{
993 int rc = TMF_RESP_FUNC_FAILED;
994 struct pm8001_device *pm8001_dev;
995 struct pm8001_hba_info *pm8001_ha;
996 struct sas_phy *phy;
Dan Williamse7db8222012-06-21 23:30:58 -0700997
jack wangdbf9bfe2009-10-14 16:19:21 +0800998 if (!dev || !dev->lldd_dev)
Dan Williamse7db8222012-06-21 23:30:58 -0700999 return -ENODEV;
jack wangdbf9bfe2009-10-14 16:19:21 +08001000
1001 pm8001_dev = dev->lldd_dev;
1002 pm8001_ha = pm8001_find_ha_by_dev(dev);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001003 phy = sas_get_local_phy(dev);
jack wangdbf9bfe2009-10-14 16:19:21 +08001004
1005 if (dev_is_sata(dev)) {
Dan Williamsf41a0c42011-12-21 21:33:17 -08001006 if (scsi_is_sas_phy_local(phy)) {
1007 rc = 0;
1008 goto out;
1009 }
jack wangdbf9bfe2009-10-14 16:19:21 +08001010 rc = sas_phy_reset(phy, 1);
Viswas G3a1ae962015-08-11 15:06:26 +05301011 if (rc) {
Joe Perches1b5d2792020-11-20 15:16:09 -08001012 pm8001_dbg(pm8001_ha, EH,
1013 "phy reset failed for device %x\n"
1014 "with rc %d\n", pm8001_dev->device_id, rc);
Viswas G3a1ae962015-08-11 15:06:26 +05301015 rc = TMF_RESP_FUNC_FAILED;
1016 goto out;
1017 }
jack wangdbf9bfe2009-10-14 16:19:21 +08001018 msleep(2000);
1019 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1020 dev, 1, 0);
Viswas G3a1ae962015-08-11 15:06:26 +05301021 if (rc) {
Joe Perches1b5d2792020-11-20 15:16:09 -08001022 pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
1023 "with rc %d\n", pm8001_dev->device_id, rc);
Viswas G3a1ae962015-08-11 15:06:26 +05301024 rc = TMF_RESP_FUNC_FAILED;
1025 }
Dan Williamsf41a0c42011-12-21 21:33:17 -08001026 } else {
1027 rc = sas_phy_reset(phy, 1);
1028 msleep(2000);
jack wangdbf9bfe2009-10-14 16:19:21 +08001029 }
Joe Perches1b5d2792020-11-20 15:16:09 -08001030 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1031 pm8001_dev->device_id, rc);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001032 out:
1033 sas_put_local_phy(phy);
jack wangdbf9bfe2009-10-14 16:19:21 +08001034 return rc;
1035}
1036
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301037/*
1038* This function handle the IT_NEXUS_XXX event or completion
1039* status code for SSP/SATA/SMP I/O request.
1040*/
1041int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
1042{
1043 int rc = TMF_RESP_FUNC_FAILED;
1044 struct pm8001_device *pm8001_dev;
1045 struct pm8001_hba_info *pm8001_ha;
1046 struct sas_phy *phy;
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301047
1048 if (!dev || !dev->lldd_dev)
1049 return -1;
1050
1051 pm8001_dev = dev->lldd_dev;
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301052 pm8001_ha = pm8001_find_ha_by_dev(dev);
1053
Joe Perches1b5d2792020-11-20 15:16:09 -08001054 pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301055
1056 phy = sas_get_local_phy(dev);
1057
1058 if (dev_is_sata(dev)) {
1059 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1060 if (scsi_is_sas_phy_local(phy)) {
1061 rc = 0;
1062 goto out;
1063 }
1064 /* send internal ssp/sata/smp abort command to FW */
1065 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1066 dev, 1, 0);
1067 msleep(100);
1068
1069 /* deregister the target device */
1070 pm8001_dev_gone_notify(dev);
1071 msleep(200);
1072
1073 /*send phy reset to hard reset target */
1074 rc = sas_phy_reset(phy, 1);
1075 msleep(2000);
1076 pm8001_dev->setds_completion = &completion_setstate;
1077
1078 wait_for_completion(&completion_setstate);
1079 } else {
1080 /* send internal ssp/sata/smp abort command to FW */
1081 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1082 dev, 1, 0);
1083 msleep(100);
1084
1085 /* deregister the target device */
1086 pm8001_dev_gone_notify(dev);
1087 msleep(200);
1088
1089 /*send phy reset to hard reset target */
1090 rc = sas_phy_reset(phy, 1);
1091 msleep(2000);
1092 }
Joe Perches1b5d2792020-11-20 15:16:09 -08001093 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1094 pm8001_dev->device_id, rc);
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301095out:
1096 sas_put_local_phy(phy);
1097
1098 return rc;
1099}
jack wangdbf9bfe2009-10-14 16:19:21 +08001100/* mandatory SAM-3, the task reset the specified LUN*/
1101int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1102{
1103 int rc = TMF_RESP_FUNC_FAILED;
1104 struct pm8001_tmf_task tmf_task;
1105 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1106 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
Nikith Ganigarakoppal34a9b812013-10-30 16:13:22 +05301107 DECLARE_COMPLETION_ONSTACK(completion_setstate);
jack wangdbf9bfe2009-10-14 16:19:21 +08001108 if (dev_is_sata(dev)) {
Dan Williamsf41a0c42011-12-21 21:33:17 -08001109 struct sas_phy *phy = sas_get_local_phy(dev);
jack wangdbf9bfe2009-10-14 16:19:21 +08001110 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1111 dev, 1, 0);
1112 rc = sas_phy_reset(phy, 1);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001113 sas_put_local_phy(phy);
Nikith Ganigarakoppal34a9b812013-10-30 16:13:22 +05301114 pm8001_dev->setds_completion = &completion_setstate;
jack wangdbf9bfe2009-10-14 16:19:21 +08001115 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1116 pm8001_dev, 0x01);
Nikith Ganigarakoppal34a9b812013-10-30 16:13:22 +05301117 wait_for_completion(&completion_setstate);
jack wangdbf9bfe2009-10-14 16:19:21 +08001118 } else {
1119 tmf_task.tmf = TMF_LU_RESET;
1120 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1121 }
1122 /* If failed, fall-through I_T_Nexus reset */
Joe Perches1b5d2792020-11-20 15:16:09 -08001123 pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
1124 pm8001_dev->device_id, rc);
jack wangdbf9bfe2009-10-14 16:19:21 +08001125 return rc;
1126}
1127
1128/* optional SAM-3 */
1129int pm8001_query_task(struct sas_task *task)
1130{
1131 u32 tag = 0xdeadbeef;
jack wangdbf9bfe2009-10-14 16:19:21 +08001132 struct scsi_lun lun;
1133 struct pm8001_tmf_task tmf_task;
1134 int rc = TMF_RESP_FUNC_FAILED;
1135 if (unlikely(!task || !task->lldd_task || !task->dev))
1136 return rc;
1137
1138 if (task->task_proto & SAS_PROTOCOL_SSP) {
1139 struct scsi_cmnd *cmnd = task->uldd_task;
1140 struct domain_device *dev = task->dev;
1141 struct pm8001_hba_info *pm8001_ha =
1142 pm8001_find_ha_by_dev(dev);
1143
1144 int_to_scsilun(cmnd->device->lun, &lun);
1145 rc = pm8001_find_tag(task, &tag);
1146 if (rc == 0) {
1147 rc = TMF_RESP_FUNC_FAILED;
1148 return rc;
1149 }
Joe Perches1b5d2792020-11-20 15:16:09 -08001150 pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
jack wangdbf9bfe2009-10-14 16:19:21 +08001151 tmf_task.tmf = TMF_QUERY_TASK;
1152 tmf_task.tag_of_task_to_be_managed = tag;
1153
1154 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1155 switch (rc) {
1156 /* The task is still in Lun, release it then */
1157 case TMF_RESP_FUNC_SUCC:
Joe Perches1b5d2792020-11-20 15:16:09 -08001158 pm8001_dbg(pm8001_ha, EH,
1159 "The task is still in Lun\n");
Mark Salyzyn6fbc7692011-09-26 07:57:36 -07001160 break;
jack wangdbf9bfe2009-10-14 16:19:21 +08001161 /* The task is not in Lun or failed, reset the phy */
1162 case TMF_RESP_FUNC_FAILED:
1163 case TMF_RESP_FUNC_COMPLETE:
Joe Perches1b5d2792020-11-20 15:16:09 -08001164 pm8001_dbg(pm8001_ha, EH,
1165 "The task is not in Lun or failed, reset the phy\n");
jack wangdbf9bfe2009-10-14 16:19:21 +08001166 break;
1167 }
1168 }
peter chang73706722019-11-14 15:39:02 +05301169 pr_err("pm80xx: rc= %d\n", rc);
jack wangdbf9bfe2009-10-14 16:19:21 +08001170 return rc;
1171}
1172
Weitao Hou32877942019-05-20 11:24:03 +08001173/* mandatory SAM-3, still need free task/ccb info, abort the specified task */
jack wangdbf9bfe2009-10-14 16:19:21 +08001174int pm8001_abort_task(struct sas_task *task)
1175{
1176 unsigned long flags;
Viswas G1db49902017-10-18 11:39:11 +05301177 u32 tag;
jack wangdbf9bfe2009-10-14 16:19:21 +08001178 struct domain_device *dev ;
Viswas G1db49902017-10-18 11:39:11 +05301179 struct pm8001_hba_info *pm8001_ha;
jack wangdbf9bfe2009-10-14 16:19:21 +08001180 struct scsi_lun lun;
1181 struct pm8001_device *pm8001_dev;
1182 struct pm8001_tmf_task tmf_task;
Viswas G869ddbd2017-10-18 11:39:13 +05301183 int rc = TMF_RESP_FUNC_FAILED, ret;
1184 u32 phy_id;
1185 struct sas_task_slow slow_task;
jack wangdbf9bfe2009-10-14 16:19:21 +08001186 if (unlikely(!task || !task->lldd_task || !task->dev))
Viswas G1db49902017-10-18 11:39:11 +05301187 return TMF_RESP_FUNC_FAILED;
1188 dev = task->dev;
1189 pm8001_dev = dev->lldd_dev;
1190 pm8001_ha = pm8001_find_ha_by_dev(dev);
Viswas G869ddbd2017-10-18 11:39:13 +05301191 phy_id = pm8001_dev->attached_phy;
peter chang51c1c5f62019-11-14 15:39:06 +05301192 ret = pm8001_find_tag(task, &tag);
1193 if (ret == 0) {
Viswas G1db49902017-10-18 11:39:11 +05301194 pm8001_printk("no tag for task:%p\n", task);
1195 return TMF_RESP_FUNC_FAILED;
1196 }
jack wangdbf9bfe2009-10-14 16:19:21 +08001197 spin_lock_irqsave(&task->task_state_lock, flags);
1198 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1199 spin_unlock_irqrestore(&task->task_state_lock, flags);
Viswas G1db49902017-10-18 11:39:11 +05301200 return TMF_RESP_FUNC_COMPLETE;
jack wangdbf9bfe2009-10-14 16:19:21 +08001201 }
Viswas G869ddbd2017-10-18 11:39:13 +05301202 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1203 if (task->slow_task == NULL) {
1204 init_completion(&slow_task.completion);
1205 task->slow_task = &slow_task;
jack wangdbf9bfe2009-10-14 16:19:21 +08001206 }
1207 spin_unlock_irqrestore(&task->task_state_lock, flags);
1208 if (task->task_proto & SAS_PROTOCOL_SSP) {
1209 struct scsi_cmnd *cmnd = task->uldd_task;
jack wangdbf9bfe2009-10-14 16:19:21 +08001210 int_to_scsilun(cmnd->device->lun, &lun);
jack_wang97ee2082009-11-05 22:33:51 +08001211 tmf_task.tmf = TMF_ABORT_TASK;
jack wangdbf9bfe2009-10-14 16:19:21 +08001212 tmf_task.tag_of_task_to_be_managed = tag;
1213 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
jack_wang97ee2082009-11-05 22:33:51 +08001214 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
jack wangdbf9bfe2009-10-14 16:19:21 +08001215 pm8001_dev->sas_device, 0, tag);
1216 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1217 task->task_proto & SAS_PROTOCOL_STP) {
Viswas G869ddbd2017-10-18 11:39:13 +05301218 if (pm8001_ha->chip_id == chip_8006) {
1219 DECLARE_COMPLETION_ONSTACK(completion_reset);
1220 DECLARE_COMPLETION_ONSTACK(completion);
1221 struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1222
1223 /* 1. Set Device state as Recovery */
1224 pm8001_dev->setds_completion = &completion;
1225 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1226 pm8001_dev, 0x03);
1227 wait_for_completion(&completion);
1228
1229 /* 2. Send Phy Control Hard Reset */
1230 reinit_completion(&completion);
peter chang51c1c5f62019-11-14 15:39:06 +05301231 phy->port_reset_status = PORT_RESET_TMO;
Viswas G869ddbd2017-10-18 11:39:13 +05301232 phy->reset_success = false;
1233 phy->enable_completion = &completion;
1234 phy->reset_completion = &completion_reset;
1235 ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
1236 PHY_HARD_RESET);
peter chang51c1c5f62019-11-14 15:39:06 +05301237 if (ret) {
1238 phy->enable_completion = NULL;
1239 phy->reset_completion = NULL;
Viswas G869ddbd2017-10-18 11:39:13 +05301240 goto out;
peter chang51c1c5f62019-11-14 15:39:06 +05301241 }
1242
1243 /* In the case of the reset timeout/fail we still
1244 * abort the command at the firmware. The assumption
1245 * here is that the drive is off doing something so
1246 * that it's not processing requests, and we want to
1247 * avoid getting a completion for this and either
1248 * leaking the task in libsas or losing the race and
1249 * getting a double free.
1250 */
Joe Perches1b5d2792020-11-20 15:16:09 -08001251 pm8001_dbg(pm8001_ha, MSG,
1252 "Waiting for local phy ctl\n");
peter chang51c1c5f62019-11-14 15:39:06 +05301253 ret = wait_for_completion_timeout(&completion,
1254 PM8001_TASK_TIMEOUT * HZ);
1255 if (!ret || !phy->reset_success) {
1256 phy->enable_completion = NULL;
1257 phy->reset_completion = NULL;
1258 } else {
1259 /* 3. Wait for Port Reset complete or
1260 * Port reset TMO
1261 */
Joe Perches1b5d2792020-11-20 15:16:09 -08001262 pm8001_dbg(pm8001_ha, MSG,
1263 "Waiting for Port reset\n");
peter chang51c1c5f62019-11-14 15:39:06 +05301264 ret = wait_for_completion_timeout(
1265 &completion_reset,
1266 PM8001_TASK_TIMEOUT * HZ);
1267 if (!ret)
1268 phy->reset_completion = NULL;
1269 WARN_ON(phy->port_reset_status ==
1270 PORT_RESET_TMO);
1271 if (phy->port_reset_status == PORT_RESET_TMO) {
1272 pm8001_dev_gone_notify(dev);
1273 goto out;
1274 }
Deepak Ukey196ba662019-07-09 15:30:48 +05301275 }
Viswas G869ddbd2017-10-18 11:39:13 +05301276
1277 /*
1278 * 4. SATA Abort ALL
1279 * we wait for the task to be aborted so that the task
1280 * is removed from the ccb. on success the caller is
1281 * going to free the task.
1282 */
1283 ret = pm8001_exec_internal_task_abort(pm8001_ha,
1284 pm8001_dev, pm8001_dev->sas_device, 1, tag);
1285 if (ret)
1286 goto out;
1287 ret = wait_for_completion_timeout(
1288 &task->slow_task->completion,
1289 PM8001_TASK_TIMEOUT * HZ);
1290 if (!ret)
1291 goto out;
1292
1293 /* 5. Set Device State as Operational */
1294 reinit_completion(&completion);
1295 pm8001_dev->setds_completion = &completion;
1296 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1297 pm8001_dev, 0x01);
1298 wait_for_completion(&completion);
1299 } else {
1300 rc = pm8001_exec_internal_task_abort(pm8001_ha,
1301 pm8001_dev, pm8001_dev->sas_device, 0, tag);
jack wangdbf9bfe2009-10-14 16:19:21 +08001302 }
Viswas G869ddbd2017-10-18 11:39:13 +05301303 rc = TMF_RESP_FUNC_COMPLETE;
jack wangdbf9bfe2009-10-14 16:19:21 +08001304 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1305 /* SMP */
jack wangdbf9bfe2009-10-14 16:19:21 +08001306 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1307 pm8001_dev->sas_device, 0, tag);
1308
1309 }
1310out:
Viswas G869ddbd2017-10-18 11:39:13 +05301311 spin_lock_irqsave(&task->task_state_lock, flags);
1312 if (task->slow_task == &slow_task)
1313 task->slow_task = NULL;
1314 spin_unlock_irqrestore(&task->task_state_lock, flags);
jack wangdbf9bfe2009-10-14 16:19:21 +08001315 if (rc != TMF_RESP_FUNC_COMPLETE)
1316 pm8001_printk("rc= %d\n", rc);
1317 return rc;
1318}
1319
1320int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1321{
jack wangdbf9bfe2009-10-14 16:19:21 +08001322 struct pm8001_tmf_task tmf_task;
1323
1324 tmf_task.tmf = TMF_ABORT_TASK_SET;
Colin Ian Kingafcd6092019-07-31 23:22:14 +01001325 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
jack wangdbf9bfe2009-10-14 16:19:21 +08001326}
1327
1328int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1329{
jack wangdbf9bfe2009-10-14 16:19:21 +08001330 struct pm8001_tmf_task tmf_task;
1331
1332 tmf_task.tmf = TMF_CLEAR_ACA;
Colin Ian Kingafcd6092019-07-31 23:22:14 +01001333 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
jack wangdbf9bfe2009-10-14 16:19:21 +08001334}
1335
1336int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1337{
jack wangdbf9bfe2009-10-14 16:19:21 +08001338 struct pm8001_tmf_task tmf_task;
1339 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1340 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1341
Joe Perches1b5d2792020-11-20 15:16:09 -08001342 pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
1343 pm8001_dev->device_id);
jack wangdbf9bfe2009-10-14 16:19:21 +08001344 tmf_task.tmf = TMF_CLEAR_TASK_SET;
Colin Ian Kingafcd6092019-07-31 23:22:14 +01001345 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
jack wangdbf9bfe2009-10-14 16:19:21 +08001346}
1347