sas_scsi_host.c 27.0 KB
Newer Older
J
James Bottomley 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Serial Attached SCSI (SAS) class SCSI Host glue.
 *
 * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
 *
 * This file is licensed under GPLv2.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

26
#include <linux/kthread.h>
27
#include <linux/firmware.h>
28
#include <linux/export.h>
29
#include <linux/ctype.h>
30

J
James Bottomley 已提交
31 32 33 34 35 36
#include "sas_internal.h"

#include <scsi/scsi_host.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_tcq.h>
#include <scsi/scsi.h>
37
#include <scsi/scsi_eh.h>
J
James Bottomley 已提交
38 39
#include <scsi/scsi_transport.h>
#include <scsi/scsi_transport_sas.h>
40
#include <scsi/sas_ata.h>
J
James Bottomley 已提交
41
#include "../scsi_sas_internal.h"
42
#include "../scsi_transport_api.h"
43
#include "../scsi_priv.h"
J
James Bottomley 已提交
44 45 46

#include <linux/err.h>
#include <linux/blkdev.h>
47
#include <linux/freezer.h>
48
#include <linux/gfp.h>
J
James Bottomley 已提交
49
#include <linux/scatterlist.h>
50
#include <linux/libata.h>
J
James Bottomley 已提交
51

52 53
/* record final status and free the task */
static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
J
James Bottomley 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
{
	struct task_status_struct *ts = &task->task_status;
	int hs = 0, stat = 0;

	if (ts->resp == SAS_TASK_UNDELIVERED) {
		/* transport error */
		hs = DID_NO_CONNECT;
	} else { /* ts->resp == SAS_TASK_COMPLETE */
		/* task delivered, what happened afterwards? */
		switch (ts->stat) {
		case SAS_DEV_NO_RESPONSE:
		case SAS_INTERRUPTED:
		case SAS_PHY_DOWN:
		case SAS_NAK_R_ERR:
		case SAS_OPEN_TO:
			hs = DID_NO_CONNECT;
			break;
		case SAS_DATA_UNDERRUN:
72 73
			scsi_set_resid(sc, ts->residual);
			if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
J
James Bottomley 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
				hs = DID_ERROR;
			break;
		case SAS_DATA_OVERRUN:
			hs = DID_ERROR;
			break;
		case SAS_QUEUE_FULL:
			hs = DID_SOFT_ERROR; /* retry */
			break;
		case SAS_DEVICE_UNKNOWN:
			hs = DID_BAD_TARGET;
			break;
		case SAS_SG_ERR:
			hs = DID_PARITY;
			break;
		case SAS_OPEN_REJECT:
			if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
				hs = DID_SOFT_ERROR; /* retry */
			else
				hs = DID_ERROR;
			break;
		case SAS_PROTO_RESPONSE:
			SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
				    "task; please report this\n",
				    task->dev->port->ha->sas_ha_name);
			break;
		case SAS_ABORTED_TASK:
			hs = DID_ABORT;
			break;
102
		case SAM_STAT_CHECK_CONDITION:
J
James Bottomley 已提交
103
			memcpy(sc->sense_buffer, ts->buf,
104
			       min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
105
			stat = SAM_STAT_CHECK_CONDITION;
J
James Bottomley 已提交
106 107 108 109 110 111
			break;
		default:
			stat = ts->stat;
			break;
		}
	}
112

J
James Bottomley 已提交
113
	sc->result = (hs << 16) | stat;
114
	ASSIGN_SAS_TASK(sc, NULL);
J
James Bottomley 已提交
115 116
	list_del_init(&task->list);
	sas_free_task(task);
117 118 119 120 121
}

static void sas_scsi_task_done(struct sas_task *task)
{
	struct scsi_cmnd *sc = task->uldd_task;
122 123 124 125 126 127 128 129 130 131
	struct domain_device *dev = task->dev;
	struct sas_ha_struct *ha = dev->port->ha;
	unsigned long flags;

	spin_lock_irqsave(&dev->done_lock, flags);
	if (test_bit(SAS_HA_FROZEN, &ha->state))
		task = NULL;
	else
		ASSIGN_SAS_TASK(sc, NULL);
	spin_unlock_irqrestore(&dev->done_lock, flags);
132

133 134
	if (unlikely(!task)) {
		/* task will be completed by the error handler */
135 136 137 138 139 140 141 142 143 144 145 146
		SAS_DPRINTK("task done but aborted\n");
		return;
	}

	if (unlikely(!sc)) {
		SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
		list_del_init(&task->list);
		sas_free_task(task);
		return;
	}

	sas_end_task(sc, task);
147
	sc->scsi_done(sc);
J
James Bottomley 已提交
148 149 150 151
}

