]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/libsas/sas_scsi_host.c
[SCSI] libsas: Remove SAS_TASK_INITIATOR_ABORTED flag
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / libsas / sas_scsi_host.c
CommitLineData
2908d778
JB
1/*
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
26#include "sas_internal.h"
27
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi.h>
f456393e 32#include <scsi/scsi_eh.h>
2908d778
JB
33#include <scsi/scsi_transport.h>
34#include <scsi/scsi_transport_sas.h>
35#include "../scsi_sas_internal.h"
79a5eb60 36#include "../scsi_transport_api.h"
2908d778
JB
37
38#include <linux/err.h>
39#include <linux/blkdev.h>
40#include <linux/scatterlist.h>
41
42/* ---------- SCSI Host glue ---------- */
43
44#define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
45#define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
46
47static void sas_scsi_task_done(struct sas_task *task)
48{
49 struct task_status_struct *ts = &task->task_status;
50 struct scsi_cmnd *sc = task->uldd_task;
f456393e 51 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
2908d778
JB
52 unsigned ts_flags = task->task_state_flags;
53 int hs = 0, stat = 0;
54
55 if (unlikely(!sc)) {
56 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
57 list_del_init(&task->list);
58 sas_free_task(task);
59 return;
60 }
61
62 if (ts->resp == SAS_TASK_UNDELIVERED) {
63 /* transport error */
64 hs = DID_NO_CONNECT;
65 } else { /* ts->resp == SAS_TASK_COMPLETE */
66 /* task delivered, what happened afterwards? */
67 switch (ts->stat) {
68 case SAS_DEV_NO_RESPONSE:
69 case SAS_INTERRUPTED:
70 case SAS_PHY_DOWN:
71 case SAS_NAK_R_ERR:
72 case SAS_OPEN_TO:
73 hs = DID_NO_CONNECT;
74 break;
75 case SAS_DATA_UNDERRUN:
76 sc->resid = ts->residual;
77 if (sc->request_bufflen - sc->resid < sc->underflow)
78 hs = DID_ERROR;
79 break;
80 case SAS_DATA_OVERRUN:
81 hs = DID_ERROR;
82 break;
83 case SAS_QUEUE_FULL:
84 hs = DID_SOFT_ERROR; /* retry */
85 break;
86 case SAS_DEVICE_UNKNOWN:
87 hs = DID_BAD_TARGET;
88 break;
89 case SAS_SG_ERR:
90 hs = DID_PARITY;
91 break;
92 case SAS_OPEN_REJECT:
93 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
94 hs = DID_SOFT_ERROR; /* retry */
95 else
96 hs = DID_ERROR;
97 break;
98 case SAS_PROTO_RESPONSE:
99 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
100 "task; please report this\n",
101 task->dev->port->ha->sas_ha_name);
102 break;
103 case SAS_ABORTED_TASK:
104 hs = DID_ABORT;
105 break;
106 case SAM_CHECK_COND:
107 memcpy(sc->sense_buffer, ts->buf,
108 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
109 stat = SAM_CHECK_COND;
110 break;
111 default:
112 stat = ts->stat;
113 break;
114 }
115 }
116 ASSIGN_SAS_TASK(sc, NULL);
117 sc->result = (hs << 16) | stat;
118 list_del_init(&task->list);
119 sas_free_task(task);
120 /* This is very ugly but this is how SCSI Core works. */
121 if (ts_flags & SAS_TASK_STATE_ABORTED)
f456393e 122 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
2908d778
JB
123 else
124 sc->scsi_done(sc);
125}
126
127static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
128{
129 enum task_attribute ta = TASK_ATTR_SIMPLE;
130 if (cmd->request && blk_rq_tagged(cmd->request)) {
131 if (cmd->device->ordered_tags &&
1bdfd554 132 (cmd->request->cmd_flags & REQ_HARDBARRIER))
2908d778
JB
133 ta = TASK_ATTR_HOQ;
134 }
135 return ta;
136}
137
138static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
139 struct domain_device *dev,
3cc27547 140 gfp_t gfp_flags)
2908d778
JB
141{
142 struct sas_task *task = sas_alloc_task(gfp_flags);
143 struct scsi_lun lun;
144
145 if (!task)
146 return NULL;
147
148 *(u32 *)cmd->sense_buffer = 0;
149 task->uldd_task = cmd;
150 ASSIGN_SAS_TASK(cmd, task);
151
152 task->dev = dev;
153 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
154
155 task->ssp_task.retry_count = 1;
156 int_to_scsilun(cmd->device->lun, &lun);
157 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
158 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
159 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
160
161 task->scatter = cmd->request_buffer;
162 task->num_scatter = cmd->use_sg;
163 task->total_xfer_len = cmd->request_bufflen;
164 task->data_dir = cmd->sc_data_direction;
165
166 task->task_done = sas_scsi_task_done;
167
168 return task;
169}
170
171static int sas_queue_up(struct sas_task *task)
172{
173 struct sas_ha_struct *sas_ha = task->dev->port->ha;
174 struct scsi_core *core = &sas_ha->core;
175 unsigned long flags;
176 LIST_HEAD(list);
177
178 spin_lock_irqsave(&core->task_queue_lock, flags);
179 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
180 spin_unlock_irqrestore(&core->task_queue_lock, flags);
181 return -SAS_QUEUE_FULL;
182 }
183 list_add_tail(&task->list, &core->task_queue);
184 core->task_queue_size += 1;
185 spin_unlock_irqrestore(&core->task_queue_lock, flags);
186 up(&core->queue_thread_sema);
187
188 return 0;
189}
190
191/**
192 * sas_queuecommand -- Enqueue a command for processing
193 * @parameters: See SCSI Core documentation
194 *
195 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
196 * call us without holding an IRQ spinlock...
197 */
198int sas_queuecommand(struct scsi_cmnd *cmd,
199 void (*scsi_done)(struct scsi_cmnd *))
200{
201 int res = 0;
202 struct domain_device *dev = cmd_to_domain_dev(cmd);
203 struct Scsi_Host *host = cmd->device->host;
204 struct sas_internal *i = to_sas_internal(host->transportt);
205
206 spin_unlock_irq(host->host_lock);
207
208 {
209 struct sas_ha_struct *sas_ha = dev->port->ha;
210 struct sas_task *task;
211
212 res = -ENOMEM;
213 task = sas_create_task(cmd, dev, GFP_ATOMIC);
214 if (!task)
215 goto out;
216
217 cmd->scsi_done = scsi_done;
218 /* Queue up, Direct Mode or Task Collector Mode. */
219 if (sas_ha->lldd_max_execute_num < 2)
220 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
221 else
222 res = sas_queue_up(task);
223
224 /* Examine */
225 if (res) {
226 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
227 ASSIGN_SAS_TASK(cmd, NULL);
228 sas_free_task(task);
229 if (res == -SAS_QUEUE_FULL) {
230 cmd->result = DID_SOFT_ERROR << 16; /* retry */
231 res = 0;
232 scsi_done(cmd);
233 }
234 goto out;
235 }
236 }
237out:
238 spin_lock_irq(host->host_lock);
239 return res;
240}
241
242static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
243{
244 struct scsi_cmnd *cmd, *n;
245
246 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
247 if (cmd == my_cmd)
248 list_del_init(&cmd->eh_entry);
249 }
250}
251
252static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
253 struct domain_device *dev)
254{
255 struct scsi_cmnd *cmd, *n;
256
257 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
258 struct domain_device *x = cmd_to_domain_dev(cmd);
259
260 if (x == dev)
261 list_del_init(&cmd->eh_entry);
262 }
263}
264
265static void sas_scsi_clear_queue_port(struct list_head *error_q,
266 struct asd_sas_port *port)
267{
268 struct scsi_cmnd *cmd, *n;
269
270 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
271 struct domain_device *dev = cmd_to_domain_dev(cmd);
272 struct asd_sas_port *x = dev->port;
273
274 if (x == port)
275 list_del_init(&cmd->eh_entry);
276 }
277}
278
279enum task_disposition {
280 TASK_IS_DONE,
281 TASK_IS_ABORTED,
282 TASK_IS_AT_LU,
283 TASK_IS_NOT_AT_LU,
284};
285
286static enum task_disposition sas_scsi_find_task(struct sas_task *task)
287{
288 struct sas_ha_struct *ha = task->dev->port->ha;
289 unsigned long flags;
290 int i, res;
291 struct sas_internal *si =
292 to_sas_internal(task->dev->port->ha->core.shost->transportt);
293
294 if (ha->lldd_max_execute_num > 1) {
295 struct scsi_core *core = &ha->core;
296 struct sas_task *t, *n;
297
298 spin_lock_irqsave(&core->task_queue_lock, flags);
299 list_for_each_entry_safe(t, n, &core->task_queue, list) {
300 if (task == t) {
301 list_del_init(&t->list);
302 spin_unlock_irqrestore(&core->task_queue_lock,
303 flags);
304 SAS_DPRINTK("%s: task 0x%p aborted from "
305 "task_queue\n",
306 __FUNCTION__, task);
307 return TASK_IS_ABORTED;
308 }
309 }
310 spin_unlock_irqrestore(&core->task_queue_lock, flags);
311 }
312
313 for (i = 0; i < 5; i++) {
314 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
315 res = si->dft->lldd_abort_task(task);
316
317 spin_lock_irqsave(&task->task_state_lock, flags);
318 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
319 spin_unlock_irqrestore(&task->task_state_lock, flags);
320 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
321 task);
322 return TASK_IS_DONE;
323 }
324 spin_unlock_irqrestore(&task->task_state_lock, flags);
325
326 if (res == TMF_RESP_FUNC_COMPLETE) {
327 SAS_DPRINTK("%s: task 0x%p is aborted\n",
328 __FUNCTION__, task);
329 return TASK_IS_ABORTED;
330 } else if (si->dft->lldd_query_task) {
331 SAS_DPRINTK("%s: querying task 0x%p\n",
332 __FUNCTION__, task);
333 res = si->dft->lldd_query_task(task);
334 if (res == TMF_RESP_FUNC_SUCC) {
335 SAS_DPRINTK("%s: task 0x%p at LU\n",
336 __FUNCTION__, task);
337 return TASK_IS_AT_LU;
338 } else if (res == TMF_RESP_FUNC_COMPLETE) {
339 SAS_DPRINTK("%s: task 0x%p not at LU\n",
340 __FUNCTION__, task);
341 return TASK_IS_NOT_AT_LU;
342 }
343 }
344 }
345 return res;
346}
347
348static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
349{
350 int res = TMF_RESP_FUNC_FAILED;
351 struct scsi_lun lun;
352 struct sas_internal *i =
353 to_sas_internal(dev->port->ha->core.shost->transportt);
354
355 int_to_scsilun(cmd->device->lun, &lun);
356
357 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
358 SAS_ADDR(dev->sas_addr),
359 cmd->device->lun);
360
361 if (i->dft->lldd_abort_task_set)
362 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
363
364 if (res == TMF_RESP_FUNC_FAILED) {
365 if (i->dft->lldd_clear_task_set)
366 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
367 }
368
369 if (res == TMF_RESP_FUNC_FAILED) {
370 if (i->dft->lldd_lu_reset)
371 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
372 }
373
374 return res;
375}
376
377static int sas_recover_I_T(struct domain_device *dev)
378{
379 int res = TMF_RESP_FUNC_FAILED;
380 struct sas_internal *i =
381 to_sas_internal(dev->port->ha->core.shost->transportt);
382
383 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
384 SAS_ADDR(dev->sas_addr));
385
386 if (i->dft->lldd_I_T_nexus_reset)
387 res = i->dft->lldd_I_T_nexus_reset(dev);
388
389 return res;
390}
391
392void sas_scsi_recover_host(struct Scsi_Host *shost)
393{
394 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
395 unsigned long flags;
396 LIST_HEAD(error_q);
397 struct scsi_cmnd *cmd, *n;
398 enum task_disposition res = TASK_IS_DONE;
399 int tmf_resp;
400 struct sas_internal *i = to_sas_internal(shost->transportt);
401
402 spin_lock_irqsave(shost->host_lock, flags);
403 list_splice_init(&shost->eh_cmd_q, &error_q);
404 spin_unlock_irqrestore(shost->host_lock, flags);
405
406 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
407
408 /* All tasks on this list were marked SAS_TASK_STATE_ABORTED
409 * by sas_scsi_timed_out() callback.
410 */
411Again:
412 SAS_DPRINTK("going over list...\n");
413 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
414 struct sas_task *task = TO_SAS_TASK(cmd);
f456393e 415 list_del_init(&cmd->eh_entry);
2908d778 416
f456393e
DW
417 if (!task) {
418 SAS_DPRINTK("%s: taskless cmd?!\n", __FUNCTION__);
419 continue;
420 }
2908d778 421 SAS_DPRINTK("trying to find task 0x%p\n", task);
2908d778
JB
422 res = sas_scsi_find_task(task);
423
424 cmd->eh_eflags = 0;
2908d778
JB
425
426 switch (res) {
427 case TASK_IS_DONE:
428 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
429 task);
430 task->task_done(task);
431 continue;
432 case TASK_IS_ABORTED:
433 SAS_DPRINTK("%s: task 0x%p is aborted\n",
434 __FUNCTION__, task);
435 task->task_done(task);
436 continue;
437 case TASK_IS_AT_LU:
438 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
439 tmf_resp = sas_recover_lu(task->dev, cmd);
440 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
441 SAS_DPRINTK("dev %016llx LU %x is "
442 "recovered\n",
443 SAS_ADDR(task->dev),
444 cmd->device->lun);
445 task->task_done(task);
446 sas_scsi_clear_queue_lu(&error_q, cmd);
447 goto Again;
448 }
449 /* fallthrough */
450 case TASK_IS_NOT_AT_LU:
451 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
452 task);
453 tmf_resp = sas_recover_I_T(task->dev);
454 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
455 SAS_DPRINTK("I_T %016llx recovered\n",
456 SAS_ADDR(task->dev->sas_addr));
457 task->task_done(task);
458 sas_scsi_clear_queue_I_T(&error_q, task->dev);
459 goto Again;
460 }
461 /* Hammer time :-) */
462 if (i->dft->lldd_clear_nexus_port) {
463 struct asd_sas_port *port = task->dev->port;
464 SAS_DPRINTK("clearing nexus for port:%d\n",
465 port->id);
466 res = i->dft->lldd_clear_nexus_port(port);
467 if (res == TMF_RESP_FUNC_COMPLETE) {
468 SAS_DPRINTK("clear nexus port:%d "
469 "succeeded\n", port->id);
470 task->task_done(task);
471 sas_scsi_clear_queue_port(&error_q,
472 port);
473 goto Again;
474 }
475 }
476 if (i->dft->lldd_clear_nexus_ha) {
477 SAS_DPRINTK("clear nexus ha\n");
478 res = i->dft->lldd_clear_nexus_ha(ha);
479 if (res == TMF_RESP_FUNC_COMPLETE) {
480 SAS_DPRINTK("clear nexus ha "
481 "succeeded\n");
482 task->task_done(task);
483 goto out;
484 }
485 }
486 /* If we are here -- this means that no amount
487 * of effort could recover from errors. Quite
488 * possibly the HA just disappeared.
489 */
490 SAS_DPRINTK("error from device %llx, LUN %x "
491 "couldn't be recovered in any way\n",
492 SAS_ADDR(task->dev->sas_addr),
493 cmd->device->lun);
494
495 task->task_done(task);
496 goto clear_q;
497 }
498 }
499out:
f456393e 500 scsi_eh_flush_done_q(&ha->eh_done_q);
2908d778
JB
501 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
502 return;
503clear_q:
504 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
505 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
506 struct sas_task *task = TO_SAS_TASK(cmd);
507 list_del_init(&cmd->eh_entry);
508 task->task_done(task);
509 }
510}
511
512enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
513{
514 struct sas_task *task = TO_SAS_TASK(cmd);
515 unsigned long flags;
516
517 if (!task) {
6d4dcd4d
DW
518 cmd->timeout_per_command /= 2;
519 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
520 cmd, task, (cmd->timeout_per_command ?
521 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
522 if (!cmd->timeout_per_command)
523 return EH_NOT_HANDLED;
524 return EH_RESET_TIMER;
2908d778
JB
525 }
526
527 spin_lock_irqsave(&task->task_state_lock, flags);
528 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
529 spin_unlock_irqrestore(&task->task_state_lock, flags);
530 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
531 cmd, task);
532 return EH_HANDLED;
533 }
b218a0d8
DW
534 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
535 spin_unlock_irqrestore(&task->task_state_lock, flags);
536 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
537 "EH_RESET_TIMER\n",
538 cmd, task);
539 return EH_RESET_TIMER;
540 }
2908d778
JB
541 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
542 spin_unlock_irqrestore(&task->task_state_lock, flags);
543
544 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
545 cmd, task);
546
547 return EH_NOT_HANDLED;
548}
549
550struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
551{
552 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
553 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
554 struct domain_device *found_dev = NULL;
555 int i;
556
557 spin_lock(&ha->phy_port_lock);
558 for (i = 0; i < ha->num_phys; i++) {
559 struct asd_sas_port *port = ha->sas_port[i];
560 struct domain_device *dev;
561
562 spin_lock(&port->dev_list_lock);
563 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
564 if (rphy == dev->rphy) {
565 found_dev = dev;
566 spin_unlock(&port->dev_list_lock);
567 goto found;
568 }
569 }
570 spin_unlock(&port->dev_list_lock);
571 }
572 found:
573 spin_unlock(&ha->phy_port_lock);
574
575 return found_dev;
576}
577
578static inline struct domain_device *sas_find_target(struct scsi_target *starget)
579{
580 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
581
582 return sas_find_dev_by_rphy(rphy);
583}
584
585int sas_target_alloc(struct scsi_target *starget)
586{
587 struct domain_device *found_dev = sas_find_target(starget);
588
589 if (!found_dev)
590 return -ENODEV;
591
592 starget->hostdata = found_dev;
593 return 0;
594}
595
596#define SAS_DEF_QD 32
597#define SAS_MAX_QD 64
598
599int sas_slave_configure(struct scsi_device *scsi_dev)
600{
601 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
602 struct sas_ha_struct *sas_ha;
603
604 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
605
606 sas_ha = dev->port->ha;
607
608 sas_read_port_mode_page(scsi_dev);
609
610 if (scsi_dev->tagged_supported) {
611 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
612 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
613 } else {
614 SAS_DPRINTK("device %llx, LUN %x doesn't support "
615 "TCQ\n", SAS_ADDR(dev->sas_addr),
616 scsi_dev->lun);
617 scsi_dev->tagged_supported = 0;
618 scsi_set_tag_type(scsi_dev, 0);
619 scsi_deactivate_tcq(scsi_dev, 1);
620 }
621
622 return 0;
623}
624
625void sas_slave_destroy(struct scsi_device *scsi_dev)
626{
627}
628
629int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
630{
631 int res = min(new_depth, SAS_MAX_QD);
632
633 if (scsi_dev->tagged_supported)
634 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
635 res);
636 else {
637 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
638 sas_printk("device %llx LUN %x queue depth changed to 1\n",
639 SAS_ADDR(dev->sas_addr),
640 scsi_dev->lun);
641 scsi_adjust_queue_depth(scsi_dev, 0, 1);
642 res = 1;
643 }
644
645 return res;
646}
647
648int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
649{
650 if (!scsi_dev->tagged_supported)
651 return 0;
652
653 scsi_deactivate_tcq(scsi_dev, 1);
654
655 scsi_set_tag_type(scsi_dev, qt);
656 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
657
658 return qt;
659}
660
661int sas_bios_param(struct scsi_device *scsi_dev,
662 struct block_device *bdev,
663 sector_t capacity, int *hsc)
664{
665 hsc[0] = 255;
666 hsc[1] = 63;
667 sector_div(capacity, 255*63);
668 hsc[2] = capacity;
669
670 return 0;
671}
672
673/* ---------- Task Collector Thread implementation ---------- */
674
675static void sas_queue(struct sas_ha_struct *sas_ha)
676{
677 struct scsi_core *core = &sas_ha->core;
678 unsigned long flags;
679 LIST_HEAD(q);
680 int can_queue;
681 int res;
682 struct sas_internal *i = to_sas_internal(core->shost->transportt);
683
684 spin_lock_irqsave(&core->task_queue_lock, flags);
685 while (!core->queue_thread_kill &&
686 !list_empty(&core->task_queue)) {
687
688 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
689 if (can_queue >= 0) {
690 can_queue = core->task_queue_size;
691 list_splice_init(&core->task_queue, &q);
692 } else {
693 struct list_head *a, *n;
694
695 can_queue = sas_ha->lldd_queue_size;
696 list_for_each_safe(a, n, &core->task_queue) {
697 list_move_tail(a, &q);
698 if (--can_queue == 0)
699 break;
700 }
701 can_queue = sas_ha->lldd_queue_size;
702 }
703 core->task_queue_size -= can_queue;
704 spin_unlock_irqrestore(&core->task_queue_lock, flags);
705 {
706 struct sas_task *task = list_entry(q.next,
707 struct sas_task,
708 list);
709 list_del_init(&q);
710 res = i->dft->lldd_execute_task(task, can_queue,
711 GFP_KERNEL);
712 if (unlikely(res))
713 __list_add(&q, task->list.prev, &task->list);
714 }
715 spin_lock_irqsave(&core->task_queue_lock, flags);
716 if (res) {
717 list_splice_init(&q, &core->task_queue); /*at head*/
718 core->task_queue_size += can_queue;
719 }
720 }
721 spin_unlock_irqrestore(&core->task_queue_lock, flags);
722}
723
724static DECLARE_COMPLETION(queue_th_comp);
725
726/**
727 * sas_queue_thread -- The Task Collector thread
728 * @_sas_ha: pointer to struct sas_ha
729 */
730static int sas_queue_thread(void *_sas_ha)
731{
732 struct sas_ha_struct *sas_ha = _sas_ha;
733 struct scsi_core *core = &sas_ha->core;
734
735 daemonize("sas_queue_%d", core->shost->host_no);
736 current->flags |= PF_NOFREEZE;
737
738 complete(&queue_th_comp);
739
740 while (1) {
741 down_interruptible(&core->queue_thread_sema);
742 sas_queue(sas_ha);
743 if (core->queue_thread_kill)
744 break;
745 }
746
747 complete(&queue_th_comp);
748
749 return 0;
750}
751
752int sas_init_queue(struct sas_ha_struct *sas_ha)
753{
754 int res;
755 struct scsi_core *core = &sas_ha->core;
756
757 spin_lock_init(&core->task_queue_lock);
758 core->task_queue_size = 0;
759 INIT_LIST_HEAD(&core->task_queue);
760 init_MUTEX_LOCKED(&core->queue_thread_sema);
761
762 res = kernel_thread(sas_queue_thread, sas_ha, 0);
763 if (res >= 0)
764 wait_for_completion(&queue_th_comp);
765
766 return res < 0 ? res : 0;
767}
768
769void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
770{
771 unsigned long flags;
772 struct scsi_core *core = &sas_ha->core;
773 struct sas_task *task, *n;
774
775 init_completion(&queue_th_comp);
776 core->queue_thread_kill = 1;
777 up(&core->queue_thread_sema);
778 wait_for_completion(&queue_th_comp);
779
780 if (!list_empty(&core->task_queue))
781 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
782 SAS_ADDR(sas_ha->sas_addr));
783
784 spin_lock_irqsave(&core->task_queue_lock, flags);
785 list_for_each_entry_safe(task, n, &core->task_queue, list) {
786 struct scsi_cmnd *cmd = task->uldd_task;
787
788 list_del_init(&task->list);
789
790 ASSIGN_SAS_TASK(cmd, NULL);
791 sas_free_task(task);
792 cmd->result = DID_ABORT << 16;
793 cmd->scsi_done(cmd);
794 }
795 spin_unlock_irqrestore(&core->task_queue_lock, flags);
796}
797
79a5eb60
DW
798static int do_sas_task_abort(struct sas_task *task)
799{
800 struct scsi_cmnd *sc = task->uldd_task;
801 struct sas_internal *si =
802 to_sas_internal(task->dev->port->ha->core.shost->transportt);
803 unsigned long flags;
804 int res;
805
806 spin_lock_irqsave(&task->task_state_lock, flags);
807 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
808 spin_unlock_irqrestore(&task->task_state_lock, flags);
809 SAS_DPRINTK("%s: Task %p already aborted.\n", __FUNCTION__,
810 task);
811 return 0;
812 }
813
79a5eb60
DW
814 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
815 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
816 spin_unlock_irqrestore(&task->task_state_lock, flags);
817
818 if (!si->dft->lldd_abort_task)
819 return -ENODEV;
820
821 res = si->dft->lldd_abort_task(task);
822 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
823 (res == TMF_RESP_FUNC_COMPLETE))
824 {
825 /* SMP commands don't have scsi_cmds(?) */
826 if (!sc) {
827 task->task_done(task);
828 return 0;
829 }
830 scsi_req_abort_cmd(sc);
831 scsi_schedule_eh(sc->device->host);
832 return 0;
833 }
834
835 spin_lock_irqsave(&task->task_state_lock, flags);
79a5eb60
DW
836 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
837 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
838 spin_unlock_irqrestore(&task->task_state_lock, flags);
839
840 return -EAGAIN;
841}
842
06328b4f 843void sas_task_abort(struct work_struct *work)
79a5eb60 844{
06328b4f
DH
845 struct sas_task *task =
846 container_of(work, struct sas_task, abort_work);
79a5eb60
DW
847 int i;
848
849 for (i = 0; i < 5; i++)
850 if (!do_sas_task_abort(task))
851 return;
852
853 SAS_DPRINTK("%s: Could not kill task!\n", __FUNCTION__);
854}
855
2908d778
JB
856EXPORT_SYMBOL_GPL(sas_queuecommand);
857EXPORT_SYMBOL_GPL(sas_target_alloc);
858EXPORT_SYMBOL_GPL(sas_slave_configure);
859EXPORT_SYMBOL_GPL(sas_slave_destroy);
860EXPORT_SYMBOL_GPL(sas_change_queue_depth);
861EXPORT_SYMBOL_GPL(sas_change_queue_type);
862EXPORT_SYMBOL_GPL(sas_bios_param);
79a5eb60 863EXPORT_SYMBOL_GPL(sas_task_abort);
dea22214 864EXPORT_SYMBOL_GPL(sas_phy_reset);
acbf167d 865EXPORT_SYMBOL_GPL(sas_phy_enable);