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))
49 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
50 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
51 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
52 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
53 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
58 PORT_TYPE_SAS
= (1U << 1),
59 PORT_TYPE_SATA
= (1U << 0),
68 HISI_SAS_INT_ABT_CMD
= 0,
69 HISI_SAS_INT_ABT_DEV
= 1,
72 enum hisi_sas_dev_type
{
73 HISI_SAS_DEV_TYPE_STP
= 0,
74 HISI_SAS_DEV_TYPE_SSP
,
75 HISI_SAS_DEV_TYPE_SATA
,
79 struct hisi_hba
*hisi_hba
;
80 struct hisi_sas_port
*port
;
81 struct asd_sas_phy sas_phy
;
82 struct sas_identify identify
;
83 struct timer_list timer
;
84 struct work_struct phyup_ws
;
85 u64 port_id
; /* from hw */
92 enum sas_linkrate minimum_linkrate
;
93 enum sas_linkrate maximum_linkrate
;
96 struct hisi_sas_port
{
97 struct asd_sas_port sas_port
;
103 struct hisi_hba
*hisi_hba
;
104 struct tasklet_struct tasklet
;
110 struct hisi_hba
*hisi_hba
;
111 struct hisi_sas_slot
*slot_prep
;
117 struct hisi_sas_device
{
118 struct hisi_hba
*hisi_hba
;
119 struct domain_device
*sas_device
;
120 struct hisi_sas_dq
*dq
;
121 struct list_head list
;
123 atomic64_t running_req
;
124 enum sas_device_type dev_type
;
130 struct hisi_sas_slot
{
131 struct list_head entry
;
132 struct sas_task
*task
;
133 struct hisi_sas_port
*port
;
136 int dlvry_queue_slot
;
138 int cmplt_queue_slot
;
142 dma_addr_t cmd_hdr_dma
;
144 dma_addr_t status_buffer_dma
;
146 dma_addr_t command_table_dma
;
147 struct hisi_sas_sge_page
*sge_page
;
148 dma_addr_t sge_page_dma
;
149 struct work_struct abort_slot
;
150 struct timer_list internal_abort_timer
;
153 struct hisi_sas_tmf_task
{
155 u16 tag_of_task_to_be_managed
;
159 int (*hw_init
)(struct hisi_hba
*hisi_hba
);
160 void (*setup_itct
)(struct hisi_hba
*hisi_hba
,
161 struct hisi_sas_device
*device
);
162 int (*slot_index_alloc
)(struct hisi_hba
*hisi_hba
, int *slot_idx
,
163 struct domain_device
*device
);
164 struct hisi_sas_device
*(*alloc_dev
)(struct domain_device
*device
);
165 void (*sl_notify
)(struct hisi_hba
*hisi_hba
, int phy_no
);
166 int (*get_free_slot
)(struct hisi_hba
*hisi_hba
, struct hisi_sas_dq
*dq
);
167 void (*start_delivery
)(struct hisi_sas_dq
*dq
);
168 int (*prep_ssp
)(struct hisi_hba
*hisi_hba
,
169 struct hisi_sas_slot
*slot
, int is_tmf
,
170 struct hisi_sas_tmf_task
*tmf
);
171 int (*prep_smp
)(struct hisi_hba
*hisi_hba
,
172 struct hisi_sas_slot
*slot
);
173 int (*prep_stp
)(struct hisi_hba
*hisi_hba
,
174 struct hisi_sas_slot
*slot
);
175 int (*prep_abort
)(struct hisi_hba
*hisi_hba
,
176 struct hisi_sas_slot
*slot
,
177 int device_id
, int abort_flag
, int tag_to_abort
);
178 int (*slot_complete
)(struct hisi_hba
*hisi_hba
,
179 struct hisi_sas_slot
*slot
);
180 void (*phys_init
)(struct hisi_hba
*hisi_hba
);
181 void (*phy_enable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
182 void (*phy_disable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
183 void (*phy_hard_reset
)(struct hisi_hba
*hisi_hba
, int phy_no
);
184 void (*phy_set_linkrate
)(struct hisi_hba
*hisi_hba
, int phy_no
,
185 struct sas_phy_linkrates
*linkrates
);
186 enum sas_linkrate (*phy_get_max_linkrate
)(void);
187 void (*free_device
)(struct hisi_hba
*hisi_hba
,
188 struct hisi_sas_device
*dev
);
189 int (*get_wideport_bitmap
)(struct hisi_hba
*hisi_hba
, int port_id
);
190 int (*soft_reset
)(struct hisi_hba
*hisi_hba
);
191 int max_command_entries
;
192 int complete_hdr_size
;
196 /* This must be the first element, used by SHOST_TO_SAS_HA */
197 struct sas_ha_struct
*p
;
199 struct platform_device
*pdev
;
203 u32 ctrl_reset_sts_reg
;
204 u32 ctrl_clock_ena_reg
;
205 u32 refclk_frequency_mhz
;
206 u8 sas_addr
[SAS_ADDR_SIZE
];
211 struct timer_list timer
;
212 struct workqueue_struct
*wq
;
214 int slot_index_count
;
215 unsigned long *slot_index_tags
;
216 unsigned long reject_stp_links_msk
;
219 struct sas_ha_struct sha
;
220 struct Scsi_Host
*shost
;
222 struct hisi_sas_cq cq
[HISI_SAS_MAX_QUEUES
];
223 struct hisi_sas_dq dq
[HISI_SAS_MAX_QUEUES
];
224 struct hisi_sas_phy phy
[HISI_SAS_MAX_PHYS
];
225 struct hisi_sas_port port
[HISI_SAS_MAX_PHYS
];
229 struct dma_pool
*sge_page_pool
;
230 struct hisi_sas_device devices
[HISI_SAS_MAX_DEVICES
];
231 struct dma_pool
*command_table_pool
;
232 struct dma_pool
*status_buffer_pool
;
233 struct hisi_sas_cmd_hdr
*cmd_hdr
[HISI_SAS_MAX_QUEUES
];
234 dma_addr_t cmd_hdr_dma
[HISI_SAS_MAX_QUEUES
];
235 void *complete_hdr
[HISI_SAS_MAX_QUEUES
];
236 dma_addr_t complete_hdr_dma
[HISI_SAS_MAX_QUEUES
];
237 struct hisi_sas_initial_fis
*initial_fis
;
238 dma_addr_t initial_fis_dma
;
239 struct hisi_sas_itct
*itct
;
241 struct hisi_sas_iost
*iost
;
243 struct hisi_sas_breakpoint
*breakpoint
;
244 dma_addr_t breakpoint_dma
;
245 struct hisi_sas_breakpoint
*sata_breakpoint
;
246 dma_addr_t sata_breakpoint_dma
;
247 struct hisi_sas_slot
*slot_info
;
249 const struct hisi_sas_hw
*hw
; /* Low level hw interface */
250 unsigned long sata_dev_bitmap
[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES
)];
251 struct work_struct rst_work
;
254 /* Generic HW DMA host memory structures */
255 /* Delivery queue header */
256 struct hisi_sas_cmd_hdr
{
267 __le32 transfer_tags
;
270 __le32 data_transfer_len
;
273 __le32 first_burst_num
;
282 __le64 cmd_table_addr
;
285 __le64 sts_buffer_addr
;
288 __le64 prd_table_addr
;
291 __le64 dif_prd_table_addr
;
294 struct hisi_sas_itct
{
302 struct hisi_sas_iost
{
309 struct hisi_sas_err_record
{
313 struct hisi_sas_initial_fis
{
314 struct hisi_sas_err_record err_record
;
315 struct dev_to_host_fis fis
;
319 struct hisi_sas_breakpoint
{
320 u8 data
[128]; /*io128 byte*/
323 struct hisi_sas_sge
{
331 struct hisi_sas_command_table_smp
{
335 struct hisi_sas_command_table_stp
{
336 struct host_to_dev_fis command_fis
;
338 u8 atapi_cdb
[ATAPI_CDB_LEN
];
341 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
342 struct hisi_sas_sge_page
{
343 struct hisi_sas_sge sge
[HISI_SAS_SGE_PAGE_CNT
];
346 struct hisi_sas_command_table_ssp
{
347 struct ssp_frame_hdr hdr
;
350 struct ssp_command_iu task
;
353 struct ssp_tmf_iu ssp_task
;
354 struct xfer_rdy_iu xfer_rdy
;
355 struct ssp_response_iu ssp_res
;
359 union hisi_sas_command_table
{
360 struct hisi_sas_command_table_ssp ssp
;
361 struct hisi_sas_command_table_smp smp
;
362 struct hisi_sas_command_table_stp stp
;
365 extern u8
hisi_sas_get_ata_protocol(u8 cmd
, int direction
);
366 extern struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
);
367 extern void hisi_sas_sata_done(struct sas_task
*task
,
368 struct hisi_sas_slot
*slot
);
369 extern int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
);
370 extern int hisi_sas_probe(struct platform_device
*pdev
,
371 const struct hisi_sas_hw
*ops
);
372 extern int hisi_sas_remove(struct platform_device
*pdev
);
374 extern void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
);
375 extern void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
,
376 struct sas_task
*task
,
377 struct hisi_sas_slot
*slot
);
378 extern void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
);
379 extern void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,