static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
					       struct domain_device *dev,
A
Al Viro 已提交
152
					       gfp_t gfp_flags)
J
James Bottomley 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
{
	struct sas_task *task = sas_alloc_task(gfp_flags);
	struct scsi_lun lun;

	if (!task)
		return NULL;

	task->uldd_task = cmd;
	ASSIGN_SAS_TASK(cmd, task);

	task->dev = dev;
	task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */

	task->ssp_task.retry_count = 1;
	int_to_scsilun(cmd->device->lun, &lun);
	memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
169
	task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
J
James Bottomley 已提交
170 171
	memcpy(task->ssp_task.cdb, cmd->cmnd, 16);

172 173 174
	task->scatter = scsi_sglist(cmd);
	task->num_scatter = scsi_sg_count(cmd);
	task->total_xfer_len = scsi_bufflen(cmd);
J
James Bottomley 已提交
175 176 177 178 179 180 181
	task->data_dir = cmd->sc_data_direction;

	task->task_done = sas_scsi_task_done;

	return task;
}

182
int sas_queue_up(struct sas_task *task)
J
James Bottomley 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196
{
	struct sas_ha_struct *sas_ha = task->dev->port->ha;
	struct scsi_core *core = &sas_ha->core;
	unsigned long flags;
	LIST_HEAD(list);

	spin_lock_irqsave(&core->task_queue_lock, flags);
	if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
		spin_unlock_irqrestore(&core->task_queue_lock, flags);
		return -SAS_QUEUE_FULL;
	}
	list_add_tail(&task->list, &core->task_queue);
	core->task_queue_size += 1;
	spin_unlock_irqrestore(&core->task_queue_lock, flags);
197
	wake_up_process(core->queue_thread);
J
James Bottomley 已提交
198 199 200 201

	return 0;
}

202
int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
J
James Bottomley 已提交
203 204
{
	struct sas_internal *i = to_sas_internal(host->transportt);
205 206 207 208
	struct domain_device *dev = cmd_to_domain_dev(cmd);
	struct sas_ha_struct *sas_ha = dev->port->ha;
	struct sas_task *task;
	int res = 0;
J
James Bottomley 已提交
209

210
	/* If the device fell off, no sense in issuing commands */
211
	if (test_bit(SAS_DEV_GONE, &dev->state)) {
212 213 214
		cmd->result = DID_BAD_TARGET << 16;
		goto out_done;
	}
J
James Bottomley 已提交
215

216
	if (dev_is_sata(dev)) {
217
		spin_lock_irq(dev->sata_dev.ap->lock);
218
		res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
219
		spin_unlock_irq(dev->sata_dev.ap->lock);
220 221
		return res;
	}
222

223 224
	task = sas_create_task(cmd, dev, GFP_ATOMIC);
	if (!task)
225
		return SCSI_MLQUEUE_HOST_BUSY;
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240
	/* Queue up, Direct Mode or Task Collector Mode. */
	if (sas_ha->lldd_max_execute_num < 2)
		res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
	else
		res = sas_queue_up(task);

	if (res)
		goto out_free_task;
	return 0;

out_free_task:
	SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
	ASSIGN_SAS_TASK(cmd, NULL);
	sas_free_task(task);
241 242 243 244
	if (res == -SAS_QUEUE_FULL)
		cmd->result = DID_SOFT_ERROR << 16; /* retry */
	else
		cmd->result = DID_ERROR << 16;
245 246 247
out_done:
	cmd->scsi_done(cmd);
	return 0;
J
James Bottomley 已提交
248 249
}

250 251 252
static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
{
	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
253
	struct sas_task *task = TO_SAS_TASK(cmd);
254

255 256 257 258 259 260
	/* At this point, we only get called following an actual abort
	 * of the task, so we should be guaranteed not to be racing with
	 * any completions from the LLD.  Task is freed after this.
	 */
	sas_end_task(cmd, task);

261 262
	/* now finish the command and move it on to the error
	 * handler done list, this also takes it off the
263 264
	 * error handler pending list.
	 */
265 266 267
	scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
}

268 269
static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
{
270
	struct domain_device *dev = cmd_to_domain_dev(cmd);
271
	struct sas_ha_struct *ha = dev->port->ha;
272
	struct sas_task *task = TO_SAS_TASK(cmd);
273 274 275 276 277 278 279 280 281 282 283

	if (!dev_is_sata(dev)) {
		sas_eh_finish_cmd(cmd);
		return;
	}

	/* report the timeout to libata */
	sas_end_task(cmd, task);
	list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
}

