target_core_pscsi.c 32 KB

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