storvsc_drv.c 59 KB

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