scsi: hisi_sas: add RAS feature for v3 hw
[linux-2.6-block.git] / drivers / scsi / hisi_sas / hisi_sas.h
CommitLineData
e8899fad
JG
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
4d558c77 15#include <linux/acpi.h>
3bc45af8 16#include <linux/clk.h>
e8899fad 17#include <linux/dmapool.h>
a25d0d3d 18#include <linux/iopoll.h>
e8899fad
JG
19#include <linux/mfd/syscon.h>
20#include <linux/module.h>
21#include <linux/of_address.h>
11b75249 22#include <linux/pci.h>
e8899fad 23#include <linux/platform_device.h>
4d558c77 24#include <linux/property.h>
e8899fad 25#include <linux/regmap.h>
6f2ff1a1 26#include <scsi/sas_ata.h>
e8899fad
JG
27#include <scsi/libsas.h>
28
7eb7869f 29#define HISI_SAS_MAX_PHYS 9
6be6de18
JG
30#define HISI_SAS_MAX_QUEUES 32
31#define HISI_SAS_QUEUE_SLOTS 512
3297ded1 32#define HISI_SAS_MAX_ITCT_ENTRIES 1024
7eb7869f 33#define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
06ec0fb9 34#define HISI_SAS_RESET_BIT 0
917d3bda 35#define HISI_SAS_REJECT_CMD_BIT 1
7eb7869f 36
f557e32c
XT
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)
6be6de18 55
42e7a693 56#define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
66ee999b 57#define HISI_SAS_MAX_SMP_RESP_SZ 1028
6f2ff1a1 58#define HISI_SAS_MAX_STP_RESP_SZ 28
42e7a693 59
98bf39fc
JG
60#define DEV_IS_EXPANDER(type) \
61 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
62 (type == SAS_FANOUT_EXPANDER_DEVICE))
63
6c7bb8a1
XC
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
abda97c2 70struct hisi_hba;
af740dbe 71
07d78592
JG
72enum {
73 PORT_TYPE_SAS = (1U << 1),
74 PORT_TYPE_SATA = (1U << 0),
75};
76
af740dbe
JG
77enum dev_status {
78 HISI_SAS_DEV_NORMAL,
79 HISI_SAS_DEV_EH,
80};
abda97c2 81
441c2740
JG
82enum {
83 HISI_SAS_INT_ABT_CMD = 0,
84 HISI_SAS_INT_ABT_DEV = 1,
85};
86
abda97c2
JG
87enum 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
2b383351
JG
93struct hisi_sas_hw_error {
94 u32 irq_msk;
95 u32 msk;
96 int shift;
97 const char *msg;
98 int reg;
729428ca 99 const struct hisi_sas_hw_error *sub;
2b383351
JG
100};
101
e402acdb
XT
102struct 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
122enum hisi_sas_bit_err_type {
123 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
124 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
125};
126
7eb7869f 127struct hisi_sas_phy {
976867e6
JG
128 struct hisi_hba *hisi_hba;
129 struct hisi_sas_port *port;
7eb7869f 130 struct asd_sas_phy sas_phy;
976867e6 131 struct sas_identify identify;
66139921 132 struct work_struct phyup_ws;
976867e6 133 u64 port_id; /* from hw */
5d74242e 134 u64 dev_sas_addr;
976867e6
JG
135 u64 frame_rcvd_size;
136 u8 frame_rcvd[32];
137 u8 phy_attached;
138 u8 reserved[3];
d0ef10c9 139 u32 phy_type;
976867e6
JG
140 enum sas_linkrate minimum_linkrate;
141 enum sas_linkrate maximum_linkrate;
7eb7869f
JG
142};
143
144struct hisi_sas_port {
145 struct asd_sas_port sas_port;
976867e6
JG
146 u8 port_attached;
147 u8 id; /* from hw */
7eb7869f
JG
148};
149
9101a079
JG
150struct hisi_sas_cq {
151 struct hisi_hba *hisi_hba;
d177c408 152 struct tasklet_struct tasklet;
e6c346f3 153 int rd_point;
9101a079
JG
154 int id;
155};
156
4fde02ad
JG
157struct hisi_sas_dq {
158 struct hisi_hba *hisi_hba;
b1a49412
XC
159 struct hisi_sas_slot *slot_prep;
160 spinlock_t lock;
4fde02ad
JG
161 int wr_point;
162 int id;
163};
164
af740dbe 165struct hisi_sas_device {
abda97c2
JG
166 struct hisi_hba *hisi_hba;
167 struct domain_device *sas_device;
640acc9a 168 struct completion *completion;
b1a49412 169 struct hisi_sas_dq *dq;
ad604832 170 struct list_head list;
abda97c2 171 u64 attached_phy;
f696cc32 172 atomic64_t running_req;
ad604832
JG
173 enum sas_device_type dev_type;
174 int device_id;
32ccba52 175 int sata_idx;
ad604832 176 u8 dev_status;
af740dbe
JG
177};
178
6be6de18 179struct hisi_sas_slot {
42e7a693
JG
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;
27a3f229
JG
186 int cmplt_queue;
187 int cmplt_queue_slot;
42e7a693 188 int idx;
cac9b2a2 189 int abort;
f557e32c
XT
190 void *buf;
191 dma_addr_t buf_dma;
42e7a693
JG
192 void *cmd_hdr;
193 dma_addr_t cmd_hdr_dma;
cac9b2a2 194 struct work_struct abort_slot;
0844a3ff 195 struct timer_list internal_abort_timer;
42e7a693
JG
196};
197
198struct hisi_sas_tmf_task {
199 u8 tmf;
200 u16 tag_of_task_to_be_managed;
6be6de18
JG
201};
202
7eb7869f 203struct hisi_sas_hw {
8ff1d571 204 int (*hw_init)(struct hisi_hba *hisi_hba);
abda97c2
JG
205 void (*setup_itct)(struct hisi_hba *hisi_hba,
206 struct hisi_sas_device *device);
685b6d6e
JG
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);
66139921 210 void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
b1a49412
XC
211 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
212 void (*start_delivery)(struct hisi_sas_dq *dq);
42e7a693
JG
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);
66ee999b
JG
216 int (*prep_smp)(struct hisi_hba *hisi_hba,
217 struct hisi_sas_slot *slot);
6f2ff1a1
JG
218 int (*prep_stp)(struct hisi_hba *hisi_hba,
219 struct hisi_sas_slot *slot);
441c2740
JG
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);
27a3f229 223 int (*slot_complete)(struct hisi_hba *hisi_hba,
405314df 224 struct hisi_sas_slot *slot);
396b8044 225 void (*phys_init)(struct hisi_hba *hisi_hba);
1eb8eeac 226 void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
e4189d53
JG
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);
c52108c6 229 void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
2ae75787
XC
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);
0258141a 233 void (*clear_itct)(struct hisi_hba *hisi_hba,
27a3f229 234 struct hisi_sas_device *dev);
0258141a 235 void (*free_device)(struct hisi_sas_device *sas_dev);
184a4635 236 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
d30ff263
XC
237 void (*dereg_device)(struct hisi_hba *hisi_hba,
238 struct domain_device *device);
06ec0fb9 239 int (*soft_reset)(struct hisi_hba *hisi_hba);
917d3bda 240 u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
a8d547bd 241 int max_command_entries;
6be6de18 242 int complete_hdr_size;
7eb7869f
JG
243};
244
245struct hisi_hba {
246 /* This must be the first element, used by SHOST_TO_SAS_HA */
247 struct sas_ha_struct *p;
248
11b75249
JG
249 struct platform_device *platform_dev;
250 struct pci_dev *pci_dev;
251 struct device *dev;
252
e26b2f40
JG
253 void __iomem *regs;
254 struct regmap *ctrl;
255 u32 ctrl_reset_reg;
256 u32 ctrl_reset_sts_reg;
257 u32 ctrl_clock_ena_reg;
3bc45af8 258 u32 refclk_frequency_mhz;
7eb7869f
JG
259 u8 sas_addr[SAS_ADDR_SIZE];
260
261 int n_phy;
fa42d80d 262 spinlock_t lock;
7eb7869f 263
fa42d80d 264 struct timer_list timer;
7e9080e1 265 struct workqueue_struct *wq;
257efd1f
JG
266
267 int slot_index_count;
268 unsigned long *slot_index_tags;
c7b9d369 269 unsigned long reject_stp_links_msk;
257efd1f 270
7eb7869f
JG
271 /* SCSI/SAS glue */
272 struct sas_ha_struct sha;
273 struct Scsi_Host *shost;
9101a079
JG
274
275 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
4fde02ad 276 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
7eb7869f
JG
277 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
278 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
e26b2f40
JG
279
280 int queue_count;
6be6de18 281
f557e32c 282 struct dma_pool *buffer_pool;
af740dbe 283 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
6be6de18
JG
284 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
285 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
286 void *complete_hdr[HISI_SAS_MAX_QUEUES];
287 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
288 struct hisi_sas_initial_fis *initial_fis;
289 dma_addr_t initial_fis_dma;
290 struct hisi_sas_itct *itct;
291 dma_addr_t itct_dma;
292 struct hisi_sas_iost *iost;
293 dma_addr_t iost_dma;
294 struct hisi_sas_breakpoint *breakpoint;
295 dma_addr_t breakpoint_dma;
296 struct hisi_sas_breakpoint *sata_breakpoint;
297 dma_addr_t sata_breakpoint_dma;
298 struct hisi_sas_slot *slot_info;
06ec0fb9 299 unsigned long flags;
7eb7869f 300 const struct hisi_sas_hw *hw; /* Low level hw interface */
32ccba52 301 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
06ec0fb9 302 struct work_struct rst_work;
7eb7869f
JG
303};
304
c799d6bd
JG
305/* Generic HW DMA host memory structures */
306/* Delivery queue header */
307struct hisi_sas_cmd_hdr {
308 /* dw0 */
309 __le32 dw0;
310
311 /* dw1 */
312 __le32 dw1;
313
314 /* dw2 */
315 __le32 dw2;
316
317 /* dw3 */
318 __le32 transfer_tags;
319
320 /* dw4 */
321 __le32 data_transfer_len;
322
323 /* dw5 */
324 __le32 first_burst_num;
325
326 /* dw6 */
327 __le32 sg_len;
328
329 /* dw7 */
330 __le32 dw7;
331
332 /* dw8-9 */
333 __le64 cmd_table_addr;
334
335 /* dw10-11 */
336 __le64 sts_buffer_addr;
337
338 /* dw12-13 */
339 __le64 prd_table_addr;
340
341 /* dw14-15 */
342 __le64 dif_prd_table_addr;
343};
344
345struct hisi_sas_itct {
346 __le64 qw0;
347 __le64 sas_addr;
348 __le64 qw2;
349 __le64 qw3;
281e3bf6 350 __le64 qw4_15[12];
c799d6bd
JG
351};
352
353struct hisi_sas_iost {
354 __le64 qw0;
355 __le64 qw1;
356 __le64 qw2;
357 __le64 qw3;
358};
359
360struct hisi_sas_err_record {
8d1eee7d 361 u32 data[4];
c799d6bd
JG
362};
363
364struct hisi_sas_initial_fis {
365 struct hisi_sas_err_record err_record;
366 struct dev_to_host_fis fis;
367 u32 rsvd[3];
368};
369
370struct hisi_sas_breakpoint {
3297ded1
XC
371 u8 data[128];
372};
373
374struct hisi_sas_sata_breakpoint {
375 struct hisi_sas_breakpoint tag[32];
c799d6bd
JG
376};
377
378struct hisi_sas_sge {
379 __le64 addr;
380 __le32 page_ctrl_0;
381 __le32 page_ctrl_1;
382 __le32 data_len;
383 __le32 data_off;
384};
385
386struct hisi_sas_command_table_smp {
387 u8 bytes[44];
388};
389
390struct hisi_sas_command_table_stp {
391 struct host_to_dev_fis command_fis;
392 u8 dummy[12];
393 u8 atapi_cdb[ATAPI_CDB_LEN];
394};
395
65e8617f 396#define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
c799d6bd
JG
397struct hisi_sas_sge_page {
398 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
f557e32c 399} __aligned(16);
c799d6bd
JG
400
401struct hisi_sas_command_table_ssp {
402 struct ssp_frame_hdr hdr;
403 union {
404 struct {
405 struct ssp_command_iu task;
406 u32 prot[6];
407 };
408 struct ssp_tmf_iu ssp_task;
409 struct xfer_rdy_iu xfer_rdy;
410 struct ssp_response_iu ssp_res;
411 } u;
412};
413
414union hisi_sas_command_table {
415 struct hisi_sas_command_table_ssp ssp;
416 struct hisi_sas_command_table_smp smp;
417 struct hisi_sas_command_table_stp stp;
f557e32c
XT
418} __aligned(16);
419
420struct hisi_sas_status_buffer {
421 struct hisi_sas_err_record err;
422 u8 iu[1024];
423} __aligned(16);
424
425struct hisi_sas_slot_buf_table {
426 struct hisi_sas_status_buffer status_buffer;
427 union hisi_sas_command_table command_header;
428 struct hisi_sas_sge_page sge_page;
c799d6bd 429};
2e244f0f 430
e21fe3a5
JG
431extern struct scsi_transport_template *hisi_sas_stt;
432extern struct scsi_host_template *hisi_sas_sht;
433
a25d0d3d 434extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
e21fe3a5
JG
435extern void hisi_sas_init_add(struct hisi_hba *hisi_hba);
436extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost);
437extern void hisi_sas_free(struct hisi_hba *hisi_hba);
6c7bb8a1 438extern u8 hisi_sas_get_ata_protocol(u8 cmd, int direction);
2e244f0f 439extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
75904077
XC
440extern void hisi_sas_sata_done(struct sas_task *task,
441 struct hisi_sas_slot *slot);
318913c6 442extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
0fa24c19 443extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
9fb10b54
JG
444extern int hisi_sas_probe(struct platform_device *pdev,
445 const struct hisi_sas_hw *ops);
446extern int hisi_sas_remove(struct platform_device *pdev);
c799d6bd 447
184a4635 448extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
27a3f229
JG
449extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
450 struct sas_task *task,
451 struct hisi_sas_slot *slot);
06ec0fb9 452extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
b4241f0f 453extern void hisi_sas_rst_work_handler(struct work_struct *work);
e402acdb 454extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
571295f8 455extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
e8899fad 456#endif