John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1 | /* |
| 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 Garry | 4d558c7 | 2016-02-04 02:26:08 +0800 | [diff] [blame^] | 15 | #include <linux/acpi.h> |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 16 | #include <linux/dmapool.h> |
| 17 | #include <linux/mfd/syscon.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_address.h> |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
John Garry | 4d558c7 | 2016-02-04 02:26:08 +0800 | [diff] [blame^] | 21 | #include <linux/property.h> |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 22 | #include <linux/regmap.h> |
John Garry | 6f2ff1a | 2016-01-26 02:47:20 +0800 | [diff] [blame] | 23 | #include <scsi/sas_ata.h> |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 24 | #include <scsi/libsas.h> |
| 25 | |
John Garry | bd8d859 | 2016-01-26 02:47:23 +0800 | [diff] [blame] | 26 | #define DRV_VERSION "v1.1" |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 27 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 28 | #define HISI_SAS_MAX_PHYS 9 |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 29 | #define HISI_SAS_MAX_QUEUES 32 |
| 30 | #define HISI_SAS_QUEUE_SLOTS 512 |
John Garry | 5560e9f | 2016-01-26 02:47:04 +0800 | [diff] [blame] | 31 | #define HISI_SAS_MAX_ITCT_ENTRIES 2048 |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 32 | #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 33 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 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) |
| 38 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 39 | #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024) |
John Garry | 66ee999 | 2015-11-18 00:50:54 +0800 | [diff] [blame] | 40 | #define HISI_SAS_MAX_SMP_RESP_SZ 1028 |
John Garry | 6f2ff1a | 2016-01-26 02:47:20 +0800 | [diff] [blame] | 41 | #define HISI_SAS_MAX_STP_RESP_SZ 28 |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 42 | |
John Garry | 98bf39f | 2016-01-26 02:47:02 +0800 | [diff] [blame] | 43 | #define DEV_IS_EXPANDER(type) \ |
| 44 | ((type == SAS_EDGE_EXPANDER_DEVICE) || \ |
| 45 | (type == SAS_FANOUT_EXPANDER_DEVICE)) |
| 46 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 47 | struct hisi_hba; |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 48 | |
John Garry | 07d7859 | 2015-11-18 00:50:47 +0800 | [diff] [blame] | 49 | enum { |
| 50 | PORT_TYPE_SAS = (1U << 1), |
| 51 | PORT_TYPE_SATA = (1U << 0), |
| 52 | }; |
| 53 | |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 54 | enum dev_status { |
| 55 | HISI_SAS_DEV_NORMAL, |
| 56 | HISI_SAS_DEV_EH, |
| 57 | }; |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 58 | |
| 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, |
| 63 | }; |
| 64 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 65 | struct hisi_sas_phy { |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 66 | struct hisi_hba *hisi_hba; |
| 67 | struct hisi_sas_port *port; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 68 | struct asd_sas_phy sas_phy; |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 69 | struct sas_identify identify; |
| 70 | struct timer_list timer; |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 71 | struct work_struct phyup_ws; |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 72 | u64 port_id; /* from hw */ |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 73 | u64 dev_sas_addr; |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 74 | u64 phy_type; |
| 75 | u64 frame_rcvd_size; |
| 76 | u8 frame_rcvd[32]; |
| 77 | u8 phy_attached; |
| 78 | u8 reserved[3]; |
| 79 | enum sas_linkrate minimum_linkrate; |
| 80 | enum sas_linkrate maximum_linkrate; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct hisi_sas_port { |
| 84 | struct asd_sas_port sas_port; |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 85 | u8 port_attached; |
| 86 | u8 id; /* from hw */ |
| 87 | struct list_head list; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 88 | }; |
| 89 | |
John Garry | 9101a07 | 2015-11-18 00:50:37 +0800 | [diff] [blame] | 90 | struct hisi_sas_cq { |
| 91 | struct hisi_hba *hisi_hba; |
| 92 | int id; |
| 93 | }; |
| 94 | |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 95 | struct hisi_sas_device { |
| 96 | enum sas_device_type dev_type; |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 97 | struct hisi_hba *hisi_hba; |
| 98 | struct domain_device *sas_device; |
| 99 | u64 attached_phy; |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 100 | u64 device_id; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 101 | u64 running_req; |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 102 | u8 dev_status; |
| 103 | }; |
| 104 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 105 | struct hisi_sas_slot { |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 106 | struct list_head entry; |
| 107 | struct sas_task *task; |
| 108 | struct hisi_sas_port *port; |
| 109 | u64 n_elem; |
| 110 | int dlvry_queue; |
| 111 | int dlvry_queue_slot; |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 112 | int cmplt_queue; |
| 113 | int cmplt_queue_slot; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 114 | int idx; |
| 115 | void *cmd_hdr; |
| 116 | dma_addr_t cmd_hdr_dma; |
| 117 | void *status_buffer; |
| 118 | dma_addr_t status_buffer_dma; |
| 119 | void *command_table; |
| 120 | dma_addr_t command_table_dma; |
| 121 | struct hisi_sas_sge_page *sge_page; |
| 122 | dma_addr_t sge_page_dma; |
| 123 | }; |
| 124 | |
| 125 | struct hisi_sas_tmf_task { |
| 126 | u8 tmf; |
| 127 | u16 tag_of_task_to_be_managed; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 128 | }; |
| 129 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 130 | struct hisi_sas_hw { |
John Garry | 8ff1d57 | 2015-11-18 00:50:46 +0800 | [diff] [blame] | 131 | int (*hw_init)(struct hisi_hba *hisi_hba); |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 132 | void (*setup_itct)(struct hisi_hba *hisi_hba, |
| 133 | struct hisi_sas_device *device); |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 134 | void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 135 | int (*get_free_slot)(struct hisi_hba *hisi_hba, int *q, int *s); |
| 136 | void (*start_delivery)(struct hisi_hba *hisi_hba); |
| 137 | int (*prep_ssp)(struct hisi_hba *hisi_hba, |
| 138 | struct hisi_sas_slot *slot, int is_tmf, |
| 139 | struct hisi_sas_tmf_task *tmf); |
John Garry | 66ee999 | 2015-11-18 00:50:54 +0800 | [diff] [blame] | 140 | int (*prep_smp)(struct hisi_hba *hisi_hba, |
| 141 | struct hisi_sas_slot *slot); |
John Garry | 6f2ff1a | 2016-01-26 02:47:20 +0800 | [diff] [blame] | 142 | int (*prep_stp)(struct hisi_hba *hisi_hba, |
| 143 | struct hisi_sas_slot *slot); |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 144 | int (*slot_complete)(struct hisi_hba *hisi_hba, |
| 145 | struct hisi_sas_slot *slot, int abort); |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 146 | void (*phy_enable)(struct hisi_hba *hisi_hba, int phy_no); |
| 147 | void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no); |
| 148 | void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no); |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 149 | void (*free_device)(struct hisi_hba *hisi_hba, |
| 150 | struct hisi_sas_device *dev); |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 151 | int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id); |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 152 | int max_command_entries; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 153 | int complete_hdr_size; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | struct hisi_hba { |
| 157 | /* This must be the first element, used by SHOST_TO_SAS_HA */ |
| 158 | struct sas_ha_struct *p; |
| 159 | |
| 160 | struct platform_device *pdev; |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 161 | void __iomem *regs; |
| 162 | struct regmap *ctrl; |
| 163 | u32 ctrl_reset_reg; |
| 164 | u32 ctrl_reset_sts_reg; |
| 165 | u32 ctrl_clock_ena_reg; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 166 | u8 sas_addr[SAS_ADDR_SIZE]; |
| 167 | |
| 168 | int n_phy; |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 169 | int scan_finished; |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 170 | spinlock_t lock; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 171 | |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 172 | struct timer_list timer; |
John Garry | 7e9080e | 2015-11-18 00:50:40 +0800 | [diff] [blame] | 173 | struct workqueue_struct *wq; |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 174 | |
| 175 | int slot_index_count; |
| 176 | unsigned long *slot_index_tags; |
| 177 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 178 | /* SCSI/SAS glue */ |
| 179 | struct sas_ha_struct sha; |
| 180 | struct Scsi_Host *shost; |
John Garry | 9101a07 | 2015-11-18 00:50:37 +0800 | [diff] [blame] | 181 | |
| 182 | struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES]; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 183 | struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS]; |
| 184 | struct hisi_sas_port port[HISI_SAS_MAX_PHYS]; |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 185 | |
| 186 | int queue_count; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 187 | int queue; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 188 | struct hisi_sas_slot *slot_prep; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 189 | |
| 190 | struct dma_pool *sge_page_pool; |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 191 | struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES]; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 192 | struct dma_pool *command_table_pool; |
| 193 | struct dma_pool *status_buffer_pool; |
| 194 | struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES]; |
| 195 | dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES]; |
| 196 | void *complete_hdr[HISI_SAS_MAX_QUEUES]; |
| 197 | dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES]; |
| 198 | struct hisi_sas_initial_fis *initial_fis; |
| 199 | dma_addr_t initial_fis_dma; |
| 200 | struct hisi_sas_itct *itct; |
| 201 | dma_addr_t itct_dma; |
| 202 | struct hisi_sas_iost *iost; |
| 203 | dma_addr_t iost_dma; |
| 204 | struct hisi_sas_breakpoint *breakpoint; |
| 205 | dma_addr_t breakpoint_dma; |
| 206 | struct hisi_sas_breakpoint *sata_breakpoint; |
| 207 | dma_addr_t sata_breakpoint_dma; |
| 208 | struct hisi_sas_slot *slot_info; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 209 | const struct hisi_sas_hw *hw; /* Low level hw interface */ |
| 210 | }; |
| 211 | |
John Garry | c799d6b | 2015-11-18 00:50:33 +0800 | [diff] [blame] | 212 | /* Generic HW DMA host memory structures */ |
| 213 | /* Delivery queue header */ |
| 214 | struct hisi_sas_cmd_hdr { |
| 215 | /* dw0 */ |
| 216 | __le32 dw0; |
| 217 | |
| 218 | /* dw1 */ |
| 219 | __le32 dw1; |
| 220 | |
| 221 | /* dw2 */ |
| 222 | __le32 dw2; |
| 223 | |
| 224 | /* dw3 */ |
| 225 | __le32 transfer_tags; |
| 226 | |
| 227 | /* dw4 */ |
| 228 | __le32 data_transfer_len; |
| 229 | |
| 230 | /* dw5 */ |
| 231 | __le32 first_burst_num; |
| 232 | |
| 233 | /* dw6 */ |
| 234 | __le32 sg_len; |
| 235 | |
| 236 | /* dw7 */ |
| 237 | __le32 dw7; |
| 238 | |
| 239 | /* dw8-9 */ |
| 240 | __le64 cmd_table_addr; |
| 241 | |
| 242 | /* dw10-11 */ |
| 243 | __le64 sts_buffer_addr; |
| 244 | |
| 245 | /* dw12-13 */ |
| 246 | __le64 prd_table_addr; |
| 247 | |
| 248 | /* dw14-15 */ |
| 249 | __le64 dif_prd_table_addr; |
| 250 | }; |
| 251 | |
| 252 | struct hisi_sas_itct { |
| 253 | __le64 qw0; |
| 254 | __le64 sas_addr; |
| 255 | __le64 qw2; |
| 256 | __le64 qw3; |
John Garry | 281e3bf | 2016-01-26 02:47:06 +0800 | [diff] [blame] | 257 | __le64 qw4_15[12]; |
John Garry | c799d6b | 2015-11-18 00:50:33 +0800 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | struct hisi_sas_iost { |
| 261 | __le64 qw0; |
| 262 | __le64 qw1; |
| 263 | __le64 qw2; |
| 264 | __le64 qw3; |
| 265 | }; |
| 266 | |
| 267 | struct hisi_sas_err_record { |
John Garry | 8d1eee7 | 2016-01-26 02:47:05 +0800 | [diff] [blame] | 268 | u32 data[4]; |
John Garry | c799d6b | 2015-11-18 00:50:33 +0800 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | struct hisi_sas_initial_fis { |
| 272 | struct hisi_sas_err_record err_record; |
| 273 | struct dev_to_host_fis fis; |
| 274 | u32 rsvd[3]; |
| 275 | }; |
| 276 | |
| 277 | struct hisi_sas_breakpoint { |
| 278 | u8 data[128]; /*io128 byte*/ |
| 279 | }; |
| 280 | |
| 281 | struct hisi_sas_sge { |
| 282 | __le64 addr; |
| 283 | __le32 page_ctrl_0; |
| 284 | __le32 page_ctrl_1; |
| 285 | __le32 data_len; |
| 286 | __le32 data_off; |
| 287 | }; |
| 288 | |
| 289 | struct hisi_sas_command_table_smp { |
| 290 | u8 bytes[44]; |
| 291 | }; |
| 292 | |
| 293 | struct hisi_sas_command_table_stp { |
| 294 | struct host_to_dev_fis command_fis; |
| 295 | u8 dummy[12]; |
| 296 | u8 atapi_cdb[ATAPI_CDB_LEN]; |
| 297 | }; |
| 298 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 299 | #define HISI_SAS_SGE_PAGE_CNT SCSI_MAX_SG_SEGMENTS |
John Garry | c799d6b | 2015-11-18 00:50:33 +0800 | [diff] [blame] | 300 | struct hisi_sas_sge_page { |
| 301 | struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT]; |
| 302 | }; |
| 303 | |
| 304 | struct hisi_sas_command_table_ssp { |
| 305 | struct ssp_frame_hdr hdr; |
| 306 | union { |
| 307 | struct { |
| 308 | struct ssp_command_iu task; |
| 309 | u32 prot[6]; |
| 310 | }; |
| 311 | struct ssp_tmf_iu ssp_task; |
| 312 | struct xfer_rdy_iu xfer_rdy; |
| 313 | struct ssp_response_iu ssp_res; |
| 314 | } u; |
| 315 | }; |
| 316 | |
| 317 | union hisi_sas_command_table { |
| 318 | struct hisi_sas_command_table_ssp ssp; |
| 319 | struct hisi_sas_command_table_smp smp; |
| 320 | struct hisi_sas_command_table_stp stp; |
| 321 | }; |
John Garry | 9fb10b5 | 2015-11-18 00:50:44 +0800 | [diff] [blame] | 322 | extern int hisi_sas_probe(struct platform_device *pdev, |
| 323 | const struct hisi_sas_hw *ops); |
| 324 | extern int hisi_sas_remove(struct platform_device *pdev); |
John Garry | c799d6b | 2015-11-18 00:50:33 +0800 | [diff] [blame] | 325 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 326 | extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy); |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 327 | extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, |
| 328 | struct sas_task *task, |
| 329 | struct hisi_sas_slot *slot); |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 330 | #endif |