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