blob: 9a776cc03d67d92ea405b6175f11607f0efb9f1e [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080029#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>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <linux/genhd.h>
37#include <linux/cdrom.h>
Christoph Hellwigd6e01752012-05-20 11:59:14 -040038#include <linux/ratelimit.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040039#include <linux/module.h>
Christoph Hellwigd6e01752012-05-20 11:59:14 -040040#include <asm/unaligned.h>
41
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080042#include <scsi/scsi.h>
43#include <scsi/scsi_device.h>
44#include <scsi/scsi_cmnd.h>
45#include <scsi/scsi_host.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070046#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
48#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050049#include <target/target_core_backend.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080050
Christoph Hellwigd6e01752012-05-20 11:59:14 -040051#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080052#include "target_core_pscsi.h"
53
54#define ISPRINT(a) ((a >= ' ') && (a <= '~'))
55
56static struct se_subsystem_api pscsi_template;
57
58static void pscsi_req_done(struct request *, int);
59
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060/* 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 */
65static int pscsi_attach_hba(struct se_hba *hba, u32 host_id)
66{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080067 struct pscsi_hba_virt *phv;
68
69 phv = kzalloc(sizeof(struct pscsi_hba_virt), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -070070 if (!phv) {
71 pr_err("Unable to allocate struct pscsi_hba_virt\n");
Andy Grovere3d6f902011-07-19 08:55:10 +000072 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080073 }
74 phv->phv_host_id = host_id;
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +000075 phv->phv_mode = PHV_VIRTUAL_HOST_ID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080076
Andy Grover5951146d2011-07-19 10:26:37 +000077 hba->hba_ptr = phv;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Andy Grover6708bb22011-06-08 10:36:43 -070079 pr_debug("CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080 " Generic Target Core Stack %s\n", hba->hba_id,
81 PSCSI_VERSION, TARGET_CORE_MOD_VERSION);
Andy Grover6708bb22011-06-08 10:36:43 -070082 pr_debug("CORE_HBA[%d] - Attached SCSI HBA to Generic\n",
Andy Grovere3d6f902011-07-19 08:55:10 +000083 hba->hba_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080084
85 return 0;
86}
87
88static 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
Andy Grover6708bb22011-06-08 10:36:43 -070096 pr_debug("CORE_HBA[%d] - Detached SCSI HBA: %s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 " Generic Target Core\n", hba->hba_id,
98 (scsi_host->hostt->name) ? (scsi_host->hostt->name) :
99 "Unknown");
100 } else
Andy Grover6708bb22011-06-08 10:36:43 -0700101 pr_debug("CORE_HBA[%d] - Detached Virtual SCSI HBA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 " from Generic Target Core\n", hba->hba_id);
103
104 kfree(phv);
105 hba->hba_ptr = NULL;
106}
107
108static int pscsi_pmode_enable_hba(struct se_hba *hba, unsigned long mode_flag)
109{
Jörn Engel8359cf42011-11-24 02:05:51 +0100110 struct pscsi_hba_virt *phv = hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 struct Scsi_Host *sh = phv->phv_lld_host;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 /*
113 * Release the struct Scsi_Host
114 */
Andy Grover6708bb22011-06-08 10:36:43 -0700115 if (!mode_flag) {
116 if (!sh)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800117 return 0;
118
119 phv->phv_lld_host = NULL;
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +0000120 phv->phv_mode = PHV_VIRTUAL_HOST_ID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800121
Andy Grover6708bb22011-06-08 10:36:43 -0700122 pr_debug("CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800123 " %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 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000133 sh = scsi_host_lookup(phv->phv_host_id);
134 if (IS_ERR(sh)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700135 pr_err("pSCSI: Unable to locate SCSI Host for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800136 " phv_host_id: %d\n", phv->phv_host_id);
Andy Grovere3d6f902011-07-19 08:55:10 +0000137 return PTR_ERR(sh);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800139
140 phv->phv_lld_host = sh;
141 phv->phv_mode = PHV_LLD_SCSI_HOST_NO;
142
Andy Grover6708bb22011-06-08 10:36:43 -0700143 pr_debug("CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800144 hba->hba_id, (sh->hostt->name) ? (sh->hostt->name) : "Unknown");
145
146 return 1;
147}
148
149static 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;
174out_free:
175 kfree(buf);
176}
177
178static void
179pscsi_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
197static int
198pscsi_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)
Andy Grovere3d6f902011-07-19 08:55:10 +0000205 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206
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
226out_free:
227 kfree(buf);
Andy Grovere3d6f902011-07-19 08:55:10 +0000228 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229}
230
231static void
232pscsi_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) {
Andy Grover6708bb22011-06-08 10:36:43 -0700262 pr_err("page_83[3]: identifier"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800263 " length zero!\n");
264 break;
265 }
Andy Grover6708bb22011-06-08 10:36:43 -0700266 pr_debug("T10 VPD Identifer Length: %d\n", ident_len);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800267
268 vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
269 if (!vpd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700270 pr_err("Unable to allocate memory for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800271 " 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
297out:
298 kfree(buf);
299}
300
301/* pscsi_add_device_to_list():
302 *
303 *
304 */
305static 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
Andy Grover6708bb22011-06-08 10:36:43 -0700322 pr_err("Set broken SCSI Device %d:%d:%d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323 " 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;
Andy Grovera1d8b492011-05-02 17:12:10 -0700333 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));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800335 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;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800356 dev = transport_add_device_to_core_hba(hba, &pscsi_template,
Andy Grover5951146d2011-07-19 10:26:37 +0000357 se_dev, dev_flags, pdv,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 &dev_limits, NULL, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700359 if (!dev) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 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
384static 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);
Andy Grover6708bb22011-06-08 10:36:43 -0700389 if (!pdv) {
390 pr_err("Unable to allocate memory for struct pscsi_dev_virt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 return NULL;
392 }
393 pdv->pdv_se_hba = hba;
394
Andy Grover6708bb22011-06-08 10:36:43 -0700395 pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv, name);
Andy Grover5951146d2011-07-19 10:26:37 +0000396 return pdv;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397}
398
399/*
400 * Called with struct Scsi_Host->host_lock called.
401 */
402static 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)
Dan Carpenter5dd7ed22011-03-14 04:06:01 -0700407 __releases(sh->host_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408{
409 struct se_device *dev;
Jörn Engel8359cf42011-11-24 02:05:51 +0100410 struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411 struct Scsi_Host *sh = sd->host;
412 struct block_device *bd;
413 u32 dev_flags = 0;
414
415 if (scsi_device_get(sd)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700416 pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800417 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);
Dan Carpenter3ae279d2011-02-09 15:34:36 -0800428 if (IS_ERR(bd)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700429 pr_err("pSCSI: blkdev_get_by_path() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 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);
Andy Grover6708bb22011-06-08 10:36:43 -0700436 if (!dev) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800437 blkdev_put(pdv->pdv_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
438 scsi_device_put(sd);
439 return NULL;
440 }
Andy Grover6708bb22011-06-08 10:36:43 -0700441 pr_debug("CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 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 */
450static 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)
Dan Carpenter5dd7ed22011-03-14 04:06:01 -0700455 __releases(sh->host_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456{
457 struct se_device *dev;
Jörn Engel8359cf42011-11-24 02:05:51 +0100458 struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800459 struct Scsi_Host *sh = sd->host;
460 u32 dev_flags = 0;
461
462 if (scsi_device_get(sd)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700463 pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464 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);
Andy Grover6708bb22011-06-08 10:36:43 -0700471 if (!dev) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472 scsi_device_put(sd);
473 return NULL;
474 }
Andy Grover6708bb22011-06-08 10:36:43 -0700475 pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800476 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 */
485static 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)
Dan Carpenter5dd7ed22011-03-14 04:06:01 -0700490 __releases(sh->host_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491{
492 struct se_device *dev;
Jörn Engel8359cf42011-11-24 02:05:51 +0100493 struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800494 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);
Andy Grover6708bb22011-06-08 10:36:43 -0700499 if (!dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 return NULL;
501
Andy Grover6708bb22011-06-08 10:36:43 -0700502 pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503 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
509static struct se_device *pscsi_create_virtdevice(
510 struct se_hba *hba,
511 struct se_subsystem_dev *se_dev,
512 void *p)
513{
Jörn Engel8359cf42011-11-24 02:05:51 +0100514 struct pscsi_dev_virt *pdv = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800515 struct se_device *dev;
516 struct scsi_device *sd;
Jörn Engel8359cf42011-11-24 02:05:51 +0100517 struct pscsi_hba_virt *phv = hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800518 struct Scsi_Host *sh = phv->phv_lld_host;
519 int legacy_mode_enable = 0;
520
Andy Grover6708bb22011-06-08 10:36:43 -0700521 if (!pdv) {
522 pr_err("Unable to locate struct pscsi_dev_virt"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 " parameter\n");
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700524 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 }
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 */
Andy Grover6708bb22011-06-08 10:36:43 -0700530 if (!sh) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800531 if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
Andy Grover6708bb22011-06-08 10:36:43 -0700532 pr_err("pSCSI: Unable to locate struct"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800533 " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700534 return ERR_PTR(-ENODEV);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 }
536 /*
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +0000537 * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 * reference, we enforce that udev_path has been set
539 */
540 if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700541 pr_err("pSCSI: udev_path attribute has not"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542 " been set before ENABLE=1\n");
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700543 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 }
545 /*
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +0000546 * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 * 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);
Andy Grover6708bb22011-06-08 10:36:43 -0700552 if (!list_empty(&hba->hba_dev_list)) {
553 pr_err("pSCSI: Unable to set hba_mode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 " with active devices\n");
555 spin_unlock(&hba->device_lock);
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700556 return ERR_PTR(-EEXIST);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 }
558 spin_unlock(&hba->device_lock);
559
560 if (pscsi_pmode_enable_hba(hba, 1) != 1)
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700561 return ERR_PTR(-ENODEV);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800562
563 legacy_mode_enable = 1;
564 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
565 sh = phv->phv_lld_host;
566 } else {
Andy Grovere3d6f902011-07-19 08:55:10 +0000567 sh = scsi_host_lookup(pdv->pdv_host_id);
568 if (IS_ERR(sh)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700569 pr_err("pSCSI: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800570 " pdv_host_id: %d\n", pdv->pdv_host_id);
Dan Carpenter942d8262011-10-02 01:59:13 +0300571 return ERR_CAST(sh);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800572 }
573 }
574 } else {
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +0000575 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) {
576 pr_err("pSCSI: PHV_VIRTUAL_HOST_ID set while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800577 " struct Scsi_Host exists\n");
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700578 return ERR_PTR(-EEXIST);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800579 }
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
Andy Grover6708bb22011-06-08 10:36:43 -0700605 if (!dev) {
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +0000606 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800607 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;
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700613 return ERR_PTR(-ENODEV);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614 }
615 return dev;
616 }
617 spin_unlock_irq(sh->host_lock);
618
Andy Grover6708bb22011-06-08 10:36:43 -0700619 pr_err("pSCSI: Unable to locate %d:%d:%d:%d\n", sh->host_no,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620 pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id);
621
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +0000622 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800623 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
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700629 return ERR_PTR(-ENODEV);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630}
631
632/* pscsi_free_device(): (Part of se_subsystem_api_t template)
633 *
634 *
635 */
636static 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
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400669static int pscsi_transport_complete(struct se_cmd *cmd, struct scatterlist *sg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800670{
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400671 struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800672 struct scsi_device *sd = pdv->pdv_sd;
673 int result;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400674 struct pscsi_plugin_task *pt = cmd->priv;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800675 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) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400684 if (!cmd->se_deve)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800685 goto after_mode_sense;
686
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400687 if (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) {
688 unsigned char *buf = transport_kmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689
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 }
Andy Grover05d1c7c2011-07-20 19:13:28 +0000697
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400698 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800699 }
700 }
701after_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;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717 u16 bdl;
718 u32 blocksize;
719
720 buf = sg_virt(&sg[0]);
Andy Grover6708bb22011-06-08 10:36:43 -0700721 if (!buf) {
722 pr_err("Unable to get buf for scatterlist\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800723 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 }
743after_mode_select:
744
745 if (status_byte(result) & CHECK_CONDITION)
746 return 1;
747
748 return 0;
749}
750
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751enum {
752 Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
753 Opt_scsi_lun_id, Opt_err
754};
755
756static 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
764static 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
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +0100781 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800782 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) {
Andy Grover6708bb22011-06-08 10:36:43 -0700789 pr_err("PSCSI[%d]: Unable to accept"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800790 " 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;
Andy Grover6708bb22011-06-08 10:36:43 -0700798 pr_debug("PSCSI[%d]: Referencing SCSI Host ID:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 " %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;
Andy Grover6708bb22011-06-08 10:36:43 -0700805 pr_debug("PSCSI[%d]: Referencing SCSI Channel"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800806 " 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;
Andy Grover6708bb22011-06-08 10:36:43 -0700813 pr_debug("PSCSI[%d]: Referencing SCSI Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800814 " 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;
Andy Grover6708bb22011-06-08 10:36:43 -0700821 pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800822 " %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
830out:
831 kfree(orig);
832 return (!ret) ? count : ret;
833}
834
835static 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)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700844 pr_err("Missing scsi_channel_id=, scsi_target_id= and"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800845 " scsi_lun_id= parameters\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000846 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800847 }
848
849 return 0;
850}
851
852static 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
Stefan Hajnoczie6a8a412012-02-23 18:15:26 +0000863 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800864 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
901static void pscsi_bi_endio(struct bio *bio, int error)
902{
903 bio_put(bio);
904}
905
Andy Grovera1d8b492011-05-02 17:12:10 -0700906static inline struct bio *pscsi_get_bio(int sg_num)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800907{
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);
Andy Grover6708bb22011-06-08 10:36:43 -0700914 if (!bio) {
915 pr_err("PSCSI: bio_kmalloc() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800916 return NULL;
917 }
918 bio->bi_end_io = pscsi_bi_endio;
919
920 return bio;
921}
922
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400923static int pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl,
924 u32 sgl_nents, enum dma_data_direction data_direction,
Christoph Hellwig02b1a742011-09-25 14:56:32 -0400925 struct bio **hbio)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800926{
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400927 struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
Christoph Hellwig02b1a742011-09-25 14:56:32 -0400928 struct bio *bio = NULL, *tbio = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800929 struct page *page;
930 struct scatterlist *sg;
Christoph Hellwig7a83aa42012-04-23 11:35:31 -0400931 u32 data_len = cmd->data_length, i, len, bytes, off;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400932 int nr_pages = (cmd->data_length + sgl[0].offset +
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800933 PAGE_SIZE - 1) >> PAGE_SHIFT;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700934 int nr_vecs = 0, rc;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400935 int rw = (data_direction == DMA_TO_DEVICE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800936
Christoph Hellwig02b1a742011-09-25 14:56:32 -0400937 *hbio = NULL;
938
Andy Grover6708bb22011-06-08 10:36:43 -0700939 pr_debug("PSCSI: nr_pages: %d\n", nr_pages);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800940
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400941 for_each_sg(sgl, sg, sgl_nents, i) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800942 page = sg_page(sg);
943 off = sg->offset;
944 len = sg->length;
945
Andy Grover6708bb22011-06-08 10:36:43 -0700946 pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947 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
Andy Grover6708bb22011-06-08 10:36:43 -0700953 if (!bio) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800954 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 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700959 bio = pscsi_get_bio(nr_vecs);
Andy Grover6708bb22011-06-08 10:36:43 -0700960 if (!bio)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800961 goto fail;
962
963 if (rw)
964 bio->bi_rw |= REQ_WRITE;
965
Andy Grover6708bb22011-06-08 10:36:43 -0700966 pr_debug("PSCSI: Allocated bio: %p,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800967 " 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
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400973 * command.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800974 */
Christoph Hellwig02b1a742011-09-25 14:56:32 -0400975 if (!*hbio)
976 *hbio = tbio = bio;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800977 else
978 tbio = tbio->bi_next = bio;
979 }
980
Andy Grover6708bb22011-06-08 10:36:43 -0700981 pr_debug("PSCSI: Calling bio_add_pc_page() i: %d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800982 " 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
Andy Grover6708bb22011-06-08 10:36:43 -0700990 pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800991 bio->bi_vcnt, nr_vecs);
992
993 if (bio->bi_vcnt > nr_vecs) {
Andy Grover6708bb22011-06-08 10:36:43 -0700994 pr_debug("PSCSI: Reached bio->bi_vcnt max:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800995 " %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 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001012
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001013 return sgl_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001014fail:
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001015 while (*hbio) {
1016 bio = *hbio;
1017 *hbio = (*hbio)->bi_next;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001018 bio->bi_next = NULL;
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001019 bio_endio(bio, 0); /* XXX: should be error */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020 }
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001021 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1022 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001023}
1024
Christoph Hellwig3d6d7202012-05-20 11:59:17 -04001025/*
1026 * Clear a lun set in the cdb if the initiator talking to use spoke
1027 * and old standards version, as we can't assume the underlying device
1028 * won't choke up on it.
1029 */
1030static inline void pscsi_clear_cdb_lun(unsigned char *cdb)
1031{
1032 switch (cdb[0]) {
1033 case READ_10: /* SBC - RDProtect */
1034 case READ_12: /* SBC - RDProtect */
1035 case READ_16: /* SBC - RDProtect */
1036 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1037 case VERIFY: /* SBC - VRProtect */
1038 case VERIFY_16: /* SBC - VRProtect */
1039 case WRITE_VERIFY: /* SBC - VRProtect */
1040 case WRITE_VERIFY_12: /* SBC - VRProtect */
1041 case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
1042 break;
1043 default:
1044 cdb[1] &= 0x1f; /* clear logical unit number */
1045 break;
1046 }
1047}
1048
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001049static int pscsi_parse_cdb(struct se_cmd *cmd)
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001050{
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001051 unsigned char *cdb = cmd->t_task_cdb;
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001052 unsigned int dummy_size;
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001053 int ret;
1054
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001055 if (cmd->se_cmd_flags & SCF_BIDI) {
1056 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1057 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1058 return -EINVAL;
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001059 }
1060
Christoph Hellwig3d6d7202012-05-20 11:59:17 -04001061 pscsi_clear_cdb_lun(cdb);
1062
Christoph Hellwig1fd032e2012-05-20 11:59:15 -04001063 /*
1064 * For REPORT LUNS we always need to emulate the respone, and for everything
1065 * related to persistent reservations and ALUA we might optionally use our
1066 * handlers before passing on the command to the physical hardware.
1067 */
1068 switch (cdb[0]) {
1069 case REPORT_LUNS:
1070 case PERSISTENT_RESERVE_IN:
1071 case PERSISTENT_RESERVE_OUT:
1072 case RELEASE:
1073 case RELEASE_10:
1074 case RESERVE:
1075 case RESERVE_10:
1076 ret = spc_parse_cdb(cmd, &dummy_size);
1077 if (ret)
1078 return ret;
1079 break;
1080 case READ_6:
1081 case READ_10:
1082 case READ_12:
1083 case READ_16:
1084 case WRITE_6:
1085 case WRITE_10:
1086 case WRITE_12:
1087 case WRITE_16:
1088 case WRITE_VERIFY:
1089 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
1090 break;
1091 default:
1092 break;
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001093 }
1094
1095 return 0;
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001096}
1097
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001098static int pscsi_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
1099 u32 sgl_nents, enum dma_data_direction data_direction)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001100{
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001101 struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
1102 struct pscsi_plugin_task *pt;
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001103 struct request *req;
1104 struct bio *hbio;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105 int ret;
1106
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001107 /*
1108 * Dynamically alloc cdb space, since it may be larger than
1109 * TCM_MAX_COMMAND_SIZE
1110 */
1111 pt = kzalloc(sizeof(*pt) + scsi_command_size(cmd->t_task_cdb), GFP_KERNEL);
1112 if (!pt) {
1113 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1114 return -ENOMEM;
1115 }
1116 cmd->priv = pt;
1117
Christoph Hellwiged3102c2012-04-23 11:35:29 -04001118 memcpy(pt->pscsi_cdb, cmd->t_task_cdb,
1119 scsi_command_size(cmd->t_task_cdb));
Christoph Hellwig485fd0d2011-10-12 11:09:12 -04001120
Christoph Hellwig64f1db32012-05-20 11:59:11 -04001121 if (!sgl) {
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001122 req = blk_get_request(pdv->pdv_sd->request_queue,
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001123 (data_direction == DMA_TO_DEVICE),
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001124 GFP_KERNEL);
1125 if (!req || IS_ERR(req)) {
1126 pr_err("PSCSI: blk_get_request() failed: %ld\n",
1127 req ? IS_ERR(req) : -ENOMEM);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001128 cmd->scsi_sense_reason =
1129 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001130 goto fail;
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001131 }
1132 } else {
Christoph Hellwig7a83aa42012-04-23 11:35:31 -04001133 BUG_ON(!cmd->data_length);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001135 ret = pscsi_map_sg(cmd, sgl, sgl_nents, data_direction, &hbio);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001136 if (ret < 0) {
1137 cmd->scsi_sense_reason =
1138 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001139 goto fail;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001140 }
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001141
1142 req = blk_make_request(pdv->pdv_sd->request_queue, hbio,
1143 GFP_KERNEL);
Dan Carpentered327ed2011-11-02 08:48:15 -07001144 if (IS_ERR(req)) {
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001145 pr_err("pSCSI: blk_make_request() failed\n");
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001146 goto fail_free_bio;
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001147 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 }
1149
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001150 req->cmd_type = REQ_TYPE_BLOCK_PC;
1151 req->end_io = pscsi_req_done;
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001152 req->end_io_data = cmd;
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001153 req->cmd_len = scsi_command_size(pt->pscsi_cdb);
1154 req->cmd = &pt->pscsi_cdb[0];
1155 req->sense = &pt->pscsi_sense[0];
1156 req->sense_len = 0;
1157 if (pdv->pdv_sd->type == TYPE_DISK)
1158 req->timeout = PS_TIMEOUT_DISK;
1159 else
1160 req->timeout = PS_TIMEOUT_OTHER;
1161 req->retries = PS_RETRY;
1162
1163 blk_execute_rq_nowait(pdv->pdv_sd->request_queue, NULL, req,
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001164 (cmd->sam_task_attr == MSG_HEAD_TAG),
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001165 pscsi_req_done);
1166
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001167 return 0;
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001168
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001169fail_free_bio:
Christoph Hellwig02b1a742011-09-25 14:56:32 -04001170 while (hbio) {
1171 struct bio *bio = hbio;
1172 hbio = hbio->bi_next;
1173 bio->bi_next = NULL;
1174 bio_endio(bio, 0); /* XXX: should be error */
1175 }
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001176 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001177fail:
1178 kfree(pt);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001179 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180}
1181
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001182static unsigned char *pscsi_get_sense_buffer(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001183{
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001184 struct pscsi_plugin_task *pt = cmd->priv;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001185
Jörn Engel8359cf42011-11-24 02:05:51 +01001186 return pt->pscsi_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001187}
1188
1189/* pscsi_get_device_rev():
1190 *
1191 *
1192 */
1193static u32 pscsi_get_device_rev(struct se_device *dev)
1194{
1195 struct pscsi_dev_virt *pdv = dev->dev_ptr;
1196 struct scsi_device *sd = pdv->pdv_sd;
1197
1198 return (sd->scsi_level - 1) ? sd->scsi_level - 1 : 1;
1199}
1200
1201/* pscsi_get_device_type():
1202 *
1203 *
1204 */
1205static u32 pscsi_get_device_type(struct se_device *dev)
1206{
1207 struct pscsi_dev_virt *pdv = dev->dev_ptr;
1208 struct scsi_device *sd = pdv->pdv_sd;
1209
1210 return sd->type;
1211}
1212
1213static sector_t pscsi_get_blocks(struct se_device *dev)
1214{
1215 struct pscsi_dev_virt *pdv = dev->dev_ptr;
1216
1217 if (pdv->pdv_bd && pdv->pdv_bd->bd_part)
1218 return pdv->pdv_bd->bd_part->nr_sects;
1219
1220 dump_stack();
1221 return 0;
1222}
1223
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001224static void pscsi_req_done(struct request *req, int uptodate)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225{
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001226 struct se_cmd *cmd = req->end_io_data;
1227 struct pscsi_plugin_task *pt = cmd->priv;
1228
1229 pt->pscsi_result = req->errors;
1230 pt->pscsi_resid = req->resid_len;
1231
1232 cmd->scsi_status = status_byte(pt->pscsi_result) << 1;
1233 if (cmd->scsi_status) {
1234 pr_debug("PSCSI Status Byte exception at cmd: %p CDB:"
1235 " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236 pt->pscsi_result);
1237 }
1238
1239 switch (host_byte(pt->pscsi_result)) {
1240 case DID_OK:
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001241 target_complete_cmd(cmd, cmd->scsi_status);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001242 break;
1243 default:
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001244 pr_debug("PSCSI Host Byte exception at cmd: %p CDB:"
1245 " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246 pt->pscsi_result);
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001247 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1248 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249 break;
1250 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 __blk_put_request(req->q, req);
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001253 kfree(pt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254}
1255
1256static struct se_subsystem_api pscsi_template = {
1257 .name = "pscsi",
1258 .owner = THIS_MODULE,
1259 .transport_type = TRANSPORT_PLUGIN_PHBA_PDEV,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001260 .attach_hba = pscsi_attach_hba,
1261 .detach_hba = pscsi_detach_hba,
1262 .pmode_enable_hba = pscsi_pmode_enable_hba,
1263 .allocate_virtdevice = pscsi_allocate_virtdevice,
1264 .create_virtdevice = pscsi_create_virtdevice,
1265 .free_device = pscsi_free_device,
1266 .transport_complete = pscsi_transport_complete,
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001267 .parse_cdb = pscsi_parse_cdb,
Christoph Hellwig5787cac2012-04-24 00:25:06 -04001268 .execute_cmd = pscsi_execute_cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269 .check_configfs_dev_params = pscsi_check_configfs_dev_params,
1270 .set_configfs_dev_params = pscsi_set_configfs_dev_params,
1271 .show_configfs_dev_params = pscsi_show_configfs_dev_params,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001272 .get_sense_buffer = pscsi_get_sense_buffer,
1273 .get_device_rev = pscsi_get_device_rev,
1274 .get_device_type = pscsi_get_device_type,
1275 .get_blocks = pscsi_get_blocks,
1276};
1277
1278static int __init pscsi_module_init(void)
1279{
1280 return transport_subsystem_register(&pscsi_template);
1281}
1282
1283static void pscsi_module_exit(void)
1284{
1285 transport_subsystem_release(&pscsi_template);
1286}
1287
1288MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
1289MODULE_AUTHOR("nab@Linux-iSCSI.org");
1290MODULE_LICENSE("GPL");
1291
1292module_init(pscsi_module_init);
1293module_exit(pscsi_module_exit);