blob: 5ba3b5dca4d5b594b1a4cb0483c1a99fabaf8a95 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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 */
Dan Williams88f3b622011-04-22 19:18:03 -070055#include "intel_sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070056#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "port.h"
58#include "remote_device.h"
59#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070060#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"
Dan Williams6f231dd2011-07-02 22:56:22 -070072#include "task.h"
73
Dan Williams88f3b622011-04-22 19:18:03 -070074enum 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}
Dan Williams6f231dd2011-07-02 22:56:22 -070080
81
Dan Williams88f3b622011-04-22 19:18:03 -070082enum 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
89enum 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
96enum 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
Dan Williams88f3b622011-04-22 19:18:03 -0700103#if !defined(DISABLE_ATAPI)
104bool scic_remote_device_is_atapi(struct scic_sds_remote_device *sci_dev)
105{
106 return sci_dev->is_atapi;
107}
108#endif
109
110
111/**
112 *
113 *
114 * Remote device timer requirements
115 */
116#define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
117#define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
118
119
120/**
121 *
122 * @sci_dev: The remote device for which the suspend is being requested.
123 *
124 * This method invokes the remote device suspend state handler. enum sci_status
125 */
126enum sci_status scic_sds_remote_device_suspend(
127 struct scic_sds_remote_device *sci_dev,
128 u32 suspend_type)
129{
130 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
131}
132
133/**
134 *
135 * @sci_dev: The remote device for which the event handling is being
136 * requested.
137 * @frame_index: This is the frame index that is being processed.
138 *
139 * This method invokes the frame handler for the remote device state machine
140 * enum sci_status
141 */
142enum sci_status scic_sds_remote_device_frame_handler(
143 struct scic_sds_remote_device *sci_dev,
144 u32 frame_index)
145{
146 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
147}
148
149/**
150 *
151 * @sci_dev: The remote device for which the event handling is being
152 * requested.
153 * @event_code: This is the event code that is to be processed.
154 *
155 * This method invokes the remote device event handler. enum sci_status
156 */
157enum sci_status scic_sds_remote_device_event_handler(
158 struct scic_sds_remote_device *sci_dev,
159 u32 event_code)
160{
161 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
162}
163
164/**
165 *
166 * @controller: The controller that is starting the io request.
167 * @sci_dev: The remote device for which the start io handling is being
168 * requested.
169 * @io_request: The io request that is being started.
170 *
171 * This method invokes the remote device start io handler. enum sci_status
172 */
173enum sci_status scic_sds_remote_device_start_io(
174 struct scic_sds_controller *controller,
175 struct scic_sds_remote_device *sci_dev,
176 struct scic_sds_request *io_request)
177{
178 return sci_dev->state_handlers->start_io_handler(
179 sci_dev, io_request);
180}
181
182/**
183 *
184 * @controller: The controller that is completing the io request.
185 * @sci_dev: The remote device for which the complete io handling is being
186 * requested.
187 * @io_request: The io request that is being completed.
188 *
189 * This method invokes the remote device complete io handler. enum sci_status
190 */
191enum sci_status scic_sds_remote_device_complete_io(
192 struct scic_sds_controller *controller,
193 struct scic_sds_remote_device *sci_dev,
194 struct scic_sds_request *io_request)
195{
196 return sci_dev->state_handlers->complete_io_handler(
197 sci_dev, io_request);
198}
199
200/**
201 *
202 * @controller: The controller that is starting the task request.
203 * @sci_dev: The remote device for which the start task handling is being
204 * requested.
205 * @io_request: The task request that is being started.
206 *
207 * This method invokes the remote device start task handler. enum sci_status
208 */
209enum sci_status scic_sds_remote_device_start_task(
210 struct scic_sds_controller *controller,
211 struct scic_sds_remote_device *sci_dev,
212 struct scic_sds_request *io_request)
213{
214 return sci_dev->state_handlers->start_task_handler(
215 sci_dev, io_request);
216}
217
218/**
219 *
220 * @controller: The controller that is completing the task request.
221 * @sci_dev: The remote device for which the complete task handling is
222 * being requested.
223 * @io_request: The task request that is being completed.
224 *
225 * This method invokes the remote device complete task handler. enum sci_status
226 */
227
228/**
229 *
230 * @sci_dev:
231 * @request:
232 *
233 * This method takes the request and bulids an appropriate SCU context for the
234 * request and then requests the controller to post the request. none
235 */
236void scic_sds_remote_device_post_request(
237 struct scic_sds_remote_device *sci_dev,
238 u32 request)
239{
240 u32 context;
241
242 context = scic_sds_remote_device_build_command_context(sci_dev, request);
243
244 scic_sds_controller_post_request(
245 scic_sds_remote_device_get_controller(sci_dev),
246 context
247 );
248}
249
250#if !defined(DISABLE_ATAPI)
251/**
252 *
253 * @sci_dev: The device to be checked.
254 *
255 * This method check the signature fis of a stp device to decide whether a
256 * device is atapi or not. true if a device is atapi device. False if a device
257 * is not atapi.
258 */
259bool scic_sds_remote_device_is_atapi(
260 struct scic_sds_remote_device *sci_dev)
261{
262 if (!sci_dev->target_protocols.u.bits.attached_stp_target)
263 return false;
264 else if (sci_dev->is_direct_attached) {
265 struct scic_sds_phy *phy;
266 struct scic_sata_phy_properties properties;
267 struct sata_fis_reg_d2h *signature_fis;
268 phy = scic_sds_port_get_a_connected_phy(sci_dev->owning_port);
269 scic_sata_phy_get_properties(phy, &properties);
270
271 /* decode the signature fis. */
272 signature_fis = &(properties.signature_fis);
273
274 if ((signature_fis->sector_count == 0x01)
275 && (signature_fis->lba_low == 0x01)
276 && (signature_fis->lba_mid == 0x14)
277 && (signature_fis->lba_high == 0xEB)
278 && ((signature_fis->device & 0x5F) == 0x00)
279 ) {
280 /* An ATA device supporting the PACKET command set. */
281 return true;
282 } else
283 return false;
284 } else {
285 /* Expander supported ATAPI device is not currently supported. */
286 return false;
287 }
288}
289#endif
290
291/**
292 *
293 * @user_parameter: This is cast to a remote device object.
294 *
295 * This method is called once the remote node context is ready to be freed.
296 * The remote device can now report that its stop operation is complete. none
297 */
298static void scic_sds_cb_remote_device_rnc_destruct_complete(
299 void *user_parameter)
300{
301 struct scic_sds_remote_device *sci_dev;
302
303 sci_dev = (struct scic_sds_remote_device *)user_parameter;
304
305 BUG_ON(sci_dev->started_request_count != 0);
306
307 sci_base_state_machine_change_state(&sci_dev->state_machine,
308 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
309}
310
311/**
312 *
313 * @user_parameter: This is cast to a remote device object.
314 *
315 * This method is called once the remote node context has transisitioned to a
316 * ready state. This is the indication that the remote device object can also
317 * transition to ready. none
318 */
319static void scic_sds_remote_device_resume_complete_handler(
320 void *user_parameter)
321{
322 struct scic_sds_remote_device *sci_dev;
323
324 sci_dev = (struct scic_sds_remote_device *)user_parameter;
325
326 if (
327 sci_base_state_machine_get_state(&sci_dev->state_machine)
328 != SCI_BASE_REMOTE_DEVICE_STATE_READY
329 ) {
330 sci_base_state_machine_change_state(
331 &sci_dev->state_machine,
332 SCI_BASE_REMOTE_DEVICE_STATE_READY
333 );
334 }
335}
336
337/**
338 *
339 * @device: This parameter specifies the device for which the request is being
340 * started.
341 * @request: This parameter specifies the request being started.
342 * @status: This parameter specifies the current start operation status.
343 *
344 * This method will perform the STP request start processing common to IO
345 * requests and task requests of all types. none
346 */
347void scic_sds_remote_device_start_request(
348 struct scic_sds_remote_device *sci_dev,
349 struct scic_sds_request *sci_req,
350 enum sci_status status)
351{
352 /* We still have a fault in starting the io complete it on the port */
353 if (status == SCI_SUCCESS)
354 scic_sds_remote_device_increment_request_count(sci_dev);
355 else{
356 sci_dev->owning_port->state_handlers->complete_io_handler(
357 sci_dev->owning_port, sci_dev, sci_req
358 );
359 }
360}
361
362
363/**
364 *
365 * @request: This parameter specifies the request being continued.
366 *
367 * This method will continue to post tc for a STP request. This method usually
368 * serves as a callback when RNC gets resumed during a task management
369 * sequence. none
370 */
371void scic_sds_remote_device_continue_request(void *dev)
372{
373 struct scic_sds_remote_device *sci_dev = dev;
374
375 /* we need to check if this request is still valid to continue. */
376 if (sci_dev->working_request)
377 scic_controller_continue_io(sci_dev->working_request);
378}
379
380/**
381 * This method will terminate all of the IO requests in the controllers IO
382 * request table that were targeted for this device.
383 * @sci_dev: This parameter specifies the remote device for which to
384 * attempt to terminate all requests.
385 *
386 * This method returns an indication as to whether all requests were
387 * successfully terminated. If a single request fails to be terminated, then
388 * this method will return the failure.
389 */
390static enum sci_status scic_sds_remote_device_terminate_requests(
391 struct scic_sds_remote_device *sci_dev)
392{
393 enum sci_status status = SCI_SUCCESS;
394 enum sci_status terminate_status = SCI_SUCCESS;
395 struct scic_sds_request *sci_req;
396 u32 index;
397 u32 request_count = sci_dev->started_request_count;
398
399 for (index = 0;
400 (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
401 index++) {
402 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
403
404 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
405 terminate_status = scic_controller_terminate_request(
406 sci_dev->owning_port->owning_controller,
407 sci_dev,
408 sci_req
409 );
410
411 if (terminate_status != SCI_SUCCESS)
412 status = terminate_status;
413
414 request_count--;
415 }
416 }
417
418 return status;
419}
420
421static enum sci_status
422default_device_handler(struct scic_sds_remote_device *sci_dev,
423 const char *func)
424{
425 dev_warn(scirdev_to_dev(sci_dev),
426 "%s: in wrong state: %d\n", func,
427 sci_base_state_machine_get_state(&sci_dev->state_machine));
428 return SCI_FAILURE_INVALID_STATE;
429}
430
431enum sci_status scic_sds_remote_device_default_start_handler(
432 struct scic_sds_remote_device *sci_dev)
433{
434 return default_device_handler(sci_dev, __func__);
435}
436
437static enum sci_status scic_sds_remote_device_default_stop_handler(
438 struct scic_sds_remote_device *sci_dev)
439{
440 return default_device_handler(sci_dev, __func__);
441}
442
443enum sci_status scic_sds_remote_device_default_fail_handler(
444 struct scic_sds_remote_device *sci_dev)
445{
446 return default_device_handler(sci_dev, __func__);
447}
448
449enum sci_status scic_sds_remote_device_default_destruct_handler(
450 struct scic_sds_remote_device *sci_dev)
451{
452 return default_device_handler(sci_dev, __func__);
453}
454
455enum sci_status scic_sds_remote_device_default_reset_handler(
456 struct scic_sds_remote_device *sci_dev)
457{
458 return default_device_handler(sci_dev, __func__);
459}
460
461enum sci_status scic_sds_remote_device_default_reset_complete_handler(
462 struct scic_sds_remote_device *sci_dev)
463{
464 return default_device_handler(sci_dev, __func__);
465}
466
467enum sci_status scic_sds_remote_device_default_suspend_handler(
468 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
469{
470 return default_device_handler(sci_dev, __func__);
471}
472
473enum sci_status scic_sds_remote_device_default_resume_handler(
474 struct scic_sds_remote_device *sci_dev)
475{
476 return default_device_handler(sci_dev, __func__);
477}
478
479/**
480 *
481 * @device: The struct scic_sds_remote_device which is then cast into a
482 * struct scic_sds_remote_device.
483 * @event_code: The event code that the struct scic_sds_controller wants the device
484 * object to process.
485 *
486 * This method is the default event handler. It will call the RNC state
487 * machine handler for any RNC events otherwise it will log a warning and
488 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
489 */
490static enum sci_status scic_sds_remote_device_core_event_handler(
491 struct scic_sds_remote_device *sci_dev,
492 u32 event_code,
493 bool is_ready_state)
494{
495 enum sci_status status;
496
497 switch (scu_get_event_type(event_code)) {
498 case SCU_EVENT_TYPE_RNC_OPS_MISC:
499 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
500 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
501 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
502 break;
503 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
504
505 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
506 status = SCI_SUCCESS;
507
508 /* Suspend the associated RNC */
509 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
510 SCI_SOFTWARE_SUSPENSION,
511 NULL, NULL);
512
513 dev_dbg(scirdev_to_dev(sci_dev),
514 "%s: device: %p event code: %x: %s\n",
515 __func__, sci_dev, event_code,
516 (is_ready_state)
517 ? "I_T_Nexus_Timeout event"
518 : "I_T_Nexus_Timeout event in wrong state");
519
520 break;
521 }
522 /* Else, fall through and treat as unhandled... */
523
524 default:
525 dev_dbg(scirdev_to_dev(sci_dev),
526 "%s: device: %p event code: %x: %s\n",
527 __func__, sci_dev, event_code,
528 (is_ready_state)
529 ? "unexpected event"
530 : "unexpected event in wrong state");
531 status = SCI_FAILURE_INVALID_STATE;
532 break;
533 }
534
535 return status;
536}
537/**
538 *
539 * @device: The struct scic_sds_remote_device which is then cast into a
540 * struct scic_sds_remote_device.
541 * @event_code: The event code that the struct scic_sds_controller wants the device
542 * object to process.
543 *
544 * This method is the default event handler. It will call the RNC state
545 * machine handler for any RNC events otherwise it will log a warning and
546 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
547 */
548static enum sci_status scic_sds_remote_device_default_event_handler(
549 struct scic_sds_remote_device *sci_dev,
550 u32 event_code)
551{
552 return scic_sds_remote_device_core_event_handler(sci_dev,
553 event_code,
554 false);
555}
556
557/**
558 *
559 * @device: The struct scic_sds_remote_device which is then cast into a
560 * struct scic_sds_remote_device.
561 * @frame_index: The frame index for which the struct scic_sds_controller wants this
562 * device object to process.
563 *
564 * This method is the default unsolicited frame handler. It logs a warning,
565 * releases the frame and returns a failure. enum sci_status
566 * SCI_FAILURE_INVALID_STATE
567 */
568enum sci_status scic_sds_remote_device_default_frame_handler(
569 struct scic_sds_remote_device *sci_dev,
570 u32 frame_index)
571{
572 dev_warn(scirdev_to_dev(sci_dev),
573 "%s: SCIC Remote Device requested to handle frame %x "
574 "while in wrong state %d\n",
575 __func__,
576 frame_index,
577 sci_base_state_machine_get_state(
578 &sci_dev->state_machine));
579
580 /* Return the frame back to the controller */
581 scic_sds_controller_release_frame(
582 scic_sds_remote_device_get_controller(sci_dev), frame_index
583 );
584
585 return SCI_FAILURE_INVALID_STATE;
586}
587
588enum sci_status scic_sds_remote_device_default_start_request_handler(
589 struct scic_sds_remote_device *sci_dev,
590 struct scic_sds_request *request)
591{
592 return default_device_handler(sci_dev, __func__);
593}
594
595enum sci_status scic_sds_remote_device_default_complete_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
602enum sci_status scic_sds_remote_device_default_continue_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/**
610 *
611 * @device: The struct scic_sds_remote_device which is then cast into a
612 * struct scic_sds_remote_device.
613 * @frame_index: The frame index for which the struct scic_sds_controller wants this
614 * device object to process.
615 *
616 * This method is a general ssp frame handler. In most cases the device object
617 * needs to route the unsolicited frame processing to the io request object.
618 * This method decodes the tag for the io request object and routes the
619 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
620 */
621enum sci_status scic_sds_remote_device_general_frame_handler(
622 struct scic_sds_remote_device *sci_dev,
623 u32 frame_index)
624{
625 enum sci_status result;
626 struct sci_ssp_frame_header *frame_header;
627 struct scic_sds_request *io_request;
628
629 result = scic_sds_unsolicited_frame_control_get_header(
630 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
631 frame_index,
632 (void **)&frame_header
633 );
634
635 if (SCI_SUCCESS == result) {
636 io_request = scic_sds_controller_get_io_request_from_tag(
637 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
638
639 if ((io_request == NULL)
640 || (io_request->target_device != sci_dev)) {
641 /*
642 * We could not map this tag to a valid IO request
643 * Just toss the frame and continue */
644 scic_sds_controller_release_frame(
645 scic_sds_remote_device_get_controller(sci_dev), frame_index
646 );
647 } else {
648 /* The IO request is now in charge of releasing the frame */
649 result = io_request->state_handlers->frame_handler(
650 io_request, frame_index);
651 }
652 }
653
654 return result;
655}
656
657/**
658 *
659 * @[in]: sci_dev This is the device object that is receiving the event.
660 * @[in]: event_code The event code to process.
661 *
662 * This is a common method for handling events reported to the remote device
663 * from the controller object. enum sci_status
664 */
665enum sci_status scic_sds_remote_device_general_event_handler(
666 struct scic_sds_remote_device *sci_dev,
667 u32 event_code)
668{
669 return scic_sds_remote_device_core_event_handler(sci_dev,
670 event_code,
671 true);
672}
673
674/*
675 * *****************************************************************************
676 * * STOPPED STATE HANDLERS
677 * ***************************************************************************** */
678
679/**
680 *
681 * @device:
682 *
683 * This method takes the struct scic_sds_remote_device from a stopped state and
684 * attempts to start it. The RNC buffer for the device is constructed and the
685 * device state machine is transitioned to the
686 * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
687 * an RNC buffer available to construct the remote device.
688 * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
689 * which to construct the remote device.
690 */
691static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
692 struct scic_sds_remote_device *sci_dev)
693{
694 enum sci_status status;
695
696 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
697 scic_sds_remote_device_resume_complete_handler, sci_dev);
698
699 if (status == SCI_SUCCESS)
700 sci_base_state_machine_change_state(&sci_dev->state_machine,
701 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
702
703 return status;
704}
705
706static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
707 struct scic_sds_remote_device *sci_dev)
708{
709 return SCI_SUCCESS;
710}
711
712/**
713 *
714 * @sci_dev: The struct scic_sds_remote_device which is cast into a
715 * struct scic_sds_remote_device.
716 *
717 * This method will destruct a struct scic_sds_remote_device that is in a stopped
718 * state. This is the only state from which a destruct request will succeed.
719 * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
720 * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
721 * enum sci_status SCI_SUCCESS
722 */
723static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
724 struct scic_sds_remote_device *sci_dev)
725{
726 struct scic_sds_controller *scic;
727
728 scic = scic_sds_remote_device_get_controller(sci_dev);
729 scic_sds_controller_free_remote_node_context(scic, sci_dev,
730 sci_dev->rnc.remote_node_index);
731 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
732
733 sci_base_state_machine_change_state(&sci_dev->state_machine,
734 SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
735
736 return SCI_SUCCESS;
737}
738
739/*
740 * *****************************************************************************
741 * * STARTING STATE HANDLERS
742 * ***************************************************************************** */
743
744static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
745 struct scic_sds_remote_device *sci_dev)
746{
747 /*
748 * This device has not yet started so there had better be no IO requests
749 */
750 BUG_ON(sci_dev->started_request_count != 0);
751
752 /*
753 * Destroy the remote node context
754 */
755 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
756 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
757
758 /*
759 * Transition to the stopping state and wait for the remote node to
760 * complete being posted and invalidated.
761 */
762 sci_base_state_machine_change_state(&sci_dev->state_machine,
763 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
764
765 return SCI_SUCCESS;
766}
767
768enum sci_status scic_sds_remote_device_ready_state_stop_handler(
769 struct scic_sds_remote_device *sci_dev)
770{
771 enum sci_status status = SCI_SUCCESS;
772
773 /* Request the parent state machine to transition to the stopping state */
774 sci_base_state_machine_change_state(&sci_dev->state_machine,
775 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
776
777 if (sci_dev->started_request_count == 0) {
778 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
779 scic_sds_cb_remote_device_rnc_destruct_complete,
780 sci_dev);
781 } else
782 status = scic_sds_remote_device_terminate_requests(sci_dev);
783
784 return status;
785}
786
787/**
788 *
789 * @device: The struct scic_sds_remote_device object which is cast to a
790 * struct scic_sds_remote_device object.
791 *
792 * This is the ready state device reset handler enum sci_status
793 */
794enum sci_status scic_sds_remote_device_ready_state_reset_handler(
795 struct scic_sds_remote_device *sci_dev)
796{
797 /* Request the parent state machine to transition to the stopping state */
798 sci_base_state_machine_change_state(&sci_dev->state_machine,
799 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
800
801 return SCI_SUCCESS;
802}
803
804/*
805 * This method will attempt to start a task request for this device object. The
806 * remote device object will issue the start request for the task and if
807 * successful it will start the request for the port object then increment its
808 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
809 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
810 * object could not get the resources to start.
811 */
812static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
813 struct scic_sds_remote_device *sci_dev,
814 struct scic_sds_request *request)
815{
816 enum sci_status result;
817
818 /* See if the port is in a state where we can start the IO request */
819 result = scic_sds_port_start_io(
820 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
821
822 if (result == SCI_SUCCESS) {
823 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
824 request);
825 if (result == SCI_SUCCESS)
826 result = scic_sds_request_start(request);
827
828 scic_sds_remote_device_start_request(sci_dev, request, result);
829 }
830
831 return result;
832}
833
834/*
835 * This method will attempt to start an io request for this device object. The
836 * remote device object will issue the start request for the io and if
837 * successful it will start the request for the port object then increment its
838 * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
839 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
840 * object could not get the resources to start.
841 */
842static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
843 struct scic_sds_remote_device *sci_dev,
844 struct scic_sds_request *request)
845{
846 enum sci_status result;
847
848 /* See if the port is in a state where we can start the IO request */
849 result = scic_sds_port_start_io(
850 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
851
852 if (result == SCI_SUCCESS) {
853 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
854 if (result == SCI_SUCCESS)
855 result = scic_sds_request_start(request);
856
857 scic_sds_remote_device_start_request(sci_dev, request, result);
858 }
859
860 return result;
861}
862
863/*
864 * This method will complete the request for the remote device object. The
865 * method will call the completion handler for the request object and if
866 * successful it will complete the request on the port object then decrement
867 * its own started_request_count. enum sci_status
868 */
869static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
870 struct scic_sds_remote_device *sci_dev,
871 struct scic_sds_request *request)
872{
873 enum sci_status result;
874
875 result = scic_sds_request_complete(request);
876
877 if (result != SCI_SUCCESS)
878 return result;
879
880 /* See if the port is in a state
881 * where we can start the IO request */
882 result = scic_sds_port_complete_io(
883 scic_sds_remote_device_get_port(sci_dev),
884 sci_dev, request);
885
886 if (result == SCI_SUCCESS)
887 scic_sds_remote_device_decrement_request_count(sci_dev);
888
889 return result;
890}
891
892/*
893 * *****************************************************************************
894 * * STOPPING STATE HANDLERS
895 * ***************************************************************************** */
896
897/**
898 *
899 * @sci_dev: The struct scic_sds_remote_device which is cast into a
900 * struct scic_sds_remote_device.
901 *
902 * This method will stop a struct scic_sds_remote_device that is already in the
903 * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
904 * since we allow a stop request on a device that is alreay stopping or
905 * stopped. enum sci_status SCI_SUCCESS
906 */
907static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
908 struct scic_sds_remote_device *device)
909{
910 /*
911 * All requests should have been terminated, but if there is an
912 * attempt to stop a device already in the stopping state, then
913 * try again to terminate. */
914 return scic_sds_remote_device_terminate_requests(device);
915}
916
917
918/**
919 *
920 * @device: The device object for which the request is completing.
921 * @request: The task request that is being completed.
922 *
923 * This method completes requests for this struct scic_sds_remote_device while it is
924 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
925 * complete method for the request object and if that is successful the port
926 * object is called to complete the task request. Then the device object itself
927 * completes the task request. If struct scic_sds_remote_device started_request_count
928 * goes to 0 and the invalidate RNC request has completed the device object can
929 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
930 */
931static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
932 struct scic_sds_remote_device *sci_dev,
933 struct scic_sds_request *request)
934{
935 enum sci_status status = SCI_SUCCESS;
936
937 status = scic_sds_request_complete(request);
938
939 if (status != SCI_SUCCESS)
940 return status;
941
942 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
943 sci_dev, request);
944 if (status != SCI_SUCCESS)
945 return status;
946
947 scic_sds_remote_device_decrement_request_count(sci_dev);
948
949 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
950 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
951 scic_sds_cb_remote_device_rnc_destruct_complete,
952 sci_dev);
953 return SCI_SUCCESS;
954}
955
956/**
957 *
958 * @device: The struct scic_sds_remote_device which is to be cast into a
959 * struct scic_sds_remote_device object.
960 *
961 * This method will complete the reset operation when the device is in the
962 * resetting state. enum sci_status
963 */
964static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
965 struct scic_sds_remote_device *sci_dev)
966{
967
968 sci_base_state_machine_change_state(
969 &sci_dev->state_machine,
970 SCI_BASE_REMOTE_DEVICE_STATE_READY
971 );
972
973 return SCI_SUCCESS;
974}
975
976/**
977 *
978 * @device: The struct scic_sds_remote_device which is to be cast into a
979 * struct scic_sds_remote_device object.
980 *
981 * This method will stop the remote device while in the resetting state.
982 * enum sci_status
983 */
984static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
985 struct scic_sds_remote_device *sci_dev)
986{
987 sci_base_state_machine_change_state(
988 &sci_dev->state_machine,
989 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
990 );
991
992 return SCI_SUCCESS;
993}
994
995/*
996 * This method completes requests for this struct scic_sds_remote_device while it is
997 * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
998 * complete method for the request object and if that is successful the port
999 * object is called to complete the task request. Then the device object itself
1000 * completes the task request. enum sci_status
1001 */
1002static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
1003 struct scic_sds_remote_device *sci_dev,
1004 struct scic_sds_request *request)
1005{
1006 enum sci_status status = SCI_SUCCESS;
1007
1008 status = scic_sds_request_complete(request);
1009
1010 if (status == SCI_SUCCESS) {
1011 status = scic_sds_port_complete_io(
1012 scic_sds_remote_device_get_port(sci_dev),
1013 sci_dev, request);
1014
1015 if (status == SCI_SUCCESS) {
1016 scic_sds_remote_device_decrement_request_count(sci_dev);
1017 }
1018 }
1019
1020 return status;
1021}
1022
1023static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1024 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1025 .start_handler = scic_sds_remote_device_default_start_handler,
1026 .stop_handler = scic_sds_remote_device_default_stop_handler,
1027 .fail_handler = scic_sds_remote_device_default_fail_handler,
1028 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1029 .reset_handler = scic_sds_remote_device_default_reset_handler,
1030 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1031 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1032 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1033 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1034 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1035 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1036 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1037 .resume_handler = scic_sds_remote_device_default_resume_handler,
1038 .event_handler = scic_sds_remote_device_default_event_handler,
1039 .frame_handler = scic_sds_remote_device_default_frame_handler
1040 },
1041 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1042 .start_handler = scic_sds_remote_device_stopped_state_start_handler,
1043 .stop_handler = scic_sds_remote_device_stopped_state_stop_handler,
1044 .fail_handler = scic_sds_remote_device_default_fail_handler,
1045 .destruct_handler = scic_sds_remote_device_stopped_state_destruct_handler,
1046 .reset_handler = scic_sds_remote_device_default_reset_handler,
1047 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1048 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1049 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1050 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1051 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1052 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1053 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1054 .resume_handler = scic_sds_remote_device_default_resume_handler,
1055 .event_handler = scic_sds_remote_device_default_event_handler,
1056 .frame_handler = scic_sds_remote_device_default_frame_handler
1057 },
1058 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1059 .start_handler = scic_sds_remote_device_default_start_handler,
1060 .stop_handler = scic_sds_remote_device_starting_state_stop_handler,
1061 .fail_handler = scic_sds_remote_device_default_fail_handler,
1062 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1063 .reset_handler = scic_sds_remote_device_default_reset_handler,
1064 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1065 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1066 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1067 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1068 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1069 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1070 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1071 .resume_handler = scic_sds_remote_device_default_resume_handler,
1072 .event_handler = scic_sds_remote_device_general_event_handler,
1073 .frame_handler = scic_sds_remote_device_default_frame_handler
1074 },
1075 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1076 .start_handler = scic_sds_remote_device_default_start_handler,
1077 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1078 .fail_handler = scic_sds_remote_device_default_fail_handler,
1079 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1080 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1081 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1082 .start_io_handler = scic_sds_remote_device_ready_state_start_io_handler,
1083 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1084 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1085 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1086 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1087 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1088 .resume_handler = scic_sds_remote_device_default_resume_handler,
1089 .event_handler = scic_sds_remote_device_general_event_handler,
1090 .frame_handler = scic_sds_remote_device_general_frame_handler,
1091 },
1092 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1093 .start_handler = scic_sds_remote_device_default_start_handler,
1094 .stop_handler = scic_sds_remote_device_stopping_state_stop_handler,
1095 .fail_handler = scic_sds_remote_device_default_fail_handler,
1096 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1097 .reset_handler = scic_sds_remote_device_default_reset_handler,
1098 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1099 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1100 .complete_io_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1101 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1102 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1103 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1104 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1105 .resume_handler = scic_sds_remote_device_default_resume_handler,
1106 .event_handler = scic_sds_remote_device_general_event_handler,
1107 .frame_handler = scic_sds_remote_device_general_frame_handler
1108 },
1109 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1110 .start_handler = scic_sds_remote_device_default_start_handler,
1111 .stop_handler = scic_sds_remote_device_default_stop_handler,
1112 .fail_handler = scic_sds_remote_device_default_fail_handler,
1113 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1114 .reset_handler = scic_sds_remote_device_default_reset_handler,
1115 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1116 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1117 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1118 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1119 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1120 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1121 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1122 .resume_handler = scic_sds_remote_device_default_resume_handler,
1123 .event_handler = scic_sds_remote_device_default_event_handler,
1124 .frame_handler = scic_sds_remote_device_general_frame_handler
1125 },
1126 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1127 .start_handler = scic_sds_remote_device_default_start_handler,
1128 .stop_handler = scic_sds_remote_device_resetting_state_stop_handler,
1129 .fail_handler = scic_sds_remote_device_default_fail_handler,
1130 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1131 .reset_handler = scic_sds_remote_device_default_reset_handler,
1132 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1133 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1134 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1135 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1136 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1137 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1138 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1139 .resume_handler = scic_sds_remote_device_default_resume_handler,
1140 .event_handler = scic_sds_remote_device_default_event_handler,
1141 .frame_handler = scic_sds_remote_device_general_frame_handler
1142 },
1143 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1144 .start_handler = scic_sds_remote_device_default_start_handler,
1145 .stop_handler = scic_sds_remote_device_default_stop_handler,
1146 .fail_handler = scic_sds_remote_device_default_fail_handler,
1147 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1148 .reset_handler = scic_sds_remote_device_default_reset_handler,
1149 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1150 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1151 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1152 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1153 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1154 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1155 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1156 .resume_handler = scic_sds_remote_device_default_resume_handler,
1157 .event_handler = scic_sds_remote_device_default_event_handler,
1158 .frame_handler = scic_sds_remote_device_default_frame_handler
1159 }
1160};
1161
1162static void scic_sds_remote_device_initial_state_enter(
1163 struct sci_base_object *object)
1164{
1165 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1166
1167 sci_dev = container_of(object, typeof(*sci_dev), parent);
1168 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1169 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1170
1171 /* Initial state is a transitional state to the stopped state */
1172 sci_base_state_machine_change_state(&sci_dev->state_machine,
1173 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1174}
1175
1176/**
1177 * isci_remote_device_change_state() - This function gets the status of the
1178 * remote_device object.
1179 * @isci_device: This parameter points to the isci_remote_device object
1180 *
1181 * status of the object as a isci_status enum.
1182 */
1183void isci_remote_device_change_state(
1184 struct isci_remote_device *isci_device,
1185 enum isci_status status)
1186{
1187 unsigned long flags;
1188
1189 spin_lock_irqsave(&isci_device->state_lock, flags);
1190 isci_device->status = status;
1191 spin_unlock_irqrestore(&isci_device->state_lock, flags);
1192}
1193
1194/**
1195 * scic_remote_device_destruct() - free remote node context and destruct
1196 * @remote_device: This parameter specifies the remote device to be destructed.
1197 *
1198 * Remote device objects are a limited resource. As such, they must be
1199 * protected. Thus calls to construct and destruct are mutually exclusive and
1200 * non-reentrant. The return value shall indicate if the device was
1201 * successfully destructed or if some failure occurred. enum sci_status This value
1202 * is returned if the device is successfully destructed.
1203 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1204 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1205 * valid, etc.).
1206 */
1207static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1208{
1209 return sci_dev->state_handlers->destruct_handler(sci_dev);
1210}
1211
Dan Williams6f231dd2011-07-02 22:56:22 -07001212/**
1213 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001214 * @ihost: This parameter specifies the isci host object.
1215 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001216 *
1217 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001218static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001219{
Dan Williamsd9c37392011-03-03 17:59:32 -08001220 dev_dbg(&ihost->pdev->dev,
1221 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001222
1223 /* There should not be any outstanding io's. All paths to
1224 * here should go through isci_remote_device_nuke_requests.
1225 * If we hit this condition, we will need a way to complete
1226 * io requests in process */
Dan Williamsd9c37392011-03-03 17:59:32 -08001227 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001228
Dan Williamsd9c37392011-03-03 17:59:32 -08001229 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001230 "%s: ** request list not empty! **\n", __func__);
1231 BUG();
1232 }
1233
Dan Williams57f20f42011-04-21 18:14:45 -07001234 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -08001235 idev->domain_dev->lldd_dev = NULL;
1236 idev->domain_dev = NULL;
1237 idev->isci_port = NULL;
1238 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001239
Dan Williamsd9c37392011-03-03 17:59:32 -08001240 clear_bit(IDEV_START_PENDING, &idev->flags);
1241 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1242 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001243}
1244
Dan Williams88f3b622011-04-22 19:18:03 -07001245/**
1246 * isci_remote_device_stop_complete() - This function is called by the scic
1247 * when the remote device stop has completed. We mark the isci device as not
1248 * ready and remove the isci remote device.
1249 * @ihost: This parameter specifies the isci host object.
1250 * @idev: This parameter specifies the remote device.
1251 * @status: This parameter specifies status of the completion.
1252 *
1253 */
1254static void isci_remote_device_stop_complete(struct isci_host *ihost,
1255 struct isci_remote_device *idev)
1256{
1257 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1258
1259 isci_remote_device_change_state(idev, isci_stopped);
1260
1261 /* after stop, we can tear down resources. */
1262 isci_remote_device_deconstruct(ihost, idev);
1263}
1264
1265static void scic_sds_remote_device_stopped_state_enter(
1266 struct sci_base_object *object)
1267{
1268 struct scic_sds_remote_device *sci_dev;
1269 struct scic_sds_controller *scic;
1270 struct isci_remote_device *idev;
1271 struct isci_host *ihost;
1272 u32 prev_state;
1273
1274 sci_dev = container_of(object, typeof(*sci_dev), parent);
1275 scic = scic_sds_remote_device_get_controller(sci_dev);
1276 ihost = sci_object_get_association(scic);
1277 idev = sci_object_get_association(sci_dev);
1278
1279 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1280 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1281
1282 /* If we are entering from the stopping state let the SCI User know that
1283 * the stop operation has completed.
1284 */
1285 prev_state = sci_dev->state_machine.previous_state_id;
1286 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1287 isci_remote_device_stop_complete(ihost, idev);
1288
1289 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1290}
1291
1292static void scic_sds_remote_device_starting_state_enter(struct sci_base_object *object)
1293{
1294 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1295 parent);
1296 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1297 struct isci_host *ihost = sci_object_get_association(scic);
1298 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1299
1300 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1301 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1302
1303 isci_remote_device_not_ready(ihost, idev,
1304 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1305}
1306
1307static void scic_sds_remote_device_ready_state_enter(struct sci_base_object *object)
1308{
1309 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1310 parent);
1311 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1312 struct isci_host *ihost = sci_object_get_association(scic);
1313 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1314
1315 SET_STATE_HANDLER(sci_dev,
1316 scic_sds_remote_device_state_handler_table,
1317 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1318
1319 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1320
1321 if (sci_dev->has_ready_substate_machine)
1322 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1323 else
1324 isci_remote_device_ready(ihost, idev);
1325}
1326
1327static void scic_sds_remote_device_ready_state_exit(
1328 struct sci_base_object *object)
1329{
1330 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1331 parent);
1332 if (sci_dev->has_ready_substate_machine)
1333 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1334 else {
1335 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1336 struct isci_host *ihost = sci_object_get_association(scic);
1337 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1338
1339 isci_remote_device_not_ready(ihost, idev,
1340 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1341 }
1342}
1343
1344static void scic_sds_remote_device_stopping_state_enter(
1345 struct sci_base_object *object)
1346{
1347 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1348
1349 SET_STATE_HANDLER(
1350 sci_dev,
1351 scic_sds_remote_device_state_handler_table,
1352 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1353 );
1354}
1355
1356static void scic_sds_remote_device_failed_state_enter(
1357 struct sci_base_object *object)
1358{
1359 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1360
1361 SET_STATE_HANDLER(
1362 sci_dev,
1363 scic_sds_remote_device_state_handler_table,
1364 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1365 );
1366}
1367
1368static void scic_sds_remote_device_resetting_state_enter(
1369 struct sci_base_object *object)
1370{
1371 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1372
1373 SET_STATE_HANDLER(
1374 sci_dev,
1375 scic_sds_remote_device_state_handler_table,
1376 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1377 );
1378
1379 scic_sds_remote_node_context_suspend(
1380 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1381}
1382
1383static void scic_sds_remote_device_resetting_state_exit(
1384 struct sci_base_object *object)
1385{
1386 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1387
1388 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1389}
1390
1391static void scic_sds_remote_device_final_state_enter(
1392 struct sci_base_object *object)
1393{
1394 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1395
1396 SET_STATE_HANDLER(
1397 sci_dev,
1398 scic_sds_remote_device_state_handler_table,
1399 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1400 );
1401}
1402
1403
1404static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1405 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1406 .enter_state = scic_sds_remote_device_initial_state_enter,
1407 },
1408 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1409 .enter_state = scic_sds_remote_device_stopped_state_enter,
1410 },
1411 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1412 .enter_state = scic_sds_remote_device_starting_state_enter,
1413 },
1414 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1415 .enter_state = scic_sds_remote_device_ready_state_enter,
1416 .exit_state = scic_sds_remote_device_ready_state_exit
1417 },
1418 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1419 .enter_state = scic_sds_remote_device_stopping_state_enter,
1420 },
1421 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1422 .enter_state = scic_sds_remote_device_failed_state_enter,
1423 },
1424 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1425 .enter_state = scic_sds_remote_device_resetting_state_enter,
1426 .exit_state = scic_sds_remote_device_resetting_state_exit
1427 },
1428 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1429 .enter_state = scic_sds_remote_device_final_state_enter,
1430 },
1431};
1432
1433/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001434 * scic_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001435 * @sci_port: SAS/SATA port through which this device is accessed.
1436 * @sci_dev: remote device to construct
1437 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001438 * This routine just performs benign initialization and does not
1439 * allocate the remote_node_context which is left to
1440 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1441 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001442 */
1443static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1444 struct scic_sds_remote_device *sci_dev)
1445{
1446 sci_dev->owning_port = sci_port;
1447 sci_dev->started_request_count = 0;
1448 sci_dev->parent.private = NULL;
1449
1450 sci_base_state_machine_construct(
1451 &sci_dev->state_machine,
1452 &sci_dev->parent,
1453 scic_sds_remote_device_state_table,
1454 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1455 );
1456
1457 sci_base_state_machine_start(
1458 &sci_dev->state_machine
1459 );
1460
1461 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1462 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1463
1464 sci_object_set_association(&sci_dev->rnc, sci_dev);
1465}
1466
1467/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001468 * scic_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001469 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001470 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1471 * the device is known to the SCI Core since it is contained in the
1472 * scic_phy object. Remote node context(s) is/are a global resource
1473 * allocated by this routine, freed by scic_remote_device_destruct().
1474 *
1475 * Returns:
1476 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1477 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1478 * sata-only controller instance.
1479 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001480 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001481static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1482 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001483{
1484 enum sci_status status;
1485 u16 remote_node_index;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001486 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001487
Dan Williamsb87ee302011-04-25 11:48:29 -07001488 scic_remote_device_construct(sci_port, sci_dev);
1489
Dan Williams88f3b622011-04-22 19:18:03 -07001490 /*
1491 * This information is request to determine how many remote node context
1492 * entries will be needed to store the remote node.
1493 */
Dan Williams88f3b622011-04-22 19:18:03 -07001494 sci_dev->is_direct_attached = true;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001495 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1496 sci_dev,
1497 &remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001498
Dan Williamsa1a113b2011-04-21 18:44:45 -07001499 if (status != SCI_SUCCESS)
1500 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001501
Dan Williamsa1a113b2011-04-21 18:44:45 -07001502 sci_dev->rnc.remote_node_index = remote_node_index;
Dan Williams88f3b622011-04-22 19:18:03 -07001503
Dan Williamsa1a113b2011-04-21 18:44:45 -07001504 scic_sds_port_get_attached_sas_address(sci_port, &sci_dev->device_address);
Dan Williams88f3b622011-04-22 19:18:03 -07001505
Dan Williamsa1a113b2011-04-21 18:44:45 -07001506 if (dev->dev_type == SAS_END_DEV)
1507 sci_dev->has_ready_substate_machine = false;
1508 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1509 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001510
Dan Williamsa1a113b2011-04-21 18:44:45 -07001511 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001512 &sci_dev->ready_substate_machine,
1513 &sci_dev->parent,
1514 scic_sds_stp_remote_device_ready_substate_table,
1515 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001516 } else if (dev_is_expander(dev)) {
1517 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001518
Dan Williamsa1a113b2011-04-21 18:44:45 -07001519 /* add the SMP ready substate machine construction here */
1520 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001521 &sci_dev->ready_substate_machine,
1522 &sci_dev->parent,
1523 scic_sds_smp_remote_device_ready_substate_table,
1524 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001525 } else
1526 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001527
Dan Williamsa1a113b2011-04-21 18:44:45 -07001528 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
Dan Williams88f3b622011-04-22 19:18:03 -07001529
Dan Williamsa1a113b2011-04-21 18:44:45 -07001530 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1531 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001532
Dan Williamsa1a113b2011-04-21 18:44:45 -07001533 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001534}
1535
Dan Williams88f3b622011-04-22 19:18:03 -07001536/**
Dan Williamsb87ee302011-04-25 11:48:29 -07001537 * scic_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001538 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001539 * Remote node context(s) is/are a global resource allocated by this
1540 * routine, freed by scic_remote_device_destruct().
1541 *
1542 * Returns:
1543 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1544 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1545 * sata-only controller instance.
1546 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001547 */
Dan Williamsb87ee302011-04-25 11:48:29 -07001548static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
Dan Williams00d680e2011-04-25 14:29:29 -07001549 struct scic_sds_remote_device *sci_dev)
Dan Williams88f3b622011-04-22 19:18:03 -07001550{
Dan Williamsb87ee302011-04-25 11:48:29 -07001551 struct scic_sds_controller *scic = sci_port->owning_controller;
Dan Williamsa1a113b2011-04-21 18:44:45 -07001552 struct domain_device *dev = sci_dev_to_domain(sci_dev);
Dan Williams88f3b622011-04-22 19:18:03 -07001553 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001554
Dan Williamsb87ee302011-04-25 11:48:29 -07001555 scic_remote_device_construct(sci_port, sci_dev);
Dan Williams00d680e2011-04-25 14:29:29 -07001556 memcpy(&sci_dev->device_address, dev->sas_addr, SAS_ADDR_SIZE);
Dan Williams88f3b622011-04-22 19:18:03 -07001557
1558 status = scic_sds_controller_allocate_remote_node_context(
1559 scic, sci_dev, &sci_dev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001560 if (status != SCI_SUCCESS)
1561 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001562
Dan Williamsa1a113b2011-04-21 18:44:45 -07001563 if (dev->dev_type == SAS_END_DEV)
1564 sci_dev->has_ready_substate_machine = false;
1565 else if (dev_is_expander(dev)) {
1566 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001567
Dan Williamsa1a113b2011-04-21 18:44:45 -07001568 /* add the SMP ready substate machine construction here */
1569 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001570 &sci_dev->ready_substate_machine,
1571 &sci_dev->parent,
1572 scic_sds_smp_remote_device_ready_substate_table,
1573 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001574 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1575 sci_dev->has_ready_substate_machine = true;
Dan Williams88f3b622011-04-22 19:18:03 -07001576
Dan Williamsa1a113b2011-04-21 18:44:45 -07001577 sci_base_state_machine_construct(
Dan Williams88f3b622011-04-22 19:18:03 -07001578 &sci_dev->ready_substate_machine,
1579 &sci_dev->parent,
1580 scic_sds_stp_remote_device_ready_substate_table,
1581 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001582 }
Dan Williams88f3b622011-04-22 19:18:03 -07001583
Dan Williamsa1a113b2011-04-21 18:44:45 -07001584 /*
1585 * For SAS-2 the physical link rate is actually a logical link
1586 * rate that incorporates multiplexing. The SCU doesn't
1587 * incorporate multiplexing and for the purposes of the
1588 * connection the logical link rate is that same as the
1589 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1590 * one another, so this code works for both situations. */
1591 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
Dan Williams00d680e2011-04-25 14:29:29 -07001592 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001593
Dan Williamsa1a113b2011-04-21 18:44:45 -07001594 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1595 sci_dev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001596
1597 return status;
1598}
1599
1600/**
1601 * scic_remote_device_start() - This method will start the supplied remote
1602 * device. This method enables normal IO requests to flow through to the
1603 * remote device.
1604 * @remote_device: This parameter specifies the device to be started.
1605 * @timeout: This parameter specifies the number of milliseconds in which the
1606 * start operation should complete.
1607 *
1608 * An indication of whether the device was successfully started. SCI_SUCCESS
1609 * This value is returned if the device was successfully started.
1610 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1611 * the device when there have been no phys added to it.
1612 */
1613static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1614 u32 timeout)
1615{
1616 return sci_dev->state_handlers->start_handler(sci_dev);
1617}
Dan Williams6f231dd2011-07-02 22:56:22 -07001618
Dan Williams00d680e2011-04-25 14:29:29 -07001619static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1620 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001621{
Dan Williams00d680e2011-04-25 14:29:29 -07001622 struct scic_sds_port *sci_port = iport->sci_port_handle;
1623 struct isci_host *ihost = iport->isci_host;
1624 struct domain_device *dev = idev->domain_dev;
1625 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001626
Dan Williams00d680e2011-04-25 14:29:29 -07001627 if (dev->parent && dev_is_expander(dev->parent))
1628 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1629 else
1630 status = scic_remote_device_da_construct(sci_port, &idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001631
1632 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001633 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1634 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001635
1636 return status;
1637 }
1638
Dan Williams57f20f42011-04-21 18:14:45 -07001639 /* XXX will be killed with sci_base_object removal */
Dan Williams00d680e2011-04-25 14:29:29 -07001640 sci_object_set_association(&idev->sci, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001641
Dan Williams6f231dd2011-07-02 22:56:22 -07001642 /* start the device. */
Dan Williams00d680e2011-04-25 14:29:29 -07001643 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001644
Dan Williams00d680e2011-04-25 14:29:29 -07001645 if (status != SCI_SUCCESS)
1646 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1647 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001648
1649 return status;
1650}
1651
Dan Williams4393aa42011-03-31 13:10:44 -07001652void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001653{
1654 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001655
Dan Williams4393aa42011-03-31 13:10:44 -07001656 dev_dbg(&ihost->pdev->dev,
1657 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001658
1659 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -07001660 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -07001661
Dan Williams4393aa42011-03-31 13:10:44 -07001662 dev_dbg(&ihost->pdev->dev,
1663 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001664}
1665
Dan Williams6f231dd2011-07-02 22:56:22 -07001666/**
1667 * This function builds the isci_remote_device when a libsas dev_found message
1668 * is received.
1669 * @isci_host: This parameter specifies the isci host object.
1670 * @port: This parameter specifies the isci_port conected to this device.
1671 *
1672 * pointer to new isci_remote_device.
1673 */
1674static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001675isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001676{
Dan Williamsd9c37392011-03-03 17:59:32 -08001677 struct isci_remote_device *idev;
1678 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001679
Dan Williamsd9c37392011-03-03 17:59:32 -08001680 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001681 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001682 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1683 break;
1684 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001685
Dan Williamsd9c37392011-03-03 17:59:32 -08001686 if (i >= SCI_MAX_REMOTE_DEVICES) {
1687 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001688 return NULL;
1689 }
1690
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001691 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1692 return NULL;
1693
1694 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1695 return NULL;
1696
Dan Williamsd9c37392011-03-03 17:59:32 -08001697 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001698
Dan Williamsd9c37392011-03-03 17:59:32 -08001699 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001700}
Dan Williams6f231dd2011-07-02 22:56:22 -07001701
1702/**
1703 * isci_remote_device_ready() - This function is called by the scic when the
1704 * remote device is ready. We mark the isci device as ready and signal the
1705 * waiting proccess.
Dan Williams037afc72011-03-31 13:10:42 -07001706 * @ihost: our valid isci_host
1707 * @idev: remote device
Dan Williams6f231dd2011-07-02 22:56:22 -07001708 *
1709 */
Dan Williams037afc72011-03-31 13:10:42 -07001710void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001711{
Dan Williams6ad31fe2011-03-04 12:10:29 -08001712 dev_dbg(&ihost->pdev->dev,
Dan Williams1a380452011-03-03 18:01:43 -08001713 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001714
Dan Williams6ad31fe2011-03-04 12:10:29 -08001715 isci_remote_device_change_state(idev, isci_ready_for_io);
1716 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1717 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001718}
1719
1720/**
1721 * isci_remote_device_not_ready() - This function is called by the scic when
1722 * the remote device is not ready. We mark the isci device as ready (not
1723 * "ready_for_io") and signal the waiting proccess.
1724 * @isci_host: This parameter specifies the isci host object.
1725 * @isci_device: This parameter specifies the remote device
1726 *
1727 */
Dan Williams037afc72011-03-31 13:10:42 -07001728void isci_remote_device_not_ready(struct isci_host *ihost,
1729 struct isci_remote_device *idev, u32 reason)
Dan Williams6f231dd2011-07-02 22:56:22 -07001730{
Dan Williams037afc72011-03-31 13:10:42 -07001731 dev_dbg(&ihost->pdev->dev,
1732 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001733
Dan Williams037afc72011-03-31 13:10:42 -07001734 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1735 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001736 else
1737 /* device ready is actually a "not ready for io" state. */
Dan Williams037afc72011-03-31 13:10:42 -07001738 isci_remote_device_change_state(idev, isci_ready);
Dan Williams6f231dd2011-07-02 22:56:22 -07001739}
1740
1741/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001742 * isci_remote_device_stop() - This function is called internally to stop the
1743 * remote device.
1744 * @isci_host: This parameter specifies the isci host object.
1745 * @isci_device: This parameter specifies the remote device.
1746 *
1747 * The status of the scic request to stop.
1748 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001749enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001750{
1751 enum sci_status status;
1752 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001753
Dan Williams6ad31fe2011-03-04 12:10:29 -08001754 dev_dbg(&ihost->pdev->dev,
1755 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001756
Dan Williams6ad31fe2011-03-04 12:10:29 -08001757 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001758
1759 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001760 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001761
Dan Williams6ad31fe2011-03-04 12:10:29 -08001762 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001763
Dan Williams6ad31fe2011-03-04 12:10:29 -08001764 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -07001765 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001766 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001767
1768 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -08001769 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -08001770 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001771 clear_bit(IDEV_ALLOCATED, &idev->flags);
1772 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001773
Dan Williams6ad31fe2011-03-04 12:10:29 -08001774 dev_dbg(&ihost->pdev->dev,
1775 "%s: idev = %p - after completion wait\n",
1776 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001777
Dan Williams6f231dd2011-07-02 22:56:22 -07001778 return status;
1779}
1780
1781/**
1782 * isci_remote_device_gone() - This function is called by libsas when a domain
1783 * device is removed.
1784 * @domain_device: This parameter specifies the libsas domain device.
1785 *
1786 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001787void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001788{
Dan Williams4393aa42011-03-31 13:10:44 -07001789 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001790 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001791
Dan Williams6ad31fe2011-03-04 12:10:29 -08001792 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001793 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001794 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001795
Dan Williams6ad31fe2011-03-04 12:10:29 -08001796 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001797}
1798
1799
1800/**
1801 * isci_remote_device_found() - This function is called by libsas when a remote
1802 * device is discovered. A remote device object is created and started. the
1803 * function then sleeps until the sci core device started message is
1804 * received.
1805 * @domain_device: This parameter specifies the libsas domain device.
1806 *
1807 * status, zero indicates success.
1808 */
1809int isci_remote_device_found(struct domain_device *domain_dev)
1810{
Dan Williams4393aa42011-03-31 13:10:44 -07001811 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001812 struct isci_port *isci_port;
1813 struct isci_phy *isci_phy;
1814 struct asd_sas_port *sas_port;
1815 struct asd_sas_phy *sas_phy;
1816 struct isci_remote_device *isci_device;
1817 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001818
Dan Williams6f231dd2011-07-02 22:56:22 -07001819 dev_dbg(&isci_host->pdev->dev,
1820 "%s: domain_device = %p\n", __func__, domain_dev);
1821
Dan Williams0cf89d12011-02-18 09:25:07 -08001822 wait_for_start(isci_host);
1823
Dan Williams6f231dd2011-07-02 22:56:22 -07001824 sas_port = domain_dev->port;
1825 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1826 port_phy_el);
1827 isci_phy = to_isci_phy(sas_phy);
1828 isci_port = isci_phy->isci_port;
1829
1830 /* we are being called for a device on this port,
1831 * so it has to come up eventually
1832 */
1833 wait_for_completion(&isci_port->start_complete);
1834
1835 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1836 (isci_stopped == isci_port_get_state(isci_port)))
1837 return -ENODEV;
1838
1839 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001840 if (!isci_device)
1841 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001842
1843 INIT_LIST_HEAD(&isci_device->node);
1844 domain_dev->lldd_dev = isci_device;
1845 isci_device->domain_dev = domain_dev;
1846 isci_device->isci_port = isci_port;
1847 isci_remote_device_change_state(isci_device, isci_starting);
1848
1849
Dan Williams1a380452011-03-03 18:01:43 -08001850 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001851 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1852
Dan Williams6ad31fe2011-03-04 12:10:29 -08001853 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001854 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -08001855 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001856
Dan Williams6f231dd2011-07-02 22:56:22 -07001857 dev_dbg(&isci_host->pdev->dev,
1858 "%s: isci_device = %p\n",
1859 __func__, isci_device);
1860
1861 if (status != SCI_SUCCESS) {
1862
Dan Williams1a380452011-03-03 18:01:43 -08001863 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001864 isci_remote_device_deconstruct(
1865 isci_host,
1866 isci_device
1867 );
Dan Williams1a380452011-03-03 18:01:43 -08001868 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001869 return -ENODEV;
1870 }
1871
Dan Williams6ad31fe2011-03-04 12:10:29 -08001872 /* wait for the device ready callback. */
1873 wait_for_device_start(isci_host, isci_device);
1874
Dan Williams6f231dd2011-07-02 22:56:22 -07001875 return 0;
1876}
1877/**
1878 * isci_device_is_reset_pending() - This function will check if there is any
1879 * pending reset condition on the device.
1880 * @request: This parameter is the isci_device object.
1881 *
1882 * true if there is a reset pending for the device.
1883 */
1884bool isci_device_is_reset_pending(
1885 struct isci_host *isci_host,
1886 struct isci_remote_device *isci_device)
1887{
1888 struct isci_request *isci_request;
1889 struct isci_request *tmp_req;
1890 bool reset_is_pending = false;
1891 unsigned long flags;
1892
1893 dev_dbg(&isci_host->pdev->dev,
1894 "%s: isci_device = %p\n", __func__, isci_device);
1895
1896 spin_lock_irqsave(&isci_host->scic_lock, flags);
1897
1898 /* Check for reset on all pending requests. */
1899 list_for_each_entry_safe(isci_request, tmp_req,
1900 &isci_device->reqs_in_process, dev_node) {
1901 dev_dbg(&isci_host->pdev->dev,
1902 "%s: isci_device = %p request = %p\n",
1903 __func__, isci_device, isci_request);
1904
1905 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001906 struct sas_task *task = isci_request_access_task(
1907 isci_request);
1908
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001909 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001910 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1911 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -07001912 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001913 }
1914 }
1915
1916 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1917
1918 dev_dbg(&isci_host->pdev->dev,
1919 "%s: isci_device = %p reset_is_pending = %d\n",
1920 __func__, isci_device, reset_is_pending);
1921
1922 return reset_is_pending;
1923}
1924
1925/**
1926 * isci_device_clear_reset_pending() - This function will clear if any pending
1927 * reset condition flags on the device.
1928 * @request: This parameter is the isci_device object.
1929 *
1930 * true if there is a reset pending for the device.
1931 */
Dan Williams4393aa42011-03-31 13:10:44 -07001932void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001933{
1934 struct isci_request *isci_request;
1935 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -07001936 unsigned long flags = 0;
1937
Dan Williams4393aa42011-03-31 13:10:44 -07001938 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1939 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001940
Dan Williams4393aa42011-03-31 13:10:44 -07001941 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001942
1943 /* Clear reset pending on all pending requests. */
1944 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -07001945 &idev->reqs_in_process, dev_node) {
1946 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1947 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001948
1949 if (isci_request->ttype == io_task) {
1950
1951 unsigned long flags2;
1952 struct sas_task *task = isci_request_access_task(
1953 isci_request);
1954
1955 spin_lock_irqsave(&task->task_state_lock, flags2);
1956 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1957 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1958 }
1959 }
Dan Williams4393aa42011-03-31 13:10:44 -07001960 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001961}