]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/hisi_sas/hisi_sas.h
scsi: hisi_sas: add an mechanism to do reset work synchronously
[mirror_ubuntu-bionic-kernel.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/iopoll.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/regmap.h>
26 #include <scsi/sas_ata.h>
27 #include <scsi/libsas.h>
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 1024
33 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
34 #define HISI_SAS_RESET_BIT 0
35 #define HISI_SAS_REJECT_CMD_BIT 1
36
37 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
38 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
39
40 #define hisi_sas_status_buf_addr(buf) \
41 (buf + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
42 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr(slot->buf)
43 #define hisi_sas_status_buf_addr_dma(slot) \
44 hisi_sas_status_buf_addr(slot->buf_dma)
45
46 #define hisi_sas_cmd_hdr_addr(buf) \
47 (buf + offsetof(struct hisi_sas_slot_buf_table, command_header))
48 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr(slot->buf)
49 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr(slot->buf_dma)
50
51 #define hisi_sas_sge_addr(buf) \
52 (buf + offsetof(struct hisi_sas_slot_buf_table, sge_page))
53 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr(slot->buf)
54 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr(slot->buf_dma)
55
56 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
57 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
58 #define HISI_SAS_MAX_STP_RESP_SZ 28
59
60 #define DEV_IS_EXPANDER(type) \
61 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
62 (type == SAS_FANOUT_EXPANDER_DEVICE))
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 struct hisi_hba;
71
72 enum {
73 PORT_TYPE_SAS = (1U << 1),
74 PORT_TYPE_SATA = (1U << 0),
75 };
76
77 enum dev_status {
78 HISI_SAS_DEV_NORMAL,
79 HISI_SAS_DEV_EH,
80 };
81
82 enum {
83 HISI_SAS_INT_ABT_CMD = 0,
84 HISI_SAS_INT_ABT_DEV = 1,
85 };
86
87 enum hisi_sas_dev_type {
88 HISI_SAS_DEV_TYPE_STP = 0,
89 HISI_SAS_DEV_TYPE_SSP,
90 HISI_SAS_DEV_TYPE_SATA,
91 };
92
93 struct hisi_sas_hw_error {
94 u32 irq_msk;
95 u32 msk;
96 int shift;
97 const char *msg;
98 int reg;
99 const struct hisi_sas_hw_error *sub;
100 };
101
102 struct hisi_sas_rst {
103 struct hisi_hba *hisi_hba;
104 struct completion *completion;
105 struct work_struct work;
106 bool done;
107 };
108
109 #define HISI_SAS_RST_WORK_INIT(r, c) \
110 { .hisi_hba = hisi_hba, \
111 .completion = &c, \
112 .work = __WORK_INITIALIZER(r.work, \
113 hisi_sas_sync_rst_work_handler), \
114 .done = false, \
115 }
116
117 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
118 DECLARE_COMPLETION_ONSTACK(c); \
119 DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \
120 struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)
121
122 enum hisi_sas_bit_err_type {
123 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
124 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
125 };
126
127 struct hisi_sas_phy {
128 struct hisi_hba *hisi_hba;
129 struct hisi_sas_port *port;
130 struct asd_sas_phy sas_phy;
131 struct sas_identify identify;
132 struct work_struct phyup_ws;
133 u64 port_id; /* from hw */
134 u64 dev_sas_addr;
135 u64 frame_rcvd_size;
136 u8 frame_rcvd[32];
137 u8 phy_attached;
138 u8 reserved[3];
139 u32 phy_type;
140 enum sas_linkrate minimum_linkrate;
141 enum sas_linkrate maximum_linkrate;
142 };
143
144 struct hisi_sas_port {
145 struct asd_sas_port sas_port;
146 u8 port_attached;
147 u8 id; /* from hw */
148 };
149
150 struct hisi_sas_cq {
151 struct hisi_hba *hisi_hba;
152 struct tasklet_struct tasklet;
153 int rd_point;
154 int id;
155 };
156
157 struct hisi_sas_dq {
158 struct hisi_hba *hisi_hba;
159 struct hisi_sas_slot *slot_prep;
160 spinlock_t lock;
161 int wr_point;
162 int id;
163 };
164
165 struct hisi_sas_device {
166 struct hisi_hba *hisi_hba;
167 struct domain_device *sas_device;
168 struct completion *completion;
169 struct hisi_sas_dq *dq;
170 struct list_head list;
171 u64 attached_phy;
172 atomic64_t running_req;
173 enum sas_device_type dev_type;
174 int device_id;
175 int sata_idx;
176 u8 dev_status;
177 };
178
179 struct hisi_sas_slot {
180 struct list_head entry;
181 struct sas_task *task;
182 struct hisi_sas_port *port;
183 u64 n_elem;
184 int dlvry_queue;
185 int dlvry_queue_slot;
186 int cmplt_queue;
187 int cmplt_queue_slot;
188 int idx;
189 int abort;
190 void *buf;
191 dma_addr_t buf_dma;
192 void *cmd_hdr;
193 dma_addr_t cmd_hdr_dma;
194 struct work_struct abort_slot;
195 struct timer_list internal_abort_timer;
196 };
197
198 struct hisi_sas_tmf_task {
199 u8 tmf;
200 u16 tag_of_task_to_be_managed;
201 };
202
203 struct hisi_sas_hw {
204 int (*hw_init)(struct hisi_hba *hisi_hba);
205 void (*setup_itct)(struct hisi_hba *hisi_hba,
206 struct hisi_sas_device *device);
207 int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
208 struct domain_device *device);
209 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
210 void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
211 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
212 void (*start_delivery)(struct hisi_sas_dq *dq);
213 int (*prep_ssp)(struct hisi_hba *hisi_hba,
214 struct hisi_sas_slot *slot, int is_tmf,
215 struct hisi_sas_tmf_task *tmf);
216 int (*prep_smp)(struct hisi_hba *hisi_hba,
217 struct hisi_sas_slot *slot);
218 int (*prep_stp)(struct hisi_hba *hisi_hba,
219 struct hisi_sas_slot *slot);
220 int (*prep_abort)(struct hisi_hba *hisi_hba,
221 struct hisi_sas_slot *slot,
222 int device_id, int abort_flag, int tag_to_abort);
223 int (*slot_complete)(struct hisi_hba *hisi_hba,
224 struct hisi_sas_slot *slot);
225 void (*phys_init)(struct hisi_hba *hisi_hba);
226 void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
227 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
228 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
229 void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
230 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
231 struct sas_phy_linkrates *linkrates);
232 enum sas_linkrate (*phy_get_max_linkrate)(void);
233 void (*clear_itct)(struct hisi_hba *hisi_hba,
234 struct hisi_sas_device *dev);
235 void (*free_device)(struct hisi_sas_device *sas_dev);
236 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
237 void (*dereg_device)(struct hisi_hba *hisi_hba,
238 struct domain_device *device);
239 int (*soft_reset)(struct hisi_hba *hisi_hba);
240 u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
241 int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type,
242 u8 reg_index, u8 reg_count, u8 *write_data);
243 int max_command_entries;
244 int complete_hdr_size;
245 };
246
247 struct hisi_hba {
248 /* This must be the first element, used by SHOST_TO_SAS_HA */
249 struct sas_ha_struct *p;
250
251 struct platform_device *platform_dev;
252 struct pci_dev *pci_dev;
253 struct device *dev;
254
255 void __iomem *regs;
256 void __iomem *sgpio_regs;
257 struct regmap *ctrl;
258 u32 ctrl_reset_reg;
259 u32 ctrl_reset_sts_reg;
260 u32 ctrl_clock_ena_reg;
261 u32 refclk_frequency_mhz;
262 u8 sas_addr[SAS_ADDR_SIZE];
263
264 int n_phy;
265 spinlock_t lock;
266
267 struct timer_list timer;
268 struct workqueue_struct *wq;
269
270 int slot_index_count;
271 unsigned long *slot_index_tags;
272 unsigned long reject_stp_links_msk;
273
274 /* SCSI/SAS glue */
275 struct sas_ha_struct sha;
276 struct Scsi_Host *shost;
277
278 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
279 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
280 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
281 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
282
283 int queue_count;
284
285 struct dma_pool *buffer_pool;
286 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
287 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
288 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
289 void *complete_hdr[HISI_SAS_MAX_QUEUES];
290 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
291 struct hisi_sas_initial_fis *initial_fis;
292 dma_addr_t initial_fis_dma;
293 struct hisi_sas_itct *itct;
294 dma_addr_t itct_dma;
295 struct hisi_sas_iost *iost;
296 dma_addr_t iost_dma;
297 struct hisi_sas_breakpoint *breakpoint;
298 dma_addr_t breakpoint_dma;
299 struct hisi_sas_breakpoint *sata_breakpoint;
300 dma_addr_t sata_breakpoint_dma;
301 struct hisi_sas_slot *slot_info;
302 unsigned long flags;
303 const struct hisi_sas_hw *hw; /* Low level hw interface */
304 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
305 struct work_struct rst_work;
306 };
307
308 /* Generic HW DMA host memory structures */
309 /* Delivery queue header */
310 struct hisi_sas_cmd_hdr {
311 /* dw0 */
312 __le32 dw0;
313
314 /* dw1 */
315 __le32 dw1;
316
317 /* dw2 */
318 __le32 dw2;
319
320 /* dw3 */
321 __le32 transfer_tags;
322
323 /* dw4 */
324 __le32 data_transfer_len;
325
326 /* dw5 */
327 __le32 first_burst_num;
328
329 /* dw6 */
330 __le32 sg_len;
331
332 /* dw7 */
333 __le32 dw7;
334
335 /* dw8-9 */
336 __le64 cmd_table_addr;
337
338 /* dw10-11 */
339 __le64 sts_buffer_addr;
340
341 /* dw12-13 */
342 __le64 prd_table_addr;
343
344 /* dw14-15 */
345 __le64 dif_prd_table_addr;
346 };
347
348 struct hisi_sas_itct {
349 __le64 qw0;
350 __le64 sas_addr;
351 __le64 qw2;
352 __le64 qw3;
353 __le64 qw4_15[12];
354 };
355
356 struct hisi_sas_iost {
357 __le64 qw0;
358 __le64 qw1;
359 __le64 qw2;
360 __le64 qw3;
361 };
362
363 struct hisi_sas_err_record {
364 u32 data[4];
365 };
366
367 struct hisi_sas_initial_fis {
368 struct hisi_sas_err_record err_record;
369 struct dev_to_host_fis fis;
370 u32 rsvd[3];
371 };
372
373 struct hisi_sas_breakpoint {
374 u8 data[128];
375 };
376
377 struct hisi_sas_sata_breakpoint {
378 struct hisi_sas_breakpoint tag[32];
379 };
380
381 struct hisi_sas_sge {
382 __le64 addr;
383 __le32 page_ctrl_0;
384 __le32 page_ctrl_1;
385 __le32 data_len;
386 __le32 data_off;
387 };
388
389 struct hisi_sas_command_table_smp {
390 u8 bytes[44];
391 };
392
393 struct hisi_sas_command_table_stp {
394 struct host_to_dev_fis command_fis;
395 u8 dummy[12];
396 u8 atapi_cdb[ATAPI_CDB_LEN];
397 };
398
399 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
400 struct hisi_sas_sge_page {
401 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
402 } __aligned(16);
403
404 struct hisi_sas_command_table_ssp {
405 struct ssp_frame_hdr hdr;
406 union {
407 struct {
408 struct ssp_command_iu task;
409 u32 prot[6];
410 };
411 struct ssp_tmf_iu ssp_task;
412 struct xfer_rdy_iu xfer_rdy;
413 struct ssp_response_iu ssp_res;
414 } u;
415 };
416
417 union hisi_sas_command_table {
418 struct hisi_sas_command_table_ssp ssp;
419 struct hisi_sas_command_table_smp smp;
420 struct hisi_sas_command_table_stp stp;
421 } __aligned(16);
422
423 struct hisi_sas_status_buffer {
424 struct hisi_sas_err_record err;
425 u8 iu[1024];
426 } __aligned(16);
427
428 struct hisi_sas_slot_buf_table {
429 struct hisi_sas_status_buffer status_buffer;
430 union hisi_sas_command_table command_header;
431 struct hisi_sas_sge_page sge_page;
432 };
433
434 extern struct scsi_transport_template *hisi_sas_stt;
435 extern struct scsi_host_template *hisi_sas_sht;
436
437 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
438 extern void hisi_sas_init_add(struct hisi_hba *hisi_hba);
439 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost);
440 extern void hisi_sas_free(struct hisi_hba *hisi_hba);
441 extern u8 hisi_sas_get_ata_protocol(u8 cmd, int direction);
442 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
443 extern void hisi_sas_sata_done(struct sas_task *task,
444 struct hisi_sas_slot *slot);
445 extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
446 extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
447 extern int hisi_sas_probe(struct platform_device *pdev,
448 const struct hisi_sas_hw *ops);
449 extern int hisi_sas_remove(struct platform_device *pdev);
450
451 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
452 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
453 struct sas_task *task,
454 struct hisi_sas_slot *slot);
455 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
456 extern void hisi_sas_rst_work_handler(struct work_struct *work);
457 extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
458 extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
459 #endif