storvsc_drv.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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_TARGET_FAILURE);
  757. }
  758. break;
  759. case SRB_STATUS_INVALID_LUN:
  760. do_work = true;
  761. process_err_fn = storvsc_remove_lun;
  762. break;
  763. case SRB_STATUS_ABORTED:
  764. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
  765. (asc == 0x2a) && (ascq == 0x9)) {
  766. do_work = true;
  767. process_err_fn = storvsc_device_scan;
  768. /*
  769. * Retry the I/O that trigerred this.
  770. */
  771. set_host_byte(scmnd, DID_REQUEUE);
  772. }
  773. break;
  774. }
  775. if (!do_work)
  776. return;
  777. /*
  778. * We need to schedule work to process this error; schedule it.
  779. */
  780. wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  781. if (!wrk) {
  782. set_host_byte(scmnd, DID_TARGET_FAILURE);
  783. return;
  784. }
  785. wrk->host = host;
  786. wrk->lun = vm_srb->lun;
  787. wrk->tgt_id = vm_srb->target_id;
  788. INIT_WORK(&wrk->work, process_err_fn);
  789. schedule_work(&wrk->work);
  790. }
  791. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
  792. struct storvsc_device *stor_dev)
  793. {
  794. struct scsi_cmnd *scmnd = cmd_request->cmd;
  795. struct scsi_sense_hdr sense_hdr;
  796. struct vmscsi_request *vm_srb;
  797. u32 data_transfer_length;
  798. struct Scsi_Host *host;
  799. u32 payload_sz = cmd_request->payload_sz;
  800. void *payload = cmd_request->payload;
  801. host = stor_dev->host;
  802. vm_srb = &cmd_request->vstor_packet.vm_srb;
  803. data_transfer_length = vm_srb->data_transfer_length;
  804. scmnd->result = vm_srb->scsi_status;
  805. if (scmnd->result) {
  806. if (scsi_normalize_sense(scmnd->sense_buffer,
  807. SCSI_SENSE_BUFFERSIZE, &sense_hdr) &&
  808. !(sense_hdr.sense_key == NOT_READY &&
  809. sense_hdr.asc == 0x03A) &&
  810. do_logging(STORVSC_LOGGING_ERROR))
  811. scsi_print_sense_hdr(scmnd->device, "storvsc",
  812. &sense_hdr);
  813. }
  814. if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
  815. storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
  816. sense_hdr.ascq);
  817. /*
  818. * The Windows driver set data_transfer_length on
  819. * SRB_STATUS_DATA_OVERRUN. On other errors, this value
  820. * is untouched. In these cases we set it to 0.
  821. */
  822. if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
  823. data_transfer_length = 0;
  824. }
  825. scsi_set_resid(scmnd,
  826. cmd_request->payload->range.len - data_transfer_length);
  827. scmnd->scsi_done(scmnd);
  828. if (payload_sz >
  829. sizeof(struct vmbus_channel_packet_multipage_buffer))
  830. kfree(payload);
  831. }
  832. static void storvsc_on_io_completion(struct storvsc_device *stor_device,
  833. struct vstor_packet *vstor_packet,
  834. struct storvsc_cmd_request *request)
  835. {
  836. struct vstor_packet *stor_pkt;
  837. struct hv_device *device = stor_device->device;
  838. stor_pkt = &request->vstor_packet;
  839. /*
  840. * The current SCSI handling on the host side does
  841. * not correctly handle:
  842. * INQUIRY command with page code parameter set to 0x80
  843. * MODE_SENSE command with cmd[2] == 0x1c
  844. *
  845. * Setup srb and scsi status so this won't be fatal.
  846. * We do this so we can distinguish truly fatal failues
  847. * (srb status == 0x4) and off-line the device in that case.
  848. */
  849. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  850. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  851. vstor_packet->vm_srb.scsi_status = 0;
  852. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  853. }
  854. /* Copy over the status...etc */
  855. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  856. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  857. stor_pkt->vm_srb.sense_info_length =
  858. vstor_packet->vm_srb.sense_info_length;
  859. if (vstor_packet->vm_srb.scsi_status != 0 ||
  860. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS)
  861. storvsc_log(device, STORVSC_LOGGING_WARN,
  862. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  863. stor_pkt->vm_srb.cdb[0],
  864. vstor_packet->vm_srb.scsi_status,
  865. vstor_packet->vm_srb.srb_status);
  866. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  867. /* CHECK_CONDITION */
  868. if (vstor_packet->vm_srb.srb_status &
  869. SRB_STATUS_AUTOSENSE_VALID) {
  870. /* autosense data available */
  871. storvsc_log(device, STORVSC_LOGGING_WARN,
  872. "stor pkt %p autosense data valid - len %d\n",
  873. request, vstor_packet->vm_srb.sense_info_length);
  874. memcpy(request->cmd->sense_buffer,
  875. vstor_packet->vm_srb.sense_data,
  876. vstor_packet->vm_srb.sense_info_length);
  877. }
  878. }
  879. stor_pkt->vm_srb.data_transfer_length =
  880. vstor_packet->vm_srb.data_transfer_length;
  881. storvsc_command_completion(request, stor_device);
  882. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  883. stor_device->drain_notify)
  884. wake_up(&stor_device->waiting_to_drain);
  885. }
  886. static void storvsc_on_receive(struct storvsc_device *stor_device,
  887. struct vstor_packet *vstor_packet,
  888. struct storvsc_cmd_request *request)
  889. {
  890. struct storvsc_scan_work *work;
  891. switch (vstor_packet->operation) {
  892. case VSTOR_OPERATION_COMPLETE_IO:
  893. storvsc_on_io_completion(stor_device, vstor_packet, request);
  894. break;
  895. case VSTOR_OPERATION_REMOVE_DEVICE:
  896. case VSTOR_OPERATION_ENUMERATE_BUS:
  897. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  898. if (!work)
  899. return;
  900. INIT_WORK(&work->work, storvsc_host_scan);
  901. work->host = stor_device->host;
  902. schedule_work(&work->work);
  903. break;
  904. case VSTOR_OPERATION_FCHBA_DATA:
  905. cache_wwn(stor_device, vstor_packet);
  906. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  907. fc_host_node_name(stor_device->host) = stor_device->node_name;
  908. fc_host_port_name(stor_device->host) = stor_device->port_name;
  909. #endif
  910. break;
  911. default:
  912. break;
  913. }
  914. }
  915. static void storvsc_on_channel_callback(void *context)
  916. {
  917. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  918. struct hv_device *device;
  919. struct storvsc_device *stor_device;
  920. u32 bytes_recvd;
  921. u64 request_id;
  922. unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
  923. struct storvsc_cmd_request *request;
  924. int ret;
  925. if (channel->primary_channel != NULL)
  926. device = channel->primary_channel->device_obj;
  927. else
  928. device = channel->device_obj;
  929. stor_device = get_in_stor_device(device);
  930. if (!stor_device)
  931. return;
  932. do {
  933. ret = vmbus_recvpacket(channel, packet,
  934. ALIGN((sizeof(struct vstor_packet) -
  935. vmscsi_size_delta), 8),
  936. &bytes_recvd, &request_id);
  937. if (ret == 0 && bytes_recvd > 0) {
  938. request = (struct storvsc_cmd_request *)
  939. (unsigned long)request_id;
  940. if ((request == &stor_device->init_request) ||
  941. (request == &stor_device->reset_request)) {
  942. memcpy(&request->vstor_packet, packet,
  943. (sizeof(struct vstor_packet) -
  944. vmscsi_size_delta));
  945. complete(&request->wait_event);
  946. } else {
  947. storvsc_on_receive(stor_device,
  948. (struct vstor_packet *)packet,
  949. request);
  950. }
  951. } else {
  952. break;
  953. }
  954. } while (1);
  955. return;
  956. }
  957. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
  958. bool is_fc)
  959. {
  960. struct vmstorage_channel_properties props;
  961. int ret;
  962. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  963. ret = vmbus_open(device->channel,
  964. ring_size,
  965. ring_size,
  966. (void *)&props,
  967. sizeof(struct vmstorage_channel_properties),
  968. storvsc_on_channel_callback, device->channel);
  969. if (ret != 0)
  970. return ret;
  971. ret = storvsc_channel_init(device, is_fc);
  972. return ret;
  973. }
  974. static int storvsc_dev_remove(struct hv_device *device)
  975. {
  976. struct storvsc_device *stor_device;
  977. unsigned long flags;
  978. stor_device = hv_get_drvdata(device);
  979. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  980. stor_device->destroy = true;
  981. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  982. /*
  983. * At this point, all outbound traffic should be disable. We
  984. * only allow inbound traffic (responses) to proceed so that
  985. * outstanding requests can be completed.
  986. */
  987. storvsc_wait_to_drain(stor_device);
  988. /*
  989. * Since we have already drained, we don't need to busy wait
  990. * as was done in final_release_stor_device()
  991. * Note that we cannot set the ext pointer to NULL until
  992. * we have drained - to drain the outgoing packets, we need to
  993. * allow incoming packets.
  994. */
  995. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  996. hv_set_drvdata(device, NULL);
  997. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  998. /* Close the channel */
  999. vmbus_close(device->channel);
  1000. kfree(stor_device);
  1001. return 0;
  1002. }
  1003. static int storvsc_do_io(struct hv_device *device,
  1004. struct storvsc_cmd_request *request)
  1005. {
  1006. struct storvsc_device *stor_device;
  1007. struct vstor_packet *vstor_packet;
  1008. struct vmbus_channel *outgoing_channel;
  1009. int ret = 0;
  1010. vstor_packet = &request->vstor_packet;
  1011. stor_device = get_out_stor_device(device);
  1012. if (!stor_device)
  1013. return -ENODEV;
  1014. request->device = device;
  1015. /*
  1016. * Select an an appropriate channel to send the request out.
  1017. */
  1018. outgoing_channel = vmbus_get_outgoing_channel(device->channel);
  1019. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  1020. vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
  1021. vmscsi_size_delta);
  1022. vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
  1023. vstor_packet->vm_srb.data_transfer_length =
  1024. request->payload->range.len;
  1025. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  1026. if (request->payload->range.len) {
  1027. ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
  1028. request->payload, request->payload_sz,
  1029. vstor_packet,
  1030. (sizeof(struct vstor_packet) -
  1031. vmscsi_size_delta),
  1032. (unsigned long)request);
  1033. } else {
  1034. ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
  1035. (sizeof(struct vstor_packet) -
  1036. vmscsi_size_delta),
  1037. (unsigned long)request,
  1038. VM_PKT_DATA_INBAND,
  1039. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1040. }
  1041. if (ret != 0)
  1042. return ret;
  1043. atomic_inc(&stor_device->num_outstanding_req);
  1044. return ret;
  1045. }
  1046. static int storvsc_device_alloc(struct scsi_device *sdevice)
  1047. {
  1048. /*
  1049. * Set blist flag to permit the reading of the VPD pages even when
  1050. * the target may claim SPC-2 compliance. MSFT targets currently
  1051. * claim SPC-2 compliance while they implement post SPC-2 features.
  1052. * With this flag we can correctly handle WRITE_SAME_16 issues.
  1053. *
  1054. * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
  1055. * still supports REPORT LUN.
  1056. */
  1057. sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
  1058. return 0;
  1059. }
  1060. static int storvsc_device_configure(struct scsi_device *sdevice)
  1061. {
  1062. blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
  1063. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  1064. blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
  1065. /* Ensure there are no gaps in presented sgls */
  1066. blk_queue_virt_boundary(sdevice->request_queue, PAGE_SIZE - 1);
  1067. sdevice->no_write_same = 1;
  1068. /*
  1069. * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
  1070. * if the device is a MSFT virtual device. If the host is
  1071. * WIN10 or newer, allow write_same.
  1072. */
  1073. if (!strncmp(sdevice->vendor, "Msft", 4)) {
  1074. switch (vmstor_proto_version) {
  1075. case VMSTOR_PROTO_VERSION_WIN8:
  1076. case VMSTOR_PROTO_VERSION_WIN8_1:
  1077. sdevice->scsi_level = SCSI_SPC_3;
  1078. break;
  1079. }
  1080. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
  1081. sdevice->no_write_same = 0;
  1082. }
  1083. return 0;
  1084. }
  1085. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  1086. sector_t capacity, int *info)
  1087. {
  1088. sector_t nsect = capacity;
  1089. sector_t cylinders = nsect;
  1090. int heads, sectors_pt;
  1091. /*
  1092. * We are making up these values; let us keep it simple.
  1093. */
  1094. heads = 0xff;
  1095. sectors_pt = 0x3f; /* Sectors per track */
  1096. sector_div(cylinders, heads * sectors_pt);
  1097. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  1098. cylinders = 0xffff;
  1099. info[0] = heads;
  1100. info[1] = sectors_pt;
  1101. info[2] = (int)cylinders;
  1102. return 0;
  1103. }
  1104. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  1105. {
  1106. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  1107. struct hv_device *device = host_dev->dev;
  1108. struct storvsc_device *stor_device;
  1109. struct storvsc_cmd_request *request;
  1110. struct vstor_packet *vstor_packet;
  1111. int ret, t;
  1112. stor_device = get_out_stor_device(device);
  1113. if (!stor_device)
  1114. return FAILED;
  1115. request = &stor_device->reset_request;
  1116. vstor_packet = &request->vstor_packet;
  1117. init_completion(&request->wait_event);
  1118. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  1119. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  1120. vstor_packet->vm_srb.path_id = stor_device->path_id;
  1121. ret = vmbus_sendpacket(device->channel, vstor_packet,
  1122. (sizeof(struct vstor_packet) -
  1123. vmscsi_size_delta),
  1124. (unsigned long)&stor_device->reset_request,
  1125. VM_PKT_DATA_INBAND,
  1126. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1127. if (ret != 0)
  1128. return FAILED;
  1129. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  1130. if (t == 0)
  1131. return TIMEOUT_ERROR;
  1132. /*
  1133. * At this point, all outstanding requests in the adapter
  1134. * should have been flushed out and return to us
  1135. * There is a potential race here where the host may be in
  1136. * the process of responding when we return from here.
  1137. * Just wait for all in-transit packets to be accounted for
  1138. * before we return from here.
  1139. */
  1140. storvsc_wait_to_drain(stor_device);
  1141. return SUCCESS;
  1142. }
  1143. /*
  1144. * The host guarantees to respond to each command, although I/O latencies might
  1145. * be unbounded on Azure. Reset the timer unconditionally to give the host a
  1146. * chance to perform EH.
  1147. */
  1148. static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
  1149. {
  1150. return BLK_EH_RESET_TIMER;
  1151. }
  1152. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  1153. {
  1154. bool allowed = true;
  1155. u8 scsi_op = scmnd->cmnd[0];
  1156. switch (scsi_op) {
  1157. /* the host does not handle WRITE_SAME, log accident usage */
  1158. case WRITE_SAME:
  1159. /*
  1160. * smartd sends this command and the host does not handle
  1161. * this. So, don't send it.
  1162. */
  1163. case SET_WINDOW:
  1164. scmnd->result = ILLEGAL_REQUEST << 16;
  1165. allowed = false;
  1166. break;
  1167. default:
  1168. break;
  1169. }
  1170. return allowed;
  1171. }
  1172. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  1173. {
  1174. int ret;
  1175. struct hv_host_device *host_dev = shost_priv(host);
  1176. struct hv_device *dev = host_dev->dev;
  1177. struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
  1178. int i;
  1179. struct scatterlist *sgl;
  1180. unsigned int sg_count = 0;
  1181. struct vmscsi_request *vm_srb;
  1182. struct scatterlist *cur_sgl;
  1183. struct vmbus_packet_mpb_array *payload;
  1184. u32 payload_sz;
  1185. u32 length;
  1186. if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
  1187. /*
  1188. * On legacy hosts filter unimplemented commands.
  1189. * Future hosts are expected to correctly handle
  1190. * unsupported commands. Furthermore, it is
  1191. * possible that some of the currently
  1192. * unsupported commands maybe supported in
  1193. * future versions of the host.
  1194. */
  1195. if (!storvsc_scsi_cmd_ok(scmnd)) {
  1196. scmnd->scsi_done(scmnd);
  1197. return 0;
  1198. }
  1199. }
  1200. /* Setup the cmd request */
  1201. cmd_request->cmd = scmnd;
  1202. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1203. vm_srb->win8_extension.time_out_value = 60;
  1204. vm_srb->win8_extension.srb_flags |=
  1205. SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1206. if (scmnd->device->tagged_supported) {
  1207. vm_srb->win8_extension.srb_flags |=
  1208. (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
  1209. vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
  1210. vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
  1211. }
  1212. /* Build the SRB */
  1213. switch (scmnd->sc_data_direction) {
  1214. case DMA_TO_DEVICE:
  1215. vm_srb->data_in = WRITE_TYPE;
  1216. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
  1217. break;
  1218. case DMA_FROM_DEVICE:
  1219. vm_srb->data_in = READ_TYPE;
  1220. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
  1221. break;
  1222. case DMA_NONE:
  1223. vm_srb->data_in = UNKNOWN_TYPE;
  1224. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
  1225. break;
  1226. default:
  1227. /*
  1228. * This is DMA_BIDIRECTIONAL or something else we are never
  1229. * supposed to see here.
  1230. */
  1231. WARN(1, "Unexpected data direction: %d\n",
  1232. scmnd->sc_data_direction);
  1233. return -EINVAL;
  1234. }
  1235. vm_srb->port_number = host_dev->port;
  1236. vm_srb->path_id = scmnd->device->channel;
  1237. vm_srb->target_id = scmnd->device->id;
  1238. vm_srb->lun = scmnd->device->lun;
  1239. vm_srb->cdb_length = scmnd->cmd_len;
  1240. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1241. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1242. sg_count = scsi_sg_count(scmnd);
  1243. length = scsi_bufflen(scmnd);
  1244. payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
  1245. payload_sz = sizeof(cmd_request->mpb);
  1246. if (sg_count) {
  1247. if (sg_count > MAX_PAGE_BUFFER_COUNT) {
  1248. payload_sz = (sg_count * sizeof(void *) +
  1249. sizeof(struct vmbus_packet_mpb_array));
  1250. payload = kmalloc(payload_sz, GFP_ATOMIC);
  1251. if (!payload)
  1252. return SCSI_MLQUEUE_DEVICE_BUSY;
  1253. }
  1254. payload->range.len = length;
  1255. payload->range.offset = sgl[0].offset;
  1256. cur_sgl = sgl;
  1257. for (i = 0; i < sg_count; i++) {
  1258. payload->range.pfn_array[i] =
  1259. page_to_pfn(sg_page((cur_sgl)));
  1260. cur_sgl = sg_next(cur_sgl);
  1261. }
  1262. } else if (scsi_sglist(scmnd)) {
  1263. payload->range.len = length;
  1264. payload->range.offset =
  1265. virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
  1266. payload->range.pfn_array[0] =
  1267. virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
  1268. }
  1269. cmd_request->payload = payload;
  1270. cmd_request->payload_sz = payload_sz;
  1271. /* Invokes the vsc to start an IO */
  1272. ret = storvsc_do_io(dev, cmd_request);
  1273. if (ret == -EAGAIN) {
  1274. if (payload_sz > sizeof(cmd_request->mpb))
  1275. kfree(payload);
  1276. /* no more space */
  1277. return SCSI_MLQUEUE_DEVICE_BUSY;
  1278. }
  1279. return 0;
  1280. }
  1281. static struct scsi_host_template scsi_driver = {
  1282. .module = THIS_MODULE,
  1283. .name = "storvsc_host_t",
  1284. .cmd_size = sizeof(struct storvsc_cmd_request),
  1285. .bios_param = storvsc_get_chs,
  1286. .queuecommand = storvsc_queuecommand,
  1287. .eh_host_reset_handler = storvsc_host_reset_handler,
  1288. .proc_name = "storvsc_host",
  1289. .eh_timed_out = storvsc_eh_timed_out,
  1290. .slave_alloc = storvsc_device_alloc,
  1291. .slave_configure = storvsc_device_configure,
  1292. .cmd_per_lun = 255,
  1293. .this_id = -1,
  1294. .use_clustering = ENABLE_CLUSTERING,
  1295. /* Make sure we dont get a sg segment crosses a page boundary */
  1296. .dma_boundary = PAGE_SIZE-1,
  1297. .no_write_same = 1,
  1298. };
  1299. enum {
  1300. SCSI_GUID,
  1301. IDE_GUID,
  1302. SFC_GUID,
  1303. };
  1304. static const struct hv_vmbus_device_id id_table[] = {
  1305. /* SCSI guid */
  1306. { HV_SCSI_GUID,
  1307. .driver_data = SCSI_GUID
  1308. },
  1309. /* IDE guid */
  1310. { HV_IDE_GUID,
  1311. .driver_data = IDE_GUID
  1312. },
  1313. /* Fibre Channel GUID */
  1314. {
  1315. HV_SYNTHFC_GUID,
  1316. .driver_data = SFC_GUID
  1317. },
  1318. { },
  1319. };
  1320. MODULE_DEVICE_TABLE(vmbus, id_table);
  1321. static int storvsc_probe(struct hv_device *device,
  1322. const struct hv_vmbus_device_id *dev_id)
  1323. {
  1324. int ret;
  1325. int num_cpus = num_online_cpus();
  1326. struct Scsi_Host *host;
  1327. struct hv_host_device *host_dev;
  1328. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1329. bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
  1330. int target = 0;
  1331. struct storvsc_device *stor_device;
  1332. int max_luns_per_target;
  1333. int max_targets;
  1334. int max_channels;
  1335. int max_sub_channels = 0;
  1336. /*
  1337. * Based on the windows host we are running on,
  1338. * set state to properly communicate with the host.
  1339. */
  1340. if (vmbus_proto_version < VERSION_WIN8) {
  1341. max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1342. max_targets = STORVSC_IDE_MAX_TARGETS;
  1343. max_channels = STORVSC_IDE_MAX_CHANNELS;
  1344. } else {
  1345. max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
  1346. max_targets = STORVSC_MAX_TARGETS;
  1347. max_channels = STORVSC_MAX_CHANNELS;
  1348. /*
  1349. * On Windows8 and above, we support sub-channels for storage.
  1350. * The number of sub-channels offerred is based on the number of
  1351. * VCPUs in the guest.
  1352. */
  1353. max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
  1354. }
  1355. scsi_driver.can_queue = (max_outstanding_req_per_channel *
  1356. (max_sub_channels + 1));
  1357. host = scsi_host_alloc(&scsi_driver,
  1358. sizeof(struct hv_host_device));
  1359. if (!host)
  1360. return -ENOMEM;
  1361. host_dev = shost_priv(host);
  1362. memset(host_dev, 0, sizeof(struct hv_host_device));
  1363. host_dev->port = host->host_no;
  1364. host_dev->dev = device;
  1365. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1366. if (!stor_device) {
  1367. ret = -ENOMEM;
  1368. goto err_out0;
  1369. }
  1370. stor_device->destroy = false;
  1371. stor_device->open_sub_channel = false;
  1372. init_waitqueue_head(&stor_device->waiting_to_drain);
  1373. stor_device->device = device;
  1374. stor_device->host = host;
  1375. hv_set_drvdata(device, stor_device);
  1376. stor_device->port_number = host->host_no;
  1377. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
  1378. if (ret)
  1379. goto err_out1;
  1380. host_dev->path = stor_device->path_id;
  1381. host_dev->target = stor_device->target_id;
  1382. switch (dev_id->driver_data) {
  1383. case SFC_GUID:
  1384. host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
  1385. host->max_id = STORVSC_FC_MAX_TARGETS;
  1386. host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
  1387. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1388. host->transportt = fc_transport_template;
  1389. #endif
  1390. break;
  1391. case SCSI_GUID:
  1392. host->max_lun = max_luns_per_target;
  1393. host->max_id = max_targets;
  1394. host->max_channel = max_channels - 1;
  1395. break;
  1396. default:
  1397. host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1398. host->max_id = STORVSC_IDE_MAX_TARGETS;
  1399. host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
  1400. break;
  1401. }
  1402. /* max cmd length */
  1403. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1404. /*
  1405. * set the table size based on the info we got
  1406. * from the host.
  1407. */
  1408. host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
  1409. /* Register the HBA and start the scsi bus scan */
  1410. ret = scsi_add_host(host, &device->device);
  1411. if (ret != 0)
  1412. goto err_out2;
  1413. if (!dev_is_ide) {
  1414. scsi_scan_host(host);
  1415. } else {
  1416. target = (device->dev_instance.b[5] << 8 |
  1417. device->dev_instance.b[4]);
  1418. ret = scsi_add_device(host, 0, target, 0);
  1419. if (ret) {
  1420. scsi_remove_host(host);
  1421. goto err_out2;
  1422. }
  1423. }
  1424. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1425. if (host->transportt == fc_transport_template) {
  1426. fc_host_node_name(host) = stor_device->node_name;
  1427. fc_host_port_name(host) = stor_device->port_name;
  1428. }
  1429. #endif
  1430. return 0;
  1431. err_out2:
  1432. /*
  1433. * Once we have connected with the host, we would need to
  1434. * to invoke storvsc_dev_remove() to rollback this state and
  1435. * this call also frees up the stor_device; hence the jump around
  1436. * err_out1 label.
  1437. */
  1438. storvsc_dev_remove(device);
  1439. goto err_out0;
  1440. err_out1:
  1441. kfree(stor_device);
  1442. err_out0:
  1443. scsi_host_put(host);
  1444. return ret;
  1445. }
  1446. static int storvsc_remove(struct hv_device *dev)
  1447. {
  1448. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1449. struct Scsi_Host *host = stor_device->host;
  1450. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1451. if (host->transportt == fc_transport_template)
  1452. fc_remove_host(host);
  1453. #endif
  1454. scsi_remove_host(host);
  1455. storvsc_dev_remove(dev);
  1456. scsi_host_put(host);
  1457. return 0;
  1458. }
  1459. static struct hv_driver storvsc_drv = {
  1460. .name = KBUILD_MODNAME,
  1461. .id_table = id_table,
  1462. .probe = storvsc_probe,
  1463. .remove = storvsc_remove,
  1464. };
  1465. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1466. static struct fc_function_template fc_transport_functions = {
  1467. .show_host_node_name = 1,
  1468. .show_host_port_name = 1,
  1469. };
  1470. #endif
  1471. static int __init storvsc_drv_init(void)
  1472. {
  1473. int ret;
  1474. /*
  1475. * Divide the ring buffer data size (which is 1 page less
  1476. * than the ring buffer size since that page is reserved for
  1477. * the ring buffer indices) by the max request size (which is
  1478. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1479. */
  1480. max_outstanding_req_per_channel =
  1481. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1482. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1483. sizeof(struct vstor_packet) + sizeof(u64) -
  1484. vmscsi_size_delta,
  1485. sizeof(u64)));
  1486. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1487. fc_transport_template = fc_attach_transport(&fc_transport_functions);
  1488. if (!fc_transport_template)
  1489. return -ENODEV;
  1490. /*
  1491. * Install Hyper-V specific timeout handler.
  1492. */
  1493. fc_transport_template->eh_timed_out = storvsc_eh_timed_out;
  1494. #endif
  1495. ret = vmbus_driver_register(&storvsc_drv);
  1496. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1497. if (ret)
  1498. fc_release_transport(fc_transport_template);
  1499. #endif
  1500. return ret;
  1501. }
  1502. static void __exit storvsc_drv_exit(void)
  1503. {
  1504. vmbus_driver_unregister(&storvsc_drv);
  1505. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1506. fc_release_transport(fc_transport_template);
  1507. #endif
  1508. }
  1509. MODULE_LICENSE("GPL");
  1510. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1511. module_init(storvsc_drv_init);
  1512. module_exit(storvsc_drv_exit);