2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
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.
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>
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
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))
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)
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)
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)
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
60 #define DEV_IS_EXPANDER(type) \
61 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
62 (type == SAS_FANOUT_EXPANDER_DEVICE))
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
73 PORT_TYPE_SAS = (1U << 1),
74 PORT_TYPE_SATA = (1U << 0),
83 HISI_SAS_INT_ABT_CMD = 0,
84 HISI_SAS_INT_ABT_DEV = 1,
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,
93 struct hisi_sas_hw_error {
99 const struct hisi_sas_hw_error *sub;
102 struct hisi_sas_rst {
103 struct hisi_hba *hisi_hba;
104 struct completion *completion;
105 struct work_struct work;
109 #define HISI_SAS_RST_WORK_INIT(r, c) \
110 { .hisi_hba = hisi_hba, \
112 .work = __WORK_INITIALIZER(r.work, \
113 hisi_sas_sync_rst_work_handler), \
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)
122 enum hisi_sas_bit_err_type {
123 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0,
124 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1,
127 enum hisi_sas_phy_event {
128 HISI_PHYE_PHY_UP = 0U,
129 HISI_PHYE_LINK_RESET,
133 struct hisi_sas_phy {
134 struct work_struct works[HISI_PHYES_NUM];
135 struct hisi_hba *hisi_hba;
136 struct hisi_sas_port *port;
137 struct asd_sas_phy sas_phy;
138 struct sas_identify identify;
139 u64 port_id; /* from hw */
145 enum sas_linkrate minimum_linkrate;
146 enum sas_linkrate maximum_linkrate;
149 struct hisi_sas_port {
150 struct asd_sas_port sas_port;
156 struct hisi_hba *hisi_hba;
157 struct tasklet_struct tasklet;
163 struct hisi_hba *hisi_hba;
164 struct list_head list;
170 struct hisi_sas_device {
171 struct hisi_hba *hisi_hba;
172 struct domain_device *sas_device;
173 struct completion *completion;
174 struct hisi_sas_dq *dq;
175 struct list_head list;
176 enum sas_device_type dev_type;
182 struct hisi_sas_slot {
183 struct list_head entry;
184 struct list_head delivery;
185 struct sas_task *task;
186 struct hisi_sas_port *port;
189 int dlvry_queue_slot;
191 int cmplt_queue_slot;
198 dma_addr_t cmd_hdr_dma;
199 struct work_struct abort_slot;
200 struct timer_list internal_abort_timer;
204 struct hisi_sas_tmf_task {
206 u16 tag_of_task_to_be_managed;
210 int (*hw_init)(struct hisi_hba *hisi_hba);
211 void (*setup_itct)(struct hisi_hba *hisi_hba,
212 struct hisi_sas_device *device);
213 int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
214 struct domain_device *device);
215 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
216 void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
217 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
218 void (*start_delivery)(struct hisi_sas_dq *dq);
219 void (*prep_ssp)(struct hisi_hba *hisi_hba,
220 struct hisi_sas_slot *slot, int is_tmf,
221 struct hisi_sas_tmf_task *tmf);
222 void (*prep_smp)(struct hisi_hba *hisi_hba,
223 struct hisi_sas_slot *slot);
224 void (*prep_stp)(struct hisi_hba *hisi_hba,
225 struct hisi_sas_slot *slot);
226 void (*prep_abort)(struct hisi_hba *hisi_hba,
227 struct hisi_sas_slot *slot,
228 int device_id, int abort_flag, int tag_to_abort);
229 int (*slot_complete)(struct hisi_hba *hisi_hba,
230 struct hisi_sas_slot *slot);
231 void (*phys_init)(struct hisi_hba *hisi_hba);
232 void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
233 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
234 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
235 void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
236 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
237 struct sas_phy_linkrates *linkrates);
238 enum sas_linkrate (*phy_get_max_linkrate)(void);
239 void (*clear_itct)(struct hisi_hba *hisi_hba,
240 struct hisi_sas_device *dev);
241 void (*free_device)(struct hisi_sas_device *sas_dev);
242 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
243 void (*dereg_device)(struct hisi_hba *hisi_hba,
244 struct domain_device *device);
245 int (*soft_reset)(struct hisi_hba *hisi_hba);
246 u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
247 int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type,
248 u8 reg_index, u8 reg_count, u8 *write_data);
249 void (*wait_cmds_complete_timeout)(struct hisi_hba *hisi_hba,
250 int delay_ms, int timeout_ms);
251 int max_command_entries;
252 int complete_hdr_size;
253 struct scsi_host_template *sht;
257 /* This must be the first element, used by SHOST_TO_SAS_HA */
258 struct sas_ha_struct *p;
260 struct platform_device *platform_dev;
261 struct pci_dev *pci_dev;
265 void __iomem *sgpio_regs;
268 u32 ctrl_reset_sts_reg;
269 u32 ctrl_clock_ena_reg;
270 u32 refclk_frequency_mhz;
271 u8 sas_addr[SAS_ADDR_SIZE];
276 struct timer_list timer;
277 struct workqueue_struct *wq;
279 int slot_index_count;
282 unsigned long *slot_index_tags;
283 unsigned long reject_stp_links_msk;
286 struct sas_ha_struct sha;
287 struct Scsi_Host *shost;
289 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
290 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
291 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
292 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
296 struct dma_pool *buffer_pool;
297 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
298 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
299 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
300 void *complete_hdr[HISI_SAS_MAX_QUEUES];
301 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
302 struct hisi_sas_initial_fis *initial_fis;
303 dma_addr_t initial_fis_dma;
304 struct hisi_sas_itct *itct;
306 struct hisi_sas_iost *iost;
308 struct hisi_sas_breakpoint *breakpoint;
309 dma_addr_t breakpoint_dma;
310 struct hisi_sas_breakpoint *sata_breakpoint;
311 dma_addr_t sata_breakpoint_dma;
312 struct hisi_sas_slot *slot_info;
314 const struct hisi_sas_hw *hw; /* Low level hw interface */
315 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
316 struct work_struct rst_work;
319 /* Generic HW DMA host memory structures */
320 /* Delivery queue header */
321 struct hisi_sas_cmd_hdr {
332 __le32 transfer_tags;
335 __le32 data_transfer_len;
338 __le32 first_burst_num;
347 __le64 cmd_table_addr;
350 __le64 sts_buffer_addr;
353 __le64 prd_table_addr;
356 __le64 dif_prd_table_addr;
359 struct hisi_sas_itct {
367 struct hisi_sas_iost {
374 struct hisi_sas_err_record {
378 struct hisi_sas_initial_fis {
379 struct hisi_sas_err_record err_record;
380 struct dev_to_host_fis fis;
384 struct hisi_sas_breakpoint {
388 struct hisi_sas_sata_breakpoint {
389 struct hisi_sas_breakpoint tag[32];
392 struct hisi_sas_sge {
400 struct hisi_sas_command_table_smp {
404 struct hisi_sas_command_table_stp {
405 struct host_to_dev_fis command_fis;
407 u8 atapi_cdb[ATAPI_CDB_LEN];
410 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
411 struct hisi_sas_sge_page {
412 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
415 struct hisi_sas_command_table_ssp {
416 struct ssp_frame_hdr hdr;
419 struct ssp_command_iu task;
422 struct ssp_tmf_iu ssp_task;
423 struct xfer_rdy_iu xfer_rdy;
424 struct ssp_response_iu ssp_res;
428 union hisi_sas_command_table {
429 struct hisi_sas_command_table_ssp ssp;
430 struct hisi_sas_command_table_smp smp;
431 struct hisi_sas_command_table_stp stp;
434 struct hisi_sas_status_buffer {
435 struct hisi_sas_err_record err;
439 struct hisi_sas_slot_buf_table {
440 struct hisi_sas_status_buffer status_buffer;
441 union hisi_sas_command_table command_header;
442 struct hisi_sas_sge_page sge_page;
445 extern struct scsi_transport_template *hisi_sas_stt;
446 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
447 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost);
448 extern void hisi_sas_free(struct hisi_hba *hisi_hba);
449 extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis,
451 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
452 extern void hisi_sas_sata_done(struct sas_task *task,
453 struct hisi_sas_slot *slot);
454 extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
455 extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
456 extern int hisi_sas_probe(struct platform_device *pdev,
457 const struct hisi_sas_hw *ops);
458 extern int hisi_sas_remove(struct platform_device *pdev);
460 extern int hisi_sas_slave_configure(struct scsi_device *sdev);
461 extern int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time);
462 extern void hisi_sas_scan_start(struct Scsi_Host *shost);
463 extern struct device_attribute *host_attrs[];
464 extern int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type);
465 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
466 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
467 struct sas_task *task,
468 struct hisi_sas_slot *slot);
469 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
470 extern void hisi_sas_rst_work_handler(struct work_struct *work);
471 extern void hisi_sas_sync_rst_work_handler(struct work_struct *work);
472 extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
473 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
474 enum hisi_sas_phy_event event);
475 extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba);
476 extern u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max);