Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / scsi / hisi_sas / hisi_sas.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (c) 2015 Linaro Ltd.
4  * Copyright (c) 2015 Hisilicon Limited.
5  */
6
7 #ifndef _HISI_SAS_H_
8 #define _HISI_SAS_H_
9
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>
26
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
35
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))
38
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)
44
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)
49
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)
54
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)
59
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
63
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
69
70 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
71                                 SHOST_DIF_TYPE2_PROTECTION | \
72                                 SHOST_DIF_TYPE3_PROTECTION)
73
74 #define HISI_SAS_DIX_PROT_MASK (SHOST_DIX_TYPE1_PROTECTION | \
75                                 SHOST_DIX_TYPE2_PROTECTION | \
76                                 SHOST_DIX_TYPE3_PROTECTION)
77
78 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK | HISI_SAS_DIX_PROT_MASK)
79
80 #define HISI_SAS_WAIT_PHYUP_TIMEOUT 20
81
82 struct hisi_hba;
83
84 enum {
85         PORT_TYPE_SAS = (1U << 1),
86         PORT_TYPE_SATA = (1U << 0),
87 };
88
89 enum dev_status {
90         HISI_SAS_DEV_INIT,
91         HISI_SAS_DEV_NORMAL,
92 };
93
94 enum {
95         HISI_SAS_INT_ABT_CMD = 0,
96         HISI_SAS_INT_ABT_DEV = 1,
97 };
98
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,
103 };
104
105 struct hisi_sas_hw_error {
106         u32 irq_msk;
107         u32 msk;
108         int shift;
109         const char *msg;
110         int reg;
111         const struct hisi_sas_hw_error *sub;
112 };
113
114 struct hisi_sas_rst {
115         struct hisi_hba *hisi_hba;
116         struct completion *completion;
117         struct work_struct work;
118         bool done;
119 };
120
121 #define HISI_SAS_RST_WORK_INIT(r, c) \
122         {       .hisi_hba = hisi_hba, \
123                 .completion = &c, \
124                 .work = __WORK_INITIALIZER(r.work, \
125                                 hisi_sas_sync_rst_work_handler), \
126                 .done = false, \
127                 }
128
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)
133
134 enum hisi_sas_bit_err_type {
135         HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
136         HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
137 };
138
139 enum hisi_sas_phy_event {
140         HISI_PHYE_PHY_UP   = 0U,
141         HISI_PHYE_LINK_RESET,
142         HISI_PHYES_NUM,
143 };
144
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;
153         spinlock_t lock;
154         u64             port_id; /* from hw */
155         u64             frame_rcvd_size;
156         u8              frame_rcvd[32];
157         u8              phy_attached;
158         u8              in_reset;
159         u8              reserved[2];
160         u32             phy_type;
161         u32             code_violation_err_count;
162         enum sas_linkrate       minimum_linkrate;
163         enum sas_linkrate       maximum_linkrate;
164         int enable;
165 };
166
167 struct hisi_sas_port {
168         struct asd_sas_port     sas_port;
169         u8      port_attached;
170         u8      id; /* from hw */
171 };
172
173 struct hisi_sas_cq {
174         struct hisi_hba *hisi_hba;
175         const struct cpumask *pci_irq_mask;
176         struct tasklet_struct tasklet;
177         int     rd_point;
178         int     id;
179 };
180
181 struct hisi_sas_dq {
182         struct hisi_hba *hisi_hba;
183         struct list_head list;
184         spinlock_t lock;
185         int     wr_point;
186         int     id;
187 };
188
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;
197         int device_id;
198         int sata_idx;
199         spinlock_t lock; /* For protecting slots */
200 };
201
202 struct hisi_sas_tmf_task {
203         int force_phy;
204         int phy_id;
205         u8 tmf;
206         u16 tag_of_task_to_be_managed;
207 };
208
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;
214         u64     n_elem;
215         u64     n_elem_dif;
216         int     dlvry_queue;
217         int     dlvry_queue_slot;
218         int     cmplt_queue;
219         int     cmplt_queue_slot;
220         int     abort;
221         int     ready;
222         int     device_id;
223         void    *cmd_hdr;
224         dma_addr_t cmd_hdr_dma;
225         struct timer_list internal_abort_timer;
226         bool is_internal;
227         struct hisi_sas_tmf_task *tmf;
228         /* Do not reorder/change members after here */
229         void    *buf;
230         dma_addr_t buf_dma;
231         u16     idx;
232 };
233
234 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
235
236 struct hisi_sas_debugfs_reg_lu {
237         char *name;
238         int off;
239 };
240
241 struct hisi_sas_debugfs_reg {
242         const struct hisi_sas_debugfs_reg_lu *lu;
243         int count;
244         int base_off;
245         union {
246                 u32 (*read_global_reg)(struct hisi_hba *hisi_hba, u32 off);
247                 u32 (*read_port_reg)(struct hisi_hba *hisi_hba, int port,
248                                      u32 off);
249         };
250 };
251
252 struct hisi_sas_hw {
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;
298
299         const struct hisi_sas_debugfs_reg *debugfs_reg_global;
300         const struct hisi_sas_debugfs_reg *debugfs_reg_port;
301 };
302
303 struct hisi_hba {
304         /* This must be the first element, used by SHOST_TO_SAS_HA */
305         struct sas_ha_struct *p;
306
307         struct platform_device *platform_dev;
308         struct pci_dev *pci_dev;
309         struct device *dev;
310
311         int prot_mask;
312
313         void __iomem *regs;
314         void __iomem *sgpio_regs;
315         struct regmap *ctrl;
316         u32 ctrl_reset_reg;
317         u32 ctrl_reset_sts_reg;
318         u32 ctrl_clock_ena_reg;
319         u32 refclk_frequency_mhz;
320         u8 sas_addr[SAS_ADDR_SIZE];
321
322         int n_phy;
323         spinlock_t lock;
324         struct semaphore sem;
325
326         struct timer_list timer;
327         struct workqueue_struct *wq;
328
329         int slot_index_count;
330         int last_slot_index;
331         int last_dev_id;
332         unsigned long *slot_index_tags;
333         unsigned long reject_stp_links_msk;
334
335         /* SCSI/SAS glue */
336         struct sas_ha_struct sha;
337         struct Scsi_Host *shost;
338
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];
343
344         int     queue_count;
345
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;
354         dma_addr_t itct_dma;
355         struct hisi_sas_iost *iost;
356         dma_addr_t iost_dma;
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;
362         unsigned long flags;
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;
367         u32 phy_state;
368         u32 intr_coal_ticks;    /* Time of interrupt coalesce in us */
369         u32 intr_coal_count;    /* Interrupt count to coalesce */
370
371         int cq_nvecs;
372         unsigned int *reply_map;
373
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;
381
382         struct dentry *debugfs_dir;
383         struct dentry *debugfs_dump_dentry;
384         bool debugfs_snapshot;
385 };
386
387 /* Generic HW DMA host memory structures */
388 /* Delivery queue header */
389 struct hisi_sas_cmd_hdr {
390         /* dw0 */
391         __le32 dw0;
392
393         /* dw1 */
394         __le32 dw1;
395
396         /* dw2 */
397         __le32 dw2;
398
399         /* dw3 */
400         __le32 transfer_tags;
401
402         /* dw4 */
403         __le32 data_transfer_len;
404
405         /* dw5 */
406         __le32 first_burst_num;
407
408         /* dw6 */
409         __le32 sg_len;
410
411         /* dw7 */
412         __le32 dw7;
413
414         /* dw8-9 */
415         __le64 cmd_table_addr;
416
417         /* dw10-11 */
418         __le64 sts_buffer_addr;
419
420         /* dw12-13 */
421         __le64 prd_table_addr;
422
423         /* dw14-15 */
424         __le64 dif_prd_table_addr;
425 };
426
427 struct hisi_sas_itct {
428         __le64 qw0;
429         __le64 sas_addr;
430         __le64 qw2;
431         __le64 qw3;
432         __le64 qw4_15[12];
433 };
434
435 struct hisi_sas_iost {
436         __le64 qw0;
437         __le64 qw1;
438         __le64 qw2;
439         __le64 qw3;
440 };
441
442 struct hisi_sas_err_record {
443         u32     data[4];
444 };
445
446 struct hisi_sas_initial_fis {
447         struct hisi_sas_err_record err_record;
448         struct dev_to_host_fis fis;
449         u32 rsvd[3];
450 };
451
452 struct hisi_sas_breakpoint {
453         u8      data[128];
454 };
455
456 struct hisi_sas_sata_breakpoint {
457         struct hisi_sas_breakpoint tag[32];
458 };
459
460 struct hisi_sas_sge {
461         __le64 addr;
462         __le32 page_ctrl_0;
463         __le32 page_ctrl_1;
464         __le32 data_len;
465         __le32 data_off;
466 };
467
468 struct hisi_sas_command_table_smp {
469         u8 bytes[44];
470 };
471
472 struct hisi_sas_command_table_stp {
473         struct  host_to_dev_fis command_fis;
474         u8      dummy[12];
475         u8      atapi_cdb[ATAPI_CDB_LEN];
476 };
477
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];
481 }  __aligned(16);
482
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];
486 }  __aligned(16);
487
488 struct hisi_sas_command_table_ssp {
489         struct ssp_frame_hdr hdr;
490         union {
491                 struct {
492                         struct ssp_command_iu task;
493                         u32 prot[7];
494                 };
495                 struct ssp_tmf_iu ssp_task;
496                 struct xfer_rdy_iu xfer_rdy;
497                 struct ssp_response_iu ssp_res;
498         } u;
499 };
500
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;
505 }  __aligned(16);
506
507 struct hisi_sas_status_buffer {
508         struct hisi_sas_err_record err;
509         u8      iu[1024];
510 }  __aligned(16);
511
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;
516 };
517
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;
521 };
522
523 extern struct scsi_transport_template *hisi_sas_stt;
524
525 extern bool hisi_sas_debugfs_enable;
526 extern struct dentry *hisi_sas_debugfs_dir;
527
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,
532                                 int direction);
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);
541
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,
547                                 int enable);
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);
566 #endif