xref: /linux/drivers/scsi/leapraid/leapraid_func.h (revision 08dbfad3f5040f5bdb6c529da20d6d4e81fefd72)
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_name: Name of the firmware event.
558  * @fw_evt_thread: Workqueue used for processing firmware events.
559  * @fw_evt_lock: Spinlock protecting access to the firmware event list.
560  * @fw_evt_list: Linked list of pending firmware events.
561  * @cur_evt: Pointer to the currently processing firmware event.
562  * @cur_evt_task: Task currently executing @cur_evt work.
563  * @fw_evt_cleanup: Flag indicating whether cleanup of events is in progress.
564  * @leapraid_evt_masks: Array of event masks for filtering firmware events.
565  */
566 struct leapraid_fw_evt_struct {
567 	u32 leapraid_evt_masks[4];
568 	char fw_evt_name[48];
569 	struct workqueue_struct *fw_evt_thread;
570 	spinlock_t fw_evt_lock; /* protects firmware event */
571 	struct list_head fw_evt_list;
572 	struct leapraid_fw_evt_work *cur_evt;
573 	struct task_struct *cur_evt_task;
574 	u8 fw_evt_cleanup;
575 };
576 
577 /**
578  * struct leapraid_rq - Represents a LeapRAID request queue
579  *
580  * @adapter: Pointer to the associated LeapRAID adapter.
581  * @msix_idx: MSI-X vector index used by this queue.
582  * @rep_post_host_idx: Index of the last processed reply descriptor.
583  * @rep_desc: Pointer to the reply descriptor associated with this queue.
584  * @name: Name of the request queue.
585  * @busy: Atomic counter indicating if the queue is busy.
586  */
587 struct leapraid_rq {
588 	struct leapraid_adapter *adapter;
589 	u8 msix_idx;
590 	u32 rep_post_host_idx;
591 	union leapraid_rep_desc_union *rep_desc;
592 	char name[LEAPRAID_NAME_LENGTH];
593 	atomic_t busy;
594 };
595 
596 /**
597  * struct leapraid_int_rq - Internal request queue for a CPU
598  *
599  * @affinity_hint: CPU affinity mask for the queue.
600  * @rq: Underlying LeapRAID request queue structure.
601  */
602 struct leapraid_int_rq {
603 	cpumask_var_t affinity_hint;
604 	struct leapraid_rq rq;
605 };
606 
607 /**
608  * struct leapraid_blk_mq_poll_rq - Polling request for LeapRAID blk-mq
609  *
610  * @busy: Atomic flag indicating request is being processed.
611  * @pause: Atomic flag to temporarily suspend polling.
612  * @rq: The underlying LeapRAID request structure.
613  */
614 struct leapraid_blk_mq_poll_rq {
615 	atomic_t busy;
616 	atomic_t pause;
617 	struct leapraid_rq rq;
618 };
619 
620 /**
621  * struct leapraid_notification_desc - Notification descriptor for LeapRAID
622  *
623  * @iopoll_qdex: Index of the I/O polling queue.
624  * @iopoll_qcnt: Count of I/O polling queues.
625  * @msix_enable: Flag indicating MSI-X is enabled.
626  * @irq_vectors_allocated: Flag indicating PCI IRQ vectors are allocated.
627  * @irq_mode: Actual interrupt mode used for allocated PCI IRQ vectors.
628  * @msix_cpu_map: CPU-ID indexed MSI-X queue map.
629  * @msix_cpu_map_sz: Number of CPU-ID slots allocated in @msix_cpu_map.
630  * @int_rqs: Array of interrupt request queues.
631  * @int_rqs_allocated: Count of allocated interrupt request queues.
632  * @blk_mq_poll_rqs: Array of blk-mq polling requests.
633  */
634 struct leapraid_notification_desc {
635 	u32 iopoll_qdex;
636 	u32 iopoll_qcnt;
637 	u8 msix_enable;
638 	u8 irq_vectors_allocated;
639 	u8 irq_mode;
640 	u8 *msix_cpu_map;
641 	u32 msix_cpu_map_sz;
642 	struct leapraid_int_rq *int_rqs;
643 	u32 int_rqs_allocated;
644 	struct leapraid_blk_mq_poll_rq *blk_mq_poll_rqs;
645 };
646 
647 /**
648  * struct leapraid_overheat_desc - Overheat descriptor for LeapRAID
649  *
650  * @fault_overheat_wq: Workqueue for adapter thermal overheat operations.
651  * @fault_overheat_work: Work structure for thermal overheat.
652  * @thermal_alert: Flag indicating if adapter thermal alert is active.
653  * @fault_overheat_wq_name: Name of the fault overheat workqueue.
654  */
655 struct leapraid_overheat_desc {
656 	struct workqueue_struct *fault_overheat_wq;
657 	struct work_struct fault_overheat_work;
658 	atomic_t thermal_alert;
659 	char fault_overheat_wq_name[48];
660 };
661 
662 /**
663  * struct leapraid_reset_desc - Reset descriptor for LeapRAID
664  *
665  * @fault_reset_wq: Workqueue for fault reset operations.
666  * @fault_reset_work: Delayed work structure for fault reset.
667  * @fault_reset_wq_name: Name of the fault reset workqueue.
668  * @host_diag_mutex: Mutex for host diagnostic operations.
669  * @adapter_reset_lock: Spinlock for adapter reset operations.
670  * @adapter_reset_mutex: Mutex for adapter reset operations.
671  * @adapter_link_resetting: Flag indicating if adapter link is resetting.
672  * @adapter_reset_results: Results of the adapter reset operation.
673  * @pending_io_cnt: Count of pending I/O operations.
674  * @reset_wait_queue: Wait queue for reset operations.
675  * @reset_cnt: Counter for reset operations.
676  */
677 struct leapraid_reset_desc {
678 	struct workqueue_struct *fault_reset_wq;
679 	struct delayed_work fault_reset_work;
680 	char fault_reset_wq_name[48];
681 	struct mutex host_diag_mutex; /* Mutex for operations. */
682 	spinlock_t adapter_reset_lock; /* Protects adapter reset state. */
683 	struct mutex adapter_reset_mutex; /* Serializes adapter reset. */
684 	u8 adapter_link_resetting;
685 	int adapter_reset_results;
686 	int pending_io_cnt;
687 	wait_queue_head_t reset_wait_queue;
688 	u32 reset_cnt;
689 };
690 
691 /**
692  * struct leapraid_scan_dev_desc - Scan device descriptor for LeapRAID
693  *
694  * @wait_scan_dev_done: Flag indicating if scan device operation is done.
695  * @driver_loading: Flag indicating if driver is loading.
696  * @first_scan_dev_fired: Flag indicating if first scan device operation fired.
697  * @scan_dev_failed: Flag indicating if scan device operation failed.
698  * @scan_start: Flag indicating if scan operation started.
699  * @scan_start_failed: Count of failed scan start operations.
700  */
701 struct leapraid_scan_dev_desc {
702 	u8 wait_scan_dev_done;
703 	u8 driver_loading;
704 	wait_queue_head_t wait_driver_loading;
705 	u8 first_scan_dev_fired;
706 	u8 scan_dev_failed;
707 	u8 scan_start;
708 	u16 scan_start_failed;
709 };
710 
711 /**
712  * struct leapraid_access_ctrl - Access control structure for LeapRAID
713  *
714  * @pci_access_lock: Mutex for PCI access control.
715  * @shost_recovering: Flag indicating if host is recovering.
716  * @shost_recover_async: Flag indicating if host is recovering
717  *                       and the 0xFFFF event.
718  * @shost_recover_wq: Wait queue for threads waiting on shost_recover_async.
719  * @host_removing: Flag indicating if host is being removed.
720  * @pcie_recovering: Flag indicating if PCIe is recovering.
721  */
722 struct leapraid_access_ctrl {
723 	struct mutex pci_access_lock; /* serializes PCI register access. */
724 	u8 shost_recovering;
725 	wait_queue_head_t recovery_waitq;
726 	u8 shost_recover_async;
727 	wait_queue_head_t shost_recover_wq;
728 	u8 host_removing;
729 	u8 pcie_recovering;
730 };
731 
732 /**
733  * struct leapraid_fw_log_desc - Firmware log descriptor for LeapRAID
734  *
735  * @fw_log_buffer: Buffer for firmware log data.
736  * @fw_log_buffer_dma: DMA address of the firmware log buffer.
737  * @fw_log_wq_name: Name of the firmware log workqueue.
738  * @fw_log_wq: Workqueue for firmware log operations.
739  * @fw_log_work: Delayed work structure for firmware log.
740  * @open_pcie_trace: Flag indicating if PCIe tracing is open.
741  * @fw_log_init_flag: Flag indicating if firmware log is initialized.
742  * @mmap_refcnt: Number of active user VMAs on fw_log_buffer.
743  * @mmap_waitq: Waitqueue for fw_log_buffer VMA teardown.
744  */
745 struct leapraid_fw_log_desc {
746 	u8 *fw_log_buffer;
747 	dma_addr_t fw_log_buffer_dma;
748 	char fw_log_wq_name[48];
749 	struct workqueue_struct *fw_log_wq;
750 	struct delayed_work fw_log_work;
751 	int open_pcie_trace;
752 	int fw_log_init_flag;
753 	atomic_t mmap_refcnt;
754 	wait_queue_head_t mmap_waitq;
755 };
756 
757 #define LEAPRAID_CARD_PORT_FLG_DIRTY	0x01
758 #define LEAPRAID_CARD_PORT_FLG_NEW	0x02
759 #define LEAPRAID_DISABLE_MP_PORT_ID	0xFF
760 /**
761  * struct leapraid_card_port - Card port structure for LeapRAID
762  *
763  * @list: List head for card port.
764  * @vphys_list: List head for virtual PHY list.
765  * @port_id: Port ID.
766  * @sas_address: SAS address.
767  * @phy_mask: Mask of PHY.
768  * @vphys_mask: Mask of virtual PHY.
769  * @flg: Flags for the port.
770  */
771 struct leapraid_card_port {
772 	struct list_head list;
773 	struct list_head vphys_list;
774 	u8 port_id;
775 	u64 sas_address;
776 	u32 phy_mask;
777 	u32 vphys_mask;
778 	u8 flg;
779 };
780 
781 /**
782  * struct leapraid_card_phy - Card PHY structure for LeapRAID
783  *
784  * @port_siblings: List head for port siblings.
785  * @card_port: Pointer to the card port.
786  * @identify: SAS identify structure.
787  * @remote_identify: Remote SAS identify structure.
788  * @phy: SAS PHY structure.
789  * @phy_id: PHY ID.
790  * @hdl: Handle for the port.
791  * @attached_hdl: Handle for the attached port.
792  * @phy_is_assigned: Flag indicating if PHY is assigned.
793  * @vphy: Flag indicating if virtual PHY.
794  */
795 struct leapraid_card_phy {
796 	struct list_head port_siblings;
797 	struct leapraid_card_port *card_port;
798 	struct sas_identify identify;
799 	struct sas_identify remote_identify;
800 	struct sas_phy *phy;
801 	u8 phy_id;
802 	u16 hdl;
803 	u16 attached_hdl;
804 	u8 phy_is_assigned;
805 	u8 vphy;
806 };
807 
808 /**
809  * struct leapraid_topo_node - SAS topology node for LeapRAID
810  *
811  * @list: List head for linking nodes.
812  * @sas_port_list: List of SAS ports.
813  * @card_port: Associated card port.
814  * @card_phy: Associated card PHY.
815  * @rphy: SAS remote PHY device.
816  * @parent_dev: Parent device pointer.
817  * @sas_address: SAS address of this node.
818  * @sas_address_parent: Parent node's SAS address.
819  * @phys_num: Number of physical links.
820  * @hdl: Handle identifier.
821  * @enc_hdl: Enclosure handle.
822  * @enc_lid: Enclosure logical identifier.
823  * @resp: Response status flag.
824  */
825 struct leapraid_topo_node {
826 	struct list_head list;
827 	struct list_head sas_port_list;
828 	struct leapraid_card_port *card_port;
829 	struct leapraid_card_phy *card_phy;
830 	struct sas_rphy *rphy;
831 	struct device *parent_dev;
832 	u64 sas_address;
833 	u64 sas_address_parent;
834 	u8 phys_num;
835 	u16 hdl;
836 	u16 enc_hdl;
837 	u64 enc_lid;
838 	u8 resp;
839 };
840 
841 /**
842  * struct leapraid_dev_topo - LeapRAID device topology management structure
843  *
844  * @topo_node_lock: Spinlock for protecting topology node operations.
845  * @sas_dev_lock: Spinlock for SAS device list access.
846  * @raid_volume_lock: Spinlock for RAID volume list access.
847  * @enc_lock: Spinlock for enclosure device list access.
848  * @sas_id: SAS domain identifier.
849  * @card: Main card topology node.
850  * @exp_list: List of expander devices.
851  * @enc_list: List of enclosure devices.
852  * @sas_dev_list: List of SAS devices.
853  * @sas_dev_init_list: List of SAS devices being initialized.
854  * @raid_volume_list: List of RAID volumes.
855  * @card_port_list: List of card ports.
856  * @pd_hdls: Array of physical disk handles.
857  * @blocking_hdls: Array of blocking handles.
858  */
859 struct leapraid_dev_topo {
860 	spinlock_t topo_node_lock; /* Protects topology node. */
861 	spinlock_t sas_dev_lock; /* Protects SAS device list access. */
862 	spinlock_t raid_volume_lock; /* Protects RAID volume list access. */
863 	spinlock_t enc_lock; /* Protects enclosure device list access. */
864 	int sas_id;
865 	struct leapraid_topo_node card;
866 	struct list_head exp_list;
867 	struct list_head enc_list;
868 	struct list_head sas_dev_list;
869 	struct list_head sas_dev_init_list;
870 	struct list_head raid_volume_list;
871 	struct list_head card_port_list;
872 	u16 pd_hdls_sz;
873 	unsigned long *pd_hdls;
874 	unsigned long *blocking_hdls;
875 };
876 
877 /**
878  * struct leapraid_boot_dev - Boot device structure for LeapRAID
879  *
880  * @dev: Device pointer.
881  * @chnl: Channel number.
882  * @form: Form factor.
883  * @pg_dev: Config page device content.
884  */
885 struct leapraid_boot_dev {
886 	void *dev;
887 	u8 chnl;
888 	u8 form;
889 	u8 pg_dev[24];
890 };
891 
892 /**
893  * struct leapraid_boot_devs - Boot device management structure
894  *
895  * @lock: Spinlock protecting boot device cache access.
896  * @requested_boot_dev: Requested primary boot device.
897  * @requested_alt_boot_dev: Requested alternate boot device.
898  * @current_boot_dev: Currently active boot device.
899  */
900 struct leapraid_boot_devs {
901 	spinlock_t lock; /* protects boot dev */
902 	struct leapraid_boot_dev requested_boot_dev;
903 	struct leapraid_boot_dev requested_alt_boot_dev;
904 	struct leapraid_boot_dev current_boot_dev;
905 };
906 
907 /**
908  * struct leapraid_adapter - Main LeapRAID adapter structure
909  *
910  * @list: List head for adapter management.
911  * @shost: SCSI host structure.
912  * @pdev: PCI device structure.
913  * @iomem_base: I/O memory mapped base address.
914  * @rep_msg_host_idx: Host index for reply messages.
915  * @mask_int: Interrupt masking flag.
916  * @adapter_attr: Adapter attributes.
917  * @mem_desc: Memory descriptor.
918  * @driver_cmds: Driver commands.
919  * @dynamic_task_desc: Dynamic task descriptor.
920  * @fw_evt_s: Firmware event structure.
921  * @notification_desc: Notification descriptor.
922  * @reset_desc: Reset descriptor.
923  * @scan_dev_desc: Device scan descriptor.
924  * @access_ctrl: Access control.
925  * @fw_log_desc: Firmware log descriptor.
926  * @dev_topo: Device topology.
927  * @boot_devs: Boot devices.
928  * @overheat_desc: Overheat processing descriptor.
929  */
930 struct leapraid_adapter {
931 	struct list_head list;
932 	struct Scsi_Host *shost;
933 	struct pci_dev *pdev;
934 	struct leapraid_reg_base __iomem *iomem_base;
935 	u32 rep_msg_host_idx;
936 	u8 mask_int;
937 
938 	struct leapraid_adapter_attr adapter_attr;
939 	struct leapraid_mem_desc mem_desc;
940 	struct leapraid_driver_cmds driver_cmds;
941 	struct leapraid_dynamic_task_desc dynamic_task_desc;
942 	struct leapraid_fw_evt_struct fw_evt_s;
943 	struct leapraid_notification_desc notification_desc;
944 	struct leapraid_reset_desc reset_desc;
945 	struct leapraid_scan_dev_desc scan_dev_desc;
946 	struct leapraid_access_ctrl access_ctrl;
947 	struct leapraid_fw_log_desc fw_log_desc;
948 	struct leapraid_dev_topo dev_topo;
949 	struct leapraid_boot_devs boot_devs;
950 	struct leapraid_overheat_desc overheat_desc;
951 };
952 
953 union cfg_param_1 {
954 	u32 form;
955 	u32 size;
956 	u32 phy_number;
957 };
958 
959 union cfg_param_2 {
960 	u32 handle;
961 	u32 form_specific;
962 };
963 
964 enum config_page_action {
965 	GET_BIOS_PG2,
966 	GET_BIOS_PG3,
967 	GET_MANUFACTURING_PG0,
968 	GET_SAS_DEVICE_PG0,
969 	GET_SAS_IOUNIT_PG0,
970 	GET_SAS_IOUNIT_PG1,
971 	GET_SAS_EXPANDER_PG0,
972 	GET_SAS_EXPANDER_PG1,
973 	GET_SAS_ENCLOSURE_PG0,
974 	GET_PHY_PG0,
975 	GET_RAID_VOLUME_PG0,
976 	GET_RAID_VOLUME_PG1,
977 	GET_PHY_DISK_PG0,
978 };
979 
980 /**
981  * struct leapraid_enc_node - Enclosure node structure
982  *
983  * @list: List head for enclosure management.
984  * @pg0: Enclosure page 0 data.
985  */
986 struct leapraid_enc_node {
987 	struct list_head list;
988 	struct leapraid_enc_p0 pg0;
989 };
990 
991 /**
992  * struct leapraid_raid_volume - RAID volume structure
993  *
994  * @list: List head for volume management.
995  * @refcnt: Reference count.
996  * @starget: SCSI target structure.
997  * @sdev: SCSI device structure.
998  * @id: Volume ID.
999  * @channel: SCSI channel.
1000  * @wwid: World wide Identifier.
1001  * @hdl: Volume handle.
1002  * @vol_type: Volume type.
1003  * @pd_num: Number of physical disks.
1004  * @resp: Response status.
1005  * @dev_info: Device information.
1006  */
1007 struct leapraid_raid_volume {
1008 	struct list_head list;
1009 	struct kref refcnt;
1010 	struct scsi_target *starget;
1011 	struct scsi_device *sdev;
1012 	unsigned int id;
1013 	unsigned int channel;
1014 	u64 wwid;
1015 	u16 hdl;
1016 	u8 vol_type;
1017 	u8 pd_num;
1018 	u8 resp;
1019 	u32 dev_info;
1020 };
1021 
leapraid_raid_volume_free(struct kref * ref)1022 static inline void leapraid_raid_volume_free(struct kref *ref)
1023 {
1024 	kfree(container_of(ref, struct leapraid_raid_volume, refcnt));
1025 }
1026 
1027 #define leapraid_raid_volume_get(volume) \
1028 	kref_get(&(volume)->refcnt)
1029 #define leapraid_raid_volume_put(volume) \
1030 	kref_put(&(volume)->refcnt, leapraid_raid_volume_free)
1031 
1032 #define LEAPRAID_TGT_FLG_RAID_MEMBER	0x01
1033 #define LEAPRAID_TGT_FLG_VOLUME		0x02
1034 #define LEAPRAID_NO_ULD_ATTACH		1
1035 /**
1036  * struct leapraid_starget_priv - SCSI target private data
1037  *
1038  * @starget: SCSI target structure.
1039  * @sas_address: SAS address.
1040  * @hdl: Device handle.
1041  * @num_luns: Number of LUNs.
1042  * @flg: Flags.
1043  * @deleted: Deletion flag.
1044  * @tm_busy: Task management busy flag.
1045  * @card_port: Associated card port.
1046  * @sas_dev: SAS device structure.
1047  */
1048 struct leapraid_starget_priv {
1049 	struct scsi_target *starget;
1050 	u64 sas_address;
1051 	u16 hdl;
1052 	int num_luns;
1053 	u32 flg;
1054 	u8 deleted;
1055 	u8 tm_busy;
1056 	struct leapraid_card_port *card_port;
1057 	struct leapraid_sas_dev *sas_dev;
1058 };
1059 
1060 #define LEAPRAID_DEVICE_FLG_INIT	0x01
1061 /**
1062  * struct leapraid_sdev_priv - SCSI device private data
1063  *
1064  * @starget_priv: Associated target private data.
1065  * @lun: Logical Unit Number.
1066  * @flg: Flags.
1067  * @ncq_prio_enable: Enables NCQ command priority for RT I/O.
1068  * @block: Block flag.
1069  * @deleted: Deletion flag.
1070  * @sep: SEP flag.
1071  */
1072 struct leapraid_sdev_priv {
1073 	struct leapraid_starget_priv *starget_priv;
1074 	unsigned int lun;
1075 	u32 flg;
1076 	u8 ncq_prio_enable;
1077 	u8 block;
1078 	u8 deleted;
1079 	u8 sep;
1080 };
1081 
1082 /**
1083  * struct leapraid_sas_dev - SAS device structure
1084  *
1085  * @list: List head for device management.
1086  * @starget: SCSI target structure.
1087  * @card_port: Associated card port.
1088  * @rphy: SAS remote PHY.
1089  * @refcnt: Reference count.
1090  * @id: Device ID.
1091  * @channel: SCSI channel.
1092  * @slot: Slot number.
1093  * @phy: PHY identifier.
1094  * @resp: Response status.
1095  * @led_on: LED state.
1096  * @sas_addr: SAS address.
1097  * @dev_name: Device name.
1098  * @hdl: Device handle.
1099  * @parent_sas_addr: Parent SAS address.
1100  * @enc_hdl: Enclosure handle.
1101  * @enc_lid: Enclosure logical ID.
1102  * @volume_hdl: Volume handle.
1103  * @volume_wwid: Volume WWID.
1104  * @dev_info: Device information.
1105  * @pend_sas_rphy_add: Pending SAS remote PHY addition flag.
1106  * @enc_level: Enclosure level.
1107  * @port_connection: Port connection.
1108  * @connector_name: Connector name.
1109  */
1110 struct leapraid_sas_dev {
1111 	struct list_head list;
1112 	struct scsi_target *starget;
1113 	struct leapraid_card_port *card_port;
1114 	struct sas_rphy *rphy;
1115 	struct kref refcnt;
1116 	unsigned int id;
1117 	unsigned int channel;
1118 	u16 slot;
1119 	u8 phy;
1120 	u8 resp;
1121 	u8 led_on;
1122 	u64 sas_addr;
1123 	u64 dev_name;
1124 	u16 hdl;
1125 	u64 parent_sas_addr;
1126 	u16 enc_hdl;
1127 	u64 enc_lid;
1128 	u16 volume_hdl;
1129 	u64 volume_wwid;
1130 	u32 dev_info;
1131 	u8 pend_sas_rphy_add;
1132 	u8 enc_level;
1133 	u8 port_connection;
1134 	u8 connector_name[LEAPRAID_SAS_DEV_P0_CON_NAME_LEN + 1];
1135 };
1136 
leapraid_sdev_free(struct kref * ref)1137 static inline void leapraid_sdev_free(struct kref *ref)
1138 {
1139 	kfree(container_of(ref, struct leapraid_sas_dev, refcnt));
1140 }
1141 
1142 #define leapraid_sdev_get(sdev)	kref_get(&(sdev)->refcnt)
1143 #define leapraid_sdev_put(sdev)	kref_put(&(sdev)->refcnt, leapraid_sdev_free)
1144 
1145 void leapraid_boot_dev_get(void *dev, u32 chnl);
1146 void leapraid_boot_dev_put(void *dev, u32 chnl);
1147 
1148 /**
1149  * struct leapraid_sas_port - SAS port structure
1150  *
1151  * @port_list: List head for port management.
1152  * @phy_list: List of PHYs in this port.
1153  * @port: SAS port structure.
1154  * @card_port: Associated card port.
1155  * @remote_identify: Remote device identification.
1156  * @rphy: SAS remote PHY.
1157  * @phys_num: Number of PHYs in this port.
1158  */
1159 struct leapraid_sas_port {
1160 	struct list_head port_list;
1161 	struct list_head phy_list;
1162 	struct sas_port *port;
1163 	struct leapraid_card_port *card_port;
1164 	struct sas_identify remote_identify;
1165 	struct sas_rphy *rphy;
1166 	u8 phys_num;
1167 };
1168 
1169 #define LEAPRAID_VPHY_FLG_DIRTY	0x01
1170 /**
1171  * struct leapraid_vphy - Virtual PHY structure
1172  *
1173  * @list: List head for PHY management.
1174  * @sas_address: SAS address.
1175  * @phy_mask: PHY mask.
1176  * @flg: Flags.
1177  */
1178 struct leapraid_vphy {
1179 	struct list_head list;
1180 	u64 sas_address;
1181 	u32 phy_mask;
1182 	u8 flg;
1183 };
1184 
1185 /**
1186  * struct leapraid_tgt_rst_list - Target reset tracking entry
1187  * @list: List node used to link reset entries.
1188  * @handle: Device handle of the target being reset.
1189  * @state: Current state of the target reset operation.
1190  *
1191  * Each entry represents a target device that is undergoing or has
1192  * undergone a target reset operation.
1193  */
1194 struct leapraid_tgt_rst_list {
1195 	struct list_head list;
1196 	u16 handle;
1197 	u16 state;
1198 };
1199 
1200 /**
1201  * struct sense_info - Parsed SCSI sense information
1202  * @sense_key: Sense key extracted from sense data.
1203  * @asc: Additional Sense Code (ASC).
1204  * @ascq: Additional Sense Code Qualifier (ASCQ).
1205  *
1206  * This structure stores key fields parsed from SCSI sense data to
1207  * simplify error handling and reporting.
1208  */
1209 struct sense_info {
1210 	u8 sense_key;
1211 	u8 asc;
1212 	u8 ascq;
1213 };
1214 
1215 /**
1216  * struct leapraid_fw_log_info - Firmware log position information
1217  * @user_position: Current log read position used by the driver.
1218  * @adapter_position: Current log write position maintained by firmware.
1219  *
1220  * This structure tracks firmware log buffer positions used for retrieving
1221  * log entries from the adapter.
1222  */
1223 struct leapraid_fw_log_info {
1224 	u32 user_position;
1225 	u32 adapter_position;
1226 };
1227 
1228 /**
1229  * enum reset_type - Reset type enumeration
1230  *
1231  * @FULL_RESET: Full hardware reset.
1232  * @PART_RESET: Partial reset.
1233  */
1234 enum reset_type {
1235 	FULL_RESET,
1236 	PART_RESET,
1237 };
1238 
1239 enum leapraid_card_port_checking_flg {
1240 	CARD_PORT_FURTHER_CHECKING_NEEDED = 0,
1241 	CARD_PORT_SKIP_CHECKING,
1242 };
1243 
1244 enum leapraid_port_checking_state {
1245 	NEW_CARD_PORT = 0,
1246 	SAME_PORT_WITH_NOTHING_CHANGED,
1247 	SAME_PORT_WITH_PARTIALLY_CHANGED_PHYS,
1248 	SAME_ADDR_WITH_PARTIALLY_CHANGED_PHYS,
1249 	SAME_ADDR_ONLY,
1250 };
1251 
1252 /**
1253  * struct leapraid_card_port_feature - Card port feature
1254  *
1255  * @dirty_flg: Dirty flag indicator.
1256  * @same_addr: Same address flag.
1257  * @exact_phy: Exact PHY match flag.
1258  * @phy_overlap: PHY overlap bitmap.
1259  * @same_port: Same port flag.
1260  * @cur_chking_old_port: Current checking old port.
1261  * @expected_old_port: Expected old port.
1262  * @same_addr_port_count: Same address port count.
1263  * @checking_state: Port checking state.
1264  */
1265 struct leapraid_card_port_feature {
1266 	u8 dirty_flg;
1267 	u8 same_addr;
1268 	u8 exact_phy;
1269 	u32 phy_overlap;
1270 	u8 same_port;
1271 	struct leapraid_card_port *cur_chking_old_port;
1272 	struct leapraid_card_port *expected_old_port;
1273 	int same_addr_port_count;
1274 	enum leapraid_port_checking_state checking_state;
1275 };
1276 
1277 #define SMP_REPORT_MANUFACTURER_INFORMATION_FRAME_TYPE	0x40
1278 #define SMP_REPORT_MANUFACTURER_INFORMATION_FUNC	0x01
1279 
1280 /**
1281  * ref: SAS-2(INCITS 457-2010) 10.4.3.5
1282  */
1283 struct leapraid_rep_manu_request {
1284 	u8 smp_frame_type;
1285 	u8 function;
1286 	u8 allocated_response_length;
1287 	u8 request_length;
1288 };
1289 
1290 /**
1291  * ref: SAS-2(INCITS 457-2010) 10.4.3.5
1292  */
1293 struct leapraid_rep_manu_reply {
1294 	u8 smp_frame_type;
1295 	u8 function;
1296 	u8 function_result;
1297 	u8 response_length;
1298 	u16 expander_change_count;
1299 	u8 r1[2];
1300 	u8 sas_format;
1301 	u8 r2[3];
1302 	u8 vendor_identification[SAS_EXPANDER_VENDOR_ID_LEN];
1303 	u8 product_identification[SAS_EXPANDER_PRODUCT_ID_LEN];
1304 	u8 product_revision_level[SAS_EXPANDER_PRODUCT_REV_LEN];
1305 	u8 comp_vendor_identification[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
1306 	u16 component_id;
1307 	u8 component_revision_level;
1308 	u8 r3;
1309 	u8 vendor_specific[8];
1310 };
1311 
1312 extern struct list_head leapraid_adapter_list;
1313 extern spinlock_t leapraid_adapter_lock;
1314 extern char driver_name[LEAPRAID_NAME_LENGTH];
1315 
1316 int leapraid_ctrl_init(struct leapraid_adapter *adapter);
1317 void leapraid_remove_ctrl(struct leapraid_adapter *adapter);
1318 void leapraid_check_scheduled_fault_start(struct leapraid_adapter *adapter);
1319 void leapraid_check_scheduled_fault_stop(struct leapraid_adapter *adapter);
1320 void leapraid_fw_log_start(struct leapraid_adapter *adapter);
1321 void leapraid_fw_log_stop(struct leapraid_adapter *adapter);
1322 int leapraid_set_pcie_and_notification(struct leapraid_adapter *adapter);
1323 void leapraid_disable_controller(struct leapraid_adapter *adapter);
1324 int leapraid_hard_reset_handler(struct leapraid_adapter *adapter,
1325 				enum reset_type type);
1326 void leapraid_mask_int(struct leapraid_adapter *adapter);
1327 void leapraid_unmask_int(struct leapraid_adapter *adapter);
1328 u32 leapraid_get_adapter_state(struct leapraid_adapter *adapter);
1329 bool leapraid_pci_removed(struct leapraid_adapter *adapter);
1330 int leapraid_check_adapter_is_op(struct leapraid_adapter *adapter, int wait,
1331 				 const char *caller);
1332 void *leapraid_get_task_desc(struct leapraid_adapter *adapter, u16 taskid);
1333 void *leapraid_get_sense_buffer(struct leapraid_adapter *adapter, u16 taskid);
1334 __le32 leapraid_get_sense_buffer_dma(struct leapraid_adapter *adapter,
1335 				     u16 taskid);
1336 void *leapraid_get_reply_vaddr(struct leapraid_adapter *adapter,
1337 			       u32 phys_addr);
1338 u16 leapraid_alloc_scsiio_taskid(struct leapraid_adapter *adapter,
1339 				 struct scsi_cmnd *scmd);
1340 void leapraid_free_taskid(struct leapraid_adapter *adapter, u16 taskid);
1341 struct leapraid_io_req_tracker *leapraid_get_io_tracker_from_taskid(
1342 		struct leapraid_adapter *adapter, u16 taskid);
1343 struct scsi_cmnd *leapraid_get_scmd_from_taskid(
1344 		struct leapraid_adapter *adapter, u16 taskid);
1345 int leapraid_scan_dev(struct leapraid_adapter *adapter, bool async_scan_dev);
1346 void leapraid_scan_dev_done(struct leapraid_adapter *adapter);
1347 void leapraid_cleanup_lists(struct leapraid_adapter *adapter);
1348 void leapraid_wait_cmds_done(struct leapraid_adapter *adapter);
1349 void leapraid_clean_active_scsi_cmds(struct leapraid_adapter *adapter);
1350 void leapraid_sync_irqs(struct leapraid_adapter *adapter, bool poll);
1351 int leapraid_rep_queue_handler(struct leapraid_rq *rq);
1352 int leapraid_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num);
1353 void leapraid_mq_polling_pause(struct leapraid_adapter *adapter);
1354 void leapraid_mq_polling_resume(struct leapraid_adapter *adapter);
1355 void leapraid_set_tm_flg(struct leapraid_adapter *adapter, u16 handle);
1356 void leapraid_clear_tm_flg(struct leapraid_adapter *adapter, u16 handle);
1357 void leapraid_async_turn_on_led(struct leapraid_adapter *adapter, u16 handle);
1358 int leapraid_issue_locked_tm(struct leapraid_adapter *adapter, u16 handle,
1359 			     uint channel, uint id, uint lun, u8 type,
1360 			     u16 taskid_task, u8 tr_method);
1361 int leapraid_issue_tm(struct leapraid_adapter *adapter, u16 handle,
1362 		      uint channel, uint id, uint lun, u8 type,
1363 		      u16 taskid_task, u8 tr_method);
1364 bool leapraid_scsiio_done(struct leapraid_adapter *adapter, u16 taskid,
1365 			  u8 msix_index, u32 rep);
1366 int leapraid_get_volume_cap(struct leapraid_adapter *adapter,
1367 			    struct leapraid_raid_volume *raid_volume);
1368 int leapraid_internal_init_cmd_priv(
1369 		struct leapraid_adapter *adapter,
1370 		struct leapraid_io_req_tracker *io_tracker);
1371 void leapraid_internal_exit_cmd_priv(
1372 		struct leapraid_adapter *adapter,
1373 		struct leapraid_io_req_tracker *io_tracker);
1374 void leapraid_clean_active_fw_evt(struct leapraid_adapter *adapter);
1375 bool leapraid_scmd_find_by_lun(struct leapraid_adapter *adapter,
1376 			       uint id, unsigned int lun, uint channel);
1377 bool leapraid_scmd_find_by_tgt(struct leapraid_adapter *adapter,
1378 			       uint id, uint channel);
1379 struct leapraid_vphy *leapraid_get_vphy_by_phy(struct leapraid_card_port *port,
1380 					       u32 phy);
1381 struct leapraid_raid_volume *leapraid_raid_volume_find_by_id(
1382 		struct leapraid_adapter *adapter, uint id, uint channel);
1383 struct leapraid_raid_volume *leapraid_raid_volume_find_by_hdl(
1384 		struct leapraid_adapter *adapter, u16 handle);
1385 struct leapraid_topo_node *leapraid_exp_find_by_sas_address(
1386 		struct leapraid_adapter *adapter, u64 sas_address,
1387 		struct leapraid_card_port *port);
1388 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_by_addr_and_rphy(
1389 		struct leapraid_adapter *adapter,
1390 		u64 sas_address, struct sas_rphy *rphy);
1391 struct leapraid_sas_dev *leapraid_get_sas_dev_by_addr(
1392 		struct leapraid_adapter *adapter, u64 sas_address,
1393 		struct leapraid_card_port *port);
1394 struct leapraid_sas_dev *leapraid_get_sas_dev_by_hdl(
1395 		struct leapraid_adapter *adapter, u16 handle);
1396 struct leapraid_sas_dev *leapraid_get_sas_dev_from_tgt(
1397 		struct leapraid_adapter *adapter,
1398 		struct leapraid_starget_priv *tgt_priv);
1399 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_from_tgt(
1400 		struct leapraid_adapter *adapter,
1401 		 struct leapraid_starget_priv *tgt_priv);
1402 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_by_hdl(
1403 		struct leapraid_adapter *adapter, u16 handle);
1404 struct leapraid_sas_dev *leapraid_hold_lock_get_sas_dev_by_addr(
1405 		struct leapraid_adapter *adapter, u64 sas_address,
1406 		struct leapraid_card_port *port);
1407 struct leapraid_sas_dev *leapraid_get_next_sas_dev_from_init_list(
1408 		struct leapraid_adapter *adapter);
1409 void leapraid_sas_dev_remove_by_sas_address(
1410 		struct leapraid_adapter *adapter,
1411 		u64 sas_address, struct leapraid_card_port *port);
1412 void leapraid_sas_dev_remove(struct leapraid_adapter *adapter,
1413 			     struct leapraid_sas_dev *sas_dev);
1414 void leapraid_raid_volume_remove(struct leapraid_adapter *adapter,
1415 				 struct leapraid_raid_volume *raid_volume);
1416 void leapraid_exp_rm(struct leapraid_adapter *adapter,
1417 		     u64 sas_address, struct leapraid_card_port *port);
1418 void leapraid_build_mpi_sg(struct leapraid_adapter *adapter,
1419 			   void *sge, dma_addr_t h2c_dma_addr, size_t h2c_size,
1420 			   dma_addr_t c2h_dma_addr, size_t c2h_size);
1421 void leapraid_build_ieee_nodata_sg(struct leapraid_adapter *adapter,
1422 				   void *sge);
1423 void leapraid_build_ieee_sg(struct leapraid_adapter *adapter,
1424 			    void *psge, dma_addr_t h2c_dma_addr,
1425 			    size_t h2c_size, dma_addr_t c2h_dma_addr,
1426 			    size_t c2h_size);
1427 int leapraid_build_scmd_ieee_sg(struct leapraid_adapter *adapter,
1428 				struct scsi_cmnd *scmd, u16 taskid);
1429 void leapraid_fire_scsi_io(struct leapraid_adapter *adapter,
1430 			   u16 taskid, u16 handle);
1431 void leapraid_fire_hpr_task(struct leapraid_adapter *adapter, u16 taskid,
1432 			    u16 msix_task);
1433 void leapraid_fire_task(struct leapraid_adapter *adapter, u16 taskid);
1434 int leapraid_cfg_get_volume_hdl(struct leapraid_adapter *adapter,
1435 				u16 pd_handle, u16 *volume_handle);
1436 int leapraid_cfg_get_volume_wwid(struct leapraid_adapter *adapter,
1437 				 u16 volume_handle, u64 *wwid);
1438 int leapraid_op_config_page(struct leapraid_adapter *adapter,
1439 			    void *cfgp, union cfg_param_1 cfgp1,
1440 			    union cfg_param_2 cfgp2,
1441 			    enum config_page_action cfg_op);
1442 void leapraid_log_req_context(struct leapraid_adapter *adapter, u16 smid,
1443 			      const void *req_data);
1444 
1445 int leapraid_change_queue_depth(struct scsi_device *sdev, int qdepth);
1446 
1447 int leapraid_ctl_release(struct inode *inode, struct file *filep);
1448 int leapraid_ctl_init(void);
1449 void leapraid_ctl_exit(void);
1450 
1451 extern struct sas_function_template leapraid_transport_functions;
1452 extern struct scsi_transport_template *leapraid_transport_template;
1453 struct leapraid_sas_port *leapraid_transport_port_add(
1454 		struct leapraid_adapter *adapter, u16 handle, u64 sas_address,
1455 		struct leapraid_card_port *card_port);
1456 void leapraid_transport_port_remove(struct leapraid_adapter *adapter,
1457 				    u64 sas_address, u64 sas_address_parent,
1458 				    struct leapraid_card_port *card_port);
1459 void leapraid_transport_add_card_phy(struct leapraid_adapter *adapter,
1460 				     struct leapraid_card_phy *card_phy,
1461 				     struct leapraid_sas_phy_p0 *phy_pg0,
1462 				     struct device *parent_dev);
1463 int leapraid_transport_add_exp_phy(struct leapraid_adapter *adapter,
1464 				   struct leapraid_card_phy *card_phy,
1465 				   struct leapraid_exp_p1 *exp_pg1,
1466 				   struct device *parent_dev);
1467 void leapraid_transport_update_links(struct leapraid_adapter *adapter,
1468 				     u64 sas_address, u16 handle,
1469 				     u8 phy_number, u8 link_rate,
1470 				     struct leapraid_card_port *card_port);
1471 void leapraid_transport_detach_phy_to_port(
1472 		struct leapraid_adapter *adapter,
1473 		struct leapraid_topo_node *topo_node,
1474 		struct leapraid_card_phy *card_phy);
1475 void leapraid_transport_attach_phy_to_port(
1476 		struct leapraid_adapter *adapter,
1477 		struct leapraid_topo_node *sas_node,
1478 		struct leapraid_card_phy *card_phy,
1479 		u64 sas_address,
1480 		struct leapraid_card_port *card_port);
1481 enum scsi_qc_status leapraid_queuecommand(struct Scsi_Host *shost,
1482 					  struct scsi_cmnd *scmd);
1483 void leapraid_overheat_cleanup(struct leapraid_adapter *adapter);
1484 void leapraid_smart_fault_detect(struct leapraid_adapter *adapter, u16 hdl);
1485 
1486 #endif /* LEAPRAID_FUNC_H_INCLUDED */
1487