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 <linux/timer.h>
25 #include <scsi/sas_ata.h>
26 #include <scsi/libsas.h>
28 #define HISI_SAS_MAX_PHYS 9
29 #define HISI_SAS_MAX_QUEUES 32
30 #define HISI_SAS_QUEUE_SLOTS 4096
31 #define HISI_SAS_MAX_ITCT_ENTRIES 1024
32 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
33 #define HISI_SAS_RESET_BIT 0
34 #define HISI_SAS_REJECT_CMD_BIT 1
35 #define HISI_SAS_MAX_COMMANDS (HISI_SAS_QUEUE_SLOTS)
36 #define HISI_SAS_RESERVED_IPTT 96
37 #define HISI_SAS_UNRESERVED_IPTT \
38 (HISI_SAS_MAX_COMMANDS - HISI_SAS_RESERVED_IPTT)
40 #define HISI_SAS_IOST_ITCT_CACHE_NUM 64
41 #define HISI_SAS_IOST_ITCT_CACHE_DW_SZ 10
43 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
44 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
46 #define hisi_sas_status_buf_addr(buf) \
47 ((buf) + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
48 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr((slot)->buf)
49 #define hisi_sas_status_buf_addr_dma(slot) \
50 hisi_sas_status_buf_addr((slot)->buf_dma)
52 #define hisi_sas_cmd_hdr_addr(buf) \
53 ((buf) + offsetof(struct hisi_sas_slot_buf_table, command_header))
54 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr((slot)->buf)
55 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr((slot)->buf_dma)
57 #define hisi_sas_sge_addr(buf) \
58 ((buf) + offsetof(struct hisi_sas_slot_buf_table, sge_page))
59 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr((slot)->buf)
60 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr((slot)->buf_dma)
62 #define hisi_sas_sge_dif_addr(buf) \
63 ((buf) + offsetof(struct hisi_sas_slot_dif_buf_table, sge_dif_page))
64 #define hisi_sas_sge_dif_addr_mem(slot) hisi_sas_sge_dif_addr((slot)->buf)
65 #define hisi_sas_sge_dif_addr_dma(slot) hisi_sas_sge_dif_addr((slot)->buf_dma)
67 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
68 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
69 #define HISI_SAS_MAX_STP_RESP_SZ 28
71 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
72 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
73 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
74 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
75 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
77 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
78 SHOST_DIF_TYPE2_PROTECTION | \
79 SHOST_DIF_TYPE3_PROTECTION)
81 #define HISI_SAS_DIX_PROT_MASK (SHOST_DIX_TYPE1_PROTECTION | \
82 SHOST_DIX_TYPE2_PROTECTION | \
83 SHOST_DIX_TYPE3_PROTECTION)
85 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK | HISI_SAS_DIX_PROT_MASK)
87 #define HISI_SAS_WAIT_PHYUP_TIMEOUT 20
88 #define CLEAR_ITCT_TIMEOUT 20
93 PORT_TYPE_SAS
= (1U << 1),
94 PORT_TYPE_SATA
= (1U << 0),
103 HISI_SAS_INT_ABT_CMD
= 0,
104 HISI_SAS_INT_ABT_DEV
= 1,
107 enum hisi_sas_dev_type
{
108 HISI_SAS_DEV_TYPE_STP
= 0,
109 HISI_SAS_DEV_TYPE_SSP
,
110 HISI_SAS_DEV_TYPE_SATA
,
113 struct hisi_sas_hw_error
{
119 const struct hisi_sas_hw_error
*sub
;
122 struct hisi_sas_rst
{
123 struct hisi_hba
*hisi_hba
;
124 struct completion
*completion
;
125 struct work_struct work
;
129 #define HISI_SAS_RST_WORK_INIT(r, c) \
130 { .hisi_hba = hisi_hba, \
132 .work = __WORK_INITIALIZER(r.work, \
133 hisi_sas_sync_rst_work_handler), \
137 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
138 DECLARE_COMPLETION_ONSTACK(c); \
139 struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)
141 enum hisi_sas_bit_err_type
{
142 HISI_SAS_ERR_SINGLE_BIT_ECC
= 0x0,
143 HISI_SAS_ERR_MULTI_BIT_ECC
= 0x1,
146 enum hisi_sas_phy_event
{
147 HISI_PHYE_PHY_UP
= 0U,
148 HISI_PHYE_LINK_RESET
,
152 struct hisi_sas_phy
{
153 struct work_struct works
[HISI_PHYES_NUM
];
154 struct hisi_hba
*hisi_hba
;
155 struct hisi_sas_port
*port
;
156 struct asd_sas_phy sas_phy
;
157 struct sas_identify identify
;
158 struct completion
*reset_completion
;
159 struct timer_list timer
;
161 u64 port_id
; /* from hw */
168 u32 code_violation_err_count
;
169 enum sas_linkrate minimum_linkrate
;
170 enum sas_linkrate maximum_linkrate
;
175 struct hisi_sas_port
{
176 struct asd_sas_port sas_port
;
182 struct hisi_hba
*hisi_hba
;
183 const struct cpumask
*irq_mask
;
190 struct hisi_hba
*hisi_hba
;
191 struct list_head list
;
197 struct hisi_sas_device
{
198 struct hisi_hba
*hisi_hba
;
199 struct domain_device
*sas_device
;
200 struct completion
*completion
;
201 struct hisi_sas_dq
*dq
;
202 struct list_head list
;
203 enum sas_device_type dev_type
;
204 enum dev_status dev_status
;
207 spinlock_t lock
; /* For protecting slots */
210 struct hisi_sas_tmf_task
{
214 u16 tag_of_task_to_be_managed
;
217 struct hisi_sas_slot
{
218 struct list_head entry
;
219 struct list_head delivery
;
220 struct sas_task
*task
;
221 struct hisi_sas_port
*port
;
225 int dlvry_queue_slot
;
227 int cmplt_queue_slot
;
232 dma_addr_t cmd_hdr_dma
;
233 struct timer_list internal_abort_timer
;
235 struct hisi_sas_tmf_task
*tmf
;
236 /* Do not reorder/change members after here */
242 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
244 struct hisi_sas_debugfs_reg_lu
{
249 struct hisi_sas_debugfs_reg
{
250 const struct hisi_sas_debugfs_reg_lu
*lu
;
254 u32 (*read_global_reg
)(struct hisi_hba
*hisi_hba
, u32 off
);
255 u32 (*read_port_reg
)(struct hisi_hba
*hisi_hba
, int port
,
260 struct hisi_sas_iost_itct_cache
{
261 u32 data
[HISI_SAS_IOST_ITCT_CACHE_DW_SZ
];
264 enum hisi_sas_debugfs_reg_array_member
{
271 enum hisi_sas_debugfs_cache_type
{
277 int (*hw_init
)(struct hisi_hba
*hisi_hba
);
278 void (*setup_itct
)(struct hisi_hba
*hisi_hba
,
279 struct hisi_sas_device
*device
);
280 int (*slot_index_alloc
)(struct hisi_hba
*hisi_hba
,
281 struct domain_device
*device
);
282 struct hisi_sas_device
*(*alloc_dev
)(struct domain_device
*device
);
283 void (*sl_notify_ssp
)(struct hisi_hba
*hisi_hba
, int phy_no
);
284 void (*start_delivery
)(struct hisi_sas_dq
*dq
);
285 void (*prep_ssp
)(struct hisi_hba
*hisi_hba
,
286 struct hisi_sas_slot
*slot
);
287 void (*prep_smp
)(struct hisi_hba
*hisi_hba
,
288 struct hisi_sas_slot
*slot
);
289 void (*prep_stp
)(struct hisi_hba
*hisi_hba
,
290 struct hisi_sas_slot
*slot
);
291 void (*prep_abort
)(struct hisi_hba
*hisi_hba
,
292 struct hisi_sas_slot
*slot
,
293 int device_id
, int abort_flag
, int tag_to_abort
);
294 void (*phys_init
)(struct hisi_hba
*hisi_hba
);
295 void (*phy_start
)(struct hisi_hba
*hisi_hba
, int phy_no
);
296 void (*phy_disable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
297 void (*phy_hard_reset
)(struct hisi_hba
*hisi_hba
, int phy_no
);
298 void (*get_events
)(struct hisi_hba
*hisi_hba
, int phy_no
);
299 void (*phy_set_linkrate
)(struct hisi_hba
*hisi_hba
, int phy_no
,
300 struct sas_phy_linkrates
*linkrates
);
301 enum sas_linkrate (*phy_get_max_linkrate
)(void);
302 int (*clear_itct
)(struct hisi_hba
*hisi_hba
,
303 struct hisi_sas_device
*dev
);
304 void (*free_device
)(struct hisi_sas_device
*sas_dev
);
305 int (*get_wideport_bitmap
)(struct hisi_hba
*hisi_hba
, int port_id
);
306 void (*dereg_device
)(struct hisi_hba
*hisi_hba
,
307 struct domain_device
*device
);
308 int (*soft_reset
)(struct hisi_hba
*hisi_hba
);
309 u32 (*get_phys_state
)(struct hisi_hba
*hisi_hba
);
310 int (*write_gpio
)(struct hisi_hba
*hisi_hba
, u8 reg_type
,
311 u8 reg_index
, u8 reg_count
, u8
*write_data
);
312 void (*wait_cmds_complete_timeout
)(struct hisi_hba
*hisi_hba
,
313 int delay_ms
, int timeout_ms
);
314 void (*snapshot_prepare
)(struct hisi_hba
*hisi_hba
);
315 void (*snapshot_restore
)(struct hisi_hba
*hisi_hba
);
316 int (*set_bist
)(struct hisi_hba
*hisi_hba
, bool enable
);
317 void (*read_iost_itct_cache
)(struct hisi_hba
*hisi_hba
,
318 enum hisi_sas_debugfs_cache_type type
,
320 int complete_hdr_size
;
321 struct scsi_host_template
*sht
;
323 const struct hisi_sas_debugfs_reg
*debugfs_reg_array
[DEBUGFS_REGS_NUM
];
324 const struct hisi_sas_debugfs_reg
*debugfs_reg_port
;
327 #define HISI_SAS_MAX_DEBUGFS_DUMP (50)
329 struct hisi_sas_debugfs_cq
{
330 struct hisi_sas_cq
*cq
;
334 struct hisi_sas_debugfs_dq
{
335 struct hisi_sas_dq
*dq
;
336 struct hisi_sas_cmd_hdr
*hdr
;
339 struct hisi_sas_debugfs_regs
{
340 struct hisi_hba
*hisi_hba
;
344 struct hisi_sas_debugfs_port
{
345 struct hisi_sas_phy
*phy
;
349 struct hisi_sas_debugfs_iost
{
350 struct hisi_sas_iost
*iost
;
353 struct hisi_sas_debugfs_itct
{
354 struct hisi_sas_itct
*itct
;
357 struct hisi_sas_debugfs_iost_cache
{
358 struct hisi_sas_iost_itct_cache
*cache
;
361 struct hisi_sas_debugfs_itct_cache
{
362 struct hisi_sas_iost_itct_cache
*cache
;
366 /* This must be the first element, used by SHOST_TO_SAS_HA */
367 struct sas_ha_struct
*p
;
369 struct platform_device
*platform_dev
;
370 struct pci_dev
*pci_dev
;
376 void __iomem
*sgpio_regs
;
379 u32 ctrl_reset_sts_reg
;
380 u32 ctrl_clock_ena_reg
;
381 u32 refclk_frequency_mhz
;
382 u8 sas_addr
[SAS_ADDR_SIZE
];
386 struct semaphore sem
;
388 struct timer_list timer
;
389 struct workqueue_struct
*wq
;
391 int slot_index_count
;
394 unsigned long *slot_index_tags
;
395 unsigned long reject_stp_links_msk
;
398 struct sas_ha_struct sha
;
399 struct Scsi_Host
*shost
;
401 struct hisi_sas_cq cq
[HISI_SAS_MAX_QUEUES
];
402 struct hisi_sas_dq dq
[HISI_SAS_MAX_QUEUES
];
403 struct hisi_sas_phy phy
[HISI_SAS_MAX_PHYS
];
404 struct hisi_sas_port port
[HISI_SAS_MAX_PHYS
];
408 struct hisi_sas_device devices
[HISI_SAS_MAX_DEVICES
];
409 struct hisi_sas_cmd_hdr
*cmd_hdr
[HISI_SAS_MAX_QUEUES
];
410 dma_addr_t cmd_hdr_dma
[HISI_SAS_MAX_QUEUES
];
411 void *complete_hdr
[HISI_SAS_MAX_QUEUES
];
412 dma_addr_t complete_hdr_dma
[HISI_SAS_MAX_QUEUES
];
413 struct hisi_sas_initial_fis
*initial_fis
;
414 dma_addr_t initial_fis_dma
;
415 struct hisi_sas_itct
*itct
;
417 struct hisi_sas_iost
*iost
;
419 struct hisi_sas_breakpoint
*breakpoint
;
420 dma_addr_t breakpoint_dma
;
421 struct hisi_sas_breakpoint
*sata_breakpoint
;
422 dma_addr_t sata_breakpoint_dma
;
423 struct hisi_sas_slot
*slot_info
;
425 const struct hisi_sas_hw
*hw
; /* Low level hw interface */
426 unsigned long sata_dev_bitmap
[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES
)];
427 struct work_struct rst_work
;
428 struct work_struct debugfs_work
;
430 u32 intr_coal_ticks
; /* Time of interrupt coalesce in us */
431 u32 intr_coal_count
; /* Interrupt count to coalesce */
434 unsigned int *reply_map
;
437 enum sas_linkrate debugfs_bist_linkrate
;
438 int debugfs_bist_code_mode
;
439 int debugfs_bist_phy_no
;
440 int debugfs_bist_mode
;
441 u32 debugfs_bist_cnt
;
442 int debugfs_bist_enable
;
444 /* debugfs memories */
445 /* Put Global AXI and RAS Register into register array */
446 struct hisi_sas_debugfs_regs debugfs_regs
[HISI_SAS_MAX_DEBUGFS_DUMP
][DEBUGFS_REGS_NUM
];
447 struct hisi_sas_debugfs_port debugfs_port_reg
[HISI_SAS_MAX_DEBUGFS_DUMP
][HISI_SAS_MAX_PHYS
];
448 struct hisi_sas_debugfs_cq debugfs_cq
[HISI_SAS_MAX_DEBUGFS_DUMP
][HISI_SAS_MAX_QUEUES
];
449 struct hisi_sas_debugfs_dq debugfs_dq
[HISI_SAS_MAX_DEBUGFS_DUMP
][HISI_SAS_MAX_QUEUES
];
450 struct hisi_sas_debugfs_iost debugfs_iost
[HISI_SAS_MAX_DEBUGFS_DUMP
];
451 struct hisi_sas_debugfs_itct debugfs_itct
[HISI_SAS_MAX_DEBUGFS_DUMP
];
452 struct hisi_sas_debugfs_iost_cache debugfs_iost_cache
[HISI_SAS_MAX_DEBUGFS_DUMP
];
453 struct hisi_sas_debugfs_itct_cache debugfs_itct_cache
[HISI_SAS_MAX_DEBUGFS_DUMP
];
455 u64 debugfs_timestamp
[HISI_SAS_MAX_DEBUGFS_DUMP
];
456 int debugfs_dump_index
;
457 struct dentry
*debugfs_dir
;
458 struct dentry
*debugfs_dump_dentry
;
459 struct dentry
*debugfs_bist_dentry
;
462 /* Generic HW DMA host memory structures */
463 /* Delivery queue header */
464 struct hisi_sas_cmd_hdr
{
475 __le32 transfer_tags
;
478 __le32 data_transfer_len
;
481 __le32 first_burst_num
;
490 __le64 cmd_table_addr
;
493 __le64 sts_buffer_addr
;
496 __le64 prd_table_addr
;
499 __le64 dif_prd_table_addr
;
502 struct hisi_sas_itct
{
510 struct hisi_sas_iost
{
517 struct hisi_sas_err_record
{
521 struct hisi_sas_initial_fis
{
522 struct hisi_sas_err_record err_record
;
523 struct dev_to_host_fis fis
;
527 struct hisi_sas_breakpoint
{
531 struct hisi_sas_sata_breakpoint
{
532 struct hisi_sas_breakpoint tag
[32];
535 struct hisi_sas_sge
{
543 struct hisi_sas_command_table_smp
{
547 struct hisi_sas_command_table_stp
{
548 struct host_to_dev_fis command_fis
;
550 u8 atapi_cdb
[ATAPI_CDB_LEN
];
553 #define HISI_SAS_SGE_PAGE_CNT (124)
554 struct hisi_sas_sge_page
{
555 struct hisi_sas_sge sge
[HISI_SAS_SGE_PAGE_CNT
];
558 #define HISI_SAS_SGE_DIF_PAGE_CNT HISI_SAS_SGE_PAGE_CNT
559 struct hisi_sas_sge_dif_page
{
560 struct hisi_sas_sge sge
[HISI_SAS_SGE_DIF_PAGE_CNT
];
563 struct hisi_sas_command_table_ssp
{
564 struct ssp_frame_hdr hdr
;
567 struct ssp_command_iu task
;
570 struct ssp_tmf_iu ssp_task
;
571 struct xfer_rdy_iu xfer_rdy
;
572 struct ssp_response_iu ssp_res
;
576 union hisi_sas_command_table
{
577 struct hisi_sas_command_table_ssp ssp
;
578 struct hisi_sas_command_table_smp smp
;
579 struct hisi_sas_command_table_stp stp
;
582 struct hisi_sas_status_buffer
{
583 struct hisi_sas_err_record err
;
587 struct hisi_sas_slot_buf_table
{
588 struct hisi_sas_status_buffer status_buffer
;
589 union hisi_sas_command_table command_header
;
590 struct hisi_sas_sge_page sge_page
;
593 struct hisi_sas_slot_dif_buf_table
{
594 struct hisi_sas_slot_buf_table slot_buf
;
595 struct hisi_sas_sge_dif_page sge_dif_page
;
598 extern struct scsi_transport_template
*hisi_sas_stt
;
600 extern bool hisi_sas_debugfs_enable
;
601 extern u32 hisi_sas_debugfs_dump_count
;
602 extern struct dentry
*hisi_sas_debugfs_dir
;
604 extern void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
);
605 extern int hisi_sas_alloc(struct hisi_hba
*hisi_hba
);
606 extern void hisi_sas_free(struct hisi_hba
*hisi_hba
);
607 extern u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
,
609 extern struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
);
610 extern void hisi_sas_sata_done(struct sas_task
*task
,
611 struct hisi_sas_slot
*slot
);
612 extern int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
);
613 extern int hisi_sas_probe(struct platform_device
*pdev
,
614 const struct hisi_sas_hw
*ops
);
615 extern int hisi_sas_remove(struct platform_device
*pdev
);
617 extern int hisi_sas_slave_configure(struct scsi_device
*sdev
);
618 extern int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
);
619 extern void hisi_sas_scan_start(struct Scsi_Host
*shost
);
620 extern int hisi_sas_host_reset(struct Scsi_Host
*shost
, int reset_type
);
621 extern void hisi_sas_phy_enable(struct hisi_hba
*hisi_hba
, int phy_no
,
623 extern void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
);
624 extern void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
,
625 struct sas_task
*task
,
626 struct hisi_sas_slot
*slot
);
627 extern void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
);
628 extern void hisi_sas_rst_work_handler(struct work_struct
*work
);
629 extern void hisi_sas_sync_rst_work_handler(struct work_struct
*work
);
630 extern void hisi_sas_sync_irqs(struct hisi_hba
*hisi_hba
);
631 extern void hisi_sas_phy_oob_ready(struct hisi_hba
*hisi_hba
, int phy_no
);
632 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
633 enum hisi_sas_phy_event event
);
634 extern void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
);
635 extern u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
);
636 extern void hisi_sas_controller_reset_prepare(struct hisi_hba
*hisi_hba
);
637 extern void hisi_sas_controller_reset_done(struct hisi_hba
*hisi_hba
);
638 extern void hisi_sas_debugfs_init(struct hisi_hba
*hisi_hba
);
639 extern void hisi_sas_debugfs_exit(struct hisi_hba
*hisi_hba
);
640 extern void hisi_sas_debugfs_work_handler(struct work_struct
*work
);