blob: d63c7d59ad3457d6c669f692e0db854a7490f839 [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);
Zhen Lei4ee8c402021-06-10 17:46:05 +0800121 if (!mem_virt_alloc)
122 return -ENOMEM;
jack wangdbf9bfe2009-10-14 16:19:21 +0800123 *pphys_addr = mem_dma_handle;
124 phys_align = (*pphys_addr + align_offset) & ~align_offset;
125 *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
126 *pphys_addr_hi = upper_32_bits(phys_align);
127 *pphys_addr_lo = lower_32_bits(phys_align);
128 return 0;
129}
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100130
jack wangdbf9bfe2009-10-14 16:19:21 +0800131/**
132 * pm8001_find_ha_by_dev - from domain device which come from sas layer to
133 * find out our hba struct.
134 * @dev: the domain device which from sas layer.
135 */
136static
137struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
138{
139 struct sas_ha_struct *sha = dev->port->ha;
140 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
141 return pm8001_ha;
142}
143
144/**
145 * pm8001_phy_control - this function should be registered to
146 * sas_domain_function_template to provide libsas used, note: this is just
147 * control the HBA phy rather than other expander phy if you want control
148 * other phy, you should use SMP command.
149 * @sas_phy: which phy in HBA phys.
150 * @func: the operation.
151 * @funcdata: always NULL.
152 */
153int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
154 void *funcdata)
155{
156 int rc = 0, phy_id = sas_phy->id;
157 struct pm8001_hba_info *pm8001_ha = NULL;
158 struct sas_phy_linkrates *rates;
Deepak Ukeycd135752018-09-11 14:18:02 +0530159 struct pm8001_phy *phy;
jack wangdbf9bfe2009-10-14 16:19:21 +0800160 DECLARE_COMPLETION_ONSTACK(completion);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500161 unsigned long flags;
jack wangdbf9bfe2009-10-14 16:19:21 +0800162 pm8001_ha = sas_phy->ha->lldd_ha;
Deepak Ukeycd135752018-09-11 14:18:02 +0530163 phy = &pm8001_ha->phy[phy_id];
jack wangdbf9bfe2009-10-14 16:19:21 +0800164 pm8001_ha->phy[phy_id].enable_completion = &completion;
165 switch (func) {
166 case PHY_FUNC_SET_LINK_RATE:
167 rates = funcdata;
168 if (rates->minimum_linkrate) {
169 pm8001_ha->phy[phy_id].minimum_linkrate =
170 rates->minimum_linkrate;
171 }
172 if (rates->maximum_linkrate) {
173 pm8001_ha->phy[phy_id].maximum_linkrate =
174 rates->maximum_linkrate;
175 }
Deepak Ukeycd135752018-09-11 14:18:02 +0530176 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800177 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
178 wait_for_completion(&completion);
179 }
180 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
181 PHY_LINK_RESET);
182 break;
183 case PHY_FUNC_HARD_RESET:
Deepak Ukeycd135752018-09-11 14:18:02 +0530184 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800185 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
186 wait_for_completion(&completion);
187 }
188 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
189 PHY_HARD_RESET);
190 break;
191 case PHY_FUNC_LINK_RESET:
Deepak Ukeycd135752018-09-11 14:18:02 +0530192 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800193 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
194 wait_for_completion(&completion);
195 }
196 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
197 PHY_LINK_RESET);
198 break;
199 case PHY_FUNC_RELEASE_SPINUP_HOLD:
200 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
201 PHY_LINK_RESET);
202 break;
203 case PHY_FUNC_DISABLE:
Deepak Ukeycd135752018-09-11 14:18:02 +0530204 if (pm8001_ha->chip_id != chip_8001) {
205 if (pm8001_ha->phy[phy_id].phy_state ==
206 PHY_STATE_LINK_UP_SPCV) {
Deepak Ukeycd135752018-09-11 14:18:02 +0530207 sas_phy_disconnected(&phy->sas_phy);
Ahmed S. Darwishde6d7542021-01-18 11:09:51 +0100208 sas_notify_phy_event(&phy->sas_phy,
Ahmed S. Darwishcd4e8172021-01-18 11:09:45 +0100209 PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
Deepak Ukeycd135752018-09-11 14:18:02 +0530210 phy->phy_attached = 0;
211 }
212 } else {
213 if (pm8001_ha->phy[phy_id].phy_state ==
214 PHY_STATE_LINK_UP_SPC) {
Deepak Ukeycd135752018-09-11 14:18:02 +0530215 sas_phy_disconnected(&phy->sas_phy);
Ahmed S. Darwishde6d7542021-01-18 11:09:51 +0100216 sas_notify_phy_event(&phy->sas_phy,
Ahmed S. Darwishcd4e8172021-01-18 11:09:45 +0100217 PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
Deepak Ukeycd135752018-09-11 14:18:02 +0530218 phy->phy_attached = 0;
219 }
220 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800221 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
222 break;
Mark Salyzynd95d0002012-01-17 09:18:57 -0500223 case PHY_FUNC_GET_EVENTS:
224 spin_lock_irqsave(&pm8001_ha->lock, flags);
Sakthivel Kf5860992013-04-17 16:37:02 +0530225 if (pm8001_ha->chip_id == chip_8001) {
226 if (-1 == pm8001_bar4_shift(pm8001_ha,
Mark Salyzynd95d0002012-01-17 09:18:57 -0500227 (phy_id < 4) ? 0x30000 : 0x40000)) {
Sakthivel Kf5860992013-04-17 16:37:02 +0530228 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
229 return -EINVAL;
230 }
Mark Salyzynd95d0002012-01-17 09:18:57 -0500231 }
232 {
233 struct sas_phy *phy = sas_phy->phy;
234 uint32_t *qp = (uint32_t *)(((char *)
235 pm8001_ha->io_mem[2].memvirtaddr)
236 + 0x1034 + (0x4000 * (phy_id & 3)));
237
238 phy->invalid_dword_count = qp[0];
239 phy->running_disparity_error_count = qp[1];
240 phy->loss_of_dword_sync_count = qp[3];
241 phy->phy_reset_problem_count = qp[4];
242 }
Sakthivel Kf5860992013-04-17 16:37:02 +0530243 if (pm8001_ha->chip_id == chip_8001)
244 pm8001_bar4_shift(pm8001_ha, 0);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500245 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
246 return 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800247 default:
Joe Perches1b5d2792020-11-20 15:16:09 -0800248 pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500249 rc = -EOPNOTSUPP;
jack wangdbf9bfe2009-10-14 16:19:21 +0800250 }
251 msleep(300);
252 return rc;
253}
254
jack wangdbf9bfe2009-10-14 16:19:21 +0800255/**
256 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
257 * command to HBA.
258 * @shost: the scsi host data.
259 */
260void pm8001_scan_start(struct Scsi_Host *shost)
261{
262 int i;
263 struct pm8001_hba_info *pm8001_ha;
264 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
265 pm8001_ha = sha->lldd_ha;
Sakthivel Kf5860992013-04-17 16:37:02 +0530266 /* SAS_RE_INITIALIZATION not available in SPCv/ve */
267 if (pm8001_ha->chip_id == chip_8001)
268 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800269 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
270 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
271}
272
273int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
274{
Dan Williamsb1124cd2011-12-19 16:42:34 -0800275 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
276
jack wangdbf9bfe2009-10-14 16:19:21 +0800277 /* give the phy enabling interrupt event time to come in (1s
278 * is empirically about all it takes) */
279 if (time < HZ)
280 return 0;
281 /* Wait for discovery to finish */
Dan Williamsb1124cd2011-12-19 16:42:34 -0800282 sas_drain_work(ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800283 return 1;
284}
285
286/**
287 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
288 * @pm8001_ha: our hba card information
289 * @ccb: the ccb which attached to smp task
290 */
291static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
292 struct pm8001_ccb_info *ccb)
293{
294 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
295}
296
297u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
298{
299 struct ata_queued_cmd *qc = task->uldd_task;
300 if (qc) {
301 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
Hannes Reineckeef026b12016-04-25 12:45:44 +0200302 qc->tf.command == ATA_CMD_FPDMA_READ ||
303 qc->tf.command == ATA_CMD_FPDMA_RECV ||
Hannes Reinecke661ce1f2016-04-25 12:45:45 +0200304 qc->tf.command == ATA_CMD_FPDMA_SEND ||
305 qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800306 *tag = qc->tag;
307 return 1;
308 }
309 }
310 return 0;
311}
312
313/**
314 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
315 * @pm8001_ha: our hba card information
316 * @ccb: the ccb which attached to sata task
317 */
318static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
319 struct pm8001_ccb_info *ccb)
320{
321 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
322}
323
324/**
325 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
326 * @pm8001_ha: our hba card information
327 * @ccb: the ccb which attached to TM
328 * @tmf: the task management IU
329 */
330static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
331 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
332{
333 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
334}
335
336/**
337 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
338 * @pm8001_ha: our hba card information
339 * @ccb: the ccb which attached to ssp task
340 */
341static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
342 struct pm8001_ccb_info *ccb)
343{
344 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
345}
Dan Williams11e16362011-09-20 15:11:03 -0700346
jack wang1cc943a2009-12-07 17:22:42 +0800347 /* Find the local port id that's attached to this device */
348static int sas_find_local_port_id(struct domain_device *dev)
349{
350 struct domain_device *pdev = dev->parent;
351
352 /* Directly attached device */
353 if (!pdev)
354 return dev->port->id;
355 while (pdev) {
356 struct domain_device *pdev_p = pdev->parent;
357 if (!pdev_p)
358 return pdev->port->id;
359 pdev = pdev->parent;
360 }
361 return 0;
362}
363
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100364#define DEV_IS_GONE(pm8001_dev) \
365 ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
jack wangdbf9bfe2009-10-14 16:19:21 +0800366/**
jack_wang97ee2082009-11-05 22:33:51 +0800367 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
jack wangdbf9bfe2009-10-14 16:19:21 +0800368 * @task: the task to be execute.
jack wangdbf9bfe2009-10-14 16:19:21 +0800369 * @gfp_flags: gfp_flags.
jack_wang97ee2082009-11-05 22:33:51 +0800370 * @is_tmf: if it is task management task.
jack wangdbf9bfe2009-10-14 16:19:21 +0800371 * @tmf: the task management IU
372 */
Christoph Hellwig79855d12014-11-05 10:36:28 +0100373static int pm8001_task_exec(struct sas_task *task,
jack wangdbf9bfe2009-10-14 16:19:21 +0800374 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
375{
376 struct domain_device *dev = task->dev;
377 struct pm8001_hba_info *pm8001_ha;
378 struct pm8001_device *pm8001_dev;
jack wang1cc943a2009-12-07 17:22:42 +0800379 struct pm8001_port *port = NULL;
jack wangdbf9bfe2009-10-14 16:19:21 +0800380 struct sas_task *t = task;
381 struct pm8001_ccb_info *ccb;
John Sperbeckcef15382019-11-14 15:39:00 +0530382 u32 tag = 0xdeadbeef, rc = 0, n_elem = 0;
Dan Williams312d3e52011-11-17 17:59:50 -0800383 unsigned long flags = 0;
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530384 enum sas_protocol task_proto = t->task_proto;
jack wangdbf9bfe2009-10-14 16:19:21 +0800385
386 if (!dev->port) {
387 struct task_status_struct *tsm = &t->task_status;
388 tsm->resp = SAS_TASK_UNDELIVERED;
389 tsm->stat = SAS_PHY_DOWN;
James Bottomleyaa9f8322013-05-07 14:44:06 -0700390 if (dev->dev_type != SAS_SATA_DEV)
jack wangdbf9bfe2009-10-14 16:19:21 +0800391 t->task_done(t);
392 return 0;
393 }
394 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
Deepak Ukey72349b62018-09-11 14:18:04 +0530395 if (pm8001_ha->controller_fatal_error) {
396 struct task_status_struct *ts = &t->task_status;
397
398 ts->resp = SAS_TASK_UNDELIVERED;
399 t->task_done(t);
400 return 0;
401 }
Joe Perches1b5d2792020-11-20 15:16:09 -0800402 pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800403 spin_lock_irqsave(&pm8001_ha->lock, flags);
404 do {
405 dev = t->dev;
406 pm8001_dev = dev->lldd_dev;
jack wang1cc943a2009-12-07 17:22:42 +0800407 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
Mark Salyzynb90b3782011-09-22 08:50:01 -0700408 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530409 if (sas_protocol_ata(task_proto)) {
jack wang1cc943a2009-12-07 17:22:42 +0800410 struct task_status_struct *ts = &t->task_status;
411 ts->resp = SAS_TASK_UNDELIVERED;
412 ts->stat = SAS_PHY_DOWN;
413
414 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
jack wang1cc943a2009-12-07 17:22:42 +0800415 t->task_done(t);
jack wang1cc943a2009-12-07 17:22:42 +0800416 spin_lock_irqsave(&pm8001_ha->lock, flags);
jack wang1cc943a2009-12-07 17:22:42 +0800417 continue;
418 } else {
419 struct task_status_struct *ts = &t->task_status;
420 ts->resp = SAS_TASK_UNDELIVERED;
421 ts->stat = SAS_PHY_DOWN;
422 t->task_done(t);
jack wang1cc943a2009-12-07 17:22:42 +0800423 continue;
424 }
425 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800426 rc = pm8001_tag_alloc(pm8001_ha, &tag);
427 if (rc)
428 goto err_out;
429 ccb = &pm8001_ha->ccb_info[tag];
430
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530431 if (!sas_protocol_ata(task_proto)) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800432 if (t->num_scatter) {
433 n_elem = dma_map_sg(pm8001_ha->dev,
434 t->scatter,
435 t->num_scatter,
436 t->data_dir);
437 if (!n_elem) {
438 rc = -ENOMEM;
jack_wang97ee2082009-11-05 22:33:51 +0800439 goto err_out_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800440 }
441 }
442 } else {
443 n_elem = t->num_scatter;
444 }
445
jack_wang97ee2082009-11-05 22:33:51 +0800446 t->lldd_task = ccb;
jack wangdbf9bfe2009-10-14 16:19:21 +0800447 ccb->n_elem = n_elem;
448 ccb->ccb_tag = tag;
449 ccb->task = t;
XinHong Zhuaed97b82014-02-14 16:01:28 +0800450 ccb->device = pm8001_dev;
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530451 switch (task_proto) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800452 case SAS_PROTOCOL_SMP:
Viswas G4a2efd42020-11-02 22:25:26 +0530453 atomic_inc(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800454 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
455 break;
456 case SAS_PROTOCOL_SSP:
Viswas G4a2efd42020-11-02 22:25:26 +0530457 atomic_inc(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800458 if (is_tmf)
459 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
460 ccb, tmf);
461 else
462 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
463 break;
464 case SAS_PROTOCOL_SATA:
465 case SAS_PROTOCOL_STP:
Viswas G4a2efd42020-11-02 22:25:26 +0530466 atomic_inc(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800467 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
468 break;
469 default:
470 dev_printk(KERN_ERR, pm8001_ha->dev,
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530471 "unknown sas_task proto: 0x%x\n", task_proto);
jack wangdbf9bfe2009-10-14 16:19:21 +0800472 rc = -EINVAL;
473 break;
474 }
475
476 if (rc) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800477 pm8001_dbg(pm8001_ha, IO, "rc is %x\n", rc);
Viswas G4a2efd42020-11-02 22:25:26 +0530478 atomic_dec(&pm8001_dev->running_req);
jack wangdbf9bfe2009-10-14 16:19:21 +0800479 goto err_out_tag;
480 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800481 /* TODO: select normal or high priority */
482 spin_lock(&t->task_state_lock);
483 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
484 spin_unlock(&t->task_state_lock);
Christoph Hellwig79855d12014-11-05 10:36:28 +0100485 } while (0);
jack wangdbf9bfe2009-10-14 16:19:21 +0800486 rc = 0;
487 goto out_done;
488
489err_out_tag:
490 pm8001_tag_free(pm8001_ha, tag);
491err_out:
492 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
Vikram Auradkara88d9db2019-11-14 15:39:04 +0530493 if (!sas_protocol_ata(task_proto))
jack wangdbf9bfe2009-10-14 16:19:21 +0800494 if (n_elem)
Deepak Ukey76cb25b2018-09-11 14:18:03 +0530495 dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter,
jack wangdbf9bfe2009-10-14 16:19:21 +0800496 t->data_dir);
497out_done:
498 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
499 return rc;
500}
501
502/**
503 * pm8001_queue_command - register for upper layer used, all IO commands sent
504 * to HBA are from this interface.
505 * @task: the task to be execute.
jack wangdbf9bfe2009-10-14 16:19:21 +0800506 * @gfp_flags: gfp_flags
507 */
Christoph Hellwig79855d12014-11-05 10:36:28 +0100508int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
jack wangdbf9bfe2009-10-14 16:19:21 +0800509{
Christoph Hellwig79855d12014-11-05 10:36:28 +0100510 return pm8001_task_exec(task, gfp_flags, 0, NULL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800511}
512
jack wangdbf9bfe2009-10-14 16:19:21 +0800513/**
514 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
515 * @pm8001_ha: our hba card information
516 * @ccb: the ccb which attached to ssp task
517 * @task: the task to be free.
518 * @ccb_idx: ccb index.
519 */
520void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
521 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
522{
523 if (!ccb->task)
524 return;
525 if (!sas_protocol_ata(task->task_proto))
526 if (ccb->n_elem)
527 dma_unmap_sg(pm8001_ha->dev, task->scatter,
528 task->num_scatter, task->data_dir);
529
530 switch (task->task_proto) {
531 case SAS_PROTOCOL_SMP:
532 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
Christoph Hellwigf73bdeb2018-10-10 19:59:50 +0200533 DMA_FROM_DEVICE);
jack wangdbf9bfe2009-10-14 16:19:21 +0800534 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
Christoph Hellwigf73bdeb2018-10-10 19:59:50 +0200535 DMA_TO_DEVICE);
jack wangdbf9bfe2009-10-14 16:19:21 +0800536 break;
537
538 case SAS_PROTOCOL_SATA:
539 case SAS_PROTOCOL_STP:
540 case SAS_PROTOCOL_SSP:
541 default:
542 /* do nothing */
543 break;
544 }
545 task->lldd_task = NULL;
546 ccb->task = NULL;
547 ccb->ccb_tag = 0xFFFFFFFF;
Mark Salyzyn5954d732012-01-17 11:52:24 -0500548 ccb->open_retry = 0;
Tomas Henzlef300542014-07-09 17:20:40 +0530549 pm8001_tag_free(pm8001_ha, ccb_idx);
jack wangdbf9bfe2009-10-14 16:19:21 +0800550}
551
552 /**
jack_wang97ee2082009-11-05 22:33:51 +0800553 * pm8001_alloc_dev - find a empty pm8001_device
jack wangdbf9bfe2009-10-14 16:19:21 +0800554 * @pm8001_ha: our hba card information
555 */
Baoyou Xie7efa59e2016-09-23 21:54:22 +0800556static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
jack wangdbf9bfe2009-10-14 16:19:21 +0800557{
558 u32 dev;
559 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
James Bottomleyaa9f8322013-05-07 14:44:06 -0700560 if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800561 pm8001_ha->devices[dev].id = dev;
562 return &pm8001_ha->devices[dev];
563 }
564 }
565 if (dev == PM8001_MAX_DEVICES) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800566 pm8001_dbg(pm8001_ha, FAIL,
567 "max support %d devices, ignore ..\n",
568 PM8001_MAX_DEVICES);
jack wangdbf9bfe2009-10-14 16:19:21 +0800569 }
570 return NULL;
571}
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530572/**
573 * pm8001_find_dev - find a matching pm8001_device
574 * @pm8001_ha: our hba card information
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100575 * @device_id: device ID to match against
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530576 */
577struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
578 u32 device_id)
579{
580 u32 dev;
581 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
582 if (pm8001_ha->devices[dev].device_id == device_id)
583 return &pm8001_ha->devices[dev];
584 }
585 if (dev == PM8001_MAX_DEVICES) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800586 pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530587 }
588 return NULL;
589}
jack wangdbf9bfe2009-10-14 16:19:21 +0800590
Ruksar Devadi4f5deeb2021-04-15 16:03:50 +0530591void pm8001_free_dev(struct pm8001_device *pm8001_dev)
jack wangdbf9bfe2009-10-14 16:19:21 +0800592{
593 u32 id = pm8001_dev->id;
594 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
595 pm8001_dev->id = id;
James Bottomleyaa9f8322013-05-07 14:44:06 -0700596 pm8001_dev->dev_type = SAS_PHY_UNUSED;
jack wangdbf9bfe2009-10-14 16:19:21 +0800597 pm8001_dev->device_id = PM8001_MAX_DEVICES;
598 pm8001_dev->sas_device = NULL;
599}
600
601/**
jack_wang97ee2082009-11-05 22:33:51 +0800602 * pm8001_dev_found_notify - libsas notify a device is found.
603 * @dev: the device structure which sas layer used.
604 *
605 * when libsas find a sas domain device, it should tell the LLDD that
606 * device is found, and then LLDD register this device to HBA firmware
607 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
608 * device ID(according to device's sas address) and returned it to LLDD. From
jack wangdbf9bfe2009-10-14 16:19:21 +0800609 * now on, we communicate with HBA FW with the device ID which HBA assigned
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800610 * rather than sas address. it is the necessary step for our HBA but it is
jack wangdbf9bfe2009-10-14 16:19:21 +0800611 * the optional for other HBA driver.
jack wangdbf9bfe2009-10-14 16:19:21 +0800612 */
613static int pm8001_dev_found_notify(struct domain_device *dev)
614{
615 unsigned long flags = 0;
616 int res = 0;
617 struct pm8001_hba_info *pm8001_ha = NULL;
618 struct domain_device *parent_dev = dev->parent;
619 struct pm8001_device *pm8001_device;
620 DECLARE_COMPLETION_ONSTACK(completion);
621 u32 flag = 0;
622 pm8001_ha = pm8001_find_ha_by_dev(dev);
623 spin_lock_irqsave(&pm8001_ha->lock, flags);
624
625 pm8001_device = pm8001_alloc_dev(pm8001_ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800626 if (!pm8001_device) {
627 res = -1;
628 goto found_out;
629 }
jack wangf01f4e62009-12-07 17:22:55 +0800630 pm8001_device->sas_device = dev;
jack wangdbf9bfe2009-10-14 16:19:21 +0800631 dev->lldd_dev = pm8001_device;
632 pm8001_device->dev_type = dev->dev_type;
633 pm8001_device->dcompletion = &completion;
John Garry924a3542019-06-10 20:41:41 +0800634 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800635 int phy_id;
636 struct ex_phy *phy;
637 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
638 phy_id++) {
639 phy = &parent_dev->ex_dev.ex_phy[phy_id];
640 if (SAS_ADDR(phy->attached_sas_addr)
641 == SAS_ADDR(dev->sas_addr)) {
642 pm8001_device->attached_phy = phy_id;
643 break;
644 }
645 }
646 if (phy_id == parent_dev->ex_dev.num_phys) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800647 pm8001_dbg(pm8001_ha, FAIL,
648 "Error: no attached dev:%016llx at ex:%016llx.\n",
649 SAS_ADDR(dev->sas_addr),
650 SAS_ADDR(parent_dev->sas_addr));
jack wangdbf9bfe2009-10-14 16:19:21 +0800651 res = -1;
652 }
653 } else {
James Bottomleyaa9f8322013-05-07 14:44:06 -0700654 if (dev->dev_type == SAS_SATA_DEV) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800655 pm8001_device->attached_phy =
656 dev->rphy->identify.phy_identifier;
Julia Lawall251f24e2018-12-30 16:53:13 +0100657 flag = 1; /* directly sata */
jack wangdbf9bfe2009-10-14 16:19:21 +0800658 }
659 } /*register this device to HBA*/
Joe Perches1b5d2792020-11-20 15:16:09 -0800660 pm8001_dbg(pm8001_ha, DISC, "Found device\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800661 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
662 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
663 wait_for_completion(&completion);
James Bottomleyaa9f8322013-05-07 14:44:06 -0700664 if (dev->dev_type == SAS_END_DEVICE)
jack wangdbf9bfe2009-10-14 16:19:21 +0800665 msleep(50);
Mark Salyzyn5c4fb762012-01-17 12:56:45 -0500666 pm8001_ha->flags = PM8001F_RUN_TIME;
jack wangdbf9bfe2009-10-14 16:19:21 +0800667 return 0;
668found_out:
669 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
670 return res;
671}
672
673int pm8001_dev_found(struct domain_device *dev)
674{
675 return pm8001_dev_found_notify(dev);
676}
677
Sakthivel Kc6b9ef52013-03-19 18:08:08 +0530678void pm8001_task_done(struct sas_task *task)
jack wangdbf9bfe2009-10-14 16:19:21 +0800679{
Dan Williamsf0bf7502012-06-21 23:36:30 -0700680 if (!del_timer(&task->slow_task->timer))
jack wangdbf9bfe2009-10-14 16:19:21 +0800681 return;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700682 complete(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800683}
684
Kees Cook77570ee2017-08-22 16:05:14 -0700685static void pm8001_tmf_timedout(struct timer_list *t)
jack wangdbf9bfe2009-10-14 16:19:21 +0800686{
Kees Cook77570ee2017-08-22 16:05:14 -0700687 struct sas_task_slow *slow = from_timer(slow, t, timer);
688 struct sas_task *task = slow->task;
jack wangdbf9bfe2009-10-14 16:19:21 +0800689
690 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700691 complete(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800692}
693
694#define PM8001_TASK_TIMEOUT 20
695/**
jack_wang97ee2082009-11-05 22:33:51 +0800696 * pm8001_exec_internal_tmf_task - execute some task management commands.
jack wangdbf9bfe2009-10-14 16:19:21 +0800697 * @dev: the wanted device.
698 * @tmf: which task management wanted to be take.
699 * @para_len: para_len.
700 * @parameter: ssp task parameter.
jack_wang97ee2082009-11-05 22:33:51 +0800701 *
702 * when errors or exception happened, we may want to do something, for example
703 * abort the issued task which result in this execption, it is done by calling
704 * this function, note it is also with the task execute interface.
jack wangdbf9bfe2009-10-14 16:19:21 +0800705 */
706static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
707 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
708{
709 int res, retry;
710 struct sas_task *task = NULL;
711 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
Anand Kumar Santhaname912457b2013-09-17 16:58:10 +0530712 struct pm8001_device *pm8001_dev = dev->lldd_dev;
713 DECLARE_COMPLETION_ONSTACK(completion_setstate);
jack wangdbf9bfe2009-10-14 16:19:21 +0800714
715 for (retry = 0; retry < 3; retry++) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700716 task = sas_alloc_slow_task(GFP_KERNEL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800717 if (!task)
718 return -ENOMEM;
719
720 task->dev = dev;
721 task->task_proto = dev->tproto;
722 memcpy(&task->ssp_task, parameter, para_len);
723 task->task_done = pm8001_task_done;
Kees Cook841b86f2017-10-23 09:40:42 +0200724 task->slow_task->timer.function = pm8001_tmf_timedout;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700725 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
726 add_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800727
Christoph Hellwig79855d12014-11-05 10:36:28 +0100728 res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf);
jack wangdbf9bfe2009-10-14 16:19:21 +0800729
730 if (res) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700731 del_timer(&task->slow_task->timer);
Joe Perches1b5d2792020-11-20 15:16:09 -0800732 pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800733 goto ex_err;
734 }
Dan Williamsf0bf7502012-06-21 23:36:30 -0700735 wait_for_completion(&task->slow_task->completion);
Anand Kumar Santhaname912457b2013-09-17 16:58:10 +0530736 if (pm8001_ha->chip_id != chip_8001) {
737 pm8001_dev->setds_completion = &completion_setstate;
Colin Ian King9e2a07e2019-03-17 18:15:32 +0000738 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
Igor Pylypiv9ec3d4c2021-03-04 22:09:08 -0800739 pm8001_dev, DS_OPERATIONAL);
Anand Kumar Santhaname912457b2013-09-17 16:58:10 +0530740 wait_for_completion(&completion_setstate);
741 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800742 res = -TMF_RESP_FUNC_FAILED;
743 /* Even TMF timed out, return direct. */
744 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
745 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800746 pm8001_dbg(pm8001_ha, FAIL,
747 "TMF task[%x]timeout.\n",
748 tmf->tmf);
jack wangdbf9bfe2009-10-14 16:19:21 +0800749 goto ex_err;
750 }
751 }
752
753 if (task->task_status.resp == SAS_TASK_COMPLETE &&
Bart Van Assched377f412021-05-23 19:54:55 -0700754 task->task_status.stat == SAS_SAM_STAT_GOOD) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800755 res = TMF_RESP_FUNC_COMPLETE;
756 break;
757 }
758
759 if (task->task_status.resp == SAS_TASK_COMPLETE &&
760 task->task_status.stat == SAS_DATA_UNDERRUN) {
761 /* no error, but return the number of bytes of
762 * underrun */
763 res = task->task_status.residual;
764 break;
765 }
766
767 if (task->task_status.resp == SAS_TASK_COMPLETE &&
768 task->task_status.stat == SAS_DATA_OVERRUN) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800769 pm8001_dbg(pm8001_ha, FAIL, "Blocked task error.\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800770 res = -EMSGSIZE;
771 break;
772 } else {
Joe Perches1b5d2792020-11-20 15:16:09 -0800773 pm8001_dbg(pm8001_ha, EH,
774 " Task to dev %016llx response:0x%x status 0x%x\n",
775 SAS_ADDR(dev->sas_addr),
776 task->task_status.resp,
777 task->task_status.stat);
Dan Williams4fcf8122011-07-29 17:26:39 -0700778 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800779 task = NULL;
780 }
781 }
782ex_err:
783 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -0700784 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800785 return res;
786}
787
788static int
789pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
790 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
791 u32 task_tag)
792{
793 int res, retry;
jack_wang97ee2082009-11-05 22:33:51 +0800794 u32 ccb_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800795 struct pm8001_ccb_info *ccb;
796 struct sas_task *task = NULL;
797
798 for (retry = 0; retry < 3; retry++) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700799 task = sas_alloc_slow_task(GFP_KERNEL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800800 if (!task)
801 return -ENOMEM;
802
803 task->dev = dev;
804 task->task_proto = dev->tproto;
805 task->task_done = pm8001_task_done;
Kees Cook841b86f2017-10-23 09:40:42 +0200806 task->slow_task->timer.function = pm8001_tmf_timedout;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700807 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
808 add_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800809
jack_wang97ee2082009-11-05 22:33:51 +0800810 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
811 if (res)
Dinghao Liuea403fd2020-08-23 17:14:53 +0800812 goto ex_err;
jack wangdbf9bfe2009-10-14 16:19:21 +0800813 ccb = &pm8001_ha->ccb_info[ccb_tag];
814 ccb->device = pm8001_dev;
815 ccb->ccb_tag = ccb_tag;
816 ccb->task = task;
Viswas G3b700e32015-08-11 15:06:28 +0530817 ccb->n_elem = 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800818
819 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
820 pm8001_dev, flag, task_tag, ccb_tag);
821
822 if (res) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700823 del_timer(&task->slow_task->timer);
Joe Perches1b5d2792020-11-20 15:16:09 -0800824 pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800825 goto ex_err;
826 }
Dan Williamsf0bf7502012-06-21 23:36:30 -0700827 wait_for_completion(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800828 res = TMF_RESP_FUNC_FAILED;
829 /* Even TMF timed out, return direct. */
830 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
831 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Joe Perches1b5d2792020-11-20 15:16:09 -0800832 pm8001_dbg(pm8001_ha, FAIL,
833 "TMF task timeout.\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800834 goto ex_err;
835 }
836 }
837
838 if (task->task_status.resp == SAS_TASK_COMPLETE &&
Bart Van Assched377f412021-05-23 19:54:55 -0700839 task->task_status.stat == SAS_SAM_STAT_GOOD) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800840 res = TMF_RESP_FUNC_COMPLETE;
841 break;
842
843 } else {
Joe Perches1b5d2792020-11-20 15:16:09 -0800844 pm8001_dbg(pm8001_ha, EH,
845 " Task to dev %016llx response: 0x%x status 0x%x\n",
846 SAS_ADDR(dev->sas_addr),
847 task->task_status.resp,
848 task->task_status.stat);
Dan Williams4fcf8122011-07-29 17:26:39 -0700849 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800850 task = NULL;
851 }
852 }
853ex_err:
854 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -0700855 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800856 return res;
857}
858
859/**
860 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
861 * @dev: the device structure which sas layer used.
862 */
863static void pm8001_dev_gone_notify(struct domain_device *dev)
864{
865 unsigned long flags = 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800866 struct pm8001_hba_info *pm8001_ha;
867 struct pm8001_device *pm8001_dev = dev->lldd_dev;
Dan Carpenter2471b892010-04-23 14:01:04 +0200868
jack wangdbf9bfe2009-10-14 16:19:21 +0800869 pm8001_ha = pm8001_find_ha_by_dev(dev);
870 spin_lock_irqsave(&pm8001_ha->lock, flags);
jack wangdbf9bfe2009-10-14 16:19:21 +0800871 if (pm8001_dev) {
Dan Carpenter2471b892010-04-23 14:01:04 +0200872 u32 device_id = pm8001_dev->device_id;
873
Joe Perches1b5d2792020-11-20 15:16:09 -0800874 pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
875 pm8001_dev->device_id, pm8001_dev->dev_type);
Viswas G4a2efd42020-11-02 22:25:26 +0530876 if (atomic_read(&pm8001_dev->running_req)) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800877 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
Luo Jiaxing8a23dbc2021-04-08 20:56:32 +0800878 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
879 dev, 1, 0);
Viswas G4a2efd42020-11-02 22:25:26 +0530880 while (atomic_read(&pm8001_dev->running_req))
Deepak Ukey196ba662019-07-09 15:30:48 +0530881 msleep(20);
jack wangdbf9bfe2009-10-14 16:19:21 +0800882 spin_lock_irqsave(&pm8001_ha->lock, flags);
883 }
884 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
885 pm8001_free_dev(pm8001_dev);
886 } else {
Joe Perches1b5d2792020-11-20 15:16:09 -0800887 pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
jack wangdbf9bfe2009-10-14 16:19:21 +0800888 }
889 dev->lldd_dev = NULL;
890 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
891}
892
893void pm8001_dev_gone(struct domain_device *dev)
894{
895 pm8001_dev_gone_notify(dev);
896}
897
898static int pm8001_issue_ssp_tmf(struct domain_device *dev,
899 u8 *lun, struct pm8001_tmf_task *tmf)
900{
901 struct sas_ssp_task ssp_task;
902 if (!(dev->tproto & SAS_PROTOCOL_SSP))
903 return TMF_RESP_FUNC_ESUPP;
904
Lee Jones412b51f2020-11-02 10:25:43 +0000905 memcpy((u8 *)&ssp_task.LUN, lun, 8);
jack wangdbf9bfe2009-10-14 16:19:21 +0800906 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
907 tmf);
908}
909
Mark Salyzyn5954d732012-01-17 11:52:24 -0500910/* retry commands by ha, by task and/or by device */
911void pm8001_open_reject_retry(
912 struct pm8001_hba_info *pm8001_ha,
913 struct sas_task *task_to_close,
914 struct pm8001_device *device_to_close)
915{
916 int i;
917 unsigned long flags;
918
919 if (pm8001_ha == NULL)
920 return;
921
922 spin_lock_irqsave(&pm8001_ha->lock, flags);
923
924 for (i = 0; i < PM8001_MAX_CCB; i++) {
925 struct sas_task *task;
926 struct task_status_struct *ts;
927 struct pm8001_device *pm8001_dev;
928 unsigned long flags1;
929 u32 tag;
930 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
931
932 pm8001_dev = ccb->device;
James Bottomleyaa9f8322013-05-07 14:44:06 -0700933 if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
Mark Salyzyn5954d732012-01-17 11:52:24 -0500934 continue;
935 if (!device_to_close) {
936 uintptr_t d = (uintptr_t)pm8001_dev
937 - (uintptr_t)&pm8001_ha->devices;
938 if (((d % sizeof(*pm8001_dev)) != 0)
939 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
940 continue;
941 } else if (pm8001_dev != device_to_close)
942 continue;
943 tag = ccb->ccb_tag;
944 if (!tag || (tag == 0xFFFFFFFF))
945 continue;
946 task = ccb->task;
947 if (!task || !task->task_done)
948 continue;
949 if (task_to_close && (task != task_to_close))
950 continue;
951 ts = &task->task_status;
952 ts->resp = SAS_TASK_COMPLETE;
953 /* Force the midlayer to retry */
954 ts->stat = SAS_OPEN_REJECT;
955 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
956 if (pm8001_dev)
Viswas G4a2efd42020-11-02 22:25:26 +0530957 atomic_dec(&pm8001_dev->running_req);
Mark Salyzyn5954d732012-01-17 11:52:24 -0500958 spin_lock_irqsave(&task->task_state_lock, flags1);
959 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
960 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
961 task->task_state_flags |= SAS_TASK_STATE_DONE;
962 if (unlikely((task->task_state_flags
963 & SAS_TASK_STATE_ABORTED))) {
964 spin_unlock_irqrestore(&task->task_state_lock,
965 flags1);
966 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
967 } else {
968 spin_unlock_irqrestore(&task->task_state_lock,
969 flags1);
970 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
971 mb();/* in order to force CPU ordering */
972 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
973 task->task_done(task);
974 spin_lock_irqsave(&pm8001_ha->lock, flags);
975 }
976 }
977
978 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
979}
980
jack wangdbf9bfe2009-10-14 16:19:21 +0800981/**
Lee Jones62690c02021-03-03 14:46:12 +0000982 * pm8001_I_T_nexus_reset()
jack wangdbf9bfe2009-10-14 16:19:21 +0800983 * Standard mandates link reset for ATA (type 0) and hard reset for
984 * SSP (type 1) , only for RECOVERY
Lee Jonesa0cf5ce2020-07-21 17:41:18 +0100985 * @dev: the device structure for the device to reset.
jack wangdbf9bfe2009-10-14 16:19:21 +0800986 */
987int pm8001_I_T_nexus_reset(struct domain_device *dev)
988{
989 int rc = TMF_RESP_FUNC_FAILED;
990 struct pm8001_device *pm8001_dev;
991 struct pm8001_hba_info *pm8001_ha;
992 struct sas_phy *phy;
Dan Williamse7db8222012-06-21 23:30:58 -0700993
jack wangdbf9bfe2009-10-14 16:19:21 +0800994 if (!dev || !dev->lldd_dev)
Dan Williamse7db8222012-06-21 23:30:58 -0700995 return -ENODEV;
jack wangdbf9bfe2009-10-14 16:19:21 +0800996
997 pm8001_dev = dev->lldd_dev;
998 pm8001_ha = pm8001_find_ha_by_dev(dev);
Dan Williamsf41a0c42011-12-21 21:33:17 -0800999 phy = sas_get_local_phy(dev);
jack wangdbf9bfe2009-10-14 16:19:21 +08001000
1001 if (dev_is_sata(dev)) {
Dan Williamsf41a0c42011-12-21 21:33:17 -08001002 if (scsi_is_sas_phy_local(phy)) {
1003 rc = 0;
1004 goto out;
1005 }
jack wangdbf9bfe2009-10-14 16:19:21 +08001006 rc = sas_phy_reset(phy, 1);
Viswas G3a1ae962015-08-11 15:06:26 +05301007 if (rc) {
Joe Perches1b5d2792020-11-20 15:16:09 -08001008 pm8001_dbg(pm8001_ha, EH,
1009 "phy reset failed for device %x\n"
1010 "with rc %d\n", pm8001_dev->device_id, rc);
Viswas G3a1ae962015-08-11 15:06:26 +05301011 rc = TMF_RESP_FUNC_FAILED;
1012 goto out;
1013 }
jack wangdbf9bfe2009-10-14 16:19:21 +08001014 msleep(2000);
Luo Jiaxing8a23dbc2021-04-08 20:56:32 +08001015 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1016 dev, 1, 0);
Viswas G3a1ae962015-08-11 15:06:26 +05301017 if (rc) {
Joe Perches1b5d2792020-11-20 15:16:09 -08001018 pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
1019 "with rc %d\n", pm8001_dev->device_id, rc);
Viswas G3a1ae962015-08-11 15:06:26 +05301020 rc = TMF_RESP_FUNC_FAILED;
1021 }
Dan Williamsf41a0c42011-12-21 21:33:17 -08001022 } else {
1023 rc = sas_phy_reset(phy, 1);
1024 msleep(2000);
jack wangdbf9bfe2009-10-14 16:19:21 +08001025 }
Joe Perches1b5d2792020-11-20 15:16:09 -08001026 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1027 pm8001_dev->device_id, rc);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001028 out:
1029 sas_put_local_phy(phy);
jack wangdbf9bfe2009-10-14 16:19:21 +08001030 return rc;
1031}
1032
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301033/*
1034* This function handle the IT_NEXUS_XXX event or completion
1035* status code for SSP/SATA/SMP I/O request.
1036*/
1037int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
1038{
1039 int rc = TMF_RESP_FUNC_FAILED;
1040 struct pm8001_device *pm8001_dev;
1041 struct pm8001_hba_info *pm8001_ha;
1042 struct sas_phy *phy;
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301043
1044 if (!dev || !dev->lldd_dev)
1045 return -1;
1046
1047 pm8001_dev = dev->lldd_dev;
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301048 pm8001_ha = pm8001_find_ha_by_dev(dev);
1049
Joe Perches1b5d2792020-11-20 15:16:09 -08001050 pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301051
1052 phy = sas_get_local_phy(dev);
1053
1054 if (dev_is_sata(dev)) {
1055 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1056 if (scsi_is_sas_phy_local(phy)) {
1057 rc = 0;
1058 goto out;
1059 }
1060 /* send internal ssp/sata/smp abort command to FW */
Luo Jiaxing8a23dbc2021-04-08 20:56:32 +08001061 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1062 dev, 1, 0);
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301063 msleep(100);
1064
1065 /* deregister the target device */
1066 pm8001_dev_gone_notify(dev);
1067 msleep(200);
1068
1069 /*send phy reset to hard reset target */
1070 rc = sas_phy_reset(phy, 1);
1071 msleep(2000);
1072 pm8001_dev->setds_completion = &completion_setstate;
1073
1074 wait_for_completion(&completion_setstate);
1075 } else {
1076 /* send internal ssp/sata/smp abort command to FW */
Luo Jiaxing8a23dbc2021-04-08 20:56:32 +08001077 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1078 dev, 1, 0);
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301079 msleep(100);
1080
1081 /* deregister the target device */
1082 pm8001_dev_gone_notify(dev);
1083 msleep(200);
1084
1085 /*send phy reset to hard reset target */
1086 rc = sas_phy_reset(phy, 1);
1087 msleep(2000);
1088 }
Joe Perches1b5d2792020-11-20 15:16:09 -08001089 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1090 pm8001_dev->device_id, rc);
Sakthivel Ka6cb3d02013-03-19 18:08:40 +05301091out:
1092 sas_put_local_phy(phy);
1093
1094 return rc;
1095}
jack wangdbf9bfe2009-10-14 16:19:21 +08001096/* mandatory SAM-3, the task reset the specified LUN*/
1097int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1098{
1099 int rc = TMF_RESP_FUNC_FAILED;
1100 struct pm8001_tmf_task tmf_task;
1101 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1102 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
Nikith Ganigarakoppal34a9b812013-10-30 16:13:22 +05301103 DECLARE_COMPLETION_ONSTACK(completion_setstate);
jack wangdbf9bfe2009-10-14 16:19:21 +08001104 if (dev_is_sata(dev)) {
Dan Williamsf41a0c42011-12-21 21:33:17 -08001105 struct sas_phy *phy = sas_get_local_phy(dev);
Luo Jiaxing8a23dbc2021-04-08 20:56:32 +08001106 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1107 dev, 1, 0);
jack wangdbf9bfe2009-10-14 16:19:21 +08001108 rc = sas_phy_reset(phy, 1);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001109 sas_put_local_phy(phy);
Nikith Ganigarakoppal34a9b812013-10-30 16:13:22 +05301110 pm8001_dev->setds_completion = &completion_setstate;
jack wangdbf9bfe2009-10-14 16:19:21 +08001111 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
Igor Pylypiv9ec3d4c2021-03-04 22:09:08 -08001112 pm8001_dev, DS_OPERATIONAL);
Nikith Ganigarakoppal34a9b812013-10-30 16:13:22 +05301113 wait_for_completion(&completion_setstate);
jack wangdbf9bfe2009-10-14 16:19:21 +08001114 } else {
1115 tmf_task.tmf = TMF_LU_RESET;
1116 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1117 }
1118 /* If failed, fall-through I_T_Nexus reset */
Joe Perches1b5d2792020-11-20 15:16:09 -08001119 pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
1120 pm8001_dev->device_id, rc);
jack wangdbf9bfe2009-10-14 16:19:21 +08001121 return rc;
1122}
1123
1124/* optional SAM-3 */
1125int pm8001_query_task(struct sas_task *task)
1126{
1127 u32 tag = 0xdeadbeef;
jack wangdbf9bfe2009-10-14 16:19:21 +08001128 struct scsi_lun lun;
1129 struct pm8001_tmf_task tmf_task;
1130 int rc = TMF_RESP_FUNC_FAILED;
1131 if (unlikely(!task || !task->lldd_task || !task->dev))
1132 return rc;
1133
1134 if (task->task_proto & SAS_PROTOCOL_SSP) {
1135 struct scsi_cmnd *cmnd = task->uldd_task;
1136 struct domain_device *dev = task->dev;
1137 struct pm8001_hba_info *pm8001_ha =
1138 pm8001_find_ha_by_dev(dev);
1139
1140 int_to_scsilun(cmnd->device->lun, &lun);
1141 rc = pm8001_find_tag(task, &tag);
1142 if (rc == 0) {
1143 rc = TMF_RESP_FUNC_FAILED;
1144 return rc;
1145 }
Joe Perches1b5d2792020-11-20 15:16:09 -08001146 pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
jack wangdbf9bfe2009-10-14 16:19:21 +08001147 tmf_task.tmf = TMF_QUERY_TASK;
1148 tmf_task.tag_of_task_to_be_managed = tag;
1149
1150 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1151 switch (rc) {
1152 /* The task is still in Lun, release it then */
1153 case TMF_RESP_FUNC_SUCC:
Joe Perches1b5d2792020-11-20 15:16:09 -08001154 pm8001_dbg(pm8001_ha, EH,
1155 "The task is still in Lun\n");
Mark Salyzyn6fbc7692011-09-26 07:57:36 -07001156 break;
jack wangdbf9bfe2009-10-14 16:19:21 +08001157 /* The task is not in Lun or failed, reset the phy */
1158 case TMF_RESP_FUNC_FAILED:
1159 case TMF_RESP_FUNC_COMPLETE:
Joe Perches1b5d2792020-11-20 15:16:09 -08001160 pm8001_dbg(pm8001_ha, EH,
1161 "The task is not in Lun or failed, reset the phy\n");
jack wangdbf9bfe2009-10-14 16:19:21 +08001162 break;
1163 }
1164 }
peter chang73706722019-11-14 15:39:02 +05301165 pr_err("pm80xx: rc= %d\n", rc);
jack wangdbf9bfe2009-10-14 16:19:21 +08001166 return rc;
1167}
1168
Weitao Hou32877942019-05-20 11:24:03 +08001169/* mandatory SAM-3, still need free task/ccb info, abort the specified task */
jack wangdbf9bfe2009-10-14 16:19:21 +08001170int pm8001_abort_task(struct sas_task *task)
1171{
1172 unsigned long flags;
Viswas G1db49902017-10-18 11:39:11 +05301173 u32 tag;
jack wangdbf9bfe2009-10-14 16:19:21 +08001174 struct domain_device *dev ;
Viswas G1db49902017-10-18 11:39:11 +05301175 struct pm8001_hba_info *pm8001_ha;
jack wangdbf9bfe2009-10-14 16:19:21 +08001176 struct scsi_lun lun;
1177 struct pm8001_device *pm8001_dev;
1178 struct pm8001_tmf_task tmf_task;
Viswas G869ddbd2017-10-18 11:39:13 +05301179 int rc = TMF_RESP_FUNC_FAILED, ret;
1180 u32 phy_id;
1181 struct sas_task_slow slow_task;
akshatzena961ea02021-01-09 18:08:43 +05301182
jack wangdbf9bfe2009-10-14 16:19:21 +08001183 if (unlikely(!task || !task->lldd_task || !task->dev))
Viswas G1db49902017-10-18 11:39:11 +05301184 return TMF_RESP_FUNC_FAILED;
akshatzena961ea02021-01-09 18:08:43 +05301185
Viswas G1db49902017-10-18 11:39:11 +05301186 dev = task->dev;
1187 pm8001_dev = dev->lldd_dev;
1188 pm8001_ha = pm8001_find_ha_by_dev(dev);
Viswas G869ddbd2017-10-18 11:39:13 +05301189 phy_id = pm8001_dev->attached_phy;
akshatzena961ea02021-01-09 18:08:43 +05301190
1191 if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
1192 // If the controller is seeing fatal errors
1193 // abort task will not get a response from the controller
1194 return TMF_RESP_FUNC_FAILED;
1195 }
1196
peter chang51c1c5f62019-11-14 15:39:06 +05301197 ret = pm8001_find_tag(task, &tag);
1198 if (ret == 0) {
Joe Perches2ce6e202020-11-23 20:36:03 -08001199 pm8001_info(pm8001_ha, "no tag for task:%p\n", task);
Viswas G1db49902017-10-18 11:39:11 +05301200 return TMF_RESP_FUNC_FAILED;
1201 }
jack wangdbf9bfe2009-10-14 16:19:21 +08001202 spin_lock_irqsave(&task->task_state_lock, flags);
1203 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1204 spin_unlock_irqrestore(&task->task_state_lock, flags);
Viswas G1db49902017-10-18 11:39:11 +05301205 return TMF_RESP_FUNC_COMPLETE;
jack wangdbf9bfe2009-10-14 16:19:21 +08001206 }
Viswas G869ddbd2017-10-18 11:39:13 +05301207 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1208 if (task->slow_task == NULL) {
1209 init_completion(&slow_task.completion);
1210 task->slow_task = &slow_task;
jack wangdbf9bfe2009-10-14 16:19:21 +08001211 }
1212 spin_unlock_irqrestore(&task->task_state_lock, flags);
1213 if (task->task_proto & SAS_PROTOCOL_SSP) {
1214 struct scsi_cmnd *cmnd = task->uldd_task;
jack wangdbf9bfe2009-10-14 16:19:21 +08001215 int_to_scsilun(cmnd->device->lun, &lun);
jack_wang97ee2082009-11-05 22:33:51 +08001216 tmf_task.tmf = TMF_ABORT_TASK;
jack wangdbf9bfe2009-10-14 16:19:21 +08001217 tmf_task.tag_of_task_to_be_managed = tag;
1218 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
jack_wang97ee2082009-11-05 22:33:51 +08001219 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
jack wangdbf9bfe2009-10-14 16:19:21 +08001220 pm8001_dev->sas_device, 0, tag);
1221 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1222 task->task_proto & SAS_PROTOCOL_STP) {
Viswas G869ddbd2017-10-18 11:39:13 +05301223 if (pm8001_ha->chip_id == chip_8006) {
1224 DECLARE_COMPLETION_ONSTACK(completion_reset);
1225 DECLARE_COMPLETION_ONSTACK(completion);
1226 struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1227
1228 /* 1. Set Device state as Recovery */
1229 pm8001_dev->setds_completion = &completion;
1230 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
Igor Pylypiv9ec3d4c2021-03-04 22:09:08 -08001231 pm8001_dev, DS_IN_RECOVERY);
Viswas G869ddbd2017-10-18 11:39:13 +05301232 wait_for_completion(&completion);
1233
1234 /* 2. Send Phy Control Hard Reset */
1235 reinit_completion(&completion);
peter chang51c1c5f62019-11-14 15:39:06 +05301236 phy->port_reset_status = PORT_RESET_TMO;
Viswas G869ddbd2017-10-18 11:39:13 +05301237 phy->reset_success = false;
1238 phy->enable_completion = &completion;
1239 phy->reset_completion = &completion_reset;
1240 ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
1241 PHY_HARD_RESET);
peter chang51c1c5f62019-11-14 15:39:06 +05301242 if (ret) {
1243 phy->enable_completion = NULL;
1244 phy->reset_completion = NULL;
Viswas G869ddbd2017-10-18 11:39:13 +05301245 goto out;
peter chang51c1c5f62019-11-14 15:39:06 +05301246 }
1247
1248 /* In the case of the reset timeout/fail we still
1249 * abort the command at the firmware. The assumption
1250 * here is that the drive is off doing something so
1251 * that it's not processing requests, and we want to
1252 * avoid getting a completion for this and either
1253 * leaking the task in libsas or losing the race and
1254 * getting a double free.
1255 */
Joe Perches1b5d2792020-11-20 15:16:09 -08001256 pm8001_dbg(pm8001_ha, MSG,
1257 "Waiting for local phy ctl\n");
peter chang51c1c5f62019-11-14 15:39:06 +05301258 ret = wait_for_completion_timeout(&completion,
1259 PM8001_TASK_TIMEOUT * HZ);
1260 if (!ret || !phy->reset_success) {
1261 phy->enable_completion = NULL;
1262 phy->reset_completion = NULL;
1263 } else {
1264 /* 3. Wait for Port Reset complete or
1265 * Port reset TMO
1266 */
Joe Perches1b5d2792020-11-20 15:16:09 -08001267 pm8001_dbg(pm8001_ha, MSG,
1268 "Waiting for Port reset\n");
peter chang51c1c5f62019-11-14 15:39:06 +05301269 ret = wait_for_completion_timeout(
1270 &completion_reset,
1271 PM8001_TASK_TIMEOUT * HZ);
1272 if (!ret)
1273 phy->reset_completion = NULL;
1274 WARN_ON(phy->port_reset_status ==
1275 PORT_RESET_TMO);
1276 if (phy->port_reset_status == PORT_RESET_TMO) {
1277 pm8001_dev_gone_notify(dev);
1278 goto out;
1279 }
Deepak Ukey196ba662019-07-09 15:30:48 +05301280 }
Viswas G869ddbd2017-10-18 11:39:13 +05301281
1282 /*
1283 * 4. SATA Abort ALL
1284 * we wait for the task to be aborted so that the task
1285 * is removed from the ccb. on success the caller is
1286 * going to free the task.
1287 */
1288 ret = pm8001_exec_internal_task_abort(pm8001_ha,
1289 pm8001_dev, pm8001_dev->sas_device, 1, tag);
1290 if (ret)
1291 goto out;
1292 ret = wait_for_completion_timeout(
1293 &task->slow_task->completion,
1294 PM8001_TASK_TIMEOUT * HZ);
1295 if (!ret)
1296 goto out;
1297
1298 /* 5. Set Device State as Operational */
1299 reinit_completion(&completion);
1300 pm8001_dev->setds_completion = &completion;
1301 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
Igor Pylypiv9ec3d4c2021-03-04 22:09:08 -08001302 pm8001_dev, DS_OPERATIONAL);
Viswas G869ddbd2017-10-18 11:39:13 +05301303 wait_for_completion(&completion);
1304 } else {
1305 rc = pm8001_exec_internal_task_abort(pm8001_ha,
1306 pm8001_dev, pm8001_dev->sas_device, 0, tag);
jack wangdbf9bfe2009-10-14 16:19:21 +08001307 }
Viswas G869ddbd2017-10-18 11:39:13 +05301308 rc = TMF_RESP_FUNC_COMPLETE;
jack wangdbf9bfe2009-10-14 16:19:21 +08001309 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1310 /* SMP */
jack wangdbf9bfe2009-10-14 16:19:21 +08001311 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1312 pm8001_dev->sas_device, 0, tag);
1313
1314 }
1315out:
Viswas G869ddbd2017-10-18 11:39:13 +05301316 spin_lock_irqsave(&task->task_state_lock, flags);
1317 if (task->slow_task == &slow_task)
1318 task->slow_task = NULL;
1319 spin_unlock_irqrestore(&task->task_state_lock, flags);
jack wangdbf9bfe2009-10-14 16:19:21 +08001320 if (rc != TMF_RESP_FUNC_COMPLETE)
Joe Perches2ce6e202020-11-23 20:36:03 -08001321 pm8001_info(pm8001_ha, "rc= %d\n", rc);
jack wangdbf9bfe2009-10-14 16:19:21 +08001322 return rc;
1323}
1324
1325int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1326{
jack wangdbf9bfe2009-10-14 16:19:21 +08001327 struct pm8001_tmf_task tmf_task;
1328
1329 tmf_task.tmf = TMF_ABORT_TASK_SET;
Colin Ian Kingafcd6092019-07-31 23:22:14 +01001330 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
jack wangdbf9bfe2009-10-14 16:19:21 +08001331}
1332
1333int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1334{
jack wangdbf9bfe2009-10-14 16:19:21 +08001335 struct pm8001_tmf_task tmf_task;
1336
1337 tmf_task.tmf = TMF_CLEAR_ACA;
Colin Ian Kingafcd6092019-07-31 23:22:14 +01001338 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
jack wangdbf9bfe2009-10-14 16:19:21 +08001339}
1340
1341int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1342{
jack wangdbf9bfe2009-10-14 16:19:21 +08001343 struct pm8001_tmf_task tmf_task;
1344 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1345 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1346
Joe Perches1b5d2792020-11-20 15:16:09 -08001347 pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
1348 pm8001_dev->device_id);
jack wangdbf9bfe2009-10-14 16:19:21 +08001349 tmf_task.tmf = TMF_CLEAR_TASK_SET;
Colin Ian Kingafcd6092019-07-31 23:22:14 +01001350 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
jack wangdbf9bfe2009-10-14 16:19:21 +08001351}