remote_device.c 42.9 KB
Newer Older
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * 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., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.GPL.
 *
 * BSD LICENSE
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Intel Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
D
Dave Jiang 已提交
55
#include <scsi/sas.h>
56
#include <linux/bitops.h>
57 58 59 60 61 62 63 64
#include "isci.h"
#include "port.h"
#include "remote_device.h"
#include "request.h"
#include "remote_node_context.h"
#include "scu_event_codes.h"
#include "task.h"

65 66 67 68 69 70 71 72 73 74
#undef C
#define C(a) (#a)
const char *dev_state_name(enum sci_remote_device_states state)
{
	static const char * const strings[] = REMOTE_DEV_STATES;

	return strings[state];
}
#undef C

75
/**
76
 * isci_remote_device_not_ready() - This function is called by the ihost when
77 78 79 80 81
 *    the remote device is not ready. We mark the isci device as ready (not
 *    "ready_for_io") and signal the waiting proccess.
 * @isci_host: This parameter specifies the isci host object.
 * @isci_device: This parameter specifies the remote device
 *
82
 * sci_lock is held on entrance to this function.
83 84 85 86
 */
static void isci_remote_device_not_ready(struct isci_host *ihost,
				  struct isci_remote_device *idev, u32 reason)
{
87
	struct isci_request *ireq;
88

89 90 91
	dev_dbg(&ihost->pdev->dev,
		"%s: isci_device = %p\n", __func__, idev);

92 93
	switch (reason) {
	case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
94
		set_bit(IDEV_GONE, &idev->flags);
95 96 97 98 99 100 101 102 103 104 105
		break;
	case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
		set_bit(IDEV_IO_NCQERROR, &idev->flags);

		/* Kill all outstanding requests for the device. */
		list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {

			dev_dbg(&ihost->pdev->dev,
				"%s: isci_device = %p request = %p\n",
				__func__, idev, ireq);

106
			sci_controller_terminate_request(ihost,
107
							  idev,
108
							  ireq);
109 110 111
		}
		/* Fall through into the default case... */
	default:
112
		clear_bit(IDEV_IO_READY, &idev->flags);
113 114
		break;
	}
115 116 117
}

/**
118
 * isci_remote_device_ready() - This function is called by the ihost when the
119 120 121 122 123 124 125 126 127 128 129
 *    remote device is ready. We mark the isci device as ready and signal the
 *    waiting proccess.
 * @ihost: our valid isci_host
 * @idev: remote device
 *
 */
static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
{
	dev_dbg(&ihost->pdev->dev,
		"%s: idev = %p\n", __func__, idev);

130
	clear_bit(IDEV_IO_NCQERROR, &idev->flags);
131
	set_bit(IDEV_IO_READY, &idev->flags);
132 133 134 135
	if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
		wake_up(&ihost->eventq);
}

136 137 138 139 140
/* called once the remote node context is ready to be freed.
 * The remote device can now report that its stop operation is complete. none
 */
static void rnc_destruct_done(void *_dev)
{
141
	struct isci_remote_device *idev = _dev;
142

143 144
	BUG_ON(idev->started_request_count != 0);
	sci_change_state(&idev->sm, SCI_DEV_STOPPED);
145
}
146

147
static enum sci_status sci_remote_device_terminate_requests(struct isci_remote_device *idev)
148
{
149
	struct isci_host *ihost = idev->owning_port->owning_controller;
150
	enum sci_status status  = SCI_SUCCESS;
151
	u32 i;
152

153
	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
D
Dan Williams 已提交
154
		struct isci_request *ireq = ihost->reqs[i];
155 156
		enum sci_status s;

D
Dan Williams 已提交
157
		if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
158
		    ireq->target_device != idev)
159
			continue;
D
Dan Williams 已提交
160

161
		s = sci_controller_terminate_request(ihost, idev, ireq);
162 163 164 165 166 167 168
		if (s != SCI_SUCCESS)
			status = s;
	}

	return status;
}

169
enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
170
					u32 timeout)
171
{
172
	struct sci_base_state_machine *sm = &idev->sm;
173
	enum sci_remote_device_states state = sm->current_state_id;
174 175

	switch (state) {
E
Edmund Nadolski 已提交
176 177 178
	case SCI_DEV_INITIAL:
	case SCI_DEV_FAILED:
	case SCI_DEV_FINAL:
179
	default:
180 181
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
182
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
183
	case SCI_DEV_STOPPED:
184
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
185
	case SCI_DEV_STARTING:
186
		/* device not started so there had better be no requests */
187
		BUG_ON(idev->started_request_count != 0);
188
		sci_remote_node_context_destruct(&idev->rnc,
189
						      rnc_destruct_done, idev);
190 191 192
		/* Transition to the stopping state and wait for the
		 * remote node to complete being posted and invalidated.
		 */
E
Edmund Nadolski 已提交
193
		sci_change_state(sm, SCI_DEV_STOPPING);
194
		return SCI_SUCCESS;
E
Edmund Nadolski 已提交
195 196 197 198 199 200 201 202 203
	case SCI_DEV_READY:
	case SCI_STP_DEV_IDLE:
	case SCI_STP_DEV_CMD:
	case SCI_STP_DEV_NCQ:
	case SCI_STP_DEV_NCQ_ERROR:
	case SCI_STP_DEV_AWAIT_RESET:
	case SCI_SMP_DEV_IDLE:
	case SCI_SMP_DEV_CMD:
		sci_change_state(sm, SCI_DEV_STOPPING);
204
		if (idev->started_request_count == 0) {
205
			sci_remote_node_context_destruct(&idev->rnc,
206
							      rnc_destruct_done, idev);
207 208
			return SCI_SUCCESS;
		} else
209
			return sci_remote_device_terminate_requests(idev);
210
		break;
E
Edmund Nadolski 已提交
211
	case SCI_DEV_STOPPING:
212 213 214 215
		/* All requests should have been terminated, but if there is an
		 * attempt to stop a device already in the stopping state, then
		 * try again to terminate.
		 */
216
		return sci_remote_device_terminate_requests(idev);
E
Edmund Nadolski 已提交
217 218
	case SCI_DEV_RESETTING:
		sci_change_state(sm, SCI_DEV_STOPPING);
219 220
		return SCI_SUCCESS;
	}
221 222
}

