storvsc_drv.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. * K. Y. Srinivasan <kys@microsoft.com>
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/wait.h>
  24. #include <linux/sched.h>
  25. #include <linux/completion.h>
  26. #include <linux/string.h>
  27. #include <linux/mm.h>
  28. #include <linux/delay.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. #include <linux/device.h>
  33. #include <linux/hyperv.h>
  34. #include <linux/blkdev.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_cmnd.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_device.h>
  39. #include <scsi/scsi_tcq.h>
  40. #include <scsi/scsi_eh.h>
  41. #include <scsi/scsi_devinfo.h>
  42. #include <scsi/scsi_dbg.h>
  43. #include <scsi/scsi_transport_fc.h>
  44. #include <scsi/scsi_transport.h>
  45. /*
  46. * All wire protocol details (storage protocol between the guest and the host)
  47. * are consolidated here.
  48. *
  49. * Begin protocol definitions.
  50. */
  51. /*
  52. * Version history:
  53. * V1 Beta: 0.1
  54. * V1 RC < 2008/1/31: 1.0
  55. * V1 RC > 2008/1/31: 2.0
  56. * Win7: 4.2
  57. * Win8: 5.1
  58. * Win8.1: 6.0
  59. * Win10: 6.2
  60. */
  61. #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
  62. (((MINOR_) & 0xff)))
  63. #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
  64. #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
  65. #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
  66. #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
  67. #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
  68. /* Packet structure describing virtual storage requests. */
  69. enum vstor_packet_operation {
  70. VSTOR_OPERATION_COMPLETE_IO = 1,
  71. VSTOR_OPERATION_REMOVE_DEVICE = 2,
  72. VSTOR_OPERATION_EXECUTE_SRB = 3,
  73. VSTOR_OPERATION_RESET_LUN = 4,
  74. VSTOR_OPERATION_RESET_ADAPTER = 5,
  75. VSTOR_OPERATION_RESET_BUS = 6,
  76. VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
  77. VSTOR_OPERATION_END_INITIALIZATION = 8,
  78. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
  79. VSTOR_OPERATION_QUERY_PROPERTIES = 10,
  80. VSTOR_OPERATION_ENUMERATE_BUS = 11,
  81. VSTOR_OPERATION_FCHBA_DATA = 12,
  82. VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
  83. VSTOR_OPERATION_MAXIMUM = 13
  84. };
  85. /*
  86. * WWN packet for Fibre Channel HBA
  87. */
  88. struct hv_fc_wwn_packet {
  89. u8 primary_active;
  90. u8 reserved1[3];
  91. u8 primary_port_wwn[8];
  92. u8 primary_node_wwn[8];
  93. u8 secondary_port_wwn[8];
  94. u8 secondary_node_wwn[8];
  95. };
  96. /*
  97. * SRB Flag Bits
  98. */
  99. #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
  100. #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
  101. #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
  102. #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
  103. #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
  104. #define SRB_FLAGS_DATA_IN 0x00000040
  105. #define SRB_FLAGS_DATA_OUT 0x00000080
  106. #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
  107. #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
  108. #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
  109. #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
  110. #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
  111. /*
  112. * This flag indicates the request is part of the workflow for processing a D3.
  113. */
  114. #define SRB_FLAGS_D3_PROCESSING 0x00000800
  115. #define SRB_FLAGS_IS_ACTIVE 0x00010000
  116. #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
  117. #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
  118. #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
  119. #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
  120. #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
  121. #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
  122. #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
  123. #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
  124. #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
  125. #define SP_UNTAGGED ((unsigned char) ~0)
  126. #define SRB_SIMPLE_TAG_REQUEST 0x20
  127. /*
  128. * Platform neutral description of a scsi request -
  129. * this remains the same across the write regardless of 32/64 bit
  130. * note: it's patterned off the SCSI_PASS_THROUGH structure
  131. */
  132. #define STORVSC_MAX_CMD_LEN 0x10
  133. #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
  134. #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
  135. #define STORVSC_SENSE_BUFFER_SIZE 0x14
  136. #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
  137. /*
  138. * Sense buffer size changed in win8; have a run-time
  139. * variable to track the size we should use. This value will
  140. * likely change during protocol negotiation but it is valid
  141. * to start by assuming pre-Win8.
  142. */
  143. static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
  144. /*
  145. * The storage protocol version is determined during the
  146. * initial exchange with the host. It will indicate which
  147. * storage functionality is available in the host.
  148. */
  149. static int vmstor_proto_version;
  150. #define STORVSC_LOGGING_NONE 0
  151. #define STORVSC_LOGGING_ERROR 1
  152. #define STORVSC_LOGGING_WARN 2
  153. static int logging_level = STORVSC_LOGGING_ERROR;
  154. module_param(logging_level, int, S_IRUGO|S_IWUSR);
  155. MODULE_PARM_DESC(logging_level,
  156. "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
  157. static inline bool do_logging(int level)
  158. {
  159. return logging_level >= level;
  160. }
  161. #define storvsc_log(dev, level, fmt, ...) \
  162. do { \
  163. if (do_logging(level)) \
  164. dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
  165. } while (0)
  166. struct vmscsi_win8_extension {
  167. /*
  168. * The following were added in Windows 8
  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. } __packed;
  177. struct vmscsi_request {
  178. u16 length;
  179. u8 srb_status;
  180. u8 scsi_status;
  181. u8 port_number;
  182. u8 path_id;
  183. u8 target_id;
  184. u8 lun;
  185. u8 cdb_length;
  186. u8 sense_info_length;
  187. u8 data_in;
  188. u8 reserved;
  189. u32 data_transfer_length;
  190. union {
  191. u8 cdb[STORVSC_MAX_CMD_LEN];
  192. u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
  193. u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
  194. };
  195. /*
  196. * The following was added in win8.
  197. */
  198. struct vmscsi_win8_extension win8_extension;
  199. } __attribute((packed));
  200. /*
  201. * The size of the vmscsi_request has changed in win8. The
  202. * additional size is because of new elements added to the
  203. * structure. These elements are valid only when we are talking
  204. * to a win8 host.
  205. * Track the correction to size we need to apply. This value
  206. * will likely change during protocol negotiation but it is
  207. * valid to start by assuming pre-Win8.
  208. */
  209. static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
  210. /*
  211. * The list of storage protocols in order of preference.
  212. */
  213. struct vmstor_protocol {
  214. int protocol_version;
  215. int sense_buffer_size;
  216. int vmscsi_size_delta;
  217. };
  218. static const struct vmstor_protocol vmstor_protocols[] = {
  219. {
  220. VMSTOR_PROTO_VERSION_WIN10,
  221. POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
  222. 0
  223. },
  224. {
  225. VMSTOR_PROTO_VERSION_WIN8_1,
  226. POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
  227. 0
  228. },
  229. {
  230. VMSTOR_PROTO_VERSION_WIN8,
  231. POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
  232. 0
  233. },
  234. {
  235. VMSTOR_PROTO_VERSION_WIN7,
  236. PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
  237. sizeof(struct vmscsi_win8_extension),
  238. },
  239. {
  240. VMSTOR_PROTO_VERSION_WIN6,
  241. PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
  242. sizeof(struct vmscsi_win8_extension),
  243. }
  244. };
  245. /*
  246. * This structure is sent during the intialization phase to get the different
  247. * properties of the channel.
  248. */
  249. #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
  250. struct vmstorage_channel_properties {
  251. u32 reserved;
  252. u16 max_channel_cnt;
  253. u16 reserved1;
  254. u32 flags;
  255. u32 max_transfer_bytes;
  256. u64 reserved2;
  257. } __packed;
  258. /* This structure is sent during the storage protocol negotiations. */
  259. struct vmstorage_protocol_version {
  260. /* Major (MSW) and minor (LSW) version numbers. */
  261. u16 major_minor;
  262. /*
  263. * Revision number is auto-incremented whenever this file is changed
  264. * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
  265. * definitely indicate incompatibility--but it does indicate mismatched
  266. * builds.
  267. * This is only used on the windows side. Just set it to 0.
  268. */
  269. u16 revision;
  270. } __packed;
  271. /* Channel Property Flags */
  272. #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
  273. #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
  274. struct vstor_packet {
  275. /* Requested operation type */
  276. enum vstor_packet_operation operation;
  277. /* Flags - see below for values */
  278. u32 flags;
  279. /* Status of the request returned from the server side. */
  280. u32 status;
  281. /* Data payload area */
  282. union {
  283. /*
  284. * Structure used to forward SCSI commands from the
  285. * client to the server.
  286. */
  287. struct vmscsi_request vm_srb;
  288. /* Structure used to query channel properties. */
  289. struct vmstorage_channel_properties storage_channel_properties;
  290. /* Used during version negotiations. */
  291. struct vmstorage_protocol_version version;
  292. /* Fibre channel address packet */
  293. struct hv_fc_wwn_packet wwn_packet;
  294. /* Number of sub-channels to create */
  295. u16 sub_channel_count;
  296. /* This will be the maximum of the union members */
  297. u8 buffer[0x34];
  298. };
  299. } __packed;
  300. /*
  301. * Packet Flags:
  302. *
  303. * This flag indicates that the server should send back a completion for this
  304. * packet.
  305. */
  306. #define REQUEST_COMPLETION_FLAG 0x1
  307. /* Matches Windows-end */
  308. enum storvsc_request_type {
  309. WRITE_TYPE = 0,
  310. READ_TYPE,
  311. UNKNOWN_TYPE,
  312. };
  313. /*
  314. * SRB status codes and masks; a subset of the codes used here.
  315. */
  316. #define SRB_STATUS_AUTOSENSE_VALID 0x80
  317. #define SRB_STATUS_QUEUE_FROZEN 0x40
  318. #define SRB_STATUS_INVALID_LUN 0x20
  319. #define SRB_STATUS_SUCCESS 0x01
  320. #define SRB_STATUS_ABORTED 0x02
  321. #define SRB_STATUS_ERROR 0x04
  322. #define SRB_STATUS_DATA_OVERRUN 0x12
  323. #define SRB_STATUS(status) \
  324. (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
  325. /*
  326. * This is the end of Protocol specific defines.
  327. */
  328. static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
  329. static u32 max_outstanding_req_per_channel;
  330. static int storvsc_vcpus_per_sub_channel = 4;
  331. module_param(storvsc_ringbuffer_size, int, S_IRUGO);
  332. MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
  333. module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
  334. MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
  335. /*
  336. * Timeout in seconds for all devices managed by this driver.
  337. */
  338. static int storvsc_timeout = 180;
  339. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  340. static struct scsi_transport_template *fc_transport_template;
  341. #endif
  342. static void storvsc_on_channel_callback(void *context);
  343. #define STORVSC_MAX_LUNS_PER_TARGET 255
  344. #define STORVSC_MAX_TARGETS 2
  345. #define STORVSC_MAX_CHANNELS 8
  346. #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
  347. #define STORVSC_FC_MAX_TARGETS 128
  348. #define STORVSC_FC_MAX_CHANNELS 8
  349. #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
  350. #define STORVSC_IDE_MAX_TARGETS 1
  351. #define STORVSC_IDE_MAX_CHANNELS 1
  352. struct storvsc_cmd_request {
  353. struct scsi_cmnd *cmd;
  354. struct hv_device *device;
  355. /* Synchronize the request/response if needed */
  356. struct completion wait_event;
  357. struct vmbus_channel_packet_multipage_buffer mpb;
  358. struct vmbus_packet_mpb_array *payload;
  359. u32 payload_sz;
  360. struct vstor_packet vstor_packet;
  361. };
  362. /* A storvsc device is a device object that contains a vmbus channel */
  363. struct storvsc_device {
  364. struct hv_device *device;
  365. bool destroy;
  366. bool drain_notify;
  367. bool open_sub_channel;
  368. atomic_t num_outstanding_req;
  369. struct Scsi_Host *host;
  370. wait_queue_head_t waiting_to_drain;
  371. /*
  372. * Each unique Port/Path/Target represents 1 channel ie scsi
  373. * controller. In reality, the pathid, targetid is always 0
  374. * and the port is set by us
  375. */
  376. unsigned int port_number;
  377. unsigned char path_id;
  378. unsigned char target_id;
  379. /*
  380. * Max I/O, the device can support.
  381. */
  382. u32 max_transfer_bytes;
  383. /* Used for vsc/vsp channel reset process */
  384. struct storvsc_cmd_request init_request;
  385. struct storvsc_cmd_request reset_request;
  386. /*
  387. * Currently active port and node names for FC devices.
  388. */
  389. u64 node_name;
  390. u64 port_name;
  391. };
  392. struct hv_host_device {
  393. struct hv_device *dev;
  394. unsigned int port;
  395. unsigned char path;
  396. unsigned char target;
  397. };
  398. struct storvsc_scan_work {
  399. struct work_struct work;
  400. struct Scsi_Host *host;
  401. u8 lun;
  402. u8 tgt_id;
  403. };
  404. static void storvsc_device_scan(struct work_struct *work)
  405. {
  406. struct storvsc_scan_work *wrk;
  407. struct scsi_device *sdev;
  408. wrk = container_of(work, struct storvsc_scan_work, work);
  409. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  410. if (!sdev)
  411. goto done;
  412. scsi_rescan_device(&sdev->sdev_gendev);
  413. scsi_device_put(sdev);
  414. done:
  415. kfree(wrk);
  416. }
  417. static void storvsc_host_scan(struct work_struct *work)
  418. {
  419. struct storvsc_scan_work *wrk;
  420. struct Scsi_Host *host;
  421. struct scsi_device *sdev;
  422. wrk = container_of(work, struct storvsc_scan_work, work);
  423. host = wrk->host;
  424. /*
  425. * Before scanning the host, first check to see if any of the
  426. * currrently known devices have been hot removed. We issue a
  427. * "unit ready" command against all currently known devices.
  428. * This I/O will result in an error for devices that have been
  429. * removed. As part of handling the I/O error, we remove the device.
  430. *
  431. * When a LUN is added or removed, the host sends us a signal to
  432. * scan the host. Thus we are forced to discover the LUNs that
  433. * may have been removed this way.
  434. */
  435. mutex_lock(&host->scan_mutex);
  436. shost_for_each_device(sdev, host)
  437. scsi_test_unit_ready(sdev, 1, 1, NULL);
  438. mutex_unlock(&host->scan_mutex);
  439. /*
  440. * Now scan the host to discover LUNs that may have been added.
  441. */
  442. scsi_scan_host(host);
  443. kfree(wrk);
  444. }
  445. static void storvsc_remove_lun(struct work_struct *work)
  446. {
  447. struct storvsc_scan_work *wrk;
  448. struct scsi_device *sdev;
  449. wrk = container_of(work, struct storvsc_scan_work, work);
  450. if (!scsi_host_get(wrk->host))
  451. goto done;
  452. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  453. if (sdev) {
  454. scsi_remove_device(sdev);
  455. scsi_device_put(sdev);
  456. }
  457. scsi_host_put(wrk->host);
  458. done:
  459. kfree(wrk);
  460. }
  461. /*
  462. * We can get incoming messages from the host that are not in response to
  463. * messages that we have sent out. An example of this would be messages
  464. * received by the guest to notify dynamic addition/removal of LUNs. To
  465. * deal with potential race conditions where the driver may be in the
  466. * midst of being unloaded when we might receive an unsolicited message
  467. * from the host, we have implemented a mechanism to gurantee sequential
  468. * consistency:
  469. *
  470. * 1) Once the device is marked as being destroyed, we will fail all
  471. * outgoing messages.
  472. * 2) We permit incoming messages when the device is being destroyed,
  473. * only to properly account for messages already sent out.
  474. */
  475. static inline struct storvsc_device *get_out_stor_device(
  476. struct hv_device *device)
  477. {
  478. struct storvsc_device *stor_device;
  479. stor_device = hv_get_drvdata(device);
  480. if (stor_device && stor_device->destroy)
  481. stor_device = NULL;
  482. return stor_device;
  483. }
  484. static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
  485. {
  486. dev->drain_notify = true;
  487. wait_event(dev->waiting_to_drain,
  488. atomic_read(&dev->num_outstanding_req) == 0);
  489. dev->drain_notify = false;
  490. }
  491. static inline struct storvsc_device *get_in_stor_device(
  492. struct hv_device *device)
  493. {
  494. struct storvsc_device *stor_device;
  495. stor_device = hv_get_drvdata(device);
  496. if (!stor_device)
  497. goto get_in_err;
  498. /*
  499. * If the device is being destroyed; allow incoming
  500. * traffic only to cleanup outstanding requests.
  501. */
  502. if (stor_device->destroy &&
  503. (atomic_read(&stor_device->num_outstanding_req) == 0))
  504. stor_device = NULL;
  505. get_in_err:
  506. return stor_device;
  507. }
  508. static void handle_sc_creation(struct vmbus_channel *new_sc)
  509. {
  510. struct hv_device *device = new_sc->primary_channel->device_obj;
  511. struct storvsc_device *stor_device;
  512. struct vmstorage_channel_properties props;
  513. stor_device = get_out_stor_device(device);
  514. if (!stor_device)
  515. return;
  516. if (stor_device->open_sub_channel == false)
  517. return;
  518. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  519. vmbus_open(new_sc,
  520. storvsc_ringbuffer_size,
  521. storvsc_ringbuffer_size,
  522. (void *)&props,
  523. sizeof(struct vmstorage_channel_properties),
  524. storvsc_on_channel_callback, new_sc);
  525. }
  526. static void handle_multichannel_storage(struct hv_device *device, int max_chns)
  527. {
  528. struct storvsc_device *stor_device;
  529. int num_cpus = num_online_cpus();
  530. int num_sc;
  531. struct storvsc_cmd_request *request;
  532. struct vstor_packet *vstor_packet;
  533. int ret, t;
  534. num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
  535. stor_device = get_out_stor_device(device);
  536. if (!stor_device)
  537. return;
  538. request = &stor_device->init_request;
  539. vstor_packet = &request->vstor_packet;
  540. stor_device->open_sub_channel = true;
  541. /*
  542. * Establish a handler for dealing with subchannels.
  543. */
  544. vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
  545. /*
  546. * Check to see if sub-channels have already been created. This
  547. * can happen when this driver is re-loaded after unloading.
  548. */
  549. if (vmbus_are_subchannels_present(device->channel))
  550. return;
  551. stor_device->open_sub_channel = false;
  552. /*
  553. * Request the host to create sub-channels.
  554. */
  555. memset(request, 0, sizeof(struct storvsc_cmd_request));
  556. init_completion(&request->wait_event);
  557. vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
  558. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  559. vstor_packet->sub_channel_count = num_sc;
  560. ret = vmbus_sendpacket(device->channel, vstor_packet,
  561. (sizeof(struct vstor_packet) -
  562. vmscsi_size_delta),
  563. (unsigned long)request,
  564. VM_PKT_DATA_INBAND,
  565. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  566. if (ret != 0)
  567. return;
  568. t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
  569. if (t == 0)
  570. return;
  571. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  572. vstor_packet->status != 0)
  573. return;
  574. /*
  575. * Now that we created the sub-channels, invoke the check; this
  576. * may trigger the callback.
  577. */
  578. stor_device->open_sub_channel = true;
  579. vmbus_are_subchannels_present(device->channel);
  580. }
  581. static void cache_wwn(struct storvsc_device *stor_device,
  582. struct vstor_packet *vstor_packet)
  583. {
  584. /*
  585. * Cache the currently active port and node ww names.
  586. */
  587. if (vstor_packet->wwn_packet.primary_active) {
  588. stor_device->node_name =
  589. wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
  590. stor_device->port_name =
  591. wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
  592. } else {
  593. stor_device->node_name =
  594. wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
  595. stor_device->port_name =
  596. wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
  597. }
  598. }
  599. static int storvsc_execute_vstor_op(struct hv_device *device,
  600. struct storvsc_cmd_request *request,
  601. bool status_check)
  602. {
  603. struct vstor_packet *vstor_packet;
  604. int ret, t;
  605. vstor_packet = &request->vstor_packet;
  606. init_completion(&request->wait_event);
  607. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  608. ret = vmbus_sendpacket(device->channel, vstor_packet,
  609. (sizeof(struct vstor_packet) -
  610. vmscsi_size_delta),
  611. (unsigned long)request,
  612. VM_PKT_DATA_INBAND,
  613. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  614. if (ret != 0)
  615. return ret;
  616. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  617. if (t == 0)
  618. return -ETIMEDOUT;
  619. if (!status_check)
  620. return ret;
  621. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  622. vstor_packet->status != 0)
  623. return -EINVAL;
  624. return ret;
  625. }
  626. static int storvsc_channel_init(struct hv_device *device, bool is_fc)
  627. {
  628. struct storvsc_device *stor_device;
  629. struct storvsc_cmd_request *request;
  630. struct vstor_packet *vstor_packet;
  631. int ret, i;
  632. int max_chns;
  633. bool process_sub_channels = false;
  634. stor_device = get_out_stor_device(device);
  635. if (!stor_device)
  636. return -ENODEV;
  637. request = &stor_device->init_request;
  638. vstor_packet = &request->vstor_packet;
  639. /*
  640. * Now, initiate the vsc/vsp initialization protocol on the open
  641. * channel
  642. */
  643. memset(request, 0, sizeof(struct storvsc_cmd_request));
  644. vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
  645. ret = storvsc_execute_vstor_op(device, request, true);
  646. if (ret)
  647. return ret;
  648. /*
  649. * Query host supported protocol version.
  650. */
  651. for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
  652. /* reuse the packet for version range supported */
  653. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  654. vstor_packet->operation =
  655. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
  656. vstor_packet->version.major_minor =
  657. vmstor_protocols[i].protocol_version;
  658. /*
  659. * The revision number is only used in Windows; set it to 0.
  660. */
  661. vstor_packet->version.revision = 0;
  662. ret = storvsc_execute_vstor_op(device, request, false);
  663. if (ret != 0)
  664. return ret;
  665. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
  666. return -EINVAL;
  667. if (vstor_packet->status == 0) {
  668. vmstor_proto_version =
  669. vmstor_protocols[i].protocol_version;
  670. sense_buffer_size =
  671. vmstor_protocols[i].sense_buffer_size;
  672. vmscsi_size_delta =
  673. vmstor_protocols[i].vmscsi_size_delta;
  674. break;
  675. }
  676. }
  677. if (vstor_packet->status != 0)
  678. return -EINVAL;
  679. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  680. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
  681. ret = storvsc_execute_vstor_op(device, request, true);
  682. if (ret != 0)
  683. return ret;
  684. /*
  685. * Check to see if multi-channel support is there.
  686. * Hosts that implement protocol version of 5.1 and above
  687. * support multi-channel.
  688. */
  689. max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
  690. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
  691. if (vstor_packet->storage_channel_properties.flags &
  692. STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
  693. process_sub_channels = true;
  694. }
  695. stor_device->max_transfer_bytes =
  696. vstor_packet->storage_channel_properties.max_transfer_bytes;
  697. if (!is_fc)
  698. goto done;
  699. /*
  700. * For FC devices retrieve FC HBA data.
  701. */
  702. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  703. vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
  704. ret = storvsc_execute_vstor_op(device, request, true);
  705. if (ret != 0)
  706. return ret;
  707. /*
  708. * Cache the currently active port and node ww names.
  709. */
  710. cache_wwn(stor_device, vstor_packet);
  711. done:
  712. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  713. vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
  714. ret = storvsc_execute_vstor_op(device, request, true);
  715. if (ret != 0)
  716. return ret;
  717. if (process_sub_channels)
  718. handle_multichannel_storage(device, max_chns);
  719. return ret;
  720. }
  721. static void storvsc_handle_error(struct vmscsi_request *vm_srb,
  722. struct scsi_cmnd *scmnd,
  723. struct Scsi_Host *host,
  724. u8 asc, u8 ascq)
  725. {
  726. struct storvsc_scan_work *wrk;
  727. void (*process_err_fn)(struct work_struct *work);
  728. bool do_work = false;
  729. switch (SRB_STATUS(vm_srb->srb_status)) {
  730. case SRB_STATUS_ERROR:
  731. /*
  732. * Let upper layer deal with error when
  733. * sense message is present.
  734. */
  735. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)
  736. break;
  737. /*
  738. * If there is an error; offline the device since all
  739. * error recovery strategies would have already been
  740. * deployed on the host side. However, if the command
  741. * were a pass-through command deal with it appropriately.
  742. */
  743. switch (scmnd->cmnd[0]) {
  744. case ATA_16:
  745. case ATA_12:
  746. set_host_byte(scmnd, DID_PASSTHROUGH);
  747. break;
  748. /*
  749. * On Some Windows hosts TEST_UNIT_READY command can return
  750. * SRB_STATUS_ERROR, let the upper level code deal with it
  751. * based on the sense information.
  752. */
  753. case TEST_UNIT_READY:
  754. break;
  755. default:
  756. set_host_byte(scmnd, DID_ERROR);
  757. }
  758. break;
  759. case SRB_STATUS_INVALID_LUN:
  760. set_host_byte(scmnd, DID_NO_CONNECT);
  761. do_work = true;
  762. process_err_fn = storvsc_remove_lun;
  763. break;
  764. case SRB_STATUS_ABORTED:
  765. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
  766. (asc == 0x2a) && (ascq == 0x9)) {
  767. do_work = true;
  768. process_err_fn = storvsc_device_scan;
  769. /*
  770. * Retry the I/O that trigerred this.
  771. */
  772. set_host_byte(scmnd, DID_REQUEUE);
  773. }
  774. break;
  775. }
  776. if (!do_work)
  777. return;
  778. /*
  779. * We need to schedule work to process this error; schedule it.
  780. */
  781. wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  782. if (!wrk) {
  783. set_host_byte(scmnd, DID_TARGET_FAILURE);
  784. return;
  785. }
  786. wrk->host = host;
  787. wrk->lun = vm_srb->lun;
  788. wrk->tgt_id = vm_srb->target_id;
  789. INIT_WORK(&wrk->work, process_err_fn);
  790. schedule_work(&wrk->work);
  791. }
  792. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
  793. struct storvsc_device *stor_dev)
  794. {
  795. struct scsi_cmnd *scmnd = cmd_request->cmd;
  796. struct scsi_sense_hdr sense_hdr;
  797. struct vmscsi_request *vm_srb;
  798. u32 data_transfer_length;
  799. struct Scsi_Host *host;
  800. u32 payload_sz = cmd_request->payload_sz;
  801. void *payload = cmd_request->payload;
  802. host = stor_dev->host;
  803. vm_srb = &cmd_request->vstor_packet.vm_srb;
  804. data_transfer_length = vm_srb->data_transfer_length;
  805. scmnd->result = vm_srb->scsi_status;
  806. if (scmnd->result) {
  807. if (scsi_normalize_sense(scmnd->sense_buffer,
  808. SCSI_SENSE_BUFFERSIZE, &sense_hdr) &&
  809. !(sense_hdr.sense_key == NOT_READY &&
  810. sense_hdr.asc == 0x03A) &&
  811. do_logging(STORVSC_LOGGING_ERROR))
  812. scsi_print_sense_hdr(scmnd->device, "storvsc",
  813. &sense_hdr);
  814. }
  815. if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
  816. storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
  817. sense_hdr.ascq);
  818. /*
  819. * The Windows driver set data_transfer_length on
  820. * SRB_STATUS_DATA_OVERRUN. On other errors, this value
  821. * is untouched. In these cases we set it to 0.
  822. */
  823. if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
  824. data_transfer_length = 0;
  825. }
  826. scsi_set_resid(scmnd,
  827. cmd_request->payload->range.len - data_transfer_length);
  828. scmnd->scsi_done(scmnd);
  829. if (payload_sz >
  830. sizeof(struct vmbus_channel_packet_multipage_buffer))
  831. kfree(payload);
  832. }
  833. static void storvsc_on_io_completion(struct storvsc_device *stor_device,
  834. struct vstor_packet *vstor_packet,
  835. struct storvsc_cmd_request *request)
  836. {
  837. struct vstor_packet *stor_pkt;
  838. struct hv_device *device = stor_device->device;
  839. stor_pkt = &request->vstor_packet;
  840. /*
  841. * The current SCSI handling on the host side does
  842. * not correctly handle:
  843. * INQUIRY command with page code parameter set to 0x80
  844. * MODE_SENSE command with cmd[2] == 0x1c
  845. *
  846. * Setup srb and scsi status so this won't be fatal.
  847. * We do this so we can distinguish truly fatal failues
  848. * (srb status == 0x4) and off-line the device in that case.
  849. */
  850. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  851. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  852. vstor_packet->vm_srb.scsi_status = 0;
  853. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  854. }
  855. /* Copy over the status...etc */
  856. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  857. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  858. stor_pkt->vm_srb.sense_info_length =
  859. vstor_packet->vm_srb.sense_info_length;
  860. if (vstor_packet->vm_srb.scsi_status != 0 ||
  861. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS)
  862. storvsc_log(device, STORVSC_LOGGING_WARN,
  863. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  864. stor_pkt->vm_srb.cdb[0],
  865. vstor_packet->vm_srb.scsi_status,
  866. vstor_packet->vm_srb.srb_status);
  867. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  868. /* CHECK_CONDITION */
  869. if (vstor_packet->vm_srb.srb_status &
  870. SRB_STATUS_AUTOSENSE_VALID) {
  871. /* autosense data available */
  872. storvsc_log(device, STORVSC_LOGGING_WARN,
  873. "stor pkt %p autosense data valid - len %d\n",
  874. request, vstor_packet->vm_srb.sense_info_length);
  875. memcpy(request->cmd->sense_buffer,
  876. vstor_packet->vm_srb.sense_data,
  877. vstor_packet->vm_srb.sense_info_length);
  878. }
  879. }
  880. stor_pkt->vm_srb.data_transfer_length =
  881. vstor_packet->vm_srb.data_transfer_length;
  882. storvsc_command_completion(request, stor_device);
  883. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  884. stor_device->drain_notify)
  885. wake_up(&stor_device->waiting_to_drain);
  886. }
  887. static void storvsc_on_receive(struct storvsc_device *stor_device,
  888. struct vstor_packet *vstor_packet,
  889. struct storvsc_cmd_request *request)
  890. {
  891. struct storvsc_scan_work *work;
  892. switch (vstor_packet->operation) {
  893. case VSTOR_OPERATION_COMPLETE_IO:
  894. storvsc_on_io_completion(stor_device, vstor_packet, request);
  895. break;
  896. case VSTOR_OPERATION_REMOVE_DEVICE:
  897. case VSTOR_OPERATION_ENUMERATE_BUS:
  898. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  899. if (!work)
  900. return;
  901. INIT_WORK(&work->work, storvsc_host_scan);
  902. work->host = stor_device->host;
  903. schedule_work(&work->work);
  904. break;
  905. case VSTOR_OPERATION_FCHBA_DATA:
  906. cache_wwn(stor_device, vstor_packet);
  907. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  908. fc_host_node_name(stor_device->host) = stor_device->node_name;
  909. fc_host_port_name(stor_device->host) = stor_device->port_name;
  910. #endif
  911. break;
  912. default:
  913. break;
  914. }
  915. }
  916. static void storvsc_on_channel_callback(void *context)
  917. {
  918. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  919. struct hv_device *device;
  920. struct storvsc_device *stor_device;
  921. u32 bytes_recvd;
  922. u64 request_id;
  923. unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
  924. struct storvsc_cmd_request *request;
  925. int ret;
  926. if (channel->primary_channel != NULL)
  927. device = channel->primary_channel->device_obj;
  928. else
  929. device = channel->device_obj;
  930. stor_device = get_in_stor_device(device);
  931. if (!stor_device)
  932. return;
  933. do {
  934. ret = vmbus_recvpacket(channel, packet,
  935. ALIGN((sizeof(struct vstor_packet) -
  936. vmscsi_size_delta), 8),
  937. &bytes_recvd, &request_id);
  938. if (ret == 0 && bytes_recvd > 0) {
  939. request = (struct storvsc_cmd_request *)
  940. (unsigned long)request_id;
  941. if ((request == &stor_device->init_request) ||
  942. (request == &stor_device->reset_request)) {
  943. memcpy(&request->vstor_packet, packet,
  944. (sizeof(struct vstor_packet) -
  945. vmscsi_size_delta));
  946. complete(&request->wait_event);
  947. } else {
  948. storvsc_on_receive(stor_device,
  949. (struct vstor_packet *)packet,
  950. request);
  951. }
  952. } else {
  953. break;
  954. }
  955. } while (1);
  956. return;
  957. }
  958. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
  959. bool is_fc)
  960. {
  961. struct vmstorage_channel_properties props;
  962. int ret;
  963. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  964. ret = vmbus_open(device->channel,
  965. ring_size,
  966. ring_size,
  967. (void *)&props,
  968. sizeof(struct vmstorage_channel_properties),
  969. storvsc_on_channel_callback, device->channel);
  970. if (ret != 0)
  971. return ret;
  972. ret = storvsc_channel_init(device, is_fc);
  973. return ret;
  974. }
  975. static int storvsc_dev_remove(struct hv_device *device)
  976. {
  977. struct storvsc_device *stor_device;
  978. unsigned long flags;
  979. stor_device = hv_get_drvdata(device);
  980. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  981. stor_device->destroy = true;
  982. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  983. /*
  984. * At this point, all outbound traffic should be disable. We
  985. * only allow inbound traffic (responses) to proceed so that
  986. * outstanding requests can be completed.
  987. */
  988. storvsc_wait_to_drain(stor_device);
  989. /*
  990. * Since we have already drained, we don't need to busy wait
  991. * as was done in final_release_stor_device()
  992. * Note that we cannot set the ext pointer to NULL until
  993. * we have drained - to drain the outgoing packets, we need to
  994. * allow incoming packets.
  995. */
  996. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  997. hv_set_drvdata(device, NULL);
  998. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  999. /* Close the channel */
  1000. vmbus_close(device->channel);
  1001. kfree(stor_device);
  1002. return 0;
  1003. }
  1004. static int storvsc_do_io(struct hv_device *device,
  1005. struct storvsc_cmd_request *request)
  1006. {
  1007. struct storvsc_device *stor_device;
  1008. struct vstor_packet *vstor_packet;
  1009. struct vmbus_channel *outgoing_channel;
  1010. int ret = 0;
  1011. vstor_packet = &request->vstor_packet;
  1012. stor_device = get_out_stor_device(device);
  1013. if (!stor_device)
  1014. return -ENODEV;
  1015. request->device = device;
  1016. /*
  1017. * Select an an appropriate channel to send the request out.
  1018. */
  1019. outgoing_channel = vmbus_get_outgoing_channel(device->channel);
  1020. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  1021. vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
  1022. vmscsi_size_delta);
  1023. vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
  1024. vstor_packet->vm_srb.data_transfer_length =
  1025. request->payload->range.len;
  1026. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  1027. if (request->payload->range.len) {
  1028. ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
  1029. request->payload, request->payload_sz,
  1030. vstor_packet,
  1031. (sizeof(struct vstor_packet) -
  1032. vmscsi_size_delta),
  1033. (unsigned long)request);
  1034. } else {
  1035. ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
  1036. (sizeof(struct vstor_packet) -
  1037. vmscsi_size_delta),
  1038. (unsigned long)request,
  1039. VM_PKT_DATA_INBAND,
  1040. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1041. }
  1042. if (ret != 0)
  1043. return ret;
  1044. atomic_inc(&stor_device->num_outstanding_req);
  1045. return ret;
  1046. }
  1047. static int storvsc_device_alloc(struct scsi_device *sdevice)
  1048. {
  1049. /*
  1050. * Set blist flag to permit the reading of the VPD pages even when
  1051. * the target may claim SPC-2 compliance. MSFT targets currently
  1052. * claim SPC-2 compliance while they implement post SPC-2 features.
  1053. * With this flag we can correctly handle WRITE_SAME_16 issues.
  1054. *
  1055. * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
  1056. * still supports REPORT LUN.
  1057. */
  1058. sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
  1059. return 0;
  1060. }
  1061. static int storvsc_device_configure(struct scsi_device *sdevice)
  1062. {
  1063. blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
  1064. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  1065. blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
  1066. /* Ensure there are no gaps in presented sgls */
  1067. blk_queue_virt_boundary(sdevice->request_queue, PAGE_SIZE - 1);
  1068. sdevice->no_write_same = 1;
  1069. /*
  1070. * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
  1071. * if the device is a MSFT virtual device. If the host is
  1072. * WIN10 or newer, allow write_same.
  1073. */
  1074. if (!strncmp(sdevice->vendor, "Msft", 4)) {
  1075. switch (vmstor_proto_version) {
  1076. case VMSTOR_PROTO_VERSION_WIN8:
  1077. case VMSTOR_PROTO_VERSION_WIN8_1:
  1078. sdevice->scsi_level = SCSI_SPC_3;
  1079. break;
  1080. }
  1081. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
  1082. sdevice->no_write_same = 0;
  1083. }
  1084. return 0;
  1085. }
  1086. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  1087. sector_t capacity, int *info)
  1088. {
  1089. sector_t nsect = capacity;
  1090. sector_t cylinders = nsect;
  1091. int heads, sectors_pt;
  1092. /*
  1093. * We are making up these values; let us keep it simple.
  1094. */
  1095. heads = 0xff;
  1096. sectors_pt = 0x3f; /* Sectors per track */
  1097. sector_div(cylinders, heads * sectors_pt);
  1098. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  1099. cylinders = 0xffff;
  1100. info[0] = heads;
  1101. info[1] = sectors_pt;
  1102. info[2] = (int)cylinders;
  1103. return 0;
  1104. }
  1105. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  1106. {
  1107. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  1108. struct hv_device *device = host_dev->dev;
  1109. struct storvsc_device *stor_device;
  1110. struct storvsc_cmd_request *request;
  1111. struct vstor_packet *vstor_packet;
  1112. int ret, t;
  1113. stor_device = get_out_stor_device(device);
  1114. if (!stor_device)
  1115. return FAILED;
  1116. request = &stor_device->reset_request;
  1117. vstor_packet = &request->vstor_packet;
  1118. init_completion(&request->wait_event);
  1119. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  1120. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  1121. vstor_packet->vm_srb.path_id = stor_device->path_id;
  1122. ret = vmbus_sendpacket(device->channel, vstor_packet,
  1123. (sizeof(struct vstor_packet) -
  1124. vmscsi_size_delta),
  1125. (unsigned long)&stor_device->reset_request,
  1126. VM_PKT_DATA_INBAND,
  1127. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1128. if (ret != 0)
  1129. return FAILED;
  1130. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  1131. if (t == 0)
  1132. return TIMEOUT_ERROR;
  1133. /*
  1134. * At this point, all outstanding requests in the adapter
  1135. * should have been flushed out and return to us
  1136. * There is a potential race here where the host may be in
  1137. * the process of responding when we return from here.
  1138. * Just wait for all in-transit packets to be accounted for
  1139. * before we return from here.
  1140. */
  1141. storvsc_wait_to_drain(stor_device);
  1142. return SUCCESS;
  1143. }
  1144. /*
  1145. * The host guarantees to respond to each command, although I/O latencies might
  1146. * be unbounded on Azure. Reset the timer unconditionally to give the host a
  1147. * chance to perform EH.
  1148. */
  1149. static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
  1150. {
  1151. return BLK_EH_RESET_TIMER;
  1152. }
  1153. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  1154. {
  1155. bool allowed = true;
  1156. u8 scsi_op = scmnd->cmnd[0];
  1157. switch (scsi_op) {
  1158. /* the host does not handle WRITE_SAME, log accident usage */
  1159. case WRITE_SAME:
  1160. /*
  1161. * smartd sends this command and the host does not handle
  1162. * this. So, don't send it.
  1163. */
  1164. case SET_WINDOW:
  1165. scmnd->result = ILLEGAL_REQUEST << 16;
  1166. allowed = false;
  1167. break;
  1168. default:
  1169. break;
  1170. }
  1171. return allowed;
  1172. }
  1173. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  1174. {
  1175. int ret;
  1176. struct hv_host_device *host_dev = shost_priv(host);
  1177. struct hv_device *dev = host_dev->dev;
  1178. struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
  1179. int i;
  1180. struct scatterlist *sgl;
  1181. unsigned int sg_count = 0;
  1182. struct vmscsi_request *vm_srb;
  1183. struct scatterlist *cur_sgl;
  1184. struct vmbus_packet_mpb_array *payload;
  1185. u32 payload_sz;
  1186. u32 length;
  1187. if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
  1188. /*
  1189. * On legacy hosts filter unimplemented commands.
  1190. * Future hosts are expected to correctly handle
  1191. * unsupported commands. Furthermore, it is
  1192. * possible that some of the currently
  1193. * unsupported commands maybe supported in
  1194. * future versions of the host.
  1195. */
  1196. if (!storvsc_scsi_cmd_ok(scmnd)) {
  1197. scmnd->scsi_done(scmnd);
  1198. return 0;
  1199. }
  1200. }
  1201. /* Setup the cmd request */
  1202. cmd_request->cmd = scmnd;
  1203. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1204. vm_srb->win8_extension.time_out_value = 60;
  1205. vm_srb->win8_extension.srb_flags |=
  1206. SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1207. if (scmnd->device->tagged_supported) {
  1208. vm_srb->win8_extension.srb_flags |=
  1209. (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
  1210. vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
  1211. vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
  1212. }
  1213. /* Build the SRB */
  1214. switch (scmnd->sc_data_direction) {
  1215. case DMA_TO_DEVICE:
  1216. vm_srb->data_in = WRITE_TYPE;
  1217. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
  1218. break;
  1219. case DMA_FROM_DEVICE:
  1220. vm_srb->data_in = READ_TYPE;
  1221. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
  1222. break;
  1223. case DMA_NONE:
  1224. vm_srb->data_in = UNKNOWN_TYPE;
  1225. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
  1226. break;
  1227. default:
  1228. /*
  1229. * This is DMA_BIDIRECTIONAL or something else we are never
  1230. * supposed to see here.
  1231. */
  1232. WARN(1, "Unexpected data direction: %d\n",
  1233. scmnd->sc_data_direction);
  1234. return -EINVAL;
  1235. }
  1236. vm_srb->port_number = host_dev->port;
  1237. vm_srb->path_id = scmnd->device->channel;
  1238. vm_srb->target_id = scmnd->device->id;
  1239. vm_srb->lun = scmnd->device->lun;
  1240. vm_srb->cdb_length = scmnd->cmd_len;
  1241. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1242. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1243. sg_count = scsi_sg_count(scmnd);
  1244. length = scsi_bufflen(scmnd);
  1245. payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
  1246. payload_sz = sizeof(cmd_request->mpb);
  1247. if (sg_count) {
  1248. if (sg_count > MAX_PAGE_BUFFER_COUNT) {
  1249. payload_sz = (sg_count * sizeof(void *) +
  1250. sizeof(struct vmbus_packet_mpb_array));
  1251. payload = kmalloc(payload_sz, GFP_ATOMIC);
  1252. if (!payload)
  1253. return SCSI_MLQUEUE_DEVICE_BUSY;
  1254. }
  1255. payload->range.len = length;
  1256. payload->range.offset = sgl[0].offset;
  1257. cur_sgl = sgl;
  1258. for (i = 0; i < sg_count; i++) {
  1259. payload->range.pfn_array[i] =
  1260. page_to_pfn(sg_page((cur_sgl)));
  1261. cur_sgl = sg_next(cur_sgl);
  1262. }
  1263. } else if (scsi_sglist(scmnd)) {
  1264. payload->range.len = length;
  1265. payload->range.offset =
  1266. virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
  1267. payload->range.pfn_array[0] =
  1268. virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
  1269. }
  1270. cmd_request->payload = payload;
  1271. cmd_request->payload_sz = payload_sz;
  1272. /* Invokes the vsc to start an IO */
  1273. ret = storvsc_do_io(dev, cmd_request);
  1274. if (ret == -EAGAIN) {
  1275. if (payload_sz > sizeof(cmd_request->mpb))
  1276. kfree(payload);
  1277. /* no more space */
  1278. return SCSI_MLQUEUE_DEVICE_BUSY;
  1279. }
  1280. return 0;
  1281. }
  1282. static struct scsi_host_template scsi_driver = {
  1283. .module = THIS_MODULE,
  1284. .name = "storvsc_host_t",
  1285. .cmd_size = sizeof(struct storvsc_cmd_request),
  1286. .bios_param = storvsc_get_chs,
  1287. .queuecommand = storvsc_queuecommand,
  1288. .eh_host_reset_handler = storvsc_host_reset_handler,
  1289. .proc_name = "storvsc_host",
  1290. .eh_timed_out = storvsc_eh_timed_out,
  1291. .slave_alloc = storvsc_device_alloc,
  1292. .slave_configure = storvsc_device_configure,
  1293. .cmd_per_lun = 2048,
  1294. .this_id = -1,
  1295. .use_clustering = ENABLE_CLUSTERING,
  1296. /* Make sure we dont get a sg segment crosses a page boundary */
  1297. .dma_boundary = PAGE_SIZE-1,
  1298. .no_write_same = 1,
  1299. };
  1300. enum {
  1301. SCSI_GUID,
  1302. IDE_GUID,
  1303. SFC_GUID,
  1304. };
  1305. static const struct hv_vmbus_device_id id_table[] = {
  1306. /* SCSI guid */
  1307. { HV_SCSI_GUID,
  1308. .driver_data = SCSI_GUID
  1309. },
  1310. /* IDE guid */
  1311. { HV_IDE_GUID,
  1312. .driver_data = IDE_GUID
  1313. },
  1314. /* Fibre Channel GUID */
  1315. {
  1316. HV_SYNTHFC_GUID,
  1317. .driver_data = SFC_GUID
  1318. },
  1319. { },
  1320. };
  1321. MODULE_DEVICE_TABLE(vmbus, id_table);
  1322. static int storvsc_probe(struct hv_device *device,
  1323. const struct hv_vmbus_device_id *dev_id)
  1324. {
  1325. int ret;
  1326. int num_cpus = num_online_cpus();
  1327. struct Scsi_Host *host;
  1328. struct hv_host_device *host_dev;
  1329. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1330. bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
  1331. int target = 0;
  1332. struct storvsc_device *stor_device;
  1333. int max_luns_per_target;
  1334. int max_targets;
  1335. int max_channels;
  1336. int max_sub_channels = 0;
  1337. /*
  1338. * Based on the windows host we are running on,
  1339. * set state to properly communicate with the host.
  1340. */
  1341. if (vmbus_proto_version < VERSION_WIN8) {
  1342. max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1343. max_targets = STORVSC_IDE_MAX_TARGETS;
  1344. max_channels = STORVSC_IDE_MAX_CHANNELS;
  1345. } else {
  1346. max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
  1347. max_targets = STORVSC_MAX_TARGETS;
  1348. max_channels = STORVSC_MAX_CHANNELS;
  1349. /*
  1350. * On Windows8 and above, we support sub-channels for storage.
  1351. * The number of sub-channels offerred is based on the number of
  1352. * VCPUs in the guest.
  1353. */
  1354. max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
  1355. }
  1356. scsi_driver.can_queue = (max_outstanding_req_per_channel *
  1357. (max_sub_channels + 1));
  1358. host = scsi_host_alloc(&scsi_driver,
  1359. sizeof(struct hv_host_device));
  1360. if (!host)
  1361. return -ENOMEM;
  1362. host_dev = shost_priv(host);
  1363. memset(host_dev, 0, sizeof(struct hv_host_device));
  1364. host_dev->port = host->host_no;
  1365. host_dev->dev = device;
  1366. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1367. if (!stor_device) {
  1368. ret = -ENOMEM;
  1369. goto err_out0;
  1370. }
  1371. stor_device->destroy = false;
  1372. stor_device->open_sub_channel = false;
  1373. init_waitqueue_head(&stor_device->waiting_to_drain);
  1374. stor_device->device = device;
  1375. stor_device->host = host;
  1376. hv_set_drvdata(device, stor_device);
  1377. stor_device->port_number = host->host_no;
  1378. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
  1379. if (ret)
  1380. goto err_out1;
  1381. host_dev->path = stor_device->path_id;
  1382. host_dev->target = stor_device->target_id;
  1383. switch (dev_id->driver_data) {
  1384. case SFC_GUID:
  1385. host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
  1386. host->max_id = STORVSC_FC_MAX_TARGETS;
  1387. host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
  1388. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1389. host->transportt = fc_transport_template;
  1390. #endif
  1391. break;
  1392. case SCSI_GUID:
  1393. host->max_lun = max_luns_per_target;
  1394. host->max_id = max_targets;
  1395. host->max_channel = max_channels - 1;
  1396. break;
  1397. default:
  1398. host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1399. host->max_id = STORVSC_IDE_MAX_TARGETS;
  1400. host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
  1401. break;
  1402. }
  1403. /* max cmd length */
  1404. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1405. /*
  1406. * set the table size based on the info we got
  1407. * from the host.
  1408. */
  1409. host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
  1410. /* Register the HBA and start the scsi bus scan */
  1411. ret = scsi_add_host(host, &device->device);
  1412. if (ret != 0)
  1413. goto err_out2;
  1414. if (!dev_is_ide) {
  1415. scsi_scan_host(host);
  1416. } else {
  1417. target = (device->dev_instance.b[5] << 8 |
  1418. device->dev_instance.b[4]);
  1419. ret = scsi_add_device(host, 0, target, 0);
  1420. if (ret) {
  1421. scsi_remove_host(host);
  1422. goto err_out2;
  1423. }
  1424. }
  1425. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1426. if (host->transportt == fc_transport_template) {
  1427. fc_host_node_name(host) = stor_device->node_name;
  1428. fc_host_port_name(host) = stor_device->port_name;
  1429. }
  1430. #endif
  1431. return 0;
  1432. err_out2:
  1433. /*
  1434. * Once we have connected with the host, we would need to
  1435. * to invoke storvsc_dev_remove() to rollback this state and
  1436. * this call also frees up the stor_device; hence the jump around
  1437. * err_out1 label.
  1438. */
  1439. storvsc_dev_remove(device);
  1440. goto err_out0;
  1441. err_out1:
  1442. kfree(stor_device);
  1443. err_out0:
  1444. scsi_host_put(host);
  1445. return ret;
  1446. }
  1447. static int storvsc_remove(struct hv_device *dev)
  1448. {
  1449. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1450. struct Scsi_Host *host = stor_device->host;
  1451. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1452. if (host->transportt == fc_transport_template)
  1453. fc_remove_host(host);
  1454. #endif
  1455. scsi_remove_host(host);
  1456. storvsc_dev_remove(dev);
  1457. scsi_host_put(host);
  1458. return 0;
  1459. }
  1460. static struct hv_driver storvsc_drv = {
  1461. .name = KBUILD_MODNAME,
  1462. .id_table = id_table,
  1463. .probe = storvsc_probe,
  1464. .remove = storvsc_remove,
  1465. };
  1466. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1467. static struct fc_function_template fc_transport_functions = {
  1468. .show_host_node_name = 1,
  1469. .show_host_port_name = 1,
  1470. };
  1471. #endif
  1472. static int __init storvsc_drv_init(void)
  1473. {
  1474. int ret;
  1475. /*
  1476. * Divide the ring buffer data size (which is 1 page less
  1477. * than the ring buffer size since that page is reserved for
  1478. * the ring buffer indices) by the max request size (which is
  1479. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1480. */
  1481. max_outstanding_req_per_channel =
  1482. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1483. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1484. sizeof(struct vstor_packet) + sizeof(u64) -
  1485. vmscsi_size_delta,
  1486. sizeof(u64)));
  1487. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1488. fc_transport_template = fc_attach_transport(&fc_transport_functions);
  1489. if (!fc_transport_template)
  1490. return -ENODEV;
  1491. /*
  1492. * Install Hyper-V specific timeout handler.
  1493. */
  1494. fc_transport_template->eh_timed_out = storvsc_eh_timed_out;
  1495. #endif
  1496. ret = vmbus_driver_register(&storvsc_drv);
  1497. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1498. if (ret)
  1499. fc_release_transport(fc_transport_template);
  1500. #endif
  1501. return ret;
  1502. }
  1503. static void __exit storvsc_drv_exit(void)
  1504. {
  1505. vmbus_driver_unregister(&storvsc_drv);
  1506. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1507. fc_release_transport(fc_transport_template);
  1508. #endif
  1509. }
  1510. MODULE_LICENSE("GPL");
  1511. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1512. module_init(storvsc_drv_init);
  1513. module_exit(storvsc_drv_exit);