J
James Bottomley 已提交
284 285 286 287 288
static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
{
	struct scsi_cmnd *cmd, *n;

	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
289 290
		if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
		    cmd->device->lun == my_cmd->device->lun)
291
			sas_eh_defer_cmd(cmd);
J
James Bottomley 已提交
292 293 294 295 296 297 298 299 300 301 302 303
	}
}

static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
				     struct domain_device *dev)
{
	struct scsi_cmnd *cmd, *n;

	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
		struct domain_device *x = cmd_to_domain_dev(cmd);

		if (x == dev)
304
			sas_eh_finish_cmd(cmd);
J
James Bottomley 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317
	}
}

static void sas_scsi_clear_queue_port(struct list_head *error_q,
				      struct asd_sas_port *port)
{
	struct scsi_cmnd *cmd, *n;

	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
		struct domain_device *dev = cmd_to_domain_dev(cmd);
		struct asd_sas_port *x = dev->port;

		if (x == port)
318
			sas_eh_finish_cmd(cmd);
J
James Bottomley 已提交
319 320 321 322 323 324 325
	}
}

enum task_disposition {
	TASK_IS_DONE,
	TASK_IS_ABORTED,
	TASK_IS_AT_LU,
326
	TASK_IS_NOT_AT_HA,
J
James Bottomley 已提交
327
	TASK_IS_NOT_AT_LU,
328
	TASK_ABORT_FAILED,
J
James Bottomley 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342
};

static enum task_disposition sas_scsi_find_task(struct sas_task *task)
{
	struct sas_ha_struct *ha = task->dev->port->ha;
	unsigned long flags;
	int i, res;
	struct sas_internal *si =
		to_sas_internal(task->dev->port->ha->core.shost->transportt);

	if (ha->lldd_max_execute_num > 1) {
		struct scsi_core *core = &ha->core;
		struct sas_task *t, *n;

343
		mutex_lock(&core->task_queue_flush);
J
James Bottomley 已提交
344
		spin_lock_irqsave(&core->task_queue_lock, flags);
345
		list_for_each_entry_safe(t, n, &core->task_queue, list)
J
James Bottomley 已提交
346 347
			if (task == t) {
				list_del_init(&t->list);
348
				break;
J
James Bottomley 已提交
349 350
			}
		spin_unlock_irqrestore(&core->task_queue_lock, flags);
351 352 353 354
		mutex_unlock(&core->task_queue_flush);

		if (task == t)
			return TASK_IS_NOT_AT_HA;
J
James Bottomley 已提交
355 356 357
	}

	for (i = 0; i < 5; i++) {
358
		SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
J
James Bottomley 已提交
359 360 361 362 363
		res = si->dft->lldd_abort_task(task);

		spin_lock_irqsave(&task->task_state_lock, flags);
		if (task->task_state_flags & SAS_TASK_STATE_DONE) {
			spin_unlock_irqrestore(&task->task_state_lock, flags);
364
			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
J
James Bottomley 已提交
365 366 367 368 369 370 371
				    task);
			return TASK_IS_DONE;
		}
		spin_unlock_irqrestore(&task->task_state_lock, flags);

		if (res == TMF_RESP_FUNC_COMPLETE) {
			SAS_DPRINTK("%s: task 0x%p is aborted\n",
372
				    __func__, task);
J
James Bottomley 已提交
373 374 375
			return TASK_IS_ABORTED;
		} else if (si->dft->lldd_query_task) {
			SAS_DPRINTK("%s: querying task 0x%p\n",
376
				    __func__, task);
J
James Bottomley 已提交
377
			res = si->dft->lldd_query_task(task);
378 379
			switch (res) {
			case TMF_RESP_FUNC_SUCC:
J
James Bottomley 已提交
380
				SAS_DPRINTK("%s: task 0x%p at LU\n",
381
					    __func__, task);
J
James Bottomley 已提交
382
				return TASK_IS_AT_LU;
383
			case TMF_RESP_FUNC_COMPLETE:
J
James Bottomley 已提交
384
				SAS_DPRINTK("%s: task 0x%p not at LU\n",
385
					    __func__, task);
J
James Bottomley 已提交
386
				return TASK_IS_NOT_AT_LU;
387 388
			case TMF_RESP_FUNC_FAILED:
                                SAS_DPRINTK("%s: task 0x%p failed to abort\n",
389
                                                __func__, task);
390 391 392
                                return TASK_ABORT_FAILED;
                        }

J
James Bottomley 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		}
	}
	return res;
}

