Skip to content

Commit f928242

Browse files
committed
Merge branch 'devel'
2 parents 2c125ec + b4da9d0 commit f928242

File tree

6 files changed

+127
-36
lines changed

6 files changed

+127
-36
lines changed

nvme-topology.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static int scan_namespace(struct nvme_namespace *n)
148148
if (ret < 0)
149149
return ret;
150150

151-
fd = open(path, O_RDONLY);
151+
fd = open_global_device(path);
152152
if (fd < 0)
153153
goto free;
154154

@@ -160,7 +160,7 @@ static int scan_namespace(struct nvme_namespace *n)
160160
if (ret < 0)
161161
goto close_fd;
162162
close_fd:
163-
close(fd);
163+
close_global_device();
164164
free:
165165
free(path);
166166
return 0;
@@ -357,21 +357,21 @@ static int verify_legacy_ns(struct nvme_namespace *n)
357357
char tmp_address[64] = "";
358358
legacy_get_pci_bdf(path, tmp_address);
359359
if (tmp_address[0]) {
360-
if (asprintf(&n->ctrl->transport, "pcie") != 1)
360+
if (asprintf(&n->ctrl->transport, "pcie") < 0)
361361
return -1;
362-
if (asprintf(&n->ctrl->address, "%s", tmp_address) != 1)
362+
if (asprintf(&n->ctrl->address, "%s", tmp_address) < 0)
363363
return -1;
364364
}
365365
}
366366

367-
fd = open(path, O_RDONLY);
367+
fd = open_global_device(path);
368368
free (path);
369369

370370
if (fd < 0)
371371
return fd;
372372

373373
ret = nvme_identify_ctrl(fd, &id);
374-
close(fd);
374+
close_global_device();
375375

376376
if (ret)
377377
return ret;
@@ -426,10 +426,10 @@ static int legacy_list(struct nvme_topology *t)
426426
continue;
427427
ret = 0;
428428

429-
fd = open(path, O_RDONLY);
430-
if (fd > 0) {
429+
fd = open_global_device(path);
430+
if (fd >= 0) {
431431
nvme_identify_ctrl(fd, &c->id);
432-
close(fd);
432+
close_global_device();
433433
}
434434
free(path);
435435

nvme.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static void *nvme_alloc(size_t len, bool *huge)
148148

149149
extern struct nvme_device *global_device;
150150

151-
static int open_dev(char *dev)
151+
int open_global_device(char *dev)
152152
{
153153
int err, fd;
154154
char device_str[64];
@@ -178,7 +178,6 @@ static int open_dev(char *dev)
178178
if (!pax->dev) {
179179
switchtec_perror(device_str);
180180
free(pax);
181-
printf("return here\n");
182181
global_device = NULL;
183182
return -ENODEV;
184183
}
@@ -214,6 +213,7 @@ static int open_dev(char *dev)
214213
}
215214

216215
rc = malloc(sizeof(struct rc_nvme_device));
216+
rc->fd = fd;
217217
rc->device.ops = &rc_ops;
218218
global_device = &rc->device;
219219
global_device->type = NVME_DEVICE_TYPE_RC;
@@ -227,27 +227,31 @@ static int open_dev(char *dev)
227227
return err;
228228
}
229229

