target_core_pscsi.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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/version.h>
  29. #include <linux/string.h>
  30. #include <linux/parser.h>
  31. #include <linux/timer.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/blk_types.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/genhd.h>
  37. #include <linux/cdrom.h>
  38. #include <linux/file.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. printk(KERN_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 = (void *)phv;
  67. printk(KERN_INFO "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. printk(KERN_INFO "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. printk(KERN_INFO "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. printk(KERN_INFO "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. printk(KERN_INFO "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. printk(KERN_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. printk(KERN_INFO "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. printk(KERN_ERR "page_83[3]: identifier"
  218. " length zero!\n");
  219. break;
  220. }
  221. printk(KERN_INFO "T10 VPD Identifer Length: %d\n", ident_len);
  222. vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
  223. if (!vpd) {
  224. printk(KERN_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. printk(KERN_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 = (sd->host->max_sectors > queue_max_hw_sectors(q)) ?
  280. queue_max_hw_sectors(q) : sd->host->max_sectors;
  281. limits->max_sectors = (sd->host->max_sectors > queue_max_sectors(q)) ?
  282. queue_max_sectors(q) : sd->host->max_sectors;
  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, (void *)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. printk(KERN_ERR "Unable to allocate memory for struct pscsi_dev_virt\n");
  334. return NULL;
  335. }
  336. pdv->pdv_se_hba = hba;
  337. printk(KERN_INFO "PSCSI: Allocated pdv: %p for %s\n", pdv, name);
  338. return (void *)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 = (struct pscsi_hba_virt *)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. printk(KERN_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. printk(KERN_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. printk(KERN_INFO "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 = (struct pscsi_hba_virt *)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. printk(KERN_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. printk(KERN_INFO "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 = (struct pscsi_hba_virt *)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. printk(KERN_INFO "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 = (struct pscsi_dev_virt *)p;
  444. struct se_device *dev;
  445. struct scsi_device *sd;
  446. struct pscsi_hba_virt *phv = (struct pscsi_hba_virt *)hba->hba_ptr;
  447. struct Scsi_Host *sh = phv->phv_lld_host;
  448. int legacy_mode_enable = 0;
  449. if (!(pdv)) {
  450. printk(KERN_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. printk(KERN_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_VIRUTAL_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. printk(KERN_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_VIRUTAL_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. printk(KERN_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. printk(KERN_ERR "pSCSI: Unable to locate"
  496. " pdv_host_id: %d\n", pdv->pdv_host_id);
  497. return (struct se_device *) sh;
  498. }
  499. }
  500. } else {
  501. if (phv->phv_mode == PHV_VIRUTAL_HOST_ID) {
  502. printk(KERN_ERR "pSCSI: PHV_VIRUTAL_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_VIRUTAL_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. printk(KERN_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_VIRUTAL_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 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->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 = task->task_se_cmd->t_task->t_task_buf;
  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. }
  619. }
  620. after_mode_sense:
  621. if (sd->type != TYPE_TAPE)
  622. goto after_mode_select;
  623. /*
  624. * Hack to correctly obtain the initiator requested blocksize for
  625. * TYPE_TAPE. Since this value is dependent upon each tape media,
  626. * struct scsi_device->sector_size will not contain the correct value
  627. * by default, so we go ahead and set it so
  628. * TRANSPORT(dev)->get_blockdev() returns the correct value to the
  629. * storage engine.
  630. */
  631. if (((cdb[0] == MODE_SELECT) || (cdb[0] == MODE_SELECT_10)) &&
  632. (status_byte(result) << 1) == SAM_STAT_GOOD) {
  633. unsigned char *buf;
  634. struct scatterlist *sg = task->task_sg;
  635. u16 bdl;
  636. u32 blocksize;
  637. buf = sg_virt(&sg[0]);
  638. if (!(buf)) {
  639. printk(KERN_ERR "Unable to get buf for scatterlist\n");
  640. goto after_mode_select;
  641. }
  642. if (cdb[0] == MODE_SELECT)
  643. bdl = (buf[3]);
  644. else
  645. bdl = (buf[6] << 8) | (buf[7]);
  646. if (!bdl)
  647. goto after_mode_select;
  648. if (cdb[0] == MODE_SELECT)
  649. blocksize = (buf[9] << 16) | (buf[10] << 8) |
  650. (buf[11]);
  651. else
  652. blocksize = (buf[13] << 16) | (buf[14] << 8) |
  653. (buf[15]);
  654. sd->sector_size = blocksize;
  655. }
  656. after_mode_select:
  657. if (status_byte(result) & CHECK_CONDITION)
  658. return 1;
  659. return 0;
  660. }
  661. static struct se_task *
  662. pscsi_alloc_task(struct se_cmd *cmd)
  663. {
  664. struct pscsi_plugin_task *pt;
  665. unsigned char *cdb = cmd->t_task->t_task_cdb;
  666. pt = kzalloc(sizeof(struct pscsi_plugin_task), GFP_KERNEL);
  667. if (!pt) {
  668. printk(KERN_ERR "Unable to allocate struct pscsi_plugin_task\n");
  669. return NULL;
  670. }
  671. /*
  672. * If TCM Core is signaling a > TCM_MAX_COMMAND_SIZE allocation,
  673. * allocate the extended CDB buffer for per struct se_task context
  674. * pt->pscsi_cdb now.
  675. */
  676. if (cmd->t_task->t_task_cdb != cmd->t_task->__t_task_cdb) {
  677. pt->pscsi_cdb = kzalloc(scsi_command_size(cdb), GFP_KERNEL);
  678. if (!(pt->pscsi_cdb)) {
  679. printk(KERN_ERR "pSCSI: Unable to allocate extended"
  680. " pt->pscsi_cdb\n");
  681. kfree(pt);
  682. return NULL;
  683. }
  684. } else
  685. pt->pscsi_cdb = &pt->__pscsi_cdb[0];
  686. return &pt->pscsi_task;
  687. }
  688. static inline void pscsi_blk_init_request(
  689. struct se_task *task,
  690. struct pscsi_plugin_task *pt,
  691. struct request *req,
  692. int bidi_read)
  693. {
  694. /*
  695. * Defined as "scsi command" in include/linux/blkdev.h.
  696. */
  697. req->cmd_type = REQ_TYPE_BLOCK_PC;
  698. /*
  699. * For the extra BIDI-COMMAND READ struct request we do not
  700. * need to setup the remaining structure members
  701. */
  702. if (bidi_read)
  703. return;
  704. /*
  705. * Setup the done function pointer for struct request,
  706. * also set the end_io_data pointer.to struct se_task.
  707. */
  708. req->end_io = pscsi_req_done;
  709. req->end_io_data = (void *)task;
  710. /*
  711. * Load the referenced struct se_task's SCSI CDB into
  712. * include/linux/blkdev.h:struct request->cmd
  713. */
  714. req->cmd_len = scsi_command_size(pt->pscsi_cdb);
  715. req->cmd = &pt->pscsi_cdb[0];
  716. /*
  717. * Setup pointer for outgoing sense data.
  718. */
  719. req->sense = (void *)&pt->pscsi_sense[0];
  720. req->sense_len = 0;
  721. }
  722. /*
  723. * Used for pSCSI data payloads for all *NON* SCF_SCSI_DATA_SG_IO_CDB
  724. */
  725. static int pscsi_blk_get_request(struct se_task *task)
  726. {
  727. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  728. struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
  729. pt->pscsi_req = blk_get_request(pdv->pdv_sd->request_queue,
  730. (task->task_data_direction == DMA_TO_DEVICE),
  731. GFP_KERNEL);
  732. if (!(pt->pscsi_req) || IS_ERR(pt->pscsi_req)) {
  733. printk(KERN_ERR "PSCSI: blk_get_request() failed: %ld\n",
  734. IS_ERR(pt->pscsi_req));
  735. return PYX_TRANSPORT_LU_COMM_FAILURE;
  736. }
  737. /*
  738. * Setup the newly allocated struct request for REQ_TYPE_BLOCK_PC,
  739. * and setup rq callback, CDB and sense.
  740. */
  741. pscsi_blk_init_request(task, pt, pt->pscsi_req, 0);
  742. return 0;
  743. }
  744. /* pscsi_do_task(): (Part of se_subsystem_api_t template)
  745. *
  746. *
  747. */
  748. static int pscsi_do_task(struct se_task *task)
  749. {
  750. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  751. struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
  752. /*
  753. * Set the struct request->timeout value based on peripheral
  754. * device type from SCSI.
  755. */
  756. if (pdv->pdv_sd->type == TYPE_DISK)
  757. pt->pscsi_req->timeout = PS_TIMEOUT_DISK;
  758. else
  759. pt->pscsi_req->timeout = PS_TIMEOUT_OTHER;
  760. pt->pscsi_req->retries = PS_RETRY;
  761. /*
  762. * Queue the struct request into the struct scsi_device->request_queue.
  763. * Also check for HEAD_OF_QUEUE SAM TASK attr from received se_cmd
  764. * descriptor
  765. */
  766. blk_execute_rq_nowait(pdv->pdv_sd->request_queue, NULL, pt->pscsi_req,
  767. (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG),
  768. pscsi_req_done);
  769. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  770. }
  771. static void pscsi_free_task(struct se_task *task)
  772. {
  773. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  774. struct se_cmd *cmd = task->task_se_cmd;
  775. /*
  776. * Release the extended CDB allocation from pscsi_alloc_task()
  777. * if one exists.
  778. */
  779. if (cmd->t_task->t_task_cdb != cmd->t_task->__t_task_cdb)
  780. kfree(pt->pscsi_cdb);
  781. /*
  782. * We do not release the bio(s) here associated with this task, as
  783. * this is handled by bio_put() and pscsi_bi_endio().
  784. */
  785. kfree(pt);
  786. }
  787. enum {
  788. Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
  789. Opt_scsi_lun_id, Opt_err
  790. };
  791. static match_table_t tokens = {
  792. {Opt_scsi_host_id, "scsi_host_id=%d"},
  793. {Opt_scsi_channel_id, "scsi_channel_id=%d"},
  794. {Opt_scsi_target_id, "scsi_target_id=%d"},
  795. {Opt_scsi_lun_id, "scsi_lun_id=%d"},
  796. {Opt_err, NULL}
  797. };
  798. static ssize_t pscsi_set_configfs_dev_params(struct se_hba *hba,
  799. struct se_subsystem_dev *se_dev,
  800. const char *page,
  801. ssize_t count)
  802. {
  803. struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
  804. struct pscsi_hba_virt *phv = hba->hba_ptr;
  805. char *orig, *ptr, *opts;
  806. substring_t args[MAX_OPT_ARGS];
  807. int ret = 0, arg, token;
  808. opts = kstrdup(page, GFP_KERNEL);
  809. if (!opts)
  810. return -ENOMEM;
  811. orig = opts;
  812. while ((ptr = strsep(&opts, ",")) != NULL) {
  813. if (!*ptr)
  814. continue;
  815. token = match_token(ptr, tokens, args);
  816. switch (token) {
  817. case Opt_scsi_host_id:
  818. if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
  819. printk(KERN_ERR "PSCSI[%d]: Unable to accept"
  820. " scsi_host_id while phv_mode =="
  821. " PHV_LLD_SCSI_HOST_NO\n",
  822. phv->phv_host_id);
  823. ret = -EINVAL;
  824. goto out;
  825. }
  826. match_int(args, &arg);
  827. pdv->pdv_host_id = arg;
  828. printk(KERN_INFO "PSCSI[%d]: Referencing SCSI Host ID:"
  829. " %d\n", phv->phv_host_id, pdv->pdv_host_id);
  830. pdv->pdv_flags |= PDF_HAS_VIRT_HOST_ID;
  831. break;
  832. case Opt_scsi_channel_id:
  833. match_int(args, &arg);
  834. pdv->pdv_channel_id = arg;
  835. printk(KERN_INFO "PSCSI[%d]: Referencing SCSI Channel"
  836. " ID: %d\n", phv->phv_host_id,
  837. pdv->pdv_channel_id);
  838. pdv->pdv_flags |= PDF_HAS_CHANNEL_ID;
  839. break;
  840. case Opt_scsi_target_id:
  841. match_int(args, &arg);
  842. pdv->pdv_target_id = arg;
  843. printk(KERN_INFO "PSCSI[%d]: Referencing SCSI Target"
  844. " ID: %d\n", phv->phv_host_id,
  845. pdv->pdv_target_id);
  846. pdv->pdv_flags |= PDF_HAS_TARGET_ID;
  847. break;
  848. case Opt_scsi_lun_id:
  849. match_int(args, &arg);
  850. pdv->pdv_lun_id = arg;
  851. printk(KERN_INFO "PSCSI[%d]: Referencing SCSI LUN ID:"
  852. " %d\n", phv->phv_host_id, pdv->pdv_lun_id);
  853. pdv->pdv_flags |= PDF_HAS_LUN_ID;
  854. break;
  855. default:
  856. break;
  857. }
  858. }
  859. out:
  860. kfree(orig);
  861. return (!ret) ? count : ret;
  862. }
  863. static ssize_t pscsi_check_configfs_dev_params(
  864. struct se_hba *hba,
  865. struct se_subsystem_dev *se_dev)
  866. {
  867. struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
  868. if (!(pdv->pdv_flags & PDF_HAS_CHANNEL_ID) ||
  869. !(pdv->pdv_flags & PDF_HAS_TARGET_ID) ||
  870. !(pdv->pdv_flags & PDF_HAS_LUN_ID)) {
  871. printk(KERN_ERR "Missing scsi_channel_id=, scsi_target_id= and"
  872. " scsi_lun_id= parameters\n");
  873. return -EINVAL;
  874. }
  875. return 0;
  876. }
  877. static ssize_t pscsi_show_configfs_dev_params(struct se_hba *hba,
  878. struct se_subsystem_dev *se_dev,
  879. char *b)
  880. {
  881. struct pscsi_hba_virt *phv = hba->hba_ptr;
  882. struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
  883. struct scsi_device *sd = pdv->pdv_sd;
  884. unsigned char host_id[16];
  885. ssize_t bl;
  886. int i;
  887. if (phv->phv_mode == PHV_VIRUTAL_HOST_ID)
  888. snprintf(host_id, 16, "%d", pdv->pdv_host_id);
  889. else
  890. snprintf(host_id, 16, "PHBA Mode");
  891. bl = sprintf(b, "SCSI Device Bus Location:"
  892. " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
  893. pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id,
  894. host_id);
  895. if (sd) {
  896. bl += sprintf(b + bl, " ");
  897. bl += sprintf(b + bl, "Vendor: ");
  898. for (i = 0; i < 8; i++) {
  899. if (ISPRINT(sd->vendor[i])) /* printable character? */
  900. bl += sprintf(b + bl, "%c", sd->vendor[i]);
  901. else
  902. bl += sprintf(b + bl, " ");
  903. }
  904. bl += sprintf(b + bl, " Model: ");
  905. for (i = 0; i < 16; i++) {
  906. if (ISPRINT(sd->model[i])) /* printable character ? */
  907. bl += sprintf(b + bl, "%c", sd->model[i]);
  908. else
  909. bl += sprintf(b + bl, " ");
  910. }
  911. bl += sprintf(b + bl, " Rev: ");
  912. for (i = 0; i < 4; i++) {
  913. if (ISPRINT(sd->rev[i])) /* printable character ? */
  914. bl += sprintf(b + bl, "%c", sd->rev[i]);
  915. else
  916. bl += sprintf(b + bl, " ");
  917. }
  918. bl += sprintf(b + bl, "\n");
  919. }
  920. return bl;
  921. }
  922. static void pscsi_bi_endio(struct bio *bio, int error)
  923. {
  924. bio_put(bio);
  925. }
  926. static inline struct bio *pscsi_get_bio(struct pscsi_dev_virt *pdv, int sg_num)
  927. {
  928. struct bio *bio;
  929. /*
  930. * Use bio_malloc() following the comment in for bio -> struct request
  931. * in block/blk-core.c:blk_make_request()
  932. */
  933. bio = bio_kmalloc(GFP_KERNEL, sg_num);
  934. if (!(bio)) {
  935. printk(KERN_ERR "PSCSI: bio_kmalloc() failed\n");
  936. return NULL;
  937. }
  938. bio->bi_end_io = pscsi_bi_endio;
  939. return bio;
  940. }
  941. #if 0
  942. #define DEBUG_PSCSI(x...) printk(x)
  943. #else
  944. #define DEBUG_PSCSI(x...)
  945. #endif
  946. static int __pscsi_map_task_SG(
  947. struct se_task *task,
  948. struct scatterlist *task_sg,
  949. u32 task_sg_num,
  950. int bidi_read)
  951. {
  952. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  953. struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
  954. struct bio *bio = NULL, *hbio = NULL, *tbio = NULL;
  955. struct page *page;
  956. struct scatterlist *sg;
  957. u32 data_len = task->task_size, i, len, bytes, off;
  958. int nr_pages = (task->task_size + task_sg[0].offset +
  959. PAGE_SIZE - 1) >> PAGE_SHIFT;
  960. int nr_vecs = 0, rc, ret = PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  961. int rw = (task->task_data_direction == DMA_TO_DEVICE);
  962. if (!task->task_size)
  963. return 0;
  964. /*
  965. * For SCF_SCSI_DATA_SG_IO_CDB, Use fs/bio.c:bio_add_page() to setup
  966. * the bio_vec maplist from TC< struct se_mem -> task->task_sg ->
  967. * struct scatterlist memory. The struct se_task->task_sg[] currently needs
  968. * to be attached to struct bios for submission to Linux/SCSI using
  969. * struct request to struct scsi_device->request_queue.
  970. *
  971. * Note that this will be changing post v2.6.28 as Target_Core_Mod/pSCSI
  972. * is ported to upstream SCSI passthrough functionality that accepts
  973. * struct scatterlist->page_link or struct page as a paraemeter.
  974. */
  975. DEBUG_PSCSI("PSCSI: nr_pages: %d\n", nr_pages);
  976. for_each_sg(task_sg, sg, task_sg_num, i) {
  977. page = sg_page(sg);
  978. off = sg->offset;
  979. len = sg->length;
  980. DEBUG_PSCSI("PSCSI: i: %d page: %p len: %d off: %d\n", i,
  981. page, len, off);
  982. while (len > 0 && data_len > 0) {
  983. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  984. bytes = min(bytes, data_len);
  985. if (!(bio)) {
  986. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  987. nr_pages -= nr_vecs;
  988. /*
  989. * Calls bio_kmalloc() and sets bio->bi_end_io()
  990. */
  991. bio = pscsi_get_bio(pdv, nr_vecs);
  992. if (!(bio))
  993. goto fail;
  994. if (rw)
  995. bio->bi_rw |= REQ_WRITE;
  996. DEBUG_PSCSI("PSCSI: Allocated bio: %p,"
  997. " dir: %s nr_vecs: %d\n", bio,
  998. (rw) ? "rw" : "r", nr_vecs);
  999. /*
  1000. * Set *hbio pointer to handle the case:
  1001. * nr_pages > BIO_MAX_PAGES, where additional
  1002. * bios need to be added to complete a given
  1003. * struct se_task
  1004. */
  1005. if (!hbio)
  1006. hbio = tbio = bio;
  1007. else
  1008. tbio = tbio->bi_next = bio;
  1009. }
  1010. DEBUG_PSCSI("PSCSI: Calling bio_add_pc_page() i: %d"
  1011. " bio: %p page: %p len: %d off: %d\n", i, bio,
  1012. page, len, off);
  1013. rc = bio_add_pc_page(pdv->pdv_sd->request_queue,
  1014. bio, page, bytes, off);
  1015. if (rc != bytes)
  1016. goto fail;
  1017. DEBUG_PSCSI("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
  1018. bio->bi_vcnt, nr_vecs);
  1019. if (bio->bi_vcnt > nr_vecs) {
  1020. DEBUG_PSCSI("PSCSI: Reached bio->bi_vcnt max:"
  1021. " %d i: %d bio: %p, allocating another"
  1022. " bio\n", bio->bi_vcnt, i, bio);
  1023. /*
  1024. * Clear the pointer so that another bio will
  1025. * be allocated with pscsi_get_bio() above, the
  1026. * current bio has already been set *tbio and
  1027. * bio->bi_next.
  1028. */
  1029. bio = NULL;
  1030. }
  1031. page++;
  1032. len -= bytes;
  1033. data_len -= bytes;
  1034. off = 0;
  1035. }
  1036. }
  1037. /*
  1038. * Setup the primary pt->pscsi_req used for non BIDI and BIDI-COMMAND
  1039. * primary SCSI WRITE poayload mapped for struct se_task->task_sg[]
  1040. */
  1041. if (!(bidi_read)) {
  1042. /*
  1043. * Starting with v2.6.31, call blk_make_request() passing in *hbio to
  1044. * allocate the pSCSI task a struct request.
  1045. */
  1046. pt->pscsi_req = blk_make_request(pdv->pdv_sd->request_queue,
  1047. hbio, GFP_KERNEL);
  1048. if (!(pt->pscsi_req)) {
  1049. printk(KERN_ERR "pSCSI: blk_make_request() failed\n");
  1050. goto fail;
  1051. }
  1052. /*
  1053. * Setup the newly allocated struct request for REQ_TYPE_BLOCK_PC,
  1054. * and setup rq callback, CDB and sense.
  1055. */
  1056. pscsi_blk_init_request(task, pt, pt->pscsi_req, 0);
  1057. return task->task_sg_num;
  1058. }
  1059. /*
  1060. * Setup the secondary pt->pscsi_req->next_rq used for the extra BIDI-COMMAND
  1061. * SCSI READ paylaod mapped for struct se_task->task_sg_bidi[]
  1062. */
  1063. pt->pscsi_req->next_rq = blk_make_request(pdv->pdv_sd->request_queue,
  1064. hbio, GFP_KERNEL);
  1065. if (!(pt->pscsi_req->next_rq)) {
  1066. printk(KERN_ERR "pSCSI: blk_make_request() failed for BIDI\n");
  1067. goto fail;
  1068. }
  1069. pscsi_blk_init_request(task, pt, pt->pscsi_req->next_rq, 1);
  1070. return task->task_sg_num;
  1071. fail:
  1072. while (hbio) {
  1073. bio = hbio;
  1074. hbio = hbio->bi_next;
  1075. bio->bi_next = NULL;
  1076. bio_endio(bio, 0);
  1077. }
  1078. return ret;
  1079. }
  1080. static int pscsi_map_task_SG(struct se_task *task)
  1081. {
  1082. int ret;
  1083. /*
  1084. * Setup the main struct request for the task->task_sg[] payload
  1085. */
  1086. ret = __pscsi_map_task_SG(task, task->task_sg, task->task_sg_num, 0);
  1087. if (ret >= 0 && task->task_sg_bidi) {
  1088. /*
  1089. * If present, set up the extra BIDI-COMMAND SCSI READ
  1090. * struct request and payload.
  1091. */
  1092. ret = __pscsi_map_task_SG(task, task->task_sg_bidi,
  1093. task->task_sg_num, 1);
  1094. }
  1095. if (ret < 0)
  1096. return PYX_TRANSPORT_LU_COMM_FAILURE;
  1097. return 0;
  1098. }
  1099. /* pscsi_map_task_non_SG():
  1100. *
  1101. *
  1102. */
  1103. static int pscsi_map_task_non_SG(struct se_task *task)
  1104. {
  1105. struct se_cmd *cmd = task->task_se_cmd;
  1106. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  1107. struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
  1108. int ret = 0;
  1109. if (pscsi_blk_get_request(task) < 0)
  1110. return PYX_TRANSPORT_LU_COMM_FAILURE;
  1111. if (!task->task_size)
  1112. return 0;
  1113. ret = blk_rq_map_kern(pdv->pdv_sd->request_queue,
  1114. pt->pscsi_req, cmd->t_task->t_task_buf,
  1115. task->task_size, GFP_KERNEL);
  1116. if (ret < 0) {
  1117. printk(KERN_ERR "PSCSI: blk_rq_map_kern() failed: %d\n", ret);
  1118. return PYX_TRANSPORT_LU_COMM_FAILURE;
  1119. }
  1120. return 0;
  1121. }
  1122. static int pscsi_CDB_none(struct se_task *task)
  1123. {
  1124. return pscsi_blk_get_request(task);
  1125. }
  1126. /* pscsi_get_cdb():
  1127. *
  1128. *
  1129. */
  1130. static unsigned char *pscsi_get_cdb(struct se_task *task)
  1131. {
  1132. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  1133. return pt->pscsi_cdb;
  1134. }
  1135. /* pscsi_get_sense_buffer():
  1136. *
  1137. *
  1138. */
  1139. static unsigned char *pscsi_get_sense_buffer(struct se_task *task)
  1140. {
  1141. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  1142. return (unsigned char *)&pt->pscsi_sense[0];
  1143. }
  1144. /* pscsi_get_device_rev():
  1145. *
  1146. *
  1147. */
  1148. static u32 pscsi_get_device_rev(struct se_device *dev)
  1149. {
  1150. struct pscsi_dev_virt *pdv = dev->dev_ptr;
  1151. struct scsi_device *sd = pdv->pdv_sd;
  1152. return (sd->scsi_level - 1) ? sd->scsi_level - 1 : 1;
  1153. }
  1154. /* pscsi_get_device_type():
  1155. *
  1156. *
  1157. */
  1158. static u32 pscsi_get_device_type(struct se_device *dev)
  1159. {
  1160. struct pscsi_dev_virt *pdv = dev->dev_ptr;
  1161. struct scsi_device *sd = pdv->pdv_sd;
  1162. return sd->type;
  1163. }
  1164. static sector_t pscsi_get_blocks(struct se_device *dev)
  1165. {
  1166. struct pscsi_dev_virt *pdv = dev->dev_ptr;
  1167. if (pdv->pdv_bd && pdv->pdv_bd->bd_part)
  1168. return pdv->pdv_bd->bd_part->nr_sects;
  1169. dump_stack();
  1170. return 0;
  1171. }
  1172. /* pscsi_handle_SAM_STATUS_failures():
  1173. *
  1174. *
  1175. */
  1176. static inline void pscsi_process_SAM_status(
  1177. struct se_task *task,
  1178. struct pscsi_plugin_task *pt)
  1179. {
  1180. task->task_scsi_status = status_byte(pt->pscsi_result);
  1181. if ((task->task_scsi_status)) {
  1182. task->task_scsi_status <<= 1;
  1183. printk(KERN_INFO "PSCSI Status Byte exception at task: %p CDB:"
  1184. " 0x%02x Result: 0x%08x\n", task, pt->pscsi_cdb[0],
  1185. pt->pscsi_result);
  1186. }
  1187. switch (host_byte(pt->pscsi_result)) {
  1188. case DID_OK:
  1189. transport_complete_task(task, (!task->task_scsi_status));
  1190. break;
  1191. default:
  1192. printk(KERN_INFO "PSCSI Host Byte exception at task: %p CDB:"
  1193. " 0x%02x Result: 0x%08x\n", task, pt->pscsi_cdb[0],
  1194. pt->pscsi_result);
  1195. task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
  1196. task->task_error_status = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1197. task->task_se_cmd->transport_error_status =
  1198. PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1199. transport_complete_task(task, 0);
  1200. break;
  1201. }
  1202. }
  1203. static void pscsi_req_done(struct request *req, int uptodate)
  1204. {
  1205. struct se_task *task = req->end_io_data;
  1206. struct pscsi_plugin_task *pt = PSCSI_TASK(task);
  1207. pt->pscsi_result = req->errors;
  1208. pt->pscsi_resid = req->resid_len;
  1209. pscsi_process_SAM_status(task, pt);
  1210. /*
  1211. * Release BIDI-READ if present
  1212. */
  1213. if (req->next_rq != NULL)
  1214. __blk_put_request(req->q, req->next_rq);
  1215. __blk_put_request(req->q, req);
  1216. pt->pscsi_req = NULL;
  1217. }
  1218. static struct se_subsystem_api pscsi_template = {
  1219. .name = "pscsi",
  1220. .owner = THIS_MODULE,
  1221. .transport_type = TRANSPORT_PLUGIN_PHBA_PDEV,
  1222. .cdb_none = pscsi_CDB_none,
  1223. .map_task_non_SG = pscsi_map_task_non_SG,
  1224. .map_task_SG = pscsi_map_task_SG,
  1225. .attach_hba = pscsi_attach_hba,
  1226. .detach_hba = pscsi_detach_hba,
  1227. .pmode_enable_hba = pscsi_pmode_enable_hba,
  1228. .allocate_virtdevice = pscsi_allocate_virtdevice,
  1229. .create_virtdevice = pscsi_create_virtdevice,
  1230. .free_device = pscsi_free_device,
  1231. .transport_complete = pscsi_transport_complete,
  1232. .alloc_task = pscsi_alloc_task,
  1233. .do_task = pscsi_do_task,
  1234. .free_task = pscsi_free_task,
  1235. .check_configfs_dev_params = pscsi_check_configfs_dev_params,
  1236. .set_configfs_dev_params = pscsi_set_configfs_dev_params,
  1237. .show_configfs_dev_params = pscsi_show_configfs_dev_params,
  1238. .get_cdb = pscsi_get_cdb,
  1239. .get_sense_buffer = pscsi_get_sense_buffer,
  1240. .get_device_rev = pscsi_get_device_rev,
  1241. .get_device_type = pscsi_get_device_type,
  1242. .get_blocks = pscsi_get_blocks,
  1243. };
  1244. static int __init pscsi_module_init(void)
  1245. {
  1246. return transport_subsystem_register(&pscsi_template);
  1247. }
  1248. static void pscsi_module_exit(void)
  1249. {
  1250. transport_subsystem_release(&pscsi_template);
  1251. }
  1252. MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
  1253. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  1254. MODULE_LICENSE("GPL");
  1255. module_init(pscsi_module_init);
  1256. module_exit(pscsi_module_exit);