223
enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
224
{
225
	struct sci_base_state_machine *sm = &idev->sm;
226
	enum sci_remote_device_states state = sm->current_state_id;
227 228

	switch (state) {
E
Edmund Nadolski 已提交
229 230 231 232 233 234 235 236 237
	case SCI_DEV_INITIAL:
	case SCI_DEV_STOPPED:
	case SCI_DEV_STARTING:
	case SCI_SMP_DEV_IDLE:
	case SCI_SMP_DEV_CMD:
	case SCI_DEV_STOPPING:
	case SCI_DEV_FAILED:
	case SCI_DEV_RESETTING:
	case SCI_DEV_FINAL:
238
	default:
239 240
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
241
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
242 243 244 245 246 247 248
	case SCI_DEV_READY:
	case SCI_STP_DEV_IDLE:
	case SCI_STP_DEV_CMD:
	case SCI_STP_DEV_NCQ:
	case SCI_STP_DEV_NCQ_ERROR:
	case SCI_STP_DEV_AWAIT_RESET:
		sci_change_state(sm, SCI_DEV_RESETTING);
249 250
		return SCI_SUCCESS;
	}
251 252
}

253
enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
254
{
255
	struct sci_base_state_machine *sm = &idev->sm;
256
	enum sci_remote_device_states state = sm->current_state_id;
257

E
Edmund Nadolski 已提交
258
	if (state != SCI_DEV_RESETTING) {
259 260
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
261 262
		return SCI_FAILURE_INVALID_STATE;
	}
263

E
Edmund Nadolski 已提交
264
	sci_change_state(sm, SCI_DEV_READY);
265 266
	return SCI_SUCCESS;
}
267

268
enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev)
269
{
270
	return sci_remote_node_context_suspend(&idev->rnc,
271 272 273
					       SCI_SOFTWARE_SUSPENSION,
					       SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
					       NULL, NULL);
274 275
}

276
enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
277
						     u32 frame_index)
278
{
279
	struct sci_base_state_machine *sm = &idev->sm;
280
	enum sci_remote_device_states state = sm->current_state_id;
281
	struct isci_host *ihost = idev->owning_port->owning_controller;
282 283 284
	enum sci_status status;

	switch (state) {
E
Edmund Nadolski 已提交
285 286 287 288 289 290
	case SCI_DEV_INITIAL:
	case SCI_DEV_STOPPED:
	case SCI_DEV_STARTING:
	case SCI_STP_DEV_IDLE:
	case SCI_SMP_DEV_IDLE:
	case SCI_DEV_FINAL:
291
	default:
292 293
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
294
		/* Return the frame back to the controller */
295
		sci_controller_release_frame(ihost, frame_index);
296
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
297 298 299 300 301 302
	case SCI_DEV_READY:
	case SCI_STP_DEV_NCQ_ERROR:
	case SCI_STP_DEV_AWAIT_RESET:
	case SCI_DEV_STOPPING:
	case SCI_DEV_FAILED:
	case SCI_DEV_RESETTING: {
303
		struct isci_request *ireq;
D
Dave Jiang 已提交
304 305 306
		struct ssp_frame_hdr hdr;
		void *frame_header;
		ssize_t word_cnt;
307

308
		status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
309
								       frame_index,
D
Dave Jiang 已提交
310
								       &frame_header);
311 312 313
		if (status != SCI_SUCCESS)
			return status;

D
Dave Jiang 已提交
314 315 316
		word_cnt = sizeof(hdr) / sizeof(u32);
		sci_swab32_cpy(&hdr, frame_header, word_cnt);

317
		ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
318
		if (ireq && ireq->target_device == idev) {
319
			/* The IO request is now in charge of releasing the frame */
320
			status = sci_io_request_frame_handler(ireq, frame_index);
321 322 323 324
		} else {
			/* We could not map this tag to a valid IO
			 * request Just toss the frame and continue
			 */
325
			sci_controller_release_frame(ihost, frame_index);
326 327 328
		}
		break;
	}
E
Edmund Nadolski 已提交
329
	case SCI_STP_DEV_NCQ: {
330
		struct dev_to_host_fis *hdr;
331

332
		status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
333 334 335 336 337
								       frame_index,
								       (void **)&hdr);
		if (status != SCI_SUCCESS)
			return status;

338 339
		if (hdr->fis_type == FIS_SETDEVBITS &&
		    (hdr->status & ATA_ERR)) {
340
			idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
341 342

			/* TODO Check sactive and complete associated IO if any. */
E
Edmund Nadolski 已提交
343
			sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
344 345
		} else if (hdr->fis_type == FIS_REGD2H &&
			   (hdr->status & ATA_ERR)) {
346 347 348 349
			/*
			 * Some devices return D2H FIS when an NCQ error is detected.
			 * Treat this like an SDB error FIS ready reason.
			 */
350 351
			idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
			sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
352 353 354
		} else
			status = SCI_FAILURE;

355
		sci_controller_release_frame(ihost, frame_index);
356 357
		break;
	}
E
Edmund Nadolski 已提交
358 359
	case SCI_STP_DEV_CMD:
	case SCI_SMP_DEV_CMD:
360 361 362 363
		/* The device does not process any UF received from the hardware while
		 * in this state.  All unsolicited frames are forwarded to the io request
		 * object.
		 */
364
		status = sci_io_request_frame_handler(idev->working_request, frame_index);
365 366 367 368
		break;
	}

	return status;
369 370
}

371
static bool is_remote_device_ready(struct isci_remote_device *idev)
372
{
373

374
	struct sci_base_state_machine *sm = &idev->sm;
375
	enum sci_remote_device_states state = sm->current_state_id;
376 377

	switch (state) {
E
Edmund Nadolski 已提交
378 379 380 381 382 383 384 385
	case SCI_DEV_READY:
	case SCI_STP_DEV_IDLE:
	case SCI_STP_DEV_CMD:
	case SCI_STP_DEV_NCQ:
	case SCI_STP_DEV_NCQ_ERROR:
	case SCI_STP_DEV_AWAIT_RESET:
	case SCI_SMP_DEV_IDLE:
	case SCI_SMP_DEV_CMD:
386 387 388 389 390 391
		return true;
	default:
		return false;
	}
}

