storvsc_drv.c 44 KB

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