target_core_pscsi.c 36 KB

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