static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
{
	int res = TMF_RESP_FUNC_FAILED;
	struct scsi_lun lun;
	struct sas_internal *i =
		to_sas_internal(dev->port->ha->core.shost->transportt);

	int_to_scsilun(cmd->device->lun, &lun);

	SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
		    SAS_ADDR(dev->sas_addr),
		    cmd->device->lun);

	if (i->dft->lldd_abort_task_set)
		res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);

	if (res == TMF_RESP_FUNC_FAILED) {
		if (i->dft->lldd_clear_task_set)
			res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
	}

	if (res == TMF_RESP_FUNC_FAILED) {
		if (i->dft->lldd_lu_reset)
			res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
	}

	return res;
}

static int sas_recover_I_T(struct domain_device *dev)
{
	int res = TMF_RESP_FUNC_FAILED;
	struct sas_internal *i =
		to_sas_internal(dev->port->ha->core.shost->transportt);

	SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
		    SAS_ADDR(dev->sas_addr));

	if (i->dft->lldd_I_T_nexus_reset)
		res = i->dft->lldd_I_T_nexus_reset(dev);

	return res;
}

442 443
/* take a reference on the last known good phy for this device */
struct sas_phy *sas_get_local_phy(struct domain_device *dev)
444
{
445 446 447
	struct sas_ha_struct *ha = dev->port->ha;
	struct sas_phy *phy;
	unsigned long flags;
448

449 450 451 452 453 454 455 456 457 458 459
	/* a published domain device always has a valid phy, it may be
	 * stale, but it is never NULL
	 */
	BUG_ON(!dev->phy);

	spin_lock_irqsave(&ha->phy_port_lock, flags);
	phy = dev->phy;
	get_device(&phy->dev);
	spin_unlock_irqrestore(&ha->phy_port_lock, flags);

	return phy;
460
}
461
EXPORT_SYMBOL_GPL(sas_get_local_phy);
462

463
/* Attempt to send a LUN reset message to a device */
464
int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
{
	struct domain_device *dev = cmd_to_domain_dev(cmd);
	struct sas_internal *i =
		to_sas_internal(dev->port->ha->core.shost->transportt);
	struct scsi_lun lun;
	int res;

	int_to_scsilun(cmd->device->lun, &lun);

	if (!i->dft->lldd_lu_reset)
		return FAILED;

	res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
		return SUCCESS;

	return FAILED;
}

/* Attempt to send a phy (bus) reset */
int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
486 487
{
	struct domain_device *dev = cmd_to_domain_dev(cmd);
488
	struct sas_phy *phy = sas_get_local_phy(dev);
489 490 491 492
	int res;

	res = sas_phy_reset(phy, 1);
	if (res)
493
		SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
494
			    kobject_name(&phy->dev.kobj),
495
			    res);
496 497
	sas_put_local_phy(phy);

498 499 500 501 502 503 504
	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
		return SUCCESS;

	return FAILED;
}

/* Try to reset a device */
505
static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
506
{
507
	int res;
508
	struct Scsi_Host *shost = cmd->device->host;
509

510
	if (!shost->hostt->eh_device_reset_handler)
511 512 513 514 515
		goto try_bus_reset;

	res = shost->hostt->eh_device_reset_handler(cmd);
	if (res == SUCCESS)
		return res;
516

517 518 519 520 521
try_bus_reset:
	if (shost->hostt->eh_bus_reset_handler)
		return shost->hostt->eh_bus_reset_handler(cmd);

	return FAILED;
522 523
}

524
static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
J
James Bottomley 已提交
525 526 527
{
	struct scsi_cmnd *cmd, *n;
	enum task_disposition res = TASK_IS_DONE;
528
	int tmf_resp, need_reset;
J
James Bottomley 已提交
529
	struct sas_internal *i = to_sas_internal(shost->transportt);
530 531
	unsigned long flags;
	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
532
	LIST_HEAD(done);
J
James Bottomley 已提交
533

534
	/* clean out any commands that won the completion vs eh race */
535
	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
536 537 538 539 540 541 542 543 544 545
		struct domain_device *dev = cmd_to_domain_dev(cmd);
		struct sas_task *task;

		spin_lock_irqsave(&dev->done_lock, flags);
		/* by this point the lldd has either observed
		 * SAS_HA_FROZEN and is leaving the task alone, or has
		 * won the race with eh and decided to complete it
		 */
		task = TO_SAS_TASK(cmd);
		spin_unlock_irqrestore(&dev->done_lock, flags);
J
James Bottomley 已提交
546

547
		if (!task)
548 549 550 551 552 553
			list_move_tail(&cmd->eh_entry, &done);
	}

 Again:
	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
		struct sas_task *task = TO_SAS_TASK(cmd);
