git: e932f0d2a3c3 - main - cam_xpt: Properly fail if a sim uses an unsupported transport.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 27 Jun 2023 03:38:30 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=e932f0d2a3c3ccbdf6c72745a75488022662f80c

commit e932f0d2a3c3ccbdf6c72745a75488022662f80c
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-06-27 03:33:25 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-06-27 03:36:06 +0000

    cam_xpt: Properly fail if a sim uses an unsupported transport.
    
    The default xport ops for a new bus is xport_default, not NULL, so
    check for that when determining if a bus failed to find a suitable
    transport.  In addition, the path needs to be freed with xpt_free_path
    instead of a plain free so that the path's reference on the sim is
    dropped; otherwise, cam_sim_free in the caller after xpt_bus_register
    returns failure will hang forever.
    
    Note that we have to exempt the xpt bus from this check as it uses
    xport_default on purpose.
    
    Reviewed by:    mav, imp
    Sponsored by:   Chelsio Communications
    Differential Revision:  https://reviews.freebsd.org/D40617
---
 sys/cam/cam_xpt.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/sys/cam/cam_xpt.c b/sys/cam/cam_xpt.c
index 7bca42cb553e..9eb42a8f9141 100644
--- a/sys/cam/cam_xpt.c
+++ b/sys/cam/cam_xpt.c
@@ -4018,7 +4018,14 @@ xpt_bus_register(struct cam_sim *sim, device_t parent, uint32_t bus)
 
 	xpt_path_inq(&cpi, path);
 
-	if (cam_ccb_success((union ccb *)&cpi)) {
+	/*
+	 * Use the results of PATH_INQ to pick a transport.  Note that
+	 * the xpt bus (which uses XPORT_UNSPECIFIED) always uses
+	 * xport_default instead of a transport from
+	 * cam_xpt_port_set.
+	 */
+	if (cam_ccb_success((union ccb *)&cpi) &&
+	    cpi.transport != XPORT_UNSPECIFIED) {
 		struct xpt_xport **xpt;
 
 		SET_FOREACH(xpt, cam_xpt_xport_set) {
@@ -4027,11 +4034,11 @@ xpt_bus_register(struct cam_sim *sim, device_t parent, uint32_t bus)
 				break;
 			}
 		}
-		if (new_bus->xport == NULL) {
+		if (new_bus->xport == &xport_default) {
 			xpt_print(path,
 			    "No transport found for %d\n", cpi.transport);
 			xpt_release_bus(new_bus);
-			free(path, M_CAMXPT);
+			xpt_free_path(path);
 			return (EINVAL);
 		}
 	}