]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/hisi_sas/hisi_sas.h
235b81998c9abdfdedf0942a6a0a65e63d2e2f33
[linux.git] / drivers / scsi / hisi_sas / hisi_sas.h
1 /*
2  * Copyright (c) 2015 Linaro Ltd.
3  * Copyright (c) 2015 Hisilicon Limited.
4  *
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.
9  *
10  */
11
12 #ifndef _HISI_SAS_H_
13 #define _HISI_SAS_H_
14
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>
30
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
39
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))
42
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)
48
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)
53
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)
58
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
62
63 #define DEV_IS_EXPANDER(type) \
64         ((type == SAS_EDGE_EXPANDER_DEVICE) || \
65         (type == SAS_FANOUT_EXPANDER_DEVICE))
66
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
72
73 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
74                                 SHOST_DIF_TYPE2_PROTECTION | \
75                                 SHOST_DIF_TYPE3_PROTECTION)
76
77 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK)
78
79 struct hisi_hba;
80
81 enum {
82         PORT_TYPE_SAS = (1U << 1),
83         PORT_TYPE_SATA = (1U << 0),
84 };
85
86 enum {
87         HISI_SAS_INT_ABT_CMD = 0,
88         HISI_SAS_INT_ABT_DEV = 1,
89 };
90
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,
95 };
96
97 struct hisi_sas_hw_error {
98         u32 irq_msk;
99         u32 msk;
100         int shift;
101         const char *msg;
102         int reg;
103         const struct hisi_sas_hw_error *sub;
104 };
105
106 struct hisi_sas_rst {
107         struct hisi_hba *hisi_hba;
108         struct completion *completion;
109         struct work_struct work;
110         bool done;
111 };
112
113 #define HISI_SAS_RST_WORK_INIT(r, c) \
114         {       .hisi_hba = hisi_hba, \
115                 .completion = &c, \
116                 .work = __WORK_INITIALIZER(r.work, \
117                                 hisi_sas_sync_rst_work_handler), \
118                 .done = false, \
119                 }
120
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)
125
126 enum hisi_sas_bit_err_type {
127         HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
128         HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
129 };
130
131 enum hisi_sas_phy_event {
132         HISI_PHYE_PHY_UP   = 0U,
133         HISI_PHYE_LINK_RESET,
134         HISI_PHYES_NUM,
135 };
136
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;
144         spinlock_t lock;
145         u64             port_id; /* from hw */
146         u64             frame_rcvd_size;
147         u8              frame_rcvd[32];
148         u8              phy_attached;
149         u8              in_reset;
150         u8              reserved[2];
151         u32             phy_type;
152         enum sas_linkrate       minimum_linkrate;
153         enum sas_linkrate       maximum_linkrate;
154 };
155
156 struct hisi_sas_port {
157         struct asd_sas_port     sas_port;
158         u8      port_attached;
159         u8      id; /* from hw */
160 };
161
162 struct hisi_sas_cq {
163         struct hisi_hba *hisi_hba;
164         struct tasklet_struct tasklet;
165         int     rd_point;
166         int     id;
167 };
168
169 struct hisi_sas_dq {
170         struct hisi_hba *hisi_hba;
171         struct list_head list;
172         spinlock_t lock;
173         int     wr_point;
174         int     id;
175 };
176
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;
184         int device_id;
185         int sata_idx;
186 };
187
188 struct hisi_sas_tmf_task {
189         int force_phy;
190         int phy_id;
191         u8 tmf;
192         u16 tag_of_task_to_be_managed;
193 };
194
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;
200         u64     n_elem;
201         int     dlvry_queue;
202         int     dlvry_queue_slot;
203         int     cmplt_queue;
204         int     cmplt_queue_slot;
205         int     abort;
206         int     ready;
207         void    *cmd_hdr;
208         dma_addr_t cmd_hdr_dma;
209         struct timer_list internal_abort_timer;
210         bool is_internal;
211         struct hisi_sas_tmf_task *tmf;
212         /* Do not reorder/change members after here */
213         void    *buf;
214         dma_addr_t buf_dma;
215         u16     idx;
216 };
217
218 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
219
220 struct hisi_sas_debugfs_reg_lu {
221         char *name;
222         int off;
223 };
224
225 struct hisi_sas_debugfs_reg {
226         const struct hisi_sas_debugfs_reg_lu *lu;
227         int count;
228         int base_off;
229         union {
230                 u32 (*read_global_reg)(struct hisi_hba *hisi_hba, u32 off);
231                 u32 (*read_port_reg)(struct hisi_hba *hisi_hba, int port,
232                                      u32 off);
233         };
234 };
235
236 struct hisi_sas_hw {
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;
282
283         const struct hisi_sas_debugfs_reg *debugfs_reg_global;
284         const struct hisi_sas_debugfs_reg *debugfs_reg_port;
285 };
286
287 struct hisi_hba {
288         /* This must be the first element, used by SHOST_TO_SAS_HA */
289         struct sas_ha_struct *p;
290
291         struct platform_device *platform_dev;
292         struct pci_dev *pci_dev;
293         struct device *dev;
294
295         int prot_mask;
296
297         void __iomem *regs;
298         void __iomem *sgpio_regs;
299         struct regmap *ctrl;
300         u32 ctrl_reset_reg;
301         u32 ctrl_reset_sts_reg;
302         u32 ctrl_clock_ena_reg;
303         u32 refclk_frequency_mhz;
304         u8 sas_addr[SAS_ADDR_SIZE];
305
306         int n_phy;
307         spinlock_t lock;
308         struct semaphore sem;
309
310         struct timer_list timer;
311         struct workqueue_struct *wq;
312
313         int slot_index_count;
314         int last_slot_index;
315         int last_dev_id;
316         unsigned long *slot_index_tags;
317         unsigned long reject_stp_links_msk;
318
319         /* SCSI/SAS glue */
320         struct sas_ha_struct sha;
321         struct Scsi_Host *shost;
322
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];
327
328         int     queue_count;
329
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;
338         dma_addr_t itct_dma;
339         struct hisi_sas_iost *iost;
340         dma_addr_t iost_dma;
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;
346         unsigned long flags;
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;
351         u32 phy_state;
352         u32 intr_coal_ticks;    /* Time of interrupt coalesce in us */
353         u32 intr_coal_count;    /* Interrupt count to coalesce */
354
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;
362
363         struct dentry *debugfs_dir;
364         struct dentry *debugfs_dump_dentry;
365         bool debugfs_snapshot;
366 };
367
368 /* Generic HW DMA host memory structures */
369 /* Delivery queue header */
370 struct hisi_sas_cmd_hdr {
371         /* dw0 */
372         __le32 dw0;
373
374         /* dw1 */
375         __le32 dw1;
376
377         /* dw2 */
378         __le32 dw2;
379
380         /* dw3 */
381         __le32 transfer_tags;
382
383         /* dw4 */
384         __le32 data_transfer_len;
385
386         /* dw5 */
387         __le32 first_burst_num;
388
389         /* dw6 */
390         __le32 sg_len;
391
392         /* dw7 */
393         __le32 dw7;
394
395         /* dw8-9 */
396         __le64 cmd_table_addr;
397
398         /* dw10-11 */
399         __le64 sts_buffer_addr;
400
401         /* dw12-13 */
402         __le64 prd_table_addr;
403
404         /* dw14-15 */
405         __le64 dif_prd_table_addr;
406 };
407
408 struct hisi_sas_itct {
409         __le64 qw0;
410         __le64 sas_addr;
411         __le64 qw2;
412         __le64 qw3;
413         __le64 qw4_15[12];
414 };
415
416 struct hisi_sas_iost {
417         __le64 qw0;
418         __le64 qw1;
419         __le64 qw2;
420         __le64 qw3;
421 };
422
423 struct hisi_sas_err_record {
424         u32     data[4];
425 };
426
427 struct hisi_sas_initial_fis {
428         struct hisi_sas_err_record err_record;
429         struct dev_to_host_fis fis;
430         u32 rsvd[3];
431 };
432
433 struct hisi_sas_breakpoint {
434         u8      data[128];
435 };
436
437 struct hisi_sas_sata_breakpoint {
438         struct hisi_sas_breakpoint tag[32];
439 };
440
441 struct hisi_sas_sge {
442         __le64 addr;
443         __le32 page_ctrl_0;
444         __le32 page_ctrl_1;
445         __le32 data_len;
446         __le32 data_off;
447 };
448
449 struct hisi_sas_command_table_smp {
450         u8 bytes[44];
451 };
452
453 struct hisi_sas_command_table_stp {
454         struct  host_to_dev_fis command_fis;
455         u8      dummy[12];
456         u8      atapi_cdb[ATAPI_CDB_LEN];
457 };
458
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];
462 }  __aligned(16);
463
464 struct hisi_sas_command_table_ssp {
465         struct ssp_frame_hdr hdr;
466         union {
467                 struct {
468                         struct ssp_command_iu task;
469                         u32 prot[7];
470                 };
471                 struct ssp_tmf_iu ssp_task;
472                 struct xfer_rdy_iu xfer_rdy;
473                 struct ssp_response_iu ssp_res;
474         } u;
475 };
476
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;
481 }  __aligned(16);
482
483 struct hisi_sas_status_buffer {
484         struct hisi_sas_err_record err;
485         u8      iu[1024];
486 }  __aligned(16);
487
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;
492 };
493
494 extern struct scsi_transport_template *hisi_sas_stt;
495
496 extern bool hisi_sas_debugfs_enable;
497 extern struct dentry *hisi_sas_debugfs_dir;
498
499 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
500 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba);
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,
503                                 int direction);
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);
512
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);
534 #endif