230-
int close_dev(struct nvme_device *device)
230+
int close_global_device()
231231
{
232-
if (!device)
232+
struct rc_nvme_device *rc;
233+
234+
if (!global_device)
233235
return 0;
234236

235-
if (device->type == NVME_DEVICE_TYPE_PAX) {
237+
if (global_device->type == NVME_DEVICE_TYPE_PAX) {
236238
struct pax_nvme_device *pax;
237-
pax = to_pax_nvme_device(device);
239+
pax = to_pax_nvme_device(global_device);
238240

239241
if (pax->channel_status == SWITCHTEC_EP_TUNNEL_DISABLED)
240242
switchtec_ep_tunnel_disable(pax->dev, pax->pdfid);
241243

242244
switchtec_close(pax->dev);
243245
free(pax);
244246

247+
global_device = NULL;
245248
return 0;
246-
} else if (device->type == NVME_DEVICE_TYPE_RC) {
247-
struct rc_nvme_device *rc;
248-
rc = to_rc_nvme_device(device);
249+
} else if (global_device->type == NVME_DEVICE_TYPE_RC) {
250+
rc = to_rc_nvme_device(global_device);
251+
close(rc->fd);
249252
free(rc);
250253

254+
global_device = NULL;
251255
return 0;
252256
}
253257

@@ -272,7 +276,7 @@ static int get_dev(int argc, char **argv)
272276
if (ret)
273277
return ret;
274278

275-
return open_dev(argv[optind]);
279+
return open_global_device(argv[optind]);
276280
}
277281

278282
int parse_and_open(int argc, char **argv, const char *desc,
@@ -2313,8 +2317,8 @@ static int fw_download(int argc, char **argv, struct command *cmd, struct plugin
23132317
}
23142318

23152319
buf = fw_buf;
2316-
if (cfg.xfer == 0 || cfg.xfer % 4096)
2317-
cfg.xfer = 4096;
2320+
2321+
cfg.xfer = 512;
23182322
if (read(fw_fd, fw_buf, fw_size) != ((ssize_t)(fw_size))) {
23192323
err = -errno;
23202324
fprintf(stderr, "read :%s :%s\n", cfg.fw, strerror(errno));
@@ -4930,7 +4934,7 @@ int main(int argc, char **argv)
49304934
general_help(&builtin);
49314935

49324936

4933-
close_dev(global_device);
4937+
close_global_device();
49344938

49354939
return err;
49364940
}

nvme.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,7 @@ void free_topology(struct nvme_topology *t);
107107
char *get_nvme_subsnqn(char *path);
108108
char *nvme_get_ctrl_attr(char *path, const char *attr);
109109

110+
int open_global_device(char *dev);
111+
int close_global_device();
112+
110113
#endif /* _NVME_H */

plugins/microchip/rc-nvme-device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "nvme-device.h"
2929

3030
struct rc_nvme_device {
31+
int fd;
3132
struct nvme_device device;
3233
};
3334

plugins/microchip/switchtec-nvme-device.c

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
#include <stdio.h>
2626
#include <unistd.h>
2727
#include <inttypes.h>
28+
#include <errno.h>
2829
#include "nvme-device.h"
2930
#include "switchtec-nvme-device.h"
3031

3132
#include <switchtec/switchtec.h>
3233
#include <switchtec/fabric.h>
3334
#include <switchtec/mrpc.h>
35+
#include <switchtec/errors.h>
36+
37+
#define INVALID_MRPC_CMD (SWITCHTEC_ERRNO_MRPC_FLAG_BIT | ERR_CMD_INVALID)
3438

3539
struct switchtec_device_manage_nvme_req
3640
{
@@ -48,15 +52,26 @@ struct switchtec_device_manage_nvme_rsp
4852
uint8_t nvme_data[SWITCHTEC_DEVICE_MANAGE_MAX_RESP - (4 * 4)];
4953
};
5054

55+
struct switchtec_admin_passthru_nvme_req {
56+
uint32_t nvme_sqe[16];
57+
uint8_t nvme_data[SWITCHTEC_NVME_ADMIN_PASSTHRU_MAX_DATA_LEN -
58+
(16 * 4)];
59+
};
5160

52-
int pax_nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd)
61+
struct switchtec_admin_passthru_nvme_rsp {
62+
uint32_t nvme_cqe[4];
63+
uint8_t nvme_data[4096];
64+
};
65+
66+
int pax_nvme_submit_admin_passthru_1k_rsp(int fd,
67+
struct nvme_passthru_cmd *cmd)
5368
{
5469
int ret;
5570

5671
struct pax_nvme_device *pax;
5772
struct switchtec_device_manage_nvme_req req;
5873
struct switchtec_device_manage_nvme_rsp rsp;
59-
int data_len;
74+
size_t data_len = 0;
6075
int status;
6176

6277
memset(&req, 0, sizeof(req));
@@ -69,23 +84,22 @@ int pax_nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd)
6984
/* sqe[9] should be 0 per spec */
7085
req.nvme_sqe[9] = 0;
7186

72-
if (cmd->data_len > sizeof(req.nvme_data))
73-
data_len = sizeof(req.nvme_data);
74-
else
75-
data_len = cmd->data_len;
87+
if (cmd->opcode & 0x1) {
88+
if (cmd->data_len > sizeof(req.nvme_data))
89+
data_len = sizeof(req.nvme_data);
90+
else
91+
data_len = cmd->data_len;
7692

77-
memcpy(req.nvme_data, (void *)cmd->addr, data_len);
93+
memcpy(req.nvme_data, (void *)cmd->addr, data_len);
94+
}
7895

7996
req.hdr.expected_rsp_len = (sizeof(rsp.nvme_cqe) + sizeof(rsp.nvme_data));
8097

8198
ret = switchtec_device_manage(pax->dev,
8299
(struct switchtec_device_manage_req *)&req,
83100
(struct switchtec_device_manage_rsp *)&rsp);
84-
85-
if (ret) {
86-
switchtec_perror("device_manage_cmd");
101+
if (ret)
87102
return ret;
88-
}
89103

90104
status = (rsp.nvme_cqe[3] & 0xfffe0000) >> 17;
91105
if (!status) {
@@ -100,6 +114,70 @@ int pax_nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd)
100114
return status;
101115
}
102116

