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