target: remove control CDB flags
[deliverable/linux.git] / drivers / target / loopback / tcm_loop.c
CommitLineData
3703b2c5
NB
1/*******************************************************************************
2 *
3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
4 * for emulated SAS initiator ports
5 *
6 * © Copyright 2011 RisingTide Systems LLC.
7 *
8 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 *
10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28#include <linux/configfs.h>
29#include <scsi/scsi.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_cmnd.h>
3703b2c5
NB
34
35#include <target/target_core_base.h>
c4795fb2 36#include <target/target_core_fabric.h>
3703b2c5 37#include <target/target_core_fabric_configfs.h>
3703b2c5 38#include <target/target_core_configfs.h>
3703b2c5
NB
39
40#include "tcm_loop.h"
41
42#define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
43
44/* Local pointer to allocated TCM configfs fabric module */
45static struct target_fabric_configfs *tcm_loop_fabric_configfs;
46
afe2cb7f 47static struct workqueue_struct *tcm_loop_workqueue;
3703b2c5
NB
48static struct kmem_cache *tcm_loop_cmd_cache;
49
50static int tcm_loop_hba_no_cnt;
51
16786454 52static int tcm_loop_queue_status(struct se_cmd *se_cmd);
3703b2c5
NB
53
54/*
55 * Called from struct target_core_fabric_ops->check_stop_free()
56 */
88dd9e26 57static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
3703b2c5
NB
58{
59 /*
60 * Do not release struct se_cmd's containing a valid TMR
61 * pointer. These will be released directly in tcm_loop_device_reset()
62 * with transport_generic_free_cmd().
63 */
c8e31f26 64 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
88dd9e26 65 return 0;
3703b2c5
NB
66 /*
67 * Release the struct se_cmd, which will make a callback to release
68 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
69 */
82f1c8a4 70 transport_generic_free_cmd(se_cmd, 0);
88dd9e26 71 return 1;
3703b2c5
NB
72}
73
35462975 74static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
3703b2c5
NB
75{
76 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
77 struct tcm_loop_cmd, tl_se_cmd);
78
79 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
80}
81
82static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
83 char **start, off_t offset,
84 int length, int inout)
85{
86 return sprintf(buffer, "tcm_loop_proc_info()\n");
87}
88
89static int tcm_loop_driver_probe(struct device *);
90static int tcm_loop_driver_remove(struct device *);
91
92static int pseudo_lld_bus_match(struct device *dev,
93 struct device_driver *dev_driver)
94{
95 return 1;
96}
97
98static struct bus_type tcm_loop_lld_bus = {
99 .name = "tcm_loop_bus",
100 .match = pseudo_lld_bus_match,
101 .probe = tcm_loop_driver_probe,
102 .remove = tcm_loop_driver_remove,
103};
104
105static struct device_driver tcm_loop_driverfs = {
106 .name = "tcm_loop",
107 .bus = &tcm_loop_lld_bus,
108};
109/*
110 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
111 */
112struct device *tcm_loop_primary;
113
114/*
115 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
116 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
117 */
118static int tcm_loop_change_queue_depth(
119 struct scsi_device *sdev,
120 int depth,
121 int reason)
122{
123 switch (reason) {
124 case SCSI_QDEPTH_DEFAULT:
125 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
126 break;
127 case SCSI_QDEPTH_QFULL:
128 scsi_track_queue_full(sdev, depth);
129 break;
130 case SCSI_QDEPTH_RAMP_UP:
131 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
132 break;
133 default:
134 return -EOPNOTSUPP;
135 }
136 return sdev->queue_depth;
137}
138
f872c9f4
CH
139/*
140 * Locate the SAM Task Attr from struct scsi_cmnd *
141 */
142static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
143{
144 if (sc->device->tagged_supported) {
145 switch (sc->tag) {
146 case HEAD_OF_QUEUE_TAG:
147 return MSG_HEAD_TAG;
148 case ORDERED_QUEUE_TAG:
149 return MSG_ORDERED_TAG;
150 default:
151 break;
152 }
153 }
154
155 return MSG_SIMPLE_TAG;
156}
157
afe2cb7f 158static void tcm_loop_submission_work(struct work_struct *work)
3703b2c5 159{
afe2cb7f
CH
160 struct tcm_loop_cmd *tl_cmd =
161 container_of(work, struct tcm_loop_cmd, work);
162 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
163 struct scsi_cmnd *sc = tl_cmd->sc;
164 struct tcm_loop_nexus *tl_nexus;
3703b2c5
NB
165 struct tcm_loop_hba *tl_hba;
166 struct tcm_loop_tpg *tl_tpg;
16786454
CH
167 struct scatterlist *sgl_bidi = NULL;
168 u32 sgl_bidi_count = 0;
169 int ret;
3703b2c5 170
3703b2c5
NB
171 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
172 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
afe2cb7f 173
0a020436
NB
174 /*
175 * Ensure that this tl_tpg reference from the incoming sc->device->id
176 * has already been configured via tcm_loop_make_naa_tpg().
177 */
178 if (!tl_tpg->tl_hba) {
179 set_host_byte(sc, DID_NO_CONNECT);
f872c9f4 180 goto out_done;
0a020436 181 }
f872c9f4
CH
182
183 tl_nexus = tl_hba->tl_nexus;
184 if (!tl_nexus) {
185 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
186 " does not exist\n");
187 set_host_byte(sc, DID_ERROR);
188 goto out_done;
3703b2c5 189 }
f872c9f4 190
afe2cb7f
CH
191 transport_init_se_cmd(se_cmd, tl_tpg->tl_se_tpg.se_tpg_tfo,
192 tl_nexus->se_sess,
f872c9f4
CH
193 scsi_bufflen(sc), sc->sc_data_direction,
194 tcm_loop_sam_attr(sc), &tl_cmd->tl_sense_buf[0]);
195
16786454
CH
196 if (scsi_bidi_cmnd(sc)) {
197 struct scsi_data_buffer *sdb = scsi_in(sc);
198
199 sgl_bidi = sdb->table.sgl;
200 sgl_bidi_count = sdb->table.nents;
f872c9f4
CH
201 se_cmd->se_cmd_flags |= SCF_BIDI;
202
16786454
CH
203 }
204
f872c9f4
CH
205 if (transport_lookup_cmd_lun(se_cmd, tl_cmd->sc->device->lun) < 0) {
206 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
207 set_host_byte(sc, DID_NO_CONNECT);
208 goto out_done;
209 }
210
16786454
CH
211 /*
212 * Because some userspace code via scsi-generic do not memset their
213 * associated read buffers, go ahead and do that here for type
64f1db38
CH
214 * non-data CDBs. Also note that this is currently guaranteed to be a
215 * single SGL for this case by target core in
216 * target_setup_cmd_from_cdb() -> transport_generic_cmd_sequencer().
16786454 217 */
64f1db38 218 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
16786454
CH
219 se_cmd->data_direction == DMA_FROM_DEVICE) {
220 struct scatterlist *sg = scsi_sglist(sc);
221 unsigned char *buf = kmap(sg_page(sg)) + sg->offset;
222
223 if (buf != NULL) {
224 memset(buf, 0, sg->length);
225 kunmap(sg_page(sg));
226 }
227 }
228
a12f41f8 229 ret = target_setup_cmd_from_cdb(se_cmd, sc->cmnd);
16786454
CH
230 if (ret == -ENOMEM) {
231 transport_send_check_condition_and_sense(se_cmd,
232 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
233 transport_generic_free_cmd(se_cmd, 0);
234 return;
ee9b866a 235 } else if (ret < 0) {
16786454
CH
236 if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
237 tcm_loop_queue_status(se_cmd);
238 else
239 transport_send_check_condition_and_sense(se_cmd,
240 se_cmd->scsi_sense_reason, 0);
241 transport_generic_free_cmd(se_cmd, 0);
242 return;
243 }
244
245 ret = transport_generic_map_mem_to_cmd(se_cmd, scsi_sglist(sc),
246 scsi_sg_count(sc), sgl_bidi, sgl_bidi_count);
247 if (ret) {
248 transport_send_check_condition_and_sense(se_cmd,
249 se_cmd->scsi_sense_reason, 0);
250 transport_generic_free_cmd(se_cmd, 0);
251 return;
252 }
253 transport_handle_cdb_direct(se_cmd);
afe2cb7f 254 return;
f872c9f4
CH
255
256out_done:
257 sc->scsi_done(sc);
afe2cb7f
CH
258 return;
259}
260
261/*
262 * ->queuecommand can be and usually is called from interrupt context, so
263 * defer the actual submission to a workqueue.
264 */
265static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
266{
267 struct tcm_loop_cmd *tl_cmd;
268
269 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
270 " scsi_buf_len: %u\n", sc->device->host->host_no,
271 sc->device->id, sc->device->channel, sc->device->lun,
272 sc->cmnd[0], scsi_bufflen(sc));
273
274 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
275 if (!tl_cmd) {
276 pr_err("Unable to allocate struct tcm_loop_cmd\n");
277 set_host_byte(sc, DID_ERROR);
278 sc->scsi_done(sc);
279 return 0;
280 }
281
282 tl_cmd->sc = sc;
283 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
284 queue_work(tcm_loop_workqueue, &tl_cmd->work);
f872c9f4 285 return 0;
3703b2c5
NB
286}
287
288/*
289 * Called from SCSI EH process context to issue a LUN_RESET TMR
290 * to struct scsi_device
291 */
292static int tcm_loop_device_reset(struct scsi_cmnd *sc)
293{
294 struct se_cmd *se_cmd = NULL;
295 struct se_portal_group *se_tpg;
296 struct se_session *se_sess;
297 struct tcm_loop_cmd *tl_cmd = NULL;
298 struct tcm_loop_hba *tl_hba;
299 struct tcm_loop_nexus *tl_nexus;
300 struct tcm_loop_tmr *tl_tmr = NULL;
301 struct tcm_loop_tpg *tl_tpg;
c8e31f26 302 int ret = FAILED, rc;
3703b2c5
NB
303 /*
304 * Locate the tcm_loop_hba_t pointer
305 */
306 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
307 /*
308 * Locate the tl_nexus and se_sess pointers
309 */
310 tl_nexus = tl_hba->tl_nexus;
311 if (!tl_nexus) {
6708bb27 312 pr_err("Unable to perform device reset without"
3703b2c5
NB
313 " active I_T Nexus\n");
314 return FAILED;
315 }
316 se_sess = tl_nexus->se_sess;
317 /*
318 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
319 */
320 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
321 se_tpg = &tl_tpg->tl_se_tpg;
322
323 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
324 if (!tl_cmd) {
6708bb27 325 pr_err("Unable to allocate memory for tl_cmd\n");
3703b2c5
NB
326 return FAILED;
327 }
328
329 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
330 if (!tl_tmr) {
6708bb27 331 pr_err("Unable to allocate memory for tl_tmr\n");
3703b2c5
NB
332 goto release;
333 }
334 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
335
336 se_cmd = &tl_cmd->tl_se_cmd;
337 /*
338 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
339 */
340 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
61db1802 341 DMA_NONE, MSG_SIMPLE_TAG,
3703b2c5 342 &tl_cmd->tl_sense_buf[0]);
c8e31f26
AG
343
344 rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
345 if (rc < 0)
3703b2c5
NB
346 goto release;
347 /*
348 * Locate the underlying TCM struct se_lun from sc->device->lun
349 */
5951146d 350 if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
3703b2c5
NB
351 goto release;
352 /*
353 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
354 * to wake us up.
355 */
356 transport_generic_handle_tmr(se_cmd);
357 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
358 /*
359 * The TMR LUN_RESET has completed, check the response status and
360 * then release allocations.
361 */
362 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
363 SUCCESS : FAILED;
364release:
365 if (se_cmd)
82f1c8a4 366 transport_generic_free_cmd(se_cmd, 1);
3703b2c5
NB
367 else
368 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
369 kfree(tl_tmr);
370 return ret;
371}
372
373static int tcm_loop_slave_alloc(struct scsi_device *sd)
374{
375 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
376 return 0;
377}
378
379static int tcm_loop_slave_configure(struct scsi_device *sd)
380{
381 return 0;
382}
383
384static struct scsi_host_template tcm_loop_driver_template = {
385 .proc_info = tcm_loop_proc_info,
386 .proc_name = "tcm_loopback",
387 .name = "TCM_Loopback",
388 .queuecommand = tcm_loop_queuecommand,
389 .change_queue_depth = tcm_loop_change_queue_depth,
390 .eh_device_reset_handler = tcm_loop_device_reset,
2e88efd3 391 .can_queue = 1024,
3703b2c5 392 .this_id = -1,
2e88efd3
CH
393 .sg_tablesize = 256,
394 .cmd_per_lun = 1024,
395 .max_sectors = 0xFFFF,
3703b2c5
NB
396 .use_clustering = DISABLE_CLUSTERING,
397 .slave_alloc = tcm_loop_slave_alloc,
398 .slave_configure = tcm_loop_slave_configure,
399 .module = THIS_MODULE,
400};
401
402static int tcm_loop_driver_probe(struct device *dev)
403{
404 struct tcm_loop_hba *tl_hba;
405 struct Scsi_Host *sh;
406 int error;
407
408 tl_hba = to_tcm_loop_hba(dev);
409
410 sh = scsi_host_alloc(&tcm_loop_driver_template,
411 sizeof(struct tcm_loop_hba));
412 if (!sh) {
6708bb27 413 pr_err("Unable to allocate struct scsi_host\n");
3703b2c5
NB
414 return -ENODEV;
415 }
416 tl_hba->sh = sh;
417
418 /*
419 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
420 */
421 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
422 /*
423 * Setup single ID, Channel and LUN for now..
424 */
425 sh->max_id = 2;
426 sh->max_lun = 0;
427 sh->max_channel = 0;
428 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
429
430 error = scsi_add_host(sh, &tl_hba->dev);
431 if (error) {
6708bb27 432 pr_err("%s: scsi_add_host failed\n", __func__);
3703b2c5
NB
433 scsi_host_put(sh);
434 return -ENODEV;
435 }
436 return 0;
437}
438
439static int tcm_loop_driver_remove(struct device *dev)
440{
441 struct tcm_loop_hba *tl_hba;
442 struct Scsi_Host *sh;
443
444 tl_hba = to_tcm_loop_hba(dev);
445 sh = tl_hba->sh;
446
447 scsi_remove_host(sh);
448 scsi_host_put(sh);
449 return 0;
450}
451
452static void tcm_loop_release_adapter(struct device *dev)
453{
454 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
455
456 kfree(tl_hba);
457}
458
459/*
460 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
461 */
462static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
463{
464 int ret;
465
466 tl_hba->dev.bus = &tcm_loop_lld_bus;
467 tl_hba->dev.parent = tcm_loop_primary;
468 tl_hba->dev.release = &tcm_loop_release_adapter;
469 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
470
471 ret = device_register(&tl_hba->dev);
472 if (ret) {
6708bb27 473 pr_err("device_register() failed for"
3703b2c5
NB
474 " tl_hba->dev: %d\n", ret);
475 return -ENODEV;
476 }
477
478 return 0;
479}
480
481/*
482 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
483 * tcm_loop SCSI bus.
484 */
485static int tcm_loop_alloc_core_bus(void)
486{
487 int ret;
488
489 tcm_loop_primary = root_device_register("tcm_loop_0");
490 if (IS_ERR(tcm_loop_primary)) {
6708bb27 491 pr_err("Unable to allocate tcm_loop_primary\n");
3703b2c5
NB
492 return PTR_ERR(tcm_loop_primary);
493 }
494
495 ret = bus_register(&tcm_loop_lld_bus);
496 if (ret) {
6708bb27 497 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
3703b2c5
NB
498 goto dev_unreg;
499 }
500
501 ret = driver_register(&tcm_loop_driverfs);
502 if (ret) {
6708bb27 503 pr_err("driver_register() failed for"
3703b2c5
NB
504 "tcm_loop_driverfs\n");
505 goto bus_unreg;
506 }
507
6708bb27 508 pr_debug("Initialized TCM Loop Core Bus\n");
3703b2c5
NB
509 return ret;
510
511bus_unreg:
512 bus_unregister(&tcm_loop_lld_bus);
513dev_unreg:
514 root_device_unregister(tcm_loop_primary);
515 return ret;
516}
517
518static void tcm_loop_release_core_bus(void)
519{
520 driver_unregister(&tcm_loop_driverfs);
521 bus_unregister(&tcm_loop_lld_bus);
522 root_device_unregister(tcm_loop_primary);
523
6708bb27 524 pr_debug("Releasing TCM Loop Core BUS\n");
3703b2c5
NB
525}
526
527static char *tcm_loop_get_fabric_name(void)
528{
529 return "loopback";
530}
531
532static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
533{
8359cf43 534 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
3703b2c5
NB
535 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
536 /*
537 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
538 * time based on the protocol dependent prefix of the passed configfs group.
539 *
540 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
541 * ProtocolID using target_core_fabric_lib.c symbols.
542 */
543 switch (tl_hba->tl_proto_id) {
544 case SCSI_PROTOCOL_SAS:
545 return sas_get_fabric_proto_ident(se_tpg);
546 case SCSI_PROTOCOL_FCP:
547 return fc_get_fabric_proto_ident(se_tpg);
548 case SCSI_PROTOCOL_ISCSI:
549 return iscsi_get_fabric_proto_ident(se_tpg);
550 default:
6708bb27 551 pr_err("Unknown tl_proto_id: 0x%02x, using"
3703b2c5
NB
552 " SAS emulation\n", tl_hba->tl_proto_id);
553 break;
554 }
555
556 return sas_get_fabric_proto_ident(se_tpg);
557}
558
559static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
560{
8359cf43 561 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
3703b2c5
NB
562 /*
563 * Return the passed NAA identifier for the SAS Target Port
564 */
565 return &tl_tpg->tl_hba->tl_wwn_address[0];
566}
567
568static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
569{
8359cf43 570 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
3703b2c5
NB
571 /*
572 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
573 * to represent the SCSI Target Port.
574 */
575 return tl_tpg->tl_tpgt;
576}
577
578static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
579{
580 return 1;
581}
582
583static u32 tcm_loop_get_pr_transport_id(
584 struct se_portal_group *se_tpg,
585 struct se_node_acl *se_nacl,
586 struct t10_pr_registration *pr_reg,
587 int *format_code,
588 unsigned char *buf)
589{
8359cf43 590 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
3703b2c5
NB
591 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
592
593 switch (tl_hba->tl_proto_id) {
594 case SCSI_PROTOCOL_SAS:
595 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
596 format_code, buf);
597 case SCSI_PROTOCOL_FCP:
598 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
599 format_code, buf);
600 case SCSI_PROTOCOL_ISCSI:
601 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
602 format_code, buf);
603 default:
6708bb27 604 pr_err("Unknown tl_proto_id: 0x%02x, using"
3703b2c5
NB
605 " SAS emulation\n", tl_hba->tl_proto_id);
606 break;
607 }
608
609 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
610 format_code, buf);
611}
612
613static u32 tcm_loop_get_pr_transport_id_len(
614 struct se_portal_group *se_tpg,
615 struct se_node_acl *se_nacl,
616 struct t10_pr_registration *pr_reg,
617 int *format_code)
618{
8359cf43 619 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
3703b2c5
NB
620 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
621
622 switch (tl_hba->tl_proto_id) {
623 case SCSI_PROTOCOL_SAS:
624 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
625 format_code);
626 case SCSI_PROTOCOL_FCP:
627 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
628 format_code);
629 case SCSI_PROTOCOL_ISCSI:
630 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
631 format_code);
632 default:
6708bb27 633 pr_err("Unknown tl_proto_id: 0x%02x, using"
3703b2c5
NB
634 " SAS emulation\n", tl_hba->tl_proto_id);
635 break;
636 }
637
638 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
639 format_code);
640}
641
642/*
643 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
644 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
645 */
646static char *tcm_loop_parse_pr_out_transport_id(
647 struct se_portal_group *se_tpg,
648 const char *buf,
649 u32 *out_tid_len,
650 char **port_nexus_ptr)
651{
8359cf43 652 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
3703b2c5
NB
653 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
654
655 switch (tl_hba->tl_proto_id) {
656 case SCSI_PROTOCOL_SAS:
657 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
658 port_nexus_ptr);
659 case SCSI_PROTOCOL_FCP:
660 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
661 port_nexus_ptr);
662 case SCSI_PROTOCOL_ISCSI:
663 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
664 port_nexus_ptr);
665 default:
6708bb27 666 pr_err("Unknown tl_proto_id: 0x%02x, using"
3703b2c5
NB
667 " SAS emulation\n", tl_hba->tl_proto_id);
668 break;
669 }
670
671 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
672 port_nexus_ptr);
673}
674
675/*
676 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
677 * based upon the incoming fabric dependent SCSI Initiator Port
678 */
679static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
680{
681 return 1;
682}
683
684static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
685{
686 return 0;
687}
688
689/*
690 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
691 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
692 */
693static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
694{
695 return 0;
696}
697
698/*
699 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
700 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
701 * It has been added here as a nop for target_fabric_tf_ops_check()
702 */
703static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
704{
705 return 0;
706}
707
708static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
709 struct se_portal_group *se_tpg)
710{
711 struct tcm_loop_nacl *tl_nacl;
712
713 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
714 if (!tl_nacl) {
6708bb27 715 pr_err("Unable to allocate struct tcm_loop_nacl\n");
3703b2c5
NB
716 return NULL;
717 }
718
719 return &tl_nacl->se_node_acl;
720}
721
722static void tcm_loop_tpg_release_fabric_acl(
723 struct se_portal_group *se_tpg,
724 struct se_node_acl *se_nacl)
725{
726 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
727 struct tcm_loop_nacl, se_node_acl);
728
729 kfree(tl_nacl);
730}
731
732static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
733{
734 return 1;
735}
736
3703b2c5
NB
737static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
738{
739 return 1;
740}
741
742static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
743{
744 return;
745}
746
747static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
748{
749 return 1;
750}
751
752static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
753{
754 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
755 struct tcm_loop_cmd, tl_se_cmd);
756
757 return tl_cmd->sc_cmd_state;
758}
759
760static int tcm_loop_shutdown_session(struct se_session *se_sess)
761{
762 return 0;
763}
764
765static void tcm_loop_close_session(struct se_session *se_sess)
766{
767 return;
768};
769
3703b2c5
NB
770static int tcm_loop_write_pending(struct se_cmd *se_cmd)
771{
772 /*
773 * Since Linux/SCSI has already sent down a struct scsi_cmnd
774 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
775 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
776 * format with transport_generic_map_mem_to_cmd().
777 *
778 * We now tell TCM to add this WRITE CDB directly into the TCM storage
779 * object execution queue.
780 */
781 transport_generic_process_write(se_cmd);
782 return 0;
783}
784
785static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
786{
787 return 0;
788}
789
790static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
791{
792 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
793 struct tcm_loop_cmd, tl_se_cmd);
794 struct scsi_cmnd *sc = tl_cmd->sc;
795
6708bb27 796 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
3703b2c5
NB
797 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
798
799 sc->result = SAM_STAT_GOOD;
800 set_host_byte(sc, DID_OK);
6cf3fa69
RD
801 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
802 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
803 scsi_set_resid(sc, se_cmd->residual_count);
3703b2c5
NB
804 sc->scsi_done(sc);
805 return 0;
806}
807
808static int tcm_loop_queue_status(struct se_cmd *se_cmd)
809{
810 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
811 struct tcm_loop_cmd, tl_se_cmd);
812 struct scsi_cmnd *sc = tl_cmd->sc;
813
6708bb27 814 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
3703b2c5
NB
815 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
816
817 if (se_cmd->sense_buffer &&
818 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
819 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
820
5951146d 821 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
3703b2c5
NB
822 SCSI_SENSE_BUFFERSIZE);
823 sc->result = SAM_STAT_CHECK_CONDITION;
824 set_driver_byte(sc, DRIVER_SENSE);
825 } else
826 sc->result = se_cmd->scsi_status;
827
828 set_host_byte(sc, DID_OK);
6cf3fa69
RD
829 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
830 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
831 scsi_set_resid(sc, se_cmd->residual_count);
3703b2c5
NB
832 sc->scsi_done(sc);
833 return 0;
834}
835
836static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
837{
838 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
839 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
840 /*
841 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
842 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
843 */
844 atomic_set(&tl_tmr->tmr_complete, 1);
845 wake_up(&tl_tmr->tl_tmr_wait);
846 return 0;
847}
848
849static u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
850{
851 return 0;
852}
853
854static u16 tcm_loop_get_fabric_sense_len(void)
855{
856 return 0;
857}
858
3703b2c5
NB
859static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
860{
861 switch (tl_hba->tl_proto_id) {
862 case SCSI_PROTOCOL_SAS:
863 return "SAS";
864 case SCSI_PROTOCOL_FCP:
865 return "FCP";
866 case SCSI_PROTOCOL_ISCSI:
867 return "iSCSI";
868 default:
869 break;
870 }
871
872 return "Unknown";
873}
874
875/* Start items for tcm_loop_port_cit */
876
877static int tcm_loop_port_link(
878 struct se_portal_group *se_tpg,
879 struct se_lun *lun)
880{
881 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
882 struct tcm_loop_tpg, tl_se_tpg);
883 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
884
885 atomic_inc(&tl_tpg->tl_tpg_port_count);
886 smp_mb__after_atomic_inc();
887 /*
888 * Add Linux/SCSI struct scsi_device by HCTL
889 */
890 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
891
6708bb27 892 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
3703b2c5
NB
893 return 0;
894}
895
896static void tcm_loop_port_unlink(
897 struct se_portal_group *se_tpg,
898 struct se_lun *se_lun)
899{
900 struct scsi_device *sd;
901 struct tcm_loop_hba *tl_hba;
902 struct tcm_loop_tpg *tl_tpg;
903
904 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
905 tl_hba = tl_tpg->tl_hba;
906
907 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
908 se_lun->unpacked_lun);
909 if (!sd) {
6708bb27 910 pr_err("Unable to locate struct scsi_device for %d:%d:"
3703b2c5
NB
911 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
912 return;
913 }
914 /*
915 * Remove Linux/SCSI struct scsi_device by HCTL
916 */
917 scsi_remove_device(sd);
918 scsi_device_put(sd);
919
920 atomic_dec(&tl_tpg->tl_tpg_port_count);
921 smp_mb__after_atomic_dec();
922
6708bb27 923 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
3703b2c5
NB
924}
925
926/* End items for tcm_loop_port_cit */
927
928/* Start items for tcm_loop_nexus_cit */
929
930static int tcm_loop_make_nexus(
931 struct tcm_loop_tpg *tl_tpg,
932 const char *name)
933{
934 struct se_portal_group *se_tpg;
935 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
936 struct tcm_loop_nexus *tl_nexus;
552523dc 937 int ret = -ENOMEM;
3703b2c5
NB
938
939 if (tl_tpg->tl_hba->tl_nexus) {
6708bb27 940 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
3703b2c5
NB
941 return -EEXIST;
942 }
943 se_tpg = &tl_tpg->tl_se_tpg;
944
945 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
946 if (!tl_nexus) {
6708bb27 947 pr_err("Unable to allocate struct tcm_loop_nexus\n");
3703b2c5
NB
948 return -ENOMEM;
949 }
950 /*
951 * Initialize the struct se_session pointer
952 */
953 tl_nexus->se_sess = transport_init_session();
552523dc
DC
954 if (IS_ERR(tl_nexus->se_sess)) {
955 ret = PTR_ERR(tl_nexus->se_sess);
3703b2c5 956 goto out;
552523dc 957 }
3703b2c5
NB
958 /*
959 * Since we are running in 'demo mode' this call with generate a
960 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
961 * Initiator port name of the passed configfs group 'name'.
962 */
963 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
964 se_tpg, (unsigned char *)name);
965 if (!tl_nexus->se_sess->se_node_acl) {
966 transport_free_session(tl_nexus->se_sess);
967 goto out;
968 }
969 /*
970 * Now, register the SAS I_T Nexus as active with the call to
971 * transport_register_session()
972 */
973 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
5951146d 974 tl_nexus->se_sess, tl_nexus);
3703b2c5 975 tl_tpg->tl_hba->tl_nexus = tl_nexus;
6708bb27 976 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
3703b2c5
NB
977 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
978 name);
979 return 0;
980
981out:
982 kfree(tl_nexus);
552523dc 983 return ret;
3703b2c5
NB
984}
985
986static int tcm_loop_drop_nexus(
987 struct tcm_loop_tpg *tpg)
988{
989 struct se_session *se_sess;
990 struct tcm_loop_nexus *tl_nexus;
991 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
992
993 tl_nexus = tpg->tl_hba->tl_nexus;
994 if (!tl_nexus)
995 return -ENODEV;
996
997 se_sess = tl_nexus->se_sess;
998 if (!se_sess)
999 return -ENODEV;
1000
1001 if (atomic_read(&tpg->tl_tpg_port_count)) {
6708bb27 1002 pr_err("Unable to remove TCM_Loop I_T Nexus with"
3703b2c5
NB
1003 " active TPG port count: %d\n",
1004 atomic_read(&tpg->tl_tpg_port_count));
1005 return -EPERM;
1006 }
1007
6708bb27 1008 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
3703b2c5
NB
1009 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1010 tl_nexus->se_sess->se_node_acl->initiatorname);
1011 /*
1012 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1013 */
1014 transport_deregister_session(tl_nexus->se_sess);
1015 tpg->tl_hba->tl_nexus = NULL;
1016 kfree(tl_nexus);
1017 return 0;
1018}
1019
1020/* End items for tcm_loop_nexus_cit */
1021
1022static ssize_t tcm_loop_tpg_show_nexus(
1023 struct se_portal_group *se_tpg,
1024 char *page)
1025{
1026 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1027 struct tcm_loop_tpg, tl_se_tpg);
1028 struct tcm_loop_nexus *tl_nexus;
1029 ssize_t ret;
1030
1031 tl_nexus = tl_tpg->tl_hba->tl_nexus;
1032 if (!tl_nexus)
1033 return -ENODEV;
1034
1035 ret = snprintf(page, PAGE_SIZE, "%s\n",
1036 tl_nexus->se_sess->se_node_acl->initiatorname);
1037
1038 return ret;
1039}
1040
1041static ssize_t tcm_loop_tpg_store_nexus(
1042 struct se_portal_group *se_tpg,
1043 const char *page,
1044 size_t count)
1045{
1046 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1047 struct tcm_loop_tpg, tl_se_tpg);
1048 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1049 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1050 int ret;
1051 /*
1052 * Shutdown the active I_T nexus if 'NULL' is passed..
1053 */
1054 if (!strncmp(page, "NULL", 4)) {
1055 ret = tcm_loop_drop_nexus(tl_tpg);
1056 return (!ret) ? count : ret;
1057 }
1058 /*
1059 * Otherwise make sure the passed virtual Initiator port WWN matches
1060 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1061 * tcm_loop_make_nexus()
1062 */
60d645a4 1063 if (strlen(page) >= TL_WWN_ADDR_LEN) {
6708bb27 1064 pr_err("Emulated NAA Sas Address: %s, exceeds"
3703b2c5
NB
1065 " max: %d\n", page, TL_WWN_ADDR_LEN);
1066 return -EINVAL;
1067 }
1068 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1069
1070 ptr = strstr(i_port, "naa.");
1071 if (ptr) {
1072 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
6708bb27 1073 pr_err("Passed SAS Initiator Port %s does not"
3703b2c5
NB
1074 " match target port protoid: %s\n", i_port,
1075 tcm_loop_dump_proto_id(tl_hba));
1076 return -EINVAL;
1077 }
1078 port_ptr = &i_port[0];
1079 goto check_newline;
1080 }
1081 ptr = strstr(i_port, "fc.");
1082 if (ptr) {
1083 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
6708bb27 1084 pr_err("Passed FCP Initiator Port %s does not"
3703b2c5
NB
1085 " match target port protoid: %s\n", i_port,
1086 tcm_loop_dump_proto_id(tl_hba));
1087 return -EINVAL;
1088 }
1089 port_ptr = &i_port[3]; /* Skip over "fc." */
1090 goto check_newline;
1091 }
1092 ptr = strstr(i_port, "iqn.");
1093 if (ptr) {
1094 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
6708bb27 1095 pr_err("Passed iSCSI Initiator Port %s does not"
3703b2c5
NB
1096 " match target port protoid: %s\n", i_port,
1097 tcm_loop_dump_proto_id(tl_hba));
1098 return -EINVAL;
1099 }
1100 port_ptr = &i_port[0];
1101 goto check_newline;
1102 }
6708bb27 1103 pr_err("Unable to locate prefix for emulated Initiator Port:"
3703b2c5
NB
1104 " %s\n", i_port);
1105 return -EINVAL;
1106 /*
1107 * Clear any trailing newline for the NAA WWN
1108 */
1109check_newline:
1110 if (i_port[strlen(i_port)-1] == '\n')
1111 i_port[strlen(i_port)-1] = '\0';
1112
1113 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1114 if (ret < 0)
1115 return ret;
1116
1117 return count;
1118}
1119
1120TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1121
1122static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1123 &tcm_loop_tpg_nexus.attr,
1124 NULL,
1125};
1126
1127/* Start items for tcm_loop_naa_cit */
1128
1129struct se_portal_group *tcm_loop_make_naa_tpg(
1130 struct se_wwn *wwn,
1131 struct config_group *group,
1132 const char *name)
1133{
1134 struct tcm_loop_hba *tl_hba = container_of(wwn,
1135 struct tcm_loop_hba, tl_hba_wwn);
1136 struct tcm_loop_tpg *tl_tpg;
1137 char *tpgt_str, *end_ptr;
1138 int ret;
1139 unsigned short int tpgt;
1140
1141 tpgt_str = strstr(name, "tpgt_");
1142 if (!tpgt_str) {
6708bb27 1143 pr_err("Unable to locate \"tpgt_#\" directory"
3703b2c5
NB
1144 " group\n");
1145 return ERR_PTR(-EINVAL);
1146 }
1147 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1148 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1149
12f09ccb 1150 if (tpgt >= TL_TPGS_PER_HBA) {
6708bb27 1151 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
3703b2c5
NB
1152 " %u\n", tpgt, TL_TPGS_PER_HBA);
1153 return ERR_PTR(-EINVAL);
1154 }
1155 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1156 tl_tpg->tl_hba = tl_hba;
1157 tl_tpg->tl_tpgt = tpgt;
1158 /*
1159 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1160 */
1161 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
5951146d 1162 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
3703b2c5
NB
1163 TRANSPORT_TPG_TYPE_NORMAL);
1164 if (ret < 0)
1165 return ERR_PTR(-ENOMEM);
1166
6708bb27 1167 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
3703b2c5
NB
1168 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1169 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1170
1171 return &tl_tpg->tl_se_tpg;
1172}
1173
1174void tcm_loop_drop_naa_tpg(
1175 struct se_portal_group *se_tpg)
1176{
1177 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1178 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1179 struct tcm_loop_tpg, tl_se_tpg);
1180 struct tcm_loop_hba *tl_hba;
1181 unsigned short tpgt;
1182
1183 tl_hba = tl_tpg->tl_hba;
1184 tpgt = tl_tpg->tl_tpgt;
1185 /*
1186 * Release the I_T Nexus for the Virtual SAS link if present
1187 */
1188 tcm_loop_drop_nexus(tl_tpg);
1189 /*
1190 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1191 */
1192 core_tpg_deregister(se_tpg);
1193
0a020436
NB
1194 tl_tpg->tl_hba = NULL;
1195 tl_tpg->tl_tpgt = 0;
1196
6708bb27 1197 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
3703b2c5
NB
1198 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1199 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1200}
1201
1202/* End items for tcm_loop_naa_cit */
1203
1204/* Start items for tcm_loop_cit */
1205
1206struct se_wwn *tcm_loop_make_scsi_hba(
1207 struct target_fabric_configfs *tf,
1208 struct config_group *group,
1209 const char *name)
1210{
1211 struct tcm_loop_hba *tl_hba;
1212 struct Scsi_Host *sh;
1213 char *ptr;
1214 int ret, off = 0;
1215
1216 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1217 if (!tl_hba) {
6708bb27 1218 pr_err("Unable to allocate struct tcm_loop_hba\n");
3703b2c5
NB
1219 return ERR_PTR(-ENOMEM);
1220 }
1221 /*
1222 * Determine the emulated Protocol Identifier and Target Port Name
1223 * based on the incoming configfs directory name.
1224 */
1225 ptr = strstr(name, "naa.");
1226 if (ptr) {
1227 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1228 goto check_len;
1229 }
1230 ptr = strstr(name, "fc.");
1231 if (ptr) {
1232 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1233 off = 3; /* Skip over "fc." */
1234 goto check_len;
1235 }
1236 ptr = strstr(name, "iqn.");
a57b5d36 1237 if (!ptr) {
6708bb27 1238 pr_err("Unable to locate prefix for emulated Target "
a57b5d36
JJ
1239 "Port: %s\n", name);
1240 ret = -EINVAL;
1241 goto out;
3703b2c5 1242 }
a57b5d36 1243 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
3703b2c5
NB
1244
1245check_len:
60d645a4 1246 if (strlen(name) >= TL_WWN_ADDR_LEN) {
6708bb27 1247 pr_err("Emulated NAA %s Address: %s, exceeds"
3703b2c5
NB
1248 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1249 TL_WWN_ADDR_LEN);
a57b5d36
JJ
1250 ret = -EINVAL;
1251 goto out;
3703b2c5
NB
1252 }
1253 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1254
1255 /*
1256 * Call device_register(tl_hba->dev) to register the emulated
1257 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1258 * device_register() callbacks in tcm_loop_driver_probe()
1259 */
1260 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1261 if (ret)
1262 goto out;
1263
1264 sh = tl_hba->sh;
1265 tcm_loop_hba_no_cnt++;
6708bb27 1266 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
3703b2c5
NB
1267 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1268 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1269
1270 return &tl_hba->tl_hba_wwn;
1271out:
1272 kfree(tl_hba);
1273 return ERR_PTR(ret);
1274}
1275
1276void tcm_loop_drop_scsi_hba(
1277 struct se_wwn *wwn)
1278{
1279 struct tcm_loop_hba *tl_hba = container_of(wwn,
1280 struct tcm_loop_hba, tl_hba_wwn);
6297b07c
NB
1281
1282 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1283 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1284 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
3703b2c5
NB
1285 /*
1286 * Call device_unregister() on the original tl_hba->dev.
1287 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1288 * release *tl_hba;
1289 */
1290 device_unregister(&tl_hba->dev);
3703b2c5
NB
1291}
1292
1293/* Start items for tcm_loop_cit */
1294static ssize_t tcm_loop_wwn_show_attr_version(
1295 struct target_fabric_configfs *tf,
1296 char *page)
1297{
1298 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1299}
1300
1301TF_WWN_ATTR_RO(tcm_loop, version);
1302
1303static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1304 &tcm_loop_wwn_version.attr,
1305 NULL,
1306};
1307
1308/* End items for tcm_loop_cit */
1309
1310static int tcm_loop_register_configfs(void)
1311{
1312 struct target_fabric_configfs *fabric;
3703b2c5
NB
1313 int ret;
1314 /*
1315 * Set the TCM Loop HBA counter to zero
1316 */
1317 tcm_loop_hba_no_cnt = 0;
1318 /*
1319 * Register the top level struct config_item_type with TCM core
1320 */
1321 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
e3d6f909 1322 if (IS_ERR(fabric)) {
6708bb27 1323 pr_err("tcm_loop_register_configfs() failed!\n");
e3d6f909 1324 return PTR_ERR(fabric);
3703b2c5
NB
1325 }
1326 /*
1327 * Setup the fabric API of function pointers used by target_core_mod
1328 */
1329 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1330 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1331 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1332 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1333 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1334 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1335 fabric->tf_ops.tpg_get_pr_transport_id_len =
1336 &tcm_loop_get_pr_transport_id_len;
1337 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1338 &tcm_loop_parse_pr_out_transport_id;
1339 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1340 fabric->tf_ops.tpg_check_demo_mode_cache =
1341 &tcm_loop_check_demo_mode_cache;
1342 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1343 &tcm_loop_check_demo_mode_write_protect;
1344 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1345 &tcm_loop_check_prod_mode_write_protect;
1346 /*
1347 * The TCM loopback fabric module runs in demo-mode to a local
1348 * virtual SCSI device, so fabric dependent initator ACLs are
1349 * not required.
1350 */
1351 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1352 fabric->tf_ops.tpg_release_fabric_acl =
1353 &tcm_loop_tpg_release_fabric_acl;
1354 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
3703b2c5
NB
1355 /*
1356 * Used for setting up remaining TCM resources in process context
1357 */
3703b2c5 1358 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
35462975 1359 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
3703b2c5
NB
1360 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1361 fabric->tf_ops.close_session = &tcm_loop_close_session;
3703b2c5
NB
1362 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1363 fabric->tf_ops.sess_get_initiator_sid = NULL;
1364 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1365 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1366 /*
1367 * Not used for TCM loopback
1368 */
1369 fabric->tf_ops.set_default_node_attributes =
1370 &tcm_loop_set_default_node_attributes;
1371 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1372 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
3703b2c5
NB
1373 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1374 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1375 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1376 fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
1377 fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
3703b2c5 1378
3703b2c5
NB
1379 /*
1380 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1381 */
1382 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1383 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1384 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1385 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1386 /*
1387 * fabric_post_link() and fabric_pre_unlink() are used for
1388 * registration and release of TCM Loop Virtual SCSI LUNs.
1389 */
1390 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1391 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1392 fabric->tf_ops.fabric_make_np = NULL;
1393 fabric->tf_ops.fabric_drop_np = NULL;
1394 /*
1395 * Setup default attribute lists for various fabric->tf_cit_tmpl
1396 */
1397 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1398 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1399 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1400 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1401 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1402 /*
1403 * Once fabric->tf_ops has been setup, now register the fabric for
1404 * use within TCM
1405 */
1406 ret = target_fabric_configfs_register(fabric);
1407 if (ret < 0) {
6708bb27 1408 pr_err("target_fabric_configfs_register() for"
3703b2c5
NB
1409 " TCM_Loop failed!\n");
1410 target_fabric_configfs_free(fabric);
1411 return -1;
1412 }
1413 /*
1414 * Setup our local pointer to *fabric.
1415 */
1416 tcm_loop_fabric_configfs = fabric;
6708bb27 1417 pr_debug("TCM_LOOP[0] - Set fabric ->"
3703b2c5
NB
1418 " tcm_loop_fabric_configfs\n");
1419 return 0;
1420}
1421
1422static void tcm_loop_deregister_configfs(void)
1423{
1424 if (!tcm_loop_fabric_configfs)
1425 return;
1426
1427 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1428 tcm_loop_fabric_configfs = NULL;
6708bb27 1429 pr_debug("TCM_LOOP[0] - Cleared"
3703b2c5
NB
1430 " tcm_loop_fabric_configfs\n");
1431}
1432
1433static int __init tcm_loop_fabric_init(void)
1434{
afe2cb7f
CH
1435 int ret = -ENOMEM;
1436
1437 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1438 if (!tcm_loop_workqueue)
1439 goto out;
3703b2c5
NB
1440
1441 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1442 sizeof(struct tcm_loop_cmd),
1443 __alignof__(struct tcm_loop_cmd),
1444 0, NULL);
1445 if (!tcm_loop_cmd_cache) {
6708bb27 1446 pr_debug("kmem_cache_create() for"
3703b2c5 1447 " tcm_loop_cmd_cache failed\n");
afe2cb7f 1448 goto out_destroy_workqueue;
3703b2c5
NB
1449 }
1450
1451 ret = tcm_loop_alloc_core_bus();
1452 if (ret)
afe2cb7f 1453 goto out_destroy_cache;
3703b2c5
NB
1454
1455 ret = tcm_loop_register_configfs();
afe2cb7f
CH
1456 if (ret)
1457 goto out_release_core_bus;
3703b2c5
NB
1458
1459 return 0;
afe2cb7f
CH
1460
1461out_release_core_bus:
1462 tcm_loop_release_core_bus();
1463out_destroy_cache:
1464 kmem_cache_destroy(tcm_loop_cmd_cache);
1465out_destroy_workqueue:
1466 destroy_workqueue(tcm_loop_workqueue);
1467out:
1468 return ret;
3703b2c5
NB
1469}
1470
1471static void __exit tcm_loop_fabric_exit(void)
1472{
1473 tcm_loop_deregister_configfs();
1474 tcm_loop_release_core_bus();
1475 kmem_cache_destroy(tcm_loop_cmd_cache);
afe2cb7f 1476 destroy_workqueue(tcm_loop_workqueue);
3703b2c5
NB
1477}
1478
1479MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1480MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1481MODULE_LICENSE("GPL");
1482module_init(tcm_loop_fabric_init);
1483module_exit(tcm_loop_fabric_exit);
This page took 0.14311 seconds and 5 git commands to generate.