blob: a6a376e200ef925abcb95db206a3b4e012393ecd [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
Dan Williams88f3b622011-04-22 19:18:03 -070056#ifndef _ISCI_REMOTE_DEVICE_H_
Dan Williams6f231dd2011-07-02 22:56:22 -070057#define _ISCI_REMOTE_DEVICE_H_
Dan Williams88f3b622011-04-22 19:18:03 -070058#include <scsi/libsas.h>
Dan Williams209fae12011-06-13 17:39:44 -070059#include <linux/kref.h>
Dan Williams88f3b622011-04-22 19:18:03 -070060#include "scu_remote_node_context.h"
61#include "remote_node_context.h"
62#include "port.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063
Dan Williams89a73012011-06-30 19:14:33 -070064enum sci_remote_device_not_ready_reason_code {
Dan Williams88f3b622011-04-22 19:18:03 -070065 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED,
66 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED,
67 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED,
68 SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED,
69 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED,
Dan Williams88f3b622011-04-22 19:18:03 -070070 SCIC_REMOTE_DEVICE_NOT_READY_REASON_CODE_MAX
Dan Williams88f3b622011-04-22 19:18:03 -070071};
72
Dan Williams78a6f062011-06-30 16:31:37 -070073/**
74 * isci_remote_device - isci representation of a sas expander / end point
75 * @device_port_width: hw setting for number of simultaneous connections
76 * @connection_rate: per-taskcontext connection rate for this device
77 * @working_request: SATA requests have no tag we for unaccelerated
78 * protocols we need a method to associate unsolicited
79 * frames with a pending request
80 */
Dan Williams6f231dd2011-07-02 22:56:22 -070081struct isci_remote_device {
Dan Williams6ad31fe2011-03-04 12:10:29 -080082 #define IDEV_START_PENDING 0
83 #define IDEV_STOP_PENDING 1
Dan Williamsd9c37392011-03-03 17:59:32 -080084 #define IDEV_ALLOCATED 2
Dan Williams5a998322011-12-12 20:32:09 -080085 #define IDEV_GONE 3
86 #define IDEV_IO_READY 4
87 #define IDEV_IO_NCQERROR 5
Jeff Skirvin726980d2012-03-08 22:41:50 -080088 #define IDEV_TXRX_SUSPENDED 6
Dan Williams6ad31fe2011-03-04 12:10:29 -080089 unsigned long flags;
Dan Williams209fae12011-06-13 17:39:44 -070090 struct kref kref;
Dan Williams6f231dd2011-07-02 22:56:22 -070091 struct isci_port *isci_port;
92 struct domain_device *domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -070093 struct list_head node;
94 struct list_head reqs_in_process;
Dan Williams78a6f062011-06-30 16:31:37 -070095 struct sci_base_state_machine sm;
96 u32 device_port_width;
97 enum sas_linkrate connection_rate;
Dan Williams78a6f062011-06-30 16:31:37 -070098 struct isci_port *owning_port;
Dan Williams89a73012011-06-30 19:14:33 -070099 struct sci_remote_node_context rnc;
Dan Williams78a6f062011-06-30 16:31:37 -0700100 /* XXX unify with device reference counting and delete */
101 u32 started_request_count;
102 struct isci_request *working_request;
103 u32 not_ready_reason;
Dan Williams6f231dd2011-07-02 22:56:22 -0700104};
105
Dan Williams6f231dd2011-07-02 22:56:22 -0700106#define ISCI_REMOTE_DEVICE_START_TIMEOUT 5000
107
Dan Williams89a73012011-06-30 19:14:33 -0700108/* device reference routines must be called under sci_lock */
Jeff Skirvin5b6bf222012-03-08 22:41:51 -0800109static inline struct isci_remote_device *isci_get_device(
110 struct domain_device *dev)
111{
112 struct isci_remote_device *idev = dev->lldd_dev;
113
114 if (idev)
115 kref_get(&idev->kref);
116 return idev;
117}
118
Dan Williams209fae12011-06-13 17:39:44 -0700119static inline struct isci_remote_device *isci_lookup_device(struct domain_device *dev)
120{
121 struct isci_remote_device *idev = dev->lldd_dev;
122
123 if (idev && !test_bit(IDEV_GONE, &idev->flags)) {
124 kref_get(&idev->kref);
125 return idev;
126 }
127
128 return NULL;
129}
130
131void isci_remote_device_release(struct kref *kref);
132static inline void isci_put_device(struct isci_remote_device *idev)
133{
134 if (idev)
135 kref_put(&idev->kref, isci_remote_device_release);
136}
137
Dan Williams6ad31fe2011-03-04 12:10:29 -0800138enum sci_status isci_remote_device_stop(struct isci_host *ihost,
139 struct isci_remote_device *idev);
Dan Williams4393aa42011-03-31 13:10:44 -0700140void isci_remote_device_nuke_requests(struct isci_host *ihost,
141 struct isci_remote_device *idev);
Dan Williams4393aa42011-03-31 13:10:44 -0700142void isci_remote_device_gone(struct domain_device *domain_dev);
143int isci_remote_device_found(struct domain_device *domain_dev);
Jeff Skirvin5412e252011-10-27 15:05:27 -0700144
Dan Williams88f3b622011-04-22 19:18:03 -0700145/**
Dan Williams89a73012011-06-30 19:14:33 -0700146 * sci_remote_device_stop() - This method will stop both transmission and
Dan Williams88f3b622011-04-22 19:18:03 -0700147 * reception of link activity for the supplied remote device. This method
148 * disables normal IO requests from flowing through to the remote device.
149 * @remote_device: This parameter specifies the device to be stopped.
150 * @timeout: This parameter specifies the number of milliseconds in which the
151 * stop operation should complete.
152 *
153 * An indication of whether the device was successfully stopped. SCI_SUCCESS
154 * This value is returned if the transmission and reception for the device was
155 * successfully stopped.
156 */
Dan Williams89a73012011-06-30 19:14:33 -0700157enum sci_status sci_remote_device_stop(
Dan Williams78a6f062011-06-30 16:31:37 -0700158 struct isci_remote_device *idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700159 u32 timeout);
160
161/**
Dan Williams89a73012011-06-30 19:14:33 -0700162 * sci_remote_device_reset() - This method will reset the device making it
Dan Williams88f3b622011-04-22 19:18:03 -0700163 * ready for operation. This method must be called anytime the device is
164 * reset either through a SMP phy control or a port hard reset request.
165 * @remote_device: This parameter specifies the device to be reset.
166 *
167 * This method does not actually cause the device hardware to be reset. This
168 * method resets the software object so that it will be operational after a
169 * device hardware reset completes. An indication of whether the device reset
170 * was accepted. SCI_SUCCESS This value is returned if the device reset is
171 * started.
172 */
Dan Williams89a73012011-06-30 19:14:33 -0700173enum sci_status sci_remote_device_reset(
Dan Williams78a6f062011-06-30 16:31:37 -0700174 struct isci_remote_device *idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700175
176/**
Dan Williams89a73012011-06-30 19:14:33 -0700177 * sci_remote_device_reset_complete() - This method informs the device object
Dan Williams88f3b622011-04-22 19:18:03 -0700178 * that the reset operation is complete and the device can resume operation
179 * again.
180 * @remote_device: This parameter specifies the device which is to be informed
181 * of the reset complete operation.
182 *
183 * An indication that the device is resuming operation. SCI_SUCCESS the device
184 * is resuming operation.
185 */
Dan Williams89a73012011-06-30 19:14:33 -0700186enum sci_status sci_remote_device_reset_complete(
Dan Williams78a6f062011-06-30 16:31:37 -0700187 struct isci_remote_device *idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700188
Dan Williams88f3b622011-04-22 19:18:03 -0700189/**
Dan Williams89a73012011-06-30 19:14:33 -0700190 * enum sci_remote_device_states - This enumeration depicts all the states
Dan Williams88f3b622011-04-22 19:18:03 -0700191 * for the common remote device state machine.
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800192 * @SCI_DEV_INITIAL: Simply the initial state for the base remote device
193 * state machine.
Dan Williams88f3b622011-04-22 19:18:03 -0700194 *
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800195 * @SCI_DEV_STOPPED: This state indicates that the remote device has
196 * successfully been stopped. In this state no new IO operations are
197 * permitted. This state is entered from the INITIAL state. This state
198 * is entered from the STOPPING state.
Dan Williams88f3b622011-04-22 19:18:03 -0700199 *
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800200 * @SCI_DEV_STARTING: This state indicates the the remote device is in
201 * the process of becoming ready (i.e. starting). In this state no new
202 * IO operations are permitted. This state is entered from the STOPPED
203 * state.
204 *
205 * @SCI_DEV_READY: This state indicates the remote device is now ready.
206 * Thus, the user is able to perform IO operations on the remote device.
207 * This state is entered from the STARTING state.
208 *
209 * @SCI_STP_DEV_IDLE: This is the idle substate for the stp remote
210 * device. When there are no active IO for the device it is is in this
211 * state.
212 *
213 * @SCI_STP_DEV_CMD: This is the command state for for the STP remote
214 * device. This state is entered when the device is processing a
215 * non-NCQ command. The device object will fail any new start IO
216 * requests until this command is complete.
217 *
218 * @SCI_STP_DEV_NCQ: This is the NCQ state for the STP remote device.
219 * This state is entered when the device is processing an NCQ reuqest.
220 * It will remain in this state so long as there is one or more NCQ
221 * requests being processed.
222 *
223 * @SCI_STP_DEV_NCQ_ERROR: This is the NCQ error state for the STP
224 * remote device. This state is entered when an SDB error FIS is
225 * received by the device object while in the NCQ state. The device
226 * object will only accept a READ LOG command while in this state.
227 *
228 * @SCI_STP_DEV_ATAPI_ERROR: This is the ATAPI error state for the STP
229 * ATAPI remote device. This state is entered when ATAPI device sends
230 * error status FIS without data while the device object is in CMD
231 * state. A suspension event is expected in this state. The device
232 * object will resume right away.
233 *
234 * @SCI_STP_DEV_AWAIT_RESET: This is the READY substate indicates the
235 * device is waiting for the RESET task coming to be recovered from
236 * certain hardware specific error.
237 *
238 * @SCI_SMP_DEV_IDLE: This is the ready operational substate for the
239 * remote device. This is the normal operational state for a remote
240 * device.
241 *
242 * @SCI_SMP_DEV_CMD: This is the suspended state for the remote device.
243 * This is the state that the device is placed in when a RNC suspend is
244 * received by the SCU hardware.
245 *
246 * @SCI_DEV_STOPPING: This state indicates that the remote device is in
247 * the process of stopping. In this state no new IO operations are
248 * permitted, but existing IO operations are allowed to complete. This
249 * state is entered from the READY state. This state is entered from
250 * the FAILED state.
251 *
252 * @SCI_DEV_FAILED: This state indicates that the remote device has
253 * failed. In this state no new IO operations are permitted. This
254 * state is entered from the INITIALIZING state. This state is entered
255 * from the READY state.
256 *
257 * @SCI_DEV_RESETTING: This state indicates the device is being reset.
258 * In this state no new IO operations are permitted. This state is
259 * entered from the READY state.
260 *
261 * @SCI_DEV_FINAL: Simply the final state for the base remote device
262 * state machine.
Dan Williams88f3b622011-04-22 19:18:03 -0700263 */
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800264#define REMOTE_DEV_STATES {\
265 C(DEV_INITIAL),\
266 C(DEV_STOPPED),\
267 C(DEV_STARTING),\
268 C(DEV_READY),\
269 C(STP_DEV_IDLE),\
270 C(STP_DEV_CMD),\
271 C(STP_DEV_NCQ),\
272 C(STP_DEV_NCQ_ERROR),\
273 C(STP_DEV_ATAPI_ERROR),\
274 C(STP_DEV_AWAIT_RESET),\
275 C(SMP_DEV_IDLE),\
276 C(SMP_DEV_CMD),\
277 C(DEV_STOPPING),\
278 C(DEV_FAILED),\
279 C(DEV_RESETTING),\
280 C(DEV_FINAL),\
281 }
282#undef C
283#define C(a) SCI_##a
284enum sci_remote_device_states REMOTE_DEV_STATES;
285#undef C
286const char *dev_state_name(enum sci_remote_device_states state);
Dan Williams88f3b622011-04-22 19:18:03 -0700287
Dan Williams89a73012011-06-30 19:14:33 -0700288static inline struct isci_remote_device *rnc_to_dev(struct sci_remote_node_context *rnc)
Dan Williams88f3b622011-04-22 19:18:03 -0700289{
Dan Williams78a6f062011-06-30 16:31:37 -0700290 struct isci_remote_device *idev;
Dan Williams88f3b622011-04-22 19:18:03 -0700291
Dan Williams78a6f062011-06-30 16:31:37 -0700292 idev = container_of(rnc, typeof(*idev), rnc);
Dan Williamsa1a113b2011-04-21 18:44:45 -0700293
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000294 return idev;
295}
296
Dan Williamsa1a113b2011-04-21 18:44:45 -0700297static inline bool dev_is_expander(struct domain_device *dev)
298{
299 return dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV;
300}
301
Dan Williams34a99152011-07-01 02:25:15 -0700302static inline void sci_remote_device_decrement_request_count(struct isci_remote_device *idev)
303{
304 /* XXX delete this voodoo when converting to the top-level device
305 * reference count
306 */
307 if (WARN_ONCE(idev->started_request_count == 0,
308 "%s: tried to decrement started_request_count past 0!?",
309 __func__))
310 /* pass */;
311 else
312 idev->started_request_count--;
313}
Dan Williams88f3b622011-04-22 19:18:03 -0700314
Jeff Skirvin6f488442012-03-08 22:41:48 -0800315static inline void isci_dev_set_hang_detection_timeout(
316 struct isci_remote_device *idev,
317 u32 timeout)
318{
319 sci_port_set_hang_detection_timeout(idev->owning_port, timeout);
320}
321
Dan Williams89a73012011-06-30 19:14:33 -0700322enum sci_status sci_remote_device_frame_handler(
Dan Williams78a6f062011-06-30 16:31:37 -0700323 struct isci_remote_device *idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700324 u32 frame_index);
325
Dan Williams89a73012011-06-30 19:14:33 -0700326enum sci_status sci_remote_device_event_handler(
Dan Williams78a6f062011-06-30 16:31:37 -0700327 struct isci_remote_device *idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700328 u32 event_code);
329
Dan Williams89a73012011-06-30 19:14:33 -0700330enum sci_status sci_remote_device_start_io(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700331 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700332 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700333 struct isci_request *ireq);
Dan Williams88f3b622011-04-22 19:18:03 -0700334
Dan Williams89a73012011-06-30 19:14:33 -0700335enum sci_status sci_remote_device_start_task(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700336 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700337 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700338 struct isci_request *ireq);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700339
Dan Williams89a73012011-06-30 19:14:33 -0700340enum sci_status sci_remote_device_complete_io(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700341 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700342 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700343 struct isci_request *ireq);
Dan Williams88f3b622011-04-22 19:18:03 -0700344
Dan Williams89a73012011-06-30 19:14:33 -0700345void sci_remote_device_post_request(
Dan Williams78a6f062011-06-30 16:31:37 -0700346 struct isci_remote_device *idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700347 u32 request);
348
Jeff Skirvin726980d2012-03-08 22:41:50 -0800349enum sci_status sci_remote_device_terminate_requests(
350 struct isci_remote_device *idev);
351
352int isci_remote_device_is_safe_to_abort(
353 struct isci_remote_device *idev);
354
355enum sci_status
356sci_remote_device_abort_requests_pending_abort(
357 struct isci_remote_device *idev);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -0800358
359enum sci_status isci_remote_device_suspend(
360 struct isci_host *ihost,
361 struct isci_remote_device *idev);
362
363enum sci_status sci_remote_device_resume(
364 struct isci_remote_device *idev,
365 scics_sds_remote_node_context_callback cb_fn,
366 void *cb_p);
367
368enum sci_status isci_remote_device_resume(
369 struct isci_host *ihost,
370 struct isci_remote_device *idev,
371 scics_sds_remote_node_context_callback cb_fn,
372 void *cb_p);
373
374enum sci_status isci_remote_device_reset(
375 struct isci_host *ihost,
376 struct isci_remote_device *idev);
377
378enum sci_status isci_remote_device_reset_complete(
379 struct isci_host *ihost,
380 struct isci_remote_device *idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700381#endif /* !defined(_ISCI_REMOTE_DEVICE_H_) */