554 555

		list_del_init(&cmd->eh_entry);
556 557 558 559 560

		spin_lock_irqsave(&task->task_state_lock, flags);
		need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
		spin_unlock_irqrestore(&task->task_state_lock, flags);

561 562
		if (need_reset) {
			SAS_DPRINTK("%s: task 0x%p requests reset\n",
563
				    __func__, task);
564 565 566
			goto reset;
		}

J
James Bottomley 已提交
567 568 569 570 571 572
		SAS_DPRINTK("trying to find task 0x%p\n", task);
		res = sas_scsi_find_task(task);

		cmd->eh_eflags = 0;

		switch (res) {
573 574 575 576 577 578 579 580
		case TASK_IS_NOT_AT_HA:
			SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
				    __func__, task,
				    cmd->retries ? "retry" : "aborted");
			if (cmd->retries)
				cmd->retries--;
			sas_eh_finish_cmd(cmd);
			continue;
J
James Bottomley 已提交
581
		case TASK_IS_DONE:
582
			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
J
James Bottomley 已提交
583
				    task);
584
			sas_eh_defer_cmd(cmd);
J
James Bottomley 已提交
585 586 587
			continue;
		case TASK_IS_ABORTED:
			SAS_DPRINTK("%s: task 0x%p is aborted\n",
588
				    __func__, task);
589
			sas_eh_defer_cmd(cmd);
J
James Bottomley 已提交
590 591 592
			continue;
		case TASK_IS_AT_LU:
			SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
593
 reset:
J
James Bottomley 已提交
594 595 596 597 598 599
			tmf_resp = sas_recover_lu(task->dev, cmd);
			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
				SAS_DPRINTK("dev %016llx LU %x is "
					    "recovered\n",
					    SAS_ADDR(task->dev),
					    cmd->device->lun);
600
				sas_eh_defer_cmd(cmd);
601
				sas_scsi_clear_queue_lu(work_q, cmd);
J
James Bottomley 已提交
602 603 604 605
				goto Again;
			}
			/* fallthrough */
		case TASK_IS_NOT_AT_LU:
606
		case TASK_ABORT_FAILED:
J
James Bottomley 已提交
607 608 609 610
			SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
				    task);
			tmf_resp = sas_recover_I_T(task->dev);
			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
611
				struct domain_device *dev = task->dev;
J
James Bottomley 已提交
612 613
				SAS_DPRINTK("I_T %016llx recovered\n",
					    SAS_ADDR(task->dev->sas_addr));
614
				sas_eh_finish_cmd(cmd);
615
				sas_scsi_clear_queue_I_T(work_q, dev);
J
James Bottomley 已提交
616 617 618
				goto Again;
			}
			/* Hammer time :-) */
619
			try_to_reset_cmd_device(cmd);
J
James Bottomley 已提交
620 621 622 623 624 625 626 627
			if (i->dft->lldd_clear_nexus_port) {
				struct asd_sas_port *port = task->dev->port;
				SAS_DPRINTK("clearing nexus for port:%d\n",
					    port->id);
				res = i->dft->lldd_clear_nexus_port(port);
				if (res == TMF_RESP_FUNC_COMPLETE) {
					SAS_DPRINTK("clear nexus port:%d "
						    "succeeded\n", port->id);
628
					sas_eh_finish_cmd(cmd);
629
					sas_scsi_clear_queue_port(work_q,
J
James Bottomley 已提交
630 631 632 633 634 635 636 637 638 639
								  port);
					goto Again;
				}
			}
			if (i->dft->lldd_clear_nexus_ha) {
				SAS_DPRINTK("clear nexus ha\n");
				res = i->dft->lldd_clear_nexus_ha(ha);
				if (res == TMF_RESP_FUNC_COMPLETE) {
					SAS_DPRINTK("clear nexus ha "
						    "succeeded\n");
640 641
					sas_eh_finish_cmd(cmd);
					goto clear_q;
J
James Bottomley 已提交
642 643 644 645 646 647 648 649 650 651 652
				}
			}
			/* If we are here -- this means that no amount
			 * of effort could recover from errors.  Quite
			 * possibly the HA just disappeared.
			 */
			SAS_DPRINTK("error from  device %llx, LUN %x "
				    "couldn't be recovered in any way\n",
				    SAS_ADDR(task->dev->sas_addr),
				    cmd->device->lun);

