target_core_pscsi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /*******************************************************************************
  2. * Filename: target_core_pscsi.c
  3. *
  4. * This file contains the generic target mode <-> Linux SCSI subsystem plugin.
  5. *
  6. * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
  7. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  8. * Copyright (c) 2007-2010 Rising Tide Systems
  9. * Copyright (c) 2008-2010 Linux-iSCSI.org
  10. *
  11. * Nicholas A. Bellinger <nab@kernel.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. *
  27. ******************************************************************************/
  28. #include <linux/string.h>
  29. #include <linux/parser.h>
  30. #include <linux/timer.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/blk_types.h>
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/genhd.h>
  36. #include <linux/cdrom.h>
  37. #include <linux/ratelimit.h>
  38. #include <linux/module.h>
  39. #include <asm/unaligned.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_device.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_tcq.h>
  45. #include <target/target_core_base.h>
  46. #include <target/target_core_backend.h>
  47. #include "target_core_alua.h"
  48. #include "target_core_pscsi.h"
  49. #define ISPRINT(a) ((a >= ' ') && (a <= '~'))
  50. static struct se_subsystem_api pscsi_template;
  51. static int pscsi_execute_cmd(struct se_cmd *cmd);
  52. static void pscsi_req_done(struct request *, int);
  53. /* pscsi_attach_hba():
  54. *
  55. * pscsi_get_sh() used scsi_host_lookup() to locate struct Scsi_Host.
  56. * from the passed SCSI Host ID.
  57. */
  58. static int pscsi_attach_hba(struct se_hba *hba, u32 host_id)
  59. {
  60. struct pscsi_hba_virt *phv;
  61. phv = kzalloc(sizeof(struct pscsi_hba_virt), GFP_KERNEL);
  62. if (!phv) {
  63. pr_err("Unable to allocate struct pscsi_hba_virt\n");
  64. return -ENOMEM;
  65. }
  66. phv->phv_host_id = host_id;
  67. phv->phv_mode = PHV_VIRTUAL_HOST_ID;
  68. hba->hba_ptr = phv;
  69. pr_debug("CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
  70. " Generic Target Core Stack %s\n", hba->hba_id,
  71. PSCSI_VERSION, TARGET_CORE_MOD_VERSION);
  72. pr_debug("CORE_HBA[%d] - Attached SCSI HBA to Generic\n",
  73. hba->hba_id);
  74. return 0;
  75. }
  76. static void pscsi_detach_hba(struct se_hba *hba)
  77. {
  78. struct pscsi_hba_virt *phv = hba->hba_ptr;
  79. struct Scsi_Host *scsi_host = phv->phv_lld_host;
  80. if (scsi_host) {
  81. scsi_host_put(scsi_host);
  82. pr_debug("CORE_HBA[%d] - Detached SCSI HBA: %s from"
  83. " Generic Target Core\n", hba->hba_id,
  84. (scsi_host->hostt->name) ? (scsi_host->hostt->name) :
  85. "Unknown");
  86. } else
  87. pr_debug("CORE_HBA[%d] - Detached Virtual SCSI HBA"
  88. " from Generic Target Core\n", hba->hba_id);
  89. kfree(phv);
  90. hba->hba_ptr = NULL;
  91. }
  92. static int pscsi_pmode_enable_hba(struct se_hba *hba, unsigned long mode_flag)
  93. {
  94. struct pscsi_hba_virt *phv = hba->hba_ptr;
  95. struct Scsi_Host *sh = phv->phv_lld_host;
  96. /*
  97. * Release the struct Scsi_Host
  98. */
  99. if (!mode_flag) {
  100. if (!sh)
  101. return 0;
  102. phv->phv_lld_host = NULL;
  103. phv->phv_mode = PHV_VIRTUAL_HOST_ID;
  104. pr_debug("CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
  105. " %s\n", hba->hba_id, (sh->hostt->name) ?
  106. (sh->hostt->name) : "Unknown");
  107. scsi_host_put(sh);
  108. return 0;
  109. }
  110. /*
  111. * Otherwise, locate struct Scsi_Host from the original passed
  112. * pSCSI Host ID and enable for phba mode
  113. */
  114. sh = scsi_host_lookup(phv->phv_host_id);
  115. if (IS_ERR(sh)) {
  116. pr_err("pSCSI: Unable to locate SCSI Host for"
  117. " phv_host_id: %d\n", phv->phv_host_id);
  118. return PTR_ERR(sh);
  119. }
  120. phv->phv_lld_host = sh;
  121. phv->phv_mode = PHV_LLD_SCSI_HOST_NO;
  122. pr_debug("CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
  123. hba->hba_id, (sh->hostt->name) ? (sh->hostt->name) : "Unknown");
  124. return 1;
  125. }
  126. static void pscsi_tape_read_blocksize(struct se_device *dev,
  127. struct scsi_device *sdev)
  128. {
  129. unsigned char cdb[MAX_COMMAND_SIZE], *buf;
  130. int ret;
  131. buf = kzalloc(12, GFP_KERNEL);
  132. if (!buf)
  133. return;
  134. memset(cdb, 0, MAX_COMMAND_SIZE);
  135. cdb[0] = MODE_SENSE;
  136. cdb[4] = 0x0c; /* 12 bytes */
  137. ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf, 12, NULL,
  138. HZ, 1, NULL);
  139. if (ret)
  140. goto out_free;
  141. /*
  142. * If MODE_SENSE still returns zero, set the default value to 1024.
  143. */
  144. sdev->sector_size = (buf[9] << 16) | (buf[10] << 8) | (buf[11]);
  145. if (!sdev->sector_size)
  146. sdev->sector_size = 1024;
  147. out_free:
  148. kfree(buf);
  149. }
  150. static void
  151. pscsi_set_inquiry_info(struct scsi_device *sdev, struct t10_wwn *wwn)
  152. {
  153. unsigned char *buf;
  154. if (sdev->inquiry_len < INQUIRY_LEN)
  155. return;
  156. buf = sdev->inquiry;
  157. if (!buf)
  158. return;
  159. /*
  160. * Use sdev->inquiry from drivers/scsi/scsi_scan.c:scsi_alloc_sdev()
  161. */
  162. memcpy(&wwn->vendor[0], &buf[8], sizeof(wwn->vendor));
  163. memcpy(&wwn->model[0], &buf[16], sizeof(wwn->model));
  164. memcpy(&wwn->revision[0], &buf[32], sizeof(wwn->revision));
  165. }
  166. static int
  167. pscsi_get_inquiry_vpd_serial(struct scsi_device *sdev, struct t10_wwn *wwn)
  168. {
  169. unsigned char cdb[MAX_COMMAND_SIZE], *buf;
  170. int ret;
  171. buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
  172. if (!buf)
  173. return -ENOMEM;
  174. memset(cdb, 0, MAX_COMMAND_SIZE);
  175. cdb[0] = INQUIRY;
  176. cdb[1] = 0x01; /* Query VPD */
  177. cdb[2] = 0x80; /* Unit Serial Number */
  178. cdb[3] = (INQUIRY_VPD_SERIAL_LEN >> 8) & 0xff;
  179. cdb[4] = (INQUIRY_VPD_SERIAL_LEN & 0xff);
  180. ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
  181. INQUIRY_VPD_SERIAL_LEN, NULL, HZ, 1, NULL);
  182. if (ret)
  183. goto out_free;
  184. snprintf(&wwn->unit_serial[0], INQUIRY_VPD_SERIAL_LEN, "%s", &buf[4]);
  185. wwn->t10_sub_dev->su_dev_flags |= SDF_FIRMWARE_VPD_UNIT_SERIAL;
  186. kfree(buf);
  187. return 0;
  188. out_free:
  189. kfree(buf);
  190. return -EPERM;
  191. }
  192. static void
  193. pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
  194. struct t10_wwn *wwn)
  195. {
  196. unsigned char cdb[MAX_COMMAND_SIZE], *buf, *page_83;
  197. int ident_len, page_len, off = 4, ret;
  198. struct t10_vpd *vpd;
  199. buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
  200. if (!buf)
  201. return;
  202. memset(cdb, 0, MAX_COMMAND_SIZE);
  203. cdb[0] = INQUIRY;
  204. cdb[1] = 0x01; /* Query VPD */
  205. cdb[2] = 0x83; /* Device Identifier */
  206. cdb[3] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN >> 8) & 0xff;
  207. cdb[4] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN & 0xff);
  208. ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
  209. INQUIRY_VPD_DEVICE_IDENTIFIER_LEN,
  210. NULL, HZ, 1, NULL);
  211. if (ret)
  212. goto out;
  213. page_len = (buf[2] << 8) | buf[3];
  214. while (page_len > 0) {
  215. /* Grab a pointer to the Identification descriptor */
  216. page_83 = &buf[off];
  217. ident_len = page_83[3];
  218. if (!ident_len) {
  219. pr_err("page_83[3]: identifier"
  220. " length zero!\n");
  221. break;
  222. }
  223. pr_debug("T10 VPD Identifer Length: %d\n", ident_len);
  224. vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
  225. if (!vpd) {
  226. pr_err("Unable to allocate memory for"
  227. " struct t10_vpd\n");
  228. goto out;
  229. }
  230. INIT_LIST_HEAD(&vpd->vpd_list);
  231. transport_set_vpd_proto_id(vpd, page_83);
  232. transport_set_vpd_assoc(vpd, page_83);
  233. if (transport_set_vpd_ident_type(vpd, page_83) < 0) {
  234. off += (ident_len + 4);
  235. page_len -= (ident_len + 4);
  236. kfree(vpd);
  237. continue;
  238. }
  239. if (transport_set_vpd_ident(vpd, page_83) < 0) {
  240. off += (ident_len + 4);
  241. page_len -= (ident_len + 4);
  242. kfree(vpd);
  243. continue;
  244. }
  245. list_add_tail(&vpd->vpd_list, &wwn->t10_vpd_list);
  246. off += (ident_len + 4);
  247. page_len -= (ident_len + 4);
  248. }
  249. out:
  250. kfree(buf);
  251. }
  252. /* pscsi_add_device_to_list():
  253. *
  254. *
  255. */
  256. static struct se_device *pscsi_add_device_to_list(
  257. struct se_hba *hba,
  258. struct se_subsystem_dev *se_dev,
  259. struct pscsi_dev_virt *pdv,
  260. struct scsi_device *sd,
  261. int dev_flags)
  262. {
  263. struct se_device *dev;
  264. struct se_dev_limits dev_limits;
  265. struct request_queue *q;
  266. struct queue_limits *limits;
  267. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  268. if (!sd->queue_depth) {
  269. sd->queue_depth = PSCSI_DEFAULT_QUEUEDEPTH;
  270. pr_err("Set broken SCSI Device %d:%d:%d"
  271. " queue_depth to %d\n", sd->channel, sd->id,
  272. sd->lun, sd->queue_depth);
  273. }
  274. /*
  275. * Setup the local scope queue_limits from struct request_queue->limits
  276. * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
  277. */
  278. q = sd->request_queue;
  279. limits = &dev_limits.limits;
  280. limits->logical_block_size = sd->sector_size;
  281. limits->max_hw_sectors = min_t(int, sd->host->max_sectors, queue_max_hw_sectors(q));
  282. limits->max_sectors = min_t(int, sd->host->max_sectors, queue_max_sectors(q));
  283. dev_limits.hw_queue_depth = sd->queue_depth;
  284. dev_limits.queue_depth = sd->queue_depth;
  285. /*
  286. * Setup our standard INQUIRY info into se_dev->t10_wwn
  287. */
  288. pscsi_set_inquiry_info(sd, &se_dev->t10_wwn);
  289. /*
  290. * Set the pointer pdv->pdv_sd to from passed struct scsi_device,
  291. * which has already been referenced with Linux SCSI code with
  292. * scsi_device_get() in this file's pscsi_create_virtdevice().
  293. *
  294. * The passthrough operations called by the transport_add_device_*
  295. * function below will require this pointer to be set for passthroug
  296. * ops.
  297. *
  298. * For the shutdown case in pscsi_free_device(), this struct
  299. * scsi_device reference is released with Linux SCSI code
  300. * scsi_device_put() and the pdv->pdv_sd cleared.
  301. */
  302. pdv->pdv_sd = sd;
  303. dev = transport_add_device_to_core_hba(hba, &pscsi_template,
  304. se_dev, dev_flags, pdv,
  305. &dev_limits, NULL, NULL);
  306. if (!dev) {
  307. pdv->pdv_sd = NULL;
  308. return NULL;
  309. }
  310. /*
  311. * Locate VPD WWN Information used for various purposes within
  312. * the Storage Engine.
  313. */
  314. if (!pscsi_get_inquiry_vpd_serial(sd, &se_dev->t10_wwn)) {
  315. /*
  316. * If VPD Unit Serial returned GOOD status, try
  317. * VPD Device Identification page (0x83).
  318. */
  319. pscsi_get_inquiry_vpd_device_ident(sd, &se_dev->t10_wwn);
  320. }
  321. /*
  322. * For TYPE_TAPE, attempt to determine blocksize with MODE_SENSE.
  323. */
  324. if (sd->type == TYPE_TAPE)
  325. pscsi_tape_read_blocksize(dev, sd);
  326. return dev;
  327. }
  328. static void *pscsi_allocate_virtdevice(struct se_hba *hba, const char *name)
  329. {
  330. struct pscsi_dev_virt *pdv;
  331. pdv = kzalloc(sizeof(struct pscsi_dev_virt), GFP_KERNEL);
  332. if (!pdv) {
  333. pr_err("Unable to allocate memory for struct pscsi_dev_virt\n");
  334. return NULL;
  335. }
  336. pdv->pdv_se_hba = hba;
  337. pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv, name);
  338. return pdv;
  339. }
  340. /*
  341. * Called with struct Scsi_Host->host_lock called.
  342. */
  343. static struct se_device *pscsi_create_type_disk(
  344. struct scsi_device *sd,
  345. struct pscsi_dev_virt *pdv,
  346. struct se_subsystem_dev *se_dev,
  347. struct se_hba *hba)
  348. __releases(sh->host_lock)
  349. {
  350. struct se_device *dev;
  351. struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
  352. struct Scsi_Host *sh = sd->host;
  353. struct block_device *bd;
  354. u32 dev_flags = 0;
  355. if (scsi_device_get(sd)) {
  356. pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
  357. sh->host_no, sd->channel, sd->id, sd->lun);
  358. spin_unlock_irq(sh->host_lock);
  359. return NULL;
  360. }
  361. spin_unlock_irq(sh->host_lock);
  362. /*
  363. * Claim exclusive struct block_device access to struct scsi_device
  364. * for TYPE_DISK using supplied udev_path
  365. */
  366. bd = blkdev_get_by_path(se_dev->se_dev_udev_path,
  367. FMODE_WRITE|FMODE_READ|FMODE_EXCL, pdv);
  368. if (IS_ERR(bd)) {
  369. pr_err("pSCSI: blkdev_get_by_path() failed\n");
  370. scsi_device_put(sd);
  371. return NULL;
  372. }
  373. pdv->pdv_bd = bd;
  374. dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
  375. if (!dev) {
  376. blkdev_put(pdv->pdv_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  377. scsi_device_put(sd);
  378. return NULL;
  379. }
  380. pr_debug("CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%d\n",
  381. phv->phv_host_id, sh->host_no, sd->channel, sd->id, sd->lun);
  382. return dev;
  383. }
  384. /*
  385. * Called with struct Scsi_Host->host_lock called.
  386. */
  387. static struct se_device *pscsi_create_type_rom(
  388. struct scsi_device *sd,
  389. struct pscsi_dev_virt *pdv,
  390. struct se_subsystem_dev *se_dev,
  391. struct se_hba *hba)
  392. __releases(sh->host_lock)
  393. {
  394. struct se_device *dev;
  395. struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
  396. struct Scsi_Host *sh = sd->host;
  397. u32 dev_flags = 0;
  398. if (scsi_device_get(sd)) {
  399. pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
  400. sh->host_no, sd->channel, sd->id, sd->lun);
  401. spin_unlock_irq(sh->host_lock);
  402. return NULL;
  403. }
  404. spin_unlock_irq(sh->host_lock);
  405. dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
  406. if (!dev) {
  407. scsi_device_put(sd);
  408. return NULL;
  409. }
  410. pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
  411. phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
  412. sd->channel, sd->id, sd->lun);
  413. return dev;
  414. }
  415. /*
  416. *Called with struct Scsi_Host->host_lock called.
  417. */
  418. static struct se_device *pscsi_create_type_other(
  419. struct scsi_device *sd,
  420. struct pscsi_dev_virt *pdv,
  421. struct se_subsystem_dev *se_dev,
  422. struct se_hba *hba)
  423. __releases(sh->host_lock)
  424. {
  425. struct se_device *dev;
  426. struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
  427. struct Scsi_Host *sh = sd->host;
  428. u32 dev_flags = 0;
  429. spin_unlock_irq(sh->host_lock);
  430. dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
  431. if (!dev)
  432. return NULL;
  433. pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
  434. phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
  435. sd->channel, sd->id, sd->lun);
  436. return dev;
  437. }
  438. static struct se_device *pscsi_create_virtdevice(
  439. struct se_hba *hba,
  440. struct se_subsystem_dev *se_dev,
  441. void *p)
  442. {
  443. struct pscsi_dev_virt *pdv = p;
  444. struct se_device *dev;
  445. struct scsi_device *sd;
  446. struct pscsi_hba_virt *phv = hba->hba_ptr;
  447. struct Scsi_Host *sh = phv->phv_lld_host;
  448. int legacy_mode_enable = 0;
  449. if (!pdv) {
  450. pr_err("Unable to locate struct pscsi_dev_virt"
  451. " parameter\n");
  452. return ERR_PTR(-EINVAL);
  453. }
  454. /*
  455. * If not running in PHV_LLD_SCSI_HOST_NO mode, locate the
  456. * struct Scsi_Host we will need to bring the TCM/pSCSI object online
  457. */
  458. if (!sh) {
  459. if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
  460. pr_err("pSCSI: Unable to locate struct"
  461. " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
  462. return ERR_PTR(-ENODEV);
  463. }
  464. /*
  465. * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device
  466. * reference, we enforce that udev_path has been set
  467. */
  468. if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH)) {
  469. pr_err("pSCSI: udev_path attribute has not"
  470. " been set before ENABLE=1\n");
  471. return ERR_PTR(-EINVAL);
  472. }
  473. /*
  474. * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID,
  475. * use the original TCM hba ID to reference Linux/SCSI Host No
  476. * and enable for PHV_LLD_SCSI_HOST_NO mode.
  477. */
  478. if (!(pdv->pdv_flags & PDF_HAS_VIRT_HOST_ID)) {
  479. spin_lock(&hba->device_lock);
  480. if (!list_empty(&hba->hba_dev_list)) {
  481. pr_err("pSCSI: Unable to set hba_mode"
  482. " with active devices\n");
  483. spin_unlock(&hba->device_lock);
  484. return ERR_PTR(-EEXIST);
  485. }
  486. spin_unlock(&hba->device_lock);
  487. if (pscsi_pmode_enable_hba(hba, 1) != 1)
  488. return ERR_PTR(-ENODEV);
  489. legacy_mode_enable = 1;
  490. hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
  491. sh = phv->phv_lld_host;
  492. } else {
  493. sh = scsi_host_lookup(pdv->pdv_host_id);
  494. if (IS_ERR(sh)) {
  495. pr_err("pSCSI: Unable to locate"
  496. " pdv_host_id: %d\n", pdv->pdv_host_id);
  497. return ERR_CAST(sh);
  498. }
  499. }
  500. } else {
  501. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) {
  502. pr_err("pSCSI: PHV_VIRTUAL_HOST_ID set while"
  503. " struct Scsi_Host exists\n");
  504. return ERR_PTR(-EEXIST);
  505. }
  506. }
  507. spin_lock_irq(sh->host_lock);
  508. list_for_each_entry(sd, &sh->__devices, siblings) {
  509. if ((pdv->pdv_channel_id != sd->channel) ||
  510. (pdv->pdv_target_id != sd->id) ||
  511. (pdv->pdv_lun_id != sd->lun))
  512. continue;
  513. /*
  514. * Functions will release the held struct scsi_host->host_lock
  515. * before calling calling pscsi_add_device_to_list() to register
  516. * struct scsi_device with target_core_mod.
  517. */
  518. switch (sd->type) {
  519. case TYPE_DISK:
  520. dev = pscsi_create_type_disk(sd, pdv, se_dev, hba);
  521. break;
  522. case TYPE_ROM:
  523. dev = pscsi_create_type_rom(sd, pdv, se_dev, hba);
  524. break;
  525. default:
  526. dev = pscsi_create_type_other(sd, pdv, se_dev, hba);
  527. break;
  528. }
  529. if (!dev) {
  530. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
  531. scsi_host_put(sh);
  532. else if (legacy_mode_enable) {
  533. pscsi_pmode_enable_hba(hba, 0);
  534. hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
  535. }
  536. pdv->pdv_sd = NULL;
  537. return ERR_PTR(-ENODEV);
  538. }
  539. return dev;
  540. }
  541. spin_unlock_irq(sh->host_lock);
  542. pr_err("pSCSI: Unable to locate %d:%d:%d:%d\n", sh->host_no,
  543. pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id);
  544. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
  545. scsi_host_put(sh);
  546. else if (legacy_mode_enable) {
  547. pscsi_pmode_enable_hba(hba, 0);
  548. hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
  549. }
  550. return ERR_PTR(-ENODEV);
  551. }
  552. /* pscsi_free_device(): (Part of se_subsystem_api_t template)
  553. *
  554. *
  555. */
  556. static void pscsi_free_device(void *p)
  557. {
  558. struct pscsi_dev_virt *pdv = p;
  559. struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
  560. struct scsi_device *sd = pdv->pdv_sd;
  561. if (sd) {
  562. /*
  563. * Release exclusive pSCSI internal struct block_device claim for
  564. * struct scsi_device with TYPE_DISK from pscsi_create_type_disk()
  565. */
  566. if ((sd->type == TYPE_DISK) && pdv->pdv_bd) {
  567. blkdev_put(pdv->pdv_bd,
  568. FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  569. pdv->pdv_bd = NULL;
  570. }
  571. /*
  572. * For HBA mode PHV_LLD_SCSI_HOST_NO, release the reference
  573. * to struct Scsi_Host now.
  574. */
  575. if ((phv->phv_mode == PHV_LLD_SCSI_HOST_NO) &&
  576. (phv->phv_lld_host != NULL))
  577. scsi_host_put(phv->phv_lld_host);
  578. if ((sd->type == TYPE_DISK) || (sd->type == TYPE_ROM))
  579. scsi_device_put(sd);
  580. pdv->pdv_sd = NULL;
  581. }
  582. kfree(pdv);
  583. }
  584. static int pscsi_transport_complete(struct se_cmd *cmd, struct scatterlist *sg)
  585. {
  586. struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
  587. struct scsi_device *sd = pdv->pdv_sd;
  588. int result;
  589. struct pscsi_plugin_task *pt = cmd->priv;
  590. unsigned char *cdb = &pt->pscsi_cdb[0];
  591. result = pt->pscsi_result;
  592. /*
  593. * Hack to make sure that Write-Protect modepage is set if R/O mode is
  594. * forced.
  595. */
  596. if (((cdb[0] == MODE_SENSE) || (cdb[0] == MODE_SENSE_10)) &&
  597. (status_byte(result) << 1) == SAM_STAT_GOOD) {
  598. if (!cmd->se_deve)
  599. goto after_mode_sense;
  600. if (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) {
  601. unsigned char *buf = transport_kmap_data_sg(cmd);
  602. if (cdb[0] == MODE_SENSE_10) {
  603. if (!(buf[3] & 0x80))
  604. buf[3] |= 0x80;
  605. } else {
  606. if (!(buf[2] & 0x80))
  607. buf[2] |= 0x80;
  608. }
  609. transport_kunmap_data_sg(cmd);
  610. }
  611. }
  612. after_mode_sense:
  613. if (sd->type != TYPE_TAPE)
  614. goto after_mode_select;
  615. /*
  616. * Hack to correctly obtain the initiator requested blocksize for
  617. * TYPE_TAPE. Since this value is dependent upon each tape media,
  618. * struct scsi_device->sector_size will not contain the correct value
  619. * by default, so we go ahead and set it so
  620. * TRANSPORT(dev)->get_blockdev() returns the correct value to the
  621. * storage engine.
  622. */
  623. if (((cdb[0] == MODE_SELECT) || (cdb[0] == MODE_SELECT_10)) &&
  624. (status_byte(result) << 1) == SAM_STAT_GOOD) {
  625. unsigned char *buf;
  626. u16 bdl;
  627. u32 blocksize;
  628. buf = sg_virt(&sg[0]);
  629. if (!buf) {
  630. pr_err("Unable to get buf for scatterlist\n");
  631. goto after_mode_select;
  632. }
  633. if (cdb[0] == MODE_SELECT)
  634. bdl = (buf[3]);
  635. else
  636. bdl = (buf[6] << 8) | (buf[7]);
  637. if (!bdl)
  638. goto after_mode_select;
  639. if (cdb[0] == MODE_SELECT)
  640. blocksize = (buf[9] << 16) | (buf[10] << 8) |
  641. (buf[11]);
  642. else
  643. blocksize = (buf[13] << 16) | (buf[14] << 8) |
  644. (buf[15]);
  645. sd->sector_size = blocksize;
  646. }
  647. after_mode_select:
  648. if (status_byte(result) & CHECK_CONDITION)
  649. return 1;
  650. return 0;
  651. }
  652. enum {
  653. Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
  654. Opt_scsi_lun_id, Opt_err
  655. };
  656. static match_table_t tokens = {
  657. {Opt_scsi_host_id, "scsi_host_id=%d"},
  658. {Opt_scsi_channel_id, "scsi_channel_id=%d"},
  659. {Opt_scsi_target_id, "scsi_target_id=%d"},
  660. {Opt_scsi_lun_id, "scsi_lun_id=%d"},
  661. {Opt_err, NULL}
  662. };
  663. static ssize_t pscsi_set_configfs_dev_params(struct se_hba *hba,
  664. struct se_subsystem_dev *se_dev,
  665. const char *page,
  666. ssize_t count)
  667. {
  668. struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
  669. struct pscsi_hba_virt *phv = hba->hba_ptr;
  670. char *orig, *ptr, *opts;
  671. substring_t args[MAX_OPT_ARGS];
  672. int ret = 0, arg, token;
  673. opts = kstrdup(page, GFP_KERNEL);
  674. if (!opts)
  675. return -ENOMEM;
  676. orig = opts;
  677. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  678. if (!*ptr)
  679. continue;
  680. token = match_token(ptr, tokens, args);
  681. switch (token) {
  682. case Opt_scsi_host_id:
  683. if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
  684. pr_err("PSCSI[%d]: Unable to accept"
  685. " scsi_host_id while phv_mode =="
  686. " PHV_LLD_SCSI_HOST_NO\n",
  687. phv->phv_host_id);
  688. ret = -EINVAL;
  689. goto out;
  690. }
  691. match_int(args, &arg);
  692. pdv->pdv_host_id = arg;
  693. pr_debug("PSCSI[%d]: Referencing SCSI Host ID:"
  694. " %d\n", phv->phv_host_id, pdv->pdv_host_id);
  695. pdv->pdv_flags |= PDF_HAS_VIRT_HOST_ID;
  696. break;
  697. case Opt_scsi_channel_id:
  698. match_int(args, &arg);
  699. pdv->pdv_channel_id = arg;
  700. pr_debug("PSCSI[%d]: Referencing SCSI Channel"
  701. " ID: %d\n", phv->phv_host_id,
  702. pdv->pdv_channel_id);
  703. pdv->pdv_flags |= PDF_HAS_CHANNEL_ID;
  704. break;
  705. case Opt_scsi_target_id:
  706. match_int(args, &arg);
  707. pdv->pdv_target_id = arg;
  708. pr_debug("PSCSI[%d]: Referencing SCSI Target"
  709. " ID: %d\n", phv->phv_host_id,
  710. pdv->pdv_target_id);
  711. pdv->pdv_flags |= PDF_HAS_TARGET_ID;
  712. break;
  713. case Opt_scsi_lun_id:
  714. match_int(args, &arg);
  715. pdv->pdv_lun_id = arg;
  716. pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:"
  717. " %d\n", phv->phv_host_id, pdv->pdv_lun_id);
  718. pdv->pdv_flags |= PDF_HAS_LUN_ID;
  719. break;
  720. default:
  721. break;
  722. }
  723. }
  724. out:
  725. kfree(orig);
  726. return (!ret) ? count : ret;
  727. }
  728. static ssize_t pscsi_check_configfs_dev_params(
  729. struct se_hba *hba,
  730. struct se_subsystem_dev *se_dev)
  731. {
  732. struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
  733. if (!(pdv->pdv_flags & PDF_HAS_CHANNEL_ID) ||
  734. !(pdv->pdv_flags & PDF_HAS_TARGET_ID) ||
  735. !(pdv->pdv_flags & PDF_HAS_LUN_ID)) {
  736. pr_err("Missing scsi_channel_id=, scsi_target_id= and"
  737. " scsi_lun_id= parameters\n");
  738. return -EINVAL;
  739. }
  740. return 0;
  741. }
  742. static ssize_t pscsi_show_configfs_dev_params(struct se_hba *hba,
  743. struct se_subsystem_dev *se_dev,
  744. char *b)
  745. {
  746. struct pscsi_hba_virt *phv = hba->hba_ptr;
  747. struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
  748. struct scsi_device *sd = pdv->pdv_sd;
  749. unsigned char host_id[16];
  750. ssize_t bl;
  751. int i;
  752. if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
  753. snprintf(host_id, 16, "%d", pdv->pdv_host_id);
  754. else
  755. snprintf(host_id, 16, "PHBA Mode");
  756. bl = sprintf(b, "SCSI Device Bus Location:"
  757. " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
  758. pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id,
  759. host_id);
  760. if (sd) {
  761. bl += sprintf(b + bl, " ");
  762. bl += sprintf(b + bl, "Vendor: ");
  763. for (i = 0; i < 8; i++) {
  764. if (ISPRINT(sd->vendor[i])) /* printable character? */
  765. bl += sprintf(b + bl, "%c", sd->vendor[i]);
  766. else
  767. bl += sprintf(b + bl, " ");
  768. }
  769. bl += sprintf(b + bl, " Model: ");
  770. for (i = 0; i < 16; i++) {
  771. if (ISPRINT(sd->model[i])) /* printable character ? */
  772. bl += sprintf(b + bl, "%c", sd->model[i]);
  773. else
  774. bl += sprintf(b + bl, " ");
  775. }
  776. bl += sprintf(b + bl, " Rev: ");
  777. for (i = 0; i < 4; i++) {
  778. if (ISPRINT(sd->rev[i])) /* printable character ? */
  779. bl += sprintf(b + bl, "%c", sd->rev[i]);
  780. else
  781. bl += sprintf(b + bl, " ");
  782. }
  783. bl += sprintf(b + bl, "\n");
  784. }
  785. return bl;
  786. }
  787. static void pscsi_bi_endio(struct bio *bio, int error)
  788. {
  789. bio_put(bio);
  790. }
  791. static inline struct bio *pscsi_get_bio(int sg_num)
  792. {
  793. struct bio *bio;
  794. /*
  795. * Use bio_malloc() following the comment in for bio -> struct request
  796. * in block/blk-core.c:blk_make_request()
  797. */
  798. bio = bio_kmalloc(GFP_KERNEL, sg_num);
  799. if (!bio) {
  800. pr_err("PSCSI: bio_kmalloc() failed\n");
  801. return NULL;
  802. }
  803. bio->bi_end_io = pscsi_bi_endio;
  804. return bio;
  805. }
  806. static int pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl,
  807. u32 sgl_nents, enum dma_data_direction data_direction,
  808. struct bio **hbio)
  809. {
  810. struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
  811. struct bio *bio = NULL, *tbio = NULL;
  812. struct page *page;
  813. struct scatterlist *sg;
  814. u32 data_len = cmd->data_length, i, len, bytes, off;
  815. int nr_pages = (cmd->data_length + sgl[0].offset +
  816. PAGE_SIZE - 1) >> PAGE_SHIFT;
  817. int nr_vecs = 0, rc;
  818. int rw = (data_direction == DMA_TO_DEVICE);
  819. *hbio = NULL;
  820. pr_debug("PSCSI: nr_pages: %d\n", nr_pages);
  821. for_each_sg(sgl, sg, sgl_nents, i) {
  822. page = sg_page(sg);
  823. off = sg->offset;
  824. len = sg->length;
  825. pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i,
  826. page, len, off);
  827. while (len > 0 && data_len > 0) {
  828. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  829. bytes = min(bytes, data_len);
  830. if (!bio) {
  831. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  832. nr_pages -= nr_vecs;
  833. /*
  834. * Calls bio_kmalloc() and sets bio->bi_end_io()
  835. */
  836. bio = pscsi_get_bio(nr_vecs);
  837. if (!bio)
  838. goto fail;
  839. if (rw)
  840. bio->bi_rw |= REQ_WRITE;
  841. pr_debug("PSCSI: Allocated bio: %p,"
  842. " dir: %s nr_vecs: %d\n", bio,
  843. (rw) ? "rw" : "r", nr_vecs);
  844. /*
  845. * Set *hbio pointer to handle the case:
  846. * nr_pages > BIO_MAX_PAGES, where additional
  847. * bios need to be added to complete a given
  848. * command.
  849. */
  850. if (!*hbio)
  851. *hbio = tbio = bio;
  852. else
  853. tbio = tbio->bi_next = bio;
  854. }
  855. pr_debug("PSCSI: Calling bio_add_pc_page() i: %d"
  856. " bio: %p page: %p len: %d off: %d\n", i, bio,
  857. page, len, off);
  858. rc = bio_add_pc_page(pdv->pdv_sd->request_queue,
  859. bio, page, bytes, off);
  860. if (rc != bytes)
  861. goto fail;
  862. pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
  863. bio->bi_vcnt, nr_vecs);
  864. if (bio->bi_vcnt > nr_vecs) {
  865. pr_debug("PSCSI: Reached bio->bi_vcnt max:"
  866. " %d i: %d bio: %p, allocating another"
  867. " bio\n", bio->bi_vcnt, i, bio);
  868. /*
  869. * Clear the pointer so that another bio will
  870. * be allocated with pscsi_get_bio() above, the
  871. * current bio has already been set *tbio and
  872. * bio->bi_next.
  873. */
  874. bio = NULL;
  875. }
  876. page++;
  877. len -= bytes;
  878. data_len -= bytes;
  879. off = 0;
  880. }
  881. }
  882. return sgl_nents;
  883. fail:
  884. while (*hbio) {
  885. bio = *hbio;
  886. *hbio = (*hbio)->bi_next;
  887. bio->bi_next = NULL;
  888. bio_endio(bio, 0); /* XXX: should be error */
  889. }
  890. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  891. return -ENOMEM;
  892. }
  893. /*
  894. * Clear a lun set in the cdb if the initiator talking to use spoke
  895. * and old standards version, as we can't assume the underlying device
  896. * won't choke up on it.
  897. */
  898. static inline void pscsi_clear_cdb_lun(unsigned char *cdb)
  899. {
  900. switch (cdb[0]) {
  901. case READ_10: /* SBC - RDProtect */
  902. case READ_12: /* SBC - RDProtect */
  903. case READ_16: /* SBC - RDProtect */
  904. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  905. case VERIFY: /* SBC - VRProtect */
  906. case VERIFY_16: /* SBC - VRProtect */
  907. case WRITE_VERIFY: /* SBC - VRProtect */
  908. case WRITE_VERIFY_12: /* SBC - VRProtect */
  909. case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
  910. break;
  911. default:
  912. cdb[1] &= 0x1f; /* clear logical unit number */
  913. break;
  914. }
  915. }
  916. static int pscsi_parse_cdb(struct se_cmd *cmd)
  917. {
  918. unsigned char *cdb = cmd->t_task_cdb;
  919. unsigned int dummy_size;
  920. int ret;
  921. if (cmd->se_cmd_flags & SCF_BIDI) {
  922. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  923. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  924. return -EINVAL;
  925. }
  926. pscsi_clear_cdb_lun(cdb);
  927. /*
  928. * For REPORT LUNS we always need to emulate the response, for everything
  929. * else the default for pSCSI is to pass the command to the underlying
  930. * LLD / physical hardware.
  931. */
  932. switch (cdb[0]) {
  933. case REPORT_LUNS:
  934. ret = spc_parse_cdb(cmd, &dummy_size);
  935. if (ret)
  936. return ret;
  937. break;
  938. case READ_6:
  939. case READ_10:
  940. case READ_12:
  941. case READ_16:
  942. case WRITE_6:
  943. case WRITE_10:
  944. case WRITE_12:
  945. case WRITE_16:
  946. case WRITE_VERIFY:
  947. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  948. /* FALLTHROUGH*/
  949. default:
  950. cmd->execute_cmd = pscsi_execute_cmd;
  951. break;
  952. }
  953. return 0;
  954. }
  955. static int pscsi_execute_cmd(struct se_cmd *cmd)
  956. {
  957. struct scatterlist *sgl = cmd->t_data_sg;
  958. u32 sgl_nents = cmd->t_data_nents;
  959. enum dma_data_direction data_direction = cmd->data_direction;
  960. struct pscsi_dev_virt *pdv = cmd->se_dev->dev_ptr;
  961. struct pscsi_plugin_task *pt;
  962. struct request *req;
  963. struct bio *hbio;
  964. int ret;
  965. /*
  966. * Dynamically alloc cdb space, since it may be larger than
  967. * TCM_MAX_COMMAND_SIZE
  968. */
  969. pt = kzalloc(sizeof(*pt) + scsi_command_size(cmd->t_task_cdb), GFP_KERNEL);
  970. if (!pt) {
  971. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  972. return -ENOMEM;
  973. }
  974. cmd->priv = pt;
  975. memcpy(pt->pscsi_cdb, cmd->t_task_cdb,
  976. scsi_command_size(cmd->t_task_cdb));
  977. if (!sgl) {
  978. req = blk_get_request(pdv->pdv_sd->request_queue,
  979. (data_direction == DMA_TO_DEVICE),
  980. GFP_KERNEL);
  981. if (!req || IS_ERR(req)) {
  982. pr_err("PSCSI: blk_get_request() failed: %ld\n",
  983. req ? IS_ERR(req) : -ENOMEM);
  984. cmd->scsi_sense_reason =
  985. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  986. goto fail;
  987. }
  988. } else {
  989. BUG_ON(!cmd->data_length);
  990. ret = pscsi_map_sg(cmd, sgl, sgl_nents, data_direction, &hbio);
  991. if (ret < 0) {
  992. cmd->scsi_sense_reason =
  993. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  994. goto fail;
  995. }
  996. req = blk_make_request(pdv->pdv_sd->request_queue, hbio,
  997. GFP_KERNEL);
  998. if (IS_ERR(req)) {
  999. pr_err("pSCSI: blk_make_request() failed\n");
  1000. goto fail_free_bio;
  1001. }
  1002. }
  1003. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1004. req->end_io = pscsi_req_done;
  1005. req->end_io_data = cmd;
  1006. req->cmd_len = scsi_command_size(pt->pscsi_cdb);
  1007. req->cmd = &pt->pscsi_cdb[0];
  1008. req->sense = &pt->pscsi_sense[0];
  1009. req->sense_len = 0;
  1010. if (pdv->pdv_sd->type == TYPE_DISK)
  1011. req->timeout = PS_TIMEOUT_DISK;
  1012. else
  1013. req->timeout = PS_TIMEOUT_OTHER;
  1014. req->retries = PS_RETRY;
  1015. blk_execute_rq_nowait(pdv->pdv_sd->request_queue, NULL, req,
  1016. (cmd->sam_task_attr == MSG_HEAD_TAG),
  1017. pscsi_req_done);
  1018. return 0;
  1019. fail_free_bio:
  1020. while (hbio) {
  1021. struct bio *bio = hbio;
  1022. hbio = hbio->bi_next;
  1023. bio->bi_next = NULL;
  1024. bio_endio(bio, 0); /* XXX: should be error */
  1025. }
  1026. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1027. fail:
  1028. kfree(pt);
  1029. return -ENOMEM;
  1030. }
  1031. static unsigned char *pscsi_get_sense_buffer(struct se_cmd *cmd)
  1032. {
  1033. struct pscsi_plugin_task *pt = cmd->priv;
  1034. return pt->pscsi_sense;
  1035. }
  1036. /* pscsi_get_device_rev():
  1037. *
  1038. *
  1039. */
  1040. static u32 pscsi_get_device_rev(struct se_device *dev)
  1041. {
  1042. struct pscsi_dev_virt *pdv = dev->dev_ptr;
  1043. struct scsi_device *sd = pdv->pdv_sd;
  1044. return (sd->scsi_level - 1) ? sd->scsi_level - 1 : 1;
  1045. }
  1046. /* pscsi_get_device_type():
  1047. *
  1048. *
  1049. */
  1050. static u32 pscsi_get_device_type(struct se_device *dev)
  1051. {
  1052. struct pscsi_dev_virt *pdv = dev->dev_ptr;
  1053. struct scsi_device *sd = pdv->pdv_sd;
  1054. return sd->type;
  1055. }
  1056. static sector_t pscsi_get_blocks(struct se_device *dev)
  1057. {
  1058. struct pscsi_dev_virt *pdv = dev->dev_ptr;
  1059. if (pdv->pdv_bd && pdv->pdv_bd->bd_part)
  1060. return pdv->pdv_bd->bd_part->nr_sects;
  1061. dump_stack();
  1062. return 0;
  1063. }
  1064. static void pscsi_req_done(struct request *req, int uptodate)
  1065. {
  1066. struct se_cmd *cmd = req->end_io_data;
  1067. struct pscsi_plugin_task *pt = cmd->priv;
  1068. pt->pscsi_result = req->errors;
  1069. pt->pscsi_resid = req->resid_len;
  1070. cmd->scsi_status = status_byte(pt->pscsi_result) << 1;
  1071. if (cmd->scsi_status) {
  1072. pr_debug("PSCSI Status Byte exception at cmd: %p CDB:"
  1073. " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
  1074. pt->pscsi_result);
  1075. }
  1076. switch (host_byte(pt->pscsi_result)) {
  1077. case DID_OK:
  1078. target_complete_cmd(cmd, cmd->scsi_status);
  1079. break;
  1080. default:
  1081. pr_debug("PSCSI Host Byte exception at cmd: %p CDB:"
  1082. " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
  1083. pt->pscsi_result);
  1084. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1085. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  1086. break;
  1087. }
  1088. __blk_put_request(req->q, req);
  1089. kfree(pt);
  1090. }
  1091. static struct se_subsystem_api pscsi_template = {
  1092. .name = "pscsi",
  1093. .owner = THIS_MODULE,
  1094. .transport_type = TRANSPORT_PLUGIN_PHBA_PDEV,
  1095. .attach_hba = pscsi_attach_hba,
  1096. .detach_hba = pscsi_detach_hba,
  1097. .pmode_enable_hba = pscsi_pmode_enable_hba,
  1098. .allocate_virtdevice = pscsi_allocate_virtdevice,
  1099. .create_virtdevice = pscsi_create_virtdevice,
  1100. .free_device = pscsi_free_device,
  1101. .transport_complete = pscsi_transport_complete,
  1102. .parse_cdb = pscsi_parse_cdb,
  1103. .check_configfs_dev_params = pscsi_check_configfs_dev_params,
  1104. .set_configfs_dev_params = pscsi_set_configfs_dev_params,
  1105. .show_configfs_dev_params = pscsi_show_configfs_dev_params,
  1106. .get_sense_buffer = pscsi_get_sense_buffer,
  1107. .get_device_rev = pscsi_get_device_rev,
  1108. .get_device_type = pscsi_get_device_type,
  1109. .get_blocks = pscsi_get_blocks,
  1110. };
  1111. static int __init pscsi_module_init(void)
  1112. {
  1113. return transport_subsystem_register(&pscsi_template);
  1114. }
  1115. static void pscsi_module_exit(void)
  1116. {
  1117. transport_subsystem_release(&pscsi_template);
  1118. }
  1119. MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
  1120. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  1121. MODULE_LICENSE("GPL");
  1122. module_init(pscsi_module_init);
  1123. module_exit(pscsi_module_exit);