storvsc_drv.c 48 KB

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