Skip to content

Commit 27579d1

Browse files
committed
Code review updates for bitfields and more
1 parent d78f390 commit 27579d1

5 files changed

Lines changed: 142 additions & 71 deletions

File tree

src/nvme/mi-mctp.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ static struct __mi_mctp_socket_ops ops = {
128128
nvme_mi_aem_socket,
129129
sendmsg,
130130
recvmsg,
131-
recv,
132131
poll,
133132
ioctl_tag,
134133
};
@@ -269,15 +268,24 @@ static int nvme_mi_mctp_aem_fd(struct nvme_mi_ep *ep)
269268

270269
static int nvme_mi_mctp_aem_purge(struct nvme_mi_ep *ep)
271270
{
272-
char buffer;
273271
struct nvme_mi_transport_mctp *mctp = ep->transport_data;
272+
struct msghdr msg = {0};
273+
struct iovec iov;
274+
char buffer;
275+
276+
iov.iov_base = &buffer;
277+
iov.iov_len = sizeof(buffer);
278+
msg.msg_iov = &iov;
279+
msg.msg_iovlen = 1;
274280

275281
// Read until there is no more data
276-
ops.recv(mctp->sd_aem, &buffer, sizeof(buffer), MSG_TRUNC);
282+
while (ops.recvmsg(mctp->sd_aem, &msg, MSG_TRUNC) > 0)
283+
;
277284

278285
return 0;
279286
}
280287

