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