target_core_pscsi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_pscsi.c
  4. *
  5. * This file contains the generic target mode <-> Linux SCSI subsystem plugin.
  6. *
  7. * (c) Copyright 2003-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. ******************************************************************************/
  12. #include <linux/string.h>
  13. #include <linux/parser.h>
  14. #include <linux/timer.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/blk_types.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/cdrom.h>
  20. #include <linux/ratelimit.h>
  21. #include <linux/module.h>
  22. #include <linux/unaligned.h>
  23. #include <scsi/scsi_device.h>
  24. #include <scsi/scsi_host.h>
  25. #include <scsi/scsi_tcq.h>
  26. #include <target/target_core_base.h>
  27. #include <target/target_core_backend.h>
  28. #include "target_core_alua.h"
  29. #include "target_core_internal.h"
  30. #include "target_core_pscsi.h"
  31. static inline struct pscsi_dev_virt *PSCSI_DEV(struct se_device *dev)
  32. {
  33. return container_of(dev, struct pscsi_dev_virt, dev);
  34. }
  35. static sense_reason_t pscsi_execute_cmd(struct se_cmd *cmd);
  36. static enum rq_end_io_ret pscsi_req_done(struct request *, blk_status_t);
  37. /* pscsi_attach_hba():
  38. *
  39. * pscsi_get_sh() used scsi_host_lookup() to locate struct Scsi_Host.
  40. * from the passed SCSI Host ID.
  41. */
  42. static int pscsi_attach_hba(struct se_hba *hba, u32 host_id)
  43. {
  44. struct pscsi_hba_virt *phv;
  45. phv = kzalloc(sizeof(struct pscsi_hba_virt), GFP_KERNEL);
  46. if (!phv) {
  47. pr_err("Unable to allocate struct pscsi_hba_virt\n");
  48. return -ENOMEM;
  49. }
  50. phv->phv_host_id = host_id;
  51. phv->phv_mode = PHV_VIRTUAL_HOST_ID;
  52. hba->hba_ptr = phv;
  53. pr_debug("CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
  54. " Generic Target Core Stack %s\n", hba->hba_id,
  55. PSCSI_VERSION, TARGET_CORE_VERSION);
  56. pr_debug("CORE_HBA[%d] - Attached SCSI HBA to Generic\n",
  57. hba->hba_id);
  58. return 0;
  59. }
  60. static void pscsi_detach_hba(struct se_hba *hba)
  61. {
  62. struct pscsi_hba_virt *phv = hba->hba_ptr;
  63. struct Scsi_Host *scsi_host = phv->phv_lld_host;
  64. if (scsi_host) {
  65. scsi_host_put(scsi_host);
  66. pr_debug("CORE_HBA[%d] - Detached SCSI HBA: %s from"
  67. " Generic Target Core\n", hba->hba_id,
  68. (scsi_host->hostt->name) ? (scsi_host->hostt->name) :
  69. "Unknown");
  70. } else
  71. pr_debug("CORE_HBA[%d] - Detached Virtual SCSI HBA"
  72. " from Generic Target Core\n", hba->hba_id);
  73. kfree(phv);
  74. hba->hba_ptr = NULL;
  75. }
  76. static int pscsi_pmode_enable_hba(struct se_hba *hba, unsigned long mode_flag)
  77. {
  78. struct pscsi_hba_virt *phv = hba->hba_ptr;
  79. struct Scsi_Host *sh = phv->phv_lld_host;
  80. /*
  81. * Release the struct Scsi_Host
  82. */
  83. if (!mode_flag) {
  84. if (!sh)
  85. return 0;
  86. phv->phv_lld_host = NULL;
  87. phv->phv_mode = PHV_VIRTUAL_HOST_ID;
  88. pr_debug("CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
  89. " %s\n", hba->hba_id, (sh->hostt->name) ?
  90. (sh->hostt->name) : "Unknown");
  91. scsi_host_put(sh);
  92. return 0;
  93. }
  94. /*
  95. * Otherwise, locate struct Scsi_Host from the original passed
  96. * pSCSI Host ID and enable for phba mode
  97. */
  98. sh = scsi_host_lookup(phv->phv_host_id);
  99. if (!sh) {
  100. pr_err("pSCSI: Unable to locate SCSI Host for"
  101. " phv_host_id: %d\n", phv->phv_host_id);
  102. return -EINVAL;
  103. }
  104. phv->phv_lld_host = sh;
  105. phv->phv_mode = PHV_LLD_SCSI_HOST_NO;
  106. pr_debug("CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
  107. hba->hba_id, (sh->hostt->name) ? (sh->hostt->name) : "Unknown");
  108. return 1;
  109. }
  110. static void pscsi_tape_read_blocksize(struct se_device *dev,
  111. struct scsi_device *sdev)
  112. {
  113. unsigned char cdb[MAX_COMMAND_SIZE], *buf;
  114. int ret;
  115. buf = kzalloc(12, GFP_KERNEL);
  116. if (!buf)
  117. goto out_free;
  118. memset(cdb, 0, MAX_COMMAND_SIZE);
  119. cdb[0] = MODE_SENSE;
  120. cdb[4] = 0x0c; /* 12 bytes */
  121. ret = scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, buf, 12, HZ, 1, NULL);
  122. if (ret)
  123. goto out_free;
  124. /*
  125. * If MODE_SENSE still returns zero, set the default value to 1024.
  126. */
  127. sdev->sector_size = get_unaligned_be24(&buf[9]);
  128. out_free:
  129. if (!sdev->sector_size)
  130. sdev->sector_size = 1024;
  131. kfree(buf);
  132. }
  133. static void
  134. pscsi_set_inquiry_info(struct scsi_device *sdev, struct t10_wwn *wwn)
  135. {
  136. if (sdev->inquiry_len < INQUIRY_LEN)
  137. return;
  138. /*
  139. * Use sdev->inquiry data from drivers/scsi/scsi_scan.c:scsi_add_lun()
  140. */
  141. BUILD_BUG_ON(sizeof(wwn->vendor) != INQUIRY_VENDOR_LEN + 1);
  142. snprintf(wwn->vendor, sizeof(wwn->vendor),
  143. "%." __stringify(INQUIRY_VENDOR_LEN) "s", sdev->vendor);
  144. BUILD_BUG_ON(sizeof(wwn->model) != INQUIRY_MODEL_LEN + 1);
  145. snprintf(wwn->model, sizeof(wwn->model),
  146. "%." __stringify(INQUIRY_MODEL_LEN) "s", sdev->model);
  147. BUILD_BUG_ON(sizeof(wwn->revision) != INQUIRY_REVISION_LEN + 1);
  148. snprintf(wwn->revision, sizeof(wwn->revision),
  149. "%." __stringify(INQUIRY_REVISION_LEN) "s", sdev->rev);
  150. }
  151. static int
  152. pscsi_get_inquiry_vpd_serial(struct scsi_device *sdev, struct t10_wwn *wwn)
  153. {
  154. unsigned char cdb[MAX_COMMAND_SIZE], *buf;
  155. int ret;
  156. buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
  157. if (!buf)
  158. return -ENOMEM;
  159. memset(cdb, 0, MAX_COMMAND_SIZE);
  160. cdb[0] = INQUIRY;
  161. cdb[1] = 0x01; /* Query VPD */
  162. cdb[2] = 0x80; /* Unit Serial Number */
  163. put_unaligned_be16(INQUIRY_VPD_SERIAL_LEN, &cdb[3]);
  164. ret = scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, buf,
  165. INQUIRY_VPD_SERIAL_LEN, HZ, 1, NULL);
  166. if (ret)
  167. goto out_free;
  168. snprintf(&wwn->unit_serial[0], INQUIRY_VPD_SERIAL_LEN, "%s", &buf[4]);
  169. wwn->t10_dev->dev_flags |= DF_FIRMWARE_VPD_UNIT_SERIAL;
  170. kfree(buf);
  171. return 0;
  172. out_free:
  173. kfree(buf);
  174. return -EPERM;
  175. }
  176. static void
  177. pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
  178. struct t10_wwn *wwn)
  179. {
  180. unsigned char cdb[MAX_COMMAND_SIZE], *buf, *page_83;
  181. int ident_len, page_len, off = 4, ret;
  182. struct t10_vpd *vpd;
  183. buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
  184. if (!buf)
  185. return;
  186. memset(cdb, 0, MAX_COMMAND_SIZE);
  187. cdb[0] = INQUIRY;
  188. cdb[1] = 0x01; /* Query VPD */
  189. cdb[2] = 0x83; /* Device Identifier */
  190. put_unaligned_be16(INQUIRY_VPD_DEVICE_IDENTIFIER_LEN, &cdb[3]);
  191. ret = scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, buf,
  192. INQUIRY_VPD_DEVICE_IDENTIFIER_LEN, HZ, 1, NULL);
  193. if (ret)
  194. goto out;
  195. page_len = get_unaligned_be16(&buf[2]);
  196. while (page_len > 0) {
  197. /* Grab a pointer to the Identification descriptor */
  198. page_83 = &buf[off];
  199. ident_len = page_83[3];
  200. if (!ident_len) {
  201. pr_err("page_83[3]: identifier"
  202. " length zero!\n");
  203. break;
  204. }
  205. pr_debug("T10 VPD Identifier Length: %d\n", ident_len);
  206. vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
  207. if (!vpd) {
  208. pr_err("Unable to allocate memory for"
  209. " struct t10_vpd\n");
  210. goto out;
  211. }
  212. INIT_LIST_HEAD(&vpd->vpd_list);
  213. transport_set_vpd_proto_id(vpd, page_83);
  214. transport_set_vpd_assoc(vpd, page_83);
  215. if (transport_set_vpd_ident_type(vpd, page_83) < 0) {
  216. off += (ident_len + 4);
  217. page_len -= (ident_len + 4);
  218. kfree(vpd);
  219. continue;
  220. }
  221. if (transport_set_vpd_ident(vpd, page_83) < 0) {
  222. off += (ident_len + 4);
  223. page_len -= (ident_len + 4);
  224. kfree(vpd);
  225. continue;
  226. }
  227. list_add_tail(&vpd->vpd_list, &wwn->t10_vpd_list);
  228. off += (ident_len + 4);
  229. page_len -= (ident_len + 4);
  230. }
  231. out:
  232. kfree(buf);
  233. }
  234. static int pscsi_add_device_to_list(struct se_device *dev,
  235. struct scsi_device *sd)
  236. {
  237. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  238. struct request_queue *q = sd->request_queue;
  239. pdv->pdv_sd = sd;
  240. if (!sd->queue_depth) {
  241. sd->queue_depth = PSCSI_DEFAULT_QUEUEDEPTH;
  242. pr_err("Set broken SCSI Device %d:%d:%llu"
  243. " queue_depth to %d\n", sd->channel, sd->id,
  244. sd->lun, sd->queue_depth);
  245. }
  246. dev->dev_attrib.hw_block_size =
  247. min_not_zero((int)sd->sector_size, 512);
  248. dev->dev_attrib.hw_max_sectors =
  249. min_not_zero(sd->host->max_sectors, queue_max_hw_sectors(q));
  250. dev->dev_attrib.hw_queue_depth = sd->queue_depth;
  251. /*
  252. * Setup our standard INQUIRY info into se_dev->t10_wwn
  253. */
  254. pscsi_set_inquiry_info(sd, &dev->t10_wwn);
  255. /*
  256. * Locate VPD WWN Information used for various purposes within
  257. * the Storage Engine.
  258. */
  259. if (!pscsi_get_inquiry_vpd_serial(sd, &dev->t10_wwn)) {
  260. /*
  261. * If VPD Unit Serial returned GOOD status, try
  262. * VPD Device Identification page (0x83).
  263. */
  264. pscsi_get_inquiry_vpd_device_ident(sd, &dev->t10_wwn);
  265. }
  266. /*
  267. * For TYPE_TAPE, attempt to determine blocksize with MODE_SENSE.
  268. */
  269. if (sd->type == TYPE_TAPE) {
  270. pscsi_tape_read_blocksize(dev, sd);
  271. dev->dev_attrib.hw_block_size = sd->sector_size;
  272. }
  273. return 0;
  274. }
  275. static struct se_device *pscsi_alloc_device(struct se_hba *hba,
  276. const char *name)
  277. {
  278. struct pscsi_dev_virt *pdv;
  279. pdv = kzalloc(sizeof(struct pscsi_dev_virt), GFP_KERNEL);
  280. if (!pdv) {
  281. pr_err("Unable to allocate memory for struct pscsi_dev_virt\n");
  282. return NULL;
  283. }
  284. pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv, name);
  285. return &pdv->dev;
  286. }
  287. /*
  288. * Called with struct Scsi_Host->host_lock called.
  289. */
  290. static int pscsi_create_type_disk(struct se_device *dev, struct scsi_device *sd)
  291. __releases(sh->host_lock)
  292. {
  293. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  294. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  295. struct Scsi_Host *sh = sd->host;
  296. struct file *bdev_file;
  297. int ret;
  298. if (scsi_device_get(sd)) {
  299. pr_err("scsi_device_get() failed for %d:%d:%d:%llu\n",
  300. sh->host_no, sd->channel, sd->id, sd->lun);
  301. spin_unlock_irq(sh->host_lock);
  302. return -EIO;
  303. }
  304. spin_unlock_irq(sh->host_lock);
  305. /*
  306. * Claim exclusive struct block_device access to struct scsi_device
  307. * for TYPE_DISK and TYPE_ZBC using supplied udev_path
  308. */
  309. bdev_file = bdev_file_open_by_path(dev->udev_path,
  310. BLK_OPEN_WRITE | BLK_OPEN_READ, pdv, NULL);
  311. if (IS_ERR(bdev_file)) {
  312. pr_err("pSCSI: bdev_file_open_by_path() failed\n");
  313. scsi_device_put(sd);
  314. return PTR_ERR(bdev_file);
  315. }
  316. pdv->pdv_bdev_file = bdev_file;
  317. ret = pscsi_add_device_to_list(dev, sd);
  318. if (ret) {
  319. fput(bdev_file);
  320. scsi_device_put(sd);
  321. return ret;
  322. }
  323. pr_debug("CORE_PSCSI[%d] - Added TYPE_%s for %d:%d:%d:%llu\n",
  324. phv->phv_host_id, sd->type == TYPE_DISK ? "DISK" : "ZBC",
  325. sh->host_no, sd->channel, sd->id, sd->lun);
  326. return 0;
  327. }
  328. /*
  329. * Called with struct Scsi_Host->host_lock called.
  330. */
  331. static int pscsi_create_type_nondisk(struct se_device *dev, struct scsi_device *sd)
  332. __releases(sh->host_lock)
  333. {
  334. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  335. struct Scsi_Host *sh = sd->host;
  336. int ret;
  337. if (scsi_device_get(sd)) {
  338. pr_err("scsi_device_get() failed for %d:%d:%d:%llu\n",
  339. sh->host_no, sd->channel, sd->id, sd->lun);
  340. spin_unlock_irq(sh->host_lock);
  341. return -EIO;
  342. }
  343. spin_unlock_irq(sh->host_lock);
  344. ret = pscsi_add_device_to_list(dev, sd);
  345. if (ret) {
  346. scsi_device_put(sd);
  347. return ret;
  348. }
  349. pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%llu\n",
  350. phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
  351. sd->channel, sd->id, sd->lun);
  352. return 0;
  353. }
  354. static int pscsi_configure_device(struct se_device *dev)
  355. {
  356. struct se_hba *hba = dev->se_hba;
  357. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  358. struct scsi_device *sd;
  359. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  360. struct Scsi_Host *sh = phv->phv_lld_host;
  361. int legacy_mode_enable = 0;
  362. int ret;
  363. if (!(pdv->pdv_flags & PDF_HAS_CHANNEL_ID) ||
  364. !(pdv->pdv_flags & PDF_HAS_TARGET_ID) ||
  365. !(pdv->pdv_flags & PDF_HAS_LUN_ID)) {
  366. pr_err("Missing scsi_channel_id=, scsi_target_id= and"
  367. " scsi_lun_id= parameters\n");
  368. return -EINVAL;
  369. }
  370. /*
  371. * If not running in PHV_LLD_SCSI_HOST_NO mode, locate the
  372. * struct Scsi_Host we will need to bring the TCM/pSCSI object online
  373. */
  374. if (!sh) {
  375. if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
  376. pr_err("pSCSI: Unable to locate struct"
  377. " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
  378. return -ENODEV;
  379. }
  380. /*
  381. * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device
  382. * reference, we enforce that udev_path has been set
  383. */
  384. if (!(dev->dev_flags & DF_USING_UDEV_PATH)) {
  385. pr_err("pSCSI: udev_path attribute has not"
  386. " been set before ENABLE=1\n");
  387. return -EINVAL;
  388. }
  389. /*
  390. * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID,
  391. * use the original TCM hba ID to reference Linux/SCSI Host No
  392. * and enable for PHV_LLD_SCSI_HOST_NO mode.
  393. */
  394. if (!(pdv->pdv_flags & PDF_HAS_VIRT_HOST_ID)) {
  395. if (hba->dev_count) {
  396. pr_err("pSCSI: Unable to set hba_mode"
  397. " with active devices\n");
  398. return -EEXIST;
  399. }
  400. if (pscsi_pmode_enable_hba(hba, 1) != 1)
  401. return -ENODEV;
  402. legacy_mode_enable = 1;
  403. hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
  404. sh = phv->phv_lld_host;
  405. } else {
  406. sh = scsi_host_lookup(pdv->pdv_host_id);
  407. if (!sh) {
  408. pr_err("pSCSI: Unable to locate"
  409. " pdv_host_id: %d\n", pdv->pdv_host_id);
  410. return -EINVAL;
  411. }
  412. pdv->pdv_lld_host = sh;
  413. }
  414. } else {
  415. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) {
  416. pr_err("pSCSI: PHV_VIRTUAL_HOST_ID set while"
  417. " struct Scsi_Host exists\n");
  418. return -EEXIST;
  419. }
  420. }
  421. spin_lock_irq(sh->host_lock);
  422. list_for_each_entry(sd, &sh->__devices, siblings) {
  423. if ((pdv->pdv_channel_id != sd->channel) ||
  424. (pdv->pdv_target_id != sd->id) ||
  425. (pdv->pdv_lun_id != sd->lun))
  426. continue;
  427. /*
  428. * Functions will release the held struct scsi_host->host_lock
  429. * before calling pscsi_add_device_to_list() to register
  430. * struct scsi_device with target_core_mod.
  431. */
  432. switch (sd->type) {
  433. case TYPE_DISK:
  434. case TYPE_ZBC:
  435. ret = pscsi_create_type_disk(dev, sd);
  436. break;
  437. default:
  438. ret = pscsi_create_type_nondisk(dev, sd);
  439. break;
  440. }
  441. if (ret) {
  442. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
  443. scsi_host_put(sh);
  444. else if (legacy_mode_enable) {
  445. pscsi_pmode_enable_hba(hba, 0);
  446. hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
  447. }
  448. pdv->pdv_sd = NULL;
  449. return ret;
  450. }
  451. return 0;
  452. }
  453. spin_unlock_irq(sh->host_lock);
  454. pr_err("pSCSI: Unable to locate %d:%d:%d:%d\n", sh->host_no,
  455. pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id);
  456. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
  457. scsi_host_put(sh);
  458. else if (legacy_mode_enable) {
  459. pscsi_pmode_enable_hba(hba, 0);
  460. hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
  461. }
  462. return -ENODEV;
  463. }
  464. static void pscsi_dev_call_rcu(struct rcu_head *p)
  465. {
  466. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  467. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  468. kfree(pdv);
  469. }
  470. static void pscsi_free_device(struct se_device *dev)
  471. {
  472. call_rcu(&dev->rcu_head, pscsi_dev_call_rcu);
  473. }
  474. static void pscsi_destroy_device(struct se_device *dev)
  475. {
  476. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  477. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  478. struct scsi_device *sd = pdv->pdv_sd;
  479. if (sd) {
  480. /*
  481. * Release exclusive pSCSI internal struct block_device claim for
  482. * struct scsi_device with TYPE_DISK or TYPE_ZBC
  483. * from pscsi_create_type_disk()
  484. */
  485. if ((sd->type == TYPE_DISK || sd->type == TYPE_ZBC) &&
  486. pdv->pdv_bdev_file) {
  487. fput(pdv->pdv_bdev_file);
  488. pdv->pdv_bdev_file = NULL;
  489. }
  490. /*
  491. * For HBA mode PHV_LLD_SCSI_HOST_NO, release the reference
  492. * to struct Scsi_Host now.
  493. */
  494. if ((phv->phv_mode == PHV_LLD_SCSI_HOST_NO) &&
  495. (phv->phv_lld_host != NULL))
  496. scsi_host_put(phv->phv_lld_host);
  497. else if (pdv->pdv_lld_host)
  498. scsi_host_put(pdv->pdv_lld_host);
  499. scsi_device_put(sd);
  500. pdv->pdv_sd = NULL;
  501. }
  502. }
  503. static void pscsi_complete_cmd(struct se_cmd *cmd, u8 scsi_status,
  504. unsigned char *req_sense, int valid_data)
  505. {
  506. struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
  507. struct scsi_device *sd = pdv->pdv_sd;
  508. unsigned char *cdb = cmd->priv;
  509. /*
  510. * Special case for REPORT_LUNs which is emulated and not passed on.
  511. */
  512. if (!cdb)
  513. return;
  514. /*
  515. * Hack to make sure that Write-Protect modepage is set if R/O mode is
  516. * forced.
  517. */
  518. if (!cmd->data_length)
  519. goto after_mode_sense;
  520. if (((cdb[0] == MODE_SENSE) || (cdb[0] == MODE_SENSE_10)) &&
  521. scsi_status == SAM_STAT_GOOD) {
  522. bool read_only = target_lun_is_rdonly(cmd);
  523. if (read_only) {
  524. unsigned char *buf;
  525. buf = transport_kmap_data_sg(cmd);
  526. if (!buf) {
  527. ; /* XXX: TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE */
  528. } else {
  529. if (cdb[0] == MODE_SENSE_10) {
  530. if (!(buf[3] & 0x80))
  531. buf[3] |= 0x80;
  532. } else {
  533. if (!(buf[2] & 0x80))
  534. buf[2] |= 0x80;
  535. }
  536. transport_kunmap_data_sg(cmd);
  537. }
  538. }
  539. }
  540. after_mode_sense:
  541. if (sd->type != TYPE_TAPE || !cmd->data_length)
  542. goto after_mode_select;
  543. /*
  544. * Hack to correctly obtain the initiator requested blocksize for
  545. * TYPE_TAPE. Since this value is dependent upon each tape media,
  546. * struct scsi_device->sector_size will not contain the correct value
  547. * by default, so we go ahead and set it so
  548. * TRANSPORT(dev)->get_blockdev() returns the correct value to the
  549. * storage engine.
  550. */
  551. if (((cdb[0] == MODE_SELECT) || (cdb[0] == MODE_SELECT_10)) &&
  552. scsi_status == SAM_STAT_GOOD) {
  553. unsigned char *buf;
  554. u16 bdl;
  555. u32 blocksize;
  556. buf = sg_virt(&cmd->t_data_sg[0]);
  557. if (!buf) {
  558. pr_err("Unable to get buf for scatterlist\n");
  559. goto after_mode_select;
  560. }
  561. if (cdb[0] == MODE_SELECT)
  562. bdl = buf[3];
  563. else
  564. bdl = get_unaligned_be16(&buf[6]);
  565. if (!bdl)
  566. goto after_mode_select;
  567. if (cdb[0] == MODE_SELECT)
  568. blocksize = get_unaligned_be24(&buf[9]);
  569. else
  570. blocksize = get_unaligned_be24(&buf[13]);
  571. sd->sector_size = blocksize;
  572. }
  573. after_mode_select:
  574. if (scsi_status == SAM_STAT_CHECK_CONDITION) {
  575. transport_copy_sense_to_cmd(cmd, req_sense);
  576. /*
  577. * check for TAPE device reads with
  578. * FM/EOM/ILI set, so that we can get data
  579. * back despite framework assumption that a
  580. * check condition means there is no data
  581. */
  582. if (sd->type == TYPE_TAPE && valid_data &&
  583. cmd->data_direction == DMA_FROM_DEVICE) {
  584. /*
  585. * is sense data valid, fixed format,
  586. * and have FM, EOM, or ILI set?
  587. */
  588. if (req_sense[0] == 0xf0 && /* valid, fixed format */
  589. req_sense[2] & 0xe0 && /* FM, EOM, or ILI */
  590. (req_sense[2] & 0xf) == 0) { /* key==NO_SENSE */
  591. pr_debug("Tape FM/EOM/ILI status detected. Treat as normal read.\n");
  592. cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL;
  593. }
  594. }
  595. }
  596. }
  597. enum {
  598. Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
  599. Opt_scsi_lun_id, Opt_err
  600. };
  601. static match_table_t tokens = {
  602. {Opt_scsi_host_id, "scsi_host_id=%d"},
  603. {Opt_scsi_channel_id, "scsi_channel_id=%d"},
  604. {Opt_scsi_target_id, "scsi_target_id=%d"},
  605. {Opt_scsi_lun_id, "scsi_lun_id=%d"},
  606. {Opt_err, NULL}
  607. };
  608. static ssize_t pscsi_set_configfs_dev_params(struct se_device *dev,
  609. const char *page, ssize_t count)
  610. {
  611. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  612. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  613. char *orig, *ptr, *opts;
  614. substring_t args[MAX_OPT_ARGS];
  615. int ret = 0, arg, token;
  616. opts = kstrdup(page, GFP_KERNEL);
  617. if (!opts)
  618. return -ENOMEM;
  619. orig = opts;
  620. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  621. if (!*ptr)
  622. continue;
  623. token = match_token(ptr, tokens, args);
  624. switch (token) {
  625. case Opt_scsi_host_id:
  626. if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
  627. pr_err("PSCSI[%d]: Unable to accept"
  628. " scsi_host_id while phv_mode =="
  629. " PHV_LLD_SCSI_HOST_NO\n",
  630. phv->phv_host_id);
  631. ret = -EINVAL;
  632. goto out;
  633. }
  634. ret = match_int(args, &arg);
  635. if (ret)
  636. goto out;
  637. pdv->pdv_host_id = arg;
  638. pr_debug("PSCSI[%d]: Referencing SCSI Host ID:"
  639. " %d\n", phv->phv_host_id, pdv->pdv_host_id);
  640. pdv->pdv_flags |= PDF_HAS_VIRT_HOST_ID;
  641. break;
  642. case Opt_scsi_channel_id:
  643. ret = match_int(args, &arg);
  644. if (ret)
  645. goto out;
  646. pdv->pdv_channel_id = arg;
  647. pr_debug("PSCSI[%d]: Referencing SCSI Channel"
  648. " ID: %d\n", phv->phv_host_id,
  649. pdv->pdv_channel_id);
  650. pdv->pdv_flags |= PDF_HAS_CHANNEL_ID;
  651. break;
  652. case Opt_scsi_target_id:
  653. ret = match_int(args, &arg);
  654. if (ret)
  655. goto out;
  656. pdv->pdv_target_id = arg;
  657. pr_debug("PSCSI[%d]: Referencing SCSI Target"
  658. " ID: %d\n", phv->phv_host_id,
  659. pdv->pdv_target_id);
  660. pdv->pdv_flags |= PDF_HAS_TARGET_ID;
  661. break;
  662. case Opt_scsi_lun_id:
  663. ret = match_int(args, &arg);
  664. if (ret)
  665. goto out;
  666. pdv->pdv_lun_id = arg;
  667. pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:"
  668. " %d\n", phv->phv_host_id, pdv->pdv_lun_id);
  669. pdv->pdv_flags |= PDF_HAS_LUN_ID;
  670. break;
  671. default:
  672. break;
  673. }
  674. }
  675. out:
  676. kfree(orig);
  677. return (!ret) ? count : ret;
  678. }
  679. static ssize_t pscsi_show_configfs_dev_params(struct se_device *dev, char *b)
  680. {
  681. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  682. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  683. struct scsi_device *sd = pdv->pdv_sd;
  684. unsigned char host_id[16];
  685. ssize_t bl;
  686. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
  687. snprintf(host_id, 16, "%d", pdv->pdv_host_id);
  688. else
  689. snprintf(host_id, 16, "PHBA Mode");
  690. bl = sprintf(b, "SCSI Device Bus Location:"
  691. " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
  692. pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id,
  693. host_id);
  694. if (sd) {
  695. bl += sprintf(b + bl, " Vendor: %."
  696. __stringify(INQUIRY_VENDOR_LEN) "s", sd->vendor);
  697. bl += sprintf(b + bl, " Model: %."
  698. __stringify(INQUIRY_MODEL_LEN) "s", sd->model);
  699. bl += sprintf(b + bl, " Rev: %."
  700. __stringify(INQUIRY_REVISION_LEN) "s\n", sd->rev);
  701. }
  702. return bl;
  703. }
  704. static void pscsi_bi_endio(struct bio *bio)
  705. {
  706. bio_uninit(bio);
  707. kfree(bio);
  708. }
  709. static sense_reason_t
  710. pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  711. struct request *req)
  712. {
  713. struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
  714. struct bio *bio = NULL;
  715. struct page *page;
  716. struct scatterlist *sg;
  717. u32 data_len = cmd->data_length, i, len, bytes, off;
  718. int nr_pages = (cmd->data_length + sgl[0].offset +
  719. PAGE_SIZE - 1) >> PAGE_SHIFT;
  720. int nr_vecs = 0, rc;
  721. int rw = (cmd->data_direction == DMA_TO_DEVICE);
  722. BUG_ON(!cmd->data_length);
  723. pr_debug("PSCSI: nr_pages: %d\n", nr_pages);
  724. for_each_sg(sgl, sg, sgl_nents, i) {
  725. page = sg_page(sg);
  726. off = sg->offset;
  727. len = sg->length;
  728. pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i,
  729. page, len, off);
  730. /*
  731. * We only have one page of data in each sg element,
  732. * we can not cross a page boundary.
  733. */
  734. if (off + len > PAGE_SIZE)
  735. goto fail;
  736. if (len > 0 && data_len > 0) {
  737. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  738. bytes = min(bytes, data_len);
  739. if (!bio) {
  740. new_bio:
  741. nr_vecs = bio_max_segs(nr_pages);
  742. bio = bio_kmalloc(nr_vecs, GFP_KERNEL);
  743. if (!bio)
  744. goto fail;
  745. bio_init(bio, NULL, bio->bi_inline_vecs, nr_vecs,
  746. rw ? REQ_OP_WRITE : REQ_OP_READ);
  747. bio->bi_end_io = pscsi_bi_endio;
  748. pr_debug("PSCSI: Allocated bio: %p,"
  749. " dir: %s nr_vecs: %d\n", bio,
  750. (rw) ? "rw" : "r", nr_vecs);
  751. }
  752. pr_debug("PSCSI: Calling bio_add_pc_page() i: %d"
  753. " bio: %p page: %p len: %d off: %d\n", i, bio,
  754. page, len, off);
  755. rc = bio_add_pc_page(pdv->pdv_sd->request_queue,
  756. bio, page, bytes, off);
  757. pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
  758. bio_segments(bio), nr_vecs);
  759. if (rc != bytes) {
  760. pr_debug("PSCSI: Reached bio->bi_vcnt max:"
  761. " %d i: %d bio: %p, allocating another"
  762. " bio\n", bio->bi_vcnt, i, bio);
  763. rc = blk_rq_append_bio(req, bio);
  764. if (rc) {
  765. pr_err("pSCSI: failed to append bio\n");
  766. goto fail;
  767. }
  768. goto new_bio;
  769. }
  770. data_len -= bytes;
  771. }
  772. }
  773. if (bio) {
  774. rc = blk_rq_append_bio(req, bio);
  775. if (rc) {
  776. pr_err("pSCSI: failed to append bio\n");
  777. goto fail;
  778. }
  779. }
  780. return 0;
  781. fail:
  782. if (bio) {
  783. bio_uninit(bio);
  784. kfree(bio);
  785. }
  786. while (req->bio) {
  787. bio = req->bio;
  788. req->bio = bio->bi_next;
  789. bio_uninit(bio);
  790. kfree(bio);
  791. }
  792. req->biotail = NULL;
  793. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  794. }
  795. static sense_reason_t
  796. pscsi_parse_cdb(struct se_cmd *cmd)
  797. {
  798. if (cmd->se_cmd_flags & SCF_BIDI)
  799. return TCM_UNSUPPORTED_SCSI_OPCODE;
  800. return passthrough_parse_cdb(cmd, pscsi_execute_cmd);
  801. }
  802. static sense_reason_t
  803. pscsi_execute_cmd(struct se_cmd *cmd)
  804. {
  805. struct scatterlist *sgl = cmd->t_data_sg;
  806. u32 sgl_nents = cmd->t_data_nents;
  807. struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
  808. struct scsi_cmnd *scmd;
  809. struct request *req;
  810. sense_reason_t ret;
  811. req = scsi_alloc_request(pdv->pdv_sd->request_queue,
  812. cmd->data_direction == DMA_TO_DEVICE ?
  813. REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
  814. if (IS_ERR(req))
  815. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  816. if (sgl) {
  817. ret = pscsi_map_sg(cmd, sgl, sgl_nents, req);
  818. if (ret)
  819. goto fail_put_request;
  820. }
  821. req->end_io = pscsi_req_done;
  822. req->end_io_data = cmd;
  823. scmd = blk_mq_rq_to_pdu(req);
  824. scmd->cmd_len = scsi_command_size(cmd->t_task_cdb);
  825. if (scmd->cmd_len > sizeof(scmd->cmnd)) {
  826. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  827. goto fail_put_request;
  828. }
  829. memcpy(scmd->cmnd, cmd->t_task_cdb, scmd->cmd_len);
  830. if (pdv->pdv_sd->type == TYPE_DISK ||
  831. pdv->pdv_sd->type == TYPE_ZBC)
  832. req->timeout = PS_TIMEOUT_DISK;
  833. else
  834. req->timeout = PS_TIMEOUT_OTHER;
  835. scmd->allowed = PS_RETRY;
  836. cmd->priv = scmd->cmnd;
  837. blk_execute_rq_nowait(req, cmd->sam_task_attr == TCM_HEAD_TAG);
  838. return 0;
  839. fail_put_request:
  840. blk_mq_free_request(req);
  841. return ret;
  842. }
  843. /* pscsi_get_device_type():
  844. *
  845. *
  846. */
  847. static u32 pscsi_get_device_type(struct se_device *dev)
  848. {
  849. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  850. struct scsi_device *sd = pdv->pdv_sd;
  851. return (sd) ? sd->type : TYPE_NO_LUN;
  852. }
  853. static sector_t pscsi_get_blocks(struct se_device *dev)
  854. {
  855. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  856. if (pdv->pdv_bdev_file)
  857. return bdev_nr_sectors(file_bdev(pdv->pdv_bdev_file));
  858. return 0;
  859. }
  860. static enum rq_end_io_ret pscsi_req_done(struct request *req,
  861. blk_status_t status)
  862. {
  863. struct se_cmd *cmd = req->end_io_data;
  864. struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
  865. enum sam_status scsi_status = scmd->result & 0xff;
  866. int valid_data = cmd->data_length - scmd->resid_len;
  867. u8 *cdb = cmd->priv;
  868. if (scsi_status != SAM_STAT_GOOD) {
  869. pr_debug("PSCSI Status Byte exception at cmd: %p CDB:"
  870. " 0x%02x Result: 0x%08x\n", cmd, cdb[0], scmd->result);
  871. }
  872. pscsi_complete_cmd(cmd, scsi_status, scmd->sense_buffer, valid_data);
  873. switch (host_byte(scmd->result)) {
  874. case DID_OK:
  875. target_complete_cmd_with_length(cmd, scsi_status, valid_data);
  876. break;
  877. default:
  878. pr_debug("PSCSI Host Byte exception at cmd: %p CDB:"
  879. " 0x%02x Result: 0x%08x\n", cmd, cdb[0], scmd->result);
  880. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  881. break;
  882. }
  883. blk_mq_free_request(req);
  884. return RQ_END_IO_NONE;
  885. }
  886. static const struct target_backend_ops pscsi_ops = {
  887. .name = "pscsi",
  888. .owner = THIS_MODULE,
  889. .transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH |
  890. TRANSPORT_FLAG_PASSTHROUGH_ALUA |
  891. TRANSPORT_FLAG_PASSTHROUGH_PGR,
  892. .attach_hba = pscsi_attach_hba,
  893. .detach_hba = pscsi_detach_hba,
  894. .pmode_enable_hba = pscsi_pmode_enable_hba,
  895. .alloc_device = pscsi_alloc_device,
  896. .configure_device = pscsi_configure_device,
  897. .destroy_device = pscsi_destroy_device,
  898. .free_device = pscsi_free_device,
  899. .parse_cdb = pscsi_parse_cdb,
  900. .set_configfs_dev_params = pscsi_set_configfs_dev_params,
  901. .show_configfs_dev_params = pscsi_show_configfs_dev_params,
  902. .get_device_type = pscsi_get_device_type,
  903. .get_blocks = pscsi_get_blocks,
  904. .tb_dev_attrib_attrs = passthrough_attrib_attrs,
  905. };
  906. static int __init pscsi_module_init(void)
  907. {
  908. return transport_backend_register(&pscsi_ops);
  909. }
  910. static void __exit pscsi_module_exit(void)
  911. {
  912. target_backend_unregister(&pscsi_ops);
  913. }
  914. MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
  915. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  916. MODULE_LICENSE("GPL");
  917. module_init(pscsi_module_init);
  918. module_exit(pscsi_module_exit);