Skip to content
/ linux Public

Commit 3937638

Browse files
Yihang Ligregkh
authored andcommitted
scsi: hisi_sas: Use macro instead of magic number
[ Upstream commit 4ca7fe9 ] The hisi_sas driver has a large number of magic numbers which makes for unfriendly code reading. Use macro definitions instead. Signed-off-by: Yihang Li <liyihang9@huawei.com> Link: https://lore.kernel.org/r/20250414080845.1220997-2-liyihang9@huawei.com Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> Stable-dep-of: 8ddc0c2 ("scsi: hisi_sas: Fix NULL pointer exception during user_scan()") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 890bcf9 commit 3937638

File tree

3 files changed

+213
-115
lines changed

3 files changed

+213
-115
lines changed

drivers/scsi/hisi_sas/hisi_sas.h

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
#define HISI_SAS_IOST_ITCT_CACHE_DW_SZ 10
4747
#define HISI_SAS_FIFO_DATA_DW_SIZE 32
4848

49+
#define HISI_SAS_REG_MEM_SIZE 4
50+
#define HISI_SAS_MAX_CDB_LEN 16
51+
#define HISI_SAS_BLK_QUEUE_DEPTH 64
52+
53+
#define BYTE_TO_DW 4
54+
#define BYTE_TO_DDW 8
55+
4956
#define HISI_SAS_STATUS_BUF_SZ (sizeof(struct hisi_sas_status_buffer))
5057
#define HISI_SAS_COMMAND_TABLE_SZ (sizeof(union hisi_sas_command_table))
5158

@@ -92,6 +99,8 @@
9299

93100
#define HISI_SAS_WAIT_PHYUP_TIMEOUT (30 * HZ)
94101
#define HISI_SAS_CLEAR_ITCT_TIMEOUT (20 * HZ)
102+
#define HISI_SAS_DELAY_FOR_PHY_DISABLE 100
103+
#define NAME_BUF_SIZE 256
95104

96105
struct hisi_hba;
97106

@@ -167,6 +176,8 @@ struct hisi_sas_debugfs_fifo {
167176
u32 rd_data[HISI_SAS_FIFO_DATA_DW_SIZE];
168177
};
169178

