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/iopoll.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/regmap.h>
26 #include <scsi/sas_ata.h>
27 #include <scsi/libsas.h>
29 #define DRV_VERSION "v1.6"
31 #define HISI_SAS_MAX_PHYS 9
32 #define HISI_SAS_MAX_QUEUES 32
33 #define HISI_SAS_QUEUE_SLOTS 512
34 #define HISI_SAS_MAX_ITCT_ENTRIES 2048
35 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
36 #define HISI_SAS_RESET_BIT 0
37 #define HISI_SAS_REJECT_CMD_BIT 1
39 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
40 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
42 #define hisi_sas_status_buf_addr(buf) \
43 (buf + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
44 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr(slot->buf)
45 #define hisi_sas_status_buf_addr_dma(slot) \
46 hisi_sas_status_buf_addr(slot->buf_dma)
48 #define hisi_sas_cmd_hdr_addr(buf) \
49 (buf + offsetof(struct hisi_sas_slot_buf_table, command_header))
50 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr(slot->buf)
51 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr(slot->buf_dma)
53 #define hisi_sas_sge_addr(buf) \
54 (buf + offsetof(struct hisi_sas_slot_buf_table, sge_page))
55 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr(slot->buf)
56 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr(slot->buf_dma)
58 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
59 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
60 #define HISI_SAS_MAX_STP_RESP_SZ 28
62 #define DEV_IS_EXPANDER(type) \
63 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
64 (type == SAS_FANOUT_EXPANDER_DEVICE))
66 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
67 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
68 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
69 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
70 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
75 PORT_TYPE_SAS
= (1U << 1),
76 PORT_TYPE_SATA
= (1U << 0),
85 HISI_SAS_INT_ABT_CMD
= 0,
86 HISI_SAS_INT_ABT_DEV
= 1,
89 enum hisi_sas_dev_type
{
90 HISI_SAS_DEV_TYPE_STP
= 0,
91 HISI_SAS_DEV_TYPE_SSP
,
92 HISI_SAS_DEV_TYPE_SATA
,
95 struct hisi_sas_hw_error
{
103 struct hisi_sas_phy
{
104 struct hisi_hba
*hisi_hba
;
105 struct hisi_sas_port
*port
;
106 struct asd_sas_phy sas_phy
;
107 struct sas_identify identify
;
108 struct timer_list timer
;
109 struct work_struct phyup_ws
;
110 u64 port_id
; /* from hw */
117 enum sas_linkrate minimum_linkrate
;
118 enum sas_linkrate maximum_linkrate
;
121 struct hisi_sas_port
{
122 struct asd_sas_port sas_port
;
128 struct hisi_hba
*hisi_hba
;
129 struct tasklet_struct tasklet
;
135 struct hisi_hba
*hisi_hba
;
136 struct hisi_sas_slot
*slot_prep
;
142 struct hisi_sas_device
{
143 struct hisi_hba
*hisi_hba
;
144 struct domain_device
*sas_device
;
145 struct completion
*completion
;
146 struct hisi_sas_dq
*dq
;
147 struct list_head list
;
149 atomic64_t running_req
;
150 enum sas_device_type dev_type
;
156 struct hisi_sas_slot
{
157 struct list_head entry
;
158 struct sas_task
*task
;
159 struct hisi_sas_port
*port
;
162 int dlvry_queue_slot
;
164 int cmplt_queue_slot
;
170 dma_addr_t cmd_hdr_dma
;
171 struct work_struct abort_slot
;
172 struct timer_list internal_abort_timer
;
175 struct hisi_sas_tmf_task
{
177 u16 tag_of_task_to_be_managed
;
181 int (*hw_init
)(struct hisi_hba
*hisi_hba
);
182 void (*setup_itct
)(struct hisi_hba
*hisi_hba
,
183 struct hisi_sas_device
*device
);
184 int (*slot_index_alloc
)(struct hisi_hba
*hisi_hba
, int *slot_idx
,
185 struct domain_device
*device
);
186 struct hisi_sas_device
*(*alloc_dev
)(struct domain_device
*device
);
187 void (*sl_notify
)(struct hisi_hba
*hisi_hba
, int phy_no
);
188 int (*get_free_slot
)(struct hisi_hba
*hisi_hba
, struct hisi_sas_dq
*dq
);
189 void (*start_delivery
)(struct hisi_sas_dq
*dq
);
190 int (*prep_ssp
)(struct hisi_hba
*hisi_hba
,
191 struct hisi_sas_slot
*slot
, int is_tmf
,
192 struct hisi_sas_tmf_task
*tmf
);
193 int (*prep_smp
)(struct hisi_hba
*hisi_hba
,
194 struct hisi_sas_slot
*slot
);
195 int (*prep_stp
)(struct hisi_hba
*hisi_hba
,
196 struct hisi_sas_slot
*slot
);
197 int (*prep_abort
)(struct hisi_hba
*hisi_hba
,
198 struct hisi_sas_slot
*slot
,
199 int device_id
, int abort_flag
, int tag_to_abort
);
200 int (*slot_complete
)(struct hisi_hba
*hisi_hba
,
201 struct hisi_sas_slot
*slot
);
202 void (*phys_init
)(struct hisi_hba
*hisi_hba
);
203 void (*phy_enable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
204 void (*phy_disable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
205 void (*phy_hard_reset
)(struct hisi_hba
*hisi_hba
, int phy_no
);
206 void (*get_events
)(struct hisi_hba
*hisi_hba
, int phy_no
);
207 void (*phy_set_linkrate
)(struct hisi_hba
*hisi_hba
, int phy_no
,
208 struct sas_phy_linkrates
*linkrates
);
209 enum sas_linkrate (*phy_get_max_linkrate
)(void);
210 void (*free_device
)(struct hisi_hba
*hisi_hba
,
211 struct hisi_sas_device
*dev
);
212 int (*get_wideport_bitmap
)(struct hisi_hba
*hisi_hba
, int port_id
);
213 void (*dereg_device
)(struct hisi_hba
*hisi_hba
,
214 struct domain_device
*device
);
215 int (*soft_reset
)(struct hisi_hba
*hisi_hba
);
216 u32 (*get_phys_state
)(struct hisi_hba
*hisi_hba
);
217 int max_command_entries
;
218 int complete_hdr_size
;
222 /* This must be the first element, used by SHOST_TO_SAS_HA */
223 struct sas_ha_struct
*p
;
225 struct platform_device
*platform_dev
;
226 struct pci_dev
*pci_dev
;
232 u32 ctrl_reset_sts_reg
;
233 u32 ctrl_clock_ena_reg
;
234 u32 refclk_frequency_mhz
;
235 u8 sas_addr
[SAS_ADDR_SIZE
];
240 struct timer_list timer
;
241 struct workqueue_struct
*wq
;
243 int slot_index_count
;
244 unsigned long *slot_index_tags
;
245 unsigned long reject_stp_links_msk
;
248 struct sas_ha_struct sha
;
249 struct Scsi_Host
*shost
;
251 struct hisi_sas_cq cq
[HISI_SAS_MAX_QUEUES
];
252 struct hisi_sas_dq dq
[HISI_SAS_MAX_QUEUES
];
253 struct hisi_sas_phy phy
[HISI_SAS_MAX_PHYS
];
254 struct hisi_sas_port port
[HISI_SAS_MAX_PHYS
];
258 struct dma_pool
*buffer_pool
;
259 struct hisi_sas_device devices
[HISI_SAS_MAX_DEVICES
];
260 struct hisi_sas_cmd_hdr
*cmd_hdr
[HISI_SAS_MAX_QUEUES
];
261 dma_addr_t cmd_hdr_dma
[HISI_SAS_MAX_QUEUES
];
262 void *complete_hdr
[HISI_SAS_MAX_QUEUES
];
263 dma_addr_t complete_hdr_dma
[HISI_SAS_MAX_QUEUES
];
264 struct hisi_sas_initial_fis
*initial_fis
;
265 dma_addr_t initial_fis_dma
;
266 struct hisi_sas_itct
*itct
;
268 struct hisi_sas_iost
*iost
;
270 struct hisi_sas_breakpoint
*breakpoint
;
271 dma_addr_t breakpoint_dma
;
272 struct hisi_sas_breakpoint
*sata_breakpoint
;
273 dma_addr_t sata_breakpoint_dma
;
274 struct hisi_sas_slot
*slot_info
;
276 const struct hisi_sas_hw
*hw
; /* Low level hw interface */
277 unsigned long sata_dev_bitmap
[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES
)];
278 struct work_struct rst_work
;
281 /* Generic HW DMA host memory structures */
282 /* Delivery queue header */
283 struct hisi_sas_cmd_hdr
{
294 __le32 transfer_tags
;
297 __le32 data_transfer_len
;
300 __le32 first_burst_num
;
309 __le64 cmd_table_addr
;
312 __le64 sts_buffer_addr
;
315 __le64 prd_table_addr
;
318 __le64 dif_prd_table_addr
;
321 struct hisi_sas_itct
{
329 struct hisi_sas_iost
{
336 struct hisi_sas_err_record
{
340 struct hisi_sas_initial_fis
{
341 struct hisi_sas_err_record err_record
;
342 struct dev_to_host_fis fis
;
346 struct hisi_sas_breakpoint
{
347 u8 data
[128]; /*io128 byte*/
350 struct hisi_sas_sge
{
358 struct hisi_sas_command_table_smp
{
362 struct hisi_sas_command_table_stp
{
363 struct host_to_dev_fis command_fis
;
365 u8 atapi_cdb
[ATAPI_CDB_LEN
];
368 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
369 struct hisi_sas_sge_page
{
370 struct hisi_sas_sge sge
[HISI_SAS_SGE_PAGE_CNT
];
373 struct hisi_sas_command_table_ssp
{
374 struct ssp_frame_hdr hdr
;
377 struct ssp_command_iu task
;
380 struct ssp_tmf_iu ssp_task
;
381 struct xfer_rdy_iu xfer_rdy
;
382 struct ssp_response_iu ssp_res
;
386 union hisi_sas_command_table
{
387 struct hisi_sas_command_table_ssp ssp
;
388 struct hisi_sas_command_table_smp smp
;
389 struct hisi_sas_command_table_stp stp
;
392 struct hisi_sas_status_buffer
{
393 struct hisi_sas_err_record err
;
397 struct hisi_sas_slot_buf_table
{
398 struct hisi_sas_status_buffer status_buffer
;
399 union hisi_sas_command_table command_header
;
400 struct hisi_sas_sge_page sge_page
;
403 extern struct scsi_transport_template
*hisi_sas_stt
;
404 extern struct scsi_host_template
*hisi_sas_sht
;
406 extern void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
);
407 extern void hisi_sas_init_add(struct hisi_hba
*hisi_hba
);
408 extern int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
);
409 extern void hisi_sas_free(struct hisi_hba
*hisi_hba
);
410 extern u8
hisi_sas_get_ata_protocol(u8 cmd
, int direction
);
411 extern struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
);
412 extern void hisi_sas_sata_done(struct sas_task
*task
,
413 struct hisi_sas_slot
*slot
);
414 extern int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
);
415 extern int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
);
416 extern int hisi_sas_probe(struct platform_device
*pdev
,
417 const struct hisi_sas_hw
*ops
);
418 extern int hisi_sas_remove(struct platform_device
*pdev
);
420 extern void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
);
421 extern void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
,
422 struct sas_task
*task
,
423 struct hisi_sas_slot
*slot
);
424 extern void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
);