Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1 | /* |
| 2 | * driver for Microsemi PQI-based storage controllers |
Kevin Barnett | b805dbf | 2017-05-03 18:54:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2017 Microsemi Corporation |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4 | * Copyright (c) 2016 PMC-Sierra, Inc. |
| 5 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 15 | * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/rtc.h> |
| 26 | #include <linux/bcd.h> |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 27 | #include <linux/reboot.h> |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 28 | #include <linux/cciss_ioctl.h> |
Christoph Hellwig | 5219822 | 2016-11-01 08:12:49 -0600 | [diff] [blame] | 29 | #include <linux/blk-mq-pci.h> |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 30 | #include <scsi/scsi_host.h> |
| 31 | #include <scsi/scsi_cmnd.h> |
| 32 | #include <scsi/scsi_device.h> |
| 33 | #include <scsi/scsi_eh.h> |
| 34 | #include <scsi/scsi_transport_sas.h> |
| 35 | #include <asm/unaligned.h> |
| 36 | #include "smartpqi.h" |
| 37 | #include "smartpqi_sis.h" |
| 38 | |
| 39 | #if !defined(BUILD_TIMESTAMP) |
| 40 | #define BUILD_TIMESTAMP |
| 41 | #endif |
| 42 | |
Don Brace | 4ae5e9d | 2018-06-18 13:23:06 -0500 | [diff] [blame] | 43 | #define DRIVER_VERSION "1.1.4-130" |
Kevin Barnett | 2d154f5f | 2017-05-03 18:55:55 -0500 | [diff] [blame] | 44 | #define DRIVER_MAJOR 1 |
Kevin Barnett | b98117c | 2017-08-10 13:47:15 -0500 | [diff] [blame] | 45 | #define DRIVER_MINOR 1 |
Don Brace | 61c187e | 2018-03-21 13:32:37 -0500 | [diff] [blame] | 46 | #define DRIVER_RELEASE 4 |
Don Brace | 4ae5e9d | 2018-06-18 13:23:06 -0500 | [diff] [blame] | 47 | #define DRIVER_REVISION 130 |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 48 | |
Kevin Barnett | 2d154f5f | 2017-05-03 18:55:55 -0500 | [diff] [blame] | 49 | #define DRIVER_NAME "Microsemi PQI Driver (v" \ |
| 50 | DRIVER_VERSION BUILD_TIMESTAMP ")" |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 51 | #define DRIVER_NAME_SHORT "smartpqi" |
| 52 | |
Kevin Barnett | e1d213b | 2017-05-03 18:53:18 -0500 | [diff] [blame] | 53 | #define PQI_EXTRA_SGL_MEMORY (12 * sizeof(struct pqi_sg_descriptor)) |
| 54 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 55 | MODULE_AUTHOR("Microsemi"); |
| 56 | MODULE_DESCRIPTION("Driver for Microsemi Smart Family Controller version " |
| 57 | DRIVER_VERSION); |
| 58 | MODULE_SUPPORTED_DEVICE("Microsemi Smart Family Controllers"); |
| 59 | MODULE_VERSION(DRIVER_VERSION); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 62 | static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info); |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 63 | static void pqi_ctrl_offline_worker(struct work_struct *work); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 64 | static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 65 | static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info); |
| 66 | static void pqi_scan_start(struct Scsi_Host *shost); |
| 67 | static void pqi_start_io(struct pqi_ctrl_info *ctrl_info, |
| 68 | struct pqi_queue_group *queue_group, enum pqi_io_path path, |
| 69 | struct pqi_io_request *io_request); |
| 70 | static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info, |
| 71 | struct pqi_iu_header *request, unsigned int flags, |
| 72 | struct pqi_raid_error_info *error_info, unsigned long timeout_msecs); |
| 73 | static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info, |
| 74 | struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb, |
| 75 | unsigned int cdb_length, struct pqi_queue_group *queue_group, |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 76 | struct pqi_encryption_info *encryption_info, bool raid_bypass); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 77 | |
| 78 | /* for flags argument to pqi_submit_raid_request_synchronous() */ |
| 79 | #define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1 |
| 80 | |
| 81 | static struct scsi_transport_template *pqi_sas_transport_template; |
| 82 | |
| 83 | static atomic_t pqi_controller_count = ATOMIC_INIT(0); |
| 84 | |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 85 | enum pqi_lockup_action { |
| 86 | NONE, |
| 87 | REBOOT, |
| 88 | PANIC |
| 89 | }; |
| 90 | |
| 91 | static enum pqi_lockup_action pqi_lockup_action = NONE; |
| 92 | |
| 93 | static struct { |
| 94 | enum pqi_lockup_action action; |
| 95 | char *name; |
| 96 | } pqi_lockup_actions[] = { |
| 97 | { |
| 98 | .action = NONE, |
| 99 | .name = "none", |
| 100 | }, |
| 101 | { |
| 102 | .action = REBOOT, |
| 103 | .name = "reboot", |
| 104 | }, |
| 105 | { |
| 106 | .action = PANIC, |
| 107 | .name = "panic", |
| 108 | }, |
| 109 | }; |
| 110 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 111 | static unsigned int pqi_supported_event_types[] = { |
| 112 | PQI_EVENT_TYPE_HOTPLUG, |
| 113 | PQI_EVENT_TYPE_HARDWARE, |
| 114 | PQI_EVENT_TYPE_PHYSICAL_DEVICE, |
| 115 | PQI_EVENT_TYPE_LOGICAL_DEVICE, |
| 116 | PQI_EVENT_TYPE_AIO_STATE_CHANGE, |
| 117 | PQI_EVENT_TYPE_AIO_CONFIG_CHANGE, |
| 118 | }; |
| 119 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 120 | static int pqi_disable_device_id_wildcards; |
| 121 | module_param_named(disable_device_id_wildcards, |
Kevin Barnett | cbe0c7b | 2017-05-03 18:53:48 -0500 | [diff] [blame] | 122 | pqi_disable_device_id_wildcards, int, 0644); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 123 | MODULE_PARM_DESC(disable_device_id_wildcards, |
| 124 | "Disable device ID wildcards."); |
| 125 | |
Kevin Barnett | 5a259e3 | 2017-05-03 18:55:43 -0500 | [diff] [blame] | 126 | static int pqi_disable_heartbeat; |
| 127 | module_param_named(disable_heartbeat, |
| 128 | pqi_disable_heartbeat, int, 0644); |
| 129 | MODULE_PARM_DESC(disable_heartbeat, |
| 130 | "Disable heartbeat."); |
| 131 | |
| 132 | static int pqi_disable_ctrl_shutdown; |
| 133 | module_param_named(disable_ctrl_shutdown, |
| 134 | pqi_disable_ctrl_shutdown, int, 0644); |
| 135 | MODULE_PARM_DESC(disable_ctrl_shutdown, |
| 136 | "Disable controller shutdown when controller locked up."); |
| 137 | |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 138 | static char *pqi_lockup_action_param; |
| 139 | module_param_named(lockup_action, |
| 140 | pqi_lockup_action_param, charp, 0644); |
| 141 | MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n" |
| 142 | "\t\tSupported: none, reboot, panic\n" |
| 143 | "\t\tDefault: none"); |
| 144 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 145 | static char *raid_levels[] = { |
| 146 | "RAID-0", |
| 147 | "RAID-4", |
| 148 | "RAID-1(1+0)", |
| 149 | "RAID-5", |
| 150 | "RAID-5+1", |
| 151 | "RAID-ADG", |
| 152 | "RAID-1(ADM)", |
| 153 | }; |
| 154 | |
| 155 | static char *pqi_raid_level_to_string(u8 raid_level) |
| 156 | { |
| 157 | if (raid_level < ARRAY_SIZE(raid_levels)) |
| 158 | return raid_levels[raid_level]; |
| 159 | |
Kevin Barnett | a9f9339 | 2017-05-03 18:55:31 -0500 | [diff] [blame] | 160 | return "RAID UNKNOWN"; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | #define SA_RAID_0 0 |
| 164 | #define SA_RAID_4 1 |
| 165 | #define SA_RAID_1 2 /* also used for RAID 10 */ |
| 166 | #define SA_RAID_5 3 /* also used for RAID 50 */ |
| 167 | #define SA_RAID_51 4 |
| 168 | #define SA_RAID_6 5 /* also used for RAID 60 */ |
| 169 | #define SA_RAID_ADM 6 /* also used for RAID 1+0 ADM */ |
| 170 | #define SA_RAID_MAX SA_RAID_ADM |
| 171 | #define SA_RAID_UNKNOWN 0xff |
| 172 | |
| 173 | static inline void pqi_scsi_done(struct scsi_cmnd *scmd) |
| 174 | { |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 175 | pqi_prep_for_scsi_done(scmd); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 176 | scmd->scsi_done(scmd); |
| 177 | } |
| 178 | |
Dave Carroll | b6e2ef6 | 2018-12-07 16:28:23 -0600 | [diff] [blame] | 179 | static inline void pqi_disable_write_same(struct scsi_device *sdev) |
| 180 | { |
| 181 | sdev->no_write_same = 1; |
| 182 | } |
| 183 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 184 | static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2) |
| 185 | { |
| 186 | return memcmp(scsi3addr1, scsi3addr2, 8) == 0; |
| 187 | } |
| 188 | |
| 189 | static inline struct pqi_ctrl_info *shost_to_hba(struct Scsi_Host *shost) |
| 190 | { |
| 191 | void *hostdata = shost_priv(shost); |
| 192 | |
| 193 | return *((struct pqi_ctrl_info **)hostdata); |
| 194 | } |
| 195 | |
| 196 | static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device) |
| 197 | { |
| 198 | return !device->is_physical_device; |
| 199 | } |
| 200 | |
Kevin Barnett | bd10cf0 | 2017-05-03 18:54:12 -0500 | [diff] [blame] | 201 | static inline bool pqi_is_external_raid_addr(u8 *scsi3addr) |
| 202 | { |
| 203 | return scsi3addr[2] != 0; |
| 204 | } |
| 205 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 206 | static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info) |
| 207 | { |
| 208 | return !ctrl_info->controller_online; |
| 209 | } |
| 210 | |
| 211 | static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info) |
| 212 | { |
| 213 | if (ctrl_info->controller_online) |
| 214 | if (!sis_is_firmware_running(ctrl_info)) |
| 215 | pqi_take_ctrl_offline(ctrl_info); |
| 216 | } |
| 217 | |
| 218 | static inline bool pqi_is_hba_lunid(u8 *scsi3addr) |
| 219 | { |
| 220 | return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID); |
| 221 | } |
| 222 | |
Kevin Barnett | ff6abb7 | 2016-08-31 14:54:41 -0500 | [diff] [blame] | 223 | static inline enum pqi_ctrl_mode pqi_get_ctrl_mode( |
| 224 | struct pqi_ctrl_info *ctrl_info) |
| 225 | { |
| 226 | return sis_read_driver_scratch(ctrl_info); |
| 227 | } |
| 228 | |
| 229 | static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info, |
| 230 | enum pqi_ctrl_mode mode) |
| 231 | { |
| 232 | sis_write_driver_scratch(ctrl_info, mode); |
| 233 | } |
| 234 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 235 | static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info) |
| 236 | { |
| 237 | ctrl_info->block_requests = true; |
| 238 | scsi_block_requests(ctrl_info->scsi_host); |
| 239 | } |
| 240 | |
| 241 | static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info) |
| 242 | { |
| 243 | ctrl_info->block_requests = false; |
| 244 | wake_up_all(&ctrl_info->block_requests_wait); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 245 | pqi_retry_raid_bypass_requests(ctrl_info); |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 246 | scsi_unblock_requests(ctrl_info->scsi_host); |
| 247 | } |
| 248 | |
| 249 | static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info) |
| 250 | { |
| 251 | return ctrl_info->block_requests; |
| 252 | } |
| 253 | |
| 254 | static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info, |
| 255 | unsigned long timeout_msecs) |
| 256 | { |
| 257 | unsigned long remaining_msecs; |
| 258 | |
| 259 | if (!pqi_ctrl_blocked(ctrl_info)) |
| 260 | return timeout_msecs; |
| 261 | |
| 262 | atomic_inc(&ctrl_info->num_blocked_threads); |
| 263 | |
| 264 | if (timeout_msecs == NO_TIMEOUT) { |
| 265 | wait_event(ctrl_info->block_requests_wait, |
| 266 | !pqi_ctrl_blocked(ctrl_info)); |
| 267 | remaining_msecs = timeout_msecs; |
| 268 | } else { |
| 269 | unsigned long remaining_jiffies; |
| 270 | |
| 271 | remaining_jiffies = |
| 272 | wait_event_timeout(ctrl_info->block_requests_wait, |
| 273 | !pqi_ctrl_blocked(ctrl_info), |
| 274 | msecs_to_jiffies(timeout_msecs)); |
| 275 | remaining_msecs = jiffies_to_msecs(remaining_jiffies); |
| 276 | } |
| 277 | |
| 278 | atomic_dec(&ctrl_info->num_blocked_threads); |
| 279 | |
| 280 | return remaining_msecs; |
| 281 | } |
| 282 | |
| 283 | static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info) |
| 284 | { |
| 285 | atomic_inc(&ctrl_info->num_busy_threads); |
| 286 | } |
| 287 | |
| 288 | static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info) |
| 289 | { |
| 290 | atomic_dec(&ctrl_info->num_busy_threads); |
| 291 | } |
| 292 | |
| 293 | static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info) |
| 294 | { |
| 295 | while (atomic_read(&ctrl_info->num_busy_threads) > |
| 296 | atomic_read(&ctrl_info->num_blocked_threads)) |
| 297 | usleep_range(1000, 2000); |
| 298 | } |
| 299 | |
Kevin Barnett | 03b288cf | 2017-05-03 18:54:49 -0500 | [diff] [blame] | 300 | static inline bool pqi_device_offline(struct pqi_scsi_dev *device) |
| 301 | { |
| 302 | return device->device_offline; |
| 303 | } |
| 304 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 305 | static inline void pqi_device_reset_start(struct pqi_scsi_dev *device) |
| 306 | { |
| 307 | device->in_reset = true; |
| 308 | } |
| 309 | |
| 310 | static inline void pqi_device_reset_done(struct pqi_scsi_dev *device) |
| 311 | { |
| 312 | device->in_reset = false; |
| 313 | } |
| 314 | |
| 315 | static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device) |
| 316 | { |
| 317 | return device->in_reset; |
| 318 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 319 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 320 | static inline void pqi_schedule_rescan_worker_with_delay( |
| 321 | struct pqi_ctrl_info *ctrl_info, unsigned long delay) |
| 322 | { |
| 323 | if (pqi_ctrl_offline(ctrl_info)) |
| 324 | return; |
| 325 | |
| 326 | schedule_delayed_work(&ctrl_info->rescan_work, delay); |
| 327 | } |
| 328 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 329 | static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info) |
| 330 | { |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 331 | pqi_schedule_rescan_worker_with_delay(ctrl_info, 0); |
| 332 | } |
| 333 | |
| 334 | #define PQI_RESCAN_WORK_DELAY (10 * HZ) |
| 335 | |
| 336 | static inline void pqi_schedule_rescan_worker_delayed( |
| 337 | struct pqi_ctrl_info *ctrl_info) |
| 338 | { |
| 339 | pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 340 | } |
| 341 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 342 | static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info) |
| 343 | { |
| 344 | cancel_delayed_work_sync(&ctrl_info->rescan_work); |
| 345 | } |
| 346 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 347 | static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info) |
| 348 | { |
| 349 | if (!ctrl_info->heartbeat_counter) |
| 350 | return 0; |
| 351 | |
| 352 | return readl(ctrl_info->heartbeat_counter); |
| 353 | } |
| 354 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 355 | static int pqi_map_single(struct pci_dev *pci_dev, |
| 356 | struct pqi_sg_descriptor *sg_descriptor, void *buffer, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 357 | size_t buffer_length, enum dma_data_direction data_direction) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 358 | { |
| 359 | dma_addr_t bus_address; |
| 360 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 361 | if (!buffer || buffer_length == 0 || data_direction == DMA_NONE) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 362 | return 0; |
| 363 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 364 | bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 365 | data_direction); |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 366 | if (dma_mapping_error(&pci_dev->dev, bus_address)) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 367 | return -ENOMEM; |
| 368 | |
| 369 | put_unaligned_le64((u64)bus_address, &sg_descriptor->address); |
| 370 | put_unaligned_le32(buffer_length, &sg_descriptor->length); |
| 371 | put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static void pqi_pci_unmap(struct pci_dev *pci_dev, |
| 377 | struct pqi_sg_descriptor *descriptors, int num_descriptors, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 378 | enum dma_data_direction data_direction) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 379 | { |
| 380 | int i; |
| 381 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 382 | if (data_direction == DMA_NONE) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 383 | return; |
| 384 | |
| 385 | for (i = 0; i < num_descriptors; i++) |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 386 | dma_unmap_single(&pci_dev->dev, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 387 | (dma_addr_t)get_unaligned_le64(&descriptors[i].address), |
| 388 | get_unaligned_le32(&descriptors[i].length), |
| 389 | data_direction); |
| 390 | } |
| 391 | |
| 392 | static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info, |
| 393 | struct pqi_raid_path_request *request, u8 cmd, |
| 394 | u8 *scsi3addr, void *buffer, size_t buffer_length, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 395 | u16 vpd_page, enum dma_data_direction *dir) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 396 | { |
| 397 | u8 *cdb; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 398 | size_t cdb_length = buffer_length; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 399 | |
| 400 | memset(request, 0, sizeof(*request)); |
| 401 | |
| 402 | request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO; |
| 403 | put_unaligned_le16(offsetof(struct pqi_raid_path_request, |
| 404 | sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH, |
| 405 | &request->header.iu_length); |
| 406 | put_unaligned_le32(buffer_length, &request->buffer_length); |
| 407 | memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number)); |
| 408 | request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE; |
| 409 | request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0; |
| 410 | |
| 411 | cdb = request->cdb; |
| 412 | |
| 413 | switch (cmd) { |
| 414 | case INQUIRY: |
| 415 | request->data_direction = SOP_READ_FLAG; |
| 416 | cdb[0] = INQUIRY; |
| 417 | if (vpd_page & VPD_PAGE) { |
| 418 | cdb[1] = 0x1; |
| 419 | cdb[2] = (u8)vpd_page; |
| 420 | } |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 421 | cdb[4] = (u8)cdb_length; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 422 | break; |
| 423 | case CISS_REPORT_LOG: |
| 424 | case CISS_REPORT_PHYS: |
| 425 | request->data_direction = SOP_READ_FLAG; |
| 426 | cdb[0] = cmd; |
| 427 | if (cmd == CISS_REPORT_PHYS) |
| 428 | cdb[1] = CISS_REPORT_PHYS_EXTENDED; |
| 429 | else |
| 430 | cdb[1] = CISS_REPORT_LOG_EXTENDED; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 431 | put_unaligned_be32(cdb_length, &cdb[6]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 432 | break; |
| 433 | case CISS_GET_RAID_MAP: |
| 434 | request->data_direction = SOP_READ_FLAG; |
| 435 | cdb[0] = CISS_READ; |
| 436 | cdb[1] = CISS_GET_RAID_MAP; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 437 | put_unaligned_be32(cdb_length, &cdb[6]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 438 | break; |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 439 | case SA_FLUSH_CACHE: |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 440 | request->data_direction = SOP_WRITE_FLAG; |
| 441 | cdb[0] = BMIC_WRITE; |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 442 | cdb[6] = BMIC_FLUSH_CACHE; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 443 | put_unaligned_be16(cdb_length, &cdb[7]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 444 | break; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 445 | case BMIC_SENSE_DIAG_OPTIONS: |
| 446 | cdb_length = 0; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 447 | case BMIC_IDENTIFY_CONTROLLER: |
| 448 | case BMIC_IDENTIFY_PHYSICAL_DEVICE: |
| 449 | request->data_direction = SOP_READ_FLAG; |
| 450 | cdb[0] = BMIC_READ; |
| 451 | cdb[6] = cmd; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 452 | put_unaligned_be16(cdb_length, &cdb[7]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 453 | break; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 454 | case BMIC_SET_DIAG_OPTIONS: |
| 455 | cdb_length = 0; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 456 | case BMIC_WRITE_HOST_WELLNESS: |
| 457 | request->data_direction = SOP_WRITE_FLAG; |
| 458 | cdb[0] = BMIC_WRITE; |
| 459 | cdb[6] = cmd; |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 460 | put_unaligned_be16(cdb_length, &cdb[7]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 461 | break; |
| 462 | default: |
| 463 | dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n", |
| 464 | cmd); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | |
| 468 | switch (request->data_direction) { |
| 469 | case SOP_READ_FLAG: |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 470 | *dir = DMA_FROM_DEVICE; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 471 | break; |
| 472 | case SOP_WRITE_FLAG: |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 473 | *dir = DMA_TO_DEVICE; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 474 | break; |
| 475 | case SOP_NO_DIRECTION_FLAG: |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 476 | *dir = DMA_NONE; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 477 | break; |
| 478 | default: |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 479 | *dir = DMA_BIDIRECTIONAL; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 483 | return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0], |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 484 | buffer, buffer_length, *dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 487 | static inline void pqi_reinit_io_request(struct pqi_io_request *io_request) |
| 488 | { |
| 489 | io_request->scmd = NULL; |
| 490 | io_request->status = 0; |
| 491 | io_request->error_info = NULL; |
| 492 | io_request->raid_bypass = false; |
| 493 | } |
| 494 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 495 | static struct pqi_io_request *pqi_alloc_io_request( |
| 496 | struct pqi_ctrl_info *ctrl_info) |
| 497 | { |
| 498 | struct pqi_io_request *io_request; |
| 499 | u16 i = ctrl_info->next_io_request_slot; /* benignly racy */ |
| 500 | |
| 501 | while (1) { |
| 502 | io_request = &ctrl_info->io_request_pool[i]; |
| 503 | if (atomic_inc_return(&io_request->refcount) == 1) |
| 504 | break; |
| 505 | atomic_dec(&io_request->refcount); |
| 506 | i = (i + 1) % ctrl_info->max_io_slots; |
| 507 | } |
| 508 | |
| 509 | /* benignly racy */ |
| 510 | ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots; |
| 511 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 512 | pqi_reinit_io_request(io_request); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 513 | |
| 514 | return io_request; |
| 515 | } |
| 516 | |
| 517 | static void pqi_free_io_request(struct pqi_io_request *io_request) |
| 518 | { |
| 519 | atomic_dec(&io_request->refcount); |
| 520 | } |
| 521 | |
| 522 | static int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info, |
| 523 | struct bmic_identify_controller *buffer) |
| 524 | { |
| 525 | int rc; |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 526 | enum dma_data_direction dir; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 527 | struct pqi_raid_path_request request; |
| 528 | |
| 529 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
| 530 | BMIC_IDENTIFY_CONTROLLER, RAID_CTLR_LUNID, buffer, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 531 | sizeof(*buffer), 0, &dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 532 | if (rc) |
| 533 | return rc; |
| 534 | |
| 535 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, |
| 536 | NULL, NO_TIMEOUT); |
| 537 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 538 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 539 | return rc; |
| 540 | } |
| 541 | |
| 542 | static int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info, |
| 543 | u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length) |
| 544 | { |
| 545 | int rc; |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 546 | enum dma_data_direction dir; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 547 | struct pqi_raid_path_request request; |
| 548 | |
| 549 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
| 550 | INQUIRY, scsi3addr, buffer, buffer_length, vpd_page, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 551 | &dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 552 | if (rc) |
| 553 | return rc; |
| 554 | |
| 555 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, |
| 556 | NULL, NO_TIMEOUT); |
| 557 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 558 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 559 | return rc; |
| 560 | } |
| 561 | |
| 562 | static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info, |
| 563 | struct pqi_scsi_dev *device, |
| 564 | struct bmic_identify_physical_device *buffer, |
| 565 | size_t buffer_length) |
| 566 | { |
| 567 | int rc; |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 568 | enum dma_data_direction dir; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 569 | u16 bmic_device_index; |
| 570 | struct pqi_raid_path_request request; |
| 571 | |
| 572 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
| 573 | BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 574 | buffer_length, 0, &dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 575 | if (rc) |
| 576 | return rc; |
| 577 | |
| 578 | bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr); |
| 579 | request.cdb[2] = (u8)bmic_device_index; |
| 580 | request.cdb[9] = (u8)(bmic_device_index >> 8); |
| 581 | |
| 582 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
| 583 | 0, NULL, NO_TIMEOUT); |
| 584 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 585 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 586 | return rc; |
| 587 | } |
| 588 | |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 589 | static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info, |
| 590 | enum bmic_flush_cache_shutdown_event shutdown_event) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 591 | { |
| 592 | int rc; |
| 593 | struct pqi_raid_path_request request; |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 594 | struct bmic_flush_cache *flush_cache; |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 595 | enum dma_data_direction dir; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * Don't bother trying to flush the cache if the controller is |
| 599 | * locked up. |
| 600 | */ |
| 601 | if (pqi_ctrl_offline(ctrl_info)) |
| 602 | return -ENXIO; |
| 603 | |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 604 | flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL); |
| 605 | if (!flush_cache) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 606 | return -ENOMEM; |
| 607 | |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 608 | flush_cache->shutdown_event = shutdown_event; |
| 609 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 610 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 611 | SA_FLUSH_CACHE, RAID_CTLR_LUNID, flush_cache, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 612 | sizeof(*flush_cache), 0, &dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 613 | if (rc) |
| 614 | goto out; |
| 615 | |
| 616 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
Kevin Barnett | d48f8fa | 2016-08-31 14:55:17 -0500 | [diff] [blame] | 617 | 0, NULL, NO_TIMEOUT); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 618 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 619 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 620 | out: |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 621 | kfree(flush_cache); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 622 | |
| 623 | return rc; |
| 624 | } |
| 625 | |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 626 | |
| 627 | #define PQI_FETCH_PTRAID_DATA (1UL<<31) |
| 628 | |
| 629 | static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info) |
| 630 | { |
| 631 | int rc; |
| 632 | struct pqi_raid_path_request request; |
| 633 | struct bmic_diag_options *diag; |
| 634 | enum dma_data_direction pci_direction; |
| 635 | |
| 636 | diag = kzalloc(sizeof(*diag), GFP_KERNEL); |
| 637 | if (!diag) |
| 638 | return -ENOMEM; |
| 639 | |
| 640 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
| 641 | BMIC_SENSE_DIAG_OPTIONS, RAID_CTLR_LUNID, diag, |
| 642 | sizeof(*diag), 0, &pci_direction); |
| 643 | if (rc) |
| 644 | goto out; |
| 645 | |
| 646 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
| 647 | 0, NULL, NO_TIMEOUT); |
| 648 | |
| 649 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, |
| 650 | pci_direction); |
| 651 | |
| 652 | if (rc) |
| 653 | goto out; |
| 654 | |
| 655 | diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA); |
| 656 | |
| 657 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
| 658 | BMIC_SET_DIAG_OPTIONS, RAID_CTLR_LUNID, diag, |
| 659 | sizeof(*diag), 0, &pci_direction); |
| 660 | if (rc) |
| 661 | goto out; |
| 662 | |
| 663 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
| 664 | 0, NULL, NO_TIMEOUT); |
| 665 | |
| 666 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, |
| 667 | pci_direction); |
| 668 | out: |
| 669 | kfree(diag); |
| 670 | |
| 671 | return rc; |
| 672 | } |
| 673 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 674 | static int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info, |
| 675 | void *buffer, size_t buffer_length) |
| 676 | { |
| 677 | int rc; |
| 678 | struct pqi_raid_path_request request; |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 679 | enum dma_data_direction dir; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 680 | |
| 681 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
| 682 | BMIC_WRITE_HOST_WELLNESS, RAID_CTLR_LUNID, buffer, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 683 | buffer_length, 0, &dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 684 | if (rc) |
| 685 | return rc; |
| 686 | |
| 687 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
| 688 | 0, NULL, NO_TIMEOUT); |
| 689 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 690 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 691 | return rc; |
| 692 | } |
| 693 | |
| 694 | #pragma pack(1) |
| 695 | |
| 696 | struct bmic_host_wellness_driver_version { |
| 697 | u8 start_tag[4]; |
| 698 | u8 driver_version_tag[2]; |
| 699 | __le16 driver_version_length; |
| 700 | char driver_version[32]; |
Mahesh Rajashekhara | b2346b5 | 2018-12-07 16:28:29 -0600 | [diff] [blame] | 701 | u8 dont_write_tag[2]; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 702 | u8 end_tag[2]; |
| 703 | }; |
| 704 | |
| 705 | #pragma pack() |
| 706 | |
| 707 | static int pqi_write_driver_version_to_host_wellness( |
| 708 | struct pqi_ctrl_info *ctrl_info) |
| 709 | { |
| 710 | int rc; |
| 711 | struct bmic_host_wellness_driver_version *buffer; |
| 712 | size_t buffer_length; |
| 713 | |
| 714 | buffer_length = sizeof(*buffer); |
| 715 | |
| 716 | buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 717 | if (!buffer) |
| 718 | return -ENOMEM; |
| 719 | |
| 720 | buffer->start_tag[0] = '<'; |
| 721 | buffer->start_tag[1] = 'H'; |
| 722 | buffer->start_tag[2] = 'W'; |
| 723 | buffer->start_tag[3] = '>'; |
| 724 | buffer->driver_version_tag[0] = 'D'; |
| 725 | buffer->driver_version_tag[1] = 'V'; |
| 726 | put_unaligned_le16(sizeof(buffer->driver_version), |
| 727 | &buffer->driver_version_length); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 728 | strncpy(buffer->driver_version, "Linux " DRIVER_VERSION, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 729 | sizeof(buffer->driver_version) - 1); |
| 730 | buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0'; |
Mahesh Rajashekhara | b2346b5 | 2018-12-07 16:28:29 -0600 | [diff] [blame] | 731 | buffer->dont_write_tag[0] = 'D'; |
| 732 | buffer->dont_write_tag[1] = 'W'; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 733 | buffer->end_tag[0] = 'Z'; |
| 734 | buffer->end_tag[1] = 'Z'; |
| 735 | |
| 736 | rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length); |
| 737 | |
| 738 | kfree(buffer); |
| 739 | |
| 740 | return rc; |
| 741 | } |
| 742 | |
| 743 | #pragma pack(1) |
| 744 | |
| 745 | struct bmic_host_wellness_time { |
| 746 | u8 start_tag[4]; |
| 747 | u8 time_tag[2]; |
| 748 | __le16 time_length; |
| 749 | u8 time[8]; |
| 750 | u8 dont_write_tag[2]; |
| 751 | u8 end_tag[2]; |
| 752 | }; |
| 753 | |
| 754 | #pragma pack() |
| 755 | |
| 756 | static int pqi_write_current_time_to_host_wellness( |
| 757 | struct pqi_ctrl_info *ctrl_info) |
| 758 | { |
| 759 | int rc; |
| 760 | struct bmic_host_wellness_time *buffer; |
| 761 | size_t buffer_length; |
| 762 | time64_t local_time; |
| 763 | unsigned int year; |
Arnd Bergmann | ed10858 | 2017-02-17 16:03:52 +0100 | [diff] [blame] | 764 | struct tm tm; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 765 | |
| 766 | buffer_length = sizeof(*buffer); |
| 767 | |
| 768 | buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 769 | if (!buffer) |
| 770 | return -ENOMEM; |
| 771 | |
| 772 | buffer->start_tag[0] = '<'; |
| 773 | buffer->start_tag[1] = 'H'; |
| 774 | buffer->start_tag[2] = 'W'; |
| 775 | buffer->start_tag[3] = '>'; |
| 776 | buffer->time_tag[0] = 'T'; |
| 777 | buffer->time_tag[1] = 'D'; |
| 778 | put_unaligned_le16(sizeof(buffer->time), |
| 779 | &buffer->time_length); |
| 780 | |
Arnd Bergmann | ed10858 | 2017-02-17 16:03:52 +0100 | [diff] [blame] | 781 | local_time = ktime_get_real_seconds(); |
| 782 | time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 783 | year = tm.tm_year + 1900; |
| 784 | |
| 785 | buffer->time[0] = bin2bcd(tm.tm_hour); |
| 786 | buffer->time[1] = bin2bcd(tm.tm_min); |
| 787 | buffer->time[2] = bin2bcd(tm.tm_sec); |
| 788 | buffer->time[3] = 0; |
| 789 | buffer->time[4] = bin2bcd(tm.tm_mon + 1); |
| 790 | buffer->time[5] = bin2bcd(tm.tm_mday); |
| 791 | buffer->time[6] = bin2bcd(year / 100); |
| 792 | buffer->time[7] = bin2bcd(year % 100); |
| 793 | |
| 794 | buffer->dont_write_tag[0] = 'D'; |
| 795 | buffer->dont_write_tag[1] = 'W'; |
| 796 | buffer->end_tag[0] = 'Z'; |
| 797 | buffer->end_tag[1] = 'Z'; |
| 798 | |
| 799 | rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length); |
| 800 | |
| 801 | kfree(buffer); |
| 802 | |
| 803 | return rc; |
| 804 | } |
| 805 | |
| 806 | #define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ) |
| 807 | |
| 808 | static void pqi_update_time_worker(struct work_struct *work) |
| 809 | { |
| 810 | int rc; |
| 811 | struct pqi_ctrl_info *ctrl_info; |
| 812 | |
| 813 | ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info, |
| 814 | update_time_work); |
| 815 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 816 | if (pqi_ctrl_offline(ctrl_info)) |
| 817 | return; |
| 818 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 819 | rc = pqi_write_current_time_to_host_wellness(ctrl_info); |
| 820 | if (rc) |
| 821 | dev_warn(&ctrl_info->pci_dev->dev, |
| 822 | "error updating time on controller\n"); |
| 823 | |
| 824 | schedule_delayed_work(&ctrl_info->update_time_work, |
| 825 | PQI_UPDATE_TIME_WORK_INTERVAL); |
| 826 | } |
| 827 | |
| 828 | static inline void pqi_schedule_update_time_worker( |
Kevin Barnett | 4fbebf1 | 2016-08-31 14:55:05 -0500 | [diff] [blame] | 829 | struct pqi_ctrl_info *ctrl_info) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 830 | { |
Kevin Barnett | 4fbebf1 | 2016-08-31 14:55:05 -0500 | [diff] [blame] | 831 | schedule_delayed_work(&ctrl_info->update_time_work, 0); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static inline void pqi_cancel_update_time_worker( |
| 835 | struct pqi_ctrl_info *ctrl_info) |
| 836 | { |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 837 | cancel_delayed_work_sync(&ctrl_info->update_time_work); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, |
| 841 | void *buffer, size_t buffer_length) |
| 842 | { |
| 843 | int rc; |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 844 | enum dma_data_direction dir; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 845 | struct pqi_raid_path_request request; |
| 846 | |
| 847 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 848 | cmd, RAID_CTLR_LUNID, buffer, buffer_length, 0, &dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 849 | if (rc) |
| 850 | return rc; |
| 851 | |
| 852 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, |
| 853 | NULL, NO_TIMEOUT); |
| 854 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 855 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 856 | return rc; |
| 857 | } |
| 858 | |
| 859 | static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, |
| 860 | void **buffer) |
| 861 | { |
| 862 | int rc; |
| 863 | size_t lun_list_length; |
| 864 | size_t lun_data_length; |
| 865 | size_t new_lun_list_length; |
| 866 | void *lun_data = NULL; |
| 867 | struct report_lun_header *report_lun_header; |
| 868 | |
| 869 | report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL); |
| 870 | if (!report_lun_header) { |
| 871 | rc = -ENOMEM; |
| 872 | goto out; |
| 873 | } |
| 874 | |
| 875 | rc = pqi_report_luns(ctrl_info, cmd, report_lun_header, |
| 876 | sizeof(*report_lun_header)); |
| 877 | if (rc) |
| 878 | goto out; |
| 879 | |
| 880 | lun_list_length = get_unaligned_be32(&report_lun_header->list_length); |
| 881 | |
| 882 | again: |
| 883 | lun_data_length = sizeof(struct report_lun_header) + lun_list_length; |
| 884 | |
| 885 | lun_data = kmalloc(lun_data_length, GFP_KERNEL); |
| 886 | if (!lun_data) { |
| 887 | rc = -ENOMEM; |
| 888 | goto out; |
| 889 | } |
| 890 | |
| 891 | if (lun_list_length == 0) { |
| 892 | memcpy(lun_data, report_lun_header, sizeof(*report_lun_header)); |
| 893 | goto out; |
| 894 | } |
| 895 | |
| 896 | rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length); |
| 897 | if (rc) |
| 898 | goto out; |
| 899 | |
| 900 | new_lun_list_length = get_unaligned_be32( |
| 901 | &((struct report_lun_header *)lun_data)->list_length); |
| 902 | |
| 903 | if (new_lun_list_length > lun_list_length) { |
| 904 | lun_list_length = new_lun_list_length; |
| 905 | kfree(lun_data); |
| 906 | goto again; |
| 907 | } |
| 908 | |
| 909 | out: |
| 910 | kfree(report_lun_header); |
| 911 | |
| 912 | if (rc) { |
| 913 | kfree(lun_data); |
| 914 | lun_data = NULL; |
| 915 | } |
| 916 | |
| 917 | *buffer = lun_data; |
| 918 | |
| 919 | return rc; |
| 920 | } |
| 921 | |
| 922 | static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, |
| 923 | void **buffer) |
| 924 | { |
| 925 | return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS, |
| 926 | buffer); |
| 927 | } |
| 928 | |
| 929 | static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info, |
| 930 | void **buffer) |
| 931 | { |
| 932 | return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer); |
| 933 | } |
| 934 | |
| 935 | static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info, |
| 936 | struct report_phys_lun_extended **physdev_list, |
| 937 | struct report_log_lun_extended **logdev_list) |
| 938 | { |
| 939 | int rc; |
| 940 | size_t logdev_list_length; |
| 941 | size_t logdev_data_length; |
| 942 | struct report_log_lun_extended *internal_logdev_list; |
| 943 | struct report_log_lun_extended *logdev_data; |
| 944 | struct report_lun_header report_lun_header; |
| 945 | |
| 946 | rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list); |
| 947 | if (rc) |
| 948 | dev_err(&ctrl_info->pci_dev->dev, |
| 949 | "report physical LUNs failed\n"); |
| 950 | |
| 951 | rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list); |
| 952 | if (rc) |
| 953 | dev_err(&ctrl_info->pci_dev->dev, |
| 954 | "report logical LUNs failed\n"); |
| 955 | |
| 956 | /* |
| 957 | * Tack the controller itself onto the end of the logical device list. |
| 958 | */ |
| 959 | |
| 960 | logdev_data = *logdev_list; |
| 961 | |
| 962 | if (logdev_data) { |
| 963 | logdev_list_length = |
| 964 | get_unaligned_be32(&logdev_data->header.list_length); |
| 965 | } else { |
| 966 | memset(&report_lun_header, 0, sizeof(report_lun_header)); |
| 967 | logdev_data = |
| 968 | (struct report_log_lun_extended *)&report_lun_header; |
| 969 | logdev_list_length = 0; |
| 970 | } |
| 971 | |
| 972 | logdev_data_length = sizeof(struct report_lun_header) + |
| 973 | logdev_list_length; |
| 974 | |
| 975 | internal_logdev_list = kmalloc(logdev_data_length + |
| 976 | sizeof(struct report_log_lun_extended), GFP_KERNEL); |
| 977 | if (!internal_logdev_list) { |
| 978 | kfree(*logdev_list); |
| 979 | *logdev_list = NULL; |
| 980 | return -ENOMEM; |
| 981 | } |
| 982 | |
| 983 | memcpy(internal_logdev_list, logdev_data, logdev_data_length); |
| 984 | memset((u8 *)internal_logdev_list + logdev_data_length, 0, |
| 985 | sizeof(struct report_log_lun_extended_entry)); |
| 986 | put_unaligned_be32(logdev_list_length + |
| 987 | sizeof(struct report_log_lun_extended_entry), |
| 988 | &internal_logdev_list->header.list_length); |
| 989 | |
| 990 | kfree(*logdev_list); |
| 991 | *logdev_list = internal_logdev_list; |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device, |
| 997 | int bus, int target, int lun) |
| 998 | { |
| 999 | device->bus = bus; |
| 1000 | device->target = target; |
| 1001 | device->lun = lun; |
| 1002 | } |
| 1003 | |
| 1004 | static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device) |
| 1005 | { |
| 1006 | u8 *scsi3addr; |
| 1007 | u32 lunid; |
Kevin Barnett | bd10cf0 | 2017-05-03 18:54:12 -0500 | [diff] [blame] | 1008 | int bus; |
| 1009 | int target; |
| 1010 | int lun; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1011 | |
| 1012 | scsi3addr = device->scsi3addr; |
| 1013 | lunid = get_unaligned_le32(scsi3addr); |
| 1014 | |
| 1015 | if (pqi_is_hba_lunid(scsi3addr)) { |
| 1016 | /* The specified device is the controller. */ |
| 1017 | pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff); |
| 1018 | device->target_lun_valid = true; |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | if (pqi_is_logical_device(device)) { |
Kevin Barnett | bd10cf0 | 2017-05-03 18:54:12 -0500 | [diff] [blame] | 1023 | if (device->is_external_raid_device) { |
| 1024 | bus = PQI_EXTERNAL_RAID_VOLUME_BUS; |
| 1025 | target = (lunid >> 16) & 0x3fff; |
| 1026 | lun = lunid & 0xff; |
| 1027 | } else { |
| 1028 | bus = PQI_RAID_VOLUME_BUS; |
| 1029 | target = 0; |
| 1030 | lun = lunid & 0x3fff; |
| 1031 | } |
| 1032 | pqi_set_bus_target_lun(device, bus, target, lun); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1033 | device->target_lun_valid = true; |
| 1034 | return; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Defer target and LUN assignment for non-controller physical devices |
| 1039 | * because the SAS transport layer will make these assignments later. |
| 1040 | */ |
| 1041 | pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0); |
| 1042 | } |
| 1043 | |
| 1044 | static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info, |
| 1045 | struct pqi_scsi_dev *device) |
| 1046 | { |
| 1047 | int rc; |
| 1048 | u8 raid_level; |
| 1049 | u8 *buffer; |
| 1050 | |
| 1051 | raid_level = SA_RAID_UNKNOWN; |
| 1052 | |
| 1053 | buffer = kmalloc(64, GFP_KERNEL); |
| 1054 | if (buffer) { |
| 1055 | rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, |
| 1056 | VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64); |
| 1057 | if (rc == 0) { |
| 1058 | raid_level = buffer[8]; |
| 1059 | if (raid_level > SA_RAID_MAX) |
| 1060 | raid_level = SA_RAID_UNKNOWN; |
| 1061 | } |
| 1062 | kfree(buffer); |
| 1063 | } |
| 1064 | |
| 1065 | device->raid_level = raid_level; |
| 1066 | } |
| 1067 | |
| 1068 | static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info, |
| 1069 | struct pqi_scsi_dev *device, struct raid_map *raid_map) |
| 1070 | { |
| 1071 | char *err_msg; |
| 1072 | u32 raid_map_size; |
| 1073 | u32 r5or6_blocks_per_row; |
| 1074 | unsigned int num_phys_disks; |
| 1075 | unsigned int num_raid_map_entries; |
| 1076 | |
| 1077 | raid_map_size = get_unaligned_le32(&raid_map->structure_size); |
| 1078 | |
| 1079 | if (raid_map_size < offsetof(struct raid_map, disk_data)) { |
| 1080 | err_msg = "RAID map too small"; |
| 1081 | goto bad_raid_map; |
| 1082 | } |
| 1083 | |
| 1084 | if (raid_map_size > sizeof(*raid_map)) { |
| 1085 | err_msg = "RAID map too large"; |
| 1086 | goto bad_raid_map; |
| 1087 | } |
| 1088 | |
| 1089 | num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) * |
| 1090 | (get_unaligned_le16(&raid_map->data_disks_per_row) + |
| 1091 | get_unaligned_le16(&raid_map->metadata_disks_per_row)); |
| 1092 | num_raid_map_entries = num_phys_disks * |
| 1093 | get_unaligned_le16(&raid_map->row_cnt); |
| 1094 | |
| 1095 | if (num_raid_map_entries > RAID_MAP_MAX_ENTRIES) { |
| 1096 | err_msg = "invalid number of map entries in RAID map"; |
| 1097 | goto bad_raid_map; |
| 1098 | } |
| 1099 | |
| 1100 | if (device->raid_level == SA_RAID_1) { |
| 1101 | if (get_unaligned_le16(&raid_map->layout_map_count) != 2) { |
| 1102 | err_msg = "invalid RAID-1 map"; |
| 1103 | goto bad_raid_map; |
| 1104 | } |
| 1105 | } else if (device->raid_level == SA_RAID_ADM) { |
| 1106 | if (get_unaligned_le16(&raid_map->layout_map_count) != 3) { |
| 1107 | err_msg = "invalid RAID-1(ADM) map"; |
| 1108 | goto bad_raid_map; |
| 1109 | } |
| 1110 | } else if ((device->raid_level == SA_RAID_5 || |
| 1111 | device->raid_level == SA_RAID_6) && |
| 1112 | get_unaligned_le16(&raid_map->layout_map_count) > 1) { |
| 1113 | /* RAID 50/60 */ |
| 1114 | r5or6_blocks_per_row = |
| 1115 | get_unaligned_le16(&raid_map->strip_size) * |
| 1116 | get_unaligned_le16(&raid_map->data_disks_per_row); |
| 1117 | if (r5or6_blocks_per_row == 0) { |
| 1118 | err_msg = "invalid RAID-5 or RAID-6 map"; |
| 1119 | goto bad_raid_map; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | return 0; |
| 1124 | |
| 1125 | bad_raid_map: |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 1126 | dev_warn(&ctrl_info->pci_dev->dev, |
Kevin Barnett | 38a7338 | 2017-09-27 16:30:05 -0500 | [diff] [blame] | 1127 | "logical device %08x%08x %s\n", |
| 1128 | *((u32 *)&device->scsi3addr), |
| 1129 | *((u32 *)&device->scsi3addr[4]), err_msg); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1130 | |
| 1131 | return -EINVAL; |
| 1132 | } |
| 1133 | |
| 1134 | static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info, |
| 1135 | struct pqi_scsi_dev *device) |
| 1136 | { |
| 1137 | int rc; |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 1138 | enum dma_data_direction dir; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1139 | struct pqi_raid_path_request request; |
| 1140 | struct raid_map *raid_map; |
| 1141 | |
| 1142 | raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL); |
| 1143 | if (!raid_map) |
| 1144 | return -ENOMEM; |
| 1145 | |
| 1146 | rc = pqi_build_raid_path_request(ctrl_info, &request, |
| 1147 | CISS_GET_RAID_MAP, device->scsi3addr, raid_map, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 1148 | sizeof(*raid_map), 0, &dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1149 | if (rc) |
| 1150 | goto error; |
| 1151 | |
| 1152 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, |
| 1153 | NULL, NO_TIMEOUT); |
| 1154 | |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 1155 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1156 | |
| 1157 | if (rc) |
| 1158 | goto error; |
| 1159 | |
| 1160 | rc = pqi_validate_raid_map(ctrl_info, device, raid_map); |
| 1161 | if (rc) |
| 1162 | goto error; |
| 1163 | |
| 1164 | device->raid_map = raid_map; |
| 1165 | |
| 1166 | return 0; |
| 1167 | |
| 1168 | error: |
| 1169 | kfree(raid_map); |
| 1170 | |
| 1171 | return rc; |
| 1172 | } |
| 1173 | |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1174 | static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1175 | struct pqi_scsi_dev *device) |
| 1176 | { |
| 1177 | int rc; |
| 1178 | u8 *buffer; |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1179 | u8 bypass_status; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1180 | |
| 1181 | buffer = kmalloc(64, GFP_KERNEL); |
| 1182 | if (!buffer) |
| 1183 | return; |
| 1184 | |
| 1185 | rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1186 | VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1187 | if (rc) |
| 1188 | goto out; |
| 1189 | |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1190 | #define RAID_BYPASS_STATUS 4 |
| 1191 | #define RAID_BYPASS_CONFIGURED 0x1 |
| 1192 | #define RAID_BYPASS_ENABLED 0x2 |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1193 | |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1194 | bypass_status = buffer[RAID_BYPASS_STATUS]; |
| 1195 | device->raid_bypass_configured = |
| 1196 | (bypass_status & RAID_BYPASS_CONFIGURED) != 0; |
| 1197 | if (device->raid_bypass_configured && |
| 1198 | (bypass_status & RAID_BYPASS_ENABLED) && |
| 1199 | pqi_get_raid_map(ctrl_info, device) == 0) |
| 1200 | device->raid_bypass_enabled = true; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1201 | |
| 1202 | out: |
| 1203 | kfree(buffer); |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Use vendor-specific VPD to determine online/offline status of a volume. |
| 1208 | */ |
| 1209 | |
| 1210 | static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info, |
| 1211 | struct pqi_scsi_dev *device) |
| 1212 | { |
| 1213 | int rc; |
| 1214 | size_t page_length; |
| 1215 | u8 volume_status = CISS_LV_STATUS_UNAVAILABLE; |
| 1216 | bool volume_offline = true; |
| 1217 | u32 volume_flags; |
| 1218 | struct ciss_vpd_logical_volume_status *vpd; |
| 1219 | |
| 1220 | vpd = kmalloc(sizeof(*vpd), GFP_KERNEL); |
| 1221 | if (!vpd) |
| 1222 | goto no_buffer; |
| 1223 | |
| 1224 | rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, |
| 1225 | VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd)); |
| 1226 | if (rc) |
| 1227 | goto out; |
| 1228 | |
| 1229 | page_length = offsetof(struct ciss_vpd_logical_volume_status, |
| 1230 | volume_status) + vpd->page_length; |
| 1231 | if (page_length < sizeof(*vpd)) |
| 1232 | goto out; |
| 1233 | |
| 1234 | volume_status = vpd->volume_status; |
| 1235 | volume_flags = get_unaligned_be32(&vpd->flags); |
| 1236 | volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0; |
| 1237 | |
| 1238 | out: |
| 1239 | kfree(vpd); |
| 1240 | no_buffer: |
| 1241 | device->volume_status = volume_status; |
| 1242 | device->volume_offline = volume_offline; |
| 1243 | } |
| 1244 | |
Kevin Barnett | 26b390a | 2018-06-18 13:22:48 -0500 | [diff] [blame] | 1245 | #define PQI_INQUIRY_PAGE0_RETRIES 3 |
| 1246 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1247 | static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info, |
| 1248 | struct pqi_scsi_dev *device) |
| 1249 | { |
| 1250 | int rc; |
| 1251 | u8 *buffer; |
Kevin Barnett | 26b390a | 2018-06-18 13:22:48 -0500 | [diff] [blame] | 1252 | unsigned int retries; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1253 | |
| 1254 | buffer = kmalloc(64, GFP_KERNEL); |
| 1255 | if (!buffer) |
| 1256 | return -ENOMEM; |
| 1257 | |
| 1258 | /* Send an inquiry to the device to see what it is. */ |
Kevin Barnett | 26b390a | 2018-06-18 13:22:48 -0500 | [diff] [blame] | 1259 | for (retries = 0;;) { |
| 1260 | rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, |
| 1261 | buffer, 64); |
| 1262 | if (rc == 0) |
| 1263 | break; |
| 1264 | if (pqi_is_logical_device(device) || |
| 1265 | rc != PQI_CMD_STATUS_ABORTED || |
| 1266 | ++retries > PQI_INQUIRY_PAGE0_RETRIES) |
| 1267 | goto out; |
| 1268 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1269 | |
| 1270 | scsi_sanitize_inquiry_string(&buffer[8], 8); |
| 1271 | scsi_sanitize_inquiry_string(&buffer[16], 16); |
| 1272 | |
| 1273 | device->devtype = buffer[0] & 0x1f; |
Kevin Barnett | cbe0c7b | 2017-05-03 18:53:48 -0500 | [diff] [blame] | 1274 | memcpy(device->vendor, &buffer[8], sizeof(device->vendor)); |
| 1275 | memcpy(device->model, &buffer[16], sizeof(device->model)); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1276 | |
| 1277 | if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) { |
Kevin Barnett | bd10cf0 | 2017-05-03 18:54:12 -0500 | [diff] [blame] | 1278 | if (device->is_external_raid_device) { |
| 1279 | device->raid_level = SA_RAID_UNKNOWN; |
| 1280 | device->volume_status = CISS_LV_OK; |
| 1281 | device->volume_offline = false; |
| 1282 | } else { |
| 1283 | pqi_get_raid_level(ctrl_info, device); |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1284 | pqi_get_raid_bypass_status(ctrl_info, device); |
Kevin Barnett | bd10cf0 | 2017-05-03 18:54:12 -0500 | [diff] [blame] | 1285 | pqi_get_volume_status(ctrl_info, device); |
| 1286 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | out: |
| 1290 | kfree(buffer); |
| 1291 | |
| 1292 | return rc; |
| 1293 | } |
| 1294 | |
| 1295 | static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info, |
| 1296 | struct pqi_scsi_dev *device, |
| 1297 | struct bmic_identify_physical_device *id_phys) |
| 1298 | { |
| 1299 | int rc; |
| 1300 | |
| 1301 | memset(id_phys, 0, sizeof(*id_phys)); |
| 1302 | |
| 1303 | rc = pqi_identify_physical_device(ctrl_info, device, |
| 1304 | id_phys, sizeof(*id_phys)); |
| 1305 | if (rc) { |
| 1306 | device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH; |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | device->queue_depth = |
| 1311 | get_unaligned_le16(&id_phys->current_queue_depth_limit); |
| 1312 | device->device_type = id_phys->device_type; |
| 1313 | device->active_path_index = id_phys->active_path_number; |
| 1314 | device->path_map = id_phys->redundant_path_present_map; |
| 1315 | memcpy(&device->box, |
| 1316 | &id_phys->alternate_paths_phys_box_on_port, |
| 1317 | sizeof(device->box)); |
| 1318 | memcpy(&device->phys_connector, |
| 1319 | &id_phys->alternate_paths_phys_connector, |
| 1320 | sizeof(device->phys_connector)); |
| 1321 | device->bay = id_phys->phys_bay_in_box; |
| 1322 | } |
| 1323 | |
| 1324 | static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info, |
| 1325 | struct pqi_scsi_dev *device) |
| 1326 | { |
| 1327 | char *status; |
| 1328 | static const char unknown_state_str[] = |
| 1329 | "Volume is in an unknown state (%u)"; |
| 1330 | char unknown_state_buffer[sizeof(unknown_state_str) + 10]; |
| 1331 | |
| 1332 | switch (device->volume_status) { |
| 1333 | case CISS_LV_OK: |
| 1334 | status = "Volume online"; |
| 1335 | break; |
| 1336 | case CISS_LV_FAILED: |
| 1337 | status = "Volume failed"; |
| 1338 | break; |
| 1339 | case CISS_LV_NOT_CONFIGURED: |
| 1340 | status = "Volume not configured"; |
| 1341 | break; |
| 1342 | case CISS_LV_DEGRADED: |
| 1343 | status = "Volume degraded"; |
| 1344 | break; |
| 1345 | case CISS_LV_READY_FOR_RECOVERY: |
| 1346 | status = "Volume ready for recovery operation"; |
| 1347 | break; |
| 1348 | case CISS_LV_UNDERGOING_RECOVERY: |
| 1349 | status = "Volume undergoing recovery"; |
| 1350 | break; |
| 1351 | case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED: |
| 1352 | status = "Wrong physical drive was replaced"; |
| 1353 | break; |
| 1354 | case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM: |
| 1355 | status = "A physical drive not properly connected"; |
| 1356 | break; |
| 1357 | case CISS_LV_HARDWARE_OVERHEATING: |
| 1358 | status = "Hardware is overheating"; |
| 1359 | break; |
| 1360 | case CISS_LV_HARDWARE_HAS_OVERHEATED: |
| 1361 | status = "Hardware has overheated"; |
| 1362 | break; |
| 1363 | case CISS_LV_UNDERGOING_EXPANSION: |
| 1364 | status = "Volume undergoing expansion"; |
| 1365 | break; |
| 1366 | case CISS_LV_NOT_AVAILABLE: |
| 1367 | status = "Volume waiting for transforming volume"; |
| 1368 | break; |
| 1369 | case CISS_LV_QUEUED_FOR_EXPANSION: |
| 1370 | status = "Volume queued for expansion"; |
| 1371 | break; |
| 1372 | case CISS_LV_DISABLED_SCSI_ID_CONFLICT: |
| 1373 | status = "Volume disabled due to SCSI ID conflict"; |
| 1374 | break; |
| 1375 | case CISS_LV_EJECTED: |
| 1376 | status = "Volume has been ejected"; |
| 1377 | break; |
| 1378 | case CISS_LV_UNDERGOING_ERASE: |
| 1379 | status = "Volume undergoing background erase"; |
| 1380 | break; |
| 1381 | case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD: |
| 1382 | status = "Volume ready for predictive spare rebuild"; |
| 1383 | break; |
| 1384 | case CISS_LV_UNDERGOING_RPI: |
| 1385 | status = "Volume undergoing rapid parity initialization"; |
| 1386 | break; |
| 1387 | case CISS_LV_PENDING_RPI: |
| 1388 | status = "Volume queued for rapid parity initialization"; |
| 1389 | break; |
| 1390 | case CISS_LV_ENCRYPTED_NO_KEY: |
| 1391 | status = "Encrypted volume inaccessible - key not present"; |
| 1392 | break; |
| 1393 | case CISS_LV_UNDERGOING_ENCRYPTION: |
| 1394 | status = "Volume undergoing encryption process"; |
| 1395 | break; |
| 1396 | case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING: |
| 1397 | status = "Volume undergoing encryption re-keying process"; |
| 1398 | break; |
| 1399 | case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER: |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 1400 | status = "Volume encrypted but encryption is disabled"; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1401 | break; |
| 1402 | case CISS_LV_PENDING_ENCRYPTION: |
| 1403 | status = "Volume pending migration to encrypted state"; |
| 1404 | break; |
| 1405 | case CISS_LV_PENDING_ENCRYPTION_REKEYING: |
| 1406 | status = "Volume pending encryption rekeying"; |
| 1407 | break; |
| 1408 | case CISS_LV_NOT_SUPPORTED: |
| 1409 | status = "Volume not supported on this controller"; |
| 1410 | break; |
| 1411 | case CISS_LV_STATUS_UNAVAILABLE: |
| 1412 | status = "Volume status not available"; |
| 1413 | break; |
| 1414 | default: |
| 1415 | snprintf(unknown_state_buffer, sizeof(unknown_state_buffer), |
| 1416 | unknown_state_str, device->volume_status); |
| 1417 | status = unknown_state_buffer; |
| 1418 | break; |
| 1419 | } |
| 1420 | |
| 1421 | dev_info(&ctrl_info->pci_dev->dev, |
| 1422 | "scsi %d:%d:%d:%d %s\n", |
| 1423 | ctrl_info->scsi_host->host_no, |
| 1424 | device->bus, device->target, device->lun, status); |
| 1425 | } |
| 1426 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1427 | static void pqi_rescan_worker(struct work_struct *work) |
| 1428 | { |
| 1429 | struct pqi_ctrl_info *ctrl_info; |
| 1430 | |
| 1431 | ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info, |
| 1432 | rescan_work); |
| 1433 | |
| 1434 | pqi_scan_scsi_devices(ctrl_info); |
| 1435 | } |
| 1436 | |
| 1437 | static int pqi_add_device(struct pqi_ctrl_info *ctrl_info, |
| 1438 | struct pqi_scsi_dev *device) |
| 1439 | { |
| 1440 | int rc; |
| 1441 | |
| 1442 | if (pqi_is_logical_device(device)) |
| 1443 | rc = scsi_add_device(ctrl_info->scsi_host, device->bus, |
| 1444 | device->target, device->lun); |
| 1445 | else |
| 1446 | rc = pqi_add_sas_device(ctrl_info->sas_host, device); |
| 1447 | |
| 1448 | return rc; |
| 1449 | } |
| 1450 | |
| 1451 | static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info, |
| 1452 | struct pqi_scsi_dev *device) |
| 1453 | { |
| 1454 | if (pqi_is_logical_device(device)) |
| 1455 | scsi_remove_device(device->sdev); |
| 1456 | else |
| 1457 | pqi_remove_sas_device(device); |
| 1458 | } |
| 1459 | |
| 1460 | /* Assumes the SCSI device list lock is held. */ |
| 1461 | |
| 1462 | static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info, |
| 1463 | int bus, int target, int lun) |
| 1464 | { |
| 1465 | struct pqi_scsi_dev *device; |
| 1466 | |
| 1467 | list_for_each_entry(device, &ctrl_info->scsi_device_list, |
| 1468 | scsi_device_list_entry) |
| 1469 | if (device->bus == bus && device->target == target && |
| 1470 | device->lun == lun) |
| 1471 | return device; |
| 1472 | |
| 1473 | return NULL; |
| 1474 | } |
| 1475 | |
| 1476 | static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1, |
| 1477 | struct pqi_scsi_dev *dev2) |
| 1478 | { |
| 1479 | if (dev1->is_physical_device != dev2->is_physical_device) |
| 1480 | return false; |
| 1481 | |
| 1482 | if (dev1->is_physical_device) |
| 1483 | return dev1->wwid == dev2->wwid; |
| 1484 | |
| 1485 | return memcmp(dev1->volume_id, dev2->volume_id, |
| 1486 | sizeof(dev1->volume_id)) == 0; |
| 1487 | } |
| 1488 | |
| 1489 | enum pqi_find_result { |
| 1490 | DEVICE_NOT_FOUND, |
| 1491 | DEVICE_CHANGED, |
| 1492 | DEVICE_SAME, |
| 1493 | }; |
| 1494 | |
| 1495 | static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info, |
| 1496 | struct pqi_scsi_dev *device_to_find, |
| 1497 | struct pqi_scsi_dev **matching_device) |
| 1498 | { |
| 1499 | struct pqi_scsi_dev *device; |
| 1500 | |
| 1501 | list_for_each_entry(device, &ctrl_info->scsi_device_list, |
| 1502 | scsi_device_list_entry) { |
| 1503 | if (pqi_scsi3addr_equal(device_to_find->scsi3addr, |
| 1504 | device->scsi3addr)) { |
| 1505 | *matching_device = device; |
| 1506 | if (pqi_device_equal(device_to_find, device)) { |
| 1507 | if (device_to_find->volume_offline) |
| 1508 | return DEVICE_CHANGED; |
| 1509 | return DEVICE_SAME; |
| 1510 | } |
| 1511 | return DEVICE_CHANGED; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | return DEVICE_NOT_FOUND; |
| 1516 | } |
| 1517 | |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1518 | #define PQI_DEV_INFO_BUFFER_LENGTH 128 |
| 1519 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1520 | static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info, |
| 1521 | char *action, struct pqi_scsi_dev *device) |
| 1522 | { |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1523 | ssize_t count; |
| 1524 | char buffer[PQI_DEV_INFO_BUFFER_LENGTH]; |
| 1525 | |
| 1526 | count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH, |
| 1527 | "%d:%d:", ctrl_info->scsi_host->host_no, device->bus); |
| 1528 | |
| 1529 | if (device->target_lun_valid) |
| 1530 | count += snprintf(buffer + count, |
| 1531 | PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1532 | "%d:%d", |
| 1533 | device->target, |
| 1534 | device->lun); |
| 1535 | else |
| 1536 | count += snprintf(buffer + count, |
| 1537 | PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1538 | "-:-"); |
| 1539 | |
| 1540 | if (pqi_is_logical_device(device)) |
| 1541 | count += snprintf(buffer + count, |
| 1542 | PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1543 | " %08x%08x", |
| 1544 | *((u32 *)&device->scsi3addr), |
| 1545 | *((u32 *)&device->scsi3addr[4])); |
| 1546 | else |
| 1547 | count += snprintf(buffer + count, |
| 1548 | PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1549 | " %016llx", device->sas_address); |
| 1550 | |
| 1551 | count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1552 | " %s %.8s %.16s ", |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1553 | scsi_device_type(device->devtype), |
| 1554 | device->vendor, |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1555 | device->model); |
| 1556 | |
| 1557 | if (pqi_is_logical_device(device)) { |
| 1558 | if (device->devtype == TYPE_DISK) |
| 1559 | count += snprintf(buffer + count, |
| 1560 | PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1561 | "SSDSmartPathCap%c En%c %-12s", |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1562 | device->raid_bypass_configured ? '+' : '-', |
| 1563 | device->raid_bypass_enabled ? '+' : '-', |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1564 | pqi_raid_level_to_string(device->raid_level)); |
| 1565 | } else { |
| 1566 | count += snprintf(buffer + count, |
| 1567 | PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1568 | "AIO%c", device->aio_enabled ? '+' : '-'); |
| 1569 | if (device->devtype == TYPE_DISK || |
| 1570 | device->devtype == TYPE_ZBC) |
| 1571 | count += snprintf(buffer + count, |
| 1572 | PQI_DEV_INFO_BUFFER_LENGTH - count, |
| 1573 | " qd=%-6d", device->queue_depth); |
| 1574 | } |
| 1575 | |
| 1576 | dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* Assumes the SCSI device list lock is held. */ |
| 1580 | |
| 1581 | static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device, |
| 1582 | struct pqi_scsi_dev *new_device) |
| 1583 | { |
| 1584 | existing_device->devtype = new_device->devtype; |
| 1585 | existing_device->device_type = new_device->device_type; |
| 1586 | existing_device->bus = new_device->bus; |
| 1587 | if (new_device->target_lun_valid) { |
| 1588 | existing_device->target = new_device->target; |
| 1589 | existing_device->lun = new_device->lun; |
| 1590 | existing_device->target_lun_valid = true; |
| 1591 | } |
| 1592 | |
| 1593 | /* By definition, the scsi3addr and wwid fields are already the same. */ |
| 1594 | |
| 1595 | existing_device->is_physical_device = new_device->is_physical_device; |
Kevin Barnett | bd10cf0 | 2017-05-03 18:54:12 -0500 | [diff] [blame] | 1596 | existing_device->is_external_raid_device = |
| 1597 | new_device->is_external_raid_device; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1598 | existing_device->aio_enabled = new_device->aio_enabled; |
| 1599 | memcpy(existing_device->vendor, new_device->vendor, |
| 1600 | sizeof(existing_device->vendor)); |
| 1601 | memcpy(existing_device->model, new_device->model, |
| 1602 | sizeof(existing_device->model)); |
| 1603 | existing_device->sas_address = new_device->sas_address; |
| 1604 | existing_device->raid_level = new_device->raid_level; |
| 1605 | existing_device->queue_depth = new_device->queue_depth; |
| 1606 | existing_device->aio_handle = new_device->aio_handle; |
| 1607 | existing_device->volume_status = new_device->volume_status; |
| 1608 | existing_device->active_path_index = new_device->active_path_index; |
| 1609 | existing_device->path_map = new_device->path_map; |
| 1610 | existing_device->bay = new_device->bay; |
| 1611 | memcpy(existing_device->box, new_device->box, |
| 1612 | sizeof(existing_device->box)); |
| 1613 | memcpy(existing_device->phys_connector, new_device->phys_connector, |
| 1614 | sizeof(existing_device->phys_connector)); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1615 | existing_device->offload_to_mirror = 0; |
| 1616 | kfree(existing_device->raid_map); |
| 1617 | existing_device->raid_map = new_device->raid_map; |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 1618 | existing_device->raid_bypass_configured = |
| 1619 | new_device->raid_bypass_configured; |
| 1620 | existing_device->raid_bypass_enabled = |
| 1621 | new_device->raid_bypass_enabled; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1622 | |
| 1623 | /* To prevent this from being freed later. */ |
| 1624 | new_device->raid_map = NULL; |
| 1625 | } |
| 1626 | |
| 1627 | static inline void pqi_free_device(struct pqi_scsi_dev *device) |
| 1628 | { |
| 1629 | if (device) { |
| 1630 | kfree(device->raid_map); |
| 1631 | kfree(device); |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | /* |
| 1636 | * Called when exposing a new device to the OS fails in order to re-adjust |
| 1637 | * our internal SCSI device list to match the SCSI ML's view. |
| 1638 | */ |
| 1639 | |
| 1640 | static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info, |
| 1641 | struct pqi_scsi_dev *device) |
| 1642 | { |
| 1643 | unsigned long flags; |
| 1644 | |
| 1645 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
| 1646 | list_del(&device->scsi_device_list_entry); |
| 1647 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags); |
| 1648 | |
| 1649 | /* Allow the device structure to be freed later. */ |
| 1650 | device->keep_device = false; |
| 1651 | } |
| 1652 | |
| 1653 | static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info, |
| 1654 | struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices) |
| 1655 | { |
| 1656 | int rc; |
| 1657 | unsigned int i; |
| 1658 | unsigned long flags; |
| 1659 | enum pqi_find_result find_result; |
| 1660 | struct pqi_scsi_dev *device; |
| 1661 | struct pqi_scsi_dev *next; |
| 1662 | struct pqi_scsi_dev *matching_device; |
Kevin Barnett | 8a994a0 | 2017-05-03 18:55:37 -0500 | [diff] [blame] | 1663 | LIST_HEAD(add_list); |
| 1664 | LIST_HEAD(delete_list); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1665 | |
| 1666 | /* |
| 1667 | * The idea here is to do as little work as possible while holding the |
| 1668 | * spinlock. That's why we go to great pains to defer anything other |
| 1669 | * than updating the internal device list until after we release the |
| 1670 | * spinlock. |
| 1671 | */ |
| 1672 | |
| 1673 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
| 1674 | |
| 1675 | /* Assume that all devices in the existing list have gone away. */ |
| 1676 | list_for_each_entry(device, &ctrl_info->scsi_device_list, |
| 1677 | scsi_device_list_entry) |
| 1678 | device->device_gone = true; |
| 1679 | |
| 1680 | for (i = 0; i < num_new_devices; i++) { |
| 1681 | device = new_device_list[i]; |
| 1682 | |
| 1683 | find_result = pqi_scsi_find_entry(ctrl_info, device, |
| 1684 | &matching_device); |
| 1685 | |
| 1686 | switch (find_result) { |
| 1687 | case DEVICE_SAME: |
| 1688 | /* |
| 1689 | * The newly found device is already in the existing |
| 1690 | * device list. |
| 1691 | */ |
| 1692 | device->new_device = false; |
| 1693 | matching_device->device_gone = false; |
| 1694 | pqi_scsi_update_device(matching_device, device); |
| 1695 | break; |
| 1696 | case DEVICE_NOT_FOUND: |
| 1697 | /* |
| 1698 | * The newly found device is NOT in the existing device |
| 1699 | * list. |
| 1700 | */ |
| 1701 | device->new_device = true; |
| 1702 | break; |
| 1703 | case DEVICE_CHANGED: |
| 1704 | /* |
| 1705 | * The original device has gone away and we need to add |
| 1706 | * the new device. |
| 1707 | */ |
| 1708 | device->new_device = true; |
| 1709 | break; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | /* Process all devices that have gone away. */ |
| 1714 | list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list, |
| 1715 | scsi_device_list_entry) { |
| 1716 | if (device->device_gone) { |
| 1717 | list_del(&device->scsi_device_list_entry); |
| 1718 | list_add_tail(&device->delete_list_entry, &delete_list); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | /* Process all new devices. */ |
| 1723 | for (i = 0; i < num_new_devices; i++) { |
| 1724 | device = new_device_list[i]; |
| 1725 | if (!device->new_device) |
| 1726 | continue; |
| 1727 | if (device->volume_offline) |
| 1728 | continue; |
| 1729 | list_add_tail(&device->scsi_device_list_entry, |
| 1730 | &ctrl_info->scsi_device_list); |
| 1731 | list_add_tail(&device->add_list_entry, &add_list); |
| 1732 | /* To prevent this device structure from being freed later. */ |
| 1733 | device->keep_device = true; |
| 1734 | } |
| 1735 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1736 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags); |
| 1737 | |
| 1738 | /* Remove all devices that have gone away. */ |
| 1739 | list_for_each_entry_safe(device, next, &delete_list, |
| 1740 | delete_list_entry) { |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1741 | if (device->volume_offline) { |
| 1742 | pqi_dev_info(ctrl_info, "offline", device); |
| 1743 | pqi_show_volume_status(ctrl_info, device); |
| 1744 | } else { |
| 1745 | pqi_dev_info(ctrl_info, "removed", device); |
| 1746 | } |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1747 | if (device->sdev) |
| 1748 | pqi_remove_device(ctrl_info, device); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1749 | list_del(&device->delete_list_entry); |
| 1750 | pqi_free_device(device); |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * Notify the SCSI ML if the queue depth of any existing device has |
| 1755 | * changed. |
| 1756 | */ |
| 1757 | list_for_each_entry(device, &ctrl_info->scsi_device_list, |
| 1758 | scsi_device_list_entry) { |
| 1759 | if (device->sdev && device->queue_depth != |
| 1760 | device->advertised_queue_depth) { |
| 1761 | device->advertised_queue_depth = device->queue_depth; |
| 1762 | scsi_change_queue_depth(device->sdev, |
| 1763 | device->advertised_queue_depth); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | /* Expose any new devices. */ |
| 1768 | list_for_each_entry_safe(device, next, &add_list, add_list_entry) { |
Kevin Barnett | 94086f5 | 2017-05-03 18:54:31 -0500 | [diff] [blame] | 1769 | if (!device->sdev) { |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1770 | pqi_dev_info(ctrl_info, "added", device); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1771 | rc = pqi_add_device(ctrl_info, device); |
| 1772 | if (rc) { |
| 1773 | dev_warn(&ctrl_info->pci_dev->dev, |
| 1774 | "scsi %d:%d:%d:%d addition failed, device not added\n", |
| 1775 | ctrl_info->scsi_host->host_no, |
| 1776 | device->bus, device->target, |
| 1777 | device->lun); |
| 1778 | pqi_fixup_botched_add(ctrl_info, device); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1779 | } |
| 1780 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | static bool pqi_is_supported_device(struct pqi_scsi_dev *device) |
| 1785 | { |
| 1786 | bool is_supported = false; |
| 1787 | |
| 1788 | switch (device->devtype) { |
| 1789 | case TYPE_DISK: |
| 1790 | case TYPE_ZBC: |
| 1791 | case TYPE_TAPE: |
| 1792 | case TYPE_MEDIUM_CHANGER: |
| 1793 | case TYPE_ENCLOSURE: |
| 1794 | is_supported = true; |
| 1795 | break; |
| 1796 | case TYPE_RAID: |
| 1797 | /* |
| 1798 | * Only support the HBA controller itself as a RAID |
| 1799 | * controller. If it's a RAID controller other than |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 1800 | * the HBA itself (an external RAID controller, for |
| 1801 | * example), we don't support it. |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1802 | */ |
| 1803 | if (pqi_is_hba_lunid(device->scsi3addr)) |
| 1804 | is_supported = true; |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | return is_supported; |
| 1809 | } |
| 1810 | |
Kevin Barnett | 94086f5 | 2017-05-03 18:54:31 -0500 | [diff] [blame] | 1811 | static inline bool pqi_skip_device(u8 *scsi3addr) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1812 | { |
Kevin Barnett | 94086f5 | 2017-05-03 18:54:31 -0500 | [diff] [blame] | 1813 | /* Ignore all masked devices. */ |
| 1814 | if (MASKED_DEVICE(scsi3addr)) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1815 | return true; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1816 | |
| 1817 | return false; |
| 1818 | } |
| 1819 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1820 | static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info) |
| 1821 | { |
| 1822 | int i; |
| 1823 | int rc; |
Kevin Barnett | 8a994a0 | 2017-05-03 18:55:37 -0500 | [diff] [blame] | 1824 | LIST_HEAD(new_device_list_head); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1825 | struct report_phys_lun_extended *physdev_list = NULL; |
| 1826 | struct report_log_lun_extended *logdev_list = NULL; |
| 1827 | struct report_phys_lun_extended_entry *phys_lun_ext_entry; |
| 1828 | struct report_log_lun_extended_entry *log_lun_ext_entry; |
| 1829 | struct bmic_identify_physical_device *id_phys = NULL; |
| 1830 | u32 num_physicals; |
| 1831 | u32 num_logicals; |
| 1832 | struct pqi_scsi_dev **new_device_list = NULL; |
| 1833 | struct pqi_scsi_dev *device; |
| 1834 | struct pqi_scsi_dev *next; |
| 1835 | unsigned int num_new_devices; |
| 1836 | unsigned int num_valid_devices; |
| 1837 | bool is_physical_device; |
| 1838 | u8 *scsi3addr; |
| 1839 | static char *out_of_memory_msg = |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1840 | "failed to allocate memory, device discovery stopped"; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1841 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1842 | rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list); |
| 1843 | if (rc) |
| 1844 | goto out; |
| 1845 | |
| 1846 | if (physdev_list) |
| 1847 | num_physicals = |
| 1848 | get_unaligned_be32(&physdev_list->header.list_length) |
| 1849 | / sizeof(physdev_list->lun_entries[0]); |
| 1850 | else |
| 1851 | num_physicals = 0; |
| 1852 | |
| 1853 | if (logdev_list) |
| 1854 | num_logicals = |
| 1855 | get_unaligned_be32(&logdev_list->header.list_length) |
| 1856 | / sizeof(logdev_list->lun_entries[0]); |
| 1857 | else |
| 1858 | num_logicals = 0; |
| 1859 | |
| 1860 | if (num_physicals) { |
| 1861 | /* |
| 1862 | * We need this buffer for calls to pqi_get_physical_disk_info() |
| 1863 | * below. We allocate it here instead of inside |
| 1864 | * pqi_get_physical_disk_info() because it's a fairly large |
| 1865 | * buffer. |
| 1866 | */ |
| 1867 | id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL); |
| 1868 | if (!id_phys) { |
| 1869 | dev_warn(&ctrl_info->pci_dev->dev, "%s\n", |
| 1870 | out_of_memory_msg); |
| 1871 | rc = -ENOMEM; |
| 1872 | goto out; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | num_new_devices = num_physicals + num_logicals; |
| 1877 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1878 | new_device_list = kmalloc_array(num_new_devices, |
| 1879 | sizeof(*new_device_list), |
| 1880 | GFP_KERNEL); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1881 | if (!new_device_list) { |
| 1882 | dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg); |
| 1883 | rc = -ENOMEM; |
| 1884 | goto out; |
| 1885 | } |
| 1886 | |
| 1887 | for (i = 0; i < num_new_devices; i++) { |
| 1888 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 1889 | if (!device) { |
| 1890 | dev_warn(&ctrl_info->pci_dev->dev, "%s\n", |
| 1891 | out_of_memory_msg); |
| 1892 | rc = -ENOMEM; |
| 1893 | goto out; |
| 1894 | } |
| 1895 | list_add_tail(&device->new_device_list_entry, |
| 1896 | &new_device_list_head); |
| 1897 | } |
| 1898 | |
| 1899 | device = NULL; |
| 1900 | num_valid_devices = 0; |
| 1901 | |
| 1902 | for (i = 0; i < num_new_devices; i++) { |
| 1903 | |
| 1904 | if (i < num_physicals) { |
| 1905 | is_physical_device = true; |
| 1906 | phys_lun_ext_entry = &physdev_list->lun_entries[i]; |
| 1907 | log_lun_ext_entry = NULL; |
| 1908 | scsi3addr = phys_lun_ext_entry->lunid; |
| 1909 | } else { |
| 1910 | is_physical_device = false; |
| 1911 | phys_lun_ext_entry = NULL; |
| 1912 | log_lun_ext_entry = |
| 1913 | &logdev_list->lun_entries[i - num_physicals]; |
| 1914 | scsi3addr = log_lun_ext_entry->lunid; |
| 1915 | } |
| 1916 | |
Kevin Barnett | 94086f5 | 2017-05-03 18:54:31 -0500 | [diff] [blame] | 1917 | if (is_physical_device && pqi_skip_device(scsi3addr)) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1918 | continue; |
| 1919 | |
| 1920 | if (device) |
| 1921 | device = list_next_entry(device, new_device_list_entry); |
| 1922 | else |
| 1923 | device = list_first_entry(&new_device_list_head, |
| 1924 | struct pqi_scsi_dev, new_device_list_entry); |
| 1925 | |
| 1926 | memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr)); |
| 1927 | device->is_physical_device = is_physical_device; |
Kevin Barnett | bd10cf0 | 2017-05-03 18:54:12 -0500 | [diff] [blame] | 1928 | if (!is_physical_device) |
| 1929 | device->is_external_raid_device = |
| 1930 | pqi_is_external_raid_addr(scsi3addr); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1931 | |
| 1932 | /* Gather information about the device. */ |
| 1933 | rc = pqi_get_device_info(ctrl_info, device); |
| 1934 | if (rc == -ENOMEM) { |
| 1935 | dev_warn(&ctrl_info->pci_dev->dev, "%s\n", |
| 1936 | out_of_memory_msg); |
| 1937 | goto out; |
| 1938 | } |
| 1939 | if (rc) { |
Kevin Barnett | 6de783f | 2017-05-03 18:55:19 -0500 | [diff] [blame] | 1940 | if (device->is_physical_device) |
| 1941 | dev_warn(&ctrl_info->pci_dev->dev, |
| 1942 | "obtaining device info failed, skipping physical device %016llx\n", |
| 1943 | get_unaligned_be64( |
| 1944 | &phys_lun_ext_entry->wwid)); |
| 1945 | else |
| 1946 | dev_warn(&ctrl_info->pci_dev->dev, |
| 1947 | "obtaining device info failed, skipping logical device %08x%08x\n", |
| 1948 | *((u32 *)&device->scsi3addr), |
| 1949 | *((u32 *)&device->scsi3addr[4])); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1950 | rc = 0; |
| 1951 | continue; |
| 1952 | } |
| 1953 | |
| 1954 | if (!pqi_is_supported_device(device)) |
| 1955 | continue; |
| 1956 | |
| 1957 | pqi_assign_bus_target_lun(device); |
| 1958 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 1959 | if (device->is_physical_device) { |
| 1960 | device->wwid = phys_lun_ext_entry->wwid; |
| 1961 | if ((phys_lun_ext_entry->device_flags & |
| 1962 | REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) && |
| 1963 | phys_lun_ext_entry->aio_handle) |
| 1964 | device->aio_enabled = true; |
| 1965 | } else { |
| 1966 | memcpy(device->volume_id, log_lun_ext_entry->volume_id, |
| 1967 | sizeof(device->volume_id)); |
| 1968 | } |
| 1969 | |
| 1970 | switch (device->devtype) { |
| 1971 | case TYPE_DISK: |
| 1972 | case TYPE_ZBC: |
| 1973 | case TYPE_ENCLOSURE: |
| 1974 | if (device->is_physical_device) { |
| 1975 | device->sas_address = |
| 1976 | get_unaligned_be64(&device->wwid); |
| 1977 | if (device->devtype == TYPE_DISK || |
| 1978 | device->devtype == TYPE_ZBC) { |
| 1979 | device->aio_handle = |
| 1980 | phys_lun_ext_entry->aio_handle; |
| 1981 | pqi_get_physical_disk_info(ctrl_info, |
| 1982 | device, id_phys); |
| 1983 | } |
| 1984 | } |
| 1985 | break; |
| 1986 | } |
| 1987 | |
| 1988 | new_device_list[num_valid_devices++] = device; |
| 1989 | } |
| 1990 | |
| 1991 | pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices); |
| 1992 | |
| 1993 | out: |
| 1994 | list_for_each_entry_safe(device, next, &new_device_list_head, |
| 1995 | new_device_list_entry) { |
| 1996 | if (device->keep_device) |
| 1997 | continue; |
| 1998 | list_del(&device->new_device_list_entry); |
| 1999 | pqi_free_device(device); |
| 2000 | } |
| 2001 | |
| 2002 | kfree(new_device_list); |
| 2003 | kfree(physdev_list); |
| 2004 | kfree(logdev_list); |
| 2005 | kfree(id_phys); |
| 2006 | |
| 2007 | return rc; |
| 2008 | } |
| 2009 | |
| 2010 | static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info) |
| 2011 | { |
| 2012 | unsigned long flags; |
| 2013 | struct pqi_scsi_dev *device; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2014 | |
Kevin Barnett | a37ef74 | 2017-05-03 18:52:22 -0500 | [diff] [blame] | 2015 | while (1) { |
| 2016 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2017 | |
Kevin Barnett | a37ef74 | 2017-05-03 18:52:22 -0500 | [diff] [blame] | 2018 | device = list_first_entry_or_null(&ctrl_info->scsi_device_list, |
| 2019 | struct pqi_scsi_dev, scsi_device_list_entry); |
| 2020 | if (device) |
| 2021 | list_del(&device->scsi_device_list_entry); |
| 2022 | |
| 2023 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, |
| 2024 | flags); |
| 2025 | |
| 2026 | if (!device) |
| 2027 | break; |
| 2028 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2029 | if (device->sdev) |
| 2030 | pqi_remove_device(ctrl_info, device); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2031 | pqi_free_device(device); |
| 2032 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info) |
| 2036 | { |
| 2037 | int rc; |
| 2038 | |
| 2039 | if (pqi_ctrl_offline(ctrl_info)) |
| 2040 | return -ENXIO; |
| 2041 | |
| 2042 | mutex_lock(&ctrl_info->scan_mutex); |
| 2043 | |
| 2044 | rc = pqi_update_scsi_devices(ctrl_info); |
| 2045 | if (rc) |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 2046 | pqi_schedule_rescan_worker_delayed(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2047 | |
| 2048 | mutex_unlock(&ctrl_info->scan_mutex); |
| 2049 | |
| 2050 | return rc; |
| 2051 | } |
| 2052 | |
| 2053 | static void pqi_scan_start(struct Scsi_Host *shost) |
| 2054 | { |
| 2055 | pqi_scan_scsi_devices(shost_to_hba(shost)); |
| 2056 | } |
| 2057 | |
| 2058 | /* Returns TRUE if scan is finished. */ |
| 2059 | |
| 2060 | static int pqi_scan_finished(struct Scsi_Host *shost, |
| 2061 | unsigned long elapsed_time) |
| 2062 | { |
| 2063 | struct pqi_ctrl_info *ctrl_info; |
| 2064 | |
| 2065 | ctrl_info = shost_priv(shost); |
| 2066 | |
| 2067 | return !mutex_is_locked(&ctrl_info->scan_mutex); |
| 2068 | } |
| 2069 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 2070 | static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info) |
| 2071 | { |
| 2072 | mutex_lock(&ctrl_info->scan_mutex); |
| 2073 | mutex_unlock(&ctrl_info->scan_mutex); |
| 2074 | } |
| 2075 | |
| 2076 | static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info) |
| 2077 | { |
| 2078 | mutex_lock(&ctrl_info->lun_reset_mutex); |
| 2079 | mutex_unlock(&ctrl_info->lun_reset_mutex); |
| 2080 | } |
| 2081 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2082 | static inline void pqi_set_encryption_info( |
| 2083 | struct pqi_encryption_info *encryption_info, struct raid_map *raid_map, |
| 2084 | u64 first_block) |
| 2085 | { |
| 2086 | u32 volume_blk_size; |
| 2087 | |
| 2088 | /* |
| 2089 | * Set the encryption tweak values based on logical block address. |
| 2090 | * If the block size is 512, the tweak value is equal to the LBA. |
| 2091 | * For other block sizes, tweak value is (LBA * block size) / 512. |
| 2092 | */ |
| 2093 | volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size); |
| 2094 | if (volume_blk_size != 512) |
| 2095 | first_block = (first_block * volume_blk_size) / 512; |
| 2096 | |
| 2097 | encryption_info->data_encryption_key_index = |
| 2098 | get_unaligned_le16(&raid_map->data_encryption_key_index); |
| 2099 | encryption_info->encrypt_tweak_lower = lower_32_bits(first_block); |
| 2100 | encryption_info->encrypt_tweak_upper = upper_32_bits(first_block); |
| 2101 | } |
| 2102 | |
| 2103 | /* |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 2104 | * Attempt to perform RAID bypass mapping for a logical volume I/O. |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2105 | */ |
| 2106 | |
| 2107 | #define PQI_RAID_BYPASS_INELIGIBLE 1 |
| 2108 | |
| 2109 | static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info, |
| 2110 | struct pqi_scsi_dev *device, struct scsi_cmnd *scmd, |
| 2111 | struct pqi_queue_group *queue_group) |
| 2112 | { |
| 2113 | struct raid_map *raid_map; |
| 2114 | bool is_write = false; |
| 2115 | u32 map_index; |
| 2116 | u64 first_block; |
| 2117 | u64 last_block; |
| 2118 | u32 block_cnt; |
| 2119 | u32 blocks_per_row; |
| 2120 | u64 first_row; |
| 2121 | u64 last_row; |
| 2122 | u32 first_row_offset; |
| 2123 | u32 last_row_offset; |
| 2124 | u32 first_column; |
| 2125 | u32 last_column; |
| 2126 | u64 r0_first_row; |
| 2127 | u64 r0_last_row; |
| 2128 | u32 r5or6_blocks_per_row; |
| 2129 | u64 r5or6_first_row; |
| 2130 | u64 r5or6_last_row; |
| 2131 | u32 r5or6_first_row_offset; |
| 2132 | u32 r5or6_last_row_offset; |
| 2133 | u32 r5or6_first_column; |
| 2134 | u32 r5or6_last_column; |
| 2135 | u16 data_disks_per_row; |
| 2136 | u32 total_disks_per_row; |
| 2137 | u16 layout_map_count; |
| 2138 | u32 stripesize; |
| 2139 | u16 strip_size; |
| 2140 | u32 first_group; |
| 2141 | u32 last_group; |
| 2142 | u32 current_group; |
| 2143 | u32 map_row; |
| 2144 | u32 aio_handle; |
| 2145 | u64 disk_block; |
| 2146 | u32 disk_block_cnt; |
| 2147 | u8 cdb[16]; |
| 2148 | u8 cdb_length; |
| 2149 | int offload_to_mirror; |
| 2150 | struct pqi_encryption_info *encryption_info_ptr; |
| 2151 | struct pqi_encryption_info encryption_info; |
| 2152 | #if BITS_PER_LONG == 32 |
| 2153 | u64 tmpdiv; |
| 2154 | #endif |
| 2155 | |
| 2156 | /* Check for valid opcode, get LBA and block count. */ |
| 2157 | switch (scmd->cmnd[0]) { |
| 2158 | case WRITE_6: |
| 2159 | is_write = true; |
| 2160 | /* fall through */ |
| 2161 | case READ_6: |
kevin Barnett | e018ef5 | 2016-09-16 15:01:51 -0500 | [diff] [blame] | 2162 | first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) | |
| 2163 | (scmd->cmnd[2] << 8) | scmd->cmnd[3]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2164 | block_cnt = (u32)scmd->cmnd[4]; |
| 2165 | if (block_cnt == 0) |
| 2166 | block_cnt = 256; |
| 2167 | break; |
| 2168 | case WRITE_10: |
| 2169 | is_write = true; |
| 2170 | /* fall through */ |
| 2171 | case READ_10: |
| 2172 | first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]); |
| 2173 | block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]); |
| 2174 | break; |
| 2175 | case WRITE_12: |
| 2176 | is_write = true; |
| 2177 | /* fall through */ |
| 2178 | case READ_12: |
| 2179 | first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]); |
| 2180 | block_cnt = get_unaligned_be32(&scmd->cmnd[6]); |
| 2181 | break; |
| 2182 | case WRITE_16: |
| 2183 | is_write = true; |
| 2184 | /* fall through */ |
| 2185 | case READ_16: |
| 2186 | first_block = get_unaligned_be64(&scmd->cmnd[2]); |
| 2187 | block_cnt = get_unaligned_be32(&scmd->cmnd[10]); |
| 2188 | break; |
| 2189 | default: |
| 2190 | /* Process via normal I/O path. */ |
| 2191 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2192 | } |
| 2193 | |
| 2194 | /* Check for write to non-RAID-0. */ |
| 2195 | if (is_write && device->raid_level != SA_RAID_0) |
| 2196 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2197 | |
| 2198 | if (unlikely(block_cnt == 0)) |
| 2199 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2200 | |
| 2201 | last_block = first_block + block_cnt - 1; |
| 2202 | raid_map = device->raid_map; |
| 2203 | |
| 2204 | /* Check for invalid block or wraparound. */ |
| 2205 | if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) || |
| 2206 | last_block < first_block) |
| 2207 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2208 | |
| 2209 | data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row); |
| 2210 | strip_size = get_unaligned_le16(&raid_map->strip_size); |
| 2211 | layout_map_count = get_unaligned_le16(&raid_map->layout_map_count); |
| 2212 | |
| 2213 | /* Calculate stripe information for the request. */ |
| 2214 | blocks_per_row = data_disks_per_row * strip_size; |
| 2215 | #if BITS_PER_LONG == 32 |
| 2216 | tmpdiv = first_block; |
| 2217 | do_div(tmpdiv, blocks_per_row); |
| 2218 | first_row = tmpdiv; |
| 2219 | tmpdiv = last_block; |
| 2220 | do_div(tmpdiv, blocks_per_row); |
| 2221 | last_row = tmpdiv; |
| 2222 | first_row_offset = (u32)(first_block - (first_row * blocks_per_row)); |
| 2223 | last_row_offset = (u32)(last_block - (last_row * blocks_per_row)); |
| 2224 | tmpdiv = first_row_offset; |
| 2225 | do_div(tmpdiv, strip_size); |
| 2226 | first_column = tmpdiv; |
| 2227 | tmpdiv = last_row_offset; |
| 2228 | do_div(tmpdiv, strip_size); |
| 2229 | last_column = tmpdiv; |
| 2230 | #else |
| 2231 | first_row = first_block / blocks_per_row; |
| 2232 | last_row = last_block / blocks_per_row; |
| 2233 | first_row_offset = (u32)(first_block - (first_row * blocks_per_row)); |
| 2234 | last_row_offset = (u32)(last_block - (last_row * blocks_per_row)); |
| 2235 | first_column = first_row_offset / strip_size; |
| 2236 | last_column = last_row_offset / strip_size; |
| 2237 | #endif |
| 2238 | |
| 2239 | /* If this isn't a single row/column then give to the controller. */ |
| 2240 | if (first_row != last_row || first_column != last_column) |
| 2241 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2242 | |
| 2243 | /* Proceeding with driver mapping. */ |
| 2244 | total_disks_per_row = data_disks_per_row + |
| 2245 | get_unaligned_le16(&raid_map->metadata_disks_per_row); |
| 2246 | map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) % |
| 2247 | get_unaligned_le16(&raid_map->row_cnt); |
| 2248 | map_index = (map_row * total_disks_per_row) + first_column; |
| 2249 | |
| 2250 | /* RAID 1 */ |
| 2251 | if (device->raid_level == SA_RAID_1) { |
| 2252 | if (device->offload_to_mirror) |
| 2253 | map_index += data_disks_per_row; |
| 2254 | device->offload_to_mirror = !device->offload_to_mirror; |
| 2255 | } else if (device->raid_level == SA_RAID_ADM) { |
| 2256 | /* RAID ADM */ |
| 2257 | /* |
| 2258 | * Handles N-way mirrors (R1-ADM) and R10 with # of drives |
| 2259 | * divisible by 3. |
| 2260 | */ |
| 2261 | offload_to_mirror = device->offload_to_mirror; |
| 2262 | if (offload_to_mirror == 0) { |
| 2263 | /* use physical disk in the first mirrored group. */ |
| 2264 | map_index %= data_disks_per_row; |
| 2265 | } else { |
| 2266 | do { |
| 2267 | /* |
| 2268 | * Determine mirror group that map_index |
| 2269 | * indicates. |
| 2270 | */ |
| 2271 | current_group = map_index / data_disks_per_row; |
| 2272 | |
| 2273 | if (offload_to_mirror != current_group) { |
| 2274 | if (current_group < |
| 2275 | layout_map_count - 1) { |
| 2276 | /* |
| 2277 | * Select raid index from |
| 2278 | * next group. |
| 2279 | */ |
| 2280 | map_index += data_disks_per_row; |
| 2281 | current_group++; |
| 2282 | } else { |
| 2283 | /* |
| 2284 | * Select raid index from first |
| 2285 | * group. |
| 2286 | */ |
| 2287 | map_index %= data_disks_per_row; |
| 2288 | current_group = 0; |
| 2289 | } |
| 2290 | } |
| 2291 | } while (offload_to_mirror != current_group); |
| 2292 | } |
| 2293 | |
| 2294 | /* Set mirror group to use next time. */ |
| 2295 | offload_to_mirror = |
| 2296 | (offload_to_mirror >= layout_map_count - 1) ? |
| 2297 | 0 : offload_to_mirror + 1; |
| 2298 | WARN_ON(offload_to_mirror >= layout_map_count); |
| 2299 | device->offload_to_mirror = offload_to_mirror; |
| 2300 | /* |
| 2301 | * Avoid direct use of device->offload_to_mirror within this |
| 2302 | * function since multiple threads might simultaneously |
| 2303 | * increment it beyond the range of device->layout_map_count -1. |
| 2304 | */ |
| 2305 | } else if ((device->raid_level == SA_RAID_5 || |
| 2306 | device->raid_level == SA_RAID_6) && layout_map_count > 1) { |
| 2307 | /* RAID 50/60 */ |
| 2308 | /* Verify first and last block are in same RAID group */ |
| 2309 | r5or6_blocks_per_row = strip_size * data_disks_per_row; |
| 2310 | stripesize = r5or6_blocks_per_row * layout_map_count; |
| 2311 | #if BITS_PER_LONG == 32 |
| 2312 | tmpdiv = first_block; |
| 2313 | first_group = do_div(tmpdiv, stripesize); |
| 2314 | tmpdiv = first_group; |
| 2315 | do_div(tmpdiv, r5or6_blocks_per_row); |
| 2316 | first_group = tmpdiv; |
| 2317 | tmpdiv = last_block; |
| 2318 | last_group = do_div(tmpdiv, stripesize); |
| 2319 | tmpdiv = last_group; |
| 2320 | do_div(tmpdiv, r5or6_blocks_per_row); |
| 2321 | last_group = tmpdiv; |
| 2322 | #else |
| 2323 | first_group = (first_block % stripesize) / r5or6_blocks_per_row; |
| 2324 | last_group = (last_block % stripesize) / r5or6_blocks_per_row; |
| 2325 | #endif |
| 2326 | if (first_group != last_group) |
| 2327 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2328 | |
| 2329 | /* Verify request is in a single row of RAID 5/6 */ |
| 2330 | #if BITS_PER_LONG == 32 |
| 2331 | tmpdiv = first_block; |
| 2332 | do_div(tmpdiv, stripesize); |
| 2333 | first_row = r5or6_first_row = r0_first_row = tmpdiv; |
| 2334 | tmpdiv = last_block; |
| 2335 | do_div(tmpdiv, stripesize); |
| 2336 | r5or6_last_row = r0_last_row = tmpdiv; |
| 2337 | #else |
| 2338 | first_row = r5or6_first_row = r0_first_row = |
| 2339 | first_block / stripesize; |
| 2340 | r5or6_last_row = r0_last_row = last_block / stripesize; |
| 2341 | #endif |
| 2342 | if (r5or6_first_row != r5or6_last_row) |
| 2343 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2344 | |
| 2345 | /* Verify request is in a single column */ |
| 2346 | #if BITS_PER_LONG == 32 |
| 2347 | tmpdiv = first_block; |
| 2348 | first_row_offset = do_div(tmpdiv, stripesize); |
| 2349 | tmpdiv = first_row_offset; |
| 2350 | first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row); |
| 2351 | r5or6_first_row_offset = first_row_offset; |
| 2352 | tmpdiv = last_block; |
| 2353 | r5or6_last_row_offset = do_div(tmpdiv, stripesize); |
| 2354 | tmpdiv = r5or6_last_row_offset; |
| 2355 | r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row); |
| 2356 | tmpdiv = r5or6_first_row_offset; |
| 2357 | do_div(tmpdiv, strip_size); |
| 2358 | first_column = r5or6_first_column = tmpdiv; |
| 2359 | tmpdiv = r5or6_last_row_offset; |
| 2360 | do_div(tmpdiv, strip_size); |
| 2361 | r5or6_last_column = tmpdiv; |
| 2362 | #else |
| 2363 | first_row_offset = r5or6_first_row_offset = |
| 2364 | (u32)((first_block % stripesize) % |
| 2365 | r5or6_blocks_per_row); |
| 2366 | |
| 2367 | r5or6_last_row_offset = |
| 2368 | (u32)((last_block % stripesize) % |
| 2369 | r5or6_blocks_per_row); |
| 2370 | |
| 2371 | first_column = r5or6_first_row_offset / strip_size; |
| 2372 | r5or6_first_column = first_column; |
| 2373 | r5or6_last_column = r5or6_last_row_offset / strip_size; |
| 2374 | #endif |
| 2375 | if (r5or6_first_column != r5or6_last_column) |
| 2376 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2377 | |
| 2378 | /* Request is eligible */ |
| 2379 | map_row = |
| 2380 | ((u32)(first_row >> raid_map->parity_rotation_shift)) % |
| 2381 | get_unaligned_le16(&raid_map->row_cnt); |
| 2382 | |
| 2383 | map_index = (first_group * |
| 2384 | (get_unaligned_le16(&raid_map->row_cnt) * |
| 2385 | total_disks_per_row)) + |
| 2386 | (map_row * total_disks_per_row) + first_column; |
| 2387 | } |
| 2388 | |
| 2389 | if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES)) |
| 2390 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2391 | |
| 2392 | aio_handle = raid_map->disk_data[map_index].aio_handle; |
| 2393 | disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) + |
| 2394 | first_row * strip_size + |
| 2395 | (first_row_offset - first_column * strip_size); |
| 2396 | disk_block_cnt = block_cnt; |
| 2397 | |
| 2398 | /* Handle differing logical/physical block sizes. */ |
| 2399 | if (raid_map->phys_blk_shift) { |
| 2400 | disk_block <<= raid_map->phys_blk_shift; |
| 2401 | disk_block_cnt <<= raid_map->phys_blk_shift; |
| 2402 | } |
| 2403 | |
| 2404 | if (unlikely(disk_block_cnt > 0xffff)) |
| 2405 | return PQI_RAID_BYPASS_INELIGIBLE; |
| 2406 | |
| 2407 | /* Build the new CDB for the physical disk I/O. */ |
| 2408 | if (disk_block > 0xffffffff) { |
| 2409 | cdb[0] = is_write ? WRITE_16 : READ_16; |
| 2410 | cdb[1] = 0; |
| 2411 | put_unaligned_be64(disk_block, &cdb[2]); |
| 2412 | put_unaligned_be32(disk_block_cnt, &cdb[10]); |
| 2413 | cdb[14] = 0; |
| 2414 | cdb[15] = 0; |
| 2415 | cdb_length = 16; |
| 2416 | } else { |
| 2417 | cdb[0] = is_write ? WRITE_10 : READ_10; |
| 2418 | cdb[1] = 0; |
| 2419 | put_unaligned_be32((u32)disk_block, &cdb[2]); |
| 2420 | cdb[6] = 0; |
| 2421 | put_unaligned_be16((u16)disk_block_cnt, &cdb[7]); |
| 2422 | cdb[9] = 0; |
| 2423 | cdb_length = 10; |
| 2424 | } |
| 2425 | |
| 2426 | if (get_unaligned_le16(&raid_map->flags) & |
| 2427 | RAID_MAP_ENCRYPTION_ENABLED) { |
| 2428 | pqi_set_encryption_info(&encryption_info, raid_map, |
| 2429 | first_block); |
| 2430 | encryption_info_ptr = &encryption_info; |
| 2431 | } else { |
| 2432 | encryption_info_ptr = NULL; |
| 2433 | } |
| 2434 | |
| 2435 | return pqi_aio_submit_io(ctrl_info, scmd, aio_handle, |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 2436 | cdb, cdb_length, queue_group, encryption_info_ptr, true); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | #define PQI_STATUS_IDLE 0x0 |
| 2440 | |
| 2441 | #define PQI_CREATE_ADMIN_QUEUE_PAIR 1 |
| 2442 | #define PQI_DELETE_ADMIN_QUEUE_PAIR 2 |
| 2443 | |
| 2444 | #define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0 |
| 2445 | #define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1 |
| 2446 | #define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2 |
| 2447 | #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3 |
| 2448 | #define PQI_DEVICE_STATE_ERROR 0x4 |
| 2449 | |
| 2450 | #define PQI_MODE_READY_TIMEOUT_SECS 30 |
| 2451 | #define PQI_MODE_READY_POLL_INTERVAL_MSECS 1 |
| 2452 | |
| 2453 | static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info) |
| 2454 | { |
| 2455 | struct pqi_device_registers __iomem *pqi_registers; |
| 2456 | unsigned long timeout; |
| 2457 | u64 signature; |
| 2458 | u8 status; |
| 2459 | |
| 2460 | pqi_registers = ctrl_info->pqi_registers; |
| 2461 | timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies; |
| 2462 | |
| 2463 | while (1) { |
| 2464 | signature = readq(&pqi_registers->signature); |
| 2465 | if (memcmp(&signature, PQI_DEVICE_SIGNATURE, |
| 2466 | sizeof(signature)) == 0) |
| 2467 | break; |
| 2468 | if (time_after(jiffies, timeout)) { |
| 2469 | dev_err(&ctrl_info->pci_dev->dev, |
| 2470 | "timed out waiting for PQI signature\n"); |
| 2471 | return -ETIMEDOUT; |
| 2472 | } |
| 2473 | msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS); |
| 2474 | } |
| 2475 | |
| 2476 | while (1) { |
| 2477 | status = readb(&pqi_registers->function_and_status_code); |
| 2478 | if (status == PQI_STATUS_IDLE) |
| 2479 | break; |
| 2480 | if (time_after(jiffies, timeout)) { |
| 2481 | dev_err(&ctrl_info->pci_dev->dev, |
| 2482 | "timed out waiting for PQI IDLE\n"); |
| 2483 | return -ETIMEDOUT; |
| 2484 | } |
| 2485 | msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS); |
| 2486 | } |
| 2487 | |
| 2488 | while (1) { |
| 2489 | if (readl(&pqi_registers->device_status) == |
| 2490 | PQI_DEVICE_STATE_ALL_REGISTERS_READY) |
| 2491 | break; |
| 2492 | if (time_after(jiffies, timeout)) { |
| 2493 | dev_err(&ctrl_info->pci_dev->dev, |
| 2494 | "timed out waiting for PQI all registers ready\n"); |
| 2495 | return -ETIMEDOUT; |
| 2496 | } |
| 2497 | msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS); |
| 2498 | } |
| 2499 | |
| 2500 | return 0; |
| 2501 | } |
| 2502 | |
| 2503 | static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request) |
| 2504 | { |
| 2505 | struct pqi_scsi_dev *device; |
| 2506 | |
| 2507 | device = io_request->scmd->device->hostdata; |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 2508 | device->raid_bypass_enabled = false; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 2509 | device->aio_enabled = false; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2510 | } |
| 2511 | |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 2512 | static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2513 | { |
| 2514 | struct pqi_ctrl_info *ctrl_info; |
Kevin Barnett | e58081a | 2016-08-31 14:54:29 -0500 | [diff] [blame] | 2515 | struct pqi_scsi_dev *device; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2516 | |
Kevin Barnett | 03b288cf | 2017-05-03 18:54:49 -0500 | [diff] [blame] | 2517 | device = sdev->hostdata; |
| 2518 | if (device->device_offline) |
| 2519 | return; |
| 2520 | |
| 2521 | device->device_offline = true; |
| 2522 | scsi_device_set_state(sdev, SDEV_OFFLINE); |
| 2523 | ctrl_info = shost_to_hba(sdev->host); |
| 2524 | pqi_schedule_rescan_worker(ctrl_info); |
| 2525 | dev_err(&ctrl_info->pci_dev->dev, "offlined %s scsi %d:%d:%d:%d\n", |
| 2526 | path, ctrl_info->scsi_host->host_no, device->bus, |
| 2527 | device->target, device->lun); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | static void pqi_process_raid_io_error(struct pqi_io_request *io_request) |
| 2531 | { |
| 2532 | u8 scsi_status; |
| 2533 | u8 host_byte; |
| 2534 | struct scsi_cmnd *scmd; |
| 2535 | struct pqi_raid_error_info *error_info; |
| 2536 | size_t sense_data_length; |
| 2537 | int residual_count; |
| 2538 | int xfer_count; |
| 2539 | struct scsi_sense_hdr sshdr; |
| 2540 | |
| 2541 | scmd = io_request->scmd; |
| 2542 | if (!scmd) |
| 2543 | return; |
| 2544 | |
| 2545 | error_info = io_request->error_info; |
| 2546 | scsi_status = error_info->status; |
| 2547 | host_byte = DID_OK; |
| 2548 | |
Kevin Barnett | f5b6320 | 2017-05-03 18:55:07 -0500 | [diff] [blame] | 2549 | switch (error_info->data_out_result) { |
| 2550 | case PQI_DATA_IN_OUT_GOOD: |
| 2551 | break; |
| 2552 | case PQI_DATA_IN_OUT_UNDERFLOW: |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2553 | xfer_count = |
| 2554 | get_unaligned_le32(&error_info->data_out_transferred); |
| 2555 | residual_count = scsi_bufflen(scmd) - xfer_count; |
| 2556 | scsi_set_resid(scmd, residual_count); |
| 2557 | if (xfer_count < scmd->underflow) |
| 2558 | host_byte = DID_SOFT_ERROR; |
Kevin Barnett | f5b6320 | 2017-05-03 18:55:07 -0500 | [diff] [blame] | 2559 | break; |
| 2560 | case PQI_DATA_IN_OUT_UNSOLICITED_ABORT: |
| 2561 | case PQI_DATA_IN_OUT_ABORTED: |
| 2562 | host_byte = DID_ABORT; |
| 2563 | break; |
| 2564 | case PQI_DATA_IN_OUT_TIMEOUT: |
| 2565 | host_byte = DID_TIME_OUT; |
| 2566 | break; |
| 2567 | case PQI_DATA_IN_OUT_BUFFER_OVERFLOW: |
| 2568 | case PQI_DATA_IN_OUT_PROTOCOL_ERROR: |
| 2569 | case PQI_DATA_IN_OUT_BUFFER_ERROR: |
| 2570 | case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA: |
| 2571 | case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE: |
| 2572 | case PQI_DATA_IN_OUT_ERROR: |
| 2573 | case PQI_DATA_IN_OUT_HARDWARE_ERROR: |
| 2574 | case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR: |
| 2575 | case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT: |
| 2576 | case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED: |
| 2577 | case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED: |
| 2578 | case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED: |
| 2579 | case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST: |
| 2580 | case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION: |
| 2581 | case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED: |
| 2582 | case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ: |
| 2583 | default: |
| 2584 | host_byte = DID_ERROR; |
| 2585 | break; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | sense_data_length = get_unaligned_le16(&error_info->sense_data_length); |
| 2589 | if (sense_data_length == 0) |
| 2590 | sense_data_length = |
| 2591 | get_unaligned_le16(&error_info->response_data_length); |
| 2592 | if (sense_data_length) { |
| 2593 | if (sense_data_length > sizeof(error_info->data)) |
| 2594 | sense_data_length = sizeof(error_info->data); |
| 2595 | |
| 2596 | if (scsi_status == SAM_STAT_CHECK_CONDITION && |
| 2597 | scsi_normalize_sense(error_info->data, |
| 2598 | sense_data_length, &sshdr) && |
| 2599 | sshdr.sense_key == HARDWARE_ERROR && |
| 2600 | sshdr.asc == 0x3e && |
| 2601 | sshdr.ascq == 0x1) { |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 2602 | pqi_take_device_offline(scmd->device, "RAID"); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2603 | host_byte = DID_NO_CONNECT; |
| 2604 | } |
| 2605 | |
| 2606 | if (sense_data_length > SCSI_SENSE_BUFFERSIZE) |
| 2607 | sense_data_length = SCSI_SENSE_BUFFERSIZE; |
| 2608 | memcpy(scmd->sense_buffer, error_info->data, |
| 2609 | sense_data_length); |
| 2610 | } |
| 2611 | |
| 2612 | scmd->result = scsi_status; |
| 2613 | set_host_byte(scmd, host_byte); |
| 2614 | } |
| 2615 | |
| 2616 | static void pqi_process_aio_io_error(struct pqi_io_request *io_request) |
| 2617 | { |
| 2618 | u8 scsi_status; |
| 2619 | u8 host_byte; |
| 2620 | struct scsi_cmnd *scmd; |
| 2621 | struct pqi_aio_error_info *error_info; |
| 2622 | size_t sense_data_length; |
| 2623 | int residual_count; |
| 2624 | int xfer_count; |
| 2625 | bool device_offline; |
| 2626 | |
| 2627 | scmd = io_request->scmd; |
| 2628 | error_info = io_request->error_info; |
| 2629 | host_byte = DID_OK; |
| 2630 | sense_data_length = 0; |
| 2631 | device_offline = false; |
| 2632 | |
| 2633 | switch (error_info->service_response) { |
| 2634 | case PQI_AIO_SERV_RESPONSE_COMPLETE: |
| 2635 | scsi_status = error_info->status; |
| 2636 | break; |
| 2637 | case PQI_AIO_SERV_RESPONSE_FAILURE: |
| 2638 | switch (error_info->status) { |
| 2639 | case PQI_AIO_STATUS_IO_ABORTED: |
| 2640 | scsi_status = SAM_STAT_TASK_ABORTED; |
| 2641 | break; |
| 2642 | case PQI_AIO_STATUS_UNDERRUN: |
| 2643 | scsi_status = SAM_STAT_GOOD; |
| 2644 | residual_count = get_unaligned_le32( |
| 2645 | &error_info->residual_count); |
| 2646 | scsi_set_resid(scmd, residual_count); |
| 2647 | xfer_count = scsi_bufflen(scmd) - residual_count; |
| 2648 | if (xfer_count < scmd->underflow) |
| 2649 | host_byte = DID_SOFT_ERROR; |
| 2650 | break; |
| 2651 | case PQI_AIO_STATUS_OVERRUN: |
| 2652 | scsi_status = SAM_STAT_GOOD; |
| 2653 | break; |
| 2654 | case PQI_AIO_STATUS_AIO_PATH_DISABLED: |
| 2655 | pqi_aio_path_disabled(io_request); |
| 2656 | scsi_status = SAM_STAT_GOOD; |
| 2657 | io_request->status = -EAGAIN; |
| 2658 | break; |
| 2659 | case PQI_AIO_STATUS_NO_PATH_TO_DEVICE: |
| 2660 | case PQI_AIO_STATUS_INVALID_DEVICE: |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 2661 | if (!io_request->raid_bypass) { |
| 2662 | device_offline = true; |
| 2663 | pqi_take_device_offline(scmd->device, "AIO"); |
| 2664 | host_byte = DID_NO_CONNECT; |
| 2665 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2666 | scsi_status = SAM_STAT_CHECK_CONDITION; |
| 2667 | break; |
| 2668 | case PQI_AIO_STATUS_IO_ERROR: |
| 2669 | default: |
| 2670 | scsi_status = SAM_STAT_CHECK_CONDITION; |
| 2671 | break; |
| 2672 | } |
| 2673 | break; |
| 2674 | case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE: |
| 2675 | case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED: |
| 2676 | scsi_status = SAM_STAT_GOOD; |
| 2677 | break; |
| 2678 | case PQI_AIO_SERV_RESPONSE_TMF_REJECTED: |
| 2679 | case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN: |
| 2680 | default: |
| 2681 | scsi_status = SAM_STAT_CHECK_CONDITION; |
| 2682 | break; |
| 2683 | } |
| 2684 | |
| 2685 | if (error_info->data_present) { |
| 2686 | sense_data_length = |
| 2687 | get_unaligned_le16(&error_info->data_length); |
| 2688 | if (sense_data_length) { |
| 2689 | if (sense_data_length > sizeof(error_info->data)) |
| 2690 | sense_data_length = sizeof(error_info->data); |
| 2691 | if (sense_data_length > SCSI_SENSE_BUFFERSIZE) |
| 2692 | sense_data_length = SCSI_SENSE_BUFFERSIZE; |
| 2693 | memcpy(scmd->sense_buffer, error_info->data, |
| 2694 | sense_data_length); |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | if (device_offline && sense_data_length == 0) |
| 2699 | scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR, |
| 2700 | 0x3e, 0x1); |
| 2701 | |
| 2702 | scmd->result = scsi_status; |
| 2703 | set_host_byte(scmd, host_byte); |
| 2704 | } |
| 2705 | |
| 2706 | static void pqi_process_io_error(unsigned int iu_type, |
| 2707 | struct pqi_io_request *io_request) |
| 2708 | { |
| 2709 | switch (iu_type) { |
| 2710 | case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR: |
| 2711 | pqi_process_raid_io_error(io_request); |
| 2712 | break; |
| 2713 | case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR: |
| 2714 | pqi_process_aio_io_error(io_request); |
| 2715 | break; |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | static int pqi_interpret_task_management_response( |
| 2720 | struct pqi_task_management_response *response) |
| 2721 | { |
| 2722 | int rc; |
| 2723 | |
| 2724 | switch (response->response_code) { |
Kevin Barnett | b17f048 | 2016-08-31 14:54:17 -0500 | [diff] [blame] | 2725 | case SOP_TMF_COMPLETE: |
| 2726 | case SOP_TMF_FUNCTION_SUCCEEDED: |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2727 | rc = 0; |
| 2728 | break; |
Mahesh Rajashekhara | 3406384 | 2018-12-07 16:28:16 -0600 | [diff] [blame] | 2729 | case SOP_TMF_REJECTED: |
| 2730 | rc = -EAGAIN; |
| 2731 | break; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2732 | default: |
| 2733 | rc = -EIO; |
| 2734 | break; |
| 2735 | } |
| 2736 | |
| 2737 | return rc; |
| 2738 | } |
| 2739 | |
| 2740 | static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info, |
| 2741 | struct pqi_queue_group *queue_group) |
| 2742 | { |
| 2743 | unsigned int num_responses; |
| 2744 | pqi_index_t oq_pi; |
| 2745 | pqi_index_t oq_ci; |
| 2746 | struct pqi_io_request *io_request; |
| 2747 | struct pqi_io_response *response; |
| 2748 | u16 request_id; |
| 2749 | |
| 2750 | num_responses = 0; |
| 2751 | oq_ci = queue_group->oq_ci_copy; |
| 2752 | |
| 2753 | while (1) { |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 2754 | oq_pi = readl(queue_group->oq_pi); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2755 | if (oq_pi == oq_ci) |
| 2756 | break; |
| 2757 | |
| 2758 | num_responses++; |
| 2759 | response = queue_group->oq_element_array + |
| 2760 | (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH); |
| 2761 | |
| 2762 | request_id = get_unaligned_le16(&response->request_id); |
| 2763 | WARN_ON(request_id >= ctrl_info->max_io_slots); |
| 2764 | |
| 2765 | io_request = &ctrl_info->io_request_pool[request_id]; |
| 2766 | WARN_ON(atomic_read(&io_request->refcount) == 0); |
| 2767 | |
| 2768 | switch (response->header.iu_type) { |
| 2769 | case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS: |
| 2770 | case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS: |
| 2771 | case PQI_RESPONSE_IU_GENERAL_MANAGEMENT: |
| 2772 | break; |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 2773 | case PQI_RESPONSE_IU_VENDOR_GENERAL: |
| 2774 | io_request->status = |
| 2775 | get_unaligned_le16( |
| 2776 | &((struct pqi_vendor_general_response *) |
| 2777 | response)->status); |
| 2778 | break; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2779 | case PQI_RESPONSE_IU_TASK_MANAGEMENT: |
| 2780 | io_request->status = |
| 2781 | pqi_interpret_task_management_response( |
| 2782 | (void *)response); |
| 2783 | break; |
| 2784 | case PQI_RESPONSE_IU_AIO_PATH_DISABLED: |
| 2785 | pqi_aio_path_disabled(io_request); |
| 2786 | io_request->status = -EAGAIN; |
| 2787 | break; |
| 2788 | case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR: |
| 2789 | case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR: |
| 2790 | io_request->error_info = ctrl_info->error_buffer + |
| 2791 | (get_unaligned_le16(&response->error_index) * |
| 2792 | PQI_ERROR_BUFFER_ELEMENT_LENGTH); |
| 2793 | pqi_process_io_error(response->header.iu_type, |
| 2794 | io_request); |
| 2795 | break; |
| 2796 | default: |
| 2797 | dev_err(&ctrl_info->pci_dev->dev, |
| 2798 | "unexpected IU type: 0x%x\n", |
| 2799 | response->header.iu_type); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2800 | break; |
| 2801 | } |
| 2802 | |
| 2803 | io_request->io_complete_callback(io_request, |
| 2804 | io_request->context); |
| 2805 | |
| 2806 | /* |
| 2807 | * Note that the I/O request structure CANNOT BE TOUCHED after |
| 2808 | * returning from the I/O completion callback! |
| 2809 | */ |
| 2810 | |
| 2811 | oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq; |
| 2812 | } |
| 2813 | |
| 2814 | if (num_responses) { |
| 2815 | queue_group->oq_ci_copy = oq_ci; |
| 2816 | writel(oq_ci, queue_group->oq_ci); |
| 2817 | } |
| 2818 | |
| 2819 | return num_responses; |
| 2820 | } |
| 2821 | |
| 2822 | static inline unsigned int pqi_num_elements_free(unsigned int pi, |
Kevin Barnett | df7a1fc | 2016-08-31 14:54:59 -0500 | [diff] [blame] | 2823 | unsigned int ci, unsigned int elements_in_queue) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2824 | { |
| 2825 | unsigned int num_elements_used; |
| 2826 | |
| 2827 | if (pi >= ci) |
| 2828 | num_elements_used = pi - ci; |
| 2829 | else |
| 2830 | num_elements_used = elements_in_queue - ci + pi; |
| 2831 | |
| 2832 | return elements_in_queue - num_elements_used - 1; |
| 2833 | } |
| 2834 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2835 | static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2836 | struct pqi_event_acknowledge_request *iu, size_t iu_length) |
| 2837 | { |
| 2838 | pqi_index_t iq_pi; |
| 2839 | pqi_index_t iq_ci; |
| 2840 | unsigned long flags; |
| 2841 | void *next_element; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2842 | struct pqi_queue_group *queue_group; |
| 2843 | |
| 2844 | queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP]; |
| 2845 | put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id); |
| 2846 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2847 | while (1) { |
| 2848 | spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags); |
| 2849 | |
| 2850 | iq_pi = queue_group->iq_pi_copy[RAID_PATH]; |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 2851 | iq_ci = readl(queue_group->iq_ci[RAID_PATH]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2852 | |
| 2853 | if (pqi_num_elements_free(iq_pi, iq_ci, |
| 2854 | ctrl_info->num_elements_per_iq)) |
| 2855 | break; |
| 2856 | |
| 2857 | spin_unlock_irqrestore( |
| 2858 | &queue_group->submit_lock[RAID_PATH], flags); |
| 2859 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2860 | if (pqi_ctrl_offline(ctrl_info)) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2861 | return; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | next_element = queue_group->iq_element_array[RAID_PATH] + |
| 2865 | (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 2866 | |
| 2867 | memcpy(next_element, iu, iu_length); |
| 2868 | |
| 2869 | iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2870 | queue_group->iq_pi_copy[RAID_PATH] = iq_pi; |
| 2871 | |
| 2872 | /* |
| 2873 | * This write notifies the controller that an IU is available to be |
| 2874 | * processed. |
| 2875 | */ |
| 2876 | writel(iq_pi, queue_group->iq_pi[RAID_PATH]); |
| 2877 | |
| 2878 | spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info, |
| 2882 | struct pqi_event *event) |
| 2883 | { |
| 2884 | struct pqi_event_acknowledge_request request; |
| 2885 | |
| 2886 | memset(&request, 0, sizeof(request)); |
| 2887 | |
| 2888 | request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT; |
| 2889 | put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH, |
| 2890 | &request.header.iu_length); |
| 2891 | request.event_type = event->event_type; |
| 2892 | request.event_id = event->event_id; |
| 2893 | request.additional_event_id = event->additional_event_id; |
| 2894 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2895 | pqi_send_event_ack(ctrl_info, &request, sizeof(request)); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | static void pqi_event_worker(struct work_struct *work) |
| 2899 | { |
| 2900 | unsigned int i; |
| 2901 | struct pqi_ctrl_info *ctrl_info; |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 2902 | struct pqi_event *event; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2903 | |
| 2904 | ctrl_info = container_of(work, struct pqi_ctrl_info, event_work); |
| 2905 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 2906 | pqi_ctrl_busy(ctrl_info); |
| 2907 | pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT); |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 2908 | if (pqi_ctrl_offline(ctrl_info)) |
| 2909 | goto out; |
| 2910 | |
| 2911 | pqi_schedule_rescan_worker_delayed(ctrl_info); |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 2912 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 2913 | event = ctrl_info->events; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2914 | for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) { |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 2915 | if (event->pending) { |
| 2916 | event->pending = false; |
| 2917 | pqi_acknowledge_event(ctrl_info, event); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2918 | } |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 2919 | event++; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2920 | } |
| 2921 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 2922 | out: |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 2923 | pqi_ctrl_unbusy(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2924 | } |
| 2925 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2926 | #define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2927 | |
Kees Cook | 74a0f57 | 2017-10-11 16:27:10 -0700 | [diff] [blame] | 2928 | static void pqi_heartbeat_timer_handler(struct timer_list *t) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2929 | { |
| 2930 | int num_interrupts; |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2931 | u32 heartbeat_count; |
Kees Cook | 74a0f57 | 2017-10-11 16:27:10 -0700 | [diff] [blame] | 2932 | struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t, |
| 2933 | heartbeat_timer); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2934 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2935 | pqi_check_ctrl_health(ctrl_info); |
| 2936 | if (pqi_ctrl_offline(ctrl_info)) |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 2937 | return; |
| 2938 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2939 | num_interrupts = atomic_read(&ctrl_info->num_interrupts); |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2940 | heartbeat_count = pqi_read_heartbeat_counter(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2941 | |
| 2942 | if (num_interrupts == ctrl_info->previous_num_interrupts) { |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2943 | if (heartbeat_count == ctrl_info->previous_heartbeat_count) { |
| 2944 | dev_err(&ctrl_info->pci_dev->dev, |
| 2945 | "no heartbeat detected - last heartbeat count: %u\n", |
| 2946 | heartbeat_count); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2947 | pqi_take_ctrl_offline(ctrl_info); |
| 2948 | return; |
| 2949 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2950 | } else { |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2951 | ctrl_info->previous_num_interrupts = num_interrupts; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2952 | } |
| 2953 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2954 | ctrl_info->previous_heartbeat_count = heartbeat_count; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2955 | mod_timer(&ctrl_info->heartbeat_timer, |
| 2956 | jiffies + PQI_HEARTBEAT_TIMER_INTERVAL); |
| 2957 | } |
| 2958 | |
| 2959 | static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info) |
| 2960 | { |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2961 | if (!ctrl_info->heartbeat_counter) |
| 2962 | return; |
| 2963 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2964 | ctrl_info->previous_num_interrupts = |
| 2965 | atomic_read(&ctrl_info->num_interrupts); |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2966 | ctrl_info->previous_heartbeat_count = |
| 2967 | pqi_read_heartbeat_counter(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2968 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2969 | ctrl_info->heartbeat_timer.expires = |
| 2970 | jiffies + PQI_HEARTBEAT_TIMER_INTERVAL; |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 2971 | add_timer(&ctrl_info->heartbeat_timer); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info) |
| 2975 | { |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 2976 | del_timer_sync(&ctrl_info->heartbeat_timer); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2977 | } |
| 2978 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 2979 | static inline int pqi_event_type_to_event_index(unsigned int event_type) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2980 | { |
| 2981 | int index; |
| 2982 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 2983 | for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++) |
| 2984 | if (event_type == pqi_supported_event_types[index]) |
| 2985 | return index; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2986 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 2987 | return -1; |
| 2988 | } |
| 2989 | |
| 2990 | static inline bool pqi_is_supported_event(unsigned int event_type) |
| 2991 | { |
| 2992 | return pqi_event_type_to_event_index(event_type) != -1; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info) |
| 2996 | { |
| 2997 | unsigned int num_events; |
| 2998 | pqi_index_t oq_pi; |
| 2999 | pqi_index_t oq_ci; |
| 3000 | struct pqi_event_queue *event_queue; |
| 3001 | struct pqi_event_response *response; |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 3002 | struct pqi_event *event; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3003 | int event_index; |
| 3004 | |
| 3005 | event_queue = &ctrl_info->event_queue; |
| 3006 | num_events = 0; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3007 | oq_ci = event_queue->oq_ci_copy; |
| 3008 | |
| 3009 | while (1) { |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3010 | oq_pi = readl(event_queue->oq_pi); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3011 | if (oq_pi == oq_ci) |
| 3012 | break; |
| 3013 | |
| 3014 | num_events++; |
| 3015 | response = event_queue->oq_element_array + |
| 3016 | (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH); |
| 3017 | |
| 3018 | event_index = |
| 3019 | pqi_event_type_to_event_index(response->event_type); |
| 3020 | |
| 3021 | if (event_index >= 0) { |
| 3022 | if (response->request_acknowlege) { |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 3023 | event = &ctrl_info->events[event_index]; |
| 3024 | event->pending = true; |
| 3025 | event->event_type = response->event_type; |
| 3026 | event->event_id = response->event_id; |
| 3027 | event->additional_event_id = |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3028 | response->additional_event_id; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS; |
| 3033 | } |
| 3034 | |
| 3035 | if (num_events) { |
| 3036 | event_queue->oq_ci_copy = oq_ci; |
| 3037 | writel(oq_ci, event_queue->oq_ci); |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 3038 | schedule_work(&ctrl_info->event_work); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | return num_events; |
| 3042 | } |
| 3043 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 3044 | #define PQI_LEGACY_INTX_MASK 0x1 |
| 3045 | |
| 3046 | static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info, |
| 3047 | bool enable_intx) |
| 3048 | { |
| 3049 | u32 intx_mask; |
| 3050 | struct pqi_device_registers __iomem *pqi_registers; |
| 3051 | volatile void __iomem *register_addr; |
| 3052 | |
| 3053 | pqi_registers = ctrl_info->pqi_registers; |
| 3054 | |
| 3055 | if (enable_intx) |
| 3056 | register_addr = &pqi_registers->legacy_intx_mask_clear; |
| 3057 | else |
| 3058 | register_addr = &pqi_registers->legacy_intx_mask_set; |
| 3059 | |
| 3060 | intx_mask = readl(register_addr); |
| 3061 | intx_mask |= PQI_LEGACY_INTX_MASK; |
| 3062 | writel(intx_mask, register_addr); |
| 3063 | } |
| 3064 | |
| 3065 | static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info, |
| 3066 | enum pqi_irq_mode new_mode) |
| 3067 | { |
| 3068 | switch (ctrl_info->irq_mode) { |
| 3069 | case IRQ_MODE_MSIX: |
| 3070 | switch (new_mode) { |
| 3071 | case IRQ_MODE_MSIX: |
| 3072 | break; |
| 3073 | case IRQ_MODE_INTX: |
| 3074 | pqi_configure_legacy_intx(ctrl_info, true); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 3075 | sis_enable_intx(ctrl_info); |
| 3076 | break; |
| 3077 | case IRQ_MODE_NONE: |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 3078 | break; |
| 3079 | } |
| 3080 | break; |
| 3081 | case IRQ_MODE_INTX: |
| 3082 | switch (new_mode) { |
| 3083 | case IRQ_MODE_MSIX: |
| 3084 | pqi_configure_legacy_intx(ctrl_info, false); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 3085 | sis_enable_msix(ctrl_info); |
| 3086 | break; |
| 3087 | case IRQ_MODE_INTX: |
| 3088 | break; |
| 3089 | case IRQ_MODE_NONE: |
| 3090 | pqi_configure_legacy_intx(ctrl_info, false); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | break; |
| 3094 | case IRQ_MODE_NONE: |
| 3095 | switch (new_mode) { |
| 3096 | case IRQ_MODE_MSIX: |
| 3097 | sis_enable_msix(ctrl_info); |
| 3098 | break; |
| 3099 | case IRQ_MODE_INTX: |
| 3100 | pqi_configure_legacy_intx(ctrl_info, true); |
| 3101 | sis_enable_intx(ctrl_info); |
| 3102 | break; |
| 3103 | case IRQ_MODE_NONE: |
| 3104 | break; |
| 3105 | } |
| 3106 | break; |
| 3107 | } |
| 3108 | |
| 3109 | ctrl_info->irq_mode = new_mode; |
| 3110 | } |
| 3111 | |
| 3112 | #define PQI_LEGACY_INTX_PENDING 0x1 |
| 3113 | |
| 3114 | static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info) |
| 3115 | { |
| 3116 | bool valid_irq; |
| 3117 | u32 intx_status; |
| 3118 | |
| 3119 | switch (ctrl_info->irq_mode) { |
| 3120 | case IRQ_MODE_MSIX: |
| 3121 | valid_irq = true; |
| 3122 | break; |
| 3123 | case IRQ_MODE_INTX: |
| 3124 | intx_status = |
| 3125 | readl(&ctrl_info->pqi_registers->legacy_intx_status); |
| 3126 | if (intx_status & PQI_LEGACY_INTX_PENDING) |
| 3127 | valid_irq = true; |
| 3128 | else |
| 3129 | valid_irq = false; |
| 3130 | break; |
| 3131 | case IRQ_MODE_NONE: |
| 3132 | default: |
| 3133 | valid_irq = false; |
| 3134 | break; |
| 3135 | } |
| 3136 | |
| 3137 | return valid_irq; |
| 3138 | } |
| 3139 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3140 | static irqreturn_t pqi_irq_handler(int irq, void *data) |
| 3141 | { |
| 3142 | struct pqi_ctrl_info *ctrl_info; |
| 3143 | struct pqi_queue_group *queue_group; |
| 3144 | unsigned int num_responses_handled; |
| 3145 | |
| 3146 | queue_group = data; |
| 3147 | ctrl_info = queue_group->ctrl_info; |
| 3148 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 3149 | if (!pqi_is_valid_irq(ctrl_info)) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3150 | return IRQ_NONE; |
| 3151 | |
| 3152 | num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group); |
| 3153 | |
| 3154 | if (irq == ctrl_info->event_irq) |
| 3155 | num_responses_handled += pqi_process_event_intr(ctrl_info); |
| 3156 | |
| 3157 | if (num_responses_handled) |
| 3158 | atomic_inc(&ctrl_info->num_interrupts); |
| 3159 | |
| 3160 | pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL); |
| 3161 | pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL); |
| 3162 | |
| 3163 | return IRQ_HANDLED; |
| 3164 | } |
| 3165 | |
| 3166 | static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info) |
| 3167 | { |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 3168 | struct pci_dev *pci_dev = ctrl_info->pci_dev; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3169 | int i; |
| 3170 | int rc; |
| 3171 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 3172 | ctrl_info->event_irq = pci_irq_vector(pci_dev, 0); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3173 | |
| 3174 | for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) { |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 3175 | rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0, |
Christoph Hellwig | 5219822 | 2016-11-01 08:12:49 -0600 | [diff] [blame] | 3176 | DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3177 | if (rc) { |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 3178 | dev_err(&pci_dev->dev, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3179 | "irq %u init failed with error %d\n", |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 3180 | pci_irq_vector(pci_dev, i), rc); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3181 | return rc; |
| 3182 | } |
| 3183 | ctrl_info->num_msix_vectors_initialized++; |
| 3184 | } |
| 3185 | |
| 3186 | return 0; |
| 3187 | } |
| 3188 | |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 3189 | static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info) |
| 3190 | { |
| 3191 | int i; |
| 3192 | |
| 3193 | for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++) |
| 3194 | free_irq(pci_irq_vector(ctrl_info->pci_dev, i), |
| 3195 | &ctrl_info->queue_groups[i]); |
| 3196 | |
| 3197 | ctrl_info->num_msix_vectors_initialized = 0; |
| 3198 | } |
| 3199 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3200 | static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info) |
| 3201 | { |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 3202 | int num_vectors_enabled; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3203 | |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 3204 | num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev, |
Christoph Hellwig | 5219822 | 2016-11-01 08:12:49 -0600 | [diff] [blame] | 3205 | PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups, |
| 3206 | PCI_IRQ_MSIX | PCI_IRQ_AFFINITY); |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 3207 | if (num_vectors_enabled < 0) { |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3208 | dev_err(&ctrl_info->pci_dev->dev, |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 3209 | "MSI-X init failed with error %d\n", |
| 3210 | num_vectors_enabled); |
| 3211 | return num_vectors_enabled; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3212 | } |
| 3213 | |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 3214 | ctrl_info->num_msix_vectors_enabled = num_vectors_enabled; |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 3215 | ctrl_info->irq_mode = IRQ_MODE_MSIX; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3216 | return 0; |
| 3217 | } |
| 3218 | |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 3219 | static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info) |
| 3220 | { |
| 3221 | if (ctrl_info->num_msix_vectors_enabled) { |
| 3222 | pci_free_irq_vectors(ctrl_info->pci_dev); |
| 3223 | ctrl_info->num_msix_vectors_enabled = 0; |
| 3224 | } |
| 3225 | } |
| 3226 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3227 | static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info) |
| 3228 | { |
| 3229 | unsigned int i; |
| 3230 | size_t alloc_length; |
| 3231 | size_t element_array_length_per_iq; |
| 3232 | size_t element_array_length_per_oq; |
| 3233 | void *element_array; |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3234 | void __iomem *next_queue_index; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3235 | void *aligned_pointer; |
| 3236 | unsigned int num_inbound_queues; |
| 3237 | unsigned int num_outbound_queues; |
| 3238 | unsigned int num_queue_indexes; |
| 3239 | struct pqi_queue_group *queue_group; |
| 3240 | |
| 3241 | element_array_length_per_iq = |
| 3242 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH * |
| 3243 | ctrl_info->num_elements_per_iq; |
| 3244 | element_array_length_per_oq = |
| 3245 | PQI_OPERATIONAL_OQ_ELEMENT_LENGTH * |
| 3246 | ctrl_info->num_elements_per_oq; |
| 3247 | num_inbound_queues = ctrl_info->num_queue_groups * 2; |
| 3248 | num_outbound_queues = ctrl_info->num_queue_groups; |
| 3249 | num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1; |
| 3250 | |
| 3251 | aligned_pointer = NULL; |
| 3252 | |
| 3253 | for (i = 0; i < num_inbound_queues; i++) { |
| 3254 | aligned_pointer = PTR_ALIGN(aligned_pointer, |
| 3255 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3256 | aligned_pointer += element_array_length_per_iq; |
| 3257 | } |
| 3258 | |
| 3259 | for (i = 0; i < num_outbound_queues; i++) { |
| 3260 | aligned_pointer = PTR_ALIGN(aligned_pointer, |
| 3261 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3262 | aligned_pointer += element_array_length_per_oq; |
| 3263 | } |
| 3264 | |
| 3265 | aligned_pointer = PTR_ALIGN(aligned_pointer, |
| 3266 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3267 | aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS * |
| 3268 | PQI_EVENT_OQ_ELEMENT_LENGTH; |
| 3269 | |
| 3270 | for (i = 0; i < num_queue_indexes; i++) { |
| 3271 | aligned_pointer = PTR_ALIGN(aligned_pointer, |
| 3272 | PQI_OPERATIONAL_INDEX_ALIGNMENT); |
| 3273 | aligned_pointer += sizeof(pqi_index_t); |
| 3274 | } |
| 3275 | |
| 3276 | alloc_length = (size_t)aligned_pointer + |
| 3277 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT; |
| 3278 | |
Kevin Barnett | e1d213b | 2017-05-03 18:53:18 -0500 | [diff] [blame] | 3279 | alloc_length += PQI_EXTRA_SGL_MEMORY; |
| 3280 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3281 | ctrl_info->queue_memory_base = |
| 3282 | dma_zalloc_coherent(&ctrl_info->pci_dev->dev, |
| 3283 | alloc_length, |
| 3284 | &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL); |
| 3285 | |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 3286 | if (!ctrl_info->queue_memory_base) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3287 | return -ENOMEM; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3288 | |
| 3289 | ctrl_info->queue_memory_length = alloc_length; |
| 3290 | |
| 3291 | element_array = PTR_ALIGN(ctrl_info->queue_memory_base, |
| 3292 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3293 | |
| 3294 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 3295 | queue_group = &ctrl_info->queue_groups[i]; |
| 3296 | queue_group->iq_element_array[RAID_PATH] = element_array; |
| 3297 | queue_group->iq_element_array_bus_addr[RAID_PATH] = |
| 3298 | ctrl_info->queue_memory_base_dma_handle + |
| 3299 | (element_array - ctrl_info->queue_memory_base); |
| 3300 | element_array += element_array_length_per_iq; |
| 3301 | element_array = PTR_ALIGN(element_array, |
| 3302 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3303 | queue_group->iq_element_array[AIO_PATH] = element_array; |
| 3304 | queue_group->iq_element_array_bus_addr[AIO_PATH] = |
| 3305 | ctrl_info->queue_memory_base_dma_handle + |
| 3306 | (element_array - ctrl_info->queue_memory_base); |
| 3307 | element_array += element_array_length_per_iq; |
| 3308 | element_array = PTR_ALIGN(element_array, |
| 3309 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3310 | } |
| 3311 | |
| 3312 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 3313 | queue_group = &ctrl_info->queue_groups[i]; |
| 3314 | queue_group->oq_element_array = element_array; |
| 3315 | queue_group->oq_element_array_bus_addr = |
| 3316 | ctrl_info->queue_memory_base_dma_handle + |
| 3317 | (element_array - ctrl_info->queue_memory_base); |
| 3318 | element_array += element_array_length_per_oq; |
| 3319 | element_array = PTR_ALIGN(element_array, |
| 3320 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3321 | } |
| 3322 | |
| 3323 | ctrl_info->event_queue.oq_element_array = element_array; |
| 3324 | ctrl_info->event_queue.oq_element_array_bus_addr = |
| 3325 | ctrl_info->queue_memory_base_dma_handle + |
| 3326 | (element_array - ctrl_info->queue_memory_base); |
| 3327 | element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS * |
| 3328 | PQI_EVENT_OQ_ELEMENT_LENGTH; |
| 3329 | |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3330 | next_queue_index = (void __iomem *)PTR_ALIGN(element_array, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3331 | PQI_OPERATIONAL_INDEX_ALIGNMENT); |
| 3332 | |
| 3333 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 3334 | queue_group = &ctrl_info->queue_groups[i]; |
| 3335 | queue_group->iq_ci[RAID_PATH] = next_queue_index; |
| 3336 | queue_group->iq_ci_bus_addr[RAID_PATH] = |
| 3337 | ctrl_info->queue_memory_base_dma_handle + |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3338 | (next_queue_index - |
| 3339 | (void __iomem *)ctrl_info->queue_memory_base); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3340 | next_queue_index += sizeof(pqi_index_t); |
| 3341 | next_queue_index = PTR_ALIGN(next_queue_index, |
| 3342 | PQI_OPERATIONAL_INDEX_ALIGNMENT); |
| 3343 | queue_group->iq_ci[AIO_PATH] = next_queue_index; |
| 3344 | queue_group->iq_ci_bus_addr[AIO_PATH] = |
| 3345 | ctrl_info->queue_memory_base_dma_handle + |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3346 | (next_queue_index - |
| 3347 | (void __iomem *)ctrl_info->queue_memory_base); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3348 | next_queue_index += sizeof(pqi_index_t); |
| 3349 | next_queue_index = PTR_ALIGN(next_queue_index, |
| 3350 | PQI_OPERATIONAL_INDEX_ALIGNMENT); |
| 3351 | queue_group->oq_pi = next_queue_index; |
| 3352 | queue_group->oq_pi_bus_addr = |
| 3353 | ctrl_info->queue_memory_base_dma_handle + |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3354 | (next_queue_index - |
| 3355 | (void __iomem *)ctrl_info->queue_memory_base); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3356 | next_queue_index += sizeof(pqi_index_t); |
| 3357 | next_queue_index = PTR_ALIGN(next_queue_index, |
| 3358 | PQI_OPERATIONAL_INDEX_ALIGNMENT); |
| 3359 | } |
| 3360 | |
| 3361 | ctrl_info->event_queue.oq_pi = next_queue_index; |
| 3362 | ctrl_info->event_queue.oq_pi_bus_addr = |
| 3363 | ctrl_info->queue_memory_base_dma_handle + |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3364 | (next_queue_index - |
| 3365 | (void __iomem *)ctrl_info->queue_memory_base); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3366 | |
| 3367 | return 0; |
| 3368 | } |
| 3369 | |
| 3370 | static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info) |
| 3371 | { |
| 3372 | unsigned int i; |
| 3373 | u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID; |
| 3374 | u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID; |
| 3375 | |
| 3376 | /* |
| 3377 | * Initialize the backpointers to the controller structure in |
| 3378 | * each operational queue group structure. |
| 3379 | */ |
| 3380 | for (i = 0; i < ctrl_info->num_queue_groups; i++) |
| 3381 | ctrl_info->queue_groups[i].ctrl_info = ctrl_info; |
| 3382 | |
| 3383 | /* |
| 3384 | * Assign IDs to all operational queues. Note that the IDs |
| 3385 | * assigned to operational IQs are independent of the IDs |
| 3386 | * assigned to operational OQs. |
| 3387 | */ |
| 3388 | ctrl_info->event_queue.oq_id = next_oq_id++; |
| 3389 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 3390 | ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++; |
| 3391 | ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++; |
| 3392 | ctrl_info->queue_groups[i].oq_id = next_oq_id++; |
| 3393 | } |
| 3394 | |
| 3395 | /* |
| 3396 | * Assign MSI-X table entry indexes to all queues. Note that the |
| 3397 | * interrupt for the event queue is shared with the first queue group. |
| 3398 | */ |
| 3399 | ctrl_info->event_queue.int_msg_num = 0; |
| 3400 | for (i = 0; i < ctrl_info->num_queue_groups; i++) |
| 3401 | ctrl_info->queue_groups[i].int_msg_num = i; |
| 3402 | |
| 3403 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 3404 | spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]); |
| 3405 | spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]); |
| 3406 | INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]); |
| 3407 | INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]); |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info) |
| 3412 | { |
| 3413 | size_t alloc_length; |
| 3414 | struct pqi_admin_queues_aligned *admin_queues_aligned; |
| 3415 | struct pqi_admin_queues *admin_queues; |
| 3416 | |
| 3417 | alloc_length = sizeof(struct pqi_admin_queues_aligned) + |
| 3418 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT; |
| 3419 | |
| 3420 | ctrl_info->admin_queue_memory_base = |
| 3421 | dma_zalloc_coherent(&ctrl_info->pci_dev->dev, |
| 3422 | alloc_length, |
| 3423 | &ctrl_info->admin_queue_memory_base_dma_handle, |
| 3424 | GFP_KERNEL); |
| 3425 | |
| 3426 | if (!ctrl_info->admin_queue_memory_base) |
| 3427 | return -ENOMEM; |
| 3428 | |
| 3429 | ctrl_info->admin_queue_memory_length = alloc_length; |
| 3430 | |
| 3431 | admin_queues = &ctrl_info->admin_queues; |
| 3432 | admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base, |
| 3433 | PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT); |
| 3434 | admin_queues->iq_element_array = |
| 3435 | &admin_queues_aligned->iq_element_array; |
| 3436 | admin_queues->oq_element_array = |
| 3437 | &admin_queues_aligned->oq_element_array; |
| 3438 | admin_queues->iq_ci = &admin_queues_aligned->iq_ci; |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3439 | admin_queues->oq_pi = |
| 3440 | (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3441 | |
| 3442 | admin_queues->iq_element_array_bus_addr = |
| 3443 | ctrl_info->admin_queue_memory_base_dma_handle + |
| 3444 | (admin_queues->iq_element_array - |
| 3445 | ctrl_info->admin_queue_memory_base); |
| 3446 | admin_queues->oq_element_array_bus_addr = |
| 3447 | ctrl_info->admin_queue_memory_base_dma_handle + |
| 3448 | (admin_queues->oq_element_array - |
| 3449 | ctrl_info->admin_queue_memory_base); |
| 3450 | admin_queues->iq_ci_bus_addr = |
| 3451 | ctrl_info->admin_queue_memory_base_dma_handle + |
| 3452 | ((void *)admin_queues->iq_ci - |
| 3453 | ctrl_info->admin_queue_memory_base); |
| 3454 | admin_queues->oq_pi_bus_addr = |
| 3455 | ctrl_info->admin_queue_memory_base_dma_handle + |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3456 | ((void __iomem *)admin_queues->oq_pi - |
| 3457 | (void __iomem *)ctrl_info->admin_queue_memory_base); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3458 | |
| 3459 | return 0; |
| 3460 | } |
| 3461 | |
| 3462 | #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ |
| 3463 | #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1 |
| 3464 | |
| 3465 | static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info) |
| 3466 | { |
| 3467 | struct pqi_device_registers __iomem *pqi_registers; |
| 3468 | struct pqi_admin_queues *admin_queues; |
| 3469 | unsigned long timeout; |
| 3470 | u8 status; |
| 3471 | u32 reg; |
| 3472 | |
| 3473 | pqi_registers = ctrl_info->pqi_registers; |
| 3474 | admin_queues = &ctrl_info->admin_queues; |
| 3475 | |
| 3476 | writeq((u64)admin_queues->iq_element_array_bus_addr, |
| 3477 | &pqi_registers->admin_iq_element_array_addr); |
| 3478 | writeq((u64)admin_queues->oq_element_array_bus_addr, |
| 3479 | &pqi_registers->admin_oq_element_array_addr); |
| 3480 | writeq((u64)admin_queues->iq_ci_bus_addr, |
| 3481 | &pqi_registers->admin_iq_ci_addr); |
| 3482 | writeq((u64)admin_queues->oq_pi_bus_addr, |
| 3483 | &pqi_registers->admin_oq_pi_addr); |
| 3484 | |
| 3485 | reg = PQI_ADMIN_IQ_NUM_ELEMENTS | |
| 3486 | (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 | |
| 3487 | (admin_queues->int_msg_num << 16); |
| 3488 | writel(reg, &pqi_registers->admin_iq_num_elements); |
| 3489 | writel(PQI_CREATE_ADMIN_QUEUE_PAIR, |
| 3490 | &pqi_registers->function_and_status_code); |
| 3491 | |
| 3492 | timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies; |
| 3493 | while (1) { |
| 3494 | status = readb(&pqi_registers->function_and_status_code); |
| 3495 | if (status == PQI_STATUS_IDLE) |
| 3496 | break; |
| 3497 | if (time_after(jiffies, timeout)) |
| 3498 | return -ETIMEDOUT; |
| 3499 | msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS); |
| 3500 | } |
| 3501 | |
| 3502 | /* |
| 3503 | * The offset registers are not initialized to the correct |
| 3504 | * offsets until *after* the create admin queue pair command |
| 3505 | * completes successfully. |
| 3506 | */ |
| 3507 | admin_queues->iq_pi = ctrl_info->iomem_base + |
| 3508 | PQI_DEVICE_REGISTERS_OFFSET + |
| 3509 | readq(&pqi_registers->admin_iq_pi_offset); |
| 3510 | admin_queues->oq_ci = ctrl_info->iomem_base + |
| 3511 | PQI_DEVICE_REGISTERS_OFFSET + |
| 3512 | readq(&pqi_registers->admin_oq_ci_offset); |
| 3513 | |
| 3514 | return 0; |
| 3515 | } |
| 3516 | |
| 3517 | static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info, |
| 3518 | struct pqi_general_admin_request *request) |
| 3519 | { |
| 3520 | struct pqi_admin_queues *admin_queues; |
| 3521 | void *next_element; |
| 3522 | pqi_index_t iq_pi; |
| 3523 | |
| 3524 | admin_queues = &ctrl_info->admin_queues; |
| 3525 | iq_pi = admin_queues->iq_pi_copy; |
| 3526 | |
| 3527 | next_element = admin_queues->iq_element_array + |
| 3528 | (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH); |
| 3529 | |
| 3530 | memcpy(next_element, request, sizeof(*request)); |
| 3531 | |
| 3532 | iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS; |
| 3533 | admin_queues->iq_pi_copy = iq_pi; |
| 3534 | |
| 3535 | /* |
| 3536 | * This write notifies the controller that an IU is available to be |
| 3537 | * processed. |
| 3538 | */ |
| 3539 | writel(iq_pi, admin_queues->iq_pi); |
| 3540 | } |
| 3541 | |
Kevin Barnett | 13bede6 | 2017-05-03 18:55:13 -0500 | [diff] [blame] | 3542 | #define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60 |
| 3543 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3544 | static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info, |
| 3545 | struct pqi_general_admin_response *response) |
| 3546 | { |
| 3547 | struct pqi_admin_queues *admin_queues; |
| 3548 | pqi_index_t oq_pi; |
| 3549 | pqi_index_t oq_ci; |
| 3550 | unsigned long timeout; |
| 3551 | |
| 3552 | admin_queues = &ctrl_info->admin_queues; |
| 3553 | oq_ci = admin_queues->oq_ci_copy; |
| 3554 | |
Kevin Barnett | 13bede6 | 2017-05-03 18:55:13 -0500 | [diff] [blame] | 3555 | timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3556 | |
| 3557 | while (1) { |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3558 | oq_pi = readl(admin_queues->oq_pi); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3559 | if (oq_pi != oq_ci) |
| 3560 | break; |
| 3561 | if (time_after(jiffies, timeout)) { |
| 3562 | dev_err(&ctrl_info->pci_dev->dev, |
| 3563 | "timed out waiting for admin response\n"); |
| 3564 | return -ETIMEDOUT; |
| 3565 | } |
Kevin Barnett | 13bede6 | 2017-05-03 18:55:13 -0500 | [diff] [blame] | 3566 | if (!sis_is_firmware_running(ctrl_info)) |
| 3567 | return -ENXIO; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3568 | usleep_range(1000, 2000); |
| 3569 | } |
| 3570 | |
| 3571 | memcpy(response, admin_queues->oq_element_array + |
| 3572 | (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response)); |
| 3573 | |
| 3574 | oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS; |
| 3575 | admin_queues->oq_ci_copy = oq_ci; |
| 3576 | writel(oq_ci, admin_queues->oq_ci); |
| 3577 | |
| 3578 | return 0; |
| 3579 | } |
| 3580 | |
| 3581 | static void pqi_start_io(struct pqi_ctrl_info *ctrl_info, |
| 3582 | struct pqi_queue_group *queue_group, enum pqi_io_path path, |
| 3583 | struct pqi_io_request *io_request) |
| 3584 | { |
| 3585 | struct pqi_io_request *next; |
| 3586 | void *next_element; |
| 3587 | pqi_index_t iq_pi; |
| 3588 | pqi_index_t iq_ci; |
| 3589 | size_t iu_length; |
| 3590 | unsigned long flags; |
| 3591 | unsigned int num_elements_needed; |
| 3592 | unsigned int num_elements_to_end_of_queue; |
| 3593 | size_t copy_count; |
| 3594 | struct pqi_iu_header *request; |
| 3595 | |
| 3596 | spin_lock_irqsave(&queue_group->submit_lock[path], flags); |
| 3597 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 3598 | if (io_request) { |
| 3599 | io_request->queue_group = queue_group; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3600 | list_add_tail(&io_request->request_list_entry, |
| 3601 | &queue_group->request_list[path]); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 3602 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3603 | |
| 3604 | iq_pi = queue_group->iq_pi_copy[path]; |
| 3605 | |
| 3606 | list_for_each_entry_safe(io_request, next, |
| 3607 | &queue_group->request_list[path], request_list_entry) { |
| 3608 | |
| 3609 | request = io_request->iu; |
| 3610 | |
| 3611 | iu_length = get_unaligned_le16(&request->iu_length) + |
| 3612 | PQI_REQUEST_HEADER_LENGTH; |
| 3613 | num_elements_needed = |
| 3614 | DIV_ROUND_UP(iu_length, |
| 3615 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 3616 | |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 3617 | iq_ci = readl(queue_group->iq_ci[path]); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3618 | |
| 3619 | if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci, |
| 3620 | ctrl_info->num_elements_per_iq)) |
| 3621 | break; |
| 3622 | |
| 3623 | put_unaligned_le16(queue_group->oq_id, |
| 3624 | &request->response_queue_id); |
| 3625 | |
| 3626 | next_element = queue_group->iq_element_array[path] + |
| 3627 | (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 3628 | |
| 3629 | num_elements_to_end_of_queue = |
| 3630 | ctrl_info->num_elements_per_iq - iq_pi; |
| 3631 | |
| 3632 | if (num_elements_needed <= num_elements_to_end_of_queue) { |
| 3633 | memcpy(next_element, request, iu_length); |
| 3634 | } else { |
| 3635 | copy_count = num_elements_to_end_of_queue * |
| 3636 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH; |
| 3637 | memcpy(next_element, request, copy_count); |
| 3638 | memcpy(queue_group->iq_element_array[path], |
| 3639 | (u8 *)request + copy_count, |
| 3640 | iu_length - copy_count); |
| 3641 | } |
| 3642 | |
| 3643 | iq_pi = (iq_pi + num_elements_needed) % |
| 3644 | ctrl_info->num_elements_per_iq; |
| 3645 | |
| 3646 | list_del(&io_request->request_list_entry); |
| 3647 | } |
| 3648 | |
| 3649 | if (iq_pi != queue_group->iq_pi_copy[path]) { |
| 3650 | queue_group->iq_pi_copy[path] = iq_pi; |
| 3651 | /* |
| 3652 | * This write notifies the controller that one or more IUs are |
| 3653 | * available to be processed. |
| 3654 | */ |
| 3655 | writel(iq_pi, queue_group->iq_pi[path]); |
| 3656 | } |
| 3657 | |
| 3658 | spin_unlock_irqrestore(&queue_group->submit_lock[path], flags); |
| 3659 | } |
| 3660 | |
Kevin Barnett | 1f37e99 | 2017-05-03 18:53:24 -0500 | [diff] [blame] | 3661 | #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10 |
| 3662 | |
| 3663 | static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info, |
| 3664 | struct completion *wait) |
| 3665 | { |
| 3666 | int rc; |
Kevin Barnett | 1f37e99 | 2017-05-03 18:53:24 -0500 | [diff] [blame] | 3667 | |
| 3668 | while (1) { |
| 3669 | if (wait_for_completion_io_timeout(wait, |
| 3670 | PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) { |
| 3671 | rc = 0; |
| 3672 | break; |
| 3673 | } |
| 3674 | |
| 3675 | pqi_check_ctrl_health(ctrl_info); |
| 3676 | if (pqi_ctrl_offline(ctrl_info)) { |
| 3677 | rc = -ENXIO; |
| 3678 | break; |
| 3679 | } |
Kevin Barnett | 1f37e99 | 2017-05-03 18:53:24 -0500 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | return rc; |
| 3683 | } |
| 3684 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3685 | static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request, |
| 3686 | void *context) |
| 3687 | { |
| 3688 | struct completion *waiting = context; |
| 3689 | |
| 3690 | complete(waiting); |
| 3691 | } |
| 3692 | |
Kevin Barnett | 26b390a | 2018-06-18 13:22:48 -0500 | [diff] [blame] | 3693 | static int pqi_process_raid_io_error_synchronous(struct pqi_raid_error_info |
| 3694 | *error_info) |
| 3695 | { |
| 3696 | int rc = -EIO; |
| 3697 | |
| 3698 | switch (error_info->data_out_result) { |
| 3699 | case PQI_DATA_IN_OUT_GOOD: |
| 3700 | if (error_info->status == SAM_STAT_GOOD) |
| 3701 | rc = 0; |
| 3702 | break; |
| 3703 | case PQI_DATA_IN_OUT_UNDERFLOW: |
| 3704 | if (error_info->status == SAM_STAT_GOOD || |
| 3705 | error_info->status == SAM_STAT_CHECK_CONDITION) |
| 3706 | rc = 0; |
| 3707 | break; |
| 3708 | case PQI_DATA_IN_OUT_ABORTED: |
| 3709 | rc = PQI_CMD_STATUS_ABORTED; |
| 3710 | break; |
| 3711 | } |
| 3712 | |
| 3713 | return rc; |
| 3714 | } |
| 3715 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3716 | static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info, |
| 3717 | struct pqi_iu_header *request, unsigned int flags, |
| 3718 | struct pqi_raid_error_info *error_info, unsigned long timeout_msecs) |
| 3719 | { |
Kevin Barnett | 957c5ab | 2018-06-18 13:22:42 -0500 | [diff] [blame] | 3720 | int rc = 0; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3721 | struct pqi_io_request *io_request; |
| 3722 | unsigned long start_jiffies; |
| 3723 | unsigned long msecs_blocked; |
| 3724 | size_t iu_length; |
Kevin Barnett | 957c5ab | 2018-06-18 13:22:42 -0500 | [diff] [blame] | 3725 | DECLARE_COMPLETION_ONSTACK(wait); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3726 | |
| 3727 | /* |
| 3728 | * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value |
| 3729 | * are mutually exclusive. |
| 3730 | */ |
| 3731 | |
| 3732 | if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) { |
| 3733 | if (down_interruptible(&ctrl_info->sync_request_sem)) |
| 3734 | return -ERESTARTSYS; |
| 3735 | } else { |
| 3736 | if (timeout_msecs == NO_TIMEOUT) { |
| 3737 | down(&ctrl_info->sync_request_sem); |
| 3738 | } else { |
| 3739 | start_jiffies = jiffies; |
| 3740 | if (down_timeout(&ctrl_info->sync_request_sem, |
| 3741 | msecs_to_jiffies(timeout_msecs))) |
| 3742 | return -ETIMEDOUT; |
| 3743 | msecs_blocked = |
| 3744 | jiffies_to_msecs(jiffies - start_jiffies); |
| 3745 | if (msecs_blocked >= timeout_msecs) |
| 3746 | return -ETIMEDOUT; |
| 3747 | timeout_msecs -= msecs_blocked; |
| 3748 | } |
| 3749 | } |
| 3750 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 3751 | pqi_ctrl_busy(ctrl_info); |
| 3752 | timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs); |
| 3753 | if (timeout_msecs == 0) { |
Kevin Barnett | 957c5ab | 2018-06-18 13:22:42 -0500 | [diff] [blame] | 3754 | pqi_ctrl_unbusy(ctrl_info); |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 3755 | rc = -ETIMEDOUT; |
| 3756 | goto out; |
| 3757 | } |
| 3758 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 3759 | if (pqi_ctrl_offline(ctrl_info)) { |
Kevin Barnett | 957c5ab | 2018-06-18 13:22:42 -0500 | [diff] [blame] | 3760 | pqi_ctrl_unbusy(ctrl_info); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 3761 | rc = -ENXIO; |
| 3762 | goto out; |
| 3763 | } |
| 3764 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3765 | io_request = pqi_alloc_io_request(ctrl_info); |
| 3766 | |
| 3767 | put_unaligned_le16(io_request->index, |
| 3768 | &(((struct pqi_raid_path_request *)request)->request_id)); |
| 3769 | |
| 3770 | if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO) |
| 3771 | ((struct pqi_raid_path_request *)request)->error_index = |
| 3772 | ((struct pqi_raid_path_request *)request)->request_id; |
| 3773 | |
| 3774 | iu_length = get_unaligned_le16(&request->iu_length) + |
| 3775 | PQI_REQUEST_HEADER_LENGTH; |
| 3776 | memcpy(io_request->iu, request, iu_length); |
| 3777 | |
Kevin Barnett | 957c5ab | 2018-06-18 13:22:42 -0500 | [diff] [blame] | 3778 | io_request->io_complete_callback = pqi_raid_synchronous_complete; |
| 3779 | io_request->context = &wait; |
| 3780 | |
| 3781 | pqi_start_io(ctrl_info, |
| 3782 | &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH, |
| 3783 | io_request); |
| 3784 | |
| 3785 | pqi_ctrl_unbusy(ctrl_info); |
| 3786 | |
| 3787 | if (timeout_msecs == NO_TIMEOUT) { |
| 3788 | pqi_wait_for_completion_io(ctrl_info, &wait); |
| 3789 | } else { |
| 3790 | if (!wait_for_completion_io_timeout(&wait, |
| 3791 | msecs_to_jiffies(timeout_msecs))) { |
| 3792 | dev_warn(&ctrl_info->pci_dev->dev, |
| 3793 | "command timed out\n"); |
| 3794 | rc = -ETIMEDOUT; |
| 3795 | } |
| 3796 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3797 | |
| 3798 | if (error_info) { |
| 3799 | if (io_request->error_info) |
| 3800 | memcpy(error_info, io_request->error_info, |
| 3801 | sizeof(*error_info)); |
| 3802 | else |
| 3803 | memset(error_info, 0, sizeof(*error_info)); |
| 3804 | } else if (rc == 0 && io_request->error_info) { |
Kevin Barnett | 26b390a | 2018-06-18 13:22:48 -0500 | [diff] [blame] | 3805 | rc = pqi_process_raid_io_error_synchronous( |
| 3806 | io_request->error_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | pqi_free_io_request(io_request); |
| 3810 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 3811 | out: |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3812 | up(&ctrl_info->sync_request_sem); |
| 3813 | |
| 3814 | return rc; |
| 3815 | } |
| 3816 | |
| 3817 | static int pqi_validate_admin_response( |
| 3818 | struct pqi_general_admin_response *response, u8 expected_function_code) |
| 3819 | { |
| 3820 | if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN) |
| 3821 | return -EINVAL; |
| 3822 | |
| 3823 | if (get_unaligned_le16(&response->header.iu_length) != |
| 3824 | PQI_GENERAL_ADMIN_IU_LENGTH) |
| 3825 | return -EINVAL; |
| 3826 | |
| 3827 | if (response->function_code != expected_function_code) |
| 3828 | return -EINVAL; |
| 3829 | |
| 3830 | if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) |
| 3831 | return -EINVAL; |
| 3832 | |
| 3833 | return 0; |
| 3834 | } |
| 3835 | |
| 3836 | static int pqi_submit_admin_request_synchronous( |
| 3837 | struct pqi_ctrl_info *ctrl_info, |
| 3838 | struct pqi_general_admin_request *request, |
| 3839 | struct pqi_general_admin_response *response) |
| 3840 | { |
| 3841 | int rc; |
| 3842 | |
| 3843 | pqi_submit_admin_request(ctrl_info, request); |
| 3844 | |
| 3845 | rc = pqi_poll_for_admin_response(ctrl_info, response); |
| 3846 | |
| 3847 | if (rc == 0) |
| 3848 | rc = pqi_validate_admin_response(response, |
| 3849 | request->function_code); |
| 3850 | |
| 3851 | return rc; |
| 3852 | } |
| 3853 | |
| 3854 | static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info) |
| 3855 | { |
| 3856 | int rc; |
| 3857 | struct pqi_general_admin_request request; |
| 3858 | struct pqi_general_admin_response response; |
| 3859 | struct pqi_device_capability *capability; |
| 3860 | struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor; |
| 3861 | |
| 3862 | capability = kmalloc(sizeof(*capability), GFP_KERNEL); |
| 3863 | if (!capability) |
| 3864 | return -ENOMEM; |
| 3865 | |
| 3866 | memset(&request, 0, sizeof(request)); |
| 3867 | |
| 3868 | request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN; |
| 3869 | put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH, |
| 3870 | &request.header.iu_length); |
| 3871 | request.function_code = |
| 3872 | PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY; |
| 3873 | put_unaligned_le32(sizeof(*capability), |
| 3874 | &request.data.report_device_capability.buffer_length); |
| 3875 | |
| 3876 | rc = pqi_map_single(ctrl_info->pci_dev, |
| 3877 | &request.data.report_device_capability.sg_descriptor, |
| 3878 | capability, sizeof(*capability), |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 3879 | DMA_FROM_DEVICE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3880 | if (rc) |
| 3881 | goto out; |
| 3882 | |
| 3883 | rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, |
| 3884 | &response); |
| 3885 | |
| 3886 | pqi_pci_unmap(ctrl_info->pci_dev, |
| 3887 | &request.data.report_device_capability.sg_descriptor, 1, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 3888 | DMA_FROM_DEVICE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3889 | |
| 3890 | if (rc) |
| 3891 | goto out; |
| 3892 | |
| 3893 | if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) { |
| 3894 | rc = -EIO; |
| 3895 | goto out; |
| 3896 | } |
| 3897 | |
| 3898 | ctrl_info->max_inbound_queues = |
| 3899 | get_unaligned_le16(&capability->max_inbound_queues); |
| 3900 | ctrl_info->max_elements_per_iq = |
| 3901 | get_unaligned_le16(&capability->max_elements_per_iq); |
| 3902 | ctrl_info->max_iq_element_length = |
| 3903 | get_unaligned_le16(&capability->max_iq_element_length) |
| 3904 | * 16; |
| 3905 | ctrl_info->max_outbound_queues = |
| 3906 | get_unaligned_le16(&capability->max_outbound_queues); |
| 3907 | ctrl_info->max_elements_per_oq = |
| 3908 | get_unaligned_le16(&capability->max_elements_per_oq); |
| 3909 | ctrl_info->max_oq_element_length = |
| 3910 | get_unaligned_le16(&capability->max_oq_element_length) |
| 3911 | * 16; |
| 3912 | |
| 3913 | sop_iu_layer_descriptor = |
| 3914 | &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP]; |
| 3915 | |
| 3916 | ctrl_info->max_inbound_iu_length_per_firmware = |
| 3917 | get_unaligned_le16( |
| 3918 | &sop_iu_layer_descriptor->max_inbound_iu_length); |
| 3919 | ctrl_info->inbound_spanning_supported = |
| 3920 | sop_iu_layer_descriptor->inbound_spanning_supported; |
| 3921 | ctrl_info->outbound_spanning_supported = |
| 3922 | sop_iu_layer_descriptor->outbound_spanning_supported; |
| 3923 | |
| 3924 | out: |
| 3925 | kfree(capability); |
| 3926 | |
| 3927 | return rc; |
| 3928 | } |
| 3929 | |
| 3930 | static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info) |
| 3931 | { |
| 3932 | if (ctrl_info->max_iq_element_length < |
| 3933 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) { |
| 3934 | dev_err(&ctrl_info->pci_dev->dev, |
| 3935 | "max. inbound queue element length of %d is less than the required length of %d\n", |
| 3936 | ctrl_info->max_iq_element_length, |
| 3937 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 3938 | return -EINVAL; |
| 3939 | } |
| 3940 | |
| 3941 | if (ctrl_info->max_oq_element_length < |
| 3942 | PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) { |
| 3943 | dev_err(&ctrl_info->pci_dev->dev, |
| 3944 | "max. outbound queue element length of %d is less than the required length of %d\n", |
| 3945 | ctrl_info->max_oq_element_length, |
| 3946 | PQI_OPERATIONAL_OQ_ELEMENT_LENGTH); |
| 3947 | return -EINVAL; |
| 3948 | } |
| 3949 | |
| 3950 | if (ctrl_info->max_inbound_iu_length_per_firmware < |
| 3951 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) { |
| 3952 | dev_err(&ctrl_info->pci_dev->dev, |
| 3953 | "max. inbound IU length of %u is less than the min. required length of %d\n", |
| 3954 | ctrl_info->max_inbound_iu_length_per_firmware, |
| 3955 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 3956 | return -EINVAL; |
| 3957 | } |
| 3958 | |
Kevin Barnett | 77668f4 | 2016-08-31 14:54:23 -0500 | [diff] [blame] | 3959 | if (!ctrl_info->inbound_spanning_supported) { |
| 3960 | dev_err(&ctrl_info->pci_dev->dev, |
| 3961 | "the controller does not support inbound spanning\n"); |
| 3962 | return -EINVAL; |
| 3963 | } |
| 3964 | |
| 3965 | if (ctrl_info->outbound_spanning_supported) { |
| 3966 | dev_err(&ctrl_info->pci_dev->dev, |
| 3967 | "the controller supports outbound spanning but this driver does not\n"); |
| 3968 | return -EINVAL; |
| 3969 | } |
| 3970 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3971 | return 0; |
| 3972 | } |
| 3973 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 3974 | static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info) |
| 3975 | { |
| 3976 | int rc; |
| 3977 | struct pqi_event_queue *event_queue; |
| 3978 | struct pqi_general_admin_request request; |
| 3979 | struct pqi_general_admin_response response; |
| 3980 | |
| 3981 | event_queue = &ctrl_info->event_queue; |
| 3982 | |
| 3983 | /* |
| 3984 | * Create OQ (Outbound Queue - device to host queue) to dedicate |
| 3985 | * to events. |
| 3986 | */ |
| 3987 | memset(&request, 0, sizeof(request)); |
| 3988 | request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN; |
| 3989 | put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH, |
| 3990 | &request.header.iu_length); |
| 3991 | request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ; |
| 3992 | put_unaligned_le16(event_queue->oq_id, |
| 3993 | &request.data.create_operational_oq.queue_id); |
| 3994 | put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr, |
| 3995 | &request.data.create_operational_oq.element_array_addr); |
| 3996 | put_unaligned_le64((u64)event_queue->oq_pi_bus_addr, |
| 3997 | &request.data.create_operational_oq.pi_addr); |
| 3998 | put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS, |
| 3999 | &request.data.create_operational_oq.num_elements); |
| 4000 | put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16, |
| 4001 | &request.data.create_operational_oq.element_length); |
| 4002 | request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP; |
| 4003 | put_unaligned_le16(event_queue->int_msg_num, |
| 4004 | &request.data.create_operational_oq.int_msg_num); |
| 4005 | |
| 4006 | rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, |
| 4007 | &response); |
| 4008 | if (rc) |
| 4009 | return rc; |
| 4010 | |
| 4011 | event_queue->oq_ci = ctrl_info->iomem_base + |
| 4012 | PQI_DEVICE_REGISTERS_OFFSET + |
| 4013 | get_unaligned_le64( |
| 4014 | &response.data.create_operational_oq.oq_ci_offset); |
| 4015 | |
| 4016 | return 0; |
| 4017 | } |
| 4018 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 4019 | static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info, |
| 4020 | unsigned int group_number) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4021 | { |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4022 | int rc; |
| 4023 | struct pqi_queue_group *queue_group; |
| 4024 | struct pqi_general_admin_request request; |
| 4025 | struct pqi_general_admin_response response; |
| 4026 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 4027 | queue_group = &ctrl_info->queue_groups[group_number]; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4028 | |
| 4029 | /* |
| 4030 | * Create IQ (Inbound Queue - host to device queue) for |
| 4031 | * RAID path. |
| 4032 | */ |
| 4033 | memset(&request, 0, sizeof(request)); |
| 4034 | request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN; |
| 4035 | put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH, |
| 4036 | &request.header.iu_length); |
| 4037 | request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ; |
| 4038 | put_unaligned_le16(queue_group->iq_id[RAID_PATH], |
| 4039 | &request.data.create_operational_iq.queue_id); |
| 4040 | put_unaligned_le64( |
| 4041 | (u64)queue_group->iq_element_array_bus_addr[RAID_PATH], |
| 4042 | &request.data.create_operational_iq.element_array_addr); |
| 4043 | put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH], |
| 4044 | &request.data.create_operational_iq.ci_addr); |
| 4045 | put_unaligned_le16(ctrl_info->num_elements_per_iq, |
| 4046 | &request.data.create_operational_iq.num_elements); |
| 4047 | put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16, |
| 4048 | &request.data.create_operational_iq.element_length); |
| 4049 | request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP; |
| 4050 | |
| 4051 | rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, |
| 4052 | &response); |
| 4053 | if (rc) { |
| 4054 | dev_err(&ctrl_info->pci_dev->dev, |
| 4055 | "error creating inbound RAID queue\n"); |
| 4056 | return rc; |
| 4057 | } |
| 4058 | |
| 4059 | queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base + |
| 4060 | PQI_DEVICE_REGISTERS_OFFSET + |
| 4061 | get_unaligned_le64( |
| 4062 | &response.data.create_operational_iq.iq_pi_offset); |
| 4063 | |
| 4064 | /* |
| 4065 | * Create IQ (Inbound Queue - host to device queue) for |
| 4066 | * Advanced I/O (AIO) path. |
| 4067 | */ |
| 4068 | memset(&request, 0, sizeof(request)); |
| 4069 | request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN; |
| 4070 | put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH, |
| 4071 | &request.header.iu_length); |
| 4072 | request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ; |
| 4073 | put_unaligned_le16(queue_group->iq_id[AIO_PATH], |
| 4074 | &request.data.create_operational_iq.queue_id); |
| 4075 | put_unaligned_le64((u64)queue_group-> |
| 4076 | iq_element_array_bus_addr[AIO_PATH], |
| 4077 | &request.data.create_operational_iq.element_array_addr); |
| 4078 | put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH], |
| 4079 | &request.data.create_operational_iq.ci_addr); |
| 4080 | put_unaligned_le16(ctrl_info->num_elements_per_iq, |
| 4081 | &request.data.create_operational_iq.num_elements); |
| 4082 | put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16, |
| 4083 | &request.data.create_operational_iq.element_length); |
| 4084 | request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP; |
| 4085 | |
| 4086 | rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, |
| 4087 | &response); |
| 4088 | if (rc) { |
| 4089 | dev_err(&ctrl_info->pci_dev->dev, |
| 4090 | "error creating inbound AIO queue\n"); |
Kevin Barnett | 339faa8 | 2018-03-21 13:32:31 -0500 | [diff] [blame] | 4091 | return rc; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base + |
| 4095 | PQI_DEVICE_REGISTERS_OFFSET + |
| 4096 | get_unaligned_le64( |
| 4097 | &response.data.create_operational_iq.iq_pi_offset); |
| 4098 | |
| 4099 | /* |
| 4100 | * Designate the 2nd IQ as the AIO path. By default, all IQs are |
| 4101 | * assumed to be for RAID path I/O unless we change the queue's |
| 4102 | * property. |
| 4103 | */ |
| 4104 | memset(&request, 0, sizeof(request)); |
| 4105 | request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN; |
| 4106 | put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH, |
| 4107 | &request.header.iu_length); |
| 4108 | request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY; |
| 4109 | put_unaligned_le16(queue_group->iq_id[AIO_PATH], |
| 4110 | &request.data.change_operational_iq_properties.queue_id); |
| 4111 | put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE, |
| 4112 | &request.data.change_operational_iq_properties.vendor_specific); |
| 4113 | |
| 4114 | rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, |
| 4115 | &response); |
| 4116 | if (rc) { |
| 4117 | dev_err(&ctrl_info->pci_dev->dev, |
| 4118 | "error changing queue property\n"); |
Kevin Barnett | 339faa8 | 2018-03-21 13:32:31 -0500 | [diff] [blame] | 4119 | return rc; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | /* |
| 4123 | * Create OQ (Outbound Queue - device to host queue). |
| 4124 | */ |
| 4125 | memset(&request, 0, sizeof(request)); |
| 4126 | request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN; |
| 4127 | put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH, |
| 4128 | &request.header.iu_length); |
| 4129 | request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ; |
| 4130 | put_unaligned_le16(queue_group->oq_id, |
| 4131 | &request.data.create_operational_oq.queue_id); |
| 4132 | put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr, |
| 4133 | &request.data.create_operational_oq.element_array_addr); |
| 4134 | put_unaligned_le64((u64)queue_group->oq_pi_bus_addr, |
| 4135 | &request.data.create_operational_oq.pi_addr); |
| 4136 | put_unaligned_le16(ctrl_info->num_elements_per_oq, |
| 4137 | &request.data.create_operational_oq.num_elements); |
| 4138 | put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16, |
| 4139 | &request.data.create_operational_oq.element_length); |
| 4140 | request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP; |
| 4141 | put_unaligned_le16(queue_group->int_msg_num, |
| 4142 | &request.data.create_operational_oq.int_msg_num); |
| 4143 | |
| 4144 | rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, |
| 4145 | &response); |
| 4146 | if (rc) { |
| 4147 | dev_err(&ctrl_info->pci_dev->dev, |
| 4148 | "error creating outbound queue\n"); |
Kevin Barnett | 339faa8 | 2018-03-21 13:32:31 -0500 | [diff] [blame] | 4149 | return rc; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | queue_group->oq_ci = ctrl_info->iomem_base + |
| 4153 | PQI_DEVICE_REGISTERS_OFFSET + |
| 4154 | get_unaligned_le64( |
| 4155 | &response.data.create_operational_oq.oq_ci_offset); |
| 4156 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4157 | return 0; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info) |
| 4161 | { |
| 4162 | int rc; |
| 4163 | unsigned int i; |
| 4164 | |
| 4165 | rc = pqi_create_event_queue(ctrl_info); |
| 4166 | if (rc) { |
| 4167 | dev_err(&ctrl_info->pci_dev->dev, |
| 4168 | "error creating event queue\n"); |
| 4169 | return rc; |
| 4170 | } |
| 4171 | |
| 4172 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 4173 | rc = pqi_create_queue_group(ctrl_info, i); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4174 | if (rc) { |
| 4175 | dev_err(&ctrl_info->pci_dev->dev, |
| 4176 | "error creating queue group number %u/%u\n", |
| 4177 | i, ctrl_info->num_queue_groups); |
| 4178 | return rc; |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | return 0; |
| 4183 | } |
| 4184 | |
| 4185 | #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \ |
| 4186 | (offsetof(struct pqi_event_config, descriptors) + \ |
| 4187 | (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor))) |
| 4188 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 4189 | static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info, |
| 4190 | bool enable_events) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4191 | { |
| 4192 | int rc; |
| 4193 | unsigned int i; |
| 4194 | struct pqi_event_config *event_config; |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 4195 | struct pqi_event_descriptor *event_descriptor; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4196 | struct pqi_general_management_request request; |
| 4197 | |
| 4198 | event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH, |
| 4199 | GFP_KERNEL); |
| 4200 | if (!event_config) |
| 4201 | return -ENOMEM; |
| 4202 | |
| 4203 | memset(&request, 0, sizeof(request)); |
| 4204 | |
| 4205 | request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG; |
| 4206 | put_unaligned_le16(offsetof(struct pqi_general_management_request, |
| 4207 | data.report_event_configuration.sg_descriptors[1]) - |
| 4208 | PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length); |
| 4209 | put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH, |
| 4210 | &request.data.report_event_configuration.buffer_length); |
| 4211 | |
| 4212 | rc = pqi_map_single(ctrl_info->pci_dev, |
| 4213 | request.data.report_event_configuration.sg_descriptors, |
| 4214 | event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 4215 | DMA_FROM_DEVICE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4216 | if (rc) |
| 4217 | goto out; |
| 4218 | |
| 4219 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
| 4220 | 0, NULL, NO_TIMEOUT); |
| 4221 | |
| 4222 | pqi_pci_unmap(ctrl_info->pci_dev, |
| 4223 | request.data.report_event_configuration.sg_descriptors, 1, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 4224 | DMA_FROM_DEVICE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4225 | |
| 4226 | if (rc) |
| 4227 | goto out; |
| 4228 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 4229 | for (i = 0; i < event_config->num_event_descriptors; i++) { |
| 4230 | event_descriptor = &event_config->descriptors[i]; |
| 4231 | if (enable_events && |
| 4232 | pqi_is_supported_event(event_descriptor->event_type)) |
| 4233 | put_unaligned_le16(ctrl_info->event_queue.oq_id, |
| 4234 | &event_descriptor->oq_id); |
| 4235 | else |
| 4236 | put_unaligned_le16(0, &event_descriptor->oq_id); |
| 4237 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4238 | |
| 4239 | memset(&request, 0, sizeof(request)); |
| 4240 | |
| 4241 | request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG; |
| 4242 | put_unaligned_le16(offsetof(struct pqi_general_management_request, |
| 4243 | data.report_event_configuration.sg_descriptors[1]) - |
| 4244 | PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length); |
| 4245 | put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH, |
| 4246 | &request.data.report_event_configuration.buffer_length); |
| 4247 | |
| 4248 | rc = pqi_map_single(ctrl_info->pci_dev, |
| 4249 | request.data.report_event_configuration.sg_descriptors, |
| 4250 | event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 4251 | DMA_TO_DEVICE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4252 | if (rc) |
| 4253 | goto out; |
| 4254 | |
| 4255 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, |
| 4256 | NULL, NO_TIMEOUT); |
| 4257 | |
| 4258 | pqi_pci_unmap(ctrl_info->pci_dev, |
| 4259 | request.data.report_event_configuration.sg_descriptors, 1, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 4260 | DMA_TO_DEVICE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4261 | |
| 4262 | out: |
| 4263 | kfree(event_config); |
| 4264 | |
| 4265 | return rc; |
| 4266 | } |
| 4267 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 4268 | static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info) |
| 4269 | { |
| 4270 | return pqi_configure_events(ctrl_info, true); |
| 4271 | } |
| 4272 | |
| 4273 | static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info) |
| 4274 | { |
| 4275 | return pqi_configure_events(ctrl_info, false); |
| 4276 | } |
| 4277 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4278 | static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info) |
| 4279 | { |
| 4280 | unsigned int i; |
| 4281 | struct device *dev; |
| 4282 | size_t sg_chain_buffer_length; |
| 4283 | struct pqi_io_request *io_request; |
| 4284 | |
| 4285 | if (!ctrl_info->io_request_pool) |
| 4286 | return; |
| 4287 | |
| 4288 | dev = &ctrl_info->pci_dev->dev; |
| 4289 | sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length; |
| 4290 | io_request = ctrl_info->io_request_pool; |
| 4291 | |
| 4292 | for (i = 0; i < ctrl_info->max_io_slots; i++) { |
| 4293 | kfree(io_request->iu); |
| 4294 | if (!io_request->sg_chain_buffer) |
| 4295 | break; |
| 4296 | dma_free_coherent(dev, sg_chain_buffer_length, |
| 4297 | io_request->sg_chain_buffer, |
| 4298 | io_request->sg_chain_buffer_dma_handle); |
| 4299 | io_request++; |
| 4300 | } |
| 4301 | |
| 4302 | kfree(ctrl_info->io_request_pool); |
| 4303 | ctrl_info->io_request_pool = NULL; |
| 4304 | } |
| 4305 | |
| 4306 | static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info) |
| 4307 | { |
| 4308 | ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev, |
| 4309 | ctrl_info->error_buffer_length, |
| 4310 | &ctrl_info->error_buffer_dma_handle, GFP_KERNEL); |
| 4311 | |
| 4312 | if (!ctrl_info->error_buffer) |
| 4313 | return -ENOMEM; |
| 4314 | |
| 4315 | return 0; |
| 4316 | } |
| 4317 | |
| 4318 | static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info) |
| 4319 | { |
| 4320 | unsigned int i; |
| 4321 | void *sg_chain_buffer; |
| 4322 | size_t sg_chain_buffer_length; |
| 4323 | dma_addr_t sg_chain_buffer_dma_handle; |
| 4324 | struct device *dev; |
| 4325 | struct pqi_io_request *io_request; |
| 4326 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 4327 | ctrl_info->io_request_pool = |
| 4328 | kcalloc(ctrl_info->max_io_slots, |
| 4329 | sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4330 | |
| 4331 | if (!ctrl_info->io_request_pool) { |
| 4332 | dev_err(&ctrl_info->pci_dev->dev, |
| 4333 | "failed to allocate I/O request pool\n"); |
| 4334 | goto error; |
| 4335 | } |
| 4336 | |
| 4337 | dev = &ctrl_info->pci_dev->dev; |
| 4338 | sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length; |
| 4339 | io_request = ctrl_info->io_request_pool; |
| 4340 | |
| 4341 | for (i = 0; i < ctrl_info->max_io_slots; i++) { |
| 4342 | io_request->iu = |
| 4343 | kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL); |
| 4344 | |
| 4345 | if (!io_request->iu) { |
| 4346 | dev_err(&ctrl_info->pci_dev->dev, |
| 4347 | "failed to allocate IU buffers\n"); |
| 4348 | goto error; |
| 4349 | } |
| 4350 | |
| 4351 | sg_chain_buffer = dma_alloc_coherent(dev, |
| 4352 | sg_chain_buffer_length, &sg_chain_buffer_dma_handle, |
| 4353 | GFP_KERNEL); |
| 4354 | |
| 4355 | if (!sg_chain_buffer) { |
| 4356 | dev_err(&ctrl_info->pci_dev->dev, |
| 4357 | "failed to allocate PQI scatter-gather chain buffers\n"); |
| 4358 | goto error; |
| 4359 | } |
| 4360 | |
| 4361 | io_request->index = i; |
| 4362 | io_request->sg_chain_buffer = sg_chain_buffer; |
| 4363 | io_request->sg_chain_buffer_dma_handle = |
| 4364 | sg_chain_buffer_dma_handle; |
| 4365 | io_request++; |
| 4366 | } |
| 4367 | |
| 4368 | return 0; |
| 4369 | |
| 4370 | error: |
| 4371 | pqi_free_all_io_requests(ctrl_info); |
| 4372 | |
| 4373 | return -ENOMEM; |
| 4374 | } |
| 4375 | |
| 4376 | /* |
| 4377 | * Calculate required resources that are sized based on max. outstanding |
| 4378 | * requests and max. transfer size. |
| 4379 | */ |
| 4380 | |
| 4381 | static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info) |
| 4382 | { |
| 4383 | u32 max_transfer_size; |
| 4384 | u32 max_sg_entries; |
| 4385 | |
| 4386 | ctrl_info->scsi_ml_can_queue = |
| 4387 | ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS; |
| 4388 | ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests; |
| 4389 | |
| 4390 | ctrl_info->error_buffer_length = |
| 4391 | ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH; |
| 4392 | |
Kevin Barnett | d727a77 | 2017-05-03 18:54:25 -0500 | [diff] [blame] | 4393 | if (reset_devices) |
| 4394 | max_transfer_size = min(ctrl_info->max_transfer_size, |
| 4395 | PQI_MAX_TRANSFER_SIZE_KDUMP); |
| 4396 | else |
| 4397 | max_transfer_size = min(ctrl_info->max_transfer_size, |
| 4398 | PQI_MAX_TRANSFER_SIZE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4399 | |
| 4400 | max_sg_entries = max_transfer_size / PAGE_SIZE; |
| 4401 | |
| 4402 | /* +1 to cover when the buffer is not page-aligned. */ |
| 4403 | max_sg_entries++; |
| 4404 | |
| 4405 | max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries); |
| 4406 | |
| 4407 | max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE; |
| 4408 | |
| 4409 | ctrl_info->sg_chain_buffer_length = |
Kevin Barnett | e1d213b | 2017-05-03 18:53:18 -0500 | [diff] [blame] | 4410 | (max_sg_entries * sizeof(struct pqi_sg_descriptor)) + |
| 4411 | PQI_EXTRA_SGL_MEMORY; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4412 | ctrl_info->sg_tablesize = max_sg_entries; |
| 4413 | ctrl_info->max_sectors = max_transfer_size / 512; |
| 4414 | } |
| 4415 | |
| 4416 | static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info) |
| 4417 | { |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4418 | int num_queue_groups; |
| 4419 | u16 num_elements_per_iq; |
| 4420 | u16 num_elements_per_oq; |
| 4421 | |
Kevin Barnett | d727a77 | 2017-05-03 18:54:25 -0500 | [diff] [blame] | 4422 | if (reset_devices) { |
| 4423 | num_queue_groups = 1; |
| 4424 | } else { |
| 4425 | int num_cpus; |
| 4426 | int max_queue_groups; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4427 | |
Kevin Barnett | d727a77 | 2017-05-03 18:54:25 -0500 | [diff] [blame] | 4428 | max_queue_groups = min(ctrl_info->max_inbound_queues / 2, |
| 4429 | ctrl_info->max_outbound_queues - 1); |
| 4430 | max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS); |
| 4431 | |
| 4432 | num_cpus = num_online_cpus(); |
| 4433 | num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors); |
| 4434 | num_queue_groups = min(num_queue_groups, max_queue_groups); |
| 4435 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4436 | |
| 4437 | ctrl_info->num_queue_groups = num_queue_groups; |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 4438 | ctrl_info->max_hw_queue_index = num_queue_groups - 1; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4439 | |
Kevin Barnett | 77668f4 | 2016-08-31 14:54:23 -0500 | [diff] [blame] | 4440 | /* |
| 4441 | * Make sure that the max. inbound IU length is an even multiple |
| 4442 | * of our inbound element length. |
| 4443 | */ |
| 4444 | ctrl_info->max_inbound_iu_length = |
| 4445 | (ctrl_info->max_inbound_iu_length_per_firmware / |
| 4446 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) * |
| 4447 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4448 | |
| 4449 | num_elements_per_iq = |
| 4450 | (ctrl_info->max_inbound_iu_length / |
| 4451 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 4452 | |
| 4453 | /* Add one because one element in each queue is unusable. */ |
| 4454 | num_elements_per_iq++; |
| 4455 | |
| 4456 | num_elements_per_iq = min(num_elements_per_iq, |
| 4457 | ctrl_info->max_elements_per_iq); |
| 4458 | |
| 4459 | num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1; |
| 4460 | num_elements_per_oq = min(num_elements_per_oq, |
| 4461 | ctrl_info->max_elements_per_oq); |
| 4462 | |
| 4463 | ctrl_info->num_elements_per_iq = num_elements_per_iq; |
| 4464 | ctrl_info->num_elements_per_oq = num_elements_per_oq; |
| 4465 | |
| 4466 | ctrl_info->max_sg_per_iu = |
| 4467 | ((ctrl_info->max_inbound_iu_length - |
| 4468 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) / |
| 4469 | sizeof(struct pqi_sg_descriptor)) + |
| 4470 | PQI_MAX_EMBEDDED_SG_DESCRIPTORS; |
| 4471 | } |
| 4472 | |
| 4473 | static inline void pqi_set_sg_descriptor( |
| 4474 | struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg) |
| 4475 | { |
| 4476 | u64 address = (u64)sg_dma_address(sg); |
| 4477 | unsigned int length = sg_dma_len(sg); |
| 4478 | |
| 4479 | put_unaligned_le64(address, &sg_descriptor->address); |
| 4480 | put_unaligned_le32(length, &sg_descriptor->length); |
| 4481 | put_unaligned_le32(0, &sg_descriptor->flags); |
| 4482 | } |
| 4483 | |
| 4484 | static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info, |
| 4485 | struct pqi_raid_path_request *request, struct scsi_cmnd *scmd, |
| 4486 | struct pqi_io_request *io_request) |
| 4487 | { |
| 4488 | int i; |
| 4489 | u16 iu_length; |
| 4490 | int sg_count; |
| 4491 | bool chained; |
| 4492 | unsigned int num_sg_in_iu; |
| 4493 | unsigned int max_sg_per_iu; |
| 4494 | struct scatterlist *sg; |
| 4495 | struct pqi_sg_descriptor *sg_descriptor; |
| 4496 | |
| 4497 | sg_count = scsi_dma_map(scmd); |
| 4498 | if (sg_count < 0) |
| 4499 | return sg_count; |
| 4500 | |
| 4501 | iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) - |
| 4502 | PQI_REQUEST_HEADER_LENGTH; |
| 4503 | |
| 4504 | if (sg_count == 0) |
| 4505 | goto out; |
| 4506 | |
| 4507 | sg = scsi_sglist(scmd); |
| 4508 | sg_descriptor = request->sg_descriptors; |
| 4509 | max_sg_per_iu = ctrl_info->max_sg_per_iu - 1; |
| 4510 | chained = false; |
| 4511 | num_sg_in_iu = 0; |
| 4512 | i = 0; |
| 4513 | |
| 4514 | while (1) { |
| 4515 | pqi_set_sg_descriptor(sg_descriptor, sg); |
| 4516 | if (!chained) |
| 4517 | num_sg_in_iu++; |
| 4518 | i++; |
| 4519 | if (i == sg_count) |
| 4520 | break; |
| 4521 | sg_descriptor++; |
| 4522 | if (i == max_sg_per_iu) { |
| 4523 | put_unaligned_le64( |
| 4524 | (u64)io_request->sg_chain_buffer_dma_handle, |
| 4525 | &sg_descriptor->address); |
| 4526 | put_unaligned_le32((sg_count - num_sg_in_iu) |
| 4527 | * sizeof(*sg_descriptor), |
| 4528 | &sg_descriptor->length); |
| 4529 | put_unaligned_le32(CISS_SG_CHAIN, |
| 4530 | &sg_descriptor->flags); |
| 4531 | chained = true; |
| 4532 | num_sg_in_iu++; |
| 4533 | sg_descriptor = io_request->sg_chain_buffer; |
| 4534 | } |
| 4535 | sg = sg_next(sg); |
| 4536 | } |
| 4537 | |
| 4538 | put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags); |
| 4539 | request->partial = chained; |
| 4540 | iu_length += num_sg_in_iu * sizeof(*sg_descriptor); |
| 4541 | |
| 4542 | out: |
| 4543 | put_unaligned_le16(iu_length, &request->header.iu_length); |
| 4544 | |
| 4545 | return 0; |
| 4546 | } |
| 4547 | |
| 4548 | static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info, |
| 4549 | struct pqi_aio_path_request *request, struct scsi_cmnd *scmd, |
| 4550 | struct pqi_io_request *io_request) |
| 4551 | { |
| 4552 | int i; |
| 4553 | u16 iu_length; |
| 4554 | int sg_count; |
Kevin Barnett | a60eec0 | 2016-08-31 14:54:11 -0500 | [diff] [blame] | 4555 | bool chained; |
| 4556 | unsigned int num_sg_in_iu; |
| 4557 | unsigned int max_sg_per_iu; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4558 | struct scatterlist *sg; |
| 4559 | struct pqi_sg_descriptor *sg_descriptor; |
| 4560 | |
| 4561 | sg_count = scsi_dma_map(scmd); |
| 4562 | if (sg_count < 0) |
| 4563 | return sg_count; |
Kevin Barnett | a60eec0 | 2016-08-31 14:54:11 -0500 | [diff] [blame] | 4564 | |
| 4565 | iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) - |
| 4566 | PQI_REQUEST_HEADER_LENGTH; |
| 4567 | num_sg_in_iu = 0; |
| 4568 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4569 | if (sg_count == 0) |
| 4570 | goto out; |
| 4571 | |
Kevin Barnett | a60eec0 | 2016-08-31 14:54:11 -0500 | [diff] [blame] | 4572 | sg = scsi_sglist(scmd); |
| 4573 | sg_descriptor = request->sg_descriptors; |
| 4574 | max_sg_per_iu = ctrl_info->max_sg_per_iu - 1; |
| 4575 | chained = false; |
| 4576 | i = 0; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4577 | |
Kevin Barnett | a60eec0 | 2016-08-31 14:54:11 -0500 | [diff] [blame] | 4578 | while (1) { |
| 4579 | pqi_set_sg_descriptor(sg_descriptor, sg); |
| 4580 | if (!chained) |
| 4581 | num_sg_in_iu++; |
| 4582 | i++; |
| 4583 | if (i == sg_count) |
| 4584 | break; |
| 4585 | sg_descriptor++; |
| 4586 | if (i == max_sg_per_iu) { |
| 4587 | put_unaligned_le64( |
| 4588 | (u64)io_request->sg_chain_buffer_dma_handle, |
| 4589 | &sg_descriptor->address); |
| 4590 | put_unaligned_le32((sg_count - num_sg_in_iu) |
| 4591 | * sizeof(*sg_descriptor), |
| 4592 | &sg_descriptor->length); |
| 4593 | put_unaligned_le32(CISS_SG_CHAIN, |
| 4594 | &sg_descriptor->flags); |
| 4595 | chained = true; |
| 4596 | num_sg_in_iu++; |
| 4597 | sg_descriptor = io_request->sg_chain_buffer; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4598 | } |
Kevin Barnett | a60eec0 | 2016-08-31 14:54:11 -0500 | [diff] [blame] | 4599 | sg = sg_next(sg); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4600 | } |
| 4601 | |
Kevin Barnett | a60eec0 | 2016-08-31 14:54:11 -0500 | [diff] [blame] | 4602 | put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags); |
| 4603 | request->partial = chained; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4604 | iu_length += num_sg_in_iu * sizeof(*sg_descriptor); |
Kevin Barnett | a60eec0 | 2016-08-31 14:54:11 -0500 | [diff] [blame] | 4605 | |
| 4606 | out: |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4607 | put_unaligned_le16(iu_length, &request->header.iu_length); |
| 4608 | request->num_sg_descriptors = num_sg_in_iu; |
| 4609 | |
| 4610 | return 0; |
| 4611 | } |
| 4612 | |
| 4613 | static void pqi_raid_io_complete(struct pqi_io_request *io_request, |
| 4614 | void *context) |
| 4615 | { |
| 4616 | struct scsi_cmnd *scmd; |
| 4617 | |
| 4618 | scmd = io_request->scmd; |
| 4619 | pqi_free_io_request(io_request); |
| 4620 | scsi_dma_unmap(scmd); |
| 4621 | pqi_scsi_done(scmd); |
| 4622 | } |
| 4623 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4624 | static int pqi_raid_submit_scsi_cmd_with_io_request( |
| 4625 | struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4626 | struct pqi_scsi_dev *device, struct scsi_cmnd *scmd, |
| 4627 | struct pqi_queue_group *queue_group) |
| 4628 | { |
| 4629 | int rc; |
| 4630 | size_t cdb_length; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4631 | struct pqi_raid_path_request *request; |
| 4632 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4633 | io_request->io_complete_callback = pqi_raid_io_complete; |
| 4634 | io_request->scmd = scmd; |
| 4635 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4636 | request = io_request->iu; |
| 4637 | memset(request, 0, |
| 4638 | offsetof(struct pqi_raid_path_request, sg_descriptors)); |
| 4639 | |
| 4640 | request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO; |
| 4641 | put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length); |
| 4642 | request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE; |
| 4643 | put_unaligned_le16(io_request->index, &request->request_id); |
| 4644 | request->error_index = request->request_id; |
| 4645 | memcpy(request->lun_number, device->scsi3addr, |
| 4646 | sizeof(request->lun_number)); |
| 4647 | |
| 4648 | cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb)); |
| 4649 | memcpy(request->cdb, scmd->cmnd, cdb_length); |
| 4650 | |
| 4651 | switch (cdb_length) { |
| 4652 | case 6: |
| 4653 | case 10: |
| 4654 | case 12: |
| 4655 | case 16: |
| 4656 | /* No bytes in the Additional CDB bytes field */ |
| 4657 | request->additional_cdb_bytes_usage = |
| 4658 | SOP_ADDITIONAL_CDB_BYTES_0; |
| 4659 | break; |
| 4660 | case 20: |
| 4661 | /* 4 bytes in the Additional cdb field */ |
| 4662 | request->additional_cdb_bytes_usage = |
| 4663 | SOP_ADDITIONAL_CDB_BYTES_4; |
| 4664 | break; |
| 4665 | case 24: |
| 4666 | /* 8 bytes in the Additional cdb field */ |
| 4667 | request->additional_cdb_bytes_usage = |
| 4668 | SOP_ADDITIONAL_CDB_BYTES_8; |
| 4669 | break; |
| 4670 | case 28: |
| 4671 | /* 12 bytes in the Additional cdb field */ |
| 4672 | request->additional_cdb_bytes_usage = |
| 4673 | SOP_ADDITIONAL_CDB_BYTES_12; |
| 4674 | break; |
| 4675 | case 32: |
| 4676 | default: |
| 4677 | /* 16 bytes in the Additional cdb field */ |
| 4678 | request->additional_cdb_bytes_usage = |
| 4679 | SOP_ADDITIONAL_CDB_BYTES_16; |
| 4680 | break; |
| 4681 | } |
| 4682 | |
| 4683 | switch (scmd->sc_data_direction) { |
| 4684 | case DMA_TO_DEVICE: |
| 4685 | request->data_direction = SOP_READ_FLAG; |
| 4686 | break; |
| 4687 | case DMA_FROM_DEVICE: |
| 4688 | request->data_direction = SOP_WRITE_FLAG; |
| 4689 | break; |
| 4690 | case DMA_NONE: |
| 4691 | request->data_direction = SOP_NO_DIRECTION_FLAG; |
| 4692 | break; |
| 4693 | case DMA_BIDIRECTIONAL: |
| 4694 | request->data_direction = SOP_BIDIRECTIONAL; |
| 4695 | break; |
| 4696 | default: |
| 4697 | dev_err(&ctrl_info->pci_dev->dev, |
| 4698 | "unknown data direction: %d\n", |
| 4699 | scmd->sc_data_direction); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4700 | break; |
| 4701 | } |
| 4702 | |
| 4703 | rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request); |
| 4704 | if (rc) { |
| 4705 | pqi_free_io_request(io_request); |
| 4706 | return SCSI_MLQUEUE_HOST_BUSY; |
| 4707 | } |
| 4708 | |
| 4709 | pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request); |
| 4710 | |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4714 | static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info, |
| 4715 | struct pqi_scsi_dev *device, struct scsi_cmnd *scmd, |
| 4716 | struct pqi_queue_group *queue_group) |
| 4717 | { |
| 4718 | struct pqi_io_request *io_request; |
| 4719 | |
| 4720 | io_request = pqi_alloc_io_request(ctrl_info); |
| 4721 | |
| 4722 | return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request, |
| 4723 | device, scmd, queue_group); |
| 4724 | } |
| 4725 | |
| 4726 | static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info) |
| 4727 | { |
| 4728 | if (!pqi_ctrl_blocked(ctrl_info)) |
| 4729 | schedule_work(&ctrl_info->raid_bypass_retry_work); |
| 4730 | } |
| 4731 | |
| 4732 | static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request) |
| 4733 | { |
| 4734 | struct scsi_cmnd *scmd; |
Kevin Barnett | 03b288cf | 2017-05-03 18:54:49 -0500 | [diff] [blame] | 4735 | struct pqi_scsi_dev *device; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4736 | struct pqi_ctrl_info *ctrl_info; |
| 4737 | |
| 4738 | if (!io_request->raid_bypass) |
| 4739 | return false; |
| 4740 | |
| 4741 | scmd = io_request->scmd; |
| 4742 | if ((scmd->result & 0xff) == SAM_STAT_GOOD) |
| 4743 | return false; |
| 4744 | if (host_byte(scmd->result) == DID_NO_CONNECT) |
| 4745 | return false; |
| 4746 | |
Kevin Barnett | 03b288cf | 2017-05-03 18:54:49 -0500 | [diff] [blame] | 4747 | device = scmd->device->hostdata; |
| 4748 | if (pqi_device_offline(device)) |
| 4749 | return false; |
| 4750 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4751 | ctrl_info = shost_to_hba(scmd->device->host); |
| 4752 | if (pqi_ctrl_offline(ctrl_info)) |
| 4753 | return false; |
| 4754 | |
| 4755 | return true; |
| 4756 | } |
| 4757 | |
| 4758 | static inline void pqi_add_to_raid_bypass_retry_list( |
| 4759 | struct pqi_ctrl_info *ctrl_info, |
| 4760 | struct pqi_io_request *io_request, bool at_head) |
| 4761 | { |
| 4762 | unsigned long flags; |
| 4763 | |
| 4764 | spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags); |
| 4765 | if (at_head) |
| 4766 | list_add(&io_request->request_list_entry, |
| 4767 | &ctrl_info->raid_bypass_retry_list); |
| 4768 | else |
| 4769 | list_add_tail(&io_request->request_list_entry, |
| 4770 | &ctrl_info->raid_bypass_retry_list); |
| 4771 | spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags); |
| 4772 | } |
| 4773 | |
| 4774 | static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request, |
| 4775 | void *context) |
| 4776 | { |
| 4777 | struct scsi_cmnd *scmd; |
| 4778 | |
| 4779 | scmd = io_request->scmd; |
| 4780 | pqi_free_io_request(io_request); |
| 4781 | pqi_scsi_done(scmd); |
| 4782 | } |
| 4783 | |
| 4784 | static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request) |
| 4785 | { |
| 4786 | struct scsi_cmnd *scmd; |
| 4787 | struct pqi_ctrl_info *ctrl_info; |
| 4788 | |
| 4789 | io_request->io_complete_callback = pqi_queued_raid_bypass_complete; |
| 4790 | scmd = io_request->scmd; |
| 4791 | scmd->result = 0; |
| 4792 | ctrl_info = shost_to_hba(scmd->device->host); |
| 4793 | |
| 4794 | pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false); |
| 4795 | pqi_schedule_bypass_retry(ctrl_info); |
| 4796 | } |
| 4797 | |
| 4798 | static int pqi_retry_raid_bypass(struct pqi_io_request *io_request) |
| 4799 | { |
| 4800 | struct scsi_cmnd *scmd; |
| 4801 | struct pqi_scsi_dev *device; |
| 4802 | struct pqi_ctrl_info *ctrl_info; |
| 4803 | struct pqi_queue_group *queue_group; |
| 4804 | |
| 4805 | scmd = io_request->scmd; |
| 4806 | device = scmd->device->hostdata; |
| 4807 | if (pqi_device_in_reset(device)) { |
| 4808 | pqi_free_io_request(io_request); |
| 4809 | set_host_byte(scmd, DID_RESET); |
| 4810 | pqi_scsi_done(scmd); |
| 4811 | return 0; |
| 4812 | } |
| 4813 | |
| 4814 | ctrl_info = shost_to_hba(scmd->device->host); |
| 4815 | queue_group = io_request->queue_group; |
| 4816 | |
| 4817 | pqi_reinit_io_request(io_request); |
| 4818 | |
| 4819 | return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request, |
| 4820 | device, scmd, queue_group); |
| 4821 | } |
| 4822 | |
| 4823 | static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request( |
| 4824 | struct pqi_ctrl_info *ctrl_info) |
| 4825 | { |
| 4826 | unsigned long flags; |
| 4827 | struct pqi_io_request *io_request; |
| 4828 | |
| 4829 | spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags); |
| 4830 | io_request = list_first_entry_or_null( |
| 4831 | &ctrl_info->raid_bypass_retry_list, |
| 4832 | struct pqi_io_request, request_list_entry); |
| 4833 | if (io_request) |
| 4834 | list_del(&io_request->request_list_entry); |
| 4835 | spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags); |
| 4836 | |
| 4837 | return io_request; |
| 4838 | } |
| 4839 | |
| 4840 | static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info) |
| 4841 | { |
| 4842 | int rc; |
| 4843 | struct pqi_io_request *io_request; |
| 4844 | |
| 4845 | pqi_ctrl_busy(ctrl_info); |
| 4846 | |
| 4847 | while (1) { |
| 4848 | if (pqi_ctrl_blocked(ctrl_info)) |
| 4849 | break; |
| 4850 | io_request = pqi_next_queued_raid_bypass_request(ctrl_info); |
| 4851 | if (!io_request) |
| 4852 | break; |
| 4853 | rc = pqi_retry_raid_bypass(io_request); |
| 4854 | if (rc) { |
| 4855 | pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, |
| 4856 | true); |
| 4857 | pqi_schedule_bypass_retry(ctrl_info); |
| 4858 | break; |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | pqi_ctrl_unbusy(ctrl_info); |
| 4863 | } |
| 4864 | |
| 4865 | static void pqi_raid_bypass_retry_worker(struct work_struct *work) |
| 4866 | { |
| 4867 | struct pqi_ctrl_info *ctrl_info; |
| 4868 | |
| 4869 | ctrl_info = container_of(work, struct pqi_ctrl_info, |
| 4870 | raid_bypass_retry_work); |
| 4871 | pqi_retry_raid_bypass_requests(ctrl_info); |
| 4872 | } |
| 4873 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 4874 | static void pqi_clear_all_queued_raid_bypass_retries( |
| 4875 | struct pqi_ctrl_info *ctrl_info) |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4876 | { |
| 4877 | unsigned long flags; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4878 | |
| 4879 | spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags); |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 4880 | INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4881 | spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags); |
| 4882 | } |
| 4883 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4884 | static void pqi_aio_io_complete(struct pqi_io_request *io_request, |
| 4885 | void *context) |
| 4886 | { |
| 4887 | struct scsi_cmnd *scmd; |
| 4888 | |
| 4889 | scmd = io_request->scmd; |
| 4890 | scsi_dma_unmap(scmd); |
| 4891 | if (io_request->status == -EAGAIN) |
| 4892 | set_host_byte(scmd, DID_IMM_RETRY); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4893 | else if (pqi_raid_bypass_retry_needed(io_request)) { |
| 4894 | pqi_queue_raid_bypass_retry(io_request); |
| 4895 | return; |
| 4896 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4897 | pqi_free_io_request(io_request); |
| 4898 | pqi_scsi_done(scmd); |
| 4899 | } |
| 4900 | |
| 4901 | static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info, |
| 4902 | struct pqi_scsi_dev *device, struct scsi_cmnd *scmd, |
| 4903 | struct pqi_queue_group *queue_group) |
| 4904 | { |
| 4905 | return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle, |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4906 | scmd->cmnd, scmd->cmd_len, queue_group, NULL, false); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info, |
| 4910 | struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb, |
| 4911 | unsigned int cdb_length, struct pqi_queue_group *queue_group, |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4912 | struct pqi_encryption_info *encryption_info, bool raid_bypass) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4913 | { |
| 4914 | int rc; |
| 4915 | struct pqi_io_request *io_request; |
| 4916 | struct pqi_aio_path_request *request; |
| 4917 | |
| 4918 | io_request = pqi_alloc_io_request(ctrl_info); |
| 4919 | io_request->io_complete_callback = pqi_aio_io_complete; |
| 4920 | io_request->scmd = scmd; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 4921 | io_request->raid_bypass = raid_bypass; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4922 | |
| 4923 | request = io_request->iu; |
| 4924 | memset(request, 0, |
| 4925 | offsetof(struct pqi_raid_path_request, sg_descriptors)); |
| 4926 | |
| 4927 | request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO; |
| 4928 | put_unaligned_le32(aio_handle, &request->nexus_id); |
| 4929 | put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length); |
| 4930 | request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE; |
| 4931 | put_unaligned_le16(io_request->index, &request->request_id); |
| 4932 | request->error_index = request->request_id; |
| 4933 | if (cdb_length > sizeof(request->cdb)) |
| 4934 | cdb_length = sizeof(request->cdb); |
| 4935 | request->cdb_length = cdb_length; |
| 4936 | memcpy(request->cdb, cdb, cdb_length); |
| 4937 | |
| 4938 | switch (scmd->sc_data_direction) { |
| 4939 | case DMA_TO_DEVICE: |
| 4940 | request->data_direction = SOP_READ_FLAG; |
| 4941 | break; |
| 4942 | case DMA_FROM_DEVICE: |
| 4943 | request->data_direction = SOP_WRITE_FLAG; |
| 4944 | break; |
| 4945 | case DMA_NONE: |
| 4946 | request->data_direction = SOP_NO_DIRECTION_FLAG; |
| 4947 | break; |
| 4948 | case DMA_BIDIRECTIONAL: |
| 4949 | request->data_direction = SOP_BIDIRECTIONAL; |
| 4950 | break; |
| 4951 | default: |
| 4952 | dev_err(&ctrl_info->pci_dev->dev, |
| 4953 | "unknown data direction: %d\n", |
| 4954 | scmd->sc_data_direction); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 4955 | break; |
| 4956 | } |
| 4957 | |
| 4958 | if (encryption_info) { |
| 4959 | request->encryption_enable = true; |
| 4960 | put_unaligned_le16(encryption_info->data_encryption_key_index, |
| 4961 | &request->data_encryption_key_index); |
| 4962 | put_unaligned_le32(encryption_info->encrypt_tweak_lower, |
| 4963 | &request->encrypt_tweak_lower); |
| 4964 | put_unaligned_le32(encryption_info->encrypt_tweak_upper, |
| 4965 | &request->encrypt_tweak_upper); |
| 4966 | } |
| 4967 | |
| 4968 | rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request); |
| 4969 | if (rc) { |
| 4970 | pqi_free_io_request(io_request); |
| 4971 | return SCSI_MLQUEUE_HOST_BUSY; |
| 4972 | } |
| 4973 | |
| 4974 | pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request); |
| 4975 | |
| 4976 | return 0; |
| 4977 | } |
| 4978 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 4979 | static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info, |
| 4980 | struct scsi_cmnd *scmd) |
| 4981 | { |
| 4982 | u16 hw_queue; |
| 4983 | |
| 4984 | hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request)); |
| 4985 | if (hw_queue > ctrl_info->max_hw_queue_index) |
| 4986 | hw_queue = 0; |
| 4987 | |
| 4988 | return hw_queue; |
| 4989 | } |
| 4990 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 4991 | /* |
| 4992 | * This function gets called just before we hand the completed SCSI request |
| 4993 | * back to the SML. |
| 4994 | */ |
| 4995 | |
| 4996 | void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd) |
| 4997 | { |
| 4998 | struct pqi_scsi_dev *device; |
| 4999 | |
| 5000 | device = scmd->device->hostdata; |
| 5001 | atomic_dec(&device->scsi_cmds_outstanding); |
| 5002 | } |
| 5003 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5004 | static int pqi_scsi_queue_command(struct Scsi_Host *shost, |
Kevin Barnett | 7d81d2b | 2016-08-31 14:55:11 -0500 | [diff] [blame] | 5005 | struct scsi_cmnd *scmd) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5006 | { |
| 5007 | int rc; |
| 5008 | struct pqi_ctrl_info *ctrl_info; |
| 5009 | struct pqi_scsi_dev *device; |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 5010 | u16 hw_queue; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5011 | struct pqi_queue_group *queue_group; |
| 5012 | bool raid_bypassed; |
| 5013 | |
| 5014 | device = scmd->device->hostdata; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5015 | ctrl_info = shost_to_hba(shost); |
| 5016 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5017 | atomic_inc(&device->scsi_cmds_outstanding); |
| 5018 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5019 | if (pqi_ctrl_offline(ctrl_info)) { |
| 5020 | set_host_byte(scmd, DID_NO_CONNECT); |
| 5021 | pqi_scsi_done(scmd); |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5025 | pqi_ctrl_busy(ctrl_info); |
| 5026 | if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device)) { |
| 5027 | rc = SCSI_MLQUEUE_HOST_BUSY; |
| 5028 | goto out; |
| 5029 | } |
| 5030 | |
Kevin Barnett | 7d81d2b | 2016-08-31 14:55:11 -0500 | [diff] [blame] | 5031 | /* |
| 5032 | * This is necessary because the SML doesn't zero out this field during |
| 5033 | * error recovery. |
| 5034 | */ |
| 5035 | scmd->result = 0; |
| 5036 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 5037 | hw_queue = pqi_get_hw_queue(ctrl_info, scmd); |
| 5038 | queue_group = &ctrl_info->queue_groups[hw_queue]; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5039 | |
| 5040 | if (pqi_is_logical_device(device)) { |
| 5041 | raid_bypassed = false; |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 5042 | if (device->raid_bypass_enabled && |
Christoph Hellwig | 57292b5 | 2017-01-31 16:57:29 +0100 | [diff] [blame] | 5043 | !blk_rq_is_passthrough(scmd->request)) { |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5044 | rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device, |
| 5045 | scmd, queue_group); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 5046 | if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY) |
| 5047 | raid_bypassed = true; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5048 | } |
| 5049 | if (!raid_bypassed) |
| 5050 | rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, |
| 5051 | queue_group); |
| 5052 | } else { |
| 5053 | if (device->aio_enabled) |
| 5054 | rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd, |
| 5055 | queue_group); |
| 5056 | else |
| 5057 | rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, |
| 5058 | queue_group); |
| 5059 | } |
| 5060 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5061 | out: |
| 5062 | pqi_ctrl_unbusy(ctrl_info); |
| 5063 | if (rc) |
| 5064 | atomic_dec(&device->scsi_cmds_outstanding); |
| 5065 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5066 | return rc; |
| 5067 | } |
| 5068 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5069 | static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info, |
| 5070 | struct pqi_queue_group *queue_group) |
| 5071 | { |
| 5072 | unsigned int path; |
| 5073 | unsigned long flags; |
| 5074 | bool list_is_empty; |
| 5075 | |
| 5076 | for (path = 0; path < 2; path++) { |
| 5077 | while (1) { |
| 5078 | spin_lock_irqsave( |
| 5079 | &queue_group->submit_lock[path], flags); |
| 5080 | list_is_empty = |
| 5081 | list_empty(&queue_group->request_list[path]); |
| 5082 | spin_unlock_irqrestore( |
| 5083 | &queue_group->submit_lock[path], flags); |
| 5084 | if (list_is_empty) |
| 5085 | break; |
| 5086 | pqi_check_ctrl_health(ctrl_info); |
| 5087 | if (pqi_ctrl_offline(ctrl_info)) |
| 5088 | return -ENXIO; |
| 5089 | usleep_range(1000, 2000); |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | return 0; |
| 5094 | } |
| 5095 | |
| 5096 | static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info) |
| 5097 | { |
| 5098 | int rc; |
| 5099 | unsigned int i; |
| 5100 | unsigned int path; |
| 5101 | struct pqi_queue_group *queue_group; |
| 5102 | pqi_index_t iq_pi; |
| 5103 | pqi_index_t iq_ci; |
| 5104 | |
| 5105 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 5106 | queue_group = &ctrl_info->queue_groups[i]; |
| 5107 | |
| 5108 | rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group); |
| 5109 | if (rc) |
| 5110 | return rc; |
| 5111 | |
| 5112 | for (path = 0; path < 2; path++) { |
| 5113 | iq_pi = queue_group->iq_pi_copy[path]; |
| 5114 | |
| 5115 | while (1) { |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 5116 | iq_ci = readl(queue_group->iq_ci[path]); |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5117 | if (iq_ci == iq_pi) |
| 5118 | break; |
| 5119 | pqi_check_ctrl_health(ctrl_info); |
| 5120 | if (pqi_ctrl_offline(ctrl_info)) |
| 5121 | return -ENXIO; |
| 5122 | usleep_range(1000, 2000); |
| 5123 | } |
| 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | return 0; |
| 5128 | } |
| 5129 | |
| 5130 | static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info, |
| 5131 | struct pqi_scsi_dev *device) |
| 5132 | { |
| 5133 | unsigned int i; |
| 5134 | unsigned int path; |
| 5135 | struct pqi_queue_group *queue_group; |
| 5136 | unsigned long flags; |
| 5137 | struct pqi_io_request *io_request; |
| 5138 | struct pqi_io_request *next; |
| 5139 | struct scsi_cmnd *scmd; |
| 5140 | struct pqi_scsi_dev *scsi_device; |
| 5141 | |
| 5142 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 5143 | queue_group = &ctrl_info->queue_groups[i]; |
| 5144 | |
| 5145 | for (path = 0; path < 2; path++) { |
| 5146 | spin_lock_irqsave( |
| 5147 | &queue_group->submit_lock[path], flags); |
| 5148 | |
| 5149 | list_for_each_entry_safe(io_request, next, |
| 5150 | &queue_group->request_list[path], |
| 5151 | request_list_entry) { |
| 5152 | scmd = io_request->scmd; |
| 5153 | if (!scmd) |
| 5154 | continue; |
| 5155 | |
| 5156 | scsi_device = scmd->device->hostdata; |
| 5157 | if (scsi_device != device) |
| 5158 | continue; |
| 5159 | |
| 5160 | list_del(&io_request->request_list_entry); |
| 5161 | set_host_byte(scmd, DID_RESET); |
| 5162 | pqi_scsi_done(scmd); |
| 5163 | } |
| 5164 | |
| 5165 | spin_unlock_irqrestore( |
| 5166 | &queue_group->submit_lock[path], flags); |
| 5167 | } |
| 5168 | } |
| 5169 | } |
| 5170 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 5171 | static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info, |
| 5172 | struct pqi_scsi_dev *device) |
| 5173 | { |
| 5174 | while (atomic_read(&device->scsi_cmds_outstanding)) { |
| 5175 | pqi_check_ctrl_health(ctrl_info); |
| 5176 | if (pqi_ctrl_offline(ctrl_info)) |
| 5177 | return -ENXIO; |
| 5178 | usleep_range(1000, 2000); |
| 5179 | } |
| 5180 | |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
| 5184 | static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info) |
| 5185 | { |
| 5186 | bool io_pending; |
| 5187 | unsigned long flags; |
| 5188 | struct pqi_scsi_dev *device; |
| 5189 | |
| 5190 | while (1) { |
| 5191 | io_pending = false; |
| 5192 | |
| 5193 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
| 5194 | list_for_each_entry(device, &ctrl_info->scsi_device_list, |
| 5195 | scsi_device_list_entry) { |
| 5196 | if (atomic_read(&device->scsi_cmds_outstanding)) { |
| 5197 | io_pending = true; |
| 5198 | break; |
| 5199 | } |
| 5200 | } |
| 5201 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, |
| 5202 | flags); |
| 5203 | |
| 5204 | if (!io_pending) |
| 5205 | break; |
| 5206 | |
| 5207 | pqi_check_ctrl_health(ctrl_info); |
| 5208 | if (pqi_ctrl_offline(ctrl_info)) |
| 5209 | return -ENXIO; |
| 5210 | |
| 5211 | usleep_range(1000, 2000); |
| 5212 | } |
| 5213 | |
| 5214 | return 0; |
| 5215 | } |
| 5216 | |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5217 | static void pqi_lun_reset_complete(struct pqi_io_request *io_request, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5218 | void *context) |
| 5219 | { |
| 5220 | struct completion *waiting = context; |
| 5221 | |
| 5222 | complete(waiting); |
| 5223 | } |
| 5224 | |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5225 | #define PQI_LUN_RESET_TIMEOUT_SECS 10 |
| 5226 | |
| 5227 | static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info, |
| 5228 | struct pqi_scsi_dev *device, struct completion *wait) |
| 5229 | { |
| 5230 | int rc; |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5231 | |
| 5232 | while (1) { |
| 5233 | if (wait_for_completion_io_timeout(wait, |
| 5234 | PQI_LUN_RESET_TIMEOUT_SECS * HZ)) { |
| 5235 | rc = 0; |
| 5236 | break; |
| 5237 | } |
| 5238 | |
| 5239 | pqi_check_ctrl_health(ctrl_info); |
| 5240 | if (pqi_ctrl_offline(ctrl_info)) { |
Kevin Barnett | 4e8415e | 2017-05-03 18:54:18 -0500 | [diff] [blame] | 5241 | rc = -ENXIO; |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5242 | break; |
| 5243 | } |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | return rc; |
| 5247 | } |
| 5248 | |
| 5249 | static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5250 | struct pqi_scsi_dev *device) |
| 5251 | { |
| 5252 | int rc; |
| 5253 | struct pqi_io_request *io_request; |
| 5254 | DECLARE_COMPLETION_ONSTACK(wait); |
| 5255 | struct pqi_task_management_request *request; |
| 5256 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5257 | io_request = pqi_alloc_io_request(ctrl_info); |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5258 | io_request->io_complete_callback = pqi_lun_reset_complete; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5259 | io_request->context = &wait; |
| 5260 | |
| 5261 | request = io_request->iu; |
| 5262 | memset(request, 0, sizeof(*request)); |
| 5263 | |
| 5264 | request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT; |
| 5265 | put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH, |
| 5266 | &request->header.iu_length); |
| 5267 | put_unaligned_le16(io_request->index, &request->request_id); |
| 5268 | memcpy(request->lun_number, device->scsi3addr, |
| 5269 | sizeof(request->lun_number)); |
| 5270 | request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET; |
| 5271 | |
| 5272 | pqi_start_io(ctrl_info, |
| 5273 | &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH, |
| 5274 | io_request); |
| 5275 | |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5276 | rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait); |
| 5277 | if (rc == 0) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5278 | rc = io_request->status; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5279 | |
| 5280 | pqi_free_io_request(io_request); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5281 | |
| 5282 | return rc; |
| 5283 | } |
| 5284 | |
Mahesh Rajashekhara | 3406384 | 2018-12-07 16:28:16 -0600 | [diff] [blame] | 5285 | #define PQI_LUN_RESET_RETRIES 3 |
| 5286 | #define PQI_LUN_RESET_RETRY_INTERVAL_MSECS 10000 |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5287 | /* Performs a reset at the LUN level. */ |
| 5288 | |
| 5289 | static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info, |
| 5290 | struct pqi_scsi_dev *device) |
| 5291 | { |
| 5292 | int rc; |
Mahesh Rajashekhara | 3406384 | 2018-12-07 16:28:16 -0600 | [diff] [blame] | 5293 | unsigned int retries; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5294 | |
Mahesh Rajashekhara | 3406384 | 2018-12-07 16:28:16 -0600 | [diff] [blame] | 5295 | for (retries = 0;;) { |
| 5296 | rc = pqi_lun_reset(ctrl_info, device); |
| 5297 | if (rc != -EAGAIN || |
| 5298 | ++retries > PQI_LUN_RESET_RETRIES) |
| 5299 | break; |
| 5300 | msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS); |
| 5301 | } |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 5302 | if (rc == 0) |
| 5303 | rc = pqi_device_wait_for_pending_io(ctrl_info, device); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5304 | |
Kevin Barnett | 14bb215 | 2016-08-31 14:54:35 -0500 | [diff] [blame] | 5305 | return rc == 0 ? SUCCESS : FAILED; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd) |
| 5309 | { |
| 5310 | int rc; |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5311 | struct Scsi_Host *shost; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5312 | struct pqi_ctrl_info *ctrl_info; |
| 5313 | struct pqi_scsi_dev *device; |
| 5314 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5315 | shost = scmd->device->host; |
| 5316 | ctrl_info = shost_to_hba(shost); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5317 | device = scmd->device->hostdata; |
| 5318 | |
| 5319 | dev_err(&ctrl_info->pci_dev->dev, |
| 5320 | "resetting scsi %d:%d:%d:%d\n", |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5321 | shost->host_no, device->bus, device->target, device->lun); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5322 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5323 | pqi_check_ctrl_health(ctrl_info); |
| 5324 | if (pqi_ctrl_offline(ctrl_info)) { |
| 5325 | rc = FAILED; |
| 5326 | goto out; |
| 5327 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5328 | |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5329 | mutex_lock(&ctrl_info->lun_reset_mutex); |
| 5330 | |
| 5331 | pqi_ctrl_block_requests(ctrl_info); |
| 5332 | pqi_ctrl_wait_until_quiesced(ctrl_info); |
| 5333 | pqi_fail_io_queued_for_device(ctrl_info, device); |
| 5334 | rc = pqi_wait_until_inbound_queues_empty(ctrl_info); |
| 5335 | pqi_device_reset_start(device); |
| 5336 | pqi_ctrl_unblock_requests(ctrl_info); |
| 5337 | |
| 5338 | if (rc) |
| 5339 | rc = FAILED; |
| 5340 | else |
| 5341 | rc = pqi_device_reset(ctrl_info, device); |
| 5342 | |
| 5343 | pqi_device_reset_done(device); |
| 5344 | |
| 5345 | mutex_unlock(&ctrl_info->lun_reset_mutex); |
| 5346 | |
| 5347 | out: |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5348 | dev_err(&ctrl_info->pci_dev->dev, |
| 5349 | "reset of scsi %d:%d:%d:%d: %s\n", |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 5350 | shost->host_no, device->bus, device->target, device->lun, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5351 | rc == SUCCESS ? "SUCCESS" : "FAILED"); |
| 5352 | |
| 5353 | return rc; |
| 5354 | } |
| 5355 | |
| 5356 | static int pqi_slave_alloc(struct scsi_device *sdev) |
| 5357 | { |
| 5358 | struct pqi_scsi_dev *device; |
| 5359 | unsigned long flags; |
| 5360 | struct pqi_ctrl_info *ctrl_info; |
| 5361 | struct scsi_target *starget; |
| 5362 | struct sas_rphy *rphy; |
| 5363 | |
| 5364 | ctrl_info = shost_to_hba(sdev->host); |
| 5365 | |
| 5366 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
| 5367 | |
| 5368 | if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) { |
| 5369 | starget = scsi_target(sdev); |
| 5370 | rphy = target_to_rphy(starget); |
| 5371 | device = pqi_find_device_by_sas_rphy(ctrl_info, rphy); |
| 5372 | if (device) { |
| 5373 | device->target = sdev_id(sdev); |
| 5374 | device->lun = sdev->lun; |
| 5375 | device->target_lun_valid = true; |
| 5376 | } |
| 5377 | } else { |
| 5378 | device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev), |
| 5379 | sdev_id(sdev), sdev->lun); |
| 5380 | } |
| 5381 | |
Kevin Barnett | 94086f5 | 2017-05-03 18:54:31 -0500 | [diff] [blame] | 5382 | if (device) { |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5383 | sdev->hostdata = device; |
| 5384 | device->sdev = sdev; |
| 5385 | if (device->queue_depth) { |
| 5386 | device->advertised_queue_depth = device->queue_depth; |
| 5387 | scsi_change_queue_depth(sdev, |
| 5388 | device->advertised_queue_depth); |
| 5389 | } |
Dave Carroll | b6e2ef6 | 2018-12-07 16:28:23 -0600 | [diff] [blame] | 5390 | if (pqi_is_logical_device(device)) |
| 5391 | pqi_disable_write_same(sdev); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags); |
| 5395 | |
| 5396 | return 0; |
| 5397 | } |
| 5398 | |
Christoph Hellwig | 5219822 | 2016-11-01 08:12:49 -0600 | [diff] [blame] | 5399 | static int pqi_map_queues(struct Scsi_Host *shost) |
| 5400 | { |
| 5401 | struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost); |
| 5402 | |
Keith Busch | f23f5bec | 2018-03-27 09:39:06 -0600 | [diff] [blame] | 5403 | return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev, 0); |
Christoph Hellwig | 5219822 | 2016-11-01 08:12:49 -0600 | [diff] [blame] | 5404 | } |
| 5405 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5406 | static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info, |
| 5407 | void __user *arg) |
| 5408 | { |
| 5409 | struct pci_dev *pci_dev; |
| 5410 | u32 subsystem_vendor; |
| 5411 | u32 subsystem_device; |
| 5412 | cciss_pci_info_struct pciinfo; |
| 5413 | |
| 5414 | if (!arg) |
| 5415 | return -EINVAL; |
| 5416 | |
| 5417 | pci_dev = ctrl_info->pci_dev; |
| 5418 | |
| 5419 | pciinfo.domain = pci_domain_nr(pci_dev->bus); |
| 5420 | pciinfo.bus = pci_dev->bus->number; |
| 5421 | pciinfo.dev_fn = pci_dev->devfn; |
| 5422 | subsystem_vendor = pci_dev->subsystem_vendor; |
| 5423 | subsystem_device = pci_dev->subsystem_device; |
| 5424 | pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) | |
| 5425 | subsystem_vendor; |
| 5426 | |
| 5427 | if (copy_to_user(arg, &pciinfo, sizeof(pciinfo))) |
| 5428 | return -EFAULT; |
| 5429 | |
| 5430 | return 0; |
| 5431 | } |
| 5432 | |
| 5433 | static int pqi_getdrivver_ioctl(void __user *arg) |
| 5434 | { |
| 5435 | u32 version; |
| 5436 | |
| 5437 | if (!arg) |
| 5438 | return -EINVAL; |
| 5439 | |
| 5440 | version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) | |
| 5441 | (DRIVER_RELEASE << 16) | DRIVER_REVISION; |
| 5442 | |
| 5443 | if (copy_to_user(arg, &version, sizeof(version))) |
| 5444 | return -EFAULT; |
| 5445 | |
| 5446 | return 0; |
| 5447 | } |
| 5448 | |
| 5449 | struct ciss_error_info { |
| 5450 | u8 scsi_status; |
| 5451 | int command_status; |
| 5452 | size_t sense_data_length; |
| 5453 | }; |
| 5454 | |
| 5455 | static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info, |
| 5456 | struct ciss_error_info *ciss_error_info) |
| 5457 | { |
| 5458 | int ciss_cmd_status; |
| 5459 | size_t sense_data_length; |
| 5460 | |
| 5461 | switch (pqi_error_info->data_out_result) { |
| 5462 | case PQI_DATA_IN_OUT_GOOD: |
| 5463 | ciss_cmd_status = CISS_CMD_STATUS_SUCCESS; |
| 5464 | break; |
| 5465 | case PQI_DATA_IN_OUT_UNDERFLOW: |
| 5466 | ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN; |
| 5467 | break; |
| 5468 | case PQI_DATA_IN_OUT_BUFFER_OVERFLOW: |
| 5469 | ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN; |
| 5470 | break; |
| 5471 | case PQI_DATA_IN_OUT_PROTOCOL_ERROR: |
| 5472 | case PQI_DATA_IN_OUT_BUFFER_ERROR: |
| 5473 | case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA: |
| 5474 | case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE: |
| 5475 | case PQI_DATA_IN_OUT_ERROR: |
| 5476 | ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR; |
| 5477 | break; |
| 5478 | case PQI_DATA_IN_OUT_HARDWARE_ERROR: |
| 5479 | case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR: |
| 5480 | case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT: |
| 5481 | case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED: |
| 5482 | case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED: |
| 5483 | case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED: |
| 5484 | case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST: |
| 5485 | case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION: |
| 5486 | case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED: |
| 5487 | case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ: |
| 5488 | ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR; |
| 5489 | break; |
| 5490 | case PQI_DATA_IN_OUT_UNSOLICITED_ABORT: |
| 5491 | ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT; |
| 5492 | break; |
| 5493 | case PQI_DATA_IN_OUT_ABORTED: |
| 5494 | ciss_cmd_status = CISS_CMD_STATUS_ABORTED; |
| 5495 | break; |
| 5496 | case PQI_DATA_IN_OUT_TIMEOUT: |
| 5497 | ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT; |
| 5498 | break; |
| 5499 | default: |
| 5500 | ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS; |
| 5501 | break; |
| 5502 | } |
| 5503 | |
| 5504 | sense_data_length = |
| 5505 | get_unaligned_le16(&pqi_error_info->sense_data_length); |
| 5506 | if (sense_data_length == 0) |
| 5507 | sense_data_length = |
| 5508 | get_unaligned_le16(&pqi_error_info->response_data_length); |
| 5509 | if (sense_data_length) |
| 5510 | if (sense_data_length > sizeof(pqi_error_info->data)) |
| 5511 | sense_data_length = sizeof(pqi_error_info->data); |
| 5512 | |
| 5513 | ciss_error_info->scsi_status = pqi_error_info->status; |
| 5514 | ciss_error_info->command_status = ciss_cmd_status; |
| 5515 | ciss_error_info->sense_data_length = sense_data_length; |
| 5516 | } |
| 5517 | |
| 5518 | static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg) |
| 5519 | { |
| 5520 | int rc; |
| 5521 | char *kernel_buffer = NULL; |
| 5522 | u16 iu_length; |
| 5523 | size_t sense_data_length; |
| 5524 | IOCTL_Command_struct iocommand; |
| 5525 | struct pqi_raid_path_request request; |
| 5526 | struct pqi_raid_error_info pqi_error_info; |
| 5527 | struct ciss_error_info ciss_error_info; |
| 5528 | |
| 5529 | if (pqi_ctrl_offline(ctrl_info)) |
| 5530 | return -ENXIO; |
| 5531 | if (!arg) |
| 5532 | return -EINVAL; |
| 5533 | if (!capable(CAP_SYS_RAWIO)) |
| 5534 | return -EPERM; |
| 5535 | if (copy_from_user(&iocommand, arg, sizeof(iocommand))) |
| 5536 | return -EFAULT; |
| 5537 | if (iocommand.buf_size < 1 && |
| 5538 | iocommand.Request.Type.Direction != XFER_NONE) |
| 5539 | return -EINVAL; |
| 5540 | if (iocommand.Request.CDBLen > sizeof(request.cdb)) |
| 5541 | return -EINVAL; |
| 5542 | if (iocommand.Request.Type.Type != TYPE_CMD) |
| 5543 | return -EINVAL; |
| 5544 | |
| 5545 | switch (iocommand.Request.Type.Direction) { |
| 5546 | case XFER_NONE: |
| 5547 | case XFER_WRITE: |
| 5548 | case XFER_READ: |
Kevin Barnett | 41555d5 | 2017-08-10 13:46:51 -0500 | [diff] [blame] | 5549 | case XFER_READ | XFER_WRITE: |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5550 | break; |
| 5551 | default: |
| 5552 | return -EINVAL; |
| 5553 | } |
| 5554 | |
| 5555 | if (iocommand.buf_size > 0) { |
| 5556 | kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL); |
| 5557 | if (!kernel_buffer) |
| 5558 | return -ENOMEM; |
| 5559 | if (iocommand.Request.Type.Direction & XFER_WRITE) { |
| 5560 | if (copy_from_user(kernel_buffer, iocommand.buf, |
| 5561 | iocommand.buf_size)) { |
| 5562 | rc = -EFAULT; |
| 5563 | goto out; |
| 5564 | } |
| 5565 | } else { |
| 5566 | memset(kernel_buffer, 0, iocommand.buf_size); |
| 5567 | } |
| 5568 | } |
| 5569 | |
| 5570 | memset(&request, 0, sizeof(request)); |
| 5571 | |
| 5572 | request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO; |
| 5573 | iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) - |
| 5574 | PQI_REQUEST_HEADER_LENGTH; |
| 5575 | memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes, |
| 5576 | sizeof(request.lun_number)); |
| 5577 | memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen); |
| 5578 | request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0; |
| 5579 | |
| 5580 | switch (iocommand.Request.Type.Direction) { |
| 5581 | case XFER_NONE: |
| 5582 | request.data_direction = SOP_NO_DIRECTION_FLAG; |
| 5583 | break; |
| 5584 | case XFER_WRITE: |
| 5585 | request.data_direction = SOP_WRITE_FLAG; |
| 5586 | break; |
| 5587 | case XFER_READ: |
| 5588 | request.data_direction = SOP_READ_FLAG; |
| 5589 | break; |
Kevin Barnett | 41555d5 | 2017-08-10 13:46:51 -0500 | [diff] [blame] | 5590 | case XFER_READ | XFER_WRITE: |
| 5591 | request.data_direction = SOP_BIDIRECTIONAL; |
| 5592 | break; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE; |
| 5596 | |
| 5597 | if (iocommand.buf_size > 0) { |
| 5598 | put_unaligned_le32(iocommand.buf_size, &request.buffer_length); |
| 5599 | |
| 5600 | rc = pqi_map_single(ctrl_info->pci_dev, |
| 5601 | &request.sg_descriptors[0], kernel_buffer, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 5602 | iocommand.buf_size, DMA_BIDIRECTIONAL); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5603 | if (rc) |
| 5604 | goto out; |
| 5605 | |
| 5606 | iu_length += sizeof(request.sg_descriptors[0]); |
| 5607 | } |
| 5608 | |
| 5609 | put_unaligned_le16(iu_length, &request.header.iu_length); |
| 5610 | |
| 5611 | rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
| 5612 | PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT); |
| 5613 | |
| 5614 | if (iocommand.buf_size > 0) |
| 5615 | pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, |
Christoph Hellwig | 6917a9c | 2018-10-11 09:47:59 +0200 | [diff] [blame] | 5616 | DMA_BIDIRECTIONAL); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5617 | |
| 5618 | memset(&iocommand.error_info, 0, sizeof(iocommand.error_info)); |
| 5619 | |
| 5620 | if (rc == 0) { |
| 5621 | pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info); |
| 5622 | iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status; |
| 5623 | iocommand.error_info.CommandStatus = |
| 5624 | ciss_error_info.command_status; |
| 5625 | sense_data_length = ciss_error_info.sense_data_length; |
| 5626 | if (sense_data_length) { |
| 5627 | if (sense_data_length > |
| 5628 | sizeof(iocommand.error_info.SenseInfo)) |
| 5629 | sense_data_length = |
| 5630 | sizeof(iocommand.error_info.SenseInfo); |
| 5631 | memcpy(iocommand.error_info.SenseInfo, |
| 5632 | pqi_error_info.data, sense_data_length); |
| 5633 | iocommand.error_info.SenseLen = sense_data_length; |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | if (copy_to_user(arg, &iocommand, sizeof(iocommand))) { |
| 5638 | rc = -EFAULT; |
| 5639 | goto out; |
| 5640 | } |
| 5641 | |
| 5642 | if (rc == 0 && iocommand.buf_size > 0 && |
| 5643 | (iocommand.Request.Type.Direction & XFER_READ)) { |
| 5644 | if (copy_to_user(iocommand.buf, kernel_buffer, |
| 5645 | iocommand.buf_size)) { |
| 5646 | rc = -EFAULT; |
| 5647 | } |
| 5648 | } |
| 5649 | |
| 5650 | out: |
| 5651 | kfree(kernel_buffer); |
| 5652 | |
| 5653 | return rc; |
| 5654 | } |
| 5655 | |
| 5656 | static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg) |
| 5657 | { |
| 5658 | int rc; |
| 5659 | struct pqi_ctrl_info *ctrl_info; |
| 5660 | |
| 5661 | ctrl_info = shost_to_hba(sdev->host); |
| 5662 | |
| 5663 | switch (cmd) { |
| 5664 | case CCISS_DEREGDISK: |
| 5665 | case CCISS_REGNEWDISK: |
| 5666 | case CCISS_REGNEWD: |
| 5667 | rc = pqi_scan_scsi_devices(ctrl_info); |
| 5668 | break; |
| 5669 | case CCISS_GETPCIINFO: |
| 5670 | rc = pqi_getpciinfo_ioctl(ctrl_info, arg); |
| 5671 | break; |
| 5672 | case CCISS_GETDRIVVER: |
| 5673 | rc = pqi_getdrivver_ioctl(arg); |
| 5674 | break; |
| 5675 | case CCISS_PASSTHRU: |
| 5676 | rc = pqi_passthru_ioctl(ctrl_info, arg); |
| 5677 | break; |
| 5678 | default: |
| 5679 | rc = -EINVAL; |
| 5680 | break; |
| 5681 | } |
| 5682 | |
| 5683 | return rc; |
| 5684 | } |
| 5685 | |
| 5686 | static ssize_t pqi_version_show(struct device *dev, |
| 5687 | struct device_attribute *attr, char *buffer) |
| 5688 | { |
| 5689 | ssize_t count = 0; |
| 5690 | struct Scsi_Host *shost; |
| 5691 | struct pqi_ctrl_info *ctrl_info; |
| 5692 | |
| 5693 | shost = class_to_shost(dev); |
| 5694 | ctrl_info = shost_to_hba(shost); |
| 5695 | |
| 5696 | count += snprintf(buffer + count, PAGE_SIZE - count, |
| 5697 | " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP); |
| 5698 | |
| 5699 | count += snprintf(buffer + count, PAGE_SIZE - count, |
| 5700 | "firmware: %s\n", ctrl_info->firmware_version); |
| 5701 | |
| 5702 | return count; |
| 5703 | } |
| 5704 | |
| 5705 | static ssize_t pqi_host_rescan_store(struct device *dev, |
| 5706 | struct device_attribute *attr, const char *buffer, size_t count) |
| 5707 | { |
| 5708 | struct Scsi_Host *shost = class_to_shost(dev); |
| 5709 | |
| 5710 | pqi_scan_start(shost); |
| 5711 | |
| 5712 | return count; |
| 5713 | } |
| 5714 | |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 5715 | static ssize_t pqi_lockup_action_show(struct device *dev, |
| 5716 | struct device_attribute *attr, char *buffer) |
| 5717 | { |
| 5718 | int count = 0; |
| 5719 | unsigned int i; |
| 5720 | |
| 5721 | for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) { |
| 5722 | if (pqi_lockup_actions[i].action == pqi_lockup_action) |
| 5723 | count += snprintf(buffer + count, PAGE_SIZE - count, |
| 5724 | "[%s] ", pqi_lockup_actions[i].name); |
| 5725 | else |
| 5726 | count += snprintf(buffer + count, PAGE_SIZE - count, |
| 5727 | "%s ", pqi_lockup_actions[i].name); |
| 5728 | } |
| 5729 | |
| 5730 | count += snprintf(buffer + count, PAGE_SIZE - count, "\n"); |
| 5731 | |
| 5732 | return count; |
| 5733 | } |
| 5734 | |
| 5735 | static ssize_t pqi_lockup_action_store(struct device *dev, |
| 5736 | struct device_attribute *attr, const char *buffer, size_t count) |
| 5737 | { |
| 5738 | unsigned int i; |
| 5739 | char *action_name; |
| 5740 | char action_name_buffer[32]; |
| 5741 | |
| 5742 | strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer)); |
| 5743 | action_name = strstrip(action_name_buffer); |
| 5744 | |
| 5745 | for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) { |
| 5746 | if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) { |
| 5747 | pqi_lockup_action = pqi_lockup_actions[i].action; |
| 5748 | return count; |
| 5749 | } |
| 5750 | } |
| 5751 | |
| 5752 | return -EINVAL; |
| 5753 | } |
| 5754 | |
Kevin Barnett | cbe0c7b | 2017-05-03 18:53:48 -0500 | [diff] [blame] | 5755 | static DEVICE_ATTR(version, 0444, pqi_version_show, NULL); |
| 5756 | static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store); |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 5757 | static DEVICE_ATTR(lockup_action, 0644, |
| 5758 | pqi_lockup_action_show, pqi_lockup_action_store); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5759 | |
| 5760 | static struct device_attribute *pqi_shost_attrs[] = { |
| 5761 | &dev_attr_version, |
| 5762 | &dev_attr_rescan, |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 5763 | &dev_attr_lockup_action, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5764 | NULL |
| 5765 | }; |
| 5766 | |
| 5767 | static ssize_t pqi_sas_address_show(struct device *dev, |
| 5768 | struct device_attribute *attr, char *buffer) |
| 5769 | { |
| 5770 | struct pqi_ctrl_info *ctrl_info; |
| 5771 | struct scsi_device *sdev; |
| 5772 | struct pqi_scsi_dev *device; |
| 5773 | unsigned long flags; |
| 5774 | u64 sas_address; |
| 5775 | |
| 5776 | sdev = to_scsi_device(dev); |
| 5777 | ctrl_info = shost_to_hba(sdev->host); |
| 5778 | |
| 5779 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
| 5780 | |
| 5781 | device = sdev->hostdata; |
| 5782 | if (pqi_is_logical_device(device)) { |
| 5783 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, |
| 5784 | flags); |
| 5785 | return -ENODEV; |
| 5786 | } |
| 5787 | sas_address = device->sas_address; |
| 5788 | |
| 5789 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags); |
| 5790 | |
| 5791 | return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address); |
| 5792 | } |
| 5793 | |
| 5794 | static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev, |
| 5795 | struct device_attribute *attr, char *buffer) |
| 5796 | { |
| 5797 | struct pqi_ctrl_info *ctrl_info; |
| 5798 | struct scsi_device *sdev; |
| 5799 | struct pqi_scsi_dev *device; |
| 5800 | unsigned long flags; |
| 5801 | |
| 5802 | sdev = to_scsi_device(dev); |
| 5803 | ctrl_info = shost_to_hba(sdev->host); |
| 5804 | |
| 5805 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
| 5806 | |
| 5807 | device = sdev->hostdata; |
Kevin Barnett | 588a63fe | 2017-05-03 18:55:25 -0500 | [diff] [blame] | 5808 | buffer[0] = device->raid_bypass_enabled ? '1' : '0'; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5809 | buffer[1] = '\n'; |
| 5810 | buffer[2] = '\0'; |
| 5811 | |
| 5812 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags); |
| 5813 | |
| 5814 | return 2; |
| 5815 | } |
| 5816 | |
Kevin Barnett | a9f9339 | 2017-05-03 18:55:31 -0500 | [diff] [blame] | 5817 | static ssize_t pqi_raid_level_show(struct device *dev, |
| 5818 | struct device_attribute *attr, char *buffer) |
| 5819 | { |
| 5820 | struct pqi_ctrl_info *ctrl_info; |
| 5821 | struct scsi_device *sdev; |
| 5822 | struct pqi_scsi_dev *device; |
| 5823 | unsigned long flags; |
| 5824 | char *raid_level; |
| 5825 | |
| 5826 | sdev = to_scsi_device(dev); |
| 5827 | ctrl_info = shost_to_hba(sdev->host); |
| 5828 | |
| 5829 | spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags); |
| 5830 | |
| 5831 | device = sdev->hostdata; |
| 5832 | |
| 5833 | if (pqi_is_logical_device(device)) |
| 5834 | raid_level = pqi_raid_level_to_string(device->raid_level); |
| 5835 | else |
| 5836 | raid_level = "N/A"; |
| 5837 | |
| 5838 | spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags); |
| 5839 | |
| 5840 | return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level); |
| 5841 | } |
| 5842 | |
Kevin Barnett | cbe0c7b | 2017-05-03 18:53:48 -0500 | [diff] [blame] | 5843 | static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL); |
| 5844 | static DEVICE_ATTR(ssd_smart_path_enabled, 0444, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5845 | pqi_ssd_smart_path_enabled_show, NULL); |
Kevin Barnett | a9f9339 | 2017-05-03 18:55:31 -0500 | [diff] [blame] | 5846 | static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5847 | |
| 5848 | static struct device_attribute *pqi_sdev_attrs[] = { |
| 5849 | &dev_attr_sas_address, |
| 5850 | &dev_attr_ssd_smart_path_enabled, |
Kevin Barnett | a9f9339 | 2017-05-03 18:55:31 -0500 | [diff] [blame] | 5851 | &dev_attr_raid_level, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5852 | NULL |
| 5853 | }; |
| 5854 | |
| 5855 | static struct scsi_host_template pqi_driver_template = { |
| 5856 | .module = THIS_MODULE, |
| 5857 | .name = DRIVER_NAME_SHORT, |
| 5858 | .proc_name = DRIVER_NAME_SHORT, |
| 5859 | .queuecommand = pqi_scsi_queue_command, |
| 5860 | .scan_start = pqi_scan_start, |
| 5861 | .scan_finished = pqi_scan_finished, |
| 5862 | .this_id = -1, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5863 | .eh_device_reset_handler = pqi_eh_device_reset_handler, |
| 5864 | .ioctl = pqi_ioctl, |
| 5865 | .slave_alloc = pqi_slave_alloc, |
Christoph Hellwig | 5219822 | 2016-11-01 08:12:49 -0600 | [diff] [blame] | 5866 | .map_queues = pqi_map_queues, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5867 | .sdev_attrs = pqi_sdev_attrs, |
| 5868 | .shost_attrs = pqi_shost_attrs, |
| 5869 | }; |
| 5870 | |
| 5871 | static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info) |
| 5872 | { |
| 5873 | int rc; |
| 5874 | struct Scsi_Host *shost; |
| 5875 | |
| 5876 | shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info)); |
| 5877 | if (!shost) { |
| 5878 | dev_err(&ctrl_info->pci_dev->dev, |
| 5879 | "scsi_host_alloc failed for controller %u\n", |
| 5880 | ctrl_info->ctrl_id); |
| 5881 | return -ENOMEM; |
| 5882 | } |
| 5883 | |
| 5884 | shost->io_port = 0; |
| 5885 | shost->n_io_port = 0; |
| 5886 | shost->this_id = -1; |
| 5887 | shost->max_channel = PQI_MAX_BUS; |
| 5888 | shost->max_cmd_len = MAX_COMMAND_SIZE; |
| 5889 | shost->max_lun = ~0; |
| 5890 | shost->max_id = ~0; |
| 5891 | shost->max_sectors = ctrl_info->max_sectors; |
| 5892 | shost->can_queue = ctrl_info->scsi_ml_can_queue; |
| 5893 | shost->cmd_per_lun = shost->can_queue; |
| 5894 | shost->sg_tablesize = ctrl_info->sg_tablesize; |
| 5895 | shost->transportt = pqi_sas_transport_template; |
Christoph Hellwig | 5219822 | 2016-11-01 08:12:49 -0600 | [diff] [blame] | 5896 | shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5897 | shost->unique_id = shost->irq; |
| 5898 | shost->nr_hw_queues = ctrl_info->num_queue_groups; |
| 5899 | shost->hostdata[0] = (unsigned long)ctrl_info; |
| 5900 | |
| 5901 | rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev); |
| 5902 | if (rc) { |
| 5903 | dev_err(&ctrl_info->pci_dev->dev, |
| 5904 | "scsi_add_host failed for controller %u\n", |
| 5905 | ctrl_info->ctrl_id); |
| 5906 | goto free_host; |
| 5907 | } |
| 5908 | |
| 5909 | rc = pqi_add_sas_host(shost, ctrl_info); |
| 5910 | if (rc) { |
| 5911 | dev_err(&ctrl_info->pci_dev->dev, |
| 5912 | "add SAS host failed for controller %u\n", |
| 5913 | ctrl_info->ctrl_id); |
| 5914 | goto remove_host; |
| 5915 | } |
| 5916 | |
| 5917 | ctrl_info->scsi_host = shost; |
| 5918 | |
| 5919 | return 0; |
| 5920 | |
| 5921 | remove_host: |
| 5922 | scsi_remove_host(shost); |
| 5923 | free_host: |
| 5924 | scsi_host_put(shost); |
| 5925 | |
| 5926 | return rc; |
| 5927 | } |
| 5928 | |
| 5929 | static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info) |
| 5930 | { |
| 5931 | struct Scsi_Host *shost; |
| 5932 | |
| 5933 | pqi_delete_sas_host(ctrl_info); |
| 5934 | |
| 5935 | shost = ctrl_info->scsi_host; |
| 5936 | if (!shost) |
| 5937 | return; |
| 5938 | |
| 5939 | scsi_remove_host(shost); |
| 5940 | scsi_host_put(shost); |
| 5941 | } |
| 5942 | |
Kevin Barnett | 336b681 | 2017-08-10 13:46:39 -0500 | [diff] [blame] | 5943 | static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info) |
| 5944 | { |
| 5945 | int rc = 0; |
| 5946 | struct pqi_device_registers __iomem *pqi_registers; |
| 5947 | unsigned long timeout; |
| 5948 | unsigned int timeout_msecs; |
| 5949 | union pqi_reset_register reset_reg; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5950 | |
Kevin Barnett | 336b681 | 2017-08-10 13:46:39 -0500 | [diff] [blame] | 5951 | pqi_registers = ctrl_info->pqi_registers; |
| 5952 | timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100; |
| 5953 | timeout = msecs_to_jiffies(timeout_msecs) + jiffies; |
| 5954 | |
| 5955 | while (1) { |
| 5956 | msleep(PQI_RESET_POLL_INTERVAL_MSECS); |
| 5957 | reset_reg.all_bits = readl(&pqi_registers->device_reset); |
| 5958 | if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED) |
| 5959 | break; |
| 5960 | pqi_check_ctrl_health(ctrl_info); |
| 5961 | if (pqi_ctrl_offline(ctrl_info)) { |
| 5962 | rc = -ENXIO; |
| 5963 | break; |
| 5964 | } |
| 5965 | if (time_after(jiffies, timeout)) { |
| 5966 | rc = -ETIMEDOUT; |
| 5967 | break; |
| 5968 | } |
| 5969 | } |
| 5970 | |
| 5971 | return rc; |
| 5972 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5973 | |
| 5974 | static int pqi_reset(struct pqi_ctrl_info *ctrl_info) |
| 5975 | { |
| 5976 | int rc; |
Kevin Barnett | 336b681 | 2017-08-10 13:46:39 -0500 | [diff] [blame] | 5977 | union pqi_reset_register reset_reg; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5978 | |
Kevin Barnett | 336b681 | 2017-08-10 13:46:39 -0500 | [diff] [blame] | 5979 | if (ctrl_info->pqi_reset_quiesce_supported) { |
| 5980 | rc = sis_pqi_reset_quiesce(ctrl_info); |
| 5981 | if (rc) { |
| 5982 | dev_err(&ctrl_info->pci_dev->dev, |
| 5983 | "PQI reset failed during quiesce with error %d\n", |
| 5984 | rc); |
| 5985 | return rc; |
| 5986 | } |
| 5987 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5988 | |
Kevin Barnett | 336b681 | 2017-08-10 13:46:39 -0500 | [diff] [blame] | 5989 | reset_reg.all_bits = 0; |
| 5990 | reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET; |
| 5991 | reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5992 | |
Kevin Barnett | 336b681 | 2017-08-10 13:46:39 -0500 | [diff] [blame] | 5993 | writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset); |
| 5994 | |
| 5995 | rc = pqi_wait_for_pqi_reset_completion(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5996 | if (rc) |
| 5997 | dev_err(&ctrl_info->pci_dev->dev, |
Kevin Barnett | 336b681 | 2017-08-10 13:46:39 -0500 | [diff] [blame] | 5998 | "PQI reset failed with error %d\n", rc); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 5999 | |
| 6000 | return rc; |
| 6001 | } |
| 6002 | |
| 6003 | static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info) |
| 6004 | { |
| 6005 | int rc; |
| 6006 | struct bmic_identify_controller *identify; |
| 6007 | |
| 6008 | identify = kmalloc(sizeof(*identify), GFP_KERNEL); |
| 6009 | if (!identify) |
| 6010 | return -ENOMEM; |
| 6011 | |
| 6012 | rc = pqi_identify_controller(ctrl_info, identify); |
| 6013 | if (rc) |
| 6014 | goto out; |
| 6015 | |
| 6016 | memcpy(ctrl_info->firmware_version, identify->firmware_version, |
| 6017 | sizeof(identify->firmware_version)); |
| 6018 | ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0'; |
| 6019 | snprintf(ctrl_info->firmware_version + |
| 6020 | strlen(ctrl_info->firmware_version), |
| 6021 | sizeof(ctrl_info->firmware_version), |
| 6022 | "-%u", get_unaligned_le16(&identify->firmware_build_number)); |
| 6023 | |
| 6024 | out: |
| 6025 | kfree(identify); |
| 6026 | |
| 6027 | return rc; |
| 6028 | } |
| 6029 | |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 6030 | struct pqi_config_table_section_info { |
| 6031 | struct pqi_ctrl_info *ctrl_info; |
| 6032 | void *section; |
| 6033 | u32 section_offset; |
| 6034 | void __iomem *section_iomem_addr; |
| 6035 | }; |
| 6036 | |
| 6037 | static inline bool pqi_is_firmware_feature_supported( |
| 6038 | struct pqi_config_table_firmware_features *firmware_features, |
| 6039 | unsigned int bit_position) |
| 6040 | { |
| 6041 | unsigned int byte_index; |
| 6042 | |
| 6043 | byte_index = bit_position / BITS_PER_BYTE; |
| 6044 | |
| 6045 | if (byte_index >= le16_to_cpu(firmware_features->num_elements)) |
| 6046 | return false; |
| 6047 | |
| 6048 | return firmware_features->features_supported[byte_index] & |
| 6049 | (1 << (bit_position % BITS_PER_BYTE)) ? true : false; |
| 6050 | } |
| 6051 | |
| 6052 | static inline bool pqi_is_firmware_feature_enabled( |
| 6053 | struct pqi_config_table_firmware_features *firmware_features, |
| 6054 | void __iomem *firmware_features_iomem_addr, |
| 6055 | unsigned int bit_position) |
| 6056 | { |
| 6057 | unsigned int byte_index; |
| 6058 | u8 __iomem *features_enabled_iomem_addr; |
| 6059 | |
| 6060 | byte_index = (bit_position / BITS_PER_BYTE) + |
| 6061 | (le16_to_cpu(firmware_features->num_elements) * 2); |
| 6062 | |
| 6063 | features_enabled_iomem_addr = firmware_features_iomem_addr + |
| 6064 | offsetof(struct pqi_config_table_firmware_features, |
| 6065 | features_supported) + byte_index; |
| 6066 | |
| 6067 | return *((__force u8 *)features_enabled_iomem_addr) & |
| 6068 | (1 << (bit_position % BITS_PER_BYTE)) ? true : false; |
| 6069 | } |
| 6070 | |
| 6071 | static inline void pqi_request_firmware_feature( |
| 6072 | struct pqi_config_table_firmware_features *firmware_features, |
| 6073 | unsigned int bit_position) |
| 6074 | { |
| 6075 | unsigned int byte_index; |
| 6076 | |
| 6077 | byte_index = (bit_position / BITS_PER_BYTE) + |
| 6078 | le16_to_cpu(firmware_features->num_elements); |
| 6079 | |
| 6080 | firmware_features->features_supported[byte_index] |= |
| 6081 | (1 << (bit_position % BITS_PER_BYTE)); |
| 6082 | } |
| 6083 | |
| 6084 | static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info, |
| 6085 | u16 first_section, u16 last_section) |
| 6086 | { |
| 6087 | struct pqi_vendor_general_request request; |
| 6088 | |
| 6089 | memset(&request, 0, sizeof(request)); |
| 6090 | |
| 6091 | request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL; |
| 6092 | put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH, |
| 6093 | &request.header.iu_length); |
| 6094 | put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE, |
| 6095 | &request.function_code); |
| 6096 | put_unaligned_le16(first_section, |
| 6097 | &request.data.config_table_update.first_section); |
| 6098 | put_unaligned_le16(last_section, |
| 6099 | &request.data.config_table_update.last_section); |
| 6100 | |
| 6101 | return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, |
| 6102 | 0, NULL, NO_TIMEOUT); |
| 6103 | } |
| 6104 | |
| 6105 | static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info, |
| 6106 | struct pqi_config_table_firmware_features *firmware_features, |
| 6107 | void __iomem *firmware_features_iomem_addr) |
| 6108 | { |
| 6109 | void *features_requested; |
| 6110 | void __iomem *features_requested_iomem_addr; |
| 6111 | |
| 6112 | features_requested = firmware_features->features_supported + |
| 6113 | le16_to_cpu(firmware_features->num_elements); |
| 6114 | |
| 6115 | features_requested_iomem_addr = firmware_features_iomem_addr + |
| 6116 | (features_requested - (void *)firmware_features); |
| 6117 | |
| 6118 | memcpy_toio(features_requested_iomem_addr, features_requested, |
| 6119 | le16_to_cpu(firmware_features->num_elements)); |
| 6120 | |
| 6121 | return pqi_config_table_update(ctrl_info, |
| 6122 | PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES, |
| 6123 | PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES); |
| 6124 | } |
| 6125 | |
| 6126 | struct pqi_firmware_feature { |
| 6127 | char *feature_name; |
| 6128 | unsigned int feature_bit; |
| 6129 | bool supported; |
| 6130 | bool enabled; |
| 6131 | void (*feature_status)(struct pqi_ctrl_info *ctrl_info, |
| 6132 | struct pqi_firmware_feature *firmware_feature); |
| 6133 | }; |
| 6134 | |
| 6135 | static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info, |
| 6136 | struct pqi_firmware_feature *firmware_feature) |
| 6137 | { |
| 6138 | if (!firmware_feature->supported) { |
| 6139 | dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n", |
| 6140 | firmware_feature->feature_name); |
| 6141 | return; |
| 6142 | } |
| 6143 | |
| 6144 | if (firmware_feature->enabled) { |
| 6145 | dev_info(&ctrl_info->pci_dev->dev, |
| 6146 | "%s enabled\n", firmware_feature->feature_name); |
| 6147 | return; |
| 6148 | } |
| 6149 | |
| 6150 | dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n", |
| 6151 | firmware_feature->feature_name); |
| 6152 | } |
| 6153 | |
| 6154 | static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info, |
| 6155 | struct pqi_firmware_feature *firmware_feature) |
| 6156 | { |
| 6157 | if (firmware_feature->feature_status) |
| 6158 | firmware_feature->feature_status(ctrl_info, firmware_feature); |
| 6159 | } |
| 6160 | |
| 6161 | static DEFINE_MUTEX(pqi_firmware_features_mutex); |
| 6162 | |
| 6163 | static struct pqi_firmware_feature pqi_firmware_features[] = { |
| 6164 | { |
| 6165 | .feature_name = "Online Firmware Activation", |
| 6166 | .feature_bit = PQI_FIRMWARE_FEATURE_OFA, |
| 6167 | .feature_status = pqi_firmware_feature_status, |
| 6168 | }, |
| 6169 | { |
| 6170 | .feature_name = "Serial Management Protocol", |
| 6171 | .feature_bit = PQI_FIRMWARE_FEATURE_SMP, |
| 6172 | .feature_status = pqi_firmware_feature_status, |
| 6173 | }, |
| 6174 | }; |
| 6175 | |
| 6176 | static void pqi_process_firmware_features( |
| 6177 | struct pqi_config_table_section_info *section_info) |
| 6178 | { |
| 6179 | int rc; |
| 6180 | struct pqi_ctrl_info *ctrl_info; |
| 6181 | struct pqi_config_table_firmware_features *firmware_features; |
| 6182 | void __iomem *firmware_features_iomem_addr; |
| 6183 | unsigned int i; |
| 6184 | unsigned int num_features_supported; |
| 6185 | |
| 6186 | ctrl_info = section_info->ctrl_info; |
| 6187 | firmware_features = section_info->section; |
| 6188 | firmware_features_iomem_addr = section_info->section_iomem_addr; |
| 6189 | |
| 6190 | for (i = 0, num_features_supported = 0; |
| 6191 | i < ARRAY_SIZE(pqi_firmware_features); i++) { |
| 6192 | if (pqi_is_firmware_feature_supported(firmware_features, |
| 6193 | pqi_firmware_features[i].feature_bit)) { |
| 6194 | pqi_firmware_features[i].supported = true; |
| 6195 | num_features_supported++; |
| 6196 | } else { |
| 6197 | pqi_firmware_feature_update(ctrl_info, |
| 6198 | &pqi_firmware_features[i]); |
| 6199 | } |
| 6200 | } |
| 6201 | |
| 6202 | if (num_features_supported == 0) |
| 6203 | return; |
| 6204 | |
| 6205 | for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) { |
| 6206 | if (!pqi_firmware_features[i].supported) |
| 6207 | continue; |
| 6208 | pqi_request_firmware_feature(firmware_features, |
| 6209 | pqi_firmware_features[i].feature_bit); |
| 6210 | } |
| 6211 | |
| 6212 | rc = pqi_enable_firmware_features(ctrl_info, firmware_features, |
| 6213 | firmware_features_iomem_addr); |
| 6214 | if (rc) { |
| 6215 | dev_err(&ctrl_info->pci_dev->dev, |
| 6216 | "failed to enable firmware features in PQI configuration table\n"); |
| 6217 | for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) { |
| 6218 | if (!pqi_firmware_features[i].supported) |
| 6219 | continue; |
| 6220 | pqi_firmware_feature_update(ctrl_info, |
| 6221 | &pqi_firmware_features[i]); |
| 6222 | } |
| 6223 | return; |
| 6224 | } |
| 6225 | |
| 6226 | for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) { |
| 6227 | if (!pqi_firmware_features[i].supported) |
| 6228 | continue; |
| 6229 | if (pqi_is_firmware_feature_enabled(firmware_features, |
| 6230 | firmware_features_iomem_addr, |
| 6231 | pqi_firmware_features[i].feature_bit)) |
| 6232 | pqi_firmware_features[i].enabled = true; |
| 6233 | pqi_firmware_feature_update(ctrl_info, |
| 6234 | &pqi_firmware_features[i]); |
| 6235 | } |
| 6236 | } |
| 6237 | |
| 6238 | static void pqi_init_firmware_features(void) |
| 6239 | { |
| 6240 | unsigned int i; |
| 6241 | |
| 6242 | for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) { |
| 6243 | pqi_firmware_features[i].supported = false; |
| 6244 | pqi_firmware_features[i].enabled = false; |
| 6245 | } |
| 6246 | } |
| 6247 | |
| 6248 | static void pqi_process_firmware_features_section( |
| 6249 | struct pqi_config_table_section_info *section_info) |
| 6250 | { |
| 6251 | mutex_lock(&pqi_firmware_features_mutex); |
| 6252 | pqi_init_firmware_features(); |
| 6253 | pqi_process_firmware_features(section_info); |
| 6254 | mutex_unlock(&pqi_firmware_features_mutex); |
| 6255 | } |
| 6256 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6257 | static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info) |
| 6258 | { |
| 6259 | u32 table_length; |
| 6260 | u32 section_offset; |
| 6261 | void __iomem *table_iomem_addr; |
| 6262 | struct pqi_config_table *config_table; |
| 6263 | struct pqi_config_table_section_header *section; |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 6264 | struct pqi_config_table_section_info section_info; |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6265 | |
| 6266 | table_length = ctrl_info->config_table_length; |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 6267 | if (table_length == 0) |
| 6268 | return 0; |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6269 | |
| 6270 | config_table = kmalloc(table_length, GFP_KERNEL); |
| 6271 | if (!config_table) { |
| 6272 | dev_err(&ctrl_info->pci_dev->dev, |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 6273 | "failed to allocate memory for PQI configuration table\n"); |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6274 | return -ENOMEM; |
| 6275 | } |
| 6276 | |
| 6277 | /* |
| 6278 | * Copy the config table contents from I/O memory space into the |
| 6279 | * temporary buffer. |
| 6280 | */ |
| 6281 | table_iomem_addr = ctrl_info->iomem_base + |
| 6282 | ctrl_info->config_table_offset; |
| 6283 | memcpy_fromio(config_table, table_iomem_addr, table_length); |
| 6284 | |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 6285 | section_info.ctrl_info = ctrl_info; |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6286 | section_offset = |
| 6287 | get_unaligned_le32(&config_table->first_section_offset); |
| 6288 | |
| 6289 | while (section_offset) { |
| 6290 | section = (void *)config_table + section_offset; |
| 6291 | |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 6292 | section_info.section = section; |
| 6293 | section_info.section_offset = section_offset; |
| 6294 | section_info.section_iomem_addr = |
| 6295 | table_iomem_addr + section_offset; |
| 6296 | |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6297 | switch (get_unaligned_le16(§ion->section_id)) { |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 6298 | case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES: |
| 6299 | pqi_process_firmware_features_section(§ion_info); |
| 6300 | break; |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6301 | case PQI_CONFIG_TABLE_SECTION_HEARTBEAT: |
Kevin Barnett | 5a259e3 | 2017-05-03 18:55:43 -0500 | [diff] [blame] | 6302 | if (pqi_disable_heartbeat) |
| 6303 | dev_warn(&ctrl_info->pci_dev->dev, |
| 6304 | "heartbeat disabled by module parameter\n"); |
| 6305 | else |
| 6306 | ctrl_info->heartbeat_counter = |
| 6307 | table_iomem_addr + |
| 6308 | section_offset + |
| 6309 | offsetof( |
| 6310 | struct pqi_config_table_heartbeat, |
| 6311 | heartbeat_counter); |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6312 | break; |
| 6313 | } |
| 6314 | |
| 6315 | section_offset = |
| 6316 | get_unaligned_le16(§ion->next_section_offset); |
| 6317 | } |
| 6318 | |
| 6319 | kfree(config_table); |
| 6320 | |
| 6321 | return 0; |
| 6322 | } |
| 6323 | |
Kevin Barnett | 162d775 | 2017-05-03 18:52:46 -0500 | [diff] [blame] | 6324 | /* Switches the controller from PQI mode back into SIS mode. */ |
| 6325 | |
| 6326 | static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info) |
| 6327 | { |
| 6328 | int rc; |
| 6329 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6330 | pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE); |
Kevin Barnett | 162d775 | 2017-05-03 18:52:46 -0500 | [diff] [blame] | 6331 | rc = pqi_reset(ctrl_info); |
| 6332 | if (rc) |
| 6333 | return rc; |
Kevin Barnett | 4f078e2 | 2017-08-10 13:46:57 -0500 | [diff] [blame] | 6334 | rc = sis_reenable_sis_mode(ctrl_info); |
| 6335 | if (rc) { |
| 6336 | dev_err(&ctrl_info->pci_dev->dev, |
| 6337 | "re-enabling SIS mode failed with error %d\n", rc); |
| 6338 | return rc; |
| 6339 | } |
Kevin Barnett | 162d775 | 2017-05-03 18:52:46 -0500 | [diff] [blame] | 6340 | pqi_save_ctrl_mode(ctrl_info, SIS_MODE); |
| 6341 | |
| 6342 | return 0; |
| 6343 | } |
| 6344 | |
| 6345 | /* |
| 6346 | * If the controller isn't already in SIS mode, this function forces it into |
| 6347 | * SIS mode. |
| 6348 | */ |
| 6349 | |
| 6350 | static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info) |
Kevin Barnett | ff6abb7 | 2016-08-31 14:54:41 -0500 | [diff] [blame] | 6351 | { |
| 6352 | if (!sis_is_firmware_running(ctrl_info)) |
| 6353 | return -ENXIO; |
| 6354 | |
Kevin Barnett | 162d775 | 2017-05-03 18:52:46 -0500 | [diff] [blame] | 6355 | if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE) |
| 6356 | return 0; |
| 6357 | |
| 6358 | if (sis_is_kernel_up(ctrl_info)) { |
| 6359 | pqi_save_ctrl_mode(ctrl_info, SIS_MODE); |
| 6360 | return 0; |
Kevin Barnett | ff6abb7 | 2016-08-31 14:54:41 -0500 | [diff] [blame] | 6361 | } |
| 6362 | |
Kevin Barnett | 162d775 | 2017-05-03 18:52:46 -0500 | [diff] [blame] | 6363 | return pqi_revert_to_sis_mode(ctrl_info); |
Kevin Barnett | ff6abb7 | 2016-08-31 14:54:41 -0500 | [diff] [blame] | 6364 | } |
| 6365 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6366 | static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info) |
| 6367 | { |
| 6368 | int rc; |
| 6369 | |
Kevin Barnett | 162d775 | 2017-05-03 18:52:46 -0500 | [diff] [blame] | 6370 | rc = pqi_force_sis_mode(ctrl_info); |
| 6371 | if (rc) |
| 6372 | return rc; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6373 | |
| 6374 | /* |
| 6375 | * Wait until the controller is ready to start accepting SIS |
| 6376 | * commands. |
| 6377 | */ |
| 6378 | rc = sis_wait_for_ctrl_ready(ctrl_info); |
Kevin Barnett | 8845fdf | 2017-05-03 18:53:36 -0500 | [diff] [blame] | 6379 | if (rc) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6380 | return rc; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6381 | |
| 6382 | /* |
| 6383 | * Get the controller properties. This allows us to determine |
| 6384 | * whether or not it supports PQI mode. |
| 6385 | */ |
| 6386 | rc = sis_get_ctrl_properties(ctrl_info); |
| 6387 | if (rc) { |
| 6388 | dev_err(&ctrl_info->pci_dev->dev, |
| 6389 | "error obtaining controller properties\n"); |
| 6390 | return rc; |
| 6391 | } |
| 6392 | |
| 6393 | rc = sis_get_pqi_capabilities(ctrl_info); |
| 6394 | if (rc) { |
| 6395 | dev_err(&ctrl_info->pci_dev->dev, |
| 6396 | "error obtaining controller capabilities\n"); |
| 6397 | return rc; |
| 6398 | } |
| 6399 | |
Kevin Barnett | d727a77 | 2017-05-03 18:54:25 -0500 | [diff] [blame] | 6400 | if (reset_devices) { |
| 6401 | if (ctrl_info->max_outstanding_requests > |
| 6402 | PQI_MAX_OUTSTANDING_REQUESTS_KDUMP) |
| 6403 | ctrl_info->max_outstanding_requests = |
| 6404 | PQI_MAX_OUTSTANDING_REQUESTS_KDUMP; |
| 6405 | } else { |
| 6406 | if (ctrl_info->max_outstanding_requests > |
| 6407 | PQI_MAX_OUTSTANDING_REQUESTS) |
| 6408 | ctrl_info->max_outstanding_requests = |
| 6409 | PQI_MAX_OUTSTANDING_REQUESTS; |
| 6410 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6411 | |
| 6412 | pqi_calculate_io_resources(ctrl_info); |
| 6413 | |
| 6414 | rc = pqi_alloc_error_buffer(ctrl_info); |
| 6415 | if (rc) { |
| 6416 | dev_err(&ctrl_info->pci_dev->dev, |
| 6417 | "failed to allocate PQI error buffer\n"); |
| 6418 | return rc; |
| 6419 | } |
| 6420 | |
| 6421 | /* |
| 6422 | * If the function we are about to call succeeds, the |
| 6423 | * controller will transition from legacy SIS mode |
| 6424 | * into PQI mode. |
| 6425 | */ |
| 6426 | rc = sis_init_base_struct_addr(ctrl_info); |
| 6427 | if (rc) { |
| 6428 | dev_err(&ctrl_info->pci_dev->dev, |
| 6429 | "error initializing PQI mode\n"); |
| 6430 | return rc; |
| 6431 | } |
| 6432 | |
| 6433 | /* Wait for the controller to complete the SIS -> PQI transition. */ |
| 6434 | rc = pqi_wait_for_pqi_mode_ready(ctrl_info); |
| 6435 | if (rc) { |
| 6436 | dev_err(&ctrl_info->pci_dev->dev, |
| 6437 | "transition to PQI mode failed\n"); |
| 6438 | return rc; |
| 6439 | } |
| 6440 | |
| 6441 | /* From here on, we are running in PQI mode. */ |
| 6442 | ctrl_info->pqi_mode_enabled = true; |
Kevin Barnett | ff6abb7 | 2016-08-31 14:54:41 -0500 | [diff] [blame] | 6443 | pqi_save_ctrl_mode(ctrl_info, PQI_MODE); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6444 | |
| 6445 | rc = pqi_alloc_admin_queues(ctrl_info); |
| 6446 | if (rc) { |
| 6447 | dev_err(&ctrl_info->pci_dev->dev, |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 6448 | "failed to allocate admin queues\n"); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6449 | return rc; |
| 6450 | } |
| 6451 | |
| 6452 | rc = pqi_create_admin_queues(ctrl_info); |
| 6453 | if (rc) { |
| 6454 | dev_err(&ctrl_info->pci_dev->dev, |
| 6455 | "error creating admin queues\n"); |
| 6456 | return rc; |
| 6457 | } |
| 6458 | |
| 6459 | rc = pqi_report_device_capability(ctrl_info); |
| 6460 | if (rc) { |
| 6461 | dev_err(&ctrl_info->pci_dev->dev, |
| 6462 | "obtaining device capability failed\n"); |
| 6463 | return rc; |
| 6464 | } |
| 6465 | |
| 6466 | rc = pqi_validate_device_capability(ctrl_info); |
| 6467 | if (rc) |
| 6468 | return rc; |
| 6469 | |
| 6470 | pqi_calculate_queue_resources(ctrl_info); |
| 6471 | |
| 6472 | rc = pqi_enable_msix_interrupts(ctrl_info); |
| 6473 | if (rc) |
| 6474 | return rc; |
| 6475 | |
| 6476 | if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) { |
| 6477 | ctrl_info->max_msix_vectors = |
| 6478 | ctrl_info->num_msix_vectors_enabled; |
| 6479 | pqi_calculate_queue_resources(ctrl_info); |
| 6480 | } |
| 6481 | |
| 6482 | rc = pqi_alloc_io_resources(ctrl_info); |
| 6483 | if (rc) |
| 6484 | return rc; |
| 6485 | |
| 6486 | rc = pqi_alloc_operational_queues(ctrl_info); |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 6487 | if (rc) { |
| 6488 | dev_err(&ctrl_info->pci_dev->dev, |
| 6489 | "failed to allocate operational queues\n"); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6490 | return rc; |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 6491 | } |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6492 | |
| 6493 | pqi_init_operational_queues(ctrl_info); |
| 6494 | |
| 6495 | rc = pqi_request_irqs(ctrl_info); |
| 6496 | if (rc) |
| 6497 | return rc; |
| 6498 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6499 | rc = pqi_create_queues(ctrl_info); |
| 6500 | if (rc) |
| 6501 | return rc; |
| 6502 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6503 | pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX); |
| 6504 | |
| 6505 | ctrl_info->controller_online = true; |
Kevin Barnett | b212c25 | 2018-12-07 16:28:10 -0600 | [diff] [blame] | 6506 | |
| 6507 | rc = pqi_process_config_table(ctrl_info); |
| 6508 | if (rc) |
| 6509 | return rc; |
| 6510 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6511 | pqi_start_heartbeat_timer(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6512 | |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 6513 | rc = pqi_enable_events(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6514 | if (rc) { |
| 6515 | dev_err(&ctrl_info->pci_dev->dev, |
Kevin Barnett | 6a50d6a | 2017-05-03 18:52:52 -0500 | [diff] [blame] | 6516 | "error enabling events\n"); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6517 | return rc; |
| 6518 | } |
| 6519 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6520 | /* Register with the SCSI subsystem. */ |
| 6521 | rc = pqi_register_scsi(ctrl_info); |
| 6522 | if (rc) |
| 6523 | return rc; |
| 6524 | |
| 6525 | rc = pqi_get_ctrl_firmware_version(ctrl_info); |
| 6526 | if (rc) { |
| 6527 | dev_err(&ctrl_info->pci_dev->dev, |
| 6528 | "error obtaining firmware version\n"); |
| 6529 | return rc; |
| 6530 | } |
| 6531 | |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 6532 | rc = pqi_set_diag_rescan(ctrl_info); |
| 6533 | if (rc) { |
| 6534 | dev_err(&ctrl_info->pci_dev->dev, |
| 6535 | "error enabling multi-lun rescan\n"); |
| 6536 | return rc; |
| 6537 | } |
| 6538 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6539 | rc = pqi_write_driver_version_to_host_wellness(ctrl_info); |
| 6540 | if (rc) { |
| 6541 | dev_err(&ctrl_info->pci_dev->dev, |
| 6542 | "error updating host wellness\n"); |
| 6543 | return rc; |
| 6544 | } |
| 6545 | |
| 6546 | pqi_schedule_update_time_worker(ctrl_info); |
| 6547 | |
| 6548 | pqi_scan_scsi_devices(ctrl_info); |
| 6549 | |
| 6550 | return 0; |
| 6551 | } |
| 6552 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6553 | static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info) |
| 6554 | { |
| 6555 | unsigned int i; |
| 6556 | struct pqi_admin_queues *admin_queues; |
| 6557 | struct pqi_event_queue *event_queue; |
| 6558 | |
| 6559 | admin_queues = &ctrl_info->admin_queues; |
| 6560 | admin_queues->iq_pi_copy = 0; |
| 6561 | admin_queues->oq_ci_copy = 0; |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 6562 | writel(0, admin_queues->oq_pi); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6563 | |
| 6564 | for (i = 0; i < ctrl_info->num_queue_groups; i++) { |
| 6565 | ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0; |
| 6566 | ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0; |
| 6567 | ctrl_info->queue_groups[i].oq_ci_copy = 0; |
| 6568 | |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 6569 | writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]); |
| 6570 | writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]); |
| 6571 | writel(0, ctrl_info->queue_groups[i].oq_pi); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6572 | } |
| 6573 | |
| 6574 | event_queue = &ctrl_info->event_queue; |
Kevin Barnett | dac12fb | 2018-06-18 13:23:00 -0500 | [diff] [blame] | 6575 | writel(0, event_queue->oq_pi); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6576 | event_queue->oq_ci_copy = 0; |
| 6577 | } |
| 6578 | |
| 6579 | static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info) |
| 6580 | { |
| 6581 | int rc; |
| 6582 | |
| 6583 | rc = pqi_force_sis_mode(ctrl_info); |
| 6584 | if (rc) |
| 6585 | return rc; |
| 6586 | |
| 6587 | /* |
| 6588 | * Wait until the controller is ready to start accepting SIS |
| 6589 | * commands. |
| 6590 | */ |
| 6591 | rc = sis_wait_for_ctrl_ready_resume(ctrl_info); |
| 6592 | if (rc) |
| 6593 | return rc; |
| 6594 | |
| 6595 | /* |
| 6596 | * If the function we are about to call succeeds, the |
| 6597 | * controller will transition from legacy SIS mode |
| 6598 | * into PQI mode. |
| 6599 | */ |
| 6600 | rc = sis_init_base_struct_addr(ctrl_info); |
| 6601 | if (rc) { |
| 6602 | dev_err(&ctrl_info->pci_dev->dev, |
| 6603 | "error initializing PQI mode\n"); |
| 6604 | return rc; |
| 6605 | } |
| 6606 | |
| 6607 | /* Wait for the controller to complete the SIS -> PQI transition. */ |
| 6608 | rc = pqi_wait_for_pqi_mode_ready(ctrl_info); |
| 6609 | if (rc) { |
| 6610 | dev_err(&ctrl_info->pci_dev->dev, |
| 6611 | "transition to PQI mode failed\n"); |
| 6612 | return rc; |
| 6613 | } |
| 6614 | |
| 6615 | /* From here on, we are running in PQI mode. */ |
| 6616 | ctrl_info->pqi_mode_enabled = true; |
| 6617 | pqi_save_ctrl_mode(ctrl_info, PQI_MODE); |
| 6618 | |
| 6619 | pqi_reinit_queues(ctrl_info); |
| 6620 | |
| 6621 | rc = pqi_create_admin_queues(ctrl_info); |
| 6622 | if (rc) { |
| 6623 | dev_err(&ctrl_info->pci_dev->dev, |
| 6624 | "error creating admin queues\n"); |
| 6625 | return rc; |
| 6626 | } |
| 6627 | |
| 6628 | rc = pqi_create_queues(ctrl_info); |
| 6629 | if (rc) |
| 6630 | return rc; |
| 6631 | |
| 6632 | pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX); |
| 6633 | |
| 6634 | ctrl_info->controller_online = true; |
| 6635 | pqi_start_heartbeat_timer(ctrl_info); |
| 6636 | pqi_ctrl_unblock_requests(ctrl_info); |
| 6637 | |
| 6638 | rc = pqi_enable_events(ctrl_info); |
| 6639 | if (rc) { |
| 6640 | dev_err(&ctrl_info->pci_dev->dev, |
Kevin Barnett | d87d547 | 2017-05-03 18:54:00 -0500 | [diff] [blame] | 6641 | "error enabling events\n"); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6642 | return rc; |
| 6643 | } |
| 6644 | |
Dave Carroll | 171c286 | 2018-12-07 16:28:35 -0600 | [diff] [blame^] | 6645 | rc = pqi_set_diag_rescan(ctrl_info); |
| 6646 | if (rc) { |
| 6647 | dev_err(&ctrl_info->pci_dev->dev, |
| 6648 | "error enabling multi-lun rescan\n"); |
| 6649 | return rc; |
| 6650 | } |
| 6651 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6652 | rc = pqi_write_driver_version_to_host_wellness(ctrl_info); |
| 6653 | if (rc) { |
| 6654 | dev_err(&ctrl_info->pci_dev->dev, |
| 6655 | "error updating host wellness\n"); |
| 6656 | return rc; |
| 6657 | } |
| 6658 | |
| 6659 | pqi_schedule_update_time_worker(ctrl_info); |
| 6660 | |
| 6661 | pqi_scan_scsi_devices(ctrl_info); |
| 6662 | |
| 6663 | return 0; |
| 6664 | } |
| 6665 | |
Kevin Barnett | a81ed5f3 | 2017-05-03 18:52:34 -0500 | [diff] [blame] | 6666 | static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev, |
| 6667 | u16 timeout) |
| 6668 | { |
| 6669 | return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2, |
| 6670 | PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout); |
| 6671 | } |
| 6672 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6673 | static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info) |
| 6674 | { |
| 6675 | int rc; |
| 6676 | u64 mask; |
| 6677 | |
| 6678 | rc = pci_enable_device(ctrl_info->pci_dev); |
| 6679 | if (rc) { |
| 6680 | dev_err(&ctrl_info->pci_dev->dev, |
| 6681 | "failed to enable PCI device\n"); |
| 6682 | return rc; |
| 6683 | } |
| 6684 | |
| 6685 | if (sizeof(dma_addr_t) > 4) |
| 6686 | mask = DMA_BIT_MASK(64); |
| 6687 | else |
| 6688 | mask = DMA_BIT_MASK(32); |
| 6689 | |
| 6690 | rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask); |
| 6691 | if (rc) { |
| 6692 | dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n"); |
| 6693 | goto disable_device; |
| 6694 | } |
| 6695 | |
| 6696 | rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT); |
| 6697 | if (rc) { |
| 6698 | dev_err(&ctrl_info->pci_dev->dev, |
| 6699 | "failed to obtain PCI resources\n"); |
| 6700 | goto disable_device; |
| 6701 | } |
| 6702 | |
| 6703 | ctrl_info->iomem_base = ioremap_nocache(pci_resource_start( |
| 6704 | ctrl_info->pci_dev, 0), |
| 6705 | sizeof(struct pqi_ctrl_registers)); |
| 6706 | if (!ctrl_info->iomem_base) { |
| 6707 | dev_err(&ctrl_info->pci_dev->dev, |
| 6708 | "failed to map memory for controller registers\n"); |
| 6709 | rc = -ENOMEM; |
| 6710 | goto release_regions; |
| 6711 | } |
| 6712 | |
Kevin Barnett | a81ed5f3 | 2017-05-03 18:52:34 -0500 | [diff] [blame] | 6713 | #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6 |
| 6714 | |
| 6715 | /* Increase the PCIe completion timeout. */ |
| 6716 | rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev, |
| 6717 | PCI_EXP_COMP_TIMEOUT_65_TO_210_MS); |
| 6718 | if (rc) { |
| 6719 | dev_err(&ctrl_info->pci_dev->dev, |
| 6720 | "failed to set PCIe completion timeout\n"); |
| 6721 | goto release_regions; |
| 6722 | } |
| 6723 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6724 | /* Enable bus mastering. */ |
| 6725 | pci_set_master(ctrl_info->pci_dev); |
| 6726 | |
Kevin Barnett | cbe0c7b | 2017-05-03 18:53:48 -0500 | [diff] [blame] | 6727 | ctrl_info->registers = ctrl_info->iomem_base; |
| 6728 | ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers; |
| 6729 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6730 | pci_set_drvdata(ctrl_info->pci_dev, ctrl_info); |
| 6731 | |
| 6732 | return 0; |
| 6733 | |
| 6734 | release_regions: |
| 6735 | pci_release_regions(ctrl_info->pci_dev); |
| 6736 | disable_device: |
| 6737 | pci_disable_device(ctrl_info->pci_dev); |
| 6738 | |
| 6739 | return rc; |
| 6740 | } |
| 6741 | |
| 6742 | static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info) |
| 6743 | { |
| 6744 | iounmap(ctrl_info->iomem_base); |
| 6745 | pci_release_regions(ctrl_info->pci_dev); |
Kevin Barnett | cbe0c7b | 2017-05-03 18:53:48 -0500 | [diff] [blame] | 6746 | if (pci_is_enabled(ctrl_info->pci_dev)) |
| 6747 | pci_disable_device(ctrl_info->pci_dev); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6748 | pci_set_drvdata(ctrl_info->pci_dev, NULL); |
| 6749 | } |
| 6750 | |
| 6751 | static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node) |
| 6752 | { |
| 6753 | struct pqi_ctrl_info *ctrl_info; |
| 6754 | |
| 6755 | ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info), |
| 6756 | GFP_KERNEL, numa_node); |
| 6757 | if (!ctrl_info) |
| 6758 | return NULL; |
| 6759 | |
| 6760 | mutex_init(&ctrl_info->scan_mutex); |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 6761 | mutex_init(&ctrl_info->lun_reset_mutex); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6762 | |
| 6763 | INIT_LIST_HEAD(&ctrl_info->scsi_device_list); |
| 6764 | spin_lock_init(&ctrl_info->scsi_device_list_lock); |
| 6765 | |
| 6766 | INIT_WORK(&ctrl_info->event_work, pqi_event_worker); |
| 6767 | atomic_set(&ctrl_info->num_interrupts, 0); |
| 6768 | |
| 6769 | INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker); |
| 6770 | INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker); |
| 6771 | |
Kees Cook | 74a0f57 | 2017-10-11 16:27:10 -0700 | [diff] [blame] | 6772 | timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0); |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6773 | INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker); |
Kevin Barnett | 98f8766 | 2017-05-03 18:53:11 -0500 | [diff] [blame] | 6774 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6775 | sema_init(&ctrl_info->sync_request_sem, |
| 6776 | PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS); |
Kevin Barnett | 7561a7e | 2017-05-03 18:52:58 -0500 | [diff] [blame] | 6777 | init_waitqueue_head(&ctrl_info->block_requests_wait); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6778 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6779 | INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list); |
| 6780 | spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock); |
| 6781 | INIT_WORK(&ctrl_info->raid_bypass_retry_work, |
| 6782 | pqi_raid_bypass_retry_worker); |
| 6783 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6784 | ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1; |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6785 | ctrl_info->irq_mode = IRQ_MODE_NONE; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6786 | ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS; |
| 6787 | |
| 6788 | return ctrl_info; |
| 6789 | } |
| 6790 | |
| 6791 | static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info) |
| 6792 | { |
| 6793 | kfree(ctrl_info); |
| 6794 | } |
| 6795 | |
| 6796 | static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info) |
| 6797 | { |
Kevin Barnett | 98bf061 | 2017-05-03 18:52:28 -0500 | [diff] [blame] | 6798 | pqi_free_irqs(ctrl_info); |
| 6799 | pqi_disable_msix_interrupts(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6800 | } |
| 6801 | |
| 6802 | static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info) |
| 6803 | { |
| 6804 | pqi_stop_heartbeat_timer(ctrl_info); |
| 6805 | pqi_free_interrupts(ctrl_info); |
| 6806 | if (ctrl_info->queue_memory_base) |
| 6807 | dma_free_coherent(&ctrl_info->pci_dev->dev, |
| 6808 | ctrl_info->queue_memory_length, |
| 6809 | ctrl_info->queue_memory_base, |
| 6810 | ctrl_info->queue_memory_base_dma_handle); |
| 6811 | if (ctrl_info->admin_queue_memory_base) |
| 6812 | dma_free_coherent(&ctrl_info->pci_dev->dev, |
| 6813 | ctrl_info->admin_queue_memory_length, |
| 6814 | ctrl_info->admin_queue_memory_base, |
| 6815 | ctrl_info->admin_queue_memory_base_dma_handle); |
| 6816 | pqi_free_all_io_requests(ctrl_info); |
| 6817 | if (ctrl_info->error_buffer) |
| 6818 | dma_free_coherent(&ctrl_info->pci_dev->dev, |
| 6819 | ctrl_info->error_buffer_length, |
| 6820 | ctrl_info->error_buffer, |
| 6821 | ctrl_info->error_buffer_dma_handle); |
| 6822 | if (ctrl_info->iomem_base) |
| 6823 | pqi_cleanup_pci_init(ctrl_info); |
| 6824 | pqi_free_ctrl_info(ctrl_info); |
| 6825 | } |
| 6826 | |
| 6827 | static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info) |
| 6828 | { |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 6829 | pqi_cancel_rescan_worker(ctrl_info); |
| 6830 | pqi_cancel_update_time_worker(ctrl_info); |
Kevin Barnett | e57a1f9 | 2016-08-31 14:54:47 -0500 | [diff] [blame] | 6831 | pqi_remove_all_scsi_devices(ctrl_info); |
| 6832 | pqi_unregister_scsi(ctrl_info); |
Kevin Barnett | 162d775 | 2017-05-03 18:52:46 -0500 | [diff] [blame] | 6833 | if (ctrl_info->pqi_mode_enabled) |
| 6834 | pqi_revert_to_sis_mode(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6835 | pqi_free_ctrl_resources(ctrl_info); |
| 6836 | } |
| 6837 | |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 6838 | static void pqi_perform_lockup_action(void) |
| 6839 | { |
| 6840 | switch (pqi_lockup_action) { |
| 6841 | case PANIC: |
| 6842 | panic("FATAL: Smart Family Controller lockup detected"); |
| 6843 | break; |
| 6844 | case REBOOT: |
| 6845 | emergency_restart(); |
| 6846 | break; |
| 6847 | case NONE: |
| 6848 | default: |
| 6849 | break; |
| 6850 | } |
| 6851 | } |
| 6852 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6853 | static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = { |
| 6854 | .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR, |
| 6855 | .status = SAM_STAT_CHECK_CONDITION, |
| 6856 | }; |
| 6857 | |
| 6858 | static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info) |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6859 | { |
| 6860 | unsigned int i; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6861 | struct pqi_io_request *io_request; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6862 | struct scsi_cmnd *scmd; |
| 6863 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6864 | for (i = 0; i < ctrl_info->max_io_slots; i++) { |
| 6865 | io_request = &ctrl_info->io_request_pool[i]; |
| 6866 | if (atomic_read(&io_request->refcount) == 0) |
| 6867 | continue; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6868 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6869 | scmd = io_request->scmd; |
| 6870 | if (scmd) { |
| 6871 | set_host_byte(scmd, DID_NO_CONNECT); |
| 6872 | } else { |
| 6873 | io_request->status = -ENXIO; |
| 6874 | io_request->error_info = |
| 6875 | &pqi_ctrl_offline_raid_error_info; |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6876 | } |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6877 | |
| 6878 | io_request->io_complete_callback(io_request, |
| 6879 | io_request->context); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6880 | } |
| 6881 | } |
| 6882 | |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6883 | static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info) |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6884 | { |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6885 | pqi_perform_lockup_action(); |
| 6886 | pqi_stop_heartbeat_timer(ctrl_info); |
| 6887 | pqi_free_interrupts(ctrl_info); |
| 6888 | pqi_cancel_rescan_worker(ctrl_info); |
| 6889 | pqi_cancel_update_time_worker(ctrl_info); |
| 6890 | pqi_ctrl_wait_until_quiesced(ctrl_info); |
| 6891 | pqi_fail_all_outstanding_requests(ctrl_info); |
| 6892 | pqi_clear_all_queued_raid_bypass_retries(ctrl_info); |
| 6893 | pqi_ctrl_unblock_requests(ctrl_info); |
| 6894 | } |
| 6895 | |
| 6896 | static void pqi_ctrl_offline_worker(struct work_struct *work) |
| 6897 | { |
| 6898 | struct pqi_ctrl_info *ctrl_info; |
| 6899 | |
| 6900 | ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work); |
| 6901 | pqi_take_ctrl_offline_deferred(ctrl_info); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info) |
| 6905 | { |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6906 | if (!ctrl_info->controller_online) |
| 6907 | return; |
| 6908 | |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6909 | ctrl_info->controller_online = false; |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6910 | ctrl_info->pqi_mode_enabled = false; |
| 6911 | pqi_ctrl_block_requests(ctrl_info); |
Kevin Barnett | 5a259e3 | 2017-05-03 18:55:43 -0500 | [diff] [blame] | 6912 | if (!pqi_disable_ctrl_shutdown) |
| 6913 | sis_shutdown_ctrl(ctrl_info); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6914 | pci_disable_device(ctrl_info->pci_dev); |
| 6915 | dev_err(&ctrl_info->pci_dev->dev, "controller offline\n"); |
Kevin Barnett | 5f31042 | 2017-05-03 18:54:55 -0500 | [diff] [blame] | 6916 | schedule_work(&ctrl_info->ctrl_offline_work); |
Kevin Barnett | 376fb88 | 2017-05-03 18:54:43 -0500 | [diff] [blame] | 6917 | } |
| 6918 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6919 | static void pqi_print_ctrl_info(struct pci_dev *pci_dev, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6920 | const struct pci_device_id *id) |
| 6921 | { |
| 6922 | char *ctrl_description; |
| 6923 | |
Kevin Barnett | 37b3684 | 2017-05-03 18:55:01 -0500 | [diff] [blame] | 6924 | if (id->driver_data) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6925 | ctrl_description = (char *)id->driver_data; |
Kevin Barnett | 37b3684 | 2017-05-03 18:55:01 -0500 | [diff] [blame] | 6926 | else |
| 6927 | ctrl_description = "Microsemi Smart Family Controller"; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6928 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6929 | dev_info(&pci_dev->dev, "%s found\n", ctrl_description); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6930 | } |
| 6931 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6932 | static int pqi_pci_probe(struct pci_dev *pci_dev, |
| 6933 | const struct pci_device_id *id) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6934 | { |
| 6935 | int rc; |
| 6936 | int node; |
| 6937 | struct pqi_ctrl_info *ctrl_info; |
| 6938 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6939 | pqi_print_ctrl_info(pci_dev, id); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6940 | |
| 6941 | if (pqi_disable_device_id_wildcards && |
| 6942 | id->subvendor == PCI_ANY_ID && |
| 6943 | id->subdevice == PCI_ANY_ID) { |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6944 | dev_warn(&pci_dev->dev, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6945 | "controller not probed because device ID wildcards are disabled\n"); |
| 6946 | return -ENODEV; |
| 6947 | } |
| 6948 | |
| 6949 | if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID) |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6950 | dev_warn(&pci_dev->dev, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6951 | "controller device ID matched using wildcards\n"); |
| 6952 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6953 | node = dev_to_node(&pci_dev->dev); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6954 | if (node == NUMA_NO_NODE) |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6955 | set_dev_node(&pci_dev->dev, 0); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6956 | |
| 6957 | ctrl_info = pqi_alloc_ctrl_info(node); |
| 6958 | if (!ctrl_info) { |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6959 | dev_err(&pci_dev->dev, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6960 | "failed to allocate controller info block\n"); |
| 6961 | return -ENOMEM; |
| 6962 | } |
| 6963 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6964 | ctrl_info->pci_dev = pci_dev; |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6965 | |
| 6966 | rc = pqi_pci_init(ctrl_info); |
| 6967 | if (rc) |
| 6968 | goto error; |
| 6969 | |
| 6970 | rc = pqi_ctrl_init(ctrl_info); |
| 6971 | if (rc) |
| 6972 | goto error; |
| 6973 | |
| 6974 | return 0; |
| 6975 | |
| 6976 | error: |
| 6977 | pqi_remove_ctrl(ctrl_info); |
| 6978 | |
| 6979 | return rc; |
| 6980 | } |
| 6981 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6982 | static void pqi_pci_remove(struct pci_dev *pci_dev) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6983 | { |
| 6984 | struct pqi_ctrl_info *ctrl_info; |
| 6985 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6986 | ctrl_info = pci_get_drvdata(pci_dev); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6987 | if (!ctrl_info) |
| 6988 | return; |
| 6989 | |
| 6990 | pqi_remove_ctrl(ctrl_info); |
| 6991 | } |
| 6992 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6993 | static void pqi_shutdown(struct pci_dev *pci_dev) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6994 | { |
| 6995 | int rc; |
| 6996 | struct pqi_ctrl_info *ctrl_info; |
| 6997 | |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 6998 | ctrl_info = pci_get_drvdata(pci_dev); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 6999 | if (!ctrl_info) |
| 7000 | goto error; |
| 7001 | |
| 7002 | /* |
| 7003 | * Write all data in the controller's battery-backed cache to |
| 7004 | * storage. |
| 7005 | */ |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 7006 | rc = pqi_flush_cache(ctrl_info, SHUTDOWN); |
Kevin Barnett | b6d4781 | 2017-08-10 13:47:03 -0500 | [diff] [blame] | 7007 | pqi_reset(ctrl_info); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7008 | if (rc == 0) |
| 7009 | return; |
| 7010 | |
| 7011 | error: |
Kevin Barnett | d91d782 | 2017-05-03 18:53:30 -0500 | [diff] [blame] | 7012 | dev_warn(&pci_dev->dev, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7013 | "unable to flush controller cache\n"); |
| 7014 | } |
| 7015 | |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 7016 | static void pqi_process_lockup_action_param(void) |
| 7017 | { |
| 7018 | unsigned int i; |
| 7019 | |
| 7020 | if (!pqi_lockup_action_param) |
| 7021 | return; |
| 7022 | |
| 7023 | for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) { |
| 7024 | if (strcmp(pqi_lockup_action_param, |
| 7025 | pqi_lockup_actions[i].name) == 0) { |
| 7026 | pqi_lockup_action = pqi_lockup_actions[i].action; |
| 7027 | return; |
| 7028 | } |
| 7029 | } |
| 7030 | |
| 7031 | pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n", |
| 7032 | DRIVER_NAME_SHORT, pqi_lockup_action_param); |
| 7033 | } |
| 7034 | |
| 7035 | static void pqi_process_module_params(void) |
| 7036 | { |
| 7037 | pqi_process_lockup_action_param(); |
| 7038 | } |
| 7039 | |
Arnd Bergmann | 5c14668 | 2017-05-18 10:32:18 +0200 | [diff] [blame] | 7040 | static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state) |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 7041 | { |
| 7042 | struct pqi_ctrl_info *ctrl_info; |
| 7043 | |
| 7044 | ctrl_info = pci_get_drvdata(pci_dev); |
| 7045 | |
| 7046 | pqi_disable_events(ctrl_info); |
| 7047 | pqi_cancel_update_time_worker(ctrl_info); |
| 7048 | pqi_cancel_rescan_worker(ctrl_info); |
| 7049 | pqi_wait_until_scan_finished(ctrl_info); |
| 7050 | pqi_wait_until_lun_reset_finished(ctrl_info); |
Kevin Barnett | 58322fe | 2017-08-10 13:46:45 -0500 | [diff] [blame] | 7051 | pqi_flush_cache(ctrl_info, SUSPEND); |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 7052 | pqi_ctrl_block_requests(ctrl_info); |
| 7053 | pqi_ctrl_wait_until_quiesced(ctrl_info); |
| 7054 | pqi_wait_until_inbound_queues_empty(ctrl_info); |
| 7055 | pqi_ctrl_wait_for_pending_io(ctrl_info); |
| 7056 | pqi_stop_heartbeat_timer(ctrl_info); |
| 7057 | |
| 7058 | if (state.event == PM_EVENT_FREEZE) |
| 7059 | return 0; |
| 7060 | |
| 7061 | pci_save_state(pci_dev); |
| 7062 | pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state)); |
| 7063 | |
| 7064 | ctrl_info->controller_online = false; |
| 7065 | ctrl_info->pqi_mode_enabled = false; |
| 7066 | |
| 7067 | return 0; |
| 7068 | } |
| 7069 | |
Arnd Bergmann | 5c14668 | 2017-05-18 10:32:18 +0200 | [diff] [blame] | 7070 | static __maybe_unused int pqi_resume(struct pci_dev *pci_dev) |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 7071 | { |
| 7072 | int rc; |
| 7073 | struct pqi_ctrl_info *ctrl_info; |
| 7074 | |
| 7075 | ctrl_info = pci_get_drvdata(pci_dev); |
| 7076 | |
| 7077 | if (pci_dev->current_state != PCI_D0) { |
| 7078 | ctrl_info->max_hw_queue_index = 0; |
| 7079 | pqi_free_interrupts(ctrl_info); |
| 7080 | pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX); |
| 7081 | rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler, |
| 7082 | IRQF_SHARED, DRIVER_NAME_SHORT, |
| 7083 | &ctrl_info->queue_groups[0]); |
| 7084 | if (rc) { |
| 7085 | dev_err(&ctrl_info->pci_dev->dev, |
| 7086 | "irq %u init failed with error %d\n", |
| 7087 | pci_dev->irq, rc); |
| 7088 | return rc; |
| 7089 | } |
| 7090 | pqi_start_heartbeat_timer(ctrl_info); |
| 7091 | pqi_ctrl_unblock_requests(ctrl_info); |
| 7092 | return 0; |
| 7093 | } |
| 7094 | |
| 7095 | pci_set_power_state(pci_dev, PCI_D0); |
| 7096 | pci_restore_state(pci_dev); |
| 7097 | |
| 7098 | return pqi_ctrl_init_resume(ctrl_info); |
| 7099 | } |
| 7100 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7101 | /* Define the PCI IDs for the controllers that we support. */ |
| 7102 | static const struct pci_device_id pqi_pci_id_table[] = { |
| 7103 | { |
| 7104 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | b0f9408 | 2018-03-05 09:01:00 -0600 | [diff] [blame] | 7105 | 0x105b, 0x1211) |
| 7106 | }, |
| 7107 | { |
| 7108 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7109 | 0x105b, 0x1321) |
| 7110 | }, |
| 7111 | { |
| 7112 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7113 | 0x152d, 0x8a22) |
| 7114 | }, |
| 7115 | { |
| 7116 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7117 | 0x152d, 0x8a23) |
| 7118 | }, |
| 7119 | { |
| 7120 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7121 | 0x152d, 0x8a24) |
| 7122 | }, |
| 7123 | { |
| 7124 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7125 | 0x152d, 0x8a36) |
| 7126 | }, |
| 7127 | { |
| 7128 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7129 | 0x152d, 0x8a37) |
| 7130 | }, |
| 7131 | { |
| 7132 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | b0f9408 | 2018-03-05 09:01:00 -0600 | [diff] [blame] | 7133 | 0x193d, 0x8460) |
| 7134 | }, |
| 7135 | { |
| 7136 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7137 | 0x193d, 0x8461) |
| 7138 | }, |
| 7139 | { |
| 7140 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7141 | 0x193d, 0xf460) |
| 7142 | }, |
| 7143 | { |
| 7144 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7145 | 0x193d, 0xf461) |
| 7146 | }, |
| 7147 | { |
| 7148 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7149 | 0x1bd4, 0x0045) |
| 7150 | }, |
| 7151 | { |
| 7152 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7153 | 0x1bd4, 0x0046) |
| 7154 | }, |
| 7155 | { |
| 7156 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7157 | 0x1bd4, 0x0047) |
| 7158 | }, |
| 7159 | { |
| 7160 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7161 | 0x1bd4, 0x0048) |
| 7162 | }, |
| 7163 | { |
| 7164 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 9f8d05f | 2018-06-18 13:22:54 -0500 | [diff] [blame] | 7165 | 0x1bd4, 0x004a) |
| 7166 | }, |
| 7167 | { |
| 7168 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7169 | 0x1bd4, 0x004b) |
| 7170 | }, |
| 7171 | { |
| 7172 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7173 | 0x1bd4, 0x004c) |
| 7174 | }, |
| 7175 | { |
| 7176 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7177 | PCI_VENDOR_ID_ADAPTEC2, 0x0110) |
| 7178 | }, |
| 7179 | { |
| 7180 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 5579006 | 2017-08-10 13:47:09 -0500 | [diff] [blame] | 7181 | PCI_VENDOR_ID_ADAPTEC2, 0x0608) |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7182 | }, |
| 7183 | { |
| 7184 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7185 | PCI_VENDOR_ID_ADAPTEC2, 0x0800) |
| 7186 | }, |
| 7187 | { |
| 7188 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7189 | PCI_VENDOR_ID_ADAPTEC2, 0x0801) |
| 7190 | }, |
| 7191 | { |
| 7192 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7193 | PCI_VENDOR_ID_ADAPTEC2, 0x0802) |
| 7194 | }, |
| 7195 | { |
| 7196 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7197 | PCI_VENDOR_ID_ADAPTEC2, 0x0803) |
| 7198 | }, |
| 7199 | { |
| 7200 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7201 | PCI_VENDOR_ID_ADAPTEC2, 0x0804) |
| 7202 | }, |
| 7203 | { |
| 7204 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7205 | PCI_VENDOR_ID_ADAPTEC2, 0x0805) |
| 7206 | }, |
| 7207 | { |
| 7208 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7209 | PCI_VENDOR_ID_ADAPTEC2, 0x0806) |
| 7210 | }, |
| 7211 | { |
| 7212 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 5579006 | 2017-08-10 13:47:09 -0500 | [diff] [blame] | 7213 | PCI_VENDOR_ID_ADAPTEC2, 0x0807) |
| 7214 | }, |
| 7215 | { |
| 7216 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7217 | PCI_VENDOR_ID_ADAPTEC2, 0x0900) |
| 7218 | }, |
| 7219 | { |
| 7220 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7221 | PCI_VENDOR_ID_ADAPTEC2, 0x0901) |
| 7222 | }, |
| 7223 | { |
| 7224 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7225 | PCI_VENDOR_ID_ADAPTEC2, 0x0902) |
| 7226 | }, |
| 7227 | { |
| 7228 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7229 | PCI_VENDOR_ID_ADAPTEC2, 0x0903) |
| 7230 | }, |
| 7231 | { |
| 7232 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7233 | PCI_VENDOR_ID_ADAPTEC2, 0x0904) |
| 7234 | }, |
| 7235 | { |
| 7236 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7237 | PCI_VENDOR_ID_ADAPTEC2, 0x0905) |
| 7238 | }, |
| 7239 | { |
| 7240 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7241 | PCI_VENDOR_ID_ADAPTEC2, 0x0906) |
| 7242 | }, |
| 7243 | { |
| 7244 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7245 | PCI_VENDOR_ID_ADAPTEC2, 0x0907) |
| 7246 | }, |
| 7247 | { |
| 7248 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7249 | PCI_VENDOR_ID_ADAPTEC2, 0x0908) |
| 7250 | }, |
| 7251 | { |
| 7252 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 5579006 | 2017-08-10 13:47:09 -0500 | [diff] [blame] | 7253 | PCI_VENDOR_ID_ADAPTEC2, 0x090a) |
| 7254 | }, |
| 7255 | { |
| 7256 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7257 | PCI_VENDOR_ID_ADAPTEC2, 0x1200) |
| 7258 | }, |
| 7259 | { |
| 7260 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7261 | PCI_VENDOR_ID_ADAPTEC2, 0x1201) |
| 7262 | }, |
| 7263 | { |
| 7264 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7265 | PCI_VENDOR_ID_ADAPTEC2, 0x1202) |
| 7266 | }, |
| 7267 | { |
| 7268 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7269 | PCI_VENDOR_ID_ADAPTEC2, 0x1280) |
| 7270 | }, |
| 7271 | { |
| 7272 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7273 | PCI_VENDOR_ID_ADAPTEC2, 0x1281) |
| 7274 | }, |
| 7275 | { |
| 7276 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | b0f9408 | 2018-03-05 09:01:00 -0600 | [diff] [blame] | 7277 | PCI_VENDOR_ID_ADAPTEC2, 0x1282) |
| 7278 | }, |
| 7279 | { |
| 7280 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7281 | PCI_VENDOR_ID_ADAPTEC2, 0x1300) |
| 7282 | }, |
| 7283 | { |
| 7284 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7285 | PCI_VENDOR_ID_ADAPTEC2, 0x1301) |
| 7286 | }, |
| 7287 | { |
| 7288 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | bd809e8 | 2017-09-27 16:29:59 -0500 | [diff] [blame] | 7289 | PCI_VENDOR_ID_ADAPTEC2, 0x1302) |
| 7290 | }, |
| 7291 | { |
| 7292 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7293 | PCI_VENDOR_ID_ADAPTEC2, 0x1303) |
| 7294 | }, |
| 7295 | { |
| 7296 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7297 | PCI_VENDOR_ID_ADAPTEC2, 0x1380) |
| 7298 | }, |
| 7299 | { |
| 7300 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 9f8d05f | 2018-06-18 13:22:54 -0500 | [diff] [blame] | 7301 | PCI_VENDOR_ID_ADVANTECH, 0x8312) |
| 7302 | }, |
| 7303 | { |
| 7304 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 5579006 | 2017-08-10 13:47:09 -0500 | [diff] [blame] | 7305 | PCI_VENDOR_ID_DELL, 0x1fe0) |
| 7306 | }, |
| 7307 | { |
| 7308 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7309 | PCI_VENDOR_ID_HP, 0x0600) |
| 7310 | }, |
| 7311 | { |
| 7312 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7313 | PCI_VENDOR_ID_HP, 0x0601) |
| 7314 | }, |
| 7315 | { |
| 7316 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7317 | PCI_VENDOR_ID_HP, 0x0602) |
| 7318 | }, |
| 7319 | { |
| 7320 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7321 | PCI_VENDOR_ID_HP, 0x0603) |
| 7322 | }, |
| 7323 | { |
| 7324 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 5579006 | 2017-08-10 13:47:09 -0500 | [diff] [blame] | 7325 | PCI_VENDOR_ID_HP, 0x0609) |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7326 | }, |
| 7327 | { |
| 7328 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7329 | PCI_VENDOR_ID_HP, 0x0650) |
| 7330 | }, |
| 7331 | { |
| 7332 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7333 | PCI_VENDOR_ID_HP, 0x0651) |
| 7334 | }, |
| 7335 | { |
| 7336 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7337 | PCI_VENDOR_ID_HP, 0x0652) |
| 7338 | }, |
| 7339 | { |
| 7340 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7341 | PCI_VENDOR_ID_HP, 0x0653) |
| 7342 | }, |
| 7343 | { |
| 7344 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7345 | PCI_VENDOR_ID_HP, 0x0654) |
| 7346 | }, |
| 7347 | { |
| 7348 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7349 | PCI_VENDOR_ID_HP, 0x0655) |
| 7350 | }, |
| 7351 | { |
| 7352 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 7eddabf | 2017-05-03 18:53:54 -0500 | [diff] [blame] | 7353 | PCI_VENDOR_ID_HP, 0x0700) |
| 7354 | }, |
| 7355 | { |
| 7356 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7357 | PCI_VENDOR_ID_HP, 0x0701) |
| 7358 | }, |
| 7359 | { |
| 7360 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7361 | PCI_VENDOR_ID_HP, 0x1001) |
| 7362 | }, |
| 7363 | { |
| 7364 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7365 | PCI_VENDOR_ID_HP, 0x1100) |
| 7366 | }, |
| 7367 | { |
| 7368 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
| 7369 | PCI_VENDOR_ID_HP, 0x1101) |
| 7370 | }, |
| 7371 | { |
| 7372 | PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7373 | PCI_ANY_ID, PCI_ANY_ID) |
| 7374 | }, |
| 7375 | { 0 } |
| 7376 | }; |
| 7377 | |
| 7378 | MODULE_DEVICE_TABLE(pci, pqi_pci_id_table); |
| 7379 | |
| 7380 | static struct pci_driver pqi_pci_driver = { |
| 7381 | .name = DRIVER_NAME_SHORT, |
| 7382 | .id_table = pqi_pci_id_table, |
| 7383 | .probe = pqi_pci_probe, |
| 7384 | .remove = pqi_pci_remove, |
| 7385 | .shutdown = pqi_shutdown, |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 7386 | #if defined(CONFIG_PM) |
| 7387 | .suspend = pqi_suspend, |
| 7388 | .resume = pqi_resume, |
| 7389 | #endif |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7390 | }; |
| 7391 | |
| 7392 | static int __init pqi_init(void) |
| 7393 | { |
| 7394 | int rc; |
| 7395 | |
| 7396 | pr_info(DRIVER_NAME "\n"); |
| 7397 | |
| 7398 | pqi_sas_transport_template = |
| 7399 | sas_attach_transport(&pqi_sas_transport_functions); |
| 7400 | if (!pqi_sas_transport_template) |
| 7401 | return -ENODEV; |
| 7402 | |
Kevin Barnett | 3c50976 | 2017-05-03 18:54:37 -0500 | [diff] [blame] | 7403 | pqi_process_module_params(); |
| 7404 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7405 | rc = pci_register_driver(&pqi_pci_driver); |
| 7406 | if (rc) |
| 7407 | sas_release_transport(pqi_sas_transport_template); |
| 7408 | |
| 7409 | return rc; |
| 7410 | } |
| 7411 | |
| 7412 | static void __exit pqi_cleanup(void) |
| 7413 | { |
| 7414 | pci_unregister_driver(&pqi_pci_driver); |
| 7415 | sas_release_transport(pqi_sas_transport_template); |
| 7416 | } |
| 7417 | |
| 7418 | module_init(pqi_init); |
| 7419 | module_exit(pqi_cleanup); |
| 7420 | |
| 7421 | static void __attribute__((unused)) verify_structures(void) |
| 7422 | { |
| 7423 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
| 7424 | sis_host_to_ctrl_doorbell) != 0x20); |
| 7425 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
| 7426 | sis_interrupt_mask) != 0x34); |
| 7427 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
| 7428 | sis_ctrl_to_host_doorbell) != 0x9c); |
| 7429 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
| 7430 | sis_ctrl_to_host_doorbell_clear) != 0xa0); |
| 7431 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
Kevin Barnett | ff6abb7 | 2016-08-31 14:54:41 -0500 | [diff] [blame] | 7432 | sis_driver_scratch) != 0xb0); |
| 7433 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7434 | sis_firmware_status) != 0xbc); |
| 7435 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
| 7436 | sis_mailbox) != 0x1000); |
| 7437 | BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers, |
| 7438 | pqi_registers) != 0x4000); |
| 7439 | |
| 7440 | BUILD_BUG_ON(offsetof(struct pqi_iu_header, |
| 7441 | iu_type) != 0x0); |
| 7442 | BUILD_BUG_ON(offsetof(struct pqi_iu_header, |
| 7443 | iu_length) != 0x2); |
| 7444 | BUILD_BUG_ON(offsetof(struct pqi_iu_header, |
| 7445 | response_queue_id) != 0x4); |
| 7446 | BUILD_BUG_ON(offsetof(struct pqi_iu_header, |
| 7447 | work_area) != 0x6); |
| 7448 | BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8); |
| 7449 | |
| 7450 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7451 | status) != 0x0); |
| 7452 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7453 | service_response) != 0x1); |
| 7454 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7455 | data_present) != 0x2); |
| 7456 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7457 | reserved) != 0x3); |
| 7458 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7459 | residual_count) != 0x4); |
| 7460 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7461 | data_length) != 0x8); |
| 7462 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7463 | reserved1) != 0xa); |
| 7464 | BUILD_BUG_ON(offsetof(struct pqi_aio_error_info, |
| 7465 | data) != 0xc); |
| 7466 | BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c); |
| 7467 | |
| 7468 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7469 | data_in_result) != 0x0); |
| 7470 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7471 | data_out_result) != 0x1); |
| 7472 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7473 | reserved) != 0x2); |
| 7474 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7475 | status) != 0x5); |
| 7476 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7477 | status_qualifier) != 0x6); |
| 7478 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7479 | sense_data_length) != 0x8); |
| 7480 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7481 | response_data_length) != 0xa); |
| 7482 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7483 | data_in_transferred) != 0xc); |
| 7484 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7485 | data_out_transferred) != 0x10); |
| 7486 | BUILD_BUG_ON(offsetof(struct pqi_raid_error_info, |
| 7487 | data) != 0x14); |
| 7488 | BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114); |
| 7489 | |
| 7490 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7491 | signature) != 0x0); |
| 7492 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7493 | function_and_status_code) != 0x8); |
| 7494 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7495 | max_admin_iq_elements) != 0x10); |
| 7496 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7497 | max_admin_oq_elements) != 0x11); |
| 7498 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7499 | admin_iq_element_length) != 0x12); |
| 7500 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7501 | admin_oq_element_length) != 0x13); |
| 7502 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7503 | max_reset_timeout) != 0x14); |
| 7504 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7505 | legacy_intx_status) != 0x18); |
| 7506 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7507 | legacy_intx_mask_set) != 0x1c); |
| 7508 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7509 | legacy_intx_mask_clear) != 0x20); |
| 7510 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7511 | device_status) != 0x40); |
| 7512 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7513 | admin_iq_pi_offset) != 0x48); |
| 7514 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7515 | admin_oq_ci_offset) != 0x50); |
| 7516 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7517 | admin_iq_element_array_addr) != 0x58); |
| 7518 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7519 | admin_oq_element_array_addr) != 0x60); |
| 7520 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7521 | admin_iq_ci_addr) != 0x68); |
| 7522 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7523 | admin_oq_pi_addr) != 0x70); |
| 7524 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7525 | admin_iq_num_elements) != 0x78); |
| 7526 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7527 | admin_oq_num_elements) != 0x79); |
| 7528 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7529 | admin_queue_int_msg_num) != 0x7a); |
| 7530 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7531 | device_error) != 0x80); |
| 7532 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7533 | error_details) != 0x88); |
| 7534 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7535 | device_reset) != 0x90); |
| 7536 | BUILD_BUG_ON(offsetof(struct pqi_device_registers, |
| 7537 | power_action) != 0x94); |
| 7538 | BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100); |
| 7539 | |
| 7540 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7541 | header.iu_type) != 0); |
| 7542 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7543 | header.iu_length) != 2); |
| 7544 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7545 | header.work_area) != 6); |
| 7546 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7547 | request_id) != 8); |
| 7548 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7549 | function_code) != 10); |
| 7550 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7551 | data.report_device_capability.buffer_length) != 44); |
| 7552 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7553 | data.report_device_capability.sg_descriptor) != 48); |
| 7554 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7555 | data.create_operational_iq.queue_id) != 12); |
| 7556 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7557 | data.create_operational_iq.element_array_addr) != 16); |
| 7558 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7559 | data.create_operational_iq.ci_addr) != 24); |
| 7560 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7561 | data.create_operational_iq.num_elements) != 32); |
| 7562 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7563 | data.create_operational_iq.element_length) != 34); |
| 7564 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7565 | data.create_operational_iq.queue_protocol) != 36); |
| 7566 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7567 | data.create_operational_oq.queue_id) != 12); |
| 7568 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7569 | data.create_operational_oq.element_array_addr) != 16); |
| 7570 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7571 | data.create_operational_oq.pi_addr) != 24); |
| 7572 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7573 | data.create_operational_oq.num_elements) != 32); |
| 7574 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7575 | data.create_operational_oq.element_length) != 34); |
| 7576 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7577 | data.create_operational_oq.queue_protocol) != 36); |
| 7578 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7579 | data.create_operational_oq.int_msg_num) != 40); |
| 7580 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7581 | data.create_operational_oq.coalescing_count) != 42); |
| 7582 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7583 | data.create_operational_oq.min_coalescing_time) != 44); |
| 7584 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7585 | data.create_operational_oq.max_coalescing_time) != 48); |
| 7586 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_request, |
| 7587 | data.delete_operational_queue.queue_id) != 12); |
| 7588 | BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64); |
| 7589 | BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request, |
| 7590 | data.create_operational_iq) != 64 - 11); |
| 7591 | BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request, |
| 7592 | data.create_operational_oq) != 64 - 11); |
| 7593 | BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request, |
| 7594 | data.delete_operational_queue) != 64 - 11); |
| 7595 | |
| 7596 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7597 | header.iu_type) != 0); |
| 7598 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7599 | header.iu_length) != 2); |
| 7600 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7601 | header.work_area) != 6); |
| 7602 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7603 | request_id) != 8); |
| 7604 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7605 | function_code) != 10); |
| 7606 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7607 | status) != 11); |
| 7608 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7609 | data.create_operational_iq.status_descriptor) != 12); |
| 7610 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7611 | data.create_operational_iq.iq_pi_offset) != 16); |
| 7612 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7613 | data.create_operational_oq.status_descriptor) != 12); |
| 7614 | BUILD_BUG_ON(offsetof(struct pqi_general_admin_response, |
| 7615 | data.create_operational_oq.oq_ci_offset) != 16); |
| 7616 | BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64); |
| 7617 | |
| 7618 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7619 | header.iu_type) != 0); |
| 7620 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7621 | header.iu_length) != 2); |
| 7622 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7623 | header.response_queue_id) != 4); |
| 7624 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7625 | header.work_area) != 6); |
| 7626 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7627 | request_id) != 8); |
| 7628 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7629 | nexus_id) != 10); |
| 7630 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7631 | buffer_length) != 12); |
| 7632 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7633 | lun_number) != 16); |
| 7634 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7635 | protocol_specific) != 24); |
| 7636 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7637 | error_index) != 27); |
| 7638 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7639 | cdb) != 32); |
| 7640 | BUILD_BUG_ON(offsetof(struct pqi_raid_path_request, |
| 7641 | sg_descriptors) != 64); |
| 7642 | BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) != |
| 7643 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 7644 | |
| 7645 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7646 | header.iu_type) != 0); |
| 7647 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7648 | header.iu_length) != 2); |
| 7649 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7650 | header.response_queue_id) != 4); |
| 7651 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7652 | header.work_area) != 6); |
| 7653 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7654 | request_id) != 8); |
| 7655 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7656 | nexus_id) != 12); |
| 7657 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7658 | buffer_length) != 16); |
| 7659 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7660 | data_encryption_key_index) != 22); |
| 7661 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7662 | encrypt_tweak_lower) != 24); |
| 7663 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7664 | encrypt_tweak_upper) != 28); |
| 7665 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7666 | cdb) != 32); |
| 7667 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7668 | error_index) != 48); |
| 7669 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7670 | num_sg_descriptors) != 50); |
| 7671 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7672 | cdb_length) != 51); |
| 7673 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7674 | lun_number) != 52); |
| 7675 | BUILD_BUG_ON(offsetof(struct pqi_aio_path_request, |
| 7676 | sg_descriptors) != 64); |
| 7677 | BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) != |
| 7678 | PQI_OPERATIONAL_IQ_ELEMENT_LENGTH); |
| 7679 | |
| 7680 | BUILD_BUG_ON(offsetof(struct pqi_io_response, |
| 7681 | header.iu_type) != 0); |
| 7682 | BUILD_BUG_ON(offsetof(struct pqi_io_response, |
| 7683 | header.iu_length) != 2); |
| 7684 | BUILD_BUG_ON(offsetof(struct pqi_io_response, |
| 7685 | request_id) != 8); |
| 7686 | BUILD_BUG_ON(offsetof(struct pqi_io_response, |
| 7687 | error_index) != 10); |
| 7688 | |
| 7689 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7690 | header.iu_type) != 0); |
| 7691 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7692 | header.iu_length) != 2); |
| 7693 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7694 | header.response_queue_id) != 4); |
| 7695 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7696 | request_id) != 8); |
| 7697 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7698 | data.report_event_configuration.buffer_length) != 12); |
| 7699 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7700 | data.report_event_configuration.sg_descriptors) != 16); |
| 7701 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7702 | data.set_event_configuration.global_event_oq_id) != 10); |
| 7703 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7704 | data.set_event_configuration.buffer_length) != 12); |
| 7705 | BUILD_BUG_ON(offsetof(struct pqi_general_management_request, |
| 7706 | data.set_event_configuration.sg_descriptors) != 16); |
| 7707 | |
| 7708 | BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor, |
| 7709 | max_inbound_iu_length) != 6); |
| 7710 | BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor, |
| 7711 | max_outbound_iu_length) != 14); |
| 7712 | BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16); |
| 7713 | |
| 7714 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7715 | data_length) != 0); |
| 7716 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7717 | iq_arbitration_priority_support_bitmask) != 8); |
| 7718 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7719 | maximum_aw_a) != 9); |
| 7720 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7721 | maximum_aw_b) != 10); |
| 7722 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7723 | maximum_aw_c) != 11); |
| 7724 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7725 | max_inbound_queues) != 16); |
| 7726 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7727 | max_elements_per_iq) != 18); |
| 7728 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7729 | max_iq_element_length) != 24); |
| 7730 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7731 | min_iq_element_length) != 26); |
| 7732 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7733 | max_outbound_queues) != 30); |
| 7734 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7735 | max_elements_per_oq) != 32); |
| 7736 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7737 | intr_coalescing_time_granularity) != 34); |
| 7738 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7739 | max_oq_element_length) != 36); |
| 7740 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7741 | min_oq_element_length) != 38); |
| 7742 | BUILD_BUG_ON(offsetof(struct pqi_device_capability, |
| 7743 | iu_layer_descriptors) != 64); |
| 7744 | BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576); |
| 7745 | |
| 7746 | BUILD_BUG_ON(offsetof(struct pqi_event_descriptor, |
| 7747 | event_type) != 0); |
| 7748 | BUILD_BUG_ON(offsetof(struct pqi_event_descriptor, |
| 7749 | oq_id) != 2); |
| 7750 | BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4); |
| 7751 | |
| 7752 | BUILD_BUG_ON(offsetof(struct pqi_event_config, |
| 7753 | num_event_descriptors) != 2); |
| 7754 | BUILD_BUG_ON(offsetof(struct pqi_event_config, |
| 7755 | descriptors) != 4); |
| 7756 | |
Kevin Barnett | 061ef06 | 2017-05-03 18:53:05 -0500 | [diff] [blame] | 7757 | BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS != |
| 7758 | ARRAY_SIZE(pqi_supported_event_types)); |
| 7759 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7760 | BUILD_BUG_ON(offsetof(struct pqi_event_response, |
| 7761 | header.iu_type) != 0); |
| 7762 | BUILD_BUG_ON(offsetof(struct pqi_event_response, |
| 7763 | header.iu_length) != 2); |
| 7764 | BUILD_BUG_ON(offsetof(struct pqi_event_response, |
| 7765 | event_type) != 8); |
| 7766 | BUILD_BUG_ON(offsetof(struct pqi_event_response, |
| 7767 | event_id) != 10); |
| 7768 | BUILD_BUG_ON(offsetof(struct pqi_event_response, |
| 7769 | additional_event_id) != 12); |
| 7770 | BUILD_BUG_ON(offsetof(struct pqi_event_response, |
| 7771 | data) != 16); |
| 7772 | BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32); |
| 7773 | |
| 7774 | BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request, |
| 7775 | header.iu_type) != 0); |
| 7776 | BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request, |
| 7777 | header.iu_length) != 2); |
| 7778 | BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request, |
| 7779 | event_type) != 8); |
| 7780 | BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request, |
| 7781 | event_id) != 10); |
| 7782 | BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request, |
| 7783 | additional_event_id) != 12); |
| 7784 | BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16); |
| 7785 | |
| 7786 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7787 | header.iu_type) != 0); |
| 7788 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7789 | header.iu_length) != 2); |
| 7790 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7791 | request_id) != 8); |
| 7792 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7793 | nexus_id) != 10); |
| 7794 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7795 | lun_number) != 16); |
| 7796 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7797 | protocol_specific) != 24); |
| 7798 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7799 | outbound_queue_id_to_manage) != 26); |
| 7800 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7801 | request_id_to_manage) != 28); |
| 7802 | BUILD_BUG_ON(offsetof(struct pqi_task_management_request, |
| 7803 | task_management_function) != 30); |
| 7804 | BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32); |
| 7805 | |
| 7806 | BUILD_BUG_ON(offsetof(struct pqi_task_management_response, |
| 7807 | header.iu_type) != 0); |
| 7808 | BUILD_BUG_ON(offsetof(struct pqi_task_management_response, |
| 7809 | header.iu_length) != 2); |
| 7810 | BUILD_BUG_ON(offsetof(struct pqi_task_management_response, |
| 7811 | request_id) != 8); |
| 7812 | BUILD_BUG_ON(offsetof(struct pqi_task_management_response, |
| 7813 | nexus_id) != 10); |
| 7814 | BUILD_BUG_ON(offsetof(struct pqi_task_management_response, |
| 7815 | additional_response_info) != 12); |
| 7816 | BUILD_BUG_ON(offsetof(struct pqi_task_management_response, |
| 7817 | response_code) != 15); |
| 7818 | BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16); |
| 7819 | |
| 7820 | BUILD_BUG_ON(offsetof(struct bmic_identify_controller, |
| 7821 | configured_logical_drive_count) != 0); |
| 7822 | BUILD_BUG_ON(offsetof(struct bmic_identify_controller, |
| 7823 | configuration_signature) != 1); |
| 7824 | BUILD_BUG_ON(offsetof(struct bmic_identify_controller, |
| 7825 | firmware_version) != 5); |
| 7826 | BUILD_BUG_ON(offsetof(struct bmic_identify_controller, |
| 7827 | extended_logical_unit_count) != 154); |
| 7828 | BUILD_BUG_ON(offsetof(struct bmic_identify_controller, |
| 7829 | firmware_build_number) != 190); |
| 7830 | BUILD_BUG_ON(offsetof(struct bmic_identify_controller, |
| 7831 | controller_mode) != 292); |
| 7832 | |
Kevin Barnett | 1be42f4 | 2017-05-03 18:53:42 -0500 | [diff] [blame] | 7833 | BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device, |
| 7834 | phys_bay_in_box) != 115); |
| 7835 | BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device, |
| 7836 | device_type) != 120); |
| 7837 | BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device, |
| 7838 | redundant_path_present_map) != 1736); |
| 7839 | BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device, |
| 7840 | active_path_number) != 1738); |
| 7841 | BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device, |
| 7842 | alternate_paths_phys_connector) != 1739); |
| 7843 | BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device, |
| 7844 | alternate_paths_phys_box_on_port) != 1755); |
| 7845 | BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device, |
| 7846 | current_queue_depth_limit) != 1796); |
| 7847 | BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560); |
| 7848 | |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7849 | BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255); |
| 7850 | BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255); |
| 7851 | BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH % |
| 7852 | PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0); |
| 7853 | BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH % |
| 7854 | PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0); |
| 7855 | BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560); |
| 7856 | BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH % |
| 7857 | PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0); |
| 7858 | BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560); |
| 7859 | BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH % |
| 7860 | PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0); |
| 7861 | |
| 7862 | BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS); |
Kevin Barnett | d727a77 | 2017-05-03 18:54:25 -0500 | [diff] [blame] | 7863 | BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= |
| 7864 | PQI_MAX_OUTSTANDING_REQUESTS_KDUMP); |
Kevin Barnett | 6c22376 | 2016-06-27 16:41:00 -0500 | [diff] [blame] | 7865 | } |