target_core_pscsi.c 29 KB

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