]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/hisi_sas/hisi_sas.h
2135de9a654b8238d610c3b38c1d8c5c9fa1ab3f
[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/dmapool.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/of_address.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 DRV_VERSION "v1.6"
28
29 #define HISI_SAS_MAX_PHYS       9
30 #define HISI_SAS_MAX_QUEUES     32
31 #define HISI_SAS_QUEUE_SLOTS 512
32 #define HISI_SAS_MAX_ITCT_ENTRIES 2048
33 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
34 #define HISI_SAS_RESET_BIT      0
35
36 #define HISI_SAS_STATUS_BUF_SZ \
37                 (sizeof(struct hisi_sas_err_record) + 1024)
38 #define HISI_SAS_COMMAND_TABLE_SZ \
39                 (((sizeof(union hisi_sas_command_table)+3)/4)*4)
40
41 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
42 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
43 #define HISI_SAS_MAX_STP_RESP_SZ 28
44
45 #define DEV_IS_EXPANDER(type) \
46         ((type == SAS_EDGE_EXPANDER_DEVICE) || \
47         (type == SAS_FANOUT_EXPANDER_DEVICE))
48
49 struct hisi_hba;
50
51 enum {
52         PORT_TYPE_SAS = (1U << 1),
53         PORT_TYPE_SATA = (1U << 0),
54 };
55
56 enum dev_status {
57         HISI_SAS_DEV_NORMAL,
58         HISI_SAS_DEV_EH,
59 };
60
61 enum {
62         HISI_SAS_INT_ABT_CMD = 0,
63         HISI_SAS_INT_ABT_DEV = 1,
64 };
65
66 enum hisi_sas_dev_type {
67         HISI_SAS_DEV_TYPE_STP = 0,
68         HISI_SAS_DEV_TYPE_SSP,
69         HISI_SAS_DEV_TYPE_SATA,
70 };
71
72 struct hisi_sas_phy {
73         struct hisi_hba *hisi_hba;
74         struct hisi_sas_port    *port;
75         struct asd_sas_phy      sas_phy;
76         struct sas_identify     identify;
77         struct timer_list       timer;
78         struct work_struct      phyup_ws;
79         u64             port_id; /* from hw */
80         u64             dev_sas_addr;
81         u64             phy_type;
82         u64             frame_rcvd_size;
83         u8              frame_rcvd[32];
84         u8              phy_attached;
85         u8              reserved[3];
86         enum sas_linkrate       minimum_linkrate;
87         enum sas_linkrate       maximum_linkrate;
88 };
89
90 struct hisi_sas_port {
91         struct asd_sas_port     sas_port;
92         u8      port_attached;
93         u8      id; /* from hw */
94         struct list_head        list;
95 };
96
97 struct hisi_sas_cq {
98         struct hisi_hba *hisi_hba;
99         struct tasklet_struct tasklet;
100         int     rd_point;
101         int     id;
102 };
103
104 struct hisi_sas_dq {
105         struct hisi_hba *hisi_hba;
106         int     wr_point;
107         int     id;
108 };
109
110 struct hisi_sas_device {
111         enum sas_device_type    dev_type;
112         struct hisi_hba         *hisi_hba;
113         struct domain_device    *sas_device;
114         u64 attached_phy;
115         u64 device_id;
116         atomic64_t running_req;
117         u8 dev_status;
118 };
119
120 struct hisi_sas_slot {
121         struct list_head entry;
122         struct sas_task *task;
123         struct hisi_sas_port    *port;
124         u64     n_elem;
125         int     dlvry_queue;
126         int     dlvry_queue_slot;
127         int     cmplt_queue;
128         int     cmplt_queue_slot;
129         int     idx;
130         int     abort;
131         void    *cmd_hdr;
132         dma_addr_t cmd_hdr_dma;
133         void    *status_buffer;
134         dma_addr_t status_buffer_dma;
135         void *command_table;
136         dma_addr_t command_table_dma;
137         struct hisi_sas_sge_page *sge_page;
138         dma_addr_t sge_page_dma;
139         struct work_struct abort_slot;
140 };
141
142 struct hisi_sas_tmf_task {
143         u8 tmf;
144         u16 tag_of_task_to_be_managed;
145 };
146
147 struct hisi_sas_hw {
148         int (*hw_init)(struct hisi_hba *hisi_hba);
149         void (*setup_itct)(struct hisi_hba *hisi_hba,
150                            struct hisi_sas_device *device);
151         int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
152                                 struct domain_device *device);
153         struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
154         void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
155         int (*get_free_slot)(struct hisi_hba *hisi_hba, u32 dev_id,
156                         int *q, int *s);
157         void (*start_delivery)(struct hisi_hba *hisi_hba);
158         int (*prep_ssp)(struct hisi_hba *hisi_hba,
159                         struct hisi_sas_slot *slot, int is_tmf,
160                         struct hisi_sas_tmf_task *tmf);
161         int (*prep_smp)(struct hisi_hba *hisi_hba,
162                         struct hisi_sas_slot *slot);
163         int (*prep_stp)(struct hisi_hba *hisi_hba,
164                         struct hisi_sas_slot *slot);
165         int (*prep_abort)(struct hisi_hba *hisi_hba,
166                           struct hisi_sas_slot *slot,
167                           int device_id, int abort_flag, int tag_to_abort);
168         int (*slot_complete)(struct hisi_hba *hisi_hba,
169                              struct hisi_sas_slot *slot, int abort);
170         void (*phys_init)(struct hisi_hba *hisi_hba);
171         void (*phy_enable)(struct hisi_hba *hisi_hba, int phy_no);
172         void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
173         void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
174         void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
175                         struct sas_phy_linkrates *linkrates);
176         enum sas_linkrate (*phy_get_max_linkrate)(void);
177         void (*free_device)(struct hisi_hba *hisi_hba,
178                             struct hisi_sas_device *dev);
179         int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
180         int (*soft_reset)(struct hisi_hba *hisi_hba);
181         int max_command_entries;
182         int complete_hdr_size;
183 };
184
185 struct hisi_hba {
186         /* This must be the first element, used by SHOST_TO_SAS_HA */
187         struct sas_ha_struct *p;
188
189         struct platform_device *pdev;
190         void __iomem *regs;
191         struct regmap *ctrl;
192         u32 ctrl_reset_reg;
193         u32 ctrl_reset_sts_reg;
194         u32 ctrl_clock_ena_reg;
195         u32 refclk_frequency_mhz;
196         u8 sas_addr[SAS_ADDR_SIZE];
197
198         int n_phy;
199         spinlock_t lock;
200
201         struct timer_list timer;
202         struct workqueue_struct *wq;
203
204         int slot_index_count;
205         unsigned long *slot_index_tags;
206
207         /* SCSI/SAS glue */
208         struct sas_ha_struct sha;
209         struct Scsi_Host *shost;
210
211         struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
212         struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
213         struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
214         struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
215
216         int     queue_count;
217         struct hisi_sas_slot    *slot_prep;
218
219         struct dma_pool *sge_page_pool;
220         struct hisi_sas_device  devices[HISI_SAS_MAX_DEVICES];
221         struct dma_pool *command_table_pool;
222         struct dma_pool *status_buffer_pool;
223         struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
224         dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
225         void *complete_hdr[HISI_SAS_MAX_QUEUES];
226         dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
227         struct hisi_sas_initial_fis *initial_fis;
228         dma_addr_t initial_fis_dma;
229         struct hisi_sas_itct *itct;
230         dma_addr_t itct_dma;
231         struct hisi_sas_iost *iost;
232         dma_addr_t iost_dma;
233         struct hisi_sas_breakpoint *breakpoint;
234         dma_addr_t breakpoint_dma;
235         struct hisi_sas_breakpoint *sata_breakpoint;
236         dma_addr_t sata_breakpoint_dma;
237         struct hisi_sas_slot    *slot_info;
238         unsigned long flags;
239         const struct hisi_sas_hw *hw;   /* Low level hw interface */
240         struct work_struct rst_work;
241 };
242
243 /* Generic HW DMA host memory structures */
244 /* Delivery queue header */
245 struct hisi_sas_cmd_hdr {
246         /* dw0 */
247         __le32 dw0;
248
249         /* dw1 */
250         __le32 dw1;
251
252         /* dw2 */
253         __le32 dw2;
254
255         /* dw3 */
256         __le32 transfer_tags;
257
258         /* dw4 */
259         __le32 data_transfer_len;
260
261         /* dw5 */
262         __le32 first_burst_num;
263
264         /* dw6 */
265         __le32 sg_len;
266
267         /* dw7 */
268         __le32 dw7;
269
270         /* dw8-9 */
271         __le64 cmd_table_addr;
272
273         /* dw10-11 */
274         __le64 sts_buffer_addr;
275
276         /* dw12-13 */
277         __le64 prd_table_addr;
278
279         /* dw14-15 */
280         __le64 dif_prd_table_addr;
281 };
282
283 struct hisi_sas_itct {
284         __le64 qw0;
285         __le64 sas_addr;
286         __le64 qw2;
287         __le64 qw3;
288         __le64 qw4_15[12];
289 };
290
291 struct hisi_sas_iost {
292         __le64 qw0;
293         __le64 qw1;
294         __le64 qw2;
295         __le64 qw3;
296 };
297
298 struct hisi_sas_err_record {
299         u32     data[4];
300 };
301
302 struct hisi_sas_initial_fis {
303         struct hisi_sas_err_record err_record;
304         struct dev_to_host_fis fis;
305         u32 rsvd[3];
306 };
307
308 struct hisi_sas_breakpoint {
309         u8      data[128];      /*io128 byte*/
310 };
311
312 struct hisi_sas_sge {
313         __le64 addr;
314         __le32 page_ctrl_0;
315         __le32 page_ctrl_1;
316         __le32 data_len;
317         __le32 data_off;
318 };
319
320 struct hisi_sas_command_table_smp {
321         u8 bytes[44];
322 };
323
324 struct hisi_sas_command_table_stp {
325         struct  host_to_dev_fis command_fis;
326         u8      dummy[12];
327         u8      atapi_cdb[ATAPI_CDB_LEN];
328 };
329
330 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
331 struct hisi_sas_sge_page {
332         struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
333 };
334
335 struct hisi_sas_command_table_ssp {
336         struct ssp_frame_hdr hdr;
337         union {
338                 struct {
339                         struct ssp_command_iu task;
340                         u32 prot[6];
341                 };
342                 struct ssp_tmf_iu ssp_task;
343                 struct xfer_rdy_iu xfer_rdy;
344                 struct ssp_response_iu ssp_res;
345         } u;
346 };
347
348 union hisi_sas_command_table {
349         struct hisi_sas_command_table_ssp ssp;
350         struct hisi_sas_command_table_smp smp;
351         struct hisi_sas_command_table_stp stp;
352 };
353
354 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
355 extern int hisi_sas_probe(struct platform_device *pdev,
356                           const struct hisi_sas_hw *ops);
357 extern int hisi_sas_remove(struct platform_device *pdev);
358
359 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
360 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
361                                     struct sas_task *task,
362                                     struct hisi_sas_slot *slot);
363 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
364 extern void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
365                                      u32 state);
366 #endif