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. /* Disgusting wrapper functions */
  393. static inline unsigned long sg_kmap_atomic(struct scatterlist *sgl, int idx)
  394. {
  395. void *addr = kmap_atomic(sg_page(sgl + idx));
  396. return (unsigned long)addr;
  397. }
  398. static inline void sg_kunmap_atomic(unsigned long addr)
  399. {
  400. kunmap_atomic((void *)addr);
  401. }
  402. /* Assume the original sgl has enough room */
  403. static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
  404. struct scatterlist *bounce_sgl,
  405. unsigned int orig_sgl_count,
  406. unsigned int bounce_sgl_count)
  407. {
  408. int i;
  409. int j = 0;
  410. unsigned long src, dest;
  411. unsigned int srclen, destlen, copylen;
  412. unsigned int total_copied = 0;
  413. unsigned long bounce_addr = 0;
  414. unsigned long dest_addr = 0;
  415. unsigned long flags;
  416. local_irq_save(flags);
  417. for (i = 0; i < orig_sgl_count; i++) {
  418. dest_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
  419. dest = dest_addr;
  420. destlen = orig_sgl[i].length;
  421. if (bounce_addr == 0)
  422. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  423. while (destlen) {
  424. src = bounce_addr + bounce_sgl[j].offset;
  425. srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
  426. copylen = min(srclen, destlen);
  427. memcpy((void *)dest, (void *)src, copylen);
  428. total_copied += copylen;
  429. bounce_sgl[j].offset += copylen;
  430. destlen -= copylen;
  431. dest += copylen;
  432. if (bounce_sgl[j].offset == bounce_sgl[j].length) {
  433. /* full */
  434. sg_kunmap_atomic(bounce_addr);
  435. j++;
  436. /*
  437. * It is possible that the number of elements
  438. * in the bounce buffer may not be equal to
  439. * the number of elements in the original
  440. * scatter list. Handle this correctly.
  441. */
  442. if (j == bounce_sgl_count) {
  443. /*
  444. * We are done; cleanup and return.
  445. */
  446. sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
  447. local_irq_restore(flags);
  448. return total_copied;
  449. }
  450. /* if we need to use another bounce buffer */
  451. if (destlen || i != orig_sgl_count - 1)
  452. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  453. } else if (destlen == 0 && i == orig_sgl_count - 1) {
  454. /* unmap the last bounce that is < PAGE_SIZE */
  455. sg_kunmap_atomic(bounce_addr);
  456. }
  457. }
  458. sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
  459. }
  460. local_irq_restore(flags);
  461. return total_copied;
  462. }
  463. /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
  464. static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
  465. struct scatterlist *bounce_sgl,
  466. unsigned int orig_sgl_count)
  467. {
  468. int i;
  469. int j = 0;
  470. unsigned long src, dest;
  471. unsigned int srclen, destlen, copylen;
  472. unsigned int total_copied = 0;
  473. unsigned long bounce_addr = 0;
  474. unsigned long src_addr = 0;
  475. unsigned long flags;
  476. local_irq_save(flags);
  477. for (i = 0; i < orig_sgl_count; i++) {
  478. src_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
  479. src = src_addr;
  480. srclen = orig_sgl[i].length;
  481. if (bounce_addr == 0)
  482. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  483. while (srclen) {
  484. /* assume bounce offset always == 0 */
  485. dest = bounce_addr + bounce_sgl[j].length;
  486. destlen = PAGE_SIZE - bounce_sgl[j].length;
  487. copylen = min(srclen, destlen);
  488. memcpy((void *)dest, (void *)src, copylen);
  489. total_copied += copylen;
  490. bounce_sgl[j].length += copylen;
  491. srclen -= copylen;
  492. src += copylen;
  493. if (bounce_sgl[j].length == PAGE_SIZE) {
  494. /* full..move to next entry */
  495. sg_kunmap_atomic(bounce_addr);
  496. j++;
  497. /* if we need to use another bounce buffer */
  498. if (srclen || i != orig_sgl_count - 1)
  499. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  500. } else if (srclen == 0 && i == orig_sgl_count - 1) {
  501. /* unmap the last bounce that is < PAGE_SIZE */
  502. sg_kunmap_atomic(bounce_addr);
  503. }
  504. }
  505. sg_kunmap_atomic(src_addr - orig_sgl[i].offset);
  506. }
  507. local_irq_restore(flags);
  508. return total_copied;
  509. }
  510. static int storvsc_channel_init(struct hv_device *device)
  511. {
  512. struct storvsc_device *stor_device;
  513. struct storvsc_cmd_request *request;
  514. struct vstor_packet *vstor_packet;
  515. int ret, t;
  516. stor_device = get_out_stor_device(device);
  517. if (!stor_device)
  518. return -ENODEV;
  519. request = &stor_device->init_request;
  520. vstor_packet = &request->vstor_packet;
  521. /*
  522. * Now, initiate the vsc/vsp initialization protocol on the open
  523. * channel
  524. */
  525. memset(request, 0, sizeof(struct storvsc_cmd_request));
  526. init_completion(&request->wait_event);
  527. vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
  528. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  529. ret = vmbus_sendpacket(device->channel, vstor_packet,
  530. sizeof(struct vstor_packet),
  531. (unsigned long)request,
  532. VM_PKT_DATA_INBAND,
  533. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  534. if (ret != 0)
  535. goto cleanup;
  536. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  537. if (t == 0) {
  538. ret = -ETIMEDOUT;
  539. goto cleanup;
  540. }
  541. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  542. vstor_packet->status != 0)
  543. goto cleanup;
  544. /* reuse the packet for version range supported */
  545. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  546. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
  547. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  548. vstor_packet->version.major_minor =
  549. storvsc_get_version(VMSTOR_CURRENT_MAJOR, VMSTOR_CURRENT_MINOR);
  550. /*
  551. * The revision number is only used in Windows; set it to 0.
  552. */
  553. vstor_packet->version.revision = 0;
  554. ret = vmbus_sendpacket(device->channel, vstor_packet,
  555. sizeof(struct vstor_packet),
  556. (unsigned long)request,
  557. VM_PKT_DATA_INBAND,
  558. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  559. if (ret != 0)
  560. goto cleanup;
  561. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  562. if (t == 0) {
  563. ret = -ETIMEDOUT;
  564. goto cleanup;
  565. }
  566. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  567. vstor_packet->status != 0)
  568. goto cleanup;
  569. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  570. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
  571. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  572. vstor_packet->storage_channel_properties.port_number =
  573. stor_device->port_number;
  574. ret = vmbus_sendpacket(device->channel, vstor_packet,
  575. sizeof(struct vstor_packet),
  576. (unsigned long)request,
  577. VM_PKT_DATA_INBAND,
  578. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  579. if (ret != 0)
  580. goto cleanup;
  581. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  582. if (t == 0) {
  583. ret = -ETIMEDOUT;
  584. goto cleanup;
  585. }
  586. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  587. vstor_packet->status != 0)
  588. goto cleanup;
  589. stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
  590. stor_device->target_id
  591. = vstor_packet->storage_channel_properties.target_id;
  592. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  593. vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
  594. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  595. ret = vmbus_sendpacket(device->channel, vstor_packet,
  596. sizeof(struct vstor_packet),
  597. (unsigned long)request,
  598. VM_PKT_DATA_INBAND,
  599. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  600. if (ret != 0)
  601. goto cleanup;
  602. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  603. if (t == 0) {
  604. ret = -ETIMEDOUT;
  605. goto cleanup;
  606. }
  607. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  608. vstor_packet->status != 0)
  609. goto cleanup;
  610. cleanup:
  611. return ret;
  612. }
  613. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
  614. {
  615. struct scsi_cmnd *scmnd = cmd_request->cmd;
  616. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  617. void (*scsi_done_fn)(struct scsi_cmnd *);
  618. struct scsi_sense_hdr sense_hdr;
  619. struct vmscsi_request *vm_srb;
  620. struct storvsc_scan_work *wrk;
  621. struct stor_mem_pools *memp = scmnd->device->hostdata;
  622. vm_srb = &cmd_request->vstor_packet.vm_srb;
  623. if (cmd_request->bounce_sgl_count) {
  624. if (vm_srb->data_in == READ_TYPE)
  625. copy_from_bounce_buffer(scsi_sglist(scmnd),
  626. cmd_request->bounce_sgl,
  627. scsi_sg_count(scmnd),
  628. cmd_request->bounce_sgl_count);
  629. destroy_bounce_buffer(cmd_request->bounce_sgl,
  630. cmd_request->bounce_sgl_count);
  631. }
  632. /*
  633. * If there is an error; offline the device since all
  634. * error recovery strategies would have already been
  635. * deployed on the host side.
  636. */
  637. if (vm_srb->srb_status == SRB_STATUS_ERROR)
  638. scmnd->result = DID_TARGET_FAILURE << 16;
  639. else
  640. scmnd->result = vm_srb->scsi_status;
  641. /*
  642. * If the LUN is invalid; remove the device.
  643. */
  644. if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
  645. struct storvsc_device *stor_dev;
  646. struct hv_device *dev = host_dev->dev;
  647. struct Scsi_Host *host;
  648. stor_dev = get_in_stor_device(dev);
  649. host = stor_dev->host;
  650. wrk = kmalloc(sizeof(struct storvsc_scan_work),
  651. GFP_ATOMIC);
  652. if (!wrk) {
  653. scmnd->result = DID_TARGET_FAILURE << 16;
  654. } else {
  655. wrk->host = host;
  656. wrk->lun = vm_srb->lun;
  657. INIT_WORK(&wrk->work, storvsc_remove_lun);
  658. schedule_work(&wrk->work);
  659. }
  660. }
  661. if (scmnd->result) {
  662. if (scsi_normalize_sense(scmnd->sense_buffer,
  663. SCSI_SENSE_BUFFERSIZE, &sense_hdr))
  664. scsi_print_sense_hdr("storvsc", &sense_hdr);
  665. }
  666. scsi_set_resid(scmnd,
  667. cmd_request->data_buffer.len -
  668. vm_srb->data_transfer_length);
  669. scsi_done_fn = scmnd->scsi_done;
  670. scmnd->host_scribble = NULL;
  671. scmnd->scsi_done = NULL;
  672. scsi_done_fn(scmnd);
  673. mempool_free(cmd_request, memp->request_mempool);
  674. }
  675. static void storvsc_on_io_completion(struct hv_device *device,
  676. struct vstor_packet *vstor_packet,
  677. struct storvsc_cmd_request *request)
  678. {
  679. struct storvsc_device *stor_device;
  680. struct vstor_packet *stor_pkt;
  681. stor_device = hv_get_drvdata(device);
  682. stor_pkt = &request->vstor_packet;
  683. /*
  684. * The current SCSI handling on the host side does
  685. * not correctly handle:
  686. * INQUIRY command with page code parameter set to 0x80
  687. * MODE_SENSE command with cmd[2] == 0x1c
  688. *
  689. * Setup srb and scsi status so this won't be fatal.
  690. * We do this so we can distinguish truly fatal failues
  691. * (srb status == 0x4) and off-line the device in that case.
  692. */
  693. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  694. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  695. vstor_packet->vm_srb.scsi_status = 0;
  696. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  697. }
  698. /* Copy over the status...etc */
  699. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  700. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  701. stor_pkt->vm_srb.sense_info_length =
  702. vstor_packet->vm_srb.sense_info_length;
  703. if (vstor_packet->vm_srb.scsi_status != 0 ||
  704. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
  705. dev_warn(&device->device,
  706. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  707. stor_pkt->vm_srb.cdb[0],
  708. vstor_packet->vm_srb.scsi_status,
  709. vstor_packet->vm_srb.srb_status);
  710. }
  711. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  712. /* CHECK_CONDITION */
  713. if (vstor_packet->vm_srb.srb_status &
  714. SRB_STATUS_AUTOSENSE_VALID) {
  715. /* autosense data available */
  716. dev_warn(&device->device,
  717. "stor pkt %p autosense data valid - len %d\n",
  718. request,
  719. vstor_packet->vm_srb.sense_info_length);
  720. memcpy(request->sense_buffer,
  721. vstor_packet->vm_srb.sense_data,
  722. vstor_packet->vm_srb.sense_info_length);
  723. }
  724. }
  725. stor_pkt->vm_srb.data_transfer_length =
  726. vstor_packet->vm_srb.data_transfer_length;
  727. storvsc_command_completion(request);
  728. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  729. stor_device->drain_notify)
  730. wake_up(&stor_device->waiting_to_drain);
  731. }
  732. static void storvsc_on_receive(struct hv_device *device,
  733. struct vstor_packet *vstor_packet,
  734. struct storvsc_cmd_request *request)
  735. {
  736. struct storvsc_scan_work *work;
  737. struct storvsc_device *stor_device;
  738. switch (vstor_packet->operation) {
  739. case VSTOR_OPERATION_COMPLETE_IO:
  740. storvsc_on_io_completion(device, vstor_packet, request);
  741. break;
  742. case VSTOR_OPERATION_REMOVE_DEVICE:
  743. case VSTOR_OPERATION_ENUMERATE_BUS:
  744. stor_device = get_in_stor_device(device);
  745. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  746. if (!work)
  747. return;
  748. INIT_WORK(&work->work, storvsc_bus_scan);
  749. work->host = stor_device->host;
  750. schedule_work(&work->work);
  751. break;
  752. default:
  753. break;
  754. }
  755. }
  756. static void storvsc_on_channel_callback(void *context)
  757. {
  758. struct hv_device *device = (struct hv_device *)context;
  759. struct storvsc_device *stor_device;
  760. u32 bytes_recvd;
  761. u64 request_id;
  762. unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
  763. struct storvsc_cmd_request *request;
  764. int ret;
  765. stor_device = get_in_stor_device(device);
  766. if (!stor_device)
  767. return;
  768. do {
  769. ret = vmbus_recvpacket(device->channel, packet,
  770. ALIGN(sizeof(struct vstor_packet), 8),
  771. &bytes_recvd, &request_id);
  772. if (ret == 0 && bytes_recvd > 0) {
  773. request = (struct storvsc_cmd_request *)
  774. (unsigned long)request_id;
  775. if ((request == &stor_device->init_request) ||
  776. (request == &stor_device->reset_request)) {
  777. memcpy(&request->vstor_packet, packet,
  778. sizeof(struct vstor_packet));
  779. complete(&request->wait_event);
  780. } else {
  781. storvsc_on_receive(device,
  782. (struct vstor_packet *)packet,
  783. request);
  784. }
  785. } else {
  786. break;
  787. }
  788. } while (1);
  789. return;
  790. }
  791. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
  792. {
  793. struct vmstorage_channel_properties props;
  794. int ret;
  795. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  796. ret = vmbus_open(device->channel,
  797. ring_size,
  798. ring_size,
  799. (void *)&props,
  800. sizeof(struct vmstorage_channel_properties),
  801. storvsc_on_channel_callback, device);
  802. if (ret != 0)
  803. return ret;
  804. ret = storvsc_channel_init(device);
  805. return ret;
  806. }
  807. static int storvsc_dev_remove(struct hv_device *device)
  808. {
  809. struct storvsc_device *stor_device;
  810. unsigned long flags;
  811. stor_device = hv_get_drvdata(device);
  812. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  813. stor_device->destroy = true;
  814. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  815. /*
  816. * At this point, all outbound traffic should be disable. We
  817. * only allow inbound traffic (responses) to proceed so that
  818. * outstanding requests can be completed.
  819. */
  820. storvsc_wait_to_drain(stor_device);
  821. /*
  822. * Since we have already drained, we don't need to busy wait
  823. * as was done in final_release_stor_device()
  824. * Note that we cannot set the ext pointer to NULL until
  825. * we have drained - to drain the outgoing packets, we need to
  826. * allow incoming packets.
  827. */
  828. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  829. hv_set_drvdata(device, NULL);
  830. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  831. /* Close the channel */
  832. vmbus_close(device->channel);
  833. kfree(stor_device);
  834. return 0;
  835. }
  836. static int storvsc_do_io(struct hv_device *device,
  837. struct storvsc_cmd_request *request)
  838. {
  839. struct storvsc_device *stor_device;
  840. struct vstor_packet *vstor_packet;
  841. int ret = 0;
  842. vstor_packet = &request->vstor_packet;
  843. stor_device = get_out_stor_device(device);
  844. if (!stor_device)
  845. return -ENODEV;
  846. request->device = device;
  847. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  848. vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
  849. vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
  850. vstor_packet->vm_srb.data_transfer_length =
  851. request->data_buffer.len;
  852. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  853. if (request->data_buffer.len) {
  854. ret = vmbus_sendpacket_multipagebuffer(device->channel,
  855. &request->data_buffer,
  856. vstor_packet,
  857. sizeof(struct vstor_packet),
  858. (unsigned long)request);
  859. } else {
  860. ret = vmbus_sendpacket(device->channel, vstor_packet,
  861. sizeof(struct vstor_packet),
  862. (unsigned long)request,
  863. VM_PKT_DATA_INBAND,
  864. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  865. }
  866. if (ret != 0)
  867. return ret;
  868. atomic_inc(&stor_device->num_outstanding_req);
  869. return ret;
  870. }
  871. static int storvsc_device_alloc(struct scsi_device *sdevice)
  872. {
  873. struct stor_mem_pools *memp;
  874. int number = STORVSC_MIN_BUF_NR;
  875. memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
  876. if (!memp)
  877. return -ENOMEM;
  878. memp->request_pool =
  879. kmem_cache_create(dev_name(&sdevice->sdev_dev),
  880. sizeof(struct storvsc_cmd_request), 0,
  881. SLAB_HWCACHE_ALIGN, NULL);
  882. if (!memp->request_pool)
  883. goto err0;
  884. memp->request_mempool = mempool_create(number, mempool_alloc_slab,
  885. mempool_free_slab,
  886. memp->request_pool);
  887. if (!memp->request_mempool)
  888. goto err1;
  889. sdevice->hostdata = memp;
  890. return 0;
  891. err1:
  892. kmem_cache_destroy(memp->request_pool);
  893. err0:
  894. kfree(memp);
  895. return -ENOMEM;
  896. }
  897. static void storvsc_device_destroy(struct scsi_device *sdevice)
  898. {
  899. struct stor_mem_pools *memp = sdevice->hostdata;
  900. mempool_destroy(memp->request_mempool);
  901. kmem_cache_destroy(memp->request_pool);
  902. kfree(memp);
  903. sdevice->hostdata = NULL;
  904. }
  905. static int storvsc_device_configure(struct scsi_device *sdevice)
  906. {
  907. scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
  908. STORVSC_MAX_IO_REQUESTS);
  909. blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
  910. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  911. return 0;
  912. }
  913. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  914. sector_t capacity, int *info)
  915. {
  916. sector_t nsect = capacity;
  917. sector_t cylinders = nsect;
  918. int heads, sectors_pt;
  919. /*
  920. * We are making up these values; let us keep it simple.
  921. */
  922. heads = 0xff;
  923. sectors_pt = 0x3f; /* Sectors per track */
  924. sector_div(cylinders, heads * sectors_pt);
  925. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  926. cylinders = 0xffff;
  927. info[0] = heads;
  928. info[1] = sectors_pt;
  929. info[2] = (int)cylinders;
  930. return 0;
  931. }
  932. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  933. {
  934. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  935. struct hv_device *device = host_dev->dev;
  936. struct storvsc_device *stor_device;
  937. struct storvsc_cmd_request *request;
  938. struct vstor_packet *vstor_packet;
  939. int ret, t;
  940. stor_device = get_out_stor_device(device);
  941. if (!stor_device)
  942. return FAILED;
  943. request = &stor_device->reset_request;
  944. vstor_packet = &request->vstor_packet;
  945. init_completion(&request->wait_event);
  946. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  947. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  948. vstor_packet->vm_srb.path_id = stor_device->path_id;
  949. ret = vmbus_sendpacket(device->channel, vstor_packet,
  950. sizeof(struct vstor_packet),
  951. (unsigned long)&stor_device->reset_request,
  952. VM_PKT_DATA_INBAND,
  953. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  954. if (ret != 0)
  955. return FAILED;
  956. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  957. if (t == 0)
  958. return TIMEOUT_ERROR;
  959. /*
  960. * At this point, all outstanding requests in the adapter
  961. * should have been flushed out and return to us
  962. */
  963. return SUCCESS;
  964. }
  965. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  966. {
  967. bool allowed = true;
  968. u8 scsi_op = scmnd->cmnd[0];
  969. switch (scsi_op) {
  970. /*
  971. * smartd sends this command and the host does not handle
  972. * this. So, don't send it.
  973. */
  974. case SET_WINDOW:
  975. scmnd->result = ILLEGAL_REQUEST << 16;
  976. allowed = false;
  977. break;
  978. default:
  979. break;
  980. }
  981. return allowed;
  982. }
  983. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  984. {
  985. int ret;
  986. struct hv_host_device *host_dev = shost_priv(host);
  987. struct hv_device *dev = host_dev->dev;
  988. struct storvsc_cmd_request *cmd_request;
  989. unsigned int request_size = 0;
  990. int i;
  991. struct scatterlist *sgl;
  992. unsigned int sg_count = 0;
  993. struct vmscsi_request *vm_srb;
  994. struct stor_mem_pools *memp = scmnd->device->hostdata;
  995. if (!storvsc_scsi_cmd_ok(scmnd)) {
  996. scmnd->scsi_done(scmnd);
  997. return 0;
  998. }
  999. request_size = sizeof(struct storvsc_cmd_request);
  1000. cmd_request = mempool_alloc(memp->request_mempool,
  1001. GFP_ATOMIC);
  1002. /*
  1003. * We might be invoked in an interrupt context; hence
  1004. * mempool_alloc() can fail.
  1005. */
  1006. if (!cmd_request)
  1007. return SCSI_MLQUEUE_DEVICE_BUSY;
  1008. memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
  1009. /* Setup the cmd request */
  1010. cmd_request->cmd = scmnd;
  1011. scmnd->host_scribble = (unsigned char *)cmd_request;
  1012. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1013. /* Build the SRB */
  1014. switch (scmnd->sc_data_direction) {
  1015. case DMA_TO_DEVICE:
  1016. vm_srb->data_in = WRITE_TYPE;
  1017. break;
  1018. case DMA_FROM_DEVICE:
  1019. vm_srb->data_in = READ_TYPE;
  1020. break;
  1021. default:
  1022. vm_srb->data_in = UNKNOWN_TYPE;
  1023. break;
  1024. }
  1025. vm_srb->port_number = host_dev->port;
  1026. vm_srb->path_id = scmnd->device->channel;
  1027. vm_srb->target_id = scmnd->device->id;
  1028. vm_srb->lun = scmnd->device->lun;
  1029. vm_srb->cdb_length = scmnd->cmd_len;
  1030. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1031. cmd_request->sense_buffer = scmnd->sense_buffer;
  1032. cmd_request->data_buffer.len = scsi_bufflen(scmnd);
  1033. if (scsi_sg_count(scmnd)) {
  1034. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1035. sg_count = scsi_sg_count(scmnd);
  1036. /* check if we need to bounce the sgl */
  1037. if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
  1038. cmd_request->bounce_sgl =
  1039. create_bounce_buffer(sgl, scsi_sg_count(scmnd),
  1040. scsi_bufflen(scmnd),
  1041. vm_srb->data_in);
  1042. if (!cmd_request->bounce_sgl) {
  1043. ret = SCSI_MLQUEUE_HOST_BUSY;
  1044. goto queue_error;
  1045. }
  1046. cmd_request->bounce_sgl_count =
  1047. ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
  1048. PAGE_SHIFT;
  1049. if (vm_srb->data_in == WRITE_TYPE)
  1050. copy_to_bounce_buffer(sgl,
  1051. cmd_request->bounce_sgl,
  1052. scsi_sg_count(scmnd));
  1053. sgl = cmd_request->bounce_sgl;
  1054. sg_count = cmd_request->bounce_sgl_count;
  1055. }
  1056. cmd_request->data_buffer.offset = sgl[0].offset;
  1057. for (i = 0; i < sg_count; i++)
  1058. cmd_request->data_buffer.pfn_array[i] =
  1059. page_to_pfn(sg_page((&sgl[i])));
  1060. } else if (scsi_sglist(scmnd)) {
  1061. cmd_request->data_buffer.offset =
  1062. virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
  1063. cmd_request->data_buffer.pfn_array[0] =
  1064. virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
  1065. }
  1066. /* Invokes the vsc to start an IO */
  1067. ret = storvsc_do_io(dev, cmd_request);
  1068. if (ret == -EAGAIN) {
  1069. /* no more space */
  1070. if (cmd_request->bounce_sgl_count) {
  1071. destroy_bounce_buffer(cmd_request->bounce_sgl,
  1072. cmd_request->bounce_sgl_count);
  1073. ret = SCSI_MLQUEUE_DEVICE_BUSY;
  1074. goto queue_error;
  1075. }
  1076. }
  1077. return 0;
  1078. queue_error:
  1079. mempool_free(cmd_request, memp->request_mempool);
  1080. scmnd->host_scribble = NULL;
  1081. return ret;
  1082. }
  1083. static struct scsi_host_template scsi_driver = {
  1084. .module = THIS_MODULE,
  1085. .name = "storvsc_host_t",
  1086. .bios_param = storvsc_get_chs,
  1087. .queuecommand = storvsc_queuecommand,
  1088. .eh_host_reset_handler = storvsc_host_reset_handler,
  1089. .slave_alloc = storvsc_device_alloc,
  1090. .slave_destroy = storvsc_device_destroy,
  1091. .slave_configure = storvsc_device_configure,
  1092. .cmd_per_lun = 1,
  1093. /* 64 max_queue * 1 target */
  1094. .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
  1095. .this_id = -1,
  1096. /* no use setting to 0 since ll_blk_rw reset it to 1 */
  1097. /* currently 32 */
  1098. .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
  1099. .use_clustering = DISABLE_CLUSTERING,
  1100. /* Make sure we dont get a sg segment crosses a page boundary */
  1101. .dma_boundary = PAGE_SIZE-1,
  1102. };
  1103. enum {
  1104. SCSI_GUID,
  1105. IDE_GUID,
  1106. };
  1107. static const struct hv_vmbus_device_id id_table[] = {
  1108. /* SCSI guid */
  1109. { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
  1110. 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  1111. .driver_data = SCSI_GUID },
  1112. /* IDE guid */
  1113. { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
  1114. 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  1115. .driver_data = IDE_GUID },
  1116. { },
  1117. };
  1118. MODULE_DEVICE_TABLE(vmbus, id_table);
  1119. static int storvsc_probe(struct hv_device *device,
  1120. const struct hv_vmbus_device_id *dev_id)
  1121. {
  1122. int ret;
  1123. struct Scsi_Host *host;
  1124. struct hv_host_device *host_dev;
  1125. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1126. int target = 0;
  1127. struct storvsc_device *stor_device;
  1128. host = scsi_host_alloc(&scsi_driver,
  1129. sizeof(struct hv_host_device));
  1130. if (!host)
  1131. return -ENOMEM;
  1132. host_dev = shost_priv(host);
  1133. memset(host_dev, 0, sizeof(struct hv_host_device));
  1134. host_dev->port = host->host_no;
  1135. host_dev->dev = device;
  1136. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1137. if (!stor_device) {
  1138. ret = -ENOMEM;
  1139. goto err_out0;
  1140. }
  1141. stor_device->destroy = false;
  1142. init_waitqueue_head(&stor_device->waiting_to_drain);
  1143. stor_device->device = device;
  1144. stor_device->host = host;
  1145. hv_set_drvdata(device, stor_device);
  1146. stor_device->port_number = host->host_no;
  1147. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
  1148. if (ret)
  1149. goto err_out1;
  1150. host_dev->path = stor_device->path_id;
  1151. host_dev->target = stor_device->target_id;
  1152. /* max # of devices per target */
  1153. host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
  1154. /* max # of targets per channel */
  1155. host->max_id = STORVSC_MAX_TARGETS;
  1156. /* max # of channels */
  1157. host->max_channel = STORVSC_MAX_CHANNELS - 1;
  1158. /* max cmd length */
  1159. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1160. /* Register the HBA and start the scsi bus scan */
  1161. ret = scsi_add_host(host, &device->device);
  1162. if (ret != 0)
  1163. goto err_out2;
  1164. if (!dev_is_ide) {
  1165. scsi_scan_host(host);
  1166. } else {
  1167. target = (device->dev_instance.b[5] << 8 |
  1168. device->dev_instance.b[4]);
  1169. ret = scsi_add_device(host, 0, target, 0);
  1170. if (ret) {
  1171. scsi_remove_host(host);
  1172. goto err_out2;
  1173. }
  1174. }
  1175. return 0;
  1176. err_out2:
  1177. /*
  1178. * Once we have connected with the host, we would need to
  1179. * to invoke storvsc_dev_remove() to rollback this state and
  1180. * this call also frees up the stor_device; hence the jump around
  1181. * err_out1 label.
  1182. */
  1183. storvsc_dev_remove(device);
  1184. goto err_out0;
  1185. err_out1:
  1186. kfree(stor_device);
  1187. err_out0:
  1188. scsi_host_put(host);
  1189. return ret;
  1190. }
  1191. static int storvsc_remove(struct hv_device *dev)
  1192. {
  1193. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1194. struct Scsi_Host *host = stor_device->host;
  1195. scsi_remove_host(host);
  1196. storvsc_dev_remove(dev);
  1197. scsi_host_put(host);
  1198. return 0;
  1199. }
  1200. static struct hv_driver storvsc_drv = {
  1201. .name = KBUILD_MODNAME,
  1202. .id_table = id_table,
  1203. .probe = storvsc_probe,
  1204. .remove = storvsc_remove,
  1205. };
  1206. static int __init storvsc_drv_init(void)
  1207. {
  1208. u32 max_outstanding_req_per_channel;
  1209. /*
  1210. * Divide the ring buffer data size (which is 1 page less
  1211. * than the ring buffer size since that page is reserved for
  1212. * the ring buffer indices) by the max request size (which is
  1213. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1214. */
  1215. max_outstanding_req_per_channel =
  1216. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1217. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1218. sizeof(struct vstor_packet) + sizeof(u64),
  1219. sizeof(u64)));
  1220. if (max_outstanding_req_per_channel <
  1221. STORVSC_MAX_IO_REQUESTS)
  1222. return -EINVAL;
  1223. return vmbus_driver_register(&storvsc_drv);
  1224. }
  1225. static void __exit storvsc_drv_exit(void)
  1226. {
  1227. vmbus_driver_unregister(&storvsc_drv);
  1228. }
  1229. MODULE_LICENSE("GPL");
  1230. MODULE_VERSION(HV_DRV_VERSION);
  1231. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1232. module_init(storvsc_drv_init);
  1233. module_exit(storvsc_drv_exit);