hisi_sas.h 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2015 Linaro Ltd.
 * Copyright (c) 2015 Hisilicon Limited.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#ifndef _HISI_SAS_H_
#define _HISI_SAS_H_

15
#include <linux/acpi.h>
16
#include <linux/clk.h>
17
#include <linux/dmapool.h>
18
#include <linux/iopoll.h>
19 20 21
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#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 28
#include <scsi/libsas.h>

J
John Garry 已提交
29
#define HISI_SAS_MAX_PHYS	9
30 31
#define HISI_SAS_MAX_QUEUES	32
#define HISI_SAS_QUEUE_SLOTS 512
32
#define HISI_SAS_MAX_ITCT_ENTRIES 1024
J
John Garry 已提交
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
J
John Garry 已提交
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
#define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))

#define hisi_sas_status_buf_addr(buf) \
	(buf + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
#define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr(slot->buf)
#define hisi_sas_status_buf_addr_dma(slot) \
	hisi_sas_status_buf_addr(slot->buf_dma)

#define hisi_sas_cmd_hdr_addr(buf) \
	(buf + offsetof(struct hisi_sas_slot_buf_table, command_header))
#define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr(slot->buf)
#define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr(slot->buf_dma)

#define hisi_sas_sge_addr(buf) \
	(buf + offsetof(struct hisi_sas_slot_buf_table, sge_page))
#define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr(slot->buf)
#define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr(slot->buf_dma)
55

J
John Garry 已提交
56
#define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
J
John Garry 已提交
57
#define HISI_SAS_MAX_SMP_RESP_SZ 1028
58
#define HISI_SAS_MAX_STP_RESP_SZ 28
J
John Garry 已提交
59

J
John Garry 已提交
60 61 62 63
#define DEV_IS_EXPANDER(type) \
	((type == SAS_EDGE_EXPANDER_DEVICE) || \
	(type == SAS_FANOUT_EXPANDER_DEVICE))

64 65 66 67 68 69
#define HISI_SAS_SATA_PROTOCOL_NONDATA		0x1
#define HISI_SAS_SATA_PROTOCOL_PIO			0x2
#define HISI_SAS_SATA_PROTOCOL_DMA			0x4
#define HISI_SAS_SATA_PROTOCOL_FPDMA		0x8
#define HISI_SAS_SATA_PROTOCOL_ATAPI		0x10

J
John Garry 已提交
70
struct hisi_hba;
J
John Garry 已提交
71

72 73 74 75 76
enum {
	PORT_TYPE_SAS = (1U << 1),
	PORT_TYPE_SATA = (1U << 0),
};

J
John Garry 已提交
77 78 79 80
enum dev_status {
	HISI_SAS_DEV_NORMAL,
	HISI_SAS_DEV_EH,
};
J
John Garry 已提交
81

82 83 84 85 86
enum {
	HISI_SAS_INT_ABT_CMD = 0,
	HISI_SAS_INT_ABT_DEV = 1,
};

J
John Garry 已提交
87 88 89 90 91 92
enum hisi_sas_dev_type {
	HISI_SAS_DEV_TYPE_STP = 0,
	HISI_SAS_DEV_TYPE_SSP,
	HISI_SAS_DEV_TYPE_SATA,
};

93 94 95 96 97 98
struct hisi_sas_hw_error {
	u32 irq_msk;
	u32 msk;
	int shift;
	const char *msg;
	int reg;
99
	const struct hisi_sas_hw_error *sub;
100 101
};

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
struct hisi_sas_rst {
	struct hisi_hba *hisi_hba;
	struct completion *completion;
	struct work_struct work;
	bool done;
};

#define HISI_SAS_RST_WORK_INIT(r, c) \
	{	.hisi_hba = hisi_hba, \
		.completion = &c, \
		.work = __WORK_INITIALIZER(r.work, \
				hisi_sas_sync_rst_work_handler), \
		.done = false, \
		}

#define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \
	DECLARE_COMPLETION_ONSTACK(c); \
	DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \
	struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c)

enum hisi_sas_bit_err_type {
	HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
	HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
};

127 128
enum hisi_sas_phy_event {
	HISI_PHYE_PHY_UP   = 0U,
129
	HISI_PHYE_LINK_RESET,
130 131 132
	HISI_PHYES_NUM,
};

