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