storvsc_drv.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <haiyangz@microsoft.com>
  7. * Hank Janssen <hjanssen@microsoft.com>
  8. * K. Y. Srinivasan <kys@microsoft.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/wait.h>
  12. #include <linux/sched.h>
  13. #include <linux/completion.h>
  14. #include <linux/string.h>
  15. #include <linux/mm.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/hyperv.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/dma-mapping.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #include <scsi/scsi_host.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_tcq.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_devinfo.h>
  31. #include <scsi/scsi_dbg.h>
  32. #include <scsi/scsi_transport_fc.h>
  33. #include <scsi/scsi_transport.h>
  34. /*
  35. * All wire protocol details (storage protocol between the guest and the host)
  36. * are consolidated here.
  37. *
  38. * Begin protocol definitions.
  39. */
  40. /*
  41. * Version history:
  42. * V1 Beta: 0.1
  43. * V1 RC < 2008/1/31: 1.0
  44. * V1 RC > 2008/1/31: 2.0
  45. * Win7: 4.2
  46. * Win8: 5.1
  47. * Win8.1: 6.0
  48. * Win10: 6.2
  49. */
  50. #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
  51. (((MINOR_) & 0xff)))
  52. #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
  53. #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
  54. #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
  55. #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
  56. #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
  57. /* channel callback timeout in ms */
  58. #define CALLBACK_TIMEOUT 2
  59. /* Packet structure describing virtual storage requests. */
  60. enum vstor_packet_operation {
  61. VSTOR_OPERATION_COMPLETE_IO = 1,
  62. VSTOR_OPERATION_REMOVE_DEVICE = 2,
  63. VSTOR_OPERATION_EXECUTE_SRB = 3,
  64. VSTOR_OPERATION_RESET_LUN = 4,
  65. VSTOR_OPERATION_RESET_ADAPTER = 5,
  66. VSTOR_OPERATION_RESET_BUS = 6,
  67. VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
  68. VSTOR_OPERATION_END_INITIALIZATION = 8,
  69. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
  70. VSTOR_OPERATION_QUERY_PROPERTIES = 10,
  71. VSTOR_OPERATION_ENUMERATE_BUS = 11,
  72. VSTOR_OPERATION_FCHBA_DATA = 12,
  73. VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
  74. VSTOR_OPERATION_MAXIMUM = 13
  75. };
  76. /*
  77. * WWN packet for Fibre Channel HBA
  78. */
  79. struct hv_fc_wwn_packet {
  80. u8 primary_active;
  81. u8 reserved1[3];
  82. u8 primary_port_wwn[8];
  83. u8 primary_node_wwn[8];
  84. u8 secondary_port_wwn[8];
  85. u8 secondary_node_wwn[8];
  86. };
  87. /*
  88. * SRB Flag Bits
  89. */
  90. #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
  91. #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
  92. #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
  93. #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
  94. #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
  95. #define SRB_FLAGS_DATA_IN 0x00000040
  96. #define SRB_FLAGS_DATA_OUT 0x00000080
  97. #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
  98. #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
  99. #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
  100. #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
  101. #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
  102. /*
  103. * This flag indicates the request is part of the workflow for processing a D3.
  104. */
  105. #define SRB_FLAGS_D3_PROCESSING 0x00000800
  106. #define SRB_FLAGS_IS_ACTIVE 0x00010000
  107. #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
  108. #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
  109. #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
  110. #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
  111. #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
  112. #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
  113. #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
  114. #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
  115. #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
  116. #define SP_UNTAGGED ((unsigned char) ~0)
  117. #define SRB_SIMPLE_TAG_REQUEST 0x20
  118. /*
  119. * Platform neutral description of a scsi request -
  120. * this remains the same across the write regardless of 32/64 bit
  121. * note: it's patterned off the SCSI_PASS_THROUGH structure
  122. */
  123. #define STORVSC_MAX_CMD_LEN 0x10
  124. /* Sense buffer size is the same for all versions since Windows 8 */
  125. #define STORVSC_SENSE_BUFFER_SIZE 0x14
  126. #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
  127. /*
  128. * The storage protocol version is determined during the
  129. * initial exchange with the host. It will indicate which
  130. * storage functionality is available in the host.
  131. */
  132. static int vmstor_proto_version;
  133. static bool hv_dev_is_fc(struct hv_device *hv_dev);
  134. #define STORVSC_LOGGING_NONE 0
  135. #define STORVSC_LOGGING_ERROR 1
  136. #define STORVSC_LOGGING_WARN 2
  137. static int logging_level = STORVSC_LOGGING_ERROR;
  138. module_param(logging_level, int, S_IRUGO|S_IWUSR);
  139. MODULE_PARM_DESC(logging_level,
  140. "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
  141. static inline bool do_logging(int level)
  142. {
  143. return logging_level >= level;
  144. }
  145. #define storvsc_log(dev, level, fmt, ...) \
  146. do { \
  147. if (do_logging(level)) \
  148. dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
  149. } while (0)
  150. #define storvsc_log_ratelimited(dev, level, fmt, ...) \
  151. do { \
  152. if (do_logging(level)) \
  153. dev_warn_ratelimited(&(dev)->device, fmt, ##__VA_ARGS__); \
  154. } while (0)
  155. struct vmscsi_request {
  156. u16 length;
  157. u8 srb_status;
  158. u8 scsi_status;
  159. u8 port_number;
  160. u8 path_id;
  161. u8 target_id;
  162. u8 lun;
  163. u8 cdb_length;
  164. u8 sense_info_length;
  165. u8 data_in;
  166. u8 reserved;
  167. u32 data_transfer_length;
  168. union {
  169. u8 cdb[STORVSC_MAX_CMD_LEN];
  170. u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
  171. u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
  172. };
  173. /*
  174. * The following was added in win8.
  175. */
  176. u16 reserve;
  177. u8 queue_tag;
  178. u8 queue_action;
  179. u32 srb_flags;
  180. u32 time_out_value;
  181. u32 queue_sort_ey;
  182. } __attribute((packed));
  183. /*
  184. * The list of windows version in order of preference.
  185. */
  186. static const int protocol_version[] = {
  187. VMSTOR_PROTO_VERSION_WIN10,
  188. VMSTOR_PROTO_VERSION_WIN8_1,
  189. VMSTOR_PROTO_VERSION_WIN8,
  190. };
  191. /*
  192. * This structure is sent during the initialization phase to get the different
  193. * properties of the channel.
  194. */
  195. #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
  196. struct vmstorage_channel_properties {
  197. u32 reserved;
  198. u16 max_channel_cnt;
  199. u16 reserved1;
  200. u32 flags;
  201. u32 max_transfer_bytes;
  202. u64 reserved2;
  203. } __packed;
  204. /* This structure is sent during the storage protocol negotiations. */
  205. struct vmstorage_protocol_version {
  206. /* Major (MSW) and minor (LSW) version numbers. */
  207. u16 major_minor;
  208. /*
  209. * Revision number is auto-incremented whenever this file is changed
  210. * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
  211. * definitely indicate incompatibility--but it does indicate mismatched
  212. * builds.
  213. * This is only used on the windows side. Just set it to 0.
  214. */
  215. u16 revision;
  216. } __packed;
  217. /* Channel Property Flags */
  218. #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
  219. #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
  220. struct vstor_packet {
  221. /* Requested operation type */
  222. enum vstor_packet_operation operation;
  223. /* Flags - see below for values */
  224. u32 flags;
  225. /* Status of the request returned from the server side. */
  226. u32 status;
  227. /* Data payload area */
  228. union {
  229. /*
  230. * Structure used to forward SCSI commands from the
  231. * client to the server.
  232. */
  233. struct vmscsi_request vm_srb;
  234. /* Structure used to query channel properties. */
  235. struct vmstorage_channel_properties storage_channel_properties;
  236. /* Used during version negotiations. */
  237. struct vmstorage_protocol_version version;
  238. /* Fibre channel address packet */
  239. struct hv_fc_wwn_packet wwn_packet;
  240. /* Number of sub-channels to create */
  241. u16 sub_channel_count;
  242. /* This will be the maximum of the union members */
  243. u8 buffer[0x34];
  244. };
  245. } __packed;
  246. /*
  247. * Packet Flags:
  248. *
  249. * This flag indicates that the server should send back a completion for this
  250. * packet.
  251. */
  252. #define REQUEST_COMPLETION_FLAG 0x1
  253. /* Matches Windows-end */
  254. enum storvsc_request_type {
  255. WRITE_TYPE = 0,
  256. READ_TYPE,
  257. UNKNOWN_TYPE,
  258. };
  259. /*
  260. * SRB status codes and masks. In the 8-bit field, the two high order bits
  261. * are flags, while the remaining 6 bits are an integer status code. The
  262. * definitions here include only the subset of the integer status codes that
  263. * are tested for in this driver.
  264. */
  265. #define SRB_STATUS_AUTOSENSE_VALID 0x80
  266. #define SRB_STATUS_QUEUE_FROZEN 0x40
  267. /* SRB status integer codes */
  268. #define SRB_STATUS_SUCCESS 0x01
  269. #define SRB_STATUS_ABORTED 0x02
  270. #define SRB_STATUS_ERROR 0x04
  271. #define SRB_STATUS_INVALID_REQUEST 0x06
  272. #define SRB_STATUS_TIMEOUT 0x09
  273. #define SRB_STATUS_SELECTION_TIMEOUT 0x0A
  274. #define SRB_STATUS_BUS_RESET 0x0E
  275. #define SRB_STATUS_DATA_OVERRUN 0x12
  276. #define SRB_STATUS_INVALID_LUN 0x20
  277. #define SRB_STATUS_INTERNAL_ERROR 0x30
  278. #define SRB_STATUS(status) \
  279. (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
  280. /*
  281. * This is the end of Protocol specific defines.
  282. */
  283. static int storvsc_ringbuffer_size = (128 * 1024);
  284. static int aligned_ringbuffer_size;
  285. static u32 max_outstanding_req_per_channel;
  286. static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth);
  287. static int storvsc_vcpus_per_sub_channel = 4;
  288. static unsigned int storvsc_max_hw_queues;
  289. module_param(storvsc_ringbuffer_size, int, S_IRUGO);
  290. MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
  291. module_param(storvsc_max_hw_queues, uint, 0644);
  292. MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues");
  293. module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
  294. MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
  295. static int ring_avail_percent_lowater = 10;
  296. module_param(ring_avail_percent_lowater, int, S_IRUGO);
  297. MODULE_PARM_DESC(ring_avail_percent_lowater,
  298. "Select a channel if available ring size > this in percent");
  299. /*
  300. * Timeout in seconds for all devices managed by this driver.
  301. */
  302. static int storvsc_timeout = 180;
  303. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  304. static struct scsi_transport_template *fc_transport_template;
  305. #endif
  306. static struct scsi_host_template scsi_driver;
  307. static void storvsc_on_channel_callback(void *context);
  308. #define STORVSC_MAX_LUNS_PER_TARGET 255
  309. #define STORVSC_MAX_TARGETS 2
  310. #define STORVSC_MAX_CHANNELS 8
  311. #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
  312. #define STORVSC_FC_MAX_TARGETS 128
  313. #define STORVSC_FC_MAX_CHANNELS 8
  314. #define STORVSC_FC_MAX_XFER_SIZE ((u32)(512 * 1024))
  315. #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
  316. #define STORVSC_IDE_MAX_TARGETS 1
  317. #define STORVSC_IDE_MAX_CHANNELS 1
  318. /*
  319. * Upper bound on the size of a storvsc packet.
  320. */
  321. #define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
  322. sizeof(struct vstor_packet))
  323. struct storvsc_cmd_request {
  324. struct scsi_cmnd *cmd;
  325. struct hv_device *device;
  326. /* Synchronize the request/response if needed */
  327. struct completion wait_event;
  328. struct vmbus_channel_packet_multipage_buffer mpb;
  329. struct vmbus_packet_mpb_array *payload;
  330. u32 payload_sz;
  331. struct vstor_packet vstor_packet;
  332. };
  333. /* A storvsc device is a device object that contains a vmbus channel */
  334. struct storvsc_device {
  335. struct hv_device *device;
  336. bool destroy;
  337. bool drain_notify;
  338. atomic_t num_outstanding_req;
  339. struct Scsi_Host *host;
  340. wait_queue_head_t waiting_to_drain;
  341. /*
  342. * Each unique Port/Path/Target represents 1 channel ie scsi
  343. * controller. In reality, the pathid, targetid is always 0
  344. * and the port is set by us
  345. */
  346. unsigned int port_number;
  347. unsigned char path_id;
  348. unsigned char target_id;
  349. /*
  350. * Max I/O, the device can support.
  351. */
  352. u32 max_transfer_bytes;
  353. /*
  354. * Number of sub-channels we will open.
  355. */
  356. u16 num_sc;
  357. struct vmbus_channel **stor_chns;
  358. /*
  359. * Mask of CPUs bound to subchannels.
  360. */
  361. struct cpumask alloced_cpus;
  362. /*
  363. * Serializes modifications of stor_chns[] from storvsc_do_io()
  364. * and storvsc_change_target_cpu().
  365. */
  366. spinlock_t lock;
  367. /* Used for vsc/vsp channel reset process */
  368. struct storvsc_cmd_request init_request;
  369. struct storvsc_cmd_request reset_request;
  370. /*
  371. * Currently active port and node names for FC devices.
  372. */
  373. u64 node_name;
  374. u64 port_name;
  375. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  376. struct fc_rport *rport;
  377. #endif
  378. };
  379. struct hv_host_device {
  380. struct hv_device *dev;
  381. unsigned int port;
  382. unsigned char path;
  383. unsigned char target;
  384. struct workqueue_struct *handle_error_wq;
  385. struct work_struct host_scan_work;
  386. struct Scsi_Host *host;
  387. };
  388. struct storvsc_scan_work {
  389. struct work_struct work;
  390. struct Scsi_Host *host;
  391. u8 lun;
  392. u8 tgt_id;
  393. };
  394. static void storvsc_device_scan(struct work_struct *work)
  395. {
  396. struct storvsc_scan_work *wrk;
  397. struct scsi_device *sdev;
  398. wrk = container_of(work, struct storvsc_scan_work, work);
  399. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  400. if (!sdev)
  401. goto done;
  402. scsi_rescan_device(sdev);
  403. scsi_device_put(sdev);
  404. done:
  405. kfree(wrk);
  406. }
  407. static void storvsc_host_scan(struct work_struct *work)
  408. {
  409. struct Scsi_Host *host;
  410. struct scsi_device *sdev;
  411. struct hv_host_device *host_device =
  412. container_of(work, struct hv_host_device, host_scan_work);
  413. host = host_device->host;
  414. /*
  415. * Before scanning the host, first check to see if any of the
  416. * currently known devices have been hot removed. We issue a
  417. * "unit ready" command against all currently known devices.
  418. * This I/O will result in an error for devices that have been
  419. * removed. As part of handling the I/O error, we remove the device.
  420. *
  421. * When a LUN is added or removed, the host sends us a signal to
  422. * scan the host. Thus we are forced to discover the LUNs that
  423. * may have been removed this way.
  424. */
  425. mutex_lock(&host->scan_mutex);
  426. shost_for_each_device(sdev, host)
  427. scsi_test_unit_ready(sdev, 1, 1, NULL);
  428. mutex_unlock(&host->scan_mutex);
  429. /*
  430. * Now scan the host to discover LUNs that may have been added.
  431. */
  432. scsi_scan_host(host);
  433. }
  434. static void storvsc_remove_lun(struct work_struct *work)
  435. {
  436. struct storvsc_scan_work *wrk;
  437. struct scsi_device *sdev;
  438. wrk = container_of(work, struct storvsc_scan_work, work);
  439. if (!scsi_host_get(wrk->host))
  440. goto done;
  441. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  442. if (sdev) {
  443. scsi_remove_device(sdev);
  444. scsi_device_put(sdev);
  445. }
  446. scsi_host_put(wrk->host);
  447. done:
  448. kfree(wrk);
  449. }
  450. /*
  451. * We can get incoming messages from the host that are not in response to
  452. * messages that we have sent out. An example of this would be messages
  453. * received by the guest to notify dynamic addition/removal of LUNs. To
  454. * deal with potential race conditions where the driver may be in the
  455. * midst of being unloaded when we might receive an unsolicited message
  456. * from the host, we have implemented a mechanism to gurantee sequential
  457. * consistency:
  458. *
  459. * 1) Once the device is marked as being destroyed, we will fail all
  460. * outgoing messages.
  461. * 2) We permit incoming messages when the device is being destroyed,
  462. * only to properly account for messages already sent out.
  463. */
  464. static inline struct storvsc_device *get_out_stor_device(
  465. struct hv_device *device)
  466. {
  467. struct storvsc_device *stor_device;
  468. stor_device = hv_get_drvdata(device);
  469. if (stor_device && stor_device->destroy)
  470. stor_device = NULL;
  471. return stor_device;
  472. }
  473. static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
  474. {
  475. dev->drain_notify = true;
  476. wait_event(dev->waiting_to_drain,
  477. atomic_read(&dev->num_outstanding_req) == 0);
  478. dev->drain_notify = false;
  479. }
  480. static inline struct storvsc_device *get_in_stor_device(
  481. struct hv_device *device)
  482. {
  483. struct storvsc_device *stor_device;
  484. stor_device = hv_get_drvdata(device);
  485. if (!stor_device)
  486. goto get_in_err;
  487. /*
  488. * If the device is being destroyed; allow incoming
  489. * traffic only to cleanup outstanding requests.
  490. */
  491. if (stor_device->destroy &&
  492. (atomic_read(&stor_device->num_outstanding_req) == 0))
  493. stor_device = NULL;
  494. get_in_err:
  495. return stor_device;
  496. }
  497. static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
  498. u32 new)
  499. {
  500. struct storvsc_device *stor_device;
  501. struct vmbus_channel *cur_chn;
  502. bool old_is_alloced = false;
  503. struct hv_device *device;
  504. unsigned long flags;
  505. int cpu;
  506. device = channel->primary_channel ?
  507. channel->primary_channel->device_obj
  508. : channel->device_obj;
  509. stor_device = get_out_stor_device(device);
  510. if (!stor_device)
  511. return;
  512. /* See storvsc_do_io() -> get_og_chn(). */
  513. spin_lock_irqsave(&stor_device->lock, flags);
  514. /*
  515. * Determines if the storvsc device has other channels assigned to
  516. * the "old" CPU to update the alloced_cpus mask and the stor_chns
  517. * array.
  518. */
  519. if (device->channel != channel && device->channel->target_cpu == old) {
  520. cur_chn = device->channel;
  521. old_is_alloced = true;
  522. goto old_is_alloced;
  523. }
  524. list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) {
  525. if (cur_chn == channel)
  526. continue;
  527. if (cur_chn->target_cpu == old) {
  528. old_is_alloced = true;
  529. goto old_is_alloced;
  530. }
  531. }
  532. old_is_alloced:
  533. if (old_is_alloced)
  534. WRITE_ONCE(stor_device->stor_chns[old], cur_chn);
  535. else
  536. cpumask_clear_cpu(old, &stor_device->alloced_cpus);
  537. /* "Flush" the stor_chns array. */
  538. for_each_possible_cpu(cpu) {
  539. if (stor_device->stor_chns[cpu] && !cpumask_test_cpu(
  540. cpu, &stor_device->alloced_cpus))
  541. WRITE_ONCE(stor_device->stor_chns[cpu], NULL);
  542. }
  543. WRITE_ONCE(stor_device->stor_chns[new], channel);
  544. cpumask_set_cpu(new, &stor_device->alloced_cpus);
  545. spin_unlock_irqrestore(&stor_device->lock, flags);
  546. }
  547. static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
  548. {
  549. struct storvsc_cmd_request *request =
  550. (struct storvsc_cmd_request *)(unsigned long)rqst_addr;
  551. if (rqst_addr == VMBUS_RQST_INIT)
  552. return VMBUS_RQST_INIT;
  553. if (rqst_addr == VMBUS_RQST_RESET)
  554. return VMBUS_RQST_RESET;
  555. /*
  556. * Cannot return an ID of 0, which is reserved for an unsolicited
  557. * message from Hyper-V.
  558. */
  559. return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1;
  560. }
  561. static void handle_sc_creation(struct vmbus_channel *new_sc)
  562. {
  563. struct hv_device *device = new_sc->primary_channel->device_obj;
  564. struct device *dev = &device->device;
  565. struct storvsc_device *stor_device;
  566. struct vmstorage_channel_properties props;
  567. int ret;
  568. stor_device = get_out_stor_device(device);
  569. if (!stor_device)
  570. return;
  571. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  572. new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
  573. new_sc->next_request_id_callback = storvsc_next_request_id;
  574. ret = vmbus_open(new_sc,
  575. aligned_ringbuffer_size,
  576. aligned_ringbuffer_size,
  577. (void *)&props,
  578. sizeof(struct vmstorage_channel_properties),
  579. storvsc_on_channel_callback, new_sc);
  580. /* In case vmbus_open() fails, we don't use the sub-channel. */
  581. if (ret != 0) {
  582. dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
  583. return;
  584. }
  585. new_sc->change_target_cpu_callback = storvsc_change_target_cpu;
  586. /* Add the sub-channel to the array of available channels. */
  587. stor_device->stor_chns[new_sc->target_cpu] = new_sc;
  588. cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
  589. }
  590. static void handle_multichannel_storage(struct hv_device *device, int max_chns)
  591. {
  592. struct device *dev = &device->device;
  593. struct storvsc_device *stor_device;
  594. int num_sc;
  595. struct storvsc_cmd_request *request;
  596. struct vstor_packet *vstor_packet;
  597. int ret, t;
  598. /*
  599. * If the number of CPUs is artificially restricted, such as
  600. * with maxcpus=1 on the kernel boot line, Hyper-V could offer
  601. * sub-channels >= the number of CPUs. These sub-channels
  602. * should not be created. The primary channel is already created
  603. * and assigned to one CPU, so check against # CPUs - 1.
  604. */
  605. num_sc = min((int)(num_online_cpus() - 1), max_chns);
  606. if (!num_sc)
  607. return;
  608. stor_device = get_out_stor_device(device);
  609. if (!stor_device)
  610. return;
  611. stor_device->num_sc = num_sc;
  612. request = &stor_device->init_request;
  613. vstor_packet = &request->vstor_packet;
  614. /*
  615. * Establish a handler for dealing with subchannels.
  616. */
  617. vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
  618. /*
  619. * Request the host to create sub-channels.
  620. */
  621. memset(request, 0, sizeof(struct storvsc_cmd_request));
  622. init_completion(&request->wait_event);
  623. vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
  624. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  625. vstor_packet->sub_channel_count = num_sc;
  626. ret = vmbus_sendpacket(device->channel, vstor_packet,
  627. sizeof(struct vstor_packet),
  628. VMBUS_RQST_INIT,
  629. VM_PKT_DATA_INBAND,
  630. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  631. if (ret != 0) {
  632. dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
  633. return;
  634. }
  635. t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
  636. if (t == 0) {
  637. dev_err(dev, "Failed to create sub-channel: timed out\n");
  638. return;
  639. }
  640. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  641. vstor_packet->status != 0) {
  642. dev_err(dev, "Failed to create sub-channel: op=%d, sts=%d\n",
  643. vstor_packet->operation, vstor_packet->status);
  644. return;
  645. }
  646. /*
  647. * We need to do nothing here, because vmbus_process_offer()
  648. * invokes channel->sc_creation_callback, which will open and use
  649. * the sub-channel(s).
  650. */
  651. }
  652. static void cache_wwn(struct storvsc_device *stor_device,
  653. struct vstor_packet *vstor_packet)
  654. {
  655. /*
  656. * Cache the currently active port and node ww names.
  657. */
  658. if (vstor_packet->wwn_packet.primary_active) {
  659. stor_device->node_name =
  660. wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
  661. stor_device->port_name =
  662. wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
  663. } else {
  664. stor_device->node_name =
  665. wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
  666. stor_device->port_name =
  667. wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
  668. }
  669. }
  670. static int storvsc_execute_vstor_op(struct hv_device *device,
  671. struct storvsc_cmd_request *request,
  672. bool status_check)
  673. {
  674. struct storvsc_device *stor_device;
  675. struct vstor_packet *vstor_packet;
  676. int ret, t;
  677. stor_device = get_out_stor_device(device);
  678. if (!stor_device)
  679. return -ENODEV;
  680. vstor_packet = &request->vstor_packet;
  681. init_completion(&request->wait_event);
  682. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  683. ret = vmbus_sendpacket(device->channel, vstor_packet,
  684. sizeof(struct vstor_packet),
  685. VMBUS_RQST_INIT,
  686. VM_PKT_DATA_INBAND,
  687. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  688. if (ret != 0)
  689. return ret;
  690. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  691. if (t == 0)
  692. return -ETIMEDOUT;
  693. if (!status_check)
  694. return ret;
  695. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  696. vstor_packet->status != 0)
  697. return -EINVAL;
  698. return ret;
  699. }
  700. static int storvsc_channel_init(struct hv_device *device, bool is_fc)
  701. {
  702. struct storvsc_device *stor_device;
  703. struct storvsc_cmd_request *request;
  704. struct vstor_packet *vstor_packet;
  705. int ret, i;
  706. int max_chns;
  707. bool process_sub_channels = false;
  708. stor_device = get_out_stor_device(device);
  709. if (!stor_device)
  710. return -ENODEV;
  711. request = &stor_device->init_request;
  712. vstor_packet = &request->vstor_packet;
  713. /*
  714. * Now, initiate the vsc/vsp initialization protocol on the open
  715. * channel
  716. */
  717. memset(request, 0, sizeof(struct storvsc_cmd_request));
  718. vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
  719. ret = storvsc_execute_vstor_op(device, request, true);
  720. if (ret)
  721. return ret;
  722. /*
  723. * Query host supported protocol version.
  724. */
  725. for (i = 0; i < ARRAY_SIZE(protocol_version); i++) {
  726. /* reuse the packet for version range supported */
  727. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  728. vstor_packet->operation =
  729. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
  730. vstor_packet->version.major_minor = protocol_version[i];
  731. /*
  732. * The revision number is only used in Windows; set it to 0.
  733. */
  734. vstor_packet->version.revision = 0;
  735. ret = storvsc_execute_vstor_op(device, request, false);
  736. if (ret != 0)
  737. return ret;
  738. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
  739. return -EINVAL;
  740. if (vstor_packet->status == 0) {
  741. vmstor_proto_version = protocol_version[i];
  742. break;
  743. }
  744. }
  745. if (vstor_packet->status != 0) {
  746. dev_err(&device->device, "Obsolete Hyper-V version\n");
  747. return -EINVAL;
  748. }
  749. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  750. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
  751. ret = storvsc_execute_vstor_op(device, request, true);
  752. if (ret != 0)
  753. return ret;
  754. /*
  755. * Check to see if multi-channel support is there.
  756. * Hosts that implement protocol version of 5.1 and above
  757. * support multi-channel.
  758. */
  759. max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
  760. /*
  761. * Allocate state to manage the sub-channels.
  762. * We allocate an array based on the numbers of possible CPUs
  763. * (Hyper-V does not support cpu online/offline).
  764. * This Array will be sparseley populated with unique
  765. * channels - primary + sub-channels.
  766. * We will however populate all the slots to evenly distribute
  767. * the load.
  768. */
  769. stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *),
  770. GFP_KERNEL);
  771. if (stor_device->stor_chns == NULL)
  772. return -ENOMEM;
  773. device->channel->change_target_cpu_callback = storvsc_change_target_cpu;
  774. stor_device->stor_chns[device->channel->target_cpu] = device->channel;
  775. cpumask_set_cpu(device->channel->target_cpu,
  776. &stor_device->alloced_cpus);
  777. if (vstor_packet->storage_channel_properties.flags &
  778. STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
  779. process_sub_channels = true;
  780. stor_device->max_transfer_bytes =
  781. vstor_packet->storage_channel_properties.max_transfer_bytes;
  782. if (!is_fc)
  783. goto done;
  784. /*
  785. * For FC devices retrieve FC HBA data.
  786. */
  787. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  788. vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
  789. ret = storvsc_execute_vstor_op(device, request, true);
  790. if (ret != 0)
  791. return ret;
  792. /*
  793. * Cache the currently active port and node ww names.
  794. */
  795. cache_wwn(stor_device, vstor_packet);
  796. done:
  797. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  798. vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
  799. ret = storvsc_execute_vstor_op(device, request, true);
  800. if (ret != 0)
  801. return ret;
  802. if (process_sub_channels)
  803. handle_multichannel_storage(device, max_chns);
  804. return ret;
  805. }
  806. static void storvsc_handle_error(struct vmscsi_request *vm_srb,
  807. struct scsi_cmnd *scmnd,
  808. struct Scsi_Host *host,
  809. u8 asc, u8 ascq)
  810. {
  811. struct storvsc_scan_work *wrk;
  812. void (*process_err_fn)(struct work_struct *work);
  813. struct hv_host_device *host_dev = shost_priv(host);
  814. switch (SRB_STATUS(vm_srb->srb_status)) {
  815. case SRB_STATUS_ERROR:
  816. case SRB_STATUS_ABORTED:
  817. case SRB_STATUS_INVALID_REQUEST:
  818. case SRB_STATUS_INTERNAL_ERROR:
  819. case SRB_STATUS_TIMEOUT:
  820. case SRB_STATUS_SELECTION_TIMEOUT:
  821. case SRB_STATUS_BUS_RESET:
  822. case SRB_STATUS_DATA_OVERRUN:
  823. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) {
  824. /* Check for capacity change */
  825. if ((asc == 0x2a) && (ascq == 0x9)) {
  826. process_err_fn = storvsc_device_scan;
  827. /* Retry the I/O that triggered this. */
  828. set_host_byte(scmnd, DID_REQUEUE);
  829. goto do_work;
  830. }
  831. /*
  832. * Check for "Operating parameters have changed"
  833. * due to Hyper-V changing the VHD/VHDX BlockSize
  834. * when adding/removing a differencing disk. This
  835. * causes discard_granularity to change, so do a
  836. * rescan to pick up the new granularity. We don't
  837. * want scsi_report_sense() to output a message
  838. * that a sysadmin wouldn't know what to do with.
  839. */
  840. if ((asc == 0x3f) && (ascq != 0x03) &&
  841. (ascq != 0x0e)) {
  842. process_err_fn = storvsc_device_scan;
  843. set_host_byte(scmnd, DID_REQUEUE);
  844. goto do_work;
  845. }
  846. /*
  847. * Otherwise, let upper layer deal with the
  848. * error when sense message is present
  849. */
  850. return;
  851. }
  852. /*
  853. * If there is an error; offline the device since all
  854. * error recovery strategies would have already been
  855. * deployed on the host side. However, if the command
  856. * were a pass-through command deal with it appropriately.
  857. */
  858. switch (scmnd->cmnd[0]) {
  859. case ATA_16:
  860. case ATA_12:
  861. set_host_byte(scmnd, DID_PASSTHROUGH);
  862. break;
  863. /*
  864. * On some Hyper-V hosts TEST_UNIT_READY command can
  865. * return SRB_STATUS_ERROR. Let the upper level code
  866. * deal with it based on the sense information.
  867. */
  868. case TEST_UNIT_READY:
  869. break;
  870. default:
  871. set_host_byte(scmnd, DID_ERROR);
  872. }
  873. return;
  874. case SRB_STATUS_INVALID_LUN:
  875. set_host_byte(scmnd, DID_NO_CONNECT);
  876. process_err_fn = storvsc_remove_lun;
  877. goto do_work;
  878. }
  879. return;
  880. do_work:
  881. /*
  882. * We need to schedule work to process this error; schedule it.
  883. */
  884. wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  885. if (!wrk) {
  886. set_host_byte(scmnd, DID_BAD_TARGET);
  887. return;
  888. }
  889. wrk->host = host;
  890. wrk->lun = vm_srb->lun;
  891. wrk->tgt_id = vm_srb->target_id;
  892. INIT_WORK(&wrk->work, process_err_fn);
  893. queue_work(host_dev->handle_error_wq, &wrk->work);
  894. }
  895. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
  896. struct storvsc_device *stor_dev)
  897. {
  898. struct scsi_cmnd *scmnd = cmd_request->cmd;
  899. struct scsi_sense_hdr sense_hdr;
  900. struct vmscsi_request *vm_srb;
  901. u32 data_transfer_length;
  902. struct Scsi_Host *host;
  903. u32 payload_sz = cmd_request->payload_sz;
  904. void *payload = cmd_request->payload;
  905. bool sense_ok;
  906. host = stor_dev->host;
  907. vm_srb = &cmd_request->vstor_packet.vm_srb;
  908. data_transfer_length = vm_srb->data_transfer_length;
  909. scmnd->result = vm_srb->scsi_status;
  910. if (scmnd->result) {
  911. sense_ok = scsi_normalize_sense(scmnd->sense_buffer,
  912. SCSI_SENSE_BUFFERSIZE, &sense_hdr);
  913. if (sense_ok && do_logging(STORVSC_LOGGING_WARN))
  914. scsi_print_sense_hdr(scmnd->device, "storvsc",
  915. &sense_hdr);
  916. }
  917. if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
  918. storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
  919. sense_hdr.ascq);
  920. /*
  921. * The Windows driver set data_transfer_length on
  922. * SRB_STATUS_DATA_OVERRUN. On other errors, this value
  923. * is untouched. In these cases we set it to 0.
  924. */
  925. if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
  926. data_transfer_length = 0;
  927. }
  928. /* Validate data_transfer_length (from Hyper-V) */
  929. if (data_transfer_length > cmd_request->payload->range.len)
  930. data_transfer_length = cmd_request->payload->range.len;
  931. scsi_set_resid(scmnd,
  932. cmd_request->payload->range.len - data_transfer_length);
  933. scsi_done(scmnd);
  934. if (payload_sz >
  935. sizeof(struct vmbus_channel_packet_multipage_buffer))
  936. kfree(payload);
  937. }
  938. static void storvsc_on_io_completion(struct storvsc_device *stor_device,
  939. struct vstor_packet *vstor_packet,
  940. struct storvsc_cmd_request *request)
  941. {
  942. struct vstor_packet *stor_pkt;
  943. struct hv_device *device = stor_device->device;
  944. stor_pkt = &request->vstor_packet;
  945. /*
  946. * The current SCSI handling on the host side does
  947. * not correctly handle:
  948. * INQUIRY command with page code parameter set to 0x80
  949. * MODE_SENSE command with cmd[2] == 0x1c
  950. * MAINTENANCE_IN is not supported by HyperV FC passthrough
  951. *
  952. * Setup srb and scsi status so this won't be fatal.
  953. * We do this so we can distinguish truly fatal failues
  954. * (srb status == 0x4) and off-line the device in that case.
  955. */
  956. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  957. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE) ||
  958. (stor_pkt->vm_srb.cdb[0] == MAINTENANCE_IN &&
  959. hv_dev_is_fc(device))) {
  960. vstor_packet->vm_srb.scsi_status = 0;
  961. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  962. }
  963. /* Copy over the status...etc */
  964. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  965. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  966. /*
  967. * Copy over the sense_info_length, but limit to the known max
  968. * size if Hyper-V returns a bad value.
  969. */
  970. stor_pkt->vm_srb.sense_info_length = min_t(u8, STORVSC_SENSE_BUFFER_SIZE,
  971. vstor_packet->vm_srb.sense_info_length);
  972. if (vstor_packet->vm_srb.scsi_status != 0 ||
  973. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS) {
  974. /*
  975. * Log TEST_UNIT_READY errors only as warnings. Hyper-V can
  976. * return errors when detecting devices using TEST_UNIT_READY,
  977. * and logging these as errors produces unhelpful noise.
  978. */
  979. int loglevel = (stor_pkt->vm_srb.cdb[0] == TEST_UNIT_READY) ?
  980. STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR;
  981. storvsc_log_ratelimited(device, loglevel,
  982. "tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x\n",
  983. scsi_cmd_to_rq(request->cmd)->tag,
  984. stor_pkt->vm_srb.cdb[0],
  985. vstor_packet->vm_srb.scsi_status,
  986. vstor_packet->vm_srb.srb_status,
  987. vstor_packet->status);
  988. }
  989. if (vstor_packet->vm_srb.scsi_status == SAM_STAT_CHECK_CONDITION &&
  990. (vstor_packet->vm_srb.srb_status & SRB_STATUS_AUTOSENSE_VALID))
  991. memcpy(request->cmd->sense_buffer,
  992. vstor_packet->vm_srb.sense_data,
  993. stor_pkt->vm_srb.sense_info_length);
  994. stor_pkt->vm_srb.data_transfer_length =
  995. vstor_packet->vm_srb.data_transfer_length;
  996. storvsc_command_completion(request, stor_device);
  997. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  998. stor_device->drain_notify)
  999. wake_up(&stor_device->waiting_to_drain);
  1000. }
  1001. static void storvsc_on_receive(struct storvsc_device *stor_device,
  1002. struct vstor_packet *vstor_packet,
  1003. struct storvsc_cmd_request *request)
  1004. {
  1005. struct hv_host_device *host_dev;
  1006. switch (vstor_packet->operation) {
  1007. case VSTOR_OPERATION_COMPLETE_IO:
  1008. storvsc_on_io_completion(stor_device, vstor_packet, request);
  1009. break;
  1010. case VSTOR_OPERATION_REMOVE_DEVICE:
  1011. case VSTOR_OPERATION_ENUMERATE_BUS:
  1012. host_dev = shost_priv(stor_device->host);
  1013. queue_work(
  1014. host_dev->handle_error_wq, &host_dev->host_scan_work);
  1015. break;
  1016. case VSTOR_OPERATION_FCHBA_DATA:
  1017. cache_wwn(stor_device, vstor_packet);
  1018. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1019. fc_host_node_name(stor_device->host) = stor_device->node_name;
  1020. fc_host_port_name(stor_device->host) = stor_device->port_name;
  1021. #endif
  1022. break;
  1023. default:
  1024. break;
  1025. }
  1026. }
  1027. static void storvsc_on_channel_callback(void *context)
  1028. {
  1029. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  1030. const struct vmpacket_descriptor *desc;
  1031. struct hv_device *device;
  1032. struct storvsc_device *stor_device;
  1033. struct Scsi_Host *shost;
  1034. unsigned long time_limit = jiffies + msecs_to_jiffies(CALLBACK_TIMEOUT);
  1035. if (channel->primary_channel != NULL)
  1036. device = channel->primary_channel->device_obj;
  1037. else
  1038. device = channel->device_obj;
  1039. stor_device = get_in_stor_device(device);
  1040. if (!stor_device)
  1041. return;
  1042. shost = stor_device->host;
  1043. foreach_vmbus_pkt(desc, channel) {
  1044. struct vstor_packet *packet = hv_pkt_data(desc);
  1045. struct storvsc_cmd_request *request = NULL;
  1046. u32 pktlen = hv_pkt_datalen(desc);
  1047. u64 rqst_id = desc->trans_id;
  1048. u32 minlen = rqst_id ? sizeof(struct vstor_packet) :
  1049. sizeof(enum vstor_packet_operation);
  1050. if (unlikely(time_after(jiffies, time_limit))) {
  1051. hv_pkt_iter_close(channel);
  1052. return;
  1053. }
  1054. if (pktlen < minlen) {
  1055. dev_err(&device->device,
  1056. "Invalid pkt: id=%llu, len=%u, minlen=%u\n",
  1057. rqst_id, pktlen, minlen);
  1058. continue;
  1059. }
  1060. if (rqst_id == VMBUS_RQST_INIT) {
  1061. request = &stor_device->init_request;
  1062. } else if (rqst_id == VMBUS_RQST_RESET) {
  1063. request = &stor_device->reset_request;
  1064. } else {
  1065. /* Hyper-V can send an unsolicited message with ID of 0 */
  1066. if (rqst_id == 0) {
  1067. /*
  1068. * storvsc_on_receive() looks at the vstor_packet in the message
  1069. * from the ring buffer.
  1070. *
  1071. * - If the operation in the vstor_packet is COMPLETE_IO, then
  1072. * we call storvsc_on_io_completion(), and dereference the
  1073. * guest memory address. Make sure we don't call
  1074. * storvsc_on_io_completion() with a guest memory address
  1075. * that is zero if Hyper-V were to construct and send such
  1076. * a bogus packet.
  1077. *
  1078. * - If the operation in the vstor_packet is FCHBA_DATA, then
  1079. * we call cache_wwn(), and access the data payload area of
  1080. * the packet (wwn_packet); however, there is no guarantee
  1081. * that the packet is big enough to contain such area.
  1082. * Future-proof the code by rejecting such a bogus packet.
  1083. */
  1084. if (packet->operation == VSTOR_OPERATION_COMPLETE_IO ||
  1085. packet->operation == VSTOR_OPERATION_FCHBA_DATA) {
  1086. dev_err(&device->device, "Invalid packet with ID of 0\n");
  1087. continue;
  1088. }
  1089. } else {
  1090. struct scsi_cmnd *scmnd;
  1091. /* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */
  1092. scmnd = scsi_host_find_tag(shost, rqst_id - 1);
  1093. if (scmnd == NULL) {
  1094. dev_err(&device->device, "Incorrect transaction ID\n");
  1095. continue;
  1096. }
  1097. request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd);
  1098. scsi_dma_unmap(scmnd);
  1099. }
  1100. storvsc_on_receive(stor_device, packet, request);
  1101. continue;
  1102. }
  1103. memcpy(&request->vstor_packet, packet,
  1104. sizeof(struct vstor_packet));
  1105. complete(&request->wait_event);
  1106. }
  1107. }
  1108. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
  1109. bool is_fc)
  1110. {
  1111. struct vmstorage_channel_properties props;
  1112. int ret;
  1113. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  1114. device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
  1115. device->channel->next_request_id_callback = storvsc_next_request_id;
  1116. ret = vmbus_open(device->channel,
  1117. ring_size,
  1118. ring_size,
  1119. (void *)&props,
  1120. sizeof(struct vmstorage_channel_properties),
  1121. storvsc_on_channel_callback, device->channel);
  1122. if (ret != 0)
  1123. return ret;
  1124. ret = storvsc_channel_init(device, is_fc);
  1125. return ret;
  1126. }
  1127. static int storvsc_dev_remove(struct hv_device *device)
  1128. {
  1129. struct storvsc_device *stor_device;
  1130. stor_device = hv_get_drvdata(device);
  1131. stor_device->destroy = true;
  1132. /* Make sure flag is set before waiting */
  1133. wmb();
  1134. /*
  1135. * At this point, all outbound traffic should be disable. We
  1136. * only allow inbound traffic (responses) to proceed so that
  1137. * outstanding requests can be completed.
  1138. */
  1139. storvsc_wait_to_drain(stor_device);
  1140. /*
  1141. * Since we have already drained, we don't need to busy wait
  1142. * as was done in final_release_stor_device()
  1143. * Note that we cannot set the ext pointer to NULL until
  1144. * we have drained - to drain the outgoing packets, we need to
  1145. * allow incoming packets.
  1146. */
  1147. hv_set_drvdata(device, NULL);
  1148. /* Close the channel */
  1149. vmbus_close(device->channel);
  1150. kfree(stor_device->stor_chns);
  1151. kfree(stor_device);
  1152. return 0;
  1153. }
  1154. static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
  1155. u16 q_num)
  1156. {
  1157. u16 slot = 0;
  1158. u16 hash_qnum;
  1159. const struct cpumask *node_mask;
  1160. int num_channels, tgt_cpu;
  1161. if (stor_device->num_sc == 0) {
  1162. stor_device->stor_chns[q_num] = stor_device->device->channel;
  1163. return stor_device->device->channel;
  1164. }
  1165. /*
  1166. * Our channel array is sparsley populated and we
  1167. * initiated I/O on a processor/hw-q that does not
  1168. * currently have a designated channel. Fix this.
  1169. * The strategy is simple:
  1170. * I. Ensure NUMA locality
  1171. * II. Distribute evenly (best effort)
  1172. */
  1173. node_mask = cpumask_of_node(cpu_to_node(q_num));
  1174. num_channels = 0;
  1175. for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
  1176. if (cpumask_test_cpu(tgt_cpu, node_mask))
  1177. num_channels++;
  1178. }
  1179. if (num_channels == 0) {
  1180. stor_device->stor_chns[q_num] = stor_device->device->channel;
  1181. return stor_device->device->channel;
  1182. }
  1183. hash_qnum = q_num;
  1184. while (hash_qnum >= num_channels)
  1185. hash_qnum -= num_channels;
  1186. for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
  1187. if (!cpumask_test_cpu(tgt_cpu, node_mask))
  1188. continue;
  1189. if (slot == hash_qnum)
  1190. break;
  1191. slot++;
  1192. }
  1193. stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
  1194. return stor_device->stor_chns[q_num];
  1195. }
  1196. static int storvsc_do_io(struct hv_device *device,
  1197. struct storvsc_cmd_request *request, u16 q_num)
  1198. {
  1199. struct storvsc_device *stor_device;
  1200. struct vstor_packet *vstor_packet;
  1201. struct vmbus_channel *outgoing_channel, *channel;
  1202. unsigned long flags;
  1203. int ret = 0;
  1204. const struct cpumask *node_mask;
  1205. int tgt_cpu;
  1206. vstor_packet = &request->vstor_packet;
  1207. stor_device = get_out_stor_device(device);
  1208. if (!stor_device)
  1209. return -ENODEV;
  1210. request->device = device;
  1211. /*
  1212. * Select an appropriate channel to send the request out.
  1213. */
  1214. /* See storvsc_change_target_cpu(). */
  1215. outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]);
  1216. if (outgoing_channel != NULL) {
  1217. if (outgoing_channel->target_cpu == q_num) {
  1218. /*
  1219. * Ideally, we want to pick a different channel if
  1220. * available on the same NUMA node.
  1221. */
  1222. node_mask = cpumask_of_node(cpu_to_node(q_num));
  1223. for_each_cpu_wrap(tgt_cpu,
  1224. &stor_device->alloced_cpus, q_num + 1) {
  1225. if (!cpumask_test_cpu(tgt_cpu, node_mask))
  1226. continue;
  1227. if (tgt_cpu == q_num)
  1228. continue;
  1229. channel = READ_ONCE(
  1230. stor_device->stor_chns[tgt_cpu]);
  1231. if (channel == NULL)
  1232. continue;
  1233. if (hv_get_avail_to_write_percent(
  1234. &channel->outbound)
  1235. > ring_avail_percent_lowater) {
  1236. outgoing_channel = channel;
  1237. goto found_channel;
  1238. }
  1239. }
  1240. /*
  1241. * All the other channels on the same NUMA node are
  1242. * busy. Try to use the channel on the current CPU
  1243. */
  1244. if (hv_get_avail_to_write_percent(
  1245. &outgoing_channel->outbound)
  1246. > ring_avail_percent_lowater)
  1247. goto found_channel;
  1248. /*
  1249. * If we reach here, all the channels on the current
  1250. * NUMA node are busy. Try to find a channel in
  1251. * other NUMA nodes
  1252. */
  1253. for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
  1254. if (cpumask_test_cpu(tgt_cpu, node_mask))
  1255. continue;
  1256. channel = READ_ONCE(
  1257. stor_device->stor_chns[tgt_cpu]);
  1258. if (channel == NULL)
  1259. continue;
  1260. if (hv_get_avail_to_write_percent(
  1261. &channel->outbound)
  1262. > ring_avail_percent_lowater) {
  1263. outgoing_channel = channel;
  1264. goto found_channel;
  1265. }
  1266. }
  1267. }
  1268. } else {
  1269. spin_lock_irqsave(&stor_device->lock, flags);
  1270. outgoing_channel = stor_device->stor_chns[q_num];
  1271. if (outgoing_channel != NULL) {
  1272. spin_unlock_irqrestore(&stor_device->lock, flags);
  1273. goto found_channel;
  1274. }
  1275. outgoing_channel = get_og_chn(stor_device, q_num);
  1276. spin_unlock_irqrestore(&stor_device->lock, flags);
  1277. }
  1278. found_channel:
  1279. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  1280. vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
  1281. vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
  1282. vstor_packet->vm_srb.data_transfer_length =
  1283. request->payload->range.len;
  1284. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  1285. if (request->payload->range.len) {
  1286. ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
  1287. request->payload, request->payload_sz,
  1288. vstor_packet,
  1289. sizeof(struct vstor_packet),
  1290. (unsigned long)request);
  1291. } else {
  1292. ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
  1293. sizeof(struct vstor_packet),
  1294. (unsigned long)request,
  1295. VM_PKT_DATA_INBAND,
  1296. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1297. }
  1298. if (ret != 0)
  1299. return ret;
  1300. atomic_inc(&stor_device->num_outstanding_req);
  1301. return ret;
  1302. }
  1303. static int storvsc_device_alloc(struct scsi_device *sdevice)
  1304. {
  1305. /*
  1306. * Set blist flag to permit the reading of the VPD pages even when
  1307. * the target may claim SPC-2 compliance. MSFT targets currently
  1308. * claim SPC-2 compliance while they implement post SPC-2 features.
  1309. * With this flag we can correctly handle WRITE_SAME_16 issues.
  1310. *
  1311. * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
  1312. * still supports REPORT LUN.
  1313. */
  1314. sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
  1315. return 0;
  1316. }
  1317. static int storvsc_device_configure(struct scsi_device *sdevice)
  1318. {
  1319. blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
  1320. /* storvsc devices don't support MAINTENANCE_IN SCSI cmd */
  1321. sdevice->no_report_opcodes = 1;
  1322. sdevice->no_write_same = 1;
  1323. /*
  1324. * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
  1325. * if the device is a MSFT virtual device. If the host is
  1326. * WIN10 or newer, allow write_same.
  1327. */
  1328. if (!strncmp(sdevice->vendor, "Msft", 4)) {
  1329. switch (vmstor_proto_version) {
  1330. case VMSTOR_PROTO_VERSION_WIN8:
  1331. case VMSTOR_PROTO_VERSION_WIN8_1:
  1332. sdevice->scsi_level = SCSI_SPC_3;
  1333. break;
  1334. }
  1335. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
  1336. sdevice->no_write_same = 0;
  1337. }
  1338. return 0;
  1339. }
  1340. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  1341. sector_t capacity, int *info)
  1342. {
  1343. sector_t nsect = capacity;
  1344. sector_t cylinders = nsect;
  1345. int heads, sectors_pt;
  1346. /*
  1347. * We are making up these values; let us keep it simple.
  1348. */
  1349. heads = 0xff;
  1350. sectors_pt = 0x3f; /* Sectors per track */
  1351. sector_div(cylinders, heads * sectors_pt);
  1352. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  1353. cylinders = 0xffff;
  1354. info[0] = heads;
  1355. info[1] = sectors_pt;
  1356. info[2] = (int)cylinders;
  1357. return 0;
  1358. }
  1359. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  1360. {
  1361. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  1362. struct hv_device *device = host_dev->dev;
  1363. struct storvsc_device *stor_device;
  1364. struct storvsc_cmd_request *request;
  1365. struct vstor_packet *vstor_packet;
  1366. int ret, t;
  1367. stor_device = get_out_stor_device(device);
  1368. if (!stor_device)
  1369. return FAILED;
  1370. request = &stor_device->reset_request;
  1371. vstor_packet = &request->vstor_packet;
  1372. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  1373. init_completion(&request->wait_event);
  1374. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  1375. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  1376. vstor_packet->vm_srb.path_id = stor_device->path_id;
  1377. ret = vmbus_sendpacket(device->channel, vstor_packet,
  1378. sizeof(struct vstor_packet),
  1379. VMBUS_RQST_RESET,
  1380. VM_PKT_DATA_INBAND,
  1381. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1382. if (ret != 0)
  1383. return FAILED;
  1384. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  1385. if (t == 0)
  1386. return TIMEOUT_ERROR;
  1387. /*
  1388. * At this point, all outstanding requests in the adapter
  1389. * should have been flushed out and return to us
  1390. * There is a potential race here where the host may be in
  1391. * the process of responding when we return from here.
  1392. * Just wait for all in-transit packets to be accounted for
  1393. * before we return from here.
  1394. */
  1395. storvsc_wait_to_drain(stor_device);
  1396. return SUCCESS;
  1397. }
  1398. /*
  1399. * The host guarantees to respond to each command, although I/O latencies might
  1400. * be unbounded on Azure. Reset the timer unconditionally to give the host a
  1401. * chance to perform EH.
  1402. */
  1403. static enum scsi_timeout_action storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
  1404. {
  1405. return SCSI_EH_RESET_TIMER;
  1406. }
  1407. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  1408. {
  1409. bool allowed = true;
  1410. u8 scsi_op = scmnd->cmnd[0];
  1411. switch (scsi_op) {
  1412. /* the host does not handle WRITE_SAME, log accident usage */
  1413. case WRITE_SAME:
  1414. /*
  1415. * smartd sends this command and the host does not handle
  1416. * this. So, don't send it.
  1417. */
  1418. case SET_WINDOW:
  1419. set_host_byte(scmnd, DID_ERROR);
  1420. allowed = false;
  1421. break;
  1422. default:
  1423. break;
  1424. }
  1425. return allowed;
  1426. }
  1427. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  1428. {
  1429. int ret;
  1430. struct hv_host_device *host_dev = shost_priv(host);
  1431. struct hv_device *dev = host_dev->dev;
  1432. struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
  1433. struct scatterlist *sgl;
  1434. struct vmscsi_request *vm_srb;
  1435. struct vmbus_packet_mpb_array *payload;
  1436. u32 payload_sz;
  1437. u32 length;
  1438. if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
  1439. /*
  1440. * On legacy hosts filter unimplemented commands.
  1441. * Future hosts are expected to correctly handle
  1442. * unsupported commands. Furthermore, it is
  1443. * possible that some of the currently
  1444. * unsupported commands maybe supported in
  1445. * future versions of the host.
  1446. */
  1447. if (!storvsc_scsi_cmd_ok(scmnd)) {
  1448. scsi_done(scmnd);
  1449. return 0;
  1450. }
  1451. }
  1452. /* Setup the cmd request */
  1453. cmd_request->cmd = scmnd;
  1454. memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet));
  1455. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1456. vm_srb->time_out_value = 60;
  1457. vm_srb->srb_flags |=
  1458. SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1459. if (scmnd->device->tagged_supported) {
  1460. vm_srb->srb_flags |=
  1461. (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
  1462. vm_srb->queue_tag = SP_UNTAGGED;
  1463. vm_srb->queue_action = SRB_SIMPLE_TAG_REQUEST;
  1464. }
  1465. /* Build the SRB */
  1466. switch (scmnd->sc_data_direction) {
  1467. case DMA_TO_DEVICE:
  1468. vm_srb->data_in = WRITE_TYPE;
  1469. vm_srb->srb_flags |= SRB_FLAGS_DATA_OUT;
  1470. break;
  1471. case DMA_FROM_DEVICE:
  1472. vm_srb->data_in = READ_TYPE;
  1473. vm_srb->srb_flags |= SRB_FLAGS_DATA_IN;
  1474. break;
  1475. case DMA_NONE:
  1476. vm_srb->data_in = UNKNOWN_TYPE;
  1477. vm_srb->srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
  1478. break;
  1479. default:
  1480. /*
  1481. * This is DMA_BIDIRECTIONAL or something else we are never
  1482. * supposed to see here.
  1483. */
  1484. WARN(1, "Unexpected data direction: %d\n",
  1485. scmnd->sc_data_direction);
  1486. return -EINVAL;
  1487. }
  1488. vm_srb->port_number = host_dev->port;
  1489. vm_srb->path_id = scmnd->device->channel;
  1490. vm_srb->target_id = scmnd->device->id;
  1491. vm_srb->lun = scmnd->device->lun;
  1492. vm_srb->cdb_length = scmnd->cmd_len;
  1493. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1494. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1495. length = scsi_bufflen(scmnd);
  1496. payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
  1497. payload->range.len = 0;
  1498. payload_sz = 0;
  1499. if (scsi_sg_count(scmnd)) {
  1500. unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset);
  1501. unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
  1502. struct scatterlist *sg;
  1503. unsigned long hvpfn, hvpfns_to_add;
  1504. int j, i = 0, sg_count;
  1505. payload_sz = (hvpg_count * sizeof(u64) +
  1506. sizeof(struct vmbus_packet_mpb_array));
  1507. if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
  1508. payload = kzalloc(payload_sz, GFP_ATOMIC);
  1509. if (!payload)
  1510. return SCSI_MLQUEUE_DEVICE_BUSY;
  1511. }
  1512. payload->range.len = length;
  1513. payload->range.offset = offset_in_hvpg;
  1514. sg_count = scsi_dma_map(scmnd);
  1515. if (sg_count < 0) {
  1516. ret = SCSI_MLQUEUE_DEVICE_BUSY;
  1517. goto err_free_payload;
  1518. }
  1519. for_each_sg(sgl, sg, sg_count, j) {
  1520. /*
  1521. * Init values for the current sgl entry. hvpfns_to_add
  1522. * is in units of Hyper-V size pages. Handling the
  1523. * PAGE_SIZE != HV_HYP_PAGE_SIZE case also handles
  1524. * values of sgl->offset that are larger than PAGE_SIZE.
  1525. * Such offsets are handled even on other than the first
  1526. * sgl entry, provided they are a multiple of PAGE_SIZE.
  1527. */
  1528. hvpfn = HVPFN_DOWN(sg_dma_address(sg));
  1529. hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) +
  1530. sg_dma_len(sg)) - hvpfn;
  1531. /*
  1532. * Fill the next portion of the PFN array with
  1533. * sequential Hyper-V PFNs for the continguous physical
  1534. * memory described by the sgl entry. The end of the
  1535. * last sgl should be reached at the same time that
  1536. * the PFN array is filled.
  1537. */
  1538. while (hvpfns_to_add--)
  1539. payload->range.pfn_array[i++] = hvpfn++;
  1540. }
  1541. }
  1542. cmd_request->payload = payload;
  1543. cmd_request->payload_sz = payload_sz;
  1544. /* Invokes the vsc to start an IO */
  1545. ret = storvsc_do_io(dev, cmd_request, get_cpu());
  1546. put_cpu();
  1547. if (ret)
  1548. scsi_dma_unmap(scmnd);
  1549. if (ret == -EAGAIN) {
  1550. /* no more space */
  1551. ret = SCSI_MLQUEUE_DEVICE_BUSY;
  1552. goto err_free_payload;
  1553. }
  1554. return 0;
  1555. err_free_payload:
  1556. if (payload_sz > sizeof(cmd_request->mpb))
  1557. kfree(payload);
  1558. return ret;
  1559. }
  1560. static struct scsi_host_template scsi_driver = {
  1561. .module = THIS_MODULE,
  1562. .name = "storvsc_host_t",
  1563. .cmd_size = sizeof(struct storvsc_cmd_request),
  1564. .bios_param = storvsc_get_chs,
  1565. .queuecommand = storvsc_queuecommand,
  1566. .eh_host_reset_handler = storvsc_host_reset_handler,
  1567. .proc_name = "storvsc_host",
  1568. .eh_timed_out = storvsc_eh_timed_out,
  1569. .slave_alloc = storvsc_device_alloc,
  1570. .slave_configure = storvsc_device_configure,
  1571. .cmd_per_lun = 2048,
  1572. .this_id = -1,
  1573. /* Ensure there are no gaps in presented sgls */
  1574. .virt_boundary_mask = HV_HYP_PAGE_SIZE - 1,
  1575. .no_write_same = 1,
  1576. .track_queue_depth = 1,
  1577. .change_queue_depth = storvsc_change_queue_depth,
  1578. };
  1579. enum {
  1580. SCSI_GUID,
  1581. IDE_GUID,
  1582. SFC_GUID,
  1583. };
  1584. static const struct hv_vmbus_device_id id_table[] = {
  1585. /* SCSI guid */
  1586. { HV_SCSI_GUID,
  1587. .driver_data = SCSI_GUID
  1588. },
  1589. /* IDE guid */
  1590. { HV_IDE_GUID,
  1591. .driver_data = IDE_GUID
  1592. },
  1593. /* Fibre Channel GUID */
  1594. {
  1595. HV_SYNTHFC_GUID,
  1596. .driver_data = SFC_GUID
  1597. },
  1598. { },
  1599. };
  1600. MODULE_DEVICE_TABLE(vmbus, id_table);
  1601. static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID };
  1602. static bool hv_dev_is_fc(struct hv_device *hv_dev)
  1603. {
  1604. return guid_equal(&fc_guid.guid, &hv_dev->dev_type);
  1605. }
  1606. static int storvsc_probe(struct hv_device *device,
  1607. const struct hv_vmbus_device_id *dev_id)
  1608. {
  1609. int ret;
  1610. int num_cpus = num_online_cpus();
  1611. int num_present_cpus = num_present_cpus();
  1612. struct Scsi_Host *host;
  1613. struct hv_host_device *host_dev;
  1614. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1615. bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
  1616. int target = 0;
  1617. struct storvsc_device *stor_device;
  1618. int max_sub_channels = 0;
  1619. u32 max_xfer_bytes;
  1620. /*
  1621. * We support sub-channels for storage on SCSI and FC controllers.
  1622. * The number of sub-channels offerred is based on the number of
  1623. * VCPUs in the guest.
  1624. */
  1625. if (!dev_is_ide)
  1626. max_sub_channels =
  1627. (num_cpus - 1) / storvsc_vcpus_per_sub_channel;
  1628. scsi_driver.can_queue = max_outstanding_req_per_channel *
  1629. (max_sub_channels + 1) *
  1630. (100 - ring_avail_percent_lowater) / 100;
  1631. host = scsi_host_alloc(&scsi_driver,
  1632. sizeof(struct hv_host_device));
  1633. if (!host)
  1634. return -ENOMEM;
  1635. host_dev = shost_priv(host);
  1636. memset(host_dev, 0, sizeof(struct hv_host_device));
  1637. host_dev->port = host->host_no;
  1638. host_dev->dev = device;
  1639. host_dev->host = host;
  1640. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1641. if (!stor_device) {
  1642. ret = -ENOMEM;
  1643. goto err_out0;
  1644. }
  1645. stor_device->destroy = false;
  1646. init_waitqueue_head(&stor_device->waiting_to_drain);
  1647. stor_device->device = device;
  1648. stor_device->host = host;
  1649. spin_lock_init(&stor_device->lock);
  1650. hv_set_drvdata(device, stor_device);
  1651. dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1);
  1652. stor_device->port_number = host->host_no;
  1653. ret = storvsc_connect_to_vsp(device, aligned_ringbuffer_size, is_fc);
  1654. if (ret)
  1655. goto err_out1;
  1656. host_dev->path = stor_device->path_id;
  1657. host_dev->target = stor_device->target_id;
  1658. switch (dev_id->driver_data) {
  1659. case SFC_GUID:
  1660. host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
  1661. host->max_id = STORVSC_FC_MAX_TARGETS;
  1662. host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
  1663. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1664. host->transportt = fc_transport_template;
  1665. #endif
  1666. break;
  1667. case SCSI_GUID:
  1668. host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
  1669. host->max_id = STORVSC_MAX_TARGETS;
  1670. host->max_channel = STORVSC_MAX_CHANNELS - 1;
  1671. break;
  1672. default:
  1673. host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1674. host->max_id = STORVSC_IDE_MAX_TARGETS;
  1675. host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
  1676. break;
  1677. }
  1678. /* max cmd length */
  1679. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1680. /*
  1681. * Any reasonable Hyper-V configuration should provide
  1682. * max_transfer_bytes value aligning to HV_HYP_PAGE_SIZE,
  1683. * protecting it from any weird value.
  1684. */
  1685. max_xfer_bytes = round_down(stor_device->max_transfer_bytes, HV_HYP_PAGE_SIZE);
  1686. if (is_fc)
  1687. max_xfer_bytes = min(max_xfer_bytes, STORVSC_FC_MAX_XFER_SIZE);
  1688. /* max_hw_sectors_kb */
  1689. host->max_sectors = max_xfer_bytes >> 9;
  1690. /*
  1691. * There are 2 requirements for Hyper-V storvsc sgl segments,
  1692. * based on which the below calculation for max segments is
  1693. * done:
  1694. *
  1695. * 1. Except for the first and last sgl segment, all sgl segments
  1696. * should be align to HV_HYP_PAGE_SIZE, that also means the
  1697. * maximum number of segments in a sgl can be calculated by
  1698. * dividing the total max transfer length by HV_HYP_PAGE_SIZE.
  1699. *
  1700. * 2. Except for the first and last, each entry in the SGL must
  1701. * have an offset that is a multiple of HV_HYP_PAGE_SIZE.
  1702. */
  1703. host->sg_tablesize = (max_xfer_bytes >> HV_HYP_PAGE_SHIFT) + 1;
  1704. /*
  1705. * For non-IDE disks, the host supports multiple channels.
  1706. * Set the number of HW queues we are supporting.
  1707. */
  1708. if (!dev_is_ide) {
  1709. if (storvsc_max_hw_queues > num_present_cpus) {
  1710. storvsc_max_hw_queues = 0;
  1711. storvsc_log(device, STORVSC_LOGGING_WARN,
  1712. "Resetting invalid storvsc_max_hw_queues value to default.\n");
  1713. }
  1714. if (storvsc_max_hw_queues)
  1715. host->nr_hw_queues = storvsc_max_hw_queues;
  1716. else
  1717. host->nr_hw_queues = num_present_cpus;
  1718. }
  1719. /*
  1720. * Set the error handler work queue.
  1721. */
  1722. host_dev->handle_error_wq =
  1723. alloc_ordered_workqueue("storvsc_error_wq_%d",
  1724. 0,
  1725. host->host_no);
  1726. if (!host_dev->handle_error_wq) {
  1727. ret = -ENOMEM;
  1728. goto err_out2;
  1729. }
  1730. INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
  1731. /* Register the HBA and start the scsi bus scan */
  1732. ret = scsi_add_host(host, &device->device);
  1733. if (ret != 0)
  1734. goto err_out3;
  1735. if (!dev_is_ide) {
  1736. scsi_scan_host(host);
  1737. } else {
  1738. target = (device->dev_instance.b[5] << 8 |
  1739. device->dev_instance.b[4]);
  1740. ret = scsi_add_device(host, 0, target, 0);
  1741. if (ret)
  1742. goto err_out4;
  1743. }
  1744. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1745. if (host->transportt == fc_transport_template) {
  1746. struct fc_rport_identifiers ids = {
  1747. .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
  1748. };
  1749. fc_host_node_name(host) = stor_device->node_name;
  1750. fc_host_port_name(host) = stor_device->port_name;
  1751. stor_device->rport = fc_remote_port_add(host, 0, &ids);
  1752. if (!stor_device->rport) {
  1753. ret = -ENOMEM;
  1754. goto err_out4;
  1755. }
  1756. }
  1757. #endif
  1758. return 0;
  1759. err_out4:
  1760. scsi_remove_host(host);
  1761. err_out3:
  1762. destroy_workqueue(host_dev->handle_error_wq);
  1763. err_out2:
  1764. /*
  1765. * Once we have connected with the host, we would need to
  1766. * invoke storvsc_dev_remove() to rollback this state and
  1767. * this call also frees up the stor_device; hence the jump around
  1768. * err_out1 label.
  1769. */
  1770. storvsc_dev_remove(device);
  1771. goto err_out0;
  1772. err_out1:
  1773. kfree(stor_device->stor_chns);
  1774. kfree(stor_device);
  1775. err_out0:
  1776. scsi_host_put(host);
  1777. return ret;
  1778. }
  1779. /* Change a scsi target's queue depth */
  1780. static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth)
  1781. {
  1782. if (queue_depth > scsi_driver.can_queue)
  1783. queue_depth = scsi_driver.can_queue;
  1784. return scsi_change_queue_depth(sdev, queue_depth);
  1785. }
  1786. static void storvsc_remove(struct hv_device *dev)
  1787. {
  1788. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1789. struct Scsi_Host *host = stor_device->host;
  1790. struct hv_host_device *host_dev = shost_priv(host);
  1791. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1792. if (host->transportt == fc_transport_template) {
  1793. fc_remote_port_delete(stor_device->rport);
  1794. fc_remove_host(host);
  1795. }
  1796. #endif
  1797. destroy_workqueue(host_dev->handle_error_wq);
  1798. scsi_remove_host(host);
  1799. storvsc_dev_remove(dev);
  1800. scsi_host_put(host);
  1801. }
  1802. static int storvsc_suspend(struct hv_device *hv_dev)
  1803. {
  1804. struct storvsc_device *stor_device = hv_get_drvdata(hv_dev);
  1805. struct Scsi_Host *host = stor_device->host;
  1806. struct hv_host_device *host_dev = shost_priv(host);
  1807. storvsc_wait_to_drain(stor_device);
  1808. drain_workqueue(host_dev->handle_error_wq);
  1809. vmbus_close(hv_dev->channel);
  1810. kfree(stor_device->stor_chns);
  1811. stor_device->stor_chns = NULL;
  1812. cpumask_clear(&stor_device->alloced_cpus);
  1813. return 0;
  1814. }
  1815. static int storvsc_resume(struct hv_device *hv_dev)
  1816. {
  1817. int ret;
  1818. ret = storvsc_connect_to_vsp(hv_dev, aligned_ringbuffer_size,
  1819. hv_dev_is_fc(hv_dev));
  1820. return ret;
  1821. }
  1822. static struct hv_driver storvsc_drv = {
  1823. .name = KBUILD_MODNAME,
  1824. .id_table = id_table,
  1825. .probe = storvsc_probe,
  1826. .remove = storvsc_remove,
  1827. .suspend = storvsc_suspend,
  1828. .resume = storvsc_resume,
  1829. .driver = {
  1830. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1831. },
  1832. };
  1833. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1834. static struct fc_function_template fc_transport_functions = {
  1835. .show_host_node_name = 1,
  1836. .show_host_port_name = 1,
  1837. };
  1838. #endif
  1839. static int __init storvsc_drv_init(void)
  1840. {
  1841. int ret;
  1842. /*
  1843. * Divide the ring buffer data size (which is 1 page less
  1844. * than the ring buffer size since that page is reserved for
  1845. * the ring buffer indices) by the max request size (which is
  1846. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1847. */
  1848. aligned_ringbuffer_size = VMBUS_RING_SIZE(storvsc_ringbuffer_size);
  1849. max_outstanding_req_per_channel =
  1850. ((aligned_ringbuffer_size - PAGE_SIZE) /
  1851. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1852. sizeof(struct vstor_packet) + sizeof(u64),
  1853. sizeof(u64)));
  1854. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1855. fc_transport_template = fc_attach_transport(&fc_transport_functions);
  1856. if (!fc_transport_template)
  1857. return -ENODEV;
  1858. #endif
  1859. ret = vmbus_driver_register(&storvsc_drv);
  1860. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1861. if (ret)
  1862. fc_release_transport(fc_transport_template);
  1863. #endif
  1864. return ret;
  1865. }
  1866. static void __exit storvsc_drv_exit(void)
  1867. {
  1868. vmbus_driver_unregister(&storvsc_drv);
  1869. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1870. fc_release_transport(fc_transport_template);
  1871. #endif
  1872. }
  1873. MODULE_LICENSE("GPL");
  1874. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1875. module_init(storvsc_drv_init);
  1876. module_exit(storvsc_drv_exit);