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