Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

nvmet: use type-name map for address treq

Currently nvmet_addr_treq_[store|show]() uses switch and if else
ladder for address transport requirements to string and reverse
mapping. With addtion of the generic nvmet_type_name_map structure
we can get rid of the switch and if else ladder with string
duplication.

Signed-off-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Chaitanya Kulkarni and committed by
Jens Axboe
87628e28 84b8d0d7

+26 -22
+26 -22
drivers/nvme/target/configfs.c
··· 147 147 148 148 CONFIGFS_ATTR(nvmet_, addr_traddr); 149 149 150 - static ssize_t nvmet_addr_treq_show(struct config_item *item, 151 - char *page) 150 + static const struct nvmet_type_name_map nvmet_addr_treq[] = { 151 + { NVMF_TREQ_NOT_SPECIFIED, "not specified" }, 152 + { NVMF_TREQ_REQUIRED, "required" }, 153 + { NVMF_TREQ_NOT_REQUIRED, "not required" }, 154 + }; 155 + 156 + static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page) 152 157 { 153 - switch (to_nvmet_port(item)->disc_addr.treq & 154 - NVME_TREQ_SECURE_CHANNEL_MASK) { 155 - case NVMF_TREQ_NOT_SPECIFIED: 156 - return sprintf(page, "not specified\n"); 157 - case NVMF_TREQ_REQUIRED: 158 - return sprintf(page, "required\n"); 159 - case NVMF_TREQ_NOT_REQUIRED: 160 - return sprintf(page, "not required\n"); 161 - default: 162 - return sprintf(page, "\n"); 158 + u8 treq = to_nvmet_port(item)->disc_addr.treq & 159 + NVME_TREQ_SECURE_CHANNEL_MASK; 160 + int i; 161 + 162 + for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) { 163 + if (treq == nvmet_addr_treq[i].type) 164 + return sprintf(page, "%s\n", nvmet_addr_treq[i].name); 163 165 } 166 + 167 + return sprintf(page, "\n"); 164 168 } 165 169 166 170 static ssize_t nvmet_addr_treq_store(struct config_item *item, ··· 172 168 { 173 169 struct nvmet_port *port = to_nvmet_port(item); 174 170 u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK; 171 + int i; 175 172 176 173 if (port->enabled) { 177 174 pr_err("Cannot modify address while enabled\n"); ··· 180 175 return -EACCES; 181 176 } 182 177 183 - if (sysfs_streq(page, "not specified")) { 184 - treq |= NVMF_TREQ_NOT_SPECIFIED; 185 - } else if (sysfs_streq(page, "required")) { 186 - treq |= NVMF_TREQ_REQUIRED; 187 - } else if (sysfs_streq(page, "not required")) { 188 - treq |= NVMF_TREQ_NOT_REQUIRED; 189 - } else { 190 - pr_err("Invalid value '%s' for treq\n", page); 191 - return -EINVAL; 178 + for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) { 179 + if (sysfs_streq(page, nvmet_addr_treq[i].name)) 180 + goto found; 192 181 } 193 - port->disc_addr.treq = treq; 194 182 183 + pr_err("Invalid value '%s' for treq\n", page); 184 + return -EINVAL; 185 + 186 + found: 187 + treq |= nvmet_addr_treq[i].type; 188 + port->disc_addr.treq = treq; 195 189 return count; 196 190 } 197 191