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 
56 #ifndef _ISCI_REMOTE_DEVICE_H_
57 #define _ISCI_REMOTE_DEVICE_H_
58 #include <scsi/libsas.h>
59 #include <linux/kref.h>
60 #include "scu_remote_node_context.h"
61 #include "remote_node_context.h"
62 #include "port.h"
63 
64 enum sci_remote_device_not_ready_reason_code {
65 	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,
70 	SCIC_REMOTE_DEVICE_NOT_READY_REASON_CODE_MAX
71 };
72 
73 /**
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  */
81 struct isci_remote_device {
82 	#define IDEV_START_PENDING 0
83 	#define IDEV_STOP_PENDING 1
84 	#define IDEV_ALLOCATED 2
85 	#define IDEV_GONE 3
86 	#define IDEV_IO_READY 4
87 	#define IDEV_IO_NCQERROR 5
88 	unsigned long flags;
89 	struct kref kref;
90 	struct isci_port *isci_port;
91 	struct domain_device *domain_dev;
92 	struct list_head node;
93 	struct list_head reqs_in_process;
94 	struct sci_base_state_machine sm;
95 	u32 device_port_width;
96 	enum sas_linkrate connection_rate;
97 	bool is_direct_attached;
98 	struct isci_port *owning_port;
99 	struct sci_remote_node_context rnc;
100 	/* XXX unify with device reference counting and delete */
101 	u32 started_request_count;
102 	struct isci_request *working_request;
103 	u32 not_ready_reason;
104 };
105 
106 #define ISCI_REMOTE_DEVICE_START_TIMEOUT 5000
107 
108 /* device reference routines must be called under sci_lock */
isci_lookup_device(struct domain_device * dev)109 static inline struct isci_remote_device *isci_lookup_device(struct domain_device *dev)
110 {
111 	struct isci_remote_device *idev = dev->lldd_dev;
112 
113 	if (idev && !test_bit(IDEV_GONE, &idev->flags)) {
114 		kref_get(&idev->kref);
115 		return idev;
116 	}
117 
118 	return NULL;
119 }
120 
121 void isci_remote_device_release(struct kref *kref);
isci_put_device(struct isci_remote_device * idev)122 static inline void isci_put_device(struct isci_remote_device *idev)
123 {
124 	if (idev)
125 		kref_put(&idev->kref, isci_remote_device_release);
126 }
127 
128 enum sci_status isci_remote_device_stop(struct isci_host *ihost,
129 					struct isci_remote_device *idev);
130 void isci_remote_device_nuke_requests(struct isci_host *ihost,
131 				      struct isci_remote_device *idev);
132 void isci_remote_device_gone(struct domain_device *domain_dev);
133 int isci_remote_device_found(struct domain_device *domain_dev);
134 
135 /**
136  * sci_remote_device_stop() - This method will stop both transmission and
137  *    reception of link activity for the supplied remote device.  This method
138  *    disables normal IO requests from flowing through to the remote device.
139  * @remote_device: This parameter specifies the device to be stopped.
140  * @timeout: This parameter specifies the number of milliseconds in which the
141  *    stop operation should complete.
142  *
143  * An indication of whether the device was successfully stopped. SCI_SUCCESS
144  * This value is returned if the transmission and reception for the device was
145  * successfully stopped.
146  */
147 enum sci_status sci_remote_device_stop(
148 	struct isci_remote_device *idev,
149 	u32 timeout);
150 
151 /**
152  * sci_remote_device_reset() - This method will reset the device making it
153  *    ready for operation. This method must be called anytime the device is
154  *    reset either through a SMP phy control or a port hard reset request.
155  * @remote_device: This parameter specifies the device to be reset.
156  *
157  * This method does not actually cause the device hardware to be reset. This
158  * method resets the software object so that it will be operational after a
159  * device hardware reset completes. An indication of whether the device reset
160  * was accepted. SCI_SUCCESS This value is returned if the device reset is
161  * started.
162  */
163 enum sci_status sci_remote_device_reset(
164 	struct isci_remote_device *idev);
165 
166 /**
167  * sci_remote_device_reset_complete() - This method informs the device object
168  *    that the reset operation is complete and the device can resume operation
169  *    again.
170  * @remote_device: This parameter specifies the device which is to be informed
171  *    of the reset complete operation.
172  *
173  * An indication that the device is resuming operation. SCI_SUCCESS the device
174  * is resuming operation.
175  */
176 enum sci_status sci_remote_device_reset_complete(
177 	struct isci_remote_device *idev);
178 
179 /**
180  * enum sci_remote_device_states - This enumeration depicts all the states
181  *    for the common remote device state machine.
182  * @SCI_DEV_INITIAL: Simply the initial state for the base remote device
183  * state machine.
184  *
185  * @SCI_DEV_STOPPED: This state indicates that the remote device has
186  * successfully been stopped.  In this state no new IO operations are
187  * permitted.  This state is entered from the INITIAL state.  This state
188  * is entered from the STOPPING state.
189  *
190  * @SCI_DEV_STARTING: This state indicates the the remote device is in
191  * the process of becoming ready (i.e. starting).  In this state no new
192  * IO operations are permitted.  This state is entered from the STOPPED
193  * state.
194  *
195  * @SCI_DEV_READY: This state indicates the remote device is now ready.
196  * Thus, the user is able to perform IO operations on the remote device.
197  * This state is entered from the STARTING state.
198  *
199  * @SCI_STP_DEV_IDLE: This is the idle substate for the stp remote
200  * device.  When there are no active IO for the device it is is in this
201  * state.
202  *
203  * @SCI_STP_DEV_CMD: This is the command state for for the STP remote
204  * device.  This state is entered when the device is processing a
205  * non-NCQ command.  The device object will fail any new start IO
206  * requests until this command is complete.
207  *
208  * @SCI_STP_DEV_NCQ: This is the NCQ state for the STP remote device.
209  * This state is entered when the device is processing an NCQ reuqest.
210  * It will remain in this state so long as there is one or more NCQ
211  * requests being processed.
212  *
213  * @SCI_STP_DEV_NCQ_ERROR: This is the NCQ error state for the STP
214  * remote device.  This state is entered when an SDB error FIS is
215  * received by the device object while in the NCQ state.  The device
216  * object will only accept a READ LOG command while in this state.
217  *
218  * @SCI_STP_DEV_ATAPI_ERROR: This is the ATAPI error state for the STP
219  * ATAPI remote device.  This state is entered when ATAPI device sends
220  * error status FIS without data while the device object is in CMD
221  * state.  A suspension event is expected in this state.  The device
222  * object will resume right away.
223  *
224  * @SCI_STP_DEV_AWAIT_RESET: This is the READY substate indicates the
225  * device is waiting for the RESET task coming to be recovered from
226  * certain hardware specific error.
227  *
228  * @SCI_SMP_DEV_IDLE: This is the ready operational substate for the
229  * remote device.  This is the normal operational state for a remote
230  * device.
231  *
232  * @SCI_SMP_DEV_CMD: This is the suspended state for the remote device.
233  * This is the state that the device is placed in when a RNC suspend is
234  * received by the SCU hardware.
235  *
236  * @SCI_DEV_STOPPING: This state indicates that the remote device is in
237  * the process of stopping.  In this state no new IO operations are
238  * permitted, but existing IO operations are allowed to complete.  This
239  * state is entered from the READY state.  This state is entered from
240  * the FAILED state.
241  *
242  * @SCI_DEV_FAILED: This state indicates that the remote device has
243  * failed.  In this state no new IO operations are permitted.  This
244  * state is entered from the INITIALIZING state.  This state is entered
245  * from the READY state.
246  *
247  * @SCI_DEV_RESETTING: This state indicates the device is being reset.
248  * In this state no new IO operations are permitted.  This state is
249  * entered from the READY state.
250  *
251  * @SCI_DEV_FINAL: Simply the final state for the base remote device
252  * state machine.
253  */
254 #define REMOTE_DEV_STATES {\
255 	C(DEV_INITIAL),\
256 	C(DEV_STOPPED),\
257 	C(DEV_STARTING),\
258 	C(DEV_READY),\
259 	C(STP_DEV_IDLE),\
260 	C(STP_DEV_CMD),\
261 	C(STP_DEV_NCQ),\
262 	C(STP_DEV_NCQ_ERROR),\
263 	C(STP_DEV_ATAPI_ERROR),\
264 	C(STP_DEV_AWAIT_RESET),\
265 	C(SMP_DEV_IDLE),\
266 	C(SMP_DEV_CMD),\
267 	C(DEV_STOPPING),\
268 	C(DEV_FAILED),\
269 	C(DEV_RESETTING),\
270 	C(DEV_FINAL),\
271 	}
272 #undef C
273 #define C(a) SCI_##a
274 enum sci_remote_device_states REMOTE_DEV_STATES;
275 #undef C
276 const char *dev_state_name(enum sci_remote_device_states state);
277 
rnc_to_dev(struct sci_remote_node_context * rnc)278 static inline struct isci_remote_device *rnc_to_dev(struct sci_remote_node_context *rnc)
279 {
280 	struct isci_remote_device *idev;
281 
282 	idev = container_of(rnc, typeof(*idev), rnc);
283 
284 	return idev;
285 }
286 
dev_is_expander(struct domain_device * dev)287 static inline bool dev_is_expander(struct domain_device *dev)
288 {
289 	return dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV;
290 }
291 
sci_remote_device_decrement_request_count(struct isci_remote_device * idev)292 static inline void sci_remote_device_decrement_request_count(struct isci_remote_device *idev)
293 {
294 	/* XXX delete this voodoo when converting to the top-level device
295 	 * reference count
296 	 */
297 	if (WARN_ONCE(idev->started_request_count == 0,
298 		      "%s: tried to decrement started_request_count past 0!?",
299 			__func__))
300 		/* pass */;
301 	else
302 		idev->started_request_count--;
303 }
304 
305 enum sci_status sci_remote_device_frame_handler(
306 	struct isci_remote_device *idev,
307 	u32 frame_index);
308 
309 enum sci_status sci_remote_device_event_handler(
310 	struct isci_remote_device *idev,
311 	u32 event_code);
312 
313 enum sci_status sci_remote_device_start_io(
314 	struct isci_host *ihost,
315 	struct isci_remote_device *idev,
316 	struct isci_request *ireq);
317 
318 enum sci_status sci_remote_device_start_task(
319 	struct isci_host *ihost,
320 	struct isci_remote_device *idev,
321 	struct isci_request *ireq);
322 
323 enum sci_status sci_remote_device_complete_io(
324 	struct isci_host *ihost,
325 	struct isci_remote_device *idev,
326 	struct isci_request *ireq);
327 
328 enum sci_status sci_remote_device_suspend(
329 	struct isci_remote_device *idev,
330 	u32 suspend_type);
331 
332 void sci_remote_device_post_request(
333 	struct isci_remote_device *idev,
334 	u32 request);
335 
336 #endif /* !defined(_ISCI_REMOTE_DEVICE_H_) */
337