storvsc_drv.c 38 KB

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