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/debugfs.h>
18 #include <linux/dmapool.h>
19 #include <linux/iopoll.h>
20 #include <linux/lcm.h>
21 #include <linux/libata.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/module.h>
24 #include <linux/of_address.h>
25 #include <linux/pci.h>
26 #include <linux/platform_device.h>
27 #include <linux/property.h>
28 #include <linux/regmap.h>
29 #include <scsi/sas_ata.h>
30 #include <scsi/libsas.h>
32 #define HISI_SAS_MAX_PHYS 9
33 #define HISI_SAS_MAX_QUEUES 32
34 #define HISI_SAS_QUEUE_SLOTS 4096
35 #define HISI_SAS_MAX_ITCT_ENTRIES 1024
36 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
37 #define HISI_SAS_RESET_BIT 0
38 #define HISI_SAS_REJECT_CMD_BIT 1
39 #define HISI_SAS_RESERVED_IPTT_CNT 96
41 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
42 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
44 #define hisi_sas_status_buf_addr(buf) \
45 ((buf) + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
46 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr((slot)->buf)
47 #define hisi_sas_status_buf_addr_dma(slot) \
48 hisi_sas_status_buf_addr((slot)->buf_dma)
50 #define hisi_sas_cmd_hdr_addr(buf) \
51 ((buf) + offsetof(struct hisi_sas_slot_buf_table, command_header))
52 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr((slot)->buf)
53 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr((slot)->buf_dma)
55 #define hisi_sas_sge_addr(buf) \
56 ((buf) + offsetof(struct hisi_sas_slot_buf_table, sge_page))
57 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr((slot)->buf)
58 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr((slot)->buf_dma)
60 #define hisi_sas_sge_dif_addr(buf) \
61 ((buf) + offsetof(struct hisi_sas_slot_dif_buf_table, sge_dif_page))
62 #define hisi_sas_sge_dif_addr_mem(slot) hisi_sas_sge_dif_addr((slot)->buf)
63 #define hisi_sas_sge_dif_addr_dma(slot) hisi_sas_sge_dif_addr((slot)->buf_dma)
65 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
66 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
67 #define HISI_SAS_MAX_STP_RESP_SZ 28
69 #define DEV_IS_EXPANDER(type) \
70 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
71 (type == SAS_FANOUT_EXPANDER_DEVICE))
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
94 PORT_TYPE_SAS
= (1U << 1),
95 PORT_TYPE_SATA
= (1U << 0),
104 HISI_SAS_INT_ABT_CMD
= 0,
105 HISI_SAS_INT_ABT_DEV
= 1,
108 enum hisi_sas_dev_type
{
109 HISI_SAS_DEV_TYPE_STP
= 0,
110 HISI_SAS_DEV_TYPE_SSP
,
111 HISI_SAS_DEV_TYPE_SATA
,
114 struct hisi_sas_hw_error
{
120 const struct hisi_sas_hw_error
*sub
;
123 struct hisi_sas_rst
{
124 struct hisi_hba
*hisi_hba
;
125 struct completion
*completion
;
126 struct work_struct work
;
130 #define HISI_SAS_RST_WORK_INIT(r, c) \
131 { .hisi_hba = hisi_hba, \
133 .work = __WORK_INITIALIZER(r.work, \
134 hisi_sas_sync_rst_work_handler), \
138 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
139 DECLARE_COMPLETION_ONSTACK(c); \
140 DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \
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
;
175 struct hisi_sas_port
{
176 struct asd_sas_port sas_port
;
182 struct hisi_hba
*hisi_hba
;
183 const struct cpumask
*pci_irq_mask
;
184 struct tasklet_struct tasklet
;
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
,
261 int (*hw_init
)(struct hisi_hba
*hisi_hba
);
262 void (*setup_itct
)(struct hisi_hba
*hisi_hba
,
263 struct hisi_sas_device
*device
);
264 int (*slot_index_alloc
)(struct hisi_hba
*hisi_hba
,
265 struct domain_device
*device
);
266 struct hisi_sas_device
*(*alloc_dev
)(struct domain_device
*device
);
267 void (*sl_notify_ssp
)(struct hisi_hba
*hisi_hba
, int phy_no
);
268 int (*get_free_slot
)(struct hisi_hba
*hisi_hba
, struct hisi_sas_dq
*dq
);
269 void (*start_delivery
)(struct hisi_sas_dq
*dq
);
270 void (*prep_ssp
)(struct hisi_hba
*hisi_hba
,
271 struct hisi_sas_slot
*slot
);
272 void (*prep_smp
)(struct hisi_hba
*hisi_hba
,
273 struct hisi_sas_slot
*slot
);
274 void (*prep_stp
)(struct hisi_hba
*hisi_hba
,
275 struct hisi_sas_slot
*slot
);
276 void (*prep_abort
)(struct hisi_hba
*hisi_hba
,
277 struct hisi_sas_slot
*slot
,
278 int device_id
, int abort_flag
, int tag_to_abort
);
279 int (*slot_complete
)(struct hisi_hba
*hisi_hba
,
280 struct hisi_sas_slot
*slot
);
281 void (*phys_init
)(struct hisi_hba
*hisi_hba
);
282 void (*phy_start
)(struct hisi_hba
*hisi_hba
, int phy_no
);
283 void (*phy_disable
)(struct hisi_hba
*hisi_hba
, int phy_no
);
284 void (*phy_hard_reset
)(struct hisi_hba
*hisi_hba
, int phy_no
);
285 void (*get_events
)(struct hisi_hba
*hisi_hba
, int phy_no
);
286 void (*phy_set_linkrate
)(struct hisi_hba
*hisi_hba
, int phy_no
,
287 struct sas_phy_linkrates
*linkrates
);
288 enum sas_linkrate (*phy_get_max_linkrate
)(void);
289 void (*clear_itct
)(struct hisi_hba
*hisi_hba
,
290 struct hisi_sas_device
*dev
);
291 void (*free_device
)(struct hisi_sas_device
*sas_dev
);
292 int (*get_wideport_bitmap
)(struct hisi_hba
*hisi_hba
, int port_id
);
293 void (*dereg_device
)(struct hisi_hba
*hisi_hba
,
294 struct domain_device
*device
);
295 int (*soft_reset
)(struct hisi_hba
*hisi_hba
);
296 u32 (*get_phys_state
)(struct hisi_hba
*hisi_hba
);
297 int (*write_gpio
)(struct hisi_hba
*hisi_hba
, u8 reg_type
,
298 u8 reg_index
, u8 reg_count
, u8
*write_data
);
299 int (*wait_cmds_complete_timeout
)(struct hisi_hba
*hisi_hba
,
300 int delay_ms
, int timeout_ms
);
301 void (*snapshot_prepare
)(struct hisi_hba
*hisi_hba
);
302 void (*snapshot_restore
)(struct hisi_hba
*hisi_hba
);
303 int max_command_entries
;
304 int complete_hdr_size
;
305 struct scsi_host_template
*sht
;
307 const struct hisi_sas_debugfs_reg
*debugfs_reg_global
;
308 const struct hisi_sas_debugfs_reg
*debugfs_reg_port
;
312 /* This must be the first element, used by SHOST_TO_SAS_HA */
313 struct sas_ha_struct
*p
;
315 struct platform_device
*platform_dev
;
316 struct pci_dev
*pci_dev
;
322 void __iomem
*sgpio_regs
;
325 u32 ctrl_reset_sts_reg
;
326 u32 ctrl_clock_ena_reg
;
327 u32 refclk_frequency_mhz
;
328 u8 sas_addr
[SAS_ADDR_SIZE
];
332 struct semaphore sem
;
334 struct timer_list timer
;
335 struct workqueue_struct
*wq
;
337 int slot_index_count
;
340 unsigned long *slot_index_tags
;
341 unsigned long reject_stp_links_msk
;
344 struct sas_ha_struct sha
;
345 struct Scsi_Host
*shost
;
347 struct hisi_sas_cq cq
[HISI_SAS_MAX_QUEUES
];
348 struct hisi_sas_dq dq
[HISI_SAS_MAX_QUEUES
];
349 struct hisi_sas_phy phy
[HISI_SAS_MAX_PHYS
];
350 struct hisi_sas_port port
[HISI_SAS_MAX_PHYS
];
354 struct hisi_sas_device devices
[HISI_SAS_MAX_DEVICES
];
355 struct hisi_sas_cmd_hdr
*cmd_hdr
[HISI_SAS_MAX_QUEUES
];
356 dma_addr_t cmd_hdr_dma
[HISI_SAS_MAX_QUEUES
];
357 void *complete_hdr
[HISI_SAS_MAX_QUEUES
];
358 dma_addr_t complete_hdr_dma
[HISI_SAS_MAX_QUEUES
];
359 struct hisi_sas_initial_fis
*initial_fis
;
360 dma_addr_t initial_fis_dma
;
361 struct hisi_sas_itct
*itct
;
363 struct hisi_sas_iost
*iost
;
365 struct hisi_sas_breakpoint
*breakpoint
;
366 dma_addr_t breakpoint_dma
;
367 struct hisi_sas_breakpoint
*sata_breakpoint
;
368 dma_addr_t sata_breakpoint_dma
;
369 struct hisi_sas_slot
*slot_info
;
371 const struct hisi_sas_hw
*hw
; /* Low level hw interface */
372 unsigned long sata_dev_bitmap
[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES
)];
373 struct work_struct rst_work
;
374 struct work_struct debugfs_work
;
376 u32 intr_coal_ticks
; /* Time of interrupt coalesce in us */
377 u32 intr_coal_count
; /* Interrupt count to coalesce */
380 unsigned int *reply_map
;
382 /* debugfs memories */
383 u32
*debugfs_global_reg
;
384 u32
*debugfs_port_reg
[HISI_SAS_MAX_PHYS
];
385 void *debugfs_complete_hdr
[HISI_SAS_MAX_QUEUES
];
386 struct hisi_sas_cmd_hdr
*debugfs_cmd_hdr
[HISI_SAS_MAX_QUEUES
];
387 struct hisi_sas_iost
*debugfs_iost
;
388 struct hisi_sas_itct
*debugfs_itct
;
390 struct dentry
*debugfs_dir
;
391 struct dentry
*debugfs_dump_dentry
;
392 bool debugfs_snapshot
;
395 /* Generic HW DMA host memory structures */
396 /* Delivery queue header */
397 struct hisi_sas_cmd_hdr
{
408 __le32 transfer_tags
;
411 __le32 data_transfer_len
;
414 __le32 first_burst_num
;
423 __le64 cmd_table_addr
;
426 __le64 sts_buffer_addr
;
429 __le64 prd_table_addr
;
432 __le64 dif_prd_table_addr
;
435 struct hisi_sas_itct
{
443 struct hisi_sas_iost
{
450 struct hisi_sas_err_record
{
454 struct hisi_sas_initial_fis
{
455 struct hisi_sas_err_record err_record
;
456 struct dev_to_host_fis fis
;
460 struct hisi_sas_breakpoint
{
464 struct hisi_sas_sata_breakpoint
{
465 struct hisi_sas_breakpoint tag
[32];
468 struct hisi_sas_sge
{
476 struct hisi_sas_command_table_smp
{
480 struct hisi_sas_command_table_stp
{
481 struct host_to_dev_fis command_fis
;
483 u8 atapi_cdb
[ATAPI_CDB_LEN
];
486 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
487 struct hisi_sas_sge_page
{
488 struct hisi_sas_sge sge
[HISI_SAS_SGE_PAGE_CNT
];
491 #define HISI_SAS_SGE_DIF_PAGE_CNT SG_CHUNK_SIZE
492 struct hisi_sas_sge_dif_page
{
493 struct hisi_sas_sge sge
[HISI_SAS_SGE_DIF_PAGE_CNT
];
496 struct hisi_sas_command_table_ssp
{
497 struct ssp_frame_hdr hdr
;
500 struct ssp_command_iu task
;
503 struct ssp_tmf_iu ssp_task
;
504 struct xfer_rdy_iu xfer_rdy
;
505 struct ssp_response_iu ssp_res
;
509 union hisi_sas_command_table
{
510 struct hisi_sas_command_table_ssp ssp
;
511 struct hisi_sas_command_table_smp smp
;
512 struct hisi_sas_command_table_stp stp
;
515 struct hisi_sas_status_buffer
{
516 struct hisi_sas_err_record err
;
520 struct hisi_sas_slot_buf_table
{
521 struct hisi_sas_status_buffer status_buffer
;
522 union hisi_sas_command_table command_header
;
523 struct hisi_sas_sge_page sge_page
;
526 struct hisi_sas_slot_dif_buf_table
{
527 struct hisi_sas_slot_buf_table slot_buf
;
528 struct hisi_sas_sge_dif_page sge_dif_page
;
531 extern struct scsi_transport_template
*hisi_sas_stt
;
533 extern bool hisi_sas_debugfs_enable
;
534 extern struct dentry
*hisi_sas_debugfs_dir
;
536 extern void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
);
537 extern int hisi_sas_alloc(struct hisi_hba
*hisi_hba
);
538 extern void hisi_sas_free(struct hisi_hba
*hisi_hba
);
539 extern u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
,
541 extern struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
);
542 extern void hisi_sas_sata_done(struct sas_task
*task
,
543 struct hisi_sas_slot
*slot
);
544 extern int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
);
545 extern int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
);
546 extern int hisi_sas_probe(struct platform_device
*pdev
,
547 const struct hisi_sas_hw
*ops
);
548 extern int hisi_sas_remove(struct platform_device
*pdev
);
550 extern int hisi_sas_slave_configure(struct scsi_device
*sdev
);
551 extern int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
);
552 extern void hisi_sas_scan_start(struct Scsi_Host
*shost
);
553 extern int hisi_sas_host_reset(struct Scsi_Host
*shost
, int reset_type
);
554 extern void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
);
555 extern void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
,
556 struct sas_task
*task
,
557 struct hisi_sas_slot
*slot
);
558 extern void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
);
559 extern void hisi_sas_rst_work_handler(struct work_struct
*work
);
560 extern void hisi_sas_sync_rst_work_handler(struct work_struct
*work
);
561 extern void hisi_sas_kill_tasklets(struct hisi_hba
*hisi_hba
);
562 extern void hisi_sas_phy_oob_ready(struct hisi_hba
*hisi_hba
, int phy_no
);
563 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
564 enum hisi_sas_phy_event event
);
565 extern void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
);
566 extern u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
);
567 extern void hisi_sas_controller_reset_prepare(struct hisi_hba
*hisi_hba
);
568 extern void hisi_sas_controller_reset_done(struct hisi_hba
*hisi_hba
);
569 extern void hisi_sas_debugfs_init(struct hisi_hba
*hisi_hba
);
570 extern void hisi_sas_debugfs_exit(struct hisi_hba
*hisi_hba
);
571 extern void hisi_sas_debugfs_work_handler(struct work_struct
*work
);