storvsc_drv.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. j++;
  498. /* if we need to use another bounce buffer */
  499. if (srclen || i != orig_sgl_count - 1)
  500. bounce_addr = sg_kmap_atomic(bounce_sgl,j);
  501. } else if (srclen == 0 && i == orig_sgl_count - 1) {
  502. /* unmap the last bounce that is < PAGE_SIZE */
  503. sg_kunmap_atomic(bounce_addr);
  504. }
  505. }
  506. sg_kunmap_atomic(src_addr - orig_sgl[i].offset);
  507. }
  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. However, if the command
  637. * were a pass-through command deal with it appropriately.
  638. */
  639. scmnd->result = vm_srb->scsi_status;
  640. if (vm_srb->srb_status == SRB_STATUS_ERROR) {
  641. switch (scmnd->cmnd[0]) {
  642. case ATA_16:
  643. case ATA_12:
  644. set_host_byte(scmnd, DID_PASSTHROUGH);
  645. break;
  646. default:
  647. set_host_byte(scmnd, DID_TARGET_FAILURE);
  648. }
  649. }
  650. /*
  651. * If the LUN is invalid; remove the device.
  652. */
  653. if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
  654. struct storvsc_device *stor_dev;
  655. struct hv_device *dev = host_dev->dev;
  656. struct Scsi_Host *host;
  657. stor_dev = get_in_stor_device(dev);
  658. host = stor_dev->host;
  659. wrk = kmalloc(sizeof(struct storvsc_scan_work),
  660. GFP_ATOMIC);
  661. if (!wrk) {
  662. scmnd->result = DID_TARGET_FAILURE << 16;
  663. } else {
  664. wrk->host = host;
  665. wrk->lun = vm_srb->lun;
  666. INIT_WORK(&wrk->work, storvsc_remove_lun);
  667. schedule_work(&wrk->work);
  668. }
  669. }
  670. if (scmnd->result) {
  671. if (scsi_normalize_sense(scmnd->sense_buffer,
  672. SCSI_SENSE_BUFFERSIZE, &sense_hdr))
  673. scsi_print_sense_hdr("storvsc", &sense_hdr);
  674. }
  675. scsi_set_resid(scmnd,
  676. cmd_request->data_buffer.len -
  677. vm_srb->data_transfer_length);
  678. scsi_done_fn = scmnd->scsi_done;
  679. scmnd->host_scribble = NULL;
  680. scmnd->scsi_done = NULL;
  681. scsi_done_fn(scmnd);
  682. mempool_free(cmd_request, memp->request_mempool);
  683. }
  684. static void storvsc_on_io_completion(struct hv_device *device,
  685. struct vstor_packet *vstor_packet,
  686. struct storvsc_cmd_request *request)
  687. {
  688. struct storvsc_device *stor_device;
  689. struct vstor_packet *stor_pkt;
  690. stor_device = hv_get_drvdata(device);
  691. stor_pkt = &request->vstor_packet;
  692. /*
  693. * The current SCSI handling on the host side does
  694. * not correctly handle:
  695. * INQUIRY command with page code parameter set to 0x80
  696. * MODE_SENSE command with cmd[2] == 0x1c
  697. *
  698. * Setup srb and scsi status so this won't be fatal.
  699. * We do this so we can distinguish truly fatal failues
  700. * (srb status == 0x4) and off-line the device in that case.
  701. */
  702. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  703. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  704. vstor_packet->vm_srb.scsi_status = 0;
  705. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  706. }
  707. /* Copy over the status...etc */
  708. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  709. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  710. stor_pkt->vm_srb.sense_info_length =
  711. vstor_packet->vm_srb.sense_info_length;
  712. if (vstor_packet->vm_srb.scsi_status != 0 ||
  713. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
  714. dev_warn(&device->device,
  715. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  716. stor_pkt->vm_srb.cdb[0],
  717. vstor_packet->vm_srb.scsi_status,
  718. vstor_packet->vm_srb.srb_status);
  719. }
  720. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  721. /* CHECK_CONDITION */
  722. if (vstor_packet->vm_srb.srb_status &
  723. SRB_STATUS_AUTOSENSE_VALID) {
  724. /* autosense data available */
  725. dev_warn(&device->device,
  726. "stor pkt %p autosense data valid - len %d\n",
  727. request,
  728. vstor_packet->vm_srb.sense_info_length);
  729. memcpy(request->sense_buffer,
  730. vstor_packet->vm_srb.sense_data,
  731. vstor_packet->vm_srb.sense_info_length);
  732. }
  733. }
  734. stor_pkt->vm_srb.data_transfer_length =
  735. vstor_packet->vm_srb.data_transfer_length;
  736. storvsc_command_completion(request);
  737. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  738. stor_device->drain_notify)
  739. wake_up(&stor_device->waiting_to_drain);
  740. }
  741. static void storvsc_on_receive(struct hv_device *device,
  742. struct vstor_packet *vstor_packet,
  743. struct storvsc_cmd_request *request)
  744. {
  745. struct storvsc_scan_work *work;
  746. struct storvsc_device *stor_device;
  747. switch (vstor_packet->operation) {
  748. case VSTOR_OPERATION_COMPLETE_IO:
  749. storvsc_on_io_completion(device, vstor_packet, request);
  750. break;
  751. case VSTOR_OPERATION_REMOVE_DEVICE:
  752. case VSTOR_OPERATION_ENUMERATE_BUS:
  753. stor_device = get_in_stor_device(device);
  754. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  755. if (!work)
  756. return;
  757. INIT_WORK(&work->work, storvsc_bus_scan);
  758. work->host = stor_device->host;
  759. schedule_work(&work->work);
  760. break;
  761. default:
  762. break;
  763. }
  764. }
  765. static void storvsc_on_channel_callback(void *context)
  766. {
  767. struct hv_device *device = (struct hv_device *)context;
  768. struct storvsc_device *stor_device;
  769. u32 bytes_recvd;
  770. u64 request_id;
  771. unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
  772. struct storvsc_cmd_request *request;
  773. int ret;
  774. stor_device = get_in_stor_device(device);
  775. if (!stor_device)
  776. return;
  777. do {
  778. ret = vmbus_recvpacket(device->channel, packet,
  779. ALIGN(sizeof(struct vstor_packet), 8),
  780. &bytes_recvd, &request_id);
  781. if (ret == 0 && bytes_recvd > 0) {
  782. request = (struct storvsc_cmd_request *)
  783. (unsigned long)request_id;
  784. if ((request == &stor_device->init_request) ||
  785. (request == &stor_device->reset_request)) {
  786. memcpy(&request->vstor_packet, packet,
  787. sizeof(struct vstor_packet));
  788. complete(&request->wait_event);
  789. } else {
  790. storvsc_on_receive(device,
  791. (struct vstor_packet *)packet,
  792. request);
  793. }
  794. } else {
  795. break;
  796. }
  797. } while (1);
  798. return;
  799. }
  800. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
  801. {
  802. struct vmstorage_channel_properties props;
  803. int ret;
  804. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  805. ret = vmbus_open(device->channel,
  806. ring_size,
  807. ring_size,
  808. (void *)&props,
  809. sizeof(struct vmstorage_channel_properties),
  810. storvsc_on_channel_callback, device);
  811. if (ret != 0)
  812. return ret;
  813. ret = storvsc_channel_init(device);
  814. return ret;
  815. }
  816. static int storvsc_dev_remove(struct hv_device *device)
  817. {
  818. struct storvsc_device *stor_device;
  819. unsigned long flags;
  820. stor_device = hv_get_drvdata(device);
  821. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  822. stor_device->destroy = true;
  823. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  824. /*
  825. * At this point, all outbound traffic should be disable. We
  826. * only allow inbound traffic (responses) to proceed so that
  827. * outstanding requests can be completed.
  828. */
  829. storvsc_wait_to_drain(stor_device);
  830. /*
  831. * Since we have already drained, we don't need to busy wait
  832. * as was done in final_release_stor_device()
  833. * Note that we cannot set the ext pointer to NULL until
  834. * we have drained - to drain the outgoing packets, we need to
  835. * allow incoming packets.
  836. */
  837. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  838. hv_set_drvdata(device, NULL);
  839. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  840. /* Close the channel */
  841. vmbus_close(device->channel);
  842. kfree(stor_device);
  843. return 0;
  844. }
  845. static int storvsc_do_io(struct hv_device *device,
  846. struct storvsc_cmd_request *request)
  847. {
  848. struct storvsc_device *stor_device;
  849. struct vstor_packet *vstor_packet;
  850. int ret = 0;
  851. vstor_packet = &request->vstor_packet;
  852. stor_device = get_out_stor_device(device);
  853. if (!stor_device)
  854. return -ENODEV;
  855. request->device = device;
  856. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  857. vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
  858. vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
  859. vstor_packet->vm_srb.data_transfer_length =
  860. request->data_buffer.len;
  861. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  862. if (request->data_buffer.len) {
  863. ret = vmbus_sendpacket_multipagebuffer(device->channel,
  864. &request->data_buffer,
  865. vstor_packet,
  866. sizeof(struct vstor_packet),
  867. (unsigned long)request);
  868. } else {
  869. ret = vmbus_sendpacket(device->channel, vstor_packet,
  870. sizeof(struct vstor_packet),
  871. (unsigned long)request,
  872. VM_PKT_DATA_INBAND,
  873. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  874. }
  875. if (ret != 0)
  876. return ret;
  877. atomic_inc(&stor_device->num_outstanding_req);
  878. return ret;
  879. }
  880. static int storvsc_device_alloc(struct scsi_device *sdevice)
  881. {
  882. struct stor_mem_pools *memp;
  883. int number = STORVSC_MIN_BUF_NR;
  884. memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
  885. if (!memp)
  886. return -ENOMEM;
  887. memp->request_pool =
  888. kmem_cache_create(dev_name(&sdevice->sdev_dev),
  889. sizeof(struct storvsc_cmd_request), 0,
  890. SLAB_HWCACHE_ALIGN, NULL);
  891. if (!memp->request_pool)
  892. goto err0;
  893. memp->request_mempool = mempool_create(number, mempool_alloc_slab,
  894. mempool_free_slab,
  895. memp->request_pool);
  896. if (!memp->request_mempool)
  897. goto err1;
  898. sdevice->hostdata = memp;
  899. return 0;
  900. err1:
  901. kmem_cache_destroy(memp->request_pool);
  902. err0:
  903. kfree(memp);
  904. return -ENOMEM;
  905. }
  906. static void storvsc_device_destroy(struct scsi_device *sdevice)
  907. {
  908. struct stor_mem_pools *memp = sdevice->hostdata;
  909. mempool_destroy(memp->request_mempool);
  910. kmem_cache_destroy(memp->request_pool);
  911. kfree(memp);
  912. sdevice->hostdata = NULL;
  913. }
  914. static int storvsc_device_configure(struct scsi_device *sdevice)
  915. {
  916. scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
  917. STORVSC_MAX_IO_REQUESTS);
  918. blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
  919. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  920. return 0;
  921. }
  922. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  923. sector_t capacity, int *info)
  924. {
  925. sector_t nsect = capacity;
  926. sector_t cylinders = nsect;
  927. int heads, sectors_pt;
  928. /*
  929. * We are making up these values; let us keep it simple.
  930. */
  931. heads = 0xff;
  932. sectors_pt = 0x3f; /* Sectors per track */
  933. sector_div(cylinders, heads * sectors_pt);
  934. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  935. cylinders = 0xffff;
  936. info[0] = heads;
  937. info[1] = sectors_pt;
  938. info[2] = (int)cylinders;
  939. return 0;
  940. }
  941. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  942. {
  943. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  944. struct hv_device *device = host_dev->dev;
  945. struct storvsc_device *stor_device;
  946. struct storvsc_cmd_request *request;
  947. struct vstor_packet *vstor_packet;
  948. int ret, t;
  949. stor_device = get_out_stor_device(device);
  950. if (!stor_device)
  951. return FAILED;
  952. request = &stor_device->reset_request;
  953. vstor_packet = &request->vstor_packet;
  954. init_completion(&request->wait_event);
  955. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  956. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  957. vstor_packet->vm_srb.path_id = stor_device->path_id;
  958. ret = vmbus_sendpacket(device->channel, vstor_packet,
  959. sizeof(struct vstor_packet),
  960. (unsigned long)&stor_device->reset_request,
  961. VM_PKT_DATA_INBAND,
  962. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  963. if (ret != 0)
  964. return FAILED;
  965. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  966. if (t == 0)
  967. return TIMEOUT_ERROR;
  968. /*
  969. * At this point, all outstanding requests in the adapter
  970. * should have been flushed out and return to us
  971. * There is a potential race here where the host may be in
  972. * the process of responding when we return from here.
  973. * Just wait for all in-transit packets to be accounted for
  974. * before we return from here.
  975. */
  976. storvsc_wait_to_drain(stor_device);
  977. return SUCCESS;
  978. }
  979. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  980. {
  981. bool allowed = true;
  982. u8 scsi_op = scmnd->cmnd[0];
  983. switch (scsi_op) {
  984. /*
  985. * smartd sends this command and the host does not handle
  986. * this. So, don't send it.
  987. */
  988. case SET_WINDOW:
  989. scmnd->result = ILLEGAL_REQUEST << 16;
  990. allowed = false;
  991. break;
  992. default:
  993. break;
  994. }
  995. return allowed;
  996. }
  997. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  998. {
  999. int ret;
  1000. struct hv_host_device *host_dev = shost_priv(host);
  1001. struct hv_device *dev = host_dev->dev;
  1002. struct storvsc_cmd_request *cmd_request;
  1003. unsigned int request_size = 0;
  1004. int i;
  1005. struct scatterlist *sgl;
  1006. unsigned int sg_count = 0;
  1007. struct vmscsi_request *vm_srb;
  1008. struct stor_mem_pools *memp = scmnd->device->hostdata;
  1009. if (!storvsc_scsi_cmd_ok(scmnd)) {
  1010. scmnd->scsi_done(scmnd);
  1011. return 0;
  1012. }
  1013. request_size = sizeof(struct storvsc_cmd_request);
  1014. cmd_request = mempool_alloc(memp->request_mempool,
  1015. GFP_ATOMIC);
  1016. /*
  1017. * We might be invoked in an interrupt context; hence
  1018. * mempool_alloc() can fail.
  1019. */
  1020. if (!cmd_request)
  1021. return SCSI_MLQUEUE_DEVICE_BUSY;
  1022. memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
  1023. /* Setup the cmd request */
  1024. cmd_request->cmd = scmnd;
  1025. scmnd->host_scribble = (unsigned char *)cmd_request;
  1026. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1027. /* Build the SRB */
  1028. switch (scmnd->sc_data_direction) {
  1029. case DMA_TO_DEVICE:
  1030. vm_srb->data_in = WRITE_TYPE;
  1031. break;
  1032. case DMA_FROM_DEVICE:
  1033. vm_srb->data_in = READ_TYPE;
  1034. break;
  1035. default:
  1036. vm_srb->data_in = UNKNOWN_TYPE;
  1037. break;
  1038. }
  1039. vm_srb->port_number = host_dev->port;
  1040. vm_srb->path_id = scmnd->device->channel;
  1041. vm_srb->target_id = scmnd->device->id;
  1042. vm_srb->lun = scmnd->device->lun;
  1043. vm_srb->cdb_length = scmnd->cmd_len;
  1044. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1045. cmd_request->sense_buffer = scmnd->sense_buffer;
  1046. cmd_request->data_buffer.len = scsi_bufflen(scmnd);
  1047. if (scsi_sg_count(scmnd)) {
  1048. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1049. sg_count = scsi_sg_count(scmnd);
  1050. /* check if we need to bounce the sgl */
  1051. if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
  1052. cmd_request->bounce_sgl =
  1053. create_bounce_buffer(sgl, scsi_sg_count(scmnd),
  1054. scsi_bufflen(scmnd),
  1055. vm_srb->data_in);
  1056. if (!cmd_request->bounce_sgl) {
  1057. ret = SCSI_MLQUEUE_HOST_BUSY;
  1058. goto queue_error;
  1059. }
  1060. cmd_request->bounce_sgl_count =
  1061. ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
  1062. PAGE_SHIFT;
  1063. if (vm_srb->data_in == WRITE_TYPE)
  1064. copy_to_bounce_buffer(sgl,
  1065. cmd_request->bounce_sgl,
  1066. scsi_sg_count(scmnd));
  1067. sgl = cmd_request->bounce_sgl;
  1068. sg_count = cmd_request->bounce_sgl_count;
  1069. }
  1070. cmd_request->data_buffer.offset = sgl[0].offset;
  1071. for (i = 0; i < sg_count; i++)
  1072. cmd_request->data_buffer.pfn_array[i] =
  1073. page_to_pfn(sg_page((&sgl[i])));
  1074. } else if (scsi_sglist(scmnd)) {
  1075. cmd_request->data_buffer.offset =
  1076. virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
  1077. cmd_request->data_buffer.pfn_array[0] =
  1078. virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
  1079. }
  1080. /* Invokes the vsc to start an IO */
  1081. ret = storvsc_do_io(dev, cmd_request);
  1082. if (ret == -EAGAIN) {
  1083. /* no more space */
  1084. if (cmd_request->bounce_sgl_count) {
  1085. destroy_bounce_buffer(cmd_request->bounce_sgl,
  1086. cmd_request->bounce_sgl_count);
  1087. ret = SCSI_MLQUEUE_DEVICE_BUSY;
  1088. goto queue_error;
  1089. }
  1090. }
  1091. return 0;
  1092. queue_error:
  1093. mempool_free(cmd_request, memp->request_mempool);
  1094. scmnd->host_scribble = NULL;
  1095. return ret;
  1096. }
  1097. static struct scsi_host_template scsi_driver = {
  1098. .module = THIS_MODULE,
  1099. .name = "storvsc_host_t",
  1100. .bios_param = storvsc_get_chs,
  1101. .queuecommand = storvsc_queuecommand,
  1102. .eh_host_reset_handler = storvsc_host_reset_handler,
  1103. .slave_alloc = storvsc_device_alloc,
  1104. .slave_destroy = storvsc_device_destroy,
  1105. .slave_configure = storvsc_device_configure,
  1106. .cmd_per_lun = 1,
  1107. /* 64 max_queue * 1 target */
  1108. .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
  1109. .this_id = -1,
  1110. /* no use setting to 0 since ll_blk_rw reset it to 1 */
  1111. /* currently 32 */
  1112. .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
  1113. .use_clustering = DISABLE_CLUSTERING,
  1114. /* Make sure we dont get a sg segment crosses a page boundary */
  1115. .dma_boundary = PAGE_SIZE-1,
  1116. };
  1117. enum {
  1118. SCSI_GUID,
  1119. IDE_GUID,
  1120. };
  1121. static const struct hv_vmbus_device_id id_table[] = {
  1122. /* SCSI guid */
  1123. { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
  1124. 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
  1125. .driver_data = SCSI_GUID },
  1126. /* IDE guid */
  1127. { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
  1128. 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
  1129. .driver_data = IDE_GUID },
  1130. { },
  1131. };
  1132. MODULE_DEVICE_TABLE(vmbus, id_table);
  1133. static int storvsc_probe(struct hv_device *device,
  1134. const struct hv_vmbus_device_id *dev_id)
  1135. {
  1136. int ret;
  1137. struct Scsi_Host *host;
  1138. struct hv_host_device *host_dev;
  1139. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1140. int target = 0;
  1141. struct storvsc_device *stor_device;
  1142. host = scsi_host_alloc(&scsi_driver,
  1143. sizeof(struct hv_host_device));
  1144. if (!host)
  1145. return -ENOMEM;
  1146. host_dev = shost_priv(host);
  1147. memset(host_dev, 0, sizeof(struct hv_host_device));
  1148. host_dev->port = host->host_no;
  1149. host_dev->dev = device;
  1150. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1151. if (!stor_device) {
  1152. ret = -ENOMEM;
  1153. goto err_out0;
  1154. }
  1155. stor_device->destroy = false;
  1156. init_waitqueue_head(&stor_device->waiting_to_drain);
  1157. stor_device->device = device;
  1158. stor_device->host = host;
  1159. hv_set_drvdata(device, stor_device);
  1160. stor_device->port_number = host->host_no;
  1161. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
  1162. if (ret)
  1163. goto err_out1;
  1164. host_dev->path = stor_device->path_id;
  1165. host_dev->target = stor_device->target_id;
  1166. /* max # of devices per target */
  1167. host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
  1168. /* max # of targets per channel */
  1169. host->max_id = STORVSC_MAX_TARGETS;
  1170. /* max # of channels */
  1171. host->max_channel = STORVSC_MAX_CHANNELS - 1;
  1172. /* max cmd length */
  1173. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1174. /* Register the HBA and start the scsi bus scan */
  1175. ret = scsi_add_host(host, &device->device);
  1176. if (ret != 0)
  1177. goto err_out2;
  1178. if (!dev_is_ide) {
  1179. scsi_scan_host(host);
  1180. } else {
  1181. target = (device->dev_instance.b[5] << 8 |
  1182. device->dev_instance.b[4]);
  1183. ret = scsi_add_device(host, 0, target, 0);
  1184. if (ret) {
  1185. scsi_remove_host(host);
  1186. goto err_out2;
  1187. }
  1188. }
  1189. return 0;
  1190. err_out2:
  1191. /*
  1192. * Once we have connected with the host, we would need to
  1193. * to invoke storvsc_dev_remove() to rollback this state and
  1194. * this call also frees up the stor_device; hence the jump around
  1195. * err_out1 label.
  1196. */
  1197. storvsc_dev_remove(device);
  1198. goto err_out0;
  1199. err_out1:
  1200. kfree(stor_device);
  1201. err_out0:
  1202. scsi_host_put(host);
  1203. return ret;
  1204. }
  1205. static int storvsc_remove(struct hv_device *dev)
  1206. {
  1207. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1208. struct Scsi_Host *host = stor_device->host;
  1209. scsi_remove_host(host);
  1210. storvsc_dev_remove(dev);
  1211. scsi_host_put(host);
  1212. return 0;
  1213. }
  1214. static struct hv_driver storvsc_drv = {
  1215. .name = KBUILD_MODNAME,
  1216. .id_table = id_table,
  1217. .probe = storvsc_probe,
  1218. .remove = storvsc_remove,
  1219. };
  1220. static int __init storvsc_drv_init(void)
  1221. {
  1222. u32 max_outstanding_req_per_channel;
  1223. /*
  1224. * Divide the ring buffer data size (which is 1 page less
  1225. * than the ring buffer size since that page is reserved for
  1226. * the ring buffer indices) by the max request size (which is
  1227. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1228. */
  1229. max_outstanding_req_per_channel =
  1230. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1231. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1232. sizeof(struct vstor_packet) + sizeof(u64),
  1233. sizeof(u64)));
  1234. if (max_outstanding_req_per_channel <
  1235. STORVSC_MAX_IO_REQUESTS)
  1236. return -EINVAL;
  1237. return vmbus_driver_register(&storvsc_drv);
  1238. }
  1239. static void __exit storvsc_drv_exit(void)
  1240. {
  1241. vmbus_driver_unregister(&storvsc_drv);
  1242. }
  1243. MODULE_LICENSE("GPL");
  1244. MODULE_VERSION(HV_DRV_VERSION);
  1245. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1246. module_init(storvsc_drv_init);
  1247. module_exit(storvsc_drv_exit);