target_core_pscsi.c 38 KB

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