target_core_pscsi.c 28 KB

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