D
Dan Williams 已提交
392 393 394 395 396 397 398 399 400 401 402 403
/*
 * called once the remote node context has transisitioned to a ready
 * state (after suspending RX and/or TX due to early D2H fis)
 */
static void atapi_remote_device_resume_done(void *_dev)
{
	struct isci_remote_device *idev = _dev;
	struct isci_request *ireq = idev->working_request;

	sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
}

404
enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
405 406 407 408 409 410 411 412
						     u32 event_code)
{
	enum sci_status status;

	switch (scu_get_event_type(event_code)) {
	case SCU_EVENT_TYPE_RNC_OPS_MISC:
	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
413
		status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
414 415 416 417 418 419
		break;
	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
		if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
			status = SCI_SUCCESS;

			/* Suspend the associated RNC */
420 421 422 423 424
			sci_remote_node_context_suspend(
				&idev->rnc,
				SCI_SOFTWARE_SUSPENSION,
				SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
				NULL, NULL);
425

426
			dev_dbg(scirdev_to_dev(idev),
427
				"%s: device: %p event code: %x: %s\n",
428 429
				__func__, idev, event_code,
				is_remote_device_ready(idev)
430 431 432 433 434 435 436
				? "I_T_Nexus_Timeout event"
				: "I_T_Nexus_Timeout event in wrong state");

			break;
		}
	/* Else, fall through and treat as unhandled... */
	default:
437
		dev_dbg(scirdev_to_dev(idev),
438
			"%s: device: %p event code: %x: %s\n",
439 440
			__func__, idev, event_code,
			is_remote_device_ready(idev)
441 442 443 444 445 446 447 448 449 450
			? "unexpected event"
			: "unexpected event in wrong state");
		status = SCI_FAILURE_INVALID_STATE;
		break;
	}

	if (status != SCI_SUCCESS)
		return status;

	return status;
451 452
}

453
static void sci_remote_device_start_request(struct isci_remote_device *idev,
454
						 struct isci_request *ireq,
455
						 enum sci_status status)
456
{
457
	struct isci_port *iport = idev->owning_port;
458 459 460

	/* cleanup requests that failed after starting on the port */
	if (status != SCI_SUCCESS)
461
		sci_port_complete_io(iport, idev, ireq);
462
	else {
463
		kref_get(&idev->kref);
D
Dan Williams 已提交
464
		idev->started_request_count++;
465
	}
466 467
}

468
enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
469
						struct isci_remote_device *idev,
470
						struct isci_request *ireq)
471
{
472
	struct sci_base_state_machine *sm = &idev->sm;
473
	enum sci_remote_device_states state = sm->current_state_id;
474
	struct isci_port *iport = idev->owning_port;
475 476 477
	enum sci_status status;

	switch (state) {
E
Edmund Nadolski 已提交
478 479 480 481 482 483 484 485
	case SCI_DEV_INITIAL:
	case SCI_DEV_STOPPED:
	case SCI_DEV_STARTING:
	case SCI_STP_DEV_NCQ_ERROR:
	case SCI_DEV_STOPPING:
	case SCI_DEV_FAILED:
	case SCI_DEV_RESETTING:
	case SCI_DEV_FINAL:
486
	default:
487 488
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
489
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
490
	case SCI_DEV_READY:
491 492 493 494 495
		/* attempt to start an io request for this device object. The remote
		 * device object will issue the start request for the io and if
		 * successful it will start the request for the port object then
		 * increment its own request count.
		 */
496
		status = sci_port_start_io(iport, idev, ireq);
497 498 499
		if (status != SCI_SUCCESS)
			return status;

500
		status = sci_remote_node_context_start_io(&idev->rnc, ireq);
501 502 503
		if (status != SCI_SUCCESS)
			break;

504
		status = sci_request_start(ireq);
505
		break;
E
Edmund Nadolski 已提交
506
	case SCI_STP_DEV_IDLE: {
507 508 509 510 511 512 513 514
		/* handle the start io operation for a sata device that is in
		 * the command idle state. - Evalute the type of IO request to
		 * be started - If its an NCQ request change to NCQ substate -
		 * If its any other command change to the CMD substate
		 *
		 * If this is a softreset we may want to have a different
		 * substate.
		 */
515
		enum sci_remote_device_states new_state;
516
		struct sas_task *task = isci_request_access_task(ireq);
517

518
		status = sci_port_start_io(iport, idev, ireq);
519 520 521
		if (status != SCI_SUCCESS)
			return status;

522
		status = sci_remote_node_context_start_io(&idev->rnc, ireq);
523 524 525
		if (status != SCI_SUCCESS)
			break;

526
		status = sci_request_start(ireq);
527 528 529
		if (status != SCI_SUCCESS)
			break;

530
		if (task->ata_task.use_ncq)
E
Edmund Nadolski 已提交
531
			new_state = SCI_STP_DEV_NCQ;
532
		else {
533
			idev->working_request = ireq;
E
Edmund Nadolski 已提交
534
			new_state = SCI_STP_DEV_CMD;
535
		}
E
Edmund Nadolski 已提交
536
		sci_change_state(sm, new_state);
537 538
		break;
	}
E
Edmund Nadolski 已提交
539
	case SCI_STP_DEV_NCQ: {
540 541 542
		struct sas_task *task = isci_request_access_task(ireq);

		if (task->ata_task.use_ncq) {
543
			status = sci_port_start_io(iport, idev, ireq);
544 545 546
			if (status != SCI_SUCCESS)
				return status;

547
			status = sci_remote_node_context_start_io(&idev->rnc, ireq);
548 549 550
			if (status != SCI_SUCCESS)
				break;

551
			status = sci_request_start(ireq);
552 553 554
		} else
			return SCI_FAILURE_INVALID_STATE;
		break;
555
	}
E
Edmund Nadolski 已提交
556
	case SCI_STP_DEV_AWAIT_RESET:
557
		return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
E
Edmund Nadolski 已提交
558
	case SCI_SMP_DEV_IDLE:
559
		status = sci_port_start_io(iport, idev, ireq);
560 561 562
		if (status != SCI_SUCCESS)
			return status;

563
		status = sci_remote_node_context_start_io(&idev->rnc, ireq);
564 565 566
		if (status != SCI_SUCCESS)
			break;

567
		status = sci_request_start(ireq);
568 569 570
		if (status != SCI_SUCCESS)
			break;

571 572
		idev->working_request = ireq;
		sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
573
		break;
E
Edmund Nadolski 已提交
574 575
	case SCI_STP_DEV_CMD:
	case SCI_SMP_DEV_CMD:
576 577 578 579 580 581
		/* device is already handling a command it can not accept new commands
		 * until this one is complete.
		 */
		return SCI_FAILURE_INVALID_STATE;
	}

582
	sci_remote_device_start_request(idev, ireq, status);
583
	return status;
584 585
}