653
			sas_eh_finish_cmd(cmd);
J
James Bottomley 已提交
654 655 656
			goto clear_q;
		}
	}
657 658
 out:
	list_splice_tail(&done, work_q);
659
	list_splice_tail_init(&ha->eh_ata_q, work_q);
660
	return;
661 662

 clear_q:
663
	SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
664 665
	list_for_each_entry_safe(cmd, n, work_q, eh_entry)
		sas_eh_finish_cmd(cmd);
666
	goto out;
667 668 669 670 671 672 673 674 675 676
}

void sas_scsi_recover_host(struct Scsi_Host *shost)
{
	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
	unsigned long flags;
	LIST_HEAD(eh_work_q);

	spin_lock_irqsave(shost->host_lock, flags);
	list_splice_init(&shost->eh_cmd_q, &eh_work_q);
677
	shost->host_eh_scheduled = 0;
678 679
	spin_unlock_irqrestore(shost->host_lock, flags);

680 681
	SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
		    __func__, shost->host_busy, shost->host_failed);
682 683
	/*
	 * Deal with commands that still have SAS tasks (i.e. they didn't
684 685
	 * complete via the normal sas_task completion mechanism),
	 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
686
	 */
687
	set_bit(SAS_HA_FROZEN, &ha->state);
688 689 690
	sas_eh_handle_sas_errors(shost, &eh_work_q);
	clear_bit(SAS_HA_FROZEN, &ha->state);
	if (list_empty(&eh_work_q))
691 692 693 694 695 696 697 698
		goto out;

	/*
	 * Now deal with SCSI commands that completed ok but have a an error
	 * code (and hopefully sense data) attached.  This is roughly what
	 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
	 * command we see here has no sas_task and is thus unknown to the HA.
	 */
699 700 701
	sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
	if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
		scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
702 703

out:
704 705 706
	if (ha->lldd_max_execute_num > 1)
		wake_up_process(ha->core.queue_thread);

707 708 709
	/* now link into libata eh --- if we have any ata devices */
	sas_ata_strategy_handler(shost);

710
	scsi_eh_flush_done_q(&ha->eh_done_q);
711

712 713
	SAS_DPRINTK("--- Exit %s: busy: %d failed: %d\n",
		    __func__, shost->host_busy, shost->host_failed);
J
James Bottomley 已提交
714 715
}

J
Jens Axboe 已提交
716
enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
J
James Bottomley 已提交
717
{
718
	scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
J
James Bottomley 已提交
719

J
Jens Axboe 已提交
720
	return BLK_EH_NOT_HANDLED;
J
James Bottomley 已提交
721 722
}

723 724 725 726 727
int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
{
	struct domain_device *dev = sdev_to_domain_dev(sdev);

	if (dev_is_sata(dev))
728
		return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
729 730 731 732

	return -EINVAL;
}

J
James Bottomley 已提交
733 734 735 736 737 738
struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
{
	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
	struct domain_device *found_dev = NULL;
	int i;
739
	unsigned long flags;
J
James Bottomley 已提交
740

741
	spin_lock_irqsave(&ha->phy_port_lock, flags);
J
James Bottomley 已提交
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	for (i = 0; i < ha->num_phys; i++) {
		struct asd_sas_port *port = ha->sas_port[i];
		struct domain_device *dev;

		spin_lock(&port->dev_list_lock);
		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
			if (rphy == dev->rphy) {
				found_dev = dev;
				spin_unlock(&port->dev_list_lock);
				goto found;
			}
		}
		spin_unlock(&port->dev_list_lock);
	}
 found:
757
	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
J
James Bottomley 已提交
758 759 760 761 762 763

	return found_dev;
}

int sas_target_alloc(struct scsi_target *starget)
{
764 765
	struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
	struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
J
James Bottomley 已提交
766 767 768 769

	if (!found_dev)
		return -ENODEV;

770
	kref_get(&found_dev->kref);
J
James Bottomley 已提交
771 772 773 774
	starget->hostdata = found_dev;
	return 0;
}

D
Dan Williams 已提交
775
#define SAS_DEF_QD 256
J
James Bottomley 已提交
776 777 778 779 780 781 782 783

int sas_slave_configure(struct scsi_device *scsi_dev)
{
	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
	struct sas_ha_struct *sas_ha;

	BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);

784 785 786 787 788
	if (dev_is_sata(dev)) {
		ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
		return 0;
	}

