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