xref: /linux/drivers/scsi/isci/remote_device.c (revision 88f3b62ac131e2549b6c262cacbd47e8cca42d6e)
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
33  *   * Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in
37  *     the documentation and/or other materials provided with the
38  *     distribution.
39  *   * Neither the name of Intel Corporation nor the names of its
40  *     contributors may be used to endorse or promote products derived
41  *     from this software without specific prior written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55 #include "intel_sas.h"
56 #include "isci.h"
57 #include "port.h"
58 #include "remote_device.h"
59 #include "request.h"
60 #include "scic_controller.h"
61 #include "scic_io_request.h"
62 #include "scic_phy.h"
63 #include "scic_port.h"
64 #include "scic_sds_controller.h"
65 #include "scic_sds_phy.h"
66 #include "scic_sds_port.h"
67 #include "remote_node_context.h"
68 #include "scic_sds_request.h"
69 #include "sci_environment.h"
70 #include "sci_util.h"
71 #include "scu_event_codes.h"
72 #include "task.h"
73 
74 enum sci_status scic_remote_device_stop(
75 	struct scic_sds_remote_device *sci_dev,
76 	u32 timeout)
77 {
78 	return sci_dev->state_handlers->stop_handler(sci_dev);
79 }
80 
81 
82 enum sci_status scic_remote_device_reset(
83 	struct scic_sds_remote_device *sci_dev)
84 {
85 	return sci_dev->state_handlers->reset_handler(sci_dev);
86 }
87 
88 
89 enum sci_status scic_remote_device_reset_complete(
90 	struct scic_sds_remote_device *sci_dev)
91 {
92 	return sci_dev->state_handlers->reset_complete_handler(sci_dev);
93 }
94 
95 
96 enum sas_linkrate scic_remote_device_get_connection_rate(
97 	struct scic_sds_remote_device *sci_dev)
98 {
99 	return sci_dev->connection_rate;
100 }
101 
102 
103 void scic_remote_device_get_protocols(
104 	struct scic_sds_remote_device *sci_dev,
105 	struct smp_discover_response_protocols *pr)
106 {
107 	pr->u.all = sci_dev->target_protocols.u.all;
108 }
109 
110 #if !defined(DISABLE_ATAPI)
111 bool scic_remote_device_is_atapi(struct scic_sds_remote_device *sci_dev)
112 {
113 	return sci_dev->is_atapi;
114 }
115 #endif
116 
117 
118 /**
119  *
120  *
121  * Remote device timer requirements
122  */
123 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
124 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
125 
126 
127 /**
128  *
129  * @sci_dev: The remote device for which the suspend is being requested.
130  *
131  * This method invokes the remote device suspend state handler. enum sci_status
132  */
133 enum sci_status scic_sds_remote_device_suspend(
134 	struct scic_sds_remote_device *sci_dev,
135 	u32 suspend_type)
136 {
137 	return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
138 }
139 
140 /**
141  *
142  * @sci_dev: The remote device for which the event handling is being
143  *    requested.
144  * @frame_index: This is the frame index that is being processed.
145  *
146  * This method invokes the frame handler for the remote device state machine
147  * enum sci_status
148  */
149 enum sci_status scic_sds_remote_device_frame_handler(
150 	struct scic_sds_remote_device *sci_dev,
151 	u32 frame_index)
152 {
153 	return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
154 }
155 
156 /**
157  *
158  * @sci_dev: The remote device for which the event handling is being
159  *    requested.
160  * @event_code: This is the event code that is to be processed.
161  *
162  * This method invokes the remote device event handler. enum sci_status
163  */
164 enum sci_status scic_sds_remote_device_event_handler(
165 	struct scic_sds_remote_device *sci_dev,
166 	u32 event_code)
167 {
168 	return sci_dev->state_handlers->event_handler(sci_dev, event_code);
169 }
170 
171 /**
172  *
173  * @controller: The controller that is starting the io request.
174  * @sci_dev: The remote device for which the start io handling is being
175  *    requested.
176  * @io_request: The io request that is being started.
177  *
178  * This method invokes the remote device start io handler. enum sci_status
179  */
180 enum sci_status scic_sds_remote_device_start_io(
181 	struct scic_sds_controller *controller,
182 	struct scic_sds_remote_device *sci_dev,
183 	struct scic_sds_request *io_request)
184 {
185 	return sci_dev->state_handlers->start_io_handler(
186 		       sci_dev, io_request);
187 }
188 
189 /**
190  *
191  * @controller: The controller that is completing the io request.
192  * @sci_dev: The remote device for which the complete io handling is being
193  *    requested.
194  * @io_request: The io request that is being completed.
195  *
196  * This method invokes the remote device complete io handler. enum sci_status
197  */
198 enum sci_status scic_sds_remote_device_complete_io(
199 	struct scic_sds_controller *controller,
200 	struct scic_sds_remote_device *sci_dev,
201 	struct scic_sds_request *io_request)
202 {
203 	return sci_dev->state_handlers->complete_io_handler(
204 		       sci_dev, io_request);
205 }
206 
207 /**
208  *
209  * @controller: The controller that is starting the task request.
210  * @sci_dev: The remote device for which the start task handling is being
211  *    requested.
212  * @io_request: The task request that is being started.
213  *
214  * This method invokes the remote device start task handler. enum sci_status
215  */
216 enum sci_status scic_sds_remote_device_start_task(
217 	struct scic_sds_controller *controller,
218 	struct scic_sds_remote_device *sci_dev,
219 	struct scic_sds_request *io_request)
220 {
221 	return sci_dev->state_handlers->start_task_handler(
222 		       sci_dev, io_request);
223 }
224 
225 /**
226  *
227  * @controller: The controller that is completing the task request.
228  * @sci_dev: The remote device for which the complete task handling is
229  *    being requested.
230  * @io_request: The task request that is being completed.
231  *
232  * This method invokes the remote device complete task handler. enum sci_status
233  */
234 
235 /**
236  *
237  * @sci_dev:
238  * @request:
239  *
240  * This method takes the request and bulids an appropriate SCU context for the
241  * request and then requests the controller to post the request. none
242  */
243 void scic_sds_remote_device_post_request(
244 	struct scic_sds_remote_device *sci_dev,
245 	u32 request)
246 {
247 	u32 context;
248 
249 	context = scic_sds_remote_device_build_command_context(sci_dev, request);
250 
251 	scic_sds_controller_post_request(
252 		scic_sds_remote_device_get_controller(sci_dev),
253 		context
254 		);
255 }
256 
257 #if !defined(DISABLE_ATAPI)
258 /**
259  *
260  * @sci_dev: The device to be checked.
261  *
262  * This method check the signature fis of a stp device to decide whether a
263  * device is atapi or not. true if a device is atapi device. False if a device
264  * is not atapi.
265  */
266 bool scic_sds_remote_device_is_atapi(
267 	struct scic_sds_remote_device *sci_dev)
268 {
269 	if (!sci_dev->target_protocols.u.bits.attached_stp_target)
270 		return false;
271 	else if (sci_dev->is_direct_attached) {
272 		struct scic_sds_phy *phy;
273 		struct scic_sata_phy_properties properties;
274 		struct sata_fis_reg_d2h *signature_fis;
275 		phy = scic_sds_port_get_a_connected_phy(sci_dev->owning_port);
276 		scic_sata_phy_get_properties(phy, &properties);
277 
278 		/* decode the signature fis. */
279 		signature_fis = &(properties.signature_fis);
280 
281 		if ((signature_fis->sector_count  == 0x01)
282 		    && (signature_fis->lba_low       == 0x01)
283 		    && (signature_fis->lba_mid       == 0x14)
284 		    && (signature_fis->lba_high      == 0xEB)
285 		    && ((signature_fis->device & 0x5F) == 0x00)
286 		    ) {
287 			/* An ATA device supporting the PACKET command set. */
288 			return true;
289 		} else
290 			return false;
291 	} else {
292 		/* Expander supported ATAPI device is not currently supported. */
293 		return false;
294 	}
295 }
296 #endif
297 
298 /**
299  *
300  * @user_parameter: This is cast to a remote device object.
301  *
302  * This method is called once the remote node context is ready to be freed.
303  * The remote device can now report that its stop operation is complete. none
304  */
305 static void scic_sds_cb_remote_device_rnc_destruct_complete(
306 	void *user_parameter)
307 {
308 	struct scic_sds_remote_device *sci_dev;
309 
310 	sci_dev = (struct scic_sds_remote_device *)user_parameter;
311 
312 	BUG_ON(sci_dev->started_request_count != 0);
313 
314 	sci_base_state_machine_change_state(&sci_dev->state_machine,
315 					    SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
316 }
317 
318 /**
319  *
320  * @user_parameter: This is cast to a remote device object.
321  *
322  * This method is called once the remote node context has transisitioned to a
323  * ready state.  This is the indication that the remote device object can also
324  * transition to ready. none
325  */
326 static void scic_sds_remote_device_resume_complete_handler(
327 	void *user_parameter)
328 {
329 	struct scic_sds_remote_device *sci_dev;
330 
331 	sci_dev = (struct scic_sds_remote_device *)user_parameter;
332 
333 	if (
334 		sci_base_state_machine_get_state(&sci_dev->state_machine)
335 		!= SCI_BASE_REMOTE_DEVICE_STATE_READY
336 		) {
337 		sci_base_state_machine_change_state(
338 			&sci_dev->state_machine,
339 			SCI_BASE_REMOTE_DEVICE_STATE_READY
340 			);
341 	}
342 }
343 
344 /**
345  *
346  * @device: This parameter specifies the device for which the request is being
347  *    started.
348  * @request: This parameter specifies the request being started.
349  * @status: This parameter specifies the current start operation status.
350  *
351  * This method will perform the STP request start processing common to IO
352  * requests and task requests of all types. none
353  */
354 void scic_sds_remote_device_start_request(
355 	struct scic_sds_remote_device *sci_dev,
356 	struct scic_sds_request *sci_req,
357 	enum sci_status status)
358 {
359 	/* We still have a fault in starting the io complete it on the port */
360 	if (status == SCI_SUCCESS)
361 		scic_sds_remote_device_increment_request_count(sci_dev);
362 	else{
363 		sci_dev->owning_port->state_handlers->complete_io_handler(
364 			sci_dev->owning_port, sci_dev, sci_req
365 			);
366 	}
367 }
368 
369 
370 /**
371  *
372  * @request: This parameter specifies the request being continued.
373  *
374  * This method will continue to post tc for a STP request. This method usually
375  * serves as a callback when RNC gets resumed during a task management
376  * sequence. none
377  */
378 void scic_sds_remote_device_continue_request(void *dev)
379 {
380 	struct scic_sds_remote_device *sci_dev = dev;
381 
382 	/* we need to check if this request is still valid to continue. */
383 	if (sci_dev->working_request)
384 		scic_controller_continue_io(sci_dev->working_request);
385 }
386 
387 /**
388  * This method will terminate all of the IO requests in the controllers IO
389  *    request table that were targeted for this device.
390  * @sci_dev: This parameter specifies the remote device for which to
391  *    attempt to terminate all requests.
392  *
393  * This method returns an indication as to whether all requests were
394  * successfully terminated.  If a single request fails to be terminated, then
395  * this method will return the failure.
396  */
397 static enum sci_status scic_sds_remote_device_terminate_requests(
398 	struct scic_sds_remote_device *sci_dev)
399 {
400 	enum sci_status status           = SCI_SUCCESS;
401 	enum sci_status terminate_status = SCI_SUCCESS;
402 	struct scic_sds_request *sci_req;
403 	u32 index;
404 	u32 request_count    = sci_dev->started_request_count;
405 
406 	for (index = 0;
407 	     (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
408 	     index++) {
409 		sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
410 
411 		if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
412 			terminate_status = scic_controller_terminate_request(
413 				sci_dev->owning_port->owning_controller,
414 				sci_dev,
415 				sci_req
416 				);
417 
418 			if (terminate_status != SCI_SUCCESS)
419 				status = terminate_status;
420 
421 			request_count--;
422 		}
423 	}
424 
425 	return status;
426 }
427 
428 static enum sci_status
429 default_device_handler(struct scic_sds_remote_device *sci_dev,
430 		       const char *func)
431 {
432 	dev_warn(scirdev_to_dev(sci_dev),
433 		 "%s: in wrong state: %d\n", func,
434 		 sci_base_state_machine_get_state(&sci_dev->state_machine));
435 	return SCI_FAILURE_INVALID_STATE;
436 }
437 
438 enum sci_status scic_sds_remote_device_default_start_handler(
439 	struct scic_sds_remote_device *sci_dev)
440 {
441 	return default_device_handler(sci_dev, __func__);
442 }
443 
444 static enum sci_status scic_sds_remote_device_default_stop_handler(
445 	struct scic_sds_remote_device *sci_dev)
446 {
447 	return default_device_handler(sci_dev, __func__);
448 }
449 
450 enum sci_status scic_sds_remote_device_default_fail_handler(
451 	struct scic_sds_remote_device *sci_dev)
452 {
453 	return default_device_handler(sci_dev, __func__);
454 }
455 
456 enum sci_status scic_sds_remote_device_default_destruct_handler(
457 	struct scic_sds_remote_device *sci_dev)
458 {
459 	return default_device_handler(sci_dev, __func__);
460 }
461 
462 enum sci_status scic_sds_remote_device_default_reset_handler(
463 	struct scic_sds_remote_device *sci_dev)
464 {
465 	return default_device_handler(sci_dev, __func__);
466 }
467 
468 enum sci_status scic_sds_remote_device_default_reset_complete_handler(
469 	struct scic_sds_remote_device *sci_dev)
470 {
471 	return default_device_handler(sci_dev, __func__);
472 }
473 
474 enum sci_status scic_sds_remote_device_default_suspend_handler(
475 	struct scic_sds_remote_device *sci_dev, u32 suspend_type)
476 {
477 	return default_device_handler(sci_dev, __func__);
478 }
479 
480 enum sci_status scic_sds_remote_device_default_resume_handler(
481 	struct scic_sds_remote_device *sci_dev)
482 {
483 	return default_device_handler(sci_dev, __func__);
484 }
485 
486 /**
487  *
488  * @device: The struct scic_sds_remote_device which is then cast into a
489  *    struct scic_sds_remote_device.
490  * @event_code: The event code that the struct scic_sds_controller wants the device
491  *    object to process.
492  *
493  * This method is the default event handler.  It will call the RNC state
494  * machine handler for any RNC events otherwise it will log a warning and
495  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
496  */
497 static enum sci_status  scic_sds_remote_device_core_event_handler(
498 	struct scic_sds_remote_device *sci_dev,
499 	u32 event_code,
500 	bool is_ready_state)
501 {
502 	enum sci_status status;
503 
504 	switch (scu_get_event_type(event_code)) {
505 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
506 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
507 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
508 		status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
509 		break;
510 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
511 
512 		if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
513 			status = SCI_SUCCESS;
514 
515 			/* Suspend the associated RNC */
516 			scic_sds_remote_node_context_suspend(&sci_dev->rnc,
517 							      SCI_SOFTWARE_SUSPENSION,
518 							      NULL, NULL);
519 
520 			dev_dbg(scirdev_to_dev(sci_dev),
521 				"%s: device: %p event code: %x: %s\n",
522 				__func__, sci_dev, event_code,
523 				(is_ready_state)
524 				? "I_T_Nexus_Timeout event"
525 				: "I_T_Nexus_Timeout event in wrong state");
526 
527 			break;
528 		}
529 	/* Else, fall through and treat as unhandled... */
530 
531 	default:
532 		dev_dbg(scirdev_to_dev(sci_dev),
533 			"%s: device: %p event code: %x: %s\n",
534 			__func__, sci_dev, event_code,
535 			(is_ready_state)
536 			? "unexpected event"
537 			: "unexpected event in wrong state");
538 		status = SCI_FAILURE_INVALID_STATE;
539 		break;
540 	}
541 
542 	return status;
543 }
544 /**
545  *
546  * @device: The struct scic_sds_remote_device which is then cast into a
547  *    struct scic_sds_remote_device.
548  * @event_code: The event code that the struct scic_sds_controller wants the device
549  *    object to process.
550  *
551  * This method is the default event handler.  It will call the RNC state
552  * machine handler for any RNC events otherwise it will log a warning and
553  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
554  */
555 static enum sci_status  scic_sds_remote_device_default_event_handler(
556 	struct scic_sds_remote_device *sci_dev,
557 	u32 event_code)
558 {
559 	return scic_sds_remote_device_core_event_handler(sci_dev,
560 							  event_code,
561 							  false);
562 }
563 
564 /**
565  *
566  * @device: The struct scic_sds_remote_device which is then cast into a
567  *    struct scic_sds_remote_device.
568  * @frame_index: The frame index for which the struct scic_sds_controller wants this
569  *    device object to process.
570  *
571  * This method is the default unsolicited frame handler.  It logs a warning,
572  * releases the frame and returns a failure. enum sci_status
573  * SCI_FAILURE_INVALID_STATE
574  */
575 enum sci_status scic_sds_remote_device_default_frame_handler(
576 	struct scic_sds_remote_device *sci_dev,
577 	u32 frame_index)
578 {
579 	dev_warn(scirdev_to_dev(sci_dev),
580 		 "%s: SCIC Remote Device requested to handle frame %x "
581 		 "while in wrong state %d\n",
582 		 __func__,
583 		 frame_index,
584 		 sci_base_state_machine_get_state(
585 			 &sci_dev->state_machine));
586 
587 	/* Return the frame back to the controller */
588 	scic_sds_controller_release_frame(
589 		scic_sds_remote_device_get_controller(sci_dev), frame_index
590 		);
591 
592 	return SCI_FAILURE_INVALID_STATE;
593 }
594 
595 enum sci_status scic_sds_remote_device_default_start_request_handler(
596 	struct scic_sds_remote_device *sci_dev,
597 	struct scic_sds_request *request)
598 {
599 	return default_device_handler(sci_dev, __func__);
600 }
601 
602 enum sci_status scic_sds_remote_device_default_complete_request_handler(
603 	struct scic_sds_remote_device *sci_dev,
604 	struct scic_sds_request *request)
605 {
606 	return default_device_handler(sci_dev, __func__);
607 }
608 
609 enum sci_status scic_sds_remote_device_default_continue_request_handler(
610 	struct scic_sds_remote_device *sci_dev,
611 	struct scic_sds_request *request)
612 {
613 	return default_device_handler(sci_dev, __func__);
614 }
615 
616 /**
617  *
618  * @device: The struct scic_sds_remote_device which is then cast into a
619  *    struct scic_sds_remote_device.
620  * @frame_index: The frame index for which the struct scic_sds_controller wants this
621  *    device object to process.
622  *
623  * This method is a general ssp frame handler.  In most cases the device object
624  * needs to route the unsolicited frame processing to the io request object.
625  * This method decodes the tag for the io request object and routes the
626  * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
627  */
628 enum sci_status scic_sds_remote_device_general_frame_handler(
629 	struct scic_sds_remote_device *sci_dev,
630 	u32 frame_index)
631 {
632 	enum sci_status result;
633 	struct sci_ssp_frame_header *frame_header;
634 	struct scic_sds_request *io_request;
635 
636 	result = scic_sds_unsolicited_frame_control_get_header(
637 		&(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
638 		frame_index,
639 		(void **)&frame_header
640 		);
641 
642 	if (SCI_SUCCESS == result) {
643 		io_request = scic_sds_controller_get_io_request_from_tag(
644 			scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
645 
646 		if ((io_request == NULL)
647 		    || (io_request->target_device != sci_dev)) {
648 			/*
649 			 * We could not map this tag to a valid IO request
650 			 * Just toss the frame and continue */
651 			scic_sds_controller_release_frame(
652 				scic_sds_remote_device_get_controller(sci_dev), frame_index
653 				);
654 		} else {
655 			/* The IO request is now in charge of releasing the frame */
656 			result = io_request->state_handlers->frame_handler(
657 				io_request, frame_index);
658 		}
659 	}
660 
661 	return result;
662 }
663 
664 /**
665  *
666  * @[in]: sci_dev This is the device object that is receiving the event.
667  * @[in]: event_code The event code to process.
668  *
669  * This is a common method for handling events reported to the remote device
670  * from the controller object. enum sci_status
671  */
672 enum sci_status scic_sds_remote_device_general_event_handler(
673 	struct scic_sds_remote_device *sci_dev,
674 	u32 event_code)
675 {
676 	return scic_sds_remote_device_core_event_handler(sci_dev,
677 							  event_code,
678 							  true);
679 }
680 
681 /*
682  * *****************************************************************************
683  * *  STOPPED STATE HANDLERS
684  * ***************************************************************************** */
685 
686 /**
687  *
688  * @device:
689  *
690  * This method takes the struct scic_sds_remote_device from a stopped state and
691  * attempts to start it.   The RNC buffer for the device is constructed and the
692  * device state machine is transitioned to the
693  * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
694  * an RNC buffer available to construct the remote device.
695  * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
696  * which to construct the remote device.
697  */
698 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
699 	struct scic_sds_remote_device *sci_dev)
700 {
701 	enum sci_status status;
702 
703 	status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
704 			scic_sds_remote_device_resume_complete_handler, sci_dev);
705 
706 	if (status == SCI_SUCCESS)
707 		sci_base_state_machine_change_state(&sci_dev->state_machine,
708 						    SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
709 
710 	return status;
711 }
712 
713 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
714 	struct scic_sds_remote_device *sci_dev)
715 {
716 	return SCI_SUCCESS;
717 }
718 
719 /**
720  *
721  * @sci_dev: The struct scic_sds_remote_device which is cast into a
722  *    struct scic_sds_remote_device.
723  *
724  * This method will destruct a struct scic_sds_remote_device that is in a stopped
725  * state.  This is the only state from which a destruct request will succeed.
726  * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
727  * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
728  * enum sci_status SCI_SUCCESS
729  */
730 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
731 	struct scic_sds_remote_device *sci_dev)
732 {
733 	struct scic_sds_controller *scic;
734 
735 	scic = scic_sds_remote_device_get_controller(sci_dev);
736 	scic_sds_controller_free_remote_node_context(scic, sci_dev,
737 						     sci_dev->rnc.remote_node_index);
738 	sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
739 
740 	sci_base_state_machine_change_state(&sci_dev->state_machine,
741 					    SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
742 
743 	return SCI_SUCCESS;
744 }
745 
746 /*
747  * *****************************************************************************
748  * *  STARTING STATE HANDLERS
749  * ***************************************************************************** */
750 
751 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
752 	struct scic_sds_remote_device *sci_dev)
753 {
754 	/*
755 	 * This device has not yet started so there had better be no IO requests
756 	 */
757 	BUG_ON(sci_dev->started_request_count != 0);
758 
759 	/*
760 	 * Destroy the remote node context
761 	 */
762 	scic_sds_remote_node_context_destruct(&sci_dev->rnc,
763 		scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
764 
765 	/*
766 	 * Transition to the stopping state and wait for the remote node to
767 	 * complete being posted and invalidated.
768 	 */
769 	sci_base_state_machine_change_state(&sci_dev->state_machine,
770 					    SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
771 
772 	return SCI_SUCCESS;
773 }
774 
775 enum sci_status scic_sds_remote_device_ready_state_stop_handler(
776 	struct scic_sds_remote_device *sci_dev)
777 {
778 	enum sci_status status = SCI_SUCCESS;
779 
780 	/* Request the parent state machine to transition to the stopping state */
781 	sci_base_state_machine_change_state(&sci_dev->state_machine,
782 					    SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
783 
784 	if (sci_dev->started_request_count == 0) {
785 		scic_sds_remote_node_context_destruct(&sci_dev->rnc,
786 			scic_sds_cb_remote_device_rnc_destruct_complete,
787 			sci_dev);
788 	} else
789 		status = scic_sds_remote_device_terminate_requests(sci_dev);
790 
791 	return status;
792 }
793 
794 /**
795  *
796  * @device: The struct scic_sds_remote_device object which is cast to a
797  *    struct scic_sds_remote_device object.
798  *
799  * This is the ready state device reset handler enum sci_status
800  */
801 enum sci_status scic_sds_remote_device_ready_state_reset_handler(
802 	struct scic_sds_remote_device *sci_dev)
803 {
804 	/* Request the parent state machine to transition to the stopping state */
805 	sci_base_state_machine_change_state(&sci_dev->state_machine,
806 					    SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
807 
808 	return SCI_SUCCESS;
809 }
810 
811 /*
812  * This method will attempt to start a task request for this device object. The
813  * remote device object will issue the start request for the task and if
814  * successful it will start the request for the port object then increment its
815  * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
816  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
817  * object could not get the resources to start.
818  */
819 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
820 	struct scic_sds_remote_device *sci_dev,
821 	struct scic_sds_request *request)
822 {
823 	enum sci_status result;
824 
825 	/* See if the port is in a state where we can start the IO request */
826 	result = scic_sds_port_start_io(
827 		scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
828 
829 	if (result == SCI_SUCCESS) {
830 		result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
831 								 request);
832 		if (result == SCI_SUCCESS)
833 			result = scic_sds_request_start(request);
834 
835 		scic_sds_remote_device_start_request(sci_dev, request, result);
836 	}
837 
838 	return result;
839 }
840 
841 /*
842  * This method will attempt to start an io request for this device object. The
843  * remote device object will issue the start request for the io and if
844  * successful it will start the request for the port object then increment its
845  * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
846  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
847  * object could not get the resources to start.
848  */
849 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
850 	struct scic_sds_remote_device *sci_dev,
851 	struct scic_sds_request *request)
852 {
853 	enum sci_status result;
854 
855 	/* See if the port is in a state where we can start the IO request */
856 	result = scic_sds_port_start_io(
857 		scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
858 
859 	if (result == SCI_SUCCESS) {
860 		result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
861 		if (result == SCI_SUCCESS)
862 			result = scic_sds_request_start(request);
863 
864 		scic_sds_remote_device_start_request(sci_dev, request, result);
865 	}
866 
867 	return result;
868 }
869 
870 /*
871  * This method will complete the request for the remote device object.  The
872  * method will call the completion handler for the request object and if
873  * successful it will complete the request on the port object then decrement
874  * its own started_request_count. enum sci_status
875  */
876 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
877 	struct scic_sds_remote_device *sci_dev,
878 	struct scic_sds_request *request)
879 {
880 	enum sci_status result;
881 
882 	result = scic_sds_request_complete(request);
883 
884 	if (result != SCI_SUCCESS)
885 		return result;
886 
887 	/* See if the port is in a state
888 	 * where we can start the IO request */
889 	result = scic_sds_port_complete_io(
890 			scic_sds_remote_device_get_port(sci_dev),
891 			sci_dev, request);
892 
893 	if (result == SCI_SUCCESS)
894 		scic_sds_remote_device_decrement_request_count(sci_dev);
895 
896 	return result;
897 }
898 
899 /*
900  * *****************************************************************************
901  * *  STOPPING STATE HANDLERS
902  * ***************************************************************************** */
903 
904 /**
905  *
906  * @sci_dev: The struct scic_sds_remote_device which is cast into a
907  *    struct scic_sds_remote_device.
908  *
909  * This method will stop a struct scic_sds_remote_device that is already in the
910  * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
911  * since we allow a stop request on a device that is alreay stopping or
912  * stopped. enum sci_status SCI_SUCCESS
913  */
914 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
915 	struct scic_sds_remote_device *device)
916 {
917 	/*
918 	 * All requests should have been terminated, but if there is an
919 	 * attempt to stop a device already in the stopping state, then
920 	 * try again to terminate. */
921 	return scic_sds_remote_device_terminate_requests(device);
922 }
923 
924 
925 /**
926  *
927  * @device: The device object for which the request is completing.
928  * @request: The task request that is being completed.
929  *
930  * This method completes requests for this struct scic_sds_remote_device while it is
931  * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
932  * complete method for the request object and if that is successful the port
933  * object is called to complete the task request. Then the device object itself
934  * completes the task request. If struct scic_sds_remote_device started_request_count
935  * goes to 0 and the invalidate RNC request has completed the device object can
936  * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
937  */
938 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
939 	struct scic_sds_remote_device *sci_dev,
940 	struct scic_sds_request *request)
941 {
942 	enum sci_status status = SCI_SUCCESS;
943 
944 	status = scic_sds_request_complete(request);
945 
946 	if (status != SCI_SUCCESS)
947 		return status;
948 
949 	status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
950 					   sci_dev, request);
951 	if (status != SCI_SUCCESS)
952 		return status;
953 
954 	scic_sds_remote_device_decrement_request_count(sci_dev);
955 
956 	if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
957 		scic_sds_remote_node_context_destruct(&sci_dev->rnc,
958 						      scic_sds_cb_remote_device_rnc_destruct_complete,
959 						      sci_dev);
960 	return SCI_SUCCESS;
961 }
962 
963 /**
964  *
965  * @device: The struct scic_sds_remote_device which is to be cast into a
966  *    struct scic_sds_remote_device object.
967  *
968  * This method will complete the reset operation when the device is in the
969  * resetting state. enum sci_status
970  */
971 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
972 	struct scic_sds_remote_device *sci_dev)
973 {
974 
975 	sci_base_state_machine_change_state(
976 		&sci_dev->state_machine,
977 		SCI_BASE_REMOTE_DEVICE_STATE_READY
978 		);
979 
980 	return SCI_SUCCESS;
981 }
982 
983 /**
984  *
985  * @device: The struct scic_sds_remote_device which is to be cast into a
986  *    struct scic_sds_remote_device object.
987  *
988  * This method will stop the remote device while in the resetting state.
989  * enum sci_status
990  */
991 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
992 	struct scic_sds_remote_device *sci_dev)
993 {
994 	sci_base_state_machine_change_state(
995 		&sci_dev->state_machine,
996 		SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
997 		);
998 
999 	return SCI_SUCCESS;
1000 }
1001 
1002 /*
1003  * This method completes requests for this struct scic_sds_remote_device while it is
1004  * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
1005  * complete method for the request object and if that is successful the port
1006  * object is called to complete the task request. Then the device object itself
1007  * completes the task request. enum sci_status
1008  */
1009 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
1010 	struct scic_sds_remote_device *sci_dev,
1011 	struct scic_sds_request *request)
1012 {
1013 	enum sci_status status = SCI_SUCCESS;
1014 
1015 	status = scic_sds_request_complete(request);
1016 
1017 	if (status == SCI_SUCCESS) {
1018 		status = scic_sds_port_complete_io(
1019 				scic_sds_remote_device_get_port(sci_dev),
1020 				sci_dev, request);
1021 
1022 		if (status == SCI_SUCCESS) {
1023 			scic_sds_remote_device_decrement_request_count(sci_dev);
1024 		}
1025 	}
1026 
1027 	return status;
1028 }
1029 
1030 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1031 	[SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1032 		.start_handler		= scic_sds_remote_device_default_start_handler,
1033 		.stop_handler		= scic_sds_remote_device_default_stop_handler,
1034 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1035 		.destruct_handler	= scic_sds_remote_device_default_destruct_handler,
1036 		.reset_handler		= scic_sds_remote_device_default_reset_handler,
1037 		.reset_complete_handler	= scic_sds_remote_device_default_reset_complete_handler,
1038 		.start_io_handler	= scic_sds_remote_device_default_start_request_handler,
1039 		.complete_io_handler	= scic_sds_remote_device_default_complete_request_handler,
1040 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1041 		.start_task_handler	= scic_sds_remote_device_default_start_request_handler,
1042 		.complete_task_handler	= scic_sds_remote_device_default_complete_request_handler,
1043 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1044 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1045 		.event_handler		= scic_sds_remote_device_default_event_handler,
1046 		.frame_handler		= scic_sds_remote_device_default_frame_handler
1047 	},
1048 	[SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1049 		.start_handler		= scic_sds_remote_device_stopped_state_start_handler,
1050 		.stop_handler		= scic_sds_remote_device_stopped_state_stop_handler,
1051 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1052 		.destruct_handler	= scic_sds_remote_device_stopped_state_destruct_handler,
1053 		.reset_handler		= scic_sds_remote_device_default_reset_handler,
1054 		.reset_complete_handler	= scic_sds_remote_device_default_reset_complete_handler,
1055 		.start_io_handler	= scic_sds_remote_device_default_start_request_handler,
1056 		.complete_io_handler	= scic_sds_remote_device_default_complete_request_handler,
1057 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1058 		.start_task_handler	= scic_sds_remote_device_default_start_request_handler,
1059 		.complete_task_handler	= scic_sds_remote_device_default_complete_request_handler,
1060 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1061 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1062 		.event_handler		= scic_sds_remote_device_default_event_handler,
1063 		.frame_handler		= scic_sds_remote_device_default_frame_handler
1064 	},
1065 	[SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1066 		.start_handler		= scic_sds_remote_device_default_start_handler,
1067 		.stop_handler		= scic_sds_remote_device_starting_state_stop_handler,
1068 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1069 		.destruct_handler	= scic_sds_remote_device_default_destruct_handler,
1070 		.reset_handler		= scic_sds_remote_device_default_reset_handler,
1071 		.reset_complete_handler	= scic_sds_remote_device_default_reset_complete_handler,
1072 		.start_io_handler	= scic_sds_remote_device_default_start_request_handler,
1073 		.complete_io_handler	= scic_sds_remote_device_default_complete_request_handler,
1074 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1075 		.start_task_handler	= scic_sds_remote_device_default_start_request_handler,
1076 		.complete_task_handler	= scic_sds_remote_device_default_complete_request_handler,
1077 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1078 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1079 		.event_handler		= scic_sds_remote_device_general_event_handler,
1080 		.frame_handler		= scic_sds_remote_device_default_frame_handler
1081 	},
1082 	[SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1083 		.start_handler		= scic_sds_remote_device_default_start_handler,
1084 		.stop_handler		= scic_sds_remote_device_ready_state_stop_handler,
1085 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1086 		.destruct_handler	= scic_sds_remote_device_default_destruct_handler,
1087 		.reset_handler		= scic_sds_remote_device_ready_state_reset_handler,
1088 		.reset_complete_handler	= scic_sds_remote_device_default_reset_complete_handler,
1089 		.start_io_handler	= scic_sds_remote_device_ready_state_start_io_handler,
1090 		.complete_io_handler	= scic_sds_remote_device_ready_state_complete_request_handler,
1091 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1092 		.start_task_handler	= scic_sds_remote_device_ready_state_start_task_handler,
1093 		.complete_task_handler	= scic_sds_remote_device_ready_state_complete_request_handler,
1094 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1095 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1096 		.event_handler		= scic_sds_remote_device_general_event_handler,
1097 		.frame_handler		= scic_sds_remote_device_general_frame_handler,
1098 	},
1099 	[SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1100 		.start_handler		= scic_sds_remote_device_default_start_handler,
1101 		.stop_handler		= scic_sds_remote_device_stopping_state_stop_handler,
1102 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1103 		.destruct_handler	= scic_sds_remote_device_default_destruct_handler,
1104 		.reset_handler		= scic_sds_remote_device_default_reset_handler,
1105 		.reset_complete_handler	= scic_sds_remote_device_default_reset_complete_handler,
1106 		.start_io_handler	= scic_sds_remote_device_default_start_request_handler,
1107 		.complete_io_handler	= scic_sds_remote_device_stopping_state_complete_request_handler,
1108 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1109 		.start_task_handler	= scic_sds_remote_device_default_start_request_handler,
1110 		.complete_task_handler	= scic_sds_remote_device_stopping_state_complete_request_handler,
1111 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1112 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1113 		.event_handler		= scic_sds_remote_device_general_event_handler,
1114 		.frame_handler		= scic_sds_remote_device_general_frame_handler
1115 	},
1116 	[SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1117 		.start_handler		= scic_sds_remote_device_default_start_handler,
1118 		.stop_handler		= scic_sds_remote_device_default_stop_handler,
1119 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1120 		.destruct_handler	= scic_sds_remote_device_default_destruct_handler,
1121 		.reset_handler		= scic_sds_remote_device_default_reset_handler,
1122 		.reset_complete_handler	= scic_sds_remote_device_default_reset_complete_handler,
1123 		.start_io_handler	= scic_sds_remote_device_default_start_request_handler,
1124 		.complete_io_handler	= scic_sds_remote_device_default_complete_request_handler,
1125 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1126 		.start_task_handler	= scic_sds_remote_device_default_start_request_handler,
1127 		.complete_task_handler	= scic_sds_remote_device_default_complete_request_handler,
1128 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1129 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1130 		.event_handler		= scic_sds_remote_device_default_event_handler,
1131 		.frame_handler		= scic_sds_remote_device_general_frame_handler
1132 	},
1133 	[SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1134 		.start_handler		= scic_sds_remote_device_default_start_handler,
1135 		.stop_handler		= scic_sds_remote_device_resetting_state_stop_handler,
1136 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1137 		.destruct_handler	= scic_sds_remote_device_default_destruct_handler,
1138 		.reset_handler		= scic_sds_remote_device_default_reset_handler,
1139 		.reset_complete_handler	= scic_sds_remote_device_resetting_state_reset_complete_handler,
1140 		.start_io_handler	= scic_sds_remote_device_default_start_request_handler,
1141 		.complete_io_handler	= scic_sds_remote_device_resetting_state_complete_request_handler,
1142 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1143 		.start_task_handler	= scic_sds_remote_device_default_start_request_handler,
1144 		.complete_task_handler	= scic_sds_remote_device_resetting_state_complete_request_handler,
1145 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1146 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1147 		.event_handler		= scic_sds_remote_device_default_event_handler,
1148 		.frame_handler		= scic_sds_remote_device_general_frame_handler
1149 	},
1150 	[SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1151 		.start_handler		= scic_sds_remote_device_default_start_handler,
1152 		.stop_handler		= scic_sds_remote_device_default_stop_handler,
1153 		.fail_handler		= scic_sds_remote_device_default_fail_handler,
1154 		.destruct_handler	= scic_sds_remote_device_default_destruct_handler,
1155 		.reset_handler		= scic_sds_remote_device_default_reset_handler,
1156 		.reset_complete_handler	= scic_sds_remote_device_default_reset_complete_handler,
1157 		.start_io_handler	= scic_sds_remote_device_default_start_request_handler,
1158 		.complete_io_handler	= scic_sds_remote_device_default_complete_request_handler,
1159 		.continue_io_handler	= scic_sds_remote_device_default_continue_request_handler,
1160 		.start_task_handler	= scic_sds_remote_device_default_start_request_handler,
1161 		.complete_task_handler	= scic_sds_remote_device_default_complete_request_handler,
1162 		.suspend_handler	= scic_sds_remote_device_default_suspend_handler,
1163 		.resume_handler		= scic_sds_remote_device_default_resume_handler,
1164 		.event_handler		= scic_sds_remote_device_default_event_handler,
1165 		.frame_handler		= scic_sds_remote_device_default_frame_handler
1166 	}
1167 };
1168 
1169 static void scic_sds_remote_device_initial_state_enter(
1170 	struct sci_base_object *object)
1171 {
1172 	struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1173 
1174 	sci_dev = container_of(object, typeof(*sci_dev), parent);
1175 	SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1176 			  SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1177 
1178 	/* Initial state is a transitional state to the stopped state */
1179 	sci_base_state_machine_change_state(&sci_dev->state_machine,
1180 					    SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1181 }
1182 
1183 /**
1184  * isci_remote_device_change_state() - This function gets the status of the
1185  *    remote_device object.
1186  * @isci_device: This parameter points to the isci_remote_device object
1187  *
1188  * status of the object as a isci_status enum.
1189  */
1190 void isci_remote_device_change_state(
1191 	struct isci_remote_device *isci_device,
1192 	enum isci_status status)
1193 {
1194 	unsigned long flags;
1195 
1196 	spin_lock_irqsave(&isci_device->state_lock, flags);
1197 	isci_device->status = status;
1198 	spin_unlock_irqrestore(&isci_device->state_lock, flags);
1199 }
1200 
1201 /**
1202  * scic_remote_device_destruct() - free remote node context and destruct
1203  * @remote_device: This parameter specifies the remote device to be destructed.
1204  *
1205  * Remote device objects are a limited resource.  As such, they must be
1206  * protected.  Thus calls to construct and destruct are mutually exclusive and
1207  * non-reentrant. The return value shall indicate if the device was
1208  * successfully destructed or if some failure occurred. enum sci_status This value
1209  * is returned if the device is successfully destructed.
1210  * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1211  * device isn't valid (e.g. it's already been destoryed, the handle isn't
1212  * valid, etc.).
1213  */
1214 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1215 {
1216 	return sci_dev->state_handlers->destruct_handler(sci_dev);
1217 }
1218 
1219 /**
1220  * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1221  * @ihost: This parameter specifies the isci host object.
1222  * @idev: This parameter specifies the remote device to be freed.
1223  *
1224  */
1225 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1226 {
1227 	dev_dbg(&ihost->pdev->dev,
1228 		"%s: isci_device = %p\n", __func__, idev);
1229 
1230 	/* There should not be any outstanding io's. All paths to
1231 	 * here should go through isci_remote_device_nuke_requests.
1232 	 * If we hit this condition, we will need a way to complete
1233 	 * io requests in process */
1234 	while (!list_empty(&idev->reqs_in_process)) {
1235 
1236 		dev_err(&ihost->pdev->dev,
1237 			"%s: ** request list not empty! **\n", __func__);
1238 		BUG();
1239 	}
1240 
1241 	scic_remote_device_destruct(&idev->sci);
1242 	idev->domain_dev->lldd_dev = NULL;
1243 	idev->domain_dev = NULL;
1244 	idev->isci_port = NULL;
1245 	list_del_init(&idev->node);
1246 
1247 	clear_bit(IDEV_START_PENDING, &idev->flags);
1248 	clear_bit(IDEV_STOP_PENDING, &idev->flags);
1249 	wake_up(&ihost->eventq);
1250 }
1251 
1252 /**
1253  * isci_remote_device_stop_complete() - This function is called by the scic
1254  *    when the remote device stop has completed. We mark the isci device as not
1255  *    ready and remove the isci remote device.
1256  * @ihost: This parameter specifies the isci host object.
1257  * @idev: This parameter specifies the remote device.
1258  * @status: This parameter specifies status of the completion.
1259  *
1260  */
1261 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1262 					     struct isci_remote_device *idev)
1263 {
1264 	dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1265 
1266 	isci_remote_device_change_state(idev, isci_stopped);
1267 
1268 	/* after stop, we can tear down resources. */
1269 	isci_remote_device_deconstruct(ihost, idev);
1270 }
1271 
1272 static void scic_sds_remote_device_stopped_state_enter(
1273 	struct sci_base_object *object)
1274 {
1275 	struct scic_sds_remote_device *sci_dev;
1276 	struct scic_sds_controller *scic;
1277 	struct isci_remote_device *idev;
1278 	struct isci_host *ihost;
1279 	u32 prev_state;
1280 
1281 	sci_dev = container_of(object, typeof(*sci_dev), parent);
1282 	scic = scic_sds_remote_device_get_controller(sci_dev);
1283 	ihost = sci_object_get_association(scic);
1284 	idev = sci_object_get_association(sci_dev);
1285 
1286 	SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1287 			  SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1288 
1289 	/* If we are entering from the stopping state let the SCI User know that
1290 	 * the stop operation has completed.
1291 	 */
1292 	prev_state = sci_dev->state_machine.previous_state_id;
1293 	if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1294 		isci_remote_device_stop_complete(ihost, idev);
1295 
1296 	scic_sds_controller_remote_device_stopped(scic, sci_dev);
1297 }
1298 
1299 static void scic_sds_remote_device_starting_state_enter(struct sci_base_object *object)
1300 {
1301 	struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1302 							      parent);
1303 	struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1304 	struct isci_host *ihost = sci_object_get_association(scic);
1305 	struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1306 
1307 	SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1308 			  SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1309 
1310 	isci_remote_device_not_ready(ihost, idev,
1311 				     SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1312 }
1313 
1314 static void scic_sds_remote_device_ready_state_enter(struct sci_base_object *object)
1315 {
1316 	struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1317 							      parent);
1318 	struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1319 	struct isci_host *ihost = sci_object_get_association(scic);
1320 	struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1321 
1322 	SET_STATE_HANDLER(sci_dev,
1323 			  scic_sds_remote_device_state_handler_table,
1324 			  SCI_BASE_REMOTE_DEVICE_STATE_READY);
1325 
1326 	scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1327 
1328 	if (sci_dev->has_ready_substate_machine)
1329 		sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1330 	else
1331 		isci_remote_device_ready(ihost, idev);
1332 }
1333 
1334 static void scic_sds_remote_device_ready_state_exit(
1335 	struct sci_base_object *object)
1336 {
1337 	struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1338 							      parent);
1339 	if (sci_dev->has_ready_substate_machine)
1340 		sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1341 	else {
1342 		struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1343 		struct isci_host *ihost = sci_object_get_association(scic);
1344 		struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1345 
1346 		isci_remote_device_not_ready(ihost, idev,
1347 					     SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1348 	}
1349 }
1350 
1351 static void scic_sds_remote_device_stopping_state_enter(
1352 	struct sci_base_object *object)
1353 {
1354 	struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1355 
1356 	SET_STATE_HANDLER(
1357 		sci_dev,
1358 		scic_sds_remote_device_state_handler_table,
1359 		SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1360 		);
1361 }
1362 
1363 static void scic_sds_remote_device_failed_state_enter(
1364 	struct sci_base_object *object)
1365 {
1366 	struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1367 
1368 	SET_STATE_HANDLER(
1369 		sci_dev,
1370 		scic_sds_remote_device_state_handler_table,
1371 		SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1372 		);
1373 }
1374 
1375 static void scic_sds_remote_device_resetting_state_enter(
1376 	struct sci_base_object *object)
1377 {
1378 	struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1379 
1380 	SET_STATE_HANDLER(
1381 		sci_dev,
1382 		scic_sds_remote_device_state_handler_table,
1383 		SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1384 		);
1385 
1386 	scic_sds_remote_node_context_suspend(
1387 		&sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1388 }
1389 
1390 static void scic_sds_remote_device_resetting_state_exit(
1391 	struct sci_base_object *object)
1392 {
1393 	struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1394 
1395 	scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1396 }
1397 
1398 static void scic_sds_remote_device_final_state_enter(
1399 	struct sci_base_object *object)
1400 {
1401 	struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1402 
1403 	SET_STATE_HANDLER(
1404 		sci_dev,
1405 		scic_sds_remote_device_state_handler_table,
1406 		SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1407 		);
1408 }
1409 
1410 
1411 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1412 	[SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1413 		.enter_state = scic_sds_remote_device_initial_state_enter,
1414 	},
1415 	[SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1416 		.enter_state = scic_sds_remote_device_stopped_state_enter,
1417 	},
1418 	[SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1419 		.enter_state = scic_sds_remote_device_starting_state_enter,
1420 	},
1421 	[SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1422 		.enter_state = scic_sds_remote_device_ready_state_enter,
1423 		.exit_state  = scic_sds_remote_device_ready_state_exit
1424 	},
1425 	[SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1426 		.enter_state = scic_sds_remote_device_stopping_state_enter,
1427 	},
1428 	[SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1429 		.enter_state = scic_sds_remote_device_failed_state_enter,
1430 	},
1431 	[SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1432 		.enter_state = scic_sds_remote_device_resetting_state_enter,
1433 		.exit_state  = scic_sds_remote_device_resetting_state_exit
1434 	},
1435 	[SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1436 		.enter_state = scic_sds_remote_device_final_state_enter,
1437 	},
1438 };
1439 
1440 /**
1441  * scic_remote_device_construct() - This method will perform the construction
1442  *    common to all remote device objects.
1443  * @sci_port: SAS/SATA port through which this device is accessed.
1444  * @sci_dev: remote device to construct
1445  *
1446  * It isn't necessary to call scic_remote_device_destruct() for device objects
1447  * that have only called this method for construction. Once subsequent
1448  * construction methods have been invoked (e.g.
1449  * scic_remote_device_da_construct()), then destruction should occur. none
1450  */
1451 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1452 				  struct scic_sds_remote_device *sci_dev)
1453 {
1454 	sci_dev->owning_port = sci_port;
1455 	sci_dev->started_request_count = 0;
1456 	sci_dev->parent.private = NULL;
1457 
1458 	sci_base_state_machine_construct(
1459 		&sci_dev->state_machine,
1460 		&sci_dev->parent,
1461 		scic_sds_remote_device_state_table,
1462 		SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1463 		);
1464 
1465 	sci_base_state_machine_start(
1466 		&sci_dev->state_machine
1467 		);
1468 
1469 	scic_sds_remote_node_context_construct(&sci_dev->rnc,
1470 					       SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1471 
1472 	sci_object_set_association(&sci_dev->rnc, sci_dev);
1473 }
1474 
1475 /**
1476  * scic_remote_device_da_construct() - This method will construct a
1477  *    SCIC_REMOTE_DEVICE object for a direct attached (da) device.  The
1478  *    information (e.g. IAF, Signature FIS, etc.) necessary to build the device
1479  *    is known to the SCI Core since it is contained in the scic_phy object.
1480  * @remote_device: This parameter specifies the remote device to be destructed.
1481  *
1482  * The user must have previously called scic_remote_device_construct() Remote
1483  * device objects are a limited resource.  As such, they must be protected.
1484  * Thus calls to construct and destruct are mutually exclusive and
1485  * non-reentrant. Indicate if the remote device was successfully constructed.
1486  * SCI_SUCCESS Returned if the device was successfully constructed.
1487  * SCI_FAILURE_DEVICE_EXISTS Returned if the device has already been
1488  * constructed.  If it's an additional phy for the target, then call
1489  * scic_remote_device_da_add_phy(). SCI_FAILURE_UNSUPPORTED_PROTOCOL Returned
1490  * if the supplied parameters necessitate creation of a remote device for which
1491  * the protocol is not supported by the underlying controller hardware.
1492  * SCI_FAILURE_INSUFFICIENT_RESOURCES This value is returned if the core
1493  * controller associated with the supplied parameters is unable to support
1494  * additional remote devices.
1495  */
1496 static enum sci_status scic_remote_device_da_construct(struct scic_sds_remote_device *sci_dev)
1497 {
1498 	enum sci_status status;
1499 	u16 remote_node_index;
1500 	struct sci_sas_identify_address_frame_protocols protocols;
1501 
1502 	/*
1503 	 * This information is request to determine how many remote node context
1504 	 * entries will be needed to store the remote node.
1505 	 */
1506 	scic_sds_port_get_attached_protocols(sci_dev->owning_port, &protocols);
1507 	sci_dev->target_protocols.u.all = protocols.u.all;
1508 	sci_dev->is_direct_attached = true;
1509 #if !defined(DISABLE_ATAPI)
1510 	sci_dev->is_atapi = scic_sds_remote_device_is_atapi(sci_dev);
1511 #endif
1512 
1513 	status = scic_sds_controller_allocate_remote_node_context(
1514 		sci_dev->owning_port->owning_controller,
1515 		sci_dev,
1516 		&remote_node_index);
1517 
1518 	if (status == SCI_SUCCESS) {
1519 		sci_dev->rnc.remote_node_index = remote_node_index;
1520 
1521 		scic_sds_port_get_attached_sas_address(
1522 			sci_dev->owning_port, &sci_dev->device_address);
1523 
1524 		if (sci_dev->target_protocols.u.bits.attached_ssp_target) {
1525 			sci_dev->has_ready_substate_machine = false;
1526 		} else if (sci_dev->target_protocols.u.bits.attached_stp_target) {
1527 			sci_dev->has_ready_substate_machine = true;
1528 
1529 			sci_base_state_machine_construct(
1530 				&sci_dev->ready_substate_machine,
1531 				&sci_dev->parent,
1532 				scic_sds_stp_remote_device_ready_substate_table,
1533 				SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1534 		} else if (sci_dev->target_protocols.u.bits.attached_smp_target) {
1535 			sci_dev->has_ready_substate_machine = true;
1536 
1537 			/* add the SMP ready substate machine construction here */
1538 			sci_base_state_machine_construct(
1539 				&sci_dev->ready_substate_machine,
1540 				&sci_dev->parent,
1541 				scic_sds_smp_remote_device_ready_substate_table,
1542 				SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1543 		}
1544 
1545 		sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(
1546 			sci_dev->owning_port);
1547 
1548 		/* / @todo Should I assign the port width by reading all of the phys on the port? */
1549 		sci_dev->device_port_width = 1;
1550 	}
1551 
1552 	return status;
1553 }
1554 
1555 static void scic_sds_remote_device_get_info_from_smp_discover_response(
1556 	struct scic_sds_remote_device *sci_dev,
1557 	struct smp_response_discover *discover_response)
1558 {
1559 	/* decode discover_response to set sas_address to sci_dev. */
1560 	sci_dev->device_address.high =
1561 		discover_response->attached_sas_address.high;
1562 
1563 	sci_dev->device_address.low =
1564 		discover_response->attached_sas_address.low;
1565 
1566 	sci_dev->target_protocols.u.all = discover_response->protocols.u.all;
1567 }
1568 
1569 /**
1570  * scic_remote_device_ea_construct() - This method will construct an
1571  *    SCIC_REMOTE_DEVICE object for an expander attached (ea) device from an
1572  *    SMP Discover Response.
1573  * @remote_device: This parameter specifies the remote device to be destructed.
1574  * @discover_response: This parameter specifies the SMP Discovery Response to
1575  *    be used in device creation.
1576  *
1577  * The user must have previously called scic_remote_device_construct() Remote
1578  * device objects are a limited resource.  As such, they must be protected.
1579  * Thus calls to construct and destruct are mutually exclusive and
1580  * non-reentrant. Indicate if the remote device was successfully constructed.
1581  * SCI_SUCCESS Returned if the device was successfully constructed.
1582  * SCI_FAILURE_DEVICE_EXISTS Returned if the device has already been
1583  * constructed.  If it's an additional phy for the target, then call
1584  * scic_ea_remote_device_add_phy(). SCI_FAILURE_UNSUPPORTED_PROTOCOL Returned
1585  * if the supplied parameters necessitate creation of a remote device for which
1586  * the protocol is not supported by the underlying controller hardware.
1587  * SCI_FAILURE_INSUFFICIENT_RESOURCES This value is returned if the core
1588  * controller associated with the supplied parameters is unable to support
1589  * additional remote devices.
1590  */
1591 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_remote_device *sci_dev,
1592 						       struct smp_response_discover *discover_response)
1593 {
1594 	enum sci_status status;
1595 	struct scic_sds_controller *scic;
1596 
1597 	scic = scic_sds_port_get_controller(sci_dev->owning_port);
1598 
1599 	scic_sds_remote_device_get_info_from_smp_discover_response(
1600 		sci_dev, discover_response);
1601 
1602 	status = scic_sds_controller_allocate_remote_node_context(
1603 		scic, sci_dev, &sci_dev->rnc.remote_node_index);
1604 
1605 	if (status == SCI_SUCCESS) {
1606 		if (sci_dev->target_protocols.u.bits.attached_ssp_target) {
1607 			sci_dev->has_ready_substate_machine = false;
1608 		} else if (sci_dev->target_protocols.u.bits.attached_smp_target) {
1609 			sci_dev->has_ready_substate_machine = true;
1610 
1611 			/* add the SMP ready substate machine construction here */
1612 			sci_base_state_machine_construct(
1613 				&sci_dev->ready_substate_machine,
1614 				&sci_dev->parent,
1615 				scic_sds_smp_remote_device_ready_substate_table,
1616 				SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1617 		} else if (sci_dev->target_protocols.u.bits.attached_stp_target) {
1618 			sci_dev->has_ready_substate_machine = true;
1619 
1620 			sci_base_state_machine_construct(
1621 				&sci_dev->ready_substate_machine,
1622 				&sci_dev->parent,
1623 				scic_sds_stp_remote_device_ready_substate_table,
1624 				SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1625 		}
1626 
1627 		/*
1628 		 * For SAS-2 the physical link rate is actually a logical link
1629 		 * rate that incorporates multiplexing.  The SCU doesn't
1630 		 * incorporate multiplexing and for the purposes of the
1631 		 * connection the logical link rate is that same as the
1632 		 * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
1633 		 * one another, so this code works for both situations. */
1634 		sci_dev->connection_rate = min_t(u16,
1635 			scic_sds_port_get_max_allowed_speed(sci_dev->owning_port),
1636 			discover_response->u2.sas1_1.negotiated_physical_link_rate
1637 			);
1638 
1639 		/* / @todo Should I assign the port width by reading all of the phys on the port? */
1640 		sci_dev->device_port_width = 1;
1641 	}
1642 
1643 	return status;
1644 }
1645 
1646 /**
1647  * scic_remote_device_start() - This method will start the supplied remote
1648  *    device.  This method enables normal IO requests to flow through to the
1649  *    remote device.
1650  * @remote_device: This parameter specifies the device to be started.
1651  * @timeout: This parameter specifies the number of milliseconds in which the
1652  *    start operation should complete.
1653  *
1654  * An indication of whether the device was successfully started. SCI_SUCCESS
1655  * This value is returned if the device was successfully started.
1656  * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1657  * the device when there have been no phys added to it.
1658  */
1659 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1660 					 u32 timeout)
1661 {
1662 	return sci_dev->state_handlers->start_handler(sci_dev);
1663 }
1664 
1665 /**
1666  * isci_remote_device_construct() - This function calls the scic remote device
1667  *    construct and start functions, it waits on the remote device start
1668  *    completion.
1669  * @port: This parameter specifies the isci port with the remote device.
1670  * @isci_device: This parameter specifies the isci remote device
1671  *
1672  * status from the scic calls, the caller to this function should clean up
1673  * resources as appropriate.
1674  */
1675 static enum sci_status isci_remote_device_construct(
1676 	struct isci_port *port,
1677 	struct isci_remote_device *isci_device)
1678 {
1679 	enum sci_status status = SCI_SUCCESS;
1680 
1681 	/* let the core do it's common constuction. */
1682 	scic_remote_device_construct(port->sci_port_handle,
1683 				     &isci_device->sci);
1684 
1685 	/* let the core do it's device specific constuction. */
1686 	if (isci_device->domain_dev->parent &&
1687 	    (isci_device->domain_dev->parent->dev_type == EDGE_DEV)) {
1688 		int i;
1689 
1690 		/* struct smp_response_discover discover_response; */
1691 		struct discover_resp discover_response;
1692 		struct domain_device *parent =
1693 			isci_device->domain_dev->parent;
1694 
1695 		struct expander_device *parent_ex = &parent->ex_dev;
1696 
1697 		for (i = 0; i < parent_ex->num_phys; i++) {
1698 
1699 			struct ex_phy *phy = &parent_ex->ex_phy[i];
1700 
1701 			if ((phy->phy_state == PHY_VACANT) ||
1702 			    (phy->phy_state == PHY_NOT_PRESENT))
1703 				continue;
1704 
1705 			if (SAS_ADDR(phy->attached_sas_addr)
1706 			    == SAS_ADDR(isci_device->domain_dev->sas_addr)) {
1707 
1708 				discover_response.attached_dev_type
1709 					= phy->attached_dev_type;
1710 				discover_response.linkrate
1711 					= phy->linkrate;
1712 				discover_response.attached_sata_host
1713 					= phy->attached_sata_host;
1714 				discover_response.attached_sata_dev
1715 					= phy->attached_sata_dev;
1716 				discover_response.attached_sata_ps
1717 					= phy->attached_sata_ps;
1718 				discover_response.iproto
1719 					= phy->attached_iproto >> 1;
1720 				discover_response.tproto
1721 					= phy->attached_tproto >> 1;
1722 				memcpy(
1723 					discover_response.attached_sas_addr,
1724 					phy->attached_sas_addr,
1725 					SAS_ADDR_SIZE
1726 					);
1727 				discover_response.attached_phy_id
1728 					= phy->attached_phy_id;
1729 				discover_response.change_count
1730 					= phy->phy_change_count;
1731 				discover_response.routing_attr
1732 					= phy->routing_attr;
1733 				discover_response.hmin_linkrate
1734 					= phy->phy->minimum_linkrate_hw;
1735 				discover_response.hmax_linkrate
1736 					= phy->phy->maximum_linkrate_hw;
1737 				discover_response.pmin_linkrate
1738 					= phy->phy->minimum_linkrate;
1739 				discover_response.pmax_linkrate
1740 					= phy->phy->maximum_linkrate;
1741 			}
1742 		}
1743 
1744 
1745 		dev_dbg(&port->isci_host->pdev->dev,
1746 			"%s: parent->dev_type = EDGE_DEV\n",
1747 			__func__);
1748 
1749 		status = scic_remote_device_ea_construct(&isci_device->sci,
1750 				(struct smp_response_discover *)&discover_response);
1751 
1752 	} else
1753 		status = scic_remote_device_da_construct(&isci_device->sci);
1754 
1755 
1756 	if (status != SCI_SUCCESS) {
1757 		dev_dbg(&port->isci_host->pdev->dev,
1758 			"%s: scic_remote_device_da_construct failed - "
1759 			"isci_device = %p\n",
1760 			__func__,
1761 			isci_device);
1762 
1763 		return status;
1764 	}
1765 
1766 	/* XXX will be killed with sci_base_object removal */
1767 	sci_object_set_association(&isci_device->sci, isci_device);
1768 
1769 	/* start the device. */
1770 	status = scic_remote_device_start(&isci_device->sci,
1771 					  ISCI_REMOTE_DEVICE_START_TIMEOUT);
1772 
1773 	if (status != SCI_SUCCESS) {
1774 		dev_warn(&port->isci_host->pdev->dev,
1775 			 "%s: scic_remote_device_start failed\n",
1776 			 __func__);
1777 		return status;
1778 	}
1779 
1780 	return status;
1781 }
1782 
1783 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1784 {
1785 	DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1786 
1787 	dev_dbg(&ihost->pdev->dev,
1788 		"%s: idev = %p\n", __func__, idev);
1789 
1790 	/* Cleanup all requests pending for this device. */
1791 	isci_terminate_pending_requests(ihost, idev, terminating);
1792 
1793 	dev_dbg(&ihost->pdev->dev,
1794 		"%s: idev = %p, done\n", __func__, idev);
1795 }
1796 
1797 /**
1798  * This function builds the isci_remote_device when a libsas dev_found message
1799  *    is received.
1800  * @isci_host: This parameter specifies the isci host object.
1801  * @port: This parameter specifies the isci_port conected to this device.
1802  *
1803  * pointer to new isci_remote_device.
1804  */
1805 static struct isci_remote_device *
1806 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1807 {
1808 	struct isci_remote_device *idev;
1809 	int i;
1810 
1811 	for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1812 		idev = &ihost->devices[i];
1813 		if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1814 			break;
1815 	}
1816 
1817 	if (i >= SCI_MAX_REMOTE_DEVICES) {
1818 		dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1819 		return NULL;
1820 	}
1821 
1822 	if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1823 		return NULL;
1824 
1825 	if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1826 		return NULL;
1827 
1828 	isci_remote_device_change_state(idev, isci_freed);
1829 
1830 	return idev;
1831 }
1832 
1833 /**
1834  * isci_remote_device_ready() - This function is called by the scic when the
1835  *    remote device is ready. We mark the isci device as ready and signal the
1836  *    waiting proccess.
1837  * @ihost: our valid isci_host
1838  * @idev: remote device
1839  *
1840  */
1841 void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
1842 {
1843 	dev_dbg(&ihost->pdev->dev,
1844 		"%s: idev = %p\n", __func__, idev);
1845 
1846 	isci_remote_device_change_state(idev, isci_ready_for_io);
1847 	if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1848 		wake_up(&ihost->eventq);
1849 }
1850 
1851 /**
1852  * isci_remote_device_not_ready() - This function is called by the scic when
1853  *    the remote device is not ready. We mark the isci device as ready (not
1854  *    "ready_for_io") and signal the waiting proccess.
1855  * @isci_host: This parameter specifies the isci host object.
1856  * @isci_device: This parameter specifies the remote device
1857  *
1858  */
1859 void isci_remote_device_not_ready(struct isci_host *ihost,
1860 				  struct isci_remote_device *idev, u32 reason)
1861 {
1862 	dev_dbg(&ihost->pdev->dev,
1863 		"%s: isci_device = %p\n", __func__, idev);
1864 
1865 	if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1866 		isci_remote_device_change_state(idev, isci_stopping);
1867 	else
1868 		/* device ready is actually a "not ready for io" state. */
1869 		isci_remote_device_change_state(idev, isci_ready);
1870 }
1871 
1872 /**
1873  * isci_remote_device_stop() - This function is called internally to stop the
1874  *    remote device.
1875  * @isci_host: This parameter specifies the isci host object.
1876  * @isci_device: This parameter specifies the remote device.
1877  *
1878  * The status of the scic request to stop.
1879  */
1880 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1881 {
1882 	enum sci_status status;
1883 	unsigned long flags;
1884 
1885 	dev_dbg(&ihost->pdev->dev,
1886 		"%s: isci_device = %p\n", __func__, idev);
1887 
1888 	isci_remote_device_change_state(idev, isci_stopping);
1889 
1890 	/* Kill all outstanding requests. */
1891 	isci_remote_device_nuke_requests(ihost, idev);
1892 
1893 	set_bit(IDEV_STOP_PENDING, &idev->flags);
1894 
1895 	spin_lock_irqsave(&ihost->scic_lock, flags);
1896 	status = scic_remote_device_stop(&idev->sci, 50);
1897 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1898 
1899 	/* Wait for the stop complete callback. */
1900 	if (status == SCI_SUCCESS) {
1901 		wait_for_device_stop(ihost, idev);
1902 		clear_bit(IDEV_ALLOCATED, &idev->flags);
1903 	}
1904 
1905 	dev_dbg(&ihost->pdev->dev,
1906 		"%s: idev = %p - after completion wait\n",
1907 		__func__, idev);
1908 
1909 	return status;
1910 }
1911 
1912 /**
1913  * isci_remote_device_gone() - This function is called by libsas when a domain
1914  *    device is removed.
1915  * @domain_device: This parameter specifies the libsas domain device.
1916  *
1917  */
1918 void isci_remote_device_gone(struct domain_device *dev)
1919 {
1920 	struct isci_host *ihost = dev_to_ihost(dev);
1921 	struct isci_remote_device *idev = dev->lldd_dev;
1922 
1923 	dev_dbg(&ihost->pdev->dev,
1924 		"%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1925 		__func__, dev, idev, idev->isci_port);
1926 
1927 	isci_remote_device_stop(ihost, idev);
1928 }
1929 
1930 
1931 /**
1932  * isci_remote_device_found() - This function is called by libsas when a remote
1933  *    device is discovered. A remote device object is created and started. the
1934  *    function then sleeps until the sci core device started message is
1935  *    received.
1936  * @domain_device: This parameter specifies the libsas domain device.
1937  *
1938  * status, zero indicates success.
1939  */
1940 int isci_remote_device_found(struct domain_device *domain_dev)
1941 {
1942 	struct isci_host *isci_host = dev_to_ihost(domain_dev);
1943 	struct isci_port *isci_port;
1944 	struct isci_phy *isci_phy;
1945 	struct asd_sas_port *sas_port;
1946 	struct asd_sas_phy *sas_phy;
1947 	struct isci_remote_device *isci_device;
1948 	enum sci_status status;
1949 
1950 	dev_dbg(&isci_host->pdev->dev,
1951 		"%s: domain_device = %p\n", __func__, domain_dev);
1952 
1953 	wait_for_start(isci_host);
1954 
1955 	sas_port = domain_dev->port;
1956 	sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1957 				   port_phy_el);
1958 	isci_phy = to_isci_phy(sas_phy);
1959 	isci_port = isci_phy->isci_port;
1960 
1961 	/* we are being called for a device on this port,
1962 	 * so it has to come up eventually
1963 	 */
1964 	wait_for_completion(&isci_port->start_complete);
1965 
1966 	if ((isci_stopping == isci_port_get_state(isci_port)) ||
1967 	    (isci_stopped == isci_port_get_state(isci_port)))
1968 		return -ENODEV;
1969 
1970 	isci_device = isci_remote_device_alloc(isci_host, isci_port);
1971 	if (!isci_device)
1972 		return -ENODEV;
1973 
1974 	INIT_LIST_HEAD(&isci_device->node);
1975 	domain_dev->lldd_dev = isci_device;
1976 	isci_device->domain_dev = domain_dev;
1977 	isci_device->isci_port = isci_port;
1978 	isci_remote_device_change_state(isci_device, isci_starting);
1979 
1980 
1981 	spin_lock_irq(&isci_host->scic_lock);
1982 	list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1983 
1984 	set_bit(IDEV_START_PENDING, &isci_device->flags);
1985 	status = isci_remote_device_construct(isci_port, isci_device);
1986 	spin_unlock_irq(&isci_host->scic_lock);
1987 
1988 	dev_dbg(&isci_host->pdev->dev,
1989 		"%s: isci_device = %p\n",
1990 		__func__, isci_device);
1991 
1992 	if (status != SCI_SUCCESS) {
1993 
1994 		spin_lock_irq(&isci_host->scic_lock);
1995 		isci_remote_device_deconstruct(
1996 			isci_host,
1997 			isci_device
1998 			);
1999 		spin_unlock_irq(&isci_host->scic_lock);
2000 		return -ENODEV;
2001 	}
2002 
2003 	/* wait for the device ready callback. */
2004 	wait_for_device_start(isci_host, isci_device);
2005 
2006 	return 0;
2007 }
2008 /**
2009  * isci_device_is_reset_pending() - This function will check if there is any
2010  *    pending reset condition on the device.
2011  * @request: This parameter is the isci_device object.
2012  *
2013  * true if there is a reset pending for the device.
2014  */
2015 bool isci_device_is_reset_pending(
2016 	struct isci_host *isci_host,
2017 	struct isci_remote_device *isci_device)
2018 {
2019 	struct isci_request *isci_request;
2020 	struct isci_request *tmp_req;
2021 	bool reset_is_pending = false;
2022 	unsigned long flags;
2023 
2024 	dev_dbg(&isci_host->pdev->dev,
2025 		"%s: isci_device = %p\n", __func__, isci_device);
2026 
2027 	spin_lock_irqsave(&isci_host->scic_lock, flags);
2028 
2029 	/* Check for reset on all pending requests. */
2030 	list_for_each_entry_safe(isci_request, tmp_req,
2031 				 &isci_device->reqs_in_process, dev_node) {
2032 		dev_dbg(&isci_host->pdev->dev,
2033 			"%s: isci_device = %p request = %p\n",
2034 			__func__, isci_device, isci_request);
2035 
2036 		if (isci_request->ttype == io_task) {
2037 			struct sas_task *task = isci_request_access_task(
2038 				isci_request);
2039 
2040 			spin_lock(&task->task_state_lock);
2041 			if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2042 				reset_is_pending = true;
2043 			spin_unlock(&task->task_state_lock);
2044 		}
2045 	}
2046 
2047 	spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2048 
2049 	dev_dbg(&isci_host->pdev->dev,
2050 		"%s: isci_device = %p reset_is_pending = %d\n",
2051 		__func__, isci_device, reset_is_pending);
2052 
2053 	return reset_is_pending;
2054 }
2055 
2056 /**
2057  * isci_device_clear_reset_pending() - This function will clear if any pending
2058  *    reset condition flags on the device.
2059  * @request: This parameter is the isci_device object.
2060  *
2061  * true if there is a reset pending for the device.
2062  */
2063 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
2064 {
2065 	struct isci_request *isci_request;
2066 	struct isci_request *tmp_req;
2067 	unsigned long flags = 0;
2068 
2069 	dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2070 		 __func__, idev, ihost);
2071 
2072 	spin_lock_irqsave(&ihost->scic_lock, flags);
2073 
2074 	/* Clear reset pending on all pending requests. */
2075 	list_for_each_entry_safe(isci_request, tmp_req,
2076 				 &idev->reqs_in_process, dev_node) {
2077 		dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2078 			 __func__, idev, isci_request);
2079 
2080 		if (isci_request->ttype == io_task) {
2081 
2082 			unsigned long flags2;
2083 			struct sas_task *task = isci_request_access_task(
2084 				isci_request);
2085 
2086 			spin_lock_irqsave(&task->task_state_lock, flags2);
2087 			task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2088 			spin_unlock_irqrestore(&task->task_state_lock, flags2);
2089 		}
2090 	}
2091 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
2092 }
2093