storvsc_drv.c 39 KB

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