storvsc_drv.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  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/blkdev.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. #include <scsi/scsi_transport_fc.h>
  44. #include <scsi/scsi_transport.h>
  45. /*
  46. * All wire protocol details (storage protocol between the guest and the host)
  47. * are consolidated here.
  48. *
  49. * Begin protocol definitions.
  50. */
  51. /*
  52. * Version history:
  53. * V1 Beta: 0.1
  54. * V1 RC < 2008/1/31: 1.0
  55. * V1 RC > 2008/1/31: 2.0
  56. * Win7: 4.2
  57. * Win8: 5.1
  58. * Win8.1: 6.0
  59. * Win10: 6.2
  60. */
  61. #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
  62. (((MINOR_) & 0xff)))
  63. #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
  64. #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
  65. #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
  66. #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
  67. #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
  68. /* Packet structure describing virtual storage requests. */
  69. enum vstor_packet_operation {
  70. VSTOR_OPERATION_COMPLETE_IO = 1,
  71. VSTOR_OPERATION_REMOVE_DEVICE = 2,
  72. VSTOR_OPERATION_EXECUTE_SRB = 3,
  73. VSTOR_OPERATION_RESET_LUN = 4,
  74. VSTOR_OPERATION_RESET_ADAPTER = 5,
  75. VSTOR_OPERATION_RESET_BUS = 6,
  76. VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
  77. VSTOR_OPERATION_END_INITIALIZATION = 8,
  78. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
  79. VSTOR_OPERATION_QUERY_PROPERTIES = 10,
  80. VSTOR_OPERATION_ENUMERATE_BUS = 11,
  81. VSTOR_OPERATION_FCHBA_DATA = 12,
  82. VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
  83. VSTOR_OPERATION_MAXIMUM = 13
  84. };
  85. /*
  86. * WWN packet for Fibre Channel HBA
  87. */
  88. struct hv_fc_wwn_packet {
  89. u8 primary_active;
  90. u8 reserved1[3];
  91. u8 primary_port_wwn[8];
  92. u8 primary_node_wwn[8];
  93. u8 secondary_port_wwn[8];
  94. u8 secondary_node_wwn[8];
  95. };
  96. /*
  97. * SRB Flag Bits
  98. */
  99. #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
  100. #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
  101. #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
  102. #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
  103. #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
  104. #define SRB_FLAGS_DATA_IN 0x00000040
  105. #define SRB_FLAGS_DATA_OUT 0x00000080
  106. #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
  107. #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
  108. #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
  109. #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
  110. #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
  111. /*
  112. * This flag indicates the request is part of the workflow for processing a D3.
  113. */
  114. #define SRB_FLAGS_D3_PROCESSING 0x00000800
  115. #define SRB_FLAGS_IS_ACTIVE 0x00010000
  116. #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
  117. #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
  118. #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
  119. #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
  120. #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
  121. #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
  122. #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
  123. #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
  124. #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
  125. #define SP_UNTAGGED ((unsigned char) ~0)
  126. #define SRB_SIMPLE_TAG_REQUEST 0x20
  127. /*
  128. * Platform neutral description of a scsi request -
  129. * this remains the same across the write regardless of 32/64 bit
  130. * note: it's patterned off the SCSI_PASS_THROUGH structure
  131. */
  132. #define STORVSC_MAX_CMD_LEN 0x10
  133. #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
  134. #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
  135. #define STORVSC_SENSE_BUFFER_SIZE 0x14
  136. #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
  137. /*
  138. * Sense buffer size changed in win8; have a run-time
  139. * variable to track the size we should use. This value will
  140. * likely change during protocol negotiation but it is valid
  141. * to start by assuming pre-Win8.
  142. */
  143. static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
  144. /*
  145. * The storage protocol version is determined during the
  146. * initial exchange with the host. It will indicate which
  147. * storage functionality is available in the host.
  148. */
  149. static int vmstor_proto_version;
  150. #define STORVSC_LOGGING_NONE 0
  151. #define STORVSC_LOGGING_ERROR 1
  152. #define STORVSC_LOGGING_WARN 2
  153. static int logging_level = STORVSC_LOGGING_ERROR;
  154. module_param(logging_level, int, S_IRUGO|S_IWUSR);
  155. MODULE_PARM_DESC(logging_level,
  156. "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
  157. static inline bool do_logging(int level)
  158. {
  159. return logging_level >= level;
  160. }
  161. #define storvsc_log(dev, level, fmt, ...) \
  162. do { \
  163. if (do_logging(level)) \
  164. dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
  165. } while (0)
  166. struct vmscsi_win8_extension {
  167. /*
  168. * The following were added in Windows 8
  169. */
  170. u16 reserve;
  171. u8 queue_tag;
  172. u8 queue_action;
  173. u32 srb_flags;
  174. u32 time_out_value;
  175. u32 queue_sort_ey;
  176. } __packed;
  177. struct vmscsi_request {
  178. u16 length;
  179. u8 srb_status;
  180. u8 scsi_status;
  181. u8 port_number;
  182. u8 path_id;
  183. u8 target_id;
  184. u8 lun;
  185. u8 cdb_length;
  186. u8 sense_info_length;
  187. u8 data_in;
  188. u8 reserved;
  189. u32 data_transfer_length;
  190. union {
  191. u8 cdb[STORVSC_MAX_CMD_LEN];
  192. u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
  193. u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
  194. };
  195. /*
  196. * The following was added in win8.
  197. */
  198. struct vmscsi_win8_extension win8_extension;
  199. } __attribute((packed));
  200. /*
  201. * The size of the vmscsi_request has changed in win8. The
  202. * additional size is because of new elements added to the
  203. * structure. These elements are valid only when we are talking
  204. * to a win8 host.
  205. * Track the correction to size we need to apply. This value
  206. * will likely change during protocol negotiation but it is
  207. * valid to start by assuming pre-Win8.
  208. */
  209. static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
  210. /*
  211. * The list of storage protocols in order of preference.
  212. */
  213. struct vmstor_protocol {
  214. int protocol_version;
  215. int sense_buffer_size;
  216. int vmscsi_size_delta;
  217. };
  218. static const struct vmstor_protocol vmstor_protocols[] = {
  219. {
  220. VMSTOR_PROTO_VERSION_WIN10,
  221. POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
  222. 0
  223. },
  224. {
  225. VMSTOR_PROTO_VERSION_WIN8_1,
  226. POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
  227. 0
  228. },
  229. {
  230. VMSTOR_PROTO_VERSION_WIN8,
  231. POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
  232. 0
  233. },
  234. {
  235. VMSTOR_PROTO_VERSION_WIN7,
  236. PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
  237. sizeof(struct vmscsi_win8_extension),
  238. },
  239. {
  240. VMSTOR_PROTO_VERSION_WIN6,
  241. PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
  242. sizeof(struct vmscsi_win8_extension),
  243. }
  244. };
  245. /*
  246. * This structure is sent during the initialization phase to get the different
  247. * properties of the channel.
  248. */
  249. #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
  250. struct vmstorage_channel_properties {
  251. u32 reserved;
  252. u16 max_channel_cnt;
  253. u16 reserved1;
  254. u32 flags;
  255. u32 max_transfer_bytes;
  256. u64 reserved2;
  257. } __packed;
  258. /* This structure is sent during the storage protocol negotiations. */
  259. struct vmstorage_protocol_version {
  260. /* Major (MSW) and minor (LSW) version numbers. */
  261. u16 major_minor;
  262. /*
  263. * Revision number is auto-incremented whenever this file is changed
  264. * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
  265. * definitely indicate incompatibility--but it does indicate mismatched
  266. * builds.
  267. * This is only used on the windows side. Just set it to 0.
  268. */
  269. u16 revision;
  270. } __packed;
  271. /* Channel Property Flags */
  272. #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
  273. #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
  274. struct vstor_packet {
  275. /* Requested operation type */
  276. enum vstor_packet_operation operation;
  277. /* Flags - see below for values */
  278. u32 flags;
  279. /* Status of the request returned from the server side. */
  280. u32 status;
  281. /* Data payload area */
  282. union {
  283. /*
  284. * Structure used to forward SCSI commands from the
  285. * client to the server.
  286. */
  287. struct vmscsi_request vm_srb;
  288. /* Structure used to query channel properties. */
  289. struct vmstorage_channel_properties storage_channel_properties;
  290. /* Used during version negotiations. */
  291. struct vmstorage_protocol_version version;
  292. /* Fibre channel address packet */
  293. struct hv_fc_wwn_packet wwn_packet;
  294. /* Number of sub-channels to create */
  295. u16 sub_channel_count;
  296. /* This will be the maximum of the union members */
  297. u8 buffer[0x34];
  298. };
  299. } __packed;
  300. /*
  301. * Packet Flags:
  302. *
  303. * This flag indicates that the server should send back a completion for this
  304. * packet.
  305. */
  306. #define REQUEST_COMPLETION_FLAG 0x1
  307. /* Matches Windows-end */
  308. enum storvsc_request_type {
  309. WRITE_TYPE = 0,
  310. READ_TYPE,
  311. UNKNOWN_TYPE,
  312. };
  313. /*
  314. * SRB status codes and masks; a subset of the codes used here.
  315. */
  316. #define SRB_STATUS_AUTOSENSE_VALID 0x80
  317. #define SRB_STATUS_QUEUE_FROZEN 0x40
  318. #define SRB_STATUS_INVALID_LUN 0x20
  319. #define SRB_STATUS_SUCCESS 0x01
  320. #define SRB_STATUS_ABORTED 0x02
  321. #define SRB_STATUS_ERROR 0x04
  322. #define SRB_STATUS_DATA_OVERRUN 0x12
  323. #define SRB_STATUS(status) \
  324. (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
  325. /*
  326. * This is the end of Protocol specific defines.
  327. */
  328. static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
  329. static u32 max_outstanding_req_per_channel;
  330. static int storvsc_vcpus_per_sub_channel = 4;
  331. module_param(storvsc_ringbuffer_size, int, S_IRUGO);
  332. MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
  333. module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
  334. MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
  335. /*
  336. * Timeout in seconds for all devices managed by this driver.
  337. */
  338. static int storvsc_timeout = 180;
  339. static int msft_blist_flags = BLIST_TRY_VPD_PAGES;
  340. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  341. static struct scsi_transport_template *fc_transport_template;
  342. #endif
  343. static void storvsc_on_channel_callback(void *context);
  344. #define STORVSC_MAX_LUNS_PER_TARGET 255
  345. #define STORVSC_MAX_TARGETS 2
  346. #define STORVSC_MAX_CHANNELS 8
  347. #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
  348. #define STORVSC_FC_MAX_TARGETS 128
  349. #define STORVSC_FC_MAX_CHANNELS 8
  350. #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
  351. #define STORVSC_IDE_MAX_TARGETS 1
  352. #define STORVSC_IDE_MAX_CHANNELS 1
  353. struct storvsc_cmd_request {
  354. struct scsi_cmnd *cmd;
  355. struct hv_device *device;
  356. /* Synchronize the request/response if needed */
  357. struct completion wait_event;
  358. struct vmbus_channel_packet_multipage_buffer mpb;
  359. struct vmbus_packet_mpb_array *payload;
  360. u32 payload_sz;
  361. struct vstor_packet vstor_packet;
  362. };
  363. /* A storvsc device is a device object that contains a vmbus channel */
  364. struct storvsc_device {
  365. struct hv_device *device;
  366. bool destroy;
  367. bool drain_notify;
  368. bool open_sub_channel;
  369. atomic_t num_outstanding_req;
  370. struct Scsi_Host *host;
  371. wait_queue_head_t waiting_to_drain;
  372. /*
  373. * Each unique Port/Path/Target represents 1 channel ie scsi
  374. * controller. In reality, the pathid, targetid is always 0
  375. * and the port is set by us
  376. */
  377. unsigned int port_number;
  378. unsigned char path_id;
  379. unsigned char target_id;
  380. /*
  381. * Max I/O, the device can support.
  382. */
  383. u32 max_transfer_bytes;
  384. /*
  385. * Number of sub-channels we will open.
  386. */
  387. u16 num_sc;
  388. struct vmbus_channel **stor_chns;
  389. /*
  390. * Mask of CPUs bound to subchannels.
  391. */
  392. struct cpumask alloced_cpus;
  393. /* Used for vsc/vsp channel reset process */
  394. struct storvsc_cmd_request init_request;
  395. struct storvsc_cmd_request reset_request;
  396. /*
  397. * Currently active port and node names for FC devices.
  398. */
  399. u64 node_name;
  400. u64 port_name;
  401. };
  402. struct hv_host_device {
  403. struct hv_device *dev;
  404. unsigned int port;
  405. unsigned char path;
  406. unsigned char target;
  407. };
  408. struct storvsc_scan_work {
  409. struct work_struct work;
  410. struct Scsi_Host *host;
  411. u8 lun;
  412. u8 tgt_id;
  413. };
  414. static void storvsc_device_scan(struct work_struct *work)
  415. {
  416. struct storvsc_scan_work *wrk;
  417. struct scsi_device *sdev;
  418. wrk = container_of(work, struct storvsc_scan_work, work);
  419. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  420. if (!sdev)
  421. goto done;
  422. scsi_rescan_device(&sdev->sdev_gendev);
  423. scsi_device_put(sdev);
  424. done:
  425. kfree(wrk);
  426. }
  427. static void storvsc_host_scan(struct work_struct *work)
  428. {
  429. struct storvsc_scan_work *wrk;
  430. struct Scsi_Host *host;
  431. struct scsi_device *sdev;
  432. wrk = container_of(work, struct storvsc_scan_work, work);
  433. host = wrk->host;
  434. /*
  435. * Before scanning the host, first check to see if any of the
  436. * currrently known devices have been hot removed. We issue a
  437. * "unit ready" command against all currently known devices.
  438. * This I/O will result in an error for devices that have been
  439. * removed. As part of handling the I/O error, we remove the device.
  440. *
  441. * When a LUN is added or removed, the host sends us a signal to
  442. * scan the host. Thus we are forced to discover the LUNs that
  443. * may have been removed this way.
  444. */
  445. mutex_lock(&host->scan_mutex);
  446. shost_for_each_device(sdev, host)
  447. scsi_test_unit_ready(sdev, 1, 1, NULL);
  448. mutex_unlock(&host->scan_mutex);
  449. /*
  450. * Now scan the host to discover LUNs that may have been added.
  451. */
  452. scsi_scan_host(host);
  453. kfree(wrk);
  454. }
  455. static void storvsc_remove_lun(struct work_struct *work)
  456. {
  457. struct storvsc_scan_work *wrk;
  458. struct scsi_device *sdev;
  459. wrk = container_of(work, struct storvsc_scan_work, work);
  460. if (!scsi_host_get(wrk->host))
  461. goto done;
  462. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  463. if (sdev) {
  464. scsi_remove_device(sdev);
  465. scsi_device_put(sdev);
  466. }
  467. scsi_host_put(wrk->host);
  468. done:
  469. kfree(wrk);
  470. }
  471. /*
  472. * We can get incoming messages from the host that are not in response to
  473. * messages that we have sent out. An example of this would be messages
  474. * received by the guest to notify dynamic addition/removal of LUNs. To
  475. * deal with potential race conditions where the driver may be in the
  476. * midst of being unloaded when we might receive an unsolicited message
  477. * from the host, we have implemented a mechanism to gurantee sequential
  478. * consistency:
  479. *
  480. * 1) Once the device is marked as being destroyed, we will fail all
  481. * outgoing messages.
  482. * 2) We permit incoming messages when the device is being destroyed,
  483. * only to properly account for messages already sent out.
  484. */
  485. static inline struct storvsc_device *get_out_stor_device(
  486. struct hv_device *device)
  487. {
  488. struct storvsc_device *stor_device;
  489. stor_device = hv_get_drvdata(device);
  490. if (stor_device && stor_device->destroy)
  491. stor_device = NULL;
  492. return stor_device;
  493. }
  494. static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
  495. {
  496. dev->drain_notify = true;
  497. wait_event(dev->waiting_to_drain,
  498. atomic_read(&dev->num_outstanding_req) == 0);
  499. dev->drain_notify = false;
  500. }
  501. static inline struct storvsc_device *get_in_stor_device(
  502. struct hv_device *device)
  503. {
  504. struct storvsc_device *stor_device;
  505. stor_device = hv_get_drvdata(device);
  506. if (!stor_device)
  507. goto get_in_err;
  508. /*
  509. * If the device is being destroyed; allow incoming
  510. * traffic only to cleanup outstanding requests.
  511. */
  512. if (stor_device->destroy &&
  513. (atomic_read(&stor_device->num_outstanding_req) == 0))
  514. stor_device = NULL;
  515. get_in_err:
  516. return stor_device;
  517. }
  518. static void handle_sc_creation(struct vmbus_channel *new_sc)
  519. {
  520. struct hv_device *device = new_sc->primary_channel->device_obj;
  521. struct storvsc_device *stor_device;
  522. struct vmstorage_channel_properties props;
  523. stor_device = get_out_stor_device(device);
  524. if (!stor_device)
  525. return;
  526. if (stor_device->open_sub_channel == false)
  527. return;
  528. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  529. vmbus_open(new_sc,
  530. storvsc_ringbuffer_size,
  531. storvsc_ringbuffer_size,
  532. (void *)&props,
  533. sizeof(struct vmstorage_channel_properties),
  534. storvsc_on_channel_callback, new_sc);
  535. if (new_sc->state == CHANNEL_OPENED_STATE) {
  536. stor_device->stor_chns[new_sc->target_cpu] = new_sc;
  537. cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
  538. }
  539. }
  540. static void handle_multichannel_storage(struct hv_device *device, int max_chns)
  541. {
  542. struct storvsc_device *stor_device;
  543. int num_cpus = num_online_cpus();
  544. int num_sc;
  545. struct storvsc_cmd_request *request;
  546. struct vstor_packet *vstor_packet;
  547. int ret, t;
  548. num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
  549. stor_device = get_out_stor_device(device);
  550. if (!stor_device)
  551. return;
  552. stor_device->num_sc = num_sc;
  553. request = &stor_device->init_request;
  554. vstor_packet = &request->vstor_packet;
  555. stor_device->open_sub_channel = true;
  556. /*
  557. * Establish a handler for dealing with subchannels.
  558. */
  559. vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
  560. /*
  561. * Check to see if sub-channels have already been created. This
  562. * can happen when this driver is re-loaded after unloading.
  563. */
  564. if (vmbus_are_subchannels_present(device->channel))
  565. return;
  566. stor_device->open_sub_channel = false;
  567. /*
  568. * Request the host to create sub-channels.
  569. */
  570. memset(request, 0, sizeof(struct storvsc_cmd_request));
  571. init_completion(&request->wait_event);
  572. vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
  573. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  574. vstor_packet->sub_channel_count = num_sc;
  575. ret = vmbus_sendpacket(device->channel, vstor_packet,
  576. (sizeof(struct vstor_packet) -
  577. vmscsi_size_delta),
  578. (unsigned long)request,
  579. VM_PKT_DATA_INBAND,
  580. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  581. if (ret != 0)
  582. return;
  583. t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
  584. if (t == 0)
  585. return;
  586. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  587. vstor_packet->status != 0)
  588. return;
  589. /*
  590. * Now that we created the sub-channels, invoke the check; this
  591. * may trigger the callback.
  592. */
  593. stor_device->open_sub_channel = true;
  594. vmbus_are_subchannels_present(device->channel);
  595. }
  596. static void cache_wwn(struct storvsc_device *stor_device,
  597. struct vstor_packet *vstor_packet)
  598. {
  599. /*
  600. * Cache the currently active port and node ww names.
  601. */
  602. if (vstor_packet->wwn_packet.primary_active) {
  603. stor_device->node_name =
  604. wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
  605. stor_device->port_name =
  606. wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
  607. } else {
  608. stor_device->node_name =
  609. wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
  610. stor_device->port_name =
  611. wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
  612. }
  613. }
  614. static int storvsc_execute_vstor_op(struct hv_device *device,
  615. struct storvsc_cmd_request *request,
  616. bool status_check)
  617. {
  618. struct vstor_packet *vstor_packet;
  619. int ret, t;
  620. vstor_packet = &request->vstor_packet;
  621. init_completion(&request->wait_event);
  622. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  623. ret = vmbus_sendpacket(device->channel, vstor_packet,
  624. (sizeof(struct vstor_packet) -
  625. vmscsi_size_delta),
  626. (unsigned long)request,
  627. VM_PKT_DATA_INBAND,
  628. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  629. if (ret != 0)
  630. return ret;
  631. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  632. if (t == 0)
  633. return -ETIMEDOUT;
  634. if (!status_check)
  635. return ret;
  636. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  637. vstor_packet->status != 0)
  638. return -EINVAL;
  639. return ret;
  640. }
  641. static int storvsc_channel_init(struct hv_device *device, bool is_fc)
  642. {
  643. struct storvsc_device *stor_device;
  644. struct storvsc_cmd_request *request;
  645. struct vstor_packet *vstor_packet;
  646. int ret, i;
  647. int max_chns;
  648. bool process_sub_channels = false;
  649. stor_device = get_out_stor_device(device);
  650. if (!stor_device)
  651. return -ENODEV;
  652. request = &stor_device->init_request;
  653. vstor_packet = &request->vstor_packet;
  654. /*
  655. * Now, initiate the vsc/vsp initialization protocol on the open
  656. * channel
  657. */
  658. memset(request, 0, sizeof(struct storvsc_cmd_request));
  659. vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
  660. ret = storvsc_execute_vstor_op(device, request, true);
  661. if (ret)
  662. return ret;
  663. /*
  664. * Query host supported protocol version.
  665. */
  666. for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
  667. /* reuse the packet for version range supported */
  668. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  669. vstor_packet->operation =
  670. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
  671. vstor_packet->version.major_minor =
  672. vmstor_protocols[i].protocol_version;
  673. /*
  674. * The revision number is only used in Windows; set it to 0.
  675. */
  676. vstor_packet->version.revision = 0;
  677. ret = storvsc_execute_vstor_op(device, request, false);
  678. if (ret != 0)
  679. return ret;
  680. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
  681. return -EINVAL;
  682. if (vstor_packet->status == 0) {
  683. vmstor_proto_version =
  684. vmstor_protocols[i].protocol_version;
  685. sense_buffer_size =
  686. vmstor_protocols[i].sense_buffer_size;
  687. vmscsi_size_delta =
  688. vmstor_protocols[i].vmscsi_size_delta;
  689. break;
  690. }
  691. }
  692. if (vstor_packet->status != 0)
  693. return -EINVAL;
  694. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  695. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
  696. ret = storvsc_execute_vstor_op(device, request, true);
  697. if (ret != 0)
  698. return ret;
  699. /*
  700. * Check to see if multi-channel support is there.
  701. * Hosts that implement protocol version of 5.1 and above
  702. * support multi-channel.
  703. */
  704. max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
  705. /*
  706. * Allocate state to manage the sub-channels.
  707. * We allocate an array based on the numbers of possible CPUs
  708. * (Hyper-V does not support cpu online/offline).
  709. * This Array will be sparseley populated with unique
  710. * channels - primary + sub-channels.
  711. * We will however populate all the slots to evenly distribute
  712. * the load.
  713. */
  714. stor_device->stor_chns = kzalloc(sizeof(void *) * num_possible_cpus(),
  715. GFP_KERNEL);
  716. if (stor_device->stor_chns == NULL)
  717. return -ENOMEM;
  718. stor_device->stor_chns[device->channel->target_cpu] = device->channel;
  719. cpumask_set_cpu(device->channel->target_cpu,
  720. &stor_device->alloced_cpus);
  721. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
  722. if (vstor_packet->storage_channel_properties.flags &
  723. STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
  724. process_sub_channels = true;
  725. }
  726. stor_device->max_transfer_bytes =
  727. vstor_packet->storage_channel_properties.max_transfer_bytes;
  728. if (!is_fc)
  729. goto done;
  730. /*
  731. * For FC devices retrieve FC HBA data.
  732. */
  733. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  734. vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
  735. ret = storvsc_execute_vstor_op(device, request, true);
  736. if (ret != 0)
  737. return ret;
  738. /*
  739. * Cache the currently active port and node ww names.
  740. */
  741. cache_wwn(stor_device, vstor_packet);
  742. done:
  743. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  744. vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
  745. ret = storvsc_execute_vstor_op(device, request, true);
  746. if (ret != 0)
  747. return ret;
  748. if (process_sub_channels)
  749. handle_multichannel_storage(device, max_chns);
  750. return ret;
  751. }
  752. static void storvsc_handle_error(struct vmscsi_request *vm_srb,
  753. struct scsi_cmnd *scmnd,
  754. struct Scsi_Host *host,
  755. u8 asc, u8 ascq)
  756. {
  757. struct storvsc_scan_work *wrk;
  758. void (*process_err_fn)(struct work_struct *work);
  759. bool do_work = false;
  760. switch (SRB_STATUS(vm_srb->srb_status)) {
  761. case SRB_STATUS_ERROR:
  762. /*
  763. * Let upper layer deal with error when
  764. * sense message is present.
  765. */
  766. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)
  767. break;
  768. /*
  769. * If there is an error; offline the device since all
  770. * error recovery strategies would have already been
  771. * deployed on the host side. However, if the command
  772. * were a pass-through command deal with it appropriately.
  773. */
  774. switch (scmnd->cmnd[0]) {
  775. case ATA_16:
  776. case ATA_12:
  777. set_host_byte(scmnd, DID_PASSTHROUGH);
  778. break;
  779. /*
  780. * On Some Windows hosts TEST_UNIT_READY command can return
  781. * SRB_STATUS_ERROR, let the upper level code deal with it
  782. * based on the sense information.
  783. */
  784. case TEST_UNIT_READY:
  785. break;
  786. default:
  787. set_host_byte(scmnd, DID_TARGET_FAILURE);
  788. }
  789. break;
  790. case SRB_STATUS_INVALID_LUN:
  791. do_work = true;
  792. process_err_fn = storvsc_remove_lun;
  793. break;
  794. case SRB_STATUS_ABORTED:
  795. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
  796. (asc == 0x2a) && (ascq == 0x9)) {
  797. do_work = true;
  798. process_err_fn = storvsc_device_scan;
  799. /*
  800. * Retry the I/O that trigerred this.
  801. */
  802. set_host_byte(scmnd, DID_REQUEUE);
  803. }
  804. break;
  805. }
  806. if (!do_work)
  807. return;
  808. /*
  809. * We need to schedule work to process this error; schedule it.
  810. */
  811. wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  812. if (!wrk) {
  813. set_host_byte(scmnd, DID_TARGET_FAILURE);
  814. return;
  815. }
  816. wrk->host = host;
  817. wrk->lun = vm_srb->lun;
  818. wrk->tgt_id = vm_srb->target_id;
  819. INIT_WORK(&wrk->work, process_err_fn);
  820. schedule_work(&wrk->work);
  821. }
  822. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
  823. struct storvsc_device *stor_dev)
  824. {
  825. struct scsi_cmnd *scmnd = cmd_request->cmd;
  826. struct scsi_sense_hdr sense_hdr;
  827. struct vmscsi_request *vm_srb;
  828. u32 data_transfer_length;
  829. struct Scsi_Host *host;
  830. u32 payload_sz = cmd_request->payload_sz;
  831. void *payload = cmd_request->payload;
  832. host = stor_dev->host;
  833. vm_srb = &cmd_request->vstor_packet.vm_srb;
  834. data_transfer_length = vm_srb->data_transfer_length;
  835. scmnd->result = vm_srb->scsi_status;
  836. if (scmnd->result) {
  837. if (scsi_normalize_sense(scmnd->sense_buffer,
  838. SCSI_SENSE_BUFFERSIZE, &sense_hdr) &&
  839. !(sense_hdr.sense_key == NOT_READY &&
  840. sense_hdr.asc == 0x03A) &&
  841. do_logging(STORVSC_LOGGING_ERROR))
  842. scsi_print_sense_hdr(scmnd->device, "storvsc",
  843. &sense_hdr);
  844. }
  845. if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
  846. storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
  847. sense_hdr.ascq);
  848. /*
  849. * The Windows driver set data_transfer_length on
  850. * SRB_STATUS_DATA_OVERRUN. On other errors, this value
  851. * is untouched. In these cases we set it to 0.
  852. */
  853. if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
  854. data_transfer_length = 0;
  855. }
  856. scsi_set_resid(scmnd,
  857. cmd_request->payload->range.len - data_transfer_length);
  858. scmnd->scsi_done(scmnd);
  859. if (payload_sz >
  860. sizeof(struct vmbus_channel_packet_multipage_buffer))
  861. kfree(payload);
  862. }
  863. static void storvsc_on_io_completion(struct storvsc_device *stor_device,
  864. struct vstor_packet *vstor_packet,
  865. struct storvsc_cmd_request *request)
  866. {
  867. struct vstor_packet *stor_pkt;
  868. struct hv_device *device = stor_device->device;
  869. stor_pkt = &request->vstor_packet;
  870. /*
  871. * The current SCSI handling on the host side does
  872. * not correctly handle:
  873. * INQUIRY command with page code parameter set to 0x80
  874. * MODE_SENSE command with cmd[2] == 0x1c
  875. *
  876. * Setup srb and scsi status so this won't be fatal.
  877. * We do this so we can distinguish truly fatal failues
  878. * (srb status == 0x4) and off-line the device in that case.
  879. */
  880. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  881. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  882. vstor_packet->vm_srb.scsi_status = 0;
  883. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  884. }
  885. /* Copy over the status...etc */
  886. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  887. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  888. stor_pkt->vm_srb.sense_info_length =
  889. vstor_packet->vm_srb.sense_info_length;
  890. if (vstor_packet->vm_srb.scsi_status != 0 ||
  891. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS)
  892. storvsc_log(device, STORVSC_LOGGING_WARN,
  893. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  894. stor_pkt->vm_srb.cdb[0],
  895. vstor_packet->vm_srb.scsi_status,
  896. vstor_packet->vm_srb.srb_status);
  897. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  898. /* CHECK_CONDITION */
  899. if (vstor_packet->vm_srb.srb_status &
  900. SRB_STATUS_AUTOSENSE_VALID) {
  901. /* autosense data available */
  902. storvsc_log(device, STORVSC_LOGGING_WARN,
  903. "stor pkt %p autosense data valid - len %d\n",
  904. request, vstor_packet->vm_srb.sense_info_length);
  905. memcpy(request->cmd->sense_buffer,
  906. vstor_packet->vm_srb.sense_data,
  907. vstor_packet->vm_srb.sense_info_length);
  908. }
  909. }
  910. stor_pkt->vm_srb.data_transfer_length =
  911. vstor_packet->vm_srb.data_transfer_length;
  912. storvsc_command_completion(request, stor_device);
  913. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  914. stor_device->drain_notify)
  915. wake_up(&stor_device->waiting_to_drain);
  916. }
  917. static void storvsc_on_receive(struct storvsc_device *stor_device,
  918. struct vstor_packet *vstor_packet,
  919. struct storvsc_cmd_request *request)
  920. {
  921. struct storvsc_scan_work *work;
  922. switch (vstor_packet->operation) {
  923. case VSTOR_OPERATION_COMPLETE_IO:
  924. storvsc_on_io_completion(stor_device, vstor_packet, request);
  925. break;
  926. case VSTOR_OPERATION_REMOVE_DEVICE:
  927. case VSTOR_OPERATION_ENUMERATE_BUS:
  928. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  929. if (!work)
  930. return;
  931. INIT_WORK(&work->work, storvsc_host_scan);
  932. work->host = stor_device->host;
  933. schedule_work(&work->work);
  934. break;
  935. case VSTOR_OPERATION_FCHBA_DATA:
  936. cache_wwn(stor_device, vstor_packet);
  937. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  938. fc_host_node_name(stor_device->host) = stor_device->node_name;
  939. fc_host_port_name(stor_device->host) = stor_device->port_name;
  940. #endif
  941. break;
  942. default:
  943. break;
  944. }
  945. }
  946. static void storvsc_on_channel_callback(void *context)
  947. {
  948. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  949. struct hv_device *device;
  950. struct storvsc_device *stor_device;
  951. u32 bytes_recvd;
  952. u64 request_id;
  953. unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
  954. struct storvsc_cmd_request *request;
  955. int ret;
  956. if (channel->primary_channel != NULL)
  957. device = channel->primary_channel->device_obj;
  958. else
  959. device = channel->device_obj;
  960. stor_device = get_in_stor_device(device);
  961. if (!stor_device)
  962. return;
  963. do {
  964. ret = vmbus_recvpacket(channel, packet,
  965. ALIGN((sizeof(struct vstor_packet) -
  966. vmscsi_size_delta), 8),
  967. &bytes_recvd, &request_id);
  968. if (ret == 0 && bytes_recvd > 0) {
  969. request = (struct storvsc_cmd_request *)
  970. (unsigned long)request_id;
  971. if ((request == &stor_device->init_request) ||
  972. (request == &stor_device->reset_request)) {
  973. memcpy(&request->vstor_packet, packet,
  974. (sizeof(struct vstor_packet) -
  975. vmscsi_size_delta));
  976. complete(&request->wait_event);
  977. } else {
  978. storvsc_on_receive(stor_device,
  979. (struct vstor_packet *)packet,
  980. request);
  981. }
  982. } else {
  983. break;
  984. }
  985. } while (1);
  986. return;
  987. }
  988. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
  989. bool is_fc)
  990. {
  991. struct vmstorage_channel_properties props;
  992. int ret;
  993. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  994. ret = vmbus_open(device->channel,
  995. ring_size,
  996. ring_size,
  997. (void *)&props,
  998. sizeof(struct vmstorage_channel_properties),
  999. storvsc_on_channel_callback, device->channel);
  1000. if (ret != 0)
  1001. return ret;
  1002. ret = storvsc_channel_init(device, is_fc);
  1003. return ret;
  1004. }
  1005. static int storvsc_dev_remove(struct hv_device *device)
  1006. {
  1007. struct storvsc_device *stor_device;
  1008. unsigned long flags;
  1009. stor_device = hv_get_drvdata(device);
  1010. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  1011. stor_device->destroy = true;
  1012. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  1013. /*
  1014. * At this point, all outbound traffic should be disable. We
  1015. * only allow inbound traffic (responses) to proceed so that
  1016. * outstanding requests can be completed.
  1017. */
  1018. storvsc_wait_to_drain(stor_device);
  1019. /*
  1020. * Since we have already drained, we don't need to busy wait
  1021. * as was done in final_release_stor_device()
  1022. * Note that we cannot set the ext pointer to NULL until
  1023. * we have drained - to drain the outgoing packets, we need to
  1024. * allow incoming packets.
  1025. */
  1026. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  1027. hv_set_drvdata(device, NULL);
  1028. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  1029. /* Close the channel */
  1030. vmbus_close(device->channel);
  1031. kfree(stor_device->stor_chns);
  1032. kfree(stor_device);
  1033. return 0;
  1034. }
  1035. static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
  1036. u16 q_num)
  1037. {
  1038. u16 slot = 0;
  1039. u16 hash_qnum;
  1040. struct cpumask alloced_mask;
  1041. int num_channels, tgt_cpu;
  1042. if (stor_device->num_sc == 0)
  1043. return stor_device->device->channel;
  1044. /*
  1045. * Our channel array is sparsley populated and we
  1046. * initiated I/O on a processor/hw-q that does not
  1047. * currently have a designated channel. Fix this.
  1048. * The strategy is simple:
  1049. * I. Ensure NUMA locality
  1050. * II. Distribute evenly (best effort)
  1051. * III. Mapping is persistent.
  1052. */
  1053. cpumask_and(&alloced_mask, &stor_device->alloced_cpus,
  1054. cpumask_of_node(cpu_to_node(q_num)));
  1055. num_channels = cpumask_weight(&alloced_mask);
  1056. if (num_channels == 0)
  1057. return stor_device->device->channel;
  1058. hash_qnum = q_num;
  1059. while (hash_qnum >= num_channels)
  1060. hash_qnum -= num_channels;
  1061. for_each_cpu(tgt_cpu, &alloced_mask) {
  1062. if (slot == hash_qnum)
  1063. break;
  1064. slot++;
  1065. }
  1066. stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
  1067. return stor_device->stor_chns[q_num];
  1068. }
  1069. static int storvsc_do_io(struct hv_device *device,
  1070. struct storvsc_cmd_request *request, u16 q_num)
  1071. {
  1072. struct storvsc_device *stor_device;
  1073. struct vstor_packet *vstor_packet;
  1074. struct vmbus_channel *outgoing_channel;
  1075. int ret = 0;
  1076. struct cpumask alloced_mask;
  1077. int tgt_cpu;
  1078. vstor_packet = &request->vstor_packet;
  1079. stor_device = get_out_stor_device(device);
  1080. if (!stor_device)
  1081. return -ENODEV;
  1082. request->device = device;
  1083. /*
  1084. * Select an an appropriate channel to send the request out.
  1085. */
  1086. if (stor_device->stor_chns[q_num] != NULL) {
  1087. outgoing_channel = stor_device->stor_chns[q_num];
  1088. if (outgoing_channel->target_cpu == smp_processor_id()) {
  1089. /*
  1090. * Ideally, we want to pick a different channel if
  1091. * available on the same NUMA node.
  1092. */
  1093. cpumask_and(&alloced_mask, &stor_device->alloced_cpus,
  1094. cpumask_of_node(cpu_to_node(q_num)));
  1095. for_each_cpu(tgt_cpu, &alloced_mask) {
  1096. if (tgt_cpu != outgoing_channel->target_cpu) {
  1097. outgoing_channel =
  1098. stor_device->stor_chns[tgt_cpu];
  1099. break;
  1100. }
  1101. }
  1102. }
  1103. } else {
  1104. outgoing_channel = get_og_chn(stor_device, q_num);
  1105. }
  1106. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  1107. vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
  1108. vmscsi_size_delta);
  1109. vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
  1110. vstor_packet->vm_srb.data_transfer_length =
  1111. request->payload->range.len;
  1112. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  1113. if (request->payload->range.len) {
  1114. ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
  1115. request->payload, request->payload_sz,
  1116. vstor_packet,
  1117. (sizeof(struct vstor_packet) -
  1118. vmscsi_size_delta),
  1119. (unsigned long)request);
  1120. } else {
  1121. ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
  1122. (sizeof(struct vstor_packet) -
  1123. vmscsi_size_delta),
  1124. (unsigned long)request,
  1125. VM_PKT_DATA_INBAND,
  1126. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1127. }
  1128. if (ret != 0)
  1129. return ret;
  1130. atomic_inc(&stor_device->num_outstanding_req);
  1131. return ret;
  1132. }
  1133. static int storvsc_device_configure(struct scsi_device *sdevice)
  1134. {
  1135. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  1136. blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
  1137. /* Ensure there are no gaps in presented sgls */
  1138. blk_queue_virt_boundary(sdevice->request_queue, PAGE_SIZE - 1);
  1139. sdevice->no_write_same = 1;
  1140. /*
  1141. * Add blist flags to permit the reading of the VPD pages even when
  1142. * the target may claim SPC-2 compliance. MSFT targets currently
  1143. * claim SPC-2 compliance while they implement post SPC-2 features.
  1144. * With this patch we can correctly handle WRITE_SAME_16 issues.
  1145. */
  1146. sdevice->sdev_bflags |= msft_blist_flags;
  1147. /*
  1148. * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
  1149. * if the device is a MSFT virtual device. If the host is
  1150. * WIN10 or newer, allow write_same.
  1151. */
  1152. if (!strncmp(sdevice->vendor, "Msft", 4)) {
  1153. switch (vmstor_proto_version) {
  1154. case VMSTOR_PROTO_VERSION_WIN8:
  1155. case VMSTOR_PROTO_VERSION_WIN8_1:
  1156. sdevice->scsi_level = SCSI_SPC_3;
  1157. break;
  1158. }
  1159. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
  1160. sdevice->no_write_same = 0;
  1161. }
  1162. return 0;
  1163. }
  1164. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  1165. sector_t capacity, int *info)
  1166. {
  1167. sector_t nsect = capacity;
  1168. sector_t cylinders = nsect;
  1169. int heads, sectors_pt;
  1170. /*
  1171. * We are making up these values; let us keep it simple.
  1172. */
  1173. heads = 0xff;
  1174. sectors_pt = 0x3f; /* Sectors per track */
  1175. sector_div(cylinders, heads * sectors_pt);
  1176. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  1177. cylinders = 0xffff;
  1178. info[0] = heads;
  1179. info[1] = sectors_pt;
  1180. info[2] = (int)cylinders;
  1181. return 0;
  1182. }
  1183. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  1184. {
  1185. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  1186. struct hv_device *device = host_dev->dev;
  1187. struct storvsc_device *stor_device;
  1188. struct storvsc_cmd_request *request;
  1189. struct vstor_packet *vstor_packet;
  1190. int ret, t;
  1191. stor_device = get_out_stor_device(device);
  1192. if (!stor_device)
  1193. return FAILED;
  1194. request = &stor_device->reset_request;
  1195. vstor_packet = &request->vstor_packet;
  1196. init_completion(&request->wait_event);
  1197. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  1198. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  1199. vstor_packet->vm_srb.path_id = stor_device->path_id;
  1200. ret = vmbus_sendpacket(device->channel, vstor_packet,
  1201. (sizeof(struct vstor_packet) -
  1202. vmscsi_size_delta),
  1203. (unsigned long)&stor_device->reset_request,
  1204. VM_PKT_DATA_INBAND,
  1205. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1206. if (ret != 0)
  1207. return FAILED;
  1208. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  1209. if (t == 0)
  1210. return TIMEOUT_ERROR;
  1211. /*
  1212. * At this point, all outstanding requests in the adapter
  1213. * should have been flushed out and return to us
  1214. * There is a potential race here where the host may be in
  1215. * the process of responding when we return from here.
  1216. * Just wait for all in-transit packets to be accounted for
  1217. * before we return from here.
  1218. */
  1219. storvsc_wait_to_drain(stor_device);
  1220. return SUCCESS;
  1221. }
  1222. /*
  1223. * The host guarantees to respond to each command, although I/O latencies might
  1224. * be unbounded on Azure. Reset the timer unconditionally to give the host a
  1225. * chance to perform EH.
  1226. */
  1227. static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
  1228. {
  1229. return BLK_EH_RESET_TIMER;
  1230. }
  1231. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  1232. {
  1233. bool allowed = true;
  1234. u8 scsi_op = scmnd->cmnd[0];
  1235. switch (scsi_op) {
  1236. /* the host does not handle WRITE_SAME, log accident usage */
  1237. case WRITE_SAME:
  1238. /*
  1239. * smartd sends this command and the host does not handle
  1240. * this. So, don't send it.
  1241. */
  1242. case SET_WINDOW:
  1243. scmnd->result = ILLEGAL_REQUEST << 16;
  1244. allowed = false;
  1245. break;
  1246. default:
  1247. break;
  1248. }
  1249. return allowed;
  1250. }
  1251. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  1252. {
  1253. int ret;
  1254. struct hv_host_device *host_dev = shost_priv(host);
  1255. struct hv_device *dev = host_dev->dev;
  1256. struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
  1257. int i;
  1258. struct scatterlist *sgl;
  1259. unsigned int sg_count = 0;
  1260. struct vmscsi_request *vm_srb;
  1261. struct scatterlist *cur_sgl;
  1262. struct vmbus_packet_mpb_array *payload;
  1263. u32 payload_sz;
  1264. u32 length;
  1265. if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
  1266. /*
  1267. * On legacy hosts filter unimplemented commands.
  1268. * Future hosts are expected to correctly handle
  1269. * unsupported commands. Furthermore, it is
  1270. * possible that some of the currently
  1271. * unsupported commands maybe supported in
  1272. * future versions of the host.
  1273. */
  1274. if (!storvsc_scsi_cmd_ok(scmnd)) {
  1275. scmnd->scsi_done(scmnd);
  1276. return 0;
  1277. }
  1278. }
  1279. /* Setup the cmd request */
  1280. cmd_request->cmd = scmnd;
  1281. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1282. vm_srb->win8_extension.time_out_value = 60;
  1283. vm_srb->win8_extension.srb_flags |=
  1284. SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1285. if (scmnd->device->tagged_supported) {
  1286. vm_srb->win8_extension.srb_flags |=
  1287. (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
  1288. vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
  1289. vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
  1290. }
  1291. /* Build the SRB */
  1292. switch (scmnd->sc_data_direction) {
  1293. case DMA_TO_DEVICE:
  1294. vm_srb->data_in = WRITE_TYPE;
  1295. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
  1296. break;
  1297. case DMA_FROM_DEVICE:
  1298. vm_srb->data_in = READ_TYPE;
  1299. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
  1300. break;
  1301. case DMA_NONE:
  1302. vm_srb->data_in = UNKNOWN_TYPE;
  1303. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
  1304. break;
  1305. default:
  1306. /*
  1307. * This is DMA_BIDIRECTIONAL or something else we are never
  1308. * supposed to see here.
  1309. */
  1310. WARN(1, "Unexpected data direction: %d\n",
  1311. scmnd->sc_data_direction);
  1312. return -EINVAL;
  1313. }
  1314. vm_srb->port_number = host_dev->port;
  1315. vm_srb->path_id = scmnd->device->channel;
  1316. vm_srb->target_id = scmnd->device->id;
  1317. vm_srb->lun = scmnd->device->lun;
  1318. vm_srb->cdb_length = scmnd->cmd_len;
  1319. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1320. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1321. sg_count = scsi_sg_count(scmnd);
  1322. length = scsi_bufflen(scmnd);
  1323. payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
  1324. payload_sz = sizeof(cmd_request->mpb);
  1325. if (sg_count) {
  1326. if (sg_count > MAX_PAGE_BUFFER_COUNT) {
  1327. payload_sz = (sg_count * sizeof(u64) +
  1328. sizeof(struct vmbus_packet_mpb_array));
  1329. payload = kzalloc(payload_sz, GFP_ATOMIC);
  1330. if (!payload)
  1331. return SCSI_MLQUEUE_DEVICE_BUSY;
  1332. }
  1333. payload->range.len = length;
  1334. payload->range.offset = sgl[0].offset;
  1335. cur_sgl = sgl;
  1336. for (i = 0; i < sg_count; i++) {
  1337. payload->range.pfn_array[i] =
  1338. page_to_pfn(sg_page((cur_sgl)));
  1339. cur_sgl = sg_next(cur_sgl);
  1340. }
  1341. }
  1342. cmd_request->payload = payload;
  1343. cmd_request->payload_sz = payload_sz;
  1344. /* Invokes the vsc to start an IO */
  1345. ret = storvsc_do_io(dev, cmd_request, get_cpu());
  1346. put_cpu();
  1347. if (ret == -EAGAIN) {
  1348. /* no more space */
  1349. return SCSI_MLQUEUE_DEVICE_BUSY;
  1350. }
  1351. return 0;
  1352. }
  1353. static struct scsi_host_template scsi_driver = {
  1354. .module = THIS_MODULE,
  1355. .name = "storvsc_host_t",
  1356. .cmd_size = sizeof(struct storvsc_cmd_request),
  1357. .bios_param = storvsc_get_chs,
  1358. .queuecommand = storvsc_queuecommand,
  1359. .eh_host_reset_handler = storvsc_host_reset_handler,
  1360. .proc_name = "storvsc_host",
  1361. .eh_timed_out = storvsc_eh_timed_out,
  1362. .slave_configure = storvsc_device_configure,
  1363. .cmd_per_lun = 255,
  1364. .this_id = -1,
  1365. .use_clustering = ENABLE_CLUSTERING,
  1366. /* Make sure we dont get a sg segment crosses a page boundary */
  1367. .dma_boundary = PAGE_SIZE-1,
  1368. .no_write_same = 1,
  1369. .track_queue_depth = 1,
  1370. };
  1371. enum {
  1372. SCSI_GUID,
  1373. IDE_GUID,
  1374. SFC_GUID,
  1375. };
  1376. static const struct hv_vmbus_device_id id_table[] = {
  1377. /* SCSI guid */
  1378. { HV_SCSI_GUID,
  1379. .driver_data = SCSI_GUID
  1380. },
  1381. /* IDE guid */
  1382. { HV_IDE_GUID,
  1383. .driver_data = IDE_GUID
  1384. },
  1385. /* Fibre Channel GUID */
  1386. {
  1387. HV_SYNTHFC_GUID,
  1388. .driver_data = SFC_GUID
  1389. },
  1390. { },
  1391. };
  1392. MODULE_DEVICE_TABLE(vmbus, id_table);
  1393. static int storvsc_probe(struct hv_device *device,
  1394. const struct hv_vmbus_device_id *dev_id)
  1395. {
  1396. int ret;
  1397. int num_cpus = num_online_cpus();
  1398. struct Scsi_Host *host;
  1399. struct hv_host_device *host_dev;
  1400. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1401. bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
  1402. int target = 0;
  1403. struct storvsc_device *stor_device;
  1404. int max_luns_per_target;
  1405. int max_targets;
  1406. int max_channels;
  1407. int max_sub_channels = 0;
  1408. /*
  1409. * Based on the windows host we are running on,
  1410. * set state to properly communicate with the host.
  1411. */
  1412. if (vmbus_proto_version < VERSION_WIN8) {
  1413. max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1414. max_targets = STORVSC_IDE_MAX_TARGETS;
  1415. max_channels = STORVSC_IDE_MAX_CHANNELS;
  1416. } else {
  1417. max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
  1418. max_targets = STORVSC_MAX_TARGETS;
  1419. max_channels = STORVSC_MAX_CHANNELS;
  1420. /*
  1421. * On Windows8 and above, we support sub-channels for storage.
  1422. * The number of sub-channels offerred is based on the number of
  1423. * VCPUs in the guest.
  1424. */
  1425. max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
  1426. }
  1427. scsi_driver.can_queue = (max_outstanding_req_per_channel *
  1428. (max_sub_channels + 1));
  1429. host = scsi_host_alloc(&scsi_driver,
  1430. sizeof(struct hv_host_device));
  1431. if (!host)
  1432. return -ENOMEM;
  1433. host_dev = shost_priv(host);
  1434. memset(host_dev, 0, sizeof(struct hv_host_device));
  1435. host_dev->port = host->host_no;
  1436. host_dev->dev = device;
  1437. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1438. if (!stor_device) {
  1439. ret = -ENOMEM;
  1440. goto err_out0;
  1441. }
  1442. stor_device->destroy = false;
  1443. stor_device->open_sub_channel = false;
  1444. init_waitqueue_head(&stor_device->waiting_to_drain);
  1445. stor_device->device = device;
  1446. stor_device->host = host;
  1447. hv_set_drvdata(device, stor_device);
  1448. stor_device->port_number = host->host_no;
  1449. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
  1450. if (ret)
  1451. goto err_out1;
  1452. host_dev->path = stor_device->path_id;
  1453. host_dev->target = stor_device->target_id;
  1454. switch (dev_id->driver_data) {
  1455. case SFC_GUID:
  1456. host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
  1457. host->max_id = STORVSC_FC_MAX_TARGETS;
  1458. host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
  1459. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1460. host->transportt = fc_transport_template;
  1461. #endif
  1462. break;
  1463. case SCSI_GUID:
  1464. host->max_lun = max_luns_per_target;
  1465. host->max_id = max_targets;
  1466. host->max_channel = max_channels - 1;
  1467. break;
  1468. default:
  1469. host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1470. host->max_id = STORVSC_IDE_MAX_TARGETS;
  1471. host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
  1472. break;
  1473. }
  1474. /* max cmd length */
  1475. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1476. /*
  1477. * set the table size based on the info we got
  1478. * from the host.
  1479. */
  1480. host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
  1481. /*
  1482. * Set the number of HW queues we are supporting.
  1483. */
  1484. if (stor_device->num_sc != 0)
  1485. host->nr_hw_queues = stor_device->num_sc + 1;
  1486. /* Register the HBA and start the scsi bus scan */
  1487. ret = scsi_add_host(host, &device->device);
  1488. if (ret != 0)
  1489. goto err_out2;
  1490. if (!dev_is_ide) {
  1491. scsi_scan_host(host);
  1492. } else {
  1493. target = (device->dev_instance.b[5] << 8 |
  1494. device->dev_instance.b[4]);
  1495. ret = scsi_add_device(host, 0, target, 0);
  1496. if (ret) {
  1497. scsi_remove_host(host);
  1498. goto err_out2;
  1499. }
  1500. }
  1501. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1502. if (host->transportt == fc_transport_template) {
  1503. fc_host_node_name(host) = stor_device->node_name;
  1504. fc_host_port_name(host) = stor_device->port_name;
  1505. }
  1506. #endif
  1507. return 0;
  1508. err_out2:
  1509. /*
  1510. * Once we have connected with the host, we would need to
  1511. * to invoke storvsc_dev_remove() to rollback this state and
  1512. * this call also frees up the stor_device; hence the jump around
  1513. * err_out1 label.
  1514. */
  1515. storvsc_dev_remove(device);
  1516. goto err_out0;
  1517. err_out1:
  1518. kfree(stor_device->stor_chns);
  1519. kfree(stor_device);
  1520. err_out0:
  1521. scsi_host_put(host);
  1522. return ret;
  1523. }
  1524. static int storvsc_remove(struct hv_device *dev)
  1525. {
  1526. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1527. struct Scsi_Host *host = stor_device->host;
  1528. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1529. if (host->transportt == fc_transport_template)
  1530. fc_remove_host(host);
  1531. #endif
  1532. scsi_remove_host(host);
  1533. storvsc_dev_remove(dev);
  1534. scsi_host_put(host);
  1535. return 0;
  1536. }
  1537. static struct hv_driver storvsc_drv = {
  1538. .name = KBUILD_MODNAME,
  1539. .id_table = id_table,
  1540. .probe = storvsc_probe,
  1541. .remove = storvsc_remove,
  1542. };
  1543. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1544. static struct fc_function_template fc_transport_functions = {
  1545. .show_host_node_name = 1,
  1546. .show_host_port_name = 1,
  1547. };
  1548. #endif
  1549. static int __init storvsc_drv_init(void)
  1550. {
  1551. int ret;
  1552. /*
  1553. * Divide the ring buffer data size (which is 1 page less
  1554. * than the ring buffer size since that page is reserved for
  1555. * the ring buffer indices) by the max request size (which is
  1556. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1557. */
  1558. max_outstanding_req_per_channel =
  1559. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1560. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1561. sizeof(struct vstor_packet) + sizeof(u64) -
  1562. vmscsi_size_delta,
  1563. sizeof(u64)));
  1564. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1565. fc_transport_template = fc_attach_transport(&fc_transport_functions);
  1566. if (!fc_transport_template)
  1567. return -ENODEV;
  1568. #endif
  1569. ret = vmbus_driver_register(&storvsc_drv);
  1570. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1571. if (ret)
  1572. fc_release_transport(fc_transport_template);
  1573. #endif
  1574. return ret;
  1575. }
  1576. static void __exit storvsc_drv_exit(void)
  1577. {
  1578. vmbus_driver_unregister(&storvsc_drv);
  1579. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1580. fc_release_transport(fc_transport_template);
  1581. #endif
  1582. }
  1583. MODULE_LICENSE("GPL");
  1584. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1585. module_init(storvsc_drv_init);
  1586. module_exit(storvsc_drv_exit);