blob: a722f2bd72abf0686453947599ce8f6fd6daf6bf [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 Garry11b75242017-06-14 23:33:17 +080021#include <linux/pci.h>
John Garrye8899fa2015-11-18 00:50:30 +080022#include <linux/platform_device.h>
John Garry4d558c72016-02-04 02:26:08 +080023#include <linux/property.h>
John Garrye8899fa2015-11-18 00:50:30 +080024#include <linux/regmap.h>
John Garry6f2ff1a2016-01-26 02:47:20 +080025#include <scsi/sas_ata.h>
John Garrye8899fa2015-11-18 00:50:30 +080026#include <scsi/libsas.h>
27
John Garry6328d902016-08-24 19:05:54 +080028#define DRV_VERSION "v1.6"
John Garrye8899fa2015-11-18 00:50:30 +080029
John Garry7eb78692015-11-18 00:50:31 +080030#define HISI_SAS_MAX_PHYS 9
John Garry6be6de12015-11-18 00:50:34 +080031#define HISI_SAS_MAX_QUEUES 32
32#define HISI_SAS_QUEUE_SLOTS 512
John Garry5560e9f2016-01-26 02:47:04 +080033#define HISI_SAS_MAX_ITCT_ENTRIES 2048
John Garry7eb78692015-11-18 00:50:31 +080034#define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
Xiang Chen06ec0fb2017-03-23 01:25:18 +080035#define HISI_SAS_RESET_BIT 0
John Garry7eb78692015-11-18 00:50:31 +080036
Xiaofei Tanf557e322017-06-29 21:02:14 +080037#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)
John Garry6be6de12015-11-18 00:50:34 +080055
John Garry42e7a692015-11-18 00:50:49 +080056#define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
John Garry66ee9992015-11-18 00:50:54 +080057#define HISI_SAS_MAX_SMP_RESP_SZ 1028
John Garry6f2ff1a2016-01-26 02:47:20 +080058#define HISI_SAS_MAX_STP_RESP_SZ 28
John Garry42e7a692015-11-18 00:50:49 +080059
John Garry98bf39f2016-01-26 02:47:02 +080060#define DEV_IS_EXPANDER(type) \
61 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
62 (type == SAS_FANOUT_EXPANDER_DEVICE))
63
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080064#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
John Garryabda97c2015-11-18 00:50:51 +080070struct hisi_hba;
John Garryaf740db2015-11-18 00:50:41 +080071
John Garry07d78592015-11-18 00:50:47 +080072enum {
73 PORT_TYPE_SAS = (1U << 1),
74 PORT_TYPE_SATA = (1U << 0),
75};
76
John Garryaf740db2015-11-18 00:50:41 +080077enum dev_status {
78 HISI_SAS_DEV_NORMAL,
79 HISI_SAS_DEV_EH,
80};
John Garryabda97c2015-11-18 00:50:51 +080081
John Garry441c2742016-08-24 19:05:47 +080082enum {
83 HISI_SAS_INT_ABT_CMD = 0,
84 HISI_SAS_INT_ABT_DEV = 1,
85};
86
John Garryabda97c2015-11-18 00:50:51 +080087enum 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
John Garry7eb78692015-11-18 00:50:31 +080093struct hisi_sas_phy {
John Garry976867e2015-11-18 00:50:42 +080094 struct hisi_hba *hisi_hba;
95 struct hisi_sas_port *port;
John Garry7eb78692015-11-18 00:50:31 +080096 struct asd_sas_phy sas_phy;
John Garry976867e2015-11-18 00:50:42 +080097 struct sas_identify identify;
98 struct timer_list timer;
John Garry66139922015-11-18 00:50:48 +080099 struct work_struct phyup_ws;
John Garry976867e2015-11-18 00:50:42 +0800100 u64 port_id; /* from hw */
John Garry5d742422015-11-18 00:50:38 +0800101 u64 dev_sas_addr;
John Garry976867e2015-11-18 00:50:42 +0800102 u64 frame_rcvd_size;
103 u8 frame_rcvd[32];
104 u8 phy_attached;
105 u8 reserved[3];
John Garryd0ef10c2017-06-26 18:27:28 +0800106 u32 phy_type;
John Garry976867e2015-11-18 00:50:42 +0800107 enum sas_linkrate minimum_linkrate;
108 enum sas_linkrate maximum_linkrate;
John Garry7eb78692015-11-18 00:50:31 +0800109};
110
111struct hisi_sas_port {
112 struct asd_sas_port sas_port;
John Garry976867e2015-11-18 00:50:42 +0800113 u8 port_attached;
114 u8 id; /* from hw */
John Garry7eb78692015-11-18 00:50:31 +0800115};
116
John Garry9101a072015-11-18 00:50:37 +0800117struct hisi_sas_cq {
118 struct hisi_hba *hisi_hba;
John Garryd177c402017-01-03 20:24:48 +0800119 struct tasklet_struct tasklet;
John Garrye6c346f2016-09-06 23:36:11 +0800120 int rd_point;
John Garry9101a072015-11-18 00:50:37 +0800121 int id;
122};
123
John Garry4fde02a2016-09-06 23:36:12 +0800124struct hisi_sas_dq {
125 struct hisi_hba *hisi_hba;
Xiang Chenb1a49412017-06-14 23:33:13 +0800126 struct hisi_sas_slot *slot_prep;
127 spinlock_t lock;
John Garry4fde02a2016-09-06 23:36:12 +0800128 int wr_point;
129 int id;
130};
131
John Garryaf740db2015-11-18 00:50:41 +0800132struct hisi_sas_device {
John Garryabda97c2015-11-18 00:50:51 +0800133 struct hisi_hba *hisi_hba;
134 struct domain_device *sas_device;
Xiang Chenb1a49412017-06-14 23:33:13 +0800135 struct hisi_sas_dq *dq;
John Garry405314d2017-03-23 01:25:21 +0800136 struct list_head list;
John Garryad604832017-06-14 23:33:12 +0800137 u64 attached_phy;
138 atomic64_t running_req;
139 enum sas_device_type dev_type;
140 int device_id;
Xiaofei Tan32ccba52017-04-10 21:21:57 +0800141 int sata_idx;
John Garryad604832017-06-14 23:33:12 +0800142 u8 dev_status;
John Garryaf740db2015-11-18 00:50:41 +0800143};
144
John Garry6be6de12015-11-18 00:50:34 +0800145struct hisi_sas_slot {
John Garry42e7a692015-11-18 00:50:49 +0800146 struct list_head entry;
147 struct sas_task *task;
148 struct hisi_sas_port *port;
149 u64 n_elem;
150 int dlvry_queue;
151 int dlvry_queue_slot;
John Garry27a3f222015-11-18 00:50:50 +0800152 int cmplt_queue;
153 int cmplt_queue_slot;
John Garry42e7a692015-11-18 00:50:49 +0800154 int idx;
John Garrycac9b2a2016-02-25 17:42:11 +0800155 int abort;
Xiaofei Tanf557e322017-06-29 21:02:14 +0800156 void *buf;
157 dma_addr_t buf_dma;
John Garry42e7a692015-11-18 00:50:49 +0800158 void *cmd_hdr;
159 dma_addr_t cmd_hdr_dma;
John Garrycac9b2a2016-02-25 17:42:11 +0800160 struct work_struct abort_slot;
John Garry0844a3f2017-04-10 21:21:59 +0800161 struct timer_list internal_abort_timer;
John Garry42e7a692015-11-18 00:50:49 +0800162};
163
164struct hisi_sas_tmf_task {
165 u8 tmf;
166 u16 tag_of_task_to_be_managed;
John Garry6be6de12015-11-18 00:50:34 +0800167};
168
John Garry7eb78692015-11-18 00:50:31 +0800169struct hisi_sas_hw {
John Garry8ff1d572015-11-18 00:50:46 +0800170 int (*hw_init)(struct hisi_hba *hisi_hba);
John Garryabda97c2015-11-18 00:50:51 +0800171 void (*setup_itct)(struct hisi_hba *hisi_hba,
172 struct hisi_sas_device *device);
John Garry685b6d62016-04-15 21:36:36 +0800173 int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
174 struct domain_device *device);
175 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
John Garry66139922015-11-18 00:50:48 +0800176 void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
Xiang Chenb1a49412017-06-14 23:33:13 +0800177 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
178 void (*start_delivery)(struct hisi_sas_dq *dq);
John Garry42e7a692015-11-18 00:50:49 +0800179 int (*prep_ssp)(struct hisi_hba *hisi_hba,
180 struct hisi_sas_slot *slot, int is_tmf,
181 struct hisi_sas_tmf_task *tmf);
John Garry66ee9992015-11-18 00:50:54 +0800182 int (*prep_smp)(struct hisi_hba *hisi_hba,
183 struct hisi_sas_slot *slot);
John Garry6f2ff1a2016-01-26 02:47:20 +0800184 int (*prep_stp)(struct hisi_hba *hisi_hba,
185 struct hisi_sas_slot *slot);
John Garry441c2742016-08-24 19:05:47 +0800186 int (*prep_abort)(struct hisi_hba *hisi_hba,
187 struct hisi_sas_slot *slot,
188 int device_id, int abort_flag, int tag_to_abort);
John Garry27a3f222015-11-18 00:50:50 +0800189 int (*slot_complete)(struct hisi_hba *hisi_hba,
John Garry405314d2017-03-23 01:25:21 +0800190 struct hisi_sas_slot *slot);
John Garry396b8042017-03-23 01:25:19 +0800191 void (*phys_init)(struct hisi_hba *hisi_hba);
John Garrye4189d52015-11-18 00:50:57 +0800192 void (*phy_enable)(struct hisi_hba *hisi_hba, int phy_no);
193 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
194 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
Xiang Chen2ae75782016-11-07 20:48:40 +0800195 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
196 struct sas_phy_linkrates *linkrates);
197 enum sas_linkrate (*phy_get_max_linkrate)(void);
John Garry27a3f222015-11-18 00:50:50 +0800198 void (*free_device)(struct hisi_hba *hisi_hba,
199 struct hisi_sas_device *dev);
John Garry184a4632015-11-18 00:50:52 +0800200 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
Xiang Chend30ff262017-06-14 23:33:32 +0800201 void (*dereg_device)(struct hisi_hba *hisi_hba,
202 struct domain_device *device);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800203 int (*soft_reset)(struct hisi_hba *hisi_hba);
John Garrya8d547b2016-01-26 02:47:03 +0800204 int max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +0800205 int complete_hdr_size;
John Garry7eb78692015-11-18 00:50:31 +0800206};
207
208struct hisi_hba {
209 /* This must be the first element, used by SHOST_TO_SAS_HA */
210 struct sas_ha_struct *p;
211
John Garry11b75242017-06-14 23:33:17 +0800212 struct platform_device *platform_dev;
213 struct pci_dev *pci_dev;
214 struct device *dev;
215
John Garrye26b2f42015-11-18 00:50:32 +0800216 void __iomem *regs;
217 struct regmap *ctrl;
218 u32 ctrl_reset_reg;
219 u32 ctrl_reset_sts_reg;
220 u32 ctrl_clock_ena_reg;
John Garry3bc45af2016-10-04 19:11:11 +0800221 u32 refclk_frequency_mhz;
John Garry7eb78692015-11-18 00:50:31 +0800222 u8 sas_addr[SAS_ADDR_SIZE];
223
224 int n_phy;
John Garryfa42d802015-11-18 00:50:43 +0800225 spinlock_t lock;
John Garry7eb78692015-11-18 00:50:31 +0800226
John Garryfa42d802015-11-18 00:50:43 +0800227 struct timer_list timer;
John Garry7e9080e2015-11-18 00:50:40 +0800228 struct workqueue_struct *wq;
John Garry257efd12015-11-18 00:50:36 +0800229
230 int slot_index_count;
231 unsigned long *slot_index_tags;
Xiaofei Tanc7b9d362017-04-10 21:21:56 +0800232 unsigned long reject_stp_links_msk;
John Garry257efd12015-11-18 00:50:36 +0800233
John Garry7eb78692015-11-18 00:50:31 +0800234 /* SCSI/SAS glue */
235 struct sas_ha_struct sha;
236 struct Scsi_Host *shost;
John Garry9101a072015-11-18 00:50:37 +0800237
238 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
John Garry4fde02a2016-09-06 23:36:12 +0800239 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
John Garry7eb78692015-11-18 00:50:31 +0800240 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
241 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
John Garrye26b2f42015-11-18 00:50:32 +0800242
243 int queue_count;
John Garry6be6de12015-11-18 00:50:34 +0800244
Xiaofei Tanf557e322017-06-29 21:02:14 +0800245 struct dma_pool *buffer_pool;
John Garryaf740db2015-11-18 00:50:41 +0800246 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
John Garry6be6de12015-11-18 00:50:34 +0800247 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
248 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
249 void *complete_hdr[HISI_SAS_MAX_QUEUES];
250 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
251 struct hisi_sas_initial_fis *initial_fis;
252 dma_addr_t initial_fis_dma;
253 struct hisi_sas_itct *itct;
254 dma_addr_t itct_dma;
255 struct hisi_sas_iost *iost;
256 dma_addr_t iost_dma;
257 struct hisi_sas_breakpoint *breakpoint;
258 dma_addr_t breakpoint_dma;
259 struct hisi_sas_breakpoint *sata_breakpoint;
260 dma_addr_t sata_breakpoint_dma;
261 struct hisi_sas_slot *slot_info;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800262 unsigned long flags;
John Garry7eb78692015-11-18 00:50:31 +0800263 const struct hisi_sas_hw *hw; /* Low level hw interface */
Xiaofei Tan32ccba52017-04-10 21:21:57 +0800264 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800265 struct work_struct rst_work;
John Garry7eb78692015-11-18 00:50:31 +0800266};
267
John Garryc799d6b2015-11-18 00:50:33 +0800268/* Generic HW DMA host memory structures */
269/* Delivery queue header */
270struct hisi_sas_cmd_hdr {
271 /* dw0 */
272 __le32 dw0;
273
274 /* dw1 */
275 __le32 dw1;
276
277 /* dw2 */
278 __le32 dw2;
279
280 /* dw3 */
281 __le32 transfer_tags;
282
283 /* dw4 */
284 __le32 data_transfer_len;
285
286 /* dw5 */
287 __le32 first_burst_num;
288
289 /* dw6 */
290 __le32 sg_len;
291
292 /* dw7 */
293 __le32 dw7;
294
295 /* dw8-9 */
296 __le64 cmd_table_addr;
297
298 /* dw10-11 */
299 __le64 sts_buffer_addr;
300
301 /* dw12-13 */
302 __le64 prd_table_addr;
303
304 /* dw14-15 */
305 __le64 dif_prd_table_addr;
306};
307
308struct hisi_sas_itct {
309 __le64 qw0;
310 __le64 sas_addr;
311 __le64 qw2;
312 __le64 qw3;
John Garry281e3bf2016-01-26 02:47:06 +0800313 __le64 qw4_15[12];
John Garryc799d6b2015-11-18 00:50:33 +0800314};
315
316struct hisi_sas_iost {
317 __le64 qw0;
318 __le64 qw1;
319 __le64 qw2;
320 __le64 qw3;
321};
322
323struct hisi_sas_err_record {
John Garry8d1eee72016-01-26 02:47:05 +0800324 u32 data[4];
John Garryc799d6b2015-11-18 00:50:33 +0800325};
326
327struct hisi_sas_initial_fis {
328 struct hisi_sas_err_record err_record;
329 struct dev_to_host_fis fis;
330 u32 rsvd[3];
331};
332
333struct hisi_sas_breakpoint {
334 u8 data[128]; /*io128 byte*/
335};
336
337struct hisi_sas_sge {
338 __le64 addr;
339 __le32 page_ctrl_0;
340 __le32 page_ctrl_1;
341 __le32 data_len;
342 __le32 data_off;
343};
344
345struct hisi_sas_command_table_smp {
346 u8 bytes[44];
347};
348
349struct hisi_sas_command_table_stp {
350 struct host_to_dev_fis command_fis;
351 u8 dummy[12];
352 u8 atapi_cdb[ATAPI_CDB_LEN];
353};
354
Ming Lin65e86172016-04-04 14:48:10 -0700355#define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
John Garryc799d6b2015-11-18 00:50:33 +0800356struct hisi_sas_sge_page {
357 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
Xiaofei Tanf557e322017-06-29 21:02:14 +0800358} __aligned(16);
John Garryc799d6b2015-11-18 00:50:33 +0800359
360struct hisi_sas_command_table_ssp {
361 struct ssp_frame_hdr hdr;
362 union {
363 struct {
364 struct ssp_command_iu task;
365 u32 prot[6];
366 };
367 struct ssp_tmf_iu ssp_task;
368 struct xfer_rdy_iu xfer_rdy;
369 struct ssp_response_iu ssp_res;
370 } u;
371};
372
373union hisi_sas_command_table {
374 struct hisi_sas_command_table_ssp ssp;
375 struct hisi_sas_command_table_smp smp;
376 struct hisi_sas_command_table_stp stp;
Xiaofei Tanf557e322017-06-29 21:02:14 +0800377} __aligned(16);
378
379struct hisi_sas_status_buffer {
380 struct hisi_sas_err_record err;
381 u8 iu[1024];
382} __aligned(16);
383
384struct hisi_sas_slot_buf_table {
385 struct hisi_sas_status_buffer status_buffer;
386 union hisi_sas_command_table command_header;
387 struct hisi_sas_sge_page sge_page;
John Garryc799d6b2015-11-18 00:50:33 +0800388};
John Garry2e244f02017-03-23 01:25:17 +0800389
John Garrye21fe3a2017-06-14 23:33:20 +0800390extern struct scsi_transport_template *hisi_sas_stt;
391extern struct scsi_host_template *hisi_sas_sht;
392
393extern void hisi_sas_init_add(struct hisi_hba *hisi_hba);
394extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost);
395extern void hisi_sas_free(struct hisi_hba *hisi_hba);
Xiang Chen6c7bb8a2017-06-14 23:33:14 +0800396extern u8 hisi_sas_get_ata_protocol(u8 cmd, int direction);
John Garry2e244f02017-03-23 01:25:17 +0800397extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
Xiang Chen75904072017-06-14 23:33:15 +0800398extern void hisi_sas_sata_done(struct sas_task *task,
399 struct hisi_sas_slot *slot);
Xiang Chen318913c2017-06-14 23:33:16 +0800400extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
John Garry0fa24c12017-06-14 23:33:18 +0800401extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
John Garry9fb10b52015-11-18 00:50:44 +0800402extern int hisi_sas_probe(struct platform_device *pdev,
403 const struct hisi_sas_hw *ops);
404extern int hisi_sas_remove(struct platform_device *pdev);
John Garryc799d6b2015-11-18 00:50:33 +0800405
John Garry184a4632015-11-18 00:50:52 +0800406extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
John Garry27a3f222015-11-18 00:50:50 +0800407extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
408 struct sas_task *task,
409 struct hisi_sas_slot *slot);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800410extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
411extern void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
412 u32 state);
John Garrye8899fa2015-11-18 00:50:30 +0800413#endif