586
static enum sci_status common_complete_io(struct isci_port *iport,
587
					  struct isci_remote_device *idev,
588
					  struct isci_request *ireq)
589
{
590 591
	enum sci_status status;

592
	status = sci_request_complete(ireq);
593 594 595
	if (status != SCI_SUCCESS)
		return status;

596
	status = sci_port_complete_io(iport, idev, ireq);
597 598 599
	if (status != SCI_SUCCESS)
		return status;

600
	sci_remote_device_decrement_request_count(idev);
601 602 603
	return status;
}

604
enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
605
						   struct isci_remote_device *idev,
606
						   struct isci_request *ireq)
607
{
608
	struct sci_base_state_machine *sm = &idev->sm;
609
	enum sci_remote_device_states state = sm->current_state_id;
610
	struct isci_port *iport = idev->owning_port;
611 612 613
	enum sci_status status;

	switch (state) {
E
Edmund Nadolski 已提交
614 615 616 617 618 619 620
	case SCI_DEV_INITIAL:
	case SCI_DEV_STOPPED:
	case SCI_DEV_STARTING:
	case SCI_STP_DEV_IDLE:
	case SCI_SMP_DEV_IDLE:
	case SCI_DEV_FAILED:
	case SCI_DEV_FINAL:
621
	default:
622 623
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
624
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
625 626 627
	case SCI_DEV_READY:
	case SCI_STP_DEV_AWAIT_RESET:
	case SCI_DEV_RESETTING:
628
		status = common_complete_io(iport, idev, ireq);
629
		break;
E
Edmund Nadolski 已提交
630 631 632
	case SCI_STP_DEV_CMD:
	case SCI_STP_DEV_NCQ:
	case SCI_STP_DEV_NCQ_ERROR:
D
Dan Williams 已提交
633
	case SCI_STP_DEV_ATAPI_ERROR:
634
		status = common_complete_io(iport, idev, ireq);
635 636 637
		if (status != SCI_SUCCESS)
			break;

638
		if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
639 640 641 642 643
			/* This request causes hardware error, device needs to be Lun Reset.
			 * So here we force the state machine to IDLE state so the rest IOs
			 * can reach RNC state handler, these IOs will be completed by RNC with
			 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
			 */
E
Edmund Nadolski 已提交
644
			sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
D
Dan Williams 已提交
645
		} else if (idev->started_request_count == 0)
E
Edmund Nadolski 已提交
646
			sci_change_state(sm, SCI_STP_DEV_IDLE);
647
		break;
E
Edmund Nadolski 已提交
648
	case SCI_SMP_DEV_CMD:
649
		status = common_complete_io(iport, idev, ireq);
650 651
		if (status != SCI_SUCCESS)
			break;
E
Edmund Nadolski 已提交
652
		sci_change_state(sm, SCI_SMP_DEV_IDLE);
653
		break;
E
Edmund Nadolski 已提交
654
	case SCI_DEV_STOPPING:
655
		status = common_complete_io(iport, idev, ireq);
656 657 658
		if (status != SCI_SUCCESS)
			break;

D
Dan Williams 已提交
659
		if (idev->started_request_count == 0)
660
			sci_remote_node_context_destruct(&idev->rnc,
D
Dan Williams 已提交
661 662
							 rnc_destruct_done,
							 idev);
663 664 665 666
		break;
	}

	if (status != SCI_SUCCESS)
667
		dev_err(scirdev_to_dev(idev),
668
			"%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
669
			"could not complete\n", __func__, iport,
670
			idev, ireq, status);
671
	else
672
		isci_put_device(idev);
673 674

	return status;
675 676
}

677
static void sci_remote_device_continue_request(void *dev)
678
{
679
	struct isci_remote_device *idev = dev;
680 681

	/* we need to check if this request is still valid to continue. */
682
	if (idev->working_request)
683
		sci_controller_continue_io(idev->working_request);
684 685
}

686
enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
687
						  struct isci_remote_device *idev,
688
						  struct isci_request *ireq)
