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/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> 28 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 36 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)) 39 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) 45 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) 50 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) 55 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 59 60 #define DEV_IS_EXPANDER(type) \ 61 ((type == SAS_EDGE_EXPANDER_DEVICE) || \ 62 (type == SAS_FANOUT_EXPANDER_DEVICE)) 63 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 69 70 struct hisi_hba; 71 72 enum { 73 PORT_TYPE_SAS = (1U << 1), 74 PORT_TYPE_SATA = (1U << 0), 75 }; 76 77 enum dev_status { 78 HISI_SAS_DEV_NORMAL, 79 HISI_SAS_DEV_EH, 80 }; 81 82 enum { 83 HISI_SAS_INT_ABT_CMD = 0, 84 HISI_SAS_INT_ABT_DEV = 1, 85 }; 86 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, 91 }; 92 93 struct hisi_sas_hw_error { 94 u32 irq_msk; 95 u32 msk; 96 int shift; 97 const char *msg; 98 int reg; 99 const struct hisi_sas_hw_error *sub; 100 }; 101 102 struct hisi_sas_rst { 103 struct hisi_hba *hisi_hba; 104 struct completion *completion; 105 struct work_struct work; 106 bool done; 107 }; 108 109 #define HISI_SAS_RST_WORK_INIT(r, c) \ 110 { .hisi_hba = hisi_hba, \ 111 .completion = &c, \ 112 .work = __WORK_INITIALIZER(r.work, \ 113 hisi_sas_sync_rst_work_handler), \ 114 .done = false, \ 115 } 116 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) 121 122 enum hisi_sas_bit_err_type { 123 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0, 124 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1, 125 }; 126 127 enum hisi_sas_phy_event { 128 HISI_PHYE_PHY_UP = 0U, 129 HISI_PHYE_LINK_RESET, 130 HISI_PHYES_NUM, 131 }; 132 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 */ 140 u64 dev_sas_addr; 141 u64 frame_rcvd_size; 142 u8 frame_rcvd[32]; 143 u8 phy_attached; 144 u8 reserved[3]; 145 u32 phy_type; 146 enum sas_linkrate minimum_linkrate; 147 enum sas_linkrate maximum_linkrate; 148 }; 149 150 struct hisi_sas_port { 151 struct asd_sas_port sas_port; 152 u8 port_attached; 153 u8 id; /* from hw */ 154 }; 155 156 struct hisi_sas_cq { 157 struct hisi_hba *hisi_hba; 158 struct tasklet_struct tasklet; 159 int rd_point; 160 int id; 161 }; 162 163 struct hisi_sas_dq { 164 struct hisi_hba *hisi_hba; 165 struct hisi_sas_slot *slot_prep; 166 spinlock_t lock; 167 int wr_point; 168 int id; 169 }; 170 171 struct hisi_sas_device { 172 struct hisi_hba *hisi_hba; 173 struct domain_device *sas_device; 174 struct completion *completion; 175 struct hisi_sas_dq *dq; 176 struct list_head list; 177 u64 attached_phy; 178 enum sas_device_type dev_type; 179 int device_id; 180 int sata_idx; 181 u8 dev_status; 182 }; 183 184 struct hisi_sas_slot { 185 struct list_head entry; 186 struct sas_task *task; 187 struct hisi_sas_port *port; 188 u64 n_elem; 189 int dlvry_queue; 190 int dlvry_queue_slot; 191 int cmplt_queue; 192 int cmplt_queue_slot; 193 int idx; 194 int abort; 195 void *buf; 196 dma_addr_t buf_dma; 197 void *cmd_hdr; 198 dma_addr_t cmd_hdr_dma; 199 struct work_struct abort_slot; 200 struct timer_list internal_abort_timer; 201 }; 202 203 struct hisi_sas_tmf_task { 204 u8 tmf; 205 u16 tag_of_task_to_be_managed; 206 }; 207 208 struct hisi_sas_hw { 209 int (*hw_init)(struct hisi_hba *hisi_hba); 210 void (*setup_itct)(struct hisi_hba *hisi_hba, 211 struct hisi_sas_device *device); 212 int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx, 213 struct domain_device *device); 214 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device); 215 void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no); 216 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq); 217 void (*start_delivery)(struct hisi_sas_dq *dq); 218 int (*prep_ssp)(struct hisi_hba *hisi_hba, 219 struct hisi_sas_slot *slot, int is_tmf, 220 struct hisi_sas_tmf_task *tmf); 221 int (*prep_smp)(struct hisi_hba *hisi_hba, 222 struct hisi_sas_slot *slot); 223 int (*prep_stp)(struct hisi_hba *hisi_hba, 224 struct hisi_sas_slot *slot); 225 int (*prep_abort)(struct hisi_hba *hisi_hba, 226 struct hisi_sas_slot *slot, 227 int device_id, int abort_flag, int tag_to_abort); 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); 232 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no); 233 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 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no, 236 struct sas_phy_linkrates *linkrates); 237 enum sas_linkrate (*phy_get_max_linkrate)(void); 238 void (*clear_itct)(struct hisi_hba *hisi_hba, 239 struct hisi_sas_device *dev); 240 void (*free_device)(struct hisi_sas_device *sas_dev); 241 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id); 242 void (*dereg_device)(struct hisi_hba *hisi_hba, 243 struct domain_device *device); 244 int (*soft_reset)(struct hisi_hba *hisi_hba); 245 u32 (*get_phys_state)(struct hisi_hba *hisi_hba); 246 int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type, 247 u8 reg_index, u8 reg_count, u8 *write_data); 248 int max_command_entries; 249 int complete_hdr_size; 250 }; 251 252 struct hisi_hba { 253 /* This must be the first element, used by SHOST_TO_SAS_HA */ 254 struct sas_ha_struct *p; 255 256 struct platform_device *platform_dev; 257 struct pci_dev *pci_dev; 258 struct device *dev; 259 260 void __iomem *regs; 261 void __iomem *sgpio_regs; 262 struct regmap *ctrl; 263 u32 ctrl_reset_reg; 264 u32 ctrl_reset_sts_reg; 265 u32 ctrl_clock_ena_reg; 266 u32 refclk_frequency_mhz; 267 u8 sas_addr[SAS_ADDR_SIZE]; 268 269 int n_phy; 270 spinlock_t lock; 271 272 struct timer_list timer; 273 struct workqueue_struct *wq; 274 275 int slot_index_count; 276 unsigned long *slot_index_tags; 277 unsigned long reject_stp_links_msk; 278 279 /* SCSI/SAS glue */ 280 struct sas_ha_struct sha; 281 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]; 285 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS]; 286 struct hisi_sas_port port[HISI_SAS_MAX_PHYS]; 287 288 int queue_count; 289 290 struct dma_pool *buffer_pool; 291 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES]; 292 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES]; 293 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES]; 294 void *complete_hdr[HISI_SAS_MAX_QUEUES]; 295 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES]; 296 struct hisi_sas_initial_fis *initial_fis; 297 dma_addr_t initial_fis_dma; 298 struct hisi_sas_itct *itct; 299 dma_addr_t itct_dma; 300 struct hisi_sas_iost *iost; 301 dma_addr_t iost_dma; 302 struct hisi_sas_breakpoint *breakpoint; 303 dma_addr_t breakpoint_dma; 304 struct hisi_sas_breakpoint *sata_breakpoint; 305 dma_addr_t sata_breakpoint_dma; 306 struct hisi_sas_slot *slot_info; 307 unsigned long flags; 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; 311 }; 312 313 /* Generic HW DMA host memory structures */ 314 /* Delivery queue header */ 315 struct hisi_sas_cmd_hdr { 316 /* dw0 */ 317 __le32 dw0; 318 319 /* dw1 */ 320 __le32 dw1; 321 322 /* dw2 */ 323 __le32 dw2; 324 325 /* dw3 */ 326 __le32 transfer_tags; 327 328 /* dw4 */ 329 __le32 data_transfer_len; 330 331 /* dw5 */ 332 __le32 first_burst_num; 333 334 /* dw6 */ 335 __le32 sg_len; 336 337 /* dw7 */ 338 __le32 dw7; 339 340 /* dw8-9 */ 341 __le64 cmd_table_addr; 342 343 /* dw10-11 */ 344 __le64 sts_buffer_addr; 345 346 /* dw12-13 */ 347 __le64 prd_table_addr; 348 349 /* dw14-15 */ 350 __le64 dif_prd_table_addr; 351 }; 352 353 struct hisi_sas_itct { 354 __le64 qw0; 355 __le64 sas_addr; 356 __le64 qw2; 357 __le64 qw3; 358 __le64 qw4_15[12]; 359 }; 360 361 struct hisi_sas_iost { 362 __le64 qw0; 363 __le64 qw1; 364 __le64 qw2; 365 __le64 qw3; 366 }; 367 368 struct hisi_sas_err_record { 369 u32 data[4]; 370 }; 371 372 struct hisi_sas_initial_fis { 373 struct hisi_sas_err_record err_record; 374 struct dev_to_host_fis fis; 375 u32 rsvd[3]; 376 }; 377 378 struct hisi_sas_breakpoint { 379 u8 data[128]; 380 }; 381 382 struct hisi_sas_sata_breakpoint { 383 struct hisi_sas_breakpoint tag[32]; 384 }; 385 386 struct hisi_sas_sge { 387 __le64 addr; 388 __le32 page_ctrl_0; 389 __le32 page_ctrl_1; 390 __le32 data_len; 391 __le32 data_off; 392 }; 393 394 struct hisi_sas_command_table_smp { 395 u8 bytes[44]; 396 }; 397 398 struct hisi_sas_command_table_stp { 399 struct host_to_dev_fis command_fis; 400 u8 dummy[12]; 401 u8 atapi_cdb[ATAPI_CDB_LEN]; 402 }; 403 404 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE 405 struct hisi_sas_sge_page { 406 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT]; 407 } __aligned(16); 408 409 struct hisi_sas_command_table_ssp { 410 struct ssp_frame_hdr hdr; 411 union { 412 struct { 413 struct ssp_command_iu task; 414 u32 prot[6]; 415 }; 416 struct ssp_tmf_iu ssp_task; 417 struct xfer_rdy_iu xfer_rdy; 418 struct ssp_response_iu ssp_res; 419 } u; 420 }; 421 422 union hisi_sas_command_table { 423 struct hisi_sas_command_table_ssp ssp; 424 struct hisi_sas_command_table_smp smp; 425 struct hisi_sas_command_table_stp stp; 426 } __aligned(16); 427 428 struct hisi_sas_status_buffer { 429 struct hisi_sas_err_record err; 430 u8 iu[1024]; 431 } __aligned(16); 432 433 struct hisi_sas_slot_buf_table { 434 struct hisi_sas_status_buffer status_buffer; 435 union hisi_sas_command_table command_header; 436 struct hisi_sas_sge_page sge_page; 437 }; 438 439 extern struct scsi_transport_template *hisi_sas_stt; 440 extern struct scsi_host_template *hisi_sas_sht; 441 442 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba); 443 extern void hisi_sas_init_add(struct hisi_hba *hisi_hba); 444 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost); 445 extern void hisi_sas_free(struct hisi_hba *hisi_hba); 446 extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, 447 int direction); 448 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port); 449 extern void hisi_sas_sata_done(struct sas_task *task, 450 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); 453 extern int hisi_sas_probe(struct platform_device *pdev, 454 const struct hisi_sas_hw *ops); 455 extern int hisi_sas_remove(struct platform_device *pdev); 456 457 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy); 458 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, 459 struct sas_task *task, 460 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 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy, 466 enum hisi_sas_phy_event event); 467 extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba); 468 #endif 469