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