storvsc_drv.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  340. static struct scsi_transport_template *fc_transport_template;
  341. #endif
  342. static void storvsc_on_channel_callback(void *context);
  343. #define STORVSC_MAX_LUNS_PER_TARGET 255
  344. #define STORVSC_MAX_TARGETS 2
  345. #define STORVSC_MAX_CHANNELS 8
  346. #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
  347. #define STORVSC_FC_MAX_TARGETS 128
  348. #define STORVSC_FC_MAX_CHANNELS 8
  349. #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
  350. #define STORVSC_IDE_MAX_TARGETS 1
  351. #define STORVSC_IDE_MAX_CHANNELS 1
  352. struct storvsc_cmd_request {
  353. struct scsi_cmnd *cmd;
  354. struct hv_device *device;
  355. /* Synchronize the request/response if needed */
  356. struct completion wait_event;
  357. struct vmbus_channel_packet_multipage_buffer mpb;
  358. struct vmbus_packet_mpb_array *payload;
  359. u32 payload_sz;
  360. struct vstor_packet vstor_packet;
  361. };
  362. /* A storvsc device is a device object that contains a vmbus channel */
  363. struct storvsc_device {
  364. struct hv_device *device;
  365. bool destroy;
  366. bool drain_notify;
  367. bool open_sub_channel;
  368. atomic_t num_outstanding_req;
  369. struct Scsi_Host *host;
  370. wait_queue_head_t waiting_to_drain;
  371. /*
  372. * Each unique Port/Path/Target represents 1 channel ie scsi
  373. * controller. In reality, the pathid, targetid is always 0
  374. * and the port is set by us
  375. */
  376. unsigned int port_number;
  377. unsigned char path_id;
  378. unsigned char target_id;
  379. /*
  380. * Max I/O, the device can support.
  381. */
  382. u32 max_transfer_bytes;
  383. /*
  384. * Number of sub-channels we will open.
  385. */
  386. u16 num_sc;
  387. struct vmbus_channel **stor_chns;
  388. /*
  389. * Mask of CPUs bound to subchannels.
  390. */
  391. struct cpumask alloced_cpus;
  392. /* Used for vsc/vsp channel reset process */
  393. struct storvsc_cmd_request init_request;
  394. struct storvsc_cmd_request reset_request;
  395. /*
  396. * Currently active port and node names for FC devices.
  397. */
  398. u64 node_name;
  399. u64 port_name;
  400. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  401. struct fc_rport *rport;
  402. #endif
  403. };
  404. struct hv_host_device {
  405. struct hv_device *dev;
  406. unsigned int port;
  407. unsigned char path;
  408. unsigned char target;
  409. };
  410. struct storvsc_scan_work {
  411. struct work_struct work;
  412. struct Scsi_Host *host;
  413. u8 lun;
  414. u8 tgt_id;
  415. };
  416. static void storvsc_device_scan(struct work_struct *work)
  417. {
  418. struct storvsc_scan_work *wrk;
  419. struct scsi_device *sdev;
  420. wrk = container_of(work, struct storvsc_scan_work, work);
  421. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  422. if (!sdev)
  423. goto done;
  424. scsi_rescan_device(&sdev->sdev_gendev);
  425. scsi_device_put(sdev);
  426. done:
  427. kfree(wrk);
  428. }
  429. static void storvsc_host_scan(struct work_struct *work)
  430. {
  431. struct storvsc_scan_work *wrk;
  432. struct Scsi_Host *host;
  433. struct scsi_device *sdev;
  434. wrk = container_of(work, struct storvsc_scan_work, work);
  435. host = wrk->host;
  436. /*
  437. * Before scanning the host, first check to see if any of the
  438. * currrently known devices have been hot removed. We issue a
  439. * "unit ready" command against all currently known devices.
  440. * This I/O will result in an error for devices that have been
  441. * removed. As part of handling the I/O error, we remove the device.
  442. *
  443. * When a LUN is added or removed, the host sends us a signal to
  444. * scan the host. Thus we are forced to discover the LUNs that
  445. * may have been removed this way.
  446. */
  447. mutex_lock(&host->scan_mutex);
  448. shost_for_each_device(sdev, host)
  449. scsi_test_unit_ready(sdev, 1, 1, NULL);
  450. mutex_unlock(&host->scan_mutex);
  451. /*
  452. * Now scan the host to discover LUNs that may have been added.
  453. */
  454. scsi_scan_host(host);
  455. kfree(wrk);
  456. }
  457. static void storvsc_remove_lun(struct work_struct *work)
  458. {
  459. struct storvsc_scan_work *wrk;
  460. struct scsi_device *sdev;
  461. wrk = container_of(work, struct storvsc_scan_work, work);
  462. if (!scsi_host_get(wrk->host))
  463. goto done;
  464. sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
  465. if (sdev) {
  466. scsi_remove_device(sdev);
  467. scsi_device_put(sdev);
  468. }
  469. scsi_host_put(wrk->host);
  470. done:
  471. kfree(wrk);
  472. }
  473. /*
  474. * We can get incoming messages from the host that are not in response to
  475. * messages that we have sent out. An example of this would be messages
  476. * received by the guest to notify dynamic addition/removal of LUNs. To
  477. * deal with potential race conditions where the driver may be in the
  478. * midst of being unloaded when we might receive an unsolicited message
  479. * from the host, we have implemented a mechanism to gurantee sequential
  480. * consistency:
  481. *
  482. * 1) Once the device is marked as being destroyed, we will fail all
  483. * outgoing messages.
  484. * 2) We permit incoming messages when the device is being destroyed,
  485. * only to properly account for messages already sent out.
  486. */
  487. static inline struct storvsc_device *get_out_stor_device(
  488. struct hv_device *device)
  489. {
  490. struct storvsc_device *stor_device;
  491. stor_device = hv_get_drvdata(device);
  492. if (stor_device && stor_device->destroy)
  493. stor_device = NULL;
  494. return stor_device;
  495. }
  496. static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
  497. {
  498. dev->drain_notify = true;
  499. wait_event(dev->waiting_to_drain,
  500. atomic_read(&dev->num_outstanding_req) == 0);
  501. dev->drain_notify = false;
  502. }
  503. static inline struct storvsc_device *get_in_stor_device(
  504. struct hv_device *device)
  505. {
  506. struct storvsc_device *stor_device;
  507. stor_device = hv_get_drvdata(device);
  508. if (!stor_device)
  509. goto get_in_err;
  510. /*
  511. * If the device is being destroyed; allow incoming
  512. * traffic only to cleanup outstanding requests.
  513. */
  514. if (stor_device->destroy &&
  515. (atomic_read(&stor_device->num_outstanding_req) == 0))
  516. stor_device = NULL;
  517. get_in_err:
  518. return stor_device;
  519. }
  520. static void handle_sc_creation(struct vmbus_channel *new_sc)
  521. {
  522. struct hv_device *device = new_sc->primary_channel->device_obj;
  523. struct storvsc_device *stor_device;
  524. struct vmstorage_channel_properties props;
  525. stor_device = get_out_stor_device(device);
  526. if (!stor_device)
  527. return;
  528. if (stor_device->open_sub_channel == false)
  529. return;
  530. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  531. vmbus_open(new_sc,
  532. storvsc_ringbuffer_size,
  533. storvsc_ringbuffer_size,
  534. (void *)&props,
  535. sizeof(struct vmstorage_channel_properties),
  536. storvsc_on_channel_callback, new_sc);
  537. if (new_sc->state == CHANNEL_OPENED_STATE) {
  538. stor_device->stor_chns[new_sc->target_cpu] = new_sc;
  539. cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
  540. }
  541. }
  542. static void handle_multichannel_storage(struct hv_device *device, int max_chns)
  543. {
  544. struct storvsc_device *stor_device;
  545. int num_sc;
  546. struct storvsc_cmd_request *request;
  547. struct vstor_packet *vstor_packet;
  548. int ret, t;
  549. /*
  550. * If the number of CPUs is artificially restricted, such as
  551. * with maxcpus=1 on the kernel boot line, Hyper-V could offer
  552. * sub-channels >= the number of CPUs. These sub-channels
  553. * should not be created. The primary channel is already created
  554. * and assigned to one CPU, so check against # CPUs - 1.
  555. */
  556. num_sc = min((int)(num_online_cpus() - 1), max_chns);
  557. if (!num_sc)
  558. return;
  559. stor_device = get_out_stor_device(device);
  560. if (!stor_device)
  561. return;
  562. stor_device->num_sc = num_sc;
  563. request = &stor_device->init_request;
  564. vstor_packet = &request->vstor_packet;
  565. stor_device->open_sub_channel = true;
  566. /*
  567. * Establish a handler for dealing with subchannels.
  568. */
  569. vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
  570. /*
  571. * Check to see if sub-channels have already been created. This
  572. * can happen when this driver is re-loaded after unloading.
  573. */
  574. if (vmbus_are_subchannels_present(device->channel))
  575. return;
  576. stor_device->open_sub_channel = false;
  577. /*
  578. * Request the host to create sub-channels.
  579. */
  580. memset(request, 0, sizeof(struct storvsc_cmd_request));
  581. init_completion(&request->wait_event);
  582. vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
  583. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  584. vstor_packet->sub_channel_count = num_sc;
  585. ret = vmbus_sendpacket(device->channel, vstor_packet,
  586. (sizeof(struct vstor_packet) -
  587. vmscsi_size_delta),
  588. (unsigned long)request,
  589. VM_PKT_DATA_INBAND,
  590. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  591. if (ret != 0)
  592. return;
  593. t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
  594. if (t == 0)
  595. return;
  596. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  597. vstor_packet->status != 0)
  598. return;
  599. /*
  600. * Now that we created the sub-channels, invoke the check; this
  601. * may trigger the callback.
  602. */
  603. stor_device->open_sub_channel = true;
  604. vmbus_are_subchannels_present(device->channel);
  605. }
  606. static void cache_wwn(struct storvsc_device *stor_device,
  607. struct vstor_packet *vstor_packet)
  608. {
  609. /*
  610. * Cache the currently active port and node ww names.
  611. */
  612. if (vstor_packet->wwn_packet.primary_active) {
  613. stor_device->node_name =
  614. wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
  615. stor_device->port_name =
  616. wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
  617. } else {
  618. stor_device->node_name =
  619. wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
  620. stor_device->port_name =
  621. wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
  622. }
  623. }
  624. static int storvsc_execute_vstor_op(struct hv_device *device,
  625. struct storvsc_cmd_request *request,
  626. bool status_check)
  627. {
  628. struct vstor_packet *vstor_packet;
  629. int ret, t;
  630. vstor_packet = &request->vstor_packet;
  631. init_completion(&request->wait_event);
  632. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  633. ret = vmbus_sendpacket(device->channel, vstor_packet,
  634. (sizeof(struct vstor_packet) -
  635. vmscsi_size_delta),
  636. (unsigned long)request,
  637. VM_PKT_DATA_INBAND,
  638. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  639. if (ret != 0)
  640. return ret;
  641. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  642. if (t == 0)
  643. return -ETIMEDOUT;
  644. if (!status_check)
  645. return ret;
  646. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
  647. vstor_packet->status != 0)
  648. return -EINVAL;
  649. return ret;
  650. }
  651. static int storvsc_channel_init(struct hv_device *device, bool is_fc)
  652. {
  653. struct storvsc_device *stor_device;
  654. struct storvsc_cmd_request *request;
  655. struct vstor_packet *vstor_packet;
  656. int ret, i;
  657. int max_chns;
  658. bool process_sub_channels = false;
  659. stor_device = get_out_stor_device(device);
  660. if (!stor_device)
  661. return -ENODEV;
  662. request = &stor_device->init_request;
  663. vstor_packet = &request->vstor_packet;
  664. /*
  665. * Now, initiate the vsc/vsp initialization protocol on the open
  666. * channel
  667. */
  668. memset(request, 0, sizeof(struct storvsc_cmd_request));
  669. vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
  670. ret = storvsc_execute_vstor_op(device, request, true);
  671. if (ret)
  672. return ret;
  673. /*
  674. * Query host supported protocol version.
  675. */
  676. for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
  677. /* reuse the packet for version range supported */
  678. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  679. vstor_packet->operation =
  680. VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
  681. vstor_packet->version.major_minor =
  682. vmstor_protocols[i].protocol_version;
  683. /*
  684. * The revision number is only used in Windows; set it to 0.
  685. */
  686. vstor_packet->version.revision = 0;
  687. ret = storvsc_execute_vstor_op(device, request, false);
  688. if (ret != 0)
  689. return ret;
  690. if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
  691. return -EINVAL;
  692. if (vstor_packet->status == 0) {
  693. vmstor_proto_version =
  694. vmstor_protocols[i].protocol_version;
  695. sense_buffer_size =
  696. vmstor_protocols[i].sense_buffer_size;
  697. vmscsi_size_delta =
  698. vmstor_protocols[i].vmscsi_size_delta;
  699. break;
  700. }
  701. }
  702. if (vstor_packet->status != 0)
  703. return -EINVAL;
  704. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  705. vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
  706. ret = storvsc_execute_vstor_op(device, request, true);
  707. if (ret != 0)
  708. return ret;
  709. /*
  710. * Check to see if multi-channel support is there.
  711. * Hosts that implement protocol version of 5.1 and above
  712. * support multi-channel.
  713. */
  714. max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
  715. /*
  716. * Allocate state to manage the sub-channels.
  717. * We allocate an array based on the numbers of possible CPUs
  718. * (Hyper-V does not support cpu online/offline).
  719. * This Array will be sparseley populated with unique
  720. * channels - primary + sub-channels.
  721. * We will however populate all the slots to evenly distribute
  722. * the load.
  723. */
  724. stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *),
  725. GFP_KERNEL);
  726. if (stor_device->stor_chns == NULL)
  727. return -ENOMEM;
  728. stor_device->stor_chns[device->channel->target_cpu] = device->channel;
  729. cpumask_set_cpu(device->channel->target_cpu,
  730. &stor_device->alloced_cpus);
  731. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
  732. if (vstor_packet->storage_channel_properties.flags &
  733. STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
  734. process_sub_channels = true;
  735. }
  736. stor_device->max_transfer_bytes =
  737. vstor_packet->storage_channel_properties.max_transfer_bytes;
  738. if (!is_fc)
  739. goto done;
  740. /*
  741. * For FC devices retrieve FC HBA data.
  742. */
  743. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  744. vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
  745. ret = storvsc_execute_vstor_op(device, request, true);
  746. if (ret != 0)
  747. return ret;
  748. /*
  749. * Cache the currently active port and node ww names.
  750. */
  751. cache_wwn(stor_device, vstor_packet);
  752. done:
  753. memset(vstor_packet, 0, sizeof(struct vstor_packet));
  754. vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
  755. ret = storvsc_execute_vstor_op(device, request, true);
  756. if (ret != 0)
  757. return ret;
  758. if (process_sub_channels)
  759. handle_multichannel_storage(device, max_chns);
  760. return ret;
  761. }
  762. static void storvsc_handle_error(struct vmscsi_request *vm_srb,
  763. struct scsi_cmnd *scmnd,
  764. struct Scsi_Host *host,
  765. u8 asc, u8 ascq)
  766. {
  767. struct storvsc_scan_work *wrk;
  768. void (*process_err_fn)(struct work_struct *work);
  769. bool do_work = false;
  770. switch (SRB_STATUS(vm_srb->srb_status)) {
  771. case SRB_STATUS_ERROR:
  772. /*
  773. * Let upper layer deal with error when
  774. * sense message is present.
  775. */
  776. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)
  777. break;
  778. /*
  779. * If there is an error; offline the device since all
  780. * error recovery strategies would have already been
  781. * deployed on the host side. However, if the command
  782. * were a pass-through command deal with it appropriately.
  783. */
  784. switch (scmnd->cmnd[0]) {
  785. case ATA_16:
  786. case ATA_12:
  787. set_host_byte(scmnd, DID_PASSTHROUGH);
  788. break;
  789. /*
  790. * On Some Windows hosts TEST_UNIT_READY command can return
  791. * SRB_STATUS_ERROR, let the upper level code deal with it
  792. * based on the sense information.
  793. */
  794. case TEST_UNIT_READY:
  795. break;
  796. default:
  797. set_host_byte(scmnd, DID_ERROR);
  798. }
  799. break;
  800. case SRB_STATUS_INVALID_LUN:
  801. set_host_byte(scmnd, DID_NO_CONNECT);
  802. do_work = true;
  803. process_err_fn = storvsc_remove_lun;
  804. break;
  805. case SRB_STATUS_ABORTED:
  806. if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
  807. (asc == 0x2a) && (ascq == 0x9)) {
  808. do_work = true;
  809. process_err_fn = storvsc_device_scan;
  810. /*
  811. * Retry the I/O that trigerred this.
  812. */
  813. set_host_byte(scmnd, DID_REQUEUE);
  814. }
  815. break;
  816. }
  817. if (!do_work)
  818. return;
  819. /*
  820. * We need to schedule work to process this error; schedule it.
  821. */
  822. wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  823. if (!wrk) {
  824. set_host_byte(scmnd, DID_TARGET_FAILURE);
  825. return;
  826. }
  827. wrk->host = host;
  828. wrk->lun = vm_srb->lun;
  829. wrk->tgt_id = vm_srb->target_id;
  830. INIT_WORK(&wrk->work, process_err_fn);
  831. schedule_work(&wrk->work);
  832. }
  833. static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
  834. struct storvsc_device *stor_dev)
  835. {
  836. struct scsi_cmnd *scmnd = cmd_request->cmd;
  837. struct scsi_sense_hdr sense_hdr;
  838. struct vmscsi_request *vm_srb;
  839. u32 data_transfer_length;
  840. struct Scsi_Host *host;
  841. u32 payload_sz = cmd_request->payload_sz;
  842. void *payload = cmd_request->payload;
  843. host = stor_dev->host;
  844. vm_srb = &cmd_request->vstor_packet.vm_srb;
  845. data_transfer_length = vm_srb->data_transfer_length;
  846. scmnd->result = vm_srb->scsi_status;
  847. if (scmnd->result) {
  848. if (scsi_normalize_sense(scmnd->sense_buffer,
  849. SCSI_SENSE_BUFFERSIZE, &sense_hdr) &&
  850. !(sense_hdr.sense_key == NOT_READY &&
  851. sense_hdr.asc == 0x03A) &&
  852. do_logging(STORVSC_LOGGING_ERROR))
  853. scsi_print_sense_hdr(scmnd->device, "storvsc",
  854. &sense_hdr);
  855. }
  856. if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
  857. storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
  858. sense_hdr.ascq);
  859. /*
  860. * The Windows driver set data_transfer_length on
  861. * SRB_STATUS_DATA_OVERRUN. On other errors, this value
  862. * is untouched. In these cases we set it to 0.
  863. */
  864. if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
  865. data_transfer_length = 0;
  866. }
  867. scsi_set_resid(scmnd,
  868. cmd_request->payload->range.len - data_transfer_length);
  869. scmnd->scsi_done(scmnd);
  870. if (payload_sz >
  871. sizeof(struct vmbus_channel_packet_multipage_buffer))
  872. kfree(payload);
  873. }
  874. static void storvsc_on_io_completion(struct storvsc_device *stor_device,
  875. struct vstor_packet *vstor_packet,
  876. struct storvsc_cmd_request *request)
  877. {
  878. struct vstor_packet *stor_pkt;
  879. struct hv_device *device = stor_device->device;
  880. stor_pkt = &request->vstor_packet;
  881. /*
  882. * The current SCSI handling on the host side does
  883. * not correctly handle:
  884. * INQUIRY command with page code parameter set to 0x80
  885. * MODE_SENSE command with cmd[2] == 0x1c
  886. *
  887. * Setup srb and scsi status so this won't be fatal.
  888. * We do this so we can distinguish truly fatal failues
  889. * (srb status == 0x4) and off-line the device in that case.
  890. */
  891. if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
  892. (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
  893. vstor_packet->vm_srb.scsi_status = 0;
  894. vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
  895. }
  896. /* Copy over the status...etc */
  897. stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
  898. stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
  899. stor_pkt->vm_srb.sense_info_length =
  900. vstor_packet->vm_srb.sense_info_length;
  901. if (vstor_packet->vm_srb.scsi_status != 0 ||
  902. vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS)
  903. storvsc_log(device, STORVSC_LOGGING_WARN,
  904. "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
  905. stor_pkt->vm_srb.cdb[0],
  906. vstor_packet->vm_srb.scsi_status,
  907. vstor_packet->vm_srb.srb_status);
  908. if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
  909. /* CHECK_CONDITION */
  910. if (vstor_packet->vm_srb.srb_status &
  911. SRB_STATUS_AUTOSENSE_VALID) {
  912. /* autosense data available */
  913. storvsc_log(device, STORVSC_LOGGING_WARN,
  914. "stor pkt %p autosense data valid - len %d\n",
  915. request, vstor_packet->vm_srb.sense_info_length);
  916. memcpy(request->cmd->sense_buffer,
  917. vstor_packet->vm_srb.sense_data,
  918. vstor_packet->vm_srb.sense_info_length);
  919. }
  920. }
  921. stor_pkt->vm_srb.data_transfer_length =
  922. vstor_packet->vm_srb.data_transfer_length;
  923. storvsc_command_completion(request, stor_device);
  924. if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
  925. stor_device->drain_notify)
  926. wake_up(&stor_device->waiting_to_drain);
  927. }
  928. static void storvsc_on_receive(struct storvsc_device *stor_device,
  929. struct vstor_packet *vstor_packet,
  930. struct storvsc_cmd_request *request)
  931. {
  932. struct storvsc_scan_work *work;
  933. switch (vstor_packet->operation) {
  934. case VSTOR_OPERATION_COMPLETE_IO:
  935. storvsc_on_io_completion(stor_device, vstor_packet, request);
  936. break;
  937. case VSTOR_OPERATION_REMOVE_DEVICE:
  938. case VSTOR_OPERATION_ENUMERATE_BUS:
  939. work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
  940. if (!work)
  941. return;
  942. INIT_WORK(&work->work, storvsc_host_scan);
  943. work->host = stor_device->host;
  944. schedule_work(&work->work);
  945. break;
  946. case VSTOR_OPERATION_FCHBA_DATA:
  947. cache_wwn(stor_device, vstor_packet);
  948. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  949. fc_host_node_name(stor_device->host) = stor_device->node_name;
  950. fc_host_port_name(stor_device->host) = stor_device->port_name;
  951. #endif
  952. break;
  953. default:
  954. break;
  955. }
  956. }
  957. static void storvsc_on_channel_callback(void *context)
  958. {
  959. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  960. const struct vmpacket_descriptor *desc;
  961. struct hv_device *device;
  962. struct storvsc_device *stor_device;
  963. if (channel->primary_channel != NULL)
  964. device = channel->primary_channel->device_obj;
  965. else
  966. device = channel->device_obj;
  967. stor_device = get_in_stor_device(device);
  968. if (!stor_device)
  969. return;
  970. foreach_vmbus_pkt(desc, channel) {
  971. void *packet = hv_pkt_data(desc);
  972. struct storvsc_cmd_request *request;
  973. request = (struct storvsc_cmd_request *)
  974. ((unsigned long)desc->trans_id);
  975. if (request == &stor_device->init_request ||
  976. request == &stor_device->reset_request) {
  977. memcpy(&request->vstor_packet, packet,
  978. (sizeof(struct vstor_packet) - vmscsi_size_delta));
  979. complete(&request->wait_event);
  980. } else {
  981. storvsc_on_receive(stor_device, packet, request);
  982. }
  983. }
  984. }
  985. static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
  986. bool is_fc)
  987. {
  988. struct vmstorage_channel_properties props;
  989. int ret;
  990. memset(&props, 0, sizeof(struct vmstorage_channel_properties));
  991. ret = vmbus_open(device->channel,
  992. ring_size,
  993. ring_size,
  994. (void *)&props,
  995. sizeof(struct vmstorage_channel_properties),
  996. storvsc_on_channel_callback, device->channel);
  997. if (ret != 0)
  998. return ret;
  999. ret = storvsc_channel_init(device, is_fc);
  1000. return ret;
  1001. }
  1002. static int storvsc_dev_remove(struct hv_device *device)
  1003. {
  1004. struct storvsc_device *stor_device;
  1005. stor_device = hv_get_drvdata(device);
  1006. stor_device->destroy = true;
  1007. /* Make sure flag is set before waiting */
  1008. wmb();
  1009. /*
  1010. * At this point, all outbound traffic should be disable. We
  1011. * only allow inbound traffic (responses) to proceed so that
  1012. * outstanding requests can be completed.
  1013. */
  1014. storvsc_wait_to_drain(stor_device);
  1015. /*
  1016. * Since we have already drained, we don't need to busy wait
  1017. * as was done in final_release_stor_device()
  1018. * Note that we cannot set the ext pointer to NULL until
  1019. * we have drained - to drain the outgoing packets, we need to
  1020. * allow incoming packets.
  1021. */
  1022. hv_set_drvdata(device, NULL);
  1023. /* Close the channel */
  1024. vmbus_close(device->channel);
  1025. kfree(stor_device->stor_chns);
  1026. kfree(stor_device);
  1027. return 0;
  1028. }
  1029. static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
  1030. u16 q_num)
  1031. {
  1032. u16 slot = 0;
  1033. u16 hash_qnum;
  1034. struct cpumask alloced_mask;
  1035. int num_channels, tgt_cpu;
  1036. if (stor_device->num_sc == 0)
  1037. return stor_device->device->channel;
  1038. /*
  1039. * Our channel array is sparsley populated and we
  1040. * initiated I/O on a processor/hw-q that does not
  1041. * currently have a designated channel. Fix this.
  1042. * The strategy is simple:
  1043. * I. Ensure NUMA locality
  1044. * II. Distribute evenly (best effort)
  1045. * III. Mapping is persistent.
  1046. */
  1047. cpumask_and(&alloced_mask, &stor_device->alloced_cpus,
  1048. cpumask_of_node(cpu_to_node(q_num)));
  1049. num_channels = cpumask_weight(&alloced_mask);
  1050. if (num_channels == 0)
  1051. return stor_device->device->channel;
  1052. hash_qnum = q_num;
  1053. while (hash_qnum >= num_channels)
  1054. hash_qnum -= num_channels;
  1055. for_each_cpu(tgt_cpu, &alloced_mask) {
  1056. if (slot == hash_qnum)
  1057. break;
  1058. slot++;
  1059. }
  1060. stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
  1061. return stor_device->stor_chns[q_num];
  1062. }
  1063. static int storvsc_do_io(struct hv_device *device,
  1064. struct storvsc_cmd_request *request, u16 q_num)
  1065. {
  1066. struct storvsc_device *stor_device;
  1067. struct vstor_packet *vstor_packet;
  1068. struct vmbus_channel *outgoing_channel;
  1069. int ret = 0;
  1070. struct cpumask alloced_mask;
  1071. int tgt_cpu;
  1072. vstor_packet = &request->vstor_packet;
  1073. stor_device = get_out_stor_device(device);
  1074. if (!stor_device)
  1075. return -ENODEV;
  1076. request->device = device;
  1077. /*
  1078. * Select an an appropriate channel to send the request out.
  1079. */
  1080. if (stor_device->stor_chns[q_num] != NULL) {
  1081. outgoing_channel = stor_device->stor_chns[q_num];
  1082. if (outgoing_channel->target_cpu == smp_processor_id()) {
  1083. /*
  1084. * Ideally, we want to pick a different channel if
  1085. * available on the same NUMA node.
  1086. */
  1087. cpumask_and(&alloced_mask, &stor_device->alloced_cpus,
  1088. cpumask_of_node(cpu_to_node(q_num)));
  1089. for_each_cpu(tgt_cpu, &alloced_mask) {
  1090. if (tgt_cpu != outgoing_channel->target_cpu) {
  1091. outgoing_channel =
  1092. stor_device->stor_chns[tgt_cpu];
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. } else {
  1098. outgoing_channel = get_og_chn(stor_device, q_num);
  1099. }
  1100. vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
  1101. vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
  1102. vmscsi_size_delta);
  1103. vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
  1104. vstor_packet->vm_srb.data_transfer_length =
  1105. request->payload->range.len;
  1106. vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
  1107. if (request->payload->range.len) {
  1108. ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
  1109. request->payload, request->payload_sz,
  1110. vstor_packet,
  1111. (sizeof(struct vstor_packet) -
  1112. vmscsi_size_delta),
  1113. (unsigned long)request);
  1114. } else {
  1115. ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
  1116. (sizeof(struct vstor_packet) -
  1117. vmscsi_size_delta),
  1118. (unsigned long)request,
  1119. VM_PKT_DATA_INBAND,
  1120. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1121. }
  1122. if (ret != 0)
  1123. return ret;
  1124. atomic_inc(&stor_device->num_outstanding_req);
  1125. return ret;
  1126. }
  1127. static int storvsc_device_alloc(struct scsi_device *sdevice)
  1128. {
  1129. /*
  1130. * Set blist flag to permit the reading of the VPD pages even when
  1131. * the target may claim SPC-2 compliance. MSFT targets currently
  1132. * claim SPC-2 compliance while they implement post SPC-2 features.
  1133. * With this flag we can correctly handle WRITE_SAME_16 issues.
  1134. *
  1135. * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
  1136. * still supports REPORT LUN.
  1137. */
  1138. sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
  1139. return 0;
  1140. }
  1141. static int storvsc_device_configure(struct scsi_device *sdevice)
  1142. {
  1143. blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
  1144. blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
  1145. /* Ensure there are no gaps in presented sgls */
  1146. blk_queue_virt_boundary(sdevice->request_queue, PAGE_SIZE - 1);
  1147. sdevice->no_write_same = 1;
  1148. /*
  1149. * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
  1150. * if the device is a MSFT virtual device. If the host is
  1151. * WIN10 or newer, allow write_same.
  1152. */
  1153. if (!strncmp(sdevice->vendor, "Msft", 4)) {
  1154. switch (vmstor_proto_version) {
  1155. case VMSTOR_PROTO_VERSION_WIN8:
  1156. case VMSTOR_PROTO_VERSION_WIN8_1:
  1157. sdevice->scsi_level = SCSI_SPC_3;
  1158. break;
  1159. }
  1160. if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
  1161. sdevice->no_write_same = 0;
  1162. }
  1163. return 0;
  1164. }
  1165. static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
  1166. sector_t capacity, int *info)
  1167. {
  1168. sector_t nsect = capacity;
  1169. sector_t cylinders = nsect;
  1170. int heads, sectors_pt;
  1171. /*
  1172. * We are making up these values; let us keep it simple.
  1173. */
  1174. heads = 0xff;
  1175. sectors_pt = 0x3f; /* Sectors per track */
  1176. sector_div(cylinders, heads * sectors_pt);
  1177. if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
  1178. cylinders = 0xffff;
  1179. info[0] = heads;
  1180. info[1] = sectors_pt;
  1181. info[2] = (int)cylinders;
  1182. return 0;
  1183. }
  1184. static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
  1185. {
  1186. struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
  1187. struct hv_device *device = host_dev->dev;
  1188. struct storvsc_device *stor_device;
  1189. struct storvsc_cmd_request *request;
  1190. struct vstor_packet *vstor_packet;
  1191. int ret, t;
  1192. stor_device = get_out_stor_device(device);
  1193. if (!stor_device)
  1194. return FAILED;
  1195. request = &stor_device->reset_request;
  1196. vstor_packet = &request->vstor_packet;
  1197. init_completion(&request->wait_event);
  1198. vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
  1199. vstor_packet->flags = REQUEST_COMPLETION_FLAG;
  1200. vstor_packet->vm_srb.path_id = stor_device->path_id;
  1201. ret = vmbus_sendpacket(device->channel, vstor_packet,
  1202. (sizeof(struct vstor_packet) -
  1203. vmscsi_size_delta),
  1204. (unsigned long)&stor_device->reset_request,
  1205. VM_PKT_DATA_INBAND,
  1206. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1207. if (ret != 0)
  1208. return FAILED;
  1209. t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
  1210. if (t == 0)
  1211. return TIMEOUT_ERROR;
  1212. /*
  1213. * At this point, all outstanding requests in the adapter
  1214. * should have been flushed out and return to us
  1215. * There is a potential race here where the host may be in
  1216. * the process of responding when we return from here.
  1217. * Just wait for all in-transit packets to be accounted for
  1218. * before we return from here.
  1219. */
  1220. storvsc_wait_to_drain(stor_device);
  1221. return SUCCESS;
  1222. }
  1223. /*
  1224. * The host guarantees to respond to each command, although I/O latencies might
  1225. * be unbounded on Azure. Reset the timer unconditionally to give the host a
  1226. * chance to perform EH.
  1227. */
  1228. static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
  1229. {
  1230. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1231. if (scmnd->device->host->transportt == fc_transport_template)
  1232. return fc_eh_timed_out(scmnd);
  1233. #endif
  1234. return BLK_EH_RESET_TIMER;
  1235. }
  1236. static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
  1237. {
  1238. bool allowed = true;
  1239. u8 scsi_op = scmnd->cmnd[0];
  1240. switch (scsi_op) {
  1241. /* the host does not handle WRITE_SAME, log accident usage */
  1242. case WRITE_SAME:
  1243. /*
  1244. * smartd sends this command and the host does not handle
  1245. * this. So, don't send it.
  1246. */
  1247. case SET_WINDOW:
  1248. scmnd->result = ILLEGAL_REQUEST << 16;
  1249. allowed = false;
  1250. break;
  1251. default:
  1252. break;
  1253. }
  1254. return allowed;
  1255. }
  1256. static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  1257. {
  1258. int ret;
  1259. struct hv_host_device *host_dev = shost_priv(host);
  1260. struct hv_device *dev = host_dev->dev;
  1261. struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
  1262. int i;
  1263. struct scatterlist *sgl;
  1264. unsigned int sg_count = 0;
  1265. struct vmscsi_request *vm_srb;
  1266. struct scatterlist *cur_sgl;
  1267. struct vmbus_packet_mpb_array *payload;
  1268. u32 payload_sz;
  1269. u32 length;
  1270. if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
  1271. /*
  1272. * On legacy hosts filter unimplemented commands.
  1273. * Future hosts are expected to correctly handle
  1274. * unsupported commands. Furthermore, it is
  1275. * possible that some of the currently
  1276. * unsupported commands maybe supported in
  1277. * future versions of the host.
  1278. */
  1279. if (!storvsc_scsi_cmd_ok(scmnd)) {
  1280. scmnd->scsi_done(scmnd);
  1281. return 0;
  1282. }
  1283. }
  1284. /* Setup the cmd request */
  1285. cmd_request->cmd = scmnd;
  1286. vm_srb = &cmd_request->vstor_packet.vm_srb;
  1287. vm_srb->win8_extension.time_out_value = 60;
  1288. vm_srb->win8_extension.srb_flags |=
  1289. SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1290. if (scmnd->device->tagged_supported) {
  1291. vm_srb->win8_extension.srb_flags |=
  1292. (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
  1293. vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
  1294. vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
  1295. }
  1296. /* Build the SRB */
  1297. switch (scmnd->sc_data_direction) {
  1298. case DMA_TO_DEVICE:
  1299. vm_srb->data_in = WRITE_TYPE;
  1300. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
  1301. break;
  1302. case DMA_FROM_DEVICE:
  1303. vm_srb->data_in = READ_TYPE;
  1304. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
  1305. break;
  1306. case DMA_NONE:
  1307. vm_srb->data_in = UNKNOWN_TYPE;
  1308. vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
  1309. break;
  1310. default:
  1311. /*
  1312. * This is DMA_BIDIRECTIONAL or something else we are never
  1313. * supposed to see here.
  1314. */
  1315. WARN(1, "Unexpected data direction: %d\n",
  1316. scmnd->sc_data_direction);
  1317. return -EINVAL;
  1318. }
  1319. vm_srb->port_number = host_dev->port;
  1320. vm_srb->path_id = scmnd->device->channel;
  1321. vm_srb->target_id = scmnd->device->id;
  1322. vm_srb->lun = scmnd->device->lun;
  1323. vm_srb->cdb_length = scmnd->cmd_len;
  1324. memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
  1325. sgl = (struct scatterlist *)scsi_sglist(scmnd);
  1326. sg_count = scsi_sg_count(scmnd);
  1327. length = scsi_bufflen(scmnd);
  1328. payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
  1329. payload_sz = sizeof(cmd_request->mpb);
  1330. if (sg_count) {
  1331. if (sg_count > MAX_PAGE_BUFFER_COUNT) {
  1332. payload_sz = (sg_count * sizeof(u64) +
  1333. sizeof(struct vmbus_packet_mpb_array));
  1334. payload = kzalloc(payload_sz, GFP_ATOMIC);
  1335. if (!payload)
  1336. return SCSI_MLQUEUE_DEVICE_BUSY;
  1337. }
  1338. payload->range.len = length;
  1339. payload->range.offset = sgl[0].offset;
  1340. cur_sgl = sgl;
  1341. for (i = 0; i < sg_count; i++) {
  1342. payload->range.pfn_array[i] =
  1343. page_to_pfn(sg_page((cur_sgl)));
  1344. cur_sgl = sg_next(cur_sgl);
  1345. }
  1346. }
  1347. cmd_request->payload = payload;
  1348. cmd_request->payload_sz = payload_sz;
  1349. /* Invokes the vsc to start an IO */
  1350. ret = storvsc_do_io(dev, cmd_request, get_cpu());
  1351. put_cpu();
  1352. if (ret == -EAGAIN) {
  1353. if (payload_sz > sizeof(cmd_request->mpb))
  1354. kfree(payload);
  1355. /* no more space */
  1356. return SCSI_MLQUEUE_DEVICE_BUSY;
  1357. }
  1358. return 0;
  1359. }
  1360. static struct scsi_host_template scsi_driver = {
  1361. .module = THIS_MODULE,
  1362. .name = "storvsc_host_t",
  1363. .cmd_size = sizeof(struct storvsc_cmd_request),
  1364. .bios_param = storvsc_get_chs,
  1365. .queuecommand = storvsc_queuecommand,
  1366. .eh_host_reset_handler = storvsc_host_reset_handler,
  1367. .proc_name = "storvsc_host",
  1368. .eh_timed_out = storvsc_eh_timed_out,
  1369. .slave_alloc = storvsc_device_alloc,
  1370. .slave_configure = storvsc_device_configure,
  1371. .cmd_per_lun = 2048,
  1372. .this_id = -1,
  1373. .use_clustering = ENABLE_CLUSTERING,
  1374. /* Make sure we dont get a sg segment crosses a page boundary */
  1375. .dma_boundary = PAGE_SIZE-1,
  1376. .no_write_same = 1,
  1377. .track_queue_depth = 1,
  1378. };
  1379. enum {
  1380. SCSI_GUID,
  1381. IDE_GUID,
  1382. SFC_GUID,
  1383. };
  1384. static const struct hv_vmbus_device_id id_table[] = {
  1385. /* SCSI guid */
  1386. { HV_SCSI_GUID,
  1387. .driver_data = SCSI_GUID
  1388. },
  1389. /* IDE guid */
  1390. { HV_IDE_GUID,
  1391. .driver_data = IDE_GUID
  1392. },
  1393. /* Fibre Channel GUID */
  1394. {
  1395. HV_SYNTHFC_GUID,
  1396. .driver_data = SFC_GUID
  1397. },
  1398. { },
  1399. };
  1400. MODULE_DEVICE_TABLE(vmbus, id_table);
  1401. static int storvsc_probe(struct hv_device *device,
  1402. const struct hv_vmbus_device_id *dev_id)
  1403. {
  1404. int ret;
  1405. int num_cpus = num_online_cpus();
  1406. struct Scsi_Host *host;
  1407. struct hv_host_device *host_dev;
  1408. bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
  1409. bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
  1410. int target = 0;
  1411. struct storvsc_device *stor_device;
  1412. int max_luns_per_target;
  1413. int max_targets;
  1414. int max_channels;
  1415. int max_sub_channels = 0;
  1416. /*
  1417. * Based on the windows host we are running on,
  1418. * set state to properly communicate with the host.
  1419. */
  1420. if (vmbus_proto_version < VERSION_WIN8) {
  1421. max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1422. max_targets = STORVSC_IDE_MAX_TARGETS;
  1423. max_channels = STORVSC_IDE_MAX_CHANNELS;
  1424. } else {
  1425. max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
  1426. max_targets = STORVSC_MAX_TARGETS;
  1427. max_channels = STORVSC_MAX_CHANNELS;
  1428. /*
  1429. * On Windows8 and above, we support sub-channels for storage
  1430. * on SCSI and FC controllers.
  1431. * The number of sub-channels offerred is based on the number of
  1432. * VCPUs in the guest.
  1433. */
  1434. if (!dev_is_ide)
  1435. max_sub_channels =
  1436. (num_cpus - 1) / storvsc_vcpus_per_sub_channel;
  1437. }
  1438. scsi_driver.can_queue = (max_outstanding_req_per_channel *
  1439. (max_sub_channels + 1));
  1440. host = scsi_host_alloc(&scsi_driver,
  1441. sizeof(struct hv_host_device));
  1442. if (!host)
  1443. return -ENOMEM;
  1444. host_dev = shost_priv(host);
  1445. memset(host_dev, 0, sizeof(struct hv_host_device));
  1446. host_dev->port = host->host_no;
  1447. host_dev->dev = device;
  1448. stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
  1449. if (!stor_device) {
  1450. ret = -ENOMEM;
  1451. goto err_out0;
  1452. }
  1453. stor_device->destroy = false;
  1454. stor_device->open_sub_channel = false;
  1455. init_waitqueue_head(&stor_device->waiting_to_drain);
  1456. stor_device->device = device;
  1457. stor_device->host = host;
  1458. hv_set_drvdata(device, stor_device);
  1459. stor_device->port_number = host->host_no;
  1460. ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
  1461. if (ret)
  1462. goto err_out1;
  1463. host_dev->path = stor_device->path_id;
  1464. host_dev->target = stor_device->target_id;
  1465. switch (dev_id->driver_data) {
  1466. case SFC_GUID:
  1467. host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
  1468. host->max_id = STORVSC_FC_MAX_TARGETS;
  1469. host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
  1470. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1471. host->transportt = fc_transport_template;
  1472. #endif
  1473. break;
  1474. case SCSI_GUID:
  1475. host->max_lun = max_luns_per_target;
  1476. host->max_id = max_targets;
  1477. host->max_channel = max_channels - 1;
  1478. break;
  1479. default:
  1480. host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
  1481. host->max_id = STORVSC_IDE_MAX_TARGETS;
  1482. host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
  1483. break;
  1484. }
  1485. /* max cmd length */
  1486. host->max_cmd_len = STORVSC_MAX_CMD_LEN;
  1487. /*
  1488. * set the table size based on the info we got
  1489. * from the host.
  1490. */
  1491. host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
  1492. /*
  1493. * Set the number of HW queues we are supporting.
  1494. */
  1495. if (stor_device->num_sc != 0)
  1496. host->nr_hw_queues = stor_device->num_sc + 1;
  1497. /* Register the HBA and start the scsi bus scan */
  1498. ret = scsi_add_host(host, &device->device);
  1499. if (ret != 0)
  1500. goto err_out2;
  1501. if (!dev_is_ide) {
  1502. scsi_scan_host(host);
  1503. } else {
  1504. target = (device->dev_instance.b[5] << 8 |
  1505. device->dev_instance.b[4]);
  1506. ret = scsi_add_device(host, 0, target, 0);
  1507. if (ret)
  1508. goto err_out3;
  1509. }
  1510. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1511. if (host->transportt == fc_transport_template) {
  1512. struct fc_rport_identifiers ids = {
  1513. .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
  1514. };
  1515. fc_host_node_name(host) = stor_device->node_name;
  1516. fc_host_port_name(host) = stor_device->port_name;
  1517. stor_device->rport = fc_remote_port_add(host, 0, &ids);
  1518. if (!stor_device->rport) {
  1519. ret = -ENOMEM;
  1520. goto err_out3;
  1521. }
  1522. }
  1523. #endif
  1524. return 0;
  1525. err_out3:
  1526. scsi_remove_host(host);
  1527. err_out2:
  1528. /*
  1529. * Once we have connected with the host, we would need to
  1530. * to invoke storvsc_dev_remove() to rollback this state and
  1531. * this call also frees up the stor_device; hence the jump around
  1532. * err_out1 label.
  1533. */
  1534. storvsc_dev_remove(device);
  1535. goto err_out0;
  1536. err_out1:
  1537. kfree(stor_device->stor_chns);
  1538. kfree(stor_device);
  1539. err_out0:
  1540. scsi_host_put(host);
  1541. return ret;
  1542. }
  1543. static int storvsc_remove(struct hv_device *dev)
  1544. {
  1545. struct storvsc_device *stor_device = hv_get_drvdata(dev);
  1546. struct Scsi_Host *host = stor_device->host;
  1547. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1548. if (host->transportt == fc_transport_template) {
  1549. fc_remote_port_delete(stor_device->rport);
  1550. fc_remove_host(host);
  1551. }
  1552. #endif
  1553. scsi_remove_host(host);
  1554. storvsc_dev_remove(dev);
  1555. scsi_host_put(host);
  1556. return 0;
  1557. }
  1558. static struct hv_driver storvsc_drv = {
  1559. .name = KBUILD_MODNAME,
  1560. .id_table = id_table,
  1561. .probe = storvsc_probe,
  1562. .remove = storvsc_remove,
  1563. };
  1564. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1565. static struct fc_function_template fc_transport_functions = {
  1566. .show_host_node_name = 1,
  1567. .show_host_port_name = 1,
  1568. };
  1569. #endif
  1570. static int __init storvsc_drv_init(void)
  1571. {
  1572. int ret;
  1573. /*
  1574. * Divide the ring buffer data size (which is 1 page less
  1575. * than the ring buffer size since that page is reserved for
  1576. * the ring buffer indices) by the max request size (which is
  1577. * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
  1578. */
  1579. max_outstanding_req_per_channel =
  1580. ((storvsc_ringbuffer_size - PAGE_SIZE) /
  1581. ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
  1582. sizeof(struct vstor_packet) + sizeof(u64) -
  1583. vmscsi_size_delta,
  1584. sizeof(u64)));
  1585. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1586. fc_transport_template = fc_attach_transport(&fc_transport_functions);
  1587. if (!fc_transport_template)
  1588. return -ENODEV;
  1589. #endif
  1590. ret = vmbus_driver_register(&storvsc_drv);
  1591. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1592. if (ret)
  1593. fc_release_transport(fc_transport_template);
  1594. #endif
  1595. return ret;
  1596. }
  1597. static void __exit storvsc_drv_exit(void)
  1598. {
  1599. vmbus_driver_unregister(&storvsc_drv);
  1600. #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
  1601. fc_release_transport(fc_transport_template);
  1602. #endif
  1603. }
  1604. MODULE_LICENSE("GPL");
  1605. MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
  1606. module_init(storvsc_drv_init);
  1607. module_exit(storvsc_drv_exit);