blob: 6ac5dfb7d1db50d14209310d555f698bf96f593a [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 Williams88f3b622011-04-22 19:18:03 -070064enum scic_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,
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
73struct scic_sds_remote_device {
74 /**
Dan Williams88f3b622011-04-22 19:18:03 -070075 * This field contains the information for the base remote device state
76 * machine.
77 */
Edmund Nadolskie3013702011-06-02 00:10:43 +000078 struct sci_base_state_machine sm;
Dan Williams88f3b622011-04-22 19:18:03 -070079
80 /**
81 * This field is the programmed device port width. This value is
82 * written to the RCN data structure to tell the SCU how many open
83 * connections this device can have.
84 */
85 u32 device_port_width;
86
87 /**
88 * This field is the programmed connection rate for this remote device. It is
89 * used to program the TC with the maximum allowed connection rate.
90 */
91 enum sas_linkrate connection_rate;
92
93 /**
Dan Williams88f3b622011-04-22 19:18:03 -070094 * This filed is assinged the value of true if the device is directly
95 * attached to the port.
96 */
97 bool is_direct_attached;
98
Dan Williams88f3b622011-04-22 19:18:03 -070099 /**
100 * This filed contains a pointer back to the port to which this device
101 * is assigned.
102 */
103 struct scic_sds_port *owning_port;
104
105 /**
106 * This field contains the SCU silicon remote node context specific
107 * information.
108 */
109 struct scic_sds_remote_node_context rnc;
110
111 /**
112 * This field contains the stated request count for the remote device. The
Edmund Nadolskie3013702011-06-02 00:10:43 +0000113 * device can not reach the SCI_DEV_STOPPED until all
Dan Williams88f3b622011-04-22 19:18:03 -0700114 * requests are complete and the rnc_posted value is false.
115 */
116 u32 started_request_count;
117
118 /**
119 * This field contains a pointer to the working request object. It is only
120 * used only for SATA requests since the unsolicited frames we get from the
121 * hardware have no Tag value to look up the io request object.
122 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700123 struct isci_request *working_request;
Dan Williams88f3b622011-04-22 19:18:03 -0700124
125 /**
126 * This field contains the reason for the remote device going not_ready. It is
127 * assigned in the state handlers and used in the state transition.
128 */
129 u32 not_ready_reason;
Dan Williams88f3b622011-04-22 19:18:03 -0700130};
Dan Williams6f231dd2011-07-02 22:56:22 -0700131
132struct isci_remote_device {
Dan Williams6ad31fe2011-03-04 12:10:29 -0800133 #define IDEV_START_PENDING 0
134 #define IDEV_STOP_PENDING 1
Dan Williamsd9c37392011-03-03 17:59:32 -0800135 #define IDEV_ALLOCATED 2
Dan Williamsd06b4872011-05-02 13:59:25 -0700136 #define IDEV_EH 3
Dan Williams209fae12011-06-13 17:39:44 -0700137 #define IDEV_GONE 4
Dan Williamsf2088262011-06-16 11:26:12 -0700138 #define IDEV_IO_READY 5
Jeff Skirvin9274f452011-06-23 17:09:02 -0700139 #define IDEV_IO_NCQERROR 6
Dan Williams6ad31fe2011-03-04 12:10:29 -0800140 unsigned long flags;
Dan Williams209fae12011-06-13 17:39:44 -0700141 struct kref kref;
Dan Williams6f231dd2011-07-02 22:56:22 -0700142 struct isci_port *isci_port;
143 struct domain_device *domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700144 struct list_head node;
145 struct list_head reqs_in_process;
Dan Williams57f20f42011-04-21 18:14:45 -0700146 struct scic_sds_remote_device sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700147};
148
Dan Williams6f231dd2011-07-02 22:56:22 -0700149#define ISCI_REMOTE_DEVICE_START_TIMEOUT 5000
150
Dan Williams209fae12011-06-13 17:39:44 -0700151/* device reference routines must be called under scic_lock */
152static inline struct isci_remote_device *isci_lookup_device(struct domain_device *dev)
153{
154 struct isci_remote_device *idev = dev->lldd_dev;
155
156 if (idev && !test_bit(IDEV_GONE, &idev->flags)) {
157 kref_get(&idev->kref);
158 return idev;
159 }
160
161 return NULL;
162}
163
164void isci_remote_device_release(struct kref *kref);
165static inline void isci_put_device(struct isci_remote_device *idev)
166{
167 if (idev)
168 kref_put(&idev->kref, isci_remote_device_release);
169}
170
Dan Williams6ad31fe2011-03-04 12:10:29 -0800171enum sci_status isci_remote_device_stop(struct isci_host *ihost,
172 struct isci_remote_device *idev);
Dan Williams4393aa42011-03-31 13:10:44 -0700173void isci_remote_device_nuke_requests(struct isci_host *ihost,
174 struct isci_remote_device *idev);
Dan Williams4393aa42011-03-31 13:10:44 -0700175void isci_remote_device_gone(struct domain_device *domain_dev);
176int isci_remote_device_found(struct domain_device *domain_dev);
177bool isci_device_is_reset_pending(struct isci_host *ihost,
178 struct isci_remote_device *idev);
179void isci_device_clear_reset_pending(struct isci_host *ihost,
180 struct isci_remote_device *idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700181/**
182 * scic_remote_device_stop() - This method will stop both transmission and
183 * reception of link activity for the supplied remote device. This method
184 * disables normal IO requests from flowing through to the remote device.
185 * @remote_device: This parameter specifies the device to be stopped.
186 * @timeout: This parameter specifies the number of milliseconds in which the
187 * stop operation should complete.
188 *
189 * An indication of whether the device was successfully stopped. SCI_SUCCESS
190 * This value is returned if the transmission and reception for the device was
191 * successfully stopped.
192 */
193enum sci_status scic_remote_device_stop(
194 struct scic_sds_remote_device *remote_device,
195 u32 timeout);
196
197/**
198 * scic_remote_device_reset() - This method will reset the device making it
199 * ready for operation. This method must be called anytime the device is
200 * reset either through a SMP phy control or a port hard reset request.
201 * @remote_device: This parameter specifies the device to be reset.
202 *
203 * This method does not actually cause the device hardware to be reset. This
204 * method resets the software object so that it will be operational after a
205 * device hardware reset completes. An indication of whether the device reset
206 * was accepted. SCI_SUCCESS This value is returned if the device reset is
207 * started.
208 */
209enum sci_status scic_remote_device_reset(
210 struct scic_sds_remote_device *remote_device);
211
212/**
213 * scic_remote_device_reset_complete() - This method informs the device object
214 * that the reset operation is complete and the device can resume operation
215 * again.
216 * @remote_device: This parameter specifies the device which is to be informed
217 * of the reset complete operation.
218 *
219 * An indication that the device is resuming operation. SCI_SUCCESS the device
220 * is resuming operation.
221 */
222enum sci_status scic_remote_device_reset_complete(
223 struct scic_sds_remote_device *remote_device);
224
Dan Williams88f3b622011-04-22 19:18:03 -0700225#define scic_remote_device_is_atapi(device_handle) false
Dan Williams88f3b622011-04-22 19:18:03 -0700226
227/**
228 * enum scic_sds_remote_device_states - This enumeration depicts all the states
229 * for the common remote device state machine.
230 *
231 *
232 */
233enum scic_sds_remote_device_states {
234 /**
235 * Simply the initial state for the base remote device state machine.
236 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000237 SCI_DEV_INITIAL,
Dan Williams88f3b622011-04-22 19:18:03 -0700238
239 /**
240 * This state indicates that the remote device has successfully been
241 * stopped. In this state no new IO operations are permitted.
242 * This state is entered from the INITIAL state.
243 * This state is entered from the STOPPING state.
244 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000245 SCI_DEV_STOPPED,
Dan Williams88f3b622011-04-22 19:18:03 -0700246
247 /**
248 * This state indicates the the remote device is in the process of
249 * becoming ready (i.e. starting). In this state no new IO operations
250 * are permitted.
251 * This state is entered from the STOPPED state.
252 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000253 SCI_DEV_STARTING,
Dan Williams88f3b622011-04-22 19:18:03 -0700254
255 /**
256 * This state indicates the remote device is now ready. Thus, the user
257 * is able to perform IO operations on the remote device.
258 * This state is entered from the STARTING state.
259 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000260 SCI_DEV_READY,
Dan Williams88f3b622011-04-22 19:18:03 -0700261
262 /**
Dan Williams88f3b622011-04-22 19:18:03 -0700263 * This is the idle substate for the stp remote device. When there are no
264 * active IO for the device it is is in this state.
265 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000266 SCI_STP_DEV_IDLE,
Dan Williams88f3b622011-04-22 19:18:03 -0700267
268 /**
269 * This is the command state for for the STP remote device. This state is
270 * entered when the device is processing a non-NCQ command. The device object
271 * will fail any new start IO requests until this command is complete.
272 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000273 SCI_STP_DEV_CMD,
Dan Williams88f3b622011-04-22 19:18:03 -0700274
275 /**
276 * This is the NCQ state for the STP remote device. This state is entered
277 * when the device is processing an NCQ reuqest. It will remain in this state
278 * so long as there is one or more NCQ requests being processed.
279 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000280 SCI_STP_DEV_NCQ,
Dan Williams88f3b622011-04-22 19:18:03 -0700281
282 /**
283 * This is the NCQ error state for the STP remote device. This state is
284 * entered when an SDB error FIS is received by the device object while in the
285 * NCQ state. The device object will only accept a READ LOG command while in
286 * this state.
287 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000288 SCI_STP_DEV_NCQ_ERROR,
Dan Williams88f3b622011-04-22 19:18:03 -0700289
Dan Williams88f3b622011-04-22 19:18:03 -0700290 /**
291 * This is the READY substate indicates the device is waiting for the RESET task
292 * coming to be recovered from certain hardware specific error.
293 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000294 SCI_STP_DEV_AWAIT_RESET,
Dan Williams88f3b622011-04-22 19:18:03 -0700295
Dan Williams88f3b622011-04-22 19:18:03 -0700296 /**
297 * This is the ready operational substate for the remote device. This is the
298 * normal operational state for a remote device.
299 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000300 SCI_SMP_DEV_IDLE,
Dan Williams88f3b622011-04-22 19:18:03 -0700301
302 /**
303 * This is the suspended state for the remote device. This is the state that
304 * the device is placed in when a RNC suspend is received by the SCU hardware.
305 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000306 SCI_SMP_DEV_CMD,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700307
308 /**
309 * This state indicates that the remote device is in the process of
310 * stopping. In this state no new IO operations are permitted, but
311 * existing IO operations are allowed to complete.
312 * This state is entered from the READY state.
313 * This state is entered from the FAILED state.
314 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000315 SCI_DEV_STOPPING,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700316
317 /**
318 * This state indicates that the remote device has failed.
319 * In this state no new IO operations are permitted.
320 * This state is entered from the INITIALIZING state.
321 * This state is entered from the READY state.
322 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000323 SCI_DEV_FAILED,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700324
325 /**
326 * This state indicates the device is being reset.
327 * In this state no new IO operations are permitted.
328 * This state is entered from the READY state.
329 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000330 SCI_DEV_RESETTING,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700331
332 /**
333 * Simply the final state for the base remote device state machine.
334 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000335 SCI_DEV_FINAL,
Dan Williams88f3b622011-04-22 19:18:03 -0700336};
337
338static inline struct scic_sds_remote_device *rnc_to_dev(struct scic_sds_remote_node_context *rnc)
339{
340 struct scic_sds_remote_device *sci_dev;
341
342 sci_dev = container_of(rnc, typeof(*sci_dev), rnc);
343
344 return sci_dev;
345}
346
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000347static inline struct isci_remote_device *sci_dev_to_idev(struct scic_sds_remote_device *sci_dev)
Dan Williamsa1a113b2011-04-21 18:44:45 -0700348{
349 struct isci_remote_device *idev = container_of(sci_dev, typeof(*idev), sci);
350
Maciej Patelczyk5d937e92011-04-28 22:06:21 +0000351 return idev;
352}
353
354static inline struct domain_device *sci_dev_to_domain(struct scic_sds_remote_device *sci_dev)
355{
356 return sci_dev_to_idev(sci_dev)->domain_dev;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700357}
358
359static inline bool dev_is_expander(struct domain_device *dev)
360{
361 return dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV;
362}
363
Dan Williams88f3b622011-04-22 19:18:03 -0700364/**
365 * scic_sds_remote_device_increment_request_count() -
366 *
367 * This macro incrments the request count for this device
368 */
369#define scic_sds_remote_device_increment_request_count(sci_dev) \
370 ((sci_dev)->started_request_count++)
371
372/**
373 * scic_sds_remote_device_decrement_request_count() -
374 *
375 * This macro decrements the request count for this device. This count will
376 * never decrment past 0.
377 */
378#define scic_sds_remote_device_decrement_request_count(sci_dev) \
379 ((sci_dev)->started_request_count > 0 ? \
380 (sci_dev)->started_request_count-- : 0)
381
382/**
383 * scic_sds_remote_device_get_request_count() -
384 *
385 * This is a helper macro to return the current device request count.
386 */
387#define scic_sds_remote_device_get_request_count(sci_dev) \
388 ((sci_dev)->started_request_count)
389
390/**
391 * scic_sds_remote_device_get_port() -
392 *
393 * This macro returns the owning port of this remote device obejct.
394 */
395#define scic_sds_remote_device_get_port(sci_dev) \
396 ((sci_dev)->owning_port)
397
398/**
399 * scic_sds_remote_device_get_controller() -
400 *
401 * This macro returns the controller object that contains this device object
402 */
403#define scic_sds_remote_device_get_controller(sci_dev) \
404 scic_sds_port_get_controller(scic_sds_remote_device_get_port(sci_dev))
405
406/**
Dan Williams88f3b622011-04-22 19:18:03 -0700407 * scic_sds_remote_device_get_port() -
408 *
409 * This macro returns the owning port of this device
410 */
411#define scic_sds_remote_device_get_port(sci_dev) \
412 ((sci_dev)->owning_port)
413
414/**
Dan Williams88f3b622011-04-22 19:18:03 -0700415 * scic_sds_remote_device_get_controller_peg() -
416 *
417 * This macro returns the controllers protocol engine group
418 */
419#define scic_sds_remote_device_get_controller_peg(sci_dev) \
420 (\
421 scic_sds_controller_get_protocol_engine_group(\
422 scic_sds_port_get_controller(\
423 scic_sds_remote_device_get_port(sci_dev) \
424 ) \
425 ) \
426 )
427
428/**
Dan Williams88f3b622011-04-22 19:18:03 -0700429 * scic_sds_remote_device_get_index() -
430 *
431 * This macro returns the remote node index for this device object
432 */
433#define scic_sds_remote_device_get_index(sci_dev) \
434 ((sci_dev)->rnc.remote_node_index)
435
436/**
437 * scic_sds_remote_device_build_command_context() -
438 *
439 * This macro builds a remote device context for the SCU post request operation
440 */
441#define scic_sds_remote_device_build_command_context(device, command) \
442 ((command) \
443 | (scic_sds_remote_device_get_controller_peg((device)) << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) \
Dan Williams1f4fa1f2011-04-26 11:44:06 -0700444 | ((device)->owning_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) \
Dan Williams88f3b622011-04-22 19:18:03 -0700445 | (scic_sds_remote_device_get_index((device))) \
446 )
447
448/**
449 * scic_sds_remote_device_set_working_request() -
450 *
451 * This macro makes the working request assingment for the remote device
452 * object. To clear the working request use this macro with a NULL request
453 * object.
454 */
455#define scic_sds_remote_device_set_working_request(device, request) \
456 ((device)->working_request = (request))
457
458enum sci_status scic_sds_remote_device_frame_handler(
459 struct scic_sds_remote_device *sci_dev,
460 u32 frame_index);
461
462enum sci_status scic_sds_remote_device_event_handler(
463 struct scic_sds_remote_device *sci_dev,
464 u32 event_code);
465
466enum sci_status scic_sds_remote_device_start_io(
467 struct scic_sds_controller *controller,
468 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700469 struct isci_request *ireq);
Dan Williams88f3b622011-04-22 19:18:03 -0700470
Dan Williamsab2e8f72011-04-27 16:32:45 -0700471enum sci_status scic_sds_remote_device_start_task(
472 struct scic_sds_controller *controller,
473 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700474 struct isci_request *ireq);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700475
Dan Williams88f3b622011-04-22 19:18:03 -0700476enum sci_status scic_sds_remote_device_complete_io(
477 struct scic_sds_controller *controller,
478 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700479 struct isci_request *ireq);
Dan Williams88f3b622011-04-22 19:18:03 -0700480
Dan Williams88f3b622011-04-22 19:18:03 -0700481enum sci_status scic_sds_remote_device_suspend(
482 struct scic_sds_remote_device *sci_dev,
483 u32 suspend_type);
484
Dan Williams88f3b622011-04-22 19:18:03 -0700485void scic_sds_remote_device_post_request(
486 struct scic_sds_remote_device *sci_dev,
487 u32 request);
488
Dan Williams88f3b622011-04-22 19:18:03 -0700489#define scic_sds_remote_device_is_atapi(sci_dev) false
Dan Williams88f3b622011-04-22 19:18:03 -0700490
Dan Williams6f231dd2011-07-02 22:56:22 -0700491#endif /* !defined(_ISCI_REMOTE_DEVICE_H_) */