xen-scsiback.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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 <target/target_core_fabric_configfs.h>
  51. #include <asm/hypervisor.h>
  52. #include <xen/xen.h>
  53. #include <xen/balloon.h>
  54. #include <xen/events.h>
  55. #include <xen/xenbus.h>
  56. #include <xen/grant_table.h>
  57. #include <xen/page.h>
  58. #include <xen/interface/grant_table.h>
  59. #include <xen/interface/io/vscsiif.h>
  60. #define VSCSI_VERSION "v0.1"
  61. #define VSCSI_NAMELEN 32
  62. struct ids_tuple {
  63. unsigned int hst; /* host */
  64. unsigned int chn; /* channel */
  65. unsigned int tgt; /* target */
  66. unsigned int lun; /* LUN */
  67. };
  68. struct v2p_entry {
  69. struct ids_tuple v; /* translate from */
  70. struct scsiback_tpg *tpg; /* translate to */
  71. unsigned int lun;
  72. struct kref kref;
  73. struct list_head l;
  74. };
  75. struct vscsibk_info {
  76. struct xenbus_device *dev;
  77. domid_t domid;
  78. unsigned int irq;
  79. struct vscsiif_back_ring ring;
  80. int ring_error;
  81. spinlock_t ring_lock;
  82. atomic_t nr_unreplied_reqs;
  83. spinlock_t v2p_lock;
  84. struct list_head v2p_entry_lists;
  85. wait_queue_head_t waiting_to_free;
  86. };
  87. /* theoretical maximum of grants for one request */
  88. #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
  89. /*
  90. * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
  91. * call to map/unmap grants. Don't choose it too large, as there are arrays
  92. * with VSCSI_GRANT_BATCH elements allocated on the stack.
  93. */
  94. #define VSCSI_GRANT_BATCH 16
  95. struct vscsibk_pend {
  96. uint16_t rqid;
  97. uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
  98. uint8_t cmd_len;
  99. uint8_t sc_data_direction;
  100. uint16_t n_sg; /* real length of SG list */
  101. uint16_t n_grants; /* SG pages and potentially SG list */
  102. uint32_t data_len;
  103. uint32_t result;
  104. struct vscsibk_info *info;
  105. struct v2p_entry *v2p;
  106. struct scatterlist *sgl;
  107. uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
  108. grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
  109. struct page *pages[VSCSI_MAX_GRANTS];
  110. struct se_cmd se_cmd;
  111. };
  112. struct scsiback_tmr {
  113. atomic_t tmr_complete;
  114. wait_queue_head_t tmr_wait;
  115. };
  116. struct scsiback_nexus {
  117. /* Pointer to TCM session for I_T Nexus */
  118. struct se_session *tvn_se_sess;
  119. };
  120. struct scsiback_tport {
  121. /* SCSI protocol the tport is providing */
  122. u8 tport_proto_id;
  123. /* Binary World Wide unique Port Name for pvscsi Target port */
  124. u64 tport_wwpn;
  125. /* ASCII formatted WWPN for pvscsi Target port */
  126. char tport_name[VSCSI_NAMELEN];
  127. /* Returned by scsiback_make_tport() */
  128. struct se_wwn tport_wwn;
  129. };
  130. struct scsiback_tpg {
  131. /* scsiback port target portal group tag for TCM */
  132. u16 tport_tpgt;
  133. /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
  134. int tv_tpg_port_count;
  135. /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
  136. int tv_tpg_fe_count;
  137. /* list for scsiback_list */
  138. struct list_head tv_tpg_list;
  139. /* Used to protect access for tpg_nexus */
  140. struct mutex tv_tpg_mutex;
  141. /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
  142. struct scsiback_nexus *tpg_nexus;
  143. /* Pointer back to scsiback_tport */
  144. struct scsiback_tport *tport;
  145. /* Returned by scsiback_make_tpg() */
  146. struct se_portal_group se_tpg;
  147. /* alias used in xenstore */
  148. char param_alias[VSCSI_NAMELEN];
  149. /* list of info structures related to this target portal group */
  150. struct list_head info_list;
  151. };
  152. #define SCSIBACK_INVALID_HANDLE (~0)
  153. static bool log_print_stat;
  154. module_param(log_print_stat, bool, 0644);
  155. static int scsiback_max_buffer_pages = 1024;
  156. module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
  157. MODULE_PARM_DESC(max_buffer_pages,
  158. "Maximum number of free pages to keep in backend buffer");
  159. static struct kmem_cache *scsiback_cachep;
  160. static DEFINE_SPINLOCK(free_pages_lock);
  161. static int free_pages_num;
  162. static LIST_HEAD(scsiback_free_pages);
  163. /* Global spinlock to protect scsiback TPG list */
  164. static DEFINE_MUTEX(scsiback_mutex);
  165. static LIST_HEAD(scsiback_list);
  166. static void scsiback_get(struct vscsibk_info *info)
  167. {
  168. atomic_inc(&info->nr_unreplied_reqs);
  169. }
  170. static void scsiback_put(struct vscsibk_info *info)
  171. {
  172. if (atomic_dec_and_test(&info->nr_unreplied_reqs))
  173. wake_up(&info->waiting_to_free);
  174. }
  175. static void put_free_pages(struct page **page, int num)
  176. {
  177. unsigned long flags;
  178. int i = free_pages_num + num, n = num;
  179. if (num == 0)
  180. return;
  181. if (i > scsiback_max_buffer_pages) {
  182. n = min(num, i - scsiback_max_buffer_pages);
  183. gnttab_free_pages(n, page + num - n);
  184. n = num - n;
  185. }
  186. spin_lock_irqsave(&free_pages_lock, flags);
  187. for (i = 0; i < n; i++)
  188. list_add(&page[i]->lru, &scsiback_free_pages);
  189. free_pages_num += n;
  190. spin_unlock_irqrestore(&free_pages_lock, flags);
  191. }
  192. static int get_free_page(struct page **page)
  193. {
  194. unsigned long flags;
  195. spin_lock_irqsave(&free_pages_lock, flags);
  196. if (list_empty(&scsiback_free_pages)) {
  197. spin_unlock_irqrestore(&free_pages_lock, flags);
  198. return gnttab_alloc_pages(1, page);
  199. }
  200. page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
  201. list_del(&page[0]->lru);
  202. free_pages_num--;
  203. spin_unlock_irqrestore(&free_pages_lock, flags);
  204. return 0;
  205. }
  206. static unsigned long vaddr_page(struct page *page)
  207. {
  208. unsigned long pfn = page_to_pfn(page);
  209. return (unsigned long)pfn_to_kaddr(pfn);
  210. }
  211. static unsigned long vaddr(struct vscsibk_pend *req, int seg)
  212. {
  213. return vaddr_page(req->pages[seg]);
  214. }
  215. static void scsiback_print_status(char *sense_buffer, int errors,
  216. struct vscsibk_pend *pending_req)
  217. {
  218. struct scsiback_tpg *tpg = pending_req->v2p->tpg;
  219. pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
  220. tpg->tport->tport_name, pending_req->v2p->lun,
  221. pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
  222. host_byte(errors), driver_byte(errors));
  223. }
  224. static void scsiback_fast_flush_area(struct vscsibk_pend *req)
  225. {
  226. struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
  227. struct page *pages[VSCSI_GRANT_BATCH];
  228. unsigned int i, invcount = 0;
  229. grant_handle_t handle;
  230. int err;
  231. kfree(req->sgl);
  232. req->sgl = NULL;
  233. req->n_sg = 0;
  234. if (!req->n_grants)
  235. return;
  236. for (i = 0; i < req->n_grants; i++) {
  237. handle = req->grant_handles[i];
  238. if (handle == SCSIBACK_INVALID_HANDLE)
  239. continue;
  240. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  241. GNTMAP_host_map, handle);
  242. req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
  243. pages[invcount] = req->pages[i];
  244. put_page(pages[invcount]);
  245. invcount++;
  246. if (invcount < VSCSI_GRANT_BATCH)
  247. continue;
  248. err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
  249. BUG_ON(err);
  250. invcount = 0;
  251. }
  252. if (invcount) {
  253. err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
  254. BUG_ON(err);
  255. }
  256. put_free_pages(req->pages, req->n_grants);
  257. req->n_grants = 0;
  258. }
  259. static void scsiback_free_translation_entry(struct kref *kref)
  260. {
  261. struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
  262. struct scsiback_tpg *tpg = entry->tpg;
  263. mutex_lock(&tpg->tv_tpg_mutex);
  264. tpg->tv_tpg_fe_count--;
  265. mutex_unlock(&tpg->tv_tpg_mutex);
  266. kfree(entry);
  267. }
  268. static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
  269. uint32_t resid, struct vscsibk_pend *pending_req)
  270. {
  271. struct vscsiif_response *ring_res;
  272. struct vscsibk_info *info = pending_req->info;
  273. int notify;
  274. struct scsi_sense_hdr sshdr;
  275. unsigned long flags;
  276. unsigned len;
  277. spin_lock_irqsave(&info->ring_lock, flags);
  278. ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
  279. info->ring.rsp_prod_pvt++;
  280. ring_res->rslt = result;
  281. ring_res->rqid = pending_req->rqid;
  282. if (sense_buffer != NULL &&
  283. scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
  284. &sshdr)) {
  285. len = min_t(unsigned, 8 + sense_buffer[7],
  286. VSCSIIF_SENSE_BUFFERSIZE);
  287. memcpy(ring_res->sense_buffer, sense_buffer, len);
  288. ring_res->sense_len = len;
  289. } else {
  290. ring_res->sense_len = 0;
  291. }
  292. ring_res->residual_len = resid;
  293. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
  294. spin_unlock_irqrestore(&info->ring_lock, flags);
  295. if (notify)
  296. notify_remote_via_irq(info->irq);
  297. if (pending_req->v2p)
  298. kref_put(&pending_req->v2p->kref,
  299. scsiback_free_translation_entry);
  300. }
  301. static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
  302. {
  303. struct vscsibk_info *info = pending_req->info;
  304. unsigned char *sense_buffer;
  305. unsigned int resid;
  306. int errors;
  307. sense_buffer = pending_req->sense_buffer;
  308. resid = pending_req->se_cmd.residual_count;
  309. errors = pending_req->result;
  310. if (errors && log_print_stat)
  311. scsiback_print_status(sense_buffer, errors, pending_req);
  312. scsiback_fast_flush_area(pending_req);
  313. scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
  314. scsiback_put(info);
  315. }
  316. static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
  317. {
  318. struct se_cmd *se_cmd = &pending_req->se_cmd;
  319. struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
  320. int rc;
  321. memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
  322. memset(se_cmd, 0, sizeof(*se_cmd));
  323. scsiback_get(pending_req->info);
  324. se_cmd->tag = pending_req->rqid;
  325. rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
  326. pending_req->sense_buffer, pending_req->v2p->lun,
  327. pending_req->data_len, 0,
  328. pending_req->sc_data_direction, 0,
  329. pending_req->sgl, pending_req->n_sg,
  330. NULL, 0, NULL, 0);
  331. if (rc < 0) {
  332. transport_send_check_condition_and_sense(se_cmd,
  333. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  334. transport_generic_free_cmd(se_cmd, 0);
  335. }
  336. }
  337. static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
  338. struct page **pg, grant_handle_t *grant, int cnt)
  339. {
  340. int err, i;
  341. if (!cnt)
  342. return 0;
  343. err = gnttab_map_refs(map, NULL, pg, cnt);
  344. BUG_ON(err);
  345. for (i = 0; i < cnt; i++) {
  346. if (unlikely(map[i].status != GNTST_okay)) {
  347. pr_err("invalid buffer -- could not remap it\n");
  348. map[i].handle = SCSIBACK_INVALID_HANDLE;
  349. err = -ENOMEM;
  350. } else {
  351. get_page(pg[i]);
  352. }
  353. grant[i] = map[i].handle;
  354. }
  355. return err;
  356. }
  357. static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
  358. struct scsiif_request_segment *seg, struct page **pg,
  359. grant_handle_t *grant, int cnt, u32 flags)
  360. {
  361. int mapcount = 0, i, err = 0;
  362. struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
  363. struct vscsibk_info *info = pending_req->info;
  364. for (i = 0; i < cnt; i++) {
  365. if (get_free_page(pg + mapcount)) {
  366. put_free_pages(pg, mapcount);
  367. pr_err("no grant page\n");
  368. return -ENOMEM;
  369. }
  370. gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
  371. flags, seg[i].gref, info->domid);
  372. mapcount++;
  373. if (mapcount < VSCSI_GRANT_BATCH)
  374. continue;
  375. err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
  376. pg += mapcount;
  377. grant += mapcount;
  378. pending_req->n_grants += mapcount;
  379. if (err)
  380. return err;
  381. mapcount = 0;
  382. }
  383. err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
  384. pending_req->n_grants += mapcount;
  385. return err;
  386. }
  387. static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
  388. struct vscsibk_pend *pending_req)
  389. {
  390. u32 flags;
  391. int i, err, n_segs, i_seg = 0;
  392. struct page **pg;
  393. struct scsiif_request_segment *seg;
  394. unsigned long end_seg = 0;
  395. unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
  396. unsigned int nr_sgl = 0;
  397. struct scatterlist *sg;
  398. grant_handle_t *grant;
  399. pending_req->n_sg = 0;
  400. pending_req->n_grants = 0;
  401. pending_req->data_len = 0;
  402. nr_segments &= ~VSCSIIF_SG_GRANT;
  403. if (!nr_segments)
  404. return 0;
  405. if (nr_segments > VSCSIIF_SG_TABLESIZE) {
  406. pr_debug("invalid parameter nr_seg = %d\n",
  407. ring_req->nr_segments);
  408. return -EINVAL;
  409. }
  410. if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
  411. err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
  412. pending_req->pages, pending_req->grant_handles,
  413. nr_segments, GNTMAP_host_map | GNTMAP_readonly);
  414. if (err)
  415. return err;
  416. nr_sgl = nr_segments;
  417. nr_segments = 0;
  418. for (i = 0; i < nr_sgl; i++) {
  419. n_segs = ring_req->seg[i].length /
  420. sizeof(struct scsiif_request_segment);
  421. if ((unsigned)ring_req->seg[i].offset +
  422. (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
  423. n_segs * sizeof(struct scsiif_request_segment) !=
  424. ring_req->seg[i].length)
  425. return -EINVAL;
  426. nr_segments += n_segs;
  427. }
  428. if (nr_segments > SG_ALL) {
  429. pr_debug("invalid nr_seg = %d\n", nr_segments);
  430. return -EINVAL;
  431. }
  432. }
  433. /* free of (sgl) in fast_flush_area() */
  434. pending_req->sgl = kmalloc_array(nr_segments,
  435. sizeof(struct scatterlist), GFP_KERNEL);
  436. if (!pending_req->sgl)
  437. return -ENOMEM;
  438. sg_init_table(pending_req->sgl, nr_segments);
  439. pending_req->n_sg = nr_segments;
  440. flags = GNTMAP_host_map;
  441. if (pending_req->sc_data_direction == DMA_TO_DEVICE)
  442. flags |= GNTMAP_readonly;
  443. pg = pending_req->pages + nr_sgl;
  444. grant = pending_req->grant_handles + nr_sgl;
  445. if (!nr_sgl) {
  446. seg = ring_req->seg;
  447. err = scsiback_gnttab_data_map_list(pending_req, seg,
  448. pg, grant, nr_segments, flags);
  449. if (err)
  450. return err;
  451. } else {
  452. for (i = 0; i < nr_sgl; i++) {
  453. seg = (struct scsiif_request_segment *)(
  454. vaddr(pending_req, i) + ring_req->seg[i].offset);
  455. n_segs = ring_req->seg[i].length /
  456. sizeof(struct scsiif_request_segment);
  457. err = scsiback_gnttab_data_map_list(pending_req, seg,
  458. pg, grant, n_segs, flags);
  459. if (err)
  460. return err;
  461. pg += n_segs;
  462. grant += n_segs;
  463. }
  464. end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
  465. seg = (struct scsiif_request_segment *)end_seg;
  466. end_seg += ring_req->seg[0].length;
  467. pg = pending_req->pages + nr_sgl;
  468. }
  469. for_each_sg(pending_req->sgl, sg, nr_segments, i) {
  470. sg_set_page(sg, pg[i], seg->length, seg->offset);
  471. pending_req->data_len += seg->length;
  472. seg++;
  473. if (nr_sgl && (unsigned long)seg >= end_seg) {
  474. i_seg++;
  475. end_seg = vaddr(pending_req, i_seg) +
  476. ring_req->seg[i_seg].offset;
  477. seg = (struct scsiif_request_segment *)end_seg;
  478. end_seg += ring_req->seg[i_seg].length;
  479. }
  480. if (sg->offset >= PAGE_SIZE ||
  481. sg->length > PAGE_SIZE ||
  482. sg->offset + sg->length > PAGE_SIZE)
  483. return -EINVAL;
  484. }
  485. return 0;
  486. }
  487. static void scsiback_disconnect(struct vscsibk_info *info)
  488. {
  489. wait_event(info->waiting_to_free,
  490. atomic_read(&info->nr_unreplied_reqs) == 0);
  491. unbind_from_irqhandler(info->irq, info);
  492. info->irq = 0;
  493. xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
  494. }
  495. static void scsiback_device_action(struct vscsibk_pend *pending_req,
  496. enum tcm_tmreq_table act, int tag)
  497. {
  498. int rc, err = FAILED;
  499. struct scsiback_tpg *tpg = pending_req->v2p->tpg;
  500. struct se_cmd *se_cmd = &pending_req->se_cmd;
  501. struct scsiback_tmr *tmr;
  502. tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
  503. if (!tmr)
  504. goto out;
  505. init_waitqueue_head(&tmr->tmr_wait);
  506. transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
  507. tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG,
  508. &pending_req->sense_buffer[0]);
  509. rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
  510. if (rc < 0)
  511. goto out;
  512. se_cmd->se_tmr_req->ref_task_tag = tag;
  513. if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
  514. goto out;
  515. transport_generic_handle_tmr(se_cmd);
  516. wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
  517. err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
  518. SUCCESS : FAILED;
  519. out:
  520. if (tmr) {
  521. transport_generic_free_cmd(&pending_req->se_cmd, 1);
  522. kfree(tmr);
  523. }
  524. scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
  525. kmem_cache_free(scsiback_cachep, pending_req);
  526. }
  527. /*
  528. Perform virtual to physical translation
  529. */
  530. static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
  531. struct ids_tuple *v)
  532. {
  533. struct v2p_entry *entry;
  534. struct list_head *head = &(info->v2p_entry_lists);
  535. unsigned long flags;
  536. spin_lock_irqsave(&info->v2p_lock, flags);
  537. list_for_each_entry(entry, head, l) {
  538. if ((entry->v.chn == v->chn) &&
  539. (entry->v.tgt == v->tgt) &&
  540. (entry->v.lun == v->lun)) {
  541. kref_get(&entry->kref);
  542. goto out;
  543. }
  544. }
  545. entry = NULL;
  546. out:
  547. spin_unlock_irqrestore(&info->v2p_lock, flags);
  548. return entry;
  549. }
  550. static int prepare_pending_reqs(struct vscsibk_info *info,
  551. struct vscsiif_request *ring_req,
  552. struct vscsibk_pend *pending_req)
  553. {
  554. struct v2p_entry *v2p;
  555. struct ids_tuple vir;
  556. pending_req->rqid = ring_req->rqid;
  557. pending_req->info = info;
  558. vir.chn = ring_req->channel;
  559. vir.tgt = ring_req->id;
  560. vir.lun = ring_req->lun;
  561. v2p = scsiback_do_translation(info, &vir);
  562. if (!v2p) {
  563. pending_req->v2p = NULL;
  564. pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
  565. vir.chn, vir.tgt, vir.lun);
  566. return -ENODEV;
  567. }
  568. pending_req->v2p = v2p;
  569. /* request range check from frontend */
  570. pending_req->sc_data_direction = ring_req->sc_data_direction;
  571. if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
  572. (pending_req->sc_data_direction != DMA_TO_DEVICE) &&
  573. (pending_req->sc_data_direction != DMA_FROM_DEVICE) &&
  574. (pending_req->sc_data_direction != DMA_NONE)) {
  575. pr_debug("invalid parameter data_dir = %d\n",
  576. pending_req->sc_data_direction);
  577. return -EINVAL;
  578. }
  579. pending_req->cmd_len = ring_req->cmd_len;
  580. if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
  581. pr_debug("invalid parameter cmd_len = %d\n",
  582. pending_req->cmd_len);
  583. return -EINVAL;
  584. }
  585. memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
  586. return 0;
  587. }
  588. static int scsiback_do_cmd_fn(struct vscsibk_info *info)
  589. {
  590. struct vscsiif_back_ring *ring = &info->ring;
  591. struct vscsiif_request ring_req;
  592. struct vscsibk_pend *pending_req;
  593. RING_IDX rc, rp;
  594. int err, more_to_do;
  595. uint32_t result;
  596. rc = ring->req_cons;
  597. rp = ring->sring->req_prod;
  598. rmb(); /* guest system is accessing ring, too */
  599. if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
  600. rc = ring->rsp_prod_pvt;
  601. pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
  602. info->domid, rp, rc, rp - rc);
  603. info->ring_error = 1;
  604. return 0;
  605. }
  606. while ((rc != rp)) {
  607. if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
  608. break;
  609. pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
  610. if (!pending_req)
  611. return 1;
  612. ring_req = *RING_GET_REQUEST(ring, rc);
  613. ring->req_cons = ++rc;
  614. err = prepare_pending_reqs(info, &ring_req, pending_req);
  615. if (err) {
  616. switch (err) {
  617. case -ENODEV:
  618. result = DID_NO_CONNECT;
  619. break;
  620. default:
  621. result = DRIVER_ERROR;
  622. break;
  623. }
  624. scsiback_do_resp_with_sense(NULL, result << 24, 0,
  625. pending_req);
  626. kmem_cache_free(scsiback_cachep, pending_req);
  627. return 1;
  628. }
  629. switch (ring_req.act) {
  630. case VSCSIIF_ACT_SCSI_CDB:
  631. if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
  632. scsiback_fast_flush_area(pending_req);
  633. scsiback_do_resp_with_sense(NULL,
  634. DRIVER_ERROR << 24, 0, pending_req);
  635. kmem_cache_free(scsiback_cachep, pending_req);
  636. } else {
  637. scsiback_cmd_exec(pending_req);
  638. }
  639. break;
  640. case VSCSIIF_ACT_SCSI_ABORT:
  641. scsiback_device_action(pending_req, TMR_ABORT_TASK,
  642. ring_req.ref_rqid);
  643. break;
  644. case VSCSIIF_ACT_SCSI_RESET:
  645. scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
  646. break;
  647. default:
  648. pr_err_ratelimited("invalid request\n");
  649. scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
  650. 0, pending_req);
  651. kmem_cache_free(scsiback_cachep, pending_req);
  652. break;
  653. }
  654. /* Yield point for this unbounded loop. */
  655. cond_resched();
  656. }
  657. RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
  658. return more_to_do;
  659. }
  660. static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
  661. {
  662. struct vscsibk_info *info = dev_id;
  663. if (info->ring_error)
  664. return IRQ_HANDLED;
  665. while (scsiback_do_cmd_fn(info))
  666. cond_resched();
  667. return IRQ_HANDLED;
  668. }
  669. static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
  670. evtchn_port_t evtchn)
  671. {
  672. void *area;
  673. struct vscsiif_sring *sring;
  674. int err;
  675. if (info->irq)
  676. return -1;
  677. err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
  678. if (err)
  679. return err;
  680. sring = (struct vscsiif_sring *)area;
  681. BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
  682. err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
  683. if (err < 0)
  684. goto unmap_page;
  685. info->irq = err;
  686. err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
  687. IRQF_ONESHOT, "vscsiif-backend", info);
  688. if (err)
  689. goto free_irq;
  690. return 0;
  691. free_irq:
  692. unbind_from_irqhandler(info->irq, info);
  693. info->irq = 0;
  694. unmap_page:
  695. xenbus_unmap_ring_vfree(info->dev, area);
  696. return err;
  697. }
  698. static int scsiback_map(struct vscsibk_info *info)
  699. {
  700. struct xenbus_device *dev = info->dev;
  701. unsigned int ring_ref, evtchn;
  702. int err;
  703. err = xenbus_gather(XBT_NIL, dev->otherend,
  704. "ring-ref", "%u", &ring_ref,
  705. "event-channel", "%u", &evtchn, NULL);
  706. if (err) {
  707. xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
  708. return err;
  709. }
  710. return scsiback_init_sring(info, ring_ref, evtchn);
  711. }
  712. /*
  713. Add a new translation entry
  714. */
  715. static int scsiback_add_translation_entry(struct vscsibk_info *info,
  716. char *phy, struct ids_tuple *v)
  717. {
  718. int err = 0;
  719. struct v2p_entry *entry;
  720. struct v2p_entry *new;
  721. struct list_head *head = &(info->v2p_entry_lists);
  722. unsigned long flags;
  723. char *lunp;
  724. unsigned long long unpacked_lun;
  725. struct se_lun *se_lun;
  726. struct scsiback_tpg *tpg_entry, *tpg = NULL;
  727. char *error = "doesn't exist";
  728. lunp = strrchr(phy, ':');
  729. if (!lunp) {
  730. pr_err("illegal format of physical device %s\n", phy);
  731. return -EINVAL;
  732. }
  733. *lunp = 0;
  734. lunp++;
  735. err = kstrtoull(lunp, 10, &unpacked_lun);
  736. if (err < 0) {
  737. pr_err("lun number not valid: %s\n", lunp);
  738. return err;
  739. }
  740. mutex_lock(&scsiback_mutex);
  741. list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
  742. if (!strcmp(phy, tpg_entry->tport->tport_name) ||
  743. !strcmp(phy, tpg_entry->param_alias)) {
  744. mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
  745. hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
  746. if (se_lun->unpacked_lun == unpacked_lun) {
  747. if (!tpg_entry->tpg_nexus)
  748. error = "nexus undefined";
  749. else
  750. tpg = tpg_entry;
  751. break;
  752. }
  753. }
  754. mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
  755. break;
  756. }
  757. }
  758. if (tpg) {
  759. mutex_lock(&tpg->tv_tpg_mutex);
  760. tpg->tv_tpg_fe_count++;
  761. mutex_unlock(&tpg->tv_tpg_mutex);
  762. }
  763. mutex_unlock(&scsiback_mutex);
  764. if (!tpg) {
  765. pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
  766. return -ENODEV;
  767. }
  768. new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
  769. if (new == NULL) {
  770. err = -ENOMEM;
  771. goto out_free;
  772. }
  773. spin_lock_irqsave(&info->v2p_lock, flags);
  774. /* Check double assignment to identical virtual ID */
  775. list_for_each_entry(entry, head, l) {
  776. if ((entry->v.chn == v->chn) &&
  777. (entry->v.tgt == v->tgt) &&
  778. (entry->v.lun == v->lun)) {
  779. pr_warn("Virtual ID is already used. Assignment was not performed.\n");
  780. err = -EEXIST;
  781. goto out;
  782. }
  783. }
  784. /* Create a new translation entry and add to the list */
  785. kref_init(&new->kref);
  786. new->v = *v;
  787. new->tpg = tpg;
  788. new->lun = unpacked_lun;
  789. list_add_tail(&new->l, head);
  790. out:
  791. spin_unlock_irqrestore(&info->v2p_lock, flags);
  792. out_free:
  793. mutex_lock(&tpg->tv_tpg_mutex);
  794. tpg->tv_tpg_fe_count--;
  795. mutex_unlock(&tpg->tv_tpg_mutex);
  796. if (err)
  797. kfree(new);
  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_show_alias(struct se_portal_group *se_tpg,
  1202. char *page)
  1203. {
  1204. struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
  1205. se_tpg);
  1206. ssize_t rb;
  1207. mutex_lock(&tpg->tv_tpg_mutex);
  1208. rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
  1209. mutex_unlock(&tpg->tv_tpg_mutex);
  1210. return rb;
  1211. }
  1212. static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
  1213. const char *page, size_t count)
  1214. {
  1215. struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
  1216. se_tpg);
  1217. int len;
  1218. if (strlen(page) >= VSCSI_NAMELEN) {
  1219. pr_err("param alias: %s, exceeds max: %d\n", page,
  1220. VSCSI_NAMELEN);
  1221. return -EINVAL;
  1222. }
  1223. mutex_lock(&tpg->tv_tpg_mutex);
  1224. len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
  1225. if (tpg->param_alias[len - 1] == '\n')
  1226. tpg->param_alias[len - 1] = '\0';
  1227. mutex_unlock(&tpg->tv_tpg_mutex);
  1228. return count;
  1229. }
  1230. TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
  1231. static struct configfs_attribute *scsiback_param_attrs[] = {
  1232. &scsiback_tpg_param_alias.attr,
  1233. NULL,
  1234. };
  1235. static int scsiback_make_nexus(struct scsiback_tpg *tpg,
  1236. const char *name)
  1237. {
  1238. struct se_portal_group *se_tpg;
  1239. struct se_session *se_sess;
  1240. struct scsiback_nexus *tv_nexus;
  1241. mutex_lock(&tpg->tv_tpg_mutex);
  1242. if (tpg->tpg_nexus) {
  1243. mutex_unlock(&tpg->tv_tpg_mutex);
  1244. pr_debug("tpg->tpg_nexus already exists\n");
  1245. return -EEXIST;
  1246. }
  1247. se_tpg = &tpg->se_tpg;
  1248. tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
  1249. if (!tv_nexus) {
  1250. mutex_unlock(&tpg->tv_tpg_mutex);
  1251. return -ENOMEM;
  1252. }
  1253. /*
  1254. * Initialize the struct se_session pointer
  1255. */
  1256. tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
  1257. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1258. mutex_unlock(&tpg->tv_tpg_mutex);
  1259. kfree(tv_nexus);
  1260. return -ENOMEM;
  1261. }
  1262. se_sess = tv_nexus->tvn_se_sess;
  1263. /*
  1264. * Since we are running in 'demo mode' this call with generate a
  1265. * struct se_node_acl for the scsiback struct se_portal_group with
  1266. * the SCSI Initiator port name of the passed configfs group 'name'.
  1267. */
  1268. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1269. se_tpg, (unsigned char *)name);
  1270. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1271. mutex_unlock(&tpg->tv_tpg_mutex);
  1272. pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
  1273. name);
  1274. goto out;
  1275. }
  1276. /* Now register the TCM pvscsi virtual I_T Nexus as active. */
  1277. transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1278. tv_nexus->tvn_se_sess, tv_nexus);
  1279. tpg->tpg_nexus = tv_nexus;
  1280. mutex_unlock(&tpg->tv_tpg_mutex);
  1281. return 0;
  1282. out:
  1283. transport_free_session(se_sess);
  1284. kfree(tv_nexus);
  1285. return -ENOMEM;
  1286. }
  1287. static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
  1288. {
  1289. struct se_session *se_sess;
  1290. struct scsiback_nexus *tv_nexus;
  1291. mutex_lock(&tpg->tv_tpg_mutex);
  1292. tv_nexus = tpg->tpg_nexus;
  1293. if (!tv_nexus) {
  1294. mutex_unlock(&tpg->tv_tpg_mutex);
  1295. return -ENODEV;
  1296. }
  1297. se_sess = tv_nexus->tvn_se_sess;
  1298. if (!se_sess) {
  1299. mutex_unlock(&tpg->tv_tpg_mutex);
  1300. return -ENODEV;
  1301. }
  1302. if (tpg->tv_tpg_port_count != 0) {
  1303. mutex_unlock(&tpg->tv_tpg_mutex);
  1304. pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
  1305. tpg->tv_tpg_port_count);
  1306. return -EBUSY;
  1307. }
  1308. if (tpg->tv_tpg_fe_count != 0) {
  1309. mutex_unlock(&tpg->tv_tpg_mutex);
  1310. pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
  1311. tpg->tv_tpg_fe_count);
  1312. return -EBUSY;
  1313. }
  1314. pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
  1315. scsiback_dump_proto_id(tpg->tport),
  1316. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1317. /*
  1318. * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
  1319. */
  1320. transport_deregister_session(tv_nexus->tvn_se_sess);
  1321. tpg->tpg_nexus = NULL;
  1322. mutex_unlock(&tpg->tv_tpg_mutex);
  1323. kfree(tv_nexus);
  1324. return 0;
  1325. }
  1326. static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg,
  1327. char *page)
  1328. {
  1329. struct scsiback_tpg *tpg = container_of(se_tpg,
  1330. struct scsiback_tpg, se_tpg);
  1331. struct scsiback_nexus *tv_nexus;
  1332. ssize_t ret;
  1333. mutex_lock(&tpg->tv_tpg_mutex);
  1334. tv_nexus = tpg->tpg_nexus;
  1335. if (!tv_nexus) {
  1336. mutex_unlock(&tpg->tv_tpg_mutex);
  1337. return -ENODEV;
  1338. }
  1339. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1340. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1341. mutex_unlock(&tpg->tv_tpg_mutex);
  1342. return ret;
  1343. }
  1344. static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg,
  1345. const char *page,
  1346. size_t count)
  1347. {
  1348. struct scsiback_tpg *tpg = container_of(se_tpg,
  1349. struct scsiback_tpg, se_tpg);
  1350. struct scsiback_tport *tport_wwn = tpg->tport;
  1351. unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
  1352. int ret;
  1353. /*
  1354. * Shutdown the active I_T nexus if 'NULL' is passed.
  1355. */
  1356. if (!strncmp(page, "NULL", 4)) {
  1357. ret = scsiback_drop_nexus(tpg);
  1358. return (!ret) ? count : ret;
  1359. }
  1360. /*
  1361. * Otherwise make sure the passed virtual Initiator port WWN matches
  1362. * the fabric protocol_id set in scsiback_make_tport(), and call
  1363. * scsiback_make_nexus().
  1364. */
  1365. if (strlen(page) >= VSCSI_NAMELEN) {
  1366. pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
  1367. page, VSCSI_NAMELEN);
  1368. return -EINVAL;
  1369. }
  1370. snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
  1371. ptr = strstr(i_port, "naa.");
  1372. if (ptr) {
  1373. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1374. pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
  1375. i_port, scsiback_dump_proto_id(tport_wwn));
  1376. return -EINVAL;
  1377. }
  1378. port_ptr = &i_port[0];
  1379. goto check_newline;
  1380. }
  1381. ptr = strstr(i_port, "fc.");
  1382. if (ptr) {
  1383. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1384. pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
  1385. i_port, scsiback_dump_proto_id(tport_wwn));
  1386. return -EINVAL;
  1387. }
  1388. port_ptr = &i_port[3]; /* Skip over "fc." */
  1389. goto check_newline;
  1390. }
  1391. ptr = strstr(i_port, "iqn.");
  1392. if (ptr) {
  1393. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1394. pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
  1395. i_port, scsiback_dump_proto_id(tport_wwn));
  1396. return -EINVAL;
  1397. }
  1398. port_ptr = &i_port[0];
  1399. goto check_newline;
  1400. }
  1401. pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
  1402. i_port);
  1403. return -EINVAL;
  1404. /*
  1405. * Clear any trailing newline for the NAA WWN
  1406. */
  1407. check_newline:
  1408. if (i_port[strlen(i_port) - 1] == '\n')
  1409. i_port[strlen(i_port) - 1] = '\0';
  1410. ret = scsiback_make_nexus(tpg, port_ptr);
  1411. if (ret < 0)
  1412. return ret;
  1413. return count;
  1414. }
  1415. TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR);
  1416. static struct configfs_attribute *scsiback_tpg_attrs[] = {
  1417. &scsiback_tpg_nexus.attr,
  1418. NULL,
  1419. };
  1420. static ssize_t
  1421. scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf,
  1422. char *page)
  1423. {
  1424. return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
  1425. UTS_RELEASE"\n",
  1426. VSCSI_VERSION, utsname()->sysname, utsname()->machine);
  1427. }
  1428. TF_WWN_ATTR_RO(scsiback, version);
  1429. static struct configfs_attribute *scsiback_wwn_attrs[] = {
  1430. &scsiback_wwn_version.attr,
  1431. NULL,
  1432. };
  1433. static char *scsiback_get_fabric_name(void)
  1434. {
  1435. return "xen-pvscsi";
  1436. }
  1437. static int scsiback_port_link(struct se_portal_group *se_tpg,
  1438. struct se_lun *lun)
  1439. {
  1440. struct scsiback_tpg *tpg = container_of(se_tpg,
  1441. struct scsiback_tpg, se_tpg);
  1442. mutex_lock(&tpg->tv_tpg_mutex);
  1443. tpg->tv_tpg_port_count++;
  1444. mutex_unlock(&tpg->tv_tpg_mutex);
  1445. return 0;
  1446. }
  1447. static void scsiback_port_unlink(struct se_portal_group *se_tpg,
  1448. struct se_lun *lun)
  1449. {
  1450. struct scsiback_tpg *tpg = container_of(se_tpg,
  1451. struct scsiback_tpg, se_tpg);
  1452. mutex_lock(&tpg->tv_tpg_mutex);
  1453. tpg->tv_tpg_port_count--;
  1454. mutex_unlock(&tpg->tv_tpg_mutex);
  1455. }
  1456. static struct se_portal_group *
  1457. scsiback_make_tpg(struct se_wwn *wwn,
  1458. struct config_group *group,
  1459. const char *name)
  1460. {
  1461. struct scsiback_tport *tport = container_of(wwn,
  1462. struct scsiback_tport, tport_wwn);
  1463. struct scsiback_tpg *tpg;
  1464. u16 tpgt;
  1465. int ret;
  1466. if (strstr(name, "tpgt_") != name)
  1467. return ERR_PTR(-EINVAL);
  1468. ret = kstrtou16(name + 5, 10, &tpgt);
  1469. if (ret)
  1470. return ERR_PTR(ret);
  1471. tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
  1472. if (!tpg)
  1473. return ERR_PTR(-ENOMEM);
  1474. mutex_init(&tpg->tv_tpg_mutex);
  1475. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1476. INIT_LIST_HEAD(&tpg->info_list);
  1477. tpg->tport = tport;
  1478. tpg->tport_tpgt = tpgt;
  1479. ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
  1480. if (ret < 0) {
  1481. kfree(tpg);
  1482. return NULL;
  1483. }
  1484. mutex_lock(&scsiback_mutex);
  1485. list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
  1486. mutex_unlock(&scsiback_mutex);
  1487. return &tpg->se_tpg;
  1488. }
  1489. static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
  1490. {
  1491. struct scsiback_tpg *tpg = container_of(se_tpg,
  1492. struct scsiback_tpg, se_tpg);
  1493. mutex_lock(&scsiback_mutex);
  1494. list_del(&tpg->tv_tpg_list);
  1495. mutex_unlock(&scsiback_mutex);
  1496. /*
  1497. * Release the virtual I_T Nexus for this xen-pvscsi TPG
  1498. */
  1499. scsiback_drop_nexus(tpg);
  1500. /*
  1501. * Deregister the se_tpg from TCM.
  1502. */
  1503. core_tpg_deregister(se_tpg);
  1504. kfree(tpg);
  1505. }
  1506. static int scsiback_check_true(struct se_portal_group *se_tpg)
  1507. {
  1508. return 1;
  1509. }
  1510. static int scsiback_check_false(struct se_portal_group *se_tpg)
  1511. {
  1512. return 0;
  1513. }
  1514. static const struct target_core_fabric_ops scsiback_ops = {
  1515. .module = THIS_MODULE,
  1516. .name = "xen-pvscsi",
  1517. .get_fabric_name = scsiback_get_fabric_name,
  1518. .tpg_get_wwn = scsiback_get_fabric_wwn,
  1519. .tpg_get_tag = scsiback_get_tag,
  1520. .tpg_check_demo_mode = scsiback_check_true,
  1521. .tpg_check_demo_mode_cache = scsiback_check_true,
  1522. .tpg_check_demo_mode_write_protect = scsiback_check_false,
  1523. .tpg_check_prod_mode_write_protect = scsiback_check_false,
  1524. .tpg_get_inst_index = scsiback_tpg_get_inst_index,
  1525. .check_stop_free = scsiback_check_stop_free,
  1526. .release_cmd = scsiback_release_cmd,
  1527. .shutdown_session = scsiback_shutdown_session,
  1528. .close_session = scsiback_close_session,
  1529. .sess_get_index = scsiback_sess_get_index,
  1530. .sess_get_initiator_sid = NULL,
  1531. .write_pending = scsiback_write_pending,
  1532. .write_pending_status = scsiback_write_pending_status,
  1533. .set_default_node_attributes = scsiback_set_default_node_attrs,
  1534. .get_cmd_state = scsiback_get_cmd_state,
  1535. .queue_data_in = scsiback_queue_data_in,
  1536. .queue_status = scsiback_queue_status,
  1537. .queue_tm_rsp = scsiback_queue_tm_rsp,
  1538. .aborted_task = scsiback_aborted_task,
  1539. /*
  1540. * Setup callers for generic logic in target_core_fabric_configfs.c
  1541. */
  1542. .fabric_make_wwn = scsiback_make_tport,
  1543. .fabric_drop_wwn = scsiback_drop_tport,
  1544. .fabric_make_tpg = scsiback_make_tpg,
  1545. .fabric_drop_tpg = scsiback_drop_tpg,
  1546. .fabric_post_link = scsiback_port_link,
  1547. .fabric_pre_unlink = scsiback_port_unlink,
  1548. .tfc_wwn_attrs = scsiback_wwn_attrs,
  1549. .tfc_tpg_base_attrs = scsiback_tpg_attrs,
  1550. .tfc_tpg_param_attrs = scsiback_param_attrs,
  1551. };
  1552. static const struct xenbus_device_id scsiback_ids[] = {
  1553. { "vscsi" },
  1554. { "" }
  1555. };
  1556. static struct xenbus_driver scsiback_driver = {
  1557. .ids = scsiback_ids,
  1558. .probe = scsiback_probe,
  1559. .remove = scsiback_remove,
  1560. .otherend_changed = scsiback_frontend_changed
  1561. };
  1562. static void scsiback_init_pend(void *p)
  1563. {
  1564. struct vscsibk_pend *pend = p;
  1565. int i;
  1566. memset(pend, 0, sizeof(*pend));
  1567. for (i = 0; i < VSCSI_MAX_GRANTS; i++)
  1568. pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
  1569. }
  1570. static int __init scsiback_init(void)
  1571. {
  1572. int ret;
  1573. if (!xen_domain())
  1574. return -ENODEV;
  1575. pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
  1576. VSCSI_VERSION, utsname()->sysname, utsname()->machine);
  1577. scsiback_cachep = kmem_cache_create("vscsiif_cache",
  1578. sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend);
  1579. if (!scsiback_cachep)
  1580. return -ENOMEM;
  1581. ret = xenbus_register_backend(&scsiback_driver);
  1582. if (ret)
  1583. goto out_cache_destroy;
  1584. ret = target_register_template(&scsiback_ops);
  1585. if (ret)
  1586. goto out_unregister_xenbus;
  1587. return 0;
  1588. out_unregister_xenbus:
  1589. xenbus_unregister_driver(&scsiback_driver);
  1590. out_cache_destroy:
  1591. kmem_cache_destroy(scsiback_cachep);
  1592. pr_err("%s: error %d\n", __func__, ret);
  1593. return ret;
  1594. }
  1595. static void __exit scsiback_exit(void)
  1596. {
  1597. struct page *page;
  1598. while (free_pages_num) {
  1599. if (get_free_page(&page))
  1600. BUG();
  1601. gnttab_free_pages(1, &page);
  1602. }
  1603. target_unregister_template(&scsiback_ops);
  1604. xenbus_unregister_driver(&scsiback_driver);
  1605. kmem_cache_destroy(scsiback_cachep);
  1606. }
  1607. module_init(scsiback_init);
  1608. module_exit(scsiback_exit);
  1609. MODULE_DESCRIPTION("Xen SCSI backend driver");
  1610. MODULE_LICENSE("Dual BSD/GPL");
  1611. MODULE_ALIAS("xen-backend:vscsi");
  1612. MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");