target_core_pscsi.c 38 KB

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