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/dmapool.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/regmap.h>
23 #include <scsi/sas_ata.h>
24 #include <scsi/libsas.h>
26 #define DRV_VERSION "v1.5"
28 #define HISI_SAS_MAX_PHYS 9
29 #define HISI_SAS_MAX_QUEUES 32
30 #define HISI_SAS_QUEUE_SLOTS 512
31 #define HISI_SAS_MAX_ITCT_ENTRIES 2048
32 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
34 #define HISI_SAS_STATUS_BUF_SZ \
35 (sizeof(struct hisi_sas_err_record) + 1024)
36 #define HISI_SAS_COMMAND_TABLE_SZ \
37 (((sizeof(union hisi_sas_command_table)+3)/4)*4)
39 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
40 #define HISI_SAS_MAX_SMP_RESP_SZ 1028
41 #define HISI_SAS_MAX_STP_RESP_SZ 28
43 #define DEV_IS_EXPANDER(type) \
44 ((type == SAS_EDGE_EXPANDER_DEVICE) || \
45 (type == SAS_FANOUT_EXPANDER_DEVICE))
50 PORT_TYPE_SAS = (1U << 1),
51 PORT_TYPE_SATA = (1U << 0),
59 enum hisi_sas_dev_type {
60 HISI_SAS_DEV_TYPE_STP = 0,
61 HISI_SAS_DEV_TYPE_SSP,
62 HISI_SAS_DEV_TYPE_SATA,
66 struct hisi_hba *hisi_hba;
67 struct hisi_sas_port *port;
68 struct asd_sas_phy sas_phy;
69 struct sas_identify identify;
70 struct timer_list timer;
71 struct work_struct phyup_ws;
72 u64 port_id; /* from hw */
79 enum sas_linkrate minimum_linkrate;
80 enum sas_linkrate maximum_linkrate;
83 struct hisi_sas_port {
84 struct asd_sas_port sas_port;
87 struct list_head list;
91 struct hisi_hba *hisi_hba;
95 struct hisi_sas_device {
96 enum sas_device_type dev_type;
97 struct hisi_hba *hisi_hba;
98 struct domain_device *sas_device;
105 struct hisi_sas_slot {
106 struct list_head entry;
107 struct sas_task *task;
108 struct hisi_sas_port *port;
111 int dlvry_queue_slot;
113 int cmplt_queue_slot;
117 dma_addr_t cmd_hdr_dma;
119 dma_addr_t status_buffer_dma;
121 dma_addr_t command_table_dma;
122 struct hisi_sas_sge_page *sge_page;
123 dma_addr_t sge_page_dma;
124 struct work_struct abort_slot;
127 struct hisi_sas_tmf_task {
129 u16 tag_of_task_to_be_managed;
133 int (*hw_init)(struct hisi_hba *hisi_hba);
134 void (*setup_itct)(struct hisi_hba *hisi_hba,
135 struct hisi_sas_device *device);
136 int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
137 struct domain_device *device);
138 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
139 void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
140 int (*get_free_slot)(struct hisi_hba *hisi_hba, int *q, int *s);
141 void (*start_delivery)(struct hisi_hba *hisi_hba);
142 int (*prep_ssp)(struct hisi_hba *hisi_hba,
143 struct hisi_sas_slot *slot, int is_tmf,
144 struct hisi_sas_tmf_task *tmf);
145 int (*prep_smp)(struct hisi_hba *hisi_hba,
146 struct hisi_sas_slot *slot);
147 int (*prep_stp)(struct hisi_hba *hisi_hba,
148 struct hisi_sas_slot *slot);
149 int (*slot_complete)(struct hisi_hba *hisi_hba,
150 struct hisi_sas_slot *slot, int abort);
151 void (*phy_enable)(struct hisi_hba *hisi_hba, int phy_no);
152 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
153 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
154 void (*free_device)(struct hisi_hba *hisi_hba,
155 struct hisi_sas_device *dev);
156 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
157 int max_command_entries;
158 int complete_hdr_size;
162 /* This must be the first element, used by SHOST_TO_SAS_HA */
163 struct sas_ha_struct *p;
165 struct platform_device *pdev;
169 u32 ctrl_reset_sts_reg;
170 u32 ctrl_clock_ena_reg;
171 u8 sas_addr[SAS_ADDR_SIZE];
177 struct timer_list timer;
178 struct workqueue_struct *wq;
180 int slot_index_count;
181 unsigned long *slot_index_tags;
184 struct sas_ha_struct sha;
185 struct Scsi_Host *shost;
187 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
188 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
189 struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
193 struct hisi_sas_slot *slot_prep;
195 struct dma_pool *sge_page_pool;
196 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
197 struct dma_pool *command_table_pool;
198 struct dma_pool *status_buffer_pool;
199 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
200 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
201 void *complete_hdr[HISI_SAS_MAX_QUEUES];
202 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
203 struct hisi_sas_initial_fis *initial_fis;
204 dma_addr_t initial_fis_dma;
205 struct hisi_sas_itct *itct;
207 struct hisi_sas_iost *iost;
209 struct hisi_sas_breakpoint *breakpoint;
210 dma_addr_t breakpoint_dma;
211 struct hisi_sas_breakpoint *sata_breakpoint;
212 dma_addr_t sata_breakpoint_dma;
213 struct hisi_sas_slot *slot_info;
214 const struct hisi_sas_hw *hw; /* Low level hw interface */
217 /* Generic HW DMA host memory structures */
218 /* Delivery queue header */
219 struct hisi_sas_cmd_hdr {
230 __le32 transfer_tags;
233 __le32 data_transfer_len;
236 __le32 first_burst_num;
245 __le64 cmd_table_addr;
248 __le64 sts_buffer_addr;
251 __le64 prd_table_addr;
254 __le64 dif_prd_table_addr;
257 struct hisi_sas_itct {
265 struct hisi_sas_iost {
272 struct hisi_sas_err_record {
276 struct hisi_sas_initial_fis {
277 struct hisi_sas_err_record err_record;
278 struct dev_to_host_fis fis;
282 struct hisi_sas_breakpoint {
283 u8 data[128]; /*io128 byte*/
286 struct hisi_sas_sge {
294 struct hisi_sas_command_table_smp {
298 struct hisi_sas_command_table_stp {
299 struct host_to_dev_fis command_fis;
301 u8 atapi_cdb[ATAPI_CDB_LEN];
304 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
305 struct hisi_sas_sge_page {
306 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
309 struct hisi_sas_command_table_ssp {
310 struct ssp_frame_hdr hdr;
313 struct ssp_command_iu task;
316 struct ssp_tmf_iu ssp_task;
317 struct xfer_rdy_iu xfer_rdy;
318 struct ssp_response_iu ssp_res;
322 union hisi_sas_command_table {
323 struct hisi_sas_command_table_ssp ssp;
324 struct hisi_sas_command_table_smp smp;
325 struct hisi_sas_command_table_stp stp;
327 extern int hisi_sas_probe(struct platform_device *pdev,
328 const struct hisi_sas_hw *ops);
329 extern int hisi_sas_remove(struct platform_device *pdev);
331 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
332 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
333 struct sas_task *task,
334 struct hisi_sas_slot *slot);