2 * driver for Microsemi PQI-based storage controllers
3 * Copyright (c) 2016 Microsemi Corporation
4 * Copyright (c) 2016 PMC-Sierra, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
15 * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/pci.h>
23 #include <scsi/scsi_device.h>
24 #include <asm/unaligned.h>
26 #include "smartpqi_sis.h"
28 /* legacy SIS interface commands */
29 #define SIS_CMD_GET_ADAPTER_PROPERTIES 0x19
30 #define SIS_CMD_INIT_BASE_STRUCT_ADDRESS 0x1b
31 #define SIS_CMD_GET_PQI_CAPABILITIES 0x3000
33 /* for submission of legacy SIS commands */
34 #define SIS_REENABLE_SIS_MODE 0x1
35 #define SIS_ENABLE_MSIX 0x40
36 #define SIS_SOFT_RESET 0x100
37 #define SIS_CMD_READY 0x200
38 #define SIS_CMD_COMPLETE 0x1000
39 #define SIS_CLEAR_CTRL_TO_HOST_DOORBELL 0x1000
40 #define SIS_CMD_STATUS_SUCCESS 0x1
41 #define SIS_CMD_COMPLETE_TIMEOUT_SECS 30
42 #define SIS_CMD_COMPLETE_POLL_INTERVAL_MSECS 10
44 /* used with SIS_CMD_GET_ADAPTER_PROPERTIES command */
45 #define SIS_EXTENDED_PROPERTIES_SUPPORTED 0x800000
46 #define SIS_SMARTARRAY_FEATURES_SUPPORTED 0x2
47 #define SIS_PQI_MODE_SUPPORTED 0x4
48 #define SIS_REQUIRED_EXTENDED_PROPERTIES \
49 (SIS_SMARTARRAY_FEATURES_SUPPORTED | SIS_PQI_MODE_SUPPORTED)
51 /* used with SIS_CMD_INIT_BASE_STRUCT_ADDRESS command */
52 #define SIS_BASE_STRUCT_REVISION 9
53 #define SIS_BASE_STRUCT_ALIGNMENT 16
55 #define SIS_CTRL_KERNEL_UP 0x80
56 #define SIS_CTRL_KERNEL_PANIC 0x100
57 #define SIS_CTRL_READY_TIMEOUT_SECS 30
58 #define SIS_CTRL_READY_POLL_INTERVAL_MSECS 10
62 /* for use with SIS_CMD_INIT_BASE_STRUCT_ADDRESS command */
63 struct sis_base_struct
{
64 __le32 revision
; /* revision of this structure */
65 __le32 flags
; /* reserved */
66 __le32 error_buffer_paddr_low
; /* lower 32 bits of physical memory */
67 /* buffer for PQI error response */
69 __le32 error_buffer_paddr_high
; /* upper 32 bits of physical */
70 /* memory buffer for PQI */
71 /* error response data */
72 __le32 error_buffer_element_length
; /* length of each PQI error */
73 /* response buffer element */
75 __le32 error_buffer_num_elements
; /* total number of PQI error */
76 /* response buffers available */
81 int sis_wait_for_ctrl_ready(struct pqi_ctrl_info
*ctrl_info
)
83 unsigned long timeout
;
86 timeout
= (SIS_CTRL_READY_TIMEOUT_SECS
* HZ
) + jiffies
;
89 status
= readl(&ctrl_info
->registers
->sis_firmware_status
);
91 if (status
& SIS_CTRL_KERNEL_PANIC
) {
92 dev_err(&ctrl_info
->pci_dev
->dev
,
93 "controller is offline: status code 0x%x\n",
95 &ctrl_info
->registers
->sis_mailbox
[7]));
98 if (status
& SIS_CTRL_KERNEL_UP
)
101 if (time_after(jiffies
, timeout
))
103 msleep(SIS_CTRL_READY_POLL_INTERVAL_MSECS
);
109 bool sis_is_firmware_running(struct pqi_ctrl_info
*ctrl_info
)
114 status
= readl(&ctrl_info
->registers
->sis_firmware_status
);
116 if (status
& SIS_CTRL_KERNEL_PANIC
)
122 dev_err(&ctrl_info
->pci_dev
->dev
,
123 "controller is offline: status code 0x%x\n",
124 readl(&ctrl_info
->registers
->sis_mailbox
[7]));
129 /* used for passing command parameters/results when issuing SIS commands */
130 struct sis_sync_cmd_params
{
131 u32 mailbox
[6]; /* mailboxes 0-5 */
134 static int sis_send_sync_cmd(struct pqi_ctrl_info
*ctrl_info
,
135 u32 cmd
, struct sis_sync_cmd_params
*params
)
137 struct pqi_ctrl_registers __iomem
*registers
;
139 unsigned long timeout
;
143 registers
= ctrl_info
->registers
;
145 /* Write the command to mailbox 0. */
146 writel(cmd
, ®isters
->sis_mailbox
[0]);
149 * Write the command parameters to mailboxes 1-4 (mailbox 5 is not used
150 * when sending a command to the controller).
152 for (i
= 1; i
<= 4; i
++)
153 writel(params
->mailbox
[i
], ®isters
->sis_mailbox
[i
]);
155 /* Clear the command doorbell. */
156 writel(SIS_CLEAR_CTRL_TO_HOST_DOORBELL
,
157 ®isters
->sis_ctrl_to_host_doorbell_clear
);
159 /* Disable doorbell interrupts by masking all interrupts. */
160 writel(~0, ®isters
->sis_interrupt_mask
);
163 * Force the completion of the interrupt mask register write before
164 * submitting the command.
166 readl(®isters
->sis_interrupt_mask
);
168 /* Submit the command to the controller. */
169 writel(SIS_CMD_READY
, ®isters
->sis_host_to_ctrl_doorbell
);
172 * Poll for command completion. Note that the call to msleep() is at
173 * the top of the loop in order to give the controller time to start
174 * processing the command before we start polling.
176 timeout
= (SIS_CMD_COMPLETE_TIMEOUT_SECS
* HZ
) + jiffies
;
178 msleep(SIS_CMD_COMPLETE_POLL_INTERVAL_MSECS
);
179 doorbell
= readl(®isters
->sis_ctrl_to_host_doorbell
);
180 if (doorbell
& SIS_CMD_COMPLETE
)
182 if (time_after(jiffies
, timeout
))
186 /* Read the command status from mailbox 0. */
187 cmd_status
= readl(®isters
->sis_mailbox
[0]);
188 if (cmd_status
!= SIS_CMD_STATUS_SUCCESS
) {
189 dev_err(&ctrl_info
->pci_dev
->dev
,
190 "SIS command failed for command 0x%x: status = 0x%x\n",
196 * The command completed successfully, so save the command status and
197 * read the values returned in mailboxes 1-5.
199 params
->mailbox
[0] = cmd_status
;
200 for (i
= 1; i
< ARRAY_SIZE(params
->mailbox
); i
++)
201 params
->mailbox
[i
] = readl(®isters
->sis_mailbox
[i
]);
207 * This function verifies that we are talking to a controller that speaks PQI.
210 int sis_get_ctrl_properties(struct pqi_ctrl_info
*ctrl_info
)
214 u32 extended_properties
;
215 struct sis_sync_cmd_params params
;
217 memset(¶ms
, 0, sizeof(params
));
219 rc
= sis_send_sync_cmd(ctrl_info
, SIS_CMD_GET_ADAPTER_PROPERTIES
,
224 properties
= params
.mailbox
[1];
226 if (!(properties
& SIS_EXTENDED_PROPERTIES_SUPPORTED
))
229 extended_properties
= params
.mailbox
[4];
231 if ((extended_properties
& SIS_REQUIRED_EXTENDED_PROPERTIES
) !=
232 SIS_REQUIRED_EXTENDED_PROPERTIES
)
238 int sis_get_pqi_capabilities(struct pqi_ctrl_info
*ctrl_info
)
241 struct sis_sync_cmd_params params
;
243 memset(¶ms
, 0, sizeof(params
));
245 rc
= sis_send_sync_cmd(ctrl_info
, SIS_CMD_GET_PQI_CAPABILITIES
,
250 ctrl_info
->max_sg_entries
= params
.mailbox
[1];
251 ctrl_info
->max_transfer_size
= params
.mailbox
[2];
252 ctrl_info
->max_outstanding_requests
= params
.mailbox
[3];
253 ctrl_info
->config_table_offset
= params
.mailbox
[4];
254 ctrl_info
->config_table_length
= params
.mailbox
[5];
259 int sis_init_base_struct_addr(struct pqi_ctrl_info
*ctrl_info
)
262 void *base_struct_unaligned
;
263 struct sis_base_struct
*base_struct
;
264 struct sis_sync_cmd_params params
;
265 unsigned long error_buffer_paddr
;
266 dma_addr_t bus_address
;
268 base_struct_unaligned
= kzalloc(sizeof(*base_struct
)
269 + SIS_BASE_STRUCT_ALIGNMENT
- 1, GFP_KERNEL
);
270 if (!base_struct_unaligned
)
273 base_struct
= PTR_ALIGN(base_struct_unaligned
,
274 SIS_BASE_STRUCT_ALIGNMENT
);
275 error_buffer_paddr
= (unsigned long)ctrl_info
->error_buffer_dma_handle
;
277 put_unaligned_le32(SIS_BASE_STRUCT_REVISION
, &base_struct
->revision
);
278 put_unaligned_le32(lower_32_bits(error_buffer_paddr
),
279 &base_struct
->error_buffer_paddr_low
);
280 put_unaligned_le32(upper_32_bits(error_buffer_paddr
),
281 &base_struct
->error_buffer_paddr_high
);
282 put_unaligned_le32(PQI_ERROR_BUFFER_ELEMENT_LENGTH
,
283 &base_struct
->error_buffer_element_length
);
284 put_unaligned_le32(ctrl_info
->max_io_slots
,
285 &base_struct
->error_buffer_num_elements
);
287 bus_address
= pci_map_single(ctrl_info
->pci_dev
, base_struct
,
288 sizeof(*base_struct
), PCI_DMA_TODEVICE
);
289 if (pci_dma_mapping_error(ctrl_info
->pci_dev
, bus_address
)) {
294 memset(¶ms
, 0, sizeof(params
));
295 params
.mailbox
[1] = lower_32_bits((u64
)bus_address
);
296 params
.mailbox
[2] = upper_32_bits((u64
)bus_address
);
297 params
.mailbox
[3] = sizeof(*base_struct
);
299 rc
= sis_send_sync_cmd(ctrl_info
, SIS_CMD_INIT_BASE_STRUCT_ADDRESS
,
302 pci_unmap_single(ctrl_info
->pci_dev
, bus_address
, sizeof(*base_struct
),
306 kfree(base_struct_unaligned
);
311 /* Enable MSI-X interrupts on the controller. */
313 void sis_enable_msix(struct pqi_ctrl_info
*ctrl_info
)
315 u32 doorbell_register
;
318 readl(&ctrl_info
->registers
->sis_host_to_ctrl_doorbell
);
319 doorbell_register
|= SIS_ENABLE_MSIX
;
321 writel(doorbell_register
,
322 &ctrl_info
->registers
->sis_host_to_ctrl_doorbell
);
325 /* Disable MSI-X interrupts on the controller. */
327 void sis_disable_msix(struct pqi_ctrl_info
*ctrl_info
)
329 u32 doorbell_register
;
332 readl(&ctrl_info
->registers
->sis_host_to_ctrl_doorbell
);
333 doorbell_register
&= ~SIS_ENABLE_MSIX
;
335 writel(doorbell_register
,
336 &ctrl_info
->registers
->sis_host_to_ctrl_doorbell
);
339 void sis_soft_reset(struct pqi_ctrl_info
*ctrl_info
)
341 writel(SIS_SOFT_RESET
,
342 &ctrl_info
->registers
->sis_host_to_ctrl_doorbell
);
345 #define SIS_MODE_READY_TIMEOUT_SECS 30
347 int sis_reenable_sis_mode(struct pqi_ctrl_info
*ctrl_info
)
350 unsigned long timeout
;
351 struct pqi_ctrl_registers __iomem
*registers
;
354 registers
= ctrl_info
->registers
;
356 writel(SIS_REENABLE_SIS_MODE
,
357 ®isters
->sis_host_to_ctrl_doorbell
);
360 timeout
= (SIS_MODE_READY_TIMEOUT_SECS
* HZ
) + jiffies
;
363 doorbell
= readl(®isters
->sis_ctrl_to_host_doorbell
);
364 if ((doorbell
& SIS_REENABLE_SIS_MODE
) == 0)
366 if (time_after(jiffies
, timeout
)) {
373 dev_err(&ctrl_info
->pci_dev
->dev
,
374 "re-enabling SIS mode failed\n");
379 void sis_write_driver_scratch(struct pqi_ctrl_info
*ctrl_info
, u32 value
)
381 writel(value
, &ctrl_info
->registers
->sis_driver_scratch
);
384 u32
sis_read_driver_scratch(struct pqi_ctrl_info
*ctrl_info
)
386 return readl(&ctrl_info
->registers
->sis_driver_scratch
);
389 static void __attribute__((unused
)) verify_structures(void)
391 BUILD_BUG_ON(offsetof(struct sis_base_struct
,
393 BUILD_BUG_ON(offsetof(struct sis_base_struct
,
395 BUILD_BUG_ON(offsetof(struct sis_base_struct
,
396 error_buffer_paddr_low
) != 0x8);
397 BUILD_BUG_ON(offsetof(struct sis_base_struct
,
398 error_buffer_paddr_high
) != 0xc);
399 BUILD_BUG_ON(offsetof(struct sis_base_struct
,
400 error_buffer_element_length
) != 0x10);
401 BUILD_BUG_ON(offsetof(struct sis_base_struct
,
402 error_buffer_num_elements
) != 0x14);
403 BUILD_BUG_ON(sizeof(struct sis_base_struct
) != 0x18);