2019-05-30 02:57:35 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2009-07-14 03:01:31 +04:00
/*
* Copyright ( c ) 2009 , Microsoft Corporation .
*
* Authors :
* Haiyang Zhang < haiyangz @ microsoft . com >
* Hank Janssen < hjanssen @ microsoft . com >
2011-05-10 18:54:19 +04:00
* K . Y . Srinivasan < kys @ microsoft . com >
2009-07-14 03:01:31 +04:00
*/
2011-08-27 22:31:23 +04:00
# include <linux/kernel.h>
2011-08-27 22:31:24 +04:00
# include <linux/wait.h>
2011-08-27 22:31:23 +04:00
# include <linux/sched.h>
# include <linux/completion.h>
# include <linux/string.h>
# include <linux/mm.h>
# include <linux/delay.h>
2009-07-14 03:01:31 +04:00
# include <linux/init.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2009-07-14 03:01:31 +04:00
# include <linux/module.h>
# include <linux/device.h>
2011-10-04 23:29:52 +04:00
# include <linux/hyperv.h>
2014-07-12 20:48:30 +04:00
# include <linux/blkdev.h>
2021-12-13 10:14:05 +03:00
# include <linux/dma-mapping.h>
2009-07-14 03:01:31 +04:00
# include <scsi/scsi.h>
# include <scsi/scsi_cmnd.h>
# include <scsi/scsi_host.h>
# include <scsi/scsi_device.h>
# include <scsi/scsi_tcq.h>
# include <scsi/scsi_eh.h>
# include <scsi/scsi_devinfo.h>
# include <scsi/scsi_dbg.h>
2015-12-24 00:15:49 +03:00
# include <scsi/scsi_transport_fc.h>
2016-01-28 05:22:44 +03:00
# include <scsi/scsi_transport.h>
2011-05-13 06:34:15 +04:00
2012-01-13 00:37:54 +04:00
/*
2012-01-13 00:38:08 +04:00
* All wire protocol details ( storage protocol between the guest and the host )
* are consolidated here .
*
* Begin protocol definitions .
2012-01-13 00:37:54 +04:00
*/
/*
* Version history :
* V1 Beta : 0.1
* V1 RC < 2008 / 1 / 31 : 1.0
* V1 RC > 2008 / 1 / 31 : 2.0
* Win7 : 4.2
2013-06-04 23:05:06 +04:00
* Win8 : 5.1
2015-08-13 18:43:48 +03:00
* Win8 .1 : 6.0
* Win10 : 6.2
2012-01-13 00:37:54 +04:00
*/
2015-08-13 18:43:47 +03:00
# define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
( ( ( MINOR_ ) & 0xff ) ) )
2015-08-13 18:43:48 +03:00
# define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
2015-08-13 18:43:47 +03:00
# define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
# define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
2015-08-13 18:43:48 +03:00
# define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
# define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
2011-08-27 22:31:24 +04:00
2022-07-25 12:37:28 +03:00
/* channel callback timeout in ms */
# define CALLBACK_TIMEOUT 2
2011-08-27 22:31:24 +04:00
/* Packet structure describing virtual storage requests. */
enum vstor_packet_operation {
VSTOR_OPERATION_COMPLETE_IO = 1 ,
VSTOR_OPERATION_REMOVE_DEVICE = 2 ,
VSTOR_OPERATION_EXECUTE_SRB = 3 ,
VSTOR_OPERATION_RESET_LUN = 4 ,
VSTOR_OPERATION_RESET_ADAPTER = 5 ,
VSTOR_OPERATION_RESET_BUS = 6 ,
VSTOR_OPERATION_BEGIN_INITIALIZATION = 7 ,
VSTOR_OPERATION_END_INITIALIZATION = 8 ,
VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9 ,
VSTOR_OPERATION_QUERY_PROPERTIES = 10 ,
2011-11-08 21:01:48 +04:00
VSTOR_OPERATION_ENUMERATE_BUS = 11 ,
2013-06-04 23:05:06 +04:00
VSTOR_OPERATION_FCHBA_DATA = 12 ,
VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13 ,
VSTOR_OPERATION_MAXIMUM = 13
} ;
/*
* WWN packet for Fibre Channel HBA
*/
struct hv_fc_wwn_packet {
2015-12-24 00:15:48 +03:00
u8 primary_active ;
u8 reserved1 [ 3 ] ;
2013-06-04 23:05:06 +04:00
u8 primary_port_wwn [ 8 ] ;
u8 primary_node_wwn [ 8 ] ;
u8 secondary_port_wwn [ 8 ] ;
u8 secondary_node_wwn [ 8 ] ;
2011-08-27 22:31:24 +04:00
} ;
2013-06-04 23:05:06 +04:00
/*
* SRB Flag Bits
*/
# define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
# define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
# define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
# define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
# define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
# define SRB_FLAGS_DATA_IN 0x00000040
# define SRB_FLAGS_DATA_OUT 0x00000080
# define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
# define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
# define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
# define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
# define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
/*
* This flag indicates the request is part of the workflow for processing a D3 .
*/
# define SRB_FLAGS_D3_PROCESSING 0x00000800
# define SRB_FLAGS_IS_ACTIVE 0x00010000
# define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
# define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
# define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
# define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
# define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
# define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
# define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
# define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
# define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
2016-12-15 05:46:01 +03:00
# define SP_UNTAGGED ((unsigned char) ~0)
# define SRB_SIMPLE_TAG_REQUEST 0x20
2013-06-04 23:05:06 +04:00
2011-08-27 22:31:24 +04:00
/*
* Platform neutral description of a scsi request -
* this remains the same across the write regardless of 32 / 64 bit
* note : it ' s patterned off the SCSI_PASS_THROUGH structure
*/
2012-01-13 00:38:05 +04:00
# define STORVSC_MAX_CMD_LEN 0x10
2013-06-04 23:05:06 +04:00
2022-05-25 14:27:02 +03:00
/* Sense buffer size is the same for all versions since Windows 8 */
2013-06-04 23:05:06 +04:00
# define STORVSC_SENSE_BUFFER_SIZE 0x14
2012-01-13 00:38:05 +04:00
# define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
2011-08-27 22:31:24 +04:00
2013-06-04 23:05:06 +04:00
/*
2015-08-13 18:43:49 +03:00
* The storage protocol version is determined during the
* initial exchange with the host . It will indicate which
* storage functionality is available in the host .
*/
2015-08-13 18:43:47 +03:00
static int vmstor_proto_version ;
2013-06-04 23:05:06 +04:00
2015-12-04 11:07:24 +03:00
# define STORVSC_LOGGING_NONE 0
# define STORVSC_LOGGING_ERROR 1
# define STORVSC_LOGGING_WARN 2
static int logging_level = STORVSC_LOGGING_ERROR ;
module_param ( logging_level , int , S_IRUGO | S_IWUSR ) ;
MODULE_PARM_DESC ( logging_level ,
" Logging level, 0 - None, 1 - Error (default), 2 - Warning. " ) ;
static inline bool do_logging ( int level )
{
return logging_level > = level ;
}
# define storvsc_log(dev, level, fmt, ...) \
do { \
if ( do_logging ( level ) ) \
dev_warn ( & ( dev ) - > device , fmt , # # __VA_ARGS__ ) ; \
} while ( 0 )
2011-08-27 22:31:24 +04:00
struct vmscsi_request {
2012-01-13 00:38:03 +04:00
u16 length ;
u8 srb_status ;
u8 scsi_status ;
2011-08-27 22:31:24 +04:00
2012-01-13 00:38:03 +04:00
u8 port_number ;
u8 path_id ;
u8 target_id ;
u8 lun ;
2011-08-27 22:31:24 +04:00
2012-01-13 00:38:03 +04:00
u8 cdb_length ;
u8 sense_info_length ;
u8 data_in ;
u8 reserved ;
2011-08-27 22:31:24 +04:00
2012-01-13 00:38:03 +04:00
u32 data_transfer_length ;
2011-08-27 22:31:24 +04:00
union {
2012-01-13 00:38:05 +04:00
u8 cdb [ STORVSC_MAX_CMD_LEN ] ;
u8 sense_data [ STORVSC_SENSE_BUFFER_SIZE ] ;
u8 reserved_array [ STORVSC_MAX_BUF_LEN_WITH_PADDING ] ;
2011-08-27 22:31:24 +04:00
} ;
2013-06-04 23:05:06 +04:00
/*
* The following was added in win8 .
*/
2022-05-25 14:27:02 +03:00
u16 reserve ;
u8 queue_tag ;
u8 queue_action ;
u32 srb_flags ;
u32 time_out_value ;
u32 queue_sort_ey ;
2013-06-04 23:05:06 +04:00
2011-08-27 22:31:24 +04:00
} __attribute ( ( packed ) ) ;
2015-08-13 18:43:48 +03:00
/*
2022-05-25 14:27:02 +03:00
* The list of windows version in order of preference .
2015-08-13 18:43:48 +03:00
*/
2022-05-25 14:27:02 +03:00
static const int protocol_version [ ] = {
2015-08-13 18:43:48 +03:00
VMSTOR_PROTO_VERSION_WIN10 ,
VMSTOR_PROTO_VERSION_WIN8_1 ,
VMSTOR_PROTO_VERSION_WIN8 ,
} ;
2011-08-27 22:31:24 +04:00
/*
2017-02-28 01:29:20 +03:00
* This structure is sent during the initialization phase to get the different
2011-08-27 22:31:24 +04:00
* properties of the channel .
*/
2013-06-04 23:05:06 +04:00
# define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
2011-08-27 22:31:24 +04:00
struct vmstorage_channel_properties {
2013-06-04 23:05:06 +04:00
u32 reserved ;
u16 max_channel_cnt ;
u16 reserved1 ;
2011-08-27 22:31:24 +04:00
2013-06-04 23:05:06 +04:00
u32 flags ;
2012-01-13 00:38:03 +04:00
u32 max_transfer_bytes ;
2011-08-27 22:31:24 +04:00
2013-06-04 23:05:06 +04:00
u64 reserved2 ;
2011-08-27 22:31:24 +04:00
} __packed ;
/* This structure is sent during the storage protocol negotiations. */
struct vmstorage_protocol_version {
/* Major (MSW) and minor (LSW) version numbers. */
2012-01-13 00:38:04 +04:00
u16 major_minor ;
2011-08-27 22:31:24 +04:00
/*
* Revision number is auto - incremented whenever this file is changed
* ( See FILL_VMSTOR_REVISION macro above ) . Mismatch does not
* definitely indicate incompatibility - - but it does indicate mismatched
* builds .
2012-01-13 00:38:03 +04:00
* This is only used on the windows side . Just set it to 0.
2011-08-27 22:31:24 +04:00
*/
2012-01-13 00:38:04 +04:00
u16 revision ;
2011-08-27 22:31:24 +04:00
} __packed ;
/* Channel Property Flags */
# define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
# define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
struct vstor_packet {
/* Requested operation type */
enum vstor_packet_operation operation ;
/* Flags - see below for values */
2012-01-13 00:38:03 +04:00
u32 flags ;
2011-08-27 22:31:24 +04:00
/* Status of the request returned from the server side. */
2012-01-13 00:38:03 +04:00
u32 status ;
2011-08-27 22:31:24 +04:00
/* Data payload area */
union {
/*
* Structure used to forward SCSI commands from the
* client to the server .
*/
struct vmscsi_request vm_srb ;
/* Structure used to query channel properties. */
struct vmstorage_channel_properties storage_channel_properties ;
/* Used during version negotiations. */
struct vmstorage_protocol_version version ;
2013-06-04 23:05:06 +04:00
/* Fibre channel address packet */
struct hv_fc_wwn_packet wwn_packet ;
/* Number of sub-channels to create */
u16 sub_channel_count ;
/* This will be the maximum of the union members */
u8 buffer [ 0x34 ] ;
2011-08-27 22:31:24 +04:00
} ;
} __packed ;
/*
2012-01-13 00:37:54 +04:00
* Packet Flags :
*
2011-08-27 22:31:24 +04:00
* This flag indicates that the server should send back a completion for this
* packet .
*/
2012-01-13 00:37:54 +04:00
2011-08-27 22:31:24 +04:00
# define REQUEST_COMPLETION_FLAG 0x1
/* Matches Windows-end */
enum storvsc_request_type {
2012-01-13 00:38:03 +04:00
WRITE_TYPE = 0 ,
2011-08-27 22:31:24 +04:00
READ_TYPE ,
UNKNOWN_TYPE ,
} ;
2012-01-13 00:37:57 +04:00
/*
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
* SRB status codes and masks . In the 8 - bit field , the two high order bits
* are flags , while the remaining 6 bits are an integer status code . The
* definitions here include only the subset of the integer status codes that
* are tested for in this driver .
2012-01-13 00:37:57 +04:00
*/
# define SRB_STATUS_AUTOSENSE_VALID 0x80
2015-10-07 04:04:01 +03:00
# define SRB_STATUS_QUEUE_FROZEN 0x40
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
/* SRB status integer codes */
# define SRB_STATUS_SUCCESS 0x01
# define SRB_STATUS_ABORTED 0x02
# define SRB_STATUS_ERROR 0x04
# define SRB_STATUS_INVALID_REQUEST 0x06
2023-08-25 20:21:24 +03:00
# define SRB_STATUS_TIMEOUT 0x09
# define SRB_STATUS_SELECTION_TIMEOUT 0x0A
# define SRB_STATUS_BUS_RESET 0x0E
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
# define SRB_STATUS_DATA_OVERRUN 0x12
# define SRB_STATUS_INVALID_LUN 0x20
2023-07-08 07:01:26 +03:00
# define SRB_STATUS_INTERNAL_ERROR 0x30
2012-01-13 00:37:57 +04:00
2015-10-07 04:04:01 +03:00
# define SRB_STATUS(status) \
( status & ~ ( SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN ) )
2012-01-13 00:38:08 +04:00
/*
* This is the end of Protocol specific defines .
*/
2019-04-02 00:42:06 +03:00
static int storvsc_ringbuffer_size = ( 128 * 1024 ) ;
2015-03-27 10:27:15 +03:00
static u32 max_outstanding_req_per_channel ;
2019-06-15 02:48:22 +03:00
static int storvsc_change_queue_depth ( struct scsi_device * sdev , int queue_depth ) ;
2015-03-27 10:27:15 +03:00
static int storvsc_vcpus_per_sub_channel = 4 ;
2021-02-25 02:29:48 +03:00
static unsigned int storvsc_max_hw_queues ;
2012-01-13 00:38:08 +04:00
module_param ( storvsc_ringbuffer_size , int , S_IRUGO ) ;
MODULE_PARM_DESC ( storvsc_ringbuffer_size , " Ring buffer size (bytes) " ) ;
2021-02-25 02:29:48 +03:00
module_param ( storvsc_max_hw_queues , uint , 0644 ) ;
MODULE_PARM_DESC ( storvsc_max_hw_queues , " Maximum number of hardware queues " ) ;
2015-03-27 10:27:15 +03:00
module_param ( storvsc_vcpus_per_sub_channel , int , S_IRUGO ) ;
2016-01-08 14:02:04 +03:00
MODULE_PARM_DESC ( storvsc_vcpus_per_sub_channel , " Ratio of VCPUs to subchannels " ) ;
2018-04-20 00:54:24 +03:00
static int ring_avail_percent_lowater = 10 ;
module_param ( ring_avail_percent_lowater , int , S_IRUGO ) ;
MODULE_PARM_DESC ( ring_avail_percent_lowater ,
" Select a channel if available ring size > this in percent " ) ;
2013-06-04 23:05:05 +04:00
/*
* Timeout in seconds for all devices managed by this driver .
*/
static int storvsc_timeout = 180 ;
2015-12-24 00:15:49 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
static struct scsi_transport_template * fc_transport_template ;
# endif
2012-01-13 00:38:08 +04:00
2020-11-09 13:04:01 +03:00
static struct scsi_host_template scsi_driver ;
2013-06-04 23:05:07 +04:00
static void storvsc_on_channel_callback ( void * context ) ;
2014-07-12 20:48:26 +04:00
# define STORVSC_MAX_LUNS_PER_TARGET 255
# define STORVSC_MAX_TARGETS 2
# define STORVSC_MAX_CHANNELS 8
2012-01-13 00:38:08 +04:00
2014-07-12 20:48:26 +04:00
# define STORVSC_FC_MAX_LUNS_PER_TARGET 255
# define STORVSC_FC_MAX_TARGETS 128
# define STORVSC_FC_MAX_CHANNELS 8
2023-07-21 00:05:02 +03:00
# define STORVSC_FC_MAX_XFER_SIZE ((u32)(512 * 1024))
2012-01-13 00:38:08 +04:00
2014-07-12 20:48:26 +04:00
# define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
# define STORVSC_IDE_MAX_TARGETS 1
# define STORVSC_IDE_MAX_CHANNELS 1
2012-01-13 00:37:57 +04:00
2021-04-08 19:14:39 +03:00
/*
2022-05-25 14:27:02 +03:00
* Upper bound on the size of a storvsc packet .
2021-04-08 19:14:39 +03:00
*/
# define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
sizeof ( struct vstor_packet ) )
2012-01-13 00:38:07 +04:00
struct storvsc_cmd_request {
struct scsi_cmnd * cmd ;
2011-08-27 22:31:24 +04:00
struct hv_device * device ;
/* Synchronize the request/response if needed */
struct completion wait_event ;
2015-03-27 10:27:20 +03:00
struct vmbus_channel_packet_multipage_buffer mpb ;
struct vmbus_packet_mpb_array * payload ;
u32 payload_sz ;
2011-08-27 22:31:24 +04:00
struct vstor_packet vstor_packet ;
} ;
/* A storvsc device is a device object that contains a vmbus channel */
struct storvsc_device {
struct hv_device * device ;
bool destroy ;
bool drain_notify ;
atomic_t num_outstanding_req ;
2011-09-13 21:59:48 +04:00
struct Scsi_Host * host ;
2011-08-27 22:31:24 +04:00
wait_queue_head_t waiting_to_drain ;
/*
* Each unique Port / Path / Target represents 1 channel ie scsi
* controller . In reality , the pathid , targetid is always 0
* and the port is set by us
*/
unsigned int port_number ;
unsigned char path_id ;
unsigned char target_id ;
2015-03-27 10:27:17 +03:00
/*
* Max I / O , the device can support .
*/
u32 max_transfer_bytes ;
2016-12-15 05:46:00 +03:00
/*
* Number of sub - channels we will open .
*/
u16 num_sc ;
struct vmbus_channel * * stor_chns ;
/*
* Mask of CPUs bound to subchannels .
*/
struct cpumask alloced_cpus ;
2020-06-17 19:46:41 +03:00
/*
* Serializes modifications of stor_chns [ ] from storvsc_do_io ( )
* and storvsc_change_target_cpu ( ) .
*/
spinlock_t lock ;
2011-08-27 22:31:24 +04:00
/* Used for vsc/vsp channel reset process */
2012-01-13 00:38:07 +04:00
struct storvsc_cmd_request init_request ;
struct storvsc_cmd_request reset_request ;
2015-12-24 00:15:49 +03:00
/*
* Currently active port and node names for FC devices .
*/
u64 node_name ;
u64 port_name ;
2017-04-17 21:37:46 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
struct fc_rport * rport ;
# endif
2011-08-27 22:31:24 +04:00
} ;
2011-12-01 16:59:20 +04:00
struct hv_host_device {
struct hv_device * dev ;
2011-08-27 22:31:25 +04:00
unsigned int port ;
unsigned char path ;
unsigned char target ;
2017-10-31 15:52:06 +03:00
struct workqueue_struct * handle_error_wq ;
2017-11-01 00:58:08 +03:00
struct work_struct host_scan_work ;
struct Scsi_Host * host ;
2011-08-27 22:31:25 +04:00
} ;
2011-11-08 21:01:49 +04:00
struct storvsc_scan_work {
struct work_struct work ;
struct Scsi_Host * host ;
2016-01-28 05:22:45 +03:00
u8 lun ;
u8 tgt_id ;
2011-11-08 21:01:49 +04:00
} ;
2013-02-22 00:04:53 +04:00
static void storvsc_device_scan ( struct work_struct * work )
{
struct storvsc_scan_work * wrk ;
struct scsi_device * sdev ;
wrk = container_of ( work , struct storvsc_scan_work , work ) ;
2016-01-28 05:22:45 +03:00
sdev = scsi_device_lookup ( wrk - > host , 0 , wrk - > tgt_id , wrk - > lun ) ;
2013-02-22 00:04:53 +04:00
if ( ! sdev )
goto done ;
2023-08-22 18:30:41 +03:00
scsi_rescan_device ( sdev ) ;
2013-02-22 00:04:53 +04:00
scsi_device_put ( sdev ) ;
done :
kfree ( wrk ) ;
}
2014-12-17 00:21:42 +03:00
static void storvsc_host_scan ( struct work_struct * work )
2011-11-08 21:01:49 +04:00
{
2014-12-17 00:21:42 +03:00
struct Scsi_Host * host ;
2014-12-17 00:21:43 +03:00
struct scsi_device * sdev ;
2017-11-01 00:58:08 +03:00
struct hv_host_device * host_device =
container_of ( work , struct hv_host_device , host_scan_work ) ;
2011-11-08 21:01:49 +04:00
2017-11-01 00:58:08 +03:00
host = host_device - > host ;
2014-12-17 00:21:43 +03:00
/*
* Before scanning the host , first check to see if any of the
2022-05-21 14:10:22 +03:00
* currently known devices have been hot removed . We issue a
2014-12-17 00:21:43 +03:00
* " unit ready " command against all currently known devices .
* This I / O will result in an error for devices that have been
* removed . As part of handling the I / O error , we remove the device .
*
* When a LUN is added or removed , the host sends us a signal to
* scan the host . Thus we are forced to discover the LUNs that
* may have been removed this way .
*/
mutex_lock ( & host - > scan_mutex ) ;
2015-07-01 12:31:27 +03:00
shost_for_each_device ( sdev , host )
2014-12-17 00:21:43 +03:00
scsi_test_unit_ready ( sdev , 1 , 1 , NULL ) ;
mutex_unlock ( & host - > scan_mutex ) ;
/*
* Now scan the host to discover LUNs that may have been added .
*/
2014-12-17 00:21:42 +03:00
scsi_scan_host ( host ) ;
2011-11-08 21:01:49 +04:00
}
2011-11-08 21:01:50 +04:00
static void storvsc_remove_lun ( struct work_struct * work )
{
struct storvsc_scan_work * wrk ;
struct scsi_device * sdev ;
wrk = container_of ( work , struct storvsc_scan_work , work ) ;
if ( ! scsi_host_get ( wrk - > host ) )
goto done ;
2016-01-28 05:22:45 +03:00
sdev = scsi_device_lookup ( wrk - > host , 0 , wrk - > tgt_id , wrk - > lun ) ;
2011-11-08 21:01:50 +04:00
if ( sdev ) {
scsi_remove_device ( sdev ) ;
scsi_device_put ( sdev ) ;
}
scsi_host_put ( wrk - > host ) ;
done :
kfree ( wrk ) ;
}
2012-01-13 00:38:08 +04:00
2012-01-13 00:38:00 +04:00
/*
* We can get incoming messages from the host that are not in response to
* messages that we have sent out . An example of this would be messages
* received by the guest to notify dynamic addition / removal of LUNs . To
* deal with potential race conditions where the driver may be in the
* midst of being unloaded when we might receive an unsolicited message
* from the host , we have implemented a mechanism to gurantee sequential
* consistency :
*
* 1 ) Once the device is marked as being destroyed , we will fail all
* outgoing messages .
* 2 ) We permit incoming messages when the device is being destroyed ,
* only to properly account for messages already sent out .
*/
2011-08-27 22:31:24 +04:00
static inline struct storvsc_device * get_out_stor_device (
struct hv_device * device )
{
struct storvsc_device * stor_device ;
2011-09-13 21:59:48 +04:00
stor_device = hv_get_drvdata ( device ) ;
2011-08-27 22:31:24 +04:00
if ( stor_device & & stor_device - > destroy )
stor_device = NULL ;
return stor_device ;
}
static inline void storvsc_wait_to_drain ( struct storvsc_device * dev )
{
dev - > drain_notify = true ;
wait_event ( dev - > waiting_to_drain ,
atomic_read ( & dev - > num_outstanding_req ) = = 0 ) ;
dev - > drain_notify = false ;
}
2009-07-14 03:01:31 +04:00
2011-08-27 22:31:22 +04:00
static inline struct storvsc_device * get_in_stor_device (
struct hv_device * device )
{
struct storvsc_device * stor_device ;
2011-09-13 21:59:48 +04:00
stor_device = hv_get_drvdata ( device ) ;
2011-08-27 22:31:22 +04:00
if ( ! stor_device )
goto get_in_err ;
/*
* If the device is being destroyed ; allow incoming
* traffic only to cleanup outstanding requests .
*/
if ( stor_device - > destroy & &
( atomic_read ( & stor_device - > num_outstanding_req ) = = 0 ) )
stor_device = NULL ;
get_in_err :
return stor_device ;
}
2020-04-06 03:15:14 +03:00
static void storvsc_change_target_cpu ( struct vmbus_channel * channel , u32 old ,
u32 new )
{
struct storvsc_device * stor_device ;
struct vmbus_channel * cur_chn ;
bool old_is_alloced = false ;
struct hv_device * device ;
unsigned long flags ;
int cpu ;
device = channel - > primary_channel ?
channel - > primary_channel - > device_obj
: channel - > device_obj ;
stor_device = get_out_stor_device ( device ) ;
if ( ! stor_device )
return ;
/* See storvsc_do_io() -> get_og_chn(). */
2020-06-17 19:46:41 +03:00
spin_lock_irqsave ( & stor_device - > lock , flags ) ;
2020-04-06 03:15:14 +03:00
/*
* Determines if the storvsc device has other channels assigned to
* the " old " CPU to update the alloced_cpus mask and the stor_chns
* array .
*/
if ( device - > channel ! = channel & & device - > channel - > target_cpu = = old ) {
cur_chn = device - > channel ;
old_is_alloced = true ;
goto old_is_alloced ;
}
list_for_each_entry ( cur_chn , & device - > channel - > sc_list , sc_list ) {
if ( cur_chn = = channel )
continue ;
if ( cur_chn - > target_cpu = = old ) {
old_is_alloced = true ;
goto old_is_alloced ;
}
}
old_is_alloced :
if ( old_is_alloced )
WRITE_ONCE ( stor_device - > stor_chns [ old ] , cur_chn ) ;
else
cpumask_clear_cpu ( old , & stor_device - > alloced_cpus ) ;
/* "Flush" the stor_chns array. */
for_each_possible_cpu ( cpu ) {
if ( stor_device - > stor_chns [ cpu ] & & ! cpumask_test_cpu (
cpu , & stor_device - > alloced_cpus ) )
WRITE_ONCE ( stor_device - > stor_chns [ cpu ] , NULL ) ;
}
WRITE_ONCE ( stor_device - > stor_chns [ new ] , channel ) ;
cpumask_set_cpu ( new , & stor_device - > alloced_cpus ) ;
2020-06-17 19:46:41 +03:00
spin_unlock_irqrestore ( & stor_device - > lock , flags ) ;
2020-04-06 03:15:14 +03:00
}
2021-05-11 00:08:41 +03:00
static u64 storvsc_next_request_id ( struct vmbus_channel * channel , u64 rqst_addr )
{
struct storvsc_cmd_request * request =
( struct storvsc_cmd_request * ) ( unsigned long ) rqst_addr ;
if ( rqst_addr = = VMBUS_RQST_INIT )
return VMBUS_RQST_INIT ;
if ( rqst_addr = = VMBUS_RQST_RESET )
return VMBUS_RQST_RESET ;
/*
* Cannot return an ID of 0 , which is reserved for an unsolicited
* message from Hyper - V .
*/
2021-08-10 07:52:41 +03:00
return ( u64 ) blk_mq_unique_tag ( scsi_cmd_to_rq ( request - > cmd ) ) + 1 ;
2021-05-11 00:08:41 +03:00
}
2013-06-04 23:05:07 +04:00
static void handle_sc_creation ( struct vmbus_channel * new_sc )
{
struct hv_device * device = new_sc - > primary_channel - > device_obj ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
struct device * dev = & device - > device ;
2013-06-04 23:05:07 +04:00
struct storvsc_device * stor_device ;
struct vmstorage_channel_properties props ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
int ret ;
2013-06-04 23:05:07 +04:00
stor_device = get_out_stor_device ( device ) ;
if ( ! stor_device )
return ;
memset ( & props , 0 , sizeof ( struct vmstorage_channel_properties ) ) ;
2021-04-08 19:14:39 +03:00
new_sc - > max_pkt_size = STORVSC_MAX_PKT_SIZE ;
2013-06-04 23:05:07 +04:00
2021-05-11 00:08:41 +03:00
new_sc - > next_request_id_callback = storvsc_next_request_id ;
2020-11-09 13:04:01 +03:00
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
ret = vmbus_open ( new_sc ,
storvsc_ringbuffer_size ,
storvsc_ringbuffer_size ,
( void * ) & props ,
sizeof ( struct vmstorage_channel_properties ) ,
storvsc_on_channel_callback , new_sc ) ;
2016-12-15 05:46:00 +03:00
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
/* In case vmbus_open() fails, we don't use the sub-channel. */
if ( ret ! = 0 ) {
dev_err ( dev , " Failed to open sub-channel: err=%d \n " , ret ) ;
return ;
2016-12-15 05:46:00 +03:00
}
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
2020-04-06 03:15:14 +03:00
new_sc - > change_target_cpu_callback = storvsc_change_target_cpu ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
/* Add the sub-channel to the array of available channels. */
stor_device - > stor_chns [ new_sc - > target_cpu ] = new_sc ;
cpumask_set_cpu ( new_sc - > target_cpu , & stor_device - > alloced_cpus ) ;
2013-06-04 23:05:07 +04:00
}
static void handle_multichannel_storage ( struct hv_device * device , int max_chns )
{
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
struct device * dev = & device - > device ;
2013-06-04 23:05:07 +04:00
struct storvsc_device * stor_device ;
int num_sc ;
struct storvsc_cmd_request * request ;
struct vstor_packet * vstor_packet ;
int ret , t ;
2019-04-01 19:10:52 +03:00
/*
* If the number of CPUs is artificially restricted , such as
* with maxcpus = 1 on the kernel boot line , Hyper - V could offer
* sub - channels > = the number of CPUs . These sub - channels
* should not be created . The primary channel is already created
* and assigned to one CPU , so check against # CPUs - 1.
*/
num_sc = min ( ( int ) ( num_online_cpus ( ) - 1 ) , max_chns ) ;
if ( ! num_sc )
return ;
2013-06-04 23:05:07 +04:00
stor_device = get_out_stor_device ( device ) ;
if ( ! stor_device )
return ;
2016-12-15 05:46:00 +03:00
stor_device - > num_sc = num_sc ;
2013-06-04 23:05:07 +04:00
request = & stor_device - > init_request ;
vstor_packet = & request - > vstor_packet ;
/*
* Establish a handler for dealing with subchannels .
*/
vmbus_set_sc_create_callback ( device - > channel , handle_sc_creation ) ;
/*
* Request the host to create sub - channels .
*/
memset ( request , 0 , sizeof ( struct storvsc_cmd_request ) ) ;
init_completion ( & request - > wait_event ) ;
vstor_packet - > operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS ;
vstor_packet - > flags = REQUEST_COMPLETION_FLAG ;
vstor_packet - > sub_channel_count = num_sc ;
ret = vmbus_sendpacket ( device - > channel , vstor_packet ,
2022-05-25 14:27:02 +03:00
sizeof ( struct vstor_packet ) ,
2021-05-11 00:08:41 +03:00
VMBUS_RQST_INIT ,
2013-06-04 23:05:07 +04:00
VM_PKT_DATA_INBAND ,
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED ) ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
if ( ret ! = 0 ) {
dev_err ( dev , " Failed to create sub-channel: err=%d \n " , ret ) ;
2013-06-04 23:05:07 +04:00
return ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
}
2013-06-04 23:05:07 +04:00
t = wait_for_completion_timeout ( & request - > wait_event , 10 * HZ ) ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
if ( t = = 0 ) {
dev_err ( dev , " Failed to create sub-channel: timed out \n " ) ;
2013-06-04 23:05:07 +04:00
return ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
}
2013-06-04 23:05:07 +04:00
if ( vstor_packet - > operation ! = VSTOR_OPERATION_COMPLETE_IO | |
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
vstor_packet - > status ! = 0 ) {
dev_err ( dev , " Failed to create sub-channel: op=%d, sts=%d \n " ,
vstor_packet - > operation , vstor_packet - > status ) ;
2013-06-04 23:05:07 +04:00
return ;
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
}
2013-06-04 23:05:07 +04:00
/*
scsi: storvsc: Fix a race in sub-channel creation that can cause panic
We can concurrently try to open the same sub-channel from 2 paths:
path #1: vmbus_onoffer() -> vmbus_process_offer() -> handle_sc_creation().
path #2: storvsc_probe() -> storvsc_connect_to_vsp() ->
-> storvsc_channel_init() -> handle_multichannel_storage() ->
-> vmbus_are_subchannels_present() -> handle_sc_creation().
They conflict with each other, but it was not an issue before the recent
commit ae6935ed7d42 ("vmbus: split ring buffer allocation from open"),
because at the beginning of vmbus_open() we checked newchannel->state so
only one path could succeed, and the other would return with -EINVAL.
After ae6935ed7d42, the failing path frees the channel's ringbuffer by
vmbus_free_ring(), and this causes a panic later.
Commit ae6935ed7d42 itself is good, and it just reveals the longstanding
race. We can resolve the issue by removing path #2, i.e. removing the
second vmbus_are_subchannels_present() in handle_multichannel_storage().
BTW, the comment "Check to see if sub-channels have already been created"
in handle_multichannel_storage() is incorrect: when we unload the driver,
we first close the sub-channel(s) and then close the primary channel, next
the host sends rescind-offer message(s) so primary->sc_list will become
empty. This means the first vmbus_are_subchannels_present() in
handle_multichannel_storage() is never useful.
Fixes: ae6935ed7d42 ("vmbus: split ring buffer allocation from open")
Cc: stable@vger.kernel.org
Cc: Long Li <longli@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2018-11-26 03:26:17 +03:00
* We need to do nothing here , because vmbus_process_offer ( )
* invokes channel - > sc_creation_callback , which will open and use
* the sub - channel ( s ) .
2013-06-04 23:05:07 +04:00
*/
}
2015-12-24 00:15:49 +03:00
static void cache_wwn ( struct storvsc_device * stor_device ,
struct vstor_packet * vstor_packet )
{
/*
* Cache the currently active port and node ww names .
*/
if ( vstor_packet - > wwn_packet . primary_active ) {
stor_device - > node_name =
wwn_to_u64 ( vstor_packet - > wwn_packet . primary_node_wwn ) ;
stor_device - > port_name =
wwn_to_u64 ( vstor_packet - > wwn_packet . primary_port_wwn ) ;
} else {
stor_device - > node_name =
wwn_to_u64 ( vstor_packet - > wwn_packet . secondary_node_wwn ) ;
stor_device - > port_name =
wwn_to_u64 ( vstor_packet - > wwn_packet . secondary_port_wwn ) ;
}
}
2015-12-24 00:15:50 +03:00
static int storvsc_execute_vstor_op ( struct hv_device * device ,
struct storvsc_cmd_request * request ,
bool status_check )
2011-08-27 22:31:22 +04:00
{
2020-12-17 23:33:20 +03:00
struct storvsc_device * stor_device ;
2011-08-27 22:31:22 +04:00
struct vstor_packet * vstor_packet ;
2015-12-24 00:15:50 +03:00
int ret , t ;
2011-08-27 22:31:22 +04:00
2020-12-17 23:33:20 +03:00
stor_device = get_out_stor_device ( device ) ;
if ( ! stor_device )
return - ENODEV ;
2011-08-27 22:31:22 +04:00
vstor_packet = & request - > vstor_packet ;
init_completion ( & request - > wait_event ) ;
vstor_packet - > flags = REQUEST_COMPLETION_FLAG ;
ret = vmbus_sendpacket ( device - > channel , vstor_packet ,
2022-05-25 14:27:02 +03:00
sizeof ( struct vstor_packet ) ,
2021-05-11 00:08:41 +03:00
VMBUS_RQST_INIT ,
2011-08-27 22:31:22 +04:00
VM_PKT_DATA_INBAND ,
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED ) ;
if ( ret ! = 0 )
2015-12-24 00:15:49 +03:00
return ret ;
2011-08-27 22:31:22 +04:00
t = wait_for_completion_timeout ( & request - > wait_event , 5 * HZ ) ;
2015-12-24 00:15:49 +03:00
if ( t = = 0 )
return - ETIMEDOUT ;
2011-08-27 22:31:22 +04:00
2015-12-24 00:15:50 +03:00
if ( ! status_check )
return ret ;
2011-08-27 22:31:22 +04:00
if ( vstor_packet - > operation ! = VSTOR_OPERATION_COMPLETE_IO | |
2015-12-24 00:15:49 +03:00
vstor_packet - > status ! = 0 )
return - EINVAL ;
2011-08-27 22:31:22 +04:00
2015-12-24 00:15:50 +03:00
return ret ;
}
static int storvsc_channel_init ( struct hv_device * device , bool is_fc )
{
struct storvsc_device * stor_device ;
struct storvsc_cmd_request * request ;
struct vstor_packet * vstor_packet ;
int ret , i ;
int max_chns ;
bool process_sub_channels = false ;
stor_device = get_out_stor_device ( device ) ;
if ( ! stor_device )
return - ENODEV ;
request = & stor_device - > init_request ;
vstor_packet = & request - > vstor_packet ;
/*
* Now , initiate the vsc / vsp initialization protocol on the open
* channel
*/
memset ( request , 0 , sizeof ( struct storvsc_cmd_request ) ) ;
vstor_packet - > operation = VSTOR_OPERATION_BEGIN_INITIALIZATION ;
ret = storvsc_execute_vstor_op ( device , request , true ) ;
if ( ret )
return ret ;
/*
* Query host supported protocol version .
*/
2011-08-27 22:31:22 +04:00
2022-05-25 14:27:02 +03:00
for ( i = 0 ; i < ARRAY_SIZE ( protocol_version ) ; i + + ) {
2015-08-13 18:43:48 +03:00
/* reuse the packet for version range supported */
memset ( vstor_packet , 0 , sizeof ( struct vstor_packet ) ) ;
vstor_packet - > operation =
VSTOR_OPERATION_QUERY_PROTOCOL_VERSION ;
2011-08-27 22:31:22 +04:00
2022-05-25 14:27:02 +03:00
vstor_packet - > version . major_minor = protocol_version [ i ] ;
2012-01-13 00:38:04 +04:00
2015-08-13 18:43:48 +03:00
/*
* The revision number is only used in Windows ; set it to 0.
*/
vstor_packet - > version . revision = 0 ;
2015-12-24 00:15:50 +03:00
ret = storvsc_execute_vstor_op ( device , request , false ) ;
2015-08-13 18:43:48 +03:00
if ( ret ! = 0 )
2015-12-24 00:15:49 +03:00
return ret ;
2011-08-27 22:31:22 +04:00
2015-12-24 00:15:49 +03:00
if ( vstor_packet - > operation ! = VSTOR_OPERATION_COMPLETE_IO )
return - EINVAL ;
2015-08-13 18:43:48 +03:00
if ( vstor_packet - > status = = 0 ) {
2022-05-25 14:27:02 +03:00
vmstor_proto_version = protocol_version [ i ] ;
2015-08-13 18:43:48 +03:00
break ;
}
2011-08-27 22:31:22 +04:00
}
2022-05-25 14:27:02 +03:00
if ( vstor_packet - > status ! = 0 ) {
dev_err ( & device - > device , " Obsolete Hyper-V version \n " ) ;
2015-12-24 00:15:49 +03:00
return - EINVAL ;
2022-05-25 14:27:02 +03:00
}
2011-08-27 22:31:22 +04:00
memset ( vstor_packet , 0 , sizeof ( struct vstor_packet ) ) ;
vstor_packet - > operation = VSTOR_OPERATION_QUERY_PROPERTIES ;
2015-12-24 00:15:50 +03:00
ret = storvsc_execute_vstor_op ( device , request , true ) ;
2011-08-27 22:31:22 +04:00
if ( ret ! = 0 )
2015-12-24 00:15:49 +03:00
return ret ;
2011-08-27 22:31:22 +04:00
2013-06-04 23:05:07 +04:00
/*
* Check to see if multi - channel support is there .
* Hosts that implement protocol version of 5.1 and above
* support multi - channel .
*/
max_chns = vstor_packet - > storage_channel_properties . max_channel_cnt ;
2016-12-15 05:46:00 +03:00
/*
* Allocate state to manage the sub - channels .
* We allocate an array based on the numbers of possible CPUs
* ( Hyper - V does not support cpu online / offline ) .
* This Array will be sparseley populated with unique
* channels - primary + sub - channels .
* We will however populate all the slots to evenly distribute
* the load .
*/
2017-03-16 09:58:23 +03:00
stor_device - > stor_chns = kcalloc ( num_possible_cpus ( ) , sizeof ( void * ) ,
2016-12-15 05:46:00 +03:00
GFP_KERNEL ) ;
if ( stor_device - > stor_chns = = NULL )
return - ENOMEM ;
2020-04-06 03:15:14 +03:00
device - > channel - > change_target_cpu_callback = storvsc_change_target_cpu ;
2016-12-15 05:46:00 +03:00
stor_device - > stor_chns [ device - > channel - > target_cpu ] = device - > channel ;
cpumask_set_cpu ( device - > channel - > target_cpu ,
& stor_device - > alloced_cpus ) ;
2022-05-25 14:27:02 +03:00
if ( vstor_packet - > storage_channel_properties . flags &
STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL )
process_sub_channels = true ;
2015-03-27 10:27:17 +03:00
stor_device - > max_transfer_bytes =
vstor_packet - > storage_channel_properties . max_transfer_bytes ;
2013-06-04 23:05:07 +04:00
2015-12-24 00:15:49 +03:00
if ( ! is_fc )
goto done ;
2015-12-24 00:15:50 +03:00
/*
* For FC devices retrieve FC HBA data .
*/
2015-12-24 00:15:49 +03:00
memset ( vstor_packet , 0 , sizeof ( struct vstor_packet ) ) ;
vstor_packet - > operation = VSTOR_OPERATION_FCHBA_DATA ;
2015-12-24 00:15:50 +03:00
ret = storvsc_execute_vstor_op ( device , request , true ) ;
2015-12-24 00:15:49 +03:00
if ( ret ! = 0 )
return ret ;
/*
* Cache the currently active port and node ww names .
*/
cache_wwn ( stor_device , vstor_packet ) ;
done :
2011-08-27 22:31:22 +04:00
memset ( vstor_packet , 0 , sizeof ( struct vstor_packet ) ) ;
vstor_packet - > operation = VSTOR_OPERATION_END_INITIALIZATION ;
2015-12-24 00:15:50 +03:00
ret = storvsc_execute_vstor_op ( device , request , true ) ;
2011-08-27 22:31:22 +04:00
if ( ret ! = 0 )
2015-12-24 00:15:49 +03:00
return ret ;
2011-08-27 22:31:22 +04:00
2013-06-04 23:05:07 +04:00
if ( process_sub_channels )
handle_multichannel_storage ( device , max_chns ) ;
2011-08-27 22:31:22 +04:00
return ret ;
}
2013-02-22 00:04:52 +04:00
static void storvsc_handle_error ( struct vmscsi_request * vm_srb ,
struct scsi_cmnd * scmnd ,
struct Scsi_Host * host ,
u8 asc , u8 ascq )
{
struct storvsc_scan_work * wrk ;
void ( * process_err_fn ) ( struct work_struct * work ) ;
2017-10-31 15:52:06 +03:00
struct hv_host_device * host_dev = shost_priv ( host ) ;
2013-02-22 00:04:52 +04:00
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
switch ( SRB_STATUS ( vm_srb - > srb_status ) ) {
case SRB_STATUS_ERROR :
case SRB_STATUS_ABORTED :
case SRB_STATUS_INVALID_REQUEST :
2023-07-08 07:01:26 +03:00
case SRB_STATUS_INTERNAL_ERROR :
2023-08-25 20:21:24 +03:00
case SRB_STATUS_TIMEOUT :
case SRB_STATUS_SELECTION_TIMEOUT :
case SRB_STATUS_BUS_RESET :
case SRB_STATUS_DATA_OVERRUN :
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
if ( vm_srb - > srb_status & SRB_STATUS_AUTOSENSE_VALID ) {
/* Check for capacity change */
if ( ( asc = = 0x2a ) & & ( ascq = = 0x9 ) ) {
process_err_fn = storvsc_device_scan ;
/* Retry the I/O that triggered this. */
set_host_byte ( scmnd , DID_REQUEUE ) ;
goto do_work ;
}
2021-06-04 20:21:03 +03:00
scsi: storvsc: Handle BlockSize change in Hyper-V VHD/VHDX file
Hyper-V uses a VHD or VHDX file on the host as the underlying storage for a
virtual disk. The VHD/VHDX file format is a sparse format where real disk
space on the host is assigned in chunks that the VHD/VHDX file format calls
the BlockSize. This BlockSize is not to be confused with the 512-byte (or
4096-byte) sector size of the underlying storage device. The default block
size for a new VHD/VHDX file is 32 Mbytes. When a guest VM touches any
disk space within a 32 Mbyte chunk of the VHD/VHDX file, Hyper-V allocates
32 Mbytes of real disk space for that section of the VHD/VHDX. Similarly,
if a discard operation is done that covers an entire 32 Mbyte chunk,
Hyper-V will free the real disk space for that portion of the VHD/VHDX.
This BlockSize is surfaced in Linux as the "discard_granularity" in
/sys/block/sd<x>/queue, which makes sense.
Hyper-V also has differencing disks that can overlay a VHD/VHDX file to
capture changes to the VHD/VHDX while preserving the original VHD/VHDX.
One example of this differencing functionality is for VM snapshots. When a
snapshot is created, a differencing disk is created. If the snapshot is
rolled back, Hyper-V can just delete the differencing disk, and the VM will
see the original disk contents at the time the snapshot was taken.
Differencing disks are used in other scenarios as well.
The BlockSize for a differencing disk defaults to 2 Mbytes, not 32 Mbytes.
The smaller default is used because changes to differencing disks are
typically scattered all over, and Hyper-V doesn't want to allocate 32
Mbytes of real disk space for a stray write here or there. The smaller
BlockSize provides more efficient use of real disk space.
When a differencing disk is added to a VHD/VHDX, Hyper-V reports
UNIT_ATTENTION with a sense code indicating "Operating parameters have
changed", because the value of discard_granularity should be changed to 2
Mbytes. When the differencing disk is removed, discard_granularity should
be changed back to 32 Mbytes. However, current code simply reports a
message from scsi_report_sense() and the value of
/sys/block/sd<x>/queue/discard_granularity is not updated. The message
isn't very actionable by a sysadmin.
Fix this by having the storvsc driver check for the sense code indicating
that the underly VHD/VHDX block size has changed, and do a rescan of the
device to pick up the new discard_granularity. With this change the entire
transition to/from differencing disks is handled automatically and
transparently, with no confusing messages being output.
Link: https://lore.kernel.org/r/1677516514-86060-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2023-02-27 19:48:34 +03:00
/*
* Check for " Operating parameters have changed "
* due to Hyper - V changing the VHD / VHDX BlockSize
* when adding / removing a differencing disk . This
* causes discard_granularity to change , so do a
* rescan to pick up the new granularity . We don ' t
* want scsi_report_sense ( ) to output a message
* that a sysadmin wouldn ' t know what to do with .
*/
if ( ( asc = = 0x3f ) & & ( ascq ! = 0x03 ) & &
( ascq ! = 0x0e ) ) {
process_err_fn = storvsc_device_scan ;
set_host_byte ( scmnd , DID_REQUEUE ) ;
goto do_work ;
}
2021-06-04 20:21:03 +03:00
/*
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
* Otherwise , let upper layer deal with the
* error when sense message is present
2021-06-04 20:21:03 +03:00
*/
return ;
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
}
2021-06-04 20:21:03 +03:00
2013-02-22 00:04:52 +04:00
/*
* If there is an error ; offline the device since all
* error recovery strategies would have already been
* deployed on the host side . However , if the command
* were a pass - through command deal with it appropriately .
*/
switch ( scmnd - > cmnd [ 0 ] ) {
case ATA_16 :
case ATA_12 :
set_host_byte ( scmnd , DID_PASSTHROUGH ) ;
break ;
2014-07-12 20:48:32 +04:00
/*
2021-06-04 20:21:03 +03:00
* On some Hyper - V hosts TEST_UNIT_READY command can
* return SRB_STATUS_ERROR . Let the upper level code
* deal with it based on the sense information .
2014-07-12 20:48:32 +04:00
*/
case TEST_UNIT_READY :
break ;
2013-02-22 00:04:52 +04:00
default :
2017-12-19 21:32:48 +03:00
set_host_byte ( scmnd , DID_ERROR ) ;
2013-02-22 00:04:52 +04:00
}
scsi: storvsc: Fix handling of srb_status and capacity change events
Current handling of the srb_status is incorrect. Commit 52e1b3b3daa9
("scsi: storvsc: Correctly handle multiple flags in srb_status")
is based on srb_status being a set of flags, when in fact only the
2 high order bits are flags and the remaining 6 bits are an integer
status. Because the integer values of interest mostly look like flags,
the code actually works when treated that way.
But in the interest of correctness going forward, fix this by treating
the low 6 bits of srb_status as an integer status code. Add handling
for SRB_STATUS_INVALID_REQUEST, which was the original intent of commit
52e1b3b3daa9. Furthermore, treat the ERROR, ABORTED, and INVALID_REQUEST
srb status codes as essentially equivalent for the cases we care about.
There's no harm in doing so, and it isn't always clear which status code
current or older versions of Hyper-V report for particular conditions.
Treating the srb status codes as equivalent has the additional benefit
of ensuring that capacity change events result in an immediate rescan
so that the new size is known to Linux. Existing code checks SCSI
sense data for capacity change events when the srb status is ABORTED.
But capacity change events are also being observed when Hyper-V reports
the srb status as ERROR. Without the immediate rescan, the new size
isn't known until something else causes a rescan (such as running
fdisk to expand a partition), and in the meantime, tools such as "lsblk"
continue to report the old size.
Fixes: 52e1b3b3daa9 ("scsi: storvsc: Correctly handle multiple flags in srb_status")
Reported-by: Juan Tian <juantian@microsoft.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/1668019722-1983-1-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
2022-11-09 21:48:42 +03:00
return ;
case SRB_STATUS_INVALID_LUN :
set_host_byte ( scmnd , DID_NO_CONNECT ) ;
process_err_fn = storvsc_remove_lun ;
goto do_work ;
2013-02-22 00:04:52 +04:00
}
2021-06-04 20:21:03 +03:00
return ;
2013-02-22 00:04:53 +04:00
2021-06-04 20:21:03 +03:00
do_work :
2013-02-22 00:04:52 +04:00
/*
* We need to schedule work to process this error ; schedule it .
*/
wrk = kmalloc ( sizeof ( struct storvsc_scan_work ) , GFP_ATOMIC ) ;
if ( ! wrk ) {
2022-08-12 04:00:19 +03:00
set_host_byte ( scmnd , DID_BAD_TARGET ) ;
2013-02-22 00:04:52 +04:00
return ;
}
wrk - > host = host ;
wrk - > lun = vm_srb - > lun ;
2016-01-28 05:22:45 +03:00
wrk - > tgt_id = vm_srb - > target_id ;
2013-02-22 00:04:52 +04:00
INIT_WORK ( & wrk - > work , process_err_fn ) ;
2017-10-31 15:52:06 +03:00
queue_work ( host_dev - > handle_error_wq , & wrk - > work ) ;
2013-02-22 00:04:52 +04:00
}
2012-01-13 00:38:01 +04:00
2015-12-24 00:15:51 +03:00
static void storvsc_command_completion ( struct storvsc_cmd_request * cmd_request ,
struct storvsc_device * stor_dev )
2011-08-27 22:31:22 +04:00
{
2012-01-13 00:38:01 +04:00
struct scsi_cmnd * scmnd = cmd_request - > cmd ;
struct scsi_sense_hdr sense_hdr ;
struct vmscsi_request * vm_srb ;
2016-12-15 05:46:03 +03:00
u32 data_transfer_length ;
2013-02-22 00:04:52 +04:00
struct Scsi_Host * host ;
2015-03-27 10:27:20 +03:00
u32 payload_sz = cmd_request - > payload_sz ;
void * payload = cmd_request - > payload ;
2021-06-04 20:21:02 +03:00
bool sense_ok ;
2013-02-22 00:04:52 +04:00
host = stor_dev - > host ;
2011-08-27 22:31:22 +04:00
2012-01-13 00:38:07 +04:00
vm_srb = & cmd_request - > vstor_packet . vm_srb ;
2016-12-15 05:46:03 +03:00
data_transfer_length = vm_srb - > data_transfer_length ;
2011-08-27 22:31:22 +04:00
2012-04-05 23:26:52 +04:00
scmnd - > result = vm_srb - > scsi_status ;
2012-01-13 00:38:01 +04:00
if ( scmnd - > result ) {
2021-06-04 20:21:02 +03:00
sense_ok = scsi_normalize_sense ( scmnd - > sense_buffer ,
SCSI_SENSE_BUFFERSIZE , & sense_hdr ) ;
if ( sense_ok & & do_logging ( STORVSC_LOGGING_WARN ) )
2014-10-24 16:26:45 +04:00
scsi_print_sense_hdr ( scmnd - > device , " storvsc " ,
& sense_hdr ) ;
2012-01-13 00:38:01 +04:00
}
2016-12-15 05:46:03 +03:00
if ( vm_srb - > srb_status ! = SRB_STATUS_SUCCESS ) {
2013-02-22 00:04:52 +04:00
storvsc_handle_error ( vm_srb , scmnd , host , sense_hdr . asc ,
sense_hdr . ascq ) ;
2016-12-15 05:46:03 +03:00
/*
* The Windows driver set data_transfer_length on
* SRB_STATUS_DATA_OVERRUN . On other errors , this value
* is untouched . In these cases we set it to 0.
*/
if ( vm_srb - > srb_status ! = SRB_STATUS_DATA_OVERRUN )
data_transfer_length = 0 ;
}
2013-02-22 00:04:52 +04:00
2020-07-06 19:09:28 +03:00
/* Validate data_transfer_length (from Hyper-V) */
if ( data_transfer_length > cmd_request - > payload - > range . len )
data_transfer_length = cmd_request - > payload - > range . len ;
2012-01-13 00:38:01 +04:00
scsi_set_resid ( scmnd ,
2016-12-15 05:46:03 +03:00
cmd_request - > payload - > range . len - data_transfer_length ) ;
2012-01-13 00:38:01 +04:00
2021-10-07 23:29:08 +03:00
scsi_done ( scmnd ) ;
2015-03-27 10:27:20 +03:00
if ( payload_sz >
sizeof ( struct vmbus_channel_packet_multipage_buffer ) )
kfree ( payload ) ;
2012-01-13 00:38:01 +04:00
}
2015-12-24 00:15:51 +03:00
static void storvsc_on_io_completion ( struct storvsc_device * stor_device ,
2012-01-13 00:38:01 +04:00
struct vstor_packet * vstor_packet ,
2012-01-13 00:38:07 +04:00
struct storvsc_cmd_request * request )
2012-01-13 00:38:01 +04:00
{
struct vstor_packet * stor_pkt ;
2015-12-24 00:15:51 +03:00
struct hv_device * device = stor_device - > device ;
2012-01-13 00:38:01 +04:00
stor_pkt = & request - > vstor_packet ;
/*
* The current SCSI handling on the host side does
* not correctly handle :
* INQUIRY command with page code parameter set to 0x80
* MODE_SENSE command with cmd [ 2 ] = = 0x1c
*
* Setup srb and scsi status so this won ' t be fatal .
* We do this so we can distinguish truly fatal failues
2011-08-27 22:31:26 +04:00
* ( srb status = = 0x4 ) and off - line the device in that case .
*/
if ( ( stor_pkt - > vm_srb . cdb [ 0 ] = = INQUIRY ) | |
2012-01-13 00:38:00 +04:00
( stor_pkt - > vm_srb . cdb [ 0 ] = = MODE_SENSE ) ) {
2011-08-27 22:31:26 +04:00
vstor_packet - > vm_srb . scsi_status = 0 ;
2012-01-13 00:37:57 +04:00
vstor_packet - > vm_srb . srb_status = SRB_STATUS_SUCCESS ;
2011-08-27 22:31:26 +04:00
}
2011-08-27 22:31:22 +04:00
/* Copy over the status...etc */
stor_pkt - > vm_srb . scsi_status = vstor_packet - > vm_srb . scsi_status ;
stor_pkt - > vm_srb . srb_status = vstor_packet - > vm_srb . srb_status ;
2020-07-06 19:09:28 +03:00
2021-06-04 20:21:01 +03:00
/*
* Copy over the sense_info_length , but limit to the known max
* size if Hyper - V returns a bad value .
*/
2022-05-25 14:27:02 +03:00
stor_pkt - > vm_srb . sense_info_length = min_t ( u8 , STORVSC_SENSE_BUFFER_SIZE ,
2021-06-04 20:21:01 +03:00
vstor_packet - > vm_srb . sense_info_length ) ;
2011-08-27 22:31:22 +04:00
2015-12-04 11:07:24 +03:00
if ( vstor_packet - > vm_srb . scsi_status ! = 0 | |
2021-08-06 20:12:50 +03:00
vstor_packet - > vm_srb . srb_status ! = SRB_STATUS_SUCCESS ) {
/*
* Log TEST_UNIT_READY errors only as warnings . Hyper - V can
* return errors when detecting devices using TEST_UNIT_READY ,
* and logging these as errors produces unhelpful noise .
*/
int loglevel = ( stor_pkt - > vm_srb . cdb [ 0 ] = = TEST_UNIT_READY ) ?
STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR ;
storvsc_log ( device , loglevel ,
2021-06-04 20:21:02 +03:00
" tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x \n " ,
2021-08-10 07:52:41 +03:00
scsi_cmd_to_rq ( request - > cmd ) - > tag ,
2015-12-04 11:07:24 +03:00
stor_pkt - > vm_srb . cdb [ 0 ] ,
vstor_packet - > vm_srb . scsi_status ,
2021-06-04 20:21:02 +03:00
vstor_packet - > vm_srb . srb_status ,
vstor_packet - > status ) ;
2021-08-06 20:12:50 +03:00
}
2015-12-04 11:07:24 +03:00
2021-06-04 20:21:01 +03:00
if ( vstor_packet - > vm_srb . scsi_status = = SAM_STAT_CHECK_CONDITION & &
( vstor_packet - > vm_srb . srb_status & SRB_STATUS_AUTOSENSE_VALID ) )
memcpy ( request - > cmd - > sense_buffer ,
vstor_packet - > vm_srb . sense_data ,
stor_pkt - > vm_srb . sense_info_length ) ;
2011-08-27 22:31:22 +04:00
stor_pkt - > vm_srb . data_transfer_length =
2021-06-04 20:21:01 +03:00
vstor_packet - > vm_srb . data_transfer_length ;
2011-08-27 22:31:22 +04:00
2015-12-24 00:15:51 +03:00
storvsc_command_completion ( request , stor_device ) ;
2011-08-27 22:31:22 +04:00
if ( atomic_dec_and_test ( & stor_device - > num_outstanding_req ) & &
stor_device - > drain_notify )
wake_up ( & stor_device - > waiting_to_drain ) ;
}
2015-12-24 00:15:51 +03:00
static void storvsc_on_receive ( struct storvsc_device * stor_device ,
2011-08-27 22:31:22 +04:00
struct vstor_packet * vstor_packet ,
2012-01-13 00:38:07 +04:00
struct storvsc_cmd_request * request )
2011-08-27 22:31:22 +04:00
{
2017-11-01 00:58:08 +03:00
struct hv_host_device * host_dev ;
2011-08-27 22:31:22 +04:00
switch ( vstor_packet - > operation ) {
case VSTOR_OPERATION_COMPLETE_IO :
2015-12-24 00:15:51 +03:00
storvsc_on_io_completion ( stor_device , vstor_packet , request ) ;
2011-08-27 22:31:22 +04:00
break ;
2011-11-08 21:01:49 +04:00
2011-08-27 22:31:22 +04:00
case VSTOR_OPERATION_REMOVE_DEVICE :
2011-11-08 21:01:49 +04:00
case VSTOR_OPERATION_ENUMERATE_BUS :
2017-11-01 00:58:08 +03:00
host_dev = shost_priv ( stor_device - > host ) ;
queue_work (
host_dev - > handle_error_wq , & host_dev - > host_scan_work ) ;
2011-11-08 21:01:49 +04:00
break ;
2011-08-27 22:31:22 +04:00
2015-12-24 00:15:49 +03:00
case VSTOR_OPERATION_FCHBA_DATA :
cache_wwn ( stor_device , vstor_packet ) ;
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
fc_host_node_name ( stor_device - > host ) = stor_device - > node_name ;
fc_host_port_name ( stor_device - > host ) = stor_device - > port_name ;
# endif
break ;
2011-08-27 22:31:22 +04:00
default :
break ;
}
}
static void storvsc_on_channel_callback ( void * context )
{
2013-06-04 23:05:07 +04:00
struct vmbus_channel * channel = ( struct vmbus_channel * ) context ;
2017-05-18 19:18:11 +03:00
const struct vmpacket_descriptor * desc ;
2013-06-04 23:05:07 +04:00
struct hv_device * device ;
2011-08-27 22:31:22 +04:00
struct storvsc_device * stor_device ;
2021-05-11 00:08:41 +03:00
struct Scsi_Host * shost ;
2022-07-25 12:37:28 +03:00
unsigned long time_limit = jiffies + msecs_to_jiffies ( CALLBACK_TIMEOUT ) ;
2011-08-27 22:31:22 +04:00
2013-06-04 23:05:07 +04:00
if ( channel - > primary_channel ! = NULL )
device = channel - > primary_channel - > device_obj ;
else
device = channel - > device_obj ;
2011-08-27 22:31:22 +04:00
stor_device = get_in_stor_device ( device ) ;
if ( ! stor_device )
return ;
2021-05-11 00:08:41 +03:00
shost = stor_device - > host ;
2017-05-18 19:18:11 +03:00
2021-05-11 00:08:41 +03:00
foreach_vmbus_pkt ( desc , channel ) {
struct vstor_packet * packet = hv_pkt_data ( desc ) ;
struct storvsc_cmd_request * request = NULL ;
2021-10-07 15:28:28 +03:00
u32 pktlen = hv_pkt_datalen ( desc ) ;
2021-05-11 00:08:41 +03:00
u64 rqst_id = desc - > trans_id ;
2022-05-25 14:27:02 +03:00
u32 minlen = rqst_id ? sizeof ( struct vstor_packet ) :
sizeof ( enum vstor_packet_operation ) ;
2017-05-18 19:18:11 +03:00
2022-07-25 12:37:28 +03:00
if ( unlikely ( time_after ( jiffies , time_limit ) ) ) {
hv_pkt_iter_close ( channel ) ;
return ;
}
2021-10-07 15:28:28 +03:00
if ( pktlen < minlen ) {
dev_err ( & device - > device ,
" Invalid pkt: id=%llu, len=%u, minlen=%u \n " ,
rqst_id , pktlen , minlen ) ;
2020-12-17 23:33:21 +03:00
continue ;
}
2021-05-11 00:08:41 +03:00
if ( rqst_id = = VMBUS_RQST_INIT ) {
request = & stor_device - > init_request ;
} else if ( rqst_id = = VMBUS_RQST_RESET ) {
request = & stor_device - > reset_request ;
2011-08-27 22:31:22 +04:00
} else {
2021-05-11 00:08:41 +03:00
/* Hyper-V can send an unsolicited message with ID of 0 */
if ( rqst_id = = 0 ) {
/*
* storvsc_on_receive ( ) looks at the vstor_packet in the message
2021-10-07 15:28:28 +03:00
* from the ring buffer .
*
* - If the operation in the vstor_packet is COMPLETE_IO , then
* we call storvsc_on_io_completion ( ) , and dereference the
* guest memory address . Make sure we don ' t call
* storvsc_on_io_completion ( ) with a guest memory address
* that is zero if Hyper - V were to construct and send such
* a bogus packet .
*
* - If the operation in the vstor_packet is FCHBA_DATA , then
* we call cache_wwn ( ) , and access the data payload area of
* the packet ( wwn_packet ) ; however , there is no guarantee
* that the packet is big enough to contain such area .
* Future - proof the code by rejecting such a bogus packet .
2021-05-11 00:08:41 +03:00
*/
2021-10-07 15:28:28 +03:00
if ( packet - > operation = = VSTOR_OPERATION_COMPLETE_IO | |
packet - > operation = = VSTOR_OPERATION_FCHBA_DATA ) {
2021-05-11 00:08:41 +03:00
dev_err ( & device - > device , " Invalid packet with ID of 0 \n " ) ;
continue ;
}
} else {
struct scsi_cmnd * scmnd ;
/* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */
scmnd = scsi_host_find_tag ( shost , rqst_id - 1 ) ;
if ( scmnd = = NULL ) {
dev_err ( & device - > device , " Incorrect transaction ID \n " ) ;
continue ;
}
request = ( struct storvsc_cmd_request * ) scsi_cmd_priv ( scmnd ) ;
2021-12-13 10:14:05 +03:00
scsi_dma_unmap ( scmnd ) ;
2021-05-11 00:08:41 +03:00
}
2017-05-18 19:18:11 +03:00
storvsc_on_receive ( stor_device , packet , request ) ;
2021-05-11 00:08:41 +03:00
continue ;
2011-08-27 22:31:22 +04:00
}
2021-05-11 00:08:41 +03:00
memcpy ( & request - > vstor_packet , packet ,
2022-05-25 14:27:02 +03:00
sizeof ( struct vstor_packet ) ) ;
2021-05-11 00:08:41 +03:00
complete ( & request - > wait_event ) ;
2017-05-18 19:18:11 +03:00
}
2011-08-27 22:31:22 +04:00
}
2015-12-24 00:15:49 +03:00
static int storvsc_connect_to_vsp ( struct hv_device * device , u32 ring_size ,
bool is_fc )
2011-08-27 22:31:22 +04:00
{
struct vmstorage_channel_properties props ;
int ret ;
memset ( & props , 0 , sizeof ( struct vmstorage_channel_properties ) ) ;
2021-04-08 19:14:39 +03:00
device - > channel - > max_pkt_size = STORVSC_MAX_PKT_SIZE ;
2021-05-11 00:08:41 +03:00
device - > channel - > next_request_id_callback = storvsc_next_request_id ;
2020-11-09 13:04:01 +03:00
2011-08-27 22:31:22 +04:00
ret = vmbus_open ( device - > channel ,
ring_size ,
ring_size ,
( void * ) & props ,
sizeof ( struct vmstorage_channel_properties ) ,
2013-06-04 23:05:07 +04:00
storvsc_on_channel_callback , device - > channel ) ;
2011-08-27 22:31:22 +04:00
if ( ret ! = 0 )
return ret ;
2015-12-24 00:15:49 +03:00
ret = storvsc_channel_init ( device , is_fc ) ;
2011-08-27 22:31:22 +04:00
return ret ;
}
2011-08-27 22:31:25 +04:00
static int storvsc_dev_remove ( struct hv_device * device )
2011-08-27 22:31:22 +04:00
{
struct storvsc_device * stor_device ;
2011-09-13 21:59:48 +04:00
stor_device = hv_get_drvdata ( device ) ;
2011-08-27 22:31:22 +04:00
stor_device - > destroy = true ;
2017-05-18 19:18:12 +03:00
/* Make sure flag is set before waiting */
wmb ( ) ;
2011-08-27 22:31:22 +04:00
/*
* At this point , all outbound traffic should be disable . We
* only allow inbound traffic ( responses ) to proceed so that
* outstanding requests can be completed .
*/
storvsc_wait_to_drain ( stor_device ) ;
/*
* Since we have already drained , we don ' t need to busy wait
* as was done in final_release_stor_device ( )
* Note that we cannot set the ext pointer to NULL until
* we have drained - to drain the outgoing packets , we need to
* allow incoming packets .
*/
2011-09-13 21:59:48 +04:00
hv_set_drvdata ( device , NULL ) ;
2011-08-27 22:31:22 +04:00
/* Close the channel */
vmbus_close ( device - > channel ) ;
2016-12-15 05:46:00 +03:00
kfree ( stor_device - > stor_chns ) ;
2011-08-27 22:31:22 +04:00
kfree ( stor_device ) ;
return 0 ;
}
2016-12-15 05:46:00 +03:00
static struct vmbus_channel * get_og_chn ( struct storvsc_device * stor_device ,
u16 q_num )
{
u16 slot = 0 ;
u16 hash_qnum ;
2018-05-18 00:07:40 +03:00
const struct cpumask * node_mask ;
2016-12-15 05:46:00 +03:00
int num_channels , tgt_cpu ;
2020-04-06 03:15:14 +03:00
if ( stor_device - > num_sc = = 0 ) {
stor_device - > stor_chns [ q_num ] = stor_device - > device - > channel ;
2016-12-15 05:46:00 +03:00
return stor_device - > device - > channel ;
2020-04-06 03:15:14 +03:00
}
2016-12-15 05:46:00 +03:00
/*
* Our channel array is sparsley populated and we
* initiated I / O on a processor / hw - q that does not
* currently have a designated channel . Fix this .
* The strategy is simple :
* I . Ensure NUMA locality
* II . Distribute evenly ( best effort )
*/
2018-05-18 00:07:40 +03:00
node_mask = cpumask_of_node ( cpu_to_node ( q_num ) ) ;
2016-12-15 05:46:00 +03:00
2018-05-18 00:07:40 +03:00
num_channels = 0 ;
for_each_cpu ( tgt_cpu , & stor_device - > alloced_cpus ) {
if ( cpumask_test_cpu ( tgt_cpu , node_mask ) )
num_channels + + ;
}
2020-04-06 03:15:14 +03:00
if ( num_channels = = 0 ) {
stor_device - > stor_chns [ q_num ] = stor_device - > device - > channel ;
2016-12-15 05:46:00 +03:00
return stor_device - > device - > channel ;
2020-04-06 03:15:14 +03:00
}
2016-12-15 05:46:00 +03:00
hash_qnum = q_num ;
while ( hash_qnum > = num_channels )
hash_qnum - = num_channels ;
2018-05-18 00:07:40 +03:00
for_each_cpu ( tgt_cpu , & stor_device - > alloced_cpus ) {
if ( ! cpumask_test_cpu ( tgt_cpu , node_mask ) )
continue ;
2016-12-15 05:46:00 +03:00
if ( slot = = hash_qnum )
break ;
slot + + ;
}
stor_device - > stor_chns [ q_num ] = stor_device - > stor_chns [ tgt_cpu ] ;
return stor_device - > stor_chns [ q_num ] ;
}
2011-08-27 22:31:25 +04:00
static int storvsc_do_io ( struct hv_device * device ,
2016-12-15 05:46:00 +03:00
struct storvsc_cmd_request * request , u16 q_num )
2011-08-27 22:31:22 +04:00
{
struct storvsc_device * stor_device ;
struct vstor_packet * vstor_packet ;
2018-04-20 00:54:24 +03:00
struct vmbus_channel * outgoing_channel , * channel ;
2020-04-06 03:15:14 +03:00
unsigned long flags ;
2011-08-27 22:31:22 +04:00
int ret = 0 ;
2018-05-18 00:07:40 +03:00
const struct cpumask * node_mask ;
2016-12-15 05:46:00 +03:00
int tgt_cpu ;
2011-08-27 22:31:22 +04:00
vstor_packet = & request - > vstor_packet ;
stor_device = get_out_stor_device ( device ) ;
if ( ! stor_device )
return - ENODEV ;
request - > device = device ;
2013-06-04 23:05:07 +04:00
/*
2020-04-06 03:15:14 +03:00
* Select an appropriate channel to send the request out .
2013-06-04 23:05:07 +04:00
*/
2020-04-06 03:15:14 +03:00
/* See storvsc_change_target_cpu(). */
outgoing_channel = READ_ONCE ( stor_device - > stor_chns [ q_num ] ) ;
if ( outgoing_channel ! = NULL ) {
2018-04-20 00:54:24 +03:00
if ( outgoing_channel - > target_cpu = = q_num ) {
2016-12-15 05:46:00 +03:00
/*
* Ideally , we want to pick a different channel if
* available on the same NUMA node .
*/
2018-05-18 00:07:40 +03:00
node_mask = cpumask_of_node ( cpu_to_node ( q_num ) ) ;
for_each_cpu_wrap ( tgt_cpu ,
& stor_device - > alloced_cpus , q_num + 1 ) {
if ( ! cpumask_test_cpu ( tgt_cpu , node_mask ) )
continue ;
2018-04-20 00:54:24 +03:00
if ( tgt_cpu = = q_num )
continue ;
2020-04-06 03:15:14 +03:00
channel = READ_ONCE (
stor_device - > stor_chns [ tgt_cpu ] ) ;
if ( channel = = NULL )
continue ;
2018-04-20 00:54:24 +03:00
if ( hv_get_avail_to_write_percent (
& channel - > outbound )
> ring_avail_percent_lowater ) {
outgoing_channel = channel ;
goto found_channel ;
}
}
/*
* All the other channels on the same NUMA node are
* busy . Try to use the channel on the current CPU
*/
if ( hv_get_avail_to_write_percent (
& outgoing_channel - > outbound )
> ring_avail_percent_lowater )
goto found_channel ;
/*
* If we reach here , all the channels on the current
* NUMA node are busy . Try to find a channel in
* other NUMA nodes
*/
2018-05-18 00:07:40 +03:00
for_each_cpu ( tgt_cpu , & stor_device - > alloced_cpus ) {
if ( cpumask_test_cpu ( tgt_cpu , node_mask ) )
continue ;
2020-04-06 03:15:14 +03:00
channel = READ_ONCE (
stor_device - > stor_chns [ tgt_cpu ] ) ;
if ( channel = = NULL )
continue ;
2018-04-20 00:54:24 +03:00
if ( hv_get_avail_to_write_percent (
& channel - > outbound )
> ring_avail_percent_lowater ) {
outgoing_channel = channel ;
goto found_channel ;
2016-12-15 05:46:00 +03:00
}
}
}
} else {
2020-06-17 19:46:41 +03:00
spin_lock_irqsave ( & stor_device - > lock , flags ) ;
2020-04-06 03:15:14 +03:00
outgoing_channel = stor_device - > stor_chns [ q_num ] ;
if ( outgoing_channel ! = NULL ) {
2020-06-17 19:46:41 +03:00
spin_unlock_irqrestore ( & stor_device - > lock , flags ) ;
2020-04-06 03:15:14 +03:00
goto found_channel ;
}
2016-12-15 05:46:00 +03:00
outgoing_channel = get_og_chn ( stor_device , q_num ) ;
2020-06-17 19:46:41 +03:00
spin_unlock_irqrestore ( & stor_device - > lock , flags ) ;
2016-12-15 05:46:00 +03:00
}
2011-08-27 22:31:22 +04:00
2018-04-20 00:54:24 +03:00
found_channel :
2011-08-27 22:31:22 +04:00
vstor_packet - > flags | = REQUEST_COMPLETION_FLAG ;
2022-05-25 14:27:02 +03:00
vstor_packet - > vm_srb . length = sizeof ( struct vmscsi_request ) ;
2011-08-27 22:31:22 +04:00
2022-05-25 14:27:02 +03:00
vstor_packet - > vm_srb . sense_info_length = STORVSC_SENSE_BUFFER_SIZE ;
2011-08-27 22:31:22 +04:00
vstor_packet - > vm_srb . data_transfer_length =
2015-03-27 10:27:20 +03:00
request - > payload - > range . len ;
2011-08-27 22:31:22 +04:00
vstor_packet - > operation = VSTOR_OPERATION_EXECUTE_SRB ;
2015-03-27 10:27:20 +03:00
if ( request - > payload - > range . len ) {
ret = vmbus_sendpacket_mpb_desc ( outgoing_channel ,
request - > payload , request - > payload_sz ,
2011-08-27 22:31:22 +04:00
vstor_packet ,
2022-05-25 14:27:02 +03:00
sizeof ( struct vstor_packet ) ,
2011-08-27 22:31:22 +04:00
( unsigned long ) request ) ;
} else {
2015-03-27 10:27:16 +03:00
ret = vmbus_sendpacket ( outgoing_channel , vstor_packet ,
2022-05-25 14:27:02 +03:00
sizeof ( struct vstor_packet ) ,
2011-08-27 22:31:22 +04:00
( unsigned long ) request ,
VM_PKT_DATA_INBAND ,
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED ) ;
}
if ( ret ! = 0 )
return ret ;
atomic_inc ( & stor_device - > num_outstanding_req ) ;
return ret ;
}
2017-03-07 20:15:53 +03:00
static int storvsc_device_alloc ( struct scsi_device * sdevice )
{
/*
* Set blist flag to permit the reading of the VPD pages even when
* the target may claim SPC - 2 compliance . MSFT targets currently
* claim SPC - 2 compliance while they implement post SPC - 2 features .
* With this flag we can correctly handle WRITE_SAME_16 issues .
*
* Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
* still supports REPORT LUN .
*/
sdevice - > sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES ;
return 0 ;
}
2011-05-10 18:54:27 +04:00
static int storvsc_device_configure ( struct scsi_device * sdevice )
{
2013-06-04 23:05:05 +04:00
blk_queue_rq_timeout ( sdevice - > request_queue , ( storvsc_timeout * HZ ) ) ;
2023-06-09 23:38:21 +03:00
/* storvsc devices don't support MAINTENANCE_IN SCSI cmd */
sdevice - > no_report_opcodes = 1 ;
2013-02-22 00:04:51 +04:00
sdevice - > no_write_same = 1 ;
2014-12-17 00:21:45 +03:00
/*
* If the host is WIN8 or WIN8 R2 , claim conformance to SPC - 3
2015-08-13 18:43:51 +03:00
* if the device is a MSFT virtual device . If the host is
* WIN10 or newer , allow write_same .
2014-12-17 00:21:45 +03:00
*/
if ( ! strncmp ( sdevice - > vendor , " Msft " , 4 ) ) {
2015-08-13 18:43:50 +03:00
switch ( vmstor_proto_version ) {
case VMSTOR_PROTO_VERSION_WIN8 :
case VMSTOR_PROTO_VERSION_WIN8_1 :
2014-12-17 00:21:45 +03:00
sdevice - > scsi_level = SCSI_SPC_3 ;
break ;
}
2015-08-13 18:43:51 +03:00
if ( vmstor_proto_version > = VMSTOR_PROTO_VERSION_WIN10 )
sdevice - > no_write_same = 0 ;
2014-12-17 00:21:45 +03:00
}
2011-05-10 18:54:27 +04:00
return 0 ;
}
2011-05-10 18:54:34 +04:00
static int storvsc_get_chs ( struct scsi_device * sdev , struct block_device * bdev ,
sector_t capacity , int * info )
{
2011-05-10 18:54:52 +04:00
sector_t nsect = capacity ;
sector_t cylinders = nsect ;
int heads , sectors_pt ;
2011-05-10 18:54:34 +04:00
2011-05-10 18:54:52 +04:00
/*
* We are making up these values ; let us keep it simple .
*/
heads = 0xff ;
sectors_pt = 0x3f ; /* Sectors per track */
sector_div ( cylinders , heads * sectors_pt ) ;
if ( ( sector_t ) ( cylinders + 1 ) * heads * sectors_pt < nsect )
cylinders = 0xffff ;
2011-05-10 18:54:34 +04:00
info [ 0 ] = heads ;
2011-05-10 18:54:52 +04:00
info [ 1 ] = sectors_pt ;
info [ 2 ] = ( int ) cylinders ;
2011-05-10 18:54:34 +04:00
return 0 ;
}
2011-05-10 18:54:37 +04:00
2012-01-13 00:37:58 +04:00
static int storvsc_host_reset_handler ( struct scsi_cmnd * scmnd )
2011-05-10 18:54:37 +04:00
{
2012-01-13 00:37:58 +04:00
struct hv_host_device * host_dev = shost_priv ( scmnd - > device - > host ) ;
struct hv_device * device = host_dev - > dev ;
2011-05-10 18:54:37 +04:00
struct storvsc_device * stor_device ;
2012-01-13 00:38:07 +04:00
struct storvsc_cmd_request * request ;
2011-05-10 18:54:37 +04:00
struct vstor_packet * vstor_packet ;
int ret , t ;
2011-08-27 22:31:03 +04:00
stor_device = get_out_stor_device ( device ) ;
2011-05-10 18:54:37 +04:00
if ( ! stor_device )
2011-11-08 21:01:43 +04:00
return FAILED ;
2011-05-10 18:54:37 +04:00
request = & stor_device - > reset_request ;
vstor_packet = & request - > vstor_packet ;
2020-07-06 19:09:28 +03:00
memset ( vstor_packet , 0 , sizeof ( struct vstor_packet ) ) ;
2011-05-10 18:54:37 +04:00
init_completion ( & request - > wait_event ) ;
vstor_packet - > operation = VSTOR_OPERATION_RESET_BUS ;
vstor_packet - > flags = REQUEST_COMPLETION_FLAG ;
vstor_packet - > vm_srb . path_id = stor_device - > path_id ;
ret = vmbus_sendpacket ( device - > channel , vstor_packet ,
2022-05-25 14:27:02 +03:00
sizeof ( struct vstor_packet ) ,
2021-05-11 00:08:41 +03:00
VMBUS_RQST_RESET ,
2011-05-10 18:54:37 +04:00
VM_PKT_DATA_INBAND ,
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED ) ;
if ( ret ! = 0 )
2011-11-08 21:01:43 +04:00
return FAILED ;
2011-05-10 18:54:37 +04:00
2011-06-17 00:16:36 +04:00
t = wait_for_completion_timeout ( & request - > wait_event , 5 * HZ ) ;
2011-11-08 21:01:43 +04:00
if ( t = = 0 )
return TIMEOUT_ERROR ;
2011-05-10 18:54:37 +04:00
/*
* At this point , all outstanding requests in the adapter
* should have been flushed out and return to us
2012-10-02 22:03:31 +04:00
* There is a potential race here where the host may be in
* the process of responding when we return from here .
* Just wait for all in - transit packets to be accounted for
* before we return from here .
2011-05-10 18:54:37 +04:00
*/
2012-10-02 22:03:31 +04:00
storvsc_wait_to_drain ( stor_device ) ;
2011-05-10 18:54:37 +04:00
2011-11-08 21:01:43 +04:00
return SUCCESS ;
2011-05-10 18:54:37 +04:00
}
2014-07-12 20:48:30 +04:00
/*
* The host guarantees to respond to each command , although I / O latencies might
* be unbounded on Azure . Reset the timer unconditionally to give the host a
* chance to perform EH .
*/
2022-10-18 23:29:50 +03:00
static enum scsi_timeout_action storvsc_eh_timed_out ( struct scsi_cmnd * scmnd )
2014-07-12 20:48:30 +04:00
{
2022-10-18 23:29:50 +03:00
return SCSI_EH_RESET_TIMER ;
2014-07-12 20:48:30 +04:00
}
2012-01-13 00:37:56 +04:00
static bool storvsc_scsi_cmd_ok ( struct scsi_cmnd * scmnd )
2011-10-10 11:37:39 +04:00
{
bool allowed = true ;
u8 scsi_op = scmnd - > cmnd [ 0 ] ;
switch ( scsi_op ) {
2013-02-22 00:04:51 +04:00
/* the host does not handle WRITE_SAME, log accident usage */
case WRITE_SAME :
2012-01-13 00:37:56 +04:00
/*
* smartd sends this command and the host does not handle
* this . So , don ' t send it .
*/
2011-10-15 08:31:57 +04:00
case SET_WINDOW :
2021-06-04 20:21:01 +03:00
set_host_byte ( scmnd , DID_ERROR ) ;
2011-10-15 08:31:57 +04:00
allowed = false ;
break ;
default :
break ;
2011-10-10 11:37:39 +04:00
}
return allowed ;
}
2011-05-10 18:54:42 +04:00
2011-11-08 21:01:45 +04:00
static int storvsc_queuecommand ( struct Scsi_Host * host , struct scsi_cmnd * scmnd )
2011-05-10 18:54:42 +04:00
{
int ret ;
2011-11-08 21:01:45 +04:00
struct hv_host_device * host_dev = shost_priv ( host ) ;
2011-05-10 18:54:42 +04:00
struct hv_device * dev = host_dev - > dev ;
2014-12-30 07:05:09 +03:00
struct storvsc_cmd_request * cmd_request = scsi_cmd_priv ( scmnd ) ;
2011-05-10 18:54:42 +04:00
struct scatterlist * sgl ;
struct vmscsi_request * vm_srb ;
2015-03-27 10:27:20 +03:00
struct vmbus_packet_mpb_array * payload ;
u32 payload_sz ;
u32 length ;
2011-05-10 18:54:42 +04:00
2015-08-13 18:43:47 +03:00
if ( vmstor_proto_version < = VMSTOR_PROTO_VERSION_WIN8 ) {
2014-07-12 20:48:28 +04:00
/*
* On legacy hosts filter unimplemented commands .
* Future hosts are expected to correctly handle
* unsupported commands . Furthermore , it is
* possible that some of the currently
* unsupported commands maybe supported in
* future versions of the host .
*/
if ( ! storvsc_scsi_cmd_ok ( scmnd ) ) {
2021-10-07 23:29:08 +03:00
scsi_done ( scmnd ) ;
2014-07-12 20:48:28 +04:00
return 0 ;
}
2011-10-10 11:37:39 +04:00
}
2011-05-10 18:54:42 +04:00
/* Setup the cmd request */
cmd_request - > cmd = scmnd ;
2020-07-06 19:09:28 +03:00
memset ( & cmd_request - > vstor_packet , 0 , sizeof ( struct vstor_packet ) ) ;
2012-01-13 00:38:07 +04:00
vm_srb = & cmd_request - > vstor_packet . vm_srb ;
2022-05-25 14:27:02 +03:00
vm_srb - > time_out_value = 60 ;
2011-05-10 18:54:42 +04:00
2022-05-25 14:27:02 +03:00
vm_srb - > srb_flags | =
2015-08-31 18:21:54 +03:00
SRB_FLAGS_DISABLE_SYNCH_TRANSFER ;
2011-05-10 18:54:42 +04:00
2016-12-15 05:46:01 +03:00
if ( scmnd - > device - > tagged_supported ) {
2022-05-25 14:27:02 +03:00
vm_srb - > srb_flags | =
2016-12-15 05:46:01 +03:00
( SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE ) ;
2022-05-25 14:27:02 +03:00
vm_srb - > queue_tag = SP_UNTAGGED ;
vm_srb - > queue_action = SRB_SIMPLE_TAG_REQUEST ;
2016-12-15 05:46:01 +03:00
}
2011-05-10 18:54:42 +04:00
/* Build the SRB */
switch ( scmnd - > sc_data_direction ) {
case DMA_TO_DEVICE :
vm_srb - > data_in = WRITE_TYPE ;
2022-05-25 14:27:02 +03:00
vm_srb - > srb_flags | = SRB_FLAGS_DATA_OUT ;
2011-05-10 18:54:42 +04:00
break ;
case DMA_FROM_DEVICE :
vm_srb - > data_in = READ_TYPE ;
2022-05-25 14:27:02 +03:00
vm_srb - > srb_flags | = SRB_FLAGS_DATA_IN ;
2011-05-10 18:54:42 +04:00
break ;
2015-06-25 19:12:11 +03:00
case DMA_NONE :
2011-05-10 18:54:42 +04:00
vm_srb - > data_in = UNKNOWN_TYPE ;
2022-05-25 14:27:02 +03:00
vm_srb - > srb_flags | = SRB_FLAGS_NO_DATA_TRANSFER ;
2011-05-10 18:54:42 +04:00
break ;
2015-06-25 19:12:11 +03:00
default :
/*
* This is DMA_BIDIRECTIONAL or something else we are never
* supposed to see here .
*/
WARN ( 1 , " Unexpected data direction: %d \n " ,
scmnd - > sc_data_direction ) ;
return - EINVAL ;
2011-05-10 18:54:42 +04:00
}
vm_srb - > port_number = host_dev - > port ;
vm_srb - > path_id = scmnd - > device - > channel ;
vm_srb - > target_id = scmnd - > device - > id ;
vm_srb - > lun = scmnd - > device - > lun ;
vm_srb - > cdb_length = scmnd - > cmd_len ;
memcpy ( vm_srb - > cdb , scmnd - > cmnd , vm_srb - > cdb_length ) ;
2015-03-27 10:27:20 +03:00
sgl = ( struct scatterlist * ) scsi_sglist ( scmnd ) ;
2011-05-10 18:54:42 +04:00
2015-03-27 10:27:20 +03:00
length = scsi_bufflen ( scmnd ) ;
payload = ( struct vmbus_packet_mpb_array * ) & cmd_request - > mpb ;
2023-05-15 20:20:41 +03:00
payload_sz = 0 ;
2015-03-27 10:27:20 +03:00
2021-12-27 07:03:11 +03:00
if ( scsi_sg_count ( scmnd ) ) {
2021-02-24 01:44:54 +03:00
unsigned long offset_in_hvpg = offset_in_hvpage ( sgl - > offset ) ;
2020-09-16 06:48:17 +03:00
unsigned int hvpg_count = HVPFN_UP ( offset_in_hvpg + length ) ;
2021-12-13 10:14:05 +03:00
struct scatterlist * sg ;
unsigned long hvpfn , hvpfns_to_add ;
2021-12-27 07:03:11 +03:00
int j , i = 0 , sg_count ;
2015-03-27 10:27:20 +03:00
2023-05-15 20:20:41 +03:00
payload_sz = ( hvpg_count * sizeof ( u64 ) +
sizeof ( struct vmbus_packet_mpb_array ) ) ;
2020-09-16 06:48:17 +03:00
2023-05-15 20:20:41 +03:00
if ( hvpg_count > MAX_PAGE_BUFFER_COUNT ) {
2016-11-23 16:46:33 +03:00
payload = kzalloc ( payload_sz , GFP_ATOMIC ) ;
2015-10-02 00:53:17 +03:00
if ( ! payload )
return SCSI_MLQUEUE_DEVICE_BUSY ;
2015-03-27 10:27:20 +03:00
}
payload - > range . len = length ;
2020-09-16 06:48:17 +03:00
payload - > range . offset = offset_in_hvpg ;
2015-03-27 10:27:20 +03:00
2021-12-13 10:14:05 +03:00
sg_count = scsi_dma_map ( scmnd ) ;
2022-01-09 03:17:58 +03:00
if ( sg_count < 0 ) {
ret = SCSI_MLQUEUE_DEVICE_BUSY ;
goto err_free_payload ;
}
2021-02-24 01:44:54 +03:00
2021-12-13 10:14:05 +03:00
for_each_sg ( sgl , sg , sg_count , j ) {
2020-09-16 06:48:17 +03:00
/*
2021-12-13 10:14:05 +03:00
* Init values for the current sgl entry . hvpfns_to_add
* is in units of Hyper - V size pages . Handling the
* PAGE_SIZE ! = HV_HYP_PAGE_SIZE case also handles
* values of sgl - > offset that are larger than PAGE_SIZE .
* Such offsets are handled even on other than the first
* sgl entry , provided they are a multiple of PAGE_SIZE .
2020-09-16 06:48:17 +03:00
*/
2021-12-13 10:14:05 +03:00
hvpfn = HVPFN_DOWN ( sg_dma_address ( sg ) ) ;
hvpfns_to_add = HVPFN_UP ( sg_dma_address ( sg ) +
sg_dma_len ( sg ) ) - hvpfn ;
2020-09-16 06:48:17 +03:00
/*
2021-02-24 01:44:54 +03:00
* Fill the next portion of the PFN array with
* sequential Hyper - V PFNs for the continguous physical
* memory described by the sgl entry . The end of the
* last sgl should be reached at the same time that
* the PFN array is filled .
2020-09-16 06:48:17 +03:00
*/
2021-02-24 01:44:54 +03:00
while ( hvpfns_to_add - - )
2021-12-13 10:14:05 +03:00
payload - > range . pfn_array [ i + + ] = hvpfn + + ;
2015-03-27 10:27:19 +03:00
}
2011-05-10 18:54:42 +04:00
}
2015-03-27 10:27:20 +03:00
cmd_request - > payload = payload ;
cmd_request - > payload_sz = payload_sz ;
2011-05-10 18:54:42 +04:00
/* Invokes the vsc to start an IO */
2016-12-15 05:46:00 +03:00
ret = storvsc_do_io ( dev , cmd_request , get_cpu ( ) ) ;
put_cpu ( ) ;
2011-05-10 18:54:50 +04:00
2022-12-04 22:52:44 +03:00
if ( ret )
scsi_dma_unmap ( scmnd ) ;
2011-08-25 20:48:58 +04:00
if ( ret = = - EAGAIN ) {
2011-05-10 18:54:42 +04:00
/* no more space */
2022-01-09 03:17:58 +03:00
ret = SCSI_MLQUEUE_DEVICE_BUSY ;
goto err_free_payload ;
2011-05-10 18:54:42 +04:00
}
2012-01-13 00:37:56 +04:00
return 0 ;
2022-01-09 03:17:58 +03:00
err_free_payload :
if ( payload_sz > sizeof ( cmd_request - > mpb ) )
kfree ( payload ) ;
return ret ;
2011-05-10 18:54:42 +04:00
}
2009-07-14 03:01:31 +04:00
static struct scsi_host_template scsi_driver = {
2009-09-02 19:37:47 +04:00
. module = THIS_MODULE ,
. name = " storvsc_host_t " ,
2014-12-30 07:05:09 +03:00
. cmd_size = sizeof ( struct storvsc_cmd_request ) ,
2009-09-02 19:37:47 +04:00
. bios_param = storvsc_get_chs ,
. queuecommand = storvsc_queuecommand ,
. eh_host_reset_handler = storvsc_host_reset_handler ,
2014-12-30 07:05:09 +03:00
. proc_name = " storvsc_host " ,
2014-07-12 20:48:30 +04:00
. eh_timed_out = storvsc_eh_timed_out ,
2017-03-07 20:15:53 +03:00
. slave_alloc = storvsc_device_alloc ,
2009-09-02 19:37:47 +04:00
. slave_configure = storvsc_device_configure ,
2018-01-25 01:49:57 +03:00
. cmd_per_lun = 2048 ,
2009-09-02 19:37:47 +04:00
. this_id = - 1 ,
2019-06-17 15:19:56 +03:00
/* Ensure there are no gaps in presented sgls */
2022-06-14 10:05:55 +03:00
. virt_boundary_mask = HV_HYP_PAGE_SIZE - 1 ,
2013-10-23 14:25:40 +04:00
. no_write_same = 1 ,
2016-12-15 05:45:58 +03:00
. track_queue_depth = 1 ,
2019-06-15 02:48:22 +03:00
. change_queue_depth = storvsc_change_queue_depth ,
2009-07-14 03:01:31 +04:00
} ;
2011-09-13 21:59:39 +04:00
enum {
SCSI_GUID ,
IDE_GUID ,
2013-06-04 23:05:08 +04:00
SFC_GUID ,
2011-09-13 21:59:39 +04:00
} ;
2011-08-25 20:48:33 +04:00
static const struct hv_vmbus_device_id id_table [ ] = {
2011-08-25 22:41:33 +04:00
/* SCSI guid */
2013-01-24 05:42:43 +04:00
{ HV_SCSI_GUID ,
. driver_data = SCSI_GUID
} ,
2011-08-27 22:31:18 +04:00
/* IDE guid */
2013-01-24 05:42:43 +04:00
{ HV_IDE_GUID ,
. driver_data = IDE_GUID
} ,
2013-06-04 23:05:08 +04:00
/* Fibre Channel GUID */
{
HV_SYNTHFC_GUID ,
. driver_data = SFC_GUID
} ,
2011-08-25 22:41:33 +04:00
{ } ,
2011-08-25 20:48:33 +04:00
} ;
2009-07-14 03:01:31 +04:00
2011-08-25 20:48:33 +04:00
MODULE_DEVICE_TABLE ( vmbus , id_table ) ;
2011-08-27 22:31:17 +04:00
2019-09-12 02:35:16 +03:00
static const struct { guid_t guid ; } fc_guid = { HV_SYNTHFC_GUID } ;
static bool hv_dev_is_fc ( struct hv_device * hv_dev )
{
return guid_equal ( & fc_guid . guid , & hv_dev - > dev_type ) ;
}
2011-09-13 21:59:38 +04:00
static int storvsc_probe ( struct hv_device * device ,
const struct hv_vmbus_device_id * dev_id )
2009-07-14 03:01:31 +04:00
{
2009-09-02 19:37:47 +04:00
int ret ;
2015-03-27 10:27:15 +03:00
int num_cpus = num_online_cpus ( ) ;
2021-02-25 02:29:48 +03:00
int num_present_cpus = num_present_cpus ( ) ;
2009-07-14 03:01:31 +04:00
struct Scsi_Host * host ;
2011-05-10 18:54:20 +04:00
struct hv_host_device * host_dev ;
2011-09-13 21:59:39 +04:00
bool dev_is_ide = ( ( dev_id - > driver_data = = IDE_GUID ) ? true : false ) ;
2015-12-24 00:15:49 +03:00
bool is_fc = ( ( dev_id - > driver_data = = SFC_GUID ) ? true : false ) ;
2011-08-27 22:31:17 +04:00
int target = 0 ;
2011-09-13 21:59:45 +04:00
struct storvsc_device * stor_device ;
2015-03-27 10:27:15 +03:00
int max_sub_channels = 0 ;
2022-06-14 10:05:55 +03:00
u32 max_xfer_bytes ;
2011-08-27 22:31:17 +04:00
2013-06-04 23:05:06 +04:00
/*
2022-05-02 19:36:29 +03:00
* We support sub - channels for storage on SCSI and FC controllers .
* The number of sub - channels offerred is based on the number of
* VCPUs in the guest .
2013-06-04 23:05:06 +04:00
*/
2022-05-02 19:36:29 +03:00
if ( ! dev_is_ide )
max_sub_channels =
( num_cpus - 1 ) / storvsc_vcpus_per_sub_channel ;
2013-06-04 23:05:06 +04:00
2018-04-20 00:54:24 +03:00
scsi_driver . can_queue = max_outstanding_req_per_channel *
( max_sub_channels + 1 ) *
( 100 - ring_avail_percent_lowater ) / 100 ;
2015-03-27 10:27:15 +03:00
2009-09-02 19:37:47 +04:00
host = scsi_host_alloc ( & scsi_driver ,
2011-05-10 18:54:19 +04:00
sizeof ( struct hv_host_device ) ) ;
2011-05-10 18:54:24 +04:00
if ( ! host )
2009-07-14 03:01:31 +04:00
return - ENOMEM ;
2011-11-08 21:01:44 +04:00
host_dev = shost_priv ( host ) ;
2011-05-10 18:54:20 +04:00
memset ( host_dev , 0 , sizeof ( struct hv_host_device ) ) ;
2009-07-14 03:01:31 +04:00
2011-05-10 18:54:20 +04:00
host_dev - > port = host - > host_no ;
2011-05-10 18:54:21 +04:00
host_dev - > dev = device ;
2017-11-01 00:58:08 +03:00
host_dev - > host = host ;
2009-07-14 03:01:31 +04:00
2011-11-08 21:01:40 +04:00
2011-09-13 21:59:46 +04:00
stor_device = kzalloc ( sizeof ( struct storvsc_device ) , GFP_KERNEL ) ;
2011-09-13 21:59:45 +04:00
if ( ! stor_device ) {
2011-11-08 21:01:41 +04:00
ret = - ENOMEM ;
2011-12-01 16:59:20 +04:00
goto err_out0 ;
2011-09-13 21:59:45 +04:00
}
2011-04-30 00:45:10 +04:00
2011-09-13 21:59:46 +04:00
stor_device - > destroy = false ;
init_waitqueue_head ( & stor_device - > waiting_to_drain ) ;
stor_device - > device = device ;
2011-09-13 21:59:48 +04:00
stor_device - > host = host ;
2020-06-17 19:46:41 +03:00
spin_lock_init ( & stor_device - > lock ) ;
2011-09-13 21:59:48 +04:00
hv_set_drvdata ( device , stor_device ) ;
2021-12-13 10:14:05 +03:00
dma_set_min_align_mask ( & device - > device , HV_HYP_PAGE_SIZE - 1 ) ;
2011-09-13 21:59:46 +04:00
2011-09-13 21:59:45 +04:00
stor_device - > port_number = host - > host_no ;
2015-12-24 00:15:49 +03:00
ret = storvsc_connect_to_vsp ( device , storvsc_ringbuffer_size , is_fc ) ;
2011-11-08 21:01:41 +04:00
if ( ret )
2011-12-01 16:59:20 +04:00
goto err_out1 ;
2009-07-14 03:01:31 +04:00
2011-09-13 21:59:45 +04:00
host_dev - > path = stor_device - > path_id ;
host_dev - > target = stor_device - > target_id ;
2009-07-14 03:01:31 +04:00
2014-07-12 20:48:26 +04:00
switch ( dev_id - > driver_data ) {
case SFC_GUID :
host - > max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET ;
host - > max_id = STORVSC_FC_MAX_TARGETS ;
host - > max_channel = STORVSC_FC_MAX_CHANNELS - 1 ;
2015-12-24 00:15:49 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
host - > transportt = fc_transport_template ;
# endif
2014-07-12 20:48:26 +04:00
break ;
case SCSI_GUID :
2022-05-02 19:36:29 +03:00
host - > max_lun = STORVSC_MAX_LUNS_PER_TARGET ;
host - > max_id = STORVSC_MAX_TARGETS ;
host - > max_channel = STORVSC_MAX_CHANNELS - 1 ;
2014-07-12 20:48:26 +04:00
break ;
default :
host - > max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET ;
host - > max_id = STORVSC_IDE_MAX_TARGETS ;
host - > max_channel = STORVSC_IDE_MAX_CHANNELS - 1 ;
break ;
}
2011-09-07 03:10:55 +04:00
/* max cmd length */
host - > max_cmd_len = STORVSC_MAX_CMD_LEN ;
2015-03-27 10:27:20 +03:00
/*
2022-06-14 10:05:55 +03:00
* Any reasonable Hyper - V configuration should provide
* max_transfer_bytes value aligning to HV_HYP_PAGE_SIZE ,
* protecting it from any weird value .
*/
max_xfer_bytes = round_down ( stor_device - > max_transfer_bytes , HV_HYP_PAGE_SIZE ) ;
2023-07-21 00:05:02 +03:00
if ( is_fc )
max_xfer_bytes = min ( max_xfer_bytes , STORVSC_FC_MAX_XFER_SIZE ) ;
2022-06-14 10:05:55 +03:00
/* max_hw_sectors_kb */
host - > max_sectors = max_xfer_bytes > > 9 ;
/*
* There are 2 requirements for Hyper - V storvsc sgl segments ,
* based on which the below calculation for max segments is
* done :
*
* 1. Except for the first and last sgl segment , all sgl segments
* should be align to HV_HYP_PAGE_SIZE , that also means the
* maximum number of segments in a sgl can be calculated by
* dividing the total max transfer length by HV_HYP_PAGE_SIZE .
*
* 2. Except for the first and last , each entry in the SGL must
* have an offset that is a multiple of HV_HYP_PAGE_SIZE .
2015-03-27 10:27:20 +03:00
*/
2022-06-14 10:05:55 +03:00
host - > sg_tablesize = ( max_xfer_bytes > > HV_HYP_PAGE_SHIFT ) + 1 ;
2016-12-15 05:46:00 +03:00
/*
2020-01-14 03:08:36 +03:00
* For non - IDE disks , the host supports multiple channels .
2016-12-15 05:46:00 +03:00
* Set the number of HW queues we are supporting .
*/
2021-02-25 02:29:48 +03:00
if ( ! dev_is_ide ) {
if ( storvsc_max_hw_queues > num_present_cpus ) {
storvsc_max_hw_queues = 0 ;
storvsc_log ( device , STORVSC_LOGGING_WARN ,
" Resetting invalid storvsc_max_hw_queues value to default. \n " ) ;
}
if ( storvsc_max_hw_queues )
host - > nr_hw_queues = storvsc_max_hw_queues ;
else
host - > nr_hw_queues = num_present_cpus ;
}
2015-03-27 10:27:20 +03:00
2017-10-31 15:52:06 +03:00
/*
* Set the error handler work queue .
*/
host_dev - > handle_error_wq =
alloc_ordered_workqueue ( " storvsc_error_wq_%d " ,
2022-08-04 18:55:34 +03:00
0 ,
2017-10-31 15:52:06 +03:00
host - > host_no ) ;
2020-11-27 06:02:06 +03:00
if ( ! host_dev - > handle_error_wq ) {
ret = - ENOMEM ;
2017-10-31 15:52:06 +03:00
goto err_out2 ;
2020-11-27 06:02:06 +03:00
}
2017-11-01 00:58:08 +03:00
INIT_WORK ( & host_dev - > host_scan_work , storvsc_host_scan ) ;
2009-07-28 00:47:24 +04:00
/* Register the HBA and start the scsi bus scan */
2011-04-30 00:45:10 +04:00
ret = scsi_add_host ( host , & device - > device ) ;
2011-08-27 22:31:17 +04:00
if ( ret ! = 0 )
2017-10-31 15:52:06 +03:00
goto err_out3 ;
2009-07-14 03:01:31 +04:00
2011-08-27 22:31:17 +04:00
if ( ! dev_is_ide ) {
scsi_scan_host ( host ) ;
2012-01-13 00:37:55 +04:00
} else {
target = ( device - > dev_instance . b [ 5 ] < < 8 |
device - > dev_instance . b [ 4 ] ) ;
ret = scsi_add_device ( host , 0 , target , 0 ) ;
2017-04-17 21:37:46 +03:00
if ( ret )
2017-10-31 15:52:06 +03:00
goto err_out4 ;
2009-07-14 03:01:31 +04:00
}
2015-12-24 00:15:49 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
if ( host - > transportt = = fc_transport_template ) {
2017-04-17 21:37:46 +03:00
struct fc_rport_identifiers ids = {
. roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR ,
} ;
2015-12-24 00:15:49 +03:00
fc_host_node_name ( host ) = stor_device - > node_name ;
fc_host_port_name ( host ) = stor_device - > port_name ;
2017-04-17 21:37:46 +03:00
stor_device - > rport = fc_remote_port_add ( host , 0 , & ids ) ;
2018-01-16 13:40:22 +03:00
if ( ! stor_device - > rport ) {
ret = - ENOMEM ;
2017-10-31 15:52:06 +03:00
goto err_out4 ;
2018-01-16 13:40:22 +03:00
}
2015-12-24 00:15:49 +03:00
}
# endif
2011-08-27 22:31:17 +04:00
return 0 ;
2009-07-14 03:01:31 +04:00
2017-10-31 15:52:06 +03:00
err_out4 :
2017-04-17 21:37:46 +03:00
scsi_remove_host ( host ) ;
2017-10-31 15:52:06 +03:00
err_out3 :
destroy_workqueue ( host_dev - > handle_error_wq ) ;
2011-12-01 16:59:20 +04:00
err_out2 :
2011-11-08 21:01:41 +04:00
/*
* Once we have connected with the host , we would need to
2022-09-08 16:07:54 +03:00
* invoke storvsc_dev_remove ( ) to rollback this state and
2011-11-08 21:01:41 +04:00
* this call also frees up the stor_device ; hence the jump around
2011-12-01 16:59:20 +04:00
* err_out1 label .
2011-11-08 21:01:41 +04:00
*/
2011-08-27 22:31:17 +04:00
storvsc_dev_remove ( device ) ;
2011-12-01 16:59:20 +04:00
goto err_out0 ;
2011-11-08 21:01:41 +04:00
err_out1 :
2016-12-15 05:46:00 +03:00
kfree ( stor_device - > stor_chns ) ;
2011-12-01 16:59:20 +04:00
kfree ( stor_device ) ;
2011-11-08 21:01:41 +04:00
err_out0 :
2011-08-27 22:31:17 +04:00
scsi_host_put ( host ) ;
2011-11-08 21:01:41 +04:00
return ret ;
2009-07-14 03:01:31 +04:00
}
2019-06-15 02:48:22 +03:00
/* Change a scsi target's queue depth */
static int storvsc_change_queue_depth ( struct scsi_device * sdev , int queue_depth )
{
if ( queue_depth > scsi_driver . can_queue )
queue_depth = scsi_driver . can_queue ;
return scsi_change_queue_depth ( sdev , queue_depth ) ;
}
2023-01-05 17:51:23 +03:00
static void storvsc_remove ( struct hv_device * dev )
2012-01-13 00:37:59 +04:00
{
struct storvsc_device * stor_device = hv_get_drvdata ( dev ) ;
struct Scsi_Host * host = stor_device - > host ;
2017-10-31 15:52:06 +03:00
struct hv_host_device * host_dev = shost_priv ( host ) ;
2012-01-13 00:37:59 +04:00
2015-12-24 00:15:49 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2017-04-17 21:37:46 +03:00
if ( host - > transportt = = fc_transport_template ) {
fc_remote_port_delete ( stor_device - > rport ) ;
2015-12-24 00:15:49 +03:00
fc_remove_host ( host ) ;
2017-04-17 21:37:46 +03:00
}
2015-12-24 00:15:49 +03:00
# endif
2017-10-31 15:52:06 +03:00
destroy_workqueue ( host_dev - > handle_error_wq ) ;
2012-01-13 00:37:59 +04:00
scsi_remove_host ( host ) ;
storvsc_dev_remove ( dev ) ;
scsi_host_put ( host ) ;
}
2019-09-12 02:35:16 +03:00
static int storvsc_suspend ( struct hv_device * hv_dev )
{
struct storvsc_device * stor_device = hv_get_drvdata ( hv_dev ) ;
struct Scsi_Host * host = stor_device - > host ;
struct hv_host_device * host_dev = shost_priv ( host ) ;
storvsc_wait_to_drain ( stor_device ) ;
drain_workqueue ( host_dev - > handle_error_wq ) ;
vmbus_close ( hv_dev - > channel ) ;
kfree ( stor_device - > stor_chns ) ;
stor_device - > stor_chns = NULL ;
cpumask_clear ( & stor_device - > alloced_cpus ) ;
return 0 ;
}
static int storvsc_resume ( struct hv_device * hv_dev )
{
int ret ;
ret = storvsc_connect_to_vsp ( hv_dev , storvsc_ringbuffer_size ,
hv_dev_is_fc ( hv_dev ) ) ;
return ret ;
}
2011-05-10 18:56:09 +04:00
static struct hv_driver storvsc_drv = {
2011-11-08 21:01:46 +04:00
. name = KBUILD_MODNAME ,
2011-08-25 20:48:33 +04:00
. id_table = id_table ,
2011-05-10 18:56:09 +04:00
. probe = storvsc_probe ,
. remove = storvsc_remove ,
2019-09-12 02:35:16 +03:00
. suspend = storvsc_suspend ,
. resume = storvsc_resume ,
2018-06-05 23:37:49 +03:00
. driver = {
. probe_type = PROBE_PREFER_ASYNCHRONOUS ,
} ,
2011-05-10 18:54:46 +04:00
} ;
2011-05-10 18:54:45 +04:00
2015-12-24 00:15:49 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
static struct fc_function_template fc_transport_functions = {
. show_host_node_name = 1 ,
. show_host_port_name = 1 ,
} ;
# endif
2011-06-07 02:49:27 +04:00
static int __init storvsc_drv_init ( void )
2011-05-10 18:54:43 +04:00
{
2015-12-24 00:15:49 +03:00
int ret ;
2011-05-10 18:55:58 +04:00
/*
* Divide the ring buffer data size ( which is 1 page less
* than the ring buffer size since that page is reserved for
* the ring buffer indices ) by the max request size ( which is
* vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64 )
*/
max_outstanding_req_per_channel =
2011-08-26 02:07:32 +04:00
( ( storvsc_ringbuffer_size - PAGE_SIZE ) /
ALIGN ( MAX_MULTIPAGE_BUFFER_PACKET +
2020-12-17 23:33:19 +03:00
sizeof ( struct vstor_packet ) + sizeof ( u64 ) ,
2011-08-26 02:07:32 +04:00
sizeof ( u64 ) ) ) ;
2011-05-10 18:54:43 +04:00
2015-12-24 00:15:49 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
fc_transport_template = fc_attach_transport ( & fc_transport_functions ) ;
if ( ! fc_transport_template )
return - ENODEV ;
# endif
ret = vmbus_driver_register ( & storvsc_drv ) ;
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
if ( ret )
fc_release_transport ( fc_transport_template ) ;
# endif
return ret ;
2011-05-10 18:54:43 +04:00
}
2011-06-07 02:49:26 +04:00
static void __exit storvsc_drv_exit ( void )
2011-05-10 18:54:43 +04:00
{
2011-08-26 02:07:32 +04:00
vmbus_driver_unregister ( & storvsc_drv ) ;
2015-12-24 00:15:49 +03:00
# if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
fc_release_transport ( fc_transport_template ) ;
# endif
2011-05-10 18:54:43 +04:00
}
2009-09-02 19:37:47 +04:00
MODULE_LICENSE ( " GPL " ) ;
2010-05-07 08:44:45 +04:00
MODULE_DESCRIPTION ( " Microsoft Hyper-V virtual storage driver " ) ;
2011-06-07 02:49:27 +04:00
module_init ( storvsc_drv_init ) ;
2011-06-07 02:49:26 +04:00
module_exit ( storvsc_drv_exit ) ;