1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Copyright (c) 2015 Linaro Ltd. 4 * Copyright (c) 2015 Hisilicon Limited. 5 */ 6 7 #ifndef _HISI_SAS_H_ 8 #define _HISI_SAS_H_ 9 10 #include <linux/acpi.h> 11 #include <linux/clk.h> 12 #include <linux/debugfs.h> 13 #include <linux/dmapool.h> 14 #include <linux/iopoll.h> 15 #include <linux/lcm.h> 16 #include <linux/libata.h> 17 #include <linux/mfd/syscon.h> 18 #include <linux/module.h> 19 #include <linux/of_address.h> 20 #include <linux/pci.h> 21 #include <linux/platform_device.h> 22 #include <linux/property.h> 23 #include <linux/regmap.h> 24 #include <scsi/sas_ata.h> 25 #include <scsi/libsas.h> 26 27 #define HISI_SAS_MAX_PHYS 9 28 #define HISI_SAS_MAX_QUEUES 32 29 #define HISI_SAS_QUEUE_SLOTS 4096 30 #define HISI_SAS_MAX_ITCT_ENTRIES 1024 31 #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES 32 #define HISI_SAS_RESET_BIT 0 33 #define HISI_SAS_REJECT_CMD_BIT 1 34 #define HISI_SAS_RESERVED_IPTT_CNT 96 35 36 #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer)) 37 #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table)) 38 39 #define hisi_sas_status_buf_addr(buf) \ 40 ((buf) + offsetof(struct hisi_sas_slot_buf_table, status_buffer)) 41 #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr((slot)->buf) 42 #define hisi_sas_status_buf_addr_dma(slot) \ 43 hisi_sas_status_buf_addr((slot)->buf_dma) 44 45 #define hisi_sas_cmd_hdr_addr(buf) \ 46 ((buf) + offsetof(struct hisi_sas_slot_buf_table, command_header)) 47 #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr((slot)->buf) 48 #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr((slot)->buf_dma) 49 50 #define hisi_sas_sge_addr(buf) \ 51 ((buf) + offsetof(struct hisi_sas_slot_buf_table, sge_page)) 52 #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr((slot)->buf) 53 #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr((slot)->buf_dma) 54 55 #define hisi_sas_sge_dif_addr(buf) \ 56 ((buf) + offsetof(struct hisi_sas_slot_dif_buf_table, sge_dif_page)) 57 #define hisi_sas_sge_dif_addr_mem(slot) hisi_sas_sge_dif_addr((slot)->buf) 58 #define hisi_sas_sge_dif_addr_dma(slot) hisi_sas_sge_dif_addr((slot)->buf_dma) 59 60 #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024) 61 #define HISI_SAS_MAX_SMP_RESP_SZ 1028 62 #define HISI_SAS_MAX_STP_RESP_SZ 28 63 64 #define DEV_IS_EXPANDER(type) \ 65 ((type == SAS_EDGE_EXPANDER_DEVICE) || \ 66 (type == SAS_FANOUT_EXPANDER_DEVICE)) 67 68 #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1 69 #define HISI_SAS_SATA_PROTOCOL_PIO 0x2 70 #define HISI_SAS_SATA_PROTOCOL_DMA 0x4 71 #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8 72 #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10 73 74 #define HISI_SAS_DIF_PROT_MASK (SHOST_DIF_TYPE1_PROTECTION | \ 75 SHOST_DIF_TYPE2_PROTECTION | \ 76 SHOST_DIF_TYPE3_PROTECTION) 77 78 #define HISI_SAS_DIX_PROT_MASK (SHOST_DIX_TYPE1_PROTECTION | \ 79 SHOST_DIX_TYPE2_PROTECTION | \ 80 SHOST_DIX_TYPE3_PROTECTION) 81 82 #define HISI_SAS_PROT_MASK (HISI_SAS_DIF_PROT_MASK | HISI_SAS_DIX_PROT_MASK) 83 84 #define HISI_SAS_WAIT_PHYUP_TIMEOUT 20 85 86 struct hisi_hba; 87 88 enum { 89 PORT_TYPE_SAS = (1U << 1), 90 PORT_TYPE_SATA = (1U << 0), 91 }; 92 93 enum dev_status { 94 HISI_SAS_DEV_INIT, 95 HISI_SAS_DEV_NORMAL, 96 }; 97 98 enum { 99 HISI_SAS_INT_ABT_CMD = 0, 100 HISI_SAS_INT_ABT_DEV = 1, 101 }; 102 103 enum hisi_sas_dev_type { 104 HISI_SAS_DEV_TYPE_STP = 0, 105 HISI_SAS_DEV_TYPE_SSP, 106 HISI_SAS_DEV_TYPE_SATA, 107 }; 108 109 struct hisi_sas_hw_error { 110 u32 irq_msk; 111 u32 msk; 112 int shift; 113 const char *msg; 114 int reg; 115 const struct hisi_sas_hw_error *sub; 116 }; 117 118 struct hisi_sas_rst { 119 struct hisi_hba *hisi_hba; 120 struct completion *completion; 121 struct work_struct work; 122 bool done; 123 }; 124 125 #define HISI_SAS_RST_WORK_INIT(r, c) \ 126 { .hisi_hba = hisi_hba, \ 127 .completion = &c, \ 128 .work = __WORK_INITIALIZER(r.work, \ 129 hisi_sas_sync_rst_work_handler), \ 130 .done = false, \ 131 } 132 133 #define HISI_SAS_DECLARE_RST_WORK_ON_STACK(r) \ 134 DECLARE_COMPLETION_ONSTACK(c); \ 135 DECLARE_WORK(w, hisi_sas_sync_rst_work_handler); \ 136 struct hisi_sas_rst r = HISI_SAS_RST_WORK_INIT(r, c) 137 138 enum hisi_sas_bit_err_type { 139 HISI_SAS_ERR_SINGLE_BIT_ECC = 0x0, 140 HISI_SAS_ERR_MULTI_BIT_ECC = 0x1, 141 }; 142 143 enum hisi_sas_phy_event { 144 HISI_PHYE_PHY_UP = 0U, 145 HISI_PHYE_LINK_RESET, 146 HISI_PHYES_NUM, 147 }; 148 149 struct hisi_sas_phy { 150 struct work_struct works[HISI_PHYES_NUM]; 151 struct hisi_hba *hisi_hba; 152 struct hisi_sas_port *port; 153 struct asd_sas_phy sas_phy; 154 struct sas_identify identify; 155 struct completion *reset_completion; 156 struct timer_list timer; 157 spinlock_t lock; 158 u64 port_id; /* from hw */ 159 u64 frame_rcvd_size; 160 u8 frame_rcvd[32]; 161 u8 phy_attached; 162 u8 in_reset; 163 u8 reserved[2]; 164 u32 phy_type; 165 u32 code_violation_err_count; 166 enum sas_linkrate minimum_linkrate; 167 enum sas_linkrate maximum_linkrate; 168 int enable; 169 }; 170 171 struct hisi_sas_port { 172 struct asd_sas_port sas_port; 173 u8 port_attached; 174 u8 id; /* from hw */ 175 }; 176 177 struct hisi_sas_cq { 178 struct hisi_hba *hisi_hba; 179 const struct cpumask *pci_irq_mask; 180 struct tasklet_struct tasklet; 181 int rd_point; 182 int id; 183 }; 184 185 struct hisi_sas_dq { 186 struct hisi_hba *hisi_hba; 187 struct list_head list; 188 spinlock_t lock; 189 int wr_point; 190 int id; 191 }; 192 193 struct hisi_sas_device { 194 struct hisi_hba *hisi_hba; 195 struct domain_device *sas_device; 196 struct completion *completion; 197 struct hisi_sas_dq *dq; 198 struct list_head list; 199 enum sas_device_type dev_type; 200 enum dev_status dev_status; 201 int device_id; 202 int sata_idx; 203 spinlock_t lock; /* For protecting slots */ 204 }; 205 206 struct hisi_sas_tmf_task { 207 int force_phy; 208 int phy_id; 209 u8 tmf; 210 u16 tag_of_task_to_be_managed; 211 }; 212 213 struct hisi_sas_slot { 214 struct list_head entry; 215 struct list_head delivery; 216 struct sas_task *task; 217 struct hisi_sas_port *port; 218 u64 n_elem; 219 u64 n_elem_dif; 220 int dlvry_queue; 221 int dlvry_queue_slot; 222 int cmplt_queue; 223 int cmplt_queue_slot; 224 int abort; 225 int ready; 226 int device_id; 227 void *cmd_hdr; 228 dma_addr_t cmd_hdr_dma; 229 struct timer_list internal_abort_timer; 230 bool is_internal; 231 struct hisi_sas_tmf_task *tmf; 232 /* Do not reorder/change members after here */ 233 void *buf; 234 dma_addr_t buf_dma; 235 u16 idx; 236 }; 237 238 #define HISI_SAS_DEBUGFS_REG(x) {#x, x} 239 240 struct hisi_sas_debugfs_reg_lu { 241 char *name; 242 int off; 243 }; 244 245 struct hisi_sas_debugfs_reg { 246 const struct hisi_sas_debugfs_reg_lu *lu; 247 int count; 248 int base_off; 249 union { 250 u32 (*read_global_reg)(struct hisi_hba *hisi_hba, u32 off); 251 u32 (*read_port_reg)(struct hisi_hba *hisi_hba, int port, 252 u32 off); 253 }; 254 }; 255 256 struct hisi_sas_hw { 257 int (*hw_init)(struct hisi_hba *hisi_hba); 258 void (*setup_itct)(struct hisi_hba *hisi_hba, 259 struct hisi_sas_device *device); 260 int (*slot_index_alloc)(struct hisi_hba *hisi_hba, 261 struct domain_device *device); 262 struct hisi_sas_device *(*alloc_dev)(struct domain_device *device); 263 void (*sl_notify_ssp)(struct hisi_hba *hisi_hba, int phy_no); 264 int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq); 265 void (*start_delivery)(struct hisi_sas_dq *dq); 266 void (*prep_ssp)(struct hisi_hba *hisi_hba, 267 struct hisi_sas_slot *slot); 268 void (*prep_smp)(struct hisi_hba *hisi_hba, 269 struct hisi_sas_slot *slot); 270 void (*prep_stp)(struct hisi_hba *hisi_hba, 271 struct hisi_sas_slot *slot); 272 void (*prep_abort)(struct hisi_hba *hisi_hba, 273 struct hisi_sas_slot *slot, 274 int device_id, int abort_flag, int tag_to_abort); 275 int (*slot_complete)(struct hisi_hba *hisi_hba, 276 struct hisi_sas_slot *slot); 277 void (*phys_init)(struct hisi_hba *hisi_hba); 278 void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no); 279 void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no); 280 void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no); 281 void (*get_events)(struct hisi_hba *hisi_hba, int phy_no); 282 void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no, 283 struct sas_phy_linkrates *linkrates); 284 enum sas_linkrate (*phy_get_max_linkrate)(void); 285 void (*clear_itct)(struct hisi_hba *hisi_hba, 286 struct hisi_sas_device *dev); 287 void (*free_device)(struct hisi_sas_device *sas_dev); 288 int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id); 289 void (*dereg_device)(struct hisi_hba *hisi_hba, 290 struct domain_device *device); 291 int (*soft_reset)(struct hisi_hba *hisi_hba); 292 u32 (*get_phys_state)(struct hisi_hba *hisi_hba); 293 int (*write_gpio)(struct hisi_hba *hisi_hba, u8 reg_type, 294 u8 reg_index, u8 reg_count, u8 *write_data); 295 int (*wait_cmds_complete_timeout)(struct hisi_hba *hisi_hba, 296 int delay_ms, int timeout_ms); 297 void (*snapshot_prepare)(struct hisi_hba *hisi_hba); 298 void (*snapshot_restore)(struct hisi_hba *hisi_hba); 299 int max_command_entries; 300 int complete_hdr_size; 301 struct scsi_host_template *sht; 302 303 const struct hisi_sas_debugfs_reg *debugfs_reg_global; 304 const struct hisi_sas_debugfs_reg *debugfs_reg_port; 305 }; 306 307 struct hisi_hba { 308 /* This must be the first element, used by SHOST_TO_SAS_HA */ 309 struct sas_ha_struct *p; 310 311 struct platform_device *platform_dev; 312 struct pci_dev *pci_dev; 313 struct device *dev; 314 315 int prot_mask; 316 317 void __iomem *regs; 318 void __iomem *sgpio_regs; 319 struct regmap *ctrl; 320 u32 ctrl_reset_reg; 321 u32 ctrl_reset_sts_reg; 322 u32 ctrl_clock_ena_reg; 323 u32 refclk_frequency_mhz; 324 u8 sas_addr[SAS_ADDR_SIZE]; 325 326 int n_phy; 327 spinlock_t lock; 328 struct semaphore sem; 329 330 struct timer_list timer; 331 struct workqueue_struct *wq; 332 333 int slot_index_count; 334 int last_slot_index; 335 int last_dev_id; 336 unsigned long *slot_index_tags; 337 unsigned long reject_stp_links_msk; 338 339 /* SCSI/SAS glue */ 340 struct sas_ha_struct sha; 341 struct Scsi_Host *shost; 342 343 struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES]; 344 struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES]; 345 struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS]; 346 struct hisi_sas_port port[HISI_SAS_MAX_PHYS]; 347 348 int queue_count; 349 350 struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES]; 351 struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES]; 352 dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES]; 353 void *complete_hdr[HISI_SAS_MAX_QUEUES]; 354 dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES]; 355 struct hisi_sas_initial_fis *initial_fis; 356 dma_addr_t initial_fis_dma; 357 struct hisi_sas_itct *itct; 358 dma_addr_t itct_dma; 359 struct hisi_sas_iost *iost; 360 dma_addr_t iost_dma; 361 struct hisi_sas_breakpoint *breakpoint; 362 dma_addr_t breakpoint_dma; 363 struct hisi_sas_breakpoint *sata_breakpoint; 364 dma_addr_t sata_breakpoint_dma; 365 struct hisi_sas_slot *slot_info; 366 unsigned long flags; 367 const struct hisi_sas_hw *hw; /* Low level hw interface */ 368 unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)]; 369 struct work_struct rst_work; 370 struct work_struct debugfs_work; 371 u32 phy_state; 372 u32 intr_coal_ticks; /* Time of interrupt coalesce in us */ 373 u32 intr_coal_count; /* Interrupt count to coalesce */ 374 375 int cq_nvecs; 376 unsigned int *reply_map; 377 378 /* debugfs memories */ 379 u32 *debugfs_global_reg; 380 u32 *debugfs_port_reg[HISI_SAS_MAX_PHYS]; 381 void *debugfs_complete_hdr[HISI_SAS_MAX_QUEUES]; 382 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr[HISI_SAS_MAX_QUEUES]; 383 struct hisi_sas_iost *debugfs_iost; 384 struct hisi_sas_itct *debugfs_itct; 385 386 struct dentry *debugfs_dir; 387 struct dentry *debugfs_dump_dentry; 388 bool debugfs_snapshot; 389 }; 390 391 /* Generic HW DMA host memory structures */ 392 /* Delivery queue header */ 393 struct hisi_sas_cmd_hdr { 394 /* dw0 */ 395 __le32 dw0; 396 397 /* dw1 */ 398 __le32 dw1; 399 400 /* dw2 */ 401 __le32 dw2; 402 403 /* dw3 */ 404 __le32 transfer_tags; 405 406 /* dw4 */ 407 __le32 data_transfer_len; 408 409 /* dw5 */ 410 __le32 first_burst_num; 411 412 /* dw6 */ 413 __le32 sg_len; 414 415 /* dw7 */ 416 __le32 dw7; 417 418 /* dw8-9 */ 419 __le64 cmd_table_addr; 420 421 /* dw10-11 */ 422 __le64 sts_buffer_addr; 423 424 /* dw12-13 */ 425 __le64 prd_table_addr; 426 427 /* dw14-15 */ 428 __le64 dif_prd_table_addr; 429 }; 430 431 struct hisi_sas_itct { 432 __le64 qw0; 433 __le64 sas_addr; 434 __le64 qw2; 435 __le64 qw3; 436 __le64 qw4_15[12]; 437 }; 438 439 struct hisi_sas_iost { 440 __le64 qw0; 441 __le64 qw1; 442 __le64 qw2; 443 __le64 qw3; 444 }; 445 446 struct hisi_sas_err_record { 447 u32 data[4]; 448 }; 449 450 struct hisi_sas_initial_fis { 451 struct hisi_sas_err_record err_record; 452 struct dev_to_host_fis fis; 453 u32 rsvd[3]; 454 }; 455 456 struct hisi_sas_breakpoint { 457 u8 data[128]; 458 }; 459 460 struct hisi_sas_sata_breakpoint { 461 struct hisi_sas_breakpoint tag[32]; 462 }; 463 464 struct hisi_sas_sge { 465 __le64 addr; 466 __le32 page_ctrl_0; 467 __le32 page_ctrl_1; 468 __le32 data_len; 469 __le32 data_off; 470 }; 471 472 struct hisi_sas_command_table_smp { 473 u8 bytes[44]; 474 }; 475 476 struct hisi_sas_command_table_stp { 477 struct host_to_dev_fis command_fis; 478 u8 dummy[12]; 479 u8 atapi_cdb[ATAPI_CDB_LEN]; 480 }; 481 482 #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE 483 struct hisi_sas_sge_page { 484 struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT]; 485 } __aligned(16); 486 487 #define HISI_SAS_SGE_DIF_PAGE_CNT SG_CHUNK_SIZE 488 struct hisi_sas_sge_dif_page { 489 struct hisi_sas_sge sge[HISI_SAS_SGE_DIF_PAGE_CNT]; 490 } __aligned(16); 491 492 struct hisi_sas_command_table_ssp { 493 struct ssp_frame_hdr hdr; 494 union { 495 struct { 496 struct ssp_command_iu task; 497 u32 prot[7]; 498 }; 499 struct ssp_tmf_iu ssp_task; 500 struct xfer_rdy_iu xfer_rdy; 501 struct ssp_response_iu ssp_res; 502 } u; 503 }; 504 505 union hisi_sas_command_table { 506 struct hisi_sas_command_table_ssp ssp; 507 struct hisi_sas_command_table_smp smp; 508 struct hisi_sas_command_table_stp stp; 509 } __aligned(16); 510 511 struct hisi_sas_status_buffer { 512 struct hisi_sas_err_record err; 513 u8 iu[1024]; 514 } __aligned(16); 515 516 struct hisi_sas_slot_buf_table { 517 struct hisi_sas_status_buffer status_buffer; 518 union hisi_sas_command_table command_header; 519 struct hisi_sas_sge_page sge_page; 520 }; 521 522 struct hisi_sas_slot_dif_buf_table { 523 struct hisi_sas_slot_buf_table slot_buf; 524 struct hisi_sas_sge_dif_page sge_dif_page; 525 }; 526 527 extern struct scsi_transport_template *hisi_sas_stt; 528 529 extern bool hisi_sas_debugfs_enable; 530 extern struct dentry *hisi_sas_debugfs_dir; 531 532 extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba); 533 extern int hisi_sas_alloc(struct hisi_hba *hisi_hba); 534 extern void hisi_sas_free(struct hisi_hba *hisi_hba); 535 extern u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, 536 int direction); 537 extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port); 538 extern void hisi_sas_sata_done(struct sas_task *task, 539 struct hisi_sas_slot *slot); 540 extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag); 541 extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba); 542 extern int hisi_sas_probe(struct platform_device *pdev, 543 const struct hisi_sas_hw *ops); 544 extern int hisi_sas_remove(struct platform_device *pdev); 545 546 extern int hisi_sas_slave_configure(struct scsi_device *sdev); 547 extern int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time); 548 extern void hisi_sas_scan_start(struct Scsi_Host *shost); 549 extern int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type); 550 extern void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, 551 int enable); 552 extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy); 553 extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, 554 struct sas_task *task, 555 struct hisi_sas_slot *slot); 556 extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba); 557 extern void hisi_sas_rst_work_handler(struct work_struct *work); 558 extern void hisi_sas_sync_rst_work_handler(struct work_struct *work); 559 extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba); 560 extern void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no); 561 extern bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy, 562 enum hisi_sas_phy_event event); 563 extern void hisi_sas_release_tasks(struct hisi_hba *hisi_hba); 564 extern u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max); 565 extern void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba); 566 extern void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba); 567 extern void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba); 568 extern void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba); 569 extern void hisi_sas_debugfs_work_handler(struct work_struct *work); 570 #endif 571