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 * @isci_host: This parameter specifies the isci host object.
71 * @isci_device: This parameter specifies the remote device to be freed.
74 static void isci_remote_device_deconstruct(
75 struct isci_host
*isci_host
,
76 struct isci_remote_device
*isci_device
)
78 dev_dbg(&isci_host
->pdev
->dev
,
79 "%s: isci_device = %p\n", __func__
, isci_device
);
81 /* There should not be any outstanding io's. All paths to
82 * here should go through isci_remote_device_nuke_requests.
83 * If we hit this condition, we will need a way to complete
84 * io requests in process */
85 while (!list_empty(&isci_device
->reqs_in_process
)) {
87 dev_err(&isci_host
->pdev
->dev
,
88 "%s: ** request list not empty! **\n", __func__
);
92 /* Remove all related references to this device and free
95 scic_remote_device_destruct(to_sci_dev(isci_device
));
96 isci_device
->domain_dev
->lldd_dev
= NULL
;
97 list_del(&isci_device
->node
);
99 clear_bit(IDEV_STOP_PENDING
, &isci_device
->flags
);
100 clear_bit(IDEV_START_PENDING
, &isci_device
->flags
);
101 wake_up(&isci_host
->eventq
);
102 complete(isci_device
->cmp
);
103 kmem_cache_free(isci_kmem_cache
, isci_device
);
108 * isci_remote_device_construct() - This function calls the scic remote device
109 * construct and start functions, it waits on the remote device start
111 * @port: This parameter specifies the isci port with the remote device.
112 * @isci_device: This parameter specifies the isci remote device
114 * status from the scic calls, the caller to this function should clean up
115 * resources as appropriate.
117 static enum sci_status
isci_remote_device_construct(
118 struct isci_port
*port
,
119 struct isci_remote_device
*isci_device
)
121 enum sci_status status
= SCI_SUCCESS
;
123 /* let the core do it's common constuction. */
124 scic_remote_device_construct(port
->sci_port_handle
,
125 to_sci_dev(isci_device
));
127 /* let the core do it's device specific constuction. */
128 if (isci_device
->domain_dev
->parent
&&
129 (isci_device
->domain_dev
->parent
->dev_type
== EDGE_DEV
)) {
132 /* struct smp_response_discover discover_response; */
133 struct discover_resp discover_response
;
134 struct domain_device
*parent
=
135 isci_device
->domain_dev
->parent
;
137 struct expander_device
*parent_ex
= &parent
->ex_dev
;
139 for (i
= 0; i
< parent_ex
->num_phys
; i
++) {
141 struct ex_phy
*phy
= &parent_ex
->ex_phy
[i
];
143 if ((phy
->phy_state
== PHY_VACANT
) ||
144 (phy
->phy_state
== PHY_NOT_PRESENT
))
147 if (SAS_ADDR(phy
->attached_sas_addr
)
148 == SAS_ADDR(isci_device
->domain_dev
->sas_addr
)) {
150 discover_response
.attached_dev_type
151 = phy
->attached_dev_type
;
152 discover_response
.linkrate
154 discover_response
.attached_sata_host
155 = phy
->attached_sata_host
;
156 discover_response
.attached_sata_dev
157 = phy
->attached_sata_dev
;
158 discover_response
.attached_sata_ps
159 = phy
->attached_sata_ps
;
160 discover_response
.iproto
161 = phy
->attached_iproto
>> 1;
162 discover_response
.tproto
163 = phy
->attached_tproto
>> 1;
165 discover_response
.attached_sas_addr
,
166 phy
->attached_sas_addr
,
169 discover_response
.attached_phy_id
170 = phy
->attached_phy_id
;
171 discover_response
.change_count
172 = phy
->phy_change_count
;
173 discover_response
.routing_attr
175 discover_response
.hmin_linkrate
176 = phy
->phy
->minimum_linkrate_hw
;
177 discover_response
.hmax_linkrate
178 = phy
->phy
->maximum_linkrate_hw
;
179 discover_response
.pmin_linkrate
180 = phy
->phy
->minimum_linkrate
;
181 discover_response
.pmax_linkrate
182 = phy
->phy
->maximum_linkrate
;
187 dev_dbg(&port
->isci_host
->pdev
->dev
,
188 "%s: parent->dev_type = EDGE_DEV\n",
191 status
= scic_remote_device_ea_construct(to_sci_dev(isci_device
),
192 (struct smp_response_discover
*)&discover_response
);
195 status
= scic_remote_device_da_construct(to_sci_dev(isci_device
));
198 if (status
!= SCI_SUCCESS
) {
199 dev_dbg(&port
->isci_host
->pdev
->dev
,
200 "%s: scic_remote_device_da_construct failed - "
201 "isci_device = %p\n",
208 sci_object_set_association(to_sci_dev(isci_device
), isci_device
);
210 BUG_ON(port
->isci_host
== NULL
);
212 /* start the device. */
213 status
= scic_remote_device_start(to_sci_dev(isci_device
),
214 ISCI_REMOTE_DEVICE_START_TIMEOUT
);
216 if (status
!= SCI_SUCCESS
) {
217 dev_warn(&port
->isci_host
->pdev
->dev
,
218 "%s: scic_remote_device_start failed\n",
228 * isci_remote_device_nuke_requests() - This function terminates all requests
229 * for a given remote device.
230 * @isci_device: This parameter specifies the remote device
233 void isci_remote_device_nuke_requests(
234 struct isci_remote_device
*isci_device
)
236 DECLARE_COMPLETION_ONSTACK(aborted_task_completion
);
237 struct isci_host
*isci_host
;
239 isci_host
= isci_device
->isci_port
->isci_host
;
241 dev_dbg(&isci_host
->pdev
->dev
,
242 "%s: isci_device = %p\n", __func__
, isci_device
);
244 /* Cleanup all requests pending for this device. */
245 isci_terminate_pending_requests(isci_host
, isci_device
, terminating
);
247 dev_dbg(&isci_host
->pdev
->dev
,
248 "%s: isci_device = %p, done\n", __func__
, isci_device
);
254 * This function builds the isci_remote_device when a libsas dev_found message
256 * @isci_host: This parameter specifies the isci host object.
257 * @port: This parameter specifies the isci_port conected to this device.
259 * pointer to new isci_remote_device.
261 static struct isci_remote_device
*
262 isci_remote_device_alloc(struct isci_host
*isci_host
, struct isci_port
*port
)
264 struct isci_remote_device
*isci_device
;
266 isci_device
= kmem_cache_zalloc(isci_kmem_cache
, GFP_KERNEL
);
269 dev_warn(&isci_host
->pdev
->dev
, "%s: failed\n", __func__
);
273 INIT_LIST_HEAD(&isci_device
->reqs_in_process
);
274 INIT_LIST_HEAD(&isci_device
->node
);
276 spin_lock_init(&isci_device
->state_lock
);
277 isci_remote_device_change_state(isci_device
, isci_freed
);
284 * isci_remote_device_ready() - This function is called by the scic when the
285 * remote device is ready. We mark the isci device as ready and signal the
287 * @idev: This parameter specifies the remote device
290 void isci_remote_device_ready(struct isci_remote_device
*idev
)
292 struct isci_host
*ihost
= idev
->isci_port
->isci_host
;
295 dev_dbg(&ihost
->pdev
->dev
,
296 "%s: isci_device = %p\n", __func__
, idev
);
298 spin_lock_irqsave(&idev
->isci_port
->remote_device_lock
, flags
);
299 isci_remote_device_change_state(idev
, isci_ready_for_io
);
300 if (test_and_clear_bit(IDEV_START_PENDING
, &idev
->flags
))
301 wake_up(&ihost
->eventq
);
302 spin_unlock_irqrestore(&idev
->isci_port
->remote_device_lock
, flags
);
306 * isci_remote_device_not_ready() - This function is called by the scic when
307 * the remote device is not ready. We mark the isci device as ready (not
308 * "ready_for_io") and signal the waiting proccess.
309 * @isci_host: This parameter specifies the isci host object.
310 * @isci_device: This parameter specifies the remote device
313 void isci_remote_device_not_ready(
314 struct isci_remote_device
*isci_device
,
317 dev_dbg(&isci_device
->isci_port
->isci_host
->pdev
->dev
,
318 "%s: isci_device = %p\n", __func__
, isci_device
);
320 if (reason_code
== SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED
)
321 isci_remote_device_change_state(isci_device
, isci_stopping
);
323 /* device ready is actually a "not ready for io" state. */
324 isci_remote_device_change_state(isci_device
, isci_ready
);
328 * isci_remote_device_stop_complete() - This function is called by the scic
329 * when the remote device stop has completed. We mark the isci device as not
330 * ready and remove the isci remote device.
331 * @isci_host: This parameter specifies the isci host object.
332 * @isci_device: This parameter specifies the remote device.
333 * @status: This parameter specifies status of the completion.
336 void isci_remote_device_stop_complete(
337 struct isci_host
*isci_host
,
338 struct isci_remote_device
*isci_device
,
339 enum sci_status status
)
341 dev_dbg(&isci_host
->pdev
->dev
,
342 "%s: complete isci_device = %p, status = 0x%x\n",
347 isci_remote_device_change_state(isci_device
, isci_stopped
);
349 /* after stop, we can tear down resources. */
350 isci_remote_device_deconstruct(isci_host
, isci_device
);
355 * isci_remote_device_start_complete() - This function is called by the scic
356 * when the remote device start has completed
357 * @isci_host: This parameter specifies the isci host object.
358 * @isci_device: This parameter specifies the remote device.
359 * @status: This parameter specifies status of the completion.
362 void isci_remote_device_start_complete(
363 struct isci_host
*isci_host
,
364 struct isci_remote_device
*isci_device
,
365 enum sci_status status
)
373 * isci_remote_device_stop() - This function is called internally to stop the
375 * @isci_host: This parameter specifies the isci host object.
376 * @isci_device: This parameter specifies the remote device.
378 * The status of the scic request to stop.
380 enum sci_status
isci_remote_device_stop(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
382 enum sci_status status
;
384 DECLARE_COMPLETION_ONSTACK(completion
);
386 dev_dbg(&ihost
->pdev
->dev
,
387 "%s: isci_device = %p\n", __func__
, idev
);
389 isci_remote_device_change_state(idev
, isci_stopping
);
390 set_bit(IDEV_STOP_PENDING
, &idev
->flags
);
391 idev
->cmp
= &completion
;
393 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
395 status
= scic_remote_device_stop(to_sci_dev(idev
), 50);
397 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
399 /* Wait for the stop complete callback. */
400 if (status
== SCI_SUCCESS
)
401 wait_for_device_stop(ihost
, idev
);
403 dev_dbg(&ihost
->pdev
->dev
,
404 "%s: idev = %p - after completion wait\n",
411 * isci_remote_device_gone() - This function is called by libsas when a domain
413 * @domain_device: This parameter specifies the libsas domain device.
416 void isci_remote_device_gone(struct domain_device
*dev
)
418 struct isci_host
*ihost
= dev
->port
->ha
->lldd_ha
;
419 struct isci_remote_device
*idev
= dev
->lldd_dev
;
421 dev_dbg(&ihost
->pdev
->dev
,
422 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
423 __func__
, dev
, idev
, idev
->isci_port
);
425 isci_remote_device_stop(ihost
, idev
);
430 * isci_remote_device_found() - This function is called by libsas when a remote
431 * device is discovered. A remote device object is created and started. the
432 * function then sleeps until the sci core device started message is
434 * @domain_device: This parameter specifies the libsas domain device.
436 * status, zero indicates success.
438 int isci_remote_device_found(struct domain_device
*domain_dev
)
441 struct isci_host
*isci_host
;
442 struct isci_port
*isci_port
;
443 struct isci_phy
*isci_phy
;
444 struct asd_sas_port
*sas_port
;
445 struct asd_sas_phy
*sas_phy
;
446 struct isci_remote_device
*isci_device
;
447 enum sci_status status
;
449 isci_host
= isci_host_from_sas_ha(domain_dev
->port
->ha
);
451 dev_dbg(&isci_host
->pdev
->dev
,
452 "%s: domain_device = %p\n", __func__
, domain_dev
);
454 wait_for_start(isci_host
);
456 sas_port
= domain_dev
->port
;
457 sas_phy
= list_first_entry(&sas_port
->phy_list
, struct asd_sas_phy
,
459 isci_phy
= to_isci_phy(sas_phy
);
460 isci_port
= isci_phy
->isci_port
;
462 /* we are being called for a device on this port,
463 * so it has to come up eventually
465 wait_for_completion(&isci_port
->start_complete
);
467 if ((isci_stopping
== isci_port_get_state(isci_port
)) ||
468 (isci_stopped
== isci_port_get_state(isci_port
)))
471 isci_device
= isci_remote_device_alloc(isci_host
, isci_port
);
473 INIT_LIST_HEAD(&isci_device
->node
);
474 domain_dev
->lldd_dev
= isci_device
;
475 isci_device
->domain_dev
= domain_dev
;
476 isci_device
->isci_port
= isci_port
;
477 isci_remote_device_change_state(isci_device
, isci_starting
);
480 spin_lock_irqsave(&isci_port
->remote_device_lock
, flags
);
481 list_add_tail(&isci_device
->node
, &isci_port
->remote_dev_list
);
483 set_bit(IDEV_START_PENDING
, &isci_device
->flags
);
484 status
= isci_remote_device_construct(isci_port
, isci_device
);
485 spin_unlock_irqrestore(&isci_port
->remote_device_lock
, flags
);
487 dev_dbg(&isci_host
->pdev
->dev
,
488 "%s: isci_device = %p\n",
489 __func__
, isci_device
);
491 if (status
!= SCI_SUCCESS
) {
493 spin_lock_irqsave(&isci_port
->remote_device_lock
, flags
);
494 isci_remote_device_deconstruct(
498 spin_unlock_irqrestore(&isci_port
->remote_device_lock
, flags
);
502 /* wait for the device ready callback. */
503 wait_for_device_start(isci_host
, isci_device
);
508 * isci_device_is_reset_pending() - This function will check if there is any
509 * pending reset condition on the device.
510 * @request: This parameter is the isci_device object.
512 * true if there is a reset pending for the device.
514 bool isci_device_is_reset_pending(
515 struct isci_host
*isci_host
,
516 struct isci_remote_device
*isci_device
)
518 struct isci_request
*isci_request
;
519 struct isci_request
*tmp_req
;
520 bool reset_is_pending
= false;
523 dev_dbg(&isci_host
->pdev
->dev
,
524 "%s: isci_device = %p\n", __func__
, isci_device
);
526 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
528 /* Check for reset on all pending requests. */
529 list_for_each_entry_safe(isci_request
, tmp_req
,
530 &isci_device
->reqs_in_process
, dev_node
) {
531 dev_dbg(&isci_host
->pdev
->dev
,
532 "%s: isci_device = %p request = %p\n",
533 __func__
, isci_device
, isci_request
);
535 if (isci_request
->ttype
== io_task
) {
538 struct sas_task
*task
= isci_request_access_task(
541 spin_lock_irqsave(&task
->task_state_lock
, flags
);
542 if (task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
)
543 reset_is_pending
= true;
544 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
548 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
550 dev_dbg(&isci_host
->pdev
->dev
,
551 "%s: isci_device = %p reset_is_pending = %d\n",
552 __func__
, isci_device
, reset_is_pending
);
554 return reset_is_pending
;
558 * isci_device_clear_reset_pending() - This function will clear if any pending
559 * reset condition flags on the device.
560 * @request: This parameter is the isci_device object.
562 * true if there is a reset pending for the device.
564 void isci_device_clear_reset_pending(struct isci_remote_device
*isci_device
)
566 struct isci_request
*isci_request
;
567 struct isci_request
*tmp_req
;
568 struct isci_host
*isci_host
= NULL
;
569 unsigned long flags
= 0;
571 /* FIXME more port gone confusion, and this time it makes the
574 if (isci_device
->isci_port
!= NULL
)
575 isci_host
= isci_device
->isci_port
->isci_host
;
578 * FIXME when the isci_host gets sorted out
581 pr_debug("%s: isci_device=%p, isci_host=%p\n",
582 __func__
, isci_device
, isci_host
);
584 if (isci_host
!= NULL
)
585 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
587 pr_err("%s: isci_device %p; isci_host == NULL!\n",
588 __func__
, isci_device
);
590 /* Clear reset pending on all pending requests. */
591 list_for_each_entry_safe(isci_request
, tmp_req
,
592 &isci_device
->reqs_in_process
, dev_node
) {
594 * FIXME when the conditional spinlock is gone
595 * change to dev_dbg()
597 pr_debug("%s: isci_device = %p request = %p\n",
598 __func__
, isci_device
, isci_request
);
600 if (isci_request
->ttype
== io_task
) {
602 unsigned long flags2
;
603 struct sas_task
*task
= isci_request_access_task(
606 spin_lock_irqsave(&task
->task_state_lock
, flags2
);
607 task
->task_state_flags
&= ~SAS_TASK_NEED_DEV_RESET
;
608 spin_unlock_irqrestore(&task
->task_state_lock
, flags2
);
612 if (isci_host
!= NULL
)
613 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
617 * isci_remote_device_change_state() - This function gets the status of the
618 * remote_device object.
619 * @isci_device: This parameter points to the isci_remote_device object
621 * status of the object as a isci_status enum.
623 void isci_remote_device_change_state(
624 struct isci_remote_device
*isci_device
,
625 enum isci_status status
)
629 spin_lock_irqsave(&isci_device
->state_lock
, flags
);
630 isci_device
->status
= status
;
631 spin_unlock_irqrestore(&isci_device
->state_lock
, flags
);