target_core_pscsi.c 32 KB

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