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/blk-mq.h>
12 #include <linux/blk-mq-pci.h>
13 #include <linux/clk.h>
14 #include <linux/debugfs.h>
15 #include <linux/dmapool.h>
16 #include <linux/iopoll.h>
17 #include <linux/lcm.h>
18 #include <linux/libata.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 <linux/timer.h>
27 #include <scsi/sas_ata.h>
28 #include <scsi/libsas.h>
30 #define HISI_SAS_MAX_PHYS 9
31 #define HISI_SAS_MAX_QUEUES 32
32 #define HISI_SAS_QUEUE_SLOTS 4096
33 #define HISI_SAS_MAX_ITCT_ENTRIES 1024
34 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
35 #define HISI_SAS_RESET_BIT 0
36 #define HISI_SAS_REJECT_CMD_BIT 1
37 #define HISI_SAS_MAX_COMMANDS (HISI_SAS_QUEUE_SLOTS)
38 #define HISI_SAS_RESERVED_IPTT 96
39 #define HISI_SAS_UNRESERVED_IPTT \
40 (HISI_SAS_MAX_COMMANDS - HISI_SAS_RESERVED_IPTT)
42 #define HISI_SAS_IOST_ITCT_CACHE_NUM 64
43 #define HISI_SAS_IOST_ITCT_CACHE_DW_SZ 10
45 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
46 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
48 #define hisi_sas_status_buf_addr(buf) \
49 ((buf) + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
50 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr((slot)->buf)
51 #define hisi_sas_status_buf_addr_dma(slot) \
52 hisi_sas_status_buf_addr((slot)->buf_dma)
54 #define hisi_sas_cmd_hdr_addr(buf) \
55 ((buf) + offsetof(struct hisi_sas_slot_buf_table, command_header))
56 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr((slot)->buf)
57 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr((slot)->buf_dma)
59 #define hisi_sas_sge_addr(buf) \
60 ((buf) + offsetof(struct hisi_sas_slot_buf_table, sge_page))
61 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr((slot)->buf)
62 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr((slot)->buf_dma)
64 #define hisi_sas_sge_dif_addr(buf) \
65 ((buf) + offsetof(struct hisi_sas_slot_dif_buf_table, sge_dif_page))
66 #define hisi_sas_sge_dif_addr_mem(slot) hisi_sas_sge_dif_addr((slot)->buf)
67 #define hisi_sas_sge_dif_addr_dma(slot) hisi_sas_sge_dif_addr((slot)->buf_dma)
69 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
70 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
71 #define HISI_SAS_MAX_STP_RESP_SZ 28
73 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
74 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
75 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
76 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
77 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
79 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
80 SHOST_DIF_TYPE2_PROTECTION | \
81 SHOST_DIF_TYPE3_PROTECTION)
83 #define HISI_SAS_DIX_PROT_MASK (SHOST_DIX_TYPE1_PROTECTION | \
84 SHOST_DIX_TYPE2_PROTECTION | \
85 SHOST_DIX_TYPE3_PROTECTION)
87 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK | HISI_SAS_DIX_PROT_MASK)
89 #define HISI_SAS_WAIT_PHYUP_TIMEOUT 20
90 #define CLEAR_ITCT_TIMEOUT 20
95 PORT_TYPE_SAS = (1U << 1),
96 PORT_TYPE_SATA = (1U << 0),
105 HISI_SAS_INT_ABT_CMD = 0,
106 HISI_SAS_INT_ABT_DEV = 1,
109 enum hisi_sas_dev_type {
110 HISI_SAS_DEV_TYPE_STP = 0,
111 HISI_SAS_DEV_TYPE_SSP,
112 HISI_SAS_DEV_TYPE_SATA,
115 struct hisi_sas_hw_error {
121 const struct hisi_sas_hw_error *sub;
124 struct hisi_sas_rst {
125 struct hisi_hba *hisi_hba;
126 struct completion *completion;
127 struct work_struct work;
131 #define HISI_SAS_RST_WORK_INIT(r, c) \
132 { .hisi_hba = hisi_hba, \
134 .work = __WORK_INITIALIZER(r.work, \
135 hisi_sas_sync_rst_work_handler), \
139 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
140 DECLARE_COMPLETION_ONSTACK(c); \
141 struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)
143 enum hisi_sas_bit_err_type {
144 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
145 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
148 enum hisi_sas_phy_event {
149 HISI_PHYE_PHY_UP = 0U,
150 HISI_PHYE_LINK_RESET,
154 struct hisi_sas_phy {
155 struct work_struct works[HISI_PHYES_NUM];
156 struct hisi_hba *hisi_hba;
157 struct hisi_sas_port *port;
158 struct asd_sas_phy sas_phy;
159 struct sas_identify identify;
160 struct completion *reset_completion;
161 struct timer_list timer;
163 u64 port_id; /* from hw */
170 u32 code_violation_err_count;
171 enum sas_linkrate minimum_linkrate;
172 enum sas_linkrate maximum_linkrate;
177 struct hisi_sas_port {
178 struct asd_sas_port sas_port;
184 struct hisi_hba *hisi_hba;
185 const struct cpumask *irq_mask;
192 struct hisi_hba *hisi_hba;
193 struct list_head list;
199 struct hisi_sas_device {
200 struct hisi_hba *hisi_hba;
201 struct domain_device *sas_device;
202 struct completion *completion;
203 struct hisi_sas_dq *dq;
204 struct list_head list;
205 enum sas_device_type dev_type;
206 enum dev_status dev_status;
209 spinlock_t lock; /* For protecting slots */
212 struct hisi_sas_tmf_task {
216 u16 tag_of_task_to_be_managed;
219 struct hisi_sas_slot {
220 struct list_head entry;
221 struct list_head delivery;
222 struct sas_task *task;
223 struct hisi_sas_port *port;
227 int dlvry_queue_slot;
229 int cmplt_queue_slot;
234 dma_addr_t cmd_hdr_dma;
235 struct timer_list internal_abort_timer;
237 struct hisi_sas_tmf_task *tmf;
238 /* Do not reorder/change members after here */
244 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
246 struct hisi_sas_debugfs_reg_lu {
251 struct hisi_sas_debugfs_reg {
252 const struct hisi_sas_debugfs_reg_lu *lu;
256 u32 (*read_global_reg)(struct hisi_hba *hisi_hba, u32 off);
257 u32 (*read_port_reg)(struct hisi_hba *hisi_hba, int port,
262 struct hisi_sas_iost_itct_cache {
263 u32 data[HISI_SAS_IOST_ITCT_CACHE_DW_SZ];
266 enum hisi_sas_debugfs_reg_array_member {
273 enum hisi_sas_debugfs_cache_type {
279 int (*hw_init)(struct hisi_hba *hisi_hba);
280 void (*setup_itct)(struct hisi_hba *hisi_hba,
281 struct hisi_sas_device *device);
282 int (*slot_index_alloc)(struct hisi_hba *hisi_hba,
283 struct domain_device *device);
284 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
285 void (*sl_notify_ssp)(struct hisi_hba *hisi_hba, int phy_no);
286 void (*start_delivery)(struct hisi_sas_dq *dq);
287 void (*prep_ssp)(struct hisi_hba *hisi_hba,
288 struct hisi_sas_slot *slot);
289 void (*prep_smp)(struct hisi_hba *hisi_hba,
290 struct hisi_sas_slot *slot);
291 void (*prep_stp)(struct hisi_hba *hisi_hba,
292 struct hisi_sas_slot *slot);
293 void (*prep_abort)(struct hisi_hba *hisi_hba,
294 struct hisi_sas_slot *slot,
295 int device_id, int abort_flag, int tag_to_abort);
296 void (*phys_init)(struct hisi_hba *hisi_hba);
297 void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
298 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
299 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
300 void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
301 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
302 struct sas_phy_linkrates *linkrates);
303 enum sas_linkrate (*phy_get_max_linkrate)(void);
304 int (*clear_itct)(struct hisi_hba *hisi_hba,
305 struct hisi_sas_device *dev);
306 void (*free_device)(struct hisi_sas_device *sas_dev);
307 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
308 void (*dereg_device)(struct hisi_hba *hisi_hba,
309 struct domain_device *device);
310 int (*soft_reset)(struct hisi_hba *hisi_hba);
311 u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
312 int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type,
313 u8 reg_index, u8 reg_count, u8 *write_data);
314 void (*wait_cmds_complete_timeout)(struct hisi_hba *hisi_hba,
315 int delay_ms, int timeout_ms);
316 void (*snapshot_prepare)(struct hisi_hba *hisi_hba);
317 void (*snapshot_restore)(struct hisi_hba *hisi_hba);
318 int (*set_bist)(struct hisi_hba *hisi_hba, bool enable);
319 void (*read_iost_itct_cache)(struct hisi_hba *hisi_hba,
320 enum hisi_sas_debugfs_cache_type type,
322 int complete_hdr_size;
323 struct scsi_host_template *sht;
325 const struct hisi_sas_debugfs_reg *debugfs_reg_array[DEBUGFS_REGS_NUM];
326 const struct hisi_sas_debugfs_reg *debugfs_reg_port;
329 #define HISI_SAS_MAX_DEBUGFS_DUMP (50)
331 struct hisi_sas_debugfs_cq {
332 struct hisi_sas_cq *cq;
336 struct hisi_sas_debugfs_dq {
337 struct hisi_sas_dq *dq;
338 struct hisi_sas_cmd_hdr *hdr;
341 struct hisi_sas_debugfs_regs {
342 struct hisi_hba *hisi_hba;
346 struct hisi_sas_debugfs_port {
347 struct hisi_sas_phy *phy;
351 struct hisi_sas_debugfs_iost {
352 struct hisi_sas_iost *iost;
355 struct hisi_sas_debugfs_itct {
356 struct hisi_sas_itct *itct;
359 struct hisi_sas_debugfs_iost_cache {
360 struct hisi_sas_iost_itct_cache *cache;
363 struct hisi_sas_debugfs_itct_cache {
364 struct hisi_sas_iost_itct_cache *cache;
368 /* This must be the first element, used by SHOST_TO_SAS_HA */
369 struct sas_ha_struct *p;
371 struct platform_device *platform_dev;
372 struct pci_dev *pci_dev;
378 void __iomem *sgpio_regs;
381 u32 ctrl_reset_sts_reg;
382 u32 ctrl_clock_ena_reg;
383 u32 refclk_frequency_mhz;
384 u8 sas_addr[SAS_ADDR_SIZE];
388 struct semaphore sem;
390 struct timer_list timer;
391 struct workqueue_struct *wq;
393 int slot_index_count;
396 unsigned long *slot_index_tags;
397 unsigned long reject_stp_links_msk;
400 struct sas_ha_struct sha;
401 struct Scsi_Host *shost;
403 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
404 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
405 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
406 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
410 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
411 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
412 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
413 void *complete_hdr[HISI_SAS_MAX_QUEUES];
414 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
415 struct hisi_sas_initial_fis *initial_fis;
416 dma_addr_t initial_fis_dma;
417 struct hisi_sas_itct *itct;
419 struct hisi_sas_iost *iost;
421 struct hisi_sas_breakpoint *breakpoint;
422 dma_addr_t breakpoint_dma;
423 struct hisi_sas_breakpoint *sata_breakpoint;
424 dma_addr_t sata_breakpoint_dma;
425 struct hisi_sas_slot *slot_info;
427 const struct hisi_sas_hw *hw; /* Low level hw interface */
428 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
429 struct work_struct rst_work;
430 struct work_struct debugfs_work;
432 u32 intr_coal_ticks; /* Time of interrupt coalesce in us */
433 u32 intr_coal_count; /* Interrupt count to coalesce */
438 enum sas_linkrate debugfs_bist_linkrate;
439 int debugfs_bist_code_mode;
440 int debugfs_bist_phy_no;
441 int debugfs_bist_mode;
442 u32 debugfs_bist_cnt;
443 int debugfs_bist_enable;
445 /* debugfs memories */
446 /* Put Global AXI and RAS Register into register array */
447 struct hisi_sas_debugfs_regs debugfs_regs[HISI_SAS_MAX_DEBUGFS_DUMP][DEBUGFS_REGS_NUM];
448 struct hisi_sas_debugfs_port debugfs_port_reg[HISI_SAS_MAX_DEBUGFS_DUMP][HISI_SAS_MAX_PHYS];
449 struct hisi_sas_debugfs_cq debugfs_cq[HISI_SAS_MAX_DEBUGFS_DUMP][HISI_SAS_MAX_QUEUES];
450 struct hisi_sas_debugfs_dq debugfs_dq[HISI_SAS_MAX_DEBUGFS_DUMP][HISI_SAS_MAX_QUEUES];
451 struct hisi_sas_debugfs_iost debugfs_iost[HISI_SAS_MAX_DEBUGFS_DUMP];
452 struct hisi_sas_debugfs_itct debugfs_itct[HISI_SAS_MAX_DEBUGFS_DUMP];
453 struct hisi_sas_debugfs_iost_cache debugfs_iost_cache[HISI_SAS_MAX_DEBUGFS_DUMP];
454 struct hisi_sas_debugfs_itct_cache debugfs_itct_cache[HISI_SAS_MAX_DEBUGFS_DUMP];
456 u64 debugfs_timestamp[HISI_SAS_MAX_DEBUGFS_DUMP];
457 int debugfs_dump_index;
458 struct dentry *debugfs_dir;
459 struct dentry *debugfs_dump_dentry;
460 struct dentry *debugfs_bist_dentry;
463 /* Generic HW DMA host memory structures */
464 /* Delivery queue header */
465 struct hisi_sas_cmd_hdr {
476 __le32 transfer_tags;
479 __le32 data_transfer_len;
482 __le32 first_burst_num;
491 __le64 cmd_table_addr;
494 __le64 sts_buffer_addr;
497 __le64 prd_table_addr;
500 __le64 dif_prd_table_addr;
503 struct hisi_sas_itct {
511 struct hisi_sas_iost {
518 struct hisi_sas_err_record {
522 struct hisi_sas_initial_fis {
523 struct hisi_sas_err_record err_record;
524 struct dev_to_host_fis fis;
528 struct hisi_sas_breakpoint {
532 struct hisi_sas_sata_breakpoint {
533 struct hisi_sas_breakpoint tag[32];
536 struct hisi_sas_sge {
544 struct hisi_sas_command_table_smp {
548 struct hisi_sas_command_table_stp {
549 struct host_to_dev_fis command_fis;
551 u8 atapi_cdb[ATAPI_CDB_LEN];
554 #define HISI_SAS_SGE_PAGE_CNT (124)
555 struct hisi_sas_sge_page {
556 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
559 #define HISI_SAS_SGE_DIF_PAGE_CNT HISI_SAS_SGE_PAGE_CNT
560 struct hisi_sas_sge_dif_page {
561 struct hisi_sas_sge sge[HISI_SAS_SGE_DIF_PAGE_CNT];
564 struct hisi_sas_command_table_ssp {
565 struct ssp_frame_hdr hdr;
568 struct ssp_command_iu task;
571 struct ssp_tmf_iu ssp_task;
572 struct xfer_rdy_iu xfer_rdy;
573 struct ssp_response_iu ssp_res;
577 union hisi_sas_command_table {
578 struct hisi_sas_command_table_ssp ssp;
579 struct hisi_sas_command_table_smp smp;
580 struct hisi_sas_command_table_stp stp;
583 struct hisi_sas_status_buffer {
584 struct hisi_sas_err_record err;
588 struct hisi_sas_slot_buf_table {
589 struct hisi_sas_status_buffer status_buffer;
590 union hisi_sas_command_table command_header;
591 struct hisi_sas_sge_page sge_page;
594 struct hisi_sas_slot_dif_buf_table {
595 struct hisi_sas_slot_buf_table slot_buf;
596 struct hisi_sas_sge_dif_page sge_dif_page;
599 extern struct scsi_transport_template *hisi_sas_stt;
601 extern bool hisi_sas_debugfs_enable;
602 extern u32 hisi_sas_debugfs_dump_count;
603 extern struct dentry *hisi_sas_debugfs_dir;
605 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
606 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba);
607 extern void hisi_sas_free(struct hisi_hba *hisi_hba);
608 extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis,
610 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
611 extern void hisi_sas_sata_done(struct sas_task *task,
612 struct hisi_sas_slot *slot);
613 extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
614 extern int hisi_sas_probe(struct platform_device *pdev,
615 const struct hisi_sas_hw *ops);
616 extern int hisi_sas_remove(struct platform_device *pdev);
618 extern int hisi_sas_slave_configure(struct scsi_device *sdev);
619 extern int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time);
620 extern void hisi_sas_scan_start(struct Scsi_Host *shost);
621 extern int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type);
622 extern void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no,
624 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
625 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
626 struct sas_task *task,
627 struct hisi_sas_slot *slot);
628 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
629 extern void hisi_sas_rst_work_handler(struct work_struct *work);
630 extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
631 extern void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba);
632 extern void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no);
633 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
634 enum hisi_sas_phy_event event);
635 extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba);
636 extern u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max);
637 extern void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba);
638 extern void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba);
639 extern void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba);
640 extern void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba);
641 extern void hisi_sas_debugfs_work_handler(struct work_struct *work);