xen-scsiback.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. /*
  2. * Xen SCSI backend driver
  3. *
  4. * Copyright (c) 2008, FUJITSU Limited
  5. *
  6. * Based on the blkback driver code.
  7. * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation; or, when distributed
  12. * separately from the Linux kernel or incorporated into other
  13. * software packages, subject to the following license:
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy
  16. * of this source file (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy, modify,
  18. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  19. * and to permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included in
  23. * all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  31. * IN THE SOFTWARE.
  32. */
  33. #define pr_fmt(fmt) "xen-pvscsi: " fmt
  34. #include <stdarg.h>
  35. #include <linux/module.h>
  36. #include <linux/utsname.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/slab.h>
  39. #include <linux/wait.h>
  40. #include <linux/sched.h>
  41. #include <linux/list.h>
  42. #include <linux/gfp.h>
  43. #include <linux/delay.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/configfs.h>
  46. #include <generated/utsrelease.h>
  47. #include <scsi/scsi_host.h> /* SG_ALL */
  48. #include <target/target_core_base.h>
  49. #include <target/target_core_fabric.h>
  50. #include <asm/hypervisor.h>
  51. #include <xen/xen.h>
  52. #include <xen/balloon.h>
  53. #include <xen/events.h>
  54. #include <xen/xenbus.h>
  55. #include <xen/grant_table.h>
  56. #include <xen/page.h>
  57. #include <xen/interface/grant_table.h>
  58. #include <xen/interface/io/vscsiif.h>
  59. #define VSCSI_VERSION "v0.1"
  60. #define VSCSI_NAMELEN 32
  61. struct ids_tuple {
  62. unsigned int hst; /* host */
  63. unsigned int chn; /* channel */
  64. unsigned int tgt; /* target */
  65. unsigned int lun; /* LUN */
  66. };
  67. struct v2p_entry {
  68. struct ids_tuple v; /* translate from */
  69. struct scsiback_tpg *tpg; /* translate to */
  70. unsigned int lun;
  71. struct kref kref;
  72. struct list_head l;
  73. };
  74. struct vscsibk_info {
  75. struct xenbus_device *dev;
  76. domid_t domid;
  77. unsigned int irq;
  78. struct vscsiif_back_ring ring;
  79. int ring_error;
  80. spinlock_t ring_lock;
  81. atomic_t nr_unreplied_reqs;
  82. spinlock_t v2p_lock;
  83. struct list_head v2p_entry_lists;
  84. wait_queue_head_t waiting_to_free;
  85. };
  86. /* theoretical maximum of grants for one request */
  87. #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
  88. /*
  89. * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
  90. * call to map/unmap grants. Don't choose it too large, as there are arrays
  91. * with VSCSI_GRANT_BATCH elements allocated on the stack.
  92. */
  93. #define VSCSI_GRANT_BATCH 16
  94. struct vscsibk_pend {
  95. uint16_t rqid;
  96. uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
  97. uint8_t cmd_len;
  98. uint8_t sc_data_direction;
  99. uint16_t n_sg; /* real length of SG list */
  100. uint16_t n_grants; /* SG pages and potentially SG list */
  101. uint32_t data_len;
  102. uint32_t result;
  103. struct vscsibk_info *info;
  104. struct v2p_entry *v2p;
  105. struct scatterlist *sgl;
  106. uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
  107. grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
  108. struct page *pages[VSCSI_MAX_GRANTS];
  109. struct se_cmd se_cmd;
  110. };
  111. struct scsiback_tmr {
  112. atomic_t tmr_complete;
  113. wait_queue_head_t tmr_wait;
  114. };
  115. struct scsiback_nexus {
  116. /* Pointer to TCM session for I_T Nexus */
  117. struct se_session *tvn_se_sess;
  118. };
  119. struct scsiback_tport {
  120. /* SCSI protocol the tport is providing */
  121. u8 tport_proto_id;
  122. /* Binary World Wide unique Port Name for pvscsi Target port */
  123. u64 tport_wwpn;
  124. /* ASCII formatted WWPN for pvscsi Target port */
  125. char tport_name[VSCSI_NAMELEN];
  126. /* Returned by scsiback_make_tport() */
  127. struct se_wwn tport_wwn;
  128. };
  129. struct scsiback_tpg {
  130. /* scsiback port target portal group tag for TCM */
  131. u16 tport_tpgt;
  132. /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
  133. int tv_tpg_port_count;
  134. /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
  135. int tv_tpg_fe_count;
  136. /* list for scsiback_list */
  137. struct list_head tv_tpg_list;
  138. /* Used to protect access for tpg_nexus */
  139. struct mutex tv_tpg_mutex;
  140. /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
  141. struct scsiback_nexus *tpg_nexus;
  142. /* Pointer back to scsiback_tport */
  143. struct scsiback_tport *tport;
  144. /* Returned by scsiback_make_tpg() */
  145. struct se_portal_group se_tpg;
  146. /* alias used in xenstore */
  147. char param_alias[VSCSI_NAMELEN];
  148. /* list of info structures related to this target portal group */
  149. struct list_head info_list;
  150. };
  151. #define SCSIBACK_INVALID_HANDLE (~0)
  152. static bool log_print_stat;
  153. module_param(log_print_stat, bool, 0644);
  154. static int scsiback_max_buffer_pages = 1024;
  155. module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
  156. MODULE_PARM_DESC(max_buffer_pages,
  157. "Maximum number of free pages to keep in backend buffer");
  158. static struct kmem_cache *scsiback_cachep;
  159. static DEFINE_SPINLOCK(free_pages_lock);
  160. static int free_pages_num;
  161. static LIST_HEAD(scsiback_free_pages);
  162. /* Global spinlock to protect scsiback TPG list */
  163. static DEFINE_MUTEX(scsiback_mutex);
  164. static LIST_HEAD(scsiback_list);
  165. static void scsiback_get(struct vscsibk_info *info)
  166. {
  167. atomic_inc(&info->nr_unreplied_reqs);
  168. }
  169. static void scsiback_put(struct vscsibk_info *info)
  170. {
  171. if (atomic_dec_and_test(&info->nr_unreplied_reqs))
  172. wake_up(&info->waiting_to_free);
  173. }
  174. static void put_free_pages(struct page **page, int num)
  175. {
  176. unsigned long flags;
  177. int i = free_pages_num + num, n = num;
  178. if (num == 0)
  179. return;
  180. if (i > scsiback_max_buffer_pages) {
  181. n = min(num, i - scsiback_max_buffer_pages);
  182. gnttab_free_pages(n, page + num - n);
  183. n = num - n;
  184. }
  185. spin_lock_irqsave(&free_pages_lock, flags);
  186. for (i = 0; i < n; i++)
  187. list_add(&page[i]->lru, &scsiback_free_pages);
  188. free_pages_num += n;
  189. spin_unlock_irqrestore(&free_pages_lock, flags);
  190. }
  191. static int get_free_page(struct page **page)
  192. {
  193. unsigned long flags;
  194. spin_lock_irqsave(&free_pages_lock, flags);
  195. if (list_empty(&scsiback_free_pages)) {
  196. spin_unlock_irqrestore(&free_pages_lock, flags);
  197. return gnttab_alloc_pages(1, page);
  198. }
  199. page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
  200. list_del(&page[0]->lru);
  201. free_pages_num--;
  202. spin_unlock_irqrestore(&free_pages_lock, flags);
  203. return 0;
  204. }
  205. static unsigned long vaddr_page(struct page *page)
  206. {
  207. unsigned long pfn = page_to_pfn(page);
  208. return (unsigned long)pfn_to_kaddr(pfn);
  209. }
  210. static unsigned long vaddr(struct vscsibk_pend *req, int seg)
  211. {
  212. return vaddr_page(req->pages[seg]);
  213. }
  214. static void scsiback_print_status(char *sense_buffer, int errors,
  215. struct vscsibk_pend *pending_req)
  216. {
  217. struct scsiback_tpg *tpg = pending_req->v2p->tpg;
  218. pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
  219. tpg->tport->tport_name, pending_req->v2p->lun,
  220. pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
  221. host_byte(errors), driver_byte(errors));
  222. }
  223. static void scsiback_fast_flush_area(struct vscsibk_pend *req)
  224. {
  225. struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
  226. struct page *pages[VSCSI_GRANT_BATCH];
  227. unsigned int i, invcount = 0;
  228. grant_handle_t handle;
  229. int err;
  230. kfree(req->sgl);
  231. req->sgl = NULL;
  232. req->n_sg = 0;
  233. if (!req->n_grants)
  234. return;
  235. for (i = 0; i < req->n_grants; i++) {
  236. handle = req->grant_handles[i];
  237. if (handle == SCSIBACK_INVALID_HANDLE)
  238. continue;
  239. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  240. GNTMAP_host_map, handle);
  241. req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
  242. pages[invcount] = req->pages[i];
  243. put_page(pages[invcount]);
  244. invcount++;
  245. if (invcount < VSCSI_GRANT_BATCH)
  246. continue;
  247. err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
  248. BUG_ON(err);
  249. invcount = 0;
  250. }
  251. if (invcount) {
  252. err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
  253. BUG_ON(err);
  254. }
  255. put_free_pages(req->pages, req->n_grants);
  256. req->n_grants = 0;
  257. }
  258. static void scsiback_free_translation_entry(struct kref *kref)
  259. {
  260. struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
  261. struct scsiback_tpg *tpg = entry->tpg;
  262. mutex_lock(&tpg->tv_tpg_mutex);
  263. tpg->tv_tpg_fe_count--;
  264. mutex_unlock(&tpg->tv_tpg_mutex);
  265. kfree(entry);
  266. }
  267. static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
  268. uint32_t resid, struct vscsibk_pend *pending_req)
  269. {
  270. struct vscsiif_response *ring_res;
  271. struct vscsibk_info *info = pending_req->info;
  272. int notify;
  273. struct scsi_sense_hdr sshdr;
  274. unsigned long flags;
  275. unsigned len;
  276. spin_lock_irqsave(&info->ring_lock, flags);
  277. ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
  278. info->ring.rsp_prod_pvt++;
  279. ring_res->rslt = result;
  280. ring_res->rqid = pending_req->rqid;
  281. if (sense_buffer != NULL &&
  282. scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
  283. &sshdr)) {
  284. len = min_t(unsigned, 8 + sense_buffer[7],
  285. VSCSIIF_SENSE_BUFFERSIZE);
  286. memcpy(ring_res->sense_buffer, sense_buffer, len);
  287. ring_res->sense_len = len;
  288. } else {
  289. ring_res->sense_len = 0;
  290. }
  291. ring_res->residual_len = resid;
  292. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
  293. spin_unlock_irqrestore(&info->ring_lock, flags);
  294. if (notify)
  295. notify_remote_via_irq(info->irq);
  296. if (pending_req->v2p)
  297. kref_put(&pending_req->v2p->kref,
  298. scsiback_free_translation_entry);
  299. }
  300. static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
  301. {
  302. struct vscsibk_info *info = pending_req->info;
  303. unsigned char *sense_buffer;
  304. unsigned int resid;
  305. int errors;
  306. sense_buffer = pending_req->sense_buffer;
  307. resid = pending_req->se_cmd.residual_count;
  308. errors = pending_req->result;
  309. if (errors && log_print_stat)
  310. scsiback_print_status(sense_buffer, errors, pending_req);
  311. scsiback_fast_flush_area(pending_req);
  312. scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
  313. scsiback_put(info);
  314. }
  315. static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
  316. {
  317. struct se_cmd *se_cmd = &pending_req->se_cmd;
  318. struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
  319. int rc;
  320. memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
  321. memset(se_cmd, 0, sizeof(*se_cmd));
  322. scsiback_get(pending_req->info);
  323. se_cmd->tag = pending_req->rqid;
  324. rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
  325. pending_req->sense_buffer, pending_req->v2p->lun,
  326. pending_req->data_len, 0,
  327. pending_req->sc_data_direction, 0,
  328. pending_req->sgl, pending_req->n_sg,
  329. NULL, 0, NULL, 0);
  330. if (rc < 0) {
  331. transport_send_check_condition_and_sense(se_cmd,
  332. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  333. transport_generic_free_cmd(se_cmd, 0);
  334. }
  335. }
  336. static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
  337. struct page **pg, grant_handle_t *grant, int cnt)
  338. {
  339. int err, i;
  340. if (!cnt)
  341. return 0;
  342. err = gnttab_map_refs(map, NULL, pg, cnt);
  343. BUG_ON(err);
  344. for (i = 0; i < cnt; i++) {
  345. if (unlikely(map[i].status != GNTST_okay)) {
  346. pr_err("invalid buffer -- could not remap it\n");
  347. map[i].handle = SCSIBACK_INVALID_HANDLE;
  348. err = -ENOMEM;
  349. } else {
  350. get_page(pg[i]);
  351. }
  352. grant[i] = map[i].handle;
  353. }
  354. return err;
  355. }
  356. static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
  357. struct scsiif_request_segment *seg, struct page **pg,
  358. grant_handle_t *grant, int cnt, u32 flags)
  359. {
  360. int mapcount = 0, i, err = 0;
  361. struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
  362. struct vscsibk_info *info = pending_req->info;
  363. for (i = 0; i < cnt; i++) {
  364. if (get_free_page(pg + mapcount)) {
  365. put_free_pages(pg, mapcount);
  366. pr_err("no grant page\n");
  367. return -ENOMEM;
  368. }
  369. gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
  370. flags, seg[i].gref, info->domid);
  371. mapcount++;
  372. if (mapcount < VSCSI_GRANT_BATCH)
  373. continue;
  374. err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
  375. pg += mapcount;
  376. grant += mapcount;
  377. pending_req->n_grants += mapcount;
  378. if (err)
  379. return err;
  380. mapcount = 0;
  381. }
  382. err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
  383. pending_req->n_grants += mapcount;
  384. return err;
  385. }
  386. static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
  387. struct vscsibk_pend *pending_req)
  388. {
  389. u32 flags;
  390. int i, err, n_segs, i_seg = 0;
  391. struct page **pg;
  392. struct scsiif_request_segment *seg;
  393. unsigned long end_seg = 0;
  394. unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
  395. unsigned int nr_sgl = 0;
  396. struct scatterlist *sg;
  397. grant_handle_t *grant;
  398. pending_req->n_sg = 0;
  399. pending_req->n_grants = 0;
  400. pending_req->data_len = 0;
  401. nr_segments &= ~VSCSIIF_SG_GRANT;
  402. if (!nr_segments)
  403. return 0;
  404. if (nr_segments > VSCSIIF_SG_TABLESIZE) {
  405. pr_debug("invalid parameter nr_seg = %d\n",
  406. ring_req->nr_segments);
  407. return -EINVAL;
  408. }
  409. if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
  410. err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
  411. pending_req->pages, pending_req->grant_handles,
  412. nr_segments, GNTMAP_host_map | GNTMAP_readonly);
  413. if (err)
  414. return err;
  415. nr_sgl = nr_segments;
  416. nr_segments = 0;
  417. for (i = 0; i < nr_sgl; i++) {
  418. n_segs = ring_req->seg[i].length /
  419. sizeof(struct scsiif_request_segment);
  420. if ((unsigned)ring_req->seg[i].offset +
  421. (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
  422. n_segs * sizeof(struct scsiif_request_segment) !=
  423. ring_req->seg[i].length)
  424. return -EINVAL;
  425. nr_segments += n_segs;
  426. }
  427. if (nr_segments > SG_ALL) {
  428. pr_debug("invalid nr_seg = %d\n", nr_segments);
  429. return -EINVAL;
  430. }
  431. }
  432. /* free of (sgl) in fast_flush_area() */
  433. pending_req->sgl = kmalloc_array(nr_segments,
  434. sizeof(struct scatterlist), GFP_KERNEL);
  435. if (!pending_req->sgl)
  436. return -ENOMEM;
  437. sg_init_table(pending_req->sgl, nr_segments);
  438. pending_req->n_sg = nr_segments;
  439. flags = GNTMAP_host_map;
  440. if (pending_req->sc_data_direction == DMA_TO_DEVICE)
  441. flags |= GNTMAP_readonly;
  442. pg = pending_req->pages + nr_sgl;
  443. grant = pending_req->grant_handles + nr_sgl;
  444. if (!nr_sgl) {
  445. seg = ring_req->seg;
  446. err = scsiback_gnttab_data_map_list(pending_req, seg,
  447. pg, grant, nr_segments, flags);
  448. if (err)
  449. return err;
  450. } else {
  451. for (i = 0; i < nr_sgl; i++) {
  452. seg = (struct scsiif_request_segment *)(
  453. vaddr(pending_req, i) + ring_req->seg[i].offset);
  454. n_segs = ring_req->seg[i].length /
  455. sizeof(struct scsiif_request_segment);
  456. err = scsiback_gnttab_data_map_list(pending_req, seg,
  457. pg, grant, n_segs, flags);
  458. if (err)
  459. return err;
  460. pg += n_segs;
  461. grant += n_segs;
  462. }
  463. end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
  464. seg = (struct scsiif_request_segment *)end_seg;
  465. end_seg += ring_req->seg[0].length;
  466. pg = pending_req->pages + nr_sgl;
  467. }
  468. for_each_sg(pending_req->sgl, sg, nr_segments, i) {
  469. sg_set_page(sg, pg[i], seg->length, seg->offset);
  470. pending_req->data_len += seg->length;
  471. seg++;
  472. if (nr_sgl && (unsigned long)seg >= end_seg) {
  473. i_seg++;
  474. end_seg = vaddr(pending_req, i_seg) +
  475. ring_req->seg[i_seg].offset;
  476. seg = (struct scsiif_request_segment *)end_seg;
  477. end_seg += ring_req->seg[i_seg].length;
  478. }
  479. if (sg->offset >= PAGE_SIZE ||
  480. sg->length > PAGE_SIZE ||
  481. sg->offset + sg->length > PAGE_SIZE)
  482. return -EINVAL;
  483. }
  484. return 0;
  485. }
  486. static void scsiback_disconnect(struct vscsibk_info *info)
  487. {
  488. wait_event(info->waiting_to_free,
  489. atomic_read(&info->nr_unreplied_reqs) == 0);
  490. unbind_from_irqhandler(info->irq, info);
  491. info->irq = 0;
  492. xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
  493. }
  494. static void scsiback_device_action(struct vscsibk_pend *pending_req,
  495. enum tcm_tmreq_table act, int tag)
  496. {
  497. int rc, err = FAILED;
  498. struct scsiback_tpg *tpg = pending_req->v2p->tpg;
  499. struct se_cmd *se_cmd = &pending_req->se_cmd;
  500. struct scsiback_tmr *tmr;
  501. tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
  502. if (!tmr)
  503. goto out;
  504. init_waitqueue_head(&tmr->tmr_wait);
  505. transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
  506. tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG,
  507. &pending_req->sense_buffer[0]);
  508. rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
  509. if (rc < 0)
  510. goto out;
  511. se_cmd->se_tmr_req->ref_task_tag = tag;
  512. if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
  513. goto out;
  514. transport_generic_handle_tmr(se_cmd);
  515. wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
  516. err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
  517. SUCCESS : FAILED;
  518. out:
  519. if (tmr) {
  520. transport_generic_free_cmd(&pending_req->se_cmd, 1);
  521. kfree(tmr);
  522. }
  523. scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
  524. kmem_cache_free(scsiback_cachep, pending_req);
  525. }
  526. /*
  527. Perform virtual to physical translation
  528. */
  529. static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
  530. struct ids_tuple *v)
  531. {
  532. struct v2p_entry *entry;
  533. struct list_head *head = &(info->v2p_entry_lists);
  534. unsigned long flags;
  535. spin_lock_irqsave(&info->v2p_lock, flags);
  536. list_for_each_entry(entry, head, l) {
  537. if ((entry->v.chn == v->chn) &&
  538. (entry->v.tgt == v->tgt) &&
  539. (entry->v.lun == v->lun)) {
  540. kref_get(&entry->kref);
  541. goto out;
  542. }
  543. }
  544. entry = NULL;
  545. out:
  546. spin_unlock_irqrestore(&info->v2p_lock, flags);
  547. return entry;
  548. }
  549. static int prepare_pending_reqs(struct vscsibk_info *info,
  550. struct vscsiif_request *ring_req,
  551. struct vscsibk_pend *pending_req)
  552. {
  553. struct v2p_entry *v2p;
  554. struct ids_tuple vir;
  555. pending_req->rqid = ring_req->rqid;
  556. pending_req->info = info;
  557. vir.chn = ring_req->channel;
  558. vir.tgt = ring_req->id;
  559. vir.lun = ring_req->lun;
  560. v2p = scsiback_do_translation(info, &vir);
  561. if (!v2p) {
  562. pending_req->v2p = NULL;
  563. pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
  564. vir.chn, vir.tgt, vir.lun);
  565. return -ENODEV;
  566. }
  567. pending_req->v2p = v2p;
  568. /* request range check from frontend */
  569. pending_req->sc_data_direction = ring_req->sc_data_direction;
  570. if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
  571. (pending_req->sc_data_direction != DMA_TO_DEVICE) &&
  572. (pending_req->sc_data_direction != DMA_FROM_DEVICE) &&
  573. (pending_req->sc_data_direction != DMA_NONE)) {
  574. pr_debug("invalid parameter data_dir = %d\n",
  575. pending_req->sc_data_direction);
  576. return -EINVAL;
  577. }
  578. pending_req->cmd_len = ring_req->cmd_len;
  579. if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
  580. pr_debug("invalid parameter cmd_len = %d\n",
  581. pending_req->cmd_len);
  582. return -EINVAL;
  583. }
  584. memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
  585. return 0;
  586. }
  587. static int scsiback_do_cmd_fn(struct vscsibk_info *info)
  588. {
  589. struct vscsiif_back_ring *ring = &info->ring;
  590. struct vscsiif_request ring_req;
  591. struct vscsibk_pend *pending_req;
  592. RING_IDX rc, rp;
  593. int err, more_to_do;
  594. uint32_t result;
  595. rc = ring->req_cons;
  596. rp = ring->sring->req_prod;
  597. rmb(); /* guest system is accessing ring, too */
  598. if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
  599. rc = ring->rsp_prod_pvt;
  600. pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
  601. info->domid, rp, rc, rp - rc);
  602. info->ring_error = 1;
  603. return 0;
  604. }
  605. while ((rc != rp)) {
  606. if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
  607. break;
  608. pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
  609. if (!pending_req)
  610. return 1;
  611. RING_COPY_REQUEST(ring, rc, &ring_req);
  612. ring->req_cons = ++rc;
  613. err = prepare_pending_reqs(info, &ring_req, pending_req);
  614. if (err) {
  615. switch (err) {
  616. case -ENODEV:
  617. result = DID_NO_CONNECT;
  618. break;
  619. default:
  620. result = DRIVER_ERROR;
  621. break;
  622. }
  623. scsiback_do_resp_with_sense(NULL, result << 24, 0,
  624. pending_req);
  625. kmem_cache_free(scsiback_cachep, pending_req);
  626. return 1;
  627. }
  628. switch (ring_req.act) {
  629. case VSCSIIF_ACT_SCSI_CDB:
  630. if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
  631. scsiback_fast_flush_area(pending_req);
  632. scsiback_do_resp_with_sense(NULL,
  633. DRIVER_ERROR << 24, 0, pending_req);
  634. kmem_cache_free(scsiback_cachep, pending_req);
  635. } else {
  636. scsiback_cmd_exec(pending_req);
  637. }
  638. break;
  639. case VSCSIIF_ACT_SCSI_ABORT:
  640. scsiback_device_action(pending_req, TMR_ABORT_TASK,
  641. ring_req.ref_rqid);
  642. break;
  643. case VSCSIIF_ACT_SCSI_RESET:
  644. scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
  645. break;
  646. default:
  647. pr_err_ratelimited("invalid request\n");
  648. scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
  649. 0, pending_req);
  650. kmem_cache_free(scsiback_cachep, pending_req);
  651. break;
  652. }
  653. /* Yield point for this unbounded loop. */
  654. cond_resched();
  655. }
  656. RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
  657. return more_to_do;
  658. }
  659. static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
  660. {
  661. struct vscsibk_info *info = dev_id;
  662. if (info->ring_error)
  663. return IRQ_HANDLED;
  664. while (scsiback_do_cmd_fn(info))
  665. cond_resched();
  666. return IRQ_HANDLED;
  667. }
  668. static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
  669. evtchn_port_t evtchn)
  670. {
  671. void *area;
  672. struct vscsiif_sring *sring;
  673. int err;
  674. if (info->irq)
  675. return -1;
  676. err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
  677. if (err)
  678. return err;
  679. sring = (struct vscsiif_sring *)area;
  680. BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
  681. err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
  682. if (err < 0)
  683. goto unmap_page;
  684. info->irq = err;
  685. err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
  686. IRQF_ONESHOT, "vscsiif-backend", info);
  687. if (err)
  688. goto free_irq;
  689. return 0;
  690. free_irq:
  691. unbind_from_irqhandler(info->irq, info);
  692. info->irq = 0;
  693. unmap_page:
  694. xenbus_unmap_ring_vfree(info->dev, area);
  695. return err;
  696. }
  697. static int scsiback_map(struct vscsibk_info *info)
  698. {
  699. struct xenbus_device *dev = info->dev;
  700. unsigned int ring_ref, evtchn;
  701. int err;
  702. err = xenbus_gather(XBT_NIL, dev->otherend,
  703. "ring-ref", "%u", &ring_ref,
  704. "event-channel", "%u", &evtchn, NULL);
  705. if (err) {
  706. xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
  707. return err;
  708. }
  709. return scsiback_init_sring(info, ring_ref, evtchn);
  710. }
  711. /*
  712. Add a new translation entry
  713. */
  714. static int scsiback_add_translation_entry(struct vscsibk_info *info,
  715. char *phy, struct ids_tuple *v)
  716. {
  717. int err = 0;
  718. struct v2p_entry *entry;
  719. struct v2p_entry *new;
  720. struct list_head *head = &(info->v2p_entry_lists);
  721. unsigned long flags;
  722. char *lunp;
  723. unsigned long long unpacked_lun;
  724. struct se_lun *se_lun;
  725. struct scsiback_tpg *tpg_entry, *tpg = NULL;
  726. char *error = "doesn't exist";
  727. lunp = strrchr(phy, ':');
  728. if (!lunp) {
  729. pr_err("illegal format of physical device %s\n", phy);
  730. return -EINVAL;
  731. }
  732. *lunp = 0;
  733. lunp++;
  734. err = kstrtoull(lunp, 10, &unpacked_lun);
  735. if (err < 0) {
  736. pr_err("lun number not valid: %s\n", lunp);
  737. return err;
  738. }
  739. mutex_lock(&scsiback_mutex);
  740. list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
  741. if (!strcmp(phy, tpg_entry->tport->tport_name) ||
  742. !strcmp(phy, tpg_entry->param_alias)) {
  743. mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
  744. hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
  745. if (se_lun->unpacked_lun == unpacked_lun) {
  746. if (!tpg_entry->tpg_nexus)
  747. error = "nexus undefined";
  748. else
  749. tpg = tpg_entry;
  750. break;
  751. }
  752. }
  753. mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
  754. break;
  755. }
  756. }
  757. if (tpg) {
  758. mutex_lock(&tpg->tv_tpg_mutex);
  759. tpg->tv_tpg_fe_count++;
  760. mutex_unlock(&tpg->tv_tpg_mutex);
  761. }
  762. mutex_unlock(&scsiback_mutex);
  763. if (!tpg) {
  764. pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
  765. return -ENODEV;
  766. }
  767. new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
  768. if (new == NULL) {
  769. err = -ENOMEM;
  770. goto out_free;
  771. }
  772. spin_lock_irqsave(&info->v2p_lock, flags);
  773. /* Check double assignment to identical virtual ID */
  774. list_for_each_entry(entry, head, l) {
  775. if ((entry->v.chn == v->chn) &&
  776. (entry->v.tgt == v->tgt) &&
  777. (entry->v.lun == v->lun)) {
  778. pr_warn("Virtual ID is already used. Assignment was not performed.\n");
  779. err = -EEXIST;
  780. goto out;
  781. }
  782. }
  783. /* Create a new translation entry and add to the list */
  784. kref_init(&new->kref);
  785. new->v = *v;
  786. new->tpg = tpg;
  787. new->lun = unpacked_lun;
  788. list_add_tail(&new->l, head);
  789. out:
  790. spin_unlock_irqrestore(&info->v2p_lock, flags);
  791. out_free:
  792. if (err) {
  793. mutex_lock(&tpg->tv_tpg_mutex);
  794. tpg->tv_tpg_fe_count--;
  795. mutex_unlock(&tpg->tv_tpg_mutex);
  796. kfree(new);
  797. }
  798. return err;
  799. }
  800. static void __scsiback_del_translation_entry(struct v2p_entry *entry)
  801. {
  802. list_del(&entry->l);
  803. kref_put(&entry->kref, scsiback_free_translation_entry);
  804. }
  805. /*
  806. Delete the translation entry specfied
  807. */
  808. static int scsiback_del_translation_entry(struct vscsibk_info *info,
  809. struct ids_tuple *v)
  810. {
  811. struct v2p_entry *entry;
  812. struct list_head *head = &(info->v2p_entry_lists);
  813. unsigned long flags;
  814. spin_lock_irqsave(&info->v2p_lock, flags);
  815. /* Find out the translation entry specified */
  816. list_for_each_entry(entry, head, l) {
  817. if ((entry->v.chn == v->chn) &&
  818. (entry->v.tgt == v->tgt) &&
  819. (entry->v.lun == v->lun)) {
  820. goto found;
  821. }
  822. }
  823. spin_unlock_irqrestore(&info->v2p_lock, flags);
  824. return 1;
  825. found:
  826. /* Delete the translation entry specfied */
  827. __scsiback_del_translation_entry(entry);
  828. spin_unlock_irqrestore(&info->v2p_lock, flags);
  829. return 0;
  830. }
  831. static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
  832. char *phy, struct ids_tuple *vir, int try)
  833. {
  834. if (!scsiback_add_translation_entry(info, phy, vir)) {
  835. if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
  836. "%d", XenbusStateInitialised)) {
  837. pr_err("xenbus_printf error %s\n", state);
  838. scsiback_del_translation_entry(info, vir);
  839. }
  840. } else if (!try) {
  841. xenbus_printf(XBT_NIL, info->dev->nodename, state,
  842. "%d", XenbusStateClosed);
  843. }
  844. }
  845. static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
  846. struct ids_tuple *vir)
  847. {
  848. if (!scsiback_del_translation_entry(info, vir)) {
  849. if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
  850. "%d", XenbusStateClosed))
  851. pr_err("xenbus_printf error %s\n", state);
  852. }
  853. }
  854. #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
  855. #define VSCSIBACK_OP_UPDATEDEV_STATE 2
  856. static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
  857. char *ent)
  858. {
  859. int err;
  860. struct ids_tuple vir;
  861. char *val;
  862. int device_state;
  863. char phy[VSCSI_NAMELEN];
  864. char str[64];
  865. char state[64];
  866. struct xenbus_device *dev = info->dev;
  867. /* read status */
  868. snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
  869. err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
  870. if (XENBUS_EXIST_ERR(err))
  871. return;
  872. /* physical SCSI device */
  873. snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
  874. val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
  875. if (IS_ERR(val)) {
  876. xenbus_printf(XBT_NIL, dev->nodename, state,
  877. "%d", XenbusStateClosed);
  878. return;
  879. }
  880. strlcpy(phy, val, VSCSI_NAMELEN);
  881. kfree(val);
  882. /* virtual SCSI device */
  883. snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
  884. err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
  885. &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
  886. if (XENBUS_EXIST_ERR(err)) {
  887. xenbus_printf(XBT_NIL, dev->nodename, state,
  888. "%d", XenbusStateClosed);
  889. return;
  890. }
  891. switch (op) {
  892. case VSCSIBACK_OP_ADD_OR_DEL_LUN:
  893. switch (device_state) {
  894. case XenbusStateInitialising:
  895. scsiback_do_add_lun(info, state, phy, &vir, 0);
  896. break;
  897. case XenbusStateConnected:
  898. scsiback_do_add_lun(info, state, phy, &vir, 1);
  899. break;
  900. case XenbusStateClosing:
  901. scsiback_do_del_lun(info, state, &vir);
  902. break;
  903. default:
  904. break;
  905. }
  906. break;
  907. case VSCSIBACK_OP_UPDATEDEV_STATE:
  908. if (device_state == XenbusStateInitialised) {
  909. /* modify vscsi-devs/dev-x/state */
  910. if (xenbus_printf(XBT_NIL, dev->nodename, state,
  911. "%d", XenbusStateConnected)) {
  912. pr_err("xenbus_printf error %s\n", str);
  913. scsiback_del_translation_entry(info, &vir);
  914. xenbus_printf(XBT_NIL, dev->nodename, state,
  915. "%d", XenbusStateClosed);
  916. }
  917. }
  918. break;
  919. /* When it is necessary, processing is added here. */
  920. default:
  921. break;
  922. }
  923. }
  924. static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
  925. {
  926. int i;
  927. char **dir;
  928. unsigned int ndir = 0;
  929. dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
  930. &ndir);
  931. if (IS_ERR(dir))
  932. return;
  933. for (i = 0; i < ndir; i++)
  934. scsiback_do_1lun_hotplug(info, op, dir[i]);
  935. kfree(dir);
  936. }
  937. static void scsiback_frontend_changed(struct xenbus_device *dev,
  938. enum xenbus_state frontend_state)
  939. {
  940. struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
  941. switch (frontend_state) {
  942. case XenbusStateInitialising:
  943. break;
  944. case XenbusStateInitialised:
  945. if (scsiback_map(info))
  946. break;
  947. scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
  948. xenbus_switch_state(dev, XenbusStateConnected);
  949. break;
  950. case XenbusStateConnected:
  951. scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
  952. if (dev->state == XenbusStateConnected)
  953. break;
  954. xenbus_switch_state(dev, XenbusStateConnected);
  955. break;
  956. case XenbusStateClosing:
  957. if (info->irq)
  958. scsiback_disconnect(info);
  959. xenbus_switch_state(dev, XenbusStateClosing);
  960. break;
  961. case XenbusStateClosed:
  962. xenbus_switch_state(dev, XenbusStateClosed);
  963. if (xenbus_dev_is_online(dev))
  964. break;
  965. /* fall through if not online */
  966. case XenbusStateUnknown:
  967. device_unregister(&dev->dev);
  968. break;
  969. case XenbusStateReconfiguring:
  970. scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
  971. xenbus_switch_state(dev, XenbusStateReconfigured);
  972. break;
  973. default:
  974. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  975. frontend_state);
  976. break;
  977. }
  978. }
  979. /*
  980. Release the translation entry specfied
  981. */
  982. static void scsiback_release_translation_entry(struct vscsibk_info *info)
  983. {
  984. struct v2p_entry *entry, *tmp;
  985. struct list_head *head = &(info->v2p_entry_lists);
  986. unsigned long flags;
  987. spin_lock_irqsave(&info->v2p_lock, flags);
  988. list_for_each_entry_safe(entry, tmp, head, l)
  989. __scsiback_del_translation_entry(entry);
  990. spin_unlock_irqrestore(&info->v2p_lock, flags);
  991. }
  992. static int scsiback_remove(struct xenbus_device *dev)
  993. {
  994. struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
  995. if (info->irq)
  996. scsiback_disconnect(info);
  997. scsiback_release_translation_entry(info);
  998. dev_set_drvdata(&dev->dev, NULL);
  999. return 0;
  1000. }
  1001. static int scsiback_probe(struct xenbus_device *dev,
  1002. const struct xenbus_device_id *id)
  1003. {
  1004. int err;
  1005. struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
  1006. GFP_KERNEL);
  1007. pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
  1008. if (!info) {
  1009. xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
  1010. return -ENOMEM;
  1011. }
  1012. info->dev = dev;
  1013. dev_set_drvdata(&dev->dev, info);
  1014. info->domid = dev->otherend_id;
  1015. spin_lock_init(&info->ring_lock);
  1016. info->ring_error = 0;
  1017. atomic_set(&info->nr_unreplied_reqs, 0);
  1018. init_waitqueue_head(&info->waiting_to_free);
  1019. info->dev = dev;
  1020. info->irq = 0;
  1021. INIT_LIST_HEAD(&info->v2p_entry_lists);
  1022. spin_lock_init(&info->v2p_lock);
  1023. err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
  1024. SG_ALL);
  1025. if (err)
  1026. xenbus_dev_error(dev, err, "writing feature-sg-grant");
  1027. err = xenbus_switch_state(dev, XenbusStateInitWait);
  1028. if (err)
  1029. goto fail;
  1030. return 0;
  1031. fail:
  1032. pr_warn("%s failed\n", __func__);
  1033. scsiback_remove(dev);
  1034. return err;
  1035. }
  1036. static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
  1037. {
  1038. switch (tport->tport_proto_id) {
  1039. case SCSI_PROTOCOL_SAS:
  1040. return "SAS";
  1041. case SCSI_PROTOCOL_FCP:
  1042. return "FCP";
  1043. case SCSI_PROTOCOL_ISCSI:
  1044. return "iSCSI";
  1045. default:
  1046. break;
  1047. }
  1048. return "Unknown";
  1049. }
  1050. static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
  1051. {
  1052. struct scsiback_tpg *tpg = container_of(se_tpg,
  1053. struct scsiback_tpg, se_tpg);
  1054. struct scsiback_tport *tport = tpg->tport;
  1055. return &tport->tport_name[0];
  1056. }
  1057. static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
  1058. {
  1059. struct scsiback_tpg *tpg = container_of(se_tpg,
  1060. struct scsiback_tpg, se_tpg);
  1061. return tpg->tport_tpgt;
  1062. }
  1063. static struct se_wwn *
  1064. scsiback_make_tport(struct target_fabric_configfs *tf,
  1065. struct config_group *group,
  1066. const char *name)
  1067. {
  1068. struct scsiback_tport *tport;
  1069. char *ptr;
  1070. u64 wwpn = 0;
  1071. int off = 0;
  1072. tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
  1073. if (!tport)
  1074. return ERR_PTR(-ENOMEM);
  1075. tport->tport_wwpn = wwpn;
  1076. /*
  1077. * Determine the emulated Protocol Identifier and Target Port Name
  1078. * based on the incoming configfs directory name.
  1079. */
  1080. ptr = strstr(name, "naa.");
  1081. if (ptr) {
  1082. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1083. goto check_len;
  1084. }
  1085. ptr = strstr(name, "fc.");
  1086. if (ptr) {
  1087. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1088. off = 3; /* Skip over "fc." */
  1089. goto check_len;
  1090. }
  1091. ptr = strstr(name, "iqn.");
  1092. if (ptr) {
  1093. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1094. goto check_len;
  1095. }
  1096. pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
  1097. kfree(tport);
  1098. return ERR_PTR(-EINVAL);
  1099. check_len:
  1100. if (strlen(name) >= VSCSI_NAMELEN) {
  1101. pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
  1102. scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
  1103. kfree(tport);
  1104. return ERR_PTR(-EINVAL);
  1105. }
  1106. snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
  1107. pr_debug("Allocated emulated Target %s Address: %s\n",
  1108. scsiback_dump_proto_id(tport), name);
  1109. return &tport->tport_wwn;
  1110. }
  1111. static void scsiback_drop_tport(struct se_wwn *wwn)
  1112. {
  1113. struct scsiback_tport *tport = container_of(wwn,
  1114. struct scsiback_tport, tport_wwn);
  1115. pr_debug("Deallocating emulated Target %s Address: %s\n",
  1116. scsiback_dump_proto_id(tport), tport->tport_name);
  1117. kfree(tport);
  1118. }
  1119. static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
  1120. {
  1121. return 1;
  1122. }
  1123. static int scsiback_check_stop_free(struct se_cmd *se_cmd)
  1124. {
  1125. /*
  1126. * Do not release struct se_cmd's containing a valid TMR pointer.
  1127. * These will be released directly in scsiback_device_action()
  1128. * with transport_generic_free_cmd().
  1129. */
  1130. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  1131. return 0;
  1132. transport_generic_free_cmd(se_cmd, 0);
  1133. return 1;
  1134. }
  1135. static void scsiback_release_cmd(struct se_cmd *se_cmd)
  1136. {
  1137. struct vscsibk_pend *pending_req = container_of(se_cmd,
  1138. struct vscsibk_pend, se_cmd);
  1139. kmem_cache_free(scsiback_cachep, pending_req);
  1140. }
  1141. static int scsiback_shutdown_session(struct se_session *se_sess)
  1142. {
  1143. return 0;
  1144. }
  1145. static void scsiback_close_session(struct se_session *se_sess)
  1146. {
  1147. }
  1148. static u32 scsiback_sess_get_index(struct se_session *se_sess)
  1149. {
  1150. return 0;
  1151. }
  1152. static int scsiback_write_pending(struct se_cmd *se_cmd)
  1153. {
  1154. /* Go ahead and process the write immediately */
  1155. target_execute_cmd(se_cmd);
  1156. return 0;
  1157. }
  1158. static int scsiback_write_pending_status(struct se_cmd *se_cmd)
  1159. {
  1160. return 0;
  1161. }
  1162. static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
  1163. {
  1164. }
  1165. static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
  1166. {
  1167. return 0;
  1168. }
  1169. static int scsiback_queue_data_in(struct se_cmd *se_cmd)
  1170. {
  1171. struct vscsibk_pend *pending_req = container_of(se_cmd,
  1172. struct vscsibk_pend, se_cmd);
  1173. pending_req->result = SAM_STAT_GOOD;
  1174. scsiback_cmd_done(pending_req);
  1175. return 0;
  1176. }
  1177. static int scsiback_queue_status(struct se_cmd *se_cmd)
  1178. {
  1179. struct vscsibk_pend *pending_req = container_of(se_cmd,
  1180. struct vscsibk_pend, se_cmd);
  1181. if (se_cmd->sense_buffer &&
  1182. ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  1183. (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
  1184. pending_req->result = (DRIVER_SENSE << 24) |
  1185. SAM_STAT_CHECK_CONDITION;
  1186. else
  1187. pending_req->result = se_cmd->scsi_status;
  1188. scsiback_cmd_done(pending_req);
  1189. return 0;
  1190. }
  1191. static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
  1192. {
  1193. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  1194. struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
  1195. atomic_set(&tmr->tmr_complete, 1);
  1196. wake_up(&tmr->tmr_wait);
  1197. }
  1198. static void scsiback_aborted_task(struct se_cmd *se_cmd)
  1199. {
  1200. }
  1201. static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
  1202. char *page)
  1203. {
  1204. struct se_portal_group *se_tpg = param_to_tpg(item);
  1205. struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
  1206. se_tpg);
  1207. ssize_t rb;
  1208. mutex_lock(&tpg->tv_tpg_mutex);
  1209. rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
  1210. mutex_unlock(&tpg->tv_tpg_mutex);
  1211. return rb;
  1212. }
  1213. static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
  1214. const char *page, size_t count)
  1215. {
  1216. struct se_portal_group *se_tpg = param_to_tpg(item);
  1217. struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
  1218. se_tpg);
  1219. int len;
  1220. if (strlen(page) >= VSCSI_NAMELEN) {
  1221. pr_err("param alias: %s, exceeds max: %d\n", page,
  1222. VSCSI_NAMELEN);
  1223. return -EINVAL;
  1224. }
  1225. mutex_lock(&tpg->tv_tpg_mutex);
  1226. len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
  1227. if (tpg->param_alias[len - 1] == '\n')
  1228. tpg->param_alias[len - 1] = '\0';
  1229. mutex_unlock(&tpg->tv_tpg_mutex);
  1230. return count;
  1231. }
  1232. CONFIGFS_ATTR(scsiback_tpg_param_, alias);
  1233. static struct configfs_attribute *scsiback_param_attrs[] = {
  1234. &scsiback_tpg_param_attr_alias,
  1235. NULL,
  1236. };
  1237. static int scsiback_make_nexus(struct scsiback_tpg *tpg,
  1238. const char *name)
  1239. {
  1240. struct se_portal_group *se_tpg;
  1241. struct se_session *se_sess;
  1242. struct scsiback_nexus *tv_nexus;
  1243. mutex_lock(&tpg->tv_tpg_mutex);
  1244. if (tpg->tpg_nexus) {
  1245. mutex_unlock(&tpg->tv_tpg_mutex);
  1246. pr_debug("tpg->tpg_nexus already exists\n");
  1247. return -EEXIST;
  1248. }
  1249. se_tpg = &tpg->se_tpg;
  1250. tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
  1251. if (!tv_nexus) {
  1252. mutex_unlock(&tpg->tv_tpg_mutex);
  1253. return -ENOMEM;
  1254. }
  1255. /*
  1256. * Initialize the struct se_session pointer
  1257. */
  1258. tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
  1259. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1260. mutex_unlock(&tpg->tv_tpg_mutex);
  1261. kfree(tv_nexus);
  1262. return -ENOMEM;
  1263. }
  1264. se_sess = tv_nexus->tvn_se_sess;
  1265. /*
  1266. * Since we are running in 'demo mode' this call with generate a
  1267. * struct se_node_acl for the scsiback struct se_portal_group with
  1268. * the SCSI Initiator port name of the passed configfs group 'name'.
  1269. */
  1270. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1271. se_tpg, (unsigned char *)name);
  1272. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1273. mutex_unlock(&tpg->tv_tpg_mutex);
  1274. pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
  1275. name);
  1276. goto out;
  1277. }
  1278. /* Now register the TCM pvscsi virtual I_T Nexus as active. */
  1279. transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1280. tv_nexus->tvn_se_sess, tv_nexus);
  1281. tpg->tpg_nexus = tv_nexus;
  1282. mutex_unlock(&tpg->tv_tpg_mutex);
  1283. return 0;
  1284. out:
  1285. transport_free_session(se_sess);
  1286. kfree(tv_nexus);
  1287. return -ENOMEM;
  1288. }
  1289. static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
  1290. {
  1291. struct se_session *se_sess;
  1292. struct scsiback_nexus *tv_nexus;
  1293. mutex_lock(&tpg->tv_tpg_mutex);
  1294. tv_nexus = tpg->tpg_nexus;
  1295. if (!tv_nexus) {
  1296. mutex_unlock(&tpg->tv_tpg_mutex);
  1297. return -ENODEV;
  1298. }
  1299. se_sess = tv_nexus->tvn_se_sess;
  1300. if (!se_sess) {
  1301. mutex_unlock(&tpg->tv_tpg_mutex);
  1302. return -ENODEV;
  1303. }
  1304. if (tpg->tv_tpg_port_count != 0) {
  1305. mutex_unlock(&tpg->tv_tpg_mutex);
  1306. pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
  1307. tpg->tv_tpg_port_count);
  1308. return -EBUSY;
  1309. }
  1310. if (tpg->tv_tpg_fe_count != 0) {
  1311. mutex_unlock(&tpg->tv_tpg_mutex);
  1312. pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
  1313. tpg->tv_tpg_fe_count);
  1314. return -EBUSY;
  1315. }
  1316. pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
  1317. scsiback_dump_proto_id(tpg->tport),
  1318. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1319. /*
  1320. * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
  1321. */
  1322. transport_deregister_session(tv_nexus->tvn_se_sess);
  1323. tpg->tpg_nexus = NULL;
  1324. mutex_unlock(&tpg->tv_tpg_mutex);
  1325. kfree(tv_nexus);
  1326. return 0;
  1327. }
  1328. static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
  1329. {
  1330. struct se_portal_group *se_tpg = to_tpg(item);
  1331. struct scsiback_tpg *tpg = container_of(se_tpg,
  1332. struct scsiback_tpg, se_tpg);
  1333. struct scsiback_nexus *tv_nexus;
  1334. ssize_t ret;
  1335. mutex_lock(&tpg->tv_tpg_mutex);
  1336. tv_nexus = tpg->tpg_nexus;
  1337. if (!tv_nexus) {
  1338. mutex_unlock(&tpg->tv_tpg_mutex);
  1339. return -ENODEV;
  1340. }
  1341. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1342. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1343. mutex_unlock(&tpg->tv_tpg_mutex);
  1344. return ret;
  1345. }
  1346. static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
  1347. const char *page, size_t count)
  1348. {
  1349. struct se_portal_group *se_tpg = to_tpg(item);
  1350. struct scsiback_tpg *tpg = container_of(se_tpg,
  1351. struct scsiback_tpg, se_tpg);
  1352. struct scsiback_tport *tport_wwn = tpg->tport;
  1353. unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
  1354. int ret;
  1355. /*
  1356. * Shutdown the active I_T nexus if 'NULL' is passed.
  1357. */
  1358. if (!strncmp(page, "NULL", 4)) {
  1359. ret = scsiback_drop_nexus(tpg);
  1360. return (!ret) ? count : ret;
  1361. }
  1362. /*
  1363. * Otherwise make sure the passed virtual Initiator port WWN matches
  1364. * the fabric protocol_id set in scsiback_make_tport(), and call
  1365. * scsiback_make_nexus().
  1366. */
  1367. if (strlen(page) >= VSCSI_NAMELEN) {
  1368. pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
  1369. page, VSCSI_NAMELEN);
  1370. return -EINVAL;
  1371. }
  1372. snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
  1373. ptr = strstr(i_port, "naa.");
  1374. if (ptr) {
  1375. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1376. pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
  1377. i_port, scsiback_dump_proto_id(tport_wwn));
  1378. return -EINVAL;
  1379. }
  1380. port_ptr = &i_port[0];
  1381. goto check_newline;
  1382. }
  1383. ptr = strstr(i_port, "fc.");
  1384. if (ptr) {
  1385. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1386. pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
  1387. i_port, scsiback_dump_proto_id(tport_wwn));
  1388. return -EINVAL;
  1389. }
  1390. port_ptr = &i_port[3]; /* Skip over "fc." */
  1391. goto check_newline;
  1392. }
  1393. ptr = strstr(i_port, "iqn.");
  1394. if (ptr) {
  1395. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1396. pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
  1397. i_port, scsiback_dump_proto_id(tport_wwn));
  1398. return -EINVAL;
  1399. }
  1400. port_ptr = &i_port[0];
  1401. goto check_newline;
  1402. }
  1403. pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
  1404. i_port);
  1405. return -EINVAL;
  1406. /*
  1407. * Clear any trailing newline for the NAA WWN
  1408. */
  1409. check_newline:
  1410. if (i_port[strlen(i_port) - 1] == '\n')
  1411. i_port[strlen(i_port) - 1] = '\0';
  1412. ret = scsiback_make_nexus(tpg, port_ptr);
  1413. if (ret < 0)
  1414. return ret;
  1415. return count;
  1416. }
  1417. CONFIGFS_ATTR(scsiback_tpg_, nexus);
  1418. static struct configfs_attribute *scsiback_tpg_attrs[] = {
  1419. &scsiback_tpg_attr_nexus,
  1420. NULL,
  1421. };
  1422. static ssize_t
  1423. scsiback_wwn_version_show(struct config_item *item, char *page)
  1424. {
  1425. return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
  1426. UTS_RELEASE"\n",
  1427. VSCSI_VERSION, utsname()->sysname, utsname()->machine);
  1428. }
  1429. CONFIGFS_ATTR_RO(scsiback_wwn_, version);
  1430. static struct configfs_attribute *scsiback_wwn_attrs[] = {
  1431. &scsiback_wwn_attr_version,
  1432. NULL,
  1433. };
  1434. static char *scsiback_get_fabric_name(void)
  1435. {
  1436. return "xen-pvscsi";
  1437. }
  1438. static int scsiback_port_link(struct se_portal_group *se_tpg,
  1439. struct se_lun *lun)
  1440. {
  1441. struct scsiback_tpg *tpg = container_of(se_tpg,
  1442. struct scsiback_tpg, se_tpg);
  1443. mutex_lock(&tpg->tv_tpg_mutex);
  1444. tpg->tv_tpg_port_count++;
  1445. mutex_unlock(&tpg->tv_tpg_mutex);
  1446. return 0;
  1447. }
  1448. static void scsiback_port_unlink(struct se_portal_group *se_tpg,
  1449. struct se_lun *lun)
  1450. {
  1451. struct scsiback_tpg *tpg = container_of(se_tpg,
  1452. struct scsiback_tpg, se_tpg);
  1453. mutex_lock(&tpg->tv_tpg_mutex);
  1454. tpg->tv_tpg_port_count--;
  1455. mutex_unlock(&tpg->tv_tpg_mutex);
  1456. }
  1457. static struct se_portal_group *
  1458. scsiback_make_tpg(struct se_wwn *wwn,
  1459. struct config_group *group,
  1460. const char *name)
  1461. {
  1462. struct scsiback_tport *tport = container_of(wwn,
  1463. struct scsiback_tport, tport_wwn);
  1464. struct scsiback_tpg *tpg;
  1465. u16 tpgt;
  1466. int ret;
  1467. if (strstr(name, "tpgt_") != name)
  1468. return ERR_PTR(-EINVAL);
  1469. ret = kstrtou16(name + 5, 10, &tpgt);
  1470. if (ret)
  1471. return ERR_PTR(ret);
  1472. tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
  1473. if (!tpg)
  1474. return ERR_PTR(-ENOMEM);
  1475. mutex_init(&tpg->tv_tpg_mutex);
  1476. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1477. INIT_LIST_HEAD(&tpg->info_list);
  1478. tpg->tport = tport;
  1479. tpg->tport_tpgt = tpgt;
  1480. ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
  1481. if (ret < 0) {
  1482. kfree(tpg);
  1483. return NULL;
  1484. }
  1485. mutex_lock(&scsiback_mutex);
  1486. list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
  1487. mutex_unlock(&scsiback_mutex);
  1488. return &tpg->se_tpg;
  1489. }
  1490. static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
  1491. {
  1492. struct scsiback_tpg *tpg = container_of(se_tpg,
  1493. struct scsiback_tpg, se_tpg);
  1494. mutex_lock(&scsiback_mutex);
  1495. list_del(&tpg->tv_tpg_list);
  1496. mutex_unlock(&scsiback_mutex);
  1497. /*
  1498. * Release the virtual I_T Nexus for this xen-pvscsi TPG
  1499. */
  1500. scsiback_drop_nexus(tpg);
  1501. /*
  1502. * Deregister the se_tpg from TCM.
  1503. */
  1504. core_tpg_deregister(se_tpg);
  1505. kfree(tpg);
  1506. }
  1507. static int scsiback_check_true(struct se_portal_group *se_tpg)
  1508. {
  1509. return 1;
  1510. }
  1511. static int scsiback_check_false(struct se_portal_group *se_tpg)
  1512. {
  1513. return 0;
  1514. }
  1515. static const struct target_core_fabric_ops scsiback_ops = {
  1516. .module = THIS_MODULE,
  1517. .name = "xen-pvscsi",
  1518. .get_fabric_name = scsiback_get_fabric_name,
  1519. .tpg_get_wwn = scsiback_get_fabric_wwn,
  1520. .tpg_get_tag = scsiback_get_tag,
  1521. .tpg_check_demo_mode = scsiback_check_true,
  1522. .tpg_check_demo_mode_cache = scsiback_check_true,
  1523. .tpg_check_demo_mode_write_protect = scsiback_check_false,
  1524. .tpg_check_prod_mode_write_protect = scsiback_check_false,
  1525. .tpg_get_inst_index = scsiback_tpg_get_inst_index,
  1526. .check_stop_free = scsiback_check_stop_free,
  1527. .release_cmd = scsiback_release_cmd,
  1528. .shutdown_session = scsiback_shutdown_session,
  1529. .close_session = scsiback_close_session,
  1530. .sess_get_index = scsiback_sess_get_index,
  1531. .sess_get_initiator_sid = NULL,
  1532. .write_pending = scsiback_write_pending,
  1533. .write_pending_status = scsiback_write_pending_status,
  1534. .set_default_node_attributes = scsiback_set_default_node_attrs,
  1535. .get_cmd_state = scsiback_get_cmd_state,
  1536. .queue_data_in = scsiback_queue_data_in,
  1537. .queue_status = scsiback_queue_status,
  1538. .queue_tm_rsp = scsiback_queue_tm_rsp,
  1539. .aborted_task = scsiback_aborted_task,
  1540. /*
  1541. * Setup callers for generic logic in target_core_fabric_configfs.c
  1542. */
  1543. .fabric_make_wwn = scsiback_make_tport,
  1544. .fabric_drop_wwn = scsiback_drop_tport,
  1545. .fabric_make_tpg = scsiback_make_tpg,
  1546. .fabric_drop_tpg = scsiback_drop_tpg,
  1547. .fabric_post_link = scsiback_port_link,
  1548. .fabric_pre_unlink = scsiback_port_unlink,
  1549. .tfc_wwn_attrs = scsiback_wwn_attrs,
  1550. .tfc_tpg_base_attrs = scsiback_tpg_attrs,
  1551. .tfc_tpg_param_attrs = scsiback_param_attrs,
  1552. };
  1553. static const struct xenbus_device_id scsiback_ids[] = {
  1554. { "vscsi" },
  1555. { "" }
  1556. };
  1557. static struct xenbus_driver scsiback_driver = {
  1558. .ids = scsiback_ids,
  1559. .probe = scsiback_probe,
  1560. .remove = scsiback_remove,
  1561. .otherend_changed = scsiback_frontend_changed
  1562. };
  1563. static void scsiback_init_pend(void *p)
  1564. {
  1565. struct vscsibk_pend *pend = p;
  1566. int i;
  1567. memset(pend, 0, sizeof(*pend));
  1568. for (i = 0; i < VSCSI_MAX_GRANTS; i++)
  1569. pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
  1570. }
  1571. static int __init scsiback_init(void)
  1572. {
  1573. int ret;
  1574. if (!xen_domain())
  1575. return -ENODEV;
  1576. pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
  1577. VSCSI_VERSION, utsname()->sysname, utsname()->machine);
  1578. scsiback_cachep = kmem_cache_create("vscsiif_cache",
  1579. sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend);
  1580. if (!scsiback_cachep)
  1581. return -ENOMEM;
  1582. ret = xenbus_register_backend(&scsiback_driver);
  1583. if (ret)
  1584. goto out_cache_destroy;
  1585. ret = target_register_template(&scsiback_ops);
  1586. if (ret)
  1587. goto out_unregister_xenbus;
  1588. return 0;
  1589. out_unregister_xenbus:
  1590. xenbus_unregister_driver(&scsiback_driver);
  1591. out_cache_destroy:
  1592. kmem_cache_destroy(scsiback_cachep);
  1593. pr_err("%s: error %d\n", __func__, ret);
  1594. return ret;
  1595. }
  1596. static void __exit scsiback_exit(void)
  1597. {
  1598. struct page *page;
  1599. while (free_pages_num) {
  1600. if (get_free_page(&page))
  1601. BUG();
  1602. gnttab_free_pages(1, &page);
  1603. }
  1604. target_unregister_template(&scsiback_ops);
  1605. xenbus_unregister_driver(&scsiback_driver);
  1606. kmem_cache_destroy(scsiback_cachep);
  1607. }
  1608. module_init(scsiback_init);
  1609. module_exit(scsiback_exit);
  1610. MODULE_DESCRIPTION("Xen SCSI backend driver");
  1611. MODULE_LICENSE("Dual BSD/GPL");
  1612. MODULE_ALIAS("xen-backend:vscsi");
  1613. MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");