storvsc_drv.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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. */
  56. #define VMSTOR_CURRENT_MAJOR 4
  57. #define VMSTOR_CURRENT_MINOR 2
  58. /* Packet structure describing virtual storage requests. */
  59. enum vstor_packet_operation {
  60. VSTOR_OPERATION_COMPLETE_IO = 1,
  61. VSTOR_OPERATION_REMOVE_DEVICE = 2,
  62. VSTOR_OPERATION_EXECUTE_SRB = 3,
  63. VSTOR_OPERATION_RESET_LUN = 4,
  64. VSTOR_OPERATION_RESET_ADAPTER = 5,
  65. VSTOR_OPERATION_RESET_BUS = 6,
  66. VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
  67. VSTOR_OPERATION_END_INITIALIZATION = 8,
  68. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
  69. VSTOR_OPERATION_QUERY_PROPERTIES = 10,
  70. VSTOR_OPERATION_ENUMERATE_BUS = 11,
  71. VSTOR_OPERATION_MAXIMUM = 11
  72. };
  73. /*
  74. * Platform neutral description of a scsi request -
  75. * this remains the same across the write regardless of 32/64 bit
  76. * note: it's patterned off the SCSI_PASS_THROUGH structure
  77. */
  78. #define STORVSC_MAX_CMD_LEN 0x10
  79. #define STORVSC_SENSE_BUFFER_SIZE 0x12
  80. #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
  81. struct vmscsi_request {
  82. u16 length;
  83. u8 srb_status;
  84. u8 scsi_status;
  85. u8 port_number;
  86. u8 path_id;
  87. u8 target_id;
  88. u8 lun;
  89. u8 cdb_length;
  90. u8 sense_info_length;
  91. u8 data_in;
  92. u8 reserved;
  93. u32 data_transfer_length;
  94. union {
  95. u8 cdb[STORVSC_MAX_CMD_LEN];
  96. u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
  97. u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
  98. };
  99. } __attribute((packed));
  100. /*
  101. * This structure is sent during the intialization phase to get the different
  102. * properties of the channel.
  103. */
  104. struct vmstorage_channel_properties {
  105. u16 protocol_version;
  106. u8 path_id;
  107. u8 target_id;
  108. /* Note: port number is only really known on the client side */
  109. u32 port_number;
  110. u32 flags;
  111. u32 max_transfer_bytes;
  112. /*
  113. * This id is unique for each channel and will correspond with
  114. * vendor specific data in the inquiry data.
  115. */
  116. u64 unique_id;
  117. } __packed;
  118. /* This structure is sent during the storage protocol negotiations. */
  119. struct vmstorage_protocol_version {
  120. /* Major (MSW) and minor (LSW) version numbers. */
  121. u16 major_minor;
  122. /*
  123. * Revision number is auto-incremented whenever this file is changed
  124. * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
  125. * definitely indicate incompatibility--but it does indicate mismatched
  126. * builds.
  127. * This is only used on the windows side. Just set it to 0.
  128. */
  129. u16 revision;
  130. } __packed;
  131. /* Channel Property Flags */
  132. #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
  133. #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
  134. struct vstor_packet {
  135. /* Requested operation type */
  136. enum vstor_packet_operation operation;
  137. /* Flags - see below for values */
  138. u32 flags;
  139. /* Status of the request returned from the server side. */
  140. u32 status;
  141. /* Data payload area */
  142. union {
  143. /*
  144. * Structure used to forward SCSI commands from the
  145. * client to the server.
  146. */
  147. struct vmscsi_request vm_srb;
  148. /* Structure used to query channel properties. */
  149. struct vmstorage_channel_properties storage_channel_properties;
  150. /* Used during version negotiations. */
  151. struct vmstorage_protocol_version version;
  152. };
  153. } __packed;
  154. /*
  155. * Packet Flags:
  156. *
  157. * This flag indicates that the server should send back a completion for this
  158. * packet.
  159. */
  160. #define REQUEST_COMPLETION_FLAG 0x1
  161. /* Matches Windows-end */
  162. enum storvsc_request_type {
  163. WRITE_TYPE = 0,
  164. READ_TYPE,
  165. UNKNOWN_TYPE,
  166. };
  167. /*
  168. * SRB status codes and masks; a subset of the codes used here.
  169. */
  170. #define SRB_STATUS_AUTOSENSE_VALID 0x80
  171. #define SRB_STATUS_INVALID_LUN 0x20
  172. #define SRB_STATUS_SUCCESS 0x01
  173. #define SRB_STATUS_ERROR 0x04
  174. /*
  175. * This is the end of Protocol specific defines.
  176. */
  177. /*
  178. * We setup a mempool to allocate request structures for this driver
  179. * on a per-lun basis. The following define specifies the number of
  180. * elements in the pool.
  181. */
  182. #define STORVSC_MIN_BUF_NR 64
  183. static int storvsc_ringbuffer_size = (20 * PAGE_SIZE);
  184. module_param(storvsc_ringbuffer_size, int, S_IRUGO);
  185. MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
  186. #define STORVSC_MAX_IO_REQUESTS 128
  187. /*
  188. * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
  189. * reality, the path/target is not used (ie always set to 0) so our
  190. * scsi host adapter essentially has 1 bus with 1 target that contains
  191. * up to 256 luns.
  192. */
  193. #define STORVSC_MAX_LUNS_PER_TARGET 64
  194. #define STORVSC_MAX_TARGETS 1
  195. #define STORVSC_MAX_CHANNELS 1
  196. struct storvsc_cmd_request {
  197. struct list_head entry;
  198. struct scsi_cmnd *cmd;
  199. unsigned int bounce_sgl_count;
  200. struct scatterlist *bounce_sgl;
  201. struct hv_device *device;
  202. /* Synchronize the request/response if needed */
  203. struct completion wait_event;
  204. unsigned char *sense_buffer;
  205. struct hv_multipage_buffer data_buffer;
  206. struct vstor_packet vstor_packet;
  207. };
  208. /* A storvsc device is a device object that contains a vmbus channel */
  209. struct storvsc_device {
  210. struct hv_device *device;
  211. bool destroy;
  212. bool drain_notify;
  213. atomic_t num_outstanding_req;
  214. struct Scsi_Host *host;
  215. wait_queue_head_t waiting_to_drain;
  216. /*
  217. * Each unique Port/Path/Target represents 1 channel ie scsi
  218. * controller. In reality, the pathid, targetid is always 0
  219. * and the port is set by us
  220. */
  221. unsigned int port_number;
  222. unsigned char path_id;
  223. unsigned char target_id;
  224. /* Used for vsc/vsp channel reset process */
  225. struct storvsc_cmd_request init_request;
  226. struct storvsc_cmd_request reset_request;
  227. };
  228. struct stor_mem_pools {
  229. struct kmem_cache *request_pool;
  230. mempool_t *request_mempool;
  231. };
  232. struct hv_host_device {
  233. struct hv_device *dev;
  234. unsigned int port;
  235. unsigned char path;
  236. unsigned char target;
  237. };
  238. struct storvsc_scan_work {
  239. struct work_struct work;
  240. struct Scsi_Host *host;
  241. uint lun;
  242. };
  243. static void storvsc_bus_scan(struct work_struct *work)
  244. {
  245. struct storvsc_scan_work *wrk;
  246. int id, order_id;
  247. wrk = container_of(work, struct storvsc_scan_work, work);
  248. for (id = 0; id < wrk->host->max_id; ++id) {
  249. if (wrk->host->reverse_ordering)
  250. order_id = wrk->host->max_id - id - 1;
  251. else
  252. order_id = id;
  253. scsi_scan_target(&wrk->host->shost_gendev, 0,
  254. order_id, SCAN_WILD_CARD, 1);
  255. }
  256. kfree(wrk);
  257. }
  258. static void storvsc_remove_lun(struct work_struct *work)
  259. {
  260. struct storvsc_scan_work *wrk;
  261. struct scsi_device *sdev;
  262. wrk = container_of(work, struct storvsc_scan_work, work);
  263. if (!scsi_host_get(wrk->host))
  264. goto done;
  265. sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
  266. if (sdev) {
  267. scsi_remove_device(sdev);
  268. scsi_device_put(sdev);
  269. }
  270. scsi_host_put(wrk->host);
  271. done:
  272. kfree(wrk);
  273. }
  274. /*
  275. * Major/minor macros. Minor version is in LSB, meaning that earlier flat
  276. * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
  277. */
  278. static inline u16 storvsc_get_version(u8 major, u8 minor)
  279. {
  280. u16 version;
  281. version = ((major << 8) | minor);
  282. return version;
  283. }
  284. /*
  285. * We can get incoming messages from the host that are not in response to
  286. * messages that we have sent out. An example of this would be messages
  287. * received by the guest to notify dynamic addition/removal of LUNs. To
  288. * deal with potential race conditions where the driver may be in the
  289. * midst of being unloaded when we might receive an unsolicited message
  290. * from the host, we have implemented a mechanism to gurantee sequential
  291. * consistency:
  292. *
  293. * 1) Once the device is marked as being destroyed, we will fail all
  294. * outgoing messages.
  295. * 2) We permit incoming messages when the device is being destroyed,
  296. * only to properly account for messages already sent out.
  297. */
  298. static inline struct storvsc_device *get_out_stor_device(
  299. struct hv_device *device)
  300. {
  301. struct storvsc_device *stor_device;
  302. stor_device = hv_get_drvdata(device);
  303. if (stor_device && stor_device->destroy)
  304. stor_device = NULL;
  305. return stor_device;
  306. }
  307. static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
  308. {
  309. dev->drain_notify = true;
  310. wait_event(dev->waiting_to_drain,
  311. atomic_read(&dev->num_outstanding_req) == 0);
  312. dev->drain_notify = false;
  313. }
  314. static inline struct storvsc_device *get_in_stor_device(
  315. struct hv_device *device)
  316. {
  317. struct storvsc_device *stor_device;
  318. stor_device = hv_get_drvdata(device);
  319. if (!stor_device)
  320. goto get_in_err;
  321. /*
  322. * If the device is being destroyed; allow incoming
  323. * traffic only to cleanup outstanding requests.
  324. */
  325. if (stor_device->destroy &&
  326. (atomic_read(&stor_device->num_outstanding_req) == 0))
  327. stor_device = NULL;
  328. get_in_err:
  329. return stor_device;
  330. }
  331. static void destroy_bounce_buffer(struct scatterlist *sgl,
  332. unsigned int sg_count)
  333. {
  334. int i;
  335. struct page *page_buf;
  336. for (i = 0; i < sg_count; i++) {
  337. page_buf = sg_page((&sgl[i]));
  338. if (page_buf != NULL)
  339. __free_page(page_buf);
  340. }
  341. kfree(sgl);
  342. }
  343. static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
  344. {
  345. int i;
  346. /* No need to check */
  347. if (sg_count < 2)
  348. return -1;
  349. /* We have at least 2 sg entries */
  350. for (i = 0; i < sg_count; i++) {
  351. if (i == 0) {
  352. /* make sure 1st one does not have hole */
  353. if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
  354. return i;
  355. } else if (i == sg_count - 1) {
  356. /* make sure last one does not have hole */
  357. if (sgl[i].offset != 0)
  358. return i;
  359. } else {
  360. /* make sure no hole in the middle */
  361. if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
  362. return i;
  363. }
  364. }
  365. return -1;
  366. }
  367. static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
  368. unsigned int sg_count,
  369. unsigned int len,
  370. int write)
  371. {
  372. int i;
  373. int num_pages;
  374. struct scatterlist *bounce_sgl;
  375. struct page *page_buf;
  376. unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
  377. num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
  378. bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
  379. if (!bounce_sgl)
  380. return NULL;
  381. for (i = 0; i < num_pages; i++) {
  382. page_buf = alloc_page(GFP_ATOMIC);
  383. if (!page_buf)
  384. goto cleanup;
  385. sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
  386. }
  387. return bounce_sgl;
  388. cleanup:
  389. destroy_bounce_buffer(bounce_sgl, num_pages);
  390. return NULL;
  391. }
  392. /* Assume the original sgl has enough room */
  393. static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
  394. struct scatterlist *bounce_sgl,
  395. unsigned int orig_sgl_count,
  396. unsigned int bounce_sgl_count)
  397. {
  398. int i;
  399. int j = 0;
  400. unsigned long src, dest;
  401. unsigned int srclen, destlen, copylen;
  402. unsigned int total_copied = 0;
  403. unsigned long bounce_addr = 0;
  404. unsigned long dest_addr = 0;
  405. unsigned long flags;
  406. local_irq_save(flags);
  407. for (i = 0; i < orig_sgl_count; i++) {
  408. dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
  409. KM_IRQ0) + orig_sgl[i].offset;
  410. dest = dest_addr;
  411. destlen = orig_sgl[i].length;
  412. if (bounce_addr == 0)
  413. bounce_addr =
  414. (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
  415. KM_IRQ0);
  416. while (destlen) {
  417. src = bounce_addr + bounce_sgl[j].offset;
  418. srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
  419. copylen = min(srclen, destlen);
  420. memcpy((void *)dest, (void *)src, copylen);
  421. total_copied += copylen;
  422. bounce_sgl[j].offset += copylen;
  423. destlen -= copylen;
  424. dest += copylen;
  425. if (bounce_sgl[j].offset == bounce_sgl[j].length) {
  426. /* full */
  427. kunmap_atomic((void *)bounce_addr, KM_IRQ0);
  428. j++;
  429. /*
  430. * It is possible that the number of elements
  431. * in the bounce buffer may not be equal to
  432. * the number of elements in the original
  433. * scatter list. Handle this correctly.
  434. */
  435. if (j == bounce_sgl_count) {
  436. /*
  437. * We are done; cleanup and return.
  438. */
  439. kunmap_atomic((void *)(dest_addr -
  440. orig_sgl[i].offset),
  441. KM_IRQ0);
  442. local_irq_restore(flags);
  443. return total_copied;
  444. }
  445. /* if we need to use another bounce buffer */
  446. if (destlen || i != orig_sgl_count - 1)
  447. bounce_addr =
  448. (unsigned long)kmap_atomic(
  449. sg_page((&bounce_sgl[j])), KM_IRQ0);
  450. } else if (destlen == 0 && i == orig_sgl_count - 1) {
  451. /* unmap the last bounce that is < PAGE_SIZE */
  452. kunmap_atomic((void *)bounce_addr, KM_IRQ0);
  453. }
  454. }
  455. kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
  456. KM_IRQ0);
  457. }
  458. local_irq_restore(flags);
  459. return total_copied;
  460. }
  461. /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
  462. static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
  463. struct scatterlist *bounce_sgl,
  464. unsigned int orig_sgl_count)
  465. {
  466. int i;
  467. int j = 0;
  468. unsigned long src, dest;
  469. unsigned int srclen, destlen, copylen;
  470. unsigned int total_copied = 0;
  471. unsigned long bounce_addr = 0;
  472. unsigned long src_addr = 0;
  473. unsigned long flags;
  474. local_irq_save(flags);
  475. for (i = 0; i < orig_sgl_count; i++) {
  476. src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
  477. KM_IRQ0) + orig_sgl[i].offset;
  478. src = src_addr;
  479. srclen = orig_sgl[i].length;
  480. if (bounce_addr == 0)
  481. bounce_addr =
  482. (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
  483. KM_IRQ0);
  484. while (srclen) {
  485. /* assume bounce offset always == 0 */
  486. dest = bounce_addr + bounce_sgl[j].length;
  487. destlen = PAGE_SIZE - bounce_sgl[j].length;
  488. copylen = min(srclen, destlen);
  489. memcpy((void *)dest, (void *)src, copylen);
  490. total_copied += copylen;
  491. bounce_sgl[j].length += copylen;
  492. srclen -= copylen;
  493. src += copylen;
  494. if (bounce_sgl[j].length == PAGE_SIZE) {
  495. /* full..move to next entry */
  496. kunmap_atomic((void *)bounce_addr, KM_IRQ0);
  497. j++;
  498. /* if we need to use another bounce buffer */
  499. if (srclen || i != orig_sgl_count - 1)
  500. bounce_addr =
  501. (unsigned long)kmap_atomic(
  502. sg_page((&bounce_sgl[j])), KM_IRQ0);
  503. } else if (srclen == 0 && i == orig_sgl_count - 1) {
  504. /* unmap the last bounce that is < PAGE_SIZE */
  505. kunmap_atomic((void *)bounce_addr, KM_IRQ0);
  506. }
  507. }
  508. kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
  509. }
  510. local_irq_restore(flags);
  511. return total_copied;
  512. }
  513. static int storvsc_channel_init(struct hv_device *device)
  514. {
  515. struct storvsc_device *stor_device;
  516. struct storvsc_cmd_request *request;
  517. struct vstor_packet *vstor_packet;
  518. int ret, t;
  519. stor_device = get_out_stor_device(device);
  520. if (!stor_device)
  521. return -ENODEV;
  522. request = &stor_device->init_request;
  523. vstor_packet = &request->vstor_packet;
  524. /*
  525. * Now, initiate the vsc/vsp initialization protocol on the open
  526. * channel
  527. */
  528. memset(request, 0, sizeof(struct storvsc_cmd_request));
  529. init_completion(&request->wait_event);
  530. vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
  531. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  532. ret = vmbus_sendpacket(device->channel, vstor_packet,
  533. sizeof(struct vstor_packet),
  534. (unsigned long)request,
  535. VM_PKT_DATA_INBAND,
  536. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  537. if (ret != 0)
  538. goto cleanup;
  539. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  540. if (t == 0) {
  541. ret = -ETIMEDOUT;
  542. goto cleanup;
  543. }
  544. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  545. vstor_packet->status != 0)
  546. goto cleanup;
  547. /* reuse the packet for version range supported */
  548. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  549. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
  550. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  551. vstor_packet->version.major_minor =
  552. storvsc_get_version(VMSTOR_CURRENT_MAJOR, VMSTOR_CURRENT_MINOR);
  553. /*
  554. * The revision number is only used in Windows; set it to 0.
  555. */
  556. vstor_packet->version.revision = 0;
  557. ret = vmbus_sendpacket(device->channel, vstor_packet,
  558. sizeof(struct vstor_packet),
  559. (unsigned long)request,
  560. VM_PKT_DATA_INBAND,
  561. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  562. if (ret != 0)
  563. goto cleanup;
  564. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  565. if (t == 0) {
  566. ret = -ETIMEDOUT;
  567. goto cleanup;
  568. }
  569. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  570. vstor_packet->status != 0)
  571. goto cleanup;
  572. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  573. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
  574. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  575. vstor_packet->storage_channel_properties.port_number =
  576. stor_device->port_number;
  577. ret = vmbus_sendpacket(device->channel, vstor_packet,
  578. sizeof(struct vstor_packet),
  579. (unsigned long)request,
  580. VM_PKT_DATA_INBAND,
  581. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  582. if (ret != 0)
  583. goto cleanup;
  584. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  585. if (t == 0) {
  586. ret = -ETIMEDOUT;
  587. goto cleanup;
  588. }
  589. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  590. vstor_packet->status != 0)
  591. goto cleanup;
  592. stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
  593. stor_device->target_id
  594. = vstor_packet->storage_channel_properties.target_id;
  595. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  596. vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
  597. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  598. ret = vmbus_sendpacket(device->channel, vstor_packet,
  599. sizeof(struct vstor_packet),
  600. (unsigned long)request,
  601. VM_PKT_DATA_INBAND,
  602. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  603. if (ret != 0)
  604. goto cleanup;
  605. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  606. if (t == 0) {
  607. ret = -ETIMEDOUT;
  608. goto cleanup;
  609. }
  610. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  611. vstor_packet->status != 0)
  612. goto cleanup;
  613. cleanup:
  614. return ret;
  615. }
  616. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
  617. {
  618. struct scsi_cmnd *scmnd = cmd_request->cmd;
  619. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  620. void (*scsi_done_fn)(struct scsi_cmnd *);
  621. struct scsi_sense_hdr sense_hdr;
  622. struct vmscsi_request *vm_srb;
  623. struct storvsc_scan_work *wrk;
  624. struct stor_mem_pools *memp = scmnd->device->hostdata;
  625. vm_srb = &cmd_request->vstor_packet.vm_srb;
  626. if (cmd_request->bounce_sgl_count) {
  627. if (vm_srb->data_in == READ_TYPE)
  628. copy_from_bounce_buffer(scsi_sglist(scmnd),
  629. cmd_request->bounce_sgl,
  630. scsi_sg_count(scmnd),
  631. cmd_request->bounce_sgl_count);
  632. destroy_bounce_buffer(cmd_request->bounce_sgl,
  633. cmd_request->bounce_sgl_count);
  634. }
  635. /*
  636. * If there is an error; offline the device since all
  637. * error recovery strategies would have already been
  638. * deployed on the host side.
  639. */
  640. if (vm_srb->srb_status == SRB_STATUS_ERROR)
  641. scmnd->result = DID_TARGET_FAILURE << 16;
  642. else
  643. scmnd->result = vm_srb->scsi_status;
  644. /*
  645. * If the LUN is invalid; remove the device.
  646. */
  647. if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
  648. struct storvsc_device *stor_dev;
  649. struct hv_device *dev = host_dev->dev;
  650. struct Scsi_Host *host;
  651. stor_dev = get_in_stor_device(dev);
  652. host = stor_dev->host;
  653. wrk = kmalloc(sizeof(struct storvsc_scan_work),
  654. GFP_ATOMIC);
  655. if (!wrk) {
  656. scmnd->result = DID_TARGET_FAILURE << 16;
  657. } else {
  658. wrk->host = host;
  659. wrk->lun = vm_srb->lun;
  660. INIT_WORK(&wrk->work, storvsc_remove_lun);
  661. schedule_work(&wrk->work);
  662. }
  663. }
  664. if (scmnd->result) {
  665. if (scsi_normalize_sense(scmnd->sense_buffer,
  666. SCSI_SENSE_BUFFERSIZE, &sense_hdr))
  667. scsi_print_sense_hdr("storvsc", &sense_hdr);
  668. }
  669. scsi_set_resid(scmnd,
  670. cmd_request->data_buffer.len -
  671. vm_srb->data_transfer_length);
  672. scsi_done_fn = scmnd->scsi_done;
  673. scmnd->host_scribble = NULL;
  674. scmnd->scsi_done = NULL;
  675. scsi_done_fn(scmnd);
  676. mempool_free(cmd_request, memp->request_mempool);
  677. }
  678. static void storvsc_on_io_completion(struct hv_device *device,
  679. struct vstor_packet *vstor_packet,
  680. struct storvsc_cmd_request *request)
  681. {
  682. struct storvsc_device *stor_device;
  683. struct vstor_packet *stor_pkt;
  684. stor_device = hv_get_drvdata(device);
  685. stor_pkt = &request->vstor_packet;
  686. /*
  687. * The current SCSI handling on the host side does
  688. * not correctly handle:
  689. * INQUIRY command with page code parameter set to 0x80
  690. * MODE_SENSE command with cmd[2] == 0x1c
  691. *
  692. * Setup srb and scsi status so this won't be fatal.
  693. * We do this so we can distinguish truly fatal failues
  694. * (srb status == 0x4) and off-line the device in that case.
  695. */
  696. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  697. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  698. vstor_packet->vm_srb.scsi_status = 0;
  699. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  700. }
  701. /* Copy over the status...etc */
  702. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  703. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  704. stor_pkt->vm_srb.sense_info_length =
  705. vstor_packet->vm_srb.sense_info_length;
  706. if (vstor_packet->vm_srb.scsi_status != 0 ||
  707. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
  708. dev_warn(&device->device,
  709. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  710. stor_pkt->vm_srb.cdb[0],
  711. vstor_packet->vm_srb.scsi_status,
  712. vstor_packet->vm_srb.srb_status);
  713. }
  714. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  715. /* CHECK_CONDITION */
  716. if (vstor_packet->vm_srb.srb_status &
  717. SRB_STATUS_AUTOSENSE_VALID) {
  718. /* autosense data available */
  719. dev_warn(&device->device,
  720. "stor pkt %p autosense data valid - len %d\n",
  721. request,
  722. vstor_packet->vm_srb.sense_info_length);
  723. memcpy(request->sense_buffer,
  724. vstor_packet->vm_srb.sense_data,
  725. vstor_packet->vm_srb.sense_info_length);
  726. }
  727. }
  728. stor_pkt->vm_srb.data_transfer_length =
  729. vstor_packet->vm_srb.data_transfer_length;
  730. storvsc_command_completion(request);
  731. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  732. stor_device->drain_notify)
  733. wake_up(&stor_device->waiting_to_drain);
  734. }
  735. static void storvsc_on_receive(struct hv_device *device,
  736. struct vstor_packet *vstor_packet,
  737. struct storvsc_cmd_request *request)
  738. {
  739. struct storvsc_scan_work *work;
  740. struct storvsc_device *stor_device;
  741. switch (vstor_packet->operation) {
  742. case VSTOR_OPERATION_COMPLETE_IO:
  743. storvsc_on_io_completion(device, vstor_packet, request);
  744. break;
  745. case VSTOR_OPERATION_REMOVE_DEVICE:
  746. case VSTOR_OPERATION_ENUMERATE_BUS:
  747. stor_device = get_in_stor_device(device);
  748. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  749. if (!work)
  750. return;
  751. INIT_WORK(&work->work, storvsc_bus_scan);
  752. work->host = stor_device->host;
  753. schedule_work(&work->work);
  754. break;
  755. default:
  756. break;
  757. }
  758. }
  759. static void storvsc_on_channel_callback(void *context)
  760. {
  761. struct hv_device *device = (struct hv_device *)context;
  762. struct storvsc_device *stor_device;
  763. u32 bytes_recvd;
  764. u64 request_id;
  765. unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
  766. struct storvsc_cmd_request *request;
  767. int ret;
  768. stor_device = get_in_stor_device(device);
  769. if (!stor_device)
  770. return;
  771. do {
  772. ret = vmbus_recvpacket(device->channel, packet,
  773. ALIGN(sizeof(struct vstor_packet), 8),
  774. &bytes_recvd, &request_id);
  775. if (ret == 0 && bytes_recvd > 0) {
  776. request = (struct storvsc_cmd_request *)
  777. (unsigned long)request_id;
  778. if ((request == &stor_device->init_request) ||
  779. (request == &stor_device->reset_request)) {
  780. memcpy(&request->vstor_packet, packet,
  781. sizeof(struct vstor_packet));
  782. complete(&request->wait_event);
  783. } else {
  784. storvsc_on_receive(device,
  785. (struct vstor_packet *)packet,
  786. request);
  787. }
  788. } else {
  789. break;
  790. }
  791. } while (1);
  792. return;
  793. }
  794. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
  795. {
  796. struct vmstorage_channel_properties props;
  797. int ret;
  798. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  799. ret = vmbus_open(device->channel,
  800. ring_size,
  801. ring_size,
  802. (void *)&props,
  803. sizeof(struct vmstorage_channel_properties),
  804. storvsc_on_channel_callback, device);
  805. if (ret != 0)
  806. return ret;
  807. ret = storvsc_channel_init(device);
  808. return ret;
  809. }
  810. static int storvsc_dev_remove(struct hv_device *device)
  811. {
  812. struct storvsc_device *stor_device;
  813. unsigned long flags;
  814. stor_device = hv_get_drvdata(device);
  815. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  816. stor_device->destroy = true;
  817. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  818. /*
  819. * At this point, all outbound traffic should be disable. We
  820. * only allow inbound traffic (responses) to proceed so that
  821. * outstanding requests can be completed.
  822. */
  823. storvsc_wait_to_drain(stor_device);
  824. /*
  825. * Since we have already drained, we don't need to busy wait
  826. * as was done in final_release_stor_device()
  827. * Note that we cannot set the ext pointer to NULL until
  828. * we have drained - to drain the outgoing packets, we need to
  829. * allow incoming packets.
  830. */
  831. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  832. hv_set_drvdata(device, NULL);
  833. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  834. /* Close the channel */
  835. vmbus_close(device->channel);
  836. kfree(stor_device);
  837. return 0;
  838. }
  839. static int storvsc_do_io(struct hv_device *device,
  840. struct storvsc_cmd_request *request)
  841. {
  842. struct storvsc_device *stor_device;
  843. struct vstor_packet *vstor_packet;
  844. int ret = 0;
  845. vstor_packet = &request->vstor_packet;
  846. stor_device = get_out_stor_device(device);
  847. if (!stor_device)
  848. return -ENODEV;
  849. request->device = device;
  850. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  851. vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
  852. vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
  853. vstor_packet->vm_srb.data_transfer_length =
  854. request->data_buffer.len;
  855. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  856. if (request->data_buffer.len) {
  857. ret = vmbus_sendpacket_multipagebuffer(device->channel,
  858. &request->data_buffer,
  859. vstor_packet,
  860. sizeof(struct vstor_packet),
  861. (unsigned long)request);
  862. } else {
  863. ret = vmbus_sendpacket(device->channel, vstor_packet,
  864. sizeof(struct vstor_packet),
  865. (unsigned long)request,
  866. VM_PKT_DATA_INBAND,
  867. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  868. }
  869. if (ret != 0)
  870. return ret;
  871. atomic_inc(&stor_device->num_outstanding_req);
  872. return ret;
  873. }
  874. static int storvsc_device_alloc(struct scsi_device *sdevice)
  875. {
  876. struct stor_mem_pools *memp;
  877. int number = STORVSC_MIN_BUF_NR;
  878. memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
  879. if (!memp)
  880. return -ENOMEM;
  881. memp->request_pool =
  882. kmem_cache_create(dev_name(&sdevice->sdev_dev),
  883. sizeof(struct storvsc_cmd_request), 0,
  884. SLAB_HWCACHE_ALIGN, NULL);
  885. if (!memp->request_pool)
  886. goto err0;
  887. memp->request_mempool = mempool_create(number, mempool_alloc_slab,
  888. mempool_free_slab,
  889. memp->request_pool);
  890. if (!memp->request_mempool)
  891. goto err1;
  892. sdevice->hostdata = memp;
  893. return 0;
  894. err1:
  895. kmem_cache_destroy(memp->request_pool);
  896. err0:
  897. kfree(memp);
  898. return -ENOMEM;
  899. }
  900. static void storvsc_device_destroy(struct scsi_device *sdevice)
  901. {
  902. struct stor_mem_pools *memp = sdevice->hostdata;
  903. mempool_destroy(memp->request_mempool);
  904. kmem_cache_destroy(memp->request_pool);
  905. kfree(memp);
  906. sdevice->hostdata = NULL;
  907. }
  908. static int storvsc_device_configure(struct scsi_device *sdevice)
  909. {
  910. scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
  911. STORVSC_MAX_IO_REQUESTS);
  912. blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
  913. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  914. return 0;
  915. }
  916. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  917. sector_t capacity, int *info)
  918. {
  919. sector_t nsect = capacity;
  920. sector_t cylinders = nsect;
  921. int heads, sectors_pt;
  922. /*
  923. * We are making up these values; let us keep it simple.
  924. */
  925. heads = 0xff;
  926. sectors_pt = 0x3f; /* Sectors per track */
  927. sector_div(cylinders, heads * sectors_pt);
  928. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  929. cylinders = 0xffff;
  930. info[0] = heads;
  931. info[1] = sectors_pt;
  932. info[2] = (int)cylinders;
  933. return 0;
  934. }
  935. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  936. {
  937. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  938. struct hv_device *device = host_dev->dev;
  939. struct storvsc_device *stor_device;
  940. struct storvsc_cmd_request *request;
  941. struct vstor_packet *vstor_packet;
  942. int ret, t;
  943. stor_device = get_out_stor_device(device);
  944. if (!stor_device)
  945. return FAILED;
  946. request = &stor_device->reset_request;
  947. vstor_packet = &request->vstor_packet;
  948. init_completion(&request->wait_event);
  949. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  950. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  951. vstor_packet->vm_srb.path_id = stor_device->path_id;
  952. ret = vmbus_sendpacket(device->channel, vstor_packet,
  953. sizeof(struct vstor_packet),
  954. (unsigned long)&stor_device->reset_request,
  955. VM_PKT_DATA_INBAND,
  956. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  957. if (ret != 0)
  958. return FAILED;
  959. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  960. if (t == 0)
  961. return TIMEOUT_ERROR;
  962. /*
  963. * At this point, all outstanding requests in the adapter
  964. * should have been flushed out and return to us
  965. */
  966. return SUCCESS;
  967. }
  968. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  969. {
  970. bool allowed = true;
  971. u8 scsi_op = scmnd->cmnd[0];
  972. switch (scsi_op) {
  973. /*
  974. * smartd sends this command and the host does not handle
  975. * this. So, don't send it.
  976. */
  977. case SET_WINDOW:
  978. scmnd->result = ILLEGAL_REQUEST << 16;
  979. allowed = false;
  980. break;
  981. default:
  982. break;
  983. }
  984. return allowed;
  985. }
  986. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  987. {
  988. int ret;
  989. struct hv_host_device *host_dev = shost_priv(host);
  990. struct hv_device *dev = host_dev->dev;
  991. struct storvsc_cmd_request *cmd_request;
  992. unsigned int request_size = 0;
  993. int i;
  994. struct scatterlist *sgl;
  995. unsigned int sg_count = 0;
  996. struct vmscsi_request *vm_srb;
  997. struct stor_mem_pools *memp = scmnd->device->hostdata;
  998. if (!storvsc_scsi_cmd_ok(scmnd)) {
  999. scmnd->scsi_done(scmnd);
  1000. return 0;
  1001. }
  1002. request_size = sizeof(struct storvsc_cmd_request);
  1003. cmd_request = mempool_alloc(memp->request_mempool,
  1004. GFP_ATOMIC);
  1005. /*
  1006. * We might be invoked in an interrupt context; hence
  1007. * mempool_alloc() can fail.
  1008. */
  1009. if (!cmd_request)
  1010. return SCSI_MLQUEUE_DEVICE_BUSY;
  1011. memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
  1012. /* Setup the cmd request */
  1013. cmd_request->cmd = scmnd;
  1014. scmnd->host_scribble = (unsigned char *)cmd_request;
  1015. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1016. /* Build the SRB */
  1017. switch (scmnd->sc_data_direction) {
  1018. case DMA_TO_DEVICE:
  1019. vm_srb->data_in = WRITE_TYPE;
  1020. break;
  1021. case DMA_FROM_DEVICE:
  1022. vm_srb->data_in = READ_TYPE;
  1023. break;
  1024. default:
  1025. vm_srb->data_in = UNKNOWN_TYPE;
  1026. break;
  1027. }
  1028. vm_srb->port_number = host_dev->port;
  1029. vm_srb->path_id = scmnd->device->channel;
  1030. vm_srb->target_id = scmnd->device->id;
  1031. vm_srb->lun = scmnd->device->lun;
  1032. vm_srb->cdb_length = scmnd->cmd_len;
  1033. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1034. cmd_request->sense_buffer = scmnd->sense_buffer;
  1035. cmd_request->data_buffer.len = scsi_bufflen(scmnd);
  1036. if (scsi_sg_count(scmnd)) {
  1037. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1038. sg_count = scsi_sg_count(scmnd);
  1039. /* check if we need to bounce the sgl */
  1040. if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
  1041. cmd_request->bounce_sgl =
  1042. create_bounce_buffer(sgl, scsi_sg_count(scmnd),
  1043. scsi_bufflen(scmnd),
  1044. vm_srb->data_in);
  1045. if (!cmd_request->bounce_sgl) {
  1046. ret = SCSI_MLQUEUE_HOST_BUSY;
  1047. goto queue_error;
  1048. }
  1049. cmd_request->bounce_sgl_count =
  1050. ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
  1051. PAGE_SHIFT;
  1052. if (vm_srb->data_in == WRITE_TYPE)
  1053. copy_to_bounce_buffer(sgl,
  1054. cmd_request->bounce_sgl,
  1055. scsi_sg_count(scmnd));
  1056. sgl = cmd_request->bounce_sgl;
  1057. sg_count = cmd_request->bounce_sgl_count;
  1058. }
  1059. cmd_request->data_buffer.offset = sgl[0].offset;
  1060. for (i = 0; i < sg_count; i++)
  1061. cmd_request->data_buffer.pfn_array[i] =
  1062. page_to_pfn(sg_page((&sgl[i])));
  1063. } else if (scsi_sglist(scmnd)) {
  1064. cmd_request->data_buffer.offset =
  1065. virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
  1066. cmd_request->data_buffer.pfn_array[0] =
  1067. virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
  1068. }
  1069. /* Invokes the vsc to start an IO */
  1070. ret = storvsc_do_io(dev, cmd_request);
  1071. if (ret == -EAGAIN) {
  1072. /* no more space */
  1073. if (cmd_request->bounce_sgl_count) {
  1074. destroy_bounce_buffer(cmd_request->bounce_sgl,
  1075. cmd_request->bounce_sgl_count);
  1076. ret = SCSI_MLQUEUE_DEVICE_BUSY;
  1077. goto queue_error;
  1078. }
  1079. }
  1080. return 0;
  1081. queue_error:
  1082. mempool_free(cmd_request, memp->request_mempool);
  1083. scmnd->host_scribble = NULL;
  1084. return ret;
  1085. }
  1086. static struct scsi_host_template scsi_driver = {
  1087. .module = THIS_MODULE,
  1088. .name = "storvsc_host_t",
  1089. .bios_param = storvsc_get_chs,
  1090. .queuecommand = storvsc_queuecommand,
  1091. .eh_host_reset_handler = storvsc_host_reset_handler,
  1092. .slave_alloc = storvsc_device_alloc,
  1093. .slave_destroy = storvsc_device_destroy,
  1094. .slave_configure = storvsc_device_configure,
  1095. .cmd_per_lun = 1,
  1096. /* 64 max_queue * 1 target */
  1097. .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
  1098. .this_id = -1,
  1099. /* no use setting to 0 since ll_blk_rw reset it to 1 */
  1100. /* currently 32 */
  1101. .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
  1102. .use_clustering = DISABLE_CLUSTERING,
  1103. /* Make sure we dont get a sg segment crosses a page boundary */
  1104. .dma_boundary = PAGE_SIZE-1,
  1105. };
  1106. enum {
  1107. SCSI_GUID,
  1108. IDE_GUID,
  1109. };
  1110. static const struct hv_vmbus_device_id id_table[] = {
  1111. /* SCSI guid */
  1112. { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
  1113. 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  1114. .driver_data = SCSI_GUID },
  1115. /* IDE guid */
  1116. { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
  1117. 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  1118. .driver_data = IDE_GUID },
  1119. { },
  1120. };
  1121. MODULE_DEVICE_TABLE(vmbus, id_table);
  1122. static int storvsc_probe(struct hv_device *device,
  1123. const struct hv_vmbus_device_id *dev_id)
  1124. {
  1125. int ret;
  1126. struct Scsi_Host *host;
  1127. struct hv_host_device *host_dev;
  1128. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1129. int target = 0;
  1130. struct storvsc_device *stor_device;
  1131. host = scsi_host_alloc(&scsi_driver,
  1132. sizeof(struct hv_host_device));
  1133. if (!host)
  1134. return -ENOMEM;
  1135. host_dev = shost_priv(host);
  1136. memset(host_dev, 0, sizeof(struct hv_host_device));
  1137. host_dev->port = host->host_no;
  1138. host_dev->dev = device;
  1139. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1140. if (!stor_device) {
  1141. ret = -ENOMEM;
  1142. goto err_out0;
  1143. }
  1144. stor_device->destroy = false;
  1145. init_waitqueue_head(&stor_device->waiting_to_drain);
  1146. stor_device->device = device;
  1147. stor_device->host = host;
  1148. hv_set_drvdata(device, stor_device);
  1149. stor_device->port_number = host->host_no;
  1150. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
  1151. if (ret)
  1152. goto err_out1;
  1153. host_dev->path = stor_device->path_id;
  1154. host_dev->target = stor_device->target_id;
  1155. /* max # of devices per target */
  1156. host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
  1157. /* max # of targets per channel */
  1158. host->max_id = STORVSC_MAX_TARGETS;
  1159. /* max # of channels */
  1160. host->max_channel = STORVSC_MAX_CHANNELS - 1;
  1161. /* max cmd length */
  1162. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1163. /* Register the HBA and start the scsi bus scan */
  1164. ret = scsi_add_host(host, &device->device);
  1165. if (ret != 0)
  1166. goto err_out2;
  1167. if (!dev_is_ide) {
  1168. scsi_scan_host(host);
  1169. } else {
  1170. target = (device->dev_instance.b[5] << 8 |
  1171. device->dev_instance.b[4]);
  1172. ret = scsi_add_device(host, 0, target, 0);
  1173. if (ret) {
  1174. scsi_remove_host(host);
  1175. goto err_out2;
  1176. }
  1177. }
  1178. return 0;
  1179. err_out2:
  1180. /*
  1181. * Once we have connected with the host, we would need to
  1182. * to invoke storvsc_dev_remove() to rollback this state and
  1183. * this call also frees up the stor_device; hence the jump around
  1184. * err_out1 label.
  1185. */
  1186. storvsc_dev_remove(device);
  1187. goto err_out0;
  1188. err_out1:
  1189. kfree(stor_device);
  1190. err_out0:
  1191. scsi_host_put(host);
  1192. return ret;
  1193. }
  1194. static int storvsc_remove(struct hv_device *dev)
  1195. {
  1196. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1197. struct Scsi_Host *host = stor_device->host;
  1198. scsi_remove_host(host);
  1199. storvsc_dev_remove(dev);
  1200. scsi_host_put(host);
  1201. return 0;
  1202. }
  1203. static struct hv_driver storvsc_drv = {
  1204. .name = KBUILD_MODNAME,
  1205. .id_table = id_table,
  1206. .probe = storvsc_probe,
  1207. .remove = storvsc_remove,
  1208. };
  1209. static int __init storvsc_drv_init(void)
  1210. {
  1211. u32 max_outstanding_req_per_channel;
  1212. /*
  1213. * Divide the ring buffer data size (which is 1 page less
  1214. * than the ring buffer size since that page is reserved for
  1215. * the ring buffer indices) by the max request size (which is
  1216. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1217. */
  1218. max_outstanding_req_per_channel =
  1219. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1220. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1221. sizeof(struct vstor_packet) + sizeof(u64),
  1222. sizeof(u64)));
  1223. if (max_outstanding_req_per_channel <
  1224. STORVSC_MAX_IO_REQUESTS)
  1225. return -EINVAL;
  1226. return vmbus_driver_register(&storvsc_drv);
  1227. }
  1228. static void __exit storvsc_drv_exit(void)
  1229. {
  1230. vmbus_driver_unregister(&storvsc_drv);
  1231. }
  1232. MODULE_LICENSE("GPL");
  1233. MODULE_VERSION(HV_DRV_VERSION);
  1234. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1235. module_init(storvsc_drv_init);
  1236. module_exit(storvsc_drv_exit);