Skip to content

Commit 535508f

Browse files
committed
cmds: move nvme cmds from util to cmds.h or types.h
Move the nvme specification parts of utils to either cmds.h or types.h and reduce the number of include dependencies. Signed-off-by: Daniel Wagner <[email protected]>
1 parent eb079f6 commit 535508f

5 files changed

Lines changed: 622 additions & 622 deletions

File tree

libnvme/src/nvme/cmds.c

Lines changed: 201 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ int nvme_get_telemetry_log(struct nvme_transport_handle *hdl, bool create,
240240
return 0;
241241
}
242242

243-
244243
static int nvme_check_get_telemetry_log(struct nvme_transport_handle *hdl,
245244
bool create, bool ctrl, bool rae,
246245
struct nvme_telemetry_log **log, enum nvme_telemetry_da da,
@@ -412,3 +411,204 @@ int nvme_get_logical_block_size(struct nvme_transport_handle *hdl,
412411

413412
return 0;
414413
}
414+
415+
static inline void nvme_init_copy_range_elbt(__u8 *elbt, __u64 eilbrt)
416+
{
417+
int i;
418+
419+
for (i = 0; i < 8; i++)
420+
elbt[9 - i] = (eilbrt >> (8 * i)) & 0xff;
421+
elbt[1] = 0;
422+
elbt[0] = 0;
423+
}
424+
425+
void nvme_init_copy_range(struct nvme_copy_range *copy, __u16 *nlbs,
426+
__u64 *slbas, __u32 *eilbrts, __u32 *elbatms,
427+
__u32 *elbats, __u16 nr)
428+
{
429+
int i;
430+
431+
for (i = 0; i < nr; i++) {
432+
copy[i].nlb = cpu_to_le16(nlbs[i]);
433+
copy[i].slba = cpu_to_le64(slbas[i]);
434+
copy[i].eilbrt = cpu_to_le32(eilbrts[i]);
435+
copy[i].elbatm = cpu_to_le16(elbatms[i]);
436+
copy[i].elbat = cpu_to_le16(elbats[i]);
437+
}
438+
}
439+
440+
void nvme_init_copy_range_f1(struct nvme_copy_range_f1 *copy, __u16 *nlbs,
441+
__u64 *slbas, __u64 *eilbrts, __u32 *elbatms,
442+
__u32 *elbats, __u16 nr)
443+
{
444+
int i;
445+
446+
for (i = 0; i < nr; i++) {
447+
copy[i].nlb = cpu_to_le16(nlbs[i]);
448+
copy[i].slba = cpu_to_le64(slbas[i]);
449+
copy[i].elbatm = cpu_to_le16(elbatms[i]);
450+
copy[i].elbat = cpu_to_le16(elbats[i]);
451+
nvme_init_copy_range_elbt(copy[i].elbt, eilbrts[i]);
452+
}
453+
}
454+
455+
void nvme_init_copy_range_f2(struct nvme_copy_range_f2 *copy, __u32 *snsids,
456+
__u16 *nlbs, __u64 *slbas, __u16 *sopts,
457+
__u32 *eilbrts, __u32 *elbatms, __u32 *elbats,
458+
__u16 nr)
459+
{
460+
int i;
461+
462+
for (i = 0; i < nr; i++) {
463+
copy[i].snsid = cpu_to_le32(snsids[i]);
464+
copy[i].nlb = cpu_to_le16(nlbs[i]);
465+
copy[i].slba = cpu_to_le64(slbas[i]);
466+
copy[i].sopt = cpu_to_le16(sopts[i]);
467+
copy[i].eilbrt = cpu_to_le32(eilbrts[i]);
468+
copy[i].elbatm = cpu_to_le16(elbatms[i]);
469+
copy[i].elbat = cpu_to_le16(elbats[i]);
470+
}
471+
}
472+
473+
void nvme_init_copy_range_f3(struct nvme_copy_range_f3 *copy, __u32 *snsids,
474+
__u16 *nlbs, __u64 *slbas, __u16 *sopts,
475+
__u64 *eilbrts, __u32 *elbatms, __u32 *elbats,
476+
__u16 nr)
477+
{
478+
int i;
479+
480+
for (i = 0; i < nr; i++) {
481+
copy[i].snsid = cpu_to_le32(snsids[i]);
482+
copy[i].nlb = cpu_to_le16(nlbs[i]);
483+
copy[i].slba = cpu_to_le64(slbas[i]);
484+
copy[i].sopt = cpu_to_le16(sopts[i]);
485+
copy[i].elbatm = cpu_to_le16(elbatms[i]);
486+
copy[i].elbat = cpu_to_le16(elbats[i]);
487+
nvme_init_copy_range_elbt(copy[i].elbt, eilbrts[i]);
488+
}
489+
}
490+
491+
void nvme_init_dsm_range(struct nvme_dsm_range *dsm, __u32 *ctx_attrs,
492+
__u32 *llbas, __u64 *slbas, __u16 nr_ranges)
493+
{
494+
int i;
495+
496+
for (i = 0; i < nr_ranges; i++) {
497+
dsm[i].cattr = cpu_to_le32(ctx_attrs[i]);
498+
dsm[i].nlb = cpu_to_le32(llbas[i]);
499+
dsm[i].slba = cpu_to_le64(slbas[i]);
500+
}
501+
}
502+
503+
void nvme_init_ctrl_list(struct nvme_ctrl_list *cntlist, __u16 num_ctrls,
504+
__u16 *ctrlist)
505+
{
506+
int i;
507+
508+
cntlist->num = cpu_to_le16(num_ctrls);
509+
for (i = 0; i < num_ctrls; i++)
510+
cntlist->identifier[i] = cpu_to_le16(ctrlist[i]);
511+
}
512+
513+
int nvme_get_feature_length(int fid, __u32 cdw11, enum nvme_data_tfr dir,
514+
__u32 *len)
515+
{
516+
switch (fid) {
517+
case NVME_FEAT_FID_LBA_RANGE:
518+
*len = sizeof(struct nvme_lba_range_type);
519+
break;
520+
case NVME_FEAT_FID_AUTO_PST:
521+
*len = sizeof(struct nvme_feat_auto_pst);
522+
break;
523+
case NVME_FEAT_FID_PLM_CONFIG:
524+
*len = sizeof(struct nvme_plm_config);
525+
break;
526+
case NVME_FEAT_FID_TIMESTAMP:
527+
*len = sizeof(struct nvme_timestamp);
528+
break;
529+
case NVME_FEAT_FID_HOST_BEHAVIOR:
530+
*len = sizeof(struct nvme_feat_host_behavior);
531+
break;
532+
case NVME_FEAT_FID_HOST_ID:
533+
*len = (cdw11 & 0x1) ? 16 : 8;
534+
break;
535+
case NVME_FEAT_FID_HOST_MEM_BUF:
536+
if (dir == NVME_DATA_TFR_HOST_TO_CTRL) {
537+
*len = 0;
538+
break;
539+
}
540+
*len = sizeof(struct nvme_host_mem_buf_attrs);
541+
break;
542+
case NVME_FEAT_FID_ARBITRATION:
543+
case NVME_FEAT_FID_POWER_MGMT:
544+
case NVME_FEAT_FID_TEMP_THRESH:
545+
case NVME_FEAT_FID_ERR_RECOVERY:
546+
case NVME_FEAT_FID_VOLATILE_WC:
547+
case NVME_FEAT_FID_NUM_QUEUES:
548+
case NVME_FEAT_FID_IRQ_COALESCE:
549+
case NVME_FEAT_FID_IRQ_CONFIG:
550+
case NVME_FEAT_FID_WRITE_ATOMIC:
551+
case NVME_FEAT_FID_ASYNC_EVENT:
552+
case NVME_FEAT_FID_KATO:
553+
case NVME_FEAT_FID_HCTM:
554+
case NVME_FEAT_FID_NOPSC:
555+
case NVME_FEAT_FID_RRL:
556+
case NVME_FEAT_FID_PLM_WINDOW:
557+
case NVME_FEAT_FID_LBA_STS_INTERVAL:
558+
case NVME_FEAT_FID_SANITIZE:
559+
case NVME_FEAT_FID_ENDURANCE_EVT_CFG:
560+
case NVME_FEAT_FID_SW_PROGRESS:
561+
case NVME_FEAT_FID_RESV_MASK:
562+
case NVME_FEAT_FID_RESV_PERSIST:
563+
case NVME_FEAT_FID_WRITE_PROTECT:
564+
case NVME_FEAT_FID_POWER_LIMIT:
565+
*len = 0;
566+
break;
567+
case NVME_FEAT_FID_ENH_CTRL_METADATA:
568+
case NVME_FEAT_FID_CTRL_METADATA:
569+
case NVME_FEAT_FID_NS_METADATA:
570+
*len = sizeof(struct nvme_host_metadata);
571+
break;
572+
case NVME_FEAT_FID_PERF_CHARACTERISTICS:
573+
*len = sizeof(struct nvme_perf_characteristics);
574+
break;
575+
case NVME_FEAT_FID_FDP_EVENTS:
576+
*len = NVME_FEAT_FDPE_NOET_MASK *
577+
sizeof(struct nvme_fdp_supported_event_desc);
578+
break;
579+
default:
580+
return -EINVAL;
581+
}
582+
return 0;
583+
}
584+
585+
int nvme_get_directive_receive_length(enum nvme_directive_dtype dtype,
586+
enum nvme_directive_receive_doper doper, __u32 *len)
587+
{
588+
switch (dtype) {
589+
case NVME_DIRECTIVE_DTYPE_IDENTIFY:
590+
switch (doper) {
591+
case NVME_DIRECTIVE_RECEIVE_IDENTIFY_DOPER_PARAM:
592+
*len = sizeof(struct nvme_id_directives);
593+
return 0;
594+
default:
595+
return -EINVAL;
596+
}
597+
case NVME_DIRECTIVE_DTYPE_STREAMS:
598+
switch (doper) {
599+
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_PARAM:
600+
*len = sizeof(struct nvme_streams_directive_params);
601+
return 0;
602+
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_STATUS:
603+
*len = (128 * 1024) * sizeof(__le16);
604+
return 0;
605+
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_RESOURCE:
606+
*len = 0;
607+
return 0;
608+
default:
609+
return -EINVAL;
610+
}
611+
default:
612+
return -EINVAL;
613+
}
614+
}

libnvme/src/nvme/cmds.h

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7115,3 +7115,123 @@ int nvme_namespace_attach_ctrls(struct nvme_transport_handle *hdl, bool ish,
71157115
*/
71167116
int nvme_namespace_detach_ctrls(struct nvme_transport_handle *hdl, bool ish,
71177117
__u32 nsid, __u16 num_ctrls, __u16 *ctrlist);
7118+
7119+
/**
7120+
* nvme_init_ctrl_list() - Initialize an nvme_ctrl_list structure from an array.
7121+
* @cntlist: The controller list structure to initialize
7122+
* @num_ctrls: The number of controllers in the array, &ctrlist.
7123+
* @ctrlist: An array of controller identifiers in CPU native endian.
7124+
*
7125+
* This is intended to be used with any command that takes a controller list
7126+
* argument. See nvme_ns_attach_ctrls() and nvme_ns_detach().
7127+
*/
7128+
void nvme_init_ctrl_list(struct nvme_ctrl_list *cntlist, __u16 num_ctrls,
7129+
__u16 *ctrlist);
7130+
7131+
/**
7132+
* nvme_init_dsm_range() - Constructs a data set range structure
7133+
* @dsm: DSM range array
7134+
* @ctx_attrs: Array of context attributes
7135+
* @llbas: Array of length in logical blocks
7136+
* @slbas: Array of starting logical blocks
7137+
* @nr_ranges: The size of the dsm arrays
7138+
*
7139+
* Each array must be the same size of size 'nr_ranges'. This is intended to be
7140+
* used with constructing a payload for nvme_dsm().
7141+
*
7142+
* Return: The nvme command status if a response was received or -errno
7143+
* otherwise.
7144+
*/
7145+
void nvme_init_dsm_range(struct nvme_dsm_range *dsm, __u32 *ctx_attrs,
7146+
__u32 *llbas, __u64 *slbas, __u16 nr_ranges);
7147+
7148+
/**
7149+
* nvme_init_copy_range() - Constructs a copy range structure
7150+
* @copy: Copy range array
7151+
* @nlbs: Number of logical blocks
7152+
* @slbas: Starting LBA
7153+
* @eilbrts: Expected initial logical block reference tag
7154+
* @elbatms: Expected logical block application tag mask
7155+
* @elbats: Expected logical block application tag
7156+
* @nr: Number of descriptors to construct
7157+
*/
7158+
void nvme_init_copy_range(struct nvme_copy_range *copy, __u16 *nlbs,
7159+
__u64 *slbas, __u32 *eilbrts, __u32 *elbatms,
7160+
__u32 *elbats, __u16 nr);
7161+
7162+
/**
7163+
* nvme_init_copy_range_f1() - Constructs a copy range f1 structure
7164+
* @copy: Copy range array
7165+
* @nlbs: Number of logical blocks
7166+
* @slbas: Starting LBA
7167+
* @eilbrts: Expected initial logical block reference tag
7168+
* @elbatms: Expected logical block application tag mask
7169+
* @elbats: Expected logical block application tag
7170+
* @nr: Number of descriptors to construct
7171+
*/
7172+
void nvme_init_copy_range_f1(struct nvme_copy_range_f1 *copy, __u16 *nlbs,
7173+
__u64 *slbas, __u64 *eilbrts, __u32 *elbatms,
7174+
__u32 *elbats, __u16 nr);
7175+
7176+
/**
7177+
* nvme_init_copy_range_f2() - Constructs a copy range f2 structure
7178+
* @copy: Copy range array
7179+
* @snsids: Source namespace identifier
7180+
* @nlbs: Number of logical blocks
7181+
* @slbas: Starting LBA
7182+
* @sopts: Source options
7183+
* @eilbrts: Expected initial logical block reference tag
7184+
* @elbatms: Expected logical block application tag mask
7185+
* @elbats: Expected logical block application tag
7186+
* @nr: Number of descriptors to construct
7187+
*/
7188+
void nvme_init_copy_range_f2(struct nvme_copy_range_f2 *copy, __u32 *snsids,
7189+
__u16 *nlbs, __u64 *slbas, __u16 *sopts,
7190+
__u32 *eilbrts, __u32 *elbatms, __u32 *elbats,
7191+
__u16 nr);
7192+
7193+
/**
7194+
* nvme_init_copy_range_f3() - Constructs a copy range f3 structure
7195+
* @copy: Copy range array
7196+
* @snsids: Source namespace identifier
7197+
* @nlbs: Number of logical blocks
7198+
* @slbas: Starting LBA
7199+
* @sopts: Source options
7200+
* @eilbrts: Expected initial logical block reference tag
7201+
* @elbatms: Expected logical block application tag mask
7202+
* @elbats: Expected logical block application tag
7203+
* @nr: Number of descriptors to construct
7204+
*/
7205+
void nvme_init_copy_range_f3(struct nvme_copy_range_f3 *copy, __u32 *snsids,
7206+
__u16 *nlbs, __u64 *slbas, __u16 *sopts,
7207+
__u64 *eilbrts, __u32 *elbatms, __u32 *elbats,
7208+
__u16 nr);
7209+
7210+
/**
7211+
* nvme_get_feature_length() - Retrieve the command payload length for a
7212+
* specific feature identifier
7213+
* @fid: Feature identifier, see &enum nvme_features_id.
7214+
* @cdw11: The cdw11 value may affect the transfer (only known fid is
7215+
* %NVME_FEAT_FID_HOST_ID)
7216+
* @dir: Data transfer direction: false - host to controller, true -
7217+
* controller to host may affect the transfer (only known fid is
7218+
* %NVME_FEAT_FID_HOST_MEM_BUF).
7219+
* @len: On success, set to this features payload length in bytes.
7220+
*
7221+
* Return: 0 on success, -1 with errno set to EINVAL if the function did not
7222+
* recognize &fid.
7223+
*/
7224+
int nvme_get_feature_length(int fid, __u32 cdw11, enum nvme_data_tfr dir,
7225+
__u32 *len);
7226+
7227+
/**
7228+
* nvme_get_directive_receive_length() - Get directive receive length
7229+
* @dtype: Directive type, see &enum nvme_directive_dtype
7230+
* @doper: Directive receive operation, see &enum nvme_directive_receive_doper
7231+
* @len: On success, set to this directives payload length in bytes.
7232+
*
7233+
* Return: 0 on success, -1 with errno set to EINVAL if the function did not
7234+
* recognize &dtype or &doper.
7235+
*/
7236+
int nvme_get_directive_receive_length(enum nvme_directive_dtype dtype,
7237+
enum nvme_directive_receive_doper doper, __u32 *len);

0 commit comments

Comments
 (0)