J
James Bottomley 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	sas_ha = dev->port->ha;

	sas_read_port_mode_page(scsi_dev);

	if (scsi_dev->tagged_supported) {
		scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
		scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
	} else {
		SAS_DPRINTK("device %llx, LUN %x doesn't support "
			    "TCQ\n", SAS_ADDR(dev->sas_addr),
			    scsi_dev->lun);
		scsi_dev->tagged_supported = 0;
		scsi_set_tag_type(scsi_dev, 0);
		scsi_deactivate_tcq(scsi_dev, 1);
	}

805 806
	scsi_dev->allow_restart = 1;

J
James Bottomley 已提交
807 808 809
	return 0;
}

D
Dan Williams 已提交
810
int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
J
James Bottomley 已提交
811
{
D
Dan Williams 已提交
812
	struct domain_device *dev = sdev_to_domain_dev(sdev);
J
James Bottomley 已提交
813

814
	if (dev_is_sata(dev))
D
Dan Williams 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828
		return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
						reason);

	switch (reason) {
	case SCSI_QDEPTH_DEFAULT:
	case SCSI_QDEPTH_RAMP_UP:
		if (!sdev->tagged_supported)
			depth = 1;
		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
		break;
	case SCSI_QDEPTH_QFULL:
		scsi_track_queue_full(sdev, depth);
		break;
	default:
829
		return -EOPNOTSUPP;
J
James Bottomley 已提交
830 831
	}

D
Dan Williams 已提交
832
	return depth;
J
James Bottomley 已提交
833 834 835 836
}

int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
{
837 838 839 840 841
	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);

	if (dev_is_sata(dev))
		return -EINVAL;

J
James Bottomley 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
	if (!scsi_dev->tagged_supported)
		return 0;

	scsi_deactivate_tcq(scsi_dev, 1);

	scsi_set_tag_type(scsi_dev, qt);
	scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);

	return qt;
}

int sas_bios_param(struct scsi_device *scsi_dev,
			  struct block_device *bdev,
			  sector_t capacity, int *hsc)
{
	hsc[0] = 255;
	hsc[1] = 63;
	sector_div(capacity, 255*63);
	hsc[2] = capacity;

	return 0;
}

/* ---------- Task Collector Thread implementation ---------- */

static void sas_queue(struct sas_ha_struct *sas_ha)
{
	struct scsi_core *core = &sas_ha->core;
	unsigned long flags;
	LIST_HEAD(q);
	int can_queue;
	int res;
	struct sas_internal *i = to_sas_internal(core->shost->transportt);

876
	mutex_lock(&core->task_queue_flush);
J
James Bottomley 已提交
877
	spin_lock_irqsave(&core->task_queue_lock, flags);
878
	while (!kthread_should_stop() &&
879 880
	       !list_empty(&core->task_queue) &&
	       !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
J
James Bottomley 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

		can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
		if (can_queue >= 0) {
			can_queue = core->task_queue_size;
			list_splice_init(&core->task_queue, &q);
		} else {
			struct list_head *a, *n;

			can_queue = sas_ha->lldd_queue_size;
			list_for_each_safe(a, n, &core->task_queue) {
				list_move_tail(a, &q);
				if (--can_queue == 0)
					break;
			}
			can_queue = sas_ha->lldd_queue_size;
		}
		core->task_queue_size -= can_queue;
		spin_unlock_irqrestore(&core->task_queue_lock, flags);
		{
			struct sas_task *task = list_entry(q.next,
							   struct sas_task,
							   list);
			list_del_init(&q);
			res = i->dft->lldd_execute_task(task, can_queue,
							GFP_KERNEL);
			if (unlikely(res))
				__list_add(&q, task->list.prev, &task->list);
		}
		spin_lock_irqsave(&core->task_queue_lock, flags);
		if (res) {
			list_splice_init(&q, &core->task_queue); /*at head*/
			core->task_queue_size += can_queue;
		}
	}
	spin_unlock_irqrestore(&core->task_queue_lock, flags);
916
	mutex_unlock(&core->task_queue_flush);
J
James Bottomley 已提交
917 918 919 920 921 922 923 924 925 926 927
}

/**
 * sas_queue_thread -- The Task Collector thread
 * @_sas_ha: pointer to struct sas_ha
 */
static int sas_queue_thread(void *_sas_ha)
{
	struct sas_ha_struct *sas_ha = _sas_ha;

	while (1) {
928 929
		set_current_state(TASK_INTERRUPTIBLE);
		schedule();
J
James Bottomley 已提交
930
		sas_queue(sas_ha);
931
		if (kthread_should_stop())
J
James Bottomley 已提交
932 933 934 935 936 937 938 939 940 941 942
			break;
	}

	return 0;
}

