storvsc_drv.c 51 KB

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