aed4eb640c26aff4369a483393b55c825dfc85f9
[pandora-kernel.git] / drivers / scsi / isci / core / scic_sds_remote_device.h
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 _SCIC_SDS_REMOTE_DEVICE_H_
57 #define _SCIC_SDS_REMOTE_DEVICE_H_
58
59 /**
60  * This file contains the structures, constants, and prototypes for the
61  *    struct scic_sds_remote_device object.
62  *
63  *
64  */
65
66 #include "intel_sas.h"
67 #include "sci_base_remote_device.h"
68 #include "sci_base_request.h"
69 #include "scu_remote_node_context.h"
70 #include "scic_sds_remote_node_context.h"
71
72 struct scic_sds_controller;
73 struct scic_sds_port;
74 struct scic_sds_request;
75 struct scic_sds_remote_device_state_handler;
76
77 /**
78  * enum scic_sds_ssp_remote_device_ready_substates -
79  *
80  * This is the enumeration of the ready substates for the
81  * struct scic_sds_remote_device.
82  */
83 enum scic_sds_ssp_remote_device_ready_substates {
84         /**
85          * This is the initial state for the remote device ready substate.
86          */
87         SCIC_SDS_SSP_REMOTE_DEVICE_READY_SUBSTATE_INITIAL,
88
89         /**
90          * This is the ready operational substate for the remote device.
91          * This is the normal operational state for a remote device.
92          */
93         SCIC_SDS_SSP_REMOTE_DEVICE_READY_SUBSTATE_OPERATIONAL,
94
95         /**
96          * This is the suspended state for the remote device. This is the state
97          * that the device is placed in when a RNC suspend is received by
98          * the SCU hardware.
99          */
100         SCIC_SDS_SSP_REMOTE_DEVICE_READY_SUBSTATE_SUSPENDED,
101
102         /**
103          * This is the final state that the device is placed in before a change
104          * to the base state machine.
105          */
106         SCIC_SDS_SSP_REMOTE_DEVICE_READY_SUBSTATE_FINAL,
107
108         SCIC_SDS_SSP_REMOTE_DEVICE_READY_MAX_SUBSTATES
109 };
110
111 /**
112  * enum scic_sds_stp_remote_device_ready_substates -
113  *
114  * This is the enumeration for the struct scic_sds_remote_device ready substates
115  * for the STP remote device.
116  */
117 enum scic_sds_stp_remote_device_ready_substates {
118         /**
119          * This is the idle substate for the stp remote device.  When there are no
120          * active IO for the device it is is in this state.
121          */
122         SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE,
123
124         /**
125          * This is the command state for for the STP remote device.  This state is
126          * entered when the device is processing a non-NCQ command.  The device object
127          * will fail any new start IO requests until this command is complete.
128          */
129         SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD,
130
131         /**
132          * This is the NCQ state for the STP remote device.  This state is entered
133          * when the device is processing an NCQ reuqest.  It will remain in this state
134          * so long as there is one or more NCQ requests being processed.
135          */
136         SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ,
137
138         /**
139          * This is the NCQ error state for the STP remote device.  This state is
140          * entered when an SDB error FIS is received by the device object while in the
141          * NCQ state.  The device object will only accept a READ LOG command while in
142          * this state.
143          */
144         SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR,
145
146 #if !defined(DISABLE_ATAPI)
147         /**
148          * This is the ATAPI error state for the STP ATAPI remote device.  This state is
149          * entered when ATAPI device sends error status FIS without data while the device
150          * object is in CMD state. A suspension event is expected in this state. The device
151          * object will resume right away.
152          */
153         SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_ATAPI_ERROR,
154 #endif
155
156         /**
157          * This is the READY substate indicates the device is waiting for the RESET task
158          * coming to be recovered from certain hardware specific error.
159          */
160         SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET,
161
162         SCIC_SDS_STP_REMOTE_DEVICE_READY_MAX_SUBSTATES
163 };
164
165 /**
166  * enum scic_sds_smp_remote_device_ready_substates -
167  *
168  * This is the enumeration of the ready substates for the SMP REMOTE DEVICE.
169  */
170 enum scic_sds_smp_remote_device_ready_substates {
171         /**
172          * This is the ready operational substate for the remote device.  This is the
173          * normal operational state for a remote device.
174          */
175         SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE,
176
177         /**
178          * This is the suspended state for the remote device.  This is the state that
179          * the device is placed in when a RNC suspend is received by the SCU hardware.
180          */
181         SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD,
182
183         SCIC_SDS_SMP_REMOTE_DEVICE_READY_MAX_SUBSTATES
184 };
185
186 /**
187  * struct scic_sds_remote_device - This structure contains the data for an SCU
188  *    implementation of the SCU Core device data.
189  *
190  *
191  */
192 struct scic_sds_remote_device {
193         /**
194          * This field is the common base for all remote device objects.
195          */
196         struct sci_base_remote_device parent;
197
198         /**
199          * This field is the programmed device port width.  This value is written to
200          * the RCN data structure to tell the SCU how many open connections this
201          * device can have.
202          */
203         u32 device_port_width;
204
205         /**
206          * This field is the programmed connection rate for this remote device.  It is
207          * used to program the TC with the maximum allowed connection rate.
208          */
209         enum sci_sas_link_rate connection_rate;
210
211         /**
212          * This field contains the allowed target protocols for this remote device.
213          */
214         struct smp_discover_response_protocols target_protocols;
215
216         /**
217          * This field contains the device SAS address.
218          */
219         struct sci_sas_address device_address;
220
221         /**
222          * This filed is assinged the value of true if the device is directly
223          * attached to the port.
224          */
225         bool is_direct_attached;
226
227 #if !defined(DISABLE_ATAPI)
228         /**
229          * This filed is assinged the value of true if the device is an ATAPI
230          * device.
231          */
232         bool is_atapi;
233 #endif
234
235         /**
236          * This filed contains a pointer back to the port to which this device
237          * is assigned.
238          */
239         struct scic_sds_port *owning_port;
240
241         /**
242          * This field contains the SCU silicon remote node context specific
243          * information.
244          */
245         struct scic_sds_remote_node_context *rnc;
246
247         /**
248          * This field contains the stated request count for the remote device.  The
249          * device can not reach the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED until all
250          * requests are complete and the rnc_posted value is false.
251          */
252         u32 started_request_count;
253
254         /**
255          * This field contains a pointer to the working request object.  It is only
256          * used only for SATA requests since the unsolicited frames we get from the
257          * hardware have no Tag value to look up the io request object.
258          */
259         struct scic_sds_request *working_request;
260
261         /**
262          * This field contains the reason for the remote device going not_ready.  It is
263          * assigned in the state handlers and used in the state transition.
264          */
265         u32 not_ready_reason;
266
267         /**
268          * This field is true if this remote device has an initialzied ready substate
269          * machine. SSP devices do not have a ready substate machine and STP devices
270          * have a ready substate machine.
271          */
272         bool has_ready_substate_machine;
273
274         /**
275          * This field contains the state machine for the ready substate machine for
276          * this struct scic_sds_remote_device object.
277          */
278         struct sci_base_state_machine ready_substate_machine;
279
280         /**
281          * This field maintains the set of state handlers for the remote device
282          * object.  These are changed each time the remote device enters a new state.
283          */
284         const struct scic_sds_remote_device_state_handler *state_handlers;
285 };
286
287 typedef enum sci_status (*scic_sds_remote_device_handler_t)(
288         struct scic_sds_remote_device *this_device);
289
290 typedef enum sci_status (*scic_sds_remote_device_suspend_handler_t)(
291         struct scic_sds_remote_device *this_device,
292         u32 suspend_type);
293
294 typedef enum sci_status (*scic_sds_remote_device_resume_handler_t)(
295         struct scic_sds_remote_device *this_device);
296
297 typedef enum sci_status (*scic_sds_remote_device_frame_handler_t)(
298         struct scic_sds_remote_device *this_device,
299         u32 frame_index);
300
301 typedef enum sci_status (*scic_sds_remote_device_event_handler_t)(
302         struct scic_sds_remote_device *this_device,
303         u32 event_code);
304
305 typedef void (*scic_sds_remote_device_ready_not_ready_handler_t)(
306         struct scic_sds_remote_device *this_device);
307
308 /**
309  * struct scic_sds_remote_device_state_handler - This structure conains the
310  *    state handlers that are needed to process requests for the SCU remote
311  *    device objects.
312  *
313  *
314  */
315 struct scic_sds_remote_device_state_handler {
316         struct sci_base_remote_device_state_handler parent;
317         scic_sds_remote_device_suspend_handler_t suspend_handler;
318         scic_sds_remote_device_resume_handler_t resume_handler;
319         scic_sds_remote_device_event_handler_t event_handler;
320         scic_sds_remote_device_frame_handler_t frame_handler;
321 };
322
323 extern const struct sci_base_state scic_sds_remote_device_state_table[];
324 extern const struct sci_base_state scic_sds_ssp_remote_device_ready_substate_table[];
325 extern const struct sci_base_state scic_sds_stp_remote_device_ready_substate_table[];
326 extern const struct sci_base_state scic_sds_smp_remote_device_ready_substate_table[];
327
328 extern const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[];
329 extern struct scic_sds_remote_device_state_handler
330         scic_sds_ssp_remote_device_ready_substate_handler_table[];
331 extern struct scic_sds_remote_device_state_handler
332         scic_sds_stp_remote_device_ready_substate_handler_table[];
333 extern struct scic_sds_remote_device_state_handler
334         scic_sds_smp_remote_device_ready_substate_handler_table[];
335
336 /**
337  * scic_sds_remote_device_increment_request_count() -
338  *
339  * This macro incrments the request count for this device
340  */
341 #define scic_sds_remote_device_increment_request_count(this_device) \
342         ((this_device)->started_request_count++)
343
344 /**
345  * scic_sds_remote_device_decrement_request_count() -
346  *
347  * This macro decrements the request count for this device.  This count will
348  * never decrment past 0.
349  */
350 #define scic_sds_remote_device_decrement_request_count(this_device) \
351         ((this_device)->started_request_count > 0 ? \
352          (this_device)->started_request_count-- : 0)
353
354 /**
355  * scic_sds_remote_device_get_request_count() -
356  *
357  * This is a helper macro to return the current device request count.
358  */
359 #define scic_sds_remote_device_get_request_count(this_device) \
360         ((this_device)->started_request_count)
361
362 /**
363  * scic_sds_remote_device_get_port() -
364  *
365  * This macro returns the owning port of this remote device obejct.
366  */
367 #define scic_sds_remote_device_get_port(this_device) \
368         ((this_device)->owning_port)
369
370 /**
371  * scic_sds_remote_device_get_controller() -
372  *
373  * This macro returns the controller object that contains this device object
374  */
375 #define scic_sds_remote_device_get_controller(this_device) \
376         scic_sds_port_get_controller(scic_sds_remote_device_get_port(this_device))
377
378 /**
379  * scic_sds_remote_device_set_state_handlers() -
380  *
381  * This macro sets the remote device state handlers pointer and is set on entry
382  * to each device state.
383  */
384 #define scic_sds_remote_device_set_state_handlers(this_device, handlers) \
385         ((this_device)->state_handlers = (handlers))
386
387 /**
388  * scic_sds_remote_device_get_base_state_machine() -
389  *
390  * This macro returns the base sate machine object for the remote device.
391  */
392 #define scic_sds_remote_device_get_base_state_machine(this_device) \
393         (&(this_device)->parent.state_machine)
394
395 /**
396  * scic_sds_remote_device_get_ready_substate_machine() -
397  *
398  * This macro returns the remote device ready substate machine
399  */
400 #define scic_sds_remote_device_get_ready_substate_machine(this_device) \
401         (&(this_device)->ready_substate_machine)
402
403 /**
404  * scic_sds_remote_device_get_port() -
405  *
406  * This macro returns the owning port of this device
407  */
408 #define scic_sds_remote_device_get_port(this_device) \
409         ((this_device)->owning_port)
410
411 /**
412  * scic_sds_remote_device_get_sequence() -
413  *
414  * This macro returns the remote device sequence value
415  */
416 #define scic_sds_remote_device_get_sequence(this_device) \
417         (\
418                 scic_sds_remote_device_get_controller(this_device)-> \
419                 remote_device_sequence[(this_device)->rnc->remote_node_index] \
420         )
421
422 /**
423  * scic_sds_remote_device_get_controller_peg() -
424  *
425  * This macro returns the controllers protocol engine group
426  */
427 #define scic_sds_remote_device_get_controller_peg(this_device) \
428         (\
429                 scic_sds_controller_get_protocol_engine_group(\
430                         scic_sds_port_get_controller(\
431                                 scic_sds_remote_device_get_port(this_device) \
432                                 ) \
433                         ) \
434         )
435
436 /**
437  * scic_sds_remote_device_get_port_index() -
438  *
439  * This macro returns the port index for the devices owning port
440  */
441 #define scic_sds_remote_device_get_port_index(this_device) \
442         (scic_sds_port_get_index(scic_sds_remote_device_get_port(this_device)))
443
444 /**
445  * scic_sds_remote_device_get_index() -
446  *
447  * This macro returns the remote node index for this device object
448  */
449 #define scic_sds_remote_device_get_index(this_device) \
450         ((this_device)->rnc->remote_node_index)
451
452 /**
453  * scic_sds_remote_device_build_command_context() -
454  *
455  * This macro builds a remote device context for the SCU post request operation
456  */
457 #define scic_sds_remote_device_build_command_context(device, command) \
458         ((command) \
459          | (scic_sds_remote_device_get_controller_peg((device)) << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) \
460          | (scic_sds_remote_device_get_port_index((device)) << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) \
461          | (scic_sds_remote_device_get_index((device))) \
462         )
463
464 /**
465  * scic_sds_remote_device_set_working_request() -
466  *
467  * This macro makes the working request assingment for the remote device
468  * object. To clear the working request use this macro with a NULL request
469  * object.
470  */
471 #define scic_sds_remote_device_set_working_request(device, request) \
472         ((device)->working_request = (request))
473
474 enum sci_status scic_sds_remote_device_frame_handler(
475         struct scic_sds_remote_device *this_device,
476         u32 frame_index);
477
478 enum sci_status scic_sds_remote_device_event_handler(
479         struct scic_sds_remote_device *this_device,
480         u32 event_code);
481
482 enum sci_status scic_sds_remote_device_start_io(
483         struct scic_sds_controller *controller,
484         struct scic_sds_remote_device *this_device,
485         struct scic_sds_request *io_request);
486
487 enum sci_status scic_sds_remote_device_complete_io(
488         struct scic_sds_controller *controller,
489         struct scic_sds_remote_device *this_device,
490         struct scic_sds_request *io_request);
491
492 enum sci_status scic_sds_remote_device_resume(
493         struct scic_sds_remote_device *this_device);
494
495 enum sci_status scic_sds_remote_device_suspend(
496         struct scic_sds_remote_device *this_device,
497         u32 suspend_type);
498
499 enum sci_status scic_sds_remote_device_start_task(
500         struct scic_sds_controller *controller,
501         struct scic_sds_remote_device *this_device,
502         struct scic_sds_request *io_request);
503
504 void scic_sds_remote_device_post_request(
505         struct scic_sds_remote_device *this_device,
506         u32 request);
507
508 #if !defined(DISABLE_ATAPI)
509 bool scic_sds_remote_device_is_atapi(
510         struct scic_sds_remote_device *this_device);
511 #else /* !defined(DISABLE_ATAPI) */
512 #define scic_sds_remote_device_is_atapi(this_device) false
513 #endif /* !defined(DISABLE_ATAPI) */
514
515 void scic_sds_remote_device_start_request(
516         struct scic_sds_remote_device *this_device,
517         struct scic_sds_request *the_request,
518         enum sci_status status);
519
520 void scic_sds_remote_device_continue_request(
521         struct scic_sds_remote_device *this_device);
522
523 enum sci_status scic_sds_remote_device_default_start_handler(
524         struct sci_base_remote_device *this_device);
525
526 enum sci_status scic_sds_remote_device_default_fail_handler(
527         struct sci_base_remote_device *this_device);
528
529 enum sci_status scic_sds_remote_device_default_destruct_handler(
530         struct sci_base_remote_device *this_device);
531
532 enum sci_status scic_sds_remote_device_default_reset_handler(
533         struct sci_base_remote_device *device);
534
535 enum sci_status scic_sds_remote_device_default_reset_complete_handler(
536         struct sci_base_remote_device *device);
537
538 enum sci_status scic_sds_remote_device_default_start_request_handler(
539         struct sci_base_remote_device *device,
540         struct sci_base_request *request);
541
542 enum sci_status scic_sds_remote_device_default_complete_request_handler(
543         struct sci_base_remote_device *device,
544         struct sci_base_request *request);
545
546 enum sci_status scic_sds_remote_device_default_continue_request_handler(
547         struct sci_base_remote_device *device,
548         struct sci_base_request *request);
549
550 enum sci_status scic_sds_remote_device_default_suspend_handler(
551         struct scic_sds_remote_device *this_device,
552         u32 suspend_type);
553
554 enum sci_status scic_sds_remote_device_default_resume_handler(
555         struct scic_sds_remote_device *this_device);
556
557
558 enum sci_status scic_sds_remote_device_default_frame_handler(
559         struct scic_sds_remote_device *this_device,
560         u32 frame_index);
561
562 enum sci_status scic_sds_remote_device_ready_state_stop_handler(
563         struct sci_base_remote_device *device);
564
565 enum sci_status scic_sds_remote_device_ready_state_reset_handler(
566         struct sci_base_remote_device *device);
567
568 enum sci_status scic_sds_remote_device_general_frame_handler(
569         struct scic_sds_remote_device *this_device,
570         u32 frame_index);
571
572 enum sci_status scic_sds_remote_device_general_event_handler(
573         struct scic_sds_remote_device *this_device,
574         u32 event_code);
575
576 enum sci_status scic_sds_ssp_remote_device_ready_suspended_substate_resume_handler(
577         struct scic_sds_remote_device *this_device);
578
579 #endif /* _SCIC_SDS_REMOTE_DEVICE_H_ */