storvsc_drv.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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/mempool.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. /*
  258. * We setup a mempool to allocate request structures for this driver
  259. * on a per-lun basis. The following define specifies the number of
  260. * elements in the pool.
  261. */
  262. #define STORVSC_MIN_BUF_NR 64
  263. static int storvsc_ringbuffer_size = (20 * PAGE_SIZE);
  264. module_param(storvsc_ringbuffer_size, int, S_IRUGO);
  265. MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
  266. /*
  267. * Timeout in seconds for all devices managed by this driver.
  268. */
  269. static int storvsc_timeout = 180;
  270. #define STORVSC_MAX_IO_REQUESTS 200
  271. static void storvsc_on_channel_callback(void *context);
  272. /*
  273. * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
  274. * reality, the path/target is not used (ie always set to 0) so our
  275. * scsi host adapter essentially has 1 bus with 1 target that contains
  276. * up to 256 luns.
  277. */
  278. #define STORVSC_MAX_LUNS_PER_TARGET 64
  279. #define STORVSC_MAX_TARGETS 1
  280. #define STORVSC_MAX_CHANNELS 1
  281. struct storvsc_cmd_request {
  282. struct list_head entry;
  283. struct scsi_cmnd *cmd;
  284. unsigned int bounce_sgl_count;
  285. struct scatterlist *bounce_sgl;
  286. struct hv_device *device;
  287. /* Synchronize the request/response if needed */
  288. struct completion wait_event;
  289. unsigned char *sense_buffer;
  290. struct hv_multipage_buffer data_buffer;
  291. struct vstor_packet vstor_packet;
  292. };
  293. /* A storvsc device is a device object that contains a vmbus channel */
  294. struct storvsc_device {
  295. struct hv_device *device;
  296. bool destroy;
  297. bool drain_notify;
  298. bool open_sub_channel;
  299. atomic_t num_outstanding_req;
  300. struct Scsi_Host *host;
  301. wait_queue_head_t waiting_to_drain;
  302. /*
  303. * Each unique Port/Path/Target represents 1 channel ie scsi
  304. * controller. In reality, the pathid, targetid is always 0
  305. * and the port is set by us
  306. */
  307. unsigned int port_number;
  308. unsigned char path_id;
  309. unsigned char target_id;
  310. /* Used for vsc/vsp channel reset process */
  311. struct storvsc_cmd_request init_request;
  312. struct storvsc_cmd_request reset_request;
  313. };
  314. struct stor_mem_pools {
  315. struct kmem_cache *request_pool;
  316. mempool_t *request_mempool;
  317. };
  318. struct hv_host_device {
  319. struct hv_device *dev;
  320. unsigned int port;
  321. unsigned char path;
  322. unsigned char target;
  323. };
  324. struct storvsc_scan_work {
  325. struct work_struct work;
  326. struct Scsi_Host *host;
  327. uint lun;
  328. };
  329. static void storvsc_device_scan(struct work_struct *work)
  330. {
  331. struct storvsc_scan_work *wrk;
  332. uint lun;
  333. struct scsi_device *sdev;
  334. wrk = container_of(work, struct storvsc_scan_work, work);
  335. lun = wrk->lun;
  336. sdev = scsi_device_lookup(wrk->host, 0, 0, lun);
  337. if (!sdev)
  338. goto done;
  339. scsi_rescan_device(&sdev->sdev_gendev);
  340. scsi_device_put(sdev);
  341. done:
  342. kfree(wrk);
  343. }
  344. static void storvsc_bus_scan(struct work_struct *work)
  345. {
  346. struct storvsc_scan_work *wrk;
  347. int id, order_id;
  348. wrk = container_of(work, struct storvsc_scan_work, work);
  349. for (id = 0; id < wrk->host->max_id; ++id) {
  350. if (wrk->host->reverse_ordering)
  351. order_id = wrk->host->max_id - id - 1;
  352. else
  353. order_id = id;
  354. scsi_scan_target(&wrk->host->shost_gendev, 0,
  355. order_id, SCAN_WILD_CARD, 1);
  356. }
  357. kfree(wrk);
  358. }
  359. static void storvsc_remove_lun(struct work_struct *work)
  360. {
  361. struct storvsc_scan_work *wrk;
  362. struct scsi_device *sdev;
  363. wrk = container_of(work, struct storvsc_scan_work, work);
  364. if (!scsi_host_get(wrk->host))
  365. goto done;
  366. sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
  367. if (sdev) {
  368. scsi_remove_device(sdev);
  369. scsi_device_put(sdev);
  370. }
  371. scsi_host_put(wrk->host);
  372. done:
  373. kfree(wrk);
  374. }
  375. /*
  376. * Major/minor macros. Minor version is in LSB, meaning that earlier flat
  377. * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
  378. */
  379. static inline u16 storvsc_get_version(u8 major, u8 minor)
  380. {
  381. u16 version;
  382. version = ((major << 8) | minor);
  383. return version;
  384. }
  385. /*
  386. * We can get incoming messages from the host that are not in response to
  387. * messages that we have sent out. An example of this would be messages
  388. * received by the guest to notify dynamic addition/removal of LUNs. To
  389. * deal with potential race conditions where the driver may be in the
  390. * midst of being unloaded when we might receive an unsolicited message
  391. * from the host, we have implemented a mechanism to gurantee sequential
  392. * consistency:
  393. *
  394. * 1) Once the device is marked as being destroyed, we will fail all
  395. * outgoing messages.
  396. * 2) We permit incoming messages when the device is being destroyed,
  397. * only to properly account for messages already sent out.
  398. */
  399. static inline struct storvsc_device *get_out_stor_device(
  400. struct hv_device *device)
  401. {
  402. struct storvsc_device *stor_device;
  403. stor_device = hv_get_drvdata(device);
  404. if (stor_device && stor_device->destroy)
  405. stor_device = NULL;
  406. return stor_device;
  407. }
  408. static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
  409. {
  410. dev->drain_notify = true;
  411. wait_event(dev->waiting_to_drain,
  412. atomic_read(&dev->num_outstanding_req) == 0);
  413. dev->drain_notify = false;
  414. }
  415. static inline struct storvsc_device *get_in_stor_device(
  416. struct hv_device *device)
  417. {
  418. struct storvsc_device *stor_device;
  419. stor_device = hv_get_drvdata(device);
  420. if (!stor_device)
  421. goto get_in_err;
  422. /*
  423. * If the device is being destroyed; allow incoming
  424. * traffic only to cleanup outstanding requests.
  425. */
  426. if (stor_device->destroy &&
  427. (atomic_read(&stor_device->num_outstanding_req) == 0))
  428. stor_device = NULL;
  429. get_in_err:
  430. return stor_device;
  431. }
  432. static void destroy_bounce_buffer(struct scatterlist *sgl,
  433. unsigned int sg_count)
  434. {
  435. int i;
  436. struct page *page_buf;
  437. for (i = 0; i < sg_count; i++) {
  438. page_buf = sg_page((&sgl[i]));
  439. if (page_buf != NULL)
  440. __free_page(page_buf);
  441. }
  442. kfree(sgl);
  443. }
  444. static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
  445. {
  446. int i;
  447. /* No need to check */
  448. if (sg_count < 2)
  449. return -1;
  450. /* We have at least 2 sg entries */
  451. for (i = 0; i < sg_count; i++) {
  452. if (i == 0) {
  453. /* make sure 1st one does not have hole */
  454. if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
  455. return i;
  456. } else if (i == sg_count - 1) {
  457. /* make sure last one does not have hole */
  458. if (sgl[i].offset != 0)
  459. return i;
  460. } else {
  461. /* make sure no hole in the middle */
  462. if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
  463. return i;
  464. }
  465. }
  466. return -1;
  467. }
  468. static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
  469. unsigned int sg_count,
  470. unsigned int len,
  471. int write)
  472. {
  473. int i;
  474. int num_pages;
  475. struct scatterlist *bounce_sgl;
  476. struct page *page_buf;
  477. unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
  478. num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
  479. bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
  480. if (!bounce_sgl)
  481. return NULL;
  482. sg_init_table(bounce_sgl, num_pages);
  483. for (i = 0; i < num_pages; i++) {
  484. page_buf = alloc_page(GFP_ATOMIC);
  485. if (!page_buf)
  486. goto cleanup;
  487. sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
  488. }
  489. return bounce_sgl;
  490. cleanup:
  491. destroy_bounce_buffer(bounce_sgl, num_pages);
  492. return NULL;
  493. }
  494. /* Disgusting wrapper functions */
  495. static inline unsigned long sg_kmap_atomic(struct scatterlist *sgl, int idx)
  496. {
  497. void *addr = kmap_atomic(sg_page(sgl + idx));
  498. return (unsigned long)addr;
  499. }
  500. static inline void sg_kunmap_atomic(unsigned long addr)
  501. {
  502. kunmap_atomic((void *)addr);
  503. }
  504. /* Assume the original sgl has enough room */
  505. static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
  506. struct scatterlist *bounce_sgl,
  507. unsigned int orig_sgl_count,
  508. unsigned int bounce_sgl_count)
  509. {
  510. int i;
  511. int j = 0;
  512. unsigned long src, dest;
  513. unsigned int srclen, destlen, copylen;
  514. unsigned int total_copied = 0;
  515. unsigned long bounce_addr = 0;
  516. unsigned long dest_addr = 0;
  517. unsigned long flags;
  518. local_irq_save(flags);
  519. for (i = 0; i < orig_sgl_count; i++) {
  520. dest_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
  521. dest = dest_addr;
  522. destlen = orig_sgl[i].length;
  523. if (bounce_addr == 0)
  524. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  525. while (destlen) {
  526. src = bounce_addr + bounce_sgl[j].offset;
  527. srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
  528. copylen = min(srclen, destlen);
  529. memcpy((void *)dest, (void *)src, copylen);
  530. total_copied += copylen;
  531. bounce_sgl[j].offset += copylen;
  532. destlen -= copylen;
  533. dest += copylen;
  534. if (bounce_sgl[j].offset == bounce_sgl[j].length) {
  535. /* full */
  536. sg_kunmap_atomic(bounce_addr);
  537. j++;
  538. /*
  539. * It is possible that the number of elements
  540. * in the bounce buffer may not be equal to
  541. * the number of elements in the original
  542. * scatter list. Handle this correctly.
  543. */
  544. if (j == bounce_sgl_count) {
  545. /*
  546. * We are done; cleanup and return.
  547. */
  548. sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
  549. local_irq_restore(flags);
  550. return total_copied;
  551. }
  552. /* if we need to use another bounce buffer */
  553. if (destlen || i != orig_sgl_count - 1)
  554. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  555. } else if (destlen == 0 && i == orig_sgl_count - 1) {
  556. /* unmap the last bounce that is < PAGE_SIZE */
  557. sg_kunmap_atomic(bounce_addr);
  558. }
  559. }
  560. sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
  561. }
  562. local_irq_restore(flags);
  563. return total_copied;
  564. }
  565. /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
  566. static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
  567. struct scatterlist *bounce_sgl,
  568. unsigned int orig_sgl_count)
  569. {
  570. int i;
  571. int j = 0;
  572. unsigned long src, dest;
  573. unsigned int srclen, destlen, copylen;
  574. unsigned int total_copied = 0;
  575. unsigned long bounce_addr = 0;
  576. unsigned long src_addr = 0;
  577. unsigned long flags;
  578. local_irq_save(flags);
  579. for (i = 0; i < orig_sgl_count; i++) {
  580. src_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
  581. src = src_addr;
  582. srclen = orig_sgl[i].length;
  583. if (bounce_addr == 0)
  584. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  585. while (srclen) {
  586. /* assume bounce offset always == 0 */
  587. dest = bounce_addr + bounce_sgl[j].length;
  588. destlen = PAGE_SIZE - bounce_sgl[j].length;
  589. copylen = min(srclen, destlen);
  590. memcpy((void *)dest, (void *)src, copylen);
  591. total_copied += copylen;
  592. bounce_sgl[j].length += copylen;
  593. srclen -= copylen;
  594. src += copylen;
  595. if (bounce_sgl[j].length == PAGE_SIZE) {
  596. /* full..move to next entry */
  597. sg_kunmap_atomic(bounce_addr);
  598. j++;
  599. /* if we need to use another bounce buffer */
  600. if (srclen || i != orig_sgl_count - 1)
  601. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  602. } else if (srclen == 0 && i == orig_sgl_count - 1) {
  603. /* unmap the last bounce that is < PAGE_SIZE */
  604. sg_kunmap_atomic(bounce_addr);
  605. }
  606. }
  607. sg_kunmap_atomic(src_addr - orig_sgl[i].offset);
  608. }
  609. local_irq_restore(flags);
  610. return total_copied;
  611. }
  612. static void handle_sc_creation(struct vmbus_channel *new_sc)
  613. {
  614. struct hv_device *device = new_sc->primary_channel->device_obj;
  615. struct storvsc_device *stor_device;
  616. struct vmstorage_channel_properties props;
  617. stor_device = get_out_stor_device(device);
  618. if (!stor_device)
  619. return;
  620. if (stor_device->open_sub_channel == false)
  621. return;
  622. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  623. vmbus_open(new_sc,
  624. storvsc_ringbuffer_size,
  625. storvsc_ringbuffer_size,
  626. (void *)&props,
  627. sizeof(struct vmstorage_channel_properties),
  628. storvsc_on_channel_callback, new_sc);
  629. }
  630. static void handle_multichannel_storage(struct hv_device *device, int max_chns)
  631. {
  632. struct storvsc_device *stor_device;
  633. int num_cpus = num_online_cpus();
  634. int num_sc;
  635. struct storvsc_cmd_request *request;
  636. struct vstor_packet *vstor_packet;
  637. int ret, t;
  638. num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
  639. stor_device = get_out_stor_device(device);
  640. if (!stor_device)
  641. return;
  642. request = &stor_device->init_request;
  643. vstor_packet = &request->vstor_packet;
  644. stor_device->open_sub_channel = true;
  645. /*
  646. * Establish a handler for dealing with subchannels.
  647. */
  648. vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
  649. /*
  650. * Check to see if sub-channels have already been created. This
  651. * can happen when this driver is re-loaded after unloading.
  652. */
  653. if (vmbus_are_subchannels_present(device->channel))
  654. return;
  655. stor_device->open_sub_channel = false;
  656. /*
  657. * Request the host to create sub-channels.
  658. */
  659. memset(request, 0, sizeof(struct storvsc_cmd_request));
  660. init_completion(&request->wait_event);
  661. vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
  662. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  663. vstor_packet->sub_channel_count = num_sc;
  664. ret = vmbus_sendpacket(device->channel, vstor_packet,
  665. (sizeof(struct vstor_packet) -
  666. vmscsi_size_delta),
  667. (unsigned long)request,
  668. VM_PKT_DATA_INBAND,
  669. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  670. if (ret != 0)
  671. return;
  672. t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
  673. if (t == 0)
  674. return;
  675. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  676. vstor_packet->status != 0)
  677. return;
  678. /*
  679. * Now that we created the sub-channels, invoke the check; this
  680. * may trigger the callback.
  681. */
  682. stor_device->open_sub_channel = true;
  683. vmbus_are_subchannels_present(device->channel);
  684. }
  685. static int storvsc_channel_init(struct hv_device *device)
  686. {
  687. struct storvsc_device *stor_device;
  688. struct storvsc_cmd_request *request;
  689. struct vstor_packet *vstor_packet;
  690. int ret, t;
  691. int max_chns;
  692. bool process_sub_channels = false;
  693. stor_device = get_out_stor_device(device);
  694. if (!stor_device)
  695. return -ENODEV;
  696. request = &stor_device->init_request;
  697. vstor_packet = &request->vstor_packet;
  698. /*
  699. * Now, initiate the vsc/vsp initialization protocol on the open
  700. * channel
  701. */
  702. memset(request, 0, sizeof(struct storvsc_cmd_request));
  703. init_completion(&request->wait_event);
  704. vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
  705. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  706. ret = vmbus_sendpacket(device->channel, vstor_packet,
  707. (sizeof(struct vstor_packet) -
  708. vmscsi_size_delta),
  709. (unsigned long)request,
  710. VM_PKT_DATA_INBAND,
  711. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  712. if (ret != 0)
  713. goto cleanup;
  714. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  715. if (t == 0) {
  716. ret = -ETIMEDOUT;
  717. goto cleanup;
  718. }
  719. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  720. vstor_packet->status != 0)
  721. goto cleanup;
  722. /* reuse the packet for version range supported */
  723. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  724. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
  725. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  726. vstor_packet->version.major_minor =
  727. storvsc_get_version(vmstor_current_major, vmstor_current_minor);
  728. /*
  729. * The revision number is only used in Windows; set it to 0.
  730. */
  731. vstor_packet->version.revision = 0;
  732. ret = vmbus_sendpacket(device->channel, vstor_packet,
  733. (sizeof(struct vstor_packet) -
  734. vmscsi_size_delta),
  735. (unsigned long)request,
  736. VM_PKT_DATA_INBAND,
  737. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  738. if (ret != 0)
  739. goto cleanup;
  740. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  741. if (t == 0) {
  742. ret = -ETIMEDOUT;
  743. goto cleanup;
  744. }
  745. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  746. vstor_packet->status != 0)
  747. goto cleanup;
  748. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  749. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
  750. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  751. ret = vmbus_sendpacket(device->channel, vstor_packet,
  752. (sizeof(struct vstor_packet) -
  753. vmscsi_size_delta),
  754. (unsigned long)request,
  755. VM_PKT_DATA_INBAND,
  756. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  757. if (ret != 0)
  758. goto cleanup;
  759. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  760. if (t == 0) {
  761. ret = -ETIMEDOUT;
  762. goto cleanup;
  763. }
  764. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  765. vstor_packet->status != 0)
  766. goto cleanup;
  767. /*
  768. * Check to see if multi-channel support is there.
  769. * Hosts that implement protocol version of 5.1 and above
  770. * support multi-channel.
  771. */
  772. max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
  773. if ((vmbus_proto_version != VERSION_WIN7) &&
  774. (vmbus_proto_version != VERSION_WS2008)) {
  775. if (vstor_packet->storage_channel_properties.flags &
  776. STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
  777. process_sub_channels = true;
  778. }
  779. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  780. vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
  781. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  782. ret = vmbus_sendpacket(device->channel, vstor_packet,
  783. (sizeof(struct vstor_packet) -
  784. vmscsi_size_delta),
  785. (unsigned long)request,
  786. VM_PKT_DATA_INBAND,
  787. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  788. if (ret != 0)
  789. goto cleanup;
  790. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  791. if (t == 0) {
  792. ret = -ETIMEDOUT;
  793. goto cleanup;
  794. }
  795. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  796. vstor_packet->status != 0)
  797. goto cleanup;
  798. if (process_sub_channels)
  799. handle_multichannel_storage(device, max_chns);
  800. cleanup:
  801. return ret;
  802. }
  803. static void storvsc_handle_error(struct vmscsi_request *vm_srb,
  804. struct scsi_cmnd *scmnd,
  805. struct Scsi_Host *host,
  806. u8 asc, u8 ascq)
  807. {
  808. struct storvsc_scan_work *wrk;
  809. void (*process_err_fn)(struct work_struct *work);
  810. bool do_work = false;
  811. switch (vm_srb->srb_status) {
  812. case SRB_STATUS_ERROR:
  813. /*
  814. * If there is an error; offline the device since all
  815. * error recovery strategies would have already been
  816. * deployed on the host side. However, if the command
  817. * were a pass-through command deal with it appropriately.
  818. */
  819. switch (scmnd->cmnd[0]) {
  820. case ATA_16:
  821. case ATA_12:
  822. set_host_byte(scmnd, DID_PASSTHROUGH);
  823. break;
  824. default:
  825. set_host_byte(scmnd, DID_TARGET_FAILURE);
  826. }
  827. break;
  828. case SRB_STATUS_INVALID_LUN:
  829. do_work = true;
  830. process_err_fn = storvsc_remove_lun;
  831. break;
  832. case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID):
  833. if ((asc == 0x2a) && (ascq == 0x9)) {
  834. do_work = true;
  835. process_err_fn = storvsc_device_scan;
  836. /*
  837. * Retry the I/O that trigerred this.
  838. */
  839. set_host_byte(scmnd, DID_REQUEUE);
  840. }
  841. break;
  842. }
  843. if (!do_work)
  844. return;
  845. /*
  846. * We need to schedule work to process this error; schedule it.
  847. */
  848. wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  849. if (!wrk) {
  850. set_host_byte(scmnd, DID_TARGET_FAILURE);
  851. return;
  852. }
  853. wrk->host = host;
  854. wrk->lun = vm_srb->lun;
  855. INIT_WORK(&wrk->work, process_err_fn);
  856. schedule_work(&wrk->work);
  857. }
  858. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
  859. {
  860. struct scsi_cmnd *scmnd = cmd_request->cmd;
  861. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  862. void (*scsi_done_fn)(struct scsi_cmnd *);
  863. struct scsi_sense_hdr sense_hdr;
  864. struct vmscsi_request *vm_srb;
  865. struct stor_mem_pools *memp = scmnd->device->hostdata;
  866. struct Scsi_Host *host;
  867. struct storvsc_device *stor_dev;
  868. struct hv_device *dev = host_dev->dev;
  869. stor_dev = get_in_stor_device(dev);
  870. host = stor_dev->host;
  871. vm_srb = &cmd_request->vstor_packet.vm_srb;
  872. if (cmd_request->bounce_sgl_count) {
  873. if (vm_srb->data_in == READ_TYPE)
  874. copy_from_bounce_buffer(scsi_sglist(scmnd),
  875. cmd_request->bounce_sgl,
  876. scsi_sg_count(scmnd),
  877. cmd_request->bounce_sgl_count);
  878. destroy_bounce_buffer(cmd_request->bounce_sgl,
  879. cmd_request->bounce_sgl_count);
  880. }
  881. scmnd->result = vm_srb->scsi_status;
  882. if (scmnd->result) {
  883. if (scsi_normalize_sense(scmnd->sense_buffer,
  884. SCSI_SENSE_BUFFERSIZE, &sense_hdr))
  885. scsi_print_sense_hdr("storvsc", &sense_hdr);
  886. }
  887. if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
  888. storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
  889. sense_hdr.ascq);
  890. scsi_set_resid(scmnd,
  891. cmd_request->data_buffer.len -
  892. vm_srb->data_transfer_length);
  893. scsi_done_fn = scmnd->scsi_done;
  894. scmnd->host_scribble = NULL;
  895. scmnd->scsi_done = NULL;
  896. scsi_done_fn(scmnd);
  897. mempool_free(cmd_request, memp->request_mempool);
  898. }
  899. static void storvsc_on_io_completion(struct hv_device *device,
  900. struct vstor_packet *vstor_packet,
  901. struct storvsc_cmd_request *request)
  902. {
  903. struct storvsc_device *stor_device;
  904. struct vstor_packet *stor_pkt;
  905. stor_device = hv_get_drvdata(device);
  906. stor_pkt = &request->vstor_packet;
  907. /*
  908. * The current SCSI handling on the host side does
  909. * not correctly handle:
  910. * INQUIRY command with page code parameter set to 0x80
  911. * MODE_SENSE command with cmd[2] == 0x1c
  912. *
  913. * Setup srb and scsi status so this won't be fatal.
  914. * We do this so we can distinguish truly fatal failues
  915. * (srb status == 0x4) and off-line the device in that case.
  916. */
  917. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  918. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  919. vstor_packet->vm_srb.scsi_status = 0;
  920. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  921. }
  922. /* Copy over the status...etc */
  923. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  924. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  925. stor_pkt->vm_srb.sense_info_length =
  926. vstor_packet->vm_srb.sense_info_length;
  927. if (vstor_packet->vm_srb.scsi_status != 0 ||
  928. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
  929. dev_warn(&device->device,
  930. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  931. stor_pkt->vm_srb.cdb[0],
  932. vstor_packet->vm_srb.scsi_status,
  933. vstor_packet->vm_srb.srb_status);
  934. }
  935. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  936. /* CHECK_CONDITION */
  937. if (vstor_packet->vm_srb.srb_status &
  938. SRB_STATUS_AUTOSENSE_VALID) {
  939. /* autosense data available */
  940. dev_warn(&device->device,
  941. "stor pkt %p autosense data valid - len %d\n",
  942. request,
  943. vstor_packet->vm_srb.sense_info_length);
  944. memcpy(request->sense_buffer,
  945. vstor_packet->vm_srb.sense_data,
  946. vstor_packet->vm_srb.sense_info_length);
  947. }
  948. }
  949. stor_pkt->vm_srb.data_transfer_length =
  950. vstor_packet->vm_srb.data_transfer_length;
  951. storvsc_command_completion(request);
  952. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  953. stor_device->drain_notify)
  954. wake_up(&stor_device->waiting_to_drain);
  955. }
  956. static void storvsc_on_receive(struct hv_device *device,
  957. struct vstor_packet *vstor_packet,
  958. struct storvsc_cmd_request *request)
  959. {
  960. struct storvsc_scan_work *work;
  961. struct storvsc_device *stor_device;
  962. switch (vstor_packet->operation) {
  963. case VSTOR_OPERATION_COMPLETE_IO:
  964. storvsc_on_io_completion(device, vstor_packet, request);
  965. break;
  966. case VSTOR_OPERATION_REMOVE_DEVICE:
  967. case VSTOR_OPERATION_ENUMERATE_BUS:
  968. stor_device = get_in_stor_device(device);
  969. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  970. if (!work)
  971. return;
  972. INIT_WORK(&work->work, storvsc_bus_scan);
  973. work->host = stor_device->host;
  974. schedule_work(&work->work);
  975. break;
  976. default:
  977. break;
  978. }
  979. }
  980. static void storvsc_on_channel_callback(void *context)
  981. {
  982. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  983. struct hv_device *device;
  984. struct storvsc_device *stor_device;
  985. u32 bytes_recvd;
  986. u64 request_id;
  987. unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
  988. struct storvsc_cmd_request *request;
  989. int ret;
  990. if (channel->primary_channel != NULL)
  991. device = channel->primary_channel->device_obj;
  992. else
  993. device = channel->device_obj;
  994. stor_device = get_in_stor_device(device);
  995. if (!stor_device)
  996. return;
  997. do {
  998. ret = vmbus_recvpacket(channel, packet,
  999. ALIGN((sizeof(struct vstor_packet) -
  1000. vmscsi_size_delta), 8),
  1001. &bytes_recvd, &request_id);
  1002. if (ret == 0 && bytes_recvd > 0) {
  1003. request = (struct storvsc_cmd_request *)
  1004. (unsigned long)request_id;
  1005. if ((request == &stor_device->init_request) ||
  1006. (request == &stor_device->reset_request)) {
  1007. memcpy(&request->vstor_packet, packet,
  1008. (sizeof(struct vstor_packet) -
  1009. vmscsi_size_delta));
  1010. complete(&request->wait_event);
  1011. } else {
  1012. storvsc_on_receive(device,
  1013. (struct vstor_packet *)packet,
  1014. request);
  1015. }
  1016. } else {
  1017. break;
  1018. }
  1019. } while (1);
  1020. return;
  1021. }
  1022. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
  1023. {
  1024. struct vmstorage_channel_properties props;
  1025. int ret;
  1026. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  1027. ret = vmbus_open(device->channel,
  1028. ring_size,
  1029. ring_size,
  1030. (void *)&props,
  1031. sizeof(struct vmstorage_channel_properties),
  1032. storvsc_on_channel_callback, device->channel);
  1033. if (ret != 0)
  1034. return ret;
  1035. ret = storvsc_channel_init(device);
  1036. return ret;
  1037. }
  1038. static int storvsc_dev_remove(struct hv_device *device)
  1039. {
  1040. struct storvsc_device *stor_device;
  1041. unsigned long flags;
  1042. stor_device = hv_get_drvdata(device);
  1043. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  1044. stor_device->destroy = true;
  1045. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  1046. /*
  1047. * At this point, all outbound traffic should be disable. We
  1048. * only allow inbound traffic (responses) to proceed so that
  1049. * outstanding requests can be completed.
  1050. */
  1051. storvsc_wait_to_drain(stor_device);
  1052. /*
  1053. * Since we have already drained, we don't need to busy wait
  1054. * as was done in final_release_stor_device()
  1055. * Note that we cannot set the ext pointer to NULL until
  1056. * we have drained - to drain the outgoing packets, we need to
  1057. * allow incoming packets.
  1058. */
  1059. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  1060. hv_set_drvdata(device, NULL);
  1061. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  1062. /* Close the channel */
  1063. vmbus_close(device->channel);
  1064. kfree(stor_device);
  1065. return 0;
  1066. }
  1067. static int storvsc_do_io(struct hv_device *device,
  1068. struct storvsc_cmd_request *request)
  1069. {
  1070. struct storvsc_device *stor_device;
  1071. struct vstor_packet *vstor_packet;
  1072. struct vmbus_channel *outgoing_channel;
  1073. int ret = 0;
  1074. vstor_packet = &request->vstor_packet;
  1075. stor_device = get_out_stor_device(device);
  1076. if (!stor_device)
  1077. return -ENODEV;
  1078. request->device = device;
  1079. /*
  1080. * Select an an appropriate channel to send the request out.
  1081. */
  1082. outgoing_channel = vmbus_get_outgoing_channel(device->channel);
  1083. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  1084. vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
  1085. vmscsi_size_delta);
  1086. vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
  1087. vstor_packet->vm_srb.data_transfer_length =
  1088. request->data_buffer.len;
  1089. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  1090. if (request->data_buffer.len) {
  1091. ret = vmbus_sendpacket_multipagebuffer(outgoing_channel,
  1092. &request->data_buffer,
  1093. vstor_packet,
  1094. (sizeof(struct vstor_packet) -
  1095. vmscsi_size_delta),
  1096. (unsigned long)request);
  1097. } else {
  1098. ret = vmbus_sendpacket(device->channel, vstor_packet,
  1099. (sizeof(struct vstor_packet) -
  1100. vmscsi_size_delta),
  1101. (unsigned long)request,
  1102. VM_PKT_DATA_INBAND,
  1103. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1104. }
  1105. if (ret != 0)
  1106. return ret;
  1107. atomic_inc(&stor_device->num_outstanding_req);
  1108. return ret;
  1109. }
  1110. static int storvsc_device_alloc(struct scsi_device *sdevice)
  1111. {
  1112. struct stor_mem_pools *memp;
  1113. int number = STORVSC_MIN_BUF_NR;
  1114. memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
  1115. if (!memp)
  1116. return -ENOMEM;
  1117. memp->request_pool =
  1118. kmem_cache_create(dev_name(&sdevice->sdev_dev),
  1119. sizeof(struct storvsc_cmd_request), 0,
  1120. SLAB_HWCACHE_ALIGN, NULL);
  1121. if (!memp->request_pool)
  1122. goto err0;
  1123. memp->request_mempool = mempool_create(number, mempool_alloc_slab,
  1124. mempool_free_slab,
  1125. memp->request_pool);
  1126. if (!memp->request_mempool)
  1127. goto err1;
  1128. sdevice->hostdata = memp;
  1129. return 0;
  1130. err1:
  1131. kmem_cache_destroy(memp->request_pool);
  1132. err0:
  1133. kfree(memp);
  1134. return -ENOMEM;
  1135. }
  1136. static void storvsc_device_destroy(struct scsi_device *sdevice)
  1137. {
  1138. struct stor_mem_pools *memp = sdevice->hostdata;
  1139. mempool_destroy(memp->request_mempool);
  1140. kmem_cache_destroy(memp->request_pool);
  1141. kfree(memp);
  1142. sdevice->hostdata = NULL;
  1143. }
  1144. static int storvsc_device_configure(struct scsi_device *sdevice)
  1145. {
  1146. scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
  1147. STORVSC_MAX_IO_REQUESTS);
  1148. blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
  1149. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  1150. blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
  1151. sdevice->no_write_same = 1;
  1152. return 0;
  1153. }
  1154. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  1155. sector_t capacity, int *info)
  1156. {
  1157. sector_t nsect = capacity;
  1158. sector_t cylinders = nsect;
  1159. int heads, sectors_pt;
  1160. /*
  1161. * We are making up these values; let us keep it simple.
  1162. */
  1163. heads = 0xff;
  1164. sectors_pt = 0x3f; /* Sectors per track */
  1165. sector_div(cylinders, heads * sectors_pt);
  1166. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  1167. cylinders = 0xffff;
  1168. info[0] = heads;
  1169. info[1] = sectors_pt;
  1170. info[2] = (int)cylinders;
  1171. return 0;
  1172. }
  1173. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  1174. {
  1175. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  1176. struct hv_device *device = host_dev->dev;
  1177. struct storvsc_device *stor_device;
  1178. struct storvsc_cmd_request *request;
  1179. struct vstor_packet *vstor_packet;
  1180. int ret, t;
  1181. stor_device = get_out_stor_device(device);
  1182. if (!stor_device)
  1183. return FAILED;
  1184. request = &stor_device->reset_request;
  1185. vstor_packet = &request->vstor_packet;
  1186. init_completion(&request->wait_event);
  1187. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  1188. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  1189. vstor_packet->vm_srb.path_id = stor_device->path_id;
  1190. ret = vmbus_sendpacket(device->channel, vstor_packet,
  1191. (sizeof(struct vstor_packet) -
  1192. vmscsi_size_delta),
  1193. (unsigned long)&stor_device->reset_request,
  1194. VM_PKT_DATA_INBAND,
  1195. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1196. if (ret != 0)
  1197. return FAILED;
  1198. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  1199. if (t == 0)
  1200. return TIMEOUT_ERROR;
  1201. /*
  1202. * At this point, all outstanding requests in the adapter
  1203. * should have been flushed out and return to us
  1204. * There is a potential race here where the host may be in
  1205. * the process of responding when we return from here.
  1206. * Just wait for all in-transit packets to be accounted for
  1207. * before we return from here.
  1208. */
  1209. storvsc_wait_to_drain(stor_device);
  1210. return SUCCESS;
  1211. }
  1212. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  1213. {
  1214. bool allowed = true;
  1215. u8 scsi_op = scmnd->cmnd[0];
  1216. switch (scsi_op) {
  1217. /* the host does not handle WRITE_SAME, log accident usage */
  1218. case WRITE_SAME:
  1219. /*
  1220. * smartd sends this command and the host does not handle
  1221. * this. So, don't send it.
  1222. */
  1223. case SET_WINDOW:
  1224. scmnd->result = ILLEGAL_REQUEST << 16;
  1225. allowed = false;
  1226. break;
  1227. default:
  1228. break;
  1229. }
  1230. return allowed;
  1231. }
  1232. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  1233. {
  1234. int ret;
  1235. struct hv_host_device *host_dev = shost_priv(host);
  1236. struct hv_device *dev = host_dev->dev;
  1237. struct storvsc_cmd_request *cmd_request;
  1238. unsigned int request_size = 0;
  1239. int i;
  1240. struct scatterlist *sgl;
  1241. unsigned int sg_count = 0;
  1242. struct vmscsi_request *vm_srb;
  1243. struct stor_mem_pools *memp = scmnd->device->hostdata;
  1244. if (!storvsc_scsi_cmd_ok(scmnd)) {
  1245. scmnd->scsi_done(scmnd);
  1246. return 0;
  1247. }
  1248. request_size = sizeof(struct storvsc_cmd_request);
  1249. cmd_request = mempool_alloc(memp->request_mempool,
  1250. GFP_ATOMIC);
  1251. /*
  1252. * We might be invoked in an interrupt context; hence
  1253. * mempool_alloc() can fail.
  1254. */
  1255. if (!cmd_request)
  1256. return SCSI_MLQUEUE_DEVICE_BUSY;
  1257. memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
  1258. /* Setup the cmd request */
  1259. cmd_request->cmd = scmnd;
  1260. scmnd->host_scribble = (unsigned char *)cmd_request;
  1261. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1262. vm_srb->win8_extension.time_out_value = 60;
  1263. /* Build the SRB */
  1264. switch (scmnd->sc_data_direction) {
  1265. case DMA_TO_DEVICE:
  1266. vm_srb->data_in = WRITE_TYPE;
  1267. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
  1268. vm_srb->win8_extension.srb_flags |=
  1269. (SRB_FLAGS_QUEUE_ACTION_ENABLE |
  1270. SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
  1271. break;
  1272. case DMA_FROM_DEVICE:
  1273. vm_srb->data_in = READ_TYPE;
  1274. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
  1275. vm_srb->win8_extension.srb_flags |=
  1276. (SRB_FLAGS_QUEUE_ACTION_ENABLE |
  1277. SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
  1278. break;
  1279. default:
  1280. vm_srb->data_in = UNKNOWN_TYPE;
  1281. vm_srb->win8_extension.srb_flags = 0;
  1282. break;
  1283. }
  1284. vm_srb->port_number = host_dev->port;
  1285. vm_srb->path_id = scmnd->device->channel;
  1286. vm_srb->target_id = scmnd->device->id;
  1287. vm_srb->lun = scmnd->device->lun;
  1288. vm_srb->cdb_length = scmnd->cmd_len;
  1289. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1290. cmd_request->sense_buffer = scmnd->sense_buffer;
  1291. cmd_request->data_buffer.len = scsi_bufflen(scmnd);
  1292. if (scsi_sg_count(scmnd)) {
  1293. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1294. sg_count = scsi_sg_count(scmnd);
  1295. /* check if we need to bounce the sgl */
  1296. if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
  1297. cmd_request->bounce_sgl =
  1298. create_bounce_buffer(sgl, scsi_sg_count(scmnd),
  1299. scsi_bufflen(scmnd),
  1300. vm_srb->data_in);
  1301. if (!cmd_request->bounce_sgl) {
  1302. ret = SCSI_MLQUEUE_HOST_BUSY;
  1303. goto queue_error;
  1304. }
  1305. cmd_request->bounce_sgl_count =
  1306. ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
  1307. PAGE_SHIFT;
  1308. if (vm_srb->data_in == WRITE_TYPE)
  1309. copy_to_bounce_buffer(sgl,
  1310. cmd_request->bounce_sgl,
  1311. scsi_sg_count(scmnd));
  1312. sgl = cmd_request->bounce_sgl;
  1313. sg_count = cmd_request->bounce_sgl_count;
  1314. }
  1315. cmd_request->data_buffer.offset = sgl[0].offset;
  1316. for (i = 0; i < sg_count; i++)
  1317. cmd_request->data_buffer.pfn_array[i] =
  1318. page_to_pfn(sg_page((&sgl[i])));
  1319. } else if (scsi_sglist(scmnd)) {
  1320. cmd_request->data_buffer.offset =
  1321. virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
  1322. cmd_request->data_buffer.pfn_array[0] =
  1323. virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
  1324. }
  1325. /* Invokes the vsc to start an IO */
  1326. ret = storvsc_do_io(dev, cmd_request);
  1327. if (ret == -EAGAIN) {
  1328. /* no more space */
  1329. if (cmd_request->bounce_sgl_count) {
  1330. destroy_bounce_buffer(cmd_request->bounce_sgl,
  1331. cmd_request->bounce_sgl_count);
  1332. ret = SCSI_MLQUEUE_DEVICE_BUSY;
  1333. goto queue_error;
  1334. }
  1335. }
  1336. return 0;
  1337. queue_error:
  1338. mempool_free(cmd_request, memp->request_mempool);
  1339. scmnd->host_scribble = NULL;
  1340. return ret;
  1341. }
  1342. static struct scsi_host_template scsi_driver = {
  1343. .module = THIS_MODULE,
  1344. .name = "storvsc_host_t",
  1345. .bios_param = storvsc_get_chs,
  1346. .queuecommand = storvsc_queuecommand,
  1347. .eh_host_reset_handler = storvsc_host_reset_handler,
  1348. .slave_alloc = storvsc_device_alloc,
  1349. .slave_destroy = storvsc_device_destroy,
  1350. .slave_configure = storvsc_device_configure,
  1351. .cmd_per_lun = 1,
  1352. /* 64 max_queue * 1 target */
  1353. .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
  1354. .this_id = -1,
  1355. /* no use setting to 0 since ll_blk_rw reset it to 1 */
  1356. /* currently 32 */
  1357. .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
  1358. .use_clustering = DISABLE_CLUSTERING,
  1359. /* Make sure we dont get a sg segment crosses a page boundary */
  1360. .dma_boundary = PAGE_SIZE-1,
  1361. .no_write_same = 1,
  1362. };
  1363. enum {
  1364. SCSI_GUID,
  1365. IDE_GUID,
  1366. SFC_GUID,
  1367. };
  1368. static const struct hv_vmbus_device_id id_table[] = {
  1369. /* SCSI guid */
  1370. { HV_SCSI_GUID,
  1371. .driver_data = SCSI_GUID
  1372. },
  1373. /* IDE guid */
  1374. { HV_IDE_GUID,
  1375. .driver_data = IDE_GUID
  1376. },
  1377. /* Fibre Channel GUID */
  1378. {
  1379. HV_SYNTHFC_GUID,
  1380. .driver_data = SFC_GUID
  1381. },
  1382. { },
  1383. };
  1384. MODULE_DEVICE_TABLE(vmbus, id_table);
  1385. static int storvsc_probe(struct hv_device *device,
  1386. const struct hv_vmbus_device_id *dev_id)
  1387. {
  1388. int ret;
  1389. struct Scsi_Host *host;
  1390. struct hv_host_device *host_dev;
  1391. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1392. int target = 0;
  1393. struct storvsc_device *stor_device;
  1394. /*
  1395. * Based on the windows host we are running on,
  1396. * set state to properly communicate with the host.
  1397. */
  1398. if (vmbus_proto_version == VERSION_WIN8) {
  1399. sense_buffer_size = POST_WIN7_STORVSC_SENSE_BUFFER_SIZE;
  1400. vmscsi_size_delta = 0;
  1401. vmstor_current_major = VMSTOR_WIN8_MAJOR;
  1402. vmstor_current_minor = VMSTOR_WIN8_MINOR;
  1403. } else {
  1404. sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
  1405. vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
  1406. vmstor_current_major = VMSTOR_WIN7_MAJOR;
  1407. vmstor_current_minor = VMSTOR_WIN7_MINOR;
  1408. }
  1409. host = scsi_host_alloc(&scsi_driver,
  1410. sizeof(struct hv_host_device));
  1411. if (!host)
  1412. return -ENOMEM;
  1413. host_dev = shost_priv(host);
  1414. memset(host_dev, 0, sizeof(struct hv_host_device));
  1415. host_dev->port = host->host_no;
  1416. host_dev->dev = device;
  1417. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1418. if (!stor_device) {
  1419. ret = -ENOMEM;
  1420. goto err_out0;
  1421. }
  1422. stor_device->destroy = false;
  1423. stor_device->open_sub_channel = false;
  1424. init_waitqueue_head(&stor_device->waiting_to_drain);
  1425. stor_device->device = device;
  1426. stor_device->host = host;
  1427. hv_set_drvdata(device, stor_device);
  1428. stor_device->port_number = host->host_no;
  1429. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
  1430. if (ret)
  1431. goto err_out1;
  1432. host_dev->path = stor_device->path_id;
  1433. host_dev->target = stor_device->target_id;
  1434. /* max # of devices per target */
  1435. host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
  1436. /* max # of targets per channel */
  1437. host->max_id = STORVSC_MAX_TARGETS;
  1438. /* max # of channels */
  1439. host->max_channel = STORVSC_MAX_CHANNELS - 1;
  1440. /* max cmd length */
  1441. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1442. /* Register the HBA and start the scsi bus scan */
  1443. ret = scsi_add_host(host, &device->device);
  1444. if (ret != 0)
  1445. goto err_out2;
  1446. if (!dev_is_ide) {
  1447. scsi_scan_host(host);
  1448. } else {
  1449. target = (device->dev_instance.b[5] << 8 |
  1450. device->dev_instance.b[4]);
  1451. ret = scsi_add_device(host, 0, target, 0);
  1452. if (ret) {
  1453. scsi_remove_host(host);
  1454. goto err_out2;
  1455. }
  1456. }
  1457. return 0;
  1458. err_out2:
  1459. /*
  1460. * Once we have connected with the host, we would need to
  1461. * to invoke storvsc_dev_remove() to rollback this state and
  1462. * this call also frees up the stor_device; hence the jump around
  1463. * err_out1 label.
  1464. */
  1465. storvsc_dev_remove(device);
  1466. goto err_out0;
  1467. err_out1:
  1468. kfree(stor_device);
  1469. err_out0:
  1470. scsi_host_put(host);
  1471. return ret;
  1472. }
  1473. static int storvsc_remove(struct hv_device *dev)
  1474. {
  1475. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1476. struct Scsi_Host *host = stor_device->host;
  1477. scsi_remove_host(host);
  1478. storvsc_dev_remove(dev);
  1479. scsi_host_put(host);
  1480. return 0;
  1481. }
  1482. static struct hv_driver storvsc_drv = {
  1483. .name = KBUILD_MODNAME,
  1484. .id_table = id_table,
  1485. .probe = storvsc_probe,
  1486. .remove = storvsc_remove,
  1487. };
  1488. static int __init storvsc_drv_init(void)
  1489. {
  1490. u32 max_outstanding_req_per_channel;
  1491. /*
  1492. * Divide the ring buffer data size (which is 1 page less
  1493. * than the ring buffer size since that page is reserved for
  1494. * the ring buffer indices) by the max request size (which is
  1495. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1496. */
  1497. max_outstanding_req_per_channel =
  1498. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1499. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1500. sizeof(struct vstor_packet) + sizeof(u64) -
  1501. vmscsi_size_delta,
  1502. sizeof(u64)));
  1503. if (max_outstanding_req_per_channel <
  1504. STORVSC_MAX_IO_REQUESTS)
  1505. return -EINVAL;
  1506. return vmbus_driver_register(&storvsc_drv);
  1507. }
  1508. static void __exit storvsc_drv_exit(void)
  1509. {
  1510. vmbus_driver_unregister(&storvsc_drv);
  1511. }
  1512. MODULE_LICENSE("GPL");
  1513. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1514. module_init(storvsc_drv_init);
  1515. module_exit(storvsc_drv_exit);