target_core_pscsi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 *, int);
  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 = (buf[9] << 16) | (buf[10] << 8) | (buf[11]);
  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. cdb[3] = (INQUIRY_VPD_SERIAL_LEN >> 8) & 0xff;
  178. cdb[4] = (INQUIRY_VPD_SERIAL_LEN & 0xff);
  179. ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
  180. INQUIRY_VPD_SERIAL_LEN, NULL, HZ, 1, NULL);
  181. if (ret)
  182. goto out_free;
  183. snprintf(&wwn->unit_serial[0], INQUIRY_VPD_SERIAL_LEN, "%s", &buf[4]);
  184. wwn->t10_dev->dev_flags |= DF_FIRMWARE_VPD_UNIT_SERIAL;
  185. kfree(buf);
  186. return 0;
  187. out_free:
  188. kfree(buf);
  189. return -EPERM;
  190. }
  191. static void
  192. pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
  193. struct t10_wwn *wwn)
  194. {
  195. unsigned char cdb[MAX_COMMAND_SIZE], *buf, *page_83;
  196. int ident_len, page_len, off = 4, ret;
  197. struct t10_vpd *vpd;
  198. buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
  199. if (!buf)
  200. return;
  201. memset(cdb, 0, MAX_COMMAND_SIZE);
  202. cdb[0] = INQUIRY;
  203. cdb[1] = 0x01; /* Query VPD */
  204. cdb[2] = 0x83; /* Device Identifier */
  205. cdb[3] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN >> 8) & 0xff;
  206. cdb[4] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN & 0xff);
  207. ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
  208. INQUIRY_VPD_DEVICE_IDENTIFIER_LEN,
  209. NULL, HZ, 1, NULL);
  210. if (ret)
  211. goto out;
  212. page_len = (buf[2] << 8) | buf[3];
  213. while (page_len > 0) {
  214. /* Grab a pointer to the Identification descriptor */
  215. page_83 = &buf[off];
  216. ident_len = page_83[3];
  217. if (!ident_len) {
  218. pr_err("page_83[3]: identifier"
  219. " length zero!\n");
  220. break;
  221. }
  222. pr_debug("T10 VPD Identifier Length: %d\n", ident_len);
  223. vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
  224. if (!vpd) {
  225. pr_err("Unable to allocate memory for"
  226. " struct t10_vpd\n");
  227. goto out;
  228. }
  229. INIT_LIST_HEAD(&vpd->vpd_list);
  230. transport_set_vpd_proto_id(vpd, page_83);
  231. transport_set_vpd_assoc(vpd, page_83);
  232. if (transport_set_vpd_ident_type(vpd, page_83) < 0) {
  233. off += (ident_len + 4);
  234. page_len -= (ident_len + 4);
  235. kfree(vpd);
  236. continue;
  237. }
  238. if (transport_set_vpd_ident(vpd, page_83) < 0) {
  239. off += (ident_len + 4);
  240. page_len -= (ident_len + 4);
  241. kfree(vpd);
  242. continue;
  243. }
  244. list_add_tail(&vpd->vpd_list, &wwn->t10_vpd_list);
  245. off += (ident_len + 4);
  246. page_len -= (ident_len + 4);
  247. }
  248. out:
  249. kfree(buf);
  250. }
  251. static int pscsi_add_device_to_list(struct se_device *dev,
  252. struct scsi_device *sd)
  253. {
  254. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  255. struct request_queue *q = sd->request_queue;
  256. pdv->pdv_sd = sd;
  257. if (!sd->queue_depth) {
  258. sd->queue_depth = PSCSI_DEFAULT_QUEUEDEPTH;
  259. pr_err("Set broken SCSI Device %d:%d:%llu"
  260. " queue_depth to %d\n", sd->channel, sd->id,
  261. sd->lun, sd->queue_depth);
  262. }
  263. dev->dev_attrib.hw_block_size =
  264. min_not_zero((int)sd->sector_size, 512);
  265. dev->dev_attrib.hw_max_sectors =
  266. min_not_zero(sd->host->max_sectors, queue_max_hw_sectors(q));
  267. dev->dev_attrib.hw_queue_depth = sd->queue_depth;
  268. /*
  269. * Setup our standard INQUIRY info into se_dev->t10_wwn
  270. */
  271. pscsi_set_inquiry_info(sd, &dev->t10_wwn);
  272. /*
  273. * Locate VPD WWN Information used for various purposes within
  274. * the Storage Engine.
  275. */
  276. if (!pscsi_get_inquiry_vpd_serial(sd, &dev->t10_wwn)) {
  277. /*
  278. * If VPD Unit Serial returned GOOD status, try
  279. * VPD Device Identification page (0x83).
  280. */
  281. pscsi_get_inquiry_vpd_device_ident(sd, &dev->t10_wwn);
  282. }
  283. /*
  284. * For TYPE_TAPE, attempt to determine blocksize with MODE_SENSE.
  285. */
  286. if (sd->type == TYPE_TAPE) {
  287. pscsi_tape_read_blocksize(dev, sd);
  288. dev->dev_attrib.hw_block_size = sd->sector_size;
  289. }
  290. return 0;
  291. }
  292. static struct se_device *pscsi_alloc_device(struct se_hba *hba,
  293. const char *name)
  294. {
  295. struct pscsi_dev_virt *pdv;
  296. pdv = kzalloc(sizeof(struct pscsi_dev_virt), GFP_KERNEL);
  297. if (!pdv) {
  298. pr_err("Unable to allocate memory for struct pscsi_dev_virt\n");
  299. return NULL;
  300. }
  301. pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv, name);
  302. return &pdv->dev;
  303. }
  304. /*
  305. * Called with struct Scsi_Host->host_lock called.
  306. */
  307. static int pscsi_create_type_disk(struct se_device *dev, struct scsi_device *sd)
  308. __releases(sh->host_lock)
  309. {
  310. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  311. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  312. struct Scsi_Host *sh = sd->host;
  313. struct block_device *bd;
  314. int ret;
  315. if (scsi_device_get(sd)) {
  316. pr_err("scsi_device_get() failed for %d:%d:%d:%llu\n",
  317. sh->host_no, sd->channel, sd->id, sd->lun);
  318. spin_unlock_irq(sh->host_lock);
  319. return -EIO;
  320. }
  321. spin_unlock_irq(sh->host_lock);
  322. /*
  323. * Claim exclusive struct block_device access to struct scsi_device
  324. * for TYPE_DISK using supplied udev_path
  325. */
  326. bd = blkdev_get_by_path(dev->udev_path,
  327. FMODE_WRITE|FMODE_READ|FMODE_EXCL, pdv);
  328. if (IS_ERR(bd)) {
  329. pr_err("pSCSI: blkdev_get_by_path() failed\n");
  330. scsi_device_put(sd);
  331. return PTR_ERR(bd);
  332. }
  333. pdv->pdv_bd = bd;
  334. ret = pscsi_add_device_to_list(dev, sd);
  335. if (ret) {
  336. blkdev_put(pdv->pdv_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  337. scsi_device_put(sd);
  338. return ret;
  339. }
  340. pr_debug("CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%llu\n",
  341. phv->phv_host_id, sh->host_no, sd->channel, sd->id, sd->lun);
  342. return 0;
  343. }
  344. /*
  345. * Called with struct Scsi_Host->host_lock called.
  346. */
  347. static int pscsi_create_type_nondisk(struct se_device *dev, struct scsi_device *sd)
  348. __releases(sh->host_lock)
  349. {
  350. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  351. struct Scsi_Host *sh = sd->host;
  352. int ret;
  353. if (scsi_device_get(sd)) {
  354. pr_err("scsi_device_get() failed for %d:%d:%d:%llu\n",
  355. sh->host_no, sd->channel, sd->id, sd->lun);
  356. spin_unlock_irq(sh->host_lock);
  357. return -EIO;
  358. }
  359. spin_unlock_irq(sh->host_lock);
  360. ret = pscsi_add_device_to_list(dev, sd);
  361. if (ret) {
  362. scsi_device_put(sd);
  363. return ret;
  364. }
  365. pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%llu\n",
  366. phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
  367. sd->channel, sd->id, sd->lun);
  368. return 0;
  369. }
  370. static int pscsi_configure_device(struct se_device *dev)
  371. {
  372. struct se_hba *hba = dev->se_hba;
  373. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  374. struct scsi_device *sd;
  375. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  376. struct Scsi_Host *sh = phv->phv_lld_host;
  377. int legacy_mode_enable = 0;
  378. int ret;
  379. if (!(pdv->pdv_flags & PDF_HAS_CHANNEL_ID) ||
  380. !(pdv->pdv_flags & PDF_HAS_TARGET_ID) ||
  381. !(pdv->pdv_flags & PDF_HAS_LUN_ID)) {
  382. pr_err("Missing scsi_channel_id=, scsi_target_id= and"
  383. " scsi_lun_id= parameters\n");
  384. return -EINVAL;
  385. }
  386. /*
  387. * If not running in PHV_LLD_SCSI_HOST_NO mode, locate the
  388. * struct Scsi_Host we will need to bring the TCM/pSCSI object online
  389. */
  390. if (!sh) {
  391. if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
  392. pr_err("pSCSI: Unable to locate struct"
  393. " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
  394. return -ENODEV;
  395. }
  396. /*
  397. * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device
  398. * reference, we enforce that udev_path has been set
  399. */
  400. if (!(dev->dev_flags & DF_USING_UDEV_PATH)) {
  401. pr_err("pSCSI: udev_path attribute has not"
  402. " been set before ENABLE=1\n");
  403. return -EINVAL;
  404. }
  405. /*
  406. * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID,
  407. * use the original TCM hba ID to reference Linux/SCSI Host No
  408. * and enable for PHV_LLD_SCSI_HOST_NO mode.
  409. */
  410. if (!(pdv->pdv_flags & PDF_HAS_VIRT_HOST_ID)) {
  411. if (hba->dev_count) {
  412. pr_err("pSCSI: Unable to set hba_mode"
  413. " with active devices\n");
  414. return -EEXIST;
  415. }
  416. if (pscsi_pmode_enable_hba(hba, 1) != 1)
  417. return -ENODEV;
  418. legacy_mode_enable = 1;
  419. hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
  420. sh = phv->phv_lld_host;
  421. } else {
  422. sh = scsi_host_lookup(pdv->pdv_host_id);
  423. if (!sh) {
  424. pr_err("pSCSI: Unable to locate"
  425. " pdv_host_id: %d\n", pdv->pdv_host_id);
  426. return -EINVAL;
  427. }
  428. pdv->pdv_lld_host = sh;
  429. }
  430. } else {
  431. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) {
  432. pr_err("pSCSI: PHV_VIRTUAL_HOST_ID set while"
  433. " struct Scsi_Host exists\n");
  434. return -EEXIST;
  435. }
  436. }
  437. spin_lock_irq(sh->host_lock);
  438. list_for_each_entry(sd, &sh->__devices, siblings) {
  439. if ((pdv->pdv_channel_id != sd->channel) ||
  440. (pdv->pdv_target_id != sd->id) ||
  441. (pdv->pdv_lun_id != sd->lun))
  442. continue;
  443. /*
  444. * Functions will release the held struct scsi_host->host_lock
  445. * before calling calling pscsi_add_device_to_list() to register
  446. * struct scsi_device with target_core_mod.
  447. */
  448. switch (sd->type) {
  449. case TYPE_DISK:
  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. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  488. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  489. struct scsi_device *sd = pdv->pdv_sd;
  490. if (sd) {
  491. /*
  492. * Release exclusive pSCSI internal struct block_device claim for
  493. * struct scsi_device with TYPE_DISK from pscsi_create_type_disk()
  494. */
  495. if ((sd->type == TYPE_DISK) && pdv->pdv_bd) {
  496. blkdev_put(pdv->pdv_bd,
  497. FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  498. pdv->pdv_bd = NULL;
  499. }
  500. /*
  501. * For HBA mode PHV_LLD_SCSI_HOST_NO, release the reference
  502. * to struct Scsi_Host now.
  503. */
  504. if ((phv->phv_mode == PHV_LLD_SCSI_HOST_NO) &&
  505. (phv->phv_lld_host != NULL))
  506. scsi_host_put(phv->phv_lld_host);
  507. else if (pdv->pdv_lld_host)
  508. scsi_host_put(pdv->pdv_lld_host);
  509. scsi_device_put(sd);
  510. pdv->pdv_sd = NULL;
  511. }
  512. call_rcu(&dev->rcu_head, pscsi_dev_call_rcu);
  513. }
  514. static void pscsi_transport_complete(struct se_cmd *cmd, struct scatterlist *sg,
  515. unsigned char *sense_buffer)
  516. {
  517. struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
  518. struct scsi_device *sd = pdv->pdv_sd;
  519. int result;
  520. struct pscsi_plugin_task *pt = cmd->priv;
  521. unsigned char *cdb;
  522. /*
  523. * Special case for REPORT_LUNs handling where pscsi_plugin_task has
  524. * not been allocated because TCM is handling the emulation directly.
  525. */
  526. if (!pt)
  527. return;
  528. cdb = &pt->pscsi_cdb[0];
  529. result = pt->pscsi_result;
  530. /*
  531. * Hack to make sure that Write-Protect modepage is set if R/O mode is
  532. * forced.
  533. */
  534. if (!cmd->data_length)
  535. goto after_mode_sense;
  536. if (((cdb[0] == MODE_SENSE) || (cdb[0] == MODE_SENSE_10)) &&
  537. (status_byte(result) << 1) == SAM_STAT_GOOD) {
  538. bool read_only = target_lun_is_rdonly(cmd);
  539. if (read_only) {
  540. unsigned char *buf;
  541. buf = transport_kmap_data_sg(cmd);
  542. if (!buf)
  543. ; /* XXX: TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE */
  544. if (cdb[0] == MODE_SENSE_10) {
  545. if (!(buf[3] & 0x80))
  546. buf[3] |= 0x80;
  547. } else {
  548. if (!(buf[2] & 0x80))
  549. buf[2] |= 0x80;
  550. }
  551. transport_kunmap_data_sg(cmd);
  552. }
  553. }
  554. after_mode_sense:
  555. if (sd->type != TYPE_TAPE || !cmd->data_length)
  556. goto after_mode_select;
  557. /*
  558. * Hack to correctly obtain the initiator requested blocksize for
  559. * TYPE_TAPE. Since this value is dependent upon each tape media,
  560. * struct scsi_device->sector_size will not contain the correct value
  561. * by default, so we go ahead and set it so
  562. * TRANSPORT(dev)->get_blockdev() returns the correct value to the
  563. * storage engine.
  564. */
  565. if (((cdb[0] == MODE_SELECT) || (cdb[0] == MODE_SELECT_10)) &&
  566. (status_byte(result) << 1) == SAM_STAT_GOOD) {
  567. unsigned char *buf;
  568. u16 bdl;
  569. u32 blocksize;
  570. buf = sg_virt(&sg[0]);
  571. if (!buf) {
  572. pr_err("Unable to get buf for scatterlist\n");
  573. goto after_mode_select;
  574. }
  575. if (cdb[0] == MODE_SELECT)
  576. bdl = (buf[3]);
  577. else
  578. bdl = (buf[6] << 8) | (buf[7]);
  579. if (!bdl)
  580. goto after_mode_select;
  581. if (cdb[0] == MODE_SELECT)
  582. blocksize = (buf[9] << 16) | (buf[10] << 8) |
  583. (buf[11]);
  584. else
  585. blocksize = (buf[13] << 16) | (buf[14] << 8) |
  586. (buf[15]);
  587. sd->sector_size = blocksize;
  588. }
  589. after_mode_select:
  590. if (sense_buffer && (status_byte(result) & CHECK_CONDITION)) {
  591. memcpy(sense_buffer, pt->pscsi_sense, TRANSPORT_SENSE_BUFFER);
  592. cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
  593. }
  594. }
  595. enum {
  596. Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
  597. Opt_scsi_lun_id, Opt_err
  598. };
  599. static match_table_t tokens = {
  600. {Opt_scsi_host_id, "scsi_host_id=%d"},
  601. {Opt_scsi_channel_id, "scsi_channel_id=%d"},
  602. {Opt_scsi_target_id, "scsi_target_id=%d"},
  603. {Opt_scsi_lun_id, "scsi_lun_id=%d"},
  604. {Opt_err, NULL}
  605. };
  606. static ssize_t pscsi_set_configfs_dev_params(struct se_device *dev,
  607. const char *page, ssize_t count)
  608. {
  609. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  610. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  611. char *orig, *ptr, *opts;
  612. substring_t args[MAX_OPT_ARGS];
  613. int ret = 0, arg, token;
  614. opts = kstrdup(page, GFP_KERNEL);
  615. if (!opts)
  616. return -ENOMEM;
  617. orig = opts;
  618. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  619. if (!*ptr)
  620. continue;
  621. token = match_token(ptr, tokens, args);
  622. switch (token) {
  623. case Opt_scsi_host_id:
  624. if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
  625. pr_err("PSCSI[%d]: Unable to accept"
  626. " scsi_host_id while phv_mode =="
  627. " PHV_LLD_SCSI_HOST_NO\n",
  628. phv->phv_host_id);
  629. ret = -EINVAL;
  630. goto out;
  631. }
  632. ret = match_int(args, &arg);
  633. if (ret)
  634. goto out;
  635. pdv->pdv_host_id = arg;
  636. pr_debug("PSCSI[%d]: Referencing SCSI Host ID:"
  637. " %d\n", phv->phv_host_id, pdv->pdv_host_id);
  638. pdv->pdv_flags |= PDF_HAS_VIRT_HOST_ID;
  639. break;
  640. case Opt_scsi_channel_id:
  641. ret = match_int(args, &arg);
  642. if (ret)
  643. goto out;
  644. pdv->pdv_channel_id = arg;
  645. pr_debug("PSCSI[%d]: Referencing SCSI Channel"
  646. " ID: %d\n", phv->phv_host_id,
  647. pdv->pdv_channel_id);
  648. pdv->pdv_flags |= PDF_HAS_CHANNEL_ID;
  649. break;
  650. case Opt_scsi_target_id:
  651. ret = match_int(args, &arg);
  652. if (ret)
  653. goto out;
  654. pdv->pdv_target_id = arg;
  655. pr_debug("PSCSI[%d]: Referencing SCSI Target"
  656. " ID: %d\n", phv->phv_host_id,
  657. pdv->pdv_target_id);
  658. pdv->pdv_flags |= PDF_HAS_TARGET_ID;
  659. break;
  660. case Opt_scsi_lun_id:
  661. ret = match_int(args, &arg);
  662. if (ret)
  663. goto out;
  664. pdv->pdv_lun_id = arg;
  665. pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:"
  666. " %d\n", phv->phv_host_id, pdv->pdv_lun_id);
  667. pdv->pdv_flags |= PDF_HAS_LUN_ID;
  668. break;
  669. default:
  670. break;
  671. }
  672. }
  673. out:
  674. kfree(orig);
  675. return (!ret) ? count : ret;
  676. }
  677. static ssize_t pscsi_show_configfs_dev_params(struct se_device *dev, char *b)
  678. {
  679. struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
  680. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  681. struct scsi_device *sd = pdv->pdv_sd;
  682. unsigned char host_id[16];
  683. ssize_t bl;
  684. int i;
  685. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
  686. snprintf(host_id, 16, "%d", pdv->pdv_host_id);
  687. else
  688. snprintf(host_id, 16, "PHBA Mode");
  689. bl = sprintf(b, "SCSI Device Bus Location:"
  690. " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
  691. pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id,
  692. host_id);
  693. if (sd) {
  694. bl += sprintf(b + bl, " ");
  695. bl += sprintf(b + bl, "Vendor: ");
  696. for (i = 0; i < 8; i++) {
  697. if (ISPRINT(sd->vendor[i])) /* printable character? */
  698. bl += sprintf(b + bl, "%c", sd->vendor[i]);
  699. else
  700. bl += sprintf(b + bl, " ");
  701. }
  702. bl += sprintf(b + bl, " Model: ");
  703. for (i = 0; i < 16; i++) {
  704. if (ISPRINT(sd->model[i])) /* printable character ? */
  705. bl += sprintf(b + bl, "%c", sd->model[i]);
  706. else
  707. bl += sprintf(b + bl, " ");
  708. }
  709. bl += sprintf(b + bl, " Rev: ");
  710. for (i = 0; i < 4; i++) {
  711. if (ISPRINT(sd->rev[i])) /* printable character ? */
  712. bl += sprintf(b + bl, "%c", sd->rev[i]);
  713. else
  714. bl += sprintf(b + bl, " ");
  715. }
  716. bl += sprintf(b + bl, "\n");
  717. }
  718. return bl;
  719. }
  720. static void pscsi_bi_endio(struct bio *bio)
  721. {
  722. bio_put(bio);
  723. }
  724. static inline struct bio *pscsi_get_bio(int nr_vecs)
  725. {
  726. struct bio *bio;
  727. /*
  728. * Use bio_malloc() following the comment in for bio -> struct request
  729. * in block/blk-core.c:blk_make_request()
  730. */
  731. bio = bio_kmalloc(GFP_KERNEL, nr_vecs);
  732. if (!bio) {
  733. pr_err("PSCSI: bio_kmalloc() failed\n");
  734. return NULL;
  735. }
  736. bio->bi_end_io = pscsi_bi_endio;
  737. return bio;
  738. }
  739. static sense_reason_t
  740. pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  741. struct request *req)
  742. {
  743. struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
  744. struct bio *bio = NULL;
  745. struct page *page;
  746. struct scatterlist *sg;
  747. u32 data_len = cmd->data_length, i, len, bytes, off;
  748. int nr_pages = (cmd->data_length + sgl[0].offset +
  749. PAGE_SIZE - 1) >> PAGE_SHIFT;
  750. int nr_vecs = 0, rc;
  751. int rw = (cmd->data_direction == DMA_TO_DEVICE);
  752. BUG_ON(!cmd->data_length);
  753. pr_debug("PSCSI: nr_pages: %d\n", nr_pages);
  754. for_each_sg(sgl, sg, sgl_nents, i) {
  755. page = sg_page(sg);
  756. off = sg->offset;
  757. len = sg->length;
  758. pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i,
  759. page, len, off);
  760. /*
  761. * We only have one page of data in each sg element,
  762. * we can not cross a page boundary.
  763. */
  764. if (off + len > PAGE_SIZE)
  765. goto fail;
  766. if (len > 0 && data_len > 0) {
  767. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  768. bytes = min(bytes, data_len);
  769. if (!bio) {
  770. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  771. nr_pages -= nr_vecs;
  772. /*
  773. * Calls bio_kmalloc() and sets bio->bi_end_io()
  774. */
  775. bio = pscsi_get_bio(nr_vecs);
  776. if (!bio)
  777. goto fail;
  778. if (rw)
  779. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  780. pr_debug("PSCSI: Allocated bio: %p,"
  781. " dir: %s nr_vecs: %d\n", bio,
  782. (rw) ? "rw" : "r", nr_vecs);
  783. }
  784. pr_debug("PSCSI: Calling bio_add_pc_page() i: %d"
  785. " bio: %p page: %p len: %d off: %d\n", i, bio,
  786. page, len, off);
  787. rc = bio_add_pc_page(pdv->pdv_sd->request_queue,
  788. bio, page, bytes, off);
  789. if (rc != bytes)
  790. goto fail;
  791. pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
  792. bio->bi_vcnt, nr_vecs);
  793. if (bio->bi_vcnt > nr_vecs) {
  794. pr_debug("PSCSI: Reached bio->bi_vcnt max:"
  795. " %d i: %d bio: %p, allocating another"
  796. " bio\n", bio->bi_vcnt, i, bio);
  797. rc = blk_rq_append_bio(req, bio);
  798. if (rc) {
  799. pr_err("pSCSI: failed to append bio\n");
  800. goto fail;
  801. }
  802. /*
  803. * Clear the pointer so that another bio will
  804. * be allocated with pscsi_get_bio() above.
  805. */
  806. bio = NULL;
  807. }
  808. data_len -= bytes;
  809. }
  810. }
  811. if (bio) {
  812. rc = blk_rq_append_bio(req, bio);
  813. if (rc) {
  814. pr_err("pSCSI: failed to append bio\n");
  815. goto fail;
  816. }
  817. }
  818. return 0;
  819. fail:
  820. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  821. }
  822. static sense_reason_t
  823. pscsi_parse_cdb(struct se_cmd *cmd)
  824. {
  825. if (cmd->se_cmd_flags & SCF_BIDI)
  826. return TCM_UNSUPPORTED_SCSI_OPCODE;
  827. return passthrough_parse_cdb(cmd, pscsi_execute_cmd);
  828. }
  829. static sense_reason_t
  830. pscsi_execute_cmd(struct se_cmd *cmd)
  831. {
  832. struct scatterlist *sgl = cmd->t_data_sg;
  833. u32 sgl_nents = cmd->t_data_nents;
  834. struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
  835. struct pscsi_plugin_task *pt;
  836. struct request *req;
  837. sense_reason_t ret;
  838. /*
  839. * Dynamically alloc cdb space, since it may be larger than
  840. * TCM_MAX_COMMAND_SIZE
  841. */
  842. pt = kzalloc(sizeof(*pt) + scsi_command_size(cmd->t_task_cdb), GFP_KERNEL);
  843. if (!pt) {
  844. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  845. }
  846. cmd->priv = pt;
  847. memcpy(pt->pscsi_cdb, cmd->t_task_cdb,
  848. scsi_command_size(cmd->t_task_cdb));
  849. req = blk_get_request(pdv->pdv_sd->request_queue,
  850. (cmd->data_direction == DMA_TO_DEVICE),
  851. GFP_KERNEL);
  852. if (IS_ERR(req)) {
  853. pr_err("PSCSI: blk_get_request() failed\n");
  854. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  855. goto fail;
  856. }
  857. blk_rq_set_block_pc(req);
  858. if (sgl) {
  859. ret = pscsi_map_sg(cmd, sgl, sgl_nents, req);
  860. if (ret)
  861. goto fail_put_request;
  862. }
  863. req->end_io = pscsi_req_done;
  864. req->end_io_data = cmd;
  865. req->cmd_len = scsi_command_size(pt->pscsi_cdb);
  866. req->cmd = &pt->pscsi_cdb[0];
  867. req->sense = &pt->pscsi_sense[0];
  868. req->sense_len = 0;
  869. if (pdv->pdv_sd->type == TYPE_DISK)
  870. req->timeout = PS_TIMEOUT_DISK;
  871. else
  872. req->timeout = PS_TIMEOUT_OTHER;
  873. req->retries = PS_RETRY;
  874. blk_execute_rq_nowait(pdv->pdv_sd->request_queue, NULL, req,
  875. (cmd->sam_task_attr == TCM_HEAD_TAG),
  876. pscsi_req_done);
  877. return 0;
  878. fail_put_request:
  879. blk_put_request(req);
  880. fail:
  881. kfree(pt);
  882. return ret;
  883. }
  884. /* pscsi_get_device_type():
  885. *
  886. *
  887. */
  888. static u32 pscsi_get_device_type(struct se_device *dev)
  889. {
  890. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  891. struct scsi_device *sd = pdv->pdv_sd;
  892. return (sd) ? sd->type : TYPE_NO_LUN;
  893. }
  894. static sector_t pscsi_get_blocks(struct se_device *dev)
  895. {
  896. struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
  897. if (pdv->pdv_bd && pdv->pdv_bd->bd_part)
  898. return pdv->pdv_bd->bd_part->nr_sects;
  899. return 0;
  900. }
  901. static void pscsi_req_done(struct request *req, int uptodate)
  902. {
  903. struct se_cmd *cmd = req->end_io_data;
  904. struct pscsi_plugin_task *pt = cmd->priv;
  905. pt->pscsi_result = req->errors;
  906. pt->pscsi_resid = req->resid_len;
  907. cmd->scsi_status = status_byte(pt->pscsi_result) << 1;
  908. if (cmd->scsi_status) {
  909. pr_debug("PSCSI Status Byte exception at cmd: %p CDB:"
  910. " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
  911. pt->pscsi_result);
  912. }
  913. switch (host_byte(pt->pscsi_result)) {
  914. case DID_OK:
  915. target_complete_cmd(cmd, cmd->scsi_status);
  916. break;
  917. default:
  918. pr_debug("PSCSI Host Byte exception at cmd: %p CDB:"
  919. " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
  920. pt->pscsi_result);
  921. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  922. break;
  923. }
  924. __blk_put_request(req->q, req);
  925. kfree(pt);
  926. }
  927. static const struct target_backend_ops pscsi_ops = {
  928. .name = "pscsi",
  929. .owner = THIS_MODULE,
  930. .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
  931. .attach_hba = pscsi_attach_hba,
  932. .detach_hba = pscsi_detach_hba,
  933. .pmode_enable_hba = pscsi_pmode_enable_hba,
  934. .alloc_device = pscsi_alloc_device,
  935. .configure_device = pscsi_configure_device,
  936. .free_device = pscsi_free_device,
  937. .transport_complete = pscsi_transport_complete,
  938. .parse_cdb = pscsi_parse_cdb,
  939. .set_configfs_dev_params = pscsi_set_configfs_dev_params,
  940. .show_configfs_dev_params = pscsi_show_configfs_dev_params,
  941. .get_device_type = pscsi_get_device_type,
  942. .get_blocks = pscsi_get_blocks,
  943. .tb_dev_attrib_attrs = passthrough_attrib_attrs,
  944. };
  945. static int __init pscsi_module_init(void)
  946. {
  947. return transport_backend_register(&pscsi_ops);
  948. }
  949. static void __exit pscsi_module_exit(void)
  950. {
  951. target_backend_unregister(&pscsi_ops);
  952. }
  953. MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
  954. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  955. MODULE_LICENSE("GPL");
  956. module_init(pscsi_module_init);
  957. module_exit(pscsi_module_exit);