xref: /linux/drivers/scsi/leapraid/leapraid_func.h (revision 049380360ca6f4cc22ac2f67fe95b98967c887f0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2026 LeapIO Tech Inc.
4  *
5  * LeapRAID storage and RAID controller driver.
6  */
7 #ifndef LEAPRAID_FUNC_H_INCLUDED
8 #define LEAPRAID_FUNC_H_INCLUDED
9 
10 #include <linux/pci.h>
11 #include <linux/aer.h>
12 #include <linux/poll.h>
13 #include <linux/atomic.h>
14 #include <linux/errno.h>
15 #include <linux/ktime.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_eh.h>
20 #include <scsi/scsicam.h>
21 #include <scsi/scsi_tcq.h>
22 #include <scsi/scsi_dbg.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_transport_sas.h>
27 
28 #include "leapraid.h"
29 
30 /* Request and reply buffer size. */
31 #define LEAPRAID_REQUEST_SIZE                   128
32 #define LEAPRAID_REPLY_SIZE                     128
33 #define LEAPRAID_CHAIN_SEG_SIZE                 128
34 #define LEAPRAID_MAX_SGES_IN_CHAIN              7
35 #define LEAPRAID_DEFAULT_CHAINS_PER_IO          19
36 #define LEAPRAID_DEFAULT_DIX_CHAINS_PER_IO      \
37 	(2 * LEAPRAID_DEFAULT_CHAINS_PER_IO) /* TODO DIX */
38 #define LEAPRAID_IEEE_SGE64_ENTRY_SIZE          16
39 #define LEAPRAID_REP_DESC_CHUNK_SIZE            16
40 #define LEAPRAID_REP_DESC_ENTRY_SIZE            8
41 #define LEAPRAID_REP_MSG_ADDR_SIZE              4
42 #define LEAPRAID_REP_RQ_CNT_SIZE                16
43 
44 #define LEAPRAID_SYS_LOG_BUF_SIZE       0x200000
45 #define LEAPRAID_SYS_LOG_BUF_RESERVE    0x1000
46 
47 /* Driver version and name. */
48 #define LEAPRAID_DRIVER_NAME            "LeapRAID"
49 #define LEAPRAID_NAME_LENGTH            48
50 #define LEAPRAID_BOARD_NAME_LENGTH      17
51 #define LEAPRAID_AUTHOR                 "LeapIO Inc."
52 #define LEAPRAID_DESCRIPTION            "LeapRAID Driver"
53 #define LEAPRAID_DRIVER_VERSION         "2.00.01.09"
54 #define LEAPRAID_MAJOR_VERSION          2
55 #define LEAPRAID_MINOR_VERSION          0
56 #define LEAPRAID_BUILD_VERSION          1
57 #define LEAPRAID_RELEASE_VERSION        9
58 #define LEAPRAID_MSG_VERSION            0x1021
59 #define LEAPRAID_HEADER_VERSION         0x0000
60 
61 /* Device ID. */
62 #define LEAPRAID_VENDOR_ID      0xD405
63 #define LEAPRAID_DEVID_HBA      0x8200
64 #define LEAPRAID_SUBVENDOR_ID   0xD405
65 #define LEAPRAID_SUBDEVID_HBA   0x8200
66 
67 #define LEAPRAID_PCI_VENDOR_ID_MASK     0xFFFF
68 
69  /* RAID virtual channel ID. */
70 #define RAID_CHANNEL    1
71 
72 /* Scatter-gather (SG) segment limits. */
73 #define LEAPRAID_MAX_PHYS_SEGMENTS      SG_CHUNK_SIZE
74 
75 #define LEAPRAID_KDUMP_MIN_PHYS_SEGMENTS        32
76 #define LEAPRAID_SG_DEPTH                       LEAPRAID_MAX_PHYS_SEGMENTS
77 
78 /* Firmware and config page operations. */
79 #define LEAPRAID_CFG_REQ_RETRY_TIMES    2
80 
81 /* Hardware access helpers. */
82 #define leapraid_readl(addr) readl(addr)
83 #define leapraid_check_reset(status) \
84 	(!((status) & LEAPRAID_CMD_RESET))
85 
86 /* Polling intervals. */
87 #define LEAPRAID_PCIE_LOG_POLLING_INTERVAL      1
88 #define LEAPRAID_FAULT_POLLING_INTERVAL         1000
89 
90 /* Init mask. */
91 #define LEAPRAID_RESET_IRQ_MASK 0x40000000
92 #define LEAPRAID_REPLY_INT_MASK 0x00000008
93 #define LEAPRAID_TO_SYS_DB_MASK 0x00000001
94 
95 /* Queue depth. */
96 #define LEAPRAID_SATA_QUEUE_DEPTH       32
97 #define LEAPRAID_SAS_QUEUE_DEPTH        64
98 #define LEAPRAID_RAID_QUEUE_DEPTH       64
99 
100 /* Target probe flag. */
101 #define LEAPRAID_NO_ULD_ATTACH_FLAG 1
102 
103 /* SCSI device and queue limits. */
104 #define LEAPRAID_MAX_SECTORS            2048
105 #define LEAPRAID_MAX_CDB_LEN            32
106 #define LEAPRAID_MAX_LUNS               16384
107 #define LEAPRAID_CAN_QUEUE_MIN          1
108 #define LEAPRAID_THIS_ID_NONE           -1
109 #define LEAPRAID_CMD_PER_LUN            128
110 #define LEAPRAID_MAX_SEGMENT_SIZE       0xffffffff
111 
112 /* SCSI sense and ASC/ASCQ and disk geometry configuration. */
113 #define DESC_FORMAT_THRESHOLD                   0x72
114 #define SENSE_KEY_MASK                          0x0F
115 #define SCSI_SENSE_RESPONSE_CODE_MASK           0x7F
116 #define ASC_FAILURE_PREDICTION_THRESHOLD_EXCEEDED       0x5D
117 #define LEAPRAID_LARGE_DISK_THRESHOLD           0x200000UL
118 #define LEAPRAID_LARGE_DISK_HEADS               255
119 #define LEAPRAID_LARGE_DISK_SECTORS             63
120 #define LEAPRAID_SMALL_DISK_HEADS               64
121 #define LEAPRAID_SMALL_DISK_SECTORS             32
122 
123 /* SMP (Serial Management Protocol). */
124 #define LEAPRAID_SMP_PT_FLAG_SGL_PTR            0x80
125 #define LEAPRAID_SMP_FRAME_HEADER_SIZE          4
126 #define LEAPRAID_SCSI_HOST_SHIFT                16
127 #define LEAPRAID_SCSI_DRIVER_SHIFT              24
128 
129 /* SCSI ASC/ASCQ definitions. */
130 #define LEAPRAID_SCSI_ASCQ_DEFAULT              0x00
131 #define LEAPRAID_SCSI_ASC_POWER_ON_RESET        0x29
132 #define LEAPRAID_SCSI_ASC_INVALID_CMD_CODE      0x20
133 #define LEAPRAID_SCSI_ASCQ_POWER_ON_RESET       0x07
134 
135 /* VPD Page 0x89 (ATA Information). */
136 #define LEAPRAID_VPD_PAGE_ATA_INFO      0x89
137 #define LEAPRAID_VPD_PG89_MAX_LEN       255
138 #define LEAPRAID_VPD_PG89_MIN_LEN       214
139 
140 /* Byte index for NCQ support flag in VPD page 0x89. */
141 #define LEAPRAID_VPD_PG89_NCQ_BYTE_IDX  213
142 #define LEAPRAID_VPD_PG89_NCQ_BIT_SHIFT 4
143 #define LEAPRAID_VPD_PG89_NCQ_BIT_MASK  0x1
144 
145 /* Readiness polling: max retries, sleep µs between. */
146 #define LEAPRAID_ADAPTER_READY_MAX_RETRY        15000
147 #define LEAPRAID_ADAPTER_READY_SLEEP_MIN_US     1000
148 #define LEAPRAID_ADAPTER_READY_SLEEP_MAX_US     1100
149 
150 /* Doorbell wait parameters. */
151 #define LEAPRAID_DB_WAIT_MAX_RETRY      20000
152 #define LEAPRAID_DB_WAIT_DELAY_US       500
153 
154 /* Basic data size definitions. */
155 #define LEAPRAID_DWORDS_BYTE_SIZE       4
156 #define LEAPRAID_WORD_BYTE_SIZE         2
157 
158 /* SGL threshold and chain offset. */
159 #define LEAPRAID_SGL_INLINE_THRESHOLD   2
160 #define LEAPRAID_CHAIN_OFFSET_DWORDS    7
161 
162 /* MSI-X group size and mask. */
163 #define LEAPRAID_MSIX_GROUP_SIZE        8
164 #define LEAPRAID_MSIX_GROUP_MASK        7
165 
166 /* Basic constants and limits.   */
167 #define LEAPRAID_BUSY_LIMIT             1
168 #define LEAPRAID_INVALID_HOST_DIAG_VAL  0xFFFFFFFF
169 
170 /* Retry/Sleep configuration. */
171 #define LEAPRAID_WRSEQ_FLUSH_KEY_VALUE  0x0
172 #define LEAPRAID_WRSEQ_1ST_KEY_VALUE    0xF
173 #define LEAPRAID_WRSEQ_2ND_KEY_VALUE    0x4
174 #define LEAPRAID_WRSEQ_3RD_KEY_VALUE    0xB
175 #define LEAPRAID_WRSEQ_4TH_KEY_VALUE    0x2
176 #define LEAPRAID_WRSEQ_5TH_KEY_VALUE    0x7
177 #define LEAPRAID_WRSEQ_6TH_KEY_VALUE    0xD
178 #define LEAPRAID_UNLOCK_RETRY_LIMIT     20
179 #define LEAPRAID_UNLOCK_SLEEP_MS        100
180 #define LEAPRAID_MSLEEP_NORMAL_MS       100
181 #define LEAPRAID_MSLEEP_EXTRA_LONG_MS   500
182 #define LEAPRAID_IO_POLL_DELAY_US       500
183 
184 /* Device/Volume configuration. */
185 #define LEAPRAID_MAX_VOLUMES_DEFAULT    32
186 #define LEAPRAID_MAX_DEV_HANDLE_DEFAULT 2048
187 #define LEAPRAID_INVALID_DEV_HANDLE     0xFFFF
188 
189 /* Commands queue depth. */
190 #define LEAPRAID_DEFAULT_CMD_QD_OFFSET  64
191 #define LEAPRAID_REPLY_QD_ALIGNMENT     16
192 /* Task ID offset. */
193 #define LEAPRAID_TASKID_OFFSET_CTRL_CMD         1
194 #define LEAPRAID_TASKID_OFFSET_CFG_OP_CMD               1
195 #define LEAPRAID_TASKID_OFFSET_TRANSPORT_CMD            2
196 #define LEAPRAID_TASKID_OFFSET_ENC_CMD                  3
197 #define LEAPRAID_TASKID_OFFSET_NOTIFY_EVENT_CMD         4
198 
199 /* Task ID offset for high-priority. */
200 #define LEAPRAID_HP_TASKID_OFFSET_CTL_CMD       0
201 #define LEAPRAID_HP_TASKID_OFFSET_TM_CMD        1
202 
203 /* Event/Boot configuration. */
204 #define LEAPRAID_EVT_MASK_COUNT 4
205 #define LEAPRAID_BOOT_DEV_SIZE  24
206 
207 /* Commands timeout. */
208 #define LEAPRAID_UNIFIED_TIMEOUT               30
209 #define LEAPRAID_CFG_OP_TIMEOUT                LEAPRAID_UNIFIED_TIMEOUT
210 #define LEAPRAID_CTL_CMD_TIMEOUT               LEAPRAID_UNIFIED_TIMEOUT
211 #define LEAPRAID_SCAN_DEV_CMD_TIMEOUT          300
212 #define LEAPRAID_ENC_CMD_TIMEOUT               LEAPRAID_UNIFIED_TIMEOUT
213 #define LEAPRAID_IO_CMD_TIMEOUT                LEAPRAID_UNIFIED_TIMEOUT
214 #define LEAPRAID_NOTIFY_EVENT_CMD_TIMEOUT      LEAPRAID_UNIFIED_TIMEOUT
215 #define LEAPRAID_TM_CMD_TIMEOUT                LEAPRAID_UNIFIED_TIMEOUT
216 #define LEAPRAID_TRANSPORT_CMD_TIMEOUT         LEAPRAID_UNIFIED_TIMEOUT
217 
218 /* Host DMA cap. */
219 #define DMA_32_BITS    32
220 #define DMA_64_BITS    64
221 #define LEAPRAID_DMA_ALIGN    16
222 
223 /* Values used to represent an invalid. */
224 #define LEAPRAID_INVALID_INDEX         -1
225 #define LEAPRAID_INVALID_MSIX_VECTORS  -1
226 #define LEAPRAID_INVALID_INITIAL_VALUE -1
227 #define LEAPRAID_OPERATION_FAILED      -1
228 
229 /* Version shift and mask. */
230 #define LEAPRAID_VER_MAJOR_SHIFT 24
231 #define LEAPRAID_VER_MINOR_SHIFT 16
232 #define LEAPRAID_VER_BUILD_SHIFT 8
233 #define LEAPRAID_VER_MASK        0xFF
234 
235 /* Interrupt type. */
236 #define LEAPRAID_INTERRUPT_MODE_MSIX 0
237 #define LEAPRAID_INTERRUPT_MODE_MSI  1
238 #define LEAPRAID_INTERRUPT_MODE_LEGACY 2
239 
240 /* SMP link reset. */
241 #define SMP_PHY_CONTROL_LINK_RESET    0x01
242 #define SMP_PHY_CONTROL_HARD_RESET    0x02
243 #define SMP_PHY_CONTROL_DISABLE       0x03
244 
245 /**
246  * struct leapraid_adapter_features - Adapter features/capabilities.
247  *
248  * @req_slot: Number of request slots supported by the adapter.
249  * @hp_slot: Number of high-priority slots supported by the adapter.
250  * @adapter_caps: Adapter capabilities.
251  * @fw_version: Firmware version of the adapter.
252  * @max_dev_handle: Maximum device handle supported by the adapter.
253  * @min_dev_handle: Minimum device handle supported by the adapter.
254  */
255 struct leapraid_adapter_features {
256 	u16 req_slot;
257 	u16 hp_slot;
258 	u32 adapter_caps;
259 	u32 fw_version;
260 	u8 max_msix_vectors;
261 	u8 max_volumes;
262 	u16 max_dev_handle;
263 	u16 min_dev_handle;
264 	u16 msg_ver;
265 	u16 product_id;
266 };
267 
268 /**
269  * struct leapraid_adapter_attr - Adapter attributes and capabilities
270  *
271  * @id: Adapter identifier.
272  * @raid_support: Indicates if RAID is supported.
273  * @bios_version: Version of the adapter BIOS.
274  * @enable_mp: Indicates if multipath (MP) support is enabled.
275  * @wideport_max_queue_depth: Maximum queue depth for wide ports.
276  * @narrowport_max_queue_depth: Maximum queue depth for narrow ports.
277  * @sata_max_queue_depth: Maximum queue depth for SATA.
278  * @raid_volume_max_queue_depth: Maximum queue depth for RAID volumes.
279  * @features: Detailed features of the adapter.
280  * @adapter_total_qd: Total queue depth available on the adapter.
281  * @io_qd: Queue depth allocated for I/O operations.
282  * @rep_msg_qd: Queue depth for reply messages.
283  * @rep_desc_qd: Queue depth for reply descriptors.
284  * @rep_desc_q_seg_cnt: Number of segments in a reply descriptor queue.
285  * @rq_cnt: Number of request queues.
286  * @task_desc_dma_size: Size of task descriptor DMA memory.
287  * @use_32_dma_mask: Indicates if 32-bit DMA mask is used.
288  * @name: Adapter name string.
289  * @board_name: Board name retrieved from manufacturing page 0.
290  */
291 struct leapraid_adapter_attr {
292 	u8 id;
293 	u8 raid_support;
294 	u32 bios_version;
295 	u8 enable_mp;
296 	u32 wideport_max_queue_depth;
297 	u32 narrowport_max_queue_depth;
298 	u32 sata_max_queue_depth;
299 	u32 raid_volume_max_queue_depth;
300 	struct leapraid_adapter_features features;
301 	u32 adapter_total_qd;
302 	u32 io_qd;
303 	u32 rep_msg_qd;
304 	u32 rep_desc_qd;
305 	u32 rep_desc_q_seg_cnt;
306 	u16 rq_cnt;
307 	u32 task_desc_dma_size;
308 	u8 use_32_dma_mask;
309 	char name[LEAPRAID_NAME_LENGTH];
310 	char board_name[LEAPRAID_BOARD_NAME_LENGTH];
311 };
312 
313 /**
314  * struct leapraid_io_req_tracker - Track a SCSI I/O request for the adapter
315  *
316  * @taskid: Unique task ID for this I/O request.
317  * @scmd: Pointer to the associated SCSI command.
318  * @chain_list: List of chain frames associated with this request.
319  * @msix_io: MSI-X vector assigned to this I/O request.
320  * @chain: Pointer to the chain memory for this request.
321  * @chain_dma: DMA address of the chain memory.
322  */
323 struct leapraid_io_req_tracker {
324 	u16 taskid;
325 	struct scsi_cmnd *scmd;
326 	struct list_head chain_list;
327 	u16 msix_io;
328 	void *chain;
329 	dma_addr_t chain_dma;
330 };
331 
332 /**
333  * struct leapraid_task_tracker - Tracks a task in the adapter
334  *
335  * @taskid: Unique task ID for this tracker.
336  * @cb_idx: Callback index associated with this task.
337  * @tracker_list: Linked list node to chain this tracker in lists.
338  */
339 struct leapraid_task_tracker {
340 	u16 taskid;
341 	u8 cb_idx;
342 	struct list_head tracker_list;
343 };
344 
345 /**
346  * struct leapraid_rep_desc_maint - Maintains reply descriptor memory
347  *
348  * @rep_desc: Pointer to the reply descriptor.
349  * @rep_desc_dma: DMA address of the reply descriptor.
350  */
351 struct leapraid_rep_desc_maint {
352 	union leapraid_rep_desc_union *rep_desc;
353 	dma_addr_t rep_desc_dma;
354 };
355 
356 /**
357  * struct leapraid_rep_desc_seg_maint -
358  * Maintains reply descriptor segment memory
359  *
360  * @rep_desc_seg: Pointer to the reply descriptor segment.
361  * @rep_desc_seg_dma: DMA address of the reply descriptor segment.
362  * @rep_desc_maint: Pointer to the main reply descriptor structure.
363  */
364 struct leapraid_rep_desc_seg_maint {
365 	void *rep_desc_seg;
366 	dma_addr_t rep_desc_seg_dma;
367 	struct leapraid_rep_desc_maint *rep_desc_maint;
368 };
369 
370 /**
371  * struct leapraid_mem_desc - Memory descriptor for LeapRAID adapter
372  *
373  * @task_desc: Pointer to task descriptor.
374  * @task_desc_dma: DMA address of task descriptor.
375  * @sg_chain_pool: DMA pool for SGL chain allocations.
376  * @sg_chain_pool_size: Size of the sg_chain_pool.
377  * @taskid_to_uniq_tag: Mapping from task ID to unique tag.
378  * @sense_data: Buffer for SCSI sense data.
379  * @sense_data_dma: DMA address of sense_data buffer.
380  * @rep_msg: Buffer for reply message.
381  * @rep_msg_dma: DMA address of reply message buffer.
382  * @rep_msg_addr: Pointer to reply message address.
383  * @rep_msg_addr_dma: DMA address of reply message address.
384  * @rep_desc_seg_maint: Pointer to reply descriptor segment.
385  * @rep_desc_q_arr: Pointer to reply descriptor queue array.
386  * @rep_desc_q_arr_dma: DMA address of reply descriptor queue array.
387  */
388 struct leapraid_mem_desc {
389 	void *task_desc;
390 	dma_addr_t task_desc_dma;
391 	struct dma_pool *sg_chain_pool;
392 	u16 sg_chain_pool_size;
393 	u16 *taskid_to_uniq_tag;
394 	u8 *sense_data;
395 	dma_addr_t sense_data_dma;
396 	u8 *rep_msg;
397 	dma_addr_t rep_msg_dma;
398 	__le32 *rep_msg_addr;
399 	dma_addr_t rep_msg_addr_dma;
400 	struct leapraid_rep_desc_seg_maint *rep_desc_seg_maint;
401 	struct leapraid_rep_desc_q_arr *rep_desc_q_arr;
402 	dma_addr_t rep_desc_q_arr_dma;
403 };
404 
405 /* internal cmd description */
406 #define LEAPRAID_FIXED_INTER_CMDS       5
407 #define LEAPRAID_FIXED_HP_CMDS          2
408 
409 #define LEAPRAID_CMD_NOT_USED           0x8000
410 #define LEAPRAID_CMD_DONE               0x0001
411 #define LEAPRAID_CMD_PENDING            0x0002
412 #define LEAPRAID_CMD_REPLY_VALID        0x0004
413 #define LEAPRAID_CMD_RESET              0x0008
414 
415 /**
416  * enum LEAPRAID_CB_INDEX - Callback index for LeapRAID driver
417  *
418  * @LEAPRAID_SCAN_DEV_CB_IDX: Scan device callback index.
419  * @LEAPRAID_CONFIG_CB_IDX: Configuration callback index.
420  * @LEAPRAID_TRANSPORT_CB_IDX: Transport callback index.
421  * @LEAPRAID_SAS_CTRL_CB_IDX: SAS controller callback index.
422  * @LEAPRAID_ENC_CB_IDX: Encryption callback index.
423  * @LEAPRAID_NOTIFY_EVENT_CB_IDX: Notify event callback index.
424  * @LEAPRAID_CTL_CB_IDX: Control callback index.
425  * @LEAPRAID_TM_CB_IDX: Task management callback index.
426  */
427 enum LEAPRAID_CB_INDEX {
428 	LEAPRAID_SCAN_DEV_CB_IDX	= 0x1,
429 	LEAPRAID_CONFIG_CB_IDX		= 0x2,
430 	LEAPRAID_TRANSPORT_CB_IDX	= 0x3,
431 	LEAPRAID_SAS_CTRL_CB_IDX	= 0x5,
432 	LEAPRAID_ENC_CB_IDX		= 0x6,
433 	LEAPRAID_NOTIFY_EVENT_CB_IDX	= 0x7,
434 	LEAPRAID_CTL_CB_IDX		= 0x8,
435 	LEAPRAID_TM_CB_IDX		= 0x9,
436 	LEAPRAID_NUM_CB_IDXS
437 };
438 
439 /**
440  * struct leapraid_default_reply - Default reply frame buffer
441  * @pad: Raw reply data buffer returned by firmware.
442  *
443  * This structure represents a generic reply frame used by the firmware
444  * when no specific reply format is required. The buffer size is defined
445  * by LEAPRAID_REPLY_SIZE and stores the raw reply data.
446  */
447 struct leapraid_default_reply {
448 	u8 pad[LEAPRAID_REPLY_SIZE];
449 };
450 
451 /**
452  * struct leapraid_sense_buffer - SCSI sense data buffer
453  * @pad: Buffer used to store sense data returned by a SCSI command.
454  *
455  * This buffer holds the sense data provided by a device.The size is
456  * defined by SCSI_SENSE_BUFFERSIZE.
457  */
458 struct leapraid_sense_buffer {
459 	u8 pad[SCSI_SENSE_BUFFERSIZE];
460 };
461 
462 /**
463  * struct leapraid_driver_cmd - Driver command tracking structure
464  *
465  * @reply: Default reply structure returned by the adapter.
466  * @done: Completion object used to signal command completion.
467  * @status: Status code returned by the firmware.
468  * @taskid: Unique task identifier for this command.
469  * @hp_taskid: Task identifier for high-priority commands.
470  * @inter_taskid: Task identifier for internal commands.
471  * @cb_idx: Callback index used to identify completion context.
472  * @async_scan_dev: True if this command is for asynchronous device scan.
473  * @sense: Sense buffer holding error information from device.
474  * @mutex: Mutex to protect access to this command structure.
475  * @list: List node for linking driver commands into lists.
476  */
477 struct leapraid_driver_cmd {
478 	struct leapraid_default_reply reply;
479 	struct completion done;
480 	u16 status;
481 	u16 taskid;
482 	u16 hp_taskid;
483 	u16 inter_taskid;
484 	u8 cb_idx;
485 	u8 async_scan_dev;
486 	struct leapraid_sense_buffer sense;
487 	struct mutex mutex; /* Mutex for driver cmd */
488 	struct list_head list;
489 };
490 
491 /**
492  * struct leapraid_driver_cmds - Collection of driver command objects
493  *
494  * @special_cmd_list: List head for tracking special driver commands.
495  * @scan_dev_cmd: Command used for asynchronous device scan operations.
496  * @cfg_op_cmd: Command for configuration operations.
497  * @transport_cmd: Command for transport-level operations.
498  * @enc_cmd: Command for enclosure management operations.
499  * @notify_event_cmd: Command for asynchronous event notification handling.
500  * @ctl_cmd: Command for generic control or maintenance operations.
501  * @tm_cmd: Task management command.
502  */
503 struct leapraid_driver_cmds {
504 	struct list_head special_cmd_list;
505 	struct leapraid_driver_cmd scan_dev_cmd;
506 	struct leapraid_driver_cmd cfg_op_cmd;
507 	struct leapraid_driver_cmd transport_cmd;
508 	struct leapraid_driver_cmd enc_cmd;
509 	struct leapraid_driver_cmd notify_event_cmd;
510 	struct leapraid_driver_cmd ctl_cmd;
511 	struct leapraid_driver_cmd tm_cmd;
512 };
513 
514 /**
515  * struct leapraid_dynamic_task_desc - Dynamic task descriptor
516  *
517  * @task_lock: Spinlock to protect concurrent access.
518  * @hp_taskid: Current high-priority task ID.
519  * @hp_cmd_qd: Fixed command queue depth for high-priority tasks.
520  * @inter_taskid: Current internal task ID.
521  * @inter_cmd_qd: Fixed command queue depth for internal tasks.
522  */
523 struct leapraid_dynamic_task_desc {
524 	spinlock_t task_lock; /* protects dynamic task */
525 	u16 hp_taskid;
526 	u16 hp_cmd_qd;
527 	u16 inter_taskid;
528 	u16 inter_cmd_qd;
529 };
530 
531 /**
532  * struct leapraid_fw_evt_work - Firmware event work structure
533  *
534  * @list: Linked list node for queuing the work.
535  * @adapter: Pointer to the associated LeapRAID adapter.
536  * @work: Work structure used by the kernel workqueue.
537  * @refcnt: Reference counter for managing the lifetime of this work.
538  * @evt_data: Pointer to firmware event data.
539  * @dev_handle: Device handle associated with the event.
540  * @evt_type: Type of firmware event.
541  * @ignore: Flag indicating whether the event should be ignored.
542  */
543 struct leapraid_fw_evt_work {
544 	struct list_head list;
545 	struct leapraid_adapter *adapter;
546 	struct work_struct work;
547 	struct kref refcnt;
548 	void *evt_data;
549 	u16 dev_handle;
550 	u16 evt_type;
551 	u8 ignore;
552 };
553 
554 /**
555  * struct leapraid_fw_evt_struct - Firmware event handling structure
556  *
557  * @fw_evt_thread: Workqueue used for processing firmware events.
558  * @fw_evt_lock: Spinlock protecting access to the firmware event list.
559  * @fw_evt_list: Linked list of pending firmware events.
560  * @cur_evt: Pointer to the currently processing firmware event.
561  * @cur_evt_task: Task currently executing @cur_evt work.
562  * @fw_evt_cleanup: Flag indicating whether cleanup of events is in progress.
563  * @leapraid_evt_masks: Array of event masks for filtering firmware events.
564  */
565 struct leapraid_fw_evt_struct {
566 	u32 leapraid_evt_masks[4];
567 	struct workqueue_struct *fw_evt_thread;
568 	spinlock_t fw_evt_lock; /* protects firmware event */
569 	struct list_head fw_evt_list;
570 	struct leapraid_fw_evt_work *cur_evt;
571 	struct task_struct *cur_evt_task;
572 	u8 fw_evt_cleanup;
573 };
574 
575 /**
576  * struct leapraid_rq - Represents a LeapRAID request queue
577  *
578  * @adapter: Pointer to the associated LeapRAID adapter.
579  * @msix_idx: MSI-X vector index used by this queue.
580  * @rep_post_host_idx: Index of the last processed reply descriptor.
581  * @rep_desc: Pointer to the reply descriptor associated with this queue.
582  * @name: Name of the request queue.
583  * @busy: Atomic counter indicating if the queue is busy.
584  */
585 struct leapraid_rq {
586 	struct leapraid_adapter *adapter;
587 	u8 msix_idx;
588 	u32 rep_post_host_idx;
589 	union leapraid_rep_desc_union *rep_desc;
590 	char name[LEAPRAID_NAME_LENGTH];
591 	atomic_t busy;
592 };
593 
594 /**
595  * struct leapraid_int_rq - Internal request queue for a CPU
596  *
597  * @affinity_hint: CPU affinity mask for the queue.
598  * @rq: Underlying LeapRAID request queue structure.
599  */
600 struct leapraid_int_rq {
601 	cpumask_var_t affinity_hint;
602 	struct leapraid_rq rq;
603 };
604 
605 /**
606  * struct leapraid_blk_mq_poll_rq - Polling request for LeapRAID blk-mq
607  *
608  * @busy: Atomic flag indicating request is being processed.
609  * @pause: Atomic flag to temporarily suspend polling.
610  * @rq: The underlying LeapRAID request structure.
611  */
612 struct leapraid_blk_mq_poll_rq {
613 	atomic_t busy;
614 	atomic_t pause;
615 	struct leapraid_rq rq;
616 };
617 
618 /**
619  * struct leapraid_notification_desc - Notification descriptor for LeapRAID
620  *
621  * @iopoll_qdex: Index of the I/O polling queue.
622  * @iopoll_qcnt: Count of I/O polling queues.
623  * @msix_enable: Flag indicating MSI-X is enabled.
624  * @irq_vectors_allocated: Flag indicating PCI IRQ vectors are allocated.
625  * @irq_mode: Actual interrupt mode used for allocated PCI IRQ vectors.
626  * @msix_cpu_map: CPU-ID indexed MSI-X queue map.
627  * @msix_cpu_map_sz: Number of CPU-ID slots allocated in @msix_cpu_map.
628  * @int_rqs: Array of interrupt request queues.
629  * @int_rqs_allocated: Count of allocated interrupt request queues.
630  * @blk_mq_poll_rqs: Array of blk-mq polling requests.
631  */
632 struct leapraid_notification_desc {
633 	u32 iopoll_qdex;
634 	u32 iopoll_qcnt;
635 	u8 msix_enable;
636 	u8 irq_vectors_allocated;
637 	u8 irq_mode;
638 	u8 *msix_cpu_map;
639 	u32 msix_cpu_map_sz;
640 	struct leapraid_int_rq *int_rqs;
641 	u32 int_rqs_allocated;
642 	struct leapraid_blk_mq_poll_rq *blk_mq_poll_rqs;
643 };
644 
645 /**
646  * struct leapraid_overheat_desc - Overheat descriptor for LeapRAID
647  *
648  * @fault_overheat_wq: Workqueue for adapter thermal overheat operations.
649  * @fault_overheat_work: Work structure for thermal overheat.
650  * @thermal_alert: Flag indicating if adapter thermal alert is active.
651  * @fault_overheat_wq_name: Name of the fault overheat workqueue.
652  */
653 struct leapraid_overheat_desc {
654 	struct workqueue_struct *fault_overheat_wq;
655 	struct work_struct fault_overheat_work;
656 	atomic_t thermal_alert;
657 	char fault_overheat_wq_name[48];
658 };
659 
660 /**
661  * struct leapraid_reset_desc - Reset descriptor for LeapRAID
662  *
663  * @fault_reset_wq: Workqueue for fault reset operations.
664  * @fault_reset_work: Delayed work structure for fault reset.
665  * @fault_reset_wq_name: Name of the fault reset workqueue.
666  * @host_diag_mutex: Mutex for host diagnostic operations.
667  * @adapter_reset_lock: Spinlock for adapter reset operations.
668  * @adapter_reset_mutex: Mutex for adapter reset operations.
669  * @adapter_link_resetting: Flag indicating if adapter link is resetting.
670  * @adapter_reset_results: Results of the adapter reset operation.
671  * @pending_io_cnt: Count of pending I/O operations.
672  * @reset_wait_queue: Wait queue for reset operations.
673  * @reset_cnt: Counter for reset operations.
674  */
675 struct leapraid_reset_desc {
676 	struct workqueue_struct *fault_reset_wq;
677 	struct delayed_work fault_reset_work;
678 	char fault_reset_wq_name[48];
679 	struct mutex host_diag_mutex; /* Mutex for operations. */
680 	spinlock_t adapter_reset_lock; /* Protects adapter reset state. */
681 	struct mutex adapter_reset_mutex; /* Serializes adapter reset. */
682 	u8 adapter_link_resetting;
683 	int adapter_reset_results;
684 	int pending_io_cnt;
685 	wait_queue_head_t reset_wait_queue;
686 	u32 reset_cnt;
687 };
688 
689 /**
690  * struct leapraid_scan_dev_desc - Scan device descriptor for LeapRAID
691  *
692  * @wait_scan_dev_done: Flag indicating if scan device operation is done.
693  * @driver_loading: Flag indicating if driver is loading.
694  * @first_scan_dev_fired: Flag indicating if first scan device operation fired.
695  * @scan_dev_failed: Flag indicating if scan device operation failed.
696  * @scan_start: Flag indicating if scan operation started.
697  * @scan_start_failed: Count of failed scan start operations.
698  */
699 struct leapraid_scan_dev_desc {
700 	u8 wait_scan_dev_done;
701 	u8 driver_loading;
702 	wait_queue_head_t wait_driver_loading;
703 	u8 first_scan_dev_fired;
704 	u8 scan_dev_failed;
705 	u8 scan_start;
706 	u16 scan_start_failed;
707 };
708 
709 /**
710  * struct leapraid_access_ctrl - Access control structure for LeapRAID
711  *
712  * @pci_access_lock: Mutex for PCI access control.
713  * @shost_recovering: Flag indicating if host is recovering.
714  * @shost_recover_async: Flag indicating if host is recovering
715  *                       and the 0xFFFF event.
716  * @shost_recover_wq: Wait queue for threads waiting on shost_recover_async.
717  * @host_removing: Flag indicating if host is being removed.
718  * @pcie_recovering: Flag indicating if PCIe is recovering.
719  */
720 struct leapraid_access_ctrl {
721 	struct mutex pci_access_lock; /* serializes PCI register access. */
722 	u8 shost_recovering;
723 	wait_queue_head_t recovery_waitq;
724 	u8 shost_recover_async;
725 	wait_queue_head_t shost_recover_wq;
726 	u8 host_removing;
727 	u8 pcie_recovering;
728 };
729 
730 /**
731  * struct leapraid_fw_log_desc - Firmware log descriptor for LeapRAID
732  *
733  * @fw_log_buffer: Buffer for firmware log data.
734  * @fw_log_buffer_dma: DMA address of the firmware log buffer.
735  * @fw_log_wq_name: Name of the firmware log workqueue.
736  * @fw_log_wq: Workqueue for firmware log operations.
737  * @fw_log_work: Delayed work structure for firmware log.
738  * @open_pcie_trace: Flag indicating if PCIe tracing is open.
739  * @fw_log_init_flag: Flag indicating if firmware log is initialized.
740  * @mmap_refcnt: Number of active user VMAs on fw_log_buffer.
741  * @mmap_waitq: Waitqueue for fw_log_buffer VMA teardown.
742  */
743 struct leapraid_fw_log_desc {
744 	u8 *fw_log_buffer;
745 	dma_addr_t fw_log_buffer_dma;
746 	char fw_log_wq_name[48];
747 	struct workqueue_struct *fw_log_wq;
748 	struct delayed_work fw_log_work;
749 	int open_pcie_trace;
750 	int fw_log_init_flag;
751 	atomic_t mmap_refcnt;
752 	wait_queue_head_t mmap_waitq;
753 };
754 
755 #define LEAPRAID_CARD_PORT_FLG_DIRTY	0x01
756 #define LEAPRAID_CARD_PORT_FLG_NEW	0x02
757 #define LEAPRAID_DISABLE_MP_PORT_ID	0xFF
758 /**
759  * struct leapraid_card_port - Card port structure for LeapRAID
760  *
761  * @list: List head for card port.
762  * @vphys_list: List head for virtual PHY list.
763  * @port_id: Port ID.
764  * @sas_address: SAS address.
765  * @phy_mask: Mask of PHY.
766  * @vphys_mask: Mask of virtual PHY.
767  * @flg: Flags for the port.
768  */
769 struct leapraid_card_port {
770 	struct list_head list;
771 	struct list_head vphys_list;
772 	u8 port_id;
773 	u64 sas_address;
774 	u32 phy_mask;
775 	u32 vphys_mask;
776 	u8 flg;
777 };
778 
779 /**
780  * struct leapraid_card_phy - Card PHY structure for LeapRAID
781  *
782  * @port_siblings: List head for port siblings.
783  * @card_port: Pointer to the card port.
784  * @identify: SAS identify structure.
785  * @remote_identify: Remote SAS identify structure.
786  * @phy: SAS PHY structure.
787  * @phy_id: PHY ID.
788  * @hdl: Handle for the port.
789  * @attached_hdl: Handle for the attached port.
790  * @phy_is_assigned: Flag indicating if PHY is assigned.
791  * @vphy: Flag indicating if virtual PHY.
792  */
793 struct leapraid_card_phy {
794 	struct list_head port_siblings;
795 	struct leapraid_card_port *card_port;
796 	struct sas_identify identify;
797 	struct sas_identify remote_identify;
798 	struct sas_phy *phy;
799 	u8 phy_id;
800 	u16 hdl;
801 	u16 attached_hdl;
802 	u8 phy_is_assigned;
803 	u8 vphy;
804 };
805 
806 /**
807  * struct leapraid_topo_node - SAS topology node for LeapRAID
808  *
809  * @list: List head for linking nodes.
810  * @sas_port_list: List of SAS ports.
811  * @card_port: Associated card port.
812  * @card_phy: Associated card PHY.
813  * @rphy: SAS remote PHY device.
814  * @parent_dev: Parent device pointer.
815  * @sas_address: SAS address of this node.
816  * @sas_address_parent: Parent node's SAS address.
817  * @phys_num: Number of physical links.
818  * @hdl: Handle identifier.
819  * @enc_hdl: Enclosure handle.
820  * @enc_lid: Enclosure logical identifier.
821  * @resp: Response status flag.
822  */
823 struct leapraid_topo_node {
824 	struct list_head list;
825 	struct list_head sas_port_list;
826 	struct leapraid_card_port *card_port;
827 	struct leapraid_card_phy *card_phy;
828 	struct sas_rphy *rphy;
829 	struct device *parent_dev;
830 	u64 sas_address;
831 	u64 sas_address_parent;
832 	u8 phys_num;
833 	u16 hdl;
834 	u16 enc_hdl;
835 	u64 enc_lid;
836 	u8 resp;
837 };
838 
839 /**
840  * struct leapraid_dev_topo - LeapRAID device topology management structure
841  *
842  * @topo_node_lock: Spinlock for protecting topology node operations.
843  * @sas_dev_lock: Spinlock for SAS device list access.
844  * @raid_volume_lock: Spinlock for RAID volume list access.
845  * @enc_lock: Spinlock for enclosure device list access.
846  * @sas_id: SAS domain identifier.
847  * @card: Main card topology node.
848  * @exp_list: List of expander devices.
849  * @enc_list: List of enclosure devices.
850  * @sas_dev_list: List of SAS devices.
851  * @sas_dev_init_list: List of SAS devices being initialized.
852  * @raid_volume_list: List of RAID volumes.
853  * @card_port_list: List of card ports.
854  * @pd_hdls: Array of physical disk handles.
855  * @blocking_hdls: Array of blocking handles.
856  */
857 struct leapraid_dev_topo {
858 	spinlock_t topo_node_lock; /* Protects topology node. */
859 	spinlock_t sas_dev_lock; /* Protects SAS device list access. */
860 	spinlock_t raid_volume_lock; /* Protects RAID volume list access. */
861 	spinlock_t enc_lock; /* Protects enclosure device list access. */
862 	int sas_id;
863 	struct leapraid_topo_node card;
864 	struct list_head exp_list;
865 	struct list_head enc_list;
866 	struct list_head sas_dev_list;
867 	struct list_head sas_dev_init_list;
868 	struct list_head raid_volume_list;
869 	struct list_head card_port_list;
870 	u16 pd_hdls_sz;
871 	unsigned long *pd_hdls;
872 	unsigned long *blocking_hdls;
873 };
874 
875 /**
876  * struct leapraid_boot_dev - Boot device structure for LeapRAID
877  *
878  * @dev: Device pointer.
879  * @chnl: Channel number.
880  * @form: Form factor.
881  * @pg_dev: Config page device content.
882  */
883 struct leapraid_boot_dev {
884 	void *dev;
885 	u8 chnl;
886 	u8 form;
887 	u8 pg_dev[24];
888 };
889 
890 /**
891  * struct leapraid_boot_devs - Boot device management structure
892  *
893  * @lock: Spinlock protecting boot device cache access.
894  * @requested_boot_dev: Requested primary boot device.
895  * @requested_alt_boot_dev: Requested alternate boot device.
896  * @current_boot_dev: Currently active boot device.
897  */
898 struct leapraid_boot_devs {
899 	spinlock_t lock; /* protects boot dev */
900 	struct leapraid_boot_dev requested_boot_dev;
901 	struct leapraid_boot_dev requested_alt_boot_dev;
902 	struct leapraid_boot_dev current_boot_dev;
903 };
904 
905 /**
906  * struct leapraid_adapter - Main LeapRAID adapter structure
907  *
908  * @list: List head for adapter management.
909  * @shost: SCSI host structure.
910  * @pdev: PCI device structure.
911  * @iomem_base: I/O memory mapped base address.
912  * @rep_msg_host_idx: Host index for reply messages.
913  * @mask_int: Interrupt masking flag.
914  * @adapter_attr: Adapter attributes.
915  * @mem_desc: Memory descriptor.
916  * @driver_cmds: Driver commands.
917  * @dynamic_task_desc: Dynamic task descriptor.
918  * @fw_evt_s: Firmware event structure.
919  * @notification_desc: Notification descriptor.
920  * @reset_desc: Reset descriptor.
921  * @scan_dev_desc: Device scan descriptor.
922  * @access_ctrl: Access control.
923  * @fw_log_desc: Firmware log descriptor.
924  * @dev_topo: Device topology.
925  * @boot_devs: Boot devices.
926  * @overheat_desc: Overheat processing descriptor.
927  */
928 struct leapraid_adapter {
929 	struct list_head list;
930 	struct Scsi_Host *shost;
931 	struct pci_dev *pdev;
932 	struct leapraid_reg_base __iomem *iomem_base;
933 	u32 rep_msg_host_idx;
934 	u8 mask_int;
935 
936 	struct leapraid_adapter_attr adapter_attr;
937 	struct leapraid_mem_desc mem_desc;
938 	struct leapraid_driver_cmds driver_cmds;
939 	struct leapraid_dynamic_task_desc dynamic_task_desc;
940 	struct leapraid_fw_evt_struct fw_evt_s;
941 	struct leapraid_notification_desc notification_desc;
942 	struct leapraid_reset_desc reset_desc;
943 	struct leapraid_scan_dev_desc scan_dev_desc;
944 	struct leapraid_access_ctrl access_ctrl;
945 	struct leapraid_fw_log_desc fw_log_desc;
946 	struct leapraid_dev_topo dev_topo;
947 	struct leapraid_boot_devs boot_devs;
948 	struct leapraid_overheat_desc overheat_desc;
949 };
950 
951 union cfg_param_1 {
952 	u32 form;
953 	u32 size;
954 	u32 phy_number;
955 };
956 
957 union cfg_param_2 {
958 	u32 handle;
959 	u32 form_specific;
960 };
961 
962 enum config_page_action {
963 	GET_BIOS_PG2,
964 	GET_BIOS_PG3,
965 	GET_MANUFACTURING_PG0,
966 	GET_SAS_DEVICE_PG0,
967 	GET_SAS_IOUNIT_PG0,
968 	GET_SAS_IOUNIT_PG1,
969 	GET_SAS_EXPANDER_PG0,
970 	GET_SAS_EXPANDER_PG1,
971 	GET_SAS_ENCLOSURE_PG0,
972 	GET_PHY_PG0,
973 	GET_RAID_VOLUME_PG0,
974 	GET_RAID_VOLUME_PG1,
975 	GET_PHY_DISK_PG0,
976 };
977 
978 /**
979  * struct leapraid_enc_node - Enclosure node structure
980  *
981  * @list: List head for enclosure management.
982  * @pg0: Enclosure page 0 data.
983  */
984 struct leapraid_enc_node {
985 	struct list_head list;
986 	struct leapraid_enc_p0 pg0;
987 };
988 
989 /**
990  * struct leapraid_raid_volume - RAID volume structure
991  *
992  * @list: List head for volume management.
993  * @refcnt: Reference count.
994  * @starget: SCSI target structure.
995  * @sdev: SCSI device structure.
996  * @id: Volume ID.
997  * @channel: SCSI channel.
998  * @wwid: World wide Identifier.
999  * @hdl: Volume handle.
1000  * @vol_type: Volume type.
1001  * @pd_num: Number of physical disks.
1002  * @resp: Response status.
1003  * @dev_info: Device information.
1004  */
1005 struct leapraid_raid_volume {
1006 	struct list_head list;
1007 	struct kref refcnt;
1008 	struct scsi_target *starget;
1009 	struct scsi_device *sdev;
1010 	unsigned int id;
1011 	unsigned int channel;
1012 	u64 wwid;
1013 	u16 hdl;
1014 	u8 vol_type;
1015 	u8 pd_num;
1016 	u8 resp;
1017 	u32 dev_info;
1018 };
1019 
leapraid_raid_volume_free(struct kref * ref)1020 static inline void leapraid_raid_volume_free(struct kref *ref)
1021 {
1022 	kfree(container_of(ref, struct leapraid_raid_volume, refcnt));
1023 }
1024 
1025 #define leapraid_raid_volume_get(volume) \
1026 	kref_get(&(volume)->refcnt)
1027 #define leapraid_raid_volume_put(volume) \
1028 	kref_put(&(volume)->refcnt, leapraid_raid_volume_free)
1029 
1030 #define LEAPRAID_TGT_FLG_RAID_MEMBER	0x01
1031 #define LEAPRAID_TGT_FLG_VOLUME		0x02
1032 #define LEAPRAID_NO_ULD_ATTACH		1
1033 /**
1034  * struct leapraid_starget_priv - SCSI target private data
1035  *
1036  * @starget: SCSI target structure.
1037  * @sas_address: SAS address.
1038  * @hdl: Device handle.
1039  * @num_luns: Number of LUNs.
1040  * @flg: Flags.
1041  * @deleted: Deletion flag.
1042  * @tm_busy: Task management busy flag.
1043  * @card_port: Associated card port.
1044  * @sas_dev: SAS device structure.
1045  */
1046 struct leapraid_starget_priv {
1047 	struct scsi_target *starget;
1048 	u64 sas_address;
1049 	u16 hdl;
1050 	int num_luns;
1051 	u32 flg;
1052 	u8 deleted;
1053 	u8 tm_busy;
1054 	struct leapraid_card_port *card_port;
1055 	struct leapraid_sas_dev *sas_dev;
1056 };
1057 
1058 #define LEAPRAID_DEVICE_FLG_INIT	0x01
1059 /**
1060  * struct leapraid_sdev_priv - SCSI device private data
1061  *
1062  * @starget_priv: Associated target private data.
1063  * @lun: Logical Unit Number.
1064  * @flg: Flags.
1065  * @ncq_prio_enable: Enables NCQ command priority for RT I/O.
1066  * @block: Block flag.
1067  * @deleted: Deletion flag.
1068  * @sep: SEP flag.
1069  */
1070 struct leapraid_sdev_priv {
1071 	struct leapraid_starget_priv *starget_priv;
1072 	unsigned int lun;
1073 	u32 flg;
1074 	u8 ncq_prio_enable;
1075 	u8 block;
1076 	u8 deleted;
1077 	u8 sep;
1078 };
1079 
1080 /**
1081  * struct leapraid_sas_dev - SAS device structure
1082  *
1083  * @list: List head for device management.
1084  * @starget: SCSI target structure.
1085  * @card_port: Associated card port.
1086  * @rphy: SAS remote PHY.
1087  * @refcnt: Reference count.
1088  * @id: Device ID.
1089  * @channel: SCSI channel.
1090  * @slot: Slot number.
1091  * @phy: PHY identifier.
1092  * @resp: Response status.
1093  * @led_on: LED state.
1094  * @sas_addr: SAS address.
1095  * @dev_name: Device name.
1096  * @hdl: Device handle.
1097  * @parent_sas_addr: Parent SAS address.
1098  * @enc_hdl: Enclosure handle.
1099  * @enc_lid: Enclosure logical ID.
1100  * @volume_hdl: Volume handle.
1101  * @volume_wwid: Volume WWID.
1102  * @dev_info: Device information.
1103  * @pend_sas_rphy_add: Pending SAS remote PHY addition flag.
1104  * @enc_level: Enclosure level.
1105  * @port_connection: Port connection.
1106  * @connector_name: Connector name.
1107  */
1108 struct leapraid_sas_dev {
1109 	struct list_head list;
1110 	struct scsi_target *starget;
1111 	struct leapraid_card_port *card_port;
1112 	struct sas_rphy *rphy;
1113 	struct kref refcnt;
1114 	unsigned int id;
1115 	unsigned int channel;
1116 	u16 slot;
1117 	u8 phy;
1118 	u8 resp;
1119 	u8 led_on;
1120 	u64 sas_addr;
1121 	u64 dev_name;
1122 	u16 hdl;
1123 	u64 parent_sas_addr;
1124 	u16 enc_hdl;
1125 	u64 enc_lid;
1126 	u16 volume_hdl;
1127 	u64 volume_wwid;
1128 	u32 dev_info;
1129 	u8 pend_sas_rphy_add;
1130 	u8 enc_level;
1131 	u8 port_connection;
1132 	u8 connector_name[LEAPRAID_SAS_DEV_P0_CON_NAME_LEN + 1];
1133 };
1134 
leapraid_sdev_free(struct kref * ref)1135 static inline void leapraid_sdev_free(struct kref *ref)
1136 {
1137 	kfree(container_of(ref, struct leapraid_sas_dev, refcnt));
1138 }
1139 
1140 #define leapraid_sdev_get(sdev)	kref_get(&(sdev)->refcnt)
1141 #define leapraid_sdev_put(sdev)	kref_put(&(sdev)->refcnt, leapraid_sdev_free)
1142 
1143 void leapraid_boot_dev_get(void *dev, u32 chnl);
1144 void leapraid_boot_dev_put(void *dev, u32 chnl);
1145 
1146 /**
1147  * struct leapraid_sas_port - SAS port structure
1148  *
1149  * @port_list: List head for port management.
1150  * @phy_list: List of PHYs in this port.
1151  * @port: SAS port structure.
1152  * @card_port: Associated card port.
1153  * @remote_identify: Remote device identification.
1154  * @rphy: SAS remote PHY.
1155  * @phys_num: Number of PHYs in this port.
1156  */
1157 struct leapraid_sas_port {
1158 	struct list_head port_list;
1159 	struct list_head phy_list;
1160 	struct sas_port *port;
1161 	struct leapraid_card_port *card_port;
1162 	struct sas_identify remote_identify;
1163 	struct sas_rphy *rphy;
1164 	u8 phys_num;
1165 };
1166 
1167 #define LEAPRAID_VPHY_FLG_DIRTY	0x01
1168 /**
1169  * struct leapraid_vphy - Virtual PHY structure
1170  *
1171  * @list: List head for PHY management.
1172  * @sas_address: SAS address.
1173  * @phy_mask: PHY mask.
1174  * @flg: Flags.
1175  */
1176 struct leapraid_vphy {
1177 	struct list_head list;
1178 	u64 sas_address;
1179 	u32 phy_mask;
1180 	u8 flg;
1181 };
1182 
1183 /**
1184  * struct leapraid_tgt_rst_list - Target reset tracking entry
1185  * @list: List node used to link reset entries.
1186  * @handle: Device handle of the target being reset.
1187  * @state: Current state of the target reset operation.
1188  *
1189  * Each entry represents a target device that is undergoing or has
1190  * undergone a target reset operation.
1191  */
1192 struct leapraid_tgt_rst_list {
1193 	struct list_head list;
1194 	u16 handle;
1195 	u16 state;
1196 };
1197 
1198 /**
1199  * struct sense_info - Parsed SCSI sense information
1200  * @sense_key: Sense key extracted from sense data.
1201  * @asc: Additional Sense Code (ASC).
1202  * @ascq: Additional Sense Code Qualifier (ASCQ).
1203  *
1204  * This structure stores key fields parsed from SCSI sense data to
1205  * simplify error handling and reporting.
1206  */
1207 struct sense_info {
1208 	u8 sense_key;
1209 	u8 asc;
1210 	u8 ascq;
1211 };
1212 
1213 /**
1214  * struct leapraid_fw_log_info - Firmware log position information
1215  * @user_position: Current log read position used by the driver.
1216  * @adapter_position: Current log write position maintained by firmware.
1217  *
1218  * This structure tracks firmware log buffer positions used for retrieving
1219  * log entries from the adapter.
1220  */
1221 struct leapraid_fw_log_info {
1222 	u32 user_position;
1223 	u32 adapter_position;
1224 };
1225 
1226 /**
1227  * enum reset_type - Reset type enumeration
1228  *
1229  * @FULL_RESET: Full hardware reset.
1230  * @PART_RESET: Partial reset.
1231  */
1232 enum reset_type {
1233 	FULL_RESET,
1234 	PART_RESET,
1235 };
1236 
1237 enum leapraid_card_port_checking_flg {
1238 	CARD_PORT_FURTHER_CHECKING_NEEDED = 0,
1239 	CARD_PORT_SKIP_CHECKING,
1240 };
1241 
1242 enum leapraid_port_checking_state {
1243 	NEW_CARD_PORT = 0,
1244 	SAME_PORT_WITH_NOTHING_CHANGED,
1245 	SAME_PORT_WITH_PARTIALLY_CHANGED_PHYS,
1246 	SAME_ADDR_WITH_PARTIALLY_CHANGED_PHYS,
1247 	SAME_ADDR_ONLY,
1248 };
1249 
1250 /**
1251  * struct leapraid_card_port_feature - Card port feature
1252  *
1253  * @dirty_flg: Dirty flag indicator.
1254  * @same_addr: Same address flag.
1255  * @exact_phy: Exact PHY match flag.
1256  * @phy_overlap: PHY overlap bitmap.
1257  * @same_port: Same port flag.
1258  * @cur_chking_old_port: Current checking old port.
1259  * @expected_old_port: Expected old port.
1260  * @same_addr_port_count: Same address port count.
1261  * @checking_state: Port checking state.
1262  */
1263 struct leapraid_card_port_feature {
1264 	u8 dirty_flg;
1265 	u8 same_addr;
1266 	u8 exact_phy;
1267 	u32 phy_overlap;
1268 	u8 same_port;
1269 	struct leapraid_card_port *cur_chking_old_port;
1270 	struct leapraid_card_port *expected_old_port;
1271 	int same_addr_port_count;
1272 	enum leapraid_port_checking_state checking_state;
1273 };
1274 
1275 #define SMP_REPORT_MANUFACTURER_INFORMATION_FRAME_TYPE	0x40
1276 #define SMP_REPORT_MANUFACTURER_INFORMATION_FUNC	0x01
1277 
1278 /**
1279  * ref: SAS-2(INCITS 457-2010) 10.4.3.5
1280  */
1281 struct leapraid_rep_manu_request {
1282 	u8 smp_frame_type;
1283 	u8 function;
1284 	u8 allocated_response_length;
1285 	u8 request_length;
1286 };
1287 
1288 /**
1289  * ref: SAS-2(INCITS 457-2010) 10.4.3.5
1290  */
1291 struct leapraid_rep_manu_reply {
1292 	u8 smp_frame_type;
1293 	u8 function;
1294 	u8 function_result;
1295 	u8 response_length;
1296 	u16 expander_change_count;
1297 	u8 r1[2];
1298 	u8 sas_format;
1299 	u8 r2[3];
1300 	u8 vendor_identification[SAS_EXPANDER_VENDOR_ID_LEN];
1301 	u8 product_identification[SAS_EXPANDER_PRODUCT_ID_LEN];
1302 	u8 product_revision_level[SAS_EXPANDER_PRODUCT_REV_LEN];
1303 	u8 comp_vendor_identification[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
1304 	u16 component_id;
1305 	u8 component_revision_level;
1306 	u8 r3;
1307 	u8 vendor_specific[8];
1308 };
1309 
1310 extern struct list_head leapraid_adapter_list;
1311 extern spinlock_t leapraid_adapter_lock;
1312 extern char driver_name[LEAPRAID_NAME_LENGTH];
1313 
1314 int leapraid_ctrl_init(struct leapraid_adapter *adapter);
1315 void leapraid_remove_ctrl(struct leapraid_adapter *adapter);
1316 void leapraid_check_scheduled_fault_start(struct leapraid_adapter *adapter);
1317 void leapraid_check_scheduled_fault_stop(struct leapraid_adapter *adapter);
1318 void leapraid_fw_log_start(struct leapraid_adapter *adapter);
1319 void leapraid_fw_log_stop(struct leapraid_adapter *adapter);
1320 int leapraid_set_pcie_and_notification(struct leapraid_adapter *adapter);
1321 void leapraid_disable_controller(struct leapraid_adapter *adapter);
1322 int leapraid_hard_reset_handler(struct leapraid_adapter *adapter,
1323 				enum reset_type type);
1324 void leapraid_mask_int(struct leapraid_adapter *adapter);
1325 void leapraid_unmask_int(struct leapraid_adapter *adapter);
1326 u32 leapraid_get_adapter_state(struct leapraid_adapter *adapter);
1327 bool leapraid_pci_removed(struct leapraid_adapter *adapter);
1328 int leapraid_check_adapter_is_op(struct leapraid_adapter *adapter, int wait,
1329 				 const char *caller);
1330 void *leapraid_get_task_desc(struct leapraid_adapter *adapter, u16 taskid);
1331 void *leapraid_get_sense_buffer(struct leapraid_adapter *adapter, u16 taskid);
1332 __le32 leapraid_get_sense_buffer_dma(struct leapraid_adapter *adapter,
1333 				     u16 taskid);
1334 void *leapraid_get_reply_vaddr(struct leapraid_adapter *adapter,
1335 			       u32 phys_addr);
1336 u16 leapraid_alloc_scsiio_taskid(struct leapraid_adapter *adapter,
1337 				 struct scsi_cmnd *scmd);
1338 void leapraid_free_taskid(struct leapraid_adapter *adapter, u16 taskid);
1339 struct leapraid_io_req_tracker *leapraid_get_io_tracker_from_taskid(
1340 		struct leapraid_adapter *adapter, u16 taskid);
1341 struct scsi_cmnd *leapraid_get_scmd_from_taskid(
1342 		struct leapraid_adapter *adapter, u16 taskid);
1343 int leapraid_scan_dev(struct leapraid_adapter *adapter, bool async_scan_dev);
1344 void leapraid_scan_dev_done(struct leapraid_adapter *adapter);
1345 void leapraid_cleanup_lists(struct leapraid_adapter *adapter);
1346 void leapraid_wait_cmds_done(struct leapraid_adapter *adapter);
1347 void leapraid_clean_active_scsi_cmds(struct leapraid_adapter *adapter);
1348 void leapraid_sync_irqs(struct leapraid_adapter *adapter, bool poll);
1349 int leapraid_rep_queue_handler(struct leapraid_rq *rq);
1350 int leapraid_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num);
1351 void leapraid_mq_polling_pause(struct leapraid_adapter *adapter);
1352 void leapraid_mq_polling_resume(struct leapraid_adapter *adapter);
1353 void leapraid_set_tm_flg(struct leapraid_adapter *adapter, u16 handle);
1354 void leapraid_clear_tm_flg(struct leapraid_adapter *adapter, u16 handle);
1355 void leapraid_async_turn_on_led(struct leapraid_adapter *adapter, u16 handle);
1356 int leapraid_issue_locked_tm(struct leapraid_adapter *adapter, u16 handle,
1357 			     uint channel, uint id, uint lun, u8 type,
1358 			     u16 taskid_task, u8 tr_method);
1359 int leapraid_issue_tm(struct leapraid_adapter *adapter, u16 handle,
1360 		      uint channel, uint id, uint lun, u8 type,
1361 		      u16 taskid_task, u8 tr_method);
1362 bool leapraid_scsiio_done(struct leapraid_adapter *adapter, u16 taskid,
1363 			  u8 msix_index, u32 rep);
1364 int leapraid_get_volume_cap(struct leapraid_adapter *adapter,
1365 			    struct leapraid_raid_volume *raid_volume);
1366 int leapraid_internal_init_cmd_priv(
1367 		struct leapraid_adapter *adapter,
1368 		struct leapraid_io_req_tracker *io_tracker);
1369 void leapraid_internal_exit_cmd_priv(
1370 		struct leapraid_adapter *adapter,
1371 		struct leapraid_io_req_tracker *io_tracker);
1372 void leapraid_clean_active_fw_evt(struct leapraid_adapter *adapter);
1373 bool leapraid_scmd_find_by_lun(struct leapraid_adapter *adapter,
1374 			       uint id, unsigned int lun, uint channel);
1375 bool leapraid_scmd_find_by_tgt(struct leapraid_adapter *adapter,
1376 			       uint id, uint channel);
1377 struct leapraid_vphy *leapraid_get_vphy_by_phy(struct leapraid_card_port *port,
1378 					       u32 phy);
1379 struct leapraid_raid_volume *leapraid_raid_volume_find_by_id(
1380 		struct leapraid_adapter *adapter, uint id, uint channel);
1381 struct leapraid_raid_volume *leapraid_raid_volume_find_by_hdl(
1382 		struct leapraid_adapter *adapter, u16 handle);
1383 struct leapraid_topo_node *leapraid_exp_find_by_sas_address(
1384 		struct leapraid_adapter *adapter, u64 sas_address,
1385 		struct leapraid_card_port *port);
1386 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_by_addr_and_rphy(
1387 		struct leapraid_adapter *adapter,
1388 		u64 sas_address, struct sas_rphy *rphy);
1389 struct leapraid_sas_dev *leapraid_get_sas_dev_by_addr(
1390 		struct leapraid_adapter *adapter, u64 sas_address,
1391 		struct leapraid_card_port *port);
1392 struct leapraid_sas_dev *leapraid_get_sas_dev_by_hdl(
1393 		struct leapraid_adapter *adapter, u16 handle);
1394 struct leapraid_sas_dev *leapraid_get_sas_dev_from_tgt(
1395 		struct leapraid_adapter *adapter,
1396 		struct leapraid_starget_priv *tgt_priv);
1397 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_from_tgt(
1398 		struct leapraid_adapter *adapter,
1399 		 struct leapraid_starget_priv *tgt_priv);
1400 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_by_hdl(
1401 		struct leapraid_adapter *adapter, u16 handle);
1402 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_by_addr(
1403 		struct leapraid_adapter *adapter, u64 sas_address,
1404 		struct leapraid_card_port *port);
1405 struct leapraid_sas_dev *leapraid_get_next_sas_dev_from_init_list(
1406 		struct leapraid_adapter *adapter);
1407 void leapraid_sas_dev_remove_by_sas_address(
1408 		struct leapraid_adapter *adapter,
1409 		u64 sas_address, struct leapraid_card_port *port);
1410 void leapraid_sas_dev_remove(struct leapraid_adapter *adapter,
1411 			     struct leapraid_sas_dev *sas_dev);
1412 void leapraid_raid_volume_remove(struct leapraid_adapter *adapter,
1413 				 struct leapraid_raid_volume *raid_volume);
1414 void leapraid_exp_rm(struct leapraid_adapter *adapter,
1415 		     u64 sas_address, struct leapraid_card_port *port);
1416 void leapraid_build_mpi_sg(struct leapraid_adapter *adapter,
1417 			   void *sge, dma_addr_t h2c_dma_addr, size_t h2c_size,
1418 			   dma_addr_t c2h_dma_addr, size_t c2h_size);
1419 void leapraid_build_ieee_nodata_sg(struct leapraid_adapter *adapter,
1420 				   void *sge);
1421 void leapraid_build_ieee_sg(struct leapraid_adapter *adapter,
1422 			    void *psge, dma_addr_t h2c_dma_addr,
1423 			    size_t h2c_size, dma_addr_t c2h_dma_addr,
1424 			    size_t c2h_size);
1425 int leapraid_build_scmd_ieee_sg(struct leapraid_adapter *adapter,
1426 				struct scsi_cmnd *scmd, u16 taskid);
1427 void leapraid_fire_scsi_io(struct leapraid_adapter *adapter,
1428 			   u16 taskid, u16 handle);
1429 void leapraid_fire_hpr_task(struct leapraid_adapter *adapter, u16 taskid,
1430 			    u16 msix_task);
1431 void leapraid_fire_task(struct leapraid_adapter *adapter, u16 taskid);
1432 int leapraid_cfg_get_volume_hdl(struct leapraid_adapter *adapter,
1433 				u16 pd_handle, u16 *volume_handle);
1434 int leapraid_cfg_get_volume_wwid(struct leapraid_adapter *adapter,
1435 				 u16 volume_handle, u64 *wwid);
1436 int leapraid_op_config_page(struct leapraid_adapter *adapter,
1437 			    void *cfgp, union cfg_param_1 cfgp1,
1438 			    union cfg_param_2 cfgp2,
1439 			    enum config_page_action cfg_op);
1440 void leapraid_log_req_context(struct leapraid_adapter *adapter, u16 smid,
1441 			      const void *req_data);
1442 
1443 int leapraid_change_queue_depth(struct scsi_device *sdev, int qdepth);
1444 
1445 int leapraid_ctl_release(struct inode *inode, struct file *filep);
1446 int leapraid_ctl_init(void);
1447 void leapraid_ctl_exit(void);
1448 
1449 extern struct sas_function_template leapraid_transport_functions;
1450 extern struct scsi_transport_template *leapraid_transport_template;
1451 struct leapraid_sas_port *leapraid_transport_port_add(
1452 		struct leapraid_adapter *adapter, u16 handle, u64 sas_address,
1453 		struct leapraid_card_port *card_port);
1454 void leapraid_transport_port_remove(struct leapraid_adapter *adapter,
1455 				    u64 sas_address, u64 sas_address_parent,
1456 				    struct leapraid_card_port *card_port);
1457 void leapraid_transport_add_card_phy(struct leapraid_adapter *adapter,
1458 				     struct leapraid_card_phy *card_phy,
1459 				     struct leapraid_sas_phy_p0 *phy_pg0,
1460 				     struct device *parent_dev);
1461 int leapraid_transport_add_exp_phy(struct leapraid_adapter *adapter,
1462 				   struct leapraid_card_phy *card_phy,
1463 				   struct leapraid_exp_p1 *exp_pg1,
1464 				   struct device *parent_dev);
1465 void leapraid_transport_update_links(struct leapraid_adapter *adapter,
1466 				     u64 sas_address, u16 handle,
1467 				     u8 phy_number, u8 link_rate,
1468 				     struct leapraid_card_port *card_port);
1469 void leapraid_transport_detach_phy_to_port(
1470 		struct leapraid_adapter *adapter,
1471 		struct leapraid_topo_node *topo_node,
1472 		struct leapraid_card_phy *card_phy);
1473 void leapraid_transport_attach_phy_to_port(
1474 		struct leapraid_adapter *adapter,
1475 		struct leapraid_topo_node *sas_node,
1476 		struct leapraid_card_phy *card_phy,
1477 		u64 sas_address,
1478 		struct leapraid_card_port *card_port);
1479 enum scsi_qc_status leapraid_queuecommand(struct Scsi_Host *shost,
1480 					  struct scsi_cmnd *scmd);
1481 void leapraid_overheat_cleanup(struct leapraid_adapter *adapter);
1482 void leapraid_smart_fault_detect(struct leapraid_adapter *adapter, u16 hdl);
1483 
1484 #endif /* LEAPRAID_FUNC_H_INCLUDED */
1485