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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
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.
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.
58 #include "scic_config_parameters.h"
59 #include "remote_device.h"
62 #include "sci_base_state_machine.h"
63 #include "remote_node_table.h"
64 #include "registers.h"
65 #include "scu_unsolicited_frame.h"
66 #include "unsolicited_frame_control.h"
67 #include "scic_sds_port_configuration_agent.h"
69 struct scic_sds_request
;
70 struct scu_task_context
;
73 * struct scic_power_control -
75 * This structure defines the fields for managing power control for direct
76 * attached disk devices.
78 struct scic_power_control
{
80 * This field is set when the power control timer is running and cleared when
86 * This field is the handle to the driver timer object. This timer is used to
87 * control when the directed attached disks can consume power.
92 * This field is used to keep track of how many phys are put into the
98 * This field is used to keep track of how many phys have been granted to consume power
100 u8 phys_granted_power
;
103 * This field is an array of phys that we are waiting on. The phys are direct
104 * mapped into requesters via struct scic_sds_phy.phy_index
106 struct scic_sds_phy
*requesters
[SCI_MAX_PHYS
];
111 * struct scic_sds_controller -
113 * This structure represents the SCU controller object.
115 struct scic_sds_controller
{
117 * This field contains the information for the base controller state
120 struct sci_base_state_machine state_machine
;
123 * This field is the driver timer object handler used to time the controller
124 * object start and stop requests.
129 * This field contains the user parameters to be utilized for this
130 * core controller object.
132 union scic_user_parameters user_parameters
;
135 * This field contains the OEM parameters to be utilized for this
136 * core controller object.
138 union scic_oem_parameters oem_parameters
;
141 * This field contains the port configuration agent for this controller.
143 struct scic_sds_port_configuration_agent port_agent
;
146 * This field is the array of device objects that are currently constructed
147 * for this controller object. This table is used as a fast lookup of device
148 * objects that need to handle device completion notifications from the
149 * hardware. The table is RNi based.
151 struct scic_sds_remote_device
*device_table
[SCI_MAX_REMOTE_DEVICES
];
154 * This field is the array of IO request objects that are currently active for
155 * this controller object. This table is used as a fast lookup of the io
156 * request object that need to handle completion queue notifications. The
157 * table is TCi based.
159 struct scic_sds_request
*io_request_table
[SCI_MAX_IO_REQUESTS
];
162 * This field is the free RNi data structure
164 struct scic_remote_node_table available_remote_nodes
;
167 * This field is the TCi pool used to manage the task context index.
169 SCI_POOL_CREATE(tci_pool
, u16
, SCI_MAX_IO_REQUESTS
);
172 * This filed is the struct scic_power_control data used to controll when direct
173 * attached devices can consume power.
175 struct scic_power_control power_control
;
178 * This field is the array of sequence values for the IO Tag fields. Even
179 * though only 4 bits of the field is used for the sequence the sequence is 16
180 * bits in size so the sequence can be bitwise or'd with the TCi to build the
183 u16 io_request_sequence
[SCI_MAX_IO_REQUESTS
];
186 * This field in the array of sequence values for the RNi. These are used
187 * to control io request build to io request start operations. The sequence
188 * value is recorded into an io request when it is built and is checked on
189 * the io request start operation to make sure that there was not a device
190 * hot plug between the build and start operation.
192 u8 remote_device_sequence
[SCI_MAX_REMOTE_DEVICES
];
195 * This field is a pointer to the memory allocated by the driver for the task
196 * context table. This data is shared between the hardware and software.
198 struct scu_task_context
*task_context_table
;
201 * This field is a pointer to the memory allocated by the driver for the
202 * remote node context table. This table is shared between the hardware and
205 union scu_remote_node_context
*remote_node_context_table
;
208 * This field is a pointer to the completion queue. This memory is
209 * written to by the hardware and read by the software.
211 u32
*completion_queue
;
214 * This field is the software copy of the completion queue get pointer. The
215 * controller object writes this value to the hardware after processing the
216 * completion entries.
218 u32 completion_queue_get
;
221 * This field is the minimum of the number of hardware supported port entries
222 * and the software requested port entries.
224 u32 logical_port_entries
;
227 * This field is the minimum number of hardware supported completion queue
228 * entries and the software requested completion queue entries.
230 u32 completion_queue_entries
;
233 * This field is the minimum number of hardware supported event entries and
234 * the software requested event entries.
236 u32 completion_event_entries
;
239 * This field is the minimum number of devices supported by the hardware and
240 * the number of devices requested by the software.
242 u32 remote_node_entries
;
245 * This field is the minimum number of IO requests supported by the hardware
246 * and the number of IO requests requested by the software.
248 u32 task_context_entries
;
251 * This object contains all of the unsolicited frame specific
252 * data utilized by the core controller.
254 struct scic_sds_unsolicited_frame_control uf_control
;
256 /* Phy Startup Data */
258 * This field is the driver timer handle for controller phy request startup.
259 * On controller start the controller will start each PHY individually in
260 * order of phy index.
262 void *phy_startup_timer
;
265 * This field is set when the phy_startup_timer is running and is cleared when
266 * the phy_startup_timer is stopped.
268 bool phy_startup_timer_pending
;
271 * This field is the index of the next phy start. It is initialized to 0 and
272 * increments for each phy index that is started.
274 u32 next_phy_to_start
;
277 * This field controlls the invalid link up notifications to the SCI_USER. If
278 * an invalid_link_up notification is reported a bit for the PHY index is set
279 * so further notifications are not made. Once the PHY object reports link up
280 * and is made part of a port then this bit for the PHY index is cleared.
285 * This field saves the current interrupt coalescing number of the controller.
287 u16 interrupt_coalesce_number
;
290 * This field saves the current interrupt coalescing timeout value in microseconds.
292 u32 interrupt_coalesce_timeout
;
295 * This field is a pointer to the memory mapped register space for the
296 * struct smu_registers.
298 struct smu_registers __iomem
*smu_registers
;
301 * This field is a pointer to the memory mapped register space for the
302 * struct scu_registers.
304 struct scu_registers __iomem
*scu_registers
;
309 struct scic_sds_controller sci
;
310 union scic_oem_parameters oem_parameters
;
312 int id
; /* unique within a given pci device */
313 struct list_head timers
;
314 void *core_ctrl_memory
;
315 struct dma_pool
*dma_pool
;
316 struct isci_phy phys
[SCI_MAX_PHYS
];
317 struct isci_port ports
[SCI_MAX_PORTS
+ 1]; /* includes dummy port */
318 struct sas_ha_struct sas_ha
;
321 spinlock_t queue_lock
;
322 spinlock_t state_lock
;
324 struct pci_dev
*pdev
;
326 enum isci_status status
;
327 #define IHOST_START_PENDING 0
328 #define IHOST_STOP_PENDING 1
330 wait_queue_head_t eventq
;
331 struct Scsi_Host
*shost
;
332 struct tasklet_struct completion_tasklet
;
333 struct list_head requests_to_complete
;
334 struct list_head requests_to_errorback
;
335 spinlock_t scic_lock
;
337 struct isci_remote_device devices
[SCI_MAX_REMOTE_DEVICES
];
341 * enum scic_sds_controller_states - This enumeration depicts all the states
342 * for the common controller state machine.
344 enum scic_sds_controller_states
{
346 * Simply the initial state for the base controller state machine.
348 SCI_BASE_CONTROLLER_STATE_INITIAL
= 0,
351 * This state indicates that the controller is reset. The memory for
352 * the controller is in it's initial state, but the controller requires
354 * This state is entered from the INITIAL state.
355 * This state is entered from the RESETTING state.
357 SCI_BASE_CONTROLLER_STATE_RESET
,
360 * This state is typically an action state that indicates the controller
361 * is in the process of initialization. In this state no new IO operations
363 * This state is entered from the RESET state.
365 SCI_BASE_CONTROLLER_STATE_INITIALIZING
,
368 * This state indicates that the controller has been successfully
369 * initialized. In this state no new IO operations are permitted.
370 * This state is entered from the INITIALIZING state.
372 SCI_BASE_CONTROLLER_STATE_INITIALIZED
,
375 * This state indicates the the controller is in the process of becoming
376 * ready (i.e. starting). In this state no new IO operations are permitted.
377 * This state is entered from the INITIALIZED state.
379 SCI_BASE_CONTROLLER_STATE_STARTING
,
382 * This state indicates the controller is now ready. Thus, the user
383 * is able to perform IO operations on the controller.
384 * This state is entered from the STARTING state.
386 SCI_BASE_CONTROLLER_STATE_READY
,
389 * This state is typically an action state that indicates the controller
390 * is in the process of resetting. Thus, the user is unable to perform
391 * IO operations on the controller. A reset is considered destructive in
393 * This state is entered from the READY state.
394 * This state is entered from the FAILED state.
395 * This state is entered from the STOPPED state.
397 SCI_BASE_CONTROLLER_STATE_RESETTING
,
400 * This state indicates that the controller is in the process of stopping.
401 * In this state no new IO operations are permitted, but existing IO
402 * operations are allowed to complete.
403 * This state is entered from the READY state.
405 SCI_BASE_CONTROLLER_STATE_STOPPING
,
408 * This state indicates that the controller has successfully been stopped.
409 * In this state no new IO operations are permitted.
410 * This state is entered from the STOPPING state.
412 SCI_BASE_CONTROLLER_STATE_STOPPED
,
415 * This state indicates that the controller could not successfully be
416 * initialized. In this state no new IO operations are permitted.
417 * This state is entered from the INITIALIZING state.
418 * This state is entered from the STARTING state.
419 * This state is entered from the STOPPING state.
420 * This state is entered from the RESETTING state.
422 SCI_BASE_CONTROLLER_STATE_FAILED
,
424 SCI_BASE_CONTROLLER_MAX_STATES
431 * struct isci_pci_info - This class represents the pci function containing the
432 * controllers. Depending on PCI SKU, there could be up to 2 controllers in
435 #define SCI_MAX_MSIX_INT (SCI_NUM_MSI_X_INT*SCI_MAX_CONTROLLERS)
437 struct isci_pci_info
{
438 struct msix_entry msix_entries
[SCI_MAX_MSIX_INT
];
439 struct isci_host
*hosts
[SCI_MAX_CONTROLLERS
];
440 struct isci_orom
*orom
;
443 static inline struct isci_pci_info
*to_pci_info(struct pci_dev
*pdev
)
445 return pci_get_drvdata(pdev
);
448 #define for_each_isci_host(id, ihost, pdev) \
449 for (id = 0, ihost = to_pci_info(pdev)->hosts[id]; \
450 id < ARRAY_SIZE(to_pci_info(pdev)->hosts) && ihost; \
451 ihost = to_pci_info(pdev)->hosts[++id])
453 static inline enum isci_status
isci_host_get_state(struct isci_host
*isci_host
)
455 return isci_host
->status
;
458 static inline void isci_host_change_state(struct isci_host
*isci_host
,
459 enum isci_status status
)
463 dev_dbg(&isci_host
->pdev
->dev
,
464 "%s: isci_host = %p, state = 0x%x",
468 spin_lock_irqsave(&isci_host
->state_lock
, flags
);
469 isci_host
->status
= status
;
470 spin_unlock_irqrestore(&isci_host
->state_lock
, flags
);
474 static inline int isci_host_can_queue(struct isci_host
*isci_host
, int num
)
479 spin_lock_irqsave(&isci_host
->queue_lock
, flags
);
480 if ((isci_host
->can_queue
- num
) < 0) {
481 dev_dbg(&isci_host
->pdev
->dev
,
482 "%s: isci_host->can_queue = %d\n",
484 isci_host
->can_queue
);
485 ret
= -SAS_QUEUE_FULL
;
488 isci_host
->can_queue
-= num
;
490 spin_unlock_irqrestore(&isci_host
->queue_lock
, flags
);
495 static inline void isci_host_can_dequeue(struct isci_host
*isci_host
, int num
)
499 spin_lock_irqsave(&isci_host
->queue_lock
, flags
);
500 isci_host
->can_queue
+= num
;
501 spin_unlock_irqrestore(&isci_host
->queue_lock
, flags
);
504 static inline void wait_for_start(struct isci_host
*ihost
)
506 wait_event(ihost
->eventq
, !test_bit(IHOST_START_PENDING
, &ihost
->flags
));
509 static inline void wait_for_stop(struct isci_host
*ihost
)
511 wait_event(ihost
->eventq
, !test_bit(IHOST_STOP_PENDING
, &ihost
->flags
));
514 static inline void wait_for_device_start(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
516 wait_event(ihost
->eventq
, !test_bit(IDEV_START_PENDING
, &idev
->flags
));
519 static inline void wait_for_device_stop(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
521 wait_event(ihost
->eventq
, !test_bit(IDEV_STOP_PENDING
, &idev
->flags
));
524 static inline struct isci_host
*dev_to_ihost(struct domain_device
*dev
)
526 return dev
->port
->ha
->lldd_ha
;
529 static inline struct isci_host
*scic_to_ihost(struct scic_sds_controller
*scic
)
531 /* XXX delete after merging scic_sds_contoller and isci_host */
532 struct isci_host
*ihost
= container_of(scic
, typeof(*ihost
), sci
);
538 * INCREMENT_QUEUE_GET() -
540 * This macro will increment the specified index to and if the index wraps to 0
541 * it will toggel the cycle bit.
543 #define INCREMENT_QUEUE_GET(index, cycle, entry_count, bit_toggle) \
545 if ((index) + 1 == entry_count) { \
547 (cycle) = (cycle) ^ (bit_toggle); \
554 * scic_sds_controller_get_port_configuration_agent() -
556 * This is a helper macro to get the port configuration agent from the
559 #define scic_sds_controller_get_port_configuration_agent(controller) \
560 (&(controller)->port_agent)
563 * scic_sds_controller_get_protocol_engine_group() -
565 * This macro returns the protocol engine group for this controller object.
566 * Presently we only support protocol engine group 0 so just return that
568 #define scic_sds_controller_get_protocol_engine_group(controller) 0
571 * scic_sds_io_tag_construct() -
573 * This macro constructs an IO tag from the sequence and index values.
575 #define scic_sds_io_tag_construct(sequence, task_index) \
576 ((sequence) << 12 | (task_index))
579 * scic_sds_io_tag_get_sequence() -
581 * This macro returns the IO sequence from the IO tag value.
583 #define scic_sds_io_tag_get_sequence(io_tag) \
584 (((io_tag) & 0xF000) >> 12)
587 * scic_sds_io_tag_get_index() -
589 * This macro returns the TCi from the io tag value
591 #define scic_sds_io_tag_get_index(io_tag) \
595 * scic_sds_io_sequence_increment() -
597 * This is a helper macro to increment the io sequence count. We may find in
598 * the future that it will be faster to store the sequence count in such a way
599 * as we dont perform the shift operation to build io tag values so therefore
600 * need a way to incrment them correctly
602 #define scic_sds_io_sequence_increment(value) \
603 ((value) = (((value) + 1) & 0x000F))
605 /* expander attached sata devices require 3 rnc slots */
606 static inline int scic_sds_remote_device_node_count(struct scic_sds_remote_device
*sci_dev
)
608 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
610 if ((dev
->dev_type
== SATA_DEV
|| (dev
->tproto
& SAS_PROTOCOL_STP
)) &&
611 !sci_dev
->is_direct_attached
)
612 return SCU_STP_REMOTE_NODE_COUNT
;
613 return SCU_SSP_REMOTE_NODE_COUNT
;
617 * scic_sds_controller_set_invalid_phy() -
619 * This macro will set the bit in the invalid phy mask for this controller
620 * object. This is used to control messages reported for invalid link up
623 #define scic_sds_controller_set_invalid_phy(controller, phy) \
624 ((controller)->invalid_phy_mask |= (1 << (phy)->phy_index))
627 * scic_sds_controller_clear_invalid_phy() -
629 * This macro will clear the bit in the invalid phy mask for this controller
630 * object. This is used to control messages reported for invalid link up
633 #define scic_sds_controller_clear_invalid_phy(controller, phy) \
634 ((controller)->invalid_phy_mask &= ~(1 << (phy)->phy_index))
636 static inline struct device
*scic_to_dev(struct scic_sds_controller
*scic
)
638 return &scic_to_ihost(scic
)->pdev
->dev
;
641 static inline struct device
*sciphy_to_dev(struct scic_sds_phy
*sci_phy
)
643 struct isci_phy
*iphy
= sci_phy_to_iphy(sci_phy
);
645 if (!iphy
|| !iphy
->isci_port
|| !iphy
->isci_port
->isci_host
)
648 return &iphy
->isci_port
->isci_host
->pdev
->dev
;
651 static inline struct device
*sciport_to_dev(struct scic_sds_port
*sci_port
)
653 struct isci_port
*iport
= sci_port_to_iport(sci_port
);
655 if (!iport
|| !iport
->isci_host
)
658 return &iport
->isci_host
->pdev
->dev
;
661 static inline struct device
*scirdev_to_dev(struct scic_sds_remote_device
*sci_dev
)
663 struct isci_remote_device
*idev
=
664 container_of(sci_dev
, typeof(*idev
), sci
);
666 if (!idev
|| !idev
->isci_port
|| !idev
->isci_port
->isci_host
)
669 return &idev
->isci_port
->isci_host
->pdev
->dev
;
678 extern int isci_si_rev
;
680 static inline bool is_a0(void)
682 return isci_si_rev
== ISCI_SI_REVA0
;
685 static inline bool is_a2(void)
687 return isci_si_rev
== ISCI_SI_REVA2
;
690 static inline bool is_b0(void)
692 return isci_si_rev
> ISCI_SI_REVA2
;
695 void scic_sds_controller_post_request(struct scic_sds_controller
*scic
,
697 void scic_sds_controller_release_frame(struct scic_sds_controller
*scic
,
699 void scic_sds_controller_copy_sata_response(void *response_buffer
,
702 enum sci_status
scic_sds_controller_allocate_remote_node_context(struct scic_sds_controller
*scic
,
703 struct scic_sds_remote_device
*sci_dev
,
705 void scic_sds_controller_free_remote_node_context(
706 struct scic_sds_controller
*scic
,
707 struct scic_sds_remote_device
*sci_dev
,
709 union scu_remote_node_context
*scic_sds_controller_get_remote_node_context_buffer(
710 struct scic_sds_controller
*scic
,
713 struct scic_sds_request
*scic_request_by_tag(struct scic_sds_controller
*scic
,
716 struct scu_task_context
*scic_sds_controller_get_task_context_buffer(
717 struct scic_sds_controller
*scic
,
720 void scic_sds_controller_power_control_queue_insert(
721 struct scic_sds_controller
*scic
,
722 struct scic_sds_phy
*sci_phy
);
724 void scic_sds_controller_power_control_queue_remove(
725 struct scic_sds_controller
*scic
,
726 struct scic_sds_phy
*sci_phy
);
728 void scic_sds_controller_link_up(
729 struct scic_sds_controller
*scic
,
730 struct scic_sds_port
*sci_port
,
731 struct scic_sds_phy
*sci_phy
);
733 void scic_sds_controller_link_down(
734 struct scic_sds_controller
*scic
,
735 struct scic_sds_port
*sci_port
,
736 struct scic_sds_phy
*sci_phy
);
738 void scic_sds_controller_remote_device_stopped(
739 struct scic_sds_controller
*scic
,
740 struct scic_sds_remote_device
*sci_dev
);
742 void scic_sds_controller_copy_task_context(
743 struct scic_sds_controller
*scic
,
744 struct scic_sds_request
*this_request
);
746 void scic_sds_controller_register_setup(struct scic_sds_controller
*scic
);
748 enum sci_status
scic_controller_continue_io(struct scic_sds_request
*sci_req
);
749 int isci_host_scan_finished(struct Scsi_Host
*, unsigned long);
750 void isci_host_scan_start(struct Scsi_Host
*);
752 int isci_host_init(struct isci_host
*);
754 void isci_host_init_controller_names(
755 struct isci_host
*isci_host
,
756 unsigned int controller_idx
);
758 void isci_host_deinit(
761 void isci_host_port_link_up(
763 struct scic_sds_port
*,
764 struct scic_sds_phy
*);
765 int isci_host_dev_found(struct domain_device
*);
767 void isci_host_remote_device_start_complete(
769 struct isci_remote_device
*,
772 void scic_controller_disable_interrupts(
773 struct scic_sds_controller
*scic
);
775 enum sci_status
scic_controller_start_io(
776 struct scic_sds_controller
*scic
,
777 struct scic_sds_remote_device
*remote_device
,
778 struct scic_sds_request
*io_request
,
781 enum sci_task_status
scic_controller_start_task(
782 struct scic_sds_controller
*scic
,
783 struct scic_sds_remote_device
*remote_device
,
784 struct scic_sds_request
*task_request
,
787 enum sci_status
scic_controller_terminate_request(
788 struct scic_sds_controller
*scic
,
789 struct scic_sds_remote_device
*remote_device
,
790 struct scic_sds_request
*request
);
792 enum sci_status
scic_controller_complete_io(
793 struct scic_sds_controller
*scic
,
794 struct scic_sds_remote_device
*remote_device
,
795 struct scic_sds_request
*io_request
);
797 u16
scic_controller_allocate_io_tag(
798 struct scic_sds_controller
*scic
);
800 enum sci_status
scic_controller_free_io_tag(
801 struct scic_sds_controller
*scic
,