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