@@ -110,7 +110,7 @@ struct workqueue_struct *nvme_delete_wq;
110110EXPORT_SYMBOL_GPL (nvme_delete_wq );
111111
112112static LIST_HEAD (nvme_subsystems );
113- static DEFINE_MUTEX (nvme_subsystems_lock );
113+ DEFINE_MUTEX (nvme_subsystems_lock );
114114
115115static DEFINE_IDA (nvme_instance_ida );
116116static dev_t nvme_ctrl_base_chr_devt ;
@@ -261,7 +261,7 @@ void nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
261261
262262static blk_status_t nvme_error_status (u16 status )
263263{
264- switch (status & 0x7ff ) {
264+ switch (status & NVME_SCT_SC_MASK ) {
265265 case NVME_SC_SUCCESS :
266266 return BLK_STS_OK ;
267267 case NVME_SC_CAP_EXCEEDED :
@@ -307,7 +307,7 @@ static void nvme_retry_req(struct request *req)
307307 u16 crd ;
308308
309309 /* The mask and shift result must be <= 3 */
310- crd = (nvme_req (req )-> status & NVME_SC_CRD ) >> 11 ;
310+ crd = (nvme_req (req )-> status & NVME_STATUS_CRD ) >> 11 ;
311311 if (crd )
312312 delay = nvme_req (req )-> ctrl -> crdt [crd - 1 ] * 100 ;
313313
@@ -329,10 +329,10 @@ static void nvme_log_error(struct request *req)
329329 nvme_sect_to_lba (ns -> head , blk_rq_pos (req )),
330330 blk_rq_bytes (req ) >> ns -> head -> lba_shift ,
331331 nvme_get_error_status_str (nr -> status ),
332- nr -> status >> 8 & 7 , /* Status Code Type */
333- nr -> status & 0xff , /* Status Code */
334- nr -> status & NVME_SC_MORE ? "MORE " : "" ,
335- nr -> status & NVME_SC_DNR ? "DNR " : "" );
332+ NVME_SCT ( nr -> status ), /* Status Code Type */
333+ nr -> status & NVME_SC_MASK , /* Status Code */
334+ nr -> status & NVME_STATUS_MORE ? "MORE " : "" ,
335+ nr -> status & NVME_STATUS_DNR ? "DNR " : "" );
336336 return ;
337337 }
338338
@@ -341,10 +341,10 @@ static void nvme_log_error(struct request *req)
341341 nvme_get_admin_opcode_str (nr -> cmd -> common .opcode ),
342342 nr -> cmd -> common .opcode ,
343343 nvme_get_error_status_str (nr -> status ),
344- nr -> status >> 8 & 7 , /* Status Code Type */
345- nr -> status & 0xff , /* Status Code */
346- nr -> status & NVME_SC_MORE ? "MORE " : "" ,
347- nr -> status & NVME_SC_DNR ? "DNR " : "" );
344+ NVME_SCT ( nr -> status ) , /* Status Code Type */
345+ nr -> status & NVME_SC_MASK , /* Status Code */
346+ nr -> status & NVME_STATUS_MORE ? "MORE " : "" ,
347+ nr -> status & NVME_STATUS_DNR ? "DNR " : "" );
348348}
349349
350350static void nvme_log_err_passthru (struct request * req )
@@ -359,10 +359,10 @@ static void nvme_log_err_passthru(struct request *req)
359359 nvme_get_admin_opcode_str (nr -> cmd -> common .opcode ),
360360 nr -> cmd -> common .opcode ,
361361 nvme_get_error_status_str (nr -> status ),
362- nr -> status >> 8 & 7 , /* Status Code Type */
363- nr -> status & 0xff , /* Status Code */
364- nr -> status & NVME_SC_MORE ? "MORE " : "" ,
365- nr -> status & NVME_SC_DNR ? "DNR " : "" ,
362+ NVME_SCT ( nr -> status ), /* Status Code Type */
363+ nr -> status & NVME_SC_MASK , /* Status Code */
364+ nr -> status & NVME_STATUS_MORE ? "MORE " : "" ,
365+ nr -> status & NVME_STATUS_DNR ? "DNR " : "" ,
366366 nr -> cmd -> common .cdw10 ,
367367 nr -> cmd -> common .cdw11 ,
368368 nr -> cmd -> common .cdw12 ,
@@ -384,11 +384,11 @@ static inline enum nvme_disposition nvme_decide_disposition(struct request *req)
384384 return COMPLETE ;
385385
386386 if (blk_noretry_request (req ) ||
387- (nvme_req (req )-> status & NVME_SC_DNR ) ||
387+ (nvme_req (req )-> status & NVME_STATUS_DNR ) ||
388388 nvme_req (req )-> retries >= nvme_max_retries )
389389 return COMPLETE ;
390390
391- if ((nvme_req (req )-> status & 0x7ff ) == NVME_SC_AUTH_REQUIRED )
391+ if ((nvme_req (req )-> status & NVME_SCT_SC_MASK ) == NVME_SC_AUTH_REQUIRED )
392392 return AUTHENTICATE ;
393393
394394 if (req -> cmd_flags & REQ_NVME_MPATH ) {
@@ -1257,7 +1257,7 @@ EXPORT_SYMBOL_NS_GPL(nvme_passthru_end, NVME_TARGET_PASSTHRU);
12571257
12581258/*
12591259 * Recommended frequency for KATO commands per NVMe 1.4 section 7.12.1:
1260- *
1260+ *
12611261 * The host should send Keep Alive commands at half of the Keep Alive Timeout
12621262 * accounting for transport roundtrip times [..].
12631263 */
@@ -2287,6 +2287,32 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
22872287 return ret ;
22882288}
22892289
2290+ int nvme_ns_get_unique_id (struct nvme_ns * ns , u8 id [16 ],
2291+ enum blk_unique_id type )
2292+ {
2293+ struct nvme_ns_ids * ids = & ns -> head -> ids ;
2294+
2295+ if (type != BLK_UID_EUI64 )
2296+ return - EINVAL ;
2297+
2298+ if (memchr_inv (ids -> nguid , 0 , sizeof (ids -> nguid ))) {
2299+ memcpy (id , & ids -> nguid , sizeof (ids -> nguid ));
2300+ return sizeof (ids -> nguid );
2301+ }
2302+ if (memchr_inv (ids -> eui64 , 0 , sizeof (ids -> eui64 ))) {
2303+ memcpy (id , & ids -> eui64 , sizeof (ids -> eui64 ));
2304+ return sizeof (ids -> eui64 );
2305+ }
2306+
2307+ return - EINVAL ;
2308+ }
2309+
2310+ static int nvme_get_unique_id (struct gendisk * disk , u8 id [16 ],
2311+ enum blk_unique_id type )
2312+ {
2313+ return nvme_ns_get_unique_id (disk -> private_data , id , type );
2314+ }
2315+
22902316#ifdef CONFIG_BLK_SED_OPAL
22912317static int nvme_sec_submit (void * data , u16 spsp , u8 secp , void * buffer , size_t len ,
22922318 bool send )
@@ -2342,6 +2368,7 @@ const struct block_device_operations nvme_bdev_ops = {
23422368 .open = nvme_open ,
23432369 .release = nvme_release ,
23442370 .getgeo = nvme_getgeo ,
2371+ .get_unique_id = nvme_get_unique_id ,
23452372 .report_zones = nvme_report_zones ,
23462373 .pr_ops = & nvme_pr_ops ,
23472374};
@@ -3942,7 +3969,7 @@ static void nvme_ns_remove_by_nsid(struct nvme_ctrl *ctrl, u32 nsid)
39423969
39433970static void nvme_validate_ns (struct nvme_ns * ns , struct nvme_ns_info * info )
39443971{
3945- int ret = NVME_SC_INVALID_NS | NVME_SC_DNR ;
3972+ int ret = NVME_SC_INVALID_NS | NVME_STATUS_DNR ;
39463973
39473974 if (!nvme_ns_ids_equal (& ns -> head -> ids , & info -> ids )) {
39483975 dev_err (ns -> ctrl -> device ,
@@ -3958,7 +3985,7 @@ static void nvme_validate_ns(struct nvme_ns *ns, struct nvme_ns_info *info)
39583985 *
39593986 * TODO: we should probably schedule a delayed retry here.
39603987 */
3961- if (ret > 0 && (ret & NVME_SC_DNR ))
3988+ if (ret > 0 && (ret & NVME_STATUS_DNR ))
39623989 nvme_ns_remove (ns );
39633990}
39643991
@@ -4150,7 +4177,7 @@ static void nvme_scan_work(struct work_struct *work)
41504177 * they report) but don't actually support it.
41514178 */
41524179 ret = nvme_scan_ns_list (ctrl );
4153- if (ret > 0 && ret & NVME_SC_DNR )
4180+ if (ret > 0 && ret & NVME_STATUS_DNR )
41544181 nvme_scan_ns_sequential (ctrl );
41554182 }
41564183 mutex_unlock (& ctrl -> scan_lock );
@@ -4670,6 +4697,9 @@ static void nvme_free_ctrl(struct device *dev)
46704697 * Initialize a NVMe controller structures. This needs to be called during
46714698 * earliest initialization so that we have the initialized structured around
46724699 * during probing.
4700+ *
4701+ * On success, the caller must use the nvme_put_ctrl() to release this when
4702+ * needed, which also invokes the ops->free_ctrl() callback.
46734703 */
46744704int nvme_init_ctrl (struct nvme_ctrl * ctrl , struct device * dev ,
46754705 const struct nvme_ctrl_ops * ops , unsigned long quirks )
@@ -4718,6 +4748,12 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
47184748 goto out ;
47194749 ctrl -> instance = ret ;
47204750
4751+ ret = nvme_auth_init_ctrl (ctrl );
4752+ if (ret )
4753+ goto out_release_instance ;
4754+
4755+ nvme_mpath_init_ctrl (ctrl );
4756+
47214757 device_initialize (& ctrl -> ctrl_device );
47224758 ctrl -> device = & ctrl -> ctrl_device ;
47234759 ctrl -> device -> devt = MKDEV (MAJOR (nvme_ctrl_base_chr_devt ),
@@ -4730,16 +4766,36 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
47304766 ctrl -> device -> groups = nvme_dev_attr_groups ;
47314767 ctrl -> device -> release = nvme_free_ctrl ;
47324768 dev_set_drvdata (ctrl -> device , ctrl );
4769+
4770+ return ret ;
4771+
4772+ out_release_instance :
4773+ ida_free (& nvme_instance_ida , ctrl -> instance );
4774+ out :
4775+ if (ctrl -> discard_page )
4776+ __free_page (ctrl -> discard_page );
4777+ cleanup_srcu_struct (& ctrl -> srcu );
4778+ return ret ;
4779+ }
4780+ EXPORT_SYMBOL_GPL (nvme_init_ctrl );
4781+
4782+ /*
4783+ * On success, returns with an elevated controller reference and caller must
4784+ * use nvme_uninit_ctrl() to properly free resources associated with the ctrl.
4785+ */
4786+ int nvme_add_ctrl (struct nvme_ctrl * ctrl )
4787+ {
4788+ int ret ;
4789+
47334790 ret = dev_set_name (ctrl -> device , "nvme%d" , ctrl -> instance );
47344791 if (ret )
4735- goto out_release_instance ;
4792+ return ret ;
47364793
4737- nvme_get_ctrl (ctrl );
47384794 cdev_init (& ctrl -> cdev , & nvme_dev_fops );
4739- ctrl -> cdev .owner = ops -> module ;
4795+ ctrl -> cdev .owner = ctrl -> ops -> module ;
47404796 ret = cdev_device_add (& ctrl -> cdev , ctrl -> device );
47414797 if (ret )
4742- goto out_free_name ;
4798+ return ret ;
47434799
47444800 /*
47454801 * Initialize latency tolerance controls. The sysfs files won't
@@ -4750,28 +4806,11 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
47504806 min (default_ps_max_latency_us , (unsigned long )S32_MAX ));
47514807
47524808 nvme_fault_inject_init (& ctrl -> fault_inject , dev_name (ctrl -> device ));
4753- nvme_mpath_init_ctrl (ctrl );
4754- ret = nvme_auth_init_ctrl (ctrl );
4755- if (ret )
4756- goto out_free_cdev ;
4809+ nvme_get_ctrl (ctrl );
47574810
47584811 return 0 ;
4759- out_free_cdev :
4760- nvme_fault_inject_fini (& ctrl -> fault_inject );
4761- dev_pm_qos_hide_latency_tolerance (ctrl -> device );
4762- cdev_device_del (& ctrl -> cdev , ctrl -> device );
4763- out_free_name :
4764- nvme_put_ctrl (ctrl );
4765- kfree_const (ctrl -> device -> kobj .name );
4766- out_release_instance :
4767- ida_free (& nvme_instance_ida , ctrl -> instance );
4768- out :
4769- if (ctrl -> discard_page )
4770- __free_page (ctrl -> discard_page );
4771- cleanup_srcu_struct (& ctrl -> srcu );
4772- return ret ;
47734812}
4774- EXPORT_SYMBOL_GPL (nvme_init_ctrl );
4813+ EXPORT_SYMBOL_GPL (nvme_add_ctrl );
47754814
47764815/* let I/O to all namespaces fail in preparation for surprise removal */
47774816void nvme_mark_namespaces_dead (struct nvme_ctrl * ctrl )
0 commit comments