blob: 19c6ffd6d4ff7fcc2ea073417736ed598faa606d [file] [log] [blame]
John Garrye8899fa2015-11-18 00:50:30 +08001/*
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
John Garry4d558c72016-02-04 02:26:08 +080015#include <linux/acpi.h>
John Garry3bc45af2016-10-04 19:11:11 +080016#include <linux/clk.h>
John Garrye8899fa2015-11-18 00:50:30 +080017#include <linux/dmapool.h>
18#include <linux/mfd/syscon.h>
19#include <linux/module.h>
20#include <linux/of_address.h>
John Garrye8899fa2015-11-18 00:50:30 +080021#include <linux/platform_device.h>
John Garry4d558c72016-02-04 02:26:08 +080022#include <linux/property.h>
John Garrye8899fa2015-11-18 00:50:30 +080023#include <linux/regmap.h>
John Garry6f2ff1a2016-01-26 02:47:20 +080024#include <scsi/sas_ata.h>
John Garrye8899fa2015-11-18 00:50:30 +080025#include <scsi/libsas.h>
26
John Garry6328d902016-08-24 19:05:54 +080027#define DRV_VERSION "v1.6"
John Garrye8899fa2015-11-18 00:50:30 +080028
John Garry7eb78692015-11-18 00:50:31 +080029#define HISI_SAS_MAX_PHYS 9
John Garry6be6de12015-11-18 00:50:34 +080030#define HISI_SAS_MAX_QUEUES 32
31#define HISI_SAS_QUEUE_SLOTS 512
John Garry5560e9f2016-01-26 02:47:04 +080032#define HISI_SAS_MAX_ITCT_ENTRIES 2048
John Garry7eb78692015-11-18 00:50:31 +080033#define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
Xiang Chen06ec0fb2017-03-23 01:25:18 +080034#define HISI_SAS_RESET_BIT 0
John Garry7eb78692015-11-18 00:50:31 +080035
John Garry6be6de12015-11-18 00:50:34 +080036#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
John Garry42e7a692015-11-18 00:50:49 +080041#define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
John Garry66ee9992015-11-18 00:50:54 +080042#define HISI_SAS_MAX_SMP_RESP_SZ 1028
John Garry6f2ff1a2016-01-26 02:47:20 +080043#define HISI_SAS_MAX_STP_RESP_SZ 28
John Garry42e7a692015-11-18 00:50:49 +080044
John Garry98bf39f2016-01-26 02:47:02 +080045#define DEV_IS_EXPANDER(type) \
46 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
47 (type == SAS_FANOUT_EXPANDER_DEVICE))
48
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080049#define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
50#define HISI_SAS_SATA_PROTOCOL_PIO 0x2
51#define HISI_SAS_SATA_PROTOCOL_DMA 0x4
52#define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
53#define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
54
John Garryabda97c2015-11-18 00:50:51 +080055struct hisi_hba;
John Garryaf740db2015-11-18 00:50:41 +080056
John Garry07d78592015-11-18 00:50:47 +080057enum {
58 PORT_TYPE_SAS = (1U << 1),
59 PORT_TYPE_SATA = (1U << 0),
60};
61
John Garryaf740db2015-11-18 00:50:41 +080062enum dev_status {
63 HISI_SAS_DEV_NORMAL,
64 HISI_SAS_DEV_EH,
65};
John Garryabda97c2015-11-18 00:50:51 +080066
John Garry441c2742016-08-24 19:05:47 +080067enum {
68 HISI_SAS_INT_ABT_CMD = 0,
69 HISI_SAS_INT_ABT_DEV = 1,
70};
71
John Garryabda97c2015-11-18 00:50:51 +080072enum hisi_sas_dev_type {
73 HISI_SAS_DEV_TYPE_STP = 0,
74 HISI_SAS_DEV_TYPE_SSP,
75 HISI_SAS_DEV_TYPE_SATA,
76};
77
John Garry7eb78692015-11-18 00:50:31 +080078struct hisi_sas_phy {
John Garry976867e2015-11-18 00:50:42 +080079 struct hisi_hba *hisi_hba;
80 struct hisi_sas_port *port;
John Garry7eb78692015-11-18 00:50:31 +080081 struct asd_sas_phy sas_phy;
John Garry976867e2015-11-18 00:50:42 +080082 struct sas_identify identify;
83 struct timer_list timer;
John Garry66139922015-11-18 00:50:48 +080084 struct work_struct phyup_ws;
John Garry976867e2015-11-18 00:50:42 +080085 u64 port_id; /* from hw */
John Garry5d742422015-11-18 00:50:38 +080086 u64 dev_sas_addr;
John Garry976867e2015-11-18 00:50:42 +080087 u64 phy_type;
88 u64 frame_rcvd_size;
89 u8 frame_rcvd[32];
90 u8 phy_attached;
91 u8 reserved[3];
92 enum sas_linkrate minimum_linkrate;
93 enum sas_linkrate maximum_linkrate;
John Garry7eb78692015-11-18 00:50:31 +080094};
95
96struct hisi_sas_port {
97 struct asd_sas_port sas_port;
John Garry976867e2015-11-18 00:50:42 +080098 u8 port_attached;
99 u8 id; /* from hw */
John Garry7eb78692015-11-18 00:50:31 +0800100};
101
John Garry9101a072015-11-18 00:50:37 +0800102struct hisi_sas_cq {
103 struct hisi_hba *hisi_hba;
John Garryd177c402017-01-03 20:24:48 +0800104 struct tasklet_struct tasklet;
John Garrye6c346f2016-09-06 23:36:11 +0800105 int rd_point;
John Garry9101a072015-11-18 00:50:37 +0800106 int id;
107};
108
John Garry4fde02a2016-09-06 23:36:12 +0800109struct hisi_sas_dq {
110 struct hisi_hba *hisi_hba;
Xiang Chenb1a49412017-06-14 23:33:13 +0800111 struct hisi_sas_slot *slot_prep;
112 spinlock_t lock;
John Garry4fde02a2016-09-06 23:36:12 +0800113 int wr_point;
114 int id;
115};
116
John Garryaf740db2015-11-18 00:50:41 +0800117struct hisi_sas_device {
John Garryabda97c2015-11-18 00:50:51 +0800118 struct hisi_hba *hisi_hba;
119 struct domain_device *sas_device;
Xiang Chenb1a49412017-06-14 23:33:13 +0800120 struct hisi_sas_dq *dq;
John Garry405314d2017-03-23 01:25:21 +0800121 struct list_head list;
John Garryad604832017-06-14 23:33:12 +0800122 u64 attached_phy;
123 atomic64_t running_req;
124 enum sas_device_type dev_type;
125 int device_id;
Xiaofei Tan32ccba52017-04-10 21:21:57 +0800126 int sata_idx;
John Garryad604832017-06-14 23:33:12 +0800127 u8 dev_status;
John Garryaf740db2015-11-18 00:50:41 +0800128};
129
John Garry6be6de12015-11-18 00:50:34 +0800130struct hisi_sas_slot {
John Garry42e7a692015-11-18 00:50:49 +0800131 struct list_head entry;
132 struct sas_task *task;
133 struct hisi_sas_port *port;
134 u64 n_elem;
135 int dlvry_queue;
136 int dlvry_queue_slot;
John Garry27a3f222015-11-18 00:50:50 +0800137 int cmplt_queue;
138 int cmplt_queue_slot;
John Garry42e7a692015-11-18 00:50:49 +0800139 int idx;
John Garrycac9b2a2016-02-25 17:42:11 +0800140 int abort;
John Garry42e7a692015-11-18 00:50:49 +0800141 void *cmd_hdr;
142 dma_addr_t cmd_hdr_dma;
143 void *status_buffer;
144 dma_addr_t status_buffer_dma;
145 void *command_table;
146 dma_addr_t command_table_dma;
147 struct hisi_sas_sge_page *sge_page;
148 dma_addr_t sge_page_dma;
John Garrycac9b2a2016-02-25 17:42:11 +0800149 struct work_struct abort_slot;
John Garry0844a3f2017-04-10 21:21:59 +0800150 struct timer_list internal_abort_timer;
John Garry42e7a692015-11-18 00:50:49 +0800151};
152
153struct hisi_sas_tmf_task {
154 u8 tmf;
155 u16 tag_of_task_to_be_managed;
John Garry6be6de12015-11-18 00:50:34 +0800156};
157
John Garry7eb78692015-11-18 00:50:31 +0800158struct hisi_sas_hw {
John Garry8ff1d572015-11-18 00:50:46 +0800159 int (*hw_init)(struct hisi_hba *hisi_hba);
John Garryabda97c2015-11-18 00:50:51 +0800160 void (*setup_itct)(struct hisi_hba *hisi_hba,
161 struct hisi_sas_device *device);
John Garry685b6d62016-04-15 21:36:36 +0800162 int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
163 struct domain_device *device);
164 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
John Garry66139922015-11-18 00:50:48 +0800165 void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
Xiang Chenb1a49412017-06-14 23:33:13 +0800166 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
167 void (*start_delivery)(struct hisi_sas_dq *dq);
John Garry42e7a692015-11-18 00:50:49 +0800168 int (*prep_ssp)(struct hisi_hba *hisi_hba,
169 struct hisi_sas_slot *slot, int is_tmf,
170 struct hisi_sas_tmf_task *tmf);
John Garry66ee9992015-11-18 00:50:54 +0800171 int (*prep_smp)(struct hisi_hba *hisi_hba,
172 struct hisi_sas_slot *slot);
John Garry6f2ff1a2016-01-26 02:47:20 +0800173 int (*prep_stp)(struct hisi_hba *hisi_hba,
174 struct hisi_sas_slot *slot);
John Garry441c2742016-08-24 19:05:47 +0800175 int (*prep_abort)(struct hisi_hba *hisi_hba,
176 struct hisi_sas_slot *slot,
177 int device_id, int abort_flag, int tag_to_abort);
John Garry27a3f222015-11-18 00:50:50 +0800178 int (*slot_complete)(struct hisi_hba *hisi_hba,
John Garry405314d2017-03-23 01:25:21 +0800179 struct hisi_sas_slot *slot);
John Garry396b8042017-03-23 01:25:19 +0800180 void (*phys_init)(struct hisi_hba *hisi_hba);
John Garrye4189d52015-11-18 00:50:57 +0800181 void (*phy_enable)(struct hisi_hba *hisi_hba, int phy_no);
182 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
183 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
Xiang Chen2ae75782016-11-07 20:48:40 +0800184 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
185 struct sas_phy_linkrates *linkrates);
186 enum sas_linkrate (*phy_get_max_linkrate)(void);
John Garry27a3f222015-11-18 00:50:50 +0800187 void (*free_device)(struct hisi_hba *hisi_hba,
188 struct hisi_sas_device *dev);
John Garry184a4632015-11-18 00:50:52 +0800189 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800190 int (*soft_reset)(struct hisi_hba *hisi_hba);
John Garrya8d547b2016-01-26 02:47:03 +0800191 int max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +0800192 int complete_hdr_size;
John Garry7eb78692015-11-18 00:50:31 +0800193};
194
195struct hisi_hba {
196 /* This must be the first element, used by SHOST_TO_SAS_HA */
197 struct sas_ha_struct *p;
198
199 struct platform_device *pdev;
John Garrye26b2f42015-11-18 00:50:32 +0800200 void __iomem *regs;
201 struct regmap *ctrl;
202 u32 ctrl_reset_reg;
203 u32 ctrl_reset_sts_reg;
204 u32 ctrl_clock_ena_reg;
John Garry3bc45af2016-10-04 19:11:11 +0800205 u32 refclk_frequency_mhz;
John Garry7eb78692015-11-18 00:50:31 +0800206 u8 sas_addr[SAS_ADDR_SIZE];
207
208 int n_phy;
John Garryfa42d802015-11-18 00:50:43 +0800209 spinlock_t lock;
John Garry7eb78692015-11-18 00:50:31 +0800210
John Garryfa42d802015-11-18 00:50:43 +0800211 struct timer_list timer;
John Garry7e9080e2015-11-18 00:50:40 +0800212 struct workqueue_struct *wq;
John Garry257efd12015-11-18 00:50:36 +0800213
214 int slot_index_count;
215 unsigned long *slot_index_tags;
Xiaofei Tanc7b9d362017-04-10 21:21:56 +0800216 unsigned long reject_stp_links_msk;
John Garry257efd12015-11-18 00:50:36 +0800217
John Garry7eb78692015-11-18 00:50:31 +0800218 /* SCSI/SAS glue */
219 struct sas_ha_struct sha;
220 struct Scsi_Host *shost;
John Garry9101a072015-11-18 00:50:37 +0800221
222 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
John Garry4fde02a2016-09-06 23:36:12 +0800223 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
John Garry7eb78692015-11-18 00:50:31 +0800224 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
225 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
John Garrye26b2f42015-11-18 00:50:32 +0800226
227 int queue_count;
John Garry6be6de12015-11-18 00:50:34 +0800228
229 struct dma_pool *sge_page_pool;
John Garryaf740db2015-11-18 00:50:41 +0800230 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
John Garry6be6de12015-11-18 00:50:34 +0800231 struct dma_pool *command_table_pool;
232 struct dma_pool *status_buffer_pool;
233 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
234 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
235 void *complete_hdr[HISI_SAS_MAX_QUEUES];
236 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
237 struct hisi_sas_initial_fis *initial_fis;
238 dma_addr_t initial_fis_dma;
239 struct hisi_sas_itct *itct;
240 dma_addr_t itct_dma;
241 struct hisi_sas_iost *iost;
242 dma_addr_t iost_dma;
243 struct hisi_sas_breakpoint *breakpoint;
244 dma_addr_t breakpoint_dma;
245 struct hisi_sas_breakpoint *sata_breakpoint;
246 dma_addr_t sata_breakpoint_dma;
247 struct hisi_sas_slot *slot_info;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800248 unsigned long flags;
John Garry7eb78692015-11-18 00:50:31 +0800249 const struct hisi_sas_hw *hw; /* Low level hw interface */
Xiaofei Tan32ccba52017-04-10 21:21:57 +0800250 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800251 struct work_struct rst_work;
John Garry7eb78692015-11-18 00:50:31 +0800252};
253
John Garryc799d6b2015-11-18 00:50:33 +0800254/* Generic HW DMA host memory structures */
255/* Delivery queue header */
256struct hisi_sas_cmd_hdr {
257 /* dw0 */
258 __le32 dw0;
259
260 /* dw1 */
261 __le32 dw1;
262
263 /* dw2 */
264 __le32 dw2;
265
266 /* dw3 */
267 __le32 transfer_tags;
268
269 /* dw4 */
270 __le32 data_transfer_len;
271
272 /* dw5 */
273 __le32 first_burst_num;
274
275 /* dw6 */
276 __le32 sg_len;
277
278 /* dw7 */
279 __le32 dw7;
280
281 /* dw8-9 */
282 __le64 cmd_table_addr;
283
284 /* dw10-11 */
285 __le64 sts_buffer_addr;
286
287 /* dw12-13 */
288 __le64 prd_table_addr;
289
290 /* dw14-15 */
291 __le64 dif_prd_table_addr;
292};
293
294struct hisi_sas_itct {
295 __le64 qw0;
296 __le64 sas_addr;
297 __le64 qw2;
298 __le64 qw3;
John Garry281e3bf2016-01-26 02:47:06 +0800299 __le64 qw4_15[12];
John Garryc799d6b2015-11-18 00:50:33 +0800300};
301
302struct hisi_sas_iost {
303 __le64 qw0;
304 __le64 qw1;
305 __le64 qw2;
306 __le64 qw3;
307};
308
309struct hisi_sas_err_record {
John Garry8d1eee72016-01-26 02:47:05 +0800310 u32 data[4];
John Garryc799d6b2015-11-18 00:50:33 +0800311};
312
313struct hisi_sas_initial_fis {
314 struct hisi_sas_err_record err_record;
315 struct dev_to_host_fis fis;
316 u32 rsvd[3];
317};
318
319struct hisi_sas_breakpoint {
320 u8 data[128]; /*io128 byte*/
321};
322
323struct hisi_sas_sge {
324 __le64 addr;
325 __le32 page_ctrl_0;
326 __le32 page_ctrl_1;
327 __le32 data_len;
328 __le32 data_off;
329};
330
331struct hisi_sas_command_table_smp {
332 u8 bytes[44];
333};
334
335struct hisi_sas_command_table_stp {
336 struct host_to_dev_fis command_fis;
337 u8 dummy[12];
338 u8 atapi_cdb[ATAPI_CDB_LEN];
339};
340
Ming Lin65e86172016-04-04 14:48:10 -0700341#define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
John Garryc799d6b2015-11-18 00:50:33 +0800342struct hisi_sas_sge_page {
343 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
344};
345
346struct hisi_sas_command_table_ssp {
347 struct ssp_frame_hdr hdr;
348 union {
349 struct {
350 struct ssp_command_iu task;
351 u32 prot[6];
352 };
353 struct ssp_tmf_iu ssp_task;
354 struct xfer_rdy_iu xfer_rdy;
355 struct ssp_response_iu ssp_res;
356 } u;
357};
358
359union hisi_sas_command_table {
360 struct hisi_sas_command_table_ssp ssp;
361 struct hisi_sas_command_table_smp smp;
362 struct hisi_sas_command_table_stp stp;
363};
John Garry2e244f02017-03-23 01:25:17 +0800364
Xiang Chen6c7bb8a2017-06-14 23:33:14 +0800365extern u8 hisi_sas_get_ata_protocol(u8 cmd, int direction);
John Garry2e244f02017-03-23 01:25:17 +0800366extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
Xiang Chen75904072017-06-14 23:33:15 +0800367extern void hisi_sas_sata_done(struct sas_task *task,
368 struct hisi_sas_slot *slot);
Xiang Chen318913c2017-06-14 23:33:16 +0800369extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
John Garry9fb10b52015-11-18 00:50:44 +0800370extern int hisi_sas_probe(struct platform_device *pdev,
371 const struct hisi_sas_hw *ops);
372extern int hisi_sas_remove(struct platform_device *pdev);
John Garryc799d6b2015-11-18 00:50:33 +0800373
John Garry184a4632015-11-18 00:50:52 +0800374extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
John Garry27a3f222015-11-18 00:50:50 +0800375extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
376 struct sas_task *task,
377 struct hisi_sas_slot *slot);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800378extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
379extern void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
380 u32 state);
John Garrye8899fa2015-11-18 00:50:30 +0800381#endif