storvsc_drv.c 43 KB

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