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