689
{
690
	struct sci_base_state_machine *sm = &idev->sm;
691
	enum sci_remote_device_states state = sm->current_state_id;
692
	struct isci_port *iport = idev->owning_port;
693 694 695
	enum sci_status status;

	switch (state) {
E
Edmund Nadolski 已提交
696 697 698 699 700 701 702 703 704
	case SCI_DEV_INITIAL:
	case SCI_DEV_STOPPED:
	case SCI_DEV_STARTING:
	case SCI_SMP_DEV_IDLE:
	case SCI_SMP_DEV_CMD:
	case SCI_DEV_STOPPING:
	case SCI_DEV_FAILED:
	case SCI_DEV_RESETTING:
	case SCI_DEV_FINAL:
705
	default:
706 707
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
708
		return SCI_FAILURE_INVALID_STATE;
E
Edmund Nadolski 已提交
709 710 711 712 713
	case SCI_STP_DEV_IDLE:
	case SCI_STP_DEV_CMD:
	case SCI_STP_DEV_NCQ:
	case SCI_STP_DEV_NCQ_ERROR:
	case SCI_STP_DEV_AWAIT_RESET:
714
		status = sci_port_start_io(iport, idev, ireq);
715 716 717
		if (status != SCI_SUCCESS)
			return status;

718
		status = sci_remote_node_context_start_task(&idev->rnc, ireq);
719 720 721
		if (status != SCI_SUCCESS)
			goto out;

722
		status = sci_request_start(ireq);
723 724 725 726 727 728 729
		if (status != SCI_SUCCESS)
			goto out;

		/* Note: If the remote device state is not IDLE this will
		 * replace the request that probably resulted in the task
		 * management request.
		 */
730
		idev->working_request = ireq;
E
Edmund Nadolski 已提交
731
		sci_change_state(sm, SCI_STP_DEV_CMD);
732 733 734 735 736 737 738 739

		/* The remote node context must cleanup the TCi to NCQ mapping
		 * table.  The only way to do this correctly is to either write
		 * to the TLCR register or to invalidate and repost the RNC. In
		 * either case the remote node context state machine will take
		 * the correct action when the remote node context is suspended
		 * and later resumed.
		 */
740 741 742 743 744
		sci_remote_node_context_suspend(
			&idev->rnc, SCI_SOFTWARE_SUSPENSION,
			SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT, NULL, NULL);
		sci_remote_node_context_resume(
			&idev->rnc, sci_remote_device_continue_request, idev);
745 746

	out:
747
		sci_remote_device_start_request(idev, ireq, status);
748 749 750 751 752
		/* We need to let the controller start request handler know that
		 * it can't post TC yet. We will provide a callback function to
		 * post TC when RNC gets resumed.
		 */
		return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
E
Edmund Nadolski 已提交
753
	case SCI_DEV_READY:
754
		status = sci_port_start_io(iport, idev, ireq);
755 756 757
		if (status != SCI_SUCCESS)
			return status;

758
		status = sci_remote_node_context_start_task(&idev->rnc, ireq);
759 760 761
		if (status != SCI_SUCCESS)
			break;

762
		status = sci_request_start(ireq);
763 764
		break;
	}
765
	sci_remote_device_start_request(idev, ireq, status);
766 767

	return status;
768 769
}