int sas_init_queue(struct sas_ha_struct *sas_ha)
{
	struct scsi_core *core = &sas_ha->core;

	spin_lock_init(&core->task_queue_lock);
943
	mutex_init(&core->task_queue_flush);
J
James Bottomley 已提交
944 945 946
	core->task_queue_size = 0;
	INIT_LIST_HEAD(&core->task_queue);

947 948 949 950 951
	core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
					 "sas_queue_%d", core->shost->host_no);
	if (IS_ERR(core->queue_thread))
		return PTR_ERR(core->queue_thread);
	return 0;
J
James Bottomley 已提交
952 953 954 955 956 957 958 959
}

void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
{
	unsigned long flags;
	struct scsi_core *core = &sas_ha->core;
	struct sas_task *task, *n;

960
	kthread_stop(core->queue_thread);
J
James Bottomley 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979

	if (!list_empty(&core->task_queue))
		SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
			    SAS_ADDR(sas_ha->sas_addr));

	spin_lock_irqsave(&core->task_queue_lock, flags);
	list_for_each_entry_safe(task, n, &core->task_queue, list) {
		struct scsi_cmnd *cmd = task->uldd_task;

		list_del_init(&task->list);

		ASSIGN_SAS_TASK(cmd, NULL);
		sas_free_task(task);
		cmd->result = DID_ABORT << 16;
		cmd->scsi_done(cmd);
	}
	spin_unlock_irqrestore(&core->task_queue_lock, flags);
}

980 981 982 983 984
/*
 * Tell an upper layer that it needs to initiate an abort for a given task.
 * This should only ever be called by an LLDD.
 */
void sas_task_abort(struct sas_task *task)
985
{
986
	struct scsi_cmnd *sc = task->uldd_task;
987

988 989 990
	/* Escape for libsas internal commands */
	if (!sc) {
		if (!del_timer(&task->timer))
991
			return;
992 993 994
		task->timer.function(task->timer.data);
		return;
	}
995

996 997
	if (dev_is_sata(task->dev)) {
		sas_ata_task_abort(task);
998 999 1000
	} else {
		struct request_queue *q = sc->device->request_queue;
		unsigned long flags;
1001

1002 1003 1004 1005 1006
		spin_lock_irqsave(q->queue_lock, flags);
		blk_abort_request(sc->request);
		spin_unlock_irqrestore(q->queue_lock, flags);
		scsi_schedule_eh(sc->device->host);
	}
1007 1008
}

1009 1010
void sas_target_destroy(struct scsi_target *starget)
{
1011
	struct domain_device *found_dev = starget->hostdata;
1012 1013 1014 1015

	if (!found_dev)
		return;

1016 1017
	starget->hostdata = NULL;
	sas_put_device(found_dev);
1018 1019
}

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
static void sas_parse_addr(u8 *sas_addr, const char *p)
{
	int i;
	for (i = 0; i < SAS_ADDR_SIZE; i++) {
		u8 h, l;
		if (!*p)
			break;
		h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
		p++;
		l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
		p++;
		sas_addr[i] = (h<<4) | l;
	}
}

#define SAS_STRING_ADDR_SIZE	16

int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
{
	int res;
	const struct firmware *fw;

	res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
	if (res)
		return res;

	if (fw->size < SAS_STRING_ADDR_SIZE) {
		res = -ENODEV;
		goto out;
	}

	sas_parse_addr(addr, fw->data);

out:
	release_firmware(fw);
	return res;
}
EXPORT_SYMBOL_GPL(sas_request_addr);

J
James Bottomley 已提交
1059 1060 1061 1062 1063 1064
EXPORT_SYMBOL_GPL(sas_queuecommand);
EXPORT_SYMBOL_GPL(sas_target_alloc);
EXPORT_SYMBOL_GPL(sas_slave_configure);
EXPORT_SYMBOL_GPL(sas_change_queue_depth);
EXPORT_SYMBOL_GPL(sas_change_queue_type);
EXPORT_SYMBOL_GPL(sas_bios_param);
1065
EXPORT_SYMBOL_GPL(sas_task_abort);
1066
EXPORT_SYMBOL_GPL(sas_phy_reset);
1067
EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1068
EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1069 1070
EXPORT_SYMBOL_GPL(sas_target_destroy);
EXPORT_SYMBOL_GPL(sas_ioctl);