scsi: libsas: aic94xx: hisi_sas: mvsas: pm8001: Use dev_is_expander()
[linux-2.6-microblaze.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/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>
31
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
40
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))
43
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)
49
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)
54
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)
59
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)
64
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
68
69 #define HISI_SAS_SATA_PROTOCOL_NONDATA          0x1
70 #define HISI_SAS_SATA_PROTOCOL_PIO                      0x2
71 #define HISI_SAS_SATA_PROTOCOL_DMA                      0x4
72 #define HISI_SAS_SATA_PROTOCOL_FPDMA            0x8
73 #define HISI_SAS_SATA_PROTOCOL_ATAPI            0x10
74
75 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \
76                                 SHOST_DIF_TYPE2_PROTECTION | \
77                                 SHOST_DIF_TYPE3_PROTECTION)
78
79 #define HISI_SAS_DIX_PROT_MASK (SHOST_DIX_TYPE1_PROTECTION | \
80                                 SHOST_DIX_TYPE2_PROTECTION | \
81                                 SHOST_DIX_TYPE3_PROTECTION)
82
83 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK | HISI_SAS_DIX_PROT_MASK)
84
85 #define HISI_SAS_WAIT_PHYUP_TIMEOUT 20
86
87 struct hisi_hba;
88
89 enum {
90         PORT_TYPE_SAS = (1U << 1),
91         PORT_TYPE_SATA = (1U << 0),
92 };
93
94 enum dev_status {
95         HISI_SAS_DEV_INIT,
96         HISI_SAS_DEV_NORMAL,
97 };
98
99 enum {
100         HISI_SAS_INT_ABT_CMD = 0,
101         HISI_SAS_INT_ABT_DEV = 1,
102 };
103
104 enum hisi_sas_dev_type {
105         HISI_SAS_DEV_TYPE_STP = 0,
106         HISI_SAS_DEV_TYPE_SSP,
107         HISI_SAS_DEV_TYPE_SATA,
108 };
109
110 struct hisi_sas_hw_error {
111         u32 irq_msk;
112         u32 msk;
113         int shift;
114         const char *msg;
115         int reg;
116         const struct hisi_sas_hw_error *sub;
117 };
118
119 struct hisi_sas_rst {
120         struct hisi_hba *hisi_hba;
121         struct completion *completion;
122         struct work_struct work;
123         bool done;
124 };
125
126 #define HISI_SAS_RST_WORK_INIT(r, c) \
127         {       .hisi_hba = hisi_hba, \
128                 .completion = &c, \
129                 .work = __WORK_INITIALIZER(r.work, \
130                                 hisi_sas_sync_rst_work_handler), \
131                 .done = false, \
132                 }
133
134 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
135         DECLARE_COMPLETION_ONSTACK(c); \
136         DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \
137         struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)
138
139 enum hisi_sas_bit_err_type {
140         HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
141         HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
142 };
143
144 enum hisi_sas_phy_event {
145         HISI_PHYE_PHY_UP   = 0U,
146         HISI_PHYE_LINK_RESET,
147         HISI_PHYES_NUM,
148 };
149
150 struct hisi_sas_phy {
151         struct work_struct      works[HISI_PHYES_NUM];
152         struct hisi_hba *hisi_hba;
153         struct hisi_sas_port    *port;
154         struct asd_sas_phy      sas_phy;
155         struct sas_identify     identify;
156         struct completion *reset_completion;
157         struct timer_list timer;
158         spinlock_t lock;
159         u64             port_id; /* from hw */
160         u64             frame_rcvd_size;
161         u8              frame_rcvd[32];
162         u8              phy_attached;
163         u8              in_reset;
164         u8              reserved[2];
165         u32             phy_type;
166         u32             code_violation_err_count;
167         enum sas_linkrate       minimum_linkrate;
168         enum sas_linkrate       maximum_linkrate;
169         int enable;
170 };
171
172 struct hisi_sas_port {
173         struct asd_sas_port     sas_port;
174         u8      port_attached;
175         u8      id; /* from hw */
176 };
177
178 struct hisi_sas_cq {
179         struct hisi_hba *hisi_hba;
180         const struct cpumask *pci_irq_mask;
181         struct tasklet_struct tasklet;
182         int     rd_point;
183         int     id;
184 };
185
186 struct hisi_sas_dq {
187         struct hisi_hba *hisi_hba;
188         struct list_head list;
189         spinlock_t lock;
190         int     wr_point;
191         int     id;
192 };
193
194 struct hisi_sas_device {
195         struct hisi_hba         *hisi_hba;
196         struct domain_device    *sas_device;
197         struct completion *completion;
198         struct hisi_sas_dq      *dq;
199         struct list_head        list;
200         enum sas_device_type    dev_type;
201         enum dev_status dev_status;
202         int device_id;
203         int sata_idx;
204         spinlock_t lock; /* For protecting slots */
205 };
206
207 struct hisi_sas_tmf_task {
208         int force_phy;
209         int phy_id;
210         u8 tmf;
211         u16 tag_of_task_to_be_managed;
212 };
213
214 struct hisi_sas_slot {
215         struct list_head entry;
216         struct list_head delivery;
217         struct sas_task *task;
218         struct hisi_sas_port    *port;
219         u64     n_elem;
220         u64     n_elem_dif;
221         int     dlvry_queue;
222         int     dlvry_queue_slot;
223         int     cmplt_queue;
224         int     cmplt_queue_slot;
225         int     abort;
226         int     ready;
227         int     device_id;
228         void    *cmd_hdr;
229         dma_addr_t cmd_hdr_dma;
230         struct timer_list internal_abort_timer;
231         bool is_internal;
232         struct hisi_sas_tmf_task *tmf;
233         /* Do not reorder/change members after here */
234         void    *buf;
235         dma_addr_t buf_dma;
236         u16     idx;
237 };
238
239 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
240
241 struct hisi_sas_debugfs_reg_lu {
242         char *name;
243         int off;
244 };
245
246 struct hisi_sas_debugfs_reg {
247         const struct hisi_sas_debugfs_reg_lu *lu;
248         int count;
249         int base_off;
250         union {
251                 u32 (*read_global_reg)(struct hisi_hba *hisi_hba, u32 off);
252                 u32 (*read_port_reg)(struct hisi_hba *hisi_hba, int port,
253                                      u32 off);
254         };
255 };
256
257 struct hisi_sas_hw {
258         int (*hw_init)(struct hisi_hba *hisi_hba);
259         void (*setup_itct)(struct hisi_hba *hisi_hba,
260                            struct hisi_sas_device *device);
261         int (*slot_index_alloc)(struct hisi_hba *hisi_hba,
262                                 struct domain_device *device);
263         struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
264         void (*sl_notify_ssp)(struct hisi_hba *hisi_hba, int phy_no);
265         int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
266         void (*start_delivery)(struct hisi_sas_dq *dq);
267         void (*prep_ssp)(struct hisi_hba *hisi_hba,
268                         struct hisi_sas_slot *slot);
269         void (*prep_smp)(struct hisi_hba *hisi_hba,
270                         struct hisi_sas_slot *slot);
271         void (*prep_stp)(struct hisi_hba *hisi_hba,
272                         struct hisi_sas_slot *slot);
273         void (*prep_abort)(struct hisi_hba *hisi_hba,
274                           struct hisi_sas_slot *slot,
275                           int device_id, int abort_flag, int tag_to_abort);
276         int (*slot_complete)(struct hisi_hba *hisi_hba,
277                              struct hisi_sas_slot *slot);
278         void (*phys_init)(struct hisi_hba *hisi_hba);
279         void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
280         void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
281         void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
282         void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
283         void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
284                         struct sas_phy_linkrates *linkrates);
285         enum sas_linkrate (*phy_get_max_linkrate)(void);
286         void (*clear_itct)(struct hisi_hba *hisi_hba,
287                             struct hisi_sas_device *dev);
288         void (*free_device)(struct hisi_sas_device *sas_dev);
289         int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
290         void (*dereg_device)(struct hisi_hba *hisi_hba,
291                                 struct domain_device *device);
292         int (*soft_reset)(struct hisi_hba *hisi_hba);
293         u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
294         int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type,
295                                 u8 reg_index, u8 reg_count, u8 *write_data);
296         int (*wait_cmds_complete_timeout)(struct hisi_hba *hisi_hba,
297                                           int delay_ms, int timeout_ms);
298         void (*snapshot_prepare)(struct hisi_hba *hisi_hba);
299         void (*snapshot_restore)(struct hisi_hba *hisi_hba);
300         int max_command_entries;
301         int complete_hdr_size;
302         struct scsi_host_template *sht;
303
304         const struct hisi_sas_debugfs_reg *debugfs_reg_global;
305         const struct hisi_sas_debugfs_reg *debugfs_reg_port;
306 };
307
308 struct hisi_hba {
309         /* This must be the first element, used by SHOST_TO_SAS_HA */
310         struct sas_ha_struct *p;
311
312         struct platform_device *platform_dev;
313         struct pci_dev *pci_dev;
314         struct device *dev;
315
316         int prot_mask;
317
318         void __iomem *regs;
319         void __iomem *sgpio_regs;
320         struct regmap *ctrl;
321         u32 ctrl_reset_reg;
322         u32 ctrl_reset_sts_reg;
323         u32 ctrl_clock_ena_reg;
324         u32 refclk_frequency_mhz;
325         u8 sas_addr[SAS_ADDR_SIZE];
326
327         int n_phy;
328         spinlock_t lock;
329         struct semaphore sem;
330
331         struct timer_list timer;
332         struct workqueue_struct *wq;
333
334         int slot_index_count;
335         int last_slot_index;
336         int last_dev_id;
337         unsigned long *slot_index_tags;
338         unsigned long reject_stp_links_msk;
339
340         /* SCSI/SAS glue */
341         struct sas_ha_struct sha;
342         struct Scsi_Host *shost;
343
344         struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
345         struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
346         struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
347         struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
348
349         int     queue_count;
350
351         struct hisi_sas_device  devices[HISI_SAS_MAX_DEVICES];
352         struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
353         dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
354         void *complete_hdr[HISI_SAS_MAX_QUEUES];
355         dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
356         struct hisi_sas_initial_fis *initial_fis;
357         dma_addr_t initial_fis_dma;
358         struct hisi_sas_itct *itct;
359         dma_addr_t itct_dma;
360         struct hisi_sas_iost *iost;
361         dma_addr_t iost_dma;
362         struct hisi_sas_breakpoint *breakpoint;
363         dma_addr_t breakpoint_dma;
364         struct hisi_sas_breakpoint *sata_breakpoint;
365         dma_addr_t sata_breakpoint_dma;
366         struct hisi_sas_slot    *slot_info;
367         unsigned long flags;
368         const struct hisi_sas_hw *hw;   /* Low level hw interface */
369         unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
370         struct work_struct rst_work;
371         struct work_struct debugfs_work;
372         u32 phy_state;
373         u32 intr_coal_ticks;    /* Time of interrupt coalesce in us */
374         u32 intr_coal_count;    /* Interrupt count to coalesce */
375
376         int cq_nvecs;
377         unsigned int *reply_map;
378
379         /* debugfs memories */
380         u32 *debugfs_global_reg;
381         u32 *debugfs_port_reg[HISI_SAS_MAX_PHYS];
382         void *debugfs_complete_hdr[HISI_SAS_MAX_QUEUES];
383         struct hisi_sas_cmd_hdr *debugfs_cmd_hdr[HISI_SAS_MAX_QUEUES];
384         struct hisi_sas_iost *debugfs_iost;
385         struct hisi_sas_itct *debugfs_itct;
386
387         struct dentry *debugfs_dir;
388         struct dentry *debugfs_dump_dentry;
389         bool debugfs_snapshot;
390 };
391
392 /* Generic HW DMA host memory structures */
393 /* Delivery queue header */
394 struct hisi_sas_cmd_hdr {
395         /* dw0 */
396         __le32 dw0;
397
398         /* dw1 */
399         __le32 dw1;
400
401         /* dw2 */
402         __le32 dw2;
403
404         /* dw3 */
405         __le32 transfer_tags;
406
407         /* dw4 */
408         __le32 data_transfer_len;
409
410         /* dw5 */
411         __le32 first_burst_num;
412
413         /* dw6 */
414         __le32 sg_len;
415
416         /* dw7 */
417         __le32 dw7;
418
419         /* dw8-9 */
420         __le64 cmd_table_addr;
421
422         /* dw10-11 */
423         __le64 sts_buffer_addr;
424
425         /* dw12-13 */
426         __le64 prd_table_addr;
427
428         /* dw14-15 */
429         __le64 dif_prd_table_addr;
430 };
431
432 struct hisi_sas_itct {
433         __le64 qw0;
434         __le64 sas_addr;
435         __le64 qw2;
436         __le64 qw3;
437         __le64 qw4_15[12];
438 };
439
440 struct hisi_sas_iost {
441         __le64 qw0;
442         __le64 qw1;
443         __le64 qw2;
444         __le64 qw3;
445 };
446
447 struct hisi_sas_err_record {
448         u32     data[4];
449 };
450
451 struct hisi_sas_initial_fis {
452         struct hisi_sas_err_record err_record;
453         struct dev_to_host_fis fis;
454         u32 rsvd[3];
455 };
456
457 struct hisi_sas_breakpoint {
458         u8      data[128];
459 };
460
461 struct hisi_sas_sata_breakpoint {
462         struct hisi_sas_breakpoint tag[32];
463 };
464
465 struct hisi_sas_sge {
466         __le64 addr;
467         __le32 page_ctrl_0;
468         __le32 page_ctrl_1;
469         __le32 data_len;
470         __le32 data_off;
471 };
472
473 struct hisi_sas_command_table_smp {
474         u8 bytes[44];
475 };
476
477 struct hisi_sas_command_table_stp {
478         struct  host_to_dev_fis command_fis;
479         u8      dummy[12];
480         u8      atapi_cdb[ATAPI_CDB_LEN];
481 };
482
483 #define HISI_SAS_SGE_PAGE_CNT (124)
484 struct hisi_sas_sge_page {
485         struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
486 }  __aligned(16);
487
488 #define HISI_SAS_SGE_DIF_PAGE_CNT   HISI_SAS_SGE_PAGE_CNT
489 struct hisi_sas_sge_dif_page {
490         struct hisi_sas_sge sge[HISI_SAS_SGE_DIF_PAGE_CNT];
491 }  __aligned(16);
492
493 struct hisi_sas_command_table_ssp {
494         struct ssp_frame_hdr hdr;
495         union {
496                 struct {
497                         struct ssp_command_iu task;
498                         u32 prot[7];
499                 };
500                 struct ssp_tmf_iu ssp_task;
501                 struct xfer_rdy_iu xfer_rdy;
502                 struct ssp_response_iu ssp_res;
503         } u;
504 };
505
506 union hisi_sas_command_table {
507         struct hisi_sas_command_table_ssp ssp;
508         struct hisi_sas_command_table_smp smp;
509         struct hisi_sas_command_table_stp stp;
510 }  __aligned(16);
511
512 struct hisi_sas_status_buffer {
513         struct hisi_sas_err_record err;
514         u8      iu[1024];
515 }  __aligned(16);
516
517 struct hisi_sas_slot_buf_table {
518         struct hisi_sas_status_buffer status_buffer;
519         union hisi_sas_command_table command_header;
520         struct hisi_sas_sge_page sge_page;
521 };
522
523 struct hisi_sas_slot_dif_buf_table {
524         struct hisi_sas_slot_buf_table slot_buf;
525         struct hisi_sas_sge_dif_page sge_dif_page;
526 };
527
528 extern struct scsi_transport_template *hisi_sas_stt;
529
530 extern bool hisi_sas_debugfs_enable;
531 extern struct dentry *hisi_sas_debugfs_dir;
532
533 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
534 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba);
535 extern void hisi_sas_free(struct hisi_hba *hisi_hba);
536 extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis,
537                                 int direction);
538 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
539 extern void hisi_sas_sata_done(struct sas_task *task,
540                             struct hisi_sas_slot *slot);
541 extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
542 extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
543 extern int hisi_sas_probe(struct platform_device *pdev,
544                           const struct hisi_sas_hw *ops);
545 extern int hisi_sas_remove(struct platform_device *pdev);
546
547 extern int hisi_sas_slave_configure(struct scsi_device *sdev);
548 extern int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time);
549 extern void hisi_sas_scan_start(struct Scsi_Host *shost);
550 extern int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type);
551 extern void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no,
552                                 int enable);
553 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
554 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
555                                     struct sas_task *task,
556                                     struct hisi_sas_slot *slot);
557 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
558 extern void hisi_sas_rst_work_handler(struct work_struct *work);
559 extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
560 extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
561 extern void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no);
562 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
563                                 enum hisi_sas_phy_event event);
564 extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba);
565 extern u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max);
566 extern void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba);
567 extern void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba);
568 extern void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba);
569 extern void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba);
570 extern void hisi_sas_debugfs_work_handler(struct work_struct *work);
571 #endif