2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
15 #include <linux/acpi.h>
16 #include <linux/clk.h>
17 #include <linux/dmapool.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
23 #include <linux/regmap.h>
24 #include <scsi/sas_ata.h>
25 #include <scsi/libsas.h>
27 #define DRV_VERSION "v1.6"
29 #define HISI_SAS_MAX_PHYS 9
30 #define HISI_SAS_MAX_QUEUES 32
31 #define HISI_SAS_QUEUE_SLOTS 512
32 #define HISI_SAS_MAX_ITCT_ENTRIES 2048
33 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
34 #define HISI_SAS_RESET_BIT 0
36 #define HISI_SAS_STATUS_BUF_SZ \
37 (sizeof(struct hisi_sas_err_record) + 1024)
38 #define HISI_SAS_COMMAND_TABLE_SZ \
39 (((sizeof(union hisi_sas_command_table)+3)/4)*4)
41 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
42 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
43 #define HISI_SAS_MAX_STP_RESP_SZ 28
45 #define DEV_IS_EXPANDER(type) \
46 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
47 (type == SAS_FANOUT_EXPANDER_DEVICE))
52 PORT_TYPE_SAS
= (1U << 1),
53 PORT_TYPE_SATA
= (1U << 0),
62 HISI_SAS_INT_ABT_CMD
= 0,
63 HISI_SAS_INT_ABT_DEV
= 1,
66 enum hisi_sas_dev_type
{
67 HISI_SAS_DEV_TYPE_STP
= 0,
68 HISI_SAS_DEV_TYPE_SSP
,
69 HISI_SAS_DEV_TYPE_SATA
,
73 struct hisi_hba
*hisi_hba
;
74 struct hisi_sas_port
*port
;
75 struct asd_sas_phy sas_phy
;
76 struct sas_identify identify
;
77 struct timer_list timer
;
78 struct work_struct phyup_ws
;
79 u64 port_id
; /* from hw */
86 enum sas_linkrate minimum_linkrate
;
87 enum sas_linkrate maximum_linkrate
;
90 struct hisi_sas_port
{
91 struct asd_sas_port sas_port
;
94 struct list_head list
;
98 struct hisi_hba
*hisi_hba
;
99 struct tasklet_struct tasklet
;
105 struct hisi_hba
*hisi_hba
;
110 struct hisi_sas_device
{
111 enum sas_device_type dev_type
;
112 struct hisi_hba
*hisi_hba
;
113 struct domain_device
*sas_device
;
116 atomic64_t running_req
;
120 struct hisi_sas_slot
{
121 struct list_head entry
;
122 struct sas_task
*task
;
123 struct hisi_sas_port
*port
;
126 int dlvry_queue_slot
;
128 int cmplt_queue_slot
;
132 dma_addr_t cmd_hdr_dma
;
134 dma_addr_t status_buffer_dma
;
136 dma_addr_t command_table_dma
;
137 struct hisi_sas_sge_page
*sge_page
;
138 dma_addr_t sge_page_dma
;
139 struct work_struct abort_slot
;
142 struct hisi_sas_tmf_task
{
144 u16 tag_of_task_to_be_managed
;
148 int (*hw_init
)(struct hisi_hba
*hisi_hba
);
149 void (*setup_itct
)(struct hisi_hba
*hisi_hba
,
150 struct hisi_sas_device
*device
);
151 int (*slot_index_alloc
)(struct hisi_hba
*hisi_hba
, int *slot_idx
,
152 struct domain_device
*device
);
153 struct hisi_sas_device
*(*alloc_dev
)(struct domain_device
*device
);
154 void (*sl_notify
)(struct hisi_hba
*hisi_hba
, int phy_no
);
155 int (*get_free_slot
)(struct hisi_hba
*hisi_hba
, u32 dev_id
,
157 void (*start_delivery
)(struct hisi_hba
*hisi_hba
);
158 int (*prep_ssp
)(struct hisi_hba
*hisi_hba
,
159 struct hisi_sas_slot
*slot
, int is_tmf
,
160 struct hisi_sas_tmf_task
*tmf
);
161 int (*prep_smp
)(struct hisi_hba
*hisi_hba
,
162 struct hisi_sas_slot
*slot
);
163 int (*prep_stp
)(struct hisi_hba
*hisi_hba
,
164 struct hisi_sas_slot
*slot
);
165 int (*prep_abort
)(struct hisi_hba
*hisi_hba
,
166 struct hisi_sas_slot
*slot
,
167 int device_id
, int abort_flag
, int tag_to_abort
);
168 int (*slot_complete
)(struct hisi_hba
*hisi_hba
,
169 struct hisi_sas_slot
*slot
, int abort
);
170 void (*phy_enable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
171 void (*phy_disable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
172 void (*phy_hard_reset
)(struct hisi_hba
*hisi_hba
, int phy_no
);
173 void (*phy_set_linkrate
)(struct hisi_hba
*hisi_hba
, int phy_no
,
174 struct sas_phy_linkrates
*linkrates
);
175 enum sas_linkrate (*phy_get_max_linkrate
)(void);
176 void (*free_device
)(struct hisi_hba
*hisi_hba
,
177 struct hisi_sas_device
*dev
);
178 int (*get_wideport_bitmap
)(struct hisi_hba
*hisi_hba
, int port_id
);
179 int (*soft_reset
)(struct hisi_hba
*hisi_hba
);
180 int max_command_entries
;
181 int complete_hdr_size
;
185 /* This must be the first element, used by SHOST_TO_SAS_HA */
186 struct sas_ha_struct
*p
;
188 struct platform_device
*pdev
;
192 u32 ctrl_reset_sts_reg
;
193 u32 ctrl_clock_ena_reg
;
194 u32 refclk_frequency_mhz
;
195 u8 sas_addr
[SAS_ADDR_SIZE
];
201 struct timer_list timer
;
202 struct workqueue_struct
*wq
;
204 int slot_index_count
;
205 unsigned long *slot_index_tags
;
208 struct sas_ha_struct sha
;
209 struct Scsi_Host
*shost
;
211 struct hisi_sas_cq cq
[HISI_SAS_MAX_QUEUES
];
212 struct hisi_sas_dq dq
[HISI_SAS_MAX_QUEUES
];
213 struct hisi_sas_phy phy
[HISI_SAS_MAX_PHYS
];
214 struct hisi_sas_port port
[HISI_SAS_MAX_PHYS
];
217 struct hisi_sas_slot
*slot_prep
;
219 struct dma_pool
*sge_page_pool
;
220 struct hisi_sas_device devices
[HISI_SAS_MAX_DEVICES
];
221 struct dma_pool
*command_table_pool
;
222 struct dma_pool
*status_buffer_pool
;
223 struct hisi_sas_cmd_hdr
*cmd_hdr
[HISI_SAS_MAX_QUEUES
];
224 dma_addr_t cmd_hdr_dma
[HISI_SAS_MAX_QUEUES
];
225 void *complete_hdr
[HISI_SAS_MAX_QUEUES
];
226 dma_addr_t complete_hdr_dma
[HISI_SAS_MAX_QUEUES
];
227 struct hisi_sas_initial_fis
*initial_fis
;
228 dma_addr_t initial_fis_dma
;
229 struct hisi_sas_itct
*itct
;
231 struct hisi_sas_iost
*iost
;
233 struct hisi_sas_breakpoint
*breakpoint
;
234 dma_addr_t breakpoint_dma
;
235 struct hisi_sas_breakpoint
*sata_breakpoint
;
236 dma_addr_t sata_breakpoint_dma
;
237 struct hisi_sas_slot
*slot_info
;
239 const struct hisi_sas_hw
*hw
; /* Low level hw interface */
240 struct work_struct rst_work
;
243 /* Generic HW DMA host memory structures */
244 /* Delivery queue header */
245 struct hisi_sas_cmd_hdr
{
256 __le32 transfer_tags
;
259 __le32 data_transfer_len
;
262 __le32 first_burst_num
;
271 __le64 cmd_table_addr
;
274 __le64 sts_buffer_addr
;
277 __le64 prd_table_addr
;
280 __le64 dif_prd_table_addr
;
283 struct hisi_sas_itct
{
291 struct hisi_sas_iost
{
298 struct hisi_sas_err_record
{
302 struct hisi_sas_initial_fis
{
303 struct hisi_sas_err_record err_record
;
304 struct dev_to_host_fis fis
;
308 struct hisi_sas_breakpoint
{
309 u8 data
[128]; /*io128 byte*/
312 struct hisi_sas_sge
{
320 struct hisi_sas_command_table_smp
{
324 struct hisi_sas_command_table_stp
{
325 struct host_to_dev_fis command_fis
;
327 u8 atapi_cdb
[ATAPI_CDB_LEN
];
330 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
331 struct hisi_sas_sge_page
{
332 struct hisi_sas_sge sge
[HISI_SAS_SGE_PAGE_CNT
];
335 struct hisi_sas_command_table_ssp
{
336 struct ssp_frame_hdr hdr
;
339 struct ssp_command_iu task
;
342 struct ssp_tmf_iu ssp_task
;
343 struct xfer_rdy_iu xfer_rdy
;
344 struct ssp_response_iu ssp_res
;
348 union hisi_sas_command_table
{
349 struct hisi_sas_command_table_ssp ssp
;
350 struct hisi_sas_command_table_smp smp
;
351 struct hisi_sas_command_table_stp stp
;
354 extern struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
);
355 extern int hisi_sas_probe(struct platform_device
*pdev
,
356 const struct hisi_sas_hw
*ops
);
357 extern int hisi_sas_remove(struct platform_device
*pdev
);
359 extern void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
);
360 extern void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
,
361 struct sas_task
*task
,
362 struct hisi_sas_slot
*slot
);
363 extern void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
);
364 extern void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,