isci: Intel(R) C600 Series Chipset Storage Control Unit Driver
[linux-flexiantxendom0-3.2.10.git] / drivers / scsi / isci / core / scic_sds_controller.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_CONTROLLER_H_
57 #define _SCIC_SDS_CONTROLLER_H_
58
59 /**
60  * This file contains the structures, constants and prototypes used for the
61  *    core controller object.
62  *
63  *
64  */
65
66 #include "sci_pool.h"
67 #include "sci_controller_constants.h"
68 #include "sci_memory_descriptor_list.h"
69 #include "sci_base_controller.h"
70 #include "scic_config_parameters.h"
71 #include "scic_sds_port.h"
72 #include "scic_sds_phy.h"
73 #include "scic_sds_remote_node_table.h"
74 #include "scu_registers.h"
75 #include "scu_constants.h"
76 #include "scu_remote_node_context.h"
77 #include "scu_task_context.h"
78 #include "scu_unsolicited_frame.h"
79 #include "scic_sds_unsolicited_frame_control.h"
80 #include "scic_sds_port_configuration_agent.h"
81 #include "scic_sds_pci.h"
82
83 struct scic_sds_remote_device;
84 struct scic_sds_request;
85 struct scic_sds_controller;
86
87
88 #define SCU_COMPLETION_RAM_ALIGNMENT            (64)
89
90 /**
91  * enum SCIC_SDS_CONTROLLER_MEMORY_DESCRIPTORS -
92  *
93  * This enumeration depects the types of MDEs that are going to be created for
94  * the controller object.
95  */
96 enum SCIC_SDS_CONTROLLER_MEMORY_DESCRIPTORS {
97         /**
98          * Completion queue MDE entry
99          */
100         SCU_MDE_COMPLETION_QUEUE,
101
102         /**
103          * Remote node context MDE entry
104          */
105         SCU_MDE_REMOTE_NODE_CONTEXT,
106
107         /**
108          * Task context MDE entry
109          */
110         SCU_MDE_TASK_CONTEXT,
111
112         /**
113          * Unsolicited frame buffer MDE entrys this is the start of the unsolicited
114          * frame buffer entries.
115          */
116         SCU_MDE_UF_BUFFER,
117
118         SCU_MAX_MDES
119 };
120
121 /**
122  *
123  *
124  * Allowed PORT configuration modes APC Automatic PORT configuration mode is
125  * defined by the OEM configuration parameters providing no PHY_MASK parameters
126  * for any PORT. i.e. There are no phys assigned to any of the ports at start.
127  * MPC Manual PORT configuration mode is defined by the OEM configuration
128  * parameters providing a PHY_MASK value for any PORT.  It is assumed that any
129  * PORT with no PHY_MASK is an invalid port and not all PHYs must be assigned.
130  * A PORT_PHY mask that assigns just a single PHY to a port and no other PHYs
131  * being assigned is sufficient to declare manual PORT configuration.
132  */
133 enum SCIC_PORT_CONFIGURATION_MODE {
134         SCIC_PORT_MANUAL_CONFIGURATION_MODE,
135         SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE
136 };
137
138 /**
139  * struct scic_power_control -
140  *
141  * This structure defines the fields for managing power control for direct
142  * attached disk devices.
143  */
144 struct scic_power_control {
145         /**
146          * This field is set when the power control timer is running and cleared when
147          * it is not.
148          */
149         bool timer_started;
150
151         /**
152          * This field is the handle to the driver timer object.  This timer is used to
153          * control when the directed attached disks can consume power.
154          */
155         void *timer;
156
157         /**
158          * This field is used to keep track of how many phys are put into the
159          * requesters field.
160          */
161         u8 phys_waiting;
162
163         /**
164          * This field is an array of phys that we are waiting on. The phys are direct
165          * mapped into requesters via struct scic_sds_phy.phy_index
166          */
167         struct scic_sds_phy *requesters[SCI_MAX_PHYS];
168
169 };
170
171 /**
172  * struct scic_sds_controller -
173  *
174  * This structure represents the SCU contoller object.
175  */
176 struct scic_sds_controller {
177         /**
178          * The struct sci_base_controller is the parent object for the struct scic_sds_controller
179          * object.
180          */
181         struct sci_base_controller parent;
182
183         /**
184          * This field is the driver timer object handler used to time the controller
185          * object start and stop requests.
186          */
187         void *timeout_timer;
188
189         /**
190          * This field contains the user parameters to be utilized for this
191          * core controller object.
192          */
193         union scic_user_parameters user_parameters;
194
195         /**
196          * This field contains the OEM parameters to be utilized for this
197          * core controller object.
198          */
199         union scic_oem_parameters oem_parameters;
200
201         /**
202          * This field contains the port configuration agent for this controller.
203          */
204         struct scic_sds_port_configuration_agent port_agent;
205
206         /**
207          * This field is the array of port objects that are controlled by this
208          * controller object.  There is one dummy port object also contained within
209          * this controller object.
210          */
211         struct scic_sds_port port_table[SCI_MAX_PORTS + 1];
212
213         /**
214          * This field is the array of phy objects that are controlled by this
215          * controller object.
216          */
217         struct scic_sds_phy phy_table[SCI_MAX_PHYS];
218
219         /**
220          * This field is the array of device objects that are currently constructed
221          * for this controller object.  This table is used as a fast lookup of device
222          * objects that need to handle device completion notifications from the
223          * hardware. The table is RNi based.
224          */
225         struct scic_sds_remote_device *device_table[SCI_MAX_REMOTE_DEVICES];
226
227         /**
228          * This field is the array of IO request objects that are currently active for
229          * this controller object.  This table is used as a fast lookup of the io
230          * request object that need to handle completion queue notifications.  The
231          * table is TCi based.
232          */
233         struct scic_sds_request *io_request_table[SCI_MAX_IO_REQUESTS];
234
235         /**
236          * This field is the free RNi data structure
237          */
238         struct scic_remote_node_table available_remote_nodes;
239
240         /**
241          * This field is the TCi pool used to manage the task context index.
242          */
243         SCI_POOL_CREATE(tci_pool, u16, SCI_MAX_IO_REQUESTS);
244
245         /**
246          * This filed is the struct scic_power_control data used to controll when direct
247          * attached devices can consume power.
248          */
249         struct scic_power_control power_control;
250
251         /**
252          * This field is the array of sequence values for the IO Tag fields.  Even
253          * though only 4 bits of the field is used for the sequence the sequence is 16
254          * bits in size so the sequence can be bitwise or'd with the TCi to build the
255          * IO Tag value.
256          */
257         u16 io_request_sequence[SCI_MAX_IO_REQUESTS];
258
259         /**
260          * This field in the array of sequence values for the RNi.  These are used
261          * to control io request build to io request start operations.  The sequence
262          * value is recorded into an io request when it is built and is checked on
263          * the io request start operation to make sure that there was not a device
264          * hot plug between the build and start operation.
265          */
266         u8 remote_device_sequence[SCI_MAX_REMOTE_DEVICES];
267
268         /**
269          * This field is a pointer to the memory allocated by the driver for the task
270          * context table.  This data is shared between the hardware and software.
271          */
272         struct scu_task_context *task_context_table;
273
274         /**
275          * This field is a pointer to the memory allocated by the driver for the
276          * remote node context table.  This table is shared between the hardware and
277          * software.
278          */
279         union scu_remote_node_context *remote_node_context_table;
280
281         /**
282          * This field is the array of physical memory requiremets for this controller
283          * object.
284          */
285         struct sci_physical_memory_descriptor memory_descriptors[SCU_MAX_MDES];
286
287         /**
288          * This field is a pointer to the completion queue.  This memory is
289          * written to by the hardware and read by the software.
290          */
291         u32 *completion_queue;
292
293         /**
294          * This field is the software copy of the completion queue get pointer.  The
295          * controller object writes this value to the hardware after processing the
296          * completion entries.
297          */
298         u32 completion_queue_get;
299
300         /**
301          * This field is the minimum of the number of hardware supported port entries
302          * and the software requested port entries.
303          */
304         u32 logical_port_entries;
305
306         /**
307          * This field is the minimum number of hardware supported completion queue
308          * entries and the software requested completion queue entries.
309          */
310         u32 completion_queue_entries;
311
312         /**
313          * This field is the minimum number of hardware supported event entries and
314          * the software requested event entries.
315          */
316         u32 completion_event_entries;
317
318         /**
319          * This field is the minimum number of devices supported by the hardware and
320          * the number of devices requested by the software.
321          */
322         u32 remote_node_entries;
323
324         /**
325          * This field is the minimum number of IO requests supported by the hardware
326          * and the number of IO requests requested by the software.
327          */
328         u32 task_context_entries;
329
330         /**
331          * This object contains all of the unsolicited frame specific
332          * data utilized by the core controller.
333          */
334         struct scic_sds_unsolicited_frame_control uf_control;
335
336         /**
337          * This field records the fact that the controller has encountered a fatal
338          * error and must be reset.
339          */
340         bool encountered_fatal_error;
341
342         /**
343          * This field specifies that the controller should ignore
344          * completion processing for non-fastpath events.  This will
345          * cause the completions to be thrown away.
346          */
347         bool restrict_completions;
348
349         /* Phy Startup Data */
350         /**
351          * This field is the driver timer handle for controller phy request startup.
352          * On controller start the controller will start each PHY individually in
353          * order of phy index.
354          */
355         void *phy_startup_timer;
356
357         /**
358          * This field is set when the phy_startup_timer is running and is cleared when
359          * the phy_startup_timer is stopped.
360          */
361         bool phy_startup_timer_pending;
362
363         /**
364          * This field is the index of the next phy start.  It is initialized to 0 and
365          * increments for each phy index that is started.
366          */
367         u32 next_phy_to_start;
368
369         /**
370          * This field controlls the invalid link up notifications to the SCI_USER.  If
371          * an invalid_link_up notification is reported a bit for the PHY index is set
372          * so further notifications are not made.  Once the PHY object reports link up
373          * and is made part of a port then this bit for the PHY index is cleared.
374          */
375         u8 invalid_phy_mask;
376
377         /*
378          * This field saves the current interrupt coalescing number of the controller.
379          */
380         u16 interrupt_coalesce_number;
381
382         /*
383          * This field saves the current interrupt coalescing timeout value in microseconds.
384          */
385         u32 interrupt_coalesce_timeout;
386
387         /**
388          * This field is a pointer to the memory mapped register space for the
389          * struct smu_registers.
390          */
391         struct smu_registers __iomem *smu_registers;
392
393         /**
394          * This field is a pointer to the memory mapped register space for the
395          * struct scu_registers.
396          */
397         struct scu_registers __iomem *scu_registers;
398
399 };
400
401 typedef void (*scic_sds_controller_phy_handler_t)(struct scic_sds_controller *,
402                                                   struct scic_sds_port *,
403                                                   struct scic_sds_phy *);
404 /**
405  * struct scic_sds_controller_state_handler -
406  *
407  * This structure contains the SDS core specific definition for the state
408  * handlers.
409  */
410 struct scic_sds_controller_state_handler {
411         struct sci_base_controller_state_handler base;
412
413         sci_base_controller_request_handler_t terminate_request;
414         scic_sds_controller_phy_handler_t link_up;
415         scic_sds_controller_phy_handler_t link_down;
416 };
417
418 extern const struct scic_sds_controller_state_handler
419         scic_sds_controller_state_handler_table[];
420 extern const struct sci_base_state scic_sds_controller_state_table[];
421
422 /**
423  * INCREMENT_QUEUE_GET() -
424  *
425  * This macro will increment the specified index to and if the index wraps to 0
426  * it will toggel the cycle bit.
427  */
428 #define INCREMENT_QUEUE_GET(index, cycle, entry_count, bit_toggle) \
429         { \
430                 if ((index) + 1 == entry_count) {       \
431                         (index) = 0; \
432                         (cycle) = (cycle) ^ (bit_toggle); \
433                 } else { \
434                         index = index + 1; \
435                 } \
436         }
437
438 /**
439  * scic_sds_controller_get_base_state_machine() -
440  *
441  * This is a helper macro that gets the base state machine for the controller
442  * object
443  */
444 #define scic_sds_controller_get_base_state_machine(this_controller) \
445         (&(this_controller)->parent.state_machine)
446
447 /**
448  * scic_sds_controller_get_port_configuration_agent() -
449  *
450  * This is a helper macro to get the port configuration agent from the
451  * controller object.
452  */
453 #define scic_sds_controller_get_port_configuration_agent(controller) \
454         (&(controller)->port_agent)
455
456 /**
457  * smu_register_write() -
458  *
459  * This macro writes to the smu_register for this controller
460  */
461 #define smu_register_write(controller, reg, value) \
462         scic_sds_pci_write_smu_dword((controller), &(reg), (value))
463
464 /**
465  * smu_register_read() -
466  *
467  * This macro reads the smu_register for this controller
468  */
469 #define smu_register_read(controller, reg) \
470         scic_sds_pci_read_smu_dword((controller), &(reg))
471
472 /**
473  * scu_register_write() -
474  *
475  * This mcaro writes the scu_register for this controller
476  */
477 #define scu_register_write(controller, reg, value) \
478         scic_sds_pci_write_scu_dword((controller), &(reg), (value))
479
480 /**
481  * scu_register_read() -
482  *
483  * This macro reads the scu_register for this controller
484  */
485 #define scu_register_read(controller, reg) \
486         scic_sds_pci_read_scu_dword((controller), &(reg))
487
488 /**
489  * scic_sds_controller_get_protocol_engine_group() -
490  *
491  * This macro returns the protocol engine group for this controller object.
492  * Presently we only support protocol engine group 0 so just return that
493  */
494 #define scic_sds_controller_get_protocol_engine_group(controller) 0
495
496 /**
497  * scic_sds_io_tag_construct() -
498  *
499  * This macro constructs an IO tag from the sequence and index values.
500  */
501 #define scic_sds_io_tag_construct(sequence, task_index) \
502         ((sequence) << 12 | (task_index))
503
504 /**
505  * scic_sds_io_tag_get_sequence() -
506  *
507  * This macro returns the IO sequence from the IO tag value.
508  */
509 #define scic_sds_io_tag_get_sequence(io_tag) \
510         (((io_tag) & 0xF000) >> 12)
511
512 /**
513  * scic_sds_io_tag_get_index() -
514  *
515  * This macro returns the TCi from the io tag value
516  */
517 #define scic_sds_io_tag_get_index(io_tag) \
518         ((io_tag) & 0x0FFF)
519
520 /**
521  * scic_sds_io_sequence_increment() -
522  *
523  * This is a helper macro to increment the io sequence count. We may find in
524  * the future that it will be faster to store the sequence count in such a way
525  * as we dont perform the shift operation to build io tag values so therefore
526  * need a way to incrment them correctly
527  */
528 #define scic_sds_io_sequence_increment(value) \
529         ((value) = (((value) + 1) & 0x000F))
530
531 #define scic_sds_remote_device_node_count(device) \
532         (\
533                 (\
534                         (device)->target_protocols.u.bits.attached_stp_target \
535                         && ((device)->is_direct_attached != true) \
536                 ) \
537                 ? SCU_STP_REMOTE_NODE_COUNT : SCU_SSP_REMOTE_NODE_COUNT \
538         )
539
540 /**
541  * scic_sds_controller_set_invalid_phy() -
542  *
543  * This macro will set the bit in the invalid phy mask for this controller
544  * object.  This is used to control messages reported for invalid link up
545  * notifications.
546  */
547 #define scic_sds_controller_set_invalid_phy(controller, phy) \
548         ((controller)->invalid_phy_mask |= (1 << (phy)->phy_index))
549
550 /**
551  * scic_sds_controller_clear_invalid_phy() -
552  *
553  * This macro will clear the bit in the invalid phy mask for this controller
554  * object.  This is used to control messages reported for invalid link up
555  * notifications.
556  */
557 #define scic_sds_controller_clear_invalid_phy(controller, phy) \
558         ((controller)->invalid_phy_mask &= ~(1 << (phy)->phy_index))
559
560 /* --------------------------------------------------------------------------- */
561
562 u32 scic_sds_controller_get_object_size(void);
563
564 /* --------------------------------------------------------------------------- */
565
566
567 /* --------------------------------------------------------------------------- */
568
569 enum SCIC_PORT_CONFIGURATION_MODE scic_sds_controller_get_port_configuration_mode(
570         struct scic_sds_controller *this_controller);
571
572 /* --------------------------------------------------------------------------- */
573
574 void scic_sds_controller_post_request(
575         struct scic_sds_controller *this_controller,
576         u32 request);
577
578 /* --------------------------------------------------------------------------- */
579
580 void scic_sds_controller_release_frame(
581         struct scic_sds_controller *this_controller,
582         u32 frame_index);
583
584 void scic_sds_controller_copy_sata_response(
585         void *response_buffer,
586         void *frame_header,
587         void *frame_buffer);
588
589 /* --------------------------------------------------------------------------- */
590
591 enum sci_status scic_sds_controller_allocate_remote_node_context(
592         struct scic_sds_controller *this_controller,
593         struct scic_sds_remote_device *the_device,
594         u16 *node_id);
595
596 void scic_sds_controller_free_remote_node_context(
597         struct scic_sds_controller *this_controller,
598         struct scic_sds_remote_device *the_device,
599         u16 node_id);
600
601 union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
602         struct scic_sds_controller *this_controller,
603         u16 node_id);
604
605 /* --------------------------------------------------------------------------- */
606
607 struct scic_sds_request *scic_sds_controller_get_io_request_from_tag(
608         struct scic_sds_controller *this_controller,
609         u16 io_tag);
610
611
612 struct scu_task_context *scic_sds_controller_get_task_context_buffer(
613         struct scic_sds_controller *this_controller,
614         u16 io_tag);
615
616 /*
617  * *****************************************************************************
618  * * CORE CONTROLLER POWER CONTROL METHODS
619  * ***************************************************************************** */
620
621
622 void scic_sds_controller_power_control_queue_insert(
623         struct scic_sds_controller *this_controller,
624         struct scic_sds_phy *the_phy);
625
626 void scic_sds_controller_power_control_queue_remove(
627         struct scic_sds_controller *this_controller,
628         struct scic_sds_phy *the_phy);
629
630 /*
631  * *****************************************************************************
632  * * CORE CONTROLLER PHY MESSAGE PROCESSING
633  * ***************************************************************************** */
634
635 void scic_sds_controller_link_up(
636         struct scic_sds_controller *this_controller,
637         struct scic_sds_port *the_port,
638         struct scic_sds_phy *the_phy);
639
640 void scic_sds_controller_link_down(
641         struct scic_sds_controller *this_controller,
642         struct scic_sds_port *the_port,
643         struct scic_sds_phy *the_phy);
644
645 /*
646  * *****************************************************************************
647  * * CORE CONTROLLER PRIVATE METHODS
648  * ***************************************************************************** */
649
650 enum sci_status scic_sds_controller_validate_memory_descriptor_table(
651         struct scic_sds_controller *this_controller);
652
653 void scic_sds_controller_ram_initialization(
654         struct scic_sds_controller *this_controller);
655
656 void scic_sds_controller_assign_task_entries(
657         struct scic_sds_controller *this_controller);
658
659 void scic_sds_controller_afe_initialization(
660         struct scic_sds_controller *this_controller);
661
662 void scic_sds_controller_enable_port_task_scheduler(
663         struct scic_sds_controller *this_controller);
664
665 void scic_sds_controller_initialize_completion_queue(
666         struct scic_sds_controller *this_controller);
667
668 void scic_sds_controller_initialize_unsolicited_frame_queue(
669         struct scic_sds_controller *this_controller);
670
671 void scic_sds_controller_phy_timer_stop(
672         struct scic_sds_controller *this_controller);
673
674 enum sci_status scic_sds_controller_start_next_phy(
675         struct scic_sds_controller *this_controller);
676
677 enum sci_status scic_sds_controller_stop_phys(
678         struct scic_sds_controller *this_controller);
679
680 enum sci_status scic_sds_controller_stop_ports(
681         struct scic_sds_controller *this_controller);
682
683 enum sci_status scic_sds_controller_stop_devices(
684         struct scic_sds_controller *this_controller);
685
686 void scic_sds_controller_copy_task_context(
687         struct scic_sds_controller *this_controller,
688         struct scic_sds_request *this_request);
689
690 void scic_sds_controller_timeout_handler(
691         struct scic_sds_controller *controller);
692
693 void scic_sds_controller_initialize_power_control(
694         struct scic_sds_controller *this_controller);
695
696 void scic_sds_controller_register_setup(
697         struct scic_sds_controller *this_controller);
698
699 void scic_sds_controller_reset_hardware(
700         struct scic_sds_controller *this_controller);
701
702
703 void scic_sds_controller_initialize_phy_startup(
704         struct scic_sds_controller *this_controller);
705
706 #endif /* _SCIC_SDS_CONTROLLER_H_ */