blob: 62672720788928ad0e9671194d1ab737cf7b577e [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
2 * Serial Attached SCSI (SAS) class SCSI Host glue.
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
Christoph Hellwigd7a54e32007-04-26 09:38:01 -040026#include <linux/kthread.h>
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -080027#include <linux/firmware.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040028#include <linux/export.h>
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -080029#include <linux/ctype.h>
Andy Shevchenko9ea4e072017-12-19 19:37:27 +020030#include <linux/kernel.h>
Christoph Hellwigd7a54e32007-04-26 09:38:01 -040031
James Bottomley2908d772006-08-29 09:22:51 -050032#include "sas_internal.h"
33
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_device.h>
36#include <scsi/scsi_tcq.h>
37#include <scsi/scsi.h>
Darrick J. Wongf4563932006-10-30 15:18:39 -080038#include <scsi/scsi_eh.h>
James Bottomley2908d772006-08-29 09:22:51 -050039#include <scsi/scsi_transport.h>
40#include <scsi/scsi_transport_sas.h>
Darrick J. Wong338ec572006-10-18 14:43:37 -070041#include <scsi/sas_ata.h>
James Bottomley2908d772006-08-29 09:22:51 -050042#include "../scsi_sas_internal.h"
Darrick J. Wong79a5eb62006-10-30 15:18:50 -080043#include "../scsi_transport_api.h"
Darrick J. Wongad689232007-01-26 14:08:52 -080044#include "../scsi_priv.h"
James Bottomley2908d772006-08-29 09:22:51 -050045
46#include <linux/err.h>
47#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070048#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/gfp.h>
James Bottomley2908d772006-08-29 09:22:51 -050050#include <linux/scatterlist.h>
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -070051#include <linux/libata.h>
James Bottomley2908d772006-08-29 09:22:51 -050052
Dan Williamsa3a14252011-12-06 23:24:42 -080053/* record final status and free the task */
54static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
James Bottomley2908d772006-08-29 09:22:51 -050055{
56 struct task_status_struct *ts = &task->task_status;
James Bottomley2908d772006-08-29 09:22:51 -050057 int hs = 0, stat = 0;
58
James Bottomley2908d772006-08-29 09:22:51 -050059 if (ts->resp == SAS_TASK_UNDELIVERED) {
60 /* transport error */
61 hs = DID_NO_CONNECT;
62 } else { /* ts->resp == SAS_TASK_COMPLETE */
63 /* task delivered, what happened afterwards? */
64 switch (ts->stat) {
65 case SAS_DEV_NO_RESPONSE:
66 case SAS_INTERRUPTED:
67 case SAS_PHY_DOWN:
68 case SAS_NAK_R_ERR:
69 case SAS_OPEN_TO:
70 hs = DID_NO_CONNECT;
71 break;
72 case SAS_DATA_UNDERRUN:
FUJITA Tomonoric13e5562007-05-26 02:46:37 +090073 scsi_set_resid(sc, ts->residual);
74 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
James Bottomley2908d772006-08-29 09:22:51 -050075 hs = DID_ERROR;
76 break;
77 case SAS_DATA_OVERRUN:
78 hs = DID_ERROR;
79 break;
80 case SAS_QUEUE_FULL:
81 hs = DID_SOFT_ERROR; /* retry */
82 break;
83 case SAS_DEVICE_UNKNOWN:
84 hs = DID_BAD_TARGET;
85 break;
86 case SAS_SG_ERR:
87 hs = DID_PARITY;
88 break;
89 case SAS_OPEN_REJECT:
90 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
91 hs = DID_SOFT_ERROR; /* retry */
92 else
93 hs = DID_ERROR;
94 break;
95 case SAS_PROTO_RESPONSE:
96 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
97 "task; please report this\n",
98 task->dev->port->ha->sas_ha_name);
99 break;
100 case SAS_ABORTED_TASK:
101 hs = DID_ABORT;
102 break;
James Bottomleydf64d3c2010-07-27 15:51:13 -0500103 case SAM_STAT_CHECK_CONDITION:
James Bottomley2908d772006-08-29 09:22:51 -0500104 memcpy(sc->sense_buffer, ts->buf,
FUJITA Tomonoricc75e8a2008-01-13 02:20:18 +0900105 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
James Bottomleydf64d3c2010-07-27 15:51:13 -0500106 stat = SAM_STAT_CHECK_CONDITION;
James Bottomley2908d772006-08-29 09:22:51 -0500107 break;
108 default:
109 stat = ts->stat;
110 break;
111 }
112 }
Dan Williamsa3a14252011-12-06 23:24:42 -0800113
James Bottomley2908d772006-08-29 09:22:51 -0500114 sc->result = (hs << 16) | stat;
Dan Williamsa3a14252011-12-06 23:24:42 -0800115 ASSIGN_SAS_TASK(sc, NULL);
James Bottomley2908d772006-08-29 09:22:51 -0500116 sas_free_task(task);
Dan Williamsa3a14252011-12-06 23:24:42 -0800117}
118
119static void sas_scsi_task_done(struct sas_task *task)
120{
121 struct scsi_cmnd *sc = task->uldd_task;
Dan Williams9095a642011-11-28 11:29:20 -0800122 struct domain_device *dev = task->dev;
123 struct sas_ha_struct *ha = dev->port->ha;
124 unsigned long flags;
Dan Williamsa3a14252011-12-06 23:24:42 -0800125
Dan Williams9095a642011-11-28 11:29:20 -0800126 spin_lock_irqsave(&dev->done_lock, flags);
127 if (test_bit(SAS_HA_FROZEN, &ha->state))
128 task = NULL;
129 else
130 ASSIGN_SAS_TASK(sc, NULL);
131 spin_unlock_irqrestore(&dev->done_lock, flags);
132
133 if (unlikely(!task)) {
134 /* task will be completed by the error handler */
Dan Williamsa3a14252011-12-06 23:24:42 -0800135 SAS_DPRINTK("task done but aborted\n");
136 return;
137 }
138
139 if (unlikely(!sc)) {
140 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
Dan Williamsa3a14252011-12-06 23:24:42 -0800141 sas_free_task(task);
142 return;
143 }
144
Dan Williamsa3a14252011-12-06 23:24:42 -0800145 sas_end_task(sc, task);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600146 sc->scsi_done(sc);
James Bottomley2908d772006-08-29 09:22:51 -0500147}
148
James Bottomley2908d772006-08-29 09:22:51 -0500149static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
150 struct domain_device *dev,
Al Viro3cc27542006-09-25 02:55:40 +0100151 gfp_t gfp_flags)
James Bottomley2908d772006-08-29 09:22:51 -0500152{
153 struct sas_task *task = sas_alloc_task(gfp_flags);
154 struct scsi_lun lun;
155
156 if (!task)
157 return NULL;
158
James Bottomley2908d772006-08-29 09:22:51 -0500159 task->uldd_task = cmd;
160 ASSIGN_SAS_TASK(cmd, task);
161
162 task->dev = dev;
163 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
164
165 task->ssp_task.retry_count = 1;
166 int_to_scsilun(cmd->device->lun, &lun);
167 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
Tejun Heo9cbbdca2010-09-03 11:56:16 +0200168 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
James Bottomleye73823f2013-05-07 15:38:18 -0700169 task->ssp_task.cmd = cmd;
James Bottomley2908d772006-08-29 09:22:51 -0500170
FUJITA Tomonoric13e5562007-05-26 02:46:37 +0900171 task->scatter = scsi_sglist(cmd);
172 task->num_scatter = scsi_sg_count(cmd);
173 task->total_xfer_len = scsi_bufflen(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500174 task->data_dir = cmd->sc_data_direction;
175
176 task->task_done = sas_scsi_task_done;
177
178 return task;
179}
180
Christoph Hellwig92bd4012011-07-11 14:49:23 -0400181int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500182{
James Bottomley2908d772006-08-29 09:22:51 -0500183 struct sas_internal *i = to_sas_internal(host->transportt);
Christoph Hellwiga923f752011-07-11 14:49:24 -0400184 struct domain_device *dev = cmd_to_domain_dev(cmd);
Christoph Hellwiga923f752011-07-11 14:49:24 -0400185 struct sas_task *task;
186 int res = 0;
James Bottomley2908d772006-08-29 09:22:51 -0500187
Christoph Hellwiga923f752011-07-11 14:49:24 -0400188 /* If the device fell off, no sense in issuing commands */
Dan Williamse1399422012-01-07 08:52:39 +0000189 if (test_bit(SAS_DEV_GONE, &dev->state)) {
Christoph Hellwiga923f752011-07-11 14:49:24 -0400190 cmd->result = DID_BAD_TARGET << 16;
191 goto out_done;
James Bottomley2908d772006-08-29 09:22:51 -0500192 }
Christoph Hellwiga923f752011-07-11 14:49:24 -0400193
194 if (dev_is_sata(dev)) {
Dan Williams312d3e52011-11-17 17:59:50 -0800195 spin_lock_irq(dev->sata_dev.ap->lock);
Christoph Hellwiga923f752011-07-11 14:49:24 -0400196 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
Dan Williams312d3e52011-11-17 17:59:50 -0800197 spin_unlock_irq(dev->sata_dev.ap->lock);
Christoph Hellwiga923f752011-07-11 14:49:24 -0400198 return res;
199 }
200
201 task = sas_create_task(cmd, dev, GFP_ATOMIC);
202 if (!task)
Christoph Hellwigac81c6a2011-07-16 15:00:35 -0400203 return SCSI_MLQUEUE_HOST_BUSY;
Christoph Hellwiga923f752011-07-11 14:49:24 -0400204
Christoph Hellwig79855d12014-11-05 10:36:28 +0100205 res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
Christoph Hellwiga923f752011-07-11 14:49:24 -0400206 if (res)
207 goto out_free_task;
208 return 0;
209
210out_free_task:
211 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
212 ASSIGN_SAS_TASK(cmd, NULL);
213 sas_free_task(task);
Christoph Hellwigac81c6a2011-07-16 15:00:35 -0400214 if (res == -SAS_QUEUE_FULL)
215 cmd->result = DID_SOFT_ERROR << 16; /* retry */
216 else
217 cmd->result = DID_ERROR << 16;
Christoph Hellwiga923f752011-07-11 14:49:24 -0400218out_done:
219 cmd->scsi_done(cmd);
220 return 0;
James Bottomley2908d772006-08-29 09:22:51 -0500221}
222
James Bottomleya8e14fe2008-02-19 21:48:42 -0600223static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
224{
James Bottomleya8e14fe2008-02-19 21:48:42 -0600225 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
Dan Williams45c73b62012-01-09 10:12:52 -0800226 struct sas_task *task = TO_SAS_TASK(cmd);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600227
Dan Williamsa3a14252011-12-06 23:24:42 -0800228 /* At this point, we only get called following an actual abort
229 * of the task, so we should be guaranteed not to be racing with
230 * any completions from the LLD. Task is freed after this.
231 */
232 sas_end_task(cmd, task);
233
James Bottomleya8e14fe2008-02-19 21:48:42 -0600234 /* now finish the command and move it on to the error
235 * handler done list, this also takes it off the
Dan Williamsa3a14252011-12-06 23:24:42 -0800236 * error handler pending list.
237 */
James Bottomleya8e14fe2008-02-19 21:48:42 -0600238 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
239}
240
Dan Williams3944f502011-11-29 12:08:50 -0800241static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
242{
Dan Williams45c73b62012-01-09 10:12:52 -0800243 struct domain_device *dev = cmd_to_domain_dev(cmd);
Dan Williams3944f502011-11-29 12:08:50 -0800244 struct sas_ha_struct *ha = dev->port->ha;
Dan Williams45c73b62012-01-09 10:12:52 -0800245 struct sas_task *task = TO_SAS_TASK(cmd);
Dan Williams3944f502011-11-29 12:08:50 -0800246
247 if (!dev_is_sata(dev)) {
248 sas_eh_finish_cmd(cmd);
249 return;
250 }
251
252 /* report the timeout to libata */
253 sas_end_task(cmd, task);
254 list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
255}
256
James Bottomley2908d772006-08-29 09:22:51 -0500257static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
258{
259 struct scsi_cmnd *cmd, *n;
260
261 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
James Bottomley63e45632008-02-22 17:07:52 -0600262 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
263 cmd->device->lun == my_cmd->device->lun)
Dan Williams3a2cdf32011-11-29 14:54:28 -0800264 sas_eh_defer_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500265 }
266}
267
268static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
269 struct domain_device *dev)
270{
271 struct scsi_cmnd *cmd, *n;
272
273 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
274 struct domain_device *x = cmd_to_domain_dev(cmd);
275
276 if (x == dev)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600277 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500278 }
279}
280
281static void sas_scsi_clear_queue_port(struct list_head *error_q,
282 struct asd_sas_port *port)
283{
284 struct scsi_cmnd *cmd, *n;
285
286 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
287 struct domain_device *dev = cmd_to_domain_dev(cmd);
288 struct asd_sas_port *x = dev->port;
289
290 if (x == port)
James Bottomleya8e14fe2008-02-19 21:48:42 -0600291 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500292 }
293}
294
295enum task_disposition {
296 TASK_IS_DONE,
297 TASK_IS_ABORTED,
298 TASK_IS_AT_LU,
299 TASK_IS_NOT_AT_LU,
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800300 TASK_ABORT_FAILED,
James Bottomley2908d772006-08-29 09:22:51 -0500301};
302
303static enum task_disposition sas_scsi_find_task(struct sas_task *task)
304{
James Bottomley2908d772006-08-29 09:22:51 -0500305 unsigned long flags;
306 int i, res;
307 struct sas_internal *si =
308 to_sas_internal(task->dev->port->ha->core.shost->transportt);
309
James Bottomley2908d772006-08-29 09:22:51 -0500310 for (i = 0; i < 5; i++) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700311 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500312 res = si->dft->lldd_abort_task(task);
313
314 spin_lock_irqsave(&task->task_state_lock, flags);
315 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
316 spin_unlock_irqrestore(&task->task_state_lock, flags);
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700317 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500318 task);
319 return TASK_IS_DONE;
320 }
321 spin_unlock_irqrestore(&task->task_state_lock, flags);
322
323 if (res == TMF_RESP_FUNC_COMPLETE) {
324 SAS_DPRINTK("%s: task 0x%p is aborted\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700325 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500326 return TASK_IS_ABORTED;
327 } else if (si->dft->lldd_query_task) {
328 SAS_DPRINTK("%s: querying task 0x%p\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700329 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500330 res = si->dft->lldd_query_task(task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800331 switch (res) {
332 case TMF_RESP_FUNC_SUCC:
James Bottomley2908d772006-08-29 09:22:51 -0500333 SAS_DPRINTK("%s: task 0x%p at LU\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700334 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500335 return TASK_IS_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800336 case TMF_RESP_FUNC_COMPLETE:
James Bottomley2908d772006-08-29 09:22:51 -0500337 SAS_DPRINTK("%s: task 0x%p not at LU\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700338 __func__, task);
James Bottomley2908d772006-08-29 09:22:51 -0500339 return TASK_IS_NOT_AT_LU;
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800340 case TMF_RESP_FUNC_FAILED:
341 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700342 __func__, task);
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800343 return TASK_ABORT_FAILED;
344 }
345
James Bottomley2908d772006-08-29 09:22:51 -0500346 }
347 }
348 return res;
349}
350
351static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
352{
353 int res = TMF_RESP_FUNC_FAILED;
354 struct scsi_lun lun;
355 struct sas_internal *i =
356 to_sas_internal(dev->port->ha->core.shost->transportt);
357
358 int_to_scsilun(cmd->device->lun, &lun);
359
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200360 SAS_DPRINTK("eh: device %llx LUN %llx has the task\n",
James Bottomley2908d772006-08-29 09:22:51 -0500361 SAS_ADDR(dev->sas_addr),
362 cmd->device->lun);
363
364 if (i->dft->lldd_abort_task_set)
365 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
366
367 if (res == TMF_RESP_FUNC_FAILED) {
368 if (i->dft->lldd_clear_task_set)
369 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
370 }
371
372 if (res == TMF_RESP_FUNC_FAILED) {
373 if (i->dft->lldd_lu_reset)
374 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
375 }
376
377 return res;
378}
379
380static int sas_recover_I_T(struct domain_device *dev)
381{
382 int res = TMF_RESP_FUNC_FAILED;
383 struct sas_internal *i =
384 to_sas_internal(dev->port->ha->core.shost->transportt);
385
386 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
387 SAS_ADDR(dev->sas_addr));
388
389 if (i->dft->lldd_I_T_nexus_reset)
390 res = i->dft->lldd_I_T_nexus_reset(dev);
391
392 return res;
393}
394
Dan Williamsf41a0c42011-12-21 21:33:17 -0800395/* take a reference on the last known good phy for this device */
396struct sas_phy *sas_get_local_phy(struct domain_device *dev)
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800397{
Dan Williamsf41a0c42011-12-21 21:33:17 -0800398 struct sas_ha_struct *ha = dev->port->ha;
399 struct sas_phy *phy;
400 unsigned long flags;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800401
Dan Williamsf41a0c42011-12-21 21:33:17 -0800402 /* a published domain device always has a valid phy, it may be
403 * stale, but it is never NULL
404 */
405 BUG_ON(!dev->phy);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800406
Dan Williamsf41a0c42011-12-21 21:33:17 -0800407 spin_lock_irqsave(&ha->phy_port_lock, flags);
408 phy = dev->phy;
409 get_device(&phy->dev);
410 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
Darrick J. Wongad689232007-01-26 14:08:52 -0800411
Dan Williamsf41a0c42011-12-21 21:33:17 -0800412 return phy;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800413}
Dan Williamsf41a0c42011-12-21 21:33:17 -0800414EXPORT_SYMBOL_GPL(sas_get_local_phy);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800415
Dan Williams5db45bd2012-06-21 23:30:48 -0700416static void sas_wait_eh(struct domain_device *dev)
417{
418 struct sas_ha_struct *ha = dev->port->ha;
419 DEFINE_WAIT(wait);
420
421 if (dev_is_sata(dev)) {
422 ata_port_wait_eh(dev->sata_dev.ap);
423 return;
424 }
425 retry:
426 spin_lock_irq(&ha->lock);
427
428 while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
429 prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
430 spin_unlock_irq(&ha->lock);
431 schedule();
432 spin_lock_irq(&ha->lock);
433 }
434 finish_wait(&ha->eh_wait_q, &wait);
435
436 spin_unlock_irq(&ha->lock);
437
438 /* make sure SCSI EH is complete */
439 if (scsi_host_in_recovery(ha->core.shost)) {
440 msleep(10);
441 goto retry;
442 }
443}
444EXPORT_SYMBOL(sas_wait_eh);
445
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200446static int sas_queue_reset(struct domain_device *dev, int reset_type,
447 u64 lun, int wait)
Dan Williams5db45bd2012-06-21 23:30:48 -0700448{
449 struct sas_ha_struct *ha = dev->port->ha;
450 int scheduled = 0, tries = 100;
451
452 /* ata: promote lun reset to bus reset */
453 if (dev_is_sata(dev)) {
454 sas_ata_schedule_reset(dev);
455 if (wait)
456 sas_ata_wait_eh(dev);
457 return SUCCESS;
458 }
459
460 while (!scheduled && tries--) {
461 spin_lock_irq(&ha->lock);
462 if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
463 !test_bit(reset_type, &dev->state)) {
464 scheduled = 1;
465 ha->eh_active++;
466 list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
467 set_bit(SAS_DEV_EH_PENDING, &dev->state);
468 set_bit(reset_type, &dev->state);
469 int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
470 scsi_schedule_eh(ha->core.shost);
471 }
472 spin_unlock_irq(&ha->lock);
473
474 if (wait)
475 sas_wait_eh(dev);
476
477 if (scheduled)
478 return SUCCESS;
479 }
480
481 SAS_DPRINTK("%s reset of %s failed\n",
482 reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
483 dev_name(&dev->rphy->dev));
484
485 return FAILED;
486}
487
Dan Williams9524c682012-06-21 23:30:53 -0700488int sas_eh_abort_handler(struct scsi_cmnd *cmd)
489{
490 int res;
491 struct sas_task *task = TO_SAS_TASK(cmd);
492 struct Scsi_Host *host = cmd->device->host;
493 struct sas_internal *i = to_sas_internal(host->transportt);
494
Dan Williams9524c682012-06-21 23:30:53 -0700495 if (!i->dft->lldd_abort_task)
496 return FAILED;
497
498 res = i->dft->lldd_abort_task(task);
499 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
500 return SUCCESS;
501
502 return FAILED;
503}
504EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
505
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800506/* Attempt to send a LUN reset message to a device */
Darrick J. Wongad689232007-01-26 14:08:52 -0800507int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
James Bottomley2908d772006-08-29 09:22:51 -0500508{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800509 int res;
Dan Williams5db45bd2012-06-21 23:30:48 -0700510 struct scsi_lun lun;
511 struct Scsi_Host *host = cmd->device->host;
512 struct domain_device *dev = cmd_to_domain_dev(cmd);
513 struct sas_internal *i = to_sas_internal(host->transportt);
514
515 if (current != host->ehandler)
516 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800517
518 int_to_scsilun(cmd->device->lun, &lun);
519
520 if (!i->dft->lldd_lu_reset)
521 return FAILED;
522
523 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
524 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
525 return SUCCESS;
526
527 return FAILED;
528}
529
Hannes Reineckecc199e72017-08-25 13:57:02 +0200530int sas_eh_target_reset_handler(struct scsi_cmnd *cmd)
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800531{
Darrick J. Wongad689232007-01-26 14:08:52 -0800532 int res;
Dan Williamse7db8222012-06-21 23:30:58 -0700533 struct Scsi_Host *host = cmd->device->host;
534 struct domain_device *dev = cmd_to_domain_dev(cmd);
535 struct sas_internal *i = to_sas_internal(host->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800536
Dan Williams5db45bd2012-06-21 23:30:48 -0700537 if (current != host->ehandler)
538 return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
539
Dan Williamse7db8222012-06-21 23:30:58 -0700540 if (!i->dft->lldd_I_T_nexus_reset)
541 return FAILED;
Dan Williamsf41a0c42011-12-21 21:33:17 -0800542
Dan Williamse7db8222012-06-21 23:30:58 -0700543 res = i->dft->lldd_I_T_nexus_reset(dev);
544 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
545 res == -ENODEV)
Darrick J. Wongad689232007-01-26 14:08:52 -0800546 return SUCCESS;
547
548 return FAILED;
549}
550
551/* Try to reset a device */
James Bottomley8de3ef22008-02-23 23:39:59 -0600552static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
Darrick J. Wongad689232007-01-26 14:08:52 -0800553{
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800554 int res;
James Bottomley8de3ef22008-02-23 23:39:59 -0600555 struct Scsi_Host *shost = cmd->device->host;
Darrick J. Wongad689232007-01-26 14:08:52 -0800556
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800557 if (!shost->hostt->eh_device_reset_handler)
Hannes Reineckecc199e72017-08-25 13:57:02 +0200558 goto try_target_reset;
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800559
560 res = shost->hostt->eh_device_reset_handler(cmd);
561 if (res == SUCCESS)
562 return res;
563
Hannes Reineckecc199e72017-08-25 13:57:02 +0200564try_target_reset:
565 if (shost->hostt->eh_target_reset_handler)
566 return shost->hostt->eh_target_reset_handler(cmd);
Darrick J. Wonga9344e62007-01-29 23:48:19 -0800567
568 return FAILED;
Darrick J. Wongad689232007-01-26 14:08:52 -0800569}
570
Dan Williams84023472012-01-20 15:23:07 -0800571static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
Darrick J. Wongad689232007-01-26 14:08:52 -0800572{
James Bottomley2908d772006-08-29 09:22:51 -0500573 struct scsi_cmnd *cmd, *n;
574 enum task_disposition res = TASK_IS_DONE;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800575 int tmf_resp, need_reset;
James Bottomley2908d772006-08-29 09:22:51 -0500576 struct sas_internal *i = to_sas_internal(shost->transportt);
Darrick J. Wongad689232007-01-26 14:08:52 -0800577 unsigned long flags;
578 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
Dan Williams45c73b62012-01-09 10:12:52 -0800579 LIST_HEAD(done);
James Bottomley2908d772006-08-29 09:22:51 -0500580
Dan Williams45c73b62012-01-09 10:12:52 -0800581 /* clean out any commands that won the completion vs eh race */
Darrick J. Wongad689232007-01-26 14:08:52 -0800582 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
Dan Williams9095a642011-11-28 11:29:20 -0800583 struct domain_device *dev = cmd_to_domain_dev(cmd);
584 struct sas_task *task;
585
586 spin_lock_irqsave(&dev->done_lock, flags);
587 /* by this point the lldd has either observed
588 * SAS_HA_FROZEN and is leaving the task alone, or has
589 * won the race with eh and decided to complete it
590 */
591 task = TO_SAS_TASK(cmd);
592 spin_unlock_irqrestore(&dev->done_lock, flags);
Darrick J. Wongf4563932006-10-30 15:18:39 -0800593
Darrick J. Wongad689232007-01-26 14:08:52 -0800594 if (!task)
Dan Williams45c73b62012-01-09 10:12:52 -0800595 list_move_tail(&cmd->eh_entry, &done);
596 }
597
598 Again:
599 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
600 struct sas_task *task = TO_SAS_TASK(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800601
602 list_del_init(&cmd->eh_entry);
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800603
604 spin_lock_irqsave(&task->task_state_lock, flags);
605 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
Darrick J. Wong3ebf6922007-01-11 14:15:17 -0800606 spin_unlock_irqrestore(&task->task_state_lock, flags);
607
James Bottomley8de3ef22008-02-23 23:39:59 -0600608 if (need_reset) {
609 SAS_DPRINTK("%s: task 0x%p requests reset\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700610 __func__, task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600611 goto reset;
612 }
613
Darrick J. Wongf4563932006-10-30 15:18:39 -0800614 SAS_DPRINTK("trying to find task 0x%p\n", task);
James Bottomley2908d772006-08-29 09:22:51 -0500615 res = sas_scsi_find_task(task);
616
James Bottomley2908d772006-08-29 09:22:51 -0500617 switch (res) {
618 case TASK_IS_DONE:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700619 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -0500620 task);
Dan Williams3944f502011-11-29 12:08:50 -0800621 sas_eh_defer_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500622 continue;
623 case TASK_IS_ABORTED:
624 SAS_DPRINTK("%s: task 0x%p is aborted\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700625 __func__, task);
Dan Williams3944f502011-11-29 12:08:50 -0800626 sas_eh_defer_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500627 continue;
628 case TASK_IS_AT_LU:
629 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
James Bottomley8de3ef22008-02-23 23:39:59 -0600630 reset:
James Bottomley2908d772006-08-29 09:22:51 -0500631 tmf_resp = sas_recover_lu(task->dev, cmd);
632 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200633 SAS_DPRINTK("dev %016llx LU %llx is "
James Bottomley2908d772006-08-29 09:22:51 -0500634 "recovered\n",
635 SAS_ADDR(task->dev),
636 cmd->device->lun);
Dan Williams3a2cdf32011-11-29 14:54:28 -0800637 sas_eh_defer_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800638 sas_scsi_clear_queue_lu(work_q, cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500639 goto Again;
640 }
641 /* fallthrough */
642 case TASK_IS_NOT_AT_LU:
Darrick J. Wong02cd7432007-01-11 14:15:46 -0800643 case TASK_ABORT_FAILED:
James Bottomley2908d772006-08-29 09:22:51 -0500644 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
645 task);
646 tmf_resp = sas_recover_I_T(task->dev);
Dan Williams26a2e682012-01-30 21:40:45 -0800647 if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
648 tmf_resp == -ENODEV) {
James Bottomley8de3ef22008-02-23 23:39:59 -0600649 struct domain_device *dev = task->dev;
James Bottomley2908d772006-08-29 09:22:51 -0500650 SAS_DPRINTK("I_T %016llx recovered\n",
651 SAS_ADDR(task->dev->sas_addr));
James Bottomleya8e14fe2008-02-19 21:48:42 -0600652 sas_eh_finish_cmd(cmd);
James Bottomley8de3ef22008-02-23 23:39:59 -0600653 sas_scsi_clear_queue_I_T(work_q, dev);
James Bottomley2908d772006-08-29 09:22:51 -0500654 goto Again;
655 }
656 /* Hammer time :-) */
James Bottomley8de3ef22008-02-23 23:39:59 -0600657 try_to_reset_cmd_device(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500658 if (i->dft->lldd_clear_nexus_port) {
659 struct asd_sas_port *port = task->dev->port;
660 SAS_DPRINTK("clearing nexus for port:%d\n",
661 port->id);
662 res = i->dft->lldd_clear_nexus_port(port);
663 if (res == TMF_RESP_FUNC_COMPLETE) {
664 SAS_DPRINTK("clear nexus port:%d "
665 "succeeded\n", port->id);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600666 sas_eh_finish_cmd(cmd);
Darrick J. Wongad689232007-01-26 14:08:52 -0800667 sas_scsi_clear_queue_port(work_q,
James Bottomley2908d772006-08-29 09:22:51 -0500668 port);
669 goto Again;
670 }
671 }
672 if (i->dft->lldd_clear_nexus_ha) {
673 SAS_DPRINTK("clear nexus ha\n");
674 res = i->dft->lldd_clear_nexus_ha(ha);
675 if (res == TMF_RESP_FUNC_COMPLETE) {
676 SAS_DPRINTK("clear nexus ha "
677 "succeeded\n");
James Bottomleya8e14fe2008-02-19 21:48:42 -0600678 sas_eh_finish_cmd(cmd);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600679 goto clear_q;
James Bottomley2908d772006-08-29 09:22:51 -0500680 }
681 }
682 /* If we are here -- this means that no amount
683 * of effort could recover from errors. Quite
684 * possibly the HA just disappeared.
685 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200686 SAS_DPRINTK("error from device %llx, LUN %llx "
James Bottomley2908d772006-08-29 09:22:51 -0500687 "couldn't be recovered in any way\n",
688 SAS_ADDR(task->dev->sas_addr),
689 cmd->device->lun);
690
James Bottomleya8e14fe2008-02-19 21:48:42 -0600691 sas_eh_finish_cmd(cmd);
James Bottomley2908d772006-08-29 09:22:51 -0500692 goto clear_q;
693 }
694 }
Dan Williams45c73b62012-01-09 10:12:52 -0800695 out:
696 list_splice_tail(&done, work_q);
Dan Williams3944f502011-11-29 12:08:50 -0800697 list_splice_tail_init(&ha->eh_ata_q, work_q);
Dan Williams84023472012-01-20 15:23:07 -0800698 return;
Dan Williams45c73b62012-01-09 10:12:52 -0800699
700 clear_q:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700701 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
James Bottomleya8e14fe2008-02-19 21:48:42 -0600702 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
703 sas_eh_finish_cmd(cmd);
Dan Williams45c73b62012-01-09 10:12:52 -0800704 goto out;
Darrick J. Wongad689232007-01-26 14:08:52 -0800705}
706
Dan Williams5db45bd2012-06-21 23:30:48 -0700707static void sas_eh_handle_resets(struct Scsi_Host *shost)
708{
709 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
710 struct sas_internal *i = to_sas_internal(shost->transportt);
711
712 /* handle directed resets to sas devices */
713 spin_lock_irq(&ha->lock);
714 while (!list_empty(&ha->eh_dev_q)) {
715 struct domain_device *dev;
716 struct ssp_device *ssp;
717
718 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
719 list_del_init(&ssp->eh_list_node);
720 dev = container_of(ssp, typeof(*dev), ssp_dev);
721 kref_get(&dev->kref);
722 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
723
724 spin_unlock_irq(&ha->lock);
725
726 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
727 i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
728
729 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
730 i->dft->lldd_I_T_nexus_reset(dev);
731
732 sas_put_device(dev);
733 spin_lock_irq(&ha->lock);
734 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
735 ha->eh_active--;
736 }
737 spin_unlock_irq(&ha->lock);
738}
739
Dan Williamse4a9c372012-06-21 23:25:27 -0700740
Darrick J. Wongad689232007-01-26 14:08:52 -0800741void sas_scsi_recover_host(struct Scsi_Host *shost)
742{
743 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
Darrick J. Wongad689232007-01-26 14:08:52 -0800744 LIST_HEAD(eh_work_q);
Dan Williamse4a9c372012-06-21 23:25:27 -0700745 int tries = 0;
746 bool retry;
Darrick J. Wongad689232007-01-26 14:08:52 -0800747
Dan Williamse4a9c372012-06-21 23:25:27 -0700748retry:
749 tries++;
750 retry = true;
751 spin_lock_irq(shost->host_lock);
Darrick J. Wongad689232007-01-26 14:08:52 -0800752 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
Dan Williamse4a9c372012-06-21 23:25:27 -0700753 spin_unlock_irq(shost->host_lock);
Darrick J. Wongad689232007-01-26 14:08:52 -0800754
Dan Williamsd230ce62012-01-11 12:08:36 -0800755 SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
Christoph Hellwig74665012014-01-22 15:29:29 +0100756 __func__, atomic_read(&shost->host_busy), shost->host_failed);
Darrick J. Wongad689232007-01-26 14:08:52 -0800757 /*
758 * Deal with commands that still have SAS tasks (i.e. they didn't
Dan Williams84023472012-01-20 15:23:07 -0800759 * complete via the normal sas_task completion mechanism),
760 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
Darrick J. Wongad689232007-01-26 14:08:52 -0800761 */
Dan Williams9095a642011-11-28 11:29:20 -0800762 set_bit(SAS_HA_FROZEN, &ha->state);
Dan Williams84023472012-01-20 15:23:07 -0800763 sas_eh_handle_sas_errors(shost, &eh_work_q);
764 clear_bit(SAS_HA_FROZEN, &ha->state);
765 if (list_empty(&eh_work_q))
Darrick J. Wongad689232007-01-26 14:08:52 -0800766 goto out;
767
768 /*
769 * Now deal with SCSI commands that completed ok but have a an error
770 * code (and hopefully sense data) attached. This is roughly what
771 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
772 * command we see here has no sas_task and is thus unknown to the HA.
773 */
Dan Williamsd230ce62012-01-11 12:08:36 -0800774 sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
775 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
776 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
Darrick J. Wongad689232007-01-26 14:08:52 -0800777
778out:
Dan Williams5db45bd2012-06-21 23:30:48 -0700779 sas_eh_handle_resets(shost);
780
James Bottomley00dd4992011-01-23 09:44:12 -0600781 /* now link into libata eh --- if we have any ata devices */
782 sas_ata_strategy_handler(shost);
783
Darrick J. Wongad689232007-01-26 14:08:52 -0800784 scsi_eh_flush_done_q(&ha->eh_done_q);
James Bottomley00dd4992011-01-23 09:44:12 -0600785
Dan Williamse4a9c372012-06-21 23:25:27 -0700786 /* check if any new eh work was scheduled during the last run */
787 spin_lock_irq(&ha->lock);
788 if (ha->eh_active == 0) {
789 shost->host_eh_scheduled = 0;
790 retry = false;
791 }
792 spin_unlock_irq(&ha->lock);
793
794 if (retry)
795 goto retry;
796
797 SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
Christoph Hellwig74665012014-01-22 15:29:29 +0100798 __func__, atomic_read(&shost->host_busy),
799 shost->host_failed, tries);
James Bottomley2908d772006-08-29 09:22:51 -0500800}
801
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700802int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
803{
804 struct domain_device *dev = sdev_to_domain_dev(sdev);
805
806 if (dev_is_sata(dev))
Jeff Garzik94be9a52009-01-16 10:17:09 -0500807 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700808
809 return -EINVAL;
810}
811
James Bottomley2908d772006-08-29 09:22:51 -0500812struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
813{
814 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
815 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
816 struct domain_device *found_dev = NULL;
817 int i;
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800818 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500819
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800820 spin_lock_irqsave(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500821 for (i = 0; i < ha->num_phys; i++) {
822 struct asd_sas_port *port = ha->sas_port[i];
823 struct domain_device *dev;
824
825 spin_lock(&port->dev_list_lock);
826 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
827 if (rphy == dev->rphy) {
828 found_dev = dev;
829 spin_unlock(&port->dev_list_lock);
830 goto found;
831 }
832 }
833 spin_unlock(&port->dev_list_lock);
834 }
835 found:
Darrick J. Wong980fa2f2007-01-11 14:15:40 -0800836 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500837
838 return found_dev;
839}
840
James Bottomley2908d772006-08-29 09:22:51 -0500841int sas_target_alloc(struct scsi_target *starget)
842{
Dan Williams735f7d22011-11-17 17:59:47 -0800843 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
844 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
James Bottomley2908d772006-08-29 09:22:51 -0500845
846 if (!found_dev)
847 return -ENODEV;
848
Dan Williams735f7d22011-11-17 17:59:47 -0800849 kref_get(&found_dev->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500850 starget->hostdata = found_dev;
851 return 0;
852}
853
Dan Williams97a14202011-09-20 15:10:46 -0700854#define SAS_DEF_QD 256
James Bottomley2908d772006-08-29 09:22:51 -0500855
856int sas_slave_configure(struct scsi_device *scsi_dev)
857{
858 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
James Bottomley2908d772006-08-29 09:22:51 -0500859
860 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
861
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700862 if (dev_is_sata(dev)) {
863 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
864 return 0;
865 }
866
James Bottomley2908d772006-08-29 09:22:51 -0500867 sas_read_port_mode_page(scsi_dev);
868
869 if (scsi_dev->tagged_supported) {
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100870 scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
James Bottomley2908d772006-08-29 09:22:51 -0500871 } else {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200872 SAS_DPRINTK("device %llx, LUN %llx doesn't support "
James Bottomley2908d772006-08-29 09:22:51 -0500873 "TCQ\n", SAS_ADDR(dev->sas_addr),
874 scsi_dev->lun);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100875 scsi_change_queue_depth(scsi_dev, 1);
James Bottomley2908d772006-08-29 09:22:51 -0500876 }
877
Darrick J. Wongf27708f2007-01-26 14:08:58 -0800878 scsi_dev->allow_restart = 1;
879
James Bottomley2908d772006-08-29 09:22:51 -0500880 return 0;
881}
882
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100883int sas_change_queue_depth(struct scsi_device *sdev, int depth)
James Bottomley2908d772006-08-29 09:22:51 -0500884{
Dan Williams97a14202011-09-20 15:10:46 -0700885 struct domain_device *dev = sdev_to_domain_dev(sdev);
James Bottomley2908d772006-08-29 09:22:51 -0500886
Dan Williamsf6e67032011-09-20 15:10:33 -0700887 if (dev_is_sata(dev))
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100888 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
Dan Williamsf6e67032011-09-20 15:10:33 -0700889
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100890 if (!sdev->tagged_supported)
891 depth = 1;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100892 return scsi_change_queue_depth(sdev, depth);
James Bottomley2908d772006-08-29 09:22:51 -0500893}
894
James Bottomley2908d772006-08-29 09:22:51 -0500895int sas_bios_param(struct scsi_device *scsi_dev,
896 struct block_device *bdev,
897 sector_t capacity, int *hsc)
898{
899 hsc[0] = 255;
900 hsc[1] = 63;
901 sector_div(capacity, 255*63);
902 hsc[2] = capacity;
903
904 return 0;
905}
906
Darrick J. Wong396819f2007-01-11 14:15:20 -0800907/*
Darrick J. Wong396819f2007-01-11 14:15:20 -0800908 * Tell an upper layer that it needs to initiate an abort for a given task.
909 * This should only ever be called by an LLDD.
910 */
911void sas_task_abort(struct sas_task *task)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800912{
Darrick J. Wong396819f2007-01-11 14:15:20 -0800913 struct scsi_cmnd *sc = task->uldd_task;
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800914
Darrick J. Wong396819f2007-01-11 14:15:20 -0800915 /* Escape for libsas internal commands */
916 if (!sc) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700917 struct sas_task_slow *slow = task->slow_task;
918
919 if (!slow)
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800920 return;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700921 if (!del_timer(&slow->timer))
922 return;
Kees Cook841b86f2017-10-23 09:40:42 +0200923 slow->timer.function(&slow->timer);
Darrick J. Wong396819f2007-01-11 14:15:20 -0800924 return;
925 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800926
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800927 if (dev_is_sata(task->dev)) {
928 sas_ata_task_abort(task);
James Bottomley1b4d0d82010-05-13 09:31:54 -0500929 } else {
930 struct request_queue *q = sc->device->request_queue;
931 unsigned long flags;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800932
James Bottomley1b4d0d82010-05-13 09:31:54 -0500933 spin_lock_irqsave(q->queue_lock, flags);
934 blk_abort_request(sc->request);
935 spin_unlock_irqrestore(q->queue_lock, flags);
James Bottomley1b4d0d82010-05-13 09:31:54 -0500936 }
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800937}
938
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700939void sas_target_destroy(struct scsi_target *starget)
940{
Dan Williams735f7d22011-11-17 17:59:47 -0800941 struct domain_device *found_dev = starget->hostdata;
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700942
943 if (!found_dev)
944 return;
945
Dan Williams735f7d22011-11-17 17:59:47 -0800946 starget->hostdata = NULL;
947 sas_put_device(found_dev);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700948}
949
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -0800950#define SAS_STRING_ADDR_SIZE 16
951
952int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
953{
954 int res;
955 const struct firmware *fw;
956
957 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
958 if (res)
959 return res;
960
961 if (fw->size < SAS_STRING_ADDR_SIZE) {
962 res = -ENODEV;
963 goto out;
964 }
965
Andy Shevchenko9ea4e072017-12-19 19:37:27 +0200966 res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2);
967 if (res)
968 goto out;
Darrick J. Wong45e6cdf2008-02-19 10:49:40 -0800969
970out:
971 release_firmware(fw);
972 return res;
973}
974EXPORT_SYMBOL_GPL(sas_request_addr);
975
James Bottomley2908d772006-08-29 09:22:51 -0500976EXPORT_SYMBOL_GPL(sas_queuecommand);
977EXPORT_SYMBOL_GPL(sas_target_alloc);
978EXPORT_SYMBOL_GPL(sas_slave_configure);
James Bottomley2908d772006-08-29 09:22:51 -0500979EXPORT_SYMBOL_GPL(sas_change_queue_depth);
James Bottomley2908d772006-08-29 09:22:51 -0500980EXPORT_SYMBOL_GPL(sas_bios_param);
Darrick J. Wong79a5eb62006-10-30 15:18:50 -0800981EXPORT_SYMBOL_GPL(sas_task_abort);
Darrick J. Wongdea22212006-11-07 17:28:55 -0800982EXPORT_SYMBOL_GPL(sas_phy_reset);
Darrick J. Wongad689232007-01-26 14:08:52 -0800983EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
Hannes Reineckecc199e72017-08-25 13:57:02 +0200984EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler);
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700985EXPORT_SYMBOL_GPL(sas_target_destroy);
986EXPORT_SYMBOL_GPL(sas_ioctl);