1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2015 Linaro Ltd.
4 * Copyright (c) 2015 Hisilicon Limited.
10 #include <linux/acpi.h>
11 #include <linux/clk.h>
12 #include <linux/debugfs.h>
13 #include <linux/dmapool.h>
14 #include <linux/iopoll.h>
15 #include <linux/lcm.h>
16 #include <linux/libata.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/pci.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 HISI_SAS_MAX_PHYS 9
28 #define HISI_SAS_MAX_QUEUES 32
29 #define HISI_SAS_QUEUE_SLOTS 4096
30 #define HISI_SAS_MAX_ITCT_ENTRIES 1024
31 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
32 #define HISI_SAS_RESET_BIT 0
33 #define HISI_SAS_REJECT_CMD_BIT 1
34 #define HISI_SAS_RESERVED_IPTT_CNT 96
36 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
37 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
39 #define hisi_sas_status_buf_addr(buf) \
40 ((buf) + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
41 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr((slot)->buf)
42 #define hisi_sas_status_buf_addr_dma(slot) \
43 hisi_sas_status_buf_addr((slot)->buf_dma)
45 #define hisi_sas_cmd_hdr_addr(buf) \
46 ((buf) + offsetof(struct hisi_sas_slot_buf_table, command_header))
47 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr((slot)->buf)
48 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr((slot)->buf_dma)
50 #define hisi_sas_sge_addr(buf) \
51 ((buf) + offsetof(struct hisi_sas_slot_buf_table, sge_page))
52 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr((slot)->buf)
53 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr((slot)->buf_dma)
55 #define hisi_sas_sge_dif_addr(buf) \
56 ((buf) + offsetof(struct hisi_sas_slot_dif_buf_table, sge_dif_page))
57 #define hisi_sas_sge_dif_addr_mem(slot) hisi_sas_sge_dif_addr((slot)->buf)
58 #define hisi_sas_sge_dif_addr_dma(slot) hisi_sas_sge_dif_addr((slot)->buf_dma)
60 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
61 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
62 #define HISI_SAS_MAX_STP_RESP_SZ 28
64 #define DEV_IS_EXPANDER(type) \
65 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
66 (type == SAS_FANOUT_EXPANDER_DEVICE))
68 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
69 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
70 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
71 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
72 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
74 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
75 SHOST_DIF_TYPE2_PROTECTION | \
76 SHOST_DIF_TYPE3_PROTECTION)
78 #define HISI_SAS_DIX_PROT_MASK (SHOST_DIX_TYPE1_PROTECTION | \
79 SHOST_DIX_TYPE2_PROTECTION | \
80 SHOST_DIX_TYPE3_PROTECTION)
82 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK | HISI_SAS_DIX_PROT_MASK)
84 #define HISI_SAS_WAIT_PHYUP_TIMEOUT 20
89 PORT_TYPE_SAS
= (1U << 1),
90 PORT_TYPE_SATA
= (1U << 0),
99 HISI_SAS_INT_ABT_CMD
= 0,
100 HISI_SAS_INT_ABT_DEV
= 1,
103 enum hisi_sas_dev_type
{
104 HISI_SAS_DEV_TYPE_STP
= 0,
105 HISI_SAS_DEV_TYPE_SSP
,
106 HISI_SAS_DEV_TYPE_SATA
,
109 struct hisi_sas_hw_error
{
115 const struct hisi_sas_hw_error
*sub
;
118 struct hisi_sas_rst
{
119 struct hisi_hba
*hisi_hba
;
120 struct completion
*completion
;
121 struct work_struct work
;
125 #define HISI_SAS_RST_WORK_INIT(r, c) \
126 { .hisi_hba = hisi_hba, \
128 .work = __WORK_INITIALIZER(r.work, \
129 hisi_sas_sync_rst_work_handler), \
133 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
134 DECLARE_COMPLETION_ONSTACK(c); \
135 DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \
136 struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)
138 enum hisi_sas_bit_err_type
{
139 HISI_SAS_ERR_SINGLE_BIT_ECC
= 0x0,
140 HISI_SAS_ERR_MULTI_BIT_ECC
= 0x1,
143 enum hisi_sas_phy_event
{
144 HISI_PHYE_PHY_UP
= 0U,
145 HISI_PHYE_LINK_RESET
,
149 struct hisi_sas_phy
{
150 struct work_struct works
[HISI_PHYES_NUM
];
151 struct hisi_hba
*hisi_hba
;
152 struct hisi_sas_port
*port
;
153 struct asd_sas_phy sas_phy
;
154 struct sas_identify identify
;
155 struct completion
*reset_completion
;
156 struct timer_list timer
;
158 u64 port_id
; /* from hw */
165 u32 code_violation_err_count
;
166 enum sas_linkrate minimum_linkrate
;
167 enum sas_linkrate maximum_linkrate
;
171 struct hisi_sas_port
{
172 struct asd_sas_port sas_port
;
178 struct hisi_hba
*hisi_hba
;
179 const struct cpumask
*pci_irq_mask
;
180 struct tasklet_struct tasklet
;
186 struct hisi_hba
*hisi_hba
;
187 struct list_head list
;
193 struct hisi_sas_device
{
194 struct hisi_hba
*hisi_hba
;
195 struct domain_device
*sas_device
;
196 struct completion
*completion
;
197 struct hisi_sas_dq
*dq
;
198 struct list_head list
;
199 enum sas_device_type dev_type
;
200 enum dev_status dev_status
;
203 spinlock_t lock
; /* For protecting slots */
206 struct hisi_sas_tmf_task
{
210 u16 tag_of_task_to_be_managed
;
213 struct hisi_sas_slot
{
214 struct list_head entry
;
215 struct list_head delivery
;
216 struct sas_task
*task
;
217 struct hisi_sas_port
*port
;
221 int dlvry_queue_slot
;
223 int cmplt_queue_slot
;
228 dma_addr_t cmd_hdr_dma
;
229 struct timer_list internal_abort_timer
;
231 struct hisi_sas_tmf_task
*tmf
;
232 /* Do not reorder/change members after here */
238 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
240 struct hisi_sas_debugfs_reg_lu
{
245 struct hisi_sas_debugfs_reg
{
246 const struct hisi_sas_debugfs_reg_lu
*lu
;
250 u32 (*read_global_reg
)(struct hisi_hba
*hisi_hba
, u32 off
);
251 u32 (*read_port_reg
)(struct hisi_hba
*hisi_hba
, int port
,
257 int (*hw_init
)(struct hisi_hba
*hisi_hba
);
258 void (*setup_itct
)(struct hisi_hba
*hisi_hba
,
259 struct hisi_sas_device
*device
);
260 int (*slot_index_alloc
)(struct hisi_hba
*hisi_hba
,
261 struct domain_device
*device
);
262 struct hisi_sas_device
*(*alloc_dev
)(struct domain_device
*device
);
263 void (*sl_notify_ssp
)(struct hisi_hba
*hisi_hba
, int phy_no
);
264 int (*get_free_slot
)(struct hisi_hba
*hisi_hba
, struct hisi_sas_dq
*dq
);
265 void (*start_delivery
)(struct hisi_sas_dq
*dq
);
266 void (*prep_ssp
)(struct hisi_hba
*hisi_hba
,
267 struct hisi_sas_slot
*slot
);
268 void (*prep_smp
)(struct hisi_hba
*hisi_hba
,
269 struct hisi_sas_slot
*slot
);
270 void (*prep_stp
)(struct hisi_hba
*hisi_hba
,
271 struct hisi_sas_slot
*slot
);
272 void (*prep_abort
)(struct hisi_hba
*hisi_hba
,
273 struct hisi_sas_slot
*slot
,
274 int device_id
, int abort_flag
, int tag_to_abort
);
275 int (*slot_complete
)(struct hisi_hba
*hisi_hba
,
276 struct hisi_sas_slot
*slot
);
277 void (*phys_init
)(struct hisi_hba
*hisi_hba
);
278 void (*phy_start
)(struct hisi_hba
*hisi_hba
, int phy_no
);
279 void (*phy_disable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
280 void (*phy_hard_reset
)(struct hisi_hba
*hisi_hba
, int phy_no
);
281 void (*get_events
)(struct hisi_hba
*hisi_hba
, int phy_no
);
282 void (*phy_set_linkrate
)(struct hisi_hba
*hisi_hba
, int phy_no
,
283 struct sas_phy_linkrates
*linkrates
);
284 enum sas_linkrate (*phy_get_max_linkrate
)(void);
285 void (*clear_itct
)(struct hisi_hba
*hisi_hba
,
286 struct hisi_sas_device
*dev
);
287 void (*free_device
)(struct hisi_sas_device
*sas_dev
);
288 int (*get_wideport_bitmap
)(struct hisi_hba
*hisi_hba
, int port_id
);
289 void (*dereg_device
)(struct hisi_hba
*hisi_hba
,
290 struct domain_device
*device
);
291 int (*soft_reset
)(struct hisi_hba
*hisi_hba
);
292 u32 (*get_phys_state
)(struct hisi_hba
*hisi_hba
);
293 int (*write_gpio
)(struct hisi_hba
*hisi_hba
, u8 reg_type
,
294 u8 reg_index
, u8 reg_count
, u8
*write_data
);
295 int (*wait_cmds_complete_timeout
)(struct hisi_hba
*hisi_hba
,
296 int delay_ms
, int timeout_ms
);
297 void (*snapshot_prepare
)(struct hisi_hba
*hisi_hba
);
298 void (*snapshot_restore
)(struct hisi_hba
*hisi_hba
);
299 int max_command_entries
;
300 int complete_hdr_size
;
301 struct scsi_host_template
*sht
;
303 const struct hisi_sas_debugfs_reg
*debugfs_reg_global
;
304 const struct hisi_sas_debugfs_reg
*debugfs_reg_port
;
308 /* This must be the first element, used by SHOST_TO_SAS_HA */
309 struct sas_ha_struct
*p
;
311 struct platform_device
*platform_dev
;
312 struct pci_dev
*pci_dev
;
318 void __iomem
*sgpio_regs
;
321 u32 ctrl_reset_sts_reg
;
322 u32 ctrl_clock_ena_reg
;
323 u32 refclk_frequency_mhz
;
324 u8 sas_addr
[SAS_ADDR_SIZE
];
328 struct semaphore sem
;
330 struct timer_list timer
;
331 struct workqueue_struct
*wq
;
333 int slot_index_count
;
336 unsigned long *slot_index_tags
;
337 unsigned long reject_stp_links_msk
;
340 struct sas_ha_struct sha
;
341 struct Scsi_Host
*shost
;
343 struct hisi_sas_cq cq
[HISI_SAS_MAX_QUEUES
];
344 struct hisi_sas_dq dq
[HISI_SAS_MAX_QUEUES
];
345 struct hisi_sas_phy phy
[HISI_SAS_MAX_PHYS
];
346 struct hisi_sas_port port
[HISI_SAS_MAX_PHYS
];
350 struct hisi_sas_device devices
[HISI_SAS_MAX_DEVICES
];
351 struct hisi_sas_cmd_hdr
*cmd_hdr
[HISI_SAS_MAX_QUEUES
];
352 dma_addr_t cmd_hdr_dma
[HISI_SAS_MAX_QUEUES
];
353 void *complete_hdr
[HISI_SAS_MAX_QUEUES
];
354 dma_addr_t complete_hdr_dma
[HISI_SAS_MAX_QUEUES
];
355 struct hisi_sas_initial_fis
*initial_fis
;
356 dma_addr_t initial_fis_dma
;
357 struct hisi_sas_itct
*itct
;
359 struct hisi_sas_iost
*iost
;
361 struct hisi_sas_breakpoint
*breakpoint
;
362 dma_addr_t breakpoint_dma
;
363 struct hisi_sas_breakpoint
*sata_breakpoint
;
364 dma_addr_t sata_breakpoint_dma
;
365 struct hisi_sas_slot
*slot_info
;
367 const struct hisi_sas_hw
*hw
; /* Low level hw interface */
368 unsigned long sata_dev_bitmap
[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES
)];
369 struct work_struct rst_work
;
370 struct work_struct debugfs_work
;
372 u32 intr_coal_ticks
; /* Time of interrupt coalesce in us */
373 u32 intr_coal_count
; /* Interrupt count to coalesce */
376 unsigned int *reply_map
;
378 /* debugfs memories */
379 u32
*debugfs_global_reg
;
380 u32
*debugfs_port_reg
[HISI_SAS_MAX_PHYS
];
381 void *debugfs_complete_hdr
[HISI_SAS_MAX_QUEUES
];
382 struct hisi_sas_cmd_hdr
*debugfs_cmd_hdr
[HISI_SAS_MAX_QUEUES
];
383 struct hisi_sas_iost
*debugfs_iost
;
384 struct hisi_sas_itct
*debugfs_itct
;
386 struct dentry
*debugfs_dir
;
387 struct dentry
*debugfs_dump_dentry
;
388 bool debugfs_snapshot
;
391 /* Generic HW DMA host memory structures */
392 /* Delivery queue header */
393 struct hisi_sas_cmd_hdr
{
404 __le32 transfer_tags
;
407 __le32 data_transfer_len
;
410 __le32 first_burst_num
;
419 __le64 cmd_table_addr
;
422 __le64 sts_buffer_addr
;
425 __le64 prd_table_addr
;
428 __le64 dif_prd_table_addr
;
431 struct hisi_sas_itct
{
439 struct hisi_sas_iost
{
446 struct hisi_sas_err_record
{
450 struct hisi_sas_initial_fis
{
451 struct hisi_sas_err_record err_record
;
452 struct dev_to_host_fis fis
;
456 struct hisi_sas_breakpoint
{
460 struct hisi_sas_sata_breakpoint
{
461 struct hisi_sas_breakpoint tag
[32];
464 struct hisi_sas_sge
{
472 struct hisi_sas_command_table_smp
{
476 struct hisi_sas_command_table_stp
{
477 struct host_to_dev_fis command_fis
;
479 u8 atapi_cdb
[ATAPI_CDB_LEN
];
482 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
483 struct hisi_sas_sge_page
{
484 struct hisi_sas_sge sge
[HISI_SAS_SGE_PAGE_CNT
];
487 #define HISI_SAS_SGE_DIF_PAGE_CNT SG_CHUNK_SIZE
488 struct hisi_sas_sge_dif_page
{
489 struct hisi_sas_sge sge
[HISI_SAS_SGE_DIF_PAGE_CNT
];
492 struct hisi_sas_command_table_ssp
{
493 struct ssp_frame_hdr hdr
;
496 struct ssp_command_iu task
;
499 struct ssp_tmf_iu ssp_task
;
500 struct xfer_rdy_iu xfer_rdy
;
501 struct ssp_response_iu ssp_res
;
505 union hisi_sas_command_table
{
506 struct hisi_sas_command_table_ssp ssp
;
507 struct hisi_sas_command_table_smp smp
;
508 struct hisi_sas_command_table_stp stp
;
511 struct hisi_sas_status_buffer
{
512 struct hisi_sas_err_record err
;
516 struct hisi_sas_slot_buf_table
{
517 struct hisi_sas_status_buffer status_buffer
;
518 union hisi_sas_command_table command_header
;
519 struct hisi_sas_sge_page sge_page
;
522 struct hisi_sas_slot_dif_buf_table
{
523 struct hisi_sas_slot_buf_table slot_buf
;
524 struct hisi_sas_sge_dif_page sge_dif_page
;
527 extern struct scsi_transport_template
*hisi_sas_stt
;
529 extern bool hisi_sas_debugfs_enable
;
530 extern struct dentry
*hisi_sas_debugfs_dir
;
532 extern void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
);
533 extern int hisi_sas_alloc(struct hisi_hba
*hisi_hba
);
534 extern void hisi_sas_free(struct hisi_hba
*hisi_hba
);
535 extern u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
,
537 extern struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
);
538 extern void hisi_sas_sata_done(struct sas_task
*task
,
539 struct hisi_sas_slot
*slot
);
540 extern int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
);
541 extern int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
);
542 extern int hisi_sas_probe(struct platform_device
*pdev
,
543 const struct hisi_sas_hw
*ops
);
544 extern int hisi_sas_remove(struct platform_device
*pdev
);
546 extern int hisi_sas_slave_configure(struct scsi_device
*sdev
);
547 extern int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
);
548 extern void hisi_sas_scan_start(struct Scsi_Host
*shost
);
549 extern int hisi_sas_host_reset(struct Scsi_Host
*shost
, int reset_type
);
550 extern void hisi_sas_phy_enable(struct hisi_hba
*hisi_hba
, int phy_no
,
552 extern void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
);
553 extern void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
,
554 struct sas_task
*task
,
555 struct hisi_sas_slot
*slot
);
556 extern void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
);
557 extern void hisi_sas_rst_work_handler(struct work_struct
*work
);
558 extern void hisi_sas_sync_rst_work_handler(struct work_struct
*work
);
559 extern void hisi_sas_kill_tasklets(struct hisi_hba
*hisi_hba
);
560 extern void hisi_sas_phy_oob_ready(struct hisi_hba
*hisi_hba
, int phy_no
);
561 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
562 enum hisi_sas_phy_event event
);
563 extern void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
);
564 extern u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
);
565 extern void hisi_sas_controller_reset_prepare(struct hisi_hba
*hisi_hba
);
566 extern void hisi_sas_controller_reset_done(struct hisi_hba
*hisi_hba
);
567 extern void hisi_sas_debugfs_init(struct hisi_hba
*hisi_hba
);
568 extern void hisi_sas_debugfs_exit(struct hisi_hba
*hisi_hba
);
569 extern void hisi_sas_debugfs_work_handler(struct work_struct
*work
);