J
John Garry 已提交
133
struct hisi_sas_phy {
134
	struct work_struct	works[HISI_PHYES_NUM];
J
John Garry 已提交
135 136
	struct hisi_hba	*hisi_hba;
	struct hisi_sas_port	*port;
J
John Garry 已提交
137
	struct asd_sas_phy	sas_phy;
J
John Garry 已提交
138 139
	struct sas_identify	identify;
	u64		port_id; /* from hw */
140
	u64		dev_sas_addr;
J
John Garry 已提交
141 142 143 144
	u64		frame_rcvd_size;
	u8		frame_rcvd[32];
	u8		phy_attached;
	u8		reserved[3];
145
	u32		phy_type;
J
John Garry 已提交
146 147
	enum sas_linkrate	minimum_linkrate;
	enum sas_linkrate	maximum_linkrate;
J
John Garry 已提交
148 149 150 151
};

struct hisi_sas_port {
	struct asd_sas_port	sas_port;
J
John Garry 已提交
152 153
	u8	port_attached;
	u8	id; /* from hw */
J
John Garry 已提交
154 155
};

156 157
struct hisi_sas_cq {
	struct hisi_hba *hisi_hba;
158
	struct tasklet_struct tasklet;
159
	int	rd_point;
160 161 162
	int	id;
};

163 164
struct hisi_sas_dq {
	struct hisi_hba *hisi_hba;
165 166
	struct hisi_sas_slot	*slot_prep;
	spinlock_t lock;
167 168 169 170
	int	wr_point;
	int	id;
};

J
John Garry 已提交
171
struct hisi_sas_device {
J
John Garry 已提交
172 173
	struct hisi_hba		*hisi_hba;
	struct domain_device	*sas_device;
174
	struct completion *completion;
175
	struct hisi_sas_dq	*dq;
176
	struct list_head	list;
J
John Garry 已提交
177
	u64 attached_phy;
178 179
	enum sas_device_type	dev_type;
	int device_id;
180
	int sata_idx;
181
	u8 dev_status;
J
John Garry 已提交
182 183
};

184
struct hisi_sas_slot {
J
John Garry 已提交
185 186 187 188 189 190
	struct list_head entry;
	struct sas_task *task;
	struct hisi_sas_port	*port;
	u64	n_elem;
	int	dlvry_queue;
	int	dlvry_queue_slot;
J
John Garry 已提交
191 192
	int	cmplt_queue;
	int	cmplt_queue_slot;
J
John Garry 已提交
193
	int	idx;
J
John Garry 已提交
194
	int	abort;
195 196
	void	*buf;
	dma_addr_t buf_dma;
J
John Garry 已提交
197 198
	void	*cmd_hdr;
	dma_addr_t cmd_hdr_dma;
J
John Garry 已提交
199
	struct work_struct abort_slot;
200
	struct timer_list internal_abort_timer;
J
John Garry 已提交
201 202 203 204 205
};

struct hisi_sas_tmf_task {
	u8 tmf;
	u16 tag_of_task_to_be_managed;
206 207
};

J
John Garry 已提交
208
struct hisi_sas_hw {
209
	int (*hw_init)(struct hisi_hba *hisi_hba);
J
John Garry 已提交
210 211
	void (*setup_itct)(struct hisi_hba *hisi_hba,
			   struct hisi_sas_device *device);
212 213 214
	int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
				struct domain_device *device);
	struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
215
	void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
216 217
	int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
	void (*start_delivery)(struct hisi_sas_dq *dq);
J
John Garry 已提交
218 219 220
	int (*prep_ssp)(struct hisi_hba *hisi_hba,
			struct hisi_sas_slot *slot, int is_tmf,
			struct hisi_sas_tmf_task *tmf);
J
John Garry 已提交
221 222
	int (*prep_smp)(struct hisi_hba *hisi_hba,
			struct hisi_sas_slot *slot);
223 224
	int (*prep_stp)(struct hisi_hba *hisi_hba,
			struct hisi_sas_slot *slot);
