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