Skip to content

Commit c6308c3

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 7152f5c commit c6308c3

5 files changed

Lines changed: 616 additions & 622 deletions

File tree

libnvme/src/nvme/cmds.c

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

241-
242241
static int nvme_check_get_telemetry_log(struct nvme_transport_handle *hdl,
243242
bool create, bool ctrl, bool rae,
244243
struct nvme_telemetry_log **log, enum nvme_telemetry_da da,
@@ -409,3 +408,203 @@ int nvme_get_logical_block_size(struct nvme_transport_handle *hdl,
409408

410409

411410

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

libnvme/src/nvme/cmds.h

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

0 commit comments

Comments
 (0)