225 226 227
	int (*prep_abort)(struct hisi_hba *hisi_hba,
			  struct hisi_sas_slot *slot,
			  int device_id, int abort_flag, int tag_to_abort);
J
John Garry 已提交
228
	int (*slot_complete)(struct hisi_hba *hisi_hba,
229
			     struct hisi_sas_slot *slot);
230
	void (*phys_init)(struct hisi_hba *hisi_hba);
231
	void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
J
John Garry 已提交
232 233
	void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
	void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
234
	void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
235 236 237
	void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
			struct sas_phy_linkrates *linkrates);
	enum sas_linkrate (*phy_get_max_linkrate)(void);
238
	void (*clear_itct)(struct hisi_hba *hisi_hba,
J
John Garry 已提交
239
			    struct hisi_sas_device *dev);
240
	void (*free_device)(struct hisi_sas_device *sas_dev);
J
John Garry 已提交
241
	int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
242 243
	void (*dereg_device)(struct hisi_hba *hisi_hba,
				struct domain_device *device);
244
	int (*soft_reset)(struct hisi_hba *hisi_hba);
245
	u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
246 247
	int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type,
				u8 reg_index, u8 reg_count, u8 *write_data);
248
	int max_command_entries;
249
	int complete_hdr_size;
J
John Garry 已提交
250 251 252 253 254 255
};

struct hisi_hba {
	/* This must be the first element, used by SHOST_TO_SAS_HA */
	struct sas_ha_struct *p;

256 257 258 259
	struct platform_device *platform_dev;
	struct pci_dev *pci_dev;
	struct device *dev;

J
John Garry 已提交
260
	void __iomem *regs;
261
	void __iomem *sgpio_regs;
J
John Garry 已提交
262 263 264 265
	struct regmap *ctrl;
	u32 ctrl_reset_reg;
	u32 ctrl_reset_sts_reg;
	u32 ctrl_clock_ena_reg;
266
	u32 refclk_frequency_mhz;
J
John Garry 已提交
267 268 269
	u8 sas_addr[SAS_ADDR_SIZE];

	int n_phy;
J
John Garry 已提交
270
	spinlock_t lock;
J
John Garry 已提交
271

J
John Garry 已提交
272
	struct timer_list timer;
J
John Garry 已提交
273
	struct workqueue_struct *wq;
J
John Garry 已提交
274 275 276

	int slot_index_count;
	unsigned long *slot_index_tags;
277
	unsigned long reject_stp_links_msk;
J
John Garry 已提交
278

J
John Garry 已提交
279 280 281
	/* SCSI/SAS glue */
	struct sas_ha_struct sha;
	struct Scsi_Host *shost;
282 283

	struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
284
	struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
J
John Garry 已提交
285 286
	struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
	struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
J
John Garry 已提交
287 288

	int	queue_count;
289

290
	struct dma_pool *buffer_pool;
J
John Garry 已提交
291
	struct hisi_sas_device	devices[HISI_SAS_MAX_DEVICES];
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	struct hisi_sas_cmd_hdr	*cmd_hdr[HISI_SAS_MAX_QUEUES];
	dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
	void *complete_hdr[HISI_SAS_MAX_QUEUES];
	dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
	struct hisi_sas_initial_fis *initial_fis;
	dma_addr_t initial_fis_dma;
	struct hisi_sas_itct *itct;
	dma_addr_t itct_dma;
	struct hisi_sas_iost *iost;
	dma_addr_t iost_dma;
	struct hisi_sas_breakpoint *breakpoint;
	dma_addr_t breakpoint_dma;
	struct hisi_sas_breakpoint *sata_breakpoint;
	dma_addr_t sata_breakpoint_dma;
	struct hisi_sas_slot	*slot_info;
307
	unsigned long flags;
J
John Garry 已提交
308
	const struct hisi_sas_hw *hw;	/* Low level hw interface */
309
	unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
310
	struct work_struct rst_work;
J
John Garry 已提交
311 312
};

J
John Garry 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/* Generic HW DMA host memory structures */
/* Delivery queue header */
struct hisi_sas_cmd_hdr {
	/* dw0 */
	__le32 dw0;

	/* dw1 */
	__le32 dw1;

	/* dw2 */
	__le32 dw2;

	/* dw3 */
	__le32 transfer_tags;

