Commit | Line | Data |
---|---|---|
ba6d10ab | 1 | // SPDX-License-Identifier: GPL-2.0-only |
2908d778 JB |
2 | /* |
3 | * Serial Attached SCSI (SAS) class SCSI Host glue. | |
4 | * | |
5 | * Copyright (C) 2005 Adaptec, Inc. All rights reserved. | |
6 | * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> | |
2908d778 JB |
7 | */ |
8 | ||
d7a54e30 | 9 | #include <linux/kthread.h> |
45e6cdf4 | 10 | #include <linux/firmware.h> |
09703660 | 11 | #include <linux/export.h> |
45e6cdf4 | 12 | #include <linux/ctype.h> |
9ea4e076 | 13 | #include <linux/kernel.h> |
d7a54e30 | 14 | |
2908d778 JB |
15 | #include "sas_internal.h" |
16 | ||
17 | #include <scsi/scsi_host.h> | |
18 | #include <scsi/scsi_device.h> | |
19 | #include <scsi/scsi_tcq.h> | |
20 | #include <scsi/scsi.h> | |
f456393e | 21 | #include <scsi/scsi_eh.h> |
2908d778 JB |
22 | #include <scsi/scsi_transport.h> |
23 | #include <scsi/scsi_transport_sas.h> | |
338ec570 | 24 | #include <scsi/sas_ata.h> |
2908d778 | 25 | #include "../scsi_sas_internal.h" |
79a5eb60 | 26 | #include "../scsi_transport_api.h" |
ad689233 | 27 | #include "../scsi_priv.h" |
2908d778 JB |
28 | |
29 | #include <linux/err.h> | |
30 | #include <linux/blkdev.h> | |
83144186 | 31 | #include <linux/freezer.h> |
5a0e3ad6 | 32 | #include <linux/gfp.h> |
2908d778 | 33 | #include <linux/scatterlist.h> |
fa1c1e8f | 34 | #include <linux/libata.h> |
2908d778 | 35 | |
a3a14252 DW |
36 | /* record final status and free the task */ |
37 | static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task) | |
2908d778 JB |
38 | { |
39 | struct task_status_struct *ts = &task->task_status; | |
2908d778 JB |
40 | int hs = 0, stat = 0; |
41 | ||
2908d778 JB |
42 | if (ts->resp == SAS_TASK_UNDELIVERED) { |
43 | /* transport error */ | |
44 | hs = DID_NO_CONNECT; | |
45 | } else { /* ts->resp == SAS_TASK_COMPLETE */ | |
46 | /* task delivered, what happened afterwards? */ | |
47 | switch (ts->stat) { | |
48 | case SAS_DEV_NO_RESPONSE: | |
49 | case SAS_INTERRUPTED: | |
50 | case SAS_PHY_DOWN: | |
51 | case SAS_NAK_R_ERR: | |
52 | case SAS_OPEN_TO: | |
53 | hs = DID_NO_CONNECT; | |
54 | break; | |
55 | case SAS_DATA_UNDERRUN: | |
c13e5566 FT |
56 | scsi_set_resid(sc, ts->residual); |
57 | if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow) | |
2908d778 JB |
58 | hs = DID_ERROR; |
59 | break; | |
60 | case SAS_DATA_OVERRUN: | |
61 | hs = DID_ERROR; | |
62 | break; | |
63 | case SAS_QUEUE_FULL: | |
64 | hs = DID_SOFT_ERROR; /* retry */ | |
65 | break; | |
66 | case SAS_DEVICE_UNKNOWN: | |
67 | hs = DID_BAD_TARGET; | |
68 | break; | |
69 | case SAS_SG_ERR: | |
70 | hs = DID_PARITY; | |
71 | break; | |
72 | case SAS_OPEN_REJECT: | |
73 | if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY) | |
74 | hs = DID_SOFT_ERROR; /* retry */ | |
75 | else | |
76 | hs = DID_ERROR; | |
77 | break; | |
78 | case SAS_PROTO_RESPONSE: | |
15ba7806 JG |
79 | pr_notice("LLDD:%s sent SAS_PROTO_RESP for an SSP task; please report this\n", |
80 | task->dev->port->ha->sas_ha_name); | |
2908d778 JB |
81 | break; |
82 | case SAS_ABORTED_TASK: | |
83 | hs = DID_ABORT; | |
84 | break; | |
df64d3ca | 85 | case SAM_STAT_CHECK_CONDITION: |
2908d778 | 86 | memcpy(sc->sense_buffer, ts->buf, |
cc75e8ab | 87 | min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size)); |
df64d3ca | 88 | stat = SAM_STAT_CHECK_CONDITION; |
2908d778 JB |
89 | break; |
90 | default: | |
91 | stat = ts->stat; | |
92 | break; | |
93 | } | |
94 | } | |
a3a14252 | 95 | |
2908d778 | 96 | sc->result = (hs << 16) | stat; |
a3a14252 | 97 | ASSIGN_SAS_TASK(sc, NULL); |
2908d778 | 98 | sas_free_task(task); |
a3a14252 DW |
99 | } |
100 | ||
101 | static void sas_scsi_task_done(struct sas_task *task) | |
102 | { | |
103 | struct scsi_cmnd *sc = task->uldd_task; | |
9095a64a DW |
104 | struct domain_device *dev = task->dev; |
105 | struct sas_ha_struct *ha = dev->port->ha; | |
106 | unsigned long flags; | |
107 | ||
108 | spin_lock_irqsave(&dev->done_lock, flags); | |
109 | if (test_bit(SAS_HA_FROZEN, &ha->state)) | |
110 | task = NULL; | |
111 | else | |
112 | ASSIGN_SAS_TASK(sc, NULL); | |
113 | spin_unlock_irqrestore(&dev->done_lock, flags); | |
a3a14252 | 114 | |
9095a64a DW |
115 | if (unlikely(!task)) { |
116 | /* task will be completed by the error handler */ | |
15ba7806 | 117 | pr_debug("task done but aborted\n"); |
a3a14252 DW |
118 | return; |
119 | } | |
120 | ||
121 | if (unlikely(!sc)) { | |
15ba7806 | 122 | pr_debug("task_done called with non existing SCSI cmnd!\n"); |
a3a14252 DW |
123 | sas_free_task(task); |
124 | return; | |
125 | } | |
126 | ||
a3a14252 | 127 | sas_end_task(sc, task); |
a8e14fec | 128 | sc->scsi_done(sc); |
2908d778 JB |
129 | } |
130 | ||
2908d778 JB |
131 | static struct sas_task *sas_create_task(struct scsi_cmnd *cmd, |
132 | struct domain_device *dev, | |
3cc27547 | 133 | gfp_t gfp_flags) |
2908d778 JB |
134 | { |
135 | struct sas_task *task = sas_alloc_task(gfp_flags); | |
136 | struct scsi_lun lun; | |
137 | ||
138 | if (!task) | |
139 | return NULL; | |
140 | ||
2908d778 JB |
141 | task->uldd_task = cmd; |
142 | ASSIGN_SAS_TASK(cmd, task); | |
143 | ||
144 | task->dev = dev; | |
145 | task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ | |
146 | ||
147 | task->ssp_task.retry_count = 1; | |
148 | int_to_scsilun(cmd->device->lun, &lun); | |
149 | memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); | |
9cbbdca4 | 150 | task->ssp_task.task_attr = TASK_ATTR_SIMPLE; |
e73823f7 | 151 | task->ssp_task.cmd = cmd; |
2908d778 | 152 | |
c13e5566 FT |
153 | task->scatter = scsi_sglist(cmd); |
154 | task->num_scatter = scsi_sg_count(cmd); | |
155 | task->total_xfer_len = scsi_bufflen(cmd); | |
2908d778 JB |
156 | task->data_dir = cmd->sc_data_direction; |
157 | ||
158 | task->task_done = sas_scsi_task_done; | |
159 | ||
160 | return task; | |
161 | } | |
162 | ||
92bd401b | 163 | int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
2908d778 | 164 | { |
2908d778 | 165 | struct sas_internal *i = to_sas_internal(host->transportt); |
a923f756 | 166 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
a923f756 CH |
167 | struct sas_task *task; |
168 | int res = 0; | |
2908d778 | 169 | |
a923f756 | 170 | /* If the device fell off, no sense in issuing commands */ |
e139942d | 171 | if (test_bit(SAS_DEV_GONE, &dev->state)) { |
a923f756 CH |
172 | cmd->result = DID_BAD_TARGET << 16; |
173 | goto out_done; | |
174 | } | |
2908d778 | 175 | |
a923f756 | 176 | if (dev_is_sata(dev)) { |
312d3e56 | 177 | spin_lock_irq(dev->sata_dev.ap->lock); |
a923f756 | 178 | res = ata_sas_queuecmd(cmd, dev->sata_dev.ap); |
312d3e56 | 179 | spin_unlock_irq(dev->sata_dev.ap->lock); |
a923f756 CH |
180 | return res; |
181 | } | |
3eb7a51a | 182 | |
a923f756 CH |
183 | task = sas_create_task(cmd, dev, GFP_ATOMIC); |
184 | if (!task) | |
ac81c6a8 | 185 | return SCSI_MLQUEUE_HOST_BUSY; |
fa1c1e8f | 186 | |
79855d17 | 187 | res = i->dft->lldd_execute_task(task, GFP_ATOMIC); |
a923f756 CH |
188 | if (res) |
189 | goto out_free_task; | |
190 | return 0; | |
191 | ||
192 | out_free_task: | |
15ba7806 | 193 | pr_debug("lldd_execute_task returned: %d\n", res); |
a923f756 CH |
194 | ASSIGN_SAS_TASK(cmd, NULL); |
195 | sas_free_task(task); | |
ac81c6a8 CH |
196 | if (res == -SAS_QUEUE_FULL) |
197 | cmd->result = DID_SOFT_ERROR << 16; /* retry */ | |
198 | else | |
199 | cmd->result = DID_ERROR << 16; | |
a923f756 CH |
200 | out_done: |
201 | cmd->scsi_done(cmd); | |
202 | return 0; | |
2908d778 JB |
203 | } |
204 | ||
a8e14fec JB |
205 | static void sas_eh_finish_cmd(struct scsi_cmnd *cmd) |
206 | { | |
a8e14fec | 207 | struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host); |
318aaf34 | 208 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
45c73b65 | 209 | struct sas_task *task = TO_SAS_TASK(cmd); |
a8e14fec | 210 | |
a3a14252 DW |
211 | /* At this point, we only get called following an actual abort |
212 | * of the task, so we should be guaranteed not to be racing with | |
213 | * any completions from the LLD. Task is freed after this. | |
214 | */ | |
215 | sas_end_task(cmd, task); | |
216 | ||
318aaf34 JY |
217 | if (dev_is_sata(dev)) { |
218 | /* defer commands to libata so that libata EH can | |
219 | * handle ata qcs correctly | |
220 | */ | |
221 | list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q); | |
222 | return; | |
223 | } | |
224 | ||
a8e14fec JB |
225 | /* now finish the command and move it on to the error |
226 | * handler done list, this also takes it off the | |
a3a14252 DW |
227 | * error handler pending list. |
228 | */ | |
a8e14fec JB |
229 | scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q); |
230 | } | |
231 | ||
2908d778 JB |
232 | static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) |
233 | { | |
234 | struct scsi_cmnd *cmd, *n; | |
235 | ||
236 | list_for_each_entry_safe(cmd, n, error_q, eh_entry) { | |
63e4563b JB |
237 | if (cmd->device->sdev_target == my_cmd->device->sdev_target && |
238 | cmd->device->lun == my_cmd->device->lun) | |
318aaf34 | 239 | sas_eh_finish_cmd(cmd); |
2908d778 JB |
240 | } |
241 | } | |
242 | ||
243 | static void sas_scsi_clear_queue_I_T(struct list_head *error_q, | |
244 | struct domain_device *dev) | |
245 | { | |
246 | struct scsi_cmnd *cmd, *n; | |
247 | ||
248 | list_for_each_entry_safe(cmd, n, error_q, eh_entry) { | |
249 | struct domain_device *x = cmd_to_domain_dev(cmd); | |
250 | ||
251 | if (x == dev) | |
a8e14fec | 252 | sas_eh_finish_cmd(cmd); |
2908d778 JB |
253 | } |
254 | } | |
255 | ||
256 | static void sas_scsi_clear_queue_port(struct list_head *error_q, | |
257 | struct asd_sas_port *port) | |
258 | { | |
259 | struct scsi_cmnd *cmd, *n; | |
260 | ||
261 | list_for_each_entry_safe(cmd, n, error_q, eh_entry) { | |
262 | struct domain_device *dev = cmd_to_domain_dev(cmd); | |
263 | struct asd_sas_port *x = dev->port; | |
264 | ||
265 | if (x == port) | |
a8e14fec | 266 | sas_eh_finish_cmd(cmd); |
2908d778 JB |
267 | } |
268 | } | |
269 | ||
270 | enum task_disposition { | |
271 | TASK_IS_DONE, | |
272 | TASK_IS_ABORTED, | |
273 | TASK_IS_AT_LU, | |
274 | TASK_IS_NOT_AT_LU, | |
02cd743b | 275 | TASK_ABORT_FAILED, |
2908d778 JB |
276 | }; |
277 | ||
278 | static enum task_disposition sas_scsi_find_task(struct sas_task *task) | |
279 | { | |
2908d778 JB |
280 | unsigned long flags; |
281 | int i, res; | |
282 | struct sas_internal *si = | |
283 | to_sas_internal(task->dev->port->ha->core.shost->transportt); | |
284 | ||
2908d778 | 285 | for (i = 0; i < 5; i++) { |
15ba7806 | 286 | pr_notice("%s: aborting task 0x%p\n", __func__, task); |
2908d778 JB |
287 | res = si->dft->lldd_abort_task(task); |
288 | ||
289 | spin_lock_irqsave(&task->task_state_lock, flags); | |
290 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { | |
291 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
15ba7806 | 292 | pr_debug("%s: task 0x%p is done\n", __func__, task); |
2908d778 JB |
293 | return TASK_IS_DONE; |
294 | } | |
295 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
296 | ||
297 | if (res == TMF_RESP_FUNC_COMPLETE) { | |
15ba7806 JG |
298 | pr_notice("%s: task 0x%p is aborted\n", |
299 | __func__, task); | |
2908d778 JB |
300 | return TASK_IS_ABORTED; |
301 | } else if (si->dft->lldd_query_task) { | |
15ba7806 | 302 | pr_notice("%s: querying task 0x%p\n", __func__, task); |
2908d778 | 303 | res = si->dft->lldd_query_task(task); |
02cd743b DW |
304 | switch (res) { |
305 | case TMF_RESP_FUNC_SUCC: | |
15ba7806 JG |
306 | pr_notice("%s: task 0x%p at LU\n", __func__, |
307 | task); | |
2908d778 | 308 | return TASK_IS_AT_LU; |
02cd743b | 309 | case TMF_RESP_FUNC_COMPLETE: |
15ba7806 JG |
310 | pr_notice("%s: task 0x%p not at LU\n", |
311 | __func__, task); | |
2908d778 | 312 | return TASK_IS_NOT_AT_LU; |
02cd743b | 313 | case TMF_RESP_FUNC_FAILED: |
15ba7806 JG |
314 | pr_notice("%s: task 0x%p failed to abort\n", |
315 | __func__, task); | |
316 | return TASK_ABORT_FAILED; | |
317 | } | |
02cd743b | 318 | |
2908d778 JB |
319 | } |
320 | } | |
321 | return res; | |
322 | } | |
323 | ||
324 | static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) | |
325 | { | |
326 | int res = TMF_RESP_FUNC_FAILED; | |
327 | struct scsi_lun lun; | |
328 | struct sas_internal *i = | |
329 | to_sas_internal(dev->port->ha->core.shost->transportt); | |
330 | ||
331 | int_to_scsilun(cmd->device->lun, &lun); | |
332 | ||
b3e3d4c6 | 333 | pr_notice("eh: device %016llx LUN 0x%llx has the task\n", |
15ba7806 JG |
334 | SAS_ADDR(dev->sas_addr), |
335 | cmd->device->lun); | |
2908d778 JB |
336 | |
337 | if (i->dft->lldd_abort_task_set) | |
338 | res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); | |
339 | ||
340 | if (res == TMF_RESP_FUNC_FAILED) { | |
341 | if (i->dft->lldd_clear_task_set) | |
342 | res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); | |
343 | } | |
344 | ||
345 | if (res == TMF_RESP_FUNC_FAILED) { | |
346 | if (i->dft->lldd_lu_reset) | |
347 | res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); | |
348 | } | |
349 | ||
350 | return res; | |
351 | } | |
352 | ||
353 | static int sas_recover_I_T(struct domain_device *dev) | |
354 | { | |
355 | int res = TMF_RESP_FUNC_FAILED; | |
356 | struct sas_internal *i = | |
357 | to_sas_internal(dev->port->ha->core.shost->transportt); | |
358 | ||
15ba7806 JG |
359 | pr_notice("I_T nexus reset for dev %016llx\n", |
360 | SAS_ADDR(dev->sas_addr)); | |
2908d778 JB |
361 | |
362 | if (i->dft->lldd_I_T_nexus_reset) | |
363 | res = i->dft->lldd_I_T_nexus_reset(dev); | |
364 | ||
365 | return res; | |
366 | } | |
367 | ||
f41a0c44 DW |
368 | /* take a reference on the last known good phy for this device */ |
369 | struct sas_phy *sas_get_local_phy(struct domain_device *dev) | |
3ebf6922 | 370 | { |
f41a0c44 DW |
371 | struct sas_ha_struct *ha = dev->port->ha; |
372 | struct sas_phy *phy; | |
373 | unsigned long flags; | |
3ebf6922 | 374 | |
f41a0c44 DW |
375 | /* a published domain device always has a valid phy, it may be |
376 | * stale, but it is never NULL | |
377 | */ | |
378 | BUG_ON(!dev->phy); | |
379 | ||
380 | spin_lock_irqsave(&ha->phy_port_lock, flags); | |
381 | phy = dev->phy; | |
382 | get_device(&phy->dev); | |
383 | spin_unlock_irqrestore(&ha->phy_port_lock, flags); | |
384 | ||
385 | return phy; | |
3ebf6922 | 386 | } |
f41a0c44 | 387 | EXPORT_SYMBOL_GPL(sas_get_local_phy); |
3ebf6922 | 388 | |
5db45bdc DW |
389 | static void sas_wait_eh(struct domain_device *dev) |
390 | { | |
391 | struct sas_ha_struct *ha = dev->port->ha; | |
392 | DEFINE_WAIT(wait); | |
393 | ||
394 | if (dev_is_sata(dev)) { | |
395 | ata_port_wait_eh(dev->sata_dev.ap); | |
396 | return; | |
397 | } | |
398 | retry: | |
399 | spin_lock_irq(&ha->lock); | |
400 | ||
401 | while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) { | |
402 | prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); | |
403 | spin_unlock_irq(&ha->lock); | |
404 | schedule(); | |
405 | spin_lock_irq(&ha->lock); | |
406 | } | |
407 | finish_wait(&ha->eh_wait_q, &wait); | |
408 | ||
409 | spin_unlock_irq(&ha->lock); | |
410 | ||
411 | /* make sure SCSI EH is complete */ | |
412 | if (scsi_host_in_recovery(ha->core.shost)) { | |
413 | msleep(10); | |
414 | goto retry; | |
415 | } | |
416 | } | |
5db45bdc | 417 | |
9cb78c16 HR |
418 | static int sas_queue_reset(struct domain_device *dev, int reset_type, |
419 | u64 lun, int wait) | |
5db45bdc DW |
420 | { |
421 | struct sas_ha_struct *ha = dev->port->ha; | |
422 | int scheduled = 0, tries = 100; | |
423 | ||
424 | /* ata: promote lun reset to bus reset */ | |
425 | if (dev_is_sata(dev)) { | |
426 | sas_ata_schedule_reset(dev); | |
427 | if (wait) | |
428 | sas_ata_wait_eh(dev); | |
429 | return SUCCESS; | |
430 | } | |
431 | ||
432 | while (!scheduled && tries--) { | |
433 | spin_lock_irq(&ha->lock); | |
434 | if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) && | |
435 | !test_bit(reset_type, &dev->state)) { | |
436 | scheduled = 1; | |
437 | ha->eh_active++; | |
438 | list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q); | |
439 | set_bit(SAS_DEV_EH_PENDING, &dev->state); | |
440 | set_bit(reset_type, &dev->state); | |
441 | int_to_scsilun(lun, &dev->ssp_dev.reset_lun); | |
442 | scsi_schedule_eh(ha->core.shost); | |
443 | } | |
444 | spin_unlock_irq(&ha->lock); | |
445 | ||
446 | if (wait) | |
447 | sas_wait_eh(dev); | |
448 | ||
449 | if (scheduled) | |
450 | return SUCCESS; | |
451 | } | |
452 | ||
15ba7806 JG |
453 | pr_warn("%s reset of %s failed\n", |
454 | reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus", | |
455 | dev_name(&dev->rphy->dev)); | |
5db45bdc DW |
456 | |
457 | return FAILED; | |
458 | } | |
459 | ||
9524c682 DW |
460 | int sas_eh_abort_handler(struct scsi_cmnd *cmd) |
461 | { | |
c9f92600 | 462 | int res = TMF_RESP_FUNC_FAILED; |
9524c682 DW |
463 | struct sas_task *task = TO_SAS_TASK(cmd); |
464 | struct Scsi_Host *host = cmd->device->host; | |
c9f92600 | 465 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
9524c682 | 466 | struct sas_internal *i = to_sas_internal(host->transportt); |
c9f92600 | 467 | unsigned long flags; |
9524c682 | 468 | |
9524c682 DW |
469 | if (!i->dft->lldd_abort_task) |
470 | return FAILED; | |
471 | ||
c9f92600 HR |
472 | spin_lock_irqsave(host->host_lock, flags); |
473 | /* We cannot do async aborts for SATA devices */ | |
474 | if (dev_is_sata(dev) && !host->host_eh_scheduled) { | |
475 | spin_unlock_irqrestore(host->host_lock, flags); | |
476 | return FAILED; | |
477 | } | |
478 | spin_unlock_irqrestore(host->host_lock, flags); | |
479 | ||
480 | if (task) | |
481 | res = i->dft->lldd_abort_task(task); | |
482 | else | |
15ba7806 | 483 | pr_notice("no task to abort\n"); |
9524c682 DW |
484 | if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) |
485 | return SUCCESS; | |
486 | ||
487 | return FAILED; | |
488 | } | |
489 | EXPORT_SYMBOL_GPL(sas_eh_abort_handler); | |
490 | ||
a9344e68 | 491 | /* Attempt to send a LUN reset message to a device */ |
ad689233 | 492 | int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) |
a9344e68 | 493 | { |
a9344e68 | 494 | int res; |
5db45bdc DW |
495 | struct scsi_lun lun; |
496 | struct Scsi_Host *host = cmd->device->host; | |
497 | struct domain_device *dev = cmd_to_domain_dev(cmd); | |
498 | struct sas_internal *i = to_sas_internal(host->transportt); | |
499 | ||
500 | if (current != host->ehandler) | |
501 | return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0); | |
a9344e68 DW |
502 | |
503 | int_to_scsilun(cmd->device->lun, &lun); | |
504 | ||
505 | if (!i->dft->lldd_lu_reset) | |
506 | return FAILED; | |
507 | ||
508 | res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); | |
509 | if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) | |
510 | return SUCCESS; | |
511 | ||
512 | return FAILED; | |
513 | } | |
514 | ||
cc199e78 | 515 | int sas_eh_target_reset_handler(struct scsi_cmnd *cmd) |
ad689233 | 516 | { |
ad689233 | 517 | int res; |
e7db8229 DW |
518 | struct Scsi_Host *host = cmd->device->host; |
519 | struct domain_device *dev = cmd_to_domain_dev(cmd); | |
520 | struct sas_internal *i = to_sas_internal(host->transportt); | |
ad689233 | 521 | |
5db45bdc DW |
522 | if (current != host->ehandler) |
523 | return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0); | |
524 | ||
e7db8229 DW |
525 | if (!i->dft->lldd_I_T_nexus_reset) |
526 | return FAILED; | |
f41a0c44 | 527 | |
e7db8229 DW |
528 | res = i->dft->lldd_I_T_nexus_reset(dev); |
529 | if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE || | |
530 | res == -ENODEV) | |
ad689233 DW |
531 | return SUCCESS; |
532 | ||
533 | return FAILED; | |
534 | } | |
535 | ||
536 | /* Try to reset a device */ | |
8de3ef25 | 537 | static int try_to_reset_cmd_device(struct scsi_cmnd *cmd) |
ad689233 | 538 | { |
a9344e68 | 539 | int res; |
8de3ef25 | 540 | struct Scsi_Host *shost = cmd->device->host; |
a9344e68 | 541 | |
ad689233 | 542 | if (!shost->hostt->eh_device_reset_handler) |
cc199e78 | 543 | goto try_target_reset; |
a9344e68 DW |
544 | |
545 | res = shost->hostt->eh_device_reset_handler(cmd); | |
546 | if (res == SUCCESS) | |
547 | return res; | |
ad689233 | 548 | |
cc199e78 HR |
549 | try_target_reset: |
550 | if (shost->hostt->eh_target_reset_handler) | |
551 | return shost->hostt->eh_target_reset_handler(cmd); | |
a9344e68 DW |
552 | |
553 | return FAILED; | |
ad689233 DW |
554 | } |
555 | ||
84023474 | 556 | static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q) |
2908d778 | 557 | { |
2908d778 JB |
558 | struct scsi_cmnd *cmd, *n; |
559 | enum task_disposition res = TASK_IS_DONE; | |
3ebf6922 | 560 | int tmf_resp, need_reset; |
2908d778 | 561 | struct sas_internal *i = to_sas_internal(shost->transportt); |
ad689233 DW |
562 | unsigned long flags; |
563 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); | |
45c73b65 | 564 | LIST_HEAD(done); |
2908d778 | 565 | |
45c73b65 | 566 | /* clean out any commands that won the completion vs eh race */ |
ad689233 | 567 | list_for_each_entry_safe(cmd, n, work_q, eh_entry) { |
9095a64a DW |
568 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
569 | struct sas_task *task; | |
570 | ||
571 | spin_lock_irqsave(&dev->done_lock, flags); | |
572 | /* by this point the lldd has either observed | |
573 | * SAS_HA_FROZEN and is leaving the task alone, or has | |
574 | * won the race with eh and decided to complete it | |
575 | */ | |
576 | task = TO_SAS_TASK(cmd); | |
577 | spin_unlock_irqrestore(&dev->done_lock, flags); | |
2908d778 | 578 | |
ad689233 | 579 | if (!task) |
45c73b65 DW |
580 | list_move_tail(&cmd->eh_entry, &done); |
581 | } | |
582 | ||
583 | Again: | |
584 | list_for_each_entry_safe(cmd, n, work_q, eh_entry) { | |
585 | struct sas_task *task = TO_SAS_TASK(cmd); | |
ad689233 DW |
586 | |
587 | list_del_init(&cmd->eh_entry); | |
3ebf6922 DW |
588 | |
589 | spin_lock_irqsave(&task->task_state_lock, flags); | |
590 | need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; | |
3ebf6922 DW |
591 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
592 | ||
8de3ef25 | 593 | if (need_reset) { |
15ba7806 JG |
594 | pr_notice("%s: task 0x%p requests reset\n", |
595 | __func__, task); | |
8de3ef25 JB |
596 | goto reset; |
597 | } | |
598 | ||
15ba7806 | 599 | pr_debug("trying to find task 0x%p\n", task); |
2908d778 JB |
600 | res = sas_scsi_find_task(task); |
601 | ||
2908d778 JB |
602 | switch (res) { |
603 | case TASK_IS_DONE: | |
15ba7806 | 604 | pr_notice("%s: task 0x%p is done\n", __func__, |
2908d778 | 605 | task); |
318aaf34 | 606 | sas_eh_finish_cmd(cmd); |
2908d778 JB |
607 | continue; |
608 | case TASK_IS_ABORTED: | |
15ba7806 JG |
609 | pr_notice("%s: task 0x%p is aborted\n", |
610 | __func__, task); | |
318aaf34 | 611 | sas_eh_finish_cmd(cmd); |
2908d778 JB |
612 | continue; |
613 | case TASK_IS_AT_LU: | |
15ba7806 | 614 | pr_info("task 0x%p is at LU: lu recover\n", task); |
8de3ef25 | 615 | reset: |
2908d778 JB |
616 | tmf_resp = sas_recover_lu(task->dev, cmd); |
617 | if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { | |
b3e3d4c6 | 618 | pr_notice("dev %016llx LU 0x%llx is recovered\n", |
15ba7806 JG |
619 | SAS_ADDR(task->dev), |
620 | cmd->device->lun); | |
318aaf34 | 621 | sas_eh_finish_cmd(cmd); |
ad689233 | 622 | sas_scsi_clear_queue_lu(work_q, cmd); |
2908d778 JB |
623 | goto Again; |
624 | } | |
df561f66 | 625 | fallthrough; |
2908d778 | 626 | case TASK_IS_NOT_AT_LU: |
02cd743b | 627 | case TASK_ABORT_FAILED: |
15ba7806 JG |
628 | pr_notice("task 0x%p is not at LU: I_T recover\n", |
629 | task); | |
2908d778 | 630 | tmf_resp = sas_recover_I_T(task->dev); |
26a2e68f DW |
631 | if (tmf_resp == TMF_RESP_FUNC_COMPLETE || |
632 | tmf_resp == -ENODEV) { | |
8de3ef25 | 633 | struct domain_device *dev = task->dev; |
15ba7806 JG |
634 | pr_notice("I_T %016llx recovered\n", |
635 | SAS_ADDR(task->dev->sas_addr)); | |
a8e14fec | 636 | sas_eh_finish_cmd(cmd); |
8de3ef25 | 637 | sas_scsi_clear_queue_I_T(work_q, dev); |
2908d778 JB |
638 | goto Again; |
639 | } | |
640 | /* Hammer time :-) */ | |
8de3ef25 | 641 | try_to_reset_cmd_device(cmd); |
2908d778 JB |
642 | if (i->dft->lldd_clear_nexus_port) { |
643 | struct asd_sas_port *port = task->dev->port; | |
15ba7806 JG |
644 | pr_debug("clearing nexus for port:%d\n", |
645 | port->id); | |
2908d778 JB |
646 | res = i->dft->lldd_clear_nexus_port(port); |
647 | if (res == TMF_RESP_FUNC_COMPLETE) { | |
15ba7806 JG |
648 | pr_notice("clear nexus port:%d succeeded\n", |
649 | port->id); | |
a8e14fec | 650 | sas_eh_finish_cmd(cmd); |
ad689233 | 651 | sas_scsi_clear_queue_port(work_q, |
2908d778 JB |
652 | port); |
653 | goto Again; | |
654 | } | |
655 | } | |
656 | if (i->dft->lldd_clear_nexus_ha) { | |
15ba7806 | 657 | pr_debug("clear nexus ha\n"); |
2908d778 JB |
658 | res = i->dft->lldd_clear_nexus_ha(ha); |
659 | if (res == TMF_RESP_FUNC_COMPLETE) { | |
15ba7806 | 660 | pr_notice("clear nexus ha succeeded\n"); |
a8e14fec | 661 | sas_eh_finish_cmd(cmd); |
a8e14fec | 662 | goto clear_q; |
2908d778 JB |
663 | } |
664 | } | |
665 | /* If we are here -- this means that no amount | |
666 | * of effort could recover from errors. Quite | |
667 | * possibly the HA just disappeared. | |
668 | */ | |
b3e3d4c6 | 669 | pr_err("error from device %016llx, LUN 0x%llx couldn't be recovered in any way\n", |
15ba7806 JG |
670 | SAS_ADDR(task->dev->sas_addr), |
671 | cmd->device->lun); | |
2908d778 | 672 | |
a8e14fec | 673 | sas_eh_finish_cmd(cmd); |
2908d778 JB |
674 | goto clear_q; |
675 | } | |
676 | } | |
45c73b65 DW |
677 | out: |
678 | list_splice_tail(&done, work_q); | |
3944f509 | 679 | list_splice_tail_init(&ha->eh_ata_q, work_q); |
84023474 | 680 | return; |
45c73b65 DW |
681 | |
682 | clear_q: | |
15ba7806 | 683 | pr_debug("--- Exit %s -- clear_q\n", __func__); |
a8e14fec JB |
684 | list_for_each_entry_safe(cmd, n, work_q, eh_entry) |
685 | sas_eh_finish_cmd(cmd); | |
45c73b65 | 686 | goto out; |
ad689233 DW |
687 | } |
688 | ||
5db45bdc DW |
689 | static void sas_eh_handle_resets(struct Scsi_Host *shost) |
690 | { | |
691 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); | |
692 | struct sas_internal *i = to_sas_internal(shost->transportt); | |
693 | ||
694 | /* handle directed resets to sas devices */ | |
695 | spin_lock_irq(&ha->lock); | |
696 | while (!list_empty(&ha->eh_dev_q)) { | |
697 | struct domain_device *dev; | |
698 | struct ssp_device *ssp; | |
699 | ||
700 | ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node); | |
701 | list_del_init(&ssp->eh_list_node); | |
702 | dev = container_of(ssp, typeof(*dev), ssp_dev); | |
703 | kref_get(&dev->kref); | |
704 | WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n"); | |
705 | ||
706 | spin_unlock_irq(&ha->lock); | |
707 | ||
708 | if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state)) | |
709 | i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun); | |
710 | ||
711 | if (test_and_clear_bit(SAS_DEV_RESET, &dev->state)) | |
712 | i->dft->lldd_I_T_nexus_reset(dev); | |
713 | ||
714 | sas_put_device(dev); | |
715 | spin_lock_irq(&ha->lock); | |
716 | clear_bit(SAS_DEV_EH_PENDING, &dev->state); | |
717 | ha->eh_active--; | |
718 | } | |
719 | spin_unlock_irq(&ha->lock); | |
720 | } | |
721 | ||
e4a9c373 | 722 | |
ad689233 DW |
723 | void sas_scsi_recover_host(struct Scsi_Host *shost) |
724 | { | |
725 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); | |
ad689233 | 726 | LIST_HEAD(eh_work_q); |
e4a9c373 DW |
727 | int tries = 0; |
728 | bool retry; | |
ad689233 | 729 | |
e4a9c373 DW |
730 | retry: |
731 | tries++; | |
732 | retry = true; | |
733 | spin_lock_irq(shost->host_lock); | |
ad689233 | 734 | list_splice_init(&shost->eh_cmd_q, &eh_work_q); |
e4a9c373 | 735 | spin_unlock_irq(shost->host_lock); |
ad689233 | 736 | |
15ba7806 JG |
737 | pr_notice("Enter %s busy: %d failed: %d\n", |
738 | __func__, scsi_host_busy(shost), shost->host_failed); | |
ad689233 DW |
739 | /* |
740 | * Deal with commands that still have SAS tasks (i.e. they didn't | |
84023474 DW |
741 | * complete via the normal sas_task completion mechanism), |
742 | * SAS_HA_FROZEN gives eh dominion over all sas_task completion. | |
ad689233 | 743 | */ |
9095a64a | 744 | set_bit(SAS_HA_FROZEN, &ha->state); |
84023474 DW |
745 | sas_eh_handle_sas_errors(shost, &eh_work_q); |
746 | clear_bit(SAS_HA_FROZEN, &ha->state); | |
747 | if (list_empty(&eh_work_q)) | |
ad689233 DW |
748 | goto out; |
749 | ||
750 | /* | |
751 | * Now deal with SCSI commands that completed ok but have a an error | |
752 | * code (and hopefully sense data) attached. This is roughly what | |
753 | * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any | |
754 | * command we see here has no sas_task and is thus unknown to the HA. | |
755 | */ | |
d230ce69 DW |
756 | sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q); |
757 | if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) | |
758 | scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); | |
ad689233 DW |
759 | |
760 | out: | |
5db45bdc DW |
761 | sas_eh_handle_resets(shost); |
762 | ||
00dd4998 JB |
763 | /* now link into libata eh --- if we have any ata devices */ |
764 | sas_ata_strategy_handler(shost); | |
765 | ||
ad689233 | 766 | scsi_eh_flush_done_q(&ha->eh_done_q); |
00dd4998 | 767 | |
e4a9c373 DW |
768 | /* check if any new eh work was scheduled during the last run */ |
769 | spin_lock_irq(&ha->lock); | |
770 | if (ha->eh_active == 0) { | |
771 | shost->host_eh_scheduled = 0; | |
772 | retry = false; | |
773 | } | |
774 | spin_unlock_irq(&ha->lock); | |
775 | ||
776 | if (retry) | |
777 | goto retry; | |
778 | ||
15ba7806 JG |
779 | pr_notice("--- Exit %s: busy: %d failed: %d tries: %d\n", |
780 | __func__, scsi_host_busy(shost), | |
781 | shost->host_failed, tries); | |
2908d778 JB |
782 | } |
783 | ||
6f4e626f | 784 | int sas_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg) |
fa1c1e8f DW |
785 | { |
786 | struct domain_device *dev = sdev_to_domain_dev(sdev); | |
787 | ||
788 | if (dev_is_sata(dev)) | |
94be9a58 | 789 | return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg); |
fa1c1e8f DW |
790 | |
791 | return -EINVAL; | |
792 | } | |
793 | ||
2908d778 JB |
794 | struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) |
795 | { | |
796 | struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); | |
797 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); | |
798 | struct domain_device *found_dev = NULL; | |
799 | int i; | |
980fa2f9 | 800 | unsigned long flags; |
2908d778 | 801 | |
980fa2f9 | 802 | spin_lock_irqsave(&ha->phy_port_lock, flags); |
2908d778 JB |
803 | for (i = 0; i < ha->num_phys; i++) { |
804 | struct asd_sas_port *port = ha->sas_port[i]; | |
805 | struct domain_device *dev; | |
806 | ||
807 | spin_lock(&port->dev_list_lock); | |
808 | list_for_each_entry(dev, &port->dev_list, dev_list_node) { | |
809 | if (rphy == dev->rphy) { | |
810 | found_dev = dev; | |
811 | spin_unlock(&port->dev_list_lock); | |
812 | goto found; | |
813 | } | |
814 | } | |
815 | spin_unlock(&port->dev_list_lock); | |
816 | } | |
817 | found: | |
980fa2f9 | 818 | spin_unlock_irqrestore(&ha->phy_port_lock, flags); |
2908d778 JB |
819 | |
820 | return found_dev; | |
821 | } | |
822 | ||
2908d778 JB |
823 | int sas_target_alloc(struct scsi_target *starget) |
824 | { | |
735f7d2f DW |
825 | struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); |
826 | struct domain_device *found_dev = sas_find_dev_by_rphy(rphy); | |
2908d778 JB |
827 | |
828 | if (!found_dev) | |
829 | return -ENODEV; | |
830 | ||
735f7d2f | 831 | kref_get(&found_dev->kref); |
2908d778 JB |
832 | starget->hostdata = found_dev; |
833 | return 0; | |
834 | } | |
835 | ||
97a1420d | 836 | #define SAS_DEF_QD 256 |
2908d778 JB |
837 | |
838 | int sas_slave_configure(struct scsi_device *scsi_dev) | |
839 | { | |
840 | struct domain_device *dev = sdev_to_domain_dev(scsi_dev); | |
2908d778 JB |
841 | |
842 | BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); | |
843 | ||
fa1c1e8f DW |
844 | if (dev_is_sata(dev)) { |
845 | ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap); | |
846 | return 0; | |
847 | } | |
848 | ||
2908d778 JB |
849 | sas_read_port_mode_page(scsi_dev); |
850 | ||
851 | if (scsi_dev->tagged_supported) { | |
db5ed4df | 852 | scsi_change_queue_depth(scsi_dev, SAS_DEF_QD); |
2908d778 | 853 | } else { |
b3e3d4c6 | 854 | pr_notice("device %016llx, LUN 0x%llx doesn't support TCQ\n", |
15ba7806 | 855 | SAS_ADDR(dev->sas_addr), scsi_dev->lun); |
db5ed4df | 856 | scsi_change_queue_depth(scsi_dev, 1); |
2908d778 JB |
857 | } |
858 | ||
f27708fc DW |
859 | scsi_dev->allow_restart = 1; |
860 | ||
2908d778 JB |
861 | return 0; |
862 | } | |
863 | ||
db5ed4df | 864 | int sas_change_queue_depth(struct scsi_device *sdev, int depth) |
2908d778 | 865 | { |
97a1420d | 866 | struct domain_device *dev = sdev_to_domain_dev(sdev); |
2908d778 | 867 | |
f6e67035 | 868 | if (dev_is_sata(dev)) |
db5ed4df | 869 | return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth); |
97a1420d | 870 | |
c40ecc12 CH |
871 | if (!sdev->tagged_supported) |
872 | depth = 1; | |
db5ed4df | 873 | return scsi_change_queue_depth(sdev, depth); |
2908d778 JB |
874 | } |
875 | ||
2908d778 JB |
876 | int sas_bios_param(struct scsi_device *scsi_dev, |
877 | struct block_device *bdev, | |
878 | sector_t capacity, int *hsc) | |
879 | { | |
880 | hsc[0] = 255; | |
881 | hsc[1] = 63; | |
882 | sector_div(capacity, 255*63); | |
883 | hsc[2] = capacity; | |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
396819fb DW |
888 | /* |
889 | * Tell an upper layer that it needs to initiate an abort for a given task. | |
890 | * This should only ever be called by an LLDD. | |
891 | */ | |
892 | void sas_task_abort(struct sas_task *task) | |
79a5eb60 | 893 | { |
396819fb | 894 | struct scsi_cmnd *sc = task->uldd_task; |
79a5eb60 | 895 | |
396819fb DW |
896 | /* Escape for libsas internal commands */ |
897 | if (!sc) { | |
f0bf750c DW |
898 | struct sas_task_slow *slow = task->slow_task; |
899 | ||
900 | if (!slow) | |
901 | return; | |
902 | if (!del_timer(&slow->timer)) | |
79a5eb60 | 903 | return; |
841b86f3 | 904 | slow->timer.function(&slow->timer); |
396819fb DW |
905 | return; |
906 | } | |
79a5eb60 | 907 | |
39795d65 | 908 | if (dev_is_sata(task->dev)) |
3a2755af | 909 | sas_ata_task_abort(task); |
39795d65 | 910 | else |
1b4d0d8e | 911 | blk_abort_request(sc->request); |
79a5eb60 DW |
912 | } |
913 | ||
fa1c1e8f DW |
914 | void sas_target_destroy(struct scsi_target *starget) |
915 | { | |
735f7d2f | 916 | struct domain_device *found_dev = starget->hostdata; |
fa1c1e8f DW |
917 | |
918 | if (!found_dev) | |
919 | return; | |
920 | ||
735f7d2f DW |
921 | starget->hostdata = NULL; |
922 | sas_put_device(found_dev); | |
fa1c1e8f DW |
923 | } |
924 | ||
45e6cdf4 DW |
925 | #define SAS_STRING_ADDR_SIZE 16 |
926 | ||
927 | int sas_request_addr(struct Scsi_Host *shost, u8 *addr) | |
928 | { | |
929 | int res; | |
930 | const struct firmware *fw; | |
931 | ||
932 | res = request_firmware(&fw, "sas_addr", &shost->shost_gendev); | |
933 | if (res) | |
934 | return res; | |
935 | ||
936 | if (fw->size < SAS_STRING_ADDR_SIZE) { | |
937 | res = -ENODEV; | |
938 | goto out; | |
939 | } | |
940 | ||
9ea4e076 AS |
941 | res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2); |
942 | if (res) | |
943 | goto out; | |
45e6cdf4 DW |
944 | |
945 | out: | |
946 | release_firmware(fw); | |
947 | return res; | |
948 | } | |
949 | EXPORT_SYMBOL_GPL(sas_request_addr); | |
950 | ||
2908d778 JB |
951 | EXPORT_SYMBOL_GPL(sas_queuecommand); |
952 | EXPORT_SYMBOL_GPL(sas_target_alloc); | |
953 | EXPORT_SYMBOL_GPL(sas_slave_configure); | |
2908d778 | 954 | EXPORT_SYMBOL_GPL(sas_change_queue_depth); |
2908d778 | 955 | EXPORT_SYMBOL_GPL(sas_bios_param); |
79a5eb60 | 956 | EXPORT_SYMBOL_GPL(sas_task_abort); |
dea22214 | 957 | EXPORT_SYMBOL_GPL(sas_phy_reset); |
ad689233 | 958 | EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); |
cc199e78 | 959 | EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler); |
fa1c1e8f DW |
960 | EXPORT_SYMBOL_GPL(sas_target_destroy); |
961 | EXPORT_SYMBOL_GPL(sas_ioctl); |