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 HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
65 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
66 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
67 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
68 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
70 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
71 SHOST_DIF_TYPE2_PROTECTION | \
72 SHOST_DIF_TYPE3_PROTECTION)
74 #define HISI_SAS_DIX_PROT_MASK (SHOST_DIX_TYPE1_PROTECTION | \
75 SHOST_DIX_TYPE2_PROTECTION | \
76 SHOST_DIX_TYPE3_PROTECTION)
78 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK | HISI_SAS_DIX_PROT_MASK)
80 #define HISI_SAS_WAIT_PHYUP_TIMEOUT 20
85 PORT_TYPE_SAS = (1U << 1),
86 PORT_TYPE_SATA = (1U << 0),
95 HISI_SAS_INT_ABT_CMD = 0,
96 HISI_SAS_INT_ABT_DEV = 1,
99 enum hisi_sas_dev_type {
100 HISI_SAS_DEV_TYPE_STP = 0,
101 HISI_SAS_DEV_TYPE_SSP,
102 HISI_SAS_DEV_TYPE_SATA,
105 struct hisi_sas_hw_error {
111 const struct hisi_sas_hw_error *sub;
114 struct hisi_sas_rst {
115 struct hisi_hba *hisi_hba;
116 struct completion *completion;
117 struct work_struct work;
121 #define HISI_SAS_RST_WORK_INIT(r, c) \
122 { .hisi_hba = hisi_hba, \
124 .work = __WORK_INITIALIZER(r.work, \
125 hisi_sas_sync_rst_work_handler), \
129 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
130 DECLARE_COMPLETION_ONSTACK(c); \
131 DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \
132 struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)
134 enum hisi_sas_bit_err_type {
135 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
136 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
139 enum hisi_sas_phy_event {
140 HISI_PHYE_PHY_UP = 0U,
141 HISI_PHYE_LINK_RESET,
145 struct hisi_sas_phy {
146 struct work_struct works[HISI_PHYES_NUM];
147 struct hisi_hba *hisi_hba;
148 struct hisi_sas_port *port;
149 struct asd_sas_phy sas_phy;
150 struct sas_identify identify;
151 struct completion *reset_completion;
152 struct timer_list timer;
154 u64 port_id; /* from hw */
161 u32 code_violation_err_count;
162 enum sas_linkrate minimum_linkrate;
163 enum sas_linkrate maximum_linkrate;
167 struct hisi_sas_port {
168 struct asd_sas_port sas_port;
174 struct hisi_hba *hisi_hba;
175 const struct cpumask *pci_irq_mask;
176 struct tasklet_struct tasklet;
182 struct hisi_hba *hisi_hba;
183 struct list_head list;
189 struct hisi_sas_device {
190 struct hisi_hba *hisi_hba;
191 struct domain_device *sas_device;
192 struct completion *completion;
193 struct hisi_sas_dq *dq;
194 struct list_head list;
195 enum sas_device_type dev_type;
196 enum dev_status dev_status;
199 spinlock_t lock; /* For protecting slots */
202 struct hisi_sas_tmf_task {
206 u16 tag_of_task_to_be_managed;
209 struct hisi_sas_slot {
210 struct list_head entry;
211 struct list_head delivery;
212 struct sas_task *task;
213 struct hisi_sas_port *port;
217 int dlvry_queue_slot;
219 int cmplt_queue_slot;
224 dma_addr_t cmd_hdr_dma;
225 struct timer_list internal_abort_timer;
227 struct hisi_sas_tmf_task *tmf;
228 /* Do not reorder/change members after here */
234 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
236 struct hisi_sas_debugfs_reg_lu {
241 struct hisi_sas_debugfs_reg {
242 const struct hisi_sas_debugfs_reg_lu *lu;
246 u32 (*read_global_reg)(struct hisi_hba *hisi_hba, u32 off);
247 u32 (*read_port_reg)(struct hisi_hba *hisi_hba, int port,
253 int (*hw_init)(struct hisi_hba *hisi_hba);
254 void (*setup_itct)(struct hisi_hba *hisi_hba,
255 struct hisi_sas_device *device);
256 int (*slot_index_alloc)(struct hisi_hba *hisi_hba,
257 struct domain_device *device);
258 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
259 void (*sl_notify_ssp)(struct hisi_hba *hisi_hba, int phy_no);
260 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
261 void (*start_delivery)(struct hisi_sas_dq *dq);
262 void (*prep_ssp)(struct hisi_hba *hisi_hba,
263 struct hisi_sas_slot *slot);
264 void (*prep_smp)(struct hisi_hba *hisi_hba,
265 struct hisi_sas_slot *slot);
266 void (*prep_stp)(struct hisi_hba *hisi_hba,
267 struct hisi_sas_slot *slot);
268 void (*prep_abort)(struct hisi_hba *hisi_hba,
269 struct hisi_sas_slot *slot,
270 int device_id, int abort_flag, int tag_to_abort);
271 int (*slot_complete)(struct hisi_hba *hisi_hba,
272 struct hisi_sas_slot *slot);
273 void (*phys_init)(struct hisi_hba *hisi_hba);
274 void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
275 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
276 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
277 void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
278 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
279 struct sas_phy_linkrates *linkrates);
280 enum sas_linkrate (*phy_get_max_linkrate)(void);
281 void (*clear_itct)(struct hisi_hba *hisi_hba,
282 struct hisi_sas_device *dev);
283 void (*free_device)(struct hisi_sas_device *sas_dev);
284 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
285 void (*dereg_device)(struct hisi_hba *hisi_hba,
286 struct domain_device *device);
287 int (*soft_reset)(struct hisi_hba *hisi_hba);
288 u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
289 int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type,
290 u8 reg_index, u8 reg_count, u8 *write_data);
291 int (*wait_cmds_complete_timeout)(struct hisi_hba *hisi_hba,
292 int delay_ms, int timeout_ms);
293 void (*snapshot_prepare)(struct hisi_hba *hisi_hba);
294 void (*snapshot_restore)(struct hisi_hba *hisi_hba);
295 int max_command_entries;
296 int complete_hdr_size;
297 struct scsi_host_template *sht;
299 const struct hisi_sas_debugfs_reg *debugfs_reg_global;
300 const struct hisi_sas_debugfs_reg *debugfs_reg_port;
304 /* This must be the first element, used by SHOST_TO_SAS_HA */
305 struct sas_ha_struct *p;
307 struct platform_device *platform_dev;
308 struct pci_dev *pci_dev;
314 void __iomem *sgpio_regs;
317 u32 ctrl_reset_sts_reg;
318 u32 ctrl_clock_ena_reg;
319 u32 refclk_frequency_mhz;
320 u8 sas_addr[SAS_ADDR_SIZE];
324 struct semaphore sem;
326 struct timer_list timer;
327 struct workqueue_struct *wq;
329 int slot_index_count;
332 unsigned long *slot_index_tags;
333 unsigned long reject_stp_links_msk;
336 struct sas_ha_struct sha;
337 struct Scsi_Host *shost;
339 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
340 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
341 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
342 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
346 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
347 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
348 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
349 void *complete_hdr[HISI_SAS_MAX_QUEUES];
350 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
351 struct hisi_sas_initial_fis *initial_fis;
352 dma_addr_t initial_fis_dma;
353 struct hisi_sas_itct *itct;
355 struct hisi_sas_iost *iost;
357 struct hisi_sas_breakpoint *breakpoint;
358 dma_addr_t breakpoint_dma;
359 struct hisi_sas_breakpoint *sata_breakpoint;
360 dma_addr_t sata_breakpoint_dma;
361 struct hisi_sas_slot *slot_info;
363 const struct hisi_sas_hw *hw; /* Low level hw interface */
364 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
365 struct work_struct rst_work;
366 struct work_struct debugfs_work;
368 u32 intr_coal_ticks; /* Time of interrupt coalesce in us */
369 u32 intr_coal_count; /* Interrupt count to coalesce */
372 unsigned int *reply_map;
374 /* debugfs memories */
375 u32 *debugfs_global_reg;
376 u32 *debugfs_port_reg[HISI_SAS_MAX_PHYS];
377 void *debugfs_complete_hdr[HISI_SAS_MAX_QUEUES];
378 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr[HISI_SAS_MAX_QUEUES];
379 struct hisi_sas_iost *debugfs_iost;
380 struct hisi_sas_itct *debugfs_itct;
382 struct dentry *debugfs_dir;
383 struct dentry *debugfs_dump_dentry;
384 bool debugfs_snapshot;
387 /* Generic HW DMA host memory structures */
388 /* Delivery queue header */
389 struct hisi_sas_cmd_hdr {
400 __le32 transfer_tags;
403 __le32 data_transfer_len;
406 __le32 first_burst_num;
415 __le64 cmd_table_addr;
418 __le64 sts_buffer_addr;
421 __le64 prd_table_addr;
424 __le64 dif_prd_table_addr;
427 struct hisi_sas_itct {
435 struct hisi_sas_iost {
442 struct hisi_sas_err_record {
446 struct hisi_sas_initial_fis {
447 struct hisi_sas_err_record err_record;
448 struct dev_to_host_fis fis;
452 struct hisi_sas_breakpoint {
456 struct hisi_sas_sata_breakpoint {
457 struct hisi_sas_breakpoint tag[32];
460 struct hisi_sas_sge {
468 struct hisi_sas_command_table_smp {
472 struct hisi_sas_command_table_stp {
473 struct host_to_dev_fis command_fis;
475 u8 atapi_cdb[ATAPI_CDB_LEN];
478 #define HISI_SAS_SGE_PAGE_CNT (124)
479 struct hisi_sas_sge_page {
480 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
483 #define HISI_SAS_SGE_DIF_PAGE_CNT HISI_SAS_SGE_PAGE_CNT
484 struct hisi_sas_sge_dif_page {
485 struct hisi_sas_sge sge[HISI_SAS_SGE_DIF_PAGE_CNT];
488 struct hisi_sas_command_table_ssp {
489 struct ssp_frame_hdr hdr;
492 struct ssp_command_iu task;
495 struct ssp_tmf_iu ssp_task;
496 struct xfer_rdy_iu xfer_rdy;
497 struct ssp_response_iu ssp_res;
501 union hisi_sas_command_table {
502 struct hisi_sas_command_table_ssp ssp;
503 struct hisi_sas_command_table_smp smp;
504 struct hisi_sas_command_table_stp stp;
507 struct hisi_sas_status_buffer {
508 struct hisi_sas_err_record err;
512 struct hisi_sas_slot_buf_table {
513 struct hisi_sas_status_buffer status_buffer;
514 union hisi_sas_command_table command_header;
515 struct hisi_sas_sge_page sge_page;
518 struct hisi_sas_slot_dif_buf_table {
519 struct hisi_sas_slot_buf_table slot_buf;
520 struct hisi_sas_sge_dif_page sge_dif_page;
523 extern struct scsi_transport_template *hisi_sas_stt;
525 extern bool hisi_sas_debugfs_enable;
526 extern struct dentry *hisi_sas_debugfs_dir;
528 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
529 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba);
530 extern void hisi_sas_free(struct hisi_hba *hisi_hba);
531 extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis,
533 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
534 extern void hisi_sas_sata_done(struct sas_task *task,
535 struct hisi_sas_slot *slot);
536 extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
537 extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
538 extern int hisi_sas_probe(struct platform_device *pdev,
539 const struct hisi_sas_hw *ops);
540 extern int hisi_sas_remove(struct platform_device *pdev);
542 extern int hisi_sas_slave_configure(struct scsi_device *sdev);
543 extern int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time);
544 extern void hisi_sas_scan_start(struct Scsi_Host *shost);
545 extern int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type);
546 extern void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no,
548 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
549 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
550 struct sas_task *task,
551 struct hisi_sas_slot *slot);
552 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
553 extern void hisi_sas_rst_work_handler(struct work_struct *work);
554 extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
555 extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
556 extern void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no);
557 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
558 enum hisi_sas_phy_event event);
559 extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba);
560 extern u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max);
561 extern void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba);
562 extern void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba);
563 extern void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba);
564 extern void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba);
565 extern void hisi_sas_debugfs_work_handler(struct work_struct *work);