D
Dan Williams 已提交
770
void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
771
{
D
Dan Williams 已提交
772
	struct isci_port *iport = idev->owning_port;
773 774
	u32 context;

D
Dan Williams 已提交
775 776 777 778
	context = request |
		  (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
		  (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
		  idev->rnc.remote_node_index;
779

D
Dan Williams 已提交
780
	sci_controller_post_request(iport->owning_controller, context);
781 782
}

783
/* called once the remote node context has transisitioned to a
784
 * ready state.  This is the indication that the remote device object can also
785
 * transition to ready.
786
 */
787
static void remote_device_resume_done(void *_dev)
788
{
789
	struct isci_remote_device *idev = _dev;
790

791
	if (is_remote_device_ready(idev))
792
		return;
793

794
	/* go 'ready' if we are not already in a ready state */
795
	sci_change_state(&idev->sm, SCI_DEV_READY);
796 797
}

798
static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
799
{
800
	struct isci_remote_device *idev = _dev;
801
	struct isci_host *ihost = idev->owning_port->owning_controller;
802 803 804 805

	/* For NCQ operation we do not issue a isci_remote_device_not_ready().
	 * As a result, avoid sending the ready notification.
	 */
806
	if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
807
		isci_remote_device_ready(ihost, idev);
808 809
}

810
static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
811
{
812
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
813

814
	/* Initial state is a transitional state to the stopped state */
815
	sci_change_state(&idev->sm, SCI_DEV_STOPPED);
816
}
817

818
/**
819
 * sci_remote_device_destruct() - free remote node context and destruct
820 821 822 823 824 825 826 827 828 829 830
 * @remote_device: This parameter specifies the remote device to be destructed.
 *
 * Remote device objects are a limited resource.  As such, they must be
 * protected.  Thus calls to construct and destruct are mutually exclusive and
 * non-reentrant. The return value shall indicate if the device was
 * successfully destructed or if some failure occurred. enum sci_status This value
 * is returned if the device is successfully destructed.
 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
 * device isn't valid (e.g. it's already been destoryed, the handle isn't
 * valid, etc.).
 */
831
static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
832
{
833
	struct sci_base_state_machine *sm = &idev->sm;
834
	enum sci_remote_device_states state = sm->current_state_id;
835
	struct isci_host *ihost;
836

E
Edmund Nadolski 已提交
837
	if (state != SCI_DEV_STOPPED) {
838 839
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
840 841 842
		return SCI_FAILURE_INVALID_STATE;
	}

843
	ihost = idev->owning_port->owning_controller;
844
	sci_controller_free_remote_node_context(ihost, idev,
845 846
						     idev->rnc.remote_node_index);
	idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
E
Edmund Nadolski 已提交
847
	sci_change_state(sm, SCI_DEV_FINAL);
848 849

	return SCI_SUCCESS;
850
}
851 852 853

/**
 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
D
Dan Williams 已提交
854 855
 * @ihost: This parameter specifies the isci host object.
 * @idev: This parameter specifies the remote device to be freed.
856 857
 *
 */
D
Dan Williams 已提交
858
static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
859
{
D
Dan Williams 已提交
860 861
	dev_dbg(&ihost->pdev->dev,
		"%s: isci_device = %p\n", __func__, idev);
862 863 864 865 866

	/* There should not be any outstanding io's. All paths to
	 * here should go through isci_remote_device_nuke_requests.
	 * If we hit this condition, we will need a way to complete
	 * io requests in process */
867
	BUG_ON(!list_empty(&idev->reqs_in_process));
868

869
	sci_remote_device_destruct(idev);
D
Dan Williams 已提交
870
	list_del_init(&idev->node);
871
	isci_put_device(idev);
872 873
}

874
static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
875
{
876
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
877
	struct isci_host *ihost = idev->owning_port->owning_controller;
878 879 880 881 882
	u32 prev_state;

	/* If we are entering from the stopping state let the SCI User know that
	 * the stop operation has completed.
	 */
883
	prev_state = idev->sm.previous_state_id;
E
Edmund Nadolski 已提交
884
	if (prev_state == SCI_DEV_STOPPING)
885
		isci_remote_device_deconstruct(ihost, idev);
886

887
	sci_controller_remote_device_stopped(ihost, idev);
888 889
}

890
static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
891
{
892
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
D
Dan Williams 已提交
893
	struct isci_host *ihost = idev->owning_port->owning_controller;
894 895 896 897 898

	isci_remote_device_not_ready(ihost, idev,
				     SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
}

899
static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
900
{
901
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
902
	struct isci_host *ihost = idev->owning_port->owning_controller;
903
	struct domain_device *dev = idev->domain_dev;
904

905
	if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
906
		sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
907
	} else if (dev_is_expander(dev)) {
908
		sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
909
	} else
910
		isci_remote_device_ready(ihost, idev);
911 912
}

913
static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
914
{
915 916
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
	struct domain_device *dev = idev->domain_dev;
917 918

	if (dev->dev_type == SAS_END_DEV) {
919
		struct isci_host *ihost = idev->owning_port->owning_controller;
920

921
		isci_remote_device_not_ready(ihost, idev,
922 923 924 925
					     SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
	}
}

926
static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
927
{
928
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
929 930 931 932
	struct isci_host *ihost = idev->owning_port->owning_controller;

	dev_dbg(&ihost->pdev->dev,
		"%s: isci_device = %p\n", __func__, idev);
933

934
	sci_remote_node_context_suspend(
935 936
		&idev->rnc, SCI_SOFTWARE_SUSPENSION,
		SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT, NULL, NULL);
937 938
}

939
static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
940
{
941
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
942 943 944 945
	struct isci_host *ihost = idev->owning_port->owning_controller;

	dev_dbg(&ihost->pdev->dev,
		"%s: isci_device = %p\n", __func__, idev);
946

947
	sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
948 949
}

950
static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
951
{
952
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
953

954
	idev->working_request = NULL;
955
	if (sci_remote_node_context_is_ready(&idev->rnc)) {
956 957 958
		/*
		 * Since the RNC is ready, it's alright to finish completion
		 * processing (e.g. signal the remote device is ready). */
959
		sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
960
	} else {
961 962
		sci_remote_node_context_resume(&idev->rnc,
			sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
963
			idev);
964 965 966
	}
}

967
static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
968
{
969
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
D
Dan Williams 已提交
970
	struct isci_host *ihost = idev->owning_port->owning_controller;
971

972
	BUG_ON(idev->working_request == NULL);
973

974
	isci_remote_device_not_ready(ihost, idev,
975 976 977
				     SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
}

978
static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
979
{
980
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
D
Dan Williams 已提交
981
	struct isci_host *ihost = idev->owning_port->owning_controller;
982

983
	if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
984
		isci_remote_device_not_ready(ihost, idev,
985
					     idev->not_ready_reason);
986 987
}

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
static void sci_stp_remote_device_atapi_error_substate_enter(
	struct sci_base_state_machine *sm)
{
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);

	/* This state is entered when an I/O is decoded with an error
	 * condition.  By this point the RNC expected suspension state is set.
	 * The error conditions suspend the device, so unsuspend here if
	 * possible.
	 */
	sci_remote_node_context_resume(&idev->rnc,
				       atapi_remote_device_resume_done,
				       idev);
}

1003
static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
1004
{
1005
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
D
Dan Williams 已提交
1006
	struct isci_host *ihost = idev->owning_port->owning_controller;
1007

1008
	isci_remote_device_ready(ihost, idev);
1009 1010
}

1011
static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
1012
{
1013
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
D
Dan Williams 已提交
1014
	struct isci_host *ihost = idev->owning_port->owning_controller;
1015

1016
	BUG_ON(idev->working_request == NULL);
1017

1018
	isci_remote_device_not_ready(ihost, idev,
1019 1020 1021
				     SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
}

1022
static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
1023
{
1024
	struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1025

1026
	idev->working_request = NULL;
1027
}
1028

1029
static const struct sci_base_state sci_remote_device_state_table[] = {
E
Edmund Nadolski 已提交
1030
	[SCI_DEV_INITIAL] = {
1031
		.enter_state = sci_remote_device_initial_state_enter,
1032
	},
E
Edmund Nadolski 已提交
1033
	[SCI_DEV_STOPPED] = {
1034
		.enter_state = sci_remote_device_stopped_state_enter,
1035
	},
E
Edmund Nadolski 已提交
1036
	[SCI_DEV_STARTING] = {
1037
		.enter_state = sci_remote_device_starting_state_enter,
1038
	},
E
Edmund Nadolski 已提交
1039
	[SCI_DEV_READY] = {
1040 1041
		.enter_state = sci_remote_device_ready_state_enter,
		.exit_state  = sci_remote_device_ready_state_exit
1042
	},
E
Edmund Nadolski 已提交
1043
	[SCI_STP_DEV_IDLE] = {
1044
		.enter_state = sci_stp_remote_device_ready_idle_substate_enter,
1045
	},
E
Edmund Nadolski 已提交
1046
	[SCI_STP_DEV_CMD] = {
1047
		.enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
1048
	},
E
Edmund Nadolski 已提交
1049 1050
	[SCI_STP_DEV_NCQ] = { },
	[SCI_STP_DEV_NCQ_ERROR] = {
1051
		.enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
1052
	},
1053 1054 1055
	[SCI_STP_DEV_ATAPI_ERROR] = {
		.enter_state = sci_stp_remote_device_atapi_error_substate_enter,
	},
E
Edmund Nadolski 已提交
1056 1057
	[SCI_STP_DEV_AWAIT_RESET] = { },
	[SCI_SMP_DEV_IDLE] = {
1058
		.enter_state = sci_smp_remote_device_ready_idle_substate_enter,
1059
	},
E
Edmund Nadolski 已提交
1060
	[SCI_SMP_DEV_CMD] = {
1061 1062
		.enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
		.exit_state  = sci_smp_remote_device_ready_cmd_substate_exit,
1063
	},
E
Edmund Nadolski 已提交
1064 1065 1066
	[SCI_DEV_STOPPING] = { },
	[SCI_DEV_FAILED] = { },
	[SCI_DEV_RESETTING] = {
1067 1068
		.enter_state = sci_remote_device_resetting_state_enter,
		.exit_state  = sci_remote_device_resetting_state_exit
1069
	},
E
Edmund Nadolski 已提交
1070
	[SCI_DEV_FINAL] = { },
1071 1072 1073
};

/**
1074
 * sci_remote_device_construct() - common construction
1075 1076 1077
 * @sci_port: SAS/SATA port through which this device is accessed.
 * @sci_dev: remote device to construct
 *
1078 1079
 * This routine just performs benign initialization and does not
 * allocate the remote_node_context which is left to
1080
 * sci_remote_device_[de]a_construct().  sci_remote_device_destruct()
1081
 * frees the remote_node_context(s) for the device.
1082
 */
1083
static void sci_remote_device_construct(struct isci_port *iport,
1084
				  struct isci_remote_device *idev)
1085
{
1086 1087
	idev->owning_port = iport;
	idev->started_request_count = 0;
1088

1089
	sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
1090

1091
	sci_remote_node_context_construct(&idev->rnc,
1092 1093 1094 1095
					       SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
}

/**
1096
 * sci_remote_device_da_construct() - construct direct attached device.
1097 1098 1099
 *
 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
 * the device is known to the SCI Core since it is contained in the
1100 1101
 * sci_phy object.  Remote node context(s) is/are a global resource
 * allocated by this routine, freed by sci_remote_device_destruct().
1102 1103 1104 1105 1106 1107
 *
 * Returns:
 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
 * sata-only controller instance.
 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1108
 */
1109
static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
1110
						       struct isci_remote_device *idev)
1111 1112
{
	enum sci_status status;
1113
	struct sci_port_properties properties;
1114

1115
	sci_remote_device_construct(iport, idev);
1116

1117 1118 1119 1120
	sci_port_get_properties(iport, &properties);
	/* Get accurate port width from port's phy mask for a DA device. */
	idev->device_port_width = hweight32(properties.phy_mask);

1121
	status = sci_controller_allocate_remote_node_context(iport->owning_controller,
D
Dan Williams 已提交
1122 1123
							     idev,
							     &idev->rnc.remote_node_index);
1124

1125 1126
	if (status != SCI_SUCCESS)
		return status;
1127

1128
	idev->connection_rate = sci_port_get_max_allowed_speed(iport);
1129

1130
	return SCI_SUCCESS;
1131 1132 1133
}

/**
1134
 * sci_remote_device_ea_construct() - construct expander attached device
1135 1136
 *
 * Remote node context(s) is/are a global resource allocated by this
1137
 * routine, freed by sci_remote_device_destruct().
1138 1139 1140 1141 1142 1143
 *
 * Returns:
 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
 * sata-only controller instance.
 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1144
 */
1145
static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
1146
						       struct isci_remote_device *idev)
1147
{
1148
	struct domain_device *dev = idev->domain_dev;
1149 1150
	enum sci_status status;

1151
	sci_remote_device_construct(iport, idev);
1152

1153
	status = sci_controller_allocate_remote_node_context(iport->owning_controller,
1154 1155
								  idev,
								  &idev->rnc.remote_node_index);
1156 1157
	if (status != SCI_SUCCESS)
		return status;
1158

D
Dan Williams 已提交
1159
	/* For SAS-2 the physical link rate is actually a logical link
1160 1161 1162 1163
	 * rate that incorporates multiplexing.  The SCU doesn't
	 * incorporate multiplexing and for the purposes of the
	 * connection the logical link rate is that same as the
	 * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
D
Dan Williams 已提交
1164 1165
	 * one another, so this code works for both situations.
	 */
1166
	idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
D
Dan Williams 已提交
1167
					 dev->linkrate);
1168

1169
	/* / @todo Should I assign the port width by reading all of the phys on the port? */
1170
	idev->device_port_width = 1;
1171

1172
	return SCI_SUCCESS;
1173 1174 1175
}

/**
1176
 * sci_remote_device_start() - This method will start the supplied remote
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
 *    device.  This method enables normal IO requests to flow through to the
 *    remote device.
 * @remote_device: This parameter specifies the device to be started.
 * @timeout: This parameter specifies the number of milliseconds in which the
 *    start operation should complete.
 *
 * An indication of whether the device was successfully started. SCI_SUCCESS
 * This value is returned if the device was successfully started.
 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
 * the device when there have been no phys added to it.
 */
1188
static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
1189
						u32 timeout)
1190
{
1191
	struct sci_base_state_machine *sm = &idev->sm;
1192
	enum sci_remote_device_states state = sm->current_state_id;
1193 1194
	enum sci_status status;

E
Edmund Nadolski 已提交
1195
	if (state != SCI_DEV_STOPPED) {
1196 1197
		dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
			 __func__, dev_state_name(state));
1198 1199 1200
		return SCI_FAILURE_INVALID_STATE;
	}

1201
	status = sci_remote_node_context_resume(&idev->rnc,
1202
						     remote_device_resume_done,
1203
						     idev);
1204 1205 1206
	if (status != SCI_SUCCESS)
		return status;

E
Edmund Nadolski 已提交
1207
	sci_change_state(sm, SCI_DEV_STARTING);
1208 1209

	return SCI_SUCCESS;
1210
}
1211

