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