mpssd.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC User Space Tools.
  19. */
  20. #define _GNU_SOURCE
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <assert.h>
  25. #include <unistd.h>
  26. #include <stdbool.h>
  27. #include <signal.h>
  28. #include <poll.h>
  29. #include <features.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/mman.h>
  33. #include <sys/socket.h>
  34. #include <linux/virtio_ring.h>
  35. #include <linux/virtio_net.h>
  36. #include <linux/virtio_console.h>
  37. #include <linux/virtio_blk.h>
  38. #include <linux/version.h>
  39. #include "mpssd.h"
  40. #include <linux/mic_ioctl.h>
  41. #include <linux/mic_common.h>
  42. #include <tools/endian.h>
  43. static void *init_mic(void *arg);
  44. static FILE *logfp;
  45. static struct mic_info mic_list;
  46. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  47. #define min_t(type, x, y) ({ \
  48. type __min1 = (x); \
  49. type __min2 = (y); \
  50. __min1 < __min2 ? __min1 : __min2; })
  51. /* align addr on a size boundary - adjust address up/down if needed */
  52. #define _ALIGN_DOWN(addr, size) ((addr)&(~((size)-1)))
  53. #define _ALIGN_UP(addr, size) _ALIGN_DOWN(addr + size - 1, size)
  54. /* align addr on a size boundary - adjust address up if needed */
  55. #define _ALIGN(addr, size) _ALIGN_UP(addr, size)
  56. /* to align the pointer to the (next) page boundary */
  57. #define PAGE_ALIGN(addr) _ALIGN(addr, PAGE_SIZE)
  58. #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
  59. #define GSO_ENABLED 1
  60. #define MAX_GSO_SIZE (64 * 1024)
  61. #define ETH_H_LEN 14
  62. #define MAX_NET_PKT_SIZE (_ALIGN_UP(MAX_GSO_SIZE + ETH_H_LEN, 64))
  63. #define MIC_DEVICE_PAGE_END 0x1000
  64. #ifndef VIRTIO_NET_HDR_F_DATA_VALID
  65. #define VIRTIO_NET_HDR_F_DATA_VALID 2 /* Csum is valid */
  66. #endif
  67. static struct {
  68. struct mic_device_desc dd;
  69. struct mic_vqconfig vqconfig[2];
  70. __u32 host_features, guest_acknowledgements;
  71. struct virtio_console_config cons_config;
  72. } virtcons_dev_page = {
  73. .dd = {
  74. .type = VIRTIO_ID_CONSOLE,
  75. .num_vq = ARRAY_SIZE(virtcons_dev_page.vqconfig),
  76. .feature_len = sizeof(virtcons_dev_page.host_features),
  77. .config_len = sizeof(virtcons_dev_page.cons_config),
  78. },
  79. .vqconfig[0] = {
  80. .num = htole16(MIC_VRING_ENTRIES),
  81. },
  82. .vqconfig[1] = {
  83. .num = htole16(MIC_VRING_ENTRIES),
  84. },
  85. };
  86. static struct {
  87. struct mic_device_desc dd;
  88. struct mic_vqconfig vqconfig[2];
  89. __u32 host_features, guest_acknowledgements;
  90. struct virtio_net_config net_config;
  91. } virtnet_dev_page = {
  92. .dd = {
  93. .type = VIRTIO_ID_NET,
  94. .num_vq = ARRAY_SIZE(virtnet_dev_page.vqconfig),
  95. .feature_len = sizeof(virtnet_dev_page.host_features),
  96. .config_len = sizeof(virtnet_dev_page.net_config),
  97. },
  98. .vqconfig[0] = {
  99. .num = htole16(MIC_VRING_ENTRIES),
  100. },
  101. .vqconfig[1] = {
  102. .num = htole16(MIC_VRING_ENTRIES),
  103. },
  104. #if GSO_ENABLED
  105. .host_features = htole32(
  106. 1 << VIRTIO_NET_F_CSUM |
  107. 1 << VIRTIO_NET_F_GSO |
  108. 1 << VIRTIO_NET_F_GUEST_TSO4 |
  109. 1 << VIRTIO_NET_F_GUEST_TSO6 |
  110. 1 << VIRTIO_NET_F_GUEST_ECN),
  111. #else
  112. .host_features = 0,
  113. #endif
  114. };
  115. static const char *mic_config_dir = "/etc/mpss";
  116. static const char *virtblk_backend = "VIRTBLK_BACKEND";
  117. static struct {
  118. struct mic_device_desc dd;
  119. struct mic_vqconfig vqconfig[1];
  120. __u32 host_features, guest_acknowledgements;
  121. struct virtio_blk_config blk_config;
  122. } virtblk_dev_page = {
  123. .dd = {
  124. .type = VIRTIO_ID_BLOCK,
  125. .num_vq = ARRAY_SIZE(virtblk_dev_page.vqconfig),
  126. .feature_len = sizeof(virtblk_dev_page.host_features),
  127. .config_len = sizeof(virtblk_dev_page.blk_config),
  128. },
  129. .vqconfig[0] = {
  130. .num = htole16(MIC_VRING_ENTRIES),
  131. },
  132. .host_features =
  133. htole32(1<<VIRTIO_BLK_F_SEG_MAX),
  134. .blk_config = {
  135. .seg_max = htole32(MIC_VRING_ENTRIES - 2),
  136. .capacity = htole64(0),
  137. }
  138. };
  139. static char *myname;
  140. static int
  141. tap_configure(struct mic_info *mic, char *dev)
  142. {
  143. pid_t pid;
  144. char *ifargv[7];
  145. char ipaddr[IFNAMSIZ];
  146. int ret = 0;
  147. pid = fork();
  148. if (pid == 0) {
  149. ifargv[0] = "ip";
  150. ifargv[1] = "link";
  151. ifargv[2] = "set";
  152. ifargv[3] = dev;
  153. ifargv[4] = "up";
  154. ifargv[5] = NULL;
  155. mpsslog("Configuring %s\n", dev);
  156. ret = execvp("ip", ifargv);
  157. if (ret < 0) {
  158. mpsslog("%s execvp failed errno %s\n",
  159. mic->name, strerror(errno));
  160. return ret;
  161. }
  162. }
  163. if (pid < 0) {
  164. mpsslog("%s fork failed errno %s\n",
  165. mic->name, strerror(errno));
  166. return ret;
  167. }
  168. ret = waitpid(pid, NULL, 0);
  169. if (ret < 0) {
  170. mpsslog("%s waitpid failed errno %s\n",
  171. mic->name, strerror(errno));
  172. return ret;
  173. }
  174. snprintf(ipaddr, IFNAMSIZ, "172.31.%d.254/24", mic->id + 1);
  175. pid = fork();
  176. if (pid == 0) {
  177. ifargv[0] = "ip";
  178. ifargv[1] = "addr";
  179. ifargv[2] = "add";
  180. ifargv[3] = ipaddr;
  181. ifargv[4] = "dev";
  182. ifargv[5] = dev;
  183. ifargv[6] = NULL;
  184. mpsslog("Configuring %s ipaddr %s\n", dev, ipaddr);
  185. ret = execvp("ip", ifargv);
  186. if (ret < 0) {
  187. mpsslog("%s execvp failed errno %s\n",
  188. mic->name, strerror(errno));
  189. return ret;
  190. }
  191. }
  192. if (pid < 0) {
  193. mpsslog("%s fork failed errno %s\n",
  194. mic->name, strerror(errno));
  195. return ret;
  196. }
  197. ret = waitpid(pid, NULL, 0);
  198. if (ret < 0) {
  199. mpsslog("%s waitpid failed errno %s\n",
  200. mic->name, strerror(errno));
  201. return ret;
  202. }
  203. mpsslog("MIC name %s %s %d DONE!\n",
  204. mic->name, __func__, __LINE__);
  205. return 0;
  206. }
  207. static int tun_alloc(struct mic_info *mic, char *dev)
  208. {
  209. struct ifreq ifr;
  210. int fd, err;
  211. #if GSO_ENABLED
  212. unsigned offload;
  213. #endif
  214. fd = open("/dev/net/tun", O_RDWR);
  215. if (fd < 0) {
  216. mpsslog("Could not open /dev/net/tun %s\n", strerror(errno));
  217. goto done;
  218. }
  219. memset(&ifr, 0, sizeof(ifr));
  220. ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
  221. if (*dev)
  222. strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  223. err = ioctl(fd, TUNSETIFF, (void *)&ifr);
  224. if (err < 0) {
  225. mpsslog("%s %s %d TUNSETIFF failed %s\n",
  226. mic->name, __func__, __LINE__, strerror(errno));
  227. close(fd);
  228. return err;
  229. }
  230. #if GSO_ENABLED
  231. offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | TUN_F_TSO_ECN;
  232. err = ioctl(fd, TUNSETOFFLOAD, offload);
  233. if (err < 0) {
  234. mpsslog("%s %s %d TUNSETOFFLOAD failed %s\n",
  235. mic->name, __func__, __LINE__, strerror(errno));
  236. close(fd);
  237. return err;
  238. }
  239. #endif
  240. strcpy(dev, ifr.ifr_name);
  241. mpsslog("Created TAP %s\n", dev);
  242. done:
  243. return fd;
  244. }
  245. #define NET_FD_VIRTIO_NET 0
  246. #define NET_FD_TUN 1
  247. #define MAX_NET_FD 2
  248. static void set_dp(struct mic_info *mic, int type, void *dp)
  249. {
  250. switch (type) {
  251. case VIRTIO_ID_CONSOLE:
  252. mic->mic_console.console_dp = dp;
  253. return;
  254. case VIRTIO_ID_NET:
  255. mic->mic_net.net_dp = dp;
  256. return;
  257. case VIRTIO_ID_BLOCK:
  258. mic->mic_virtblk.block_dp = dp;
  259. return;
  260. }
  261. mpsslog("%s %s %d not found\n", mic->name, __func__, type);
  262. assert(0);
  263. }
  264. static void *get_dp(struct mic_info *mic, int type)
  265. {
  266. switch (type) {
  267. case VIRTIO_ID_CONSOLE:
  268. return mic->mic_console.console_dp;
  269. case VIRTIO_ID_NET:
  270. return mic->mic_net.net_dp;
  271. case VIRTIO_ID_BLOCK:
  272. return mic->mic_virtblk.block_dp;
  273. }
  274. mpsslog("%s %s %d not found\n", mic->name, __func__, type);
  275. assert(0);
  276. return NULL;
  277. }
  278. static struct mic_device_desc *get_device_desc(struct mic_info *mic, int type)
  279. {
  280. struct mic_device_desc *d;
  281. int i;
  282. void *dp = get_dp(mic, type);
  283. for (i = sizeof(struct mic_bootparam); i < PAGE_SIZE;
  284. i += mic_total_desc_size(d)) {
  285. d = dp + i;
  286. /* End of list */
  287. if (d->type == 0)
  288. break;
  289. if (d->type == -1)
  290. continue;
  291. mpsslog("%s %s d-> type %d d %p\n",
  292. mic->name, __func__, d->type, d);
  293. if (d->type == (__u8)type)
  294. return d;
  295. }
  296. mpsslog("%s %s %d not found\n", mic->name, __func__, type);
  297. return NULL;
  298. }
  299. /* See comments in vhost.c for explanation of next_desc() */
  300. static unsigned next_desc(struct vring_desc *desc)
  301. {
  302. unsigned int next;
  303. if (!(le16toh(desc->flags) & VRING_DESC_F_NEXT))
  304. return -1U;
  305. next = le16toh(desc->next);
  306. return next;
  307. }
  308. /* Sum up all the IOVEC length */
  309. static ssize_t
  310. sum_iovec_len(struct mic_copy_desc *copy)
  311. {
  312. ssize_t sum = 0;
  313. unsigned int i;
  314. for (i = 0; i < copy->iovcnt; i++)
  315. sum += copy->iov[i].iov_len;
  316. return sum;
  317. }
  318. static inline void verify_out_len(struct mic_info *mic,
  319. struct mic_copy_desc *copy)
  320. {
  321. if (copy->out_len != sum_iovec_len(copy)) {
  322. mpsslog("%s %s %d BUG copy->out_len 0x%x len 0x%zx\n",
  323. mic->name, __func__, __LINE__,
  324. copy->out_len, sum_iovec_len(copy));
  325. assert(copy->out_len == sum_iovec_len(copy));
  326. }
  327. }
  328. /* Display an iovec */
  329. static void
  330. disp_iovec(struct mic_info *mic, struct mic_copy_desc *copy,
  331. const char *s, int line)
  332. {
  333. unsigned int i;
  334. for (i = 0; i < copy->iovcnt; i++)
  335. mpsslog("%s %s %d copy->iov[%d] addr %p len 0x%zx\n",
  336. mic->name, s, line, i,
  337. copy->iov[i].iov_base, copy->iov[i].iov_len);
  338. }
  339. static inline __u16 read_avail_idx(struct mic_vring *vr)
  340. {
  341. return ACCESS_ONCE(vr->info->avail_idx);
  342. }
  343. static inline void txrx_prepare(int type, bool tx, struct mic_vring *vr,
  344. struct mic_copy_desc *copy, ssize_t len)
  345. {
  346. copy->vr_idx = tx ? 0 : 1;
  347. copy->update_used = true;
  348. if (type == VIRTIO_ID_NET)
  349. copy->iov[1].iov_len = len - sizeof(struct virtio_net_hdr);
  350. else
  351. copy->iov[0].iov_len = len;
  352. }
  353. /* Central API which triggers the copies */
  354. static int
  355. mic_virtio_copy(struct mic_info *mic, int fd,
  356. struct mic_vring *vr, struct mic_copy_desc *copy)
  357. {
  358. int ret;
  359. ret = ioctl(fd, MIC_VIRTIO_COPY_DESC, copy);
  360. if (ret) {
  361. mpsslog("%s %s %d errno %s ret %d\n",
  362. mic->name, __func__, __LINE__,
  363. strerror(errno), ret);
  364. }
  365. return ret;
  366. }
  367. static inline unsigned _vring_size(unsigned int num, unsigned long align)
  368. {
  369. return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (3 + num)
  370. + align - 1) & ~(align - 1))
  371. + sizeof(__u16) * 3 + sizeof(struct vring_used_elem) * num;
  372. }
  373. /*
  374. * This initialization routine requires at least one
  375. * vring i.e. vr0. vr1 is optional.
  376. */
  377. static void *
  378. init_vr(struct mic_info *mic, int fd, int type,
  379. struct mic_vring *vr0, struct mic_vring *vr1, int num_vq)
  380. {
  381. int vr_size;
  382. char *va;
  383. vr_size = PAGE_ALIGN(_vring_size(MIC_VRING_ENTRIES,
  384. MIC_VIRTIO_RING_ALIGN) +
  385. sizeof(struct _mic_vring_info));
  386. va = mmap(NULL, MIC_DEVICE_PAGE_END + vr_size * num_vq,
  387. PROT_READ, MAP_SHARED, fd, 0);
  388. if (MAP_FAILED == va) {
  389. mpsslog("%s %s %d mmap failed errno %s\n",
  390. mic->name, __func__, __LINE__,
  391. strerror(errno));
  392. goto done;
  393. }
  394. set_dp(mic, type, va);
  395. vr0->va = (struct mic_vring *)&va[MIC_DEVICE_PAGE_END];
  396. vr0->info = vr0->va +
  397. _vring_size(MIC_VRING_ENTRIES, MIC_VIRTIO_RING_ALIGN);
  398. vring_init(&vr0->vr,
  399. MIC_VRING_ENTRIES, vr0->va, MIC_VIRTIO_RING_ALIGN);
  400. mpsslog("%s %s vr0 %p vr0->info %p vr_size 0x%x vring 0x%x ",
  401. __func__, mic->name, vr0->va, vr0->info, vr_size,
  402. _vring_size(MIC_VRING_ENTRIES, MIC_VIRTIO_RING_ALIGN));
  403. mpsslog("magic 0x%x expected 0x%x\n",
  404. le32toh(vr0->info->magic), MIC_MAGIC + type);
  405. assert(le32toh(vr0->info->magic) == MIC_MAGIC + type);
  406. if (vr1) {
  407. vr1->va = (struct mic_vring *)
  408. &va[MIC_DEVICE_PAGE_END + vr_size];
  409. vr1->info = vr1->va + _vring_size(MIC_VRING_ENTRIES,
  410. MIC_VIRTIO_RING_ALIGN);
  411. vring_init(&vr1->vr,
  412. MIC_VRING_ENTRIES, vr1->va, MIC_VIRTIO_RING_ALIGN);
  413. mpsslog("%s %s vr1 %p vr1->info %p vr_size 0x%x vring 0x%x ",
  414. __func__, mic->name, vr1->va, vr1->info, vr_size,
  415. _vring_size(MIC_VRING_ENTRIES, MIC_VIRTIO_RING_ALIGN));
  416. mpsslog("magic 0x%x expected 0x%x\n",
  417. le32toh(vr1->info->magic), MIC_MAGIC + type + 1);
  418. assert(le32toh(vr1->info->magic) == MIC_MAGIC + type + 1);
  419. }
  420. done:
  421. return va;
  422. }
  423. static int
  424. wait_for_card_driver(struct mic_info *mic, int fd, int type)
  425. {
  426. struct pollfd pollfd;
  427. int err;
  428. struct mic_device_desc *desc = get_device_desc(mic, type);
  429. __u8 prev_status;
  430. if (!desc)
  431. return -ENODEV;
  432. prev_status = desc->status;
  433. pollfd.fd = fd;
  434. mpsslog("%s %s Waiting .... desc-> type %d status 0x%x\n",
  435. mic->name, __func__, type, desc->status);
  436. while (1) {
  437. pollfd.events = POLLIN;
  438. pollfd.revents = 0;
  439. err = poll(&pollfd, 1, -1);
  440. if (err < 0) {
  441. mpsslog("%s %s poll failed %s\n",
  442. mic->name, __func__, strerror(errno));
  443. continue;
  444. }
  445. if (pollfd.revents) {
  446. if (desc->status != prev_status) {
  447. mpsslog("%s %s Waiting... desc-> type %d "
  448. "status 0x%x\n",
  449. mic->name, __func__, type,
  450. desc->status);
  451. prev_status = desc->status;
  452. }
  453. if (desc->status & VIRTIO_CONFIG_S_DRIVER_OK) {
  454. mpsslog("%s %s poll.revents %d\n",
  455. mic->name, __func__, pollfd.revents);
  456. mpsslog("%s %s desc-> type %d status 0x%x\n",
  457. mic->name, __func__, type,
  458. desc->status);
  459. break;
  460. }
  461. }
  462. }
  463. return 0;
  464. }
  465. /* Spin till we have some descriptors */
  466. static void
  467. spin_for_descriptors(struct mic_info *mic, struct mic_vring *vr)
  468. {
  469. __u16 avail_idx = read_avail_idx(vr);
  470. while (avail_idx == le16toh(ACCESS_ONCE(vr->vr.avail->idx))) {
  471. #ifdef DEBUG
  472. mpsslog("%s %s waiting for desc avail %d info_avail %d\n",
  473. mic->name, __func__,
  474. le16toh(vr->vr.avail->idx), vr->info->avail_idx);
  475. #endif
  476. sched_yield();
  477. }
  478. }
  479. static void *
  480. virtio_net(void *arg)
  481. {
  482. static __u8 vnet_hdr[2][sizeof(struct virtio_net_hdr)];
  483. static __u8 vnet_buf[2][MAX_NET_PKT_SIZE] __attribute__ ((aligned(64)));
  484. struct iovec vnet_iov[2][2] = {
  485. { { .iov_base = vnet_hdr[0], .iov_len = sizeof(vnet_hdr[0]) },
  486. { .iov_base = vnet_buf[0], .iov_len = sizeof(vnet_buf[0]) } },
  487. { { .iov_base = vnet_hdr[1], .iov_len = sizeof(vnet_hdr[1]) },
  488. { .iov_base = vnet_buf[1], .iov_len = sizeof(vnet_buf[1]) } },
  489. };
  490. struct iovec *iov0 = vnet_iov[0], *iov1 = vnet_iov[1];
  491. struct mic_info *mic = (struct mic_info *)arg;
  492. char if_name[IFNAMSIZ];
  493. struct pollfd net_poll[MAX_NET_FD];
  494. struct mic_vring tx_vr, rx_vr;
  495. struct mic_copy_desc copy;
  496. struct mic_device_desc *desc;
  497. int err;
  498. snprintf(if_name, IFNAMSIZ, "mic%d", mic->id);
  499. mic->mic_net.tap_fd = tun_alloc(mic, if_name);
  500. if (mic->mic_net.tap_fd < 0)
  501. goto done;
  502. if (tap_configure(mic, if_name))
  503. goto done;
  504. mpsslog("MIC name %s id %d\n", mic->name, mic->id);
  505. net_poll[NET_FD_VIRTIO_NET].fd = mic->mic_net.virtio_net_fd;
  506. net_poll[NET_FD_VIRTIO_NET].events = POLLIN;
  507. net_poll[NET_FD_TUN].fd = mic->mic_net.tap_fd;
  508. net_poll[NET_FD_TUN].events = POLLIN;
  509. if (MAP_FAILED == init_vr(mic, mic->mic_net.virtio_net_fd,
  510. VIRTIO_ID_NET, &tx_vr, &rx_vr,
  511. virtnet_dev_page.dd.num_vq)) {
  512. mpsslog("%s init_vr failed %s\n",
  513. mic->name, strerror(errno));
  514. goto done;
  515. }
  516. copy.iovcnt = 2;
  517. desc = get_device_desc(mic, VIRTIO_ID_NET);
  518. while (1) {
  519. ssize_t len;
  520. net_poll[NET_FD_VIRTIO_NET].revents = 0;
  521. net_poll[NET_FD_TUN].revents = 0;
  522. /* Start polling for data from tap and virtio net */
  523. err = poll(net_poll, 2, -1);
  524. if (err < 0) {
  525. mpsslog("%s poll failed %s\n",
  526. __func__, strerror(errno));
  527. continue;
  528. }
  529. if (!(desc->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  530. err = wait_for_card_driver(mic,
  531. mic->mic_net.virtio_net_fd,
  532. VIRTIO_ID_NET);
  533. if (err) {
  534. mpsslog("%s %s %d Exiting...\n",
  535. mic->name, __func__, __LINE__);
  536. break;
  537. }
  538. }
  539. /*
  540. * Check if there is data to be read from TUN and write to
  541. * virtio net fd if there is.
  542. */
  543. if (net_poll[NET_FD_TUN].revents & POLLIN) {
  544. copy.iov = iov0;
  545. len = readv(net_poll[NET_FD_TUN].fd,
  546. copy.iov, copy.iovcnt);
  547. if (len > 0) {
  548. struct virtio_net_hdr *hdr
  549. = (struct virtio_net_hdr *)vnet_hdr[0];
  550. /* Disable checksums on the card since we are on
  551. a reliable PCIe link */
  552. hdr->flags |= VIRTIO_NET_HDR_F_DATA_VALID;
  553. #ifdef DEBUG
  554. mpsslog("%s %s %d hdr->flags 0x%x ", mic->name,
  555. __func__, __LINE__, hdr->flags);
  556. mpsslog("copy.out_len %d hdr->gso_type 0x%x\n",
  557. copy.out_len, hdr->gso_type);
  558. #endif
  559. #ifdef DEBUG
  560. disp_iovec(mic, copy, __func__, __LINE__);
  561. mpsslog("%s %s %d read from tap 0x%lx\n",
  562. mic->name, __func__, __LINE__,
  563. len);
  564. #endif
  565. spin_for_descriptors(mic, &tx_vr);
  566. txrx_prepare(VIRTIO_ID_NET, 1, &tx_vr, &copy,
  567. len);
  568. err = mic_virtio_copy(mic,
  569. mic->mic_net.virtio_net_fd, &tx_vr,
  570. &copy);
  571. if (err < 0) {
  572. mpsslog("%s %s %d mic_virtio_copy %s\n",
  573. mic->name, __func__, __LINE__,
  574. strerror(errno));
  575. }
  576. if (!err)
  577. verify_out_len(mic, &copy);
  578. #ifdef DEBUG
  579. disp_iovec(mic, copy, __func__, __LINE__);
  580. mpsslog("%s %s %d wrote to net 0x%lx\n",
  581. mic->name, __func__, __LINE__,
  582. sum_iovec_len(&copy));
  583. #endif
  584. /* Reinitialize IOV for next run */
  585. iov0[1].iov_len = MAX_NET_PKT_SIZE;
  586. } else if (len < 0) {
  587. disp_iovec(mic, &copy, __func__, __LINE__);
  588. mpsslog("%s %s %d read failed %s ", mic->name,
  589. __func__, __LINE__, strerror(errno));
  590. mpsslog("cnt %d sum %zd\n",
  591. copy.iovcnt, sum_iovec_len(&copy));
  592. }
  593. }
  594. /*
  595. * Check if there is data to be read from virtio net and
  596. * write to TUN if there is.
  597. */
  598. if (net_poll[NET_FD_VIRTIO_NET].revents & POLLIN) {
  599. while (rx_vr.info->avail_idx !=
  600. le16toh(rx_vr.vr.avail->idx)) {
  601. copy.iov = iov1;
  602. txrx_prepare(VIRTIO_ID_NET, 0, &rx_vr, &copy,
  603. MAX_NET_PKT_SIZE
  604. + sizeof(struct virtio_net_hdr));
  605. err = mic_virtio_copy(mic,
  606. mic->mic_net.virtio_net_fd, &rx_vr,
  607. &copy);
  608. if (!err) {
  609. #ifdef DEBUG
  610. struct virtio_net_hdr *hdr
  611. = (struct virtio_net_hdr *)
  612. vnet_hdr[1];
  613. mpsslog("%s %s %d hdr->flags 0x%x, ",
  614. mic->name, __func__, __LINE__,
  615. hdr->flags);
  616. mpsslog("out_len %d gso_type 0x%x\n",
  617. copy.out_len,
  618. hdr->gso_type);
  619. #endif
  620. /* Set the correct output iov_len */
  621. iov1[1].iov_len = copy.out_len -
  622. sizeof(struct virtio_net_hdr);
  623. verify_out_len(mic, &copy);
  624. #ifdef DEBUG
  625. disp_iovec(mic, copy, __func__,
  626. __LINE__);
  627. mpsslog("%s %s %d ",
  628. mic->name, __func__, __LINE__);
  629. mpsslog("read from net 0x%lx\n",
  630. sum_iovec_len(copy));
  631. #endif
  632. len = writev(net_poll[NET_FD_TUN].fd,
  633. copy.iov, copy.iovcnt);
  634. if (len != sum_iovec_len(&copy)) {
  635. mpsslog("Tun write failed %s ",
  636. strerror(errno));
  637. mpsslog("len 0x%zx ", len);
  638. mpsslog("read_len 0x%zx\n",
  639. sum_iovec_len(&copy));
  640. } else {
  641. #ifdef DEBUG
  642. disp_iovec(mic, &copy, __func__,
  643. __LINE__);
  644. mpsslog("%s %s %d ",
  645. mic->name, __func__,
  646. __LINE__);
  647. mpsslog("wrote to tap 0x%lx\n",
  648. len);
  649. #endif
  650. }
  651. } else {
  652. mpsslog("%s %s %d mic_virtio_copy %s\n",
  653. mic->name, __func__, __LINE__,
  654. strerror(errno));
  655. break;
  656. }
  657. }
  658. }
  659. if (net_poll[NET_FD_VIRTIO_NET].revents & POLLERR)
  660. mpsslog("%s: %s: POLLERR\n", __func__, mic->name);
  661. }
  662. done:
  663. pthread_exit(NULL);
  664. }
  665. /* virtio_console */
  666. #define VIRTIO_CONSOLE_FD 0
  667. #define MONITOR_FD (VIRTIO_CONSOLE_FD + 1)
  668. #define MAX_CONSOLE_FD (MONITOR_FD + 1) /* must be the last one + 1 */
  669. #define MAX_BUFFER_SIZE PAGE_SIZE
  670. static void *
  671. virtio_console(void *arg)
  672. {
  673. static __u8 vcons_buf[2][PAGE_SIZE];
  674. struct iovec vcons_iov[2] = {
  675. { .iov_base = vcons_buf[0], .iov_len = sizeof(vcons_buf[0]) },
  676. { .iov_base = vcons_buf[1], .iov_len = sizeof(vcons_buf[1]) },
  677. };
  678. struct iovec *iov0 = &vcons_iov[0], *iov1 = &vcons_iov[1];
  679. struct mic_info *mic = (struct mic_info *)arg;
  680. int err;
  681. struct pollfd console_poll[MAX_CONSOLE_FD];
  682. int pty_fd;
  683. char *pts_name;
  684. ssize_t len;
  685. struct mic_vring tx_vr, rx_vr;
  686. struct mic_copy_desc copy;
  687. struct mic_device_desc *desc;
  688. pty_fd = posix_openpt(O_RDWR);
  689. if (pty_fd < 0) {
  690. mpsslog("can't open a pseudoterminal master device: %s\n",
  691. strerror(errno));
  692. goto _return;
  693. }
  694. pts_name = ptsname(pty_fd);
  695. if (pts_name == NULL) {
  696. mpsslog("can't get pts name\n");
  697. goto _close_pty;
  698. }
  699. printf("%s console message goes to %s\n", mic->name, pts_name);
  700. mpsslog("%s console message goes to %s\n", mic->name, pts_name);
  701. err = grantpt(pty_fd);
  702. if (err < 0) {
  703. mpsslog("can't grant access: %s %s\n",
  704. pts_name, strerror(errno));
  705. goto _close_pty;
  706. }
  707. err = unlockpt(pty_fd);
  708. if (err < 0) {
  709. mpsslog("can't unlock a pseudoterminal: %s %s\n",
  710. pts_name, strerror(errno));
  711. goto _close_pty;
  712. }
  713. console_poll[MONITOR_FD].fd = pty_fd;
  714. console_poll[MONITOR_FD].events = POLLIN;
  715. console_poll[VIRTIO_CONSOLE_FD].fd = mic->mic_console.virtio_console_fd;
  716. console_poll[VIRTIO_CONSOLE_FD].events = POLLIN;
  717. if (MAP_FAILED == init_vr(mic, mic->mic_console.virtio_console_fd,
  718. VIRTIO_ID_CONSOLE, &tx_vr, &rx_vr,
  719. virtcons_dev_page.dd.num_vq)) {
  720. mpsslog("%s init_vr failed %s\n",
  721. mic->name, strerror(errno));
  722. goto _close_pty;
  723. }
  724. copy.iovcnt = 1;
  725. desc = get_device_desc(mic, VIRTIO_ID_CONSOLE);
  726. for (;;) {
  727. console_poll[MONITOR_FD].revents = 0;
  728. console_poll[VIRTIO_CONSOLE_FD].revents = 0;
  729. err = poll(console_poll, MAX_CONSOLE_FD, -1);
  730. if (err < 0) {
  731. mpsslog("%s %d: poll failed: %s\n", __func__, __LINE__,
  732. strerror(errno));
  733. continue;
  734. }
  735. if (!(desc->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  736. err = wait_for_card_driver(mic,
  737. mic->mic_console.virtio_console_fd,
  738. VIRTIO_ID_CONSOLE);
  739. if (err) {
  740. mpsslog("%s %s %d Exiting...\n",
  741. mic->name, __func__, __LINE__);
  742. break;
  743. }
  744. }
  745. if (console_poll[MONITOR_FD].revents & POLLIN) {
  746. copy.iov = iov0;
  747. len = readv(pty_fd, copy.iov, copy.iovcnt);
  748. if (len > 0) {
  749. #ifdef DEBUG
  750. disp_iovec(mic, copy, __func__, __LINE__);
  751. mpsslog("%s %s %d read from tap 0x%lx\n",
  752. mic->name, __func__, __LINE__,
  753. len);
  754. #endif
  755. spin_for_descriptors(mic, &tx_vr);
  756. txrx_prepare(VIRTIO_ID_CONSOLE, 1, &tx_vr,
  757. &copy, len);
  758. err = mic_virtio_copy(mic,
  759. mic->mic_console.virtio_console_fd,
  760. &tx_vr, &copy);
  761. if (err < 0) {
  762. mpsslog("%s %s %d mic_virtio_copy %s\n",
  763. mic->name, __func__, __LINE__,
  764. strerror(errno));
  765. }
  766. if (!err)
  767. verify_out_len(mic, &copy);
  768. #ifdef DEBUG
  769. disp_iovec(mic, copy, __func__, __LINE__);
  770. mpsslog("%s %s %d wrote to net 0x%lx\n",
  771. mic->name, __func__, __LINE__,
  772. sum_iovec_len(copy));
  773. #endif
  774. /* Reinitialize IOV for next run */
  775. iov0->iov_len = PAGE_SIZE;
  776. } else if (len < 0) {
  777. disp_iovec(mic, &copy, __func__, __LINE__);
  778. mpsslog("%s %s %d read failed %s ",
  779. mic->name, __func__, __LINE__,
  780. strerror(errno));
  781. mpsslog("cnt %d sum %zd\n",
  782. copy.iovcnt, sum_iovec_len(&copy));
  783. }
  784. }
  785. if (console_poll[VIRTIO_CONSOLE_FD].revents & POLLIN) {
  786. while (rx_vr.info->avail_idx !=
  787. le16toh(rx_vr.vr.avail->idx)) {
  788. copy.iov = iov1;
  789. txrx_prepare(VIRTIO_ID_CONSOLE, 0, &rx_vr,
  790. &copy, PAGE_SIZE);
  791. err = mic_virtio_copy(mic,
  792. mic->mic_console.virtio_console_fd,
  793. &rx_vr, &copy);
  794. if (!err) {
  795. /* Set the correct output iov_len */
  796. iov1->iov_len = copy.out_len;
  797. verify_out_len(mic, &copy);
  798. #ifdef DEBUG
  799. disp_iovec(mic, copy, __func__,
  800. __LINE__);
  801. mpsslog("%s %s %d ",
  802. mic->name, __func__, __LINE__);
  803. mpsslog("read from net 0x%lx\n",
  804. sum_iovec_len(copy));
  805. #endif
  806. len = writev(pty_fd,
  807. copy.iov, copy.iovcnt);
  808. if (len != sum_iovec_len(&copy)) {
  809. mpsslog("Tun write failed %s ",
  810. strerror(errno));
  811. mpsslog("len 0x%zx ", len);
  812. mpsslog("read_len 0x%zx\n",
  813. sum_iovec_len(&copy));
  814. } else {
  815. #ifdef DEBUG
  816. disp_iovec(mic, copy, __func__,
  817. __LINE__);
  818. mpsslog("%s %s %d ",
  819. mic->name, __func__,
  820. __LINE__);
  821. mpsslog("wrote to tap 0x%lx\n",
  822. len);
  823. #endif
  824. }
  825. } else {
  826. mpsslog("%s %s %d mic_virtio_copy %s\n",
  827. mic->name, __func__, __LINE__,
  828. strerror(errno));
  829. break;
  830. }
  831. }
  832. }
  833. if (console_poll[NET_FD_VIRTIO_NET].revents & POLLERR)
  834. mpsslog("%s: %s: POLLERR\n", __func__, mic->name);
  835. }
  836. _close_pty:
  837. close(pty_fd);
  838. _return:
  839. pthread_exit(NULL);
  840. }
  841. static void
  842. add_virtio_device(struct mic_info *mic, struct mic_device_desc *dd)
  843. {
  844. char path[PATH_MAX];
  845. int fd, err;
  846. snprintf(path, PATH_MAX, "/dev/vop_virtio%d", mic->id);
  847. fd = open(path, O_RDWR);
  848. if (fd < 0) {
  849. mpsslog("Could not open %s %s\n", path, strerror(errno));
  850. return;
  851. }
  852. err = ioctl(fd, MIC_VIRTIO_ADD_DEVICE, dd);
  853. if (err < 0) {
  854. mpsslog("Could not add %d %s\n", dd->type, strerror(errno));
  855. close(fd);
  856. return;
  857. }
  858. switch (dd->type) {
  859. case VIRTIO_ID_NET:
  860. mic->mic_net.virtio_net_fd = fd;
  861. mpsslog("Added VIRTIO_ID_NET for %s\n", mic->name);
  862. break;
  863. case VIRTIO_ID_CONSOLE:
  864. mic->mic_console.virtio_console_fd = fd;
  865. mpsslog("Added VIRTIO_ID_CONSOLE for %s\n", mic->name);
  866. break;
  867. case VIRTIO_ID_BLOCK:
  868. mic->mic_virtblk.virtio_block_fd = fd;
  869. mpsslog("Added VIRTIO_ID_BLOCK for %s\n", mic->name);
  870. break;
  871. }
  872. }
  873. static bool
  874. set_backend_file(struct mic_info *mic)
  875. {
  876. FILE *config;
  877. char buff[PATH_MAX], *line, *evv, *p;
  878. snprintf(buff, PATH_MAX, "%s/mpssd%03d.conf", mic_config_dir, mic->id);
  879. config = fopen(buff, "r");
  880. if (config == NULL)
  881. return false;
  882. do { /* look for "virtblk_backend=XXXX" */
  883. line = fgets(buff, PATH_MAX, config);
  884. if (line == NULL)
  885. break;
  886. if (*line == '#')
  887. continue;
  888. p = strchr(line, '\n');
  889. if (p)
  890. *p = '\0';
  891. } while (strncmp(line, virtblk_backend, strlen(virtblk_backend)) != 0);
  892. fclose(config);
  893. if (line == NULL)
  894. return false;
  895. evv = strchr(line, '=');
  896. if (evv == NULL)
  897. return false;
  898. mic->mic_virtblk.backend_file = malloc(strlen(evv) + 1);
  899. if (mic->mic_virtblk.backend_file == NULL) {
  900. mpsslog("%s %d can't allocate memory\n", mic->name, mic->id);
  901. return false;
  902. }
  903. strcpy(mic->mic_virtblk.backend_file, evv + 1);
  904. return true;
  905. }
  906. #define SECTOR_SIZE 512
  907. static bool
  908. set_backend_size(struct mic_info *mic)
  909. {
  910. mic->mic_virtblk.backend_size = lseek(mic->mic_virtblk.backend, 0,
  911. SEEK_END);
  912. if (mic->mic_virtblk.backend_size < 0) {
  913. mpsslog("%s: can't seek: %s\n",
  914. mic->name, mic->mic_virtblk.backend_file);
  915. return false;
  916. }
  917. virtblk_dev_page.blk_config.capacity =
  918. mic->mic_virtblk.backend_size / SECTOR_SIZE;
  919. if ((mic->mic_virtblk.backend_size % SECTOR_SIZE) != 0)
  920. virtblk_dev_page.blk_config.capacity++;
  921. virtblk_dev_page.blk_config.capacity =
  922. htole64(virtblk_dev_page.blk_config.capacity);
  923. return true;
  924. }
  925. static bool
  926. open_backend(struct mic_info *mic)
  927. {
  928. if (!set_backend_file(mic))
  929. goto _error_exit;
  930. mic->mic_virtblk.backend = open(mic->mic_virtblk.backend_file, O_RDWR);
  931. if (mic->mic_virtblk.backend < 0) {
  932. mpsslog("%s: can't open: %s\n", mic->name,
  933. mic->mic_virtblk.backend_file);
  934. goto _error_free;
  935. }
  936. if (!set_backend_size(mic))
  937. goto _error_close;
  938. mic->mic_virtblk.backend_addr = mmap(NULL,
  939. mic->mic_virtblk.backend_size,
  940. PROT_READ|PROT_WRITE, MAP_SHARED,
  941. mic->mic_virtblk.backend, 0L);
  942. if (mic->mic_virtblk.backend_addr == MAP_FAILED) {
  943. mpsslog("%s: can't map: %s %s\n",
  944. mic->name, mic->mic_virtblk.backend_file,
  945. strerror(errno));
  946. goto _error_close;
  947. }
  948. return true;
  949. _error_close:
  950. close(mic->mic_virtblk.backend);
  951. _error_free:
  952. free(mic->mic_virtblk.backend_file);
  953. _error_exit:
  954. return false;
  955. }
  956. static void
  957. close_backend(struct mic_info *mic)
  958. {
  959. munmap(mic->mic_virtblk.backend_addr, mic->mic_virtblk.backend_size);
  960. close(mic->mic_virtblk.backend);
  961. free(mic->mic_virtblk.backend_file);
  962. }
  963. static bool
  964. start_virtblk(struct mic_info *mic, struct mic_vring *vring)
  965. {
  966. if (((unsigned long)&virtblk_dev_page.blk_config % 8) != 0) {
  967. mpsslog("%s: blk_config is not 8 byte aligned.\n",
  968. mic->name);
  969. return false;
  970. }
  971. add_virtio_device(mic, &virtblk_dev_page.dd);
  972. if (MAP_FAILED == init_vr(mic, mic->mic_virtblk.virtio_block_fd,
  973. VIRTIO_ID_BLOCK, vring, NULL,
  974. virtblk_dev_page.dd.num_vq)) {
  975. mpsslog("%s init_vr failed %s\n",
  976. mic->name, strerror(errno));
  977. return false;
  978. }
  979. return true;
  980. }
  981. static void
  982. stop_virtblk(struct mic_info *mic)
  983. {
  984. int vr_size, ret;
  985. vr_size = PAGE_ALIGN(_vring_size(MIC_VRING_ENTRIES,
  986. MIC_VIRTIO_RING_ALIGN) +
  987. sizeof(struct _mic_vring_info));
  988. ret = munmap(mic->mic_virtblk.block_dp,
  989. MIC_DEVICE_PAGE_END + vr_size * virtblk_dev_page.dd.num_vq);
  990. if (ret < 0)
  991. mpsslog("%s munmap errno %d\n", mic->name, errno);
  992. close(mic->mic_virtblk.virtio_block_fd);
  993. }
  994. static __u8
  995. header_error_check(struct vring_desc *desc)
  996. {
  997. if (le32toh(desc->len) != sizeof(struct virtio_blk_outhdr)) {
  998. mpsslog("%s() %d: length is not sizeof(virtio_blk_outhd)\n",
  999. __func__, __LINE__);
  1000. return -EIO;
  1001. }
  1002. if (!(le16toh(desc->flags) & VRING_DESC_F_NEXT)) {
  1003. mpsslog("%s() %d: alone\n",
  1004. __func__, __LINE__);
  1005. return -EIO;
  1006. }
  1007. if (le16toh(desc->flags) & VRING_DESC_F_WRITE) {
  1008. mpsslog("%s() %d: not read\n",
  1009. __func__, __LINE__);
  1010. return -EIO;
  1011. }
  1012. return 0;
  1013. }
  1014. static int
  1015. read_header(int fd, struct virtio_blk_outhdr *hdr, __u32 desc_idx)
  1016. {
  1017. struct iovec iovec;
  1018. struct mic_copy_desc copy;
  1019. iovec.iov_len = sizeof(*hdr);
  1020. iovec.iov_base = hdr;
  1021. copy.iov = &iovec;
  1022. copy.iovcnt = 1;
  1023. copy.vr_idx = 0; /* only one vring on virtio_block */
  1024. copy.update_used = false; /* do not update used index */
  1025. return ioctl(fd, MIC_VIRTIO_COPY_DESC, &copy);
  1026. }
  1027. static int
  1028. transfer_blocks(int fd, struct iovec *iovec, __u32 iovcnt)
  1029. {
  1030. struct mic_copy_desc copy;
  1031. copy.iov = iovec;
  1032. copy.iovcnt = iovcnt;
  1033. copy.vr_idx = 0; /* only one vring on virtio_block */
  1034. copy.update_used = false; /* do not update used index */
  1035. return ioctl(fd, MIC_VIRTIO_COPY_DESC, &copy);
  1036. }
  1037. static __u8
  1038. status_error_check(struct vring_desc *desc)
  1039. {
  1040. if (le32toh(desc->len) != sizeof(__u8)) {
  1041. mpsslog("%s() %d: length is not sizeof(status)\n",
  1042. __func__, __LINE__);
  1043. return -EIO;
  1044. }
  1045. return 0;
  1046. }
  1047. static int
  1048. write_status(int fd, __u8 *status)
  1049. {
  1050. struct iovec iovec;
  1051. struct mic_copy_desc copy;
  1052. iovec.iov_base = status;
  1053. iovec.iov_len = sizeof(*status);
  1054. copy.iov = &iovec;
  1055. copy.iovcnt = 1;
  1056. copy.vr_idx = 0; /* only one vring on virtio_block */
  1057. copy.update_used = true; /* Update used index */
  1058. return ioctl(fd, MIC_VIRTIO_COPY_DESC, &copy);
  1059. }
  1060. #ifndef VIRTIO_BLK_T_GET_ID
  1061. #define VIRTIO_BLK_T_GET_ID 8
  1062. #endif
  1063. static void *
  1064. virtio_block(void *arg)
  1065. {
  1066. struct mic_info *mic = (struct mic_info *)arg;
  1067. int ret;
  1068. struct pollfd block_poll;
  1069. struct mic_vring vring;
  1070. __u16 avail_idx;
  1071. __u32 desc_idx;
  1072. struct vring_desc *desc;
  1073. struct iovec *iovec, *piov;
  1074. __u8 status;
  1075. __u32 buffer_desc_idx;
  1076. struct virtio_blk_outhdr hdr;
  1077. void *fos;
  1078. for (;;) { /* forever */
  1079. if (!open_backend(mic)) { /* No virtblk */
  1080. for (mic->mic_virtblk.signaled = 0;
  1081. !mic->mic_virtblk.signaled;)
  1082. sleep(1);
  1083. continue;
  1084. }
  1085. /* backend file is specified. */
  1086. if (!start_virtblk(mic, &vring))
  1087. goto _close_backend;
  1088. iovec = malloc(sizeof(*iovec) *
  1089. le32toh(virtblk_dev_page.blk_config.seg_max));
  1090. if (!iovec) {
  1091. mpsslog("%s: can't alloc iovec: %s\n",
  1092. mic->name, strerror(ENOMEM));
  1093. goto _stop_virtblk;
  1094. }
  1095. block_poll.fd = mic->mic_virtblk.virtio_block_fd;
  1096. block_poll.events = POLLIN;
  1097. for (mic->mic_virtblk.signaled = 0;
  1098. !mic->mic_virtblk.signaled;) {
  1099. block_poll.revents = 0;
  1100. /* timeout in 1 sec to see signaled */
  1101. ret = poll(&block_poll, 1, 1000);
  1102. if (ret < 0) {
  1103. mpsslog("%s %d: poll failed: %s\n",
  1104. __func__, __LINE__,
  1105. strerror(errno));
  1106. continue;
  1107. }
  1108. if (!(block_poll.revents & POLLIN)) {
  1109. #ifdef DEBUG
  1110. mpsslog("%s %d: block_poll.revents=0x%x\n",
  1111. __func__, __LINE__, block_poll.revents);
  1112. #endif
  1113. continue;
  1114. }
  1115. /* POLLIN */
  1116. while (vring.info->avail_idx !=
  1117. le16toh(vring.vr.avail->idx)) {
  1118. /* read header element */
  1119. avail_idx =
  1120. vring.info->avail_idx &
  1121. (vring.vr.num - 1);
  1122. desc_idx = le16toh(
  1123. vring.vr.avail->ring[avail_idx]);
  1124. desc = &vring.vr.desc[desc_idx];
  1125. #ifdef DEBUG
  1126. mpsslog("%s() %d: avail_idx=%d ",
  1127. __func__, __LINE__,
  1128. vring.info->avail_idx);
  1129. mpsslog("vring.vr.num=%d desc=%p\n",
  1130. vring.vr.num, desc);
  1131. #endif
  1132. status = header_error_check(desc);
  1133. ret = read_header(
  1134. mic->mic_virtblk.virtio_block_fd,
  1135. &hdr, desc_idx);
  1136. if (ret < 0) {
  1137. mpsslog("%s() %d %s: ret=%d %s\n",
  1138. __func__, __LINE__,
  1139. mic->name, ret,
  1140. strerror(errno));
  1141. break;
  1142. }
  1143. /* buffer element */
  1144. piov = iovec;
  1145. status = 0;
  1146. fos = mic->mic_virtblk.backend_addr +
  1147. (hdr.sector * SECTOR_SIZE);
  1148. buffer_desc_idx = next_desc(desc);
  1149. desc_idx = buffer_desc_idx;
  1150. for (desc = &vring.vr.desc[buffer_desc_idx];
  1151. desc->flags & VRING_DESC_F_NEXT;
  1152. desc_idx = next_desc(desc),
  1153. desc = &vring.vr.desc[desc_idx]) {
  1154. piov->iov_len = desc->len;
  1155. piov->iov_base = fos;
  1156. piov++;
  1157. fos += desc->len;
  1158. }
  1159. /* Returning NULLs for VIRTIO_BLK_T_GET_ID. */
  1160. if (hdr.type & ~(VIRTIO_BLK_T_OUT |
  1161. VIRTIO_BLK_T_GET_ID)) {
  1162. /*
  1163. VIRTIO_BLK_T_IN - does not do
  1164. anything. Probably for documenting.
  1165. VIRTIO_BLK_T_SCSI_CMD - for
  1166. virtio_scsi.
  1167. VIRTIO_BLK_T_FLUSH - turned off in
  1168. config space.
  1169. VIRTIO_BLK_T_BARRIER - defined but not
  1170. used in anywhere.
  1171. */
  1172. mpsslog("%s() %d: type %x ",
  1173. __func__, __LINE__,
  1174. hdr.type);
  1175. mpsslog("is not supported\n");
  1176. status = -ENOTSUP;
  1177. } else {
  1178. ret = transfer_blocks(
  1179. mic->mic_virtblk.virtio_block_fd,
  1180. iovec,
  1181. piov - iovec);
  1182. if (ret < 0 &&
  1183. status != 0)
  1184. status = ret;
  1185. }
  1186. /* write status and update used pointer */
  1187. if (status != 0)
  1188. status = status_error_check(desc);
  1189. ret = write_status(
  1190. mic->mic_virtblk.virtio_block_fd,
  1191. &status);
  1192. #ifdef DEBUG
  1193. mpsslog("%s() %d: write status=%d on desc=%p\n",
  1194. __func__, __LINE__,
  1195. status, desc);
  1196. #endif
  1197. }
  1198. }
  1199. free(iovec);
  1200. _stop_virtblk:
  1201. stop_virtblk(mic);
  1202. _close_backend:
  1203. close_backend(mic);
  1204. } /* forever */
  1205. pthread_exit(NULL);
  1206. }
  1207. static void
  1208. reset(struct mic_info *mic)
  1209. {
  1210. #define RESET_TIMEOUT 120
  1211. int i = RESET_TIMEOUT;
  1212. setsysfs(mic->name, "state", "reset");
  1213. while (i) {
  1214. char *state;
  1215. state = readsysfs(mic->name, "state");
  1216. if (!state)
  1217. goto retry;
  1218. mpsslog("%s: %s %d state %s\n",
  1219. mic->name, __func__, __LINE__, state);
  1220. if (!strcmp(state, "ready")) {
  1221. free(state);
  1222. break;
  1223. }
  1224. free(state);
  1225. retry:
  1226. sleep(1);
  1227. i--;
  1228. }
  1229. }
  1230. static int
  1231. get_mic_shutdown_status(struct mic_info *mic, char *shutdown_status)
  1232. {
  1233. if (!strcmp(shutdown_status, "nop"))
  1234. return MIC_NOP;
  1235. if (!strcmp(shutdown_status, "crashed"))
  1236. return MIC_CRASHED;
  1237. if (!strcmp(shutdown_status, "halted"))
  1238. return MIC_HALTED;
  1239. if (!strcmp(shutdown_status, "poweroff"))
  1240. return MIC_POWER_OFF;
  1241. if (!strcmp(shutdown_status, "restart"))
  1242. return MIC_RESTART;
  1243. mpsslog("%s: BUG invalid status %s\n", mic->name, shutdown_status);
  1244. /* Invalid state */
  1245. assert(0);
  1246. };
  1247. static int get_mic_state(struct mic_info *mic)
  1248. {
  1249. char *state = NULL;
  1250. enum mic_states mic_state;
  1251. while (!state) {
  1252. state = readsysfs(mic->name, "state");
  1253. sleep(1);
  1254. }
  1255. mpsslog("%s: %s %d state %s\n",
  1256. mic->name, __func__, __LINE__, state);
  1257. if (!strcmp(state, "ready")) {
  1258. mic_state = MIC_READY;
  1259. } else if (!strcmp(state, "booting")) {
  1260. mic_state = MIC_BOOTING;
  1261. } else if (!strcmp(state, "online")) {
  1262. mic_state = MIC_ONLINE;
  1263. } else if (!strcmp(state, "shutting_down")) {
  1264. mic_state = MIC_SHUTTING_DOWN;
  1265. } else if (!strcmp(state, "reset_failed")) {
  1266. mic_state = MIC_RESET_FAILED;
  1267. } else if (!strcmp(state, "resetting")) {
  1268. mic_state = MIC_RESETTING;
  1269. } else {
  1270. mpsslog("%s: BUG invalid state %s\n", mic->name, state);
  1271. assert(0);
  1272. }
  1273. free(state);
  1274. return mic_state;
  1275. };
  1276. static void mic_handle_shutdown(struct mic_info *mic)
  1277. {
  1278. #define SHUTDOWN_TIMEOUT 60
  1279. int i = SHUTDOWN_TIMEOUT;
  1280. char *shutdown_status;
  1281. while (i) {
  1282. shutdown_status = readsysfs(mic->name, "shutdown_status");
  1283. if (!shutdown_status) {
  1284. sleep(1);
  1285. continue;
  1286. }
  1287. mpsslog("%s: %s %d shutdown_status %s\n",
  1288. mic->name, __func__, __LINE__, shutdown_status);
  1289. switch (get_mic_shutdown_status(mic, shutdown_status)) {
  1290. case MIC_RESTART:
  1291. mic->restart = 1;
  1292. case MIC_HALTED:
  1293. case MIC_POWER_OFF:
  1294. case MIC_CRASHED:
  1295. free(shutdown_status);
  1296. goto reset;
  1297. default:
  1298. break;
  1299. }
  1300. free(shutdown_status);
  1301. sleep(1);
  1302. i--;
  1303. }
  1304. reset:
  1305. if (!i)
  1306. mpsslog("%s: %s %d timing out waiting for shutdown_status %s\n",
  1307. mic->name, __func__, __LINE__, shutdown_status);
  1308. reset(mic);
  1309. }
  1310. static int open_state_fd(struct mic_info *mic)
  1311. {
  1312. char pathname[PATH_MAX];
  1313. int fd;
  1314. snprintf(pathname, PATH_MAX - 1, "%s/%s/%s",
  1315. MICSYSFSDIR, mic->name, "state");
  1316. fd = open(pathname, O_RDONLY);
  1317. if (fd < 0)
  1318. mpsslog("%s: opening file %s failed %s\n",
  1319. mic->name, pathname, strerror(errno));
  1320. return fd;
  1321. }
  1322. static int block_till_state_change(int fd, struct mic_info *mic)
  1323. {
  1324. struct pollfd ufds[1];
  1325. char value[PAGE_SIZE];
  1326. int ret;
  1327. ufds[0].fd = fd;
  1328. ufds[0].events = POLLERR | POLLPRI;
  1329. ret = poll(ufds, 1, -1);
  1330. if (ret < 0) {
  1331. mpsslog("%s: %s %d poll failed %s\n",
  1332. mic->name, __func__, __LINE__, strerror(errno));
  1333. return ret;
  1334. }
  1335. ret = lseek(fd, 0, SEEK_SET);
  1336. if (ret < 0) {
  1337. mpsslog("%s: %s %d Failed to seek to 0: %s\n",
  1338. mic->name, __func__, __LINE__, strerror(errno));
  1339. return ret;
  1340. }
  1341. ret = read(fd, value, sizeof(value));
  1342. if (ret < 0) {
  1343. mpsslog("%s: %s %d Failed to read sysfs entry: %s\n",
  1344. mic->name, __func__, __LINE__, strerror(errno));
  1345. return ret;
  1346. }
  1347. return 0;
  1348. }
  1349. static void *
  1350. mic_config(void *arg)
  1351. {
  1352. struct mic_info *mic = (struct mic_info *)arg;
  1353. int fd, ret, stat = 0;
  1354. fd = open_state_fd(mic);
  1355. if (fd < 0) {
  1356. mpsslog("%s: %s %d open state fd failed %s\n",
  1357. mic->name, __func__, __LINE__, strerror(errno));
  1358. goto exit;
  1359. }
  1360. do {
  1361. ret = block_till_state_change(fd, mic);
  1362. if (ret < 0) {
  1363. mpsslog("%s: %s %d block_till_state_change error %s\n",
  1364. mic->name, __func__, __LINE__, strerror(errno));
  1365. goto close_exit;
  1366. }
  1367. switch (get_mic_state(mic)) {
  1368. case MIC_SHUTTING_DOWN:
  1369. mic_handle_shutdown(mic);
  1370. break;
  1371. case MIC_READY:
  1372. case MIC_RESET_FAILED:
  1373. ret = kill(mic->pid, SIGTERM);
  1374. mpsslog("%s: %s %d kill pid %d ret %d\n",
  1375. mic->name, __func__, __LINE__,
  1376. mic->pid, ret);
  1377. if (!ret) {
  1378. ret = waitpid(mic->pid, &stat,
  1379. WIFSIGNALED(stat));
  1380. mpsslog("%s: %s %d waitpid ret %d pid %d\n",
  1381. mic->name, __func__, __LINE__,
  1382. ret, mic->pid);
  1383. }
  1384. if (mic->boot_on_resume) {
  1385. setsysfs(mic->name, "state", "boot");
  1386. mic->boot_on_resume = 0;
  1387. }
  1388. goto close_exit;
  1389. default:
  1390. break;
  1391. }
  1392. } while (1);
  1393. close_exit:
  1394. close(fd);
  1395. exit:
  1396. init_mic(mic);
  1397. pthread_exit(NULL);
  1398. }
  1399. static void
  1400. set_cmdline(struct mic_info *mic)
  1401. {
  1402. char buffer[PATH_MAX];
  1403. int len;
  1404. len = snprintf(buffer, PATH_MAX,
  1405. "clocksource=tsc highres=off nohz=off ");
  1406. len += snprintf(buffer + len, PATH_MAX - len,
  1407. "cpufreq_on;corec6_off;pc3_off;pc6_off ");
  1408. len += snprintf(buffer + len, PATH_MAX - len,
  1409. "ifcfg=static;address,172.31.%d.1;netmask,255.255.255.0",
  1410. mic->id + 1);
  1411. setsysfs(mic->name, "cmdline", buffer);
  1412. mpsslog("%s: Command line: \"%s\"\n", mic->name, buffer);
  1413. snprintf(buffer, PATH_MAX, "172.31.%d.1", mic->id + 1);
  1414. mpsslog("%s: IPADDR: \"%s\"\n", mic->name, buffer);
  1415. }
  1416. static void
  1417. set_log_buf_info(struct mic_info *mic)
  1418. {
  1419. int fd;
  1420. off_t len;
  1421. char system_map[] = "/lib/firmware/mic/System.map";
  1422. char *map, *temp, log_buf[17] = {'\0'};
  1423. fd = open(system_map, O_RDONLY);
  1424. if (fd < 0) {
  1425. mpsslog("%s: Opening System.map failed: %d\n",
  1426. mic->name, errno);
  1427. return;
  1428. }
  1429. len = lseek(fd, 0, SEEK_END);
  1430. if (len < 0) {
  1431. mpsslog("%s: Reading System.map size failed: %d\n",
  1432. mic->name, errno);
  1433. close(fd);
  1434. return;
  1435. }
  1436. map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
  1437. if (map == MAP_FAILED) {
  1438. mpsslog("%s: mmap of System.map failed: %d\n",
  1439. mic->name, errno);
  1440. close(fd);
  1441. return;
  1442. }
  1443. temp = strstr(map, "__log_buf");
  1444. if (!temp) {
  1445. mpsslog("%s: __log_buf not found: %d\n", mic->name, errno);
  1446. munmap(map, len);
  1447. close(fd);
  1448. return;
  1449. }
  1450. strncpy(log_buf, temp - 19, 16);
  1451. setsysfs(mic->name, "log_buf_addr", log_buf);
  1452. mpsslog("%s: log_buf_addr: %s\n", mic->name, log_buf);
  1453. temp = strstr(map, "log_buf_len");
  1454. if (!temp) {
  1455. mpsslog("%s: log_buf_len not found: %d\n", mic->name, errno);
  1456. munmap(map, len);
  1457. close(fd);
  1458. return;
  1459. }
  1460. strncpy(log_buf, temp - 19, 16);
  1461. setsysfs(mic->name, "log_buf_len", log_buf);
  1462. mpsslog("%s: log_buf_len: %s\n", mic->name, log_buf);
  1463. munmap(map, len);
  1464. close(fd);
  1465. }
  1466. static void
  1467. change_virtblk_backend(int x, siginfo_t *siginfo, void *p)
  1468. {
  1469. struct mic_info *mic;
  1470. for (mic = mic_list.next; mic != NULL; mic = mic->next)
  1471. mic->mic_virtblk.signaled = 1/* true */;
  1472. }
  1473. static void
  1474. set_mic_boot_params(struct mic_info *mic)
  1475. {
  1476. set_log_buf_info(mic);
  1477. set_cmdline(mic);
  1478. }
  1479. static void *
  1480. init_mic(void *arg)
  1481. {
  1482. struct mic_info *mic = (struct mic_info *)arg;
  1483. struct sigaction ignore = {
  1484. .sa_flags = 0,
  1485. .sa_handler = SIG_IGN
  1486. };
  1487. struct sigaction act = {
  1488. .sa_flags = SA_SIGINFO,
  1489. .sa_sigaction = change_virtblk_backend,
  1490. };
  1491. char buffer[PATH_MAX];
  1492. int err, fd;
  1493. /*
  1494. * Currently, one virtio block device is supported for each MIC card
  1495. * at a time. Any user (or test) can send a SIGUSR1 to the MIC daemon.
  1496. * The signal informs the virtio block backend about a change in the
  1497. * configuration file which specifies the virtio backend file name on
  1498. * the host. Virtio block backend then re-reads the configuration file
  1499. * and switches to the new block device. This signalling mechanism may
  1500. * not be required once multiple virtio block devices are supported by
  1501. * the MIC daemon.
  1502. */
  1503. sigaction(SIGUSR1, &ignore, NULL);
  1504. retry:
  1505. fd = open_state_fd(mic);
  1506. if (fd < 0) {
  1507. mpsslog("%s: %s %d open state fd failed %s\n",
  1508. mic->name, __func__, __LINE__, strerror(errno));
  1509. sleep(2);
  1510. goto retry;
  1511. }
  1512. if (mic->restart) {
  1513. snprintf(buffer, PATH_MAX, "boot");
  1514. setsysfs(mic->name, "state", buffer);
  1515. mpsslog("%s restarting mic %d\n",
  1516. mic->name, mic->restart);
  1517. mic->restart = 0;
  1518. }
  1519. while (1) {
  1520. while (block_till_state_change(fd, mic)) {
  1521. mpsslog("%s: %s %d block_till_state_change error %s\n",
  1522. mic->name, __func__, __LINE__, strerror(errno));
  1523. sleep(2);
  1524. continue;
  1525. }
  1526. if (get_mic_state(mic) == MIC_BOOTING)
  1527. break;
  1528. }
  1529. mic->pid = fork();
  1530. switch (mic->pid) {
  1531. case 0:
  1532. add_virtio_device(mic, &virtcons_dev_page.dd);
  1533. add_virtio_device(mic, &virtnet_dev_page.dd);
  1534. err = pthread_create(&mic->mic_console.console_thread, NULL,
  1535. virtio_console, mic);
  1536. if (err)
  1537. mpsslog("%s virtcons pthread_create failed %s\n",
  1538. mic->name, strerror(err));
  1539. err = pthread_create(&mic->mic_net.net_thread, NULL,
  1540. virtio_net, mic);
  1541. if (err)
  1542. mpsslog("%s virtnet pthread_create failed %s\n",
  1543. mic->name, strerror(err));
  1544. err = pthread_create(&mic->mic_virtblk.block_thread, NULL,
  1545. virtio_block, mic);
  1546. if (err)
  1547. mpsslog("%s virtblk pthread_create failed %s\n",
  1548. mic->name, strerror(err));
  1549. sigemptyset(&act.sa_mask);
  1550. err = sigaction(SIGUSR1, &act, NULL);
  1551. if (err)
  1552. mpsslog("%s sigaction SIGUSR1 failed %s\n",
  1553. mic->name, strerror(errno));
  1554. while (1)
  1555. sleep(60);
  1556. case -1:
  1557. mpsslog("fork failed MIC name %s id %d errno %d\n",
  1558. mic->name, mic->id, errno);
  1559. break;
  1560. default:
  1561. err = pthread_create(&mic->config_thread, NULL,
  1562. mic_config, mic);
  1563. if (err)
  1564. mpsslog("%s mic_config pthread_create failed %s\n",
  1565. mic->name, strerror(err));
  1566. }
  1567. return NULL;
  1568. }
  1569. static void
  1570. start_daemon(void)
  1571. {
  1572. struct mic_info *mic;
  1573. int err;
  1574. for (mic = mic_list.next; mic; mic = mic->next) {
  1575. set_mic_boot_params(mic);
  1576. err = pthread_create(&mic->init_thread, NULL, init_mic, mic);
  1577. if (err)
  1578. mpsslog("%s init_mic pthread_create failed %s\n",
  1579. mic->name, strerror(err));
  1580. }
  1581. while (1)
  1582. sleep(60);
  1583. }
  1584. static int
  1585. init_mic_list(void)
  1586. {
  1587. struct mic_info *mic = &mic_list;
  1588. struct dirent *file;
  1589. DIR *dp;
  1590. int cnt = 0;
  1591. dp = opendir(MICSYSFSDIR);
  1592. if (!dp)
  1593. return 0;
  1594. while ((file = readdir(dp)) != NULL) {
  1595. if (!strncmp(file->d_name, "mic", 3)) {
  1596. mic->next = calloc(1, sizeof(struct mic_info));
  1597. if (mic->next) {
  1598. mic = mic->next;
  1599. mic->id = atoi(&file->d_name[3]);
  1600. mic->name = malloc(strlen(file->d_name) + 16);
  1601. if (mic->name)
  1602. strcpy(mic->name, file->d_name);
  1603. mpsslog("MIC name %s id %d\n", mic->name,
  1604. mic->id);
  1605. cnt++;
  1606. }
  1607. }
  1608. }
  1609. closedir(dp);
  1610. return cnt;
  1611. }
  1612. void
  1613. mpsslog(char *format, ...)
  1614. {
  1615. va_list args;
  1616. char buffer[4096];
  1617. char ts[52], *ts1;
  1618. time_t t;
  1619. if (logfp == NULL)
  1620. return;
  1621. va_start(args, format);
  1622. vsprintf(buffer, format, args);
  1623. va_end(args);
  1624. time(&t);
  1625. ts1 = ctime_r(&t, ts);
  1626. ts1[strlen(ts1) - 1] = '\0';
  1627. fprintf(logfp, "%s: %s", ts1, buffer);
  1628. fflush(logfp);
  1629. }
  1630. int
  1631. main(int argc, char *argv[])
  1632. {
  1633. int cnt;
  1634. pid_t pid;
  1635. myname = argv[0];
  1636. logfp = fopen(LOGFILE_NAME, "a+");
  1637. if (!logfp) {
  1638. fprintf(stderr, "cannot open logfile '%s'\n", LOGFILE_NAME);
  1639. exit(1);
  1640. }
  1641. pid = fork();
  1642. switch (pid) {
  1643. case 0:
  1644. break;
  1645. case -1:
  1646. exit(2);
  1647. default:
  1648. exit(0);
  1649. }
  1650. mpsslog("MIC Daemon start\n");
  1651. cnt = init_mic_list();
  1652. if (cnt == 0) {
  1653. mpsslog("MIC module not loaded\n");
  1654. exit(3);
  1655. }
  1656. mpsslog("MIC found %d devices\n", cnt);
  1657. start_daemon();
  1658. exit(0);
  1659. }