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