Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 36 | #include <linux/genhd.h> |
| 37 | #include <linux/cdrom.h> |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 38 | #include <linux/ratelimit.h> |
Paul Gortmaker | 827509e | 2011-08-30 14:20:44 -0400 | [diff] [blame] | 39 | #include <linux/module.h> |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 40 | #include <asm/unaligned.h> |
| 41 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 42 | #include <scsi/scsi.h> |
| 43 | #include <scsi/scsi_device.h> |
| 44 | #include <scsi/scsi_cmnd.h> |
| 45 | #include <scsi/scsi_host.h> |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 46 | #include <scsi/scsi_tcq.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 47 | |
| 48 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 49 | #include <target/target_core_backend.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 50 | |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 51 | #include "target_core_alua.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 67 | struct pscsi_hba_virt *phv; |
| 68 | |
| 69 | phv = kzalloc(sizeof(struct pscsi_hba_virt), GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 70 | if (!phv) { |
| 71 | pr_err("Unable to allocate struct pscsi_hba_virt\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 72 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 73 | } |
| 74 | phv->phv_host_id = host_id; |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 75 | phv->phv_mode = PHV_VIRTUAL_HOST_ID; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 76 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 77 | hba->hba_ptr = phv; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 78 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 79 | pr_debug("CORE_HBA[%d] - TCM SCSI HBA Driver %s on" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 80 | " Generic Target Core Stack %s\n", hba->hba_id, |
| 81 | PSCSI_VERSION, TARGET_CORE_MOD_VERSION); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 82 | pr_debug("CORE_HBA[%d] - Attached SCSI HBA to Generic\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 83 | hba->hba_id); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 96 | pr_debug("CORE_HBA[%d] - Detached SCSI HBA: %s from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 97 | " Generic Target Core\n", hba->hba_id, |
| 98 | (scsi_host->hostt->name) ? (scsi_host->hostt->name) : |
| 99 | "Unknown"); |
| 100 | } else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 101 | pr_debug("CORE_HBA[%d] - Detached Virtual SCSI HBA" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 110 | struct pscsi_hba_virt *phv = hba->hba_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 111 | struct Scsi_Host *sh = phv->phv_lld_host; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 112 | /* |
| 113 | * Release the struct Scsi_Host |
| 114 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 115 | if (!mode_flag) { |
| 116 | if (!sh) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 117 | return 0; |
| 118 | |
| 119 | phv->phv_lld_host = NULL; |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 120 | phv->phv_mode = PHV_VIRTUAL_HOST_ID; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 121 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 122 | pr_debug("CORE_HBA[%d] - Disabled pSCSI HBA Passthrough" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 133 | sh = scsi_host_lookup(phv->phv_host_id); |
| 134 | if (IS_ERR(sh)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 135 | pr_err("pSCSI: Unable to locate SCSI Host for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 136 | " phv_host_id: %d\n", phv->phv_host_id); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 137 | return PTR_ERR(sh); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 138 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 139 | |
| 140 | phv->phv_lld_host = sh; |
| 141 | phv->phv_mode = PHV_LLD_SCSI_HOST_NO; |
| 142 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 143 | pr_debug("CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 205 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 228 | return -EPERM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 262 | pr_err("page_83[3]: identifier" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 263 | " length zero!\n"); |
| 264 | break; |
| 265 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 266 | pr_debug("T10 VPD Identifer Length: %d\n", ident_len); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 267 | |
| 268 | vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL); |
| 269 | if (!vpd) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 270 | pr_err("Unable to allocate memory for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 322 | pr_err("Set broken SCSI Device %d:%d:%d" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 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)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 356 | dev = transport_add_device_to_core_hba(hba, &pscsi_template, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 357 | se_dev, dev_flags, pdv, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 358 | &dev_limits, NULL, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 359 | if (!dev) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 389 | if (!pdv) { |
| 390 | pr_err("Unable to allocate memory for struct pscsi_dev_virt\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | pdv->pdv_se_hba = hba; |
| 394 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 395 | pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv, name); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 396 | return pdv; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) |
Dan Carpenter | 5dd7ed2 | 2011-03-14 04:06:01 -0700 | [diff] [blame] | 407 | __releases(sh->host_lock) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 408 | { |
| 409 | struct se_device *dev; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 410 | struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 411 | struct Scsi_Host *sh = sd->host; |
| 412 | struct block_device *bd; |
| 413 | u32 dev_flags = 0; |
| 414 | |
| 415 | if (scsi_device_get(sd)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 416 | pr_err("scsi_device_get() failed for %d:%d:%d:%d\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Dan Carpenter | 3ae279d | 2011-02-09 15:34:36 -0800 | [diff] [blame] | 428 | if (IS_ERR(bd)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 429 | pr_err("pSCSI: blkdev_get_by_path() failed\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 436 | if (!dev) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 437 | blkdev_put(pdv->pdv_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL); |
| 438 | scsi_device_put(sd); |
| 439 | return NULL; |
| 440 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 441 | pr_debug("CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%d\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) |
Dan Carpenter | 5dd7ed2 | 2011-03-14 04:06:01 -0700 | [diff] [blame] | 455 | __releases(sh->host_lock) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 456 | { |
| 457 | struct se_device *dev; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 458 | struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 459 | struct Scsi_Host *sh = sd->host; |
| 460 | u32 dev_flags = 0; |
| 461 | |
| 462 | if (scsi_device_get(sd)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 463 | pr_err("scsi_device_get() failed for %d:%d:%d:%d\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 471 | if (!dev) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 472 | scsi_device_put(sd); |
| 473 | return NULL; |
| 474 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 475 | pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) |
Dan Carpenter | 5dd7ed2 | 2011-03-14 04:06:01 -0700 | [diff] [blame] | 490 | __releases(sh->host_lock) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 491 | { |
| 492 | struct se_device *dev; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 493 | struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 499 | if (!dev) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 500 | return NULL; |
| 501 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 502 | pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 514 | struct pscsi_dev_virt *pdv = p; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 515 | struct se_device *dev; |
| 516 | struct scsi_device *sd; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 517 | struct pscsi_hba_virt *phv = hba->hba_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 518 | struct Scsi_Host *sh = phv->phv_lld_host; |
| 519 | int legacy_mode_enable = 0; |
| 520 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 521 | if (!pdv) { |
| 522 | pr_err("Unable to locate struct pscsi_dev_virt" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 523 | " parameter\n"); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 524 | return ERR_PTR(-EINVAL); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 530 | if (!sh) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 531 | if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 532 | pr_err("pSCSI: Unable to locate struct" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 533 | " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n"); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 534 | return ERR_PTR(-ENODEV); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 535 | } |
| 536 | /* |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 537 | * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 538 | * reference, we enforce that udev_path has been set |
| 539 | */ |
| 540 | if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 541 | pr_err("pSCSI: udev_path attribute has not" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 542 | " been set before ENABLE=1\n"); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 543 | return ERR_PTR(-EINVAL); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 544 | } |
| 545 | /* |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 546 | * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 552 | if (!list_empty(&hba->hba_dev_list)) { |
| 553 | pr_err("pSCSI: Unable to set hba_mode" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 554 | " with active devices\n"); |
| 555 | spin_unlock(&hba->device_lock); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 556 | return ERR_PTR(-EEXIST); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 557 | } |
| 558 | spin_unlock(&hba->device_lock); |
| 559 | |
| 560 | if (pscsi_pmode_enable_hba(hba, 1) != 1) |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 561 | return ERR_PTR(-ENODEV); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 562 | |
| 563 | legacy_mode_enable = 1; |
| 564 | hba->hba_flags |= HBA_FLAGS_PSCSI_MODE; |
| 565 | sh = phv->phv_lld_host; |
| 566 | } else { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 567 | sh = scsi_host_lookup(pdv->pdv_host_id); |
| 568 | if (IS_ERR(sh)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 569 | pr_err("pSCSI: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 570 | " pdv_host_id: %d\n", pdv->pdv_host_id); |
Dan Carpenter | 942d826 | 2011-10-02 01:59:13 +0300 | [diff] [blame] | 571 | return ERR_CAST(sh); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | } else { |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 575 | if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) { |
| 576 | pr_err("pSCSI: PHV_VIRTUAL_HOST_ID set while" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 577 | " struct Scsi_Host exists\n"); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 578 | return ERR_PTR(-EEXIST); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 605 | if (!dev) { |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 606 | if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 613 | return ERR_PTR(-ENODEV); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 614 | } |
| 615 | return dev; |
| 616 | } |
| 617 | spin_unlock_irq(sh->host_lock); |
| 618 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 619 | pr_err("pSCSI: Unable to locate %d:%d:%d:%d\n", sh->host_no, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 620 | pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id); |
| 621 | |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 622 | if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 629 | return ERR_PTR(-ENODEV); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 669 | static int pscsi_transport_complete(struct se_cmd *cmd, struct scatterlist *sg) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 670 | { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 671 | struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 672 | struct scsi_device *sd = pdv->pdv_sd; |
| 673 | int result; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 674 | struct pscsi_plugin_task *pt = cmd->priv; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 684 | if (!cmd->se_deve) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 685 | goto after_mode_sense; |
| 686 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 687 | if (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) { |
| 688 | unsigned char *buf = transport_kmap_data_sg(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 697 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 698 | transport_kunmap_data_sg(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 717 | u16 bdl; |
| 718 | u32 blocksize; |
| 719 | |
| 720 | buf = sg_virt(&sg[0]); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 721 | if (!buf) { |
| 722 | pr_err("Unable to get buf for scatterlist\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Sebastian Andrzej Siewior | 90c161b | 2011-11-23 20:53:17 +0100 | [diff] [blame] | 781 | while ((ptr = strsep(&opts, ",\n")) != NULL) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 789 | pr_err("PSCSI[%d]: Unable to accept" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 798 | pr_debug("PSCSI[%d]: Referencing SCSI Host ID:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 805 | pr_debug("PSCSI[%d]: Referencing SCSI Channel" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 813 | pr_debug("PSCSI[%d]: Referencing SCSI Target" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 821 | pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 844 | pr_err("Missing scsi_channel_id=, scsi_target_id= and" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 845 | " scsi_lun_id= parameters\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 846 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Stefan Hajnoczi | e6a8a41 | 2012-02-23 18:15:26 +0000 | [diff] [blame] | 863 | if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 906 | static inline struct bio *pscsi_get_bio(int sg_num) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 914 | if (!bio) { |
| 915 | pr_err("PSCSI: bio_kmalloc() failed\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 916 | return NULL; |
| 917 | } |
| 918 | bio->bi_end_io = pscsi_bi_endio; |
| 919 | |
| 920 | return bio; |
| 921 | } |
| 922 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 923 | static int pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, |
| 924 | u32 sgl_nents, enum dma_data_direction data_direction, |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 925 | struct bio **hbio) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 926 | { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 927 | struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr; |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 928 | struct bio *bio = NULL, *tbio = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 929 | struct page *page; |
| 930 | struct scatterlist *sg; |
Christoph Hellwig | 7a83aa4 | 2012-04-23 11:35:31 -0400 | [diff] [blame] | 931 | u32 data_len = cmd->data_length, i, len, bytes, off; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 932 | int nr_pages = (cmd->data_length + sgl[0].offset + |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 933 | PAGE_SIZE - 1) >> PAGE_SHIFT; |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 934 | int nr_vecs = 0, rc; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 935 | int rw = (data_direction == DMA_TO_DEVICE); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 936 | |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 937 | *hbio = NULL; |
| 938 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 939 | pr_debug("PSCSI: nr_pages: %d\n", nr_pages); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 940 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 941 | for_each_sg(sgl, sg, sgl_nents, i) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 942 | page = sg_page(sg); |
| 943 | off = sg->offset; |
| 944 | len = sg->length; |
| 945 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 946 | pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 953 | if (!bio) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 959 | bio = pscsi_get_bio(nr_vecs); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 960 | if (!bio) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 961 | goto fail; |
| 962 | |
| 963 | if (rw) |
| 964 | bio->bi_rw |= REQ_WRITE; |
| 965 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 966 | pr_debug("PSCSI: Allocated bio: %p," |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 973 | * command. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 974 | */ |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 975 | if (!*hbio) |
| 976 | *hbio = tbio = bio; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 977 | else |
| 978 | tbio = tbio->bi_next = bio; |
| 979 | } |
| 980 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 981 | pr_debug("PSCSI: Calling bio_add_pc_page() i: %d" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 990 | pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 991 | bio->bi_vcnt, nr_vecs); |
| 992 | |
| 993 | if (bio->bi_vcnt > nr_vecs) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 994 | pr_debug("PSCSI: Reached bio->bi_vcnt max:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1012 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1013 | return sgl_nents; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1014 | fail: |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1015 | while (*hbio) { |
| 1016 | bio = *hbio; |
| 1017 | *hbio = (*hbio)->bi_next; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1018 | bio->bi_next = NULL; |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1019 | bio_endio(bio, 0); /* XXX: should be error */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1020 | } |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1021 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1022 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame^] | 1025 | static int pscsi_parse_cdb(struct se_cmd *cmd) |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1026 | { |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1027 | unsigned char *cdb = cmd->t_task_cdb; |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame^] | 1028 | unsigned int dummy_size; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1029 | int ret; |
| 1030 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame^] | 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; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame^] | 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; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | return 0; |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1070 | } |
| 1071 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1072 | static int pscsi_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl, |
| 1073 | u32 sgl_nents, enum dma_data_direction data_direction) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1074 | { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1075 | struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr; |
| 1076 | struct pscsi_plugin_task *pt; |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1077 | struct request *req; |
| 1078 | struct bio *hbio; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1079 | int ret; |
| 1080 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 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 | |
Christoph Hellwig | ed3102c | 2012-04-23 11:35:29 -0400 | [diff] [blame] | 1092 | memcpy(pt->pscsi_cdb, cmd->t_task_cdb, |
| 1093 | scsi_command_size(cmd->t_task_cdb)); |
Christoph Hellwig | 485fd0d | 2011-10-12 11:09:12 -0400 | [diff] [blame] | 1094 | |
Christoph Hellwig | 64f1db3 | 2012-05-20 11:59:11 -0400 | [diff] [blame] | 1095 | if (!sgl) { |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1096 | req = blk_get_request(pdv->pdv_sd->request_queue, |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1097 | (data_direction == DMA_TO_DEVICE), |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 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); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1102 | cmd->scsi_sense_reason = |
| 1103 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1104 | goto fail; |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1105 | } |
| 1106 | } else { |
Christoph Hellwig | 7a83aa4 | 2012-04-23 11:35:31 -0400 | [diff] [blame] | 1107 | BUG_ON(!cmd->data_length); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1108 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1109 | ret = pscsi_map_sg(cmd, sgl, sgl_nents, data_direction, &hbio); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1110 | if (ret < 0) { |
| 1111 | cmd->scsi_sense_reason = |
| 1112 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1113 | goto fail; |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1114 | } |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1115 | |
| 1116 | req = blk_make_request(pdv->pdv_sd->request_queue, hbio, |
| 1117 | GFP_KERNEL); |
Dan Carpenter | ed327ed | 2011-11-02 08:48:15 -0700 | [diff] [blame] | 1118 | if (IS_ERR(req)) { |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1119 | pr_err("pSCSI: blk_make_request() failed\n"); |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1120 | goto fail_free_bio; |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1121 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1124 | req->cmd_type = REQ_TYPE_BLOCK_PC; |
| 1125 | req->end_io = pscsi_req_done; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1126 | req->end_io_data = cmd; |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 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, |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1138 | (cmd->sam_task_attr == MSG_HEAD_TAG), |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1139 | pscsi_req_done); |
| 1140 | |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1141 | return 0; |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 1142 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1143 | fail_free_bio: |
Christoph Hellwig | 02b1a74 | 2011-09-25 14:56:32 -0400 | [diff] [blame] | 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 | } |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1150 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1151 | fail: |
| 1152 | kfree(pt); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1153 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1156 | static unsigned char *pscsi_get_sense_buffer(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1157 | { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1158 | struct pscsi_plugin_task *pt = cmd->priv; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1159 | |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 1160 | return pt->pscsi_sense; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1198 | static void pscsi_req_done(struct request *req, int uptodate) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1199 | { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 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], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1210 | pt->pscsi_result); |
| 1211 | } |
| 1212 | |
| 1213 | switch (host_byte(pt->pscsi_result)) { |
| 1214 | case DID_OK: |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1215 | target_complete_cmd(cmd, cmd->scsi_status); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1216 | break; |
| 1217 | default: |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1218 | pr_debug("PSCSI Host Byte exception at cmd: %p CDB:" |
| 1219 | " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1220 | pt->pscsi_result); |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1221 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
| 1222 | target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1223 | break; |
| 1224 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1225 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1226 | __blk_put_request(req->q, req); |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1227 | kfree(pt); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static struct se_subsystem_api pscsi_template = { |
| 1231 | .name = "pscsi", |
| 1232 | .owner = THIS_MODULE, |
| 1233 | .transport_type = TRANSPORT_PLUGIN_PHBA_PDEV, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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, |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1241 | .parse_cdb = pscsi_parse_cdb, |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 1242 | .execute_cmd = pscsi_execute_cmd, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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); |