D
Dan Williams 已提交
1212 1213
static enum sci_status isci_remote_device_construct(struct isci_port *iport,
						    struct isci_remote_device *idev)
1214
{
D
Dan Williams 已提交
1215 1216 1217
	struct isci_host *ihost = iport->isci_host;
	struct domain_device *dev = idev->domain_dev;
	enum sci_status status;
1218

D
Dan Williams 已提交
1219
	if (dev->parent && dev_is_expander(dev->parent))
1220
		status = sci_remote_device_ea_construct(iport, idev);
D
Dan Williams 已提交
1221
	else
1222
		status = sci_remote_device_da_construct(iport, idev);
1223 1224

	if (status != SCI_SUCCESS) {
D
Dan Williams 已提交
1225 1226
		dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
			__func__, status);
1227 1228 1229 1230 1231

		return status;
	}

	/* start the device. */
1232
	status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1233

D
Dan Williams 已提交
1234 1235 1236
	if (status != SCI_SUCCESS)
		dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
			 status);
1237 1238 1239 1240

	return status;
}

1241
void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1242 1243 1244
{
	DECLARE_COMPLETION_ONSTACK(aborted_task_completion);

1245 1246
	dev_dbg(&ihost->pdev->dev,
		"%s: idev = %p\n", __func__, idev);
1247 1248

	/* Cleanup all requests pending for this device. */
1249
	isci_terminate_pending_requests(ihost, idev);
1250

1251 1252
	dev_dbg(&ihost->pdev->dev,
		"%s: idev = %p, done\n", __func__, idev);
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
}

