6c93f20f3dd3ffe9c58388268642c9f7f0700ead
[linux-2.6.git] / drivers / scsi / isci / remote_device.c
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 <scsi/sas.h>
56 #include "isci.h"
57 #include "port.h"
58 #include "remote_device.h"
59 #include "request.h"
60 #include "remote_node_context.h"
61 #include "scu_event_codes.h"
62 #include "task.h"
63
64 /**
65  * isci_remote_device_change_state() - This function gets the status of the
66  *    remote_device object.
67  * @isci_device: This parameter points to the isci_remote_device object
68  *
69  * status of the object as a isci_status enum.
70  */
71 void isci_remote_device_change_state(
72         struct isci_remote_device *isci_device,
73         enum isci_status status)
74 {
75         unsigned long flags;
76
77         spin_lock_irqsave(&isci_device->state_lock, flags);
78         isci_device->status = status;
79         spin_unlock_irqrestore(&isci_device->state_lock, flags);
80 }
81
82 /**
83  * isci_remote_device_not_ready() - This function is called by the scic when
84  *    the remote device is not ready. We mark the isci device as ready (not
85  *    "ready_for_io") and signal the waiting proccess.
86  * @isci_host: This parameter specifies the isci host object.
87  * @isci_device: This parameter specifies the remote device
88  *
89  */
90 static void isci_remote_device_not_ready(struct isci_host *ihost,
91                                   struct isci_remote_device *idev, u32 reason)
92 {
93         dev_dbg(&ihost->pdev->dev,
94                 "%s: isci_device = %p\n", __func__, idev);
95
96         if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
97                 isci_remote_device_change_state(idev, isci_stopping);
98         else
99                 /* device ready is actually a "not ready for io" state. */
100                 isci_remote_device_change_state(idev, isci_ready);
101 }
102
103 /**
104  * isci_remote_device_ready() - This function is called by the scic when the
105  *    remote device is ready. We mark the isci device as ready and signal the
106  *    waiting proccess.
107  * @ihost: our valid isci_host
108  * @idev: remote device
109  *
110  */
111 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
112 {
113         dev_dbg(&ihost->pdev->dev,
114                 "%s: idev = %p\n", __func__, idev);
115
116         isci_remote_device_change_state(idev, isci_ready_for_io);
117         if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
118                 wake_up(&ihost->eventq);
119 }
120
121 /* called once the remote node context is ready to be freed.
122  * The remote device can now report that its stop operation is complete. none
123  */
124 static void rnc_destruct_done(void *_dev)
125 {
126         struct scic_sds_remote_device *sci_dev = _dev;
127
128         BUG_ON(sci_dev->started_request_count != 0);
129         sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
130 }
131
132 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
133 {
134         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
135         u32 i, request_count = sci_dev->started_request_count;
136         enum sci_status status  = SCI_SUCCESS;
137
138         for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
139                 struct scic_sds_request *sci_req;
140                 enum sci_status s;
141
142                 sci_req = scic->io_request_table[i];
143                 if (!sci_req || sci_req->target_device != sci_dev)
144                         continue;
145                 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
146                 if (s != SCI_SUCCESS)
147                         status = s;
148         }
149
150         return status;
151 }
152
153 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
154                                         u32 timeout)
155 {
156         struct sci_base_state_machine *sm = &sci_dev->sm;
157         enum scic_sds_remote_device_states state = sm->current_state_id;
158
159         switch (state) {
160         case SCI_DEV_INITIAL:
161         case SCI_DEV_FAILED:
162         case SCI_DEV_FINAL:
163         default:
164                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
165                          __func__, state);
166                 return SCI_FAILURE_INVALID_STATE;
167         case SCI_DEV_STOPPED:
168                 return SCI_SUCCESS;
169         case SCI_DEV_STARTING:
170                 /* device not started so there had better be no requests */
171                 BUG_ON(sci_dev->started_request_count != 0);
172                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
173                                                       rnc_destruct_done, sci_dev);
174                 /* Transition to the stopping state and wait for the
175                  * remote node to complete being posted and invalidated.
176                  */
177                 sci_change_state(sm, SCI_DEV_STOPPING);
178                 return SCI_SUCCESS;
179         case SCI_DEV_READY:
180         case SCI_STP_DEV_IDLE:
181         case SCI_STP_DEV_CMD:
182         case SCI_STP_DEV_NCQ:
183         case SCI_STP_DEV_NCQ_ERROR:
184         case SCI_STP_DEV_AWAIT_RESET:
185         case SCI_SMP_DEV_IDLE:
186         case SCI_SMP_DEV_CMD:
187                 sci_change_state(sm, SCI_DEV_STOPPING);
188                 if (sci_dev->started_request_count == 0) {
189                         scic_sds_remote_node_context_destruct(&sci_dev->rnc,
190                                                               rnc_destruct_done, sci_dev);
191                         return SCI_SUCCESS;
192                 } else
193                         return scic_sds_remote_device_terminate_requests(sci_dev);
194                 break;
195         case SCI_DEV_STOPPING:
196                 /* All requests should have been terminated, but if there is an
197                  * attempt to stop a device already in the stopping state, then
198                  * try again to terminate.
199                  */
200                 return scic_sds_remote_device_terminate_requests(sci_dev);
201         case SCI_DEV_RESETTING:
202                 sci_change_state(sm, SCI_DEV_STOPPING);
203                 return SCI_SUCCESS;
204         }
205 }
206
207 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
208 {
209         struct sci_base_state_machine *sm = &sci_dev->sm;
210         enum scic_sds_remote_device_states state = sm->current_state_id;
211
212         switch (state) {
213         case SCI_DEV_INITIAL:
214         case SCI_DEV_STOPPED:
215         case SCI_DEV_STARTING:
216         case SCI_SMP_DEV_IDLE:
217         case SCI_SMP_DEV_CMD:
218         case SCI_DEV_STOPPING:
219         case SCI_DEV_FAILED:
220         case SCI_DEV_RESETTING:
221         case SCI_DEV_FINAL:
222         default:
223                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
224                          __func__, state);
225                 return SCI_FAILURE_INVALID_STATE;
226         case SCI_DEV_READY:
227         case SCI_STP_DEV_IDLE:
228         case SCI_STP_DEV_CMD:
229         case SCI_STP_DEV_NCQ:
230         case SCI_STP_DEV_NCQ_ERROR:
231         case SCI_STP_DEV_AWAIT_RESET:
232                 sci_change_state(sm, SCI_DEV_RESETTING);
233                 return SCI_SUCCESS;
234         }
235 }
236
237 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
238 {
239         struct sci_base_state_machine *sm = &sci_dev->sm;
240         enum scic_sds_remote_device_states state = sm->current_state_id;
241
242         if (state != SCI_DEV_RESETTING) {
243                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
244                          __func__, state);
245                 return SCI_FAILURE_INVALID_STATE;
246         }
247
248         sci_change_state(sm, SCI_DEV_READY);
249         return SCI_SUCCESS;
250 }
251
252 enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
253                                                u32 suspend_type)
254 {
255         struct sci_base_state_machine *sm = &sci_dev->sm;
256         enum scic_sds_remote_device_states state = sm->current_state_id;
257
258         if (state != SCI_STP_DEV_CMD) {
259                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
260                          __func__, state);
261                 return SCI_FAILURE_INVALID_STATE;
262         }
263
264         return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
265                                                     suspend_type, NULL, NULL);
266 }
267
268 enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
269                                                      u32 frame_index)
270 {
271         struct sci_base_state_machine *sm = &sci_dev->sm;
272         enum scic_sds_remote_device_states state = sm->current_state_id;
273         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
274         enum sci_status status;
275
276         switch (state) {
277         case SCI_DEV_INITIAL:
278         case SCI_DEV_STOPPED:
279         case SCI_DEV_STARTING:
280         case SCI_STP_DEV_IDLE:
281         case SCI_SMP_DEV_IDLE:
282         case SCI_DEV_FINAL:
283         default:
284                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
285                          __func__, state);
286                 /* Return the frame back to the controller */
287                 scic_sds_controller_release_frame(scic, frame_index);
288                 return SCI_FAILURE_INVALID_STATE;
289         case SCI_DEV_READY:
290         case SCI_STP_DEV_NCQ_ERROR:
291         case SCI_STP_DEV_AWAIT_RESET:
292         case SCI_DEV_STOPPING:
293         case SCI_DEV_FAILED:
294         case SCI_DEV_RESETTING: {
295                 struct scic_sds_request *sci_req;
296                 struct ssp_frame_hdr hdr;
297                 void *frame_header;
298                 ssize_t word_cnt;
299
300                 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
301                                                                        frame_index,
302                                                                        &frame_header);
303                 if (status != SCI_SUCCESS)
304                         return status;
305
306                 word_cnt = sizeof(hdr) / sizeof(u32);
307                 sci_swab32_cpy(&hdr, frame_header, word_cnt);
308
309                 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
310                 if (sci_req && sci_req->target_device == sci_dev) {
311                         /* The IO request is now in charge of releasing the frame */
312                         status = scic_sds_io_request_frame_handler(sci_req, frame_index);
313                 } else {
314                         /* We could not map this tag to a valid IO
315                          * request Just toss the frame and continue
316                          */
317                         scic_sds_controller_release_frame(scic, frame_index);
318                 }
319                 break;
320         }
321         case SCI_STP_DEV_NCQ: {
322                 struct dev_to_host_fis *hdr;
323
324                 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
325                                                                        frame_index,
326                                                                        (void **)&hdr);
327                 if (status != SCI_SUCCESS)
328                         return status;
329
330                 if (hdr->fis_type == FIS_SETDEVBITS &&
331                     (hdr->status & ATA_ERR)) {
332                         sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
333
334                         /* TODO Check sactive and complete associated IO if any. */
335                         sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
336                 } else if (hdr->fis_type == FIS_REGD2H &&
337                            (hdr->status & ATA_ERR)) {
338                         /*
339                          * Some devices return D2H FIS when an NCQ error is detected.
340                          * Treat this like an SDB error FIS ready reason.
341                          */
342                         sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
343                         sci_change_state(&sci_dev->sm, SCI_STP_DEV_NCQ_ERROR);
344                 } else
345                         status = SCI_FAILURE;
346
347                 scic_sds_controller_release_frame(scic, frame_index);
348                 break;
349         }
350         case SCI_STP_DEV_CMD:
351         case SCI_SMP_DEV_CMD:
352                 /* The device does not process any UF received from the hardware while
353                  * in this state.  All unsolicited frames are forwarded to the io request
354                  * object.
355                  */
356                 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
357                 break;
358         }
359
360         return status;
361 }
362
363 static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
364 {
365
366         struct sci_base_state_machine *sm = &sci_dev->sm;
367         enum scic_sds_remote_device_states state = sm->current_state_id;
368
369         switch (state) {
370         case SCI_DEV_READY:
371         case SCI_STP_DEV_IDLE:
372         case SCI_STP_DEV_CMD:
373         case SCI_STP_DEV_NCQ:
374         case SCI_STP_DEV_NCQ_ERROR:
375         case SCI_STP_DEV_AWAIT_RESET:
376         case SCI_SMP_DEV_IDLE:
377         case SCI_SMP_DEV_CMD:
378                 return true;
379         default:
380                 return false;
381         }
382 }
383
384 enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
385                                                      u32 event_code)
386 {
387         struct sci_base_state_machine *sm = &sci_dev->sm;
388         enum scic_sds_remote_device_states state = sm->current_state_id;
389         enum sci_status status;
390
391         switch (scu_get_event_type(event_code)) {
392         case SCU_EVENT_TYPE_RNC_OPS_MISC:
393         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
394         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
395                 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
396                 break;
397         case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
398                 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
399                         status = SCI_SUCCESS;
400
401                         /* Suspend the associated RNC */
402                         scic_sds_remote_node_context_suspend(&sci_dev->rnc,
403                                                               SCI_SOFTWARE_SUSPENSION,
404                                                               NULL, NULL);
405
406                         dev_dbg(scirdev_to_dev(sci_dev),
407                                 "%s: device: %p event code: %x: %s\n",
408                                 __func__, sci_dev, event_code,
409                                 is_remote_device_ready(sci_dev)
410                                 ? "I_T_Nexus_Timeout event"
411                                 : "I_T_Nexus_Timeout event in wrong state");
412
413                         break;
414                 }
415         /* Else, fall through and treat as unhandled... */
416         default:
417                 dev_dbg(scirdev_to_dev(sci_dev),
418                         "%s: device: %p event code: %x: %s\n",
419                         __func__, sci_dev, event_code,
420                         is_remote_device_ready(sci_dev)
421                         ? "unexpected event"
422                         : "unexpected event in wrong state");
423                 status = SCI_FAILURE_INVALID_STATE;
424                 break;
425         }
426
427         if (status != SCI_SUCCESS)
428                 return status;
429
430         if (state == SCI_STP_DEV_IDLE) {
431
432                 /* We pick up suspension events to handle specifically to this
433                  * state. We resume the RNC right away.
434                  */
435                 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
436                     scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
437                         status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
438         }
439
440         return status;
441 }
442
443 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
444                                                  struct scic_sds_request *sci_req,
445                                                  enum sci_status status)
446 {
447         struct scic_sds_port *sci_port = sci_dev->owning_port;
448
449         /* cleanup requests that failed after starting on the port */
450         if (status != SCI_SUCCESS)
451                 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
452         else
453                 scic_sds_remote_device_increment_request_count(sci_dev);
454 }
455
456 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
457                                                 struct scic_sds_remote_device *sci_dev,
458                                                 struct scic_sds_request *sci_req)
459 {
460         struct sci_base_state_machine *sm = &sci_dev->sm;
461         enum scic_sds_remote_device_states state = sm->current_state_id;
462         struct scic_sds_port *sci_port = sci_dev->owning_port;
463         struct isci_request *ireq = sci_req_to_ireq(sci_req);
464         enum sci_status status;
465
466         switch (state) {
467         case SCI_DEV_INITIAL:
468         case SCI_DEV_STOPPED:
469         case SCI_DEV_STARTING:
470         case SCI_STP_DEV_NCQ_ERROR:
471         case SCI_DEV_STOPPING:
472         case SCI_DEV_FAILED:
473         case SCI_DEV_RESETTING:
474         case SCI_DEV_FINAL:
475         default:
476                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
477                          __func__, state);
478                 return SCI_FAILURE_INVALID_STATE;
479         case SCI_DEV_READY:
480                 /* attempt to start an io request for this device object. The remote
481                  * device object will issue the start request for the io and if
482                  * successful it will start the request for the port object then
483                  * increment its own request count.
484                  */
485                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
486                 if (status != SCI_SUCCESS)
487                         return status;
488
489                 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
490                 if (status != SCI_SUCCESS)
491                         break;
492
493                 status = scic_sds_request_start(sci_req);
494                 break;
495         case SCI_STP_DEV_IDLE: {
496                 /* handle the start io operation for a sata device that is in
497                  * the command idle state. - Evalute the type of IO request to
498                  * be started - If its an NCQ request change to NCQ substate -
499                  * If its any other command change to the CMD substate
500                  *
501                  * If this is a softreset we may want to have a different
502                  * substate.
503                  */
504                 enum scic_sds_remote_device_states new_state;
505                 struct sas_task *task = isci_request_access_task(ireq);
506
507                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
508                 if (status != SCI_SUCCESS)
509                         return status;
510
511                 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
512                 if (status != SCI_SUCCESS)
513                         break;
514
515                 status = scic_sds_request_start(sci_req);
516                 if (status != SCI_SUCCESS)
517                         break;
518
519                 if (task->ata_task.use_ncq)
520                         new_state = SCI_STP_DEV_NCQ;
521                 else {
522                         sci_dev->working_request = sci_req;
523                         new_state = SCI_STP_DEV_CMD;
524                 }
525                 sci_change_state(sm, new_state);
526                 break;
527         }
528         case SCI_STP_DEV_NCQ: {
529                 struct sas_task *task = isci_request_access_task(ireq);
530
531                 if (task->ata_task.use_ncq) {
532                         status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
533                         if (status != SCI_SUCCESS)
534                                 return status;
535
536                         status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
537                         if (status != SCI_SUCCESS)
538                                 break;
539
540                         status = scic_sds_request_start(sci_req);
541                 } else
542                         return SCI_FAILURE_INVALID_STATE;
543                 break;
544         }
545         case SCI_STP_DEV_AWAIT_RESET:
546                 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
547         case SCI_SMP_DEV_IDLE:
548                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
549                 if (status != SCI_SUCCESS)
550                         return status;
551
552                 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
553                 if (status != SCI_SUCCESS)
554                         break;
555
556                 status = scic_sds_request_start(sci_req);
557                 if (status != SCI_SUCCESS)
558                         break;
559
560                 sci_dev->working_request = sci_req;
561                 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_CMD);
562                 break;
563         case SCI_STP_DEV_CMD:
564         case SCI_SMP_DEV_CMD:
565                 /* device is already handling a command it can not accept new commands
566                  * until this one is complete.
567                  */
568                 return SCI_FAILURE_INVALID_STATE;
569         }
570
571         scic_sds_remote_device_start_request(sci_dev, sci_req, status);
572         return status;
573 }
574
575 static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
576                                           struct scic_sds_remote_device *sci_dev,
577                                           struct scic_sds_request *sci_req)
578 {
579         enum sci_status status;
580
581         status = scic_sds_request_complete(sci_req);
582         if (status != SCI_SUCCESS)
583                 return status;
584
585         status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
586         if (status != SCI_SUCCESS)
587                 return status;
588
589         scic_sds_remote_device_decrement_request_count(sci_dev);
590         return status;
591 }
592
593 enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
594                                                    struct scic_sds_remote_device *sci_dev,
595                                                    struct scic_sds_request *sci_req)
596 {
597         struct sci_base_state_machine *sm = &sci_dev->sm;
598         enum scic_sds_remote_device_states state = sm->current_state_id;
599         struct scic_sds_port *sci_port = sci_dev->owning_port;
600         enum sci_status status;
601
602         switch (state) {
603         case SCI_DEV_INITIAL:
604         case SCI_DEV_STOPPED:
605         case SCI_DEV_STARTING:
606         case SCI_STP_DEV_IDLE:
607         case SCI_SMP_DEV_IDLE:
608         case SCI_DEV_FAILED:
609         case SCI_DEV_FINAL:
610         default:
611                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
612                          __func__, state);
613                 return SCI_FAILURE_INVALID_STATE;
614         case SCI_DEV_READY:
615         case SCI_STP_DEV_AWAIT_RESET:
616         case SCI_DEV_RESETTING:
617                 status = common_complete_io(sci_port, sci_dev, sci_req);
618                 break;
619         case SCI_STP_DEV_CMD:
620         case SCI_STP_DEV_NCQ:
621         case SCI_STP_DEV_NCQ_ERROR:
622                 status = common_complete_io(sci_port, sci_dev, sci_req);
623                 if (status != SCI_SUCCESS)
624                         break;
625
626                 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
627                         /* This request causes hardware error, device needs to be Lun Reset.
628                          * So here we force the state machine to IDLE state so the rest IOs
629                          * can reach RNC state handler, these IOs will be completed by RNC with
630                          * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
631                          */
632                         sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
633                 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
634                         sci_change_state(sm, SCI_STP_DEV_IDLE);
635                 break;
636         case SCI_SMP_DEV_CMD:
637                 status = common_complete_io(sci_port, sci_dev, sci_req);
638                 if (status != SCI_SUCCESS)
639                         break;
640                 sci_change_state(sm, SCI_SMP_DEV_IDLE);
641                 break;
642         case SCI_DEV_STOPPING:
643                 status = common_complete_io(sci_port, sci_dev, sci_req);
644                 if (status != SCI_SUCCESS)
645                         break;
646
647                 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
648                         scic_sds_remote_node_context_destruct(&sci_dev->rnc,
649                                                               rnc_destruct_done,
650                                                               sci_dev);
651                 break;
652         }
653
654         if (status != SCI_SUCCESS)
655                 dev_err(scirdev_to_dev(sci_dev),
656                         "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
657                         "could not complete\n", __func__, sci_port,
658                         sci_dev, sci_req, status);
659
660         return status;
661 }
662
663 static void scic_sds_remote_device_continue_request(void *dev)
664 {
665         struct scic_sds_remote_device *sci_dev = dev;
666
667         /* we need to check if this request is still valid to continue. */
668         if (sci_dev->working_request)
669                 scic_controller_continue_io(sci_dev->working_request);
670 }
671
672 enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
673                                                   struct scic_sds_remote_device *sci_dev,
674                                                   struct scic_sds_request *sci_req)
675 {
676         struct sci_base_state_machine *sm = &sci_dev->sm;
677         enum scic_sds_remote_device_states state = sm->current_state_id;
678         struct scic_sds_port *sci_port = sci_dev->owning_port;
679         enum sci_status status;
680
681         switch (state) {
682         case SCI_DEV_INITIAL:
683         case SCI_DEV_STOPPED:
684         case SCI_DEV_STARTING:
685         case SCI_SMP_DEV_IDLE:
686         case SCI_SMP_DEV_CMD:
687         case SCI_DEV_STOPPING:
688         case SCI_DEV_FAILED:
689         case SCI_DEV_RESETTING:
690         case SCI_DEV_FINAL:
691         default:
692                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
693                          __func__, state);
694                 return SCI_FAILURE_INVALID_STATE;
695         case SCI_STP_DEV_IDLE:
696         case SCI_STP_DEV_CMD:
697         case SCI_STP_DEV_NCQ:
698         case SCI_STP_DEV_NCQ_ERROR:
699         case SCI_STP_DEV_AWAIT_RESET:
700                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
701                 if (status != SCI_SUCCESS)
702                         return status;
703
704                 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
705                 if (status != SCI_SUCCESS)
706                         goto out;
707
708                 status = scic_sds_request_start(sci_req);
709                 if (status != SCI_SUCCESS)
710                         goto out;
711
712                 /* Note: If the remote device state is not IDLE this will
713                  * replace the request that probably resulted in the task
714                  * management request.
715                  */
716                 sci_dev->working_request = sci_req;
717                 sci_change_state(sm, SCI_STP_DEV_CMD);
718
719                 /* The remote node context must cleanup the TCi to NCQ mapping
720                  * table.  The only way to do this correctly is to either write
721                  * to the TLCR register or to invalidate and repost the RNC. In
722                  * either case the remote node context state machine will take
723                  * the correct action when the remote node context is suspended
724                  * and later resumed.
725                  */
726                 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
727                                 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
728                 scic_sds_remote_node_context_resume(&sci_dev->rnc,
729                                 scic_sds_remote_device_continue_request,
730                                                     sci_dev);
731
732         out:
733                 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
734                 /* We need to let the controller start request handler know that
735                  * it can't post TC yet. We will provide a callback function to
736                  * post TC when RNC gets resumed.
737                  */
738                 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
739         case SCI_DEV_READY:
740                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
741                 if (status != SCI_SUCCESS)
742                         return status;
743
744                 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
745                 if (status != SCI_SUCCESS)
746                         break;
747
748                 status = scic_sds_request_start(sci_req);
749                 break;
750         }
751         scic_sds_remote_device_start_request(sci_dev, sci_req, status);
752
753         return status;
754 }
755
756 /**
757  *
758  * @sci_dev:
759  * @request:
760  *
761  * This method takes the request and bulids an appropriate SCU context for the
762  * request and then requests the controller to post the request. none
763  */
764 void scic_sds_remote_device_post_request(
765         struct scic_sds_remote_device *sci_dev,
766         u32 request)
767 {
768         u32 context;
769
770         context = scic_sds_remote_device_build_command_context(sci_dev, request);
771
772         scic_sds_controller_post_request(
773                 scic_sds_remote_device_get_controller(sci_dev),
774                 context
775                 );
776 }
777
778 /* called once the remote node context has transisitioned to a
779  * ready state.  This is the indication that the remote device object can also
780  * transition to ready.
781  */
782 static void remote_device_resume_done(void *_dev)
783 {
784         struct scic_sds_remote_device *sci_dev = _dev;
785
786         if (is_remote_device_ready(sci_dev))
787                 return;
788
789         /* go 'ready' if we are not already in a ready state */
790         sci_change_state(&sci_dev->sm, SCI_DEV_READY);
791 }
792
793 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
794 {
795         struct scic_sds_remote_device *sci_dev = _dev;
796         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
797         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
798
799         /* For NCQ operation we do not issue a isci_remote_device_not_ready().
800          * As a result, avoid sending the ready notification.
801          */
802         if (sci_dev->sm.previous_state_id != SCI_STP_DEV_NCQ)
803                 isci_remote_device_ready(scic_to_ihost(scic), idev);
804 }
805
806 static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
807 {
808         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
809
810         /* Initial state is a transitional state to the stopped state */
811         sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
812 }
813
814 /**
815  * scic_remote_device_destruct() - free remote node context and destruct
816  * @remote_device: This parameter specifies the remote device to be destructed.
817  *
818  * Remote device objects are a limited resource.  As such, they must be
819  * protected.  Thus calls to construct and destruct are mutually exclusive and
820  * non-reentrant. The return value shall indicate if the device was
821  * successfully destructed or if some failure occurred. enum sci_status This value
822  * is returned if the device is successfully destructed.
823  * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
824  * device isn't valid (e.g. it's already been destoryed, the handle isn't
825  * valid, etc.).
826  */
827 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
828 {
829         struct sci_base_state_machine *sm = &sci_dev->sm;
830         enum scic_sds_remote_device_states state = sm->current_state_id;
831         struct scic_sds_controller *scic;
832
833         if (state != SCI_DEV_STOPPED) {
834                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
835                          __func__, state);
836                 return SCI_FAILURE_INVALID_STATE;
837         }
838
839         scic = sci_dev->owning_port->owning_controller;
840         scic_sds_controller_free_remote_node_context(scic, sci_dev,
841                                                      sci_dev->rnc.remote_node_index);
842         sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
843         sci_change_state(sm, SCI_DEV_FINAL);
844
845         return SCI_SUCCESS;
846 }
847
848 /**
849  * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
850  * @ihost: This parameter specifies the isci host object.
851  * @idev: This parameter specifies the remote device to be freed.
852  *
853  */
854 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
855 {
856         dev_dbg(&ihost->pdev->dev,
857                 "%s: isci_device = %p\n", __func__, idev);
858
859         /* There should not be any outstanding io's. All paths to
860          * here should go through isci_remote_device_nuke_requests.
861          * If we hit this condition, we will need a way to complete
862          * io requests in process */
863         while (!list_empty(&idev->reqs_in_process)) {
864
865                 dev_err(&ihost->pdev->dev,
866                         "%s: ** request list not empty! **\n", __func__);
867                 BUG();
868         }
869
870         scic_remote_device_destruct(&idev->sci);
871         idev->domain_dev->lldd_dev = NULL;
872         idev->domain_dev = NULL;
873         idev->isci_port = NULL;
874         list_del_init(&idev->node);
875
876         clear_bit(IDEV_START_PENDING, &idev->flags);
877         clear_bit(IDEV_STOP_PENDING, &idev->flags);
878         clear_bit(IDEV_EH, &idev->flags);
879         wake_up(&ihost->eventq);
880 }
881
882 /**
883  * isci_remote_device_stop_complete() - This function is called by the scic
884  *    when the remote device stop has completed. We mark the isci device as not
885  *    ready and remove the isci remote device.
886  * @ihost: This parameter specifies the isci host object.
887  * @idev: This parameter specifies the remote device.
888  * @status: This parameter specifies status of the completion.
889  *
890  */
891 static void isci_remote_device_stop_complete(struct isci_host *ihost,
892                                              struct isci_remote_device *idev)
893 {
894         dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
895
896         isci_remote_device_change_state(idev, isci_stopped);
897
898         /* after stop, we can tear down resources. */
899         isci_remote_device_deconstruct(ihost, idev);
900 }
901
902 static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
903 {
904         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
905         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
906         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
907         u32 prev_state;
908
909         /* If we are entering from the stopping state let the SCI User know that
910          * the stop operation has completed.
911          */
912         prev_state = sci_dev->sm.previous_state_id;
913         if (prev_state == SCI_DEV_STOPPING)
914                 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
915
916         scic_sds_controller_remote_device_stopped(scic, sci_dev);
917 }
918
919 static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
920 {
921         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
922         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
923         struct isci_host *ihost = scic_to_ihost(scic);
924         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
925
926         isci_remote_device_not_ready(ihost, idev,
927                                      SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
928 }
929
930 static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
931 {
932         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
933         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
934         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
935         struct domain_device *dev = idev->domain_dev;
936
937         scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
938
939         if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
940                 sci_change_state(&sci_dev->sm, SCI_STP_DEV_IDLE);
941         } else if (dev_is_expander(dev)) {
942                 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_IDLE);
943         } else
944                 isci_remote_device_ready(scic_to_ihost(scic), idev);
945 }
946
947 static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
948 {
949         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
950         struct domain_device *dev = sci_dev_to_domain(sci_dev);
951
952         if (dev->dev_type == SAS_END_DEV) {
953                 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
954                 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
955
956                 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
957                                              SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
958         }
959 }
960
961 static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
962 {
963         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
964
965         scic_sds_remote_node_context_suspend(
966                 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
967 }
968
969 static void scic_sds_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
970 {
971         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
972
973         scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
974 }
975
976 static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
977 {
978         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
979
980         sci_dev->working_request = NULL;
981         if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
982                 /*
983                  * Since the RNC is ready, it's alright to finish completion
984                  * processing (e.g. signal the remote device is ready). */
985                 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
986         } else {
987                 scic_sds_remote_node_context_resume(&sci_dev->rnc,
988                         scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
989                         sci_dev);
990         }
991 }
992
993 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
994 {
995         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
996         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
997
998         BUG_ON(sci_dev->working_request == NULL);
999
1000         isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1001                                      SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1002 }
1003
1004 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
1005 {
1006         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1007         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1008         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1009
1010         if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1011                 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
1012                                              sci_dev->not_ready_reason);
1013 }
1014
1015 static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
1016 {
1017         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1018         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1019
1020         isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
1021 }
1022
1023 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
1024 {
1025         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1026         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1027
1028         BUG_ON(sci_dev->working_request == NULL);
1029
1030         isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1031                                      SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1032 }
1033
1034 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
1035 {
1036         struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1037
1038         sci_dev->working_request = NULL;
1039 }
1040
1041 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1042         [SCI_DEV_INITIAL] = {
1043                 .enter_state = scic_sds_remote_device_initial_state_enter,
1044         },
1045         [SCI_DEV_STOPPED] = {
1046                 .enter_state = scic_sds_remote_device_stopped_state_enter,
1047         },
1048         [SCI_DEV_STARTING] = {
1049                 .enter_state = scic_sds_remote_device_starting_state_enter,
1050         },
1051         [SCI_DEV_READY] = {
1052                 .enter_state = scic_sds_remote_device_ready_state_enter,
1053                 .exit_state  = scic_sds_remote_device_ready_state_exit
1054         },
1055         [SCI_STP_DEV_IDLE] = {
1056                 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1057         },
1058         [SCI_STP_DEV_CMD] = {
1059                 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1060         },
1061         [SCI_STP_DEV_NCQ] = { },
1062         [SCI_STP_DEV_NCQ_ERROR] = {
1063                 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1064         },
1065         [SCI_STP_DEV_AWAIT_RESET] = { },
1066         [SCI_SMP_DEV_IDLE] = {
1067                 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1068         },
1069         [SCI_SMP_DEV_CMD] = {
1070                 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1071                 .exit_state  = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1072         },
1073         [SCI_DEV_STOPPING] = { },
1074         [SCI_DEV_FAILED] = { },
1075         [SCI_DEV_RESETTING] = {
1076                 .enter_state = scic_sds_remote_device_resetting_state_enter,
1077                 .exit_state  = scic_sds_remote_device_resetting_state_exit
1078         },
1079         [SCI_DEV_FINAL] = { },
1080 };
1081
1082 /**
1083  * scic_remote_device_construct() - common construction
1084  * @sci_port: SAS/SATA port through which this device is accessed.
1085  * @sci_dev: remote device to construct
1086  *
1087  * This routine just performs benign initialization and does not
1088  * allocate the remote_node_context which is left to
1089  * scic_remote_device_[de]a_construct().  scic_remote_device_destruct()
1090  * frees the remote_node_context(s) for the device.
1091  */
1092 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1093                                   struct scic_sds_remote_device *sci_dev)
1094 {
1095         sci_dev->owning_port = sci_port;
1096         sci_dev->started_request_count = 0;
1097
1098         sci_base_state_machine_construct(&sci_dev->sm,
1099                                          scic_sds_remote_device_state_table,
1100                                          SCI_DEV_INITIAL);
1101
1102         sci_base_state_machine_start(&sci_dev->sm);
1103
1104         scic_sds_remote_node_context_construct(&sci_dev->rnc,
1105                                                SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1106 }
1107
1108 /**
1109  * scic_remote_device_da_construct() - construct direct attached device.
1110  *
1111  * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1112  * the device is known to the SCI Core since it is contained in the
1113  * scic_phy object.  Remote node context(s) is/are a global resource
1114  * allocated by this routine, freed by scic_remote_device_destruct().
1115  *
1116  * Returns:
1117  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1118  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1119  * sata-only controller instance.
1120  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1121  */
1122 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1123                                                        struct scic_sds_remote_device *sci_dev)
1124 {
1125         enum sci_status status;
1126         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1127
1128         scic_remote_device_construct(sci_port, sci_dev);
1129
1130         /*
1131          * This information is request to determine how many remote node context
1132          * entries will be needed to store the remote node.
1133          */
1134         sci_dev->is_direct_attached = true;
1135         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1136                                                                   sci_dev,
1137                                                                   &sci_dev->rnc.remote_node_index);
1138
1139         if (status != SCI_SUCCESS)
1140                 return status;
1141
1142         if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1143             (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1144                 /* pass */;
1145         else
1146                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1147
1148         sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1149
1150         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1151         sci_dev->device_port_width = 1;
1152
1153         return SCI_SUCCESS;
1154 }
1155
1156 /**
1157  * scic_remote_device_ea_construct() - construct expander attached device
1158  *
1159  * Remote node context(s) is/are a global resource allocated by this
1160  * routine, freed by scic_remote_device_destruct().
1161  *
1162  * Returns:
1163  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1164  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1165  * sata-only controller instance.
1166  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1167  */
1168 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1169                                                        struct scic_sds_remote_device *sci_dev)
1170 {
1171         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1172         enum sci_status status;
1173
1174         scic_remote_device_construct(sci_port, sci_dev);
1175
1176         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1177                                                                   sci_dev,
1178                                                                   &sci_dev->rnc.remote_node_index);
1179         if (status != SCI_SUCCESS)
1180                 return status;
1181
1182         if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1183             (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1184                 /* pass */;
1185         else
1186                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1187
1188         /*
1189          * For SAS-2 the physical link rate is actually a logical link
1190          * rate that incorporates multiplexing.  The SCU doesn't
1191          * incorporate multiplexing and for the purposes of the
1192          * connection the logical link rate is that same as the
1193          * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
1194          * one another, so this code works for both situations. */
1195         sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1196                                          dev->linkrate);
1197
1198         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1199         sci_dev->device_port_width = 1;
1200
1201         return SCI_SUCCESS;
1202 }
1203
1204 /**
1205  * scic_remote_device_start() - This method will start the supplied remote
1206  *    device.  This method enables normal IO requests to flow through to the
1207  *    remote device.
1208  * @remote_device: This parameter specifies the device to be started.
1209  * @timeout: This parameter specifies the number of milliseconds in which the
1210  *    start operation should complete.
1211  *
1212  * An indication of whether the device was successfully started. SCI_SUCCESS
1213  * This value is returned if the device was successfully started.
1214  * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1215  * the device when there have been no phys added to it.
1216  */
1217 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1218                                                 u32 timeout)
1219 {
1220         struct sci_base_state_machine *sm = &sci_dev->sm;
1221         enum scic_sds_remote_device_states state = sm->current_state_id;
1222         enum sci_status status;
1223
1224         if (state != SCI_DEV_STOPPED) {
1225                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1226                          __func__, state);
1227                 return SCI_FAILURE_INVALID_STATE;
1228         }
1229
1230         status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1231                                                      remote_device_resume_done,
1232                                                      sci_dev);
1233         if (status != SCI_SUCCESS)
1234                 return status;
1235
1236         sci_change_state(sm, SCI_DEV_STARTING);
1237
1238         return SCI_SUCCESS;
1239 }
1240
1241 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1242                                                     struct isci_remote_device *idev)
1243 {
1244         struct scic_sds_port *sci_port = &iport->sci;
1245         struct isci_host *ihost = iport->isci_host;
1246         struct domain_device *dev = idev->domain_dev;
1247         enum sci_status status;
1248
1249         if (dev->parent && dev_is_expander(dev->parent))
1250                 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1251         else
1252                 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1253
1254         if (status != SCI_SUCCESS) {
1255                 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1256                         __func__, status);
1257
1258                 return status;
1259         }
1260
1261         /* start the device. */
1262         status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1263
1264         if (status != SCI_SUCCESS)
1265                 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1266                          status);
1267
1268         return status;
1269 }
1270
1271 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1272 {
1273         DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1274
1275         dev_dbg(&ihost->pdev->dev,
1276                 "%s: idev = %p\n", __func__, idev);
1277
1278         /* Cleanup all requests pending for this device. */
1279         isci_terminate_pending_requests(ihost, idev, terminating);
1280
1281         dev_dbg(&ihost->pdev->dev,
1282                 "%s: idev = %p, done\n", __func__, idev);
1283 }
1284
1285 /**
1286  * This function builds the isci_remote_device when a libsas dev_found message
1287  *    is received.
1288  * @isci_host: This parameter specifies the isci host object.
1289  * @port: This parameter specifies the isci_port conected to this device.
1290  *
1291  * pointer to new isci_remote_device.
1292  */
1293 static struct isci_remote_device *
1294 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1295 {
1296         struct isci_remote_device *idev;
1297         int i;
1298
1299         for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1300                 idev = &ihost->devices[i];
1301                 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1302                         break;
1303         }
1304
1305         if (i >= SCI_MAX_REMOTE_DEVICES) {
1306                 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1307                 return NULL;
1308         }
1309
1310         if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1311                 return NULL;
1312
1313         if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1314                 return NULL;
1315
1316         isci_remote_device_change_state(idev, isci_freed);
1317
1318         return idev;
1319 }
1320
1321 /**
1322  * isci_remote_device_stop() - This function is called internally to stop the
1323  *    remote device.
1324  * @isci_host: This parameter specifies the isci host object.
1325  * @isci_device: This parameter specifies the remote device.
1326  *
1327  * The status of the scic request to stop.
1328  */
1329 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1330 {
1331         enum sci_status status;
1332         unsigned long flags;
1333
1334         dev_dbg(&ihost->pdev->dev,
1335                 "%s: isci_device = %p\n", __func__, idev);
1336
1337         isci_remote_device_change_state(idev, isci_stopping);
1338
1339         /* Kill all outstanding requests. */
1340         isci_remote_device_nuke_requests(ihost, idev);
1341
1342         set_bit(IDEV_STOP_PENDING, &idev->flags);
1343
1344         spin_lock_irqsave(&ihost->scic_lock, flags);
1345         status = scic_remote_device_stop(&idev->sci, 50);
1346         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1347
1348         /* Wait for the stop complete callback. */
1349         if (status == SCI_SUCCESS) {
1350                 wait_for_device_stop(ihost, idev);
1351                 clear_bit(IDEV_ALLOCATED, &idev->flags);
1352         }
1353
1354         dev_dbg(&ihost->pdev->dev,
1355                 "%s: idev = %p - after completion wait\n",
1356                 __func__, idev);
1357
1358         return status;
1359 }
1360
1361 /**
1362  * isci_remote_device_gone() - This function is called by libsas when a domain
1363  *    device is removed.
1364  * @domain_device: This parameter specifies the libsas domain device.
1365  *
1366  */
1367 void isci_remote_device_gone(struct domain_device *dev)
1368 {
1369         struct isci_host *ihost = dev_to_ihost(dev);
1370         struct isci_remote_device *idev = dev->lldd_dev;
1371
1372         dev_dbg(&ihost->pdev->dev,
1373                 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1374                 __func__, dev, idev, idev->isci_port);
1375
1376         isci_remote_device_stop(ihost, idev);
1377 }
1378
1379
1380 /**
1381  * isci_remote_device_found() - This function is called by libsas when a remote
1382  *    device is discovered. A remote device object is created and started. the
1383  *    function then sleeps until the sci core device started message is
1384  *    received.
1385  * @domain_device: This parameter specifies the libsas domain device.
1386  *
1387  * status, zero indicates success.
1388  */
1389 int isci_remote_device_found(struct domain_device *domain_dev)
1390 {
1391         struct isci_host *isci_host = dev_to_ihost(domain_dev);
1392         struct isci_port *isci_port;
1393         struct isci_phy *isci_phy;
1394         struct asd_sas_port *sas_port;
1395         struct asd_sas_phy *sas_phy;
1396         struct isci_remote_device *isci_device;
1397         enum sci_status status;
1398
1399         dev_dbg(&isci_host->pdev->dev,
1400                 "%s: domain_device = %p\n", __func__, domain_dev);
1401
1402         wait_for_start(isci_host);
1403
1404         sas_port = domain_dev->port;
1405         sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1406                                    port_phy_el);
1407         isci_phy = to_isci_phy(sas_phy);
1408         isci_port = isci_phy->isci_port;
1409
1410         /* we are being called for a device on this port,
1411          * so it has to come up eventually
1412          */
1413         wait_for_completion(&isci_port->start_complete);
1414
1415         if ((isci_stopping == isci_port_get_state(isci_port)) ||
1416             (isci_stopped == isci_port_get_state(isci_port)))
1417                 return -ENODEV;
1418
1419         isci_device = isci_remote_device_alloc(isci_host, isci_port);
1420         if (!isci_device)
1421                 return -ENODEV;
1422
1423         INIT_LIST_HEAD(&isci_device->node);
1424         domain_dev->lldd_dev = isci_device;
1425         isci_device->domain_dev = domain_dev;
1426         isci_device->isci_port = isci_port;
1427         isci_remote_device_change_state(isci_device, isci_starting);
1428
1429
1430         spin_lock_irq(&isci_host->scic_lock);
1431         list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1432
1433         set_bit(IDEV_START_PENDING, &isci_device->flags);
1434         status = isci_remote_device_construct(isci_port, isci_device);
1435         spin_unlock_irq(&isci_host->scic_lock);
1436
1437         dev_dbg(&isci_host->pdev->dev,
1438                 "%s: isci_device = %p\n",
1439                 __func__, isci_device);
1440
1441         if (status != SCI_SUCCESS) {
1442
1443                 spin_lock_irq(&isci_host->scic_lock);
1444                 isci_remote_device_deconstruct(
1445                         isci_host,
1446                         isci_device
1447                         );
1448                 spin_unlock_irq(&isci_host->scic_lock);
1449                 return -ENODEV;
1450         }
1451
1452         /* wait for the device ready callback. */
1453         wait_for_device_start(isci_host, isci_device);
1454
1455         return 0;
1456 }
1457 /**
1458  * isci_device_is_reset_pending() - This function will check if there is any
1459  *    pending reset condition on the device.
1460  * @request: This parameter is the isci_device object.
1461  *
1462  * true if there is a reset pending for the device.
1463  */
1464 bool isci_device_is_reset_pending(
1465         struct isci_host *isci_host,
1466         struct isci_remote_device *isci_device)
1467 {
1468         struct isci_request *isci_request;
1469         struct isci_request *tmp_req;
1470         bool reset_is_pending = false;
1471         unsigned long flags;
1472
1473         dev_dbg(&isci_host->pdev->dev,
1474                 "%s: isci_device = %p\n", __func__, isci_device);
1475
1476         spin_lock_irqsave(&isci_host->scic_lock, flags);
1477
1478         /* Check for reset on all pending requests. */
1479         list_for_each_entry_safe(isci_request, tmp_req,
1480                                  &isci_device->reqs_in_process, dev_node) {
1481                 dev_dbg(&isci_host->pdev->dev,
1482                         "%s: isci_device = %p request = %p\n",
1483                         __func__, isci_device, isci_request);
1484
1485                 if (isci_request->ttype == io_task) {
1486                         struct sas_task *task = isci_request_access_task(
1487                                 isci_request);
1488
1489                         spin_lock(&task->task_state_lock);
1490                         if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1491                                 reset_is_pending = true;
1492                         spin_unlock(&task->task_state_lock);
1493                 }
1494         }
1495
1496         spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1497
1498         dev_dbg(&isci_host->pdev->dev,
1499                 "%s: isci_device = %p reset_is_pending = %d\n",
1500                 __func__, isci_device, reset_is_pending);
1501
1502         return reset_is_pending;
1503 }
1504
1505 /**
1506  * isci_device_clear_reset_pending() - This function will clear if any pending
1507  *    reset condition flags on the device.
1508  * @request: This parameter is the isci_device object.
1509  *
1510  * true if there is a reset pending for the device.
1511  */
1512 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1513 {
1514         struct isci_request *isci_request;
1515         struct isci_request *tmp_req;
1516         unsigned long flags = 0;
1517
1518         dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1519                  __func__, idev, ihost);
1520
1521         spin_lock_irqsave(&ihost->scic_lock, flags);
1522
1523         /* Clear reset pending on all pending requests. */
1524         list_for_each_entry_safe(isci_request, tmp_req,
1525                                  &idev->reqs_in_process, dev_node) {
1526                 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1527                          __func__, idev, isci_request);
1528
1529                 if (isci_request->ttype == io_task) {
1530
1531                         unsigned long flags2;
1532                         struct sas_task *task = isci_request_access_task(
1533                                 isci_request);
1534
1535                         spin_lock_irqsave(&task->task_state_lock, flags2);
1536                         task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1537                         spin_unlock_irqrestore(&task->task_state_lock, flags2);
1538                 }
1539         }
1540         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1541 }