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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
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.
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.
57 #include "scic_io_request.h"
58 #include "scic_remote_device.h"
60 #include "scic_port.h"
62 #include "remote_device.h"
69 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
70 * @ihost: This parameter specifies the isci host object.
71 * @idev: This parameter specifies the remote device to be freed.
74 static void isci_remote_device_deconstruct(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
76 dev_dbg(&ihost
->pdev
->dev
,
77 "%s: isci_device = %p\n", __func__
, idev
);
79 /* There should not be any outstanding io's. All paths to
80 * here should go through isci_remote_device_nuke_requests.
81 * If we hit this condition, we will need a way to complete
82 * io requests in process */
83 while (!list_empty(&idev
->reqs_in_process
)) {
85 dev_err(&ihost
->pdev
->dev
,
86 "%s: ** request list not empty! **\n", __func__
);
90 scic_remote_device_destruct(to_sci_dev(idev
));
91 idev
->domain_dev
->lldd_dev
= NULL
;
92 idev
->domain_dev
= NULL
;
93 idev
->isci_port
= NULL
;
94 list_del_init(&idev
->node
);
96 clear_bit(IDEV_START_PENDING
, &idev
->flags
);
97 clear_bit(IDEV_STOP_PENDING
, &idev
->flags
);
98 wake_up(&ihost
->eventq
);
103 * isci_remote_device_construct() - This function calls the scic remote device
104 * construct and start functions, it waits on the remote device start
106 * @port: This parameter specifies the isci port with the remote device.
107 * @isci_device: This parameter specifies the isci remote device
109 * status from the scic calls, the caller to this function should clean up
110 * resources as appropriate.
112 static enum sci_status
isci_remote_device_construct(
113 struct isci_port
*port
,
114 struct isci_remote_device
*isci_device
)
116 enum sci_status status
= SCI_SUCCESS
;
118 /* let the core do it's common constuction. */
119 scic_remote_device_construct(port
->sci_port_handle
,
120 to_sci_dev(isci_device
));
122 /* let the core do it's device specific constuction. */
123 if (isci_device
->domain_dev
->parent
&&
124 (isci_device
->domain_dev
->parent
->dev_type
== EDGE_DEV
)) {
127 /* struct smp_response_discover discover_response; */
128 struct discover_resp discover_response
;
129 struct domain_device
*parent
=
130 isci_device
->domain_dev
->parent
;
132 struct expander_device
*parent_ex
= &parent
->ex_dev
;
134 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
136 struct ex_phy
*phy
= &parent_ex
->ex_phy
[i
];
138 if ((phy
->phy_state
== PHY_VACANT
) ||
139 (phy
->phy_state
== PHY_NOT_PRESENT
))
142 if (SAS_ADDR(phy
->attached_sas_addr
)
143 == SAS_ADDR(isci_device
->domain_dev
->sas_addr
)) {
145 discover_response
.attached_dev_type
146 = phy
->attached_dev_type
;
147 discover_response
.linkrate
149 discover_response
.attached_sata_host
150 = phy
->attached_sata_host
;
151 discover_response
.attached_sata_dev
152 = phy
->attached_sata_dev
;
153 discover_response
.attached_sata_ps
154 = phy
->attached_sata_ps
;
155 discover_response
.iproto
156 = phy
->attached_iproto
>> 1;
157 discover_response
.tproto
158 = phy
->attached_tproto
>> 1;
160 discover_response
.attached_sas_addr
,
161 phy
->attached_sas_addr
,
164 discover_response
.attached_phy_id
165 = phy
->attached_phy_id
;
166 discover_response
.change_count
167 = phy
->phy_change_count
;
168 discover_response
.routing_attr
170 discover_response
.hmin_linkrate
171 = phy
->phy
->minimum_linkrate_hw
;
172 discover_response
.hmax_linkrate
173 = phy
->phy
->maximum_linkrate_hw
;
174 discover_response
.pmin_linkrate
175 = phy
->phy
->minimum_linkrate
;
176 discover_response
.pmax_linkrate
177 = phy
->phy
->maximum_linkrate
;
182 dev_dbg(&port
->isci_host
->pdev
->dev
,
183 "%s: parent->dev_type = EDGE_DEV\n",
186 status
= scic_remote_device_ea_construct(to_sci_dev(isci_device
),
187 (struct smp_response_discover
*)&discover_response
);
190 status
= scic_remote_device_da_construct(to_sci_dev(isci_device
));
193 if (status
!= SCI_SUCCESS
) {
194 dev_dbg(&port
->isci_host
->pdev
->dev
,
195 "%s: scic_remote_device_da_construct failed - "
196 "isci_device = %p\n",
203 sci_object_set_association(to_sci_dev(isci_device
), isci_device
);
205 BUG_ON(port
->isci_host
== NULL
);
207 /* start the device. */
208 status
= scic_remote_device_start(to_sci_dev(isci_device
),
209 ISCI_REMOTE_DEVICE_START_TIMEOUT
);
211 if (status
!= SCI_SUCCESS
) {
212 dev_warn(&port
->isci_host
->pdev
->dev
,
213 "%s: scic_remote_device_start failed\n",
221 void isci_remote_device_nuke_requests(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
223 DECLARE_COMPLETION_ONSTACK(aborted_task_completion
);
225 dev_dbg(&ihost
->pdev
->dev
,
226 "%s: idev = %p\n", __func__
, idev
);
228 /* Cleanup all requests pending for this device. */
229 isci_terminate_pending_requests(ihost
, idev
, terminating
);
231 dev_dbg(&ihost
->pdev
->dev
,
232 "%s: idev = %p, done\n", __func__
, idev
);
236 * This function builds the isci_remote_device when a libsas dev_found message
238 * @isci_host: This parameter specifies the isci host object.
239 * @port: This parameter specifies the isci_port conected to this device.
241 * pointer to new isci_remote_device.
243 static struct isci_remote_device
*
244 isci_remote_device_alloc(struct isci_host
*ihost
, struct isci_port
*iport
)
246 struct isci_remote_device
*idev
;
249 for (i
= 0; i
< SCI_MAX_REMOTE_DEVICES
; i
++) {
250 idev
= idev_by_id(ihost
, i
);
251 if (!test_and_set_bit(IDEV_ALLOCATED
, &idev
->flags
))
255 if (i
>= SCI_MAX_REMOTE_DEVICES
) {
256 dev_warn(&ihost
->pdev
->dev
, "%s: failed\n", __func__
);
260 BUG_ON(!list_empty(&idev
->reqs_in_process
));
261 BUG_ON(!list_empty(&idev
->node
));
262 isci_remote_device_change_state(idev
, isci_freed
);
268 * isci_remote_device_ready() - This function is called by the scic when the
269 * remote device is ready. We mark the isci device as ready and signal the
271 * @ihost: our valid isci_host
272 * @idev: remote device
275 void isci_remote_device_ready(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
277 dev_dbg(&ihost
->pdev
->dev
,
278 "%s: idev = %p\n", __func__
, idev
);
280 isci_remote_device_change_state(idev
, isci_ready_for_io
);
281 if (test_and_clear_bit(IDEV_START_PENDING
, &idev
->flags
))
282 wake_up(&ihost
->eventq
);
286 * isci_remote_device_not_ready() - This function is called by the scic when
287 * the remote device is not ready. We mark the isci device as ready (not
288 * "ready_for_io") and signal the waiting proccess.
289 * @isci_host: This parameter specifies the isci host object.
290 * @isci_device: This parameter specifies the remote device
293 void isci_remote_device_not_ready(struct isci_host
*ihost
,
294 struct isci_remote_device
*idev
, u32 reason
)
296 dev_dbg(&ihost
->pdev
->dev
,
297 "%s: isci_device = %p\n", __func__
, idev
);
299 if (reason
== SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED
)
300 isci_remote_device_change_state(idev
, isci_stopping
);
302 /* device ready is actually a "not ready for io" state. */
303 isci_remote_device_change_state(idev
, isci_ready
);
307 * isci_remote_device_stop_complete() - This function is called by the scic
308 * when the remote device stop has completed. We mark the isci device as not
309 * ready and remove the isci remote device.
310 * @isci_host: This parameter specifies the isci host object.
311 * @isci_device: This parameter specifies the remote device.
312 * @status: This parameter specifies status of the completion.
315 void isci_remote_device_stop_complete(
316 struct isci_host
*isci_host
,
317 struct isci_remote_device
*isci_device
,
318 enum sci_status status
)
320 dev_dbg(&isci_host
->pdev
->dev
,
321 "%s: complete isci_device = %p, status = 0x%x\n",
326 isci_remote_device_change_state(isci_device
, isci_stopped
);
328 /* after stop, we can tear down resources. */
329 isci_remote_device_deconstruct(isci_host
, isci_device
);
334 * isci_remote_device_start_complete() - This function is called by the scic
335 * when the remote device start has completed
336 * @isci_host: This parameter specifies the isci host object.
337 * @isci_device: This parameter specifies the remote device.
338 * @status: This parameter specifies status of the completion.
341 void isci_remote_device_start_complete(
342 struct isci_host
*isci_host
,
343 struct isci_remote_device
*isci_device
,
344 enum sci_status status
)
352 * isci_remote_device_stop() - This function is called internally to stop the
354 * @isci_host: This parameter specifies the isci host object.
355 * @isci_device: This parameter specifies the remote device.
357 * The status of the scic request to stop.
359 enum sci_status
isci_remote_device_stop(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
361 enum sci_status status
;
364 dev_dbg(&ihost
->pdev
->dev
,
365 "%s: isci_device = %p\n", __func__
, idev
);
367 isci_remote_device_change_state(idev
, isci_stopping
);
369 /* Kill all outstanding requests. */
370 isci_remote_device_nuke_requests(ihost
, idev
);
372 set_bit(IDEV_STOP_PENDING
, &idev
->flags
);
374 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
375 status
= scic_remote_device_stop(to_sci_dev(idev
), 50);
376 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
378 /* Wait for the stop complete callback. */
379 if (status
== SCI_SUCCESS
) {
380 wait_for_device_stop(ihost
, idev
);
381 clear_bit(IDEV_ALLOCATED
, &idev
->flags
);
384 dev_dbg(&ihost
->pdev
->dev
,
385 "%s: idev = %p - after completion wait\n",
392 * isci_remote_device_gone() - This function is called by libsas when a domain
394 * @domain_device: This parameter specifies the libsas domain device.
397 void isci_remote_device_gone(struct domain_device
*dev
)
399 struct isci_host
*ihost
= dev_to_ihost(dev
);
400 struct isci_remote_device
*idev
= dev
->lldd_dev
;
402 dev_dbg(&ihost
->pdev
->dev
,
403 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
404 __func__
, dev
, idev
, idev
->isci_port
);
406 isci_remote_device_stop(ihost
, idev
);
411 * isci_remote_device_found() - This function is called by libsas when a remote
412 * device is discovered. A remote device object is created and started. the
413 * function then sleeps until the sci core device started message is
415 * @domain_device: This parameter specifies the libsas domain device.
417 * status, zero indicates success.
419 int isci_remote_device_found(struct domain_device
*domain_dev
)
421 struct isci_host
*isci_host
= dev_to_ihost(domain_dev
);
422 struct isci_port
*isci_port
;
423 struct isci_phy
*isci_phy
;
424 struct asd_sas_port
*sas_port
;
425 struct asd_sas_phy
*sas_phy
;
426 struct isci_remote_device
*isci_device
;
427 enum sci_status status
;
429 dev_dbg(&isci_host
->pdev
->dev
,
430 "%s: domain_device = %p\n", __func__
, domain_dev
);
432 wait_for_start(isci_host
);
434 sas_port
= domain_dev
->port
;
435 sas_phy
= list_first_entry(&sas_port
->phy_list
, struct asd_sas_phy
,
437 isci_phy
= to_isci_phy(sas_phy
);
438 isci_port
= isci_phy
->isci_port
;
440 /* we are being called for a device on this port,
441 * so it has to come up eventually
443 wait_for_completion(&isci_port
->start_complete
);
445 if ((isci_stopping
== isci_port_get_state(isci_port
)) ||
446 (isci_stopped
== isci_port_get_state(isci_port
)))
449 isci_device
= isci_remote_device_alloc(isci_host
, isci_port
);
453 INIT_LIST_HEAD(&isci_device
->node
);
454 domain_dev
->lldd_dev
= isci_device
;
455 isci_device
->domain_dev
= domain_dev
;
456 isci_device
->isci_port
= isci_port
;
457 isci_remote_device_change_state(isci_device
, isci_starting
);
460 spin_lock_irq(&isci_host
->scic_lock
);
461 list_add_tail(&isci_device
->node
, &isci_port
->remote_dev_list
);
463 set_bit(IDEV_START_PENDING
, &isci_device
->flags
);
464 status
= isci_remote_device_construct(isci_port
, isci_device
);
465 spin_unlock_irq(&isci_host
->scic_lock
);
467 dev_dbg(&isci_host
->pdev
->dev
,
468 "%s: isci_device = %p\n",
469 __func__
, isci_device
);
471 if (status
!= SCI_SUCCESS
) {
473 spin_lock_irq(&isci_host
->scic_lock
);
474 isci_remote_device_deconstruct(
478 spin_unlock_irq(&isci_host
->scic_lock
);
482 /* wait for the device ready callback. */
483 wait_for_device_start(isci_host
, isci_device
);
488 * isci_device_is_reset_pending() - This function will check if there is any
489 * pending reset condition on the device.
490 * @request: This parameter is the isci_device object.
492 * true if there is a reset pending for the device.
494 bool isci_device_is_reset_pending(
495 struct isci_host
*isci_host
,
496 struct isci_remote_device
*isci_device
)
498 struct isci_request
*isci_request
;
499 struct isci_request
*tmp_req
;
500 bool reset_is_pending
= false;
503 dev_dbg(&isci_host
->pdev
->dev
,
504 "%s: isci_device = %p\n", __func__
, isci_device
);
506 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
508 /* Check for reset on all pending requests. */
509 list_for_each_entry_safe(isci_request
, tmp_req
,
510 &isci_device
->reqs_in_process
, dev_node
) {
511 dev_dbg(&isci_host
->pdev
->dev
,
512 "%s: isci_device = %p request = %p\n",
513 __func__
, isci_device
, isci_request
);
515 if (isci_request
->ttype
== io_task
) {
516 struct sas_task
*task
= isci_request_access_task(
519 spin_lock(&task
->task_state_lock
);
520 if (task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
)
521 reset_is_pending
= true;
522 spin_unlock(&task
->task_state_lock
);
526 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
528 dev_dbg(&isci_host
->pdev
->dev
,
529 "%s: isci_device = %p reset_is_pending = %d\n",
530 __func__
, isci_device
, reset_is_pending
);
532 return reset_is_pending
;
536 * isci_device_clear_reset_pending() - This function will clear if any pending
537 * reset condition flags on the device.
538 * @request: This parameter is the isci_device object.
540 * true if there is a reset pending for the device.
542 void isci_device_clear_reset_pending(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
544 struct isci_request
*isci_request
;
545 struct isci_request
*tmp_req
;
546 unsigned long flags
= 0;
548 dev_dbg(&ihost
->pdev
->dev
, "%s: idev=%p, ihost=%p\n",
549 __func__
, idev
, ihost
);
551 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
553 /* Clear reset pending on all pending requests. */
554 list_for_each_entry_safe(isci_request
, tmp_req
,
555 &idev
->reqs_in_process
, dev_node
) {
556 dev_dbg(&ihost
->pdev
->dev
, "%s: idev = %p request = %p\n",
557 __func__
, idev
, isci_request
);
559 if (isci_request
->ttype
== io_task
) {
561 unsigned long flags2
;
562 struct sas_task
*task
= isci_request_access_task(
565 spin_lock_irqsave(&task
->task_state_lock
, flags2
);
566 task
->task_state_flags
&= ~SAS_TASK_NEED_DEV_RESET
;
567 spin_unlock_irqrestore(&task
->task_state_lock
, flags2
);
570 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
574 * isci_remote_device_change_state() - This function gets the status of the
575 * remote_device object.
576 * @isci_device: This parameter points to the isci_remote_device object
578 * status of the object as a isci_status enum.
580 void isci_remote_device_change_state(
581 struct isci_remote_device
*isci_device
,
582 enum isci_status status
)
586 spin_lock_irqsave(&isci_device
->state_lock
, flags
);
587 isci_device
->status
= status
;
588 spin_unlock_irqrestore(&isci_device
->state_lock
, flags
);