/**
 * This function builds the isci_remote_device when a libsas dev_found message
 *    is received.
 * @isci_host: This parameter specifies the isci host object.
 * @port: This parameter specifies the isci_port conected to this device.
 *
 * pointer to new isci_remote_device.
 */
static struct isci_remote_device *
D
Dan Williams 已提交
1264
isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1265
{
D
Dan Williams 已提交
1266 1267
	struct isci_remote_device *idev;
	int i;
1268

D
Dan Williams 已提交
1269
	for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1270
		idev = &ihost->devices[i];
D
Dan Williams 已提交
1271 1272 1273
		if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
			break;
	}
1274

D
Dan Williams 已提交
1275 1276
	if (i >= SCI_MAX_REMOTE_DEVICES) {
		dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1277 1278 1279
		return NULL;
	}

1280 1281 1282 1283 1284 1285
	if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
		return NULL;

	if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
		return NULL;

D
Dan Williams 已提交
1286
	return idev;
1287 1288
}

1289 1290 1291 1292 1293 1294 1295 1296 1297
void isci_remote_device_release(struct kref *kref)
{
	struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
	struct isci_host *ihost = idev->isci_port->isci_host;

	idev->domain_dev = NULL;
	idev->isci_port = NULL;
	clear_bit(IDEV_START_PENDING, &idev->flags);
	clear_bit(IDEV_STOP_PENDING, &idev->flags);
1298
	clear_bit(IDEV_IO_READY, &idev->flags);
1299 1300 1301 1302 1303 1304
	clear_bit(IDEV_GONE, &idev->flags);
	smp_mb__before_clear_bit();
	clear_bit(IDEV_ALLOCATED, &idev->flags);
	wake_up(&ihost->eventq);
}

1305 1306 1307 1308 1309 1310
/**
 * isci_remote_device_stop() - This function is called internally to stop the
 *    remote device.
 * @isci_host: This parameter specifies the isci host object.
 * @isci_device: This parameter specifies the remote device.
 *
1311
 * The status of the ihost request to stop.
1312
 */
1313
enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1314 1315 1316 1317
{
	enum sci_status status;
	unsigned long flags;

1318 1319
	dev_dbg(&ihost->pdev->dev,
		"%s: isci_device = %p\n", __func__, idev);
1320

1321 1322 1323 1324
	spin_lock_irqsave(&ihost->scic_lock, flags);
	idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
	set_bit(IDEV_GONE, &idev->flags);
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1325 1326

	/* Kill all outstanding requests. */
1327
	isci_remote_device_nuke_requests(ihost, idev);
1328

1329
	set_bit(IDEV_STOP_PENDING, &idev->flags);
1330

1331
	spin_lock_irqsave(&ihost->scic_lock, flags);
1332
	status = sci_remote_device_stop(idev, 50);
1333
	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1334 1335

	/* Wait for the stop complete callback. */
1336 1337 1338
	if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
		/* nothing to wait for */;
	else
1339
		wait_for_device_stop(ihost, idev);
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

	return status;
}

/**
 * isci_remote_device_gone() - This function is called by libsas when a domain
 *    device is removed.
 * @domain_device: This parameter specifies the libsas domain device.
 *
 */
1350
void isci_remote_device_gone(struct domain_device *dev)
1351
{
1352
	struct isci_host *ihost = dev_to_ihost(dev);
1353
	struct isci_remote_device *idev = dev->lldd_dev;
1354

1355
	dev_dbg(&ihost->pdev->dev,
1356
		"%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1357
		__func__, dev, idev, idev->isci_port);
1358

1359
	isci_remote_device_stop(ihost, idev);
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
}


/**
 * isci_remote_device_found() - This function is called by libsas when a remote
 *    device is discovered. A remote device object is created and started. the
 *    function then sleeps until the sci core device started message is
 *    received.
 * @domain_device: This parameter specifies the libsas domain device.
 *
 * status, zero indicates success.
 */
1372
int isci_remote_device_found(struct domain_device *dev)
1373
{
1374 1375
	struct isci_host *isci_host = dev_to_ihost(dev);
	struct isci_port *isci_port = dev->port->lldd_port;
1376 1377 1378 1379
	struct isci_remote_device *isci_device;
	enum sci_status status;

	dev_dbg(&isci_host->pdev->dev,
1380
		"%s: domain_device = %p\n", __func__, dev);
1381

1382 1383
	if (!isci_port)
		return -ENODEV;
1384 1385

	isci_device = isci_remote_device_alloc(isci_host, isci_port);
D
Dan Williams 已提交
1386 1387
	if (!isci_device)
		return -ENODEV;
1388

1389
	kref_init(&isci_device->kref);
1390
	INIT_LIST_HEAD(&isci_device->node);
1391 1392

	spin_lock_irq(&isci_host->scic_lock);
1393
	isci_device->domain_dev = dev;
1394 1395 1396
	isci_device->isci_port = isci_port;
	list_add_tail(&isci_device->node, &isci_port->remote_dev_list);

1397
	set_bit(IDEV_START_PENDING, &isci_device->flags);
1398 1399 1400 1401 1402 1403
	status = isci_remote_device_construct(isci_port, isci_device);

	dev_dbg(&isci_host->pdev->dev,
		"%s: isci_device = %p\n",
		__func__, isci_device);

1404 1405
	if (status == SCI_SUCCESS) {
		/* device came up, advertise it to the world */
1406
		dev->lldd_dev = isci_device;
1407 1408 1409
	} else
		isci_put_device(isci_device);
	spin_unlock_irq(&isci_host->scic_lock);
1410

1411 1412 1413
	/* wait for the device ready callback. */
	wait_for_device_start(isci_host, isci_device);

1414
	return status == SCI_SUCCESS ? 0 : -ENODEV;
1415
}