179+
#define FRAME_RCVD_BUF 32
180+
#define SAS_PHY_RESV_SIZE 2
170181
struct hisi_sas_phy {
171182
struct work_struct works[HISI_PHYES_NUM];
172183
struct hisi_hba *hisi_hba;
@@ -178,10 +189,10 @@ struct hisi_sas_phy {
178189
spinlock_t lock;
179190
u64 port_id; /* from hw */
180191
u64 frame_rcvd_size;
181-
u8 frame_rcvd[32];
192+
u8 frame_rcvd[FRAME_RCVD_BUF];
182193
u8 phy_attached;
183194
u8 in_reset;
184-
u8 reserved[2];
195+
u8 reserved[SAS_PHY_RESV_SIZE];
185196
u32 phy_type;
186197
u32 code_violation_err_count;
187198
enum sas_linkrate minimum_linkrate;
@@ -348,6 +359,7 @@ struct hisi_sas_hw {
348359
};
349360

350361
#define HISI_SAS_MAX_DEBUGFS_DUMP (50)
362+
#define HISI_SAS_DEFAULT_DEBUGFS_DUMP 1
351363

352364
struct hisi_sas_debugfs_cq {
353365
struct hisi_sas_cq *cq;
@@ -527,12 +539,13 @@ struct hisi_sas_cmd_hdr {
527539
__le64 dif_prd_table_addr;
528540
};
529541

542+
#define ITCT_RESV_DDW 12
530543
struct hisi_sas_itct {
531544
__le64 qw0;
532545
__le64 sas_addr;
533546
__le64 qw2;
534547
__le64 qw3;
535-
__le64 qw4_15[12];
548+
__le64 qw4_15[ITCT_RESV_DDW];
536549
};
537550

538551
struct hisi_sas_iost {
@@ -542,22 +555,26 @@ struct hisi_sas_iost {
542555
__le64 qw3;
543556
};
544557

558+
#define ERROR_RECORD_BUF_DW 4
545559
struct hisi_sas_err_record {
546-
u32 data[4];
560+
u32 data[ERROR_RECORD_BUF_DW];
547561
};
548562

563+
#define FIS_RESV_DW 3
549564
struct hisi_sas_initial_fis {
550565
struct hisi_sas_err_record err_record;
551566
struct dev_to_host_fis fis;
552-
u32 rsvd[3];
567+
u32 rsvd[FIS_RESV_DW];
553568
};
554569

570+
#define BREAKPOINT_DATA_SIZE 128
555571
struct hisi_sas_breakpoint {
556-
u8 data[128];
572+
u8 data[BREAKPOINT_DATA_SIZE];
557573
};
558574

575+
#define BREAKPOINT_TAG_NUM 32
559576
struct hisi_sas_sata_breakpoint {
560-
struct hisi_sas_breakpoint tag[32];
577+
struct hisi_sas_breakpoint tag[BREAKPOINT_TAG_NUM];
561578
};
562579

563580
struct hisi_sas_sge {
@@ -568,13 +585,15 @@ struct hisi_sas_sge {
568585
__le32 data_off;
569586
};
570587

588+
#define SMP_CMD_TABLE_SIZE 44
571589
struct hisi_sas_command_table_smp {
572-
u8 bytes[44];
590+
u8 bytes[SMP_CMD_TABLE_SIZE];
573591
};
574592

593+
#define DUMMY_BUF_SIZE 12
575594
struct hisi_sas_command_table_stp {
576595
struct host_to_dev_fis command_fis;
577-
u8 dummy[12];
596+
u8 dummy[DUMMY_BUF_SIZE];
578597
u8 atapi_cdb[ATAPI_CDB_LEN];
579598
};
580599

@@ -588,12 +607,13 @@ struct hisi_sas_sge_dif_page {
588607
struct hisi_sas_sge sge[HISI_SAS_SGE_DIF_PAGE_CNT];
589608
} __aligned(16);
590609

610+
#define PROT_BUF_SIZE 7
591611
struct hisi_sas_command_table_ssp {
592612
struct ssp_frame_hdr hdr;
593613
union {
594614
struct {
595615
struct ssp_command_iu task;
596-
u32 prot[7];
616+
u32 prot[PROT_BUF_SIZE];
597617
};
598618
struct ssp_tmf_iu ssp_task;
599619
struct xfer_rdy_iu xfer_rdy;
@@ -607,9 +627,10 @@ union hisi_sas_command_table {
607627
struct hisi_sas_command_table_stp stp;
608628
} __aligned(16);
609629

630+
#define IU_BUF_SIZE 1024
610631
struct hisi_sas_status_buffer {
611632
struct hisi_sas_err_record err;
612-
u8 iu[1024];
633+
u8 iu[IU_BUF_SIZE];
613634
} __aligned(16);
614635

615636
struct hisi_sas_slot_buf_table {

drivers/scsi/hisi_sas/hisi_sas_main.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
#include "hisi_sas.h"
88
#define DRV_NAME "hisi_sas"
99

10+
#define LINK_RATE_BIT_MASK 2
11+
#define FIS_BUF_SIZE 20
12+
#define WAIT_CMD_COMPLETE_DELAY 100
13+
#define WAIT_CMD_COMPLETE_TMROUT 5000
14+
#define DELAY_FOR_LINK_READY 2000
15+
#define BLK_CNT_OPTIMIZE_MARK 64
16+
#define HZ_TO_MHZ 1000000
17+
#define DELAY_FOR_SOFTRESET_MAX 1000
18+
#define DELAY_FOR_SOFTRESET_MIN 900
19+
1020
#define DEV_IS_GONE(dev) \
1121
((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
1222

@@ -127,7 +137,7 @@ u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
127137

128138
max -= SAS_LINK_RATE_1_5_GBPS;
129139
for (i = 0; i <= max; i++)
130-
rate |= 1 << (i * 2);
140+
rate |= 1 << (i * LINK_RATE_BIT_MASK);
131141
return rate;
132142
}
133143
EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
@@ -877,7 +887,7 @@ int hisi_sas_device_configure(struct scsi_device *sdev,
877887
if (ret)
878888
return ret;
879889
if (!dev_is_sata(dev))
880-
sas_change_queue_depth(sdev, 64);
890+
sas_change_queue_depth(sdev, HISI_SAS_BLK_QUEUE_DEPTH);
881891

882892
return 0;
883893
}
@@ -1239,7 +1249,7 @@ static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
12391249
sas_phy->phy->minimum_linkrate = min;
12401250

12411251
hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1242-
msleep(100);
1252+
msleep(HISI_SAS_DELAY_FOR_PHY_DISABLE);
12431253
hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
12441254
hisi_sas_phy_enable(hisi_hba, phy_no, 1);
12451255

@@ -1269,7 +1279,7 @@ static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
12691279

12701280
case PHY_FUNC_LINK_RESET:
12711281
hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1272-
msleep(100);
1282+
msleep(HISI_SAS_DELAY_FOR_PHY_DISABLE);
12731283
hisi_sas_phy_enable(hisi_hba, phy_no, 1);
12741284
break;
12751285

@@ -1324,7 +1334,7 @@ static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
13241334

13251335
static int hisi_sas_softreset_ata_disk(struct domain_device *device)
13261336
{
1327-
u8 fis[20] = {0};
1337+
u8 fis[FIS_BUF_SIZE] = {0};
13281338
struct ata_port *ap = device->sata_dev.ap;
13291339
struct ata_link *link;
13301340
int rc = TMF_RESP_FUNC_FAILED;
@@ -1341,7 +1351,7 @@ static int hisi_sas_softreset_ata_disk(struct domain_device *device)
13411351
}
13421352

13431353
if (rc == TMF_RESP_FUNC_COMPLETE) {
1344-
usleep_range(900, 1000);
1354+
usleep_range(DELAY_FOR_SOFTRESET_MIN, DELAY_FOR_SOFTRESET_MAX);
13451355
ata_for_each_link(link, ap, EDGE) {
13461356
int pmp = sata_srst_pmp(link);
13471357

@@ -1460,7 +1470,7 @@ static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
14601470
struct device *dev = hisi_hba->dev;
14611471
int rc = TMF_RESP_FUNC_FAILED;
14621472
struct ata_link *link;
1463-
u8 fis[20] = {0};
1473+
u8 fis[FIS_BUF_SIZE] = {0};
14641474
int i;
14651475

14661476
for (i = 0; i < hisi_hba->n_phy; i++) {
@@ -1527,7 +1537,9 @@ void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
15271537
hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
15281538

15291539
scsi_block_requests(shost);
1530-
hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1540+
hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba,
1541+
WAIT_CMD_COMPLETE_DELAY,
1542+
WAIT_CMD_COMPLETE_TMROUT);
15311543

15321544
/*
15331545
* hisi_hba->timer is only used for v1/v2 hw, and check hw->sht
@@ -1828,7 +1840,7 @@ static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
18281840
rc = ata_wait_after_reset(link, jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT,
18291841
smp_ata_check_ready_type);
18301842
} else {
1831-
msleep(2000);
1843+
msleep(DELAY_FOR_LINK_READY);
18321844
}
18331845

18341846
return rc;
@@ -2243,12 +2255,14 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba)
22432255
goto err_out;
22442256

22452257
/* roundup to avoid overly large block size */
2246-
max_command_entries_ru = roundup(max_command_entries, 64);
2258+
max_command_entries_ru = roundup(max_command_entries,
2259+
BLK_CNT_OPTIMIZE_MARK);
22472260
if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
22482261
sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
22492262
else
22502263
sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2251-
sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2264+
2265+
sz_slot_buf_ru = roundup(sz_slot_buf_ru, BLK_CNT_OPTIMIZE_MARK);
22522266
s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
22532267
blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
22542268
slots_per_blk = s / sz_slot_buf_ru;
@@ -2413,7 +2427,8 @@ int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
24132427
if (IS_ERR(refclk))
24142428
dev_dbg(dev, "no ref clk property\n");
24152429
else
2416-
hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2430+
hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) /
2431+
HZ_TO_MHZ;
24172432

24182433
if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
24192434
dev_err(dev, "could not get property phy-count\n");
@@ -2530,7 +2545,7 @@ int hisi_sas_probe(struct platform_device *pdev,
25302545
shost->max_id = HISI_SAS_MAX_DEVICES;
25312546
shost->max_lun = ~0;
25322547
shost->max_channel = 1;
2533-
shost->max_cmd_len = 16;
2548+
shost->max_cmd_len = HISI_SAS_MAX_CDB_LEN;
25342549
if (hisi_hba->hw->slot_index_alloc) {
25352550
shost->can_queue = HISI_SAS_MAX_COMMANDS;
25362551
shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;

0 commit comments

Comments
 (0)