117+
int pax_nvme_submit_admin_passthru_4k_rsp(int fd,
118+
struct nvme_passthru_cmd *cmd)
119+
{
120+
int ret;
121+
122+
struct pax_nvme_device *pax;
123+
struct switchtec_admin_passthru_nvme_req req;
124+
struct switchtec_admin_passthru_nvme_rsp rsp;
125+
size_t data_len = 0;
126+
size_t rsp_len;
127+
int status;
128+
129+
memset(&req, 0, sizeof(req));
130+
memset(&rsp, 0, sizeof(rsp));
131+
pax = to_pax_nvme_device(global_device);
132+
133+
memcpy(&req.nvme_sqe, cmd, 16 * 4);
134+
135+
/* sqe[9] should be 0 per spec */
136+
req.nvme_sqe[9] = 0;
137+
138+
if (cmd->opcode & 0x1) {
139+
if (cmd->data_len > sizeof(req.nvme_data))
140+
data_len = sizeof(req.nvme_data);
141+
else
142+
data_len = cmd->data_len;
143+
144+
memcpy(req.nvme_data, (void *)cmd->addr, data_len);
145+
}
146+
147+
data_len = data_len + 16 * 4;
148+
149+
rsp_len = (sizeof(rsp.nvme_cqe) + cmd->data_len);
150+
151+
ret = switchtec_nvme_admin_passthru(pax->dev, pax->pdfid,
152+
data_len, &req,
153+
&rsp_len, &rsp);
154+
if (ret)
155+
return ret;
156+
157+
status = (rsp.nvme_cqe[3] & 0xfffe0000) >> 17;
158+
if (!status) {
159+
cmd->result = rsp.nvme_cqe[0];
160+
if (cmd->addr) {
161+
memcpy((uint64_t *)cmd->addr,
162+
rsp.nvme_data,
163+
rsp_len - 4 * 4);
164+
}
165+
}
166+
167+
return status;
168+
}
169+
170+
int pax_nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd)
171+
{
172+
int ret;
173+
174+
ret = pax_nvme_submit_admin_passthru_4k_rsp(fd, cmd);
175+
if (ret && errno == INVALID_MRPC_CMD)
176+
ret = pax_nvme_submit_admin_passthru_1k_rsp(fd, cmd);
177+
178+
return ret;
179+
}
180+
103181
int pax_nvme_io(int fd, struct nvme_user_io *io)
104182
{
105183
fprintf(stderr, "%s not implemented.\n", __FUNCTION__);
@@ -126,8 +204,13 @@ int pax_nvme_ns_rescan(int fd)
126204

127205
int pax_nvme_submit_passthru(int fd, int ioctl_cmd, struct nvme_passthru_cmd *cmd)
128206
{
129-
fprintf(stderr, "%s not implemented.\n", __FUNCTION__);
130-
return -1;
207+
if (ioctl_cmd != (int)NVME_IOCTL_ADMIN_CMD) {
208+
fprintf(stderr, "%s only supports NVME_IOCTL_ADMIN_CMD.\n",
209+
__FUNCTION__);
210+
return -1;
211+
}
212+
213+
return pax_nvme_submit_admin_passthru(fd, cmd);
131214
}
132215

133216
int pax_nvme_submit_io_passthru(int fd, struct nvme_passthru_cmd *cmd)

plugins/microchip/switchtec-nvme.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ int get_nvme_info(int fd, struct list_item *item, const char *node)
7979
if (item->nsid <= 0)
8080
return item->nsid;
8181
err = nvme_identify_ns(fd, item->nsid,
82-
0, &item->ns);
82+
1, &item->ns);
8383
if (err)
8484
return err;
8585
strcpy(item->node, node);

0 commit comments

Comments
 (0)