vmbus_drv.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/sysctl.h>
  29. #include <linux/slab.h>
  30. #include <linux/acpi.h>
  31. #include <linux/completion.h>
  32. #include <linux/hyperv.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/cpu.h>
  36. #include <linux/sched/task_stack.h>
  37. #include <asm/mshyperv.h>
  38. #include <linux/notifier.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/screen_info.h>
  41. #include <linux/kdebug.h>
  42. #include <linux/efi.h>
  43. #include <linux/random.h>
  44. #include "hyperv_vmbus.h"
  45. struct vmbus_dynid {
  46. struct list_head node;
  47. struct hv_vmbus_device_id id;
  48. };
  49. static struct acpi_device *hv_acpi_dev;
  50. static struct completion probe_event;
  51. static int hyperv_cpuhp_online;
  52. static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
  53. void *args)
  54. {
  55. struct pt_regs *regs;
  56. regs = current_pt_regs();
  57. hyperv_report_panic(regs, val);
  58. return NOTIFY_DONE;
  59. }
  60. static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
  61. void *args)
  62. {
  63. struct die_args *die = (struct die_args *)args;
  64. struct pt_regs *regs = die->regs;
  65. hyperv_report_panic(regs, val);
  66. return NOTIFY_DONE;
  67. }
  68. static struct notifier_block hyperv_die_block = {
  69. .notifier_call = hyperv_die_event,
  70. };
  71. static struct notifier_block hyperv_panic_block = {
  72. .notifier_call = hyperv_panic_event,
  73. };
  74. static const char *fb_mmio_name = "fb_range";
  75. static struct resource *fb_mmio;
  76. static struct resource *hyperv_mmio;
  77. static DEFINE_SEMAPHORE(hyperv_mmio_lock);
  78. static int vmbus_exists(void)
  79. {
  80. if (hv_acpi_dev == NULL)
  81. return -ENODEV;
  82. return 0;
  83. }
  84. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  85. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  86. {
  87. int i;
  88. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  89. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  90. }
  91. static u8 channel_monitor_group(const struct vmbus_channel *channel)
  92. {
  93. return (u8)channel->offermsg.monitorid / 32;
  94. }
  95. static u8 channel_monitor_offset(const struct vmbus_channel *channel)
  96. {
  97. return (u8)channel->offermsg.monitorid % 32;
  98. }
  99. static u32 channel_pending(const struct vmbus_channel *channel,
  100. const struct hv_monitor_page *monitor_page)
  101. {
  102. u8 monitor_group = channel_monitor_group(channel);
  103. return monitor_page->trigger_group[monitor_group].pending;
  104. }
  105. static u32 channel_latency(const struct vmbus_channel *channel,
  106. const struct hv_monitor_page *monitor_page)
  107. {
  108. u8 monitor_group = channel_monitor_group(channel);
  109. u8 monitor_offset = channel_monitor_offset(channel);
  110. return monitor_page->latency[monitor_group][monitor_offset];
  111. }
  112. static u32 channel_conn_id(struct vmbus_channel *channel,
  113. struct hv_monitor_page *monitor_page)
  114. {
  115. u8 monitor_group = channel_monitor_group(channel);
  116. u8 monitor_offset = channel_monitor_offset(channel);
  117. return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
  118. }
  119. static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
  120. char *buf)
  121. {
  122. struct hv_device *hv_dev = device_to_hv_device(dev);
  123. if (!hv_dev->channel)
  124. return -ENODEV;
  125. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
  126. }
  127. static DEVICE_ATTR_RO(id);
  128. static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
  129. char *buf)
  130. {
  131. struct hv_device *hv_dev = device_to_hv_device(dev);
  132. if (!hv_dev->channel)
  133. return -ENODEV;
  134. return sprintf(buf, "%d\n", hv_dev->channel->state);
  135. }
  136. static DEVICE_ATTR_RO(state);
  137. static ssize_t monitor_id_show(struct device *dev,
  138. struct device_attribute *dev_attr, char *buf)
  139. {
  140. struct hv_device *hv_dev = device_to_hv_device(dev);
  141. if (!hv_dev->channel)
  142. return -ENODEV;
  143. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
  144. }
  145. static DEVICE_ATTR_RO(monitor_id);
  146. static ssize_t class_id_show(struct device *dev,
  147. struct device_attribute *dev_attr, char *buf)
  148. {
  149. struct hv_device *hv_dev = device_to_hv_device(dev);
  150. if (!hv_dev->channel)
  151. return -ENODEV;
  152. return sprintf(buf, "{%pUl}\n",
  153. hv_dev->channel->offermsg.offer.if_type.b);
  154. }
  155. static DEVICE_ATTR_RO(class_id);
  156. static ssize_t device_id_show(struct device *dev,
  157. struct device_attribute *dev_attr, char *buf)
  158. {
  159. struct hv_device *hv_dev = device_to_hv_device(dev);
  160. if (!hv_dev->channel)
  161. return -ENODEV;
  162. return sprintf(buf, "{%pUl}\n",
  163. hv_dev->channel->offermsg.offer.if_instance.b);
  164. }
  165. static DEVICE_ATTR_RO(device_id);
  166. static ssize_t modalias_show(struct device *dev,
  167. struct device_attribute *dev_attr, char *buf)
  168. {
  169. struct hv_device *hv_dev = device_to_hv_device(dev);
  170. char alias_name[VMBUS_ALIAS_LEN + 1];
  171. print_alias_name(hv_dev, alias_name);
  172. return sprintf(buf, "vmbus:%s\n", alias_name);
  173. }
  174. static DEVICE_ATTR_RO(modalias);
  175. static ssize_t server_monitor_pending_show(struct device *dev,
  176. struct device_attribute *dev_attr,
  177. char *buf)
  178. {
  179. struct hv_device *hv_dev = device_to_hv_device(dev);
  180. if (!hv_dev->channel)
  181. return -ENODEV;
  182. return sprintf(buf, "%d\n",
  183. channel_pending(hv_dev->channel,
  184. vmbus_connection.monitor_pages[1]));
  185. }
  186. static DEVICE_ATTR_RO(server_monitor_pending);
  187. static ssize_t client_monitor_pending_show(struct device *dev,
  188. struct device_attribute *dev_attr,
  189. char *buf)
  190. {
  191. struct hv_device *hv_dev = device_to_hv_device(dev);
  192. if (!hv_dev->channel)
  193. return -ENODEV;
  194. return sprintf(buf, "%d\n",
  195. channel_pending(hv_dev->channel,
  196. vmbus_connection.monitor_pages[1]));
  197. }
  198. static DEVICE_ATTR_RO(client_monitor_pending);
  199. static ssize_t server_monitor_latency_show(struct device *dev,
  200. struct device_attribute *dev_attr,
  201. char *buf)
  202. {
  203. struct hv_device *hv_dev = device_to_hv_device(dev);
  204. if (!hv_dev->channel)
  205. return -ENODEV;
  206. return sprintf(buf, "%d\n",
  207. channel_latency(hv_dev->channel,
  208. vmbus_connection.monitor_pages[0]));
  209. }
  210. static DEVICE_ATTR_RO(server_monitor_latency);
  211. static ssize_t client_monitor_latency_show(struct device *dev,
  212. struct device_attribute *dev_attr,
  213. char *buf)
  214. {
  215. struct hv_device *hv_dev = device_to_hv_device(dev);
  216. if (!hv_dev->channel)
  217. return -ENODEV;
  218. return sprintf(buf, "%d\n",
  219. channel_latency(hv_dev->channel,
  220. vmbus_connection.monitor_pages[1]));
  221. }
  222. static DEVICE_ATTR_RO(client_monitor_latency);
  223. static ssize_t server_monitor_conn_id_show(struct device *dev,
  224. struct device_attribute *dev_attr,
  225. char *buf)
  226. {
  227. struct hv_device *hv_dev = device_to_hv_device(dev);
  228. if (!hv_dev->channel)
  229. return -ENODEV;
  230. return sprintf(buf, "%d\n",
  231. channel_conn_id(hv_dev->channel,
  232. vmbus_connection.monitor_pages[0]));
  233. }
  234. static DEVICE_ATTR_RO(server_monitor_conn_id);
  235. static ssize_t client_monitor_conn_id_show(struct device *dev,
  236. struct device_attribute *dev_attr,
  237. char *buf)
  238. {
  239. struct hv_device *hv_dev = device_to_hv_device(dev);
  240. if (!hv_dev->channel)
  241. return -ENODEV;
  242. return sprintf(buf, "%d\n",
  243. channel_conn_id(hv_dev->channel,
  244. vmbus_connection.monitor_pages[1]));
  245. }
  246. static DEVICE_ATTR_RO(client_monitor_conn_id);
  247. static ssize_t out_intr_mask_show(struct device *dev,
  248. struct device_attribute *dev_attr, char *buf)
  249. {
  250. struct hv_device *hv_dev = device_to_hv_device(dev);
  251. struct hv_ring_buffer_debug_info outbound;
  252. if (!hv_dev->channel)
  253. return -ENODEV;
  254. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  255. return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
  256. }
  257. static DEVICE_ATTR_RO(out_intr_mask);
  258. static ssize_t out_read_index_show(struct device *dev,
  259. struct device_attribute *dev_attr, char *buf)
  260. {
  261. struct hv_device *hv_dev = device_to_hv_device(dev);
  262. struct hv_ring_buffer_debug_info outbound;
  263. if (!hv_dev->channel)
  264. return -ENODEV;
  265. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  266. return sprintf(buf, "%d\n", outbound.current_read_index);
  267. }
  268. static DEVICE_ATTR_RO(out_read_index);
  269. static ssize_t out_write_index_show(struct device *dev,
  270. struct device_attribute *dev_attr,
  271. char *buf)
  272. {
  273. struct hv_device *hv_dev = device_to_hv_device(dev);
  274. struct hv_ring_buffer_debug_info outbound;
  275. if (!hv_dev->channel)
  276. return -ENODEV;
  277. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  278. return sprintf(buf, "%d\n", outbound.current_write_index);
  279. }
  280. static DEVICE_ATTR_RO(out_write_index);
  281. static ssize_t out_read_bytes_avail_show(struct device *dev,
  282. struct device_attribute *dev_attr,
  283. char *buf)
  284. {
  285. struct hv_device *hv_dev = device_to_hv_device(dev);
  286. struct hv_ring_buffer_debug_info outbound;
  287. if (!hv_dev->channel)
  288. return -ENODEV;
  289. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  290. return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
  291. }
  292. static DEVICE_ATTR_RO(out_read_bytes_avail);
  293. static ssize_t out_write_bytes_avail_show(struct device *dev,
  294. struct device_attribute *dev_attr,
  295. char *buf)
  296. {
  297. struct hv_device *hv_dev = device_to_hv_device(dev);
  298. struct hv_ring_buffer_debug_info outbound;
  299. if (!hv_dev->channel)
  300. return -ENODEV;
  301. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  302. return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
  303. }
  304. static DEVICE_ATTR_RO(out_write_bytes_avail);
  305. static ssize_t in_intr_mask_show(struct device *dev,
  306. struct device_attribute *dev_attr, char *buf)
  307. {
  308. struct hv_device *hv_dev = device_to_hv_device(dev);
  309. struct hv_ring_buffer_debug_info inbound;
  310. if (!hv_dev->channel)
  311. return -ENODEV;
  312. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  313. return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
  314. }
  315. static DEVICE_ATTR_RO(in_intr_mask);
  316. static ssize_t in_read_index_show(struct device *dev,
  317. struct device_attribute *dev_attr, char *buf)
  318. {
  319. struct hv_device *hv_dev = device_to_hv_device(dev);
  320. struct hv_ring_buffer_debug_info inbound;
  321. if (!hv_dev->channel)
  322. return -ENODEV;
  323. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  324. return sprintf(buf, "%d\n", inbound.current_read_index);
  325. }
  326. static DEVICE_ATTR_RO(in_read_index);
  327. static ssize_t in_write_index_show(struct device *dev,
  328. struct device_attribute *dev_attr, char *buf)
  329. {
  330. struct hv_device *hv_dev = device_to_hv_device(dev);
  331. struct hv_ring_buffer_debug_info inbound;
  332. if (!hv_dev->channel)
  333. return -ENODEV;
  334. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  335. return sprintf(buf, "%d\n", inbound.current_write_index);
  336. }
  337. static DEVICE_ATTR_RO(in_write_index);
  338. static ssize_t in_read_bytes_avail_show(struct device *dev,
  339. struct device_attribute *dev_attr,
  340. char *buf)
  341. {
  342. struct hv_device *hv_dev = device_to_hv_device(dev);
  343. struct hv_ring_buffer_debug_info inbound;
  344. if (!hv_dev->channel)
  345. return -ENODEV;
  346. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  347. return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
  348. }
  349. static DEVICE_ATTR_RO(in_read_bytes_avail);
  350. static ssize_t in_write_bytes_avail_show(struct device *dev,
  351. struct device_attribute *dev_attr,
  352. char *buf)
  353. {
  354. struct hv_device *hv_dev = device_to_hv_device(dev);
  355. struct hv_ring_buffer_debug_info inbound;
  356. if (!hv_dev->channel)
  357. return -ENODEV;
  358. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  359. return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
  360. }
  361. static DEVICE_ATTR_RO(in_write_bytes_avail);
  362. static ssize_t channel_vp_mapping_show(struct device *dev,
  363. struct device_attribute *dev_attr,
  364. char *buf)
  365. {
  366. struct hv_device *hv_dev = device_to_hv_device(dev);
  367. struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
  368. unsigned long flags;
  369. int buf_size = PAGE_SIZE, n_written, tot_written;
  370. struct list_head *cur;
  371. if (!channel)
  372. return -ENODEV;
  373. tot_written = snprintf(buf, buf_size, "%u:%u\n",
  374. channel->offermsg.child_relid, channel->target_cpu);
  375. spin_lock_irqsave(&channel->lock, flags);
  376. list_for_each(cur, &channel->sc_list) {
  377. if (tot_written >= buf_size - 1)
  378. break;
  379. cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
  380. n_written = scnprintf(buf + tot_written,
  381. buf_size - tot_written,
  382. "%u:%u\n",
  383. cur_sc->offermsg.child_relid,
  384. cur_sc->target_cpu);
  385. tot_written += n_written;
  386. }
  387. spin_unlock_irqrestore(&channel->lock, flags);
  388. return tot_written;
  389. }
  390. static DEVICE_ATTR_RO(channel_vp_mapping);
  391. static ssize_t vendor_show(struct device *dev,
  392. struct device_attribute *dev_attr,
  393. char *buf)
  394. {
  395. struct hv_device *hv_dev = device_to_hv_device(dev);
  396. return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
  397. }
  398. static DEVICE_ATTR_RO(vendor);
  399. static ssize_t device_show(struct device *dev,
  400. struct device_attribute *dev_attr,
  401. char *buf)
  402. {
  403. struct hv_device *hv_dev = device_to_hv_device(dev);
  404. return sprintf(buf, "0x%x\n", hv_dev->device_id);
  405. }
  406. static DEVICE_ATTR_RO(device);
  407. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  408. static struct attribute *vmbus_dev_attrs[] = {
  409. &dev_attr_id.attr,
  410. &dev_attr_state.attr,
  411. &dev_attr_monitor_id.attr,
  412. &dev_attr_class_id.attr,
  413. &dev_attr_device_id.attr,
  414. &dev_attr_modalias.attr,
  415. &dev_attr_server_monitor_pending.attr,
  416. &dev_attr_client_monitor_pending.attr,
  417. &dev_attr_server_monitor_latency.attr,
  418. &dev_attr_client_monitor_latency.attr,
  419. &dev_attr_server_monitor_conn_id.attr,
  420. &dev_attr_client_monitor_conn_id.attr,
  421. &dev_attr_out_intr_mask.attr,
  422. &dev_attr_out_read_index.attr,
  423. &dev_attr_out_write_index.attr,
  424. &dev_attr_out_read_bytes_avail.attr,
  425. &dev_attr_out_write_bytes_avail.attr,
  426. &dev_attr_in_intr_mask.attr,
  427. &dev_attr_in_read_index.attr,
  428. &dev_attr_in_write_index.attr,
  429. &dev_attr_in_read_bytes_avail.attr,
  430. &dev_attr_in_write_bytes_avail.attr,
  431. &dev_attr_channel_vp_mapping.attr,
  432. &dev_attr_vendor.attr,
  433. &dev_attr_device.attr,
  434. NULL,
  435. };
  436. ATTRIBUTE_GROUPS(vmbus_dev);
  437. /*
  438. * vmbus_uevent - add uevent for our device
  439. *
  440. * This routine is invoked when a device is added or removed on the vmbus to
  441. * generate a uevent to udev in the userspace. The udev will then look at its
  442. * rule and the uevent generated here to load the appropriate driver
  443. *
  444. * The alias string will be of the form vmbus:guid where guid is the string
  445. * representation of the device guid (each byte of the guid will be
  446. * represented with two hex characters.
  447. */
  448. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  449. {
  450. struct hv_device *dev = device_to_hv_device(device);
  451. int ret;
  452. char alias_name[VMBUS_ALIAS_LEN + 1];
  453. print_alias_name(dev, alias_name);
  454. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  455. return ret;
  456. }
  457. static const uuid_le null_guid;
  458. static inline bool is_null_guid(const uuid_le *guid)
  459. {
  460. if (uuid_le_cmp(*guid, null_guid))
  461. return false;
  462. return true;
  463. }
  464. /*
  465. * Return a matching hv_vmbus_device_id pointer.
  466. * If there is no match, return NULL.
  467. */
  468. static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
  469. const uuid_le *guid)
  470. {
  471. const struct hv_vmbus_device_id *id = NULL;
  472. struct vmbus_dynid *dynid;
  473. /* Look at the dynamic ids first, before the static ones */
  474. spin_lock(&drv->dynids.lock);
  475. list_for_each_entry(dynid, &drv->dynids.list, node) {
  476. if (!uuid_le_cmp(dynid->id.guid, *guid)) {
  477. id = &dynid->id;
  478. break;
  479. }
  480. }
  481. spin_unlock(&drv->dynids.lock);
  482. if (id)
  483. return id;
  484. id = drv->id_table;
  485. if (id == NULL)
  486. return NULL; /* empty device table */
  487. for (; !is_null_guid(&id->guid); id++)
  488. if (!uuid_le_cmp(id->guid, *guid))
  489. return id;
  490. return NULL;
  491. }
  492. /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
  493. static int vmbus_add_dynid(struct hv_driver *drv, uuid_le *guid)
  494. {
  495. struct vmbus_dynid *dynid;
  496. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  497. if (!dynid)
  498. return -ENOMEM;
  499. dynid->id.guid = *guid;
  500. spin_lock(&drv->dynids.lock);
  501. list_add_tail(&dynid->node, &drv->dynids.list);
  502. spin_unlock(&drv->dynids.lock);
  503. return driver_attach(&drv->driver);
  504. }
  505. static void vmbus_free_dynids(struct hv_driver *drv)
  506. {
  507. struct vmbus_dynid *dynid, *n;
  508. spin_lock(&drv->dynids.lock);
  509. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  510. list_del(&dynid->node);
  511. kfree(dynid);
  512. }
  513. spin_unlock(&drv->dynids.lock);
  514. }
  515. /*
  516. * store_new_id - sysfs frontend to vmbus_add_dynid()
  517. *
  518. * Allow GUIDs to be added to an existing driver via sysfs.
  519. */
  520. static ssize_t new_id_store(struct device_driver *driver, const char *buf,
  521. size_t count)
  522. {
  523. struct hv_driver *drv = drv_to_hv_drv(driver);
  524. uuid_le guid;
  525. ssize_t retval;
  526. retval = uuid_le_to_bin(buf, &guid);
  527. if (retval)
  528. return retval;
  529. if (hv_vmbus_get_id(drv, &guid))
  530. return -EEXIST;
  531. retval = vmbus_add_dynid(drv, &guid);
  532. if (retval)
  533. return retval;
  534. return count;
  535. }
  536. static DRIVER_ATTR_WO(new_id);
  537. /*
  538. * store_remove_id - remove a PCI device ID from this driver
  539. *
  540. * Removes a dynamic pci device ID to this driver.
  541. */
  542. static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
  543. size_t count)
  544. {
  545. struct hv_driver *drv = drv_to_hv_drv(driver);
  546. struct vmbus_dynid *dynid, *n;
  547. uuid_le guid;
  548. ssize_t retval;
  549. retval = uuid_le_to_bin(buf, &guid);
  550. if (retval)
  551. return retval;
  552. retval = -ENODEV;
  553. spin_lock(&drv->dynids.lock);
  554. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  555. struct hv_vmbus_device_id *id = &dynid->id;
  556. if (!uuid_le_cmp(id->guid, guid)) {
  557. list_del(&dynid->node);
  558. kfree(dynid);
  559. retval = count;
  560. break;
  561. }
  562. }
  563. spin_unlock(&drv->dynids.lock);
  564. return retval;
  565. }
  566. static DRIVER_ATTR_WO(remove_id);
  567. static struct attribute *vmbus_drv_attrs[] = {
  568. &driver_attr_new_id.attr,
  569. &driver_attr_remove_id.attr,
  570. NULL,
  571. };
  572. ATTRIBUTE_GROUPS(vmbus_drv);
  573. /*
  574. * vmbus_match - Attempt to match the specified device to the specified driver
  575. */
  576. static int vmbus_match(struct device *device, struct device_driver *driver)
  577. {
  578. struct hv_driver *drv = drv_to_hv_drv(driver);
  579. struct hv_device *hv_dev = device_to_hv_device(device);
  580. /* The hv_sock driver handles all hv_sock offers. */
  581. if (is_hvsock_channel(hv_dev->channel))
  582. return drv->hvsock;
  583. if (hv_vmbus_get_id(drv, &hv_dev->dev_type))
  584. return 1;
  585. return 0;
  586. }
  587. /*
  588. * vmbus_probe - Add the new vmbus's child device
  589. */
  590. static int vmbus_probe(struct device *child_device)
  591. {
  592. int ret = 0;
  593. struct hv_driver *drv =
  594. drv_to_hv_drv(child_device->driver);
  595. struct hv_device *dev = device_to_hv_device(child_device);
  596. const struct hv_vmbus_device_id *dev_id;
  597. dev_id = hv_vmbus_get_id(drv, &dev->dev_type);
  598. if (drv->probe) {
  599. ret = drv->probe(dev, dev_id);
  600. if (ret != 0)
  601. pr_err("probe failed for device %s (%d)\n",
  602. dev_name(child_device), ret);
  603. } else {
  604. pr_err("probe not set for driver %s\n",
  605. dev_name(child_device));
  606. ret = -ENODEV;
  607. }
  608. return ret;
  609. }
  610. /*
  611. * vmbus_remove - Remove a vmbus device
  612. */
  613. static int vmbus_remove(struct device *child_device)
  614. {
  615. struct hv_driver *drv;
  616. struct hv_device *dev = device_to_hv_device(child_device);
  617. if (child_device->driver) {
  618. drv = drv_to_hv_drv(child_device->driver);
  619. if (drv->remove)
  620. drv->remove(dev);
  621. }
  622. return 0;
  623. }
  624. /*
  625. * vmbus_shutdown - Shutdown a vmbus device
  626. */
  627. static void vmbus_shutdown(struct device *child_device)
  628. {
  629. struct hv_driver *drv;
  630. struct hv_device *dev = device_to_hv_device(child_device);
  631. /* The device may not be attached yet */
  632. if (!child_device->driver)
  633. return;
  634. drv = drv_to_hv_drv(child_device->driver);
  635. if (drv->shutdown)
  636. drv->shutdown(dev);
  637. }
  638. /*
  639. * vmbus_device_release - Final callback release of the vmbus child device
  640. */
  641. static void vmbus_device_release(struct device *device)
  642. {
  643. struct hv_device *hv_dev = device_to_hv_device(device);
  644. struct vmbus_channel *channel = hv_dev->channel;
  645. mutex_lock(&vmbus_connection.channel_mutex);
  646. hv_process_channel_removal(channel->offermsg.child_relid);
  647. mutex_unlock(&vmbus_connection.channel_mutex);
  648. kfree(hv_dev);
  649. }
  650. /* The one and only one */
  651. static struct bus_type hv_bus = {
  652. .name = "vmbus",
  653. .match = vmbus_match,
  654. .shutdown = vmbus_shutdown,
  655. .remove = vmbus_remove,
  656. .probe = vmbus_probe,
  657. .uevent = vmbus_uevent,
  658. .dev_groups = vmbus_dev_groups,
  659. .drv_groups = vmbus_drv_groups,
  660. };
  661. struct onmessage_work_context {
  662. struct work_struct work;
  663. struct hv_message msg;
  664. };
  665. static void vmbus_onmessage_work(struct work_struct *work)
  666. {
  667. struct onmessage_work_context *ctx;
  668. /* Do not process messages if we're in DISCONNECTED state */
  669. if (vmbus_connection.conn_state == DISCONNECTED)
  670. return;
  671. ctx = container_of(work, struct onmessage_work_context,
  672. work);
  673. vmbus_onmessage(&ctx->msg);
  674. kfree(ctx);
  675. }
  676. static void hv_process_timer_expiration(struct hv_message *msg,
  677. struct hv_per_cpu_context *hv_cpu)
  678. {
  679. struct clock_event_device *dev = hv_cpu->clk_evt;
  680. if (dev->event_handler)
  681. dev->event_handler(dev);
  682. vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
  683. }
  684. void vmbus_on_msg_dpc(unsigned long data)
  685. {
  686. struct hv_per_cpu_context *hv_cpu = (void *)data;
  687. void *page_addr = hv_cpu->synic_message_page;
  688. struct hv_message *msg = (struct hv_message *)page_addr +
  689. VMBUS_MESSAGE_SINT;
  690. struct vmbus_channel_message_header *hdr;
  691. const struct vmbus_channel_message_table_entry *entry;
  692. struct onmessage_work_context *ctx;
  693. u32 message_type = msg->header.message_type;
  694. if (message_type == HVMSG_NONE)
  695. /* no msg */
  696. return;
  697. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  698. trace_vmbus_on_msg_dpc(hdr);
  699. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  700. WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
  701. goto msg_handled;
  702. }
  703. entry = &channel_message_table[hdr->msgtype];
  704. if (entry->handler_type == VMHT_BLOCKING) {
  705. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  706. if (ctx == NULL)
  707. return;
  708. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  709. memcpy(&ctx->msg, msg, sizeof(*msg));
  710. /*
  711. * The host can generate a rescind message while we
  712. * may still be handling the original offer. We deal with
  713. * this condition by ensuring the processing is done on the
  714. * same CPU.
  715. */
  716. switch (hdr->msgtype) {
  717. case CHANNELMSG_RESCIND_CHANNELOFFER:
  718. /*
  719. * If we are handling the rescind message;
  720. * schedule the work on the global work queue.
  721. */
  722. schedule_work_on(vmbus_connection.connect_cpu,
  723. &ctx->work);
  724. break;
  725. case CHANNELMSG_OFFERCHANNEL:
  726. atomic_inc(&vmbus_connection.offer_in_progress);
  727. queue_work_on(vmbus_connection.connect_cpu,
  728. vmbus_connection.work_queue,
  729. &ctx->work);
  730. break;
  731. default:
  732. queue_work(vmbus_connection.work_queue, &ctx->work);
  733. }
  734. } else
  735. entry->message_handler(hdr);
  736. msg_handled:
  737. vmbus_signal_eom(msg, message_type);
  738. }
  739. /*
  740. * Direct callback for channels using other deferred processing
  741. */
  742. static void vmbus_channel_isr(struct vmbus_channel *channel)
  743. {
  744. void (*callback_fn)(void *);
  745. callback_fn = READ_ONCE(channel->onchannel_callback);
  746. if (likely(callback_fn != NULL))
  747. (*callback_fn)(channel->channel_callback_context);
  748. }
  749. /*
  750. * Schedule all channels with events pending
  751. */
  752. static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
  753. {
  754. unsigned long *recv_int_page;
  755. u32 maxbits, relid;
  756. if (vmbus_proto_version < VERSION_WIN8) {
  757. maxbits = MAX_NUM_CHANNELS_SUPPORTED;
  758. recv_int_page = vmbus_connection.recv_int_page;
  759. } else {
  760. /*
  761. * When the host is win8 and beyond, the event page
  762. * can be directly checked to get the id of the channel
  763. * that has the interrupt pending.
  764. */
  765. void *page_addr = hv_cpu->synic_event_page;
  766. union hv_synic_event_flags *event
  767. = (union hv_synic_event_flags *)page_addr +
  768. VMBUS_MESSAGE_SINT;
  769. maxbits = HV_EVENT_FLAGS_COUNT;
  770. recv_int_page = event->flags;
  771. }
  772. if (unlikely(!recv_int_page))
  773. return;
  774. for_each_set_bit(relid, recv_int_page, maxbits) {
  775. struct vmbus_channel *channel;
  776. if (!sync_test_and_clear_bit(relid, recv_int_page))
  777. continue;
  778. /* Special case - vmbus channel protocol msg */
  779. if (relid == 0)
  780. continue;
  781. rcu_read_lock();
  782. /* Find channel based on relid */
  783. list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
  784. if (channel->offermsg.child_relid != relid)
  785. continue;
  786. if (channel->rescind)
  787. continue;
  788. trace_vmbus_chan_sched(channel);
  789. ++channel->interrupts;
  790. switch (channel->callback_mode) {
  791. case HV_CALL_ISR:
  792. vmbus_channel_isr(channel);
  793. break;
  794. case HV_CALL_BATCHED:
  795. hv_begin_read(&channel->inbound);
  796. /* fallthrough */
  797. case HV_CALL_DIRECT:
  798. tasklet_schedule(&channel->callback_event);
  799. }
  800. }
  801. rcu_read_unlock();
  802. }
  803. }
  804. static void vmbus_isr(void)
  805. {
  806. struct hv_per_cpu_context *hv_cpu
  807. = this_cpu_ptr(hv_context.cpu_context);
  808. void *page_addr = hv_cpu->synic_event_page;
  809. struct hv_message *msg;
  810. union hv_synic_event_flags *event;
  811. bool handled = false;
  812. if (unlikely(page_addr == NULL))
  813. return;
  814. event = (union hv_synic_event_flags *)page_addr +
  815. VMBUS_MESSAGE_SINT;
  816. /*
  817. * Check for events before checking for messages. This is the order
  818. * in which events and messages are checked in Windows guests on
  819. * Hyper-V, and the Windows team suggested we do the same.
  820. */
  821. if ((vmbus_proto_version == VERSION_WS2008) ||
  822. (vmbus_proto_version == VERSION_WIN7)) {
  823. /* Since we are a child, we only need to check bit 0 */
  824. if (sync_test_and_clear_bit(0, event->flags))
  825. handled = true;
  826. } else {
  827. /*
  828. * Our host is win8 or above. The signaling mechanism
  829. * has changed and we can directly look at the event page.
  830. * If bit n is set then we have an interrup on the channel
  831. * whose id is n.
  832. */
  833. handled = true;
  834. }
  835. if (handled)
  836. vmbus_chan_sched(hv_cpu);
  837. page_addr = hv_cpu->synic_message_page;
  838. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  839. /* Check if there are actual msgs to be processed */
  840. if (msg->header.message_type != HVMSG_NONE) {
  841. if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
  842. hv_process_timer_expiration(msg, hv_cpu);
  843. else
  844. tasklet_schedule(&hv_cpu->msg_dpc);
  845. }
  846. add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  847. }
  848. /*
  849. * vmbus_bus_init -Main vmbus driver initialization routine.
  850. *
  851. * Here, we
  852. * - initialize the vmbus driver context
  853. * - invoke the vmbus hv main init routine
  854. * - retrieve the channel offers
  855. */
  856. static int vmbus_bus_init(void)
  857. {
  858. int ret;
  859. /* Hypervisor initialization...setup hypercall page..etc */
  860. ret = hv_init();
  861. if (ret != 0) {
  862. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  863. return ret;
  864. }
  865. ret = bus_register(&hv_bus);
  866. if (ret)
  867. return ret;
  868. hv_setup_vmbus_irq(vmbus_isr);
  869. ret = hv_synic_alloc();
  870. if (ret)
  871. goto err_alloc;
  872. /*
  873. * Initialize the per-cpu interrupt state and
  874. * connect to the host.
  875. */
  876. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
  877. hv_synic_init, hv_synic_cleanup);
  878. if (ret < 0)
  879. goto err_alloc;
  880. hyperv_cpuhp_online = ret;
  881. ret = vmbus_connect();
  882. if (ret)
  883. goto err_connect;
  884. /*
  885. * Only register if the crash MSRs are available
  886. */
  887. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  888. register_die_notifier(&hyperv_die_block);
  889. atomic_notifier_chain_register(&panic_notifier_list,
  890. &hyperv_panic_block);
  891. }
  892. vmbus_request_offers();
  893. return 0;
  894. err_connect:
  895. cpuhp_remove_state(hyperv_cpuhp_online);
  896. err_alloc:
  897. hv_synic_free();
  898. hv_remove_vmbus_irq();
  899. bus_unregister(&hv_bus);
  900. return ret;
  901. }
  902. /**
  903. * __vmbus_child_driver_register() - Register a vmbus's driver
  904. * @hv_driver: Pointer to driver structure you want to register
  905. * @owner: owner module of the drv
  906. * @mod_name: module name string
  907. *
  908. * Registers the given driver with Linux through the 'driver_register()' call
  909. * and sets up the hyper-v vmbus handling for this driver.
  910. * It will return the state of the 'driver_register()' call.
  911. *
  912. */
  913. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  914. {
  915. int ret;
  916. pr_info("registering driver %s\n", hv_driver->name);
  917. ret = vmbus_exists();
  918. if (ret < 0)
  919. return ret;
  920. hv_driver->driver.name = hv_driver->name;
  921. hv_driver->driver.owner = owner;
  922. hv_driver->driver.mod_name = mod_name;
  923. hv_driver->driver.bus = &hv_bus;
  924. spin_lock_init(&hv_driver->dynids.lock);
  925. INIT_LIST_HEAD(&hv_driver->dynids.list);
  926. ret = driver_register(&hv_driver->driver);
  927. return ret;
  928. }
  929. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  930. /**
  931. * vmbus_driver_unregister() - Unregister a vmbus's driver
  932. * @hv_driver: Pointer to driver structure you want to
  933. * un-register
  934. *
  935. * Un-register the given driver that was previous registered with a call to
  936. * vmbus_driver_register()
  937. */
  938. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  939. {
  940. pr_info("unregistering driver %s\n", hv_driver->name);
  941. if (!vmbus_exists()) {
  942. driver_unregister(&hv_driver->driver);
  943. vmbus_free_dynids(hv_driver);
  944. }
  945. }
  946. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  947. /*
  948. * Called when last reference to channel is gone.
  949. */
  950. static void vmbus_chan_release(struct kobject *kobj)
  951. {
  952. struct vmbus_channel *channel
  953. = container_of(kobj, struct vmbus_channel, kobj);
  954. kfree_rcu(channel, rcu);
  955. }
  956. struct vmbus_chan_attribute {
  957. struct attribute attr;
  958. ssize_t (*show)(const struct vmbus_channel *chan, char *buf);
  959. ssize_t (*store)(struct vmbus_channel *chan,
  960. const char *buf, size_t count);
  961. };
  962. #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
  963. struct vmbus_chan_attribute chan_attr_##_name \
  964. = __ATTR(_name, _mode, _show, _store)
  965. #define VMBUS_CHAN_ATTR_RW(_name) \
  966. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
  967. #define VMBUS_CHAN_ATTR_RO(_name) \
  968. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
  969. #define VMBUS_CHAN_ATTR_WO(_name) \
  970. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
  971. static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
  972. struct attribute *attr, char *buf)
  973. {
  974. const struct vmbus_chan_attribute *attribute
  975. = container_of(attr, struct vmbus_chan_attribute, attr);
  976. const struct vmbus_channel *chan
  977. = container_of(kobj, struct vmbus_channel, kobj);
  978. if (!attribute->show)
  979. return -EIO;
  980. return attribute->show(chan, buf);
  981. }
  982. static const struct sysfs_ops vmbus_chan_sysfs_ops = {
  983. .show = vmbus_chan_attr_show,
  984. };
  985. static ssize_t out_mask_show(const struct vmbus_channel *channel, char *buf)
  986. {
  987. const struct hv_ring_buffer_info *rbi = &channel->outbound;
  988. return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
  989. }
  990. static VMBUS_CHAN_ATTR_RO(out_mask);
  991. static ssize_t in_mask_show(const struct vmbus_channel *channel, char *buf)
  992. {
  993. const struct hv_ring_buffer_info *rbi = &channel->inbound;
  994. return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
  995. }
  996. static VMBUS_CHAN_ATTR_RO(in_mask);
  997. static ssize_t read_avail_show(const struct vmbus_channel *channel, char *buf)
  998. {
  999. const struct hv_ring_buffer_info *rbi = &channel->inbound;
  1000. return sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
  1001. }
  1002. static VMBUS_CHAN_ATTR_RO(read_avail);
  1003. static ssize_t write_avail_show(const struct vmbus_channel *channel, char *buf)
  1004. {
  1005. const struct hv_ring_buffer_info *rbi = &channel->outbound;
  1006. return sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
  1007. }
  1008. static VMBUS_CHAN_ATTR_RO(write_avail);
  1009. static ssize_t show_target_cpu(const struct vmbus_channel *channel, char *buf)
  1010. {
  1011. return sprintf(buf, "%u\n", channel->target_cpu);
  1012. }
  1013. static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
  1014. static ssize_t channel_pending_show(const struct vmbus_channel *channel,
  1015. char *buf)
  1016. {
  1017. return sprintf(buf, "%d\n",
  1018. channel_pending(channel,
  1019. vmbus_connection.monitor_pages[1]));
  1020. }
  1021. static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
  1022. static ssize_t channel_latency_show(const struct vmbus_channel *channel,
  1023. char *buf)
  1024. {
  1025. return sprintf(buf, "%d\n",
  1026. channel_latency(channel,
  1027. vmbus_connection.monitor_pages[1]));
  1028. }
  1029. static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
  1030. static ssize_t channel_interrupts_show(const struct vmbus_channel *channel, char *buf)
  1031. {
  1032. return sprintf(buf, "%llu\n", channel->interrupts);
  1033. }
  1034. static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
  1035. static ssize_t channel_events_show(const struct vmbus_channel *channel, char *buf)
  1036. {
  1037. return sprintf(buf, "%llu\n", channel->sig_events);
  1038. }
  1039. static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
  1040. static ssize_t subchannel_monitor_id_show(const struct vmbus_channel *channel,
  1041. char *buf)
  1042. {
  1043. return sprintf(buf, "%u\n", channel->offermsg.monitorid);
  1044. }
  1045. static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
  1046. static ssize_t subchannel_id_show(const struct vmbus_channel *channel,
  1047. char *buf)
  1048. {
  1049. return sprintf(buf, "%u\n",
  1050. channel->offermsg.offer.sub_channel_index);
  1051. }
  1052. static VMBUS_CHAN_ATTR_RO(subchannel_id);
  1053. static struct attribute *vmbus_chan_attrs[] = {
  1054. &chan_attr_out_mask.attr,
  1055. &chan_attr_in_mask.attr,
  1056. &chan_attr_read_avail.attr,
  1057. &chan_attr_write_avail.attr,
  1058. &chan_attr_cpu.attr,
  1059. &chan_attr_pending.attr,
  1060. &chan_attr_latency.attr,
  1061. &chan_attr_interrupts.attr,
  1062. &chan_attr_events.attr,
  1063. &chan_attr_monitor_id.attr,
  1064. &chan_attr_subchannel_id.attr,
  1065. NULL
  1066. };
  1067. static struct kobj_type vmbus_chan_ktype = {
  1068. .sysfs_ops = &vmbus_chan_sysfs_ops,
  1069. .release = vmbus_chan_release,
  1070. .default_attrs = vmbus_chan_attrs,
  1071. };
  1072. /*
  1073. * vmbus_add_channel_kobj - setup a sub-directory under device/channels
  1074. */
  1075. int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
  1076. {
  1077. struct kobject *kobj = &channel->kobj;
  1078. u32 relid = channel->offermsg.child_relid;
  1079. int ret;
  1080. kobj->kset = dev->channels_kset;
  1081. ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
  1082. "%u", relid);
  1083. if (ret)
  1084. return ret;
  1085. kobject_uevent(kobj, KOBJ_ADD);
  1086. return 0;
  1087. }
  1088. /*
  1089. * vmbus_device_create - Creates and registers a new child device
  1090. * on the vmbus.
  1091. */
  1092. struct hv_device *vmbus_device_create(const uuid_le *type,
  1093. const uuid_le *instance,
  1094. struct vmbus_channel *channel)
  1095. {
  1096. struct hv_device *child_device_obj;
  1097. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  1098. if (!child_device_obj) {
  1099. pr_err("Unable to allocate device object for child device\n");
  1100. return NULL;
  1101. }
  1102. child_device_obj->channel = channel;
  1103. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  1104. memcpy(&child_device_obj->dev_instance, instance,
  1105. sizeof(uuid_le));
  1106. child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
  1107. return child_device_obj;
  1108. }
  1109. /*
  1110. * vmbus_device_register - Register the child device
  1111. */
  1112. int vmbus_device_register(struct hv_device *child_device_obj)
  1113. {
  1114. struct kobject *kobj = &child_device_obj->device.kobj;
  1115. int ret;
  1116. dev_set_name(&child_device_obj->device, "%pUl",
  1117. child_device_obj->channel->offermsg.offer.if_instance.b);
  1118. child_device_obj->device.bus = &hv_bus;
  1119. child_device_obj->device.parent = &hv_acpi_dev->dev;
  1120. child_device_obj->device.release = vmbus_device_release;
  1121. /*
  1122. * Register with the LDM. This will kick off the driver/device
  1123. * binding...which will eventually call vmbus_match() and vmbus_probe()
  1124. */
  1125. ret = device_register(&child_device_obj->device);
  1126. if (ret) {
  1127. pr_err("Unable to register child device\n");
  1128. return ret;
  1129. }
  1130. child_device_obj->channels_kset = kset_create_and_add("channels",
  1131. NULL, kobj);
  1132. if (!child_device_obj->channels_kset) {
  1133. ret = -ENOMEM;
  1134. goto err_dev_unregister;
  1135. }
  1136. ret = vmbus_add_channel_kobj(child_device_obj,
  1137. child_device_obj->channel);
  1138. if (ret) {
  1139. pr_err("Unable to register primary channeln");
  1140. goto err_kset_unregister;
  1141. }
  1142. return 0;
  1143. err_kset_unregister:
  1144. kset_unregister(child_device_obj->channels_kset);
  1145. err_dev_unregister:
  1146. device_unregister(&child_device_obj->device);
  1147. return ret;
  1148. }
  1149. /*
  1150. * vmbus_device_unregister - Remove the specified child device
  1151. * from the vmbus.
  1152. */
  1153. void vmbus_device_unregister(struct hv_device *device_obj)
  1154. {
  1155. pr_debug("child device %s unregistered\n",
  1156. dev_name(&device_obj->device));
  1157. kset_unregister(device_obj->channels_kset);
  1158. /*
  1159. * Kick off the process of unregistering the device.
  1160. * This will call vmbus_remove() and eventually vmbus_device_release()
  1161. */
  1162. device_unregister(&device_obj->device);
  1163. }
  1164. /*
  1165. * VMBUS is an acpi enumerated device. Get the information we
  1166. * need from DSDT.
  1167. */
  1168. #define VTPM_BASE_ADDRESS 0xfed40000
  1169. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
  1170. {
  1171. resource_size_t start = 0;
  1172. resource_size_t end = 0;
  1173. struct resource *new_res;
  1174. struct resource **old_res = &hyperv_mmio;
  1175. struct resource **prev_res = NULL;
  1176. switch (res->type) {
  1177. /*
  1178. * "Address" descriptors are for bus windows. Ignore
  1179. * "memory" descriptors, which are for registers on
  1180. * devices.
  1181. */
  1182. case ACPI_RESOURCE_TYPE_ADDRESS32:
  1183. start = res->data.address32.address.minimum;
  1184. end = res->data.address32.address.maximum;
  1185. break;
  1186. case ACPI_RESOURCE_TYPE_ADDRESS64:
  1187. start = res->data.address64.address.minimum;
  1188. end = res->data.address64.address.maximum;
  1189. break;
  1190. default:
  1191. /* Unused resource type */
  1192. return AE_OK;
  1193. }
  1194. /*
  1195. * Ignore ranges that are below 1MB, as they're not
  1196. * necessary or useful here.
  1197. */
  1198. if (end < 0x100000)
  1199. return AE_OK;
  1200. new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
  1201. if (!new_res)
  1202. return AE_NO_MEMORY;
  1203. /* If this range overlaps the virtual TPM, truncate it. */
  1204. if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
  1205. end = VTPM_BASE_ADDRESS;
  1206. new_res->name = "hyperv mmio";
  1207. new_res->flags = IORESOURCE_MEM;
  1208. new_res->start = start;
  1209. new_res->end = end;
  1210. /*
  1211. * If two ranges are adjacent, merge them.
  1212. */
  1213. do {
  1214. if (!*old_res) {
  1215. *old_res = new_res;
  1216. break;
  1217. }
  1218. if (((*old_res)->end + 1) == new_res->start) {
  1219. (*old_res)->end = new_res->end;
  1220. kfree(new_res);
  1221. break;
  1222. }
  1223. if ((*old_res)->start == new_res->end + 1) {
  1224. (*old_res)->start = new_res->start;
  1225. kfree(new_res);
  1226. break;
  1227. }
  1228. if ((*old_res)->start > new_res->end) {
  1229. new_res->sibling = *old_res;
  1230. if (prev_res)
  1231. (*prev_res)->sibling = new_res;
  1232. *old_res = new_res;
  1233. break;
  1234. }
  1235. prev_res = old_res;
  1236. old_res = &(*old_res)->sibling;
  1237. } while (1);
  1238. return AE_OK;
  1239. }
  1240. static int vmbus_acpi_remove(struct acpi_device *device)
  1241. {
  1242. struct resource *cur_res;
  1243. struct resource *next_res;
  1244. if (hyperv_mmio) {
  1245. if (fb_mmio) {
  1246. __release_region(hyperv_mmio, fb_mmio->start,
  1247. resource_size(fb_mmio));
  1248. fb_mmio = NULL;
  1249. }
  1250. for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
  1251. next_res = cur_res->sibling;
  1252. kfree(cur_res);
  1253. }
  1254. }
  1255. return 0;
  1256. }
  1257. static void vmbus_reserve_fb(void)
  1258. {
  1259. int size;
  1260. /*
  1261. * Make a claim for the frame buffer in the resource tree under the
  1262. * first node, which will be the one below 4GB. The length seems to
  1263. * be underreported, particularly in a Generation 1 VM. So start out
  1264. * reserving a larger area and make it smaller until it succeeds.
  1265. */
  1266. if (screen_info.lfb_base) {
  1267. if (efi_enabled(EFI_BOOT))
  1268. size = max_t(__u32, screen_info.lfb_size, 0x800000);
  1269. else
  1270. size = max_t(__u32, screen_info.lfb_size, 0x4000000);
  1271. for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
  1272. fb_mmio = __request_region(hyperv_mmio,
  1273. screen_info.lfb_base, size,
  1274. fb_mmio_name, 0);
  1275. }
  1276. }
  1277. }
  1278. /**
  1279. * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
  1280. * @new: If successful, supplied a pointer to the
  1281. * allocated MMIO space.
  1282. * @device_obj: Identifies the caller
  1283. * @min: Minimum guest physical address of the
  1284. * allocation
  1285. * @max: Maximum guest physical address
  1286. * @size: Size of the range to be allocated
  1287. * @align: Alignment of the range to be allocated
  1288. * @fb_overlap_ok: Whether this allocation can be allowed
  1289. * to overlap the video frame buffer.
  1290. *
  1291. * This function walks the resources granted to VMBus by the
  1292. * _CRS object in the ACPI namespace underneath the parent
  1293. * "bridge" whether that's a root PCI bus in the Generation 1
  1294. * case or a Module Device in the Generation 2 case. It then
  1295. * attempts to allocate from the global MMIO pool in a way that
  1296. * matches the constraints supplied in these parameters and by
  1297. * that _CRS.
  1298. *
  1299. * Return: 0 on success, -errno on failure
  1300. */
  1301. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  1302. resource_size_t min, resource_size_t max,
  1303. resource_size_t size, resource_size_t align,
  1304. bool fb_overlap_ok)
  1305. {
  1306. struct resource *iter, *shadow;
  1307. resource_size_t range_min, range_max, start;
  1308. const char *dev_n = dev_name(&device_obj->device);
  1309. int retval;
  1310. retval = -ENXIO;
  1311. down(&hyperv_mmio_lock);
  1312. /*
  1313. * If overlaps with frame buffers are allowed, then first attempt to
  1314. * make the allocation from within the reserved region. Because it
  1315. * is already reserved, no shadow allocation is necessary.
  1316. */
  1317. if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
  1318. !(max < fb_mmio->start)) {
  1319. range_min = fb_mmio->start;
  1320. range_max = fb_mmio->end;
  1321. start = (range_min + align - 1) & ~(align - 1);
  1322. for (; start + size - 1 <= range_max; start += align) {
  1323. *new = request_mem_region_exclusive(start, size, dev_n);
  1324. if (*new) {
  1325. retval = 0;
  1326. goto exit;
  1327. }
  1328. }
  1329. }
  1330. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1331. if ((iter->start >= max) || (iter->end <= min))
  1332. continue;
  1333. range_min = iter->start;
  1334. range_max = iter->end;
  1335. start = (range_min + align - 1) & ~(align - 1);
  1336. for (; start + size - 1 <= range_max; start += align) {
  1337. shadow = __request_region(iter, start, size, NULL,
  1338. IORESOURCE_BUSY);
  1339. if (!shadow)
  1340. continue;
  1341. *new = request_mem_region_exclusive(start, size, dev_n);
  1342. if (*new) {
  1343. shadow->name = (char *)*new;
  1344. retval = 0;
  1345. goto exit;
  1346. }
  1347. __release_region(iter, start, size);
  1348. }
  1349. }
  1350. exit:
  1351. up(&hyperv_mmio_lock);
  1352. return retval;
  1353. }
  1354. EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
  1355. /**
  1356. * vmbus_free_mmio() - Free a memory-mapped I/O range.
  1357. * @start: Base address of region to release.
  1358. * @size: Size of the range to be allocated
  1359. *
  1360. * This function releases anything requested by
  1361. * vmbus_mmio_allocate().
  1362. */
  1363. void vmbus_free_mmio(resource_size_t start, resource_size_t size)
  1364. {
  1365. struct resource *iter;
  1366. down(&hyperv_mmio_lock);
  1367. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1368. if ((iter->start >= start + size) || (iter->end <= start))
  1369. continue;
  1370. __release_region(iter, start, size);
  1371. }
  1372. release_mem_region(start, size);
  1373. up(&hyperv_mmio_lock);
  1374. }
  1375. EXPORT_SYMBOL_GPL(vmbus_free_mmio);
  1376. static int vmbus_acpi_add(struct acpi_device *device)
  1377. {
  1378. acpi_status result;
  1379. int ret_val = -ENODEV;
  1380. struct acpi_device *ancestor;
  1381. hv_acpi_dev = device;
  1382. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  1383. vmbus_walk_resources, NULL);
  1384. if (ACPI_FAILURE(result))
  1385. goto acpi_walk_err;
  1386. /*
  1387. * Some ancestor of the vmbus acpi device (Gen1 or Gen2
  1388. * firmware) is the VMOD that has the mmio ranges. Get that.
  1389. */
  1390. for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
  1391. result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
  1392. vmbus_walk_resources, NULL);
  1393. if (ACPI_FAILURE(result))
  1394. continue;
  1395. if (hyperv_mmio) {
  1396. vmbus_reserve_fb();
  1397. break;
  1398. }
  1399. }
  1400. ret_val = 0;
  1401. acpi_walk_err:
  1402. complete(&probe_event);
  1403. if (ret_val)
  1404. vmbus_acpi_remove(device);
  1405. return ret_val;
  1406. }
  1407. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  1408. {"VMBUS", 0},
  1409. {"VMBus", 0},
  1410. {"", 0},
  1411. };
  1412. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  1413. static struct acpi_driver vmbus_acpi_driver = {
  1414. .name = "vmbus",
  1415. .ids = vmbus_acpi_device_ids,
  1416. .ops = {
  1417. .add = vmbus_acpi_add,
  1418. .remove = vmbus_acpi_remove,
  1419. },
  1420. };
  1421. static void hv_kexec_handler(void)
  1422. {
  1423. hv_synic_clockevents_cleanup();
  1424. vmbus_initiate_unload(false);
  1425. vmbus_connection.conn_state = DISCONNECTED;
  1426. /* Make sure conn_state is set as hv_synic_cleanup checks for it */
  1427. mb();
  1428. cpuhp_remove_state(hyperv_cpuhp_online);
  1429. hyperv_cleanup();
  1430. };
  1431. static void hv_crash_handler(struct pt_regs *regs)
  1432. {
  1433. vmbus_initiate_unload(true);
  1434. /*
  1435. * In crash handler we can't schedule synic cleanup for all CPUs,
  1436. * doing the cleanup for current CPU only. This should be sufficient
  1437. * for kdump.
  1438. */
  1439. vmbus_connection.conn_state = DISCONNECTED;
  1440. hv_synic_cleanup(smp_processor_id());
  1441. hyperv_cleanup();
  1442. };
  1443. static int __init hv_acpi_init(void)
  1444. {
  1445. int ret, t;
  1446. if (!hv_is_hyperv_initialized())
  1447. return -ENODEV;
  1448. init_completion(&probe_event);
  1449. /*
  1450. * Get ACPI resources first.
  1451. */
  1452. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  1453. if (ret)
  1454. return ret;
  1455. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  1456. if (t == 0) {
  1457. ret = -ETIMEDOUT;
  1458. goto cleanup;
  1459. }
  1460. ret = vmbus_bus_init();
  1461. if (ret)
  1462. goto cleanup;
  1463. hv_setup_kexec_handler(hv_kexec_handler);
  1464. hv_setup_crash_handler(hv_crash_handler);
  1465. return 0;
  1466. cleanup:
  1467. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1468. hv_acpi_dev = NULL;
  1469. return ret;
  1470. }
  1471. static void __exit vmbus_exit(void)
  1472. {
  1473. int cpu;
  1474. hv_remove_kexec_handler();
  1475. hv_remove_crash_handler();
  1476. vmbus_connection.conn_state = DISCONNECTED;
  1477. hv_synic_clockevents_cleanup();
  1478. vmbus_disconnect();
  1479. hv_remove_vmbus_irq();
  1480. for_each_online_cpu(cpu) {
  1481. struct hv_per_cpu_context *hv_cpu
  1482. = per_cpu_ptr(hv_context.cpu_context, cpu);
  1483. tasklet_kill(&hv_cpu->msg_dpc);
  1484. }
  1485. vmbus_free_channels();
  1486. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  1487. unregister_die_notifier(&hyperv_die_block);
  1488. atomic_notifier_chain_unregister(&panic_notifier_list,
  1489. &hyperv_panic_block);
  1490. }
  1491. bus_unregister(&hv_bus);
  1492. cpuhp_remove_state(hyperv_cpuhp_online);
  1493. hv_synic_free();
  1494. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1495. }
  1496. MODULE_LICENSE("GPL");
  1497. subsys_initcall(hv_acpi_init);
  1498. module_exit(vmbus_exit);