target: move code for CDB emulation
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / target / target_core_pscsi.c
1 /*******************************************************************************
2  * Filename:  target_core_pscsi.c
3  *
4  * This file contains the generic target mode <-> Linux SCSI subsystem plugin.
5  *
6  * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
7  * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8  * Copyright (c) 2007-2010 Rising Tide Systems
9  * Copyright (c) 2008-2010 Linux-iSCSI.org
10  *
11  * Nicholas A. Bellinger <nab@kernel.org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  *
27  ******************************************************************************/
28
29 #include <linux/string.h>
30 #include <linux/parser.h>
31 #include <linux/timer.h>
32 #include <linux/blkdev.h>
33 #include <linux/blk_types.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/genhd.h>
37 #include <linux/cdrom.h>
38 #include <linux/ratelimit.h>
39 #include <linux/module.h>
40 #include <asm/unaligned.h>
41
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsi_tcq.h>
47
48 #include <target/target_core_base.h>
49 #include <target/target_core_backend.h>
50
51 #include "target_core_alua.h"
52 #include "target_core_pscsi.h"
53
54 #define ISPRINT(a)  ((a >= ' ') && (a <= '~'))
55
56 static struct se_subsystem_api pscsi_template;
57
58 static void pscsi_req_done(struct request *, int);
59
60 /*      pscsi_attach_hba():
61  *
62  *      pscsi_get_sh() used scsi_host_lookup() to locate struct Scsi_Host.
63  *      from the passed SCSI Host ID.
64  */
65 static int pscsi_attach_hba(struct se_hba *hba, u32 host_id)
66 {
67         struct pscsi_hba_virt *phv;
68
69         phv = kzalloc(sizeof(struct pscsi_hba_virt), GFP_KERNEL);
70         if (!phv) {
71                 pr_err("Unable to allocate struct pscsi_hba_virt\n");
72                 return -ENOMEM;
73         }
74         phv->phv_host_id = host_id;
75         phv->phv_mode = PHV_VIRTUAL_HOST_ID;
76
77         hba->hba_ptr = phv;
78
79         pr_debug("CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
80                 " Generic Target Core Stack %s\n", hba->hba_id,
81                 PSCSI_VERSION, TARGET_CORE_MOD_VERSION);
82         pr_debug("CORE_HBA[%d] - Attached SCSI HBA to Generic\n",
83                hba->hba_id);
84
85         return 0;
86 }
87
88 static void pscsi_detach_hba(struct se_hba *hba)
89 {
90         struct pscsi_hba_virt *phv = hba->hba_ptr;
91         struct Scsi_Host *scsi_host = phv->phv_lld_host;
92
93         if (scsi_host) {
94                 scsi_host_put(scsi_host);
95
96                 pr_debug("CORE_HBA[%d] - Detached SCSI HBA: %s from"
97                         " Generic Target Core\n", hba->hba_id,
98                         (scsi_host->hostt->name) ? (scsi_host->hostt->name) :
99                         "Unknown");
100         } else
101                 pr_debug("CORE_HBA[%d] - Detached Virtual SCSI HBA"
102                         " from Generic Target Core\n", hba->hba_id);
103
104         kfree(phv);
105         hba->hba_ptr = NULL;
106 }
107
108 static int pscsi_pmode_enable_hba(struct se_hba *hba, unsigned long mode_flag)
109 {
110         struct pscsi_hba_virt *phv = hba->hba_ptr;
111         struct Scsi_Host *sh = phv->phv_lld_host;
112         /*
113          * Release the struct Scsi_Host
114          */
115         if (!mode_flag) {
116                 if (!sh)
117                         return 0;
118
119                 phv->phv_lld_host = NULL;
120                 phv->phv_mode = PHV_VIRTUAL_HOST_ID;
121
122                 pr_debug("CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
123                         " %s\n", hba->hba_id, (sh->hostt->name) ?
124                         (sh->hostt->name) : "Unknown");
125
126                 scsi_host_put(sh);
127                 return 0;
128         }
129         /*
130          * Otherwise, locate struct Scsi_Host from the original passed
131          * pSCSI Host ID and enable for phba mode
132          */
133         sh = scsi_host_lookup(phv->phv_host_id);
134         if (IS_ERR(sh)) {
135                 pr_err("pSCSI: Unable to locate SCSI Host for"
136                         " phv_host_id: %d\n", phv->phv_host_id);
137                 return PTR_ERR(sh);
138         }
139
140         phv->phv_lld_host = sh;
141         phv->phv_mode = PHV_LLD_SCSI_HOST_NO;
142
143         pr_debug("CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
144                 hba->hba_id, (sh->hostt->name) ? (sh->hostt->name) : "Unknown");
145
146         return 1;
147 }
148
149 static void pscsi_tape_read_blocksize(struct se_device *dev,
150                 struct scsi_device *sdev)
151 {
152         unsigned char cdb[MAX_COMMAND_SIZE], *buf;
153         int ret;
154
155         buf = kzalloc(12, GFP_KERNEL);
156         if (!buf)
157                 return;
158
159         memset(cdb, 0, MAX_COMMAND_SIZE);
160         cdb[0] = MODE_SENSE;
161         cdb[4] = 0x0c; /* 12 bytes */
162
163         ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf, 12, NULL,
164                         HZ, 1, NULL);
165         if (ret)
166                 goto out_free;
167
168         /*
169          * If MODE_SENSE still returns zero, set the default value to 1024.
170          */
171         sdev->sector_size = (buf[9] << 16) | (buf[10] << 8) | (buf[11]);
172         if (!sdev->sector_size)
173                 sdev->sector_size = 1024;
174 out_free:
175         kfree(buf);
176 }
177
178 static void
179 pscsi_set_inquiry_info(struct scsi_device *sdev, struct t10_wwn *wwn)
180 {
181         unsigned char *buf;
182
183         if (sdev->inquiry_len < INQUIRY_LEN)
184                 return;
185
186         buf = sdev->inquiry;
187         if (!buf)
188                 return;
189         /*
190          * Use sdev->inquiry from drivers/scsi/scsi_scan.c:scsi_alloc_sdev()
191          */
192         memcpy(&wwn->vendor[0], &buf[8], sizeof(wwn->vendor));
193         memcpy(&wwn->model[0], &buf[16], sizeof(wwn->model));
194         memcpy(&wwn->revision[0], &buf[32], sizeof(wwn->revision));
195 }
196
197 static int
198 pscsi_get_inquiry_vpd_serial(struct scsi_device *sdev, struct t10_wwn *wwn)
199 {
200         unsigned char cdb[MAX_COMMAND_SIZE], *buf;
201         int ret;
202
203         buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
204         if (!buf)
205                 return -ENOMEM;
206
207         memset(cdb, 0, MAX_COMMAND_SIZE);
208         cdb[0] = INQUIRY;
209         cdb[1] = 0x01; /* Query VPD */
210         cdb[2] = 0x80; /* Unit Serial Number */
211         cdb[3] = (INQUIRY_VPD_SERIAL_LEN >> 8) & 0xff;
212         cdb[4] = (INQUIRY_VPD_SERIAL_LEN & 0xff);
213
214         ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
215                               INQUIRY_VPD_SERIAL_LEN, NULL, HZ, 1, NULL);
216         if (ret)
217                 goto out_free;
218
219         snprintf(&wwn->unit_serial[0], INQUIRY_VPD_SERIAL_LEN, "%s", &buf[4]);
220
221         wwn->t10_sub_dev->su_dev_flags |= SDF_FIRMWARE_VPD_UNIT_SERIAL;
222
223         kfree(buf);
224         return 0;
225
226 out_free:
227         kfree(buf);
228         return -EPERM;
229 }
230
231 static void
232 pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
233                 struct t10_wwn *wwn)
234 {
235         unsigned char cdb[MAX_COMMAND_SIZE], *buf, *page_83;
236         int ident_len, page_len, off = 4, ret;
237         struct t10_vpd *vpd;
238
239         buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
240         if (!buf)
241                 return;
242
243         memset(cdb, 0, MAX_COMMAND_SIZE);
244         cdb[0] = INQUIRY;
245         cdb[1] = 0x01; /* Query VPD */
246         cdb[2] = 0x83; /* Device Identifier */
247         cdb[3] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN >> 8) & 0xff;
248         cdb[4] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN & 0xff);
249
250         ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
251                               INQUIRY_VPD_DEVICE_IDENTIFIER_LEN,
252                               NULL, HZ, 1, NULL);
253         if (ret)
254                 goto out;
255
256         page_len = (buf[2] << 8) | buf[3];
257         while (page_len > 0) {
258                 /* Grab a pointer to the Identification descriptor */
259                 page_83 = &buf[off];
260                 ident_len = page_83[3];
261                 if (!ident_len) {
262                         pr_err("page_83[3]: identifier"
263                                         " length zero!\n");
264                         break;
265                 }
266                 pr_debug("T10 VPD Identifer Length: %d\n", ident_len);
267
268                 vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
269                 if (!vpd) {
270                         pr_err("Unable to allocate memory for"
271                                         " struct t10_vpd\n");
272                         goto out;
273                 }
274                 INIT_LIST_HEAD(&vpd->vpd_list);
275
276                 transport_set_vpd_proto_id(vpd, page_83);
277                 transport_set_vpd_assoc(vpd, page_83);
278
279                 if (transport_set_vpd_ident_type(vpd, page_83) < 0) {
280                         off += (ident_len + 4);
281                         page_len -= (ident_len + 4);
282                         kfree(vpd);
283                         continue;
284                 }
285                 if (transport_set_vpd_ident(vpd, page_83) < 0) {
286                         off += (ident_len + 4);
287                         page_len -= (ident_len + 4);
288                         kfree(vpd);
289                         continue;
290                 }
291
292                 list_add_tail(&vpd->vpd_list, &wwn->t10_vpd_list);
293                 off += (ident_len + 4);
294                 page_len -= (ident_len + 4);
295         }
296
297 out:
298         kfree(buf);
299 }
300
301 /*      pscsi_add_device_to_list():
302  *
303  *
304  */
305 static struct se_device *pscsi_add_device_to_list(
306         struct se_hba *hba,
307         struct se_subsystem_dev *se_dev,
308         struct pscsi_dev_virt *pdv,
309         struct scsi_device *sd,
310         int dev_flags)
311 {
312         struct se_device *dev;
313         struct se_dev_limits dev_limits;
314         struct request_queue *q;
315         struct queue_limits *limits;
316
317         memset(&dev_limits, 0, sizeof(struct se_dev_limits));
318
319         if (!sd->queue_depth) {
320                 sd->queue_depth = PSCSI_DEFAULT_QUEUEDEPTH;
321
322                 pr_err("Set broken SCSI Device %d:%d:%d"
323                         " queue_depth to %d\n", sd->channel, sd->id,
324                                 sd->lun, sd->queue_depth);
325         }
326         /*
327          * Setup the local scope queue_limits from struct request_queue->limits
328          * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
329          */
330         q = sd->request_queue;
331         limits = &dev_limits.limits;
332         limits->logical_block_size = sd->sector_size;
333         limits->max_hw_sectors = min_t(int, sd->host->max_sectors, queue_max_hw_sectors(q));
334         limits->max_sectors = min_t(int, sd->host->max_sectors, queue_max_sectors(q));
335         dev_limits.hw_queue_depth = sd->queue_depth;
336         dev_limits.queue_depth = sd->queue_depth;
337         /*
338          * Setup our standard INQUIRY info into se_dev->t10_wwn
339          */
340         pscsi_set_inquiry_info(sd, &se_dev->t10_wwn);
341
342         /*
343          * Set the pointer pdv->pdv_sd to from passed struct scsi_device,
344          * which has already been referenced with Linux SCSI code with
345          * scsi_device_get() in this file's pscsi_create_virtdevice().
346          *
347          * The passthrough operations called by the transport_add_device_*
348          * function below will require this pointer to be set for passthroug
349          *  ops.
350          *
351          * For the shutdown case in pscsi_free_device(), this struct
352          * scsi_device  reference is released with Linux SCSI code
353          * scsi_device_put() and the pdv->pdv_sd cleared.
354          */
355         pdv->pdv_sd = sd;
356         dev = transport_add_device_to_core_hba(hba, &pscsi_template,
357                                 se_dev, dev_flags, pdv,
358                                 &dev_limits, NULL, NULL);
359         if (!dev) {
360                 pdv->pdv_sd = NULL;
361                 return NULL;
362         }
363
364         /*
365          * Locate VPD WWN Information used for various purposes within
366          * the Storage Engine.
367          */
368         if (!pscsi_get_inquiry_vpd_serial(sd, &se_dev->t10_wwn)) {
369                 /*
370                  * If VPD Unit Serial returned GOOD status, try
371                  * VPD Device Identification page (0x83).
372                  */
373                 pscsi_get_inquiry_vpd_device_ident(sd, &se_dev->t10_wwn);
374         }
375
376         /*
377          * For TYPE_TAPE, attempt to determine blocksize with MODE_SENSE.
378          */
379         if (sd->type == TYPE_TAPE)
380                 pscsi_tape_read_blocksize(dev, sd);
381         return dev;
382 }
383
384 static void *pscsi_allocate_virtdevice(struct se_hba *hba, const char *name)
385 {
386         struct pscsi_dev_virt *pdv;
387
388         pdv = kzalloc(sizeof(struct pscsi_dev_virt), GFP_KERNEL);
389         if (!pdv) {
390                 pr_err("Unable to allocate memory for struct pscsi_dev_virt\n");
391                 return NULL;
392         }
393         pdv->pdv_se_hba = hba;
394
395         pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv, name);
396         return pdv;
397 }
398
399 /*
400  * Called with struct Scsi_Host->host_lock called.
401  */
402 static struct se_device *pscsi_create_type_disk(
403         struct scsi_device *sd,
404         struct pscsi_dev_virt *pdv,
405         struct se_subsystem_dev *se_dev,
406         struct se_hba *hba)
407         __releases(sh->host_lock)
408 {
409         struct se_device *dev;
410         struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
411         struct Scsi_Host *sh = sd->host;
412         struct block_device *bd;
413         u32 dev_flags = 0;
414
415         if (scsi_device_get(sd)) {
416                 pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
417                         sh->host_no, sd->channel, sd->id, sd->lun);
418                 spin_unlock_irq(sh->host_lock);
419                 return NULL;
420         }
421         spin_unlock_irq(sh->host_lock);
422         /*
423          * Claim exclusive struct block_device access to struct scsi_device
424          * for TYPE_DISK using supplied udev_path
425          */
426         bd = blkdev_get_by_path(se_dev->se_dev_udev_path,
427                                 FMODE_WRITE|FMODE_READ|FMODE_EXCL, pdv);
428         if (IS_ERR(bd)) {
429                 pr_err("pSCSI: blkdev_get_by_path() failed\n");
430                 scsi_device_put(sd);
431                 return NULL;
432         }
433         pdv->pdv_bd = bd;
434
435         dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
436         if (!dev) {
437                 blkdev_put(pdv->pdv_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
438                 scsi_device_put(sd);
439                 return NULL;
440         }
441         pr_debug("CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%d\n",
442                 phv->phv_host_id, sh->host_no, sd->channel, sd->id, sd->lun);
443
444         return dev;
445 }
446
447 /*
448  * Called with struct Scsi_Host->host_lock called.
449  */
450 static struct se_device *pscsi_create_type_rom(
451         struct scsi_device *sd,
452         struct pscsi_dev_virt *pdv,
453         struct se_subsystem_dev *se_dev,
454         struct se_hba *hba)
455         __releases(sh->host_lock)
456 {
457         struct se_device *dev;
458         struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
459         struct Scsi_Host *sh = sd->host;
460         u32 dev_flags = 0;
461
462         if (scsi_device_get(sd)) {
463                 pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
464                         sh->host_no, sd->channel, sd->id, sd->lun);
465                 spin_unlock_irq(sh->host_lock);
466                 return NULL;
467         }
468         spin_unlock_irq(sh->host_lock);
469
470         dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
471         if (!dev) {
472                 scsi_device_put(sd);
473                 return NULL;
474         }
475         pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
476                 phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
477                 sd->channel, sd->id, sd->lun);
478
479         return dev;
480 }
481
482 /*
483  *Called with struct Scsi_Host->host_lock called.
484  */
485 static struct se_device *pscsi_create_type_other(
486         struct scsi_device *sd,
487         struct pscsi_dev_virt *pdv,
488         struct se_subsystem_dev *se_dev,
489         struct se_hba *hba)
490         __releases(sh->host_lock)
491 {
492         struct se_device *dev;
493         struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
494         struct Scsi_Host *sh = sd->host;
495         u32 dev_flags = 0;
496
497         spin_unlock_irq(sh->host_lock);
498         dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
499         if (!dev)
500                 return NULL;
501
502         pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
503                 phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
504                 sd->channel, sd->id, sd->lun);
505
506         return dev;
507 }
508
509 static struct se_device *pscsi_create_virtdevice(
510         struct se_hba *hba,
511         struct se_subsystem_dev *se_dev,
512         void *p)
513 {
514         struct pscsi_dev_virt *pdv = p;
515         struct se_device *dev;
516         struct scsi_device *sd;
517         struct pscsi_hba_virt *phv = hba->hba_ptr;
518         struct Scsi_Host *sh = phv->phv_lld_host;
519         int legacy_mode_enable = 0;
520
521         if (!pdv) {
522                 pr_err("Unable to locate struct pscsi_dev_virt"
523                                 " parameter\n");
524                 return ERR_PTR(-EINVAL);
525         }
526         /*
527          * If not running in PHV_LLD_SCSI_HOST_NO mode, locate the
528          * struct Scsi_Host we will need to bring the TCM/pSCSI object online
529          */
530         if (!sh) {
531                 if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
532                         pr_err("pSCSI: Unable to locate struct"
533                                 " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
534                         return ERR_PTR(-ENODEV);
535                 }
536                 /*
537                  * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device
538                  * reference, we enforce that udev_path has been set
539                  */
540                 if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH)) {
541                         pr_err("pSCSI: udev_path attribute has not"
542                                 " been set before ENABLE=1\n");
543                         return ERR_PTR(-EINVAL);
544                 }
545                 /*
546                  * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID,
547                  * use the original TCM hba ID to reference Linux/SCSI Host No
548                  * and enable for PHV_LLD_SCSI_HOST_NO mode.
549                  */
550                 if (!(pdv->pdv_flags & PDF_HAS_VIRT_HOST_ID)) {
551                         spin_lock(&hba->device_lock);
552                         if (!list_empty(&hba->hba_dev_list)) {
553                                 pr_err("pSCSI: Unable to set hba_mode"
554                                         " with active devices\n");
555                                 spin_unlock(&hba->device_lock);
556                                 return ERR_PTR(-EEXIST);
557                         }
558                         spin_unlock(&hba->device_lock);
559
560                         if (pscsi_pmode_enable_hba(hba, 1) != 1)
561                                 return ERR_PTR(-ENODEV);
562
563                         legacy_mode_enable = 1;
564                         hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
565                         sh = phv->phv_lld_host;
566                 } else {
567                         sh = scsi_host_lookup(pdv->pdv_host_id);
568                         if (IS_ERR(sh)) {
569                                 pr_err("pSCSI: Unable to locate"
570                                         " pdv_host_id: %d\n", pdv->pdv_host_id);
571                                 return ERR_CAST(sh);
572                         }
573                 }
574         } else {
575                 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) {
576                         pr_err("pSCSI: PHV_VIRTUAL_HOST_ID set while"
577                                 " struct Scsi_Host exists\n");
578                         return ERR_PTR(-EEXIST);
579                 }
580         }
581
582         spin_lock_irq(sh->host_lock);
583         list_for_each_entry(sd, &sh->__devices, siblings) {
584                 if ((pdv->pdv_channel_id != sd->channel) ||
585                     (pdv->pdv_target_id != sd->id) ||
586                     (pdv->pdv_lun_id != sd->lun))
587                         continue;
588                 /*
589                  * Functions will release the held struct scsi_host->host_lock
590                  * before calling calling pscsi_add_device_to_list() to register
591                  * struct scsi_device with target_core_mod.
592                  */
593                 switch (sd->type) {
594                 case TYPE_DISK:
595                         dev = pscsi_create_type_disk(sd, pdv, se_dev, hba);
596                         break;
597                 case TYPE_ROM:
598                         dev = pscsi_create_type_rom(sd, pdv, se_dev, hba);
599                         break;
600                 default:
601                         dev = pscsi_create_type_other(sd, pdv, se_dev, hba);
602                         break;
603                 }
604
605                 if (!dev) {
606                         if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
607                                 scsi_host_put(sh);
608                         else if (legacy_mode_enable) {
609                                 pscsi_pmode_enable_hba(hba, 0);
610                                 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
611                         }
612                         pdv->pdv_sd = NULL;
613                         return ERR_PTR(-ENODEV);
614                 }
615                 return dev;
616         }
617         spin_unlock_irq(sh->host_lock);
618
619         pr_err("pSCSI: Unable to locate %d:%d:%d:%d\n", sh->host_no,
620                 pdv->pdv_channel_id,  pdv->pdv_target_id, pdv->pdv_lun_id);
621
622         if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
623                 scsi_host_put(sh);
624         else if (legacy_mode_enable) {
625                 pscsi_pmode_enable_hba(hba, 0);
626                 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
627         }
628
629         return ERR_PTR(-ENODEV);
630 }
631
632 /*      pscsi_free_device(): (Part of se_subsystem_api_t template)
633  *
634  *
635  */
636 static void pscsi_free_device(void *p)
637 {
638         struct pscsi_dev_virt *pdv = p;
639         struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
640         struct scsi_device *sd = pdv->pdv_sd;
641
642         if (sd) {
643                 /*
644                  * Release exclusive pSCSI internal struct block_device claim for
645                  * struct scsi_device with TYPE_DISK from pscsi_create_type_disk()
646                  */
647                 if ((sd->type == TYPE_DISK) && pdv->pdv_bd) {
648                         blkdev_put(pdv->pdv_bd,
649                                    FMODE_WRITE|FMODE_READ|FMODE_EXCL);
650                         pdv->pdv_bd = NULL;
651                 }
652                 /*
653                  * For HBA mode PHV_LLD_SCSI_HOST_NO, release the reference
654                  * to struct Scsi_Host now.
655                  */
656                 if ((phv->phv_mode == PHV_LLD_SCSI_HOST_NO) &&
657                     (phv->phv_lld_host != NULL))
658                         scsi_host_put(phv->phv_lld_host);
659
660                 if ((sd->type == TYPE_DISK) || (sd->type == TYPE_ROM))
661                         scsi_device_put(sd);
662
663                 pdv->pdv_sd = NULL;
664         }
665
666         kfree(pdv);
667 }
668
669 static int pscsi_transport_complete(struct se_cmd *cmd, struct scatterlist *sg)
670 {
671         struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
672         struct scsi_device *sd = pdv->pdv_sd;
673         int result;
674         struct pscsi_plugin_task *pt = cmd->priv;
675         unsigned char *cdb = &pt->pscsi_cdb[0];
676
677         result = pt->pscsi_result;
678         /*
679          * Hack to make sure that Write-Protect modepage is set if R/O mode is
680          * forced.
681          */
682         if (((cdb[0] == MODE_SENSE) || (cdb[0] == MODE_SENSE_10)) &&
683              (status_byte(result) << 1) == SAM_STAT_GOOD) {
684                 if (!cmd->se_deve)
685                         goto after_mode_sense;
686
687                 if (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) {
688                         unsigned char *buf = transport_kmap_data_sg(cmd);
689
690                         if (cdb[0] == MODE_SENSE_10) {
691                                 if (!(buf[3] & 0x80))
692                                         buf[3] |= 0x80;
693                         } else {
694                                 if (!(buf[2] & 0x80))
695                                         buf[2] |= 0x80;
696                         }
697
698                         transport_kunmap_data_sg(cmd);
699                 }
700         }
701 after_mode_sense:
702
703         if (sd->type != TYPE_TAPE)
704                 goto after_mode_select;
705
706         /*
707          * Hack to correctly obtain the initiator requested blocksize for
708          * TYPE_TAPE.  Since this value is dependent upon each tape media,
709          * struct scsi_device->sector_size will not contain the correct value
710          * by default, so we go ahead and set it so
711          * TRANSPORT(dev)->get_blockdev() returns the correct value to the
712          * storage engine.
713          */
714         if (((cdb[0] == MODE_SELECT) || (cdb[0] == MODE_SELECT_10)) &&
715               (status_byte(result) << 1) == SAM_STAT_GOOD) {
716                 unsigned char *buf;
717                 u16 bdl;
718                 u32 blocksize;
719
720                 buf = sg_virt(&sg[0]);
721                 if (!buf) {
722                         pr_err("Unable to get buf for scatterlist\n");
723                         goto after_mode_select;
724                 }
725
726                 if (cdb[0] == MODE_SELECT)
727                         bdl = (buf[3]);
728                 else
729                         bdl = (buf[6] << 8) | (buf[7]);
730
731                 if (!bdl)
732                         goto after_mode_select;
733
734                 if (cdb[0] == MODE_SELECT)
735                         blocksize = (buf[9] << 16) | (buf[10] << 8) |
736                                         (buf[11]);
737                 else
738                         blocksize = (buf[13] << 16) | (buf[14] << 8) |
739                                         (buf[15]);
740
741                 sd->sector_size = blocksize;
742         }
743 after_mode_select:
744
745         if (status_byte(result) & CHECK_CONDITION)
746                 return 1;
747
748         return 0;
749 }
750
751 enum {
752         Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
753         Opt_scsi_lun_id, Opt_err
754 };
755
756 static match_table_t tokens = {
757         {Opt_scsi_host_id, "scsi_host_id=%d"},
758         {Opt_scsi_channel_id, "scsi_channel_id=%d"},
759         {Opt_scsi_target_id, "scsi_target_id=%d"},
760         {Opt_scsi_lun_id, "scsi_lun_id=%d"},
761         {Opt_err, NULL}
762 };
763
764 static ssize_t pscsi_set_configfs_dev_params(struct se_hba *hba,
765         struct se_subsystem_dev *se_dev,
766         const char *page,
767         ssize_t count)
768 {
769         struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
770         struct pscsi_hba_virt *phv = hba->hba_ptr;
771         char *orig, *ptr, *opts;
772         substring_t args[MAX_OPT_ARGS];
773         int ret = 0, arg, token;
774
775         opts = kstrdup(page, GFP_KERNEL);
776         if (!opts)
777                 return -ENOMEM;
778
779         orig = opts;
780
781         while ((ptr = strsep(&opts, ",\n")) != NULL) {
782                 if (!*ptr)
783                         continue;
784
785                 token = match_token(ptr, tokens, args);
786                 switch (token) {
787                 case Opt_scsi_host_id:
788                         if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
789                                 pr_err("PSCSI[%d]: Unable to accept"
790                                         " scsi_host_id while phv_mode =="
791                                         " PHV_LLD_SCSI_HOST_NO\n",
792                                         phv->phv_host_id);
793                                 ret = -EINVAL;
794                                 goto out;
795                         }
796                         match_int(args, &arg);
797                         pdv->pdv_host_id = arg;
798                         pr_debug("PSCSI[%d]: Referencing SCSI Host ID:"
799                                 " %d\n", phv->phv_host_id, pdv->pdv_host_id);
800                         pdv->pdv_flags |= PDF_HAS_VIRT_HOST_ID;
801                         break;
802                 case Opt_scsi_channel_id:
803                         match_int(args, &arg);
804                         pdv->pdv_channel_id = arg;
805                         pr_debug("PSCSI[%d]: Referencing SCSI Channel"
806                                 " ID: %d\n",  phv->phv_host_id,
807                                 pdv->pdv_channel_id);
808                         pdv->pdv_flags |= PDF_HAS_CHANNEL_ID;
809                         break;
810                 case Opt_scsi_target_id:
811                         match_int(args, &arg);
812                         pdv->pdv_target_id = arg;
813                         pr_debug("PSCSI[%d]: Referencing SCSI Target"
814                                 " ID: %d\n", phv->phv_host_id,
815                                 pdv->pdv_target_id);
816                         pdv->pdv_flags |= PDF_HAS_TARGET_ID;
817                         break;
818                 case Opt_scsi_lun_id:
819                         match_int(args, &arg);
820                         pdv->pdv_lun_id = arg;
821                         pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:"
822                                 " %d\n", phv->phv_host_id, pdv->pdv_lun_id);
823                         pdv->pdv_flags |= PDF_HAS_LUN_ID;
824                         break;
825                 default:
826                         break;
827                 }
828         }
829
830 out:
831         kfree(orig);
832         return (!ret) ? count : ret;
833 }
834
835 static ssize_t pscsi_check_configfs_dev_params(
836         struct se_hba *hba,
837         struct se_subsystem_dev *se_dev)
838 {
839         struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
840
841         if (!(pdv->pdv_flags & PDF_HAS_CHANNEL_ID) ||
842             !(pdv->pdv_flags & PDF_HAS_TARGET_ID) ||
843             !(pdv->pdv_flags & PDF_HAS_LUN_ID)) {
844                 pr_err("Missing scsi_channel_id=, scsi_target_id= and"
845                         " scsi_lun_id= parameters\n");
846                 return -EINVAL;
847         }
848
849         return 0;
850 }
851
852 static ssize_t pscsi_show_configfs_dev_params(struct se_hba *hba,
853                                               struct se_subsystem_dev *se_dev,
854                                               char *b)
855 {
856         struct pscsi_hba_virt *phv = hba->hba_ptr;
857         struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
858         struct scsi_device *sd = pdv->pdv_sd;
859         unsigned char host_id[16];
860         ssize_t bl;
861         int i;
862
863         if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
864                 snprintf(host_id, 16, "%d", pdv->pdv_host_id);
865         else
866                 snprintf(host_id, 16, "PHBA Mode");
867
868         bl = sprintf(b, "SCSI Device Bus Location:"
869                 " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
870                 pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id,
871                 host_id);
872
873         if (sd) {
874                 bl += sprintf(b + bl, "        ");
875                 bl += sprintf(b + bl, "Vendor: ");
876                 for (i = 0; i < 8; i++) {
877                         if (ISPRINT(sd->vendor[i]))   /* printable character? */
878                                 bl += sprintf(b + bl, "%c", sd->vendor[i]);
879                         else
880                                 bl += sprintf(b + bl, " ");
881                 }
882                 bl += sprintf(b + bl, " Model: ");
883                 for (i = 0; i < 16; i++) {
884                         if (ISPRINT(sd->model[i]))   /* printable character ? */
885                                 bl += sprintf(b + bl, "%c", sd->model[i]);
886                         else
887                                 bl += sprintf(b + bl, " ");
888                 }
889                 bl += sprintf(b + bl, " Rev: ");
890                 for (i = 0; i < 4; i++) {
891                         if (ISPRINT(sd->rev[i]))   /* printable character ? */
892                                 bl += sprintf(b + bl, "%c", sd->rev[i]);
893                         else
894                                 bl += sprintf(b + bl, " ");
895                 }
896                 bl += sprintf(b + bl, "\n");
897         }
898         return bl;
899 }
900
901 static void pscsi_bi_endio(struct bio *bio, int error)
902 {
903         bio_put(bio);
904 }
905
906 static inline struct bio *pscsi_get_bio(int sg_num)
907 {
908         struct bio *bio;
909         /*
910          * Use bio_malloc() following the comment in for bio -> struct request
911          * in block/blk-core.c:blk_make_request()
912          */
913         bio = bio_kmalloc(GFP_KERNEL, sg_num);
914         if (!bio) {
915                 pr_err("PSCSI: bio_kmalloc() failed\n");
916                 return NULL;
917         }
918         bio->bi_end_io = pscsi_bi_endio;
919
920         return bio;
921 }
922
923 static int pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl,
924                 u32 sgl_nents, enum dma_data_direction data_direction,
925                 struct bio **hbio)
926 {
927         struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
928         struct bio *bio = NULL, *tbio = NULL;
929         struct page *page;
930         struct scatterlist *sg;
931         u32 data_len = cmd->data_length, i, len, bytes, off;
932         int nr_pages = (cmd->data_length + sgl[0].offset +
933                         PAGE_SIZE - 1) >> PAGE_SHIFT;
934         int nr_vecs = 0, rc;
935         int rw = (data_direction == DMA_TO_DEVICE);
936
937         *hbio = NULL;
938
939         pr_debug("PSCSI: nr_pages: %d\n", nr_pages);
940
941         for_each_sg(sgl, sg, sgl_nents, i) {
942                 page = sg_page(sg);
943                 off = sg->offset;
944                 len = sg->length;
945
946                 pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i,
947                         page, len, off);
948
949                 while (len > 0 && data_len > 0) {
950                         bytes = min_t(unsigned int, len, PAGE_SIZE - off);
951                         bytes = min(bytes, data_len);
952
953                         if (!bio) {
954                                 nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
955                                 nr_pages -= nr_vecs;
956                                 /*
957                                  * Calls bio_kmalloc() and sets bio->bi_end_io()
958                                  */
959                                 bio = pscsi_get_bio(nr_vecs);
960                                 if (!bio)
961                                         goto fail;
962
963                                 if (rw)
964                                         bio->bi_rw |= REQ_WRITE;
965
966                                 pr_debug("PSCSI: Allocated bio: %p,"
967                                         " dir: %s nr_vecs: %d\n", bio,
968                                         (rw) ? "rw" : "r", nr_vecs);
969                                 /*
970                                  * Set *hbio pointer to handle the case:
971                                  * nr_pages > BIO_MAX_PAGES, where additional
972                                  * bios need to be added to complete a given
973                                  * command.
974                                  */
975                                 if (!*hbio)
976                                         *hbio = tbio = bio;
977                                 else
978                                         tbio = tbio->bi_next = bio;
979                         }
980
981                         pr_debug("PSCSI: Calling bio_add_pc_page() i: %d"
982                                 " bio: %p page: %p len: %d off: %d\n", i, bio,
983                                 page, len, off);
984
985                         rc = bio_add_pc_page(pdv->pdv_sd->request_queue,
986                                         bio, page, bytes, off);
987                         if (rc != bytes)
988                                 goto fail;
989
990                         pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
991                                 bio->bi_vcnt, nr_vecs);
992
993                         if (bio->bi_vcnt > nr_vecs) {
994                                 pr_debug("PSCSI: Reached bio->bi_vcnt max:"
995                                         " %d i: %d bio: %p, allocating another"
996                                         " bio\n", bio->bi_vcnt, i, bio);
997                                 /*
998                                  * Clear the pointer so that another bio will
999                                  * be allocated with pscsi_get_bio() above, the
1000                                  * current bio has already been set *tbio and
1001                                  * bio->bi_next.
1002                                  */
1003                                 bio = NULL;
1004                         }
1005
1006                         page++;
1007                         len -= bytes;
1008                         data_len -= bytes;
1009                         off = 0;
1010                 }
1011         }
1012
1013         return sgl_nents;
1014 fail:
1015         while (*hbio) {
1016                 bio = *hbio;
1017                 *hbio = (*hbio)->bi_next;
1018                 bio->bi_next = NULL;
1019                 bio_endio(bio, 0);      /* XXX: should be error */
1020         }
1021         cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1022         return -ENOMEM;
1023 }
1024
1025 static int pscsi_parse_cdb(struct se_cmd *cmd)
1026 {
1027         unsigned char *cdb = cmd->t_task_cdb;
1028         unsigned int dummy_size;
1029         int ret;
1030
1031         if (cmd->se_cmd_flags & SCF_BIDI) {
1032                 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1033                 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1034                 return -EINVAL;
1035         }
1036
1037         /*
1038          * For REPORT LUNS we always need to emulate the respone, and for everything
1039          * related to persistent reservations and ALUA we might optionally use our
1040          * handlers before passing on the command to the physical hardware.
1041          */
1042         switch (cdb[0]) {
1043         case REPORT_LUNS:
1044         case PERSISTENT_RESERVE_IN:
1045         case PERSISTENT_RESERVE_OUT:
1046         case RELEASE:
1047         case RELEASE_10:
1048         case RESERVE:
1049         case RESERVE_10:
1050                 ret = spc_parse_cdb(cmd, &dummy_size);
1051                 if (ret)
1052                         return ret;
1053                 break;
1054         case READ_6:
1055         case READ_10:
1056         case READ_12:
1057         case READ_16:
1058         case WRITE_6:
1059         case WRITE_10:
1060         case WRITE_12:
1061         case WRITE_16:
1062         case WRITE_VERIFY:
1063                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
1064                 break;
1065         default:
1066                 break;
1067         }
1068
1069         return 0;
1070 }
1071
1072 static int pscsi_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
1073                 u32 sgl_nents, enum dma_data_direction data_direction)
1074 {
1075         struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
1076         struct pscsi_plugin_task *pt;
1077         struct request *req;
1078         struct bio *hbio;
1079         int ret;
1080
1081         /*
1082          * Dynamically alloc cdb space, since it may be larger than
1083          * TCM_MAX_COMMAND_SIZE
1084          */
1085         pt = kzalloc(sizeof(*pt) + scsi_command_size(cmd->t_task_cdb), GFP_KERNEL);
1086         if (!pt) {
1087                 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1088                 return -ENOMEM;
1089         }
1090         cmd->priv = pt;
1091
1092         memcpy(pt->pscsi_cdb, cmd->t_task_cdb,
1093                 scsi_command_size(cmd->t_task_cdb));
1094
1095         if (!sgl) {
1096                 req = blk_get_request(pdv->pdv_sd->request_queue,
1097                                 (data_direction == DMA_TO_DEVICE),
1098                                 GFP_KERNEL);
1099                 if (!req || IS_ERR(req)) {
1100                         pr_err("PSCSI: blk_get_request() failed: %ld\n",
1101                                         req ? IS_ERR(req) : -ENOMEM);
1102                         cmd->scsi_sense_reason =
1103                                 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1104                         goto fail;
1105                 }
1106         } else {
1107                 BUG_ON(!cmd->data_length);
1108
1109                 ret = pscsi_map_sg(cmd, sgl, sgl_nents, data_direction, &hbio);
1110                 if (ret < 0) {
1111                         cmd->scsi_sense_reason =
1112                                 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1113                         goto fail;
1114                 }
1115
1116                 req = blk_make_request(pdv->pdv_sd->request_queue, hbio,
1117                                        GFP_KERNEL);
1118                 if (IS_ERR(req)) {
1119                         pr_err("pSCSI: blk_make_request() failed\n");
1120                         goto fail_free_bio;
1121                 }
1122         }
1123
1124         req->cmd_type = REQ_TYPE_BLOCK_PC;
1125         req->end_io = pscsi_req_done;
1126         req->end_io_data = cmd;
1127         req->cmd_len = scsi_command_size(pt->pscsi_cdb);
1128         req->cmd = &pt->pscsi_cdb[0];
1129         req->sense = &pt->pscsi_sense[0];
1130         req->sense_len = 0;
1131         if (pdv->pdv_sd->type == TYPE_DISK)
1132                 req->timeout = PS_TIMEOUT_DISK;
1133         else
1134                 req->timeout = PS_TIMEOUT_OTHER;
1135         req->retries = PS_RETRY;
1136
1137         blk_execute_rq_nowait(pdv->pdv_sd->request_queue, NULL, req,
1138                         (cmd->sam_task_attr == MSG_HEAD_TAG),
1139                         pscsi_req_done);
1140
1141         return 0;
1142
1143 fail_free_bio:
1144         while (hbio) {
1145                 struct bio *bio = hbio;
1146                 hbio = hbio->bi_next;
1147                 bio->bi_next = NULL;
1148                 bio_endio(bio, 0);      /* XXX: should be error */
1149         }
1150         cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1151 fail:
1152         kfree(pt);
1153         return -ENOMEM;
1154 }
1155
1156 static unsigned char *pscsi_get_sense_buffer(struct se_cmd *cmd)
1157 {
1158         struct pscsi_plugin_task *pt = cmd->priv;
1159
1160         return pt->pscsi_sense;
1161 }
1162
1163 /*      pscsi_get_device_rev():
1164  *
1165  *
1166  */
1167 static u32 pscsi_get_device_rev(struct se_device *dev)
1168 {
1169         struct pscsi_dev_virt *pdv = dev->dev_ptr;
1170         struct scsi_device *sd = pdv->pdv_sd;
1171
1172         return (sd->scsi_level - 1) ? sd->scsi_level - 1 : 1;
1173 }
1174
1175 /*      pscsi_get_device_type():
1176  *
1177  *
1178  */
1179 static u32 pscsi_get_device_type(struct se_device *dev)
1180 {
1181         struct pscsi_dev_virt *pdv = dev->dev_ptr;
1182         struct scsi_device *sd = pdv->pdv_sd;
1183
1184         return sd->type;
1185 }
1186
1187 static sector_t pscsi_get_blocks(struct se_device *dev)
1188 {
1189         struct pscsi_dev_virt *pdv = dev->dev_ptr;
1190
1191         if (pdv->pdv_bd && pdv->pdv_bd->bd_part)
1192                 return pdv->pdv_bd->bd_part->nr_sects;
1193
1194         dump_stack();
1195         return 0;
1196 }
1197
1198 static void pscsi_req_done(struct request *req, int uptodate)
1199 {
1200         struct se_cmd *cmd = req->end_io_data;
1201         struct pscsi_plugin_task *pt = cmd->priv;
1202
1203         pt->pscsi_result = req->errors;
1204         pt->pscsi_resid = req->resid_len;
1205
1206         cmd->scsi_status = status_byte(pt->pscsi_result) << 1;
1207         if (cmd->scsi_status) {
1208                 pr_debug("PSCSI Status Byte exception at cmd: %p CDB:"
1209                         " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
1210                         pt->pscsi_result);
1211         }
1212
1213         switch (host_byte(pt->pscsi_result)) {
1214         case DID_OK:
1215                 target_complete_cmd(cmd, cmd->scsi_status);
1216                 break;
1217         default:
1218                 pr_debug("PSCSI Host Byte exception at cmd: %p CDB:"
1219                         " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
1220                         pt->pscsi_result);
1221                 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1222                 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
1223                 break;
1224         }
1225
1226         __blk_put_request(req->q, req);
1227         kfree(pt);
1228 }
1229
1230 static struct se_subsystem_api pscsi_template = {
1231         .name                   = "pscsi",
1232         .owner                  = THIS_MODULE,
1233         .transport_type         = TRANSPORT_PLUGIN_PHBA_PDEV,
1234         .attach_hba             = pscsi_attach_hba,
1235         .detach_hba             = pscsi_detach_hba,
1236         .pmode_enable_hba       = pscsi_pmode_enable_hba,
1237         .allocate_virtdevice    = pscsi_allocate_virtdevice,
1238         .create_virtdevice      = pscsi_create_virtdevice,
1239         .free_device            = pscsi_free_device,
1240         .transport_complete     = pscsi_transport_complete,
1241         .parse_cdb              = pscsi_parse_cdb,
1242         .execute_cmd            = pscsi_execute_cmd,
1243         .check_configfs_dev_params = pscsi_check_configfs_dev_params,
1244         .set_configfs_dev_params = pscsi_set_configfs_dev_params,
1245         .show_configfs_dev_params = pscsi_show_configfs_dev_params,
1246         .get_sense_buffer       = pscsi_get_sense_buffer,
1247         .get_device_rev         = pscsi_get_device_rev,
1248         .get_device_type        = pscsi_get_device_type,
1249         .get_blocks             = pscsi_get_blocks,
1250 };
1251
1252 static int __init pscsi_module_init(void)
1253 {
1254         return transport_subsystem_register(&pscsi_template);
1255 }
1256
1257 static void pscsi_module_exit(void)
1258 {
1259         transport_subsystem_release(&pscsi_template);
1260 }
1261
1262 MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
1263 MODULE_AUTHOR("nab@Linux-iSCSI.org");
1264 MODULE_LICENSE("GPL");
1265
1266 module_init(pscsi_module_init);
1267 module_exit(pscsi_module_exit);