288+
281289
static int nvme_mi_mctp_aem_read(struct nvme_mi_ep *ep,
282290
struct nvme_mi_resp *resp)
283291
{

src/nvme/mi.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@
2323

2424
#define NUM_ENABLES (256u)
2525

26+
_Static_assert(sizeof(struct nvme_mi_aem_supported_list_header) == 5,
27+
"size_of_nvme_mi_aem_supported_list_header_is_not_5_bytes");
28+
_Static_assert(sizeof(struct nvme_mi_aem_supported_item) == 3,
29+
"sizeof_nvme_mi_aem_supported_item_is_not_3_bytes");
30+
_Static_assert(sizeof(struct nvme_mi_aem_enable_item) == 3,
31+
"size_of_ae_enable_item_t_is_not_3_bytes");
32+
_Static_assert(sizeof(struct nvme_mi_aem_enable_list_header) == 5,
33+
"size_of_nvme_mi_aem_enable_list_header_is_not_5_bytes");
34+
_Static_assert(sizeof(struct nvme_mi_aem_occ_data) == 9,
35+
"size_of_nvme_mi_aem_occ_data_is_not_9_bytes");
36+
_Static_assert(sizeof(struct nvme_mi_aem_occ_list_hdr) == 7,
37+
"size_of_nvme_mi_aem_occ_list_hdr_is_not_7_bytes");
38+
2639
static int nvme_mi_get_async_message(nvme_mi_ep_t ep,
2740
struct nvme_mi_aem_msg *aem_msg, size_t *aem_msg_len);
2841

@@ -2172,17 +2185,19 @@ static int validate_occ_list_update_ctx(
21722185
//Make sure header is the right version and length
21732186
errno = EPROTO;
21742187
goto err_cleanup;
2175-
} else if (occ_header->aeolli.aeoltl > len) {
2188+
} else if (nvme_mi_aem_aeolli_get_aeoltl(occ_header->aeolli) > len) {
21762189
//Full length is bigger than the data that was received
21772190
errno = EPROTO;
21782191
goto err_cleanup;
21792192
} else if (check_generation_num &&
2180-
ctx->last_generation_num == (int) occ_header->aemti.aemgn) {
2193+
ctx->last_generation_num ==
2194+
(int) nvme_mi_aem_aemti_get_aemgn(occ_header->aemti)) {
21812195
//This is a duplicate and shouldn't be parsed.
21822196
//Let's just act like there's no updates
21832197
occ_header->numaeo = 0;
21842198
} else if (check_generation_num) {
2185-
ctx->last_generation_num = occ_header->aemti.aemgn;
2199+
ctx->last_generation_num =
2200+
nvme_mi_aem_aemti_get_aemgn(occ_header->aemti);
21862201
}
21872202

21882203
//Header is fine. Let's go through the data
@@ -2212,7 +2227,7 @@ static int validate_occ_list_update_ctx(
22122227
uint32_t offset = sizeof(*current) + current->aeosil + current->aeovsil;
22132228

22142229
bytes_so_far += offset;
2215-
if (bytes_so_far > occ_header->aeolli.aeoltl) {
2230+
if (bytes_so_far > nvme_mi_aem_aeolli_get_aeoltl(occ_header->aeolli)) {
22162231
errno = EPROTO;
22172232
goto err_cleanup;
22182233
}
@@ -2316,8 +2331,8 @@ static int aem_disable_enabled(nvme_mi_ep_t ep)
23162331

23172332
for (int i = 0; i < NUM_ENABLES; i++) {
23182333
if (already_enabled.enabled[i]) {
2319-
sync_data[sync_data_count].aeei.aeeid = i;
2320-
sync_data[sync_data_count].aeei.aee = false;
2334+
nvme_mi_aem_aeei_set_aeeid(i, &sync_data[sync_data_count]);
2335+
nvme_mi_aem_aeei_set_aee(false, &sync_data[sync_data_count]);
23212336
sync_data_count++;
23222337
}
23232338
}
@@ -2374,8 +2389,8 @@ int nvme_mi_aem_enable(nvme_mi_ep_t ep,
23742389
//Now, let's do a fresh enable of what's asked
23752390
for (int i = 0; i < NUM_ENABLES; i++) {
23762391
if (config->enabled_map.enabled[i]) {
2377-
sync_data[sync_data_count].aeei.aeeid = i;
2378-
sync_data[sync_data_count].aeei.aee = true;
2392+
nvme_mi_aem_aeei_set_aeeid(i, &sync_data[sync_data_count]);
2393+
nvme_mi_aem_aeei_set_aee(true, &sync_data[sync_data_count]);
23792394
sync_data_count++;
23802395
}
23812396
}
@@ -2438,8 +2453,11 @@ int nvme_mi_aem_get_enabled(nvme_mi_ep_t ep,
24382453
struct nvme_mi_aem_enable_item *items =
24392454
(struct nvme_mi_aem_enable_item *)(enabled_list + 1);
24402455

2441-
for (int i = 0; i < enabled_list->hdr.numaes; i++)
2442-
enabled_map->enabled[items[i].aeei.aeeid] = items[i].aeei.aee;
2456+
for (int i = 0; i < enabled_list->hdr.numaes; i++) {
2457+
__u8 aeeid = nvme_mi_aem_aeei_get_aeeid(items[i].aeei);
2458+
bool enabled = nvme_mi_aem_aeei_get_aee(items[i].aeei);
2459+
enabled_map->enabled[aeeid] = enabled;
2460+
}
24432461

24442462
cleanup:
24452463
free(enabled_list);
@@ -2547,7 +2565,7 @@ int nvme_mi_aem_process(nvme_mi_ep_t ep, void *userdata)
25472565
if (action == NVME_MI_AEM_HNA_ACK) {
25482566
response_len = sizeof(response_buffer);
25492567

2550-
rc = aem_ack(ep, &response->occ_list_hdr, &response_len);
2568+
rc = nvme_mi_aem_ack(ep, &response->occ_list_hdr, &response_len);
25512569
if (rc)
25522570
goto cleanup;
25532571

src/nvme/mi.h

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686

8787
#include <endian.h>
8888
#include <stdint.h>
89+
#include <ccan/endian/endian.h>
8990

9091
#include "types.h"
9192
#include "tree.h"
@@ -327,9 +328,6 @@ struct nvme_mi_aem_supported_list_header {
327328
__le16 aest;
328329
__u8 aeslhl; //Should be 5
329330
} __attribute__((packed));
330-
_Static_assert(sizeof(struct nvme_mi_aem_supported_list_header) == 5,
331-
"size_of_nvme_mi_aem_supported_list_header_is_not_5_bytes");
332-
333331

334332
/**
335333
* struct nvme_mi_aem_supported_item - AE Supported List Item
@@ -341,14 +339,33 @@ _Static_assert(sizeof(struct nvme_mi_aem_supported_list_header) == 5,
341339
*/
342340
struct nvme_mi_aem_supported_item {
343341
__u8 aesl;//Length of this item. Set to 3
344-
struct {
345-
__le16 aeis : 8; //Identifier of supported ae
346-
__le16 reserved : 7;
347-
__le16 aese : 1; //AE Support Enabled
348-
} __attribute__((packed)) aesi;
342+
__le16 aesi;
349343
} __attribute__((packed));
350-
_Static_assert(sizeof(struct nvme_mi_aem_supported_item) == 3,
351-
"sizeof_nvme_mi_aem_supported_item_is_not_3_bytes");
344+
345+
static inline bool nvme_mi_aem_aesi_get_aese(__le16 aesi)
346+
{
347+
return !!(le16_to_cpu(aesi) & 0x8000);
348+
}
349+
350+
static inline __u8 nvme_mi_aem_aesi_get_aesid(__le16 aesi)
351+
{
352+
return le16_to_cpu(aesi) & 0xff;
353+
}
354+
355+
static inline void nvme_mi_aem_aesi_set_aesid(__u8 aesid, struct nvme_mi_aem_supported_item *item)
356+
{
357+
__u16 temp = le16_to_cpu(item->aesi);
358+
359+
item->aesi = cpu_to_le16((temp & 0xFF00) | aesid);
360+
}
361+
362+
static inline void nvme_mi_aem_aesi_set_aee(bool enabled, struct nvme_mi_aem_supported_item *item)
363+
{
364+
__u16 temp = le16_to_cpu(item->aesi);
365+
__u8 bit = (enabled) ? 1 : 0;
366+
367+
item->aesi = cpu_to_le16((temp & 0xFF) | (bit << 15));
368+
}
352369

353370
/**
354371
* struct nvme_mi_aem_supported_list - AE Supported List received with GET CONFIG Asynchronous Event
@@ -369,14 +386,33 @@ struct nvme_mi_aem_supported_list {
369386
*/
370387
struct nvme_mi_aem_enable_item {
371388
__u8 aeel;
372-
struct{
373-
__le16 aeeid : 8; //AE identifier
374-
__le16 reserved : 7;
375-
__le16 aee : 1; //AE enabled bit
376-
} __attribute__((packed)) aeei;
377-
};
378-
_Static_assert(sizeof(struct nvme_mi_aem_enable_item) == 3,
379-
"size_of_ae_enable_item_t_is_not_3_bytes");
389+
__le16 aeei;
390+
}__attribute__((packed)) ;
391+
392+
static inline bool nvme_mi_aem_aeei_get_aee(__le16 aeei)
393+
{
394+
return !!(le16_to_cpu(aeei) & 0x8000);
395+
}
396+
397+
static inline __u8 nvme_mi_aem_aeei_get_aeeid(__le16 aeei)
398+
{
399+
return (le16_to_cpu(aeei) & 0xFF);
400+
}
401+
402+
static inline void nvme_mi_aem_aeei_set_aeeid(__u8 aeeid, struct nvme_mi_aem_enable_item *item)
403+
{
404+
__u16 temp = le16_to_cpu(item->aeei);
405+
406+
item->aeei = cpu_to_le16((temp & 0xFF00) | aeeid);
407+
}
408+
409+
static inline void nvme_mi_aem_aeei_set_aee(bool enabled, struct nvme_mi_aem_enable_item *item)
410+
{
411+
__u16 temp = le16_to_cpu(item->aeei);
412+
__u8 bit = (enabled) ? 1 : 0;
413+
414+
item->aeei = cpu_to_le16((temp & 0xFF) | (bit << 15));
415+
}
380416

381417
/**
382418
* struct nvme_mi_aem_enable_list_header - AE Enable list header
@@ -391,8 +427,6 @@ struct nvme_mi_aem_enable_list_header {
391427
__le16 aeetl;
392428
__u8 aeelhl;
393429
} __attribute__((packed));
394-
_Static_assert(sizeof(struct nvme_mi_aem_enable_list_header) == 5,
395-
"size_of_nvme_mi_aem_enable_list_header_is_not_5_bytes");
396430

397431
/**
398432
* struct nvme_mi_aem_enable_list - AE enable list sent with SET CONFIG Asyncronous Event
@@ -428,8 +462,6 @@ struct nvme_mi_aem_occ_data {
428462
__u8 aessi;
429463
} __attribute__((packed)) aeoui;
430464
} __attribute__((packed));
431-
_Static_assert(sizeof(struct nvme_mi_aem_occ_data) == 9,
432-
"size_of_nvme_mi_aem_occ_data_is_not_9_bytes");
433465

434466
/**
435467
* struct nvme_mi_aem_occ_list_hdr - AE occurrence list header
@@ -446,18 +478,33 @@ _Static_assert(sizeof(struct nvme_mi_aem_occ_data) == 9,
446478
struct nvme_mi_aem_occ_list_hdr {
447479
__u8 numaeo;
448480
__u8 aelver;
449-
struct {
450-
unsigned int aeoltl: 23;
451-
unsigned int overflow: 1;
452-
} __attribute__((packed)) aeolli;
481+
__u8 aeolli[3];//24-bits
453482
__u8 aeolhl;
454-
struct {
455-
unsigned int aemrc: 3;
456-
unsigned int aemgn: 5;
457-
} __attribute__((packed)) aemti;
483+
__u8 aemti;
458484
} __attribute__((packed));
459-
_Static_assert(sizeof(struct nvme_mi_aem_occ_list_hdr) == 7,
460-
"size_of_nvme_mi_aem_occ_list_hdr_is_not_7_bytes");
485+
486+
static inline __u8 nvme_mi_aem_aemti_get_aemgn(__u8 aemti)
487+
{
488+
return aemti >> 3 & 0x1f;
489+
}
490+
491+
static inline __u32 nvme_mi_aem_aeolli_get_aeoltl(__u8 *aeolli)
492+
{
493+
//First 23-bits contain the aeoltl
494+
__u32 aeoltl = le32_to_cpu(aeolli[0] | (aeolli[1] << 8) | (aeolli[2] << 16));
495+
496+
return aeoltl & 0x7FFFFF;
497+
}
498+
499+
static inline void nvme_mi_aem_aeolli_set_aeoltl(__u32 aeoltl, struct nvme_mi_aem_occ_list_hdr *hdr)
500+
{
501+
//First 23-bits contain the aeoltl
502+
__u32 le_bytes = cpu_to_le32(aeoltl);
503+
504+
hdr->aeolli[0] = le_bytes & 0xFF;
505+
hdr->aeolli[1] = (le_bytes >> 8) & 0xFF;
506+
hdr->aeolli[2] = (hdr->aeolli[2] & 0b10000000) | ((le_bytes >> 16) & 0x7F);
507+
}
461508

462509

463510
/**
@@ -1013,7 +1060,7 @@ int nvme_mi_mi_read_mi_data_ctrl(nvme_mi_ep_t ep, __u16 ctrl_id,
10131060
*
10141061
* See &struct nvme_mi_nvm_ss_health_status.
10151062
*
1016-
* Return: The nvme command status if a response was received (see
1063+
* Return`The nvme command status if a response was received (see
10171064
* &enum nvme_status_field) or -1 with errno set otherwise..
10181065
*/
10191066
int nvme_mi_mi_subsystem_health_status_poll(nvme_mi_ep_t ep, bool clear,
@@ -1249,9 +1296,9 @@ int nvme_mi_mi_config_set_async_event(nvme_mi_ep_t ep,
12491296
struct nvme_mi_aem_occ_list_hdr *occ_list,
12501297
size_t *occ_list_num_bytes);
12511298

1252-
static inline int aem_ack(nvme_mi_ep_t ep,
1253-
struct nvme_mi_aem_occ_list_hdr *occ_list,
1254-
size_t *occ_list_num_bytes)
1299+
static inline int nvme_mi_aem_ack(nvme_mi_ep_t ep,
1300+
struct nvme_mi_aem_occ_list_hdr *occ_list,
1301+
size_t *occ_list_num_bytes)
12551302
{
12561303
//An AEM Ack is defined as a SET CONFIG AE with no AE enable items
12571304
struct nvme_mi_aem_enable_list list = {0};

src/nvme/private.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ struct __mi_mctp_socket_ops {
309309
int (*aem_socket)(__u8 eid, unsigned int network);
310310
ssize_t (*sendmsg)(int, const struct msghdr *, int);
311311
ssize_t (*recvmsg)(int, struct msghdr *, int);
312-
ssize_t (*recv)(int __fd, void *__buf, size_t __n, int __flags);
313312
int (*poll)(struct pollfd *, nfds_t, int);
314313
int (*ioctl_tag)(int, unsigned long, struct mctp_ioc_tag_ctl *);
315314
};

0 commit comments

Comments
 (0)