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