hisi_sas.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #ifndef _HISI_SAS_H_
  12. #define _HISI_SAS_H_
  13. #include <linux/acpi.h>
  14. #include <linux/clk.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/iopoll.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. #define HISI_SAS_MAX_PHYS 9
  27. #define HISI_SAS_MAX_QUEUES 32
  28. #define HISI_SAS_QUEUE_SLOTS 512
  29. #define HISI_SAS_MAX_ITCT_ENTRIES 1024
  30. #define HISI_SAS_MAX_DEVICES HISI_SAS_MAX_ITCT_ENTRIES
  31. #define HISI_SAS_RESET_BIT 0
  32. #define HISI_SAS_REJECT_CMD_BIT 1
  33. #define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
  34. #define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
  35. #define hisi_sas_status_buf_addr(buf) \
  36. (buf + offsetof(struct hisi_sas_slot_buf_table, status_buffer))
  37. #define hisi_sas_status_buf_addr_mem(slot) hisi_sas_status_buf_addr(slot->buf)
  38. #define hisi_sas_status_buf_addr_dma(slot) \
  39. hisi_sas_status_buf_addr(slot->buf_dma)
  40. #define hisi_sas_cmd_hdr_addr(buf) \
  41. (buf + offsetof(struct hisi_sas_slot_buf_table, command_header))
  42. #define hisi_sas_cmd_hdr_addr_mem(slot) hisi_sas_cmd_hdr_addr(slot->buf)
  43. #define hisi_sas_cmd_hdr_addr_dma(slot) hisi_sas_cmd_hdr_addr(slot->buf_dma)
  44. #define hisi_sas_sge_addr(buf) \
  45. (buf + offsetof(struct hisi_sas_slot_buf_table, sge_page))
  46. #define hisi_sas_sge_addr_mem(slot) hisi_sas_sge_addr(slot->buf)
  47. #define hisi_sas_sge_addr_dma(slot) hisi_sas_sge_addr(slot->buf_dma)
  48. #define HISI_SAS_MAX_SSP_RESP_SZ (sizeof(struct ssp_frame_hdr) + 1024)
  49. #define HISI_SAS_MAX_SMP_RESP_SZ 1028
  50. #define HISI_SAS_MAX_STP_RESP_SZ 28
  51. #define DEV_IS_EXPANDER(type) \
  52. ((type == SAS_EDGE_EXPANDER_DEVICE) || \
  53. (type == SAS_FANOUT_EXPANDER_DEVICE))
  54. #define HISI_SAS_SATA_PROTOCOL_NONDATA 0x1
  55. #define HISI_SAS_SATA_PROTOCOL_PIO 0x2
  56. #define HISI_SAS_SATA_PROTOCOL_DMA 0x4
  57. #define HISI_SAS_SATA_PROTOCOL_FPDMA 0x8
  58. #define HISI_SAS_SATA_PROTOCOL_ATAPI 0x10
  59. struct hisi_hba;
  60. enum {
  61. PORT_TYPE_SAS = (1U << 1),
  62. PORT_TYPE_SATA = (1U << 0),
  63. };
  64. enum dev_status {
  65. HISI_SAS_DEV_NORMAL,
  66. HISI_SAS_DEV_EH,
  67. };
  68. enum {
  69. HISI_SAS_INT_ABT_CMD = 0,
  70. HISI_SAS_INT_ABT_DEV = 1,
  71. };
  72. enum hisi_sas_dev_type {
  73. HISI_SAS_DEV_TYPE_STP = 0,
  74. HISI_SAS_DEV_TYPE_SSP,
  75. HISI_SAS_DEV_TYPE_SATA,
  76. };
  77. struct hisi_sas_hw_error {
  78. u32 irq_msk;
  79. u32 msk;
  80. int shift;
  81. const char *msg;
  82. int reg;
  83. const struct hisi_sas_hw_error *sub;
  84. };
  85. struct hisi_sas_phy {
  86. struct hisi_hba *hisi_hba;
  87. struct hisi_sas_port *port;
  88. struct asd_sas_phy sas_phy;
  89. struct sas_identify identify;
  90. struct work_struct phyup_ws;
  91. u64 port_id; /* from hw */
  92. u64 dev_sas_addr;
  93. u64 frame_rcvd_size;
  94. u8 frame_rcvd[32];
  95. u8 phy_attached;
  96. u8 reserved[3];
  97. u32 phy_type;
  98. enum sas_linkrate minimum_linkrate;
  99. enum sas_linkrate maximum_linkrate;
  100. };
  101. struct hisi_sas_port {
  102. struct asd_sas_port sas_port;
  103. u8 port_attached;
  104. u8 id; /* from hw */
  105. };
  106. struct hisi_sas_cq {
  107. struct hisi_hba *hisi_hba;
  108. struct tasklet_struct tasklet;
  109. int rd_point;
  110. int id;
  111. };
  112. struct hisi_sas_dq {
  113. struct hisi_hba *hisi_hba;
  114. struct hisi_sas_slot *slot_prep;
  115. spinlock_t lock;
  116. int wr_point;
  117. int id;
  118. };
  119. struct hisi_sas_device {
  120. struct hisi_hba *hisi_hba;
  121. struct domain_device *sas_device;
  122. struct completion *completion;
  123. struct hisi_sas_dq *dq;
  124. struct list_head list;
  125. u64 attached_phy;
  126. atomic64_t running_req;
  127. enum sas_device_type dev_type;
  128. int device_id;
  129. int sata_idx;
  130. u8 dev_status;
  131. };
  132. struct hisi_sas_slot {
  133. struct list_head entry;
  134. struct sas_task *task;
  135. struct hisi_sas_port *port;
  136. u64 n_elem;
  137. int dlvry_queue;
  138. int dlvry_queue_slot;
  139. int cmplt_queue;
  140. int cmplt_queue_slot;
  141. int idx;
  142. int abort;
  143. void *buf;
  144. dma_addr_t buf_dma;
  145. void *cmd_hdr;
  146. dma_addr_t cmd_hdr_dma;
  147. struct work_struct abort_slot;
  148. struct timer_list internal_abort_timer;
  149. };
  150. struct hisi_sas_tmf_task {
  151. u8 tmf;
  152. u16 tag_of_task_to_be_managed;
  153. };
  154. struct hisi_sas_hw {
  155. int (*hw_init)(struct hisi_hba *hisi_hba);
  156. void (*setup_itct)(struct hisi_hba *hisi_hba,
  157. struct hisi_sas_device *device);
  158. int (*slot_index_alloc)(struct hisi_hba *hisi_hba, int *slot_idx,
  159. struct domain_device *device);
  160. struct hisi_sas_device *(*alloc_dev)(struct domain_device *device);
  161. void (*sl_notify)(struct hisi_hba *hisi_hba, int phy_no);
  162. int (*get_free_slot)(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq);
  163. void (*start_delivery)(struct hisi_sas_dq *dq);
  164. int (*prep_ssp)(struct hisi_hba *hisi_hba,
  165. struct hisi_sas_slot *slot, int is_tmf,
  166. struct hisi_sas_tmf_task *tmf);
  167. int (*prep_smp)(struct hisi_hba *hisi_hba,
  168. struct hisi_sas_slot *slot);
  169. int (*prep_stp)(struct hisi_hba *hisi_hba,
  170. struct hisi_sas_slot *slot);
  171. int (*prep_abort)(struct hisi_hba *hisi_hba,
  172. struct hisi_sas_slot *slot,
  173. int device_id, int abort_flag, int tag_to_abort);
  174. int (*slot_complete)(struct hisi_hba *hisi_hba,
  175. struct hisi_sas_slot *slot);
  176. void (*phys_init)(struct hisi_hba *hisi_hba);
  177. void (*phy_start)(struct hisi_hba *hisi_hba, int phy_no);
  178. void (*phy_disable)(struct hisi_hba *hisi_hba, int phy_no);
  179. void (*phy_hard_reset)(struct hisi_hba *hisi_hba, int phy_no);
  180. void (*get_events)(struct hisi_hba *hisi_hba, int phy_no);
  181. void (*phy_set_linkrate)(struct hisi_hba *hisi_hba, int phy_no,
  182. struct sas_phy_linkrates *linkrates);
  183. enum sas_linkrate (*phy_get_max_linkrate)(void);
  184. void (*free_device)(struct hisi_hba *hisi_hba,
  185. struct hisi_sas_device *dev);
  186. int (*get_wideport_bitmap)(struct hisi_hba *hisi_hba, int port_id);
  187. void (*dereg_device)(struct hisi_hba *hisi_hba,
  188. struct domain_device *device);
  189. int (*soft_reset)(struct hisi_hba *hisi_hba);
  190. u32 (*get_phys_state)(struct hisi_hba *hisi_hba);
  191. int max_command_entries;
  192. int complete_hdr_size;
  193. };
  194. struct hisi_hba {
  195. /* This must be the first element, used by SHOST_TO_SAS_HA */
  196. struct sas_ha_struct *p;
  197. struct platform_device *platform_dev;
  198. struct pci_dev *pci_dev;
  199. struct device *dev;
  200. void __iomem *regs;
  201. struct regmap *ctrl;
  202. u32 ctrl_reset_reg;
  203. u32 ctrl_reset_sts_reg;
  204. u32 ctrl_clock_ena_reg;
  205. u32 refclk_frequency_mhz;
  206. u8 sas_addr[SAS_ADDR_SIZE];
  207. int n_phy;
  208. spinlock_t lock;
  209. struct timer_list timer;
  210. struct workqueue_struct *wq;
  211. int slot_index_count;
  212. unsigned long *slot_index_tags;
  213. unsigned long reject_stp_links_msk;
  214. /* SCSI/SAS glue */
  215. struct sas_ha_struct sha;
  216. struct Scsi_Host *shost;
  217. struct hisi_sas_cq cq[HISI_SAS_MAX_QUEUES];
  218. struct hisi_sas_dq dq[HISI_SAS_MAX_QUEUES];
  219. struct hisi_sas_phy phy[HISI_SAS_MAX_PHYS];
  220. struct hisi_sas_port port[HISI_SAS_MAX_PHYS];
  221. int queue_count;
  222. struct dma_pool *buffer_pool;
  223. struct hisi_sas_device devices[HISI_SAS_MAX_DEVICES];
  224. struct hisi_sas_cmd_hdr *cmd_hdr[HISI_SAS_MAX_QUEUES];
  225. dma_addr_t cmd_hdr_dma[HISI_SAS_MAX_QUEUES];
  226. void *complete_hdr[HISI_SAS_MAX_QUEUES];
  227. dma_addr_t complete_hdr_dma[HISI_SAS_MAX_QUEUES];
  228. struct hisi_sas_initial_fis *initial_fis;
  229. dma_addr_t initial_fis_dma;
  230. struct hisi_sas_itct *itct;
  231. dma_addr_t itct_dma;
  232. struct hisi_sas_iost *iost;
  233. dma_addr_t iost_dma;
  234. struct hisi_sas_breakpoint *breakpoint;
  235. dma_addr_t breakpoint_dma;
  236. struct hisi_sas_breakpoint *sata_breakpoint;
  237. dma_addr_t sata_breakpoint_dma;
  238. struct hisi_sas_slot *slot_info;
  239. unsigned long flags;
  240. const struct hisi_sas_hw *hw; /* Low level hw interface */
  241. unsigned long sata_dev_bitmap[BITS_TO_LONGS(HISI_SAS_MAX_DEVICES)];
  242. struct work_struct rst_work;
  243. };
  244. /* Generic HW DMA host memory structures */
  245. /* Delivery queue header */
  246. struct hisi_sas_cmd_hdr {
  247. /* dw0 */
  248. __le32 dw0;
  249. /* dw1 */
  250. __le32 dw1;
  251. /* dw2 */
  252. __le32 dw2;
  253. /* dw3 */
  254. __le32 transfer_tags;
  255. /* dw4 */
  256. __le32 data_transfer_len;
  257. /* dw5 */
  258. __le32 first_burst_num;
  259. /* dw6 */
  260. __le32 sg_len;
  261. /* dw7 */
  262. __le32 dw7;
  263. /* dw8-9 */
  264. __le64 cmd_table_addr;
  265. /* dw10-11 */
  266. __le64 sts_buffer_addr;
  267. /* dw12-13 */
  268. __le64 prd_table_addr;
  269. /* dw14-15 */
  270. __le64 dif_prd_table_addr;
  271. };
  272. struct hisi_sas_itct {
  273. __le64 qw0;
  274. __le64 sas_addr;
  275. __le64 qw2;
  276. __le64 qw3;
  277. __le64 qw4_15[12];
  278. };
  279. struct hisi_sas_iost {
  280. __le64 qw0;
  281. __le64 qw1;
  282. __le64 qw2;
  283. __le64 qw3;
  284. };
  285. struct hisi_sas_err_record {
  286. u32 data[4];
  287. };
  288. struct hisi_sas_initial_fis {
  289. struct hisi_sas_err_record err_record;
  290. struct dev_to_host_fis fis;
  291. u32 rsvd[3];
  292. };
  293. struct hisi_sas_breakpoint {
  294. u8 data[128];
  295. };
  296. struct hisi_sas_sata_breakpoint {
  297. struct hisi_sas_breakpoint tag[32];
  298. };
  299. struct hisi_sas_sge {
  300. __le64 addr;
  301. __le32 page_ctrl_0;
  302. __le32 page_ctrl_1;
  303. __le32 data_len;
  304. __le32 data_off;
  305. };
  306. struct hisi_sas_command_table_smp {
  307. u8 bytes[44];
  308. };
  309. struct hisi_sas_command_table_stp {
  310. struct host_to_dev_fis command_fis;
  311. u8 dummy[12];
  312. u8 atapi_cdb[ATAPI_CDB_LEN];
  313. };
  314. #define HISI_SAS_SGE_PAGE_CNT SG_CHUNK_SIZE
  315. struct hisi_sas_sge_page {
  316. struct hisi_sas_sge sge[HISI_SAS_SGE_PAGE_CNT];
  317. } __aligned(16);
  318. struct hisi_sas_command_table_ssp {
  319. struct ssp_frame_hdr hdr;
  320. union {
  321. struct {
  322. struct ssp_command_iu task;
  323. u32 prot[6];
  324. };
  325. struct ssp_tmf_iu ssp_task;
  326. struct xfer_rdy_iu xfer_rdy;
  327. struct ssp_response_iu ssp_res;
  328. } u;
  329. };
  330. union hisi_sas_command_table {
  331. struct hisi_sas_command_table_ssp ssp;
  332. struct hisi_sas_command_table_smp smp;
  333. struct hisi_sas_command_table_stp stp;
  334. } __aligned(16);
  335. struct hisi_sas_status_buffer {
  336. struct hisi_sas_err_record err;
  337. u8 iu[1024];
  338. } __aligned(16);
  339. struct hisi_sas_slot_buf_table {
  340. struct hisi_sas_status_buffer status_buffer;
  341. union hisi_sas_command_table command_header;
  342. struct hisi_sas_sge_page sge_page;
  343. };
  344. extern struct scsi_transport_template *hisi_sas_stt;
  345. extern struct scsi_host_template *hisi_sas_sht;
  346. extern void hisi_sas_stop_phys(struct hisi_hba *hisi_hba);
  347. extern void hisi_sas_init_add(struct hisi_hba *hisi_hba);
  348. extern int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost);
  349. extern void hisi_sas_free(struct hisi_hba *hisi_hba);
  350. extern u8 hisi_sas_get_ata_protocol(u8 cmd, int direction);
  351. extern struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port);
  352. extern void hisi_sas_sata_done(struct sas_task *task,
  353. struct hisi_sas_slot *slot);
  354. extern int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag);
  355. extern int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba);
  356. extern int hisi_sas_probe(struct platform_device *pdev,
  357. const struct hisi_sas_hw *ops);
  358. extern int hisi_sas_remove(struct platform_device *pdev);
  359. extern void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy);
  360. extern void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba,
  361. struct sas_task *task,
  362. struct hisi_sas_slot *slot);
  363. extern void hisi_sas_init_mem(struct hisi_hba *hisi_hba);
  364. extern void hisi_sas_rst_work_handler(struct work_struct *work);
  365. extern void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba);
  366. #endif