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/mfd/syscon.h>
22 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/property.h>
27 #include <linux/regmap.h>
28 #include <scsi/sas_ata.h>
29 #include <scsi/libsas.h>
31 #define HISI_SAS_MAX_PHYS 9
32 #define HISI_SAS_MAX_QUEUES 32
33 #define HISI_SAS_QUEUE_SLOTS 512
34 #define HISI_SAS_MAX_ITCT_ENTRIES 1024
35 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
36 #define HISI_SAS_RESET_BIT 0
37 #define HISI_SAS_REJECT_CMD_BIT 1
38 #define HISI_SAS_RESERVED_IPTT_CNT 96
40 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
41 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
43 #define hisi_sas_status_buf_addr(buf) \
44 (buf + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
45 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr(slot->buf)
46 #define hisi_sas_status_buf_addr_dma(slot) \
47 hisi_sas_status_buf_addr(slot->buf_dma)
49 #define hisi_sas_cmd_hdr_addr(buf) \
50 (buf + offsetof(struct hisi_sas_slot_buf_table, command_header))
51 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr(slot->buf)
52 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr(slot->buf_dma)
54 #define hisi_sas_sge_addr(buf) \
55 (buf + offsetof(struct hisi_sas_slot_buf_table, sge_page))
56 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr(slot->buf)
57 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr(slot->buf_dma)
59 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
60 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
61 #define HISI_SAS_MAX_STP_RESP_SZ 28
63 #define DEV_IS_EXPANDER(type) \
64 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
65 (type == SAS_FANOUT_EXPANDER_DEVICE))
67 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
68 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
69 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
70 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
71 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
73 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
74 SHOST_DIF_TYPE2_PROTECTION | \
75 SHOST_DIF_TYPE3_PROTECTION)
77 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK)
82 PORT_TYPE_SAS = (1U << 1),
83 PORT_TYPE_SATA = (1U << 0),
87 HISI_SAS_INT_ABT_CMD = 0,
88 HISI_SAS_INT_ABT_DEV = 1,
91 enum hisi_sas_dev_type {
92 HISI_SAS_DEV_TYPE_STP = 0,
93 HISI_SAS_DEV_TYPE_SSP,
94 HISI_SAS_DEV_TYPE_SATA,
97 struct hisi_sas_hw_error {
103 const struct hisi_sas_hw_error *sub;
106 struct hisi_sas_rst {
107 struct hisi_hba *hisi_hba;
108 struct completion *completion;
109 struct work_struct work;
113 #define HISI_SAS_RST_WORK_INIT(r, c) \
114 { .hisi_hba = hisi_hba, \
116 .work = __WORK_INITIALIZER(r.work, \
117 hisi_sas_sync_rst_work_handler), \
121 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
122 DECLARE_COMPLETION_ONSTACK(c); \
123 DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \
124 struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)
126 enum hisi_sas_bit_err_type {
127 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
128 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
131 enum hisi_sas_phy_event {
132 HISI_PHYE_PHY_UP = 0U,
133 HISI_PHYE_LINK_RESET,
137 struct hisi_sas_phy {
138 struct work_struct works[HISI_PHYES_NUM];
139 struct hisi_hba *hisi_hba;
140 struct hisi_sas_port *port;
141 struct asd_sas_phy sas_phy;
142 struct sas_identify identify;
143 struct completion *reset_completion;
145 u64 port_id; /* from hw */
152 enum sas_linkrate minimum_linkrate;
153 enum sas_linkrate maximum_linkrate;
156 struct hisi_sas_port {
157 struct asd_sas_port sas_port;
163 struct hisi_hba *hisi_hba;
164 struct tasklet_struct tasklet;
170 struct hisi_hba *hisi_hba;
171 struct list_head list;
177 struct hisi_sas_device {
178 struct hisi_hba *hisi_hba;
179 struct domain_device *sas_device;
180 struct completion *completion;
181 struct hisi_sas_dq *dq;
182 struct list_head list;
183 enum sas_device_type dev_type;
188 struct hisi_sas_tmf_task {
192 u16 tag_of_task_to_be_managed;
195 struct hisi_sas_slot {
196 struct list_head entry;
197 struct list_head delivery;
198 struct sas_task *task;
199 struct hisi_sas_port *port;
202 int dlvry_queue_slot;
204 int cmplt_queue_slot;
208 dma_addr_t cmd_hdr_dma;
209 struct timer_list internal_abort_timer;
211 struct hisi_sas_tmf_task *tmf;
212 /* Do not reorder/change members after here */
218 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
220 struct hisi_sas_debugfs_reg_lu {
225 struct hisi_sas_debugfs_reg {
226 const struct hisi_sas_debugfs_reg_lu *lu;
230 u32 (*read_global_reg)(struct hisi_hba *hisi_hba, u32 off);
231 u32 (*read_port_reg)(struct hisi_hba *hisi_hba, int port,
237 int (*hw_init)(struct hisi_hba *hisi_hba);
238 void (*setup_itct)(struct hisi_hba *hisi_hba,
239 struct hisi_sas_device *device);
240 int (*slot_index_alloc)(struct hisi_hba *hisi_hba,
241 struct domain_device *device);
242 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
243 void (*sl_notify_ssp)(struct hisi_hba *hisi_hba, int phy_no);
244 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
245 void (*start_delivery)(struct hisi_sas_dq *dq);
246 void (*prep_ssp)(struct hisi_hba *hisi_hba,
247 struct hisi_sas_slot *slot);
248 void (*prep_smp)(struct hisi_hba *hisi_hba,
249 struct hisi_sas_slot *slot);
250 void (*prep_stp)(struct hisi_hba *hisi_hba,
251 struct hisi_sas_slot *slot);
252 void (*prep_abort)(struct hisi_hba *hisi_hba,
253 struct hisi_sas_slot *slot,
254 int device_id, int abort_flag, int tag_to_abort);
255 int (*slot_complete)(struct hisi_hba *hisi_hba,
256 struct hisi_sas_slot *slot);
257 void (*phys_init)(struct hisi_hba *hisi_hba);
258 void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
259 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
260 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
261 void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
262 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
263 struct sas_phy_linkrates *linkrates);
264 enum sas_linkrate (*phy_get_max_linkrate)(void);
265 void (*clear_itct)(struct hisi_hba *hisi_hba,
266 struct hisi_sas_device *dev);
267 void (*free_device)(struct hisi_sas_device *sas_dev);
268 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
269 void (*dereg_device)(struct hisi_hba *hisi_hba,
270 struct domain_device *device);
271 int (*soft_reset)(struct hisi_hba *hisi_hba);
272 u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
273 int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type,
274 u8 reg_index, u8 reg_count, u8 *write_data);
275 int (*wait_cmds_complete_timeout)(struct hisi_hba *hisi_hba,
276 int delay_ms, int timeout_ms);
277 void (*snapshot_prepare)(struct hisi_hba *hisi_hba);
278 void (*snapshot_restore)(struct hisi_hba *hisi_hba);
279 int max_command_entries;
280 int complete_hdr_size;
281 struct scsi_host_template *sht;
283 const struct hisi_sas_debugfs_reg *debugfs_reg_global;
284 const struct hisi_sas_debugfs_reg *debugfs_reg_port;
288 /* This must be the first element, used by SHOST_TO_SAS_HA */
289 struct sas_ha_struct *p;
291 struct platform_device *platform_dev;
292 struct pci_dev *pci_dev;
298 void __iomem *sgpio_regs;
301 u32 ctrl_reset_sts_reg;
302 u32 ctrl_clock_ena_reg;
303 u32 refclk_frequency_mhz;
304 u8 sas_addr[SAS_ADDR_SIZE];
308 struct semaphore sem;
310 struct timer_list timer;
311 struct workqueue_struct *wq;
313 int slot_index_count;
316 unsigned long *slot_index_tags;
317 unsigned long reject_stp_links_msk;
320 struct sas_ha_struct sha;
321 struct Scsi_Host *shost;
323 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
324 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
325 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
326 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
330 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
331 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
332 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
333 void *complete_hdr[HISI_SAS_MAX_QUEUES];
334 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
335 struct hisi_sas_initial_fis *initial_fis;
336 dma_addr_t initial_fis_dma;
337 struct hisi_sas_itct *itct;
339 struct hisi_sas_iost *iost;
341 struct hisi_sas_breakpoint *breakpoint;
342 dma_addr_t breakpoint_dma;
343 struct hisi_sas_breakpoint *sata_breakpoint;
344 dma_addr_t sata_breakpoint_dma;
345 struct hisi_sas_slot *slot_info;
347 const struct hisi_sas_hw *hw; /* Low level hw interface */
348 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
349 struct work_struct rst_work;
350 struct work_struct debugfs_work;
352 u32 intr_coal_ticks; /* Time of interrupt coalesce in us */
353 u32 intr_coal_count; /* Interrupt count to coalesce */
355 /* debugfs memories */
356 u32 *debugfs_global_reg;
357 u32 *debugfs_port_reg[HISI_SAS_MAX_PHYS];
358 void *debugfs_complete_hdr[HISI_SAS_MAX_QUEUES];
359 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr[HISI_SAS_MAX_QUEUES];
360 struct hisi_sas_iost *debugfs_iost;
361 struct hisi_sas_itct *debugfs_itct;
363 struct dentry *debugfs_dir;
364 struct dentry *debugfs_dump_dentry;
365 bool debugfs_snapshot;
368 /* Generic HW DMA host memory structures */
369 /* Delivery queue header */
370 struct hisi_sas_cmd_hdr {
381 __le32 transfer_tags;
384 __le32 data_transfer_len;
387 __le32 first_burst_num;
396 __le64 cmd_table_addr;
399 __le64 sts_buffer_addr;
402 __le64 prd_table_addr;
405 __le64 dif_prd_table_addr;
408 struct hisi_sas_itct {
416 struct hisi_sas_iost {
423 struct hisi_sas_err_record {
427 struct hisi_sas_initial_fis {
428 struct hisi_sas_err_record err_record;
429 struct dev_to_host_fis fis;
433 struct hisi_sas_breakpoint {
437 struct hisi_sas_sata_breakpoint {
438 struct hisi_sas_breakpoint tag[32];
441 struct hisi_sas_sge {
449 struct hisi_sas_command_table_smp {
453 struct hisi_sas_command_table_stp {
454 struct host_to_dev_fis command_fis;
456 u8 atapi_cdb[ATAPI_CDB_LEN];
459 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
460 struct hisi_sas_sge_page {
461 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
464 struct hisi_sas_command_table_ssp {
465 struct ssp_frame_hdr hdr;
468 struct ssp_command_iu task;
471 struct ssp_tmf_iu ssp_task;
472 struct xfer_rdy_iu xfer_rdy;
473 struct ssp_response_iu ssp_res;
477 union hisi_sas_command_table {
478 struct hisi_sas_command_table_ssp ssp;
479 struct hisi_sas_command_table_smp smp;
480 struct hisi_sas_command_table_stp stp;
483 struct hisi_sas_status_buffer {
484 struct hisi_sas_err_record err;
488 struct hisi_sas_slot_buf_table {
489 struct hisi_sas_status_buffer status_buffer;
490 union hisi_sas_command_table command_header;
491 struct hisi_sas_sge_page sge_page;
494 extern struct scsi_transport_template *hisi_sas_stt;
496 extern bool hisi_sas_debugfs_enable;
497 extern struct dentry *hisi_sas_debugfs_dir;
499 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
500 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost);
501 extern void hisi_sas_free(struct hisi_hba *hisi_hba);
502 extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis,
504 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
505 extern void hisi_sas_sata_done(struct sas_task *task,
506 struct hisi_sas_slot *slot);
507 extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
508 extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
509 extern int hisi_sas_probe(struct platform_device *pdev,
510 const struct hisi_sas_hw *ops);
511 extern int hisi_sas_remove(struct platform_device *pdev);
513 extern int hisi_sas_slave_configure(struct scsi_device *sdev);
514 extern int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time);
515 extern void hisi_sas_scan_start(struct Scsi_Host *shost);
516 extern int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type);
517 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
518 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
519 struct sas_task *task,
520 struct hisi_sas_slot *slot);
521 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
522 extern void hisi_sas_rst_work_handler(struct work_struct *work);
523 extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
524 extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
525 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
526 enum hisi_sas_phy_event event);
527 extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba);
528 extern u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max);
529 extern void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba);
530 extern void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba);
531 extern void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba);
532 extern void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba);
533 extern void hisi_sas_debugfs_work_handler(struct work_struct *work);