	/* dw4 */
	__le32 data_transfer_len;

	/* dw5 */
	__le32 first_burst_num;

	/* dw6 */
	__le32 sg_len;

	/* dw7 */
	__le32 dw7;

	/* dw8-9 */
	__le64 cmd_table_addr;

	/* dw10-11 */
	__le64 sts_buffer_addr;

	/* dw12-13 */
	__le64 prd_table_addr;

	/* dw14-15 */
	__le64 dif_prd_table_addr;
};

struct hisi_sas_itct {
	__le64 qw0;
	__le64 sas_addr;
	__le64 qw2;
	__le64 qw3;
358
	__le64 qw4_15[12];
J
John Garry 已提交
359 360 361 362 363 364 365 366 367 368
};

struct hisi_sas_iost {
	__le64 qw0;
	__le64 qw1;
	__le64 qw2;
	__le64 qw3;
};

struct hisi_sas_err_record {
369
	u32	data[4];
J
John Garry 已提交
370 371 372 373 374 375 376 377 378
};

struct hisi_sas_initial_fis {
	struct hisi_sas_err_record err_record;
	struct dev_to_host_fis fis;
	u32 rsvd[3];
};

struct hisi_sas_breakpoint {
379 380 381 382 383
	u8	data[128];
};

struct hisi_sas_sata_breakpoint {
	struct hisi_sas_breakpoint tag[32];
J
John Garry 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
};

struct hisi_sas_sge {
	__le64 addr;
	__le32 page_ctrl_0;
	__le32 page_ctrl_1;
	__le32 data_len;
	__le32 data_off;
};

struct hisi_sas_command_table_smp {
	u8 bytes[44];
};

struct hisi_sas_command_table_stp {
	struct	host_to_dev_fis command_fis;
	u8	dummy[12];
	u8	atapi_cdb[ATAPI_CDB_LEN];
};

404
#define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
J
John Garry 已提交
405 406
struct hisi_sas_sge_page {
	struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
407
}  __aligned(16);
J
John Garry 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

struct hisi_sas_command_table_ssp {
	struct ssp_frame_hdr hdr;
	union {
		struct {
			struct ssp_command_iu task;
			u32 prot[6];
		};
		struct ssp_tmf_iu ssp_task;
		struct xfer_rdy_iu xfer_rdy;
		struct ssp_response_iu ssp_res;
	} u;
};

union hisi_sas_command_table {
	struct hisi_sas_command_table_ssp ssp;
	struct hisi_sas_command_table_smp smp;
	struct hisi_sas_command_table_stp stp;
426 427 428 429 430 431 432 433 434 435 436
}  __aligned(16);

struct hisi_sas_status_buffer {
	struct hisi_sas_err_record err;
	u8	iu[1024];
}  __aligned(16);

struct hisi_sas_slot_buf_table {
	struct hisi_sas_status_buffer status_buffer;
	union hisi_sas_command_table command_header;
	struct hisi_sas_sge_page sge_page;
J
John Garry 已提交
437
};
438

439 440 441
extern struct scsi_transport_template *hisi_sas_stt;
extern struct scsi_host_template *hisi_sas_sht;

442
extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
443 444 445
extern void hisi_sas_init_add(struct hisi_hba *hisi_hba);
extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost);
extern void hisi_sas_free(struct hisi_hba *hisi_hba);
446 447
extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis,
				int direction);
448
extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
449 450
extern void hisi_sas_sata_done(struct sas_task *task,
			    struct hisi_sas_slot *slot);
451
extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
452
extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
J
John Garry 已提交
453 454 455
extern int hisi_sas_probe(struct platform_device *pdev,
			  const struct hisi_sas_hw *ops);
extern int hisi_sas_remove(struct platform_device *pdev);
J
John Garry 已提交
456

J
John Garry 已提交
457
extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
J
John Garry 已提交
458 459 460
extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
				    struct sas_task *task,
				    struct hisi_sas_slot *slot);
461
extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
462
extern void hisi_sas_rst_work_handler(struct work_struct *work);
463
extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
464
extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
465 466
extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
				enum hisi_sas_phy_event event);
467
extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba);
468
#endif