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