target_core_pscsi.c 28 KB

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