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/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/property.h>
24 #include <linux/regmap.h>
25 #include <scsi/sas_ata.h>
26 #include <scsi/libsas.h>
28 #define DRV_VERSION "v1.6"
30 #define HISI_SAS_MAX_PHYS 9
31 #define HISI_SAS_MAX_QUEUES 32
32 #define HISI_SAS_QUEUE_SLOTS 512
33 #define HISI_SAS_MAX_ITCT_ENTRIES 2048
34 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
35 #define HISI_SAS_RESET_BIT 0
37 #define HISI_SAS_STATUS_BUF_SZ \
38 (sizeof(struct hisi_sas_err_record) + 1024)
39 #define HISI_SAS_COMMAND_TABLE_SZ \
40 (((sizeof(union hisi_sas_command_table)+3)/4)*4)
42 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
43 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
44 #define HISI_SAS_MAX_STP_RESP_SZ 28
46 #define DEV_IS_EXPANDER(type) \
47 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
48 (type == SAS_FANOUT_EXPANDER_DEVICE))
50 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
51 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
52 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
53 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
54 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
59 PORT_TYPE_SAS
= (1U << 1),
60 PORT_TYPE_SATA
= (1U << 0),
69 HISI_SAS_INT_ABT_CMD
= 0,
70 HISI_SAS_INT_ABT_DEV
= 1,
73 enum hisi_sas_dev_type
{
74 HISI_SAS_DEV_TYPE_STP
= 0,
75 HISI_SAS_DEV_TYPE_SSP
,
76 HISI_SAS_DEV_TYPE_SATA
,
80 struct hisi_hba
*hisi_hba
;
81 struct hisi_sas_port
*port
;
82 struct asd_sas_phy sas_phy
;
83 struct sas_identify identify
;
84 struct timer_list timer
;
85 struct work_struct phyup_ws
;
86 u64 port_id
; /* from hw */
93 enum sas_linkrate minimum_linkrate
;
94 enum sas_linkrate maximum_linkrate
;
97 struct hisi_sas_port
{
98 struct asd_sas_port sas_port
;
104 struct hisi_hba
*hisi_hba
;
105 struct tasklet_struct tasklet
;
111 struct hisi_hba
*hisi_hba
;
112 struct hisi_sas_slot
*slot_prep
;
118 struct hisi_sas_device
{
119 struct hisi_hba
*hisi_hba
;
120 struct domain_device
*sas_device
;
121 struct hisi_sas_dq
*dq
;
122 struct list_head list
;
124 atomic64_t running_req
;
125 enum sas_device_type dev_type
;
131 struct hisi_sas_slot
{
132 struct list_head entry
;
133 struct sas_task
*task
;
134 struct hisi_sas_port
*port
;
137 int dlvry_queue_slot
;
139 int cmplt_queue_slot
;
143 dma_addr_t cmd_hdr_dma
;
145 dma_addr_t status_buffer_dma
;
147 dma_addr_t command_table_dma
;
148 struct hisi_sas_sge_page
*sge_page
;
149 dma_addr_t sge_page_dma
;
150 struct work_struct abort_slot
;
151 struct timer_list internal_abort_timer
;
154 struct hisi_sas_tmf_task
{
156 u16 tag_of_task_to_be_managed
;
160 int (*hw_init
)(struct hisi_hba
*hisi_hba
);
161 void (*setup_itct
)(struct hisi_hba
*hisi_hba
,
162 struct hisi_sas_device
*device
);
163 int (*slot_index_alloc
)(struct hisi_hba
*hisi_hba
, int *slot_idx
,
164 struct domain_device
*device
);
165 struct hisi_sas_device
*(*alloc_dev
)(struct domain_device
*device
);
166 void (*sl_notify
)(struct hisi_hba
*hisi_hba
, int phy_no
);
167 int (*get_free_slot
)(struct hisi_hba
*hisi_hba
, struct hisi_sas_dq
*dq
);
168 void (*start_delivery
)(struct hisi_sas_dq
*dq
);
169 int (*prep_ssp
)(struct hisi_hba
*hisi_hba
,
170 struct hisi_sas_slot
*slot
, int is_tmf
,
171 struct hisi_sas_tmf_task
*tmf
);
172 int (*prep_smp
)(struct hisi_hba
*hisi_hba
,
173 struct hisi_sas_slot
*slot
);
174 int (*prep_stp
)(struct hisi_hba
*hisi_hba
,
175 struct hisi_sas_slot
*slot
);
176 int (*prep_abort
)(struct hisi_hba
*hisi_hba
,
177 struct hisi_sas_slot
*slot
,
178 int device_id
, int abort_flag
, int tag_to_abort
);
179 int (*slot_complete
)(struct hisi_hba
*hisi_hba
,
180 struct hisi_sas_slot
*slot
);
181 void (*phys_init
)(struct hisi_hba
*hisi_hba
);
182 void (*phy_enable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
183 void (*phy_disable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
184 void (*phy_hard_reset
)(struct hisi_hba
*hisi_hba
, int phy_no
);
185 void (*phy_set_linkrate
)(struct hisi_hba
*hisi_hba
, int phy_no
,
186 struct sas_phy_linkrates
*linkrates
);
187 enum sas_linkrate (*phy_get_max_linkrate
)(void);
188 void (*free_device
)(struct hisi_hba
*hisi_hba
,
189 struct hisi_sas_device
*dev
);
190 int (*get_wideport_bitmap
)(struct hisi_hba
*hisi_hba
, int port_id
);
191 int (*soft_reset
)(struct hisi_hba
*hisi_hba
);
192 int max_command_entries
;
193 int complete_hdr_size
;
197 /* This must be the first element, used by SHOST_TO_SAS_HA */
198 struct sas_ha_struct
*p
;
200 struct platform_device
*platform_dev
;
201 struct pci_dev
*pci_dev
;
207 u32 ctrl_reset_sts_reg
;
208 u32 ctrl_clock_ena_reg
;
209 u32 refclk_frequency_mhz
;
210 u8 sas_addr
[SAS_ADDR_SIZE
];
215 struct timer_list timer
;
216 struct workqueue_struct
*wq
;
218 int slot_index_count
;
219 unsigned long *slot_index_tags
;
220 unsigned long reject_stp_links_msk
;
223 struct sas_ha_struct sha
;
224 struct Scsi_Host
*shost
;
226 struct hisi_sas_cq cq
[HISI_SAS_MAX_QUEUES
];
227 struct hisi_sas_dq dq
[HISI_SAS_MAX_QUEUES
];
228 struct hisi_sas_phy phy
[HISI_SAS_MAX_PHYS
];
229 struct hisi_sas_port port
[HISI_SAS_MAX_PHYS
];
233 struct dma_pool
*sge_page_pool
;
234 struct hisi_sas_device devices
[HISI_SAS_MAX_DEVICES
];
235 struct dma_pool
*command_table_pool
;
236 struct dma_pool
*status_buffer_pool
;
237 struct hisi_sas_cmd_hdr
*cmd_hdr
[HISI_SAS_MAX_QUEUES
];
238 dma_addr_t cmd_hdr_dma
[HISI_SAS_MAX_QUEUES
];
239 void *complete_hdr
[HISI_SAS_MAX_QUEUES
];
240 dma_addr_t complete_hdr_dma
[HISI_SAS_MAX_QUEUES
];
241 struct hisi_sas_initial_fis
*initial_fis
;
242 dma_addr_t initial_fis_dma
;
243 struct hisi_sas_itct
*itct
;
245 struct hisi_sas_iost
*iost
;
247 struct hisi_sas_breakpoint
*breakpoint
;
248 dma_addr_t breakpoint_dma
;
249 struct hisi_sas_breakpoint
*sata_breakpoint
;
250 dma_addr_t sata_breakpoint_dma
;
251 struct hisi_sas_slot
*slot_info
;
253 const struct hisi_sas_hw
*hw
; /* Low level hw interface */
254 unsigned long sata_dev_bitmap
[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES
)];
255 struct work_struct rst_work
;
258 /* Generic HW DMA host memory structures */
259 /* Delivery queue header */
260 struct hisi_sas_cmd_hdr
{
271 __le32 transfer_tags
;
274 __le32 data_transfer_len
;
277 __le32 first_burst_num
;
286 __le64 cmd_table_addr
;
289 __le64 sts_buffer_addr
;
292 __le64 prd_table_addr
;
295 __le64 dif_prd_table_addr
;
298 struct hisi_sas_itct
{
306 struct hisi_sas_iost
{
313 struct hisi_sas_err_record
{
317 struct hisi_sas_initial_fis
{
318 struct hisi_sas_err_record err_record
;
319 struct dev_to_host_fis fis
;
323 struct hisi_sas_breakpoint
{
324 u8 data
[128]; /*io128 byte*/
327 struct hisi_sas_sge
{
335 struct hisi_sas_command_table_smp
{
339 struct hisi_sas_command_table_stp
{
340 struct host_to_dev_fis command_fis
;
342 u8 atapi_cdb
[ATAPI_CDB_LEN
];
345 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
346 struct hisi_sas_sge_page
{
347 struct hisi_sas_sge sge
[HISI_SAS_SGE_PAGE_CNT
];
350 struct hisi_sas_command_table_ssp
{
351 struct ssp_frame_hdr hdr
;
354 struct ssp_command_iu task
;
357 struct ssp_tmf_iu ssp_task
;
358 struct xfer_rdy_iu xfer_rdy
;
359 struct ssp_response_iu ssp_res
;
363 union hisi_sas_command_table
{
364 struct hisi_sas_command_table_ssp ssp
;
365 struct hisi_sas_command_table_smp smp
;
366 struct hisi_sas_command_table_stp stp
;
369 extern u8
hisi_sas_get_ata_protocol(u8 cmd
, int direction
);
370 extern struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
);
371 extern void hisi_sas_sata_done(struct sas_task
*task
,
372 struct hisi_sas_slot
*slot
);
373 extern int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
);
374 extern int hisi_sas_probe(struct platform_device
*pdev
,
375 const struct hisi_sas_hw
*ops
);
376 extern int hisi_sas_remove(struct platform_device
*pdev
);
378 extern void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
);
379 extern void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
,
380 struct sas_task
*task
,
381 struct hisi_sas_slot
*slot
);
382 extern void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
);
383 extern void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,