blob: 1553221fe4d7e3ea950cee531abea02e676cbb8b [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56#include "isci.h"
57#include "scic_io_request.h"
58#include "scic_remote_device.h"
59#include "scic_phy.h"
60#include "scic_port.h"
61#include "port.h"
62#include "remote_device.h"
63#include "request.h"
64#include "task.h"
65
66
67
68/**
69 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -080070 * @ihost: This parameter specifies the isci host object.
71 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -070072 *
73 */
Dan Williamsd9c37392011-03-03 17:59:32 -080074static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -070075{
Dan Williamsd9c37392011-03-03 17:59:32 -080076 dev_dbg(&ihost->pdev->dev,
77 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -070078
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 */
Dan Williamsd9c37392011-03-03 17:59:32 -080083 while (!list_empty(&idev->reqs_in_process)) {
Dan Williams6f231dd2011-07-02 22:56:22 -070084
Dan Williamsd9c37392011-03-03 17:59:32 -080085 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -070086 "%s: ** request list not empty! **\n", __func__);
87 BUG();
88 }
89
Dan Williams57f20f42011-04-21 18:14:45 -070090 scic_remote_device_destruct(&idev->sci);
Dan Williamsd9c37392011-03-03 17:59:32 -080091 idev->domain_dev->lldd_dev = NULL;
92 idev->domain_dev = NULL;
93 idev->isci_port = NULL;
94 list_del_init(&idev->node);
Dan Williams6ad31fe2011-03-04 12:10:29 -080095
Dan Williamsd9c37392011-03-03 17:59:32 -080096 clear_bit(IDEV_START_PENDING, &idev->flags);
97 clear_bit(IDEV_STOP_PENDING, &idev->flags);
98 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -070099}
100
101
102/**
103 * isci_remote_device_construct() - This function calls the scic remote device
104 * construct and start functions, it waits on the remote device start
105 * completion.
106 * @port: This parameter specifies the isci port with the remote device.
107 * @isci_device: This parameter specifies the isci remote device
108 *
109 * status from the scic calls, the caller to this function should clean up
110 * resources as appropriate.
111 */
112static enum sci_status isci_remote_device_construct(
113 struct isci_port *port,
114 struct isci_remote_device *isci_device)
115{
116 enum sci_status status = SCI_SUCCESS;
117
118 /* let the core do it's common constuction. */
119 scic_remote_device_construct(port->sci_port_handle,
Dan Williams57f20f42011-04-21 18:14:45 -0700120 &isci_device->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700121
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)) {
125 int i;
126
127 /* struct smp_response_discover discover_response; */
128 struct discover_resp discover_response;
129 struct domain_device *parent =
130 isci_device->domain_dev->parent;
131
132 struct expander_device *parent_ex = &parent->ex_dev;
133
134 for (i = 0; i < parent_ex->num_phys; i++) {
135
136 struct ex_phy *phy = &parent_ex->ex_phy[i];
137
138 if ((phy->phy_state == PHY_VACANT) ||
139 (phy->phy_state == PHY_NOT_PRESENT))
140 continue;
141
142 if (SAS_ADDR(phy->attached_sas_addr)
143 == SAS_ADDR(isci_device->domain_dev->sas_addr)) {
144
145 discover_response.attached_dev_type
146 = phy->attached_dev_type;
147 discover_response.linkrate
148 = phy->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;
159 memcpy(
160 discover_response.attached_sas_addr,
161 phy->attached_sas_addr,
162 SAS_ADDR_SIZE
163 );
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
169 = phy->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;
178 }
179 }
180
181
182 dev_dbg(&port->isci_host->pdev->dev,
183 "%s: parent->dev_type = EDGE_DEV\n",
184 __func__);
185
Dan Williams57f20f42011-04-21 18:14:45 -0700186 status = scic_remote_device_ea_construct(&isci_device->sci,
Dan Williams3a97eec2011-03-04 11:51:43 -0800187 (struct smp_response_discover *)&discover_response);
Dan Williams6f231dd2011-07-02 22:56:22 -0700188
189 } else
Dan Williams57f20f42011-04-21 18:14:45 -0700190 status = scic_remote_device_da_construct(&isci_device->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700191
192
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",
197 __func__,
198 isci_device);
199
200 return status;
201 }
202
Dan Williams57f20f42011-04-21 18:14:45 -0700203 /* XXX will be killed with sci_base_object removal */
204 sci_object_set_association(&isci_device->sci, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700205
Dan Williams6f231dd2011-07-02 22:56:22 -0700206 /* start the device. */
Dan Williams57f20f42011-04-21 18:14:45 -0700207 status = scic_remote_device_start(&isci_device->sci,
Dan Williams3a97eec2011-03-04 11:51:43 -0800208 ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -0700209
210 if (status != SCI_SUCCESS) {
211 dev_warn(&port->isci_host->pdev->dev,
212 "%s: scic_remote_device_start failed\n",
213 __func__);
214 return status;
215 }
216
217 return status;
218}
219
Dan Williams4393aa42011-03-31 13:10:44 -0700220void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700221{
222 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700223
Dan Williams4393aa42011-03-31 13:10:44 -0700224 dev_dbg(&ihost->pdev->dev,
225 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700226
227 /* Cleanup all requests pending for this device. */
Dan Williams4393aa42011-03-31 13:10:44 -0700228 isci_terminate_pending_requests(ihost, idev, terminating);
Dan Williams6f231dd2011-07-02 22:56:22 -0700229
Dan Williams4393aa42011-03-31 13:10:44 -0700230 dev_dbg(&ihost->pdev->dev,
231 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700232}
233
Dan Williams6f231dd2011-07-02 22:56:22 -0700234/**
235 * This function builds the isci_remote_device when a libsas dev_found message
236 * is received.
237 * @isci_host: This parameter specifies the isci host object.
238 * @port: This parameter specifies the isci_port conected to this device.
239 *
240 * pointer to new isci_remote_device.
241 */
242static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -0800243isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -0700244{
Dan Williamsd9c37392011-03-03 17:59:32 -0800245 struct isci_remote_device *idev;
246 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -0700247
Dan Williamsd9c37392011-03-03 17:59:32 -0800248 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -0700249 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -0800250 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
251 break;
252 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700253
Dan Williamsd9c37392011-03-03 17:59:32 -0800254 if (i >= SCI_MAX_REMOTE_DEVICES) {
255 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700256 return NULL;
257 }
258
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700259 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
260 return NULL;
261
262 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
263 return NULL;
264
Dan Williamsd9c37392011-03-03 17:59:32 -0800265 isci_remote_device_change_state(idev, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -0700266
Dan Williamsd9c37392011-03-03 17:59:32 -0800267 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700268}
Dan Williams6f231dd2011-07-02 22:56:22 -0700269
270/**
271 * isci_remote_device_ready() - This function is called by the scic when the
272 * remote device is ready. We mark the isci device as ready and signal the
273 * waiting proccess.
Dan Williams037afc72011-03-31 13:10:42 -0700274 * @ihost: our valid isci_host
275 * @idev: remote device
Dan Williams6f231dd2011-07-02 22:56:22 -0700276 *
277 */
Dan Williams037afc72011-03-31 13:10:42 -0700278void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700279{
Dan Williams6ad31fe2011-03-04 12:10:29 -0800280 dev_dbg(&ihost->pdev->dev,
Dan Williams1a380452011-03-03 18:01:43 -0800281 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700282
Dan Williams6ad31fe2011-03-04 12:10:29 -0800283 isci_remote_device_change_state(idev, isci_ready_for_io);
284 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
285 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700286}
287
288/**
289 * isci_remote_device_not_ready() - This function is called by the scic when
290 * the remote device is not ready. We mark the isci device as ready (not
291 * "ready_for_io") and signal the waiting proccess.
292 * @isci_host: This parameter specifies the isci host object.
293 * @isci_device: This parameter specifies the remote device
294 *
295 */
Dan Williams037afc72011-03-31 13:10:42 -0700296void isci_remote_device_not_ready(struct isci_host *ihost,
297 struct isci_remote_device *idev, u32 reason)
Dan Williams6f231dd2011-07-02 22:56:22 -0700298{
Dan Williams037afc72011-03-31 13:10:42 -0700299 dev_dbg(&ihost->pdev->dev,
300 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700301
Dan Williams037afc72011-03-31 13:10:42 -0700302 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
303 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -0700304 else
305 /* device ready is actually a "not ready for io" state. */
Dan Williams037afc72011-03-31 13:10:42 -0700306 isci_remote_device_change_state(idev, isci_ready);
Dan Williams6f231dd2011-07-02 22:56:22 -0700307}
308
309/**
310 * isci_remote_device_stop_complete() - This function is called by the scic
311 * when the remote device stop has completed. We mark the isci device as not
312 * ready and remove the isci remote device.
313 * @isci_host: This parameter specifies the isci host object.
314 * @isci_device: This parameter specifies the remote device.
315 * @status: This parameter specifies status of the completion.
316 *
317 */
318void isci_remote_device_stop_complete(
319 struct isci_host *isci_host,
320 struct isci_remote_device *isci_device,
321 enum sci_status status)
322{
Dan Williams6f231dd2011-07-02 22:56:22 -0700323 dev_dbg(&isci_host->pdev->dev,
324 "%s: complete isci_device = %p, status = 0x%x\n",
325 __func__,
326 isci_device,
327 status);
328
329 isci_remote_device_change_state(isci_device, isci_stopped);
330
331 /* after stop, we can tear down resources. */
332 isci_remote_device_deconstruct(isci_host, isci_device);
333
Dan Williams6f231dd2011-07-02 22:56:22 -0700334}
335
336/**
337 * isci_remote_device_start_complete() - This function is called by the scic
338 * when the remote device start has completed
339 * @isci_host: This parameter specifies the isci host object.
340 * @isci_device: This parameter specifies the remote device.
341 * @status: This parameter specifies status of the completion.
342 *
343 */
344void isci_remote_device_start_complete(
345 struct isci_host *isci_host,
346 struct isci_remote_device *isci_device,
347 enum sci_status status)
348{
349
350
351}
352
353
354/**
355 * isci_remote_device_stop() - This function is called internally to stop the
356 * remote device.
357 * @isci_host: This parameter specifies the isci host object.
358 * @isci_device: This parameter specifies the remote device.
359 *
360 * The status of the scic request to stop.
361 */
Dan Williams6ad31fe2011-03-04 12:10:29 -0800362enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700363{
364 enum sci_status status;
365 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700366
Dan Williams6ad31fe2011-03-04 12:10:29 -0800367 dev_dbg(&ihost->pdev->dev,
368 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700369
Dan Williams6ad31fe2011-03-04 12:10:29 -0800370 isci_remote_device_change_state(idev, isci_stopping);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -0700371
372 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -0700373 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -0700374
Dan Williams6ad31fe2011-03-04 12:10:29 -0800375 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700376
Dan Williams6ad31fe2011-03-04 12:10:29 -0800377 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams57f20f42011-04-21 18:14:45 -0700378 status = scic_remote_device_stop(&idev->sci, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800379 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700380
381 /* Wait for the stop complete callback. */
Dan Williamsd9c37392011-03-03 17:59:32 -0800382 if (status == SCI_SUCCESS) {
Dan Williams6ad31fe2011-03-04 12:10:29 -0800383 wait_for_device_stop(ihost, idev);
Dan Williamsd9c37392011-03-03 17:59:32 -0800384 clear_bit(IDEV_ALLOCATED, &idev->flags);
385 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700386
Dan Williams6ad31fe2011-03-04 12:10:29 -0800387 dev_dbg(&ihost->pdev->dev,
388 "%s: idev = %p - after completion wait\n",
389 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700390
Dan Williams6f231dd2011-07-02 22:56:22 -0700391 return status;
392}
393
394/**
395 * isci_remote_device_gone() - This function is called by libsas when a domain
396 * device is removed.
397 * @domain_device: This parameter specifies the libsas domain device.
398 *
399 */
Dan Williams6ad31fe2011-03-04 12:10:29 -0800400void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700401{
Dan Williams4393aa42011-03-31 13:10:44 -0700402 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -0800403 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700404
Dan Williams6ad31fe2011-03-04 12:10:29 -0800405 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700406 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -0800407 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -0700408
Dan Williams6ad31fe2011-03-04 12:10:29 -0800409 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700410}
411
412
413/**
414 * isci_remote_device_found() - This function is called by libsas when a remote
415 * device is discovered. A remote device object is created and started. the
416 * function then sleeps until the sci core device started message is
417 * received.
418 * @domain_device: This parameter specifies the libsas domain device.
419 *
420 * status, zero indicates success.
421 */
422int isci_remote_device_found(struct domain_device *domain_dev)
423{
Dan Williams4393aa42011-03-31 13:10:44 -0700424 struct isci_host *isci_host = dev_to_ihost(domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700425 struct isci_port *isci_port;
426 struct isci_phy *isci_phy;
427 struct asd_sas_port *sas_port;
428 struct asd_sas_phy *sas_phy;
429 struct isci_remote_device *isci_device;
430 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700431
Dan Williams6f231dd2011-07-02 22:56:22 -0700432 dev_dbg(&isci_host->pdev->dev,
433 "%s: domain_device = %p\n", __func__, domain_dev);
434
Dan Williams0cf89d12011-02-18 09:25:07 -0800435 wait_for_start(isci_host);
436
Dan Williams6f231dd2011-07-02 22:56:22 -0700437 sas_port = domain_dev->port;
438 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
439 port_phy_el);
440 isci_phy = to_isci_phy(sas_phy);
441 isci_port = isci_phy->isci_port;
442
443 /* we are being called for a device on this port,
444 * so it has to come up eventually
445 */
446 wait_for_completion(&isci_port->start_complete);
447
448 if ((isci_stopping == isci_port_get_state(isci_port)) ||
449 (isci_stopped == isci_port_get_state(isci_port)))
450 return -ENODEV;
451
452 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -0800453 if (!isci_device)
454 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -0700455
456 INIT_LIST_HEAD(&isci_device->node);
457 domain_dev->lldd_dev = isci_device;
458 isci_device->domain_dev = domain_dev;
459 isci_device->isci_port = isci_port;
460 isci_remote_device_change_state(isci_device, isci_starting);
461
462
Dan Williams1a380452011-03-03 18:01:43 -0800463 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700464 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
465
Dan Williams6ad31fe2011-03-04 12:10:29 -0800466 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700467 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams1a380452011-03-03 18:01:43 -0800468 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700469
Dan Williams6f231dd2011-07-02 22:56:22 -0700470 dev_dbg(&isci_host->pdev->dev,
471 "%s: isci_device = %p\n",
472 __func__, isci_device);
473
474 if (status != SCI_SUCCESS) {
475
Dan Williams1a380452011-03-03 18:01:43 -0800476 spin_lock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700477 isci_remote_device_deconstruct(
478 isci_host,
479 isci_device
480 );
Dan Williams1a380452011-03-03 18:01:43 -0800481 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700482 return -ENODEV;
483 }
484
Dan Williams6ad31fe2011-03-04 12:10:29 -0800485 /* wait for the device ready callback. */
486 wait_for_device_start(isci_host, isci_device);
487
Dan Williams6f231dd2011-07-02 22:56:22 -0700488 return 0;
489}
490/**
491 * isci_device_is_reset_pending() - This function will check if there is any
492 * pending reset condition on the device.
493 * @request: This parameter is the isci_device object.
494 *
495 * true if there is a reset pending for the device.
496 */
497bool isci_device_is_reset_pending(
498 struct isci_host *isci_host,
499 struct isci_remote_device *isci_device)
500{
501 struct isci_request *isci_request;
502 struct isci_request *tmp_req;
503 bool reset_is_pending = false;
504 unsigned long flags;
505
506 dev_dbg(&isci_host->pdev->dev,
507 "%s: isci_device = %p\n", __func__, isci_device);
508
509 spin_lock_irqsave(&isci_host->scic_lock, flags);
510
511 /* Check for reset on all pending requests. */
512 list_for_each_entry_safe(isci_request, tmp_req,
513 &isci_device->reqs_in_process, dev_node) {
514 dev_dbg(&isci_host->pdev->dev,
515 "%s: isci_device = %p request = %p\n",
516 __func__, isci_device, isci_request);
517
518 if (isci_request->ttype == io_task) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700519 struct sas_task *task = isci_request_access_task(
520 isci_request);
521
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700522 spin_lock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700523 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
524 reset_is_pending = true;
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700525 spin_unlock(&task->task_state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700526 }
527 }
528
529 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
530
531 dev_dbg(&isci_host->pdev->dev,
532 "%s: isci_device = %p reset_is_pending = %d\n",
533 __func__, isci_device, reset_is_pending);
534
535 return reset_is_pending;
536}
537
538/**
539 * isci_device_clear_reset_pending() - This function will clear if any pending
540 * reset condition flags on the device.
541 * @request: This parameter is the isci_device object.
542 *
543 * true if there is a reset pending for the device.
544 */
Dan Williams4393aa42011-03-31 13:10:44 -0700545void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700546{
547 struct isci_request *isci_request;
548 struct isci_request *tmp_req;
Dan Williams6f231dd2011-07-02 22:56:22 -0700549 unsigned long flags = 0;
550
Dan Williams4393aa42011-03-31 13:10:44 -0700551 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
552 __func__, idev, ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -0700553
Dan Williams4393aa42011-03-31 13:10:44 -0700554 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700555
556 /* Clear reset pending on all pending requests. */
557 list_for_each_entry_safe(isci_request, tmp_req,
Dan Williams4393aa42011-03-31 13:10:44 -0700558 &idev->reqs_in_process, dev_node) {
559 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
560 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700561
562 if (isci_request->ttype == io_task) {
563
564 unsigned long flags2;
565 struct sas_task *task = isci_request_access_task(
566 isci_request);
567
568 spin_lock_irqsave(&task->task_state_lock, flags2);
569 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
570 spin_unlock_irqrestore(&task->task_state_lock, flags2);
571 }
572 }
Dan Williams4393aa42011-03-31 13:10:44 -0700573 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700574}
575
576/**
577 * isci_remote_device_change_state() - This function gets the status of the
578 * remote_device object.
579 * @isci_device: This parameter points to the isci_remote_device object
580 *
581 * status of the object as a isci_status enum.
582 */
583void isci_remote_device_change_state(
584 struct isci_remote_device *isci_device,
585 enum isci_status status)
586{
587 unsigned long flags;
588
Dan Williams6f231dd2011-07-02 22:56:22 -0700589 spin_lock_irqsave(&isci_device->state_lock, flags);
590 isci_device->status = status;
591 spin_unlock_irqrestore(&isci_device->state_lock, flags);
592}