target_core_spc.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SCSI Primary Commands (SPC) parsing and emulation.
  4. *
  5. * (c) Copyright 2002-2013 Datera, Inc.
  6. *
  7. * Nicholas A. Bellinger <nab@kernel.org>
  8. */
  9. #include <linux/hex.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/unaligned.h>
  13. #include <scsi/scsi_proto.h>
  14. #include <scsi/scsi_common.h>
  15. #include <scsi/scsi_tcq.h>
  16. #include <target/target_core_base.h>
  17. #include <target/target_core_backend.h>
  18. #include <target/target_core_fabric.h>
  19. #include "target_core_internal.h"
  20. #include "target_core_alua.h"
  21. #include "target_core_pr.h"
  22. #include "target_core_ua.h"
  23. #include "target_core_xcopy.h"
  24. #define PD_TEXT_ID_INFO_HDR_LEN 4
  25. static void spc_fill_alua_data(struct se_lun *lun, unsigned char *buf)
  26. {
  27. struct t10_alua_tg_pt_gp *tg_pt_gp;
  28. /*
  29. * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
  30. */
  31. buf[5] = 0x80;
  32. /*
  33. * Set TPGS field for explicit and/or implicit ALUA access type
  34. * and opteration.
  35. *
  36. * See spc4r17 section 6.4.2 Table 135
  37. */
  38. rcu_read_lock();
  39. tg_pt_gp = rcu_dereference(lun->lun_tg_pt_gp);
  40. if (tg_pt_gp)
  41. buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
  42. rcu_read_unlock();
  43. }
  44. static u16
  45. spc_find_scsi_transport_vd(int proto_id)
  46. {
  47. switch (proto_id) {
  48. case SCSI_PROTOCOL_FCP:
  49. return SCSI_VERSION_DESCRIPTOR_FCP4;
  50. case SCSI_PROTOCOL_ISCSI:
  51. return SCSI_VERSION_DESCRIPTOR_ISCSI;
  52. case SCSI_PROTOCOL_SAS:
  53. return SCSI_VERSION_DESCRIPTOR_SAS3;
  54. case SCSI_PROTOCOL_SBP:
  55. return SCSI_VERSION_DESCRIPTOR_SBP3;
  56. case SCSI_PROTOCOL_SRP:
  57. return SCSI_VERSION_DESCRIPTOR_SRP;
  58. default:
  59. pr_warn("Cannot find VERSION DESCRIPTOR value for unknown SCSI"
  60. " transport PROTOCOL IDENTIFIER %#x\n", proto_id);
  61. return 0;
  62. }
  63. }
  64. sense_reason_t
  65. spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf)
  66. {
  67. struct se_lun *lun = cmd->se_lun;
  68. struct se_portal_group *tpg = lun->lun_tpg;
  69. struct se_device *dev = cmd->se_dev;
  70. struct se_session *sess = cmd->se_sess;
  71. /* Set RMB (removable media) for tape devices */
  72. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  73. buf[1] = 0x80;
  74. buf[2] = 0x06; /* SPC-4 */
  75. /*
  76. * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
  77. *
  78. * SPC4 says:
  79. * A RESPONSE DATA FORMAT field set to 2h indicates that the
  80. * standard INQUIRY data is in the format defined in this
  81. * standard. Response data format values less than 2h are
  82. * obsolete. Response data format values greater than 2h are
  83. * reserved.
  84. */
  85. buf[3] = 2;
  86. /*
  87. * Enable SCCS and TPGS fields for Emulated ALUA
  88. */
  89. spc_fill_alua_data(lun, buf);
  90. /*
  91. * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY
  92. */
  93. if (dev->dev_attrib.emulate_3pc)
  94. buf[5] |= 0x8;
  95. /*
  96. * Set Protection (PROTECT) bit when DIF has been enabled on the
  97. * device, and the fabric supports VERIFY + PASS. Also report
  98. * PROTECT=1 if sess_prot_type has been configured to allow T10-PI
  99. * to unprotected devices.
  100. */
  101. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  102. if (dev->dev_attrib.pi_prot_type || cmd->se_sess->sess_prot_type)
  103. buf[5] |= 0x1;
  104. }
  105. /*
  106. * Set MULTIP bit to indicate presence of multiple SCSI target ports
  107. */
  108. if (dev->export_count > 1)
  109. buf[6] |= 0x10;
  110. buf[7] = 0x2; /* CmdQue=1 */
  111. /*
  112. * ASCII data fields described as being left-aligned shall have any
  113. * unused bytes at the end of the field (i.e., highest offset) and the
  114. * unused bytes shall be filled with ASCII space characters (20h).
  115. */
  116. memset(&buf[8], 0x20,
  117. INQUIRY_VENDOR_LEN + INQUIRY_MODEL_LEN + INQUIRY_REVISION_LEN);
  118. memcpy(&buf[8], dev->t10_wwn.vendor,
  119. strnlen(dev->t10_wwn.vendor, INQUIRY_VENDOR_LEN));
  120. memcpy(&buf[16], dev->t10_wwn.model,
  121. strnlen(dev->t10_wwn.model, INQUIRY_MODEL_LEN));
  122. memcpy(&buf[32], dev->t10_wwn.revision,
  123. strnlen(dev->t10_wwn.revision, INQUIRY_REVISION_LEN));
  124. /*
  125. * Set the VERSION DESCRIPTOR fields
  126. */
  127. put_unaligned_be16(SCSI_VERSION_DESCRIPTOR_SAM5, &buf[58]);
  128. put_unaligned_be16(spc_find_scsi_transport_vd(tpg->proto_id), &buf[60]);
  129. put_unaligned_be16(SCSI_VERSION_DESCRIPTOR_SPC4, &buf[62]);
  130. if (cmd->se_dev->transport->get_device_type(dev) == TYPE_DISK)
  131. put_unaligned_be16(SCSI_VERSION_DESCRIPTOR_SBC3, &buf[64]);
  132. buf[4] = 91; /* Set additional length to 91 */
  133. return 0;
  134. }
  135. EXPORT_SYMBOL(spc_emulate_inquiry_std);
  136. /* unit serial number */
  137. static sense_reason_t
  138. spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
  139. {
  140. struct se_device *dev = cmd->se_dev;
  141. u16 len;
  142. if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  143. len = sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial);
  144. len++; /* Extra Byte for NULL Terminator */
  145. buf[3] = len;
  146. }
  147. return 0;
  148. }
  149. /*
  150. * Generate NAA IEEE Registered Extended designator
  151. */
  152. void spc_gen_naa_6h_vendor_specific(struct se_device *dev,
  153. unsigned char *buf)
  154. {
  155. unsigned char *p = &dev->t10_wwn.unit_serial[0];
  156. u32 company_id = dev->t10_wwn.company_id;
  157. int cnt, off = 0;
  158. bool next = true;
  159. /*
  160. * Start NAA IEEE Registered Extended Identifier/Designator
  161. */
  162. buf[off] = 0x6 << 4;
  163. /* IEEE COMPANY_ID */
  164. buf[off++] |= (company_id >> 20) & 0xf;
  165. buf[off++] = (company_id >> 12) & 0xff;
  166. buf[off++] = (company_id >> 4) & 0xff;
  167. buf[off] = (company_id & 0xf) << 4;
  168. /*
  169. * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
  170. * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
  171. * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
  172. * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
  173. * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
  174. * per device uniqeness.
  175. */
  176. for (cnt = off + 13; *p && off < cnt; p++) {
  177. int val = hex_to_bin(*p);
  178. if (val < 0)
  179. continue;
  180. if (next) {
  181. next = false;
  182. buf[off++] |= val;
  183. } else {
  184. next = true;
  185. buf[off] = val << 4;
  186. }
  187. }
  188. }
  189. /*
  190. * Device identification VPD, for a complete list of
  191. * DESIGNATOR TYPEs see spc4r17 Table 459.
  192. */
  193. sense_reason_t
  194. spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
  195. {
  196. struct se_device *dev = cmd->se_dev;
  197. struct se_lun *lun = cmd->se_lun;
  198. struct se_portal_group *tpg = NULL;
  199. struct t10_alua_lu_gp_member *lu_gp_mem;
  200. struct t10_alua_tg_pt_gp *tg_pt_gp;
  201. unsigned char *prod = &dev->t10_wwn.model[0];
  202. u32 off = 0;
  203. u16 len = 0, id_len;
  204. off = 4;
  205. /*
  206. * NAA IEEE Registered Extended Assigned designator format, see
  207. * spc4r17 section 7.7.3.6.5
  208. *
  209. * We depend upon a target_core_mod/ConfigFS provided
  210. * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
  211. * value in order to return the NAA id.
  212. */
  213. if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL))
  214. goto check_t10_vend_desc;
  215. /* CODE SET == Binary */
  216. buf[off++] = 0x1;
  217. /* Set ASSOCIATION == addressed logical unit: 0)b */
  218. buf[off] = 0x00;
  219. /* Identifier/Designator type == NAA identifier */
  220. buf[off++] |= 0x3;
  221. off++;
  222. /* Identifier/Designator length */
  223. buf[off++] = 0x10;
  224. /* NAA IEEE Registered Extended designator */
  225. spc_gen_naa_6h_vendor_specific(dev, &buf[off]);
  226. len = 20;
  227. off = (len + 4);
  228. check_t10_vend_desc:
  229. /*
  230. * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
  231. */
  232. id_len = 8; /* For Vendor field */
  233. if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL)
  234. id_len += sprintf(&buf[off+12], "%s:%s", prod,
  235. &dev->t10_wwn.unit_serial[0]);
  236. buf[off] = 0x2; /* ASCII */
  237. buf[off+1] = 0x1; /* T10 Vendor ID */
  238. buf[off+2] = 0x0;
  239. /* left align Vendor ID and pad with spaces */
  240. memset(&buf[off+4], 0x20, INQUIRY_VENDOR_LEN);
  241. memcpy(&buf[off+4], dev->t10_wwn.vendor,
  242. strnlen(dev->t10_wwn.vendor, INQUIRY_VENDOR_LEN));
  243. /* Extra Byte for NULL Terminator */
  244. id_len++;
  245. /* Identifier Length */
  246. buf[off+3] = id_len;
  247. /* Header size for Designation descriptor */
  248. len += (id_len + 4);
  249. off += (id_len + 4);
  250. if (1) {
  251. struct t10_alua_lu_gp *lu_gp;
  252. u32 padding, scsi_name_len, scsi_target_len;
  253. u16 lu_gp_id = 0;
  254. u16 tg_pt_gp_id = 0;
  255. u16 tpgt;
  256. tpg = lun->lun_tpg;
  257. /*
  258. * Relative target port identifer, see spc4r17
  259. * section 7.7.3.7
  260. *
  261. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  262. * section 7.5.1 Table 362
  263. */
  264. buf[off] = tpg->proto_id << 4;
  265. buf[off++] |= 0x1; /* CODE SET == Binary */
  266. buf[off] = 0x80; /* Set PIV=1 */
  267. /* Set ASSOCIATION == target port: 01b */
  268. buf[off] |= 0x10;
  269. /* DESIGNATOR TYPE == Relative target port identifer */
  270. buf[off++] |= 0x4;
  271. off++; /* Skip over Reserved */
  272. buf[off++] = 4; /* DESIGNATOR LENGTH */
  273. /* Skip over Obsolete field in RTPI payload
  274. * in Table 472 */
  275. off += 2;
  276. put_unaligned_be16(lun->lun_tpg->tpg_rtpi, &buf[off]);
  277. off += 2;
  278. len += 8; /* Header size + Designation descriptor */
  279. /*
  280. * Target port group identifier, see spc4r17
  281. * section 7.7.3.8
  282. *
  283. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  284. * section 7.5.1 Table 362
  285. */
  286. rcu_read_lock();
  287. tg_pt_gp = rcu_dereference(lun->lun_tg_pt_gp);
  288. if (!tg_pt_gp) {
  289. rcu_read_unlock();
  290. goto check_lu_gp;
  291. }
  292. tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
  293. rcu_read_unlock();
  294. buf[off] = tpg->proto_id << 4;
  295. buf[off++] |= 0x1; /* CODE SET == Binary */
  296. buf[off] = 0x80; /* Set PIV=1 */
  297. /* Set ASSOCIATION == target port: 01b */
  298. buf[off] |= 0x10;
  299. /* DESIGNATOR TYPE == Target port group identifier */
  300. buf[off++] |= 0x5;
  301. off++; /* Skip over Reserved */
  302. buf[off++] = 4; /* DESIGNATOR LENGTH */
  303. off += 2; /* Skip over Reserved Field */
  304. put_unaligned_be16(tg_pt_gp_id, &buf[off]);
  305. off += 2;
  306. len += 8; /* Header size + Designation descriptor */
  307. /*
  308. * Logical Unit Group identifier, see spc4r17
  309. * section 7.7.3.8
  310. */
  311. check_lu_gp:
  312. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  313. if (!lu_gp_mem)
  314. goto check_scsi_name;
  315. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  316. lu_gp = lu_gp_mem->lu_gp;
  317. if (!lu_gp) {
  318. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  319. goto check_scsi_name;
  320. }
  321. lu_gp_id = lu_gp->lu_gp_id;
  322. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  323. buf[off++] |= 0x1; /* CODE SET == Binary */
  324. /* DESIGNATOR TYPE == Logical Unit Group identifier */
  325. buf[off++] |= 0x6;
  326. off++; /* Skip over Reserved */
  327. buf[off++] = 4; /* DESIGNATOR LENGTH */
  328. off += 2; /* Skip over Reserved Field */
  329. put_unaligned_be16(lu_gp_id, &buf[off]);
  330. off += 2;
  331. len += 8; /* Header size + Designation descriptor */
  332. /*
  333. * SCSI name string designator, see spc4r17
  334. * section 7.7.3.11
  335. *
  336. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  337. * section 7.5.1 Table 362
  338. */
  339. check_scsi_name:
  340. buf[off] = tpg->proto_id << 4;
  341. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  342. buf[off] = 0x80; /* Set PIV=1 */
  343. /* Set ASSOCIATION == target port: 01b */
  344. buf[off] |= 0x10;
  345. /* DESIGNATOR TYPE == SCSI name string */
  346. buf[off++] |= 0x8;
  347. off += 2; /* Skip over Reserved and length */
  348. /*
  349. * SCSI name string identifer containing, $FABRIC_MOD
  350. * dependent information. For LIO-Target and iSCSI
  351. * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
  352. * UTF-8 encoding.
  353. */
  354. tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
  355. scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
  356. tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
  357. scsi_name_len += 1 /* Include NULL terminator */;
  358. /*
  359. * The null-terminated, null-padded (see 4.4.2) SCSI
  360. * NAME STRING field contains a UTF-8 format string.
  361. * The number of bytes in the SCSI NAME STRING field
  362. * (i.e., the value in the DESIGNATOR LENGTH field)
  363. * shall be no larger than 256 and shall be a multiple
  364. * of four.
  365. */
  366. padding = ((-scsi_name_len) & 3);
  367. if (padding)
  368. scsi_name_len += padding;
  369. if (scsi_name_len > 256)
  370. scsi_name_len = 256;
  371. buf[off-1] = scsi_name_len;
  372. off += scsi_name_len;
  373. /* Header size + Designation descriptor */
  374. len += (scsi_name_len + 4);
  375. /*
  376. * Target device designator
  377. */
  378. buf[off] = tpg->proto_id << 4;
  379. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  380. buf[off] = 0x80; /* Set PIV=1 */
  381. /* Set ASSOCIATION == target device: 10b */
  382. buf[off] |= 0x20;
  383. /* DESIGNATOR TYPE == SCSI name string */
  384. buf[off++] |= 0x8;
  385. off += 2; /* Skip over Reserved and length */
  386. /*
  387. * SCSI name string identifer containing, $FABRIC_MOD
  388. * dependent information. For LIO-Target and iSCSI
  389. * Target Port, this means "<iSCSI name>" in
  390. * UTF-8 encoding.
  391. */
  392. scsi_target_len = sprintf(&buf[off], "%s",
  393. tpg->se_tpg_tfo->tpg_get_wwn(tpg));
  394. scsi_target_len += 1 /* Include NULL terminator */;
  395. /*
  396. * The null-terminated, null-padded (see 4.4.2) SCSI
  397. * NAME STRING field contains a UTF-8 format string.
  398. * The number of bytes in the SCSI NAME STRING field
  399. * (i.e., the value in the DESIGNATOR LENGTH field)
  400. * shall be no larger than 256 and shall be a multiple
  401. * of four.
  402. */
  403. padding = ((-scsi_target_len) & 3);
  404. if (padding)
  405. scsi_target_len += padding;
  406. if (scsi_target_len > 256)
  407. scsi_target_len = 256;
  408. buf[off-1] = scsi_target_len;
  409. off += scsi_target_len;
  410. /* Header size + Designation descriptor */
  411. len += (scsi_target_len + 4);
  412. }
  413. put_unaligned_be16(len, &buf[2]); /* Page Length for VPD 0x83 */
  414. return 0;
  415. }
  416. EXPORT_SYMBOL(spc_emulate_evpd_83);
  417. /* Extended INQUIRY Data VPD Page */
  418. static sense_reason_t
  419. spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
  420. {
  421. struct se_device *dev = cmd->se_dev;
  422. struct se_session *sess = cmd->se_sess;
  423. buf[3] = 0x3c;
  424. /*
  425. * Set GRD_CHK + REF_CHK for TYPE1 protection, or GRD_CHK
  426. * only for TYPE3 protection.
  427. */
  428. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  429. if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT ||
  430. cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE1_PROT)
  431. buf[4] = 0x5;
  432. else if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE3_PROT ||
  433. cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE3_PROT)
  434. buf[4] = 0x4;
  435. }
  436. /* logical unit supports type 1 and type 3 protection */
  437. if ((dev->transport->get_device_type(dev) == TYPE_DISK) &&
  438. (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) &&
  439. (dev->dev_attrib.pi_prot_type || cmd->se_sess->sess_prot_type)) {
  440. buf[4] |= (0x3 << 3);
  441. }
  442. /* Set HEADSUP, ORDSUP, SIMPSUP */
  443. buf[5] = 0x07;
  444. /* If WriteCache emulation is enabled, set V_SUP */
  445. if (target_check_wce(dev))
  446. buf[6] = 0x01;
  447. /* If an LBA map is present set R_SUP */
  448. spin_lock(&cmd->se_dev->t10_alua.lba_map_lock);
  449. if (!list_empty(&dev->t10_alua.lba_map_list))
  450. buf[8] = 0x10;
  451. spin_unlock(&cmd->se_dev->t10_alua.lba_map_lock);
  452. return 0;
  453. }
  454. /* Block Limits VPD page */
  455. static sense_reason_t
  456. spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
  457. {
  458. struct se_device *dev = cmd->se_dev;
  459. u32 mtl = 0;
  460. int have_tp = 0, opt, min;
  461. u32 io_max_blocks;
  462. /*
  463. * Following spc3r22 section 6.5.3 Block Limits VPD page, when
  464. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  465. * different page length for Thin Provisioning.
  466. */
  467. if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
  468. have_tp = 1;
  469. buf[0] = dev->transport->get_device_type(dev);
  470. /* Set WSNZ to 1 */
  471. buf[4] = 0x01;
  472. /*
  473. * Set MAXIMUM COMPARE AND WRITE LENGTH
  474. */
  475. if (dev->dev_attrib.emulate_caw)
  476. buf[5] = 0x01;
  477. /*
  478. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  479. */
  480. if (dev->transport->get_io_min && (min = dev->transport->get_io_min(dev)))
  481. put_unaligned_be16(min / dev->dev_attrib.block_size, &buf[6]);
  482. else
  483. put_unaligned_be16(1, &buf[6]);
  484. /*
  485. * Set MAXIMUM TRANSFER LENGTH
  486. *
  487. * XXX: Currently assumes single PAGE_SIZE per scatterlist for fabrics
  488. * enforcing maximum HW scatter-gather-list entry limit
  489. */
  490. if (cmd->se_tfo->max_data_sg_nents) {
  491. mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE) /
  492. dev->dev_attrib.block_size;
  493. }
  494. io_max_blocks = mult_frac(dev->dev_attrib.hw_max_sectors,
  495. dev->dev_attrib.hw_block_size,
  496. dev->dev_attrib.block_size);
  497. put_unaligned_be32(min_not_zero(mtl, io_max_blocks), &buf[8]);
  498. /*
  499. * Set OPTIMAL TRANSFER LENGTH
  500. */
  501. if (dev->transport->get_io_opt && (opt = dev->transport->get_io_opt(dev)))
  502. put_unaligned_be32(opt / dev->dev_attrib.block_size, &buf[12]);
  503. else
  504. put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
  505. put_unaligned_be16(12, &buf[2]);
  506. if (!have_tp)
  507. goto try_atomic;
  508. /*
  509. * Set MAXIMUM UNMAP LBA COUNT
  510. */
  511. put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
  512. /*
  513. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  514. */
  515. put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
  516. &buf[24]);
  517. /*
  518. * Set OPTIMAL UNMAP GRANULARITY
  519. */
  520. put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
  521. /*
  522. * UNMAP GRANULARITY ALIGNMENT
  523. */
  524. put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
  525. &buf[32]);
  526. if (dev->dev_attrib.unmap_granularity_alignment != 0)
  527. buf[32] |= 0x80; /* Set the UGAVALID bit */
  528. /*
  529. * MAXIMUM WRITE SAME LENGTH
  530. */
  531. put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
  532. put_unaligned_be16(40, &buf[2]);
  533. try_atomic:
  534. /*
  535. * ATOMIC
  536. */
  537. if (!dev->dev_attrib.atomic_max_len)
  538. goto done;
  539. if (dev->dev_attrib.atomic_max_len < io_max_blocks)
  540. put_unaligned_be32(dev->dev_attrib.atomic_max_len, &buf[44]);
  541. else
  542. put_unaligned_be32(io_max_blocks, &buf[44]);
  543. put_unaligned_be32(dev->dev_attrib.atomic_alignment, &buf[48]);
  544. put_unaligned_be32(dev->dev_attrib.atomic_granularity, &buf[52]);
  545. put_unaligned_be32(dev->dev_attrib.atomic_max_with_boundary, &buf[56]);
  546. put_unaligned_be32(dev->dev_attrib.atomic_max_boundary, &buf[60]);
  547. put_unaligned_be16(60, &buf[2]);
  548. done:
  549. return 0;
  550. }
  551. /* Block Device Characteristics VPD page */
  552. static sense_reason_t
  553. spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  554. {
  555. struct se_device *dev = cmd->se_dev;
  556. buf[0] = dev->transport->get_device_type(dev);
  557. buf[3] = 0x3c;
  558. buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
  559. return 0;
  560. }
  561. /* Thin Provisioning VPD */
  562. static sense_reason_t
  563. spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  564. {
  565. struct se_device *dev = cmd->se_dev;
  566. /*
  567. * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
  568. *
  569. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  570. * zero, then the page length shall be set to 0004h. If the DP bit
  571. * is set to one, then the page length shall be set to the value
  572. * defined in table 162.
  573. */
  574. buf[0] = dev->transport->get_device_type(dev);
  575. /*
  576. * Set Hardcoded length mentioned above for DP=0
  577. */
  578. put_unaligned_be16(0x0004, &buf[2]);
  579. /*
  580. * The THRESHOLD EXPONENT field indicates the threshold set size in
  581. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  582. * 2(threshold exponent)).
  583. *
  584. * Note that this is currently set to 0x00 as mkp says it will be
  585. * changing again. We can enable this once it has settled in T10
  586. * and is actually used by Linux/SCSI ML code.
  587. */
  588. buf[4] = 0x00;
  589. /*
  590. * A TPU bit set to one indicates that the device server supports
  591. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  592. * that the device server does not support the UNMAP command.
  593. */
  594. if (dev->dev_attrib.emulate_tpu != 0)
  595. buf[5] = 0x80;
  596. /*
  597. * A TPWS bit set to one indicates that the device server supports
  598. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  599. * A TPWS bit set to zero indicates that the device server does not
  600. * support the use of the WRITE SAME (16) command to unmap LBAs.
  601. */
  602. if (dev->dev_attrib.emulate_tpws != 0)
  603. buf[5] |= 0x40 | 0x20;
  604. /*
  605. * The unmap_zeroes_data set means that the underlying device supports
  606. * REQ_OP_DISCARD and has the discard_zeroes_data bit set. This
  607. * satisfies the SBC requirements for LBPRZ, meaning that a subsequent
  608. * read will return zeroes after an UNMAP or WRITE SAME (16) to an LBA
  609. * See sbc4r36 6.6.4.
  610. */
  611. if (((dev->dev_attrib.emulate_tpu != 0) ||
  612. (dev->dev_attrib.emulate_tpws != 0)) &&
  613. (dev->dev_attrib.unmap_zeroes_data != 0))
  614. buf[5] |= 0x04;
  615. return 0;
  616. }
  617. /* Referrals VPD page */
  618. static sense_reason_t
  619. spc_emulate_evpd_b3(struct se_cmd *cmd, unsigned char *buf)
  620. {
  621. struct se_device *dev = cmd->se_dev;
  622. buf[0] = dev->transport->get_device_type(dev);
  623. buf[3] = 0x0c;
  624. put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[8]);
  625. put_unaligned_be32(dev->t10_alua.lba_map_segment_multiplier, &buf[12]);
  626. return 0;
  627. }
  628. static sense_reason_t
  629. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  630. static struct {
  631. uint8_t page;
  632. sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
  633. } evpd_handlers[] = {
  634. { .page = 0x00, .emulate = spc_emulate_evpd_00 },
  635. { .page = 0x80, .emulate = spc_emulate_evpd_80 },
  636. { .page = 0x83, .emulate = spc_emulate_evpd_83 },
  637. { .page = 0x86, .emulate = spc_emulate_evpd_86 },
  638. { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
  639. { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
  640. { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
  641. { .page = 0xb3, .emulate = spc_emulate_evpd_b3 },
  642. };
  643. /* supported vital product data pages */
  644. static sense_reason_t
  645. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  646. {
  647. int p;
  648. /*
  649. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  650. * Registered Extended LUN WWN has been set via ConfigFS
  651. * during device creation/restart.
  652. */
  653. if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  654. buf[3] = ARRAY_SIZE(evpd_handlers);
  655. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  656. buf[p + 4] = evpd_handlers[p].page;
  657. }
  658. return 0;
  659. }
  660. static sense_reason_t
  661. spc_emulate_inquiry(struct se_cmd *cmd)
  662. {
  663. struct se_device *dev = cmd->se_dev;
  664. unsigned char *rbuf;
  665. unsigned char *cdb = cmd->t_task_cdb;
  666. unsigned char *buf;
  667. sense_reason_t ret;
  668. int p;
  669. int len = 0;
  670. buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
  671. if (!buf) {
  672. pr_err("Unable to allocate response buffer for INQUIRY\n");
  673. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  674. }
  675. buf[0] = dev->transport->get_device_type(dev);
  676. if (!(cdb[1] & 0x1)) {
  677. if (cdb[2]) {
  678. pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
  679. cdb[2]);
  680. ret = TCM_INVALID_CDB_FIELD;
  681. goto out;
  682. }
  683. ret = spc_emulate_inquiry_std(cmd, buf);
  684. len = buf[4] + 5;
  685. goto out;
  686. }
  687. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
  688. if (cdb[2] == evpd_handlers[p].page) {
  689. buf[1] = cdb[2];
  690. ret = evpd_handlers[p].emulate(cmd, buf);
  691. len = get_unaligned_be16(&buf[2]) + 4;
  692. goto out;
  693. }
  694. }
  695. pr_debug("Unknown VPD Code: 0x%02x\n", cdb[2]);
  696. ret = TCM_INVALID_CDB_FIELD;
  697. out:
  698. rbuf = transport_kmap_data_sg(cmd);
  699. if (rbuf) {
  700. memcpy(rbuf, buf, min_t(u32, SE_INQUIRY_BUF, cmd->data_length));
  701. transport_kunmap_data_sg(cmd);
  702. }
  703. kfree(buf);
  704. if (!ret)
  705. target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, len);
  706. return ret;
  707. }
  708. static int spc_modesense_rwrecovery(struct se_cmd *cmd, u8 pc, u8 *p)
  709. {
  710. p[0] = 0x01;
  711. p[1] = 0x0a;
  712. /* No changeable values for now */
  713. if (pc == 1)
  714. goto out;
  715. out:
  716. return 12;
  717. }
  718. static int spc_modesense_control(struct se_cmd *cmd, u8 pc, u8 *p)
  719. {
  720. struct se_device *dev = cmd->se_dev;
  721. struct se_session *sess = cmd->se_sess;
  722. p[0] = 0x0a;
  723. p[1] = 0x0a;
  724. /* No changeable values for now */
  725. if (pc == 1)
  726. goto out;
  727. /* GLTSD: No implicit save of log parameters */
  728. p[2] = (1 << 1);
  729. if (target_sense_desc_format(dev))
  730. /* D_SENSE: Descriptor format sense data for 64bit sectors */
  731. p[2] |= (1 << 2);
  732. /*
  733. * From spc4r23, 7.4.7 Control mode page
  734. *
  735. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  736. * restrictions on the algorithm used for reordering commands
  737. * having the SIMPLE task attribute (see SAM-4).
  738. *
  739. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  740. * Code Description
  741. * 0h Restricted reordering
  742. * 1h Unrestricted reordering allowed
  743. * 2h to 7h Reserved
  744. * 8h to Fh Vendor specific
  745. *
  746. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  747. * the device server shall order the processing sequence of commands
  748. * having the SIMPLE task attribute such that data integrity is maintained
  749. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  750. * requests is halted at any time, the final value of all data observable
  751. * on the medium shall be the same as if all the commands had been processed
  752. * with the ORDERED task attribute).
  753. *
  754. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  755. * device server may reorder the processing sequence of commands having the
  756. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  757. * command sequence order shall be explicitly handled by the application client
  758. * through the selection of appropriate ommands and task attributes.
  759. */
  760. p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  761. /*
  762. * From spc4r17, section 7.4.6 Control mode Page
  763. *
  764. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  765. *
  766. * 00b: The logical unit shall clear any unit attention condition
  767. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  768. * status and shall not establish a unit attention condition when a com-
  769. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  770. * status.
  771. *
  772. * 10b: The logical unit shall not clear any unit attention condition
  773. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  774. * status and shall not establish a unit attention condition when
  775. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  776. * CONFLICT status.
  777. *
  778. * 11b a The logical unit shall not clear any unit attention condition
  779. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  780. * status and shall establish a unit attention condition for the
  781. * initiator port associated with the I_T nexus on which the BUSY,
  782. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  783. * Depending on the status, the additional sense code shall be set to
  784. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  785. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  786. * command, a unit attention condition shall be established only once
  787. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  788. * to the number of commands completed with one of those status codes.
  789. */
  790. switch (dev->dev_attrib.emulate_ua_intlck_ctrl) {
  791. case TARGET_UA_INTLCK_CTRL_ESTABLISH_UA:
  792. p[4] = 0x30;
  793. break;
  794. case TARGET_UA_INTLCK_CTRL_NO_CLEAR:
  795. p[4] = 0x20;
  796. break;
  797. default: /* TARGET_UA_INTLCK_CTRL_CLEAR */
  798. p[4] = 0x00;
  799. break;
  800. }
  801. /*
  802. * From spc4r17, section 7.4.6 Control mode Page
  803. *
  804. * Task Aborted Status (TAS) bit set to zero.
  805. *
  806. * A task aborted status (TAS) bit set to zero specifies that aborted
  807. * tasks shall be terminated by the device server without any response
  808. * to the application client. A TAS bit set to one specifies that tasks
  809. * aborted by the actions of an I_T nexus other than the I_T nexus on
  810. * which the command was received shall be completed with TASK ABORTED
  811. * status (see SAM-4).
  812. */
  813. p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
  814. /*
  815. * From spc4r30, section 7.5.7 Control mode page
  816. *
  817. * Application Tag Owner (ATO) bit set to one.
  818. *
  819. * If the ATO bit is set to one the device server shall not modify the
  820. * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection
  821. * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE
  822. * TAG field.
  823. */
  824. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  825. if (dev->dev_attrib.pi_prot_type || sess->sess_prot_type)
  826. p[5] |= 0x80;
  827. }
  828. p[8] = 0xff;
  829. p[9] = 0xff;
  830. p[11] = 30;
  831. out:
  832. return 12;
  833. }
  834. static int spc_modesense_caching(struct se_cmd *cmd, u8 pc, u8 *p)
  835. {
  836. struct se_device *dev = cmd->se_dev;
  837. p[0] = 0x08;
  838. p[1] = 0x12;
  839. /* No changeable values for now */
  840. if (pc == 1)
  841. goto out;
  842. if (target_check_wce(dev))
  843. p[2] = 0x04; /* Write Cache Enable */
  844. p[12] = 0x20; /* Disabled Read Ahead */
  845. out:
  846. return 20;
  847. }
  848. static int spc_modesense_informational_exceptions(struct se_cmd *cmd, u8 pc, unsigned char *p)
  849. {
  850. p[0] = 0x1c;
  851. p[1] = 0x0a;
  852. /* No changeable values for now */
  853. if (pc == 1)
  854. goto out;
  855. out:
  856. return 12;
  857. }
  858. static struct {
  859. uint8_t page;
  860. uint8_t subpage;
  861. int (*emulate)(struct se_cmd *, u8, unsigned char *);
  862. } modesense_handlers[] = {
  863. { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
  864. { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
  865. { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
  866. { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
  867. };
  868. static void spc_modesense_write_protect(unsigned char *buf, int type)
  869. {
  870. /*
  871. * I believe that the WP bit (bit 7) in the mode header is the same for
  872. * all device types..
  873. */
  874. switch (type) {
  875. case TYPE_DISK:
  876. case TYPE_TAPE:
  877. default:
  878. buf[0] |= 0x80; /* WP bit */
  879. break;
  880. }
  881. }
  882. static void spc_modesense_dpofua(unsigned char *buf, int type)
  883. {
  884. switch (type) {
  885. case TYPE_DISK:
  886. buf[0] |= 0x10; /* DPOFUA bit */
  887. break;
  888. default:
  889. break;
  890. }
  891. }
  892. static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  893. {
  894. *buf++ = 8;
  895. put_unaligned_be32(min(blocks, 0xffffffffull), buf);
  896. buf += 4;
  897. put_unaligned_be32(block_size, buf);
  898. return 9;
  899. }
  900. static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  901. {
  902. if (blocks <= 0xffffffff)
  903. return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
  904. *buf++ = 1; /* LONGLBA */
  905. buf += 2;
  906. *buf++ = 16;
  907. put_unaligned_be64(blocks, buf);
  908. buf += 12;
  909. put_unaligned_be32(block_size, buf);
  910. return 17;
  911. }
  912. static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
  913. {
  914. struct se_device *dev = cmd->se_dev;
  915. char *cdb = cmd->t_task_cdb;
  916. unsigned char buf[SE_MODE_PAGE_BUF], *rbuf;
  917. int type = dev->transport->get_device_type(dev);
  918. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  919. bool dbd = !!(cdb[1] & 0x08);
  920. bool llba = ten ? !!(cdb[1] & 0x10) : false;
  921. u8 pc = cdb[2] >> 6;
  922. u8 page = cdb[2] & 0x3f;
  923. u8 subpage = cdb[3];
  924. int length = 0;
  925. int ret;
  926. int i;
  927. memset(buf, 0, SE_MODE_PAGE_BUF);
  928. /*
  929. * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
  930. * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
  931. */
  932. length = ten ? 3 : 2;
  933. /* DEVICE-SPECIFIC PARAMETER */
  934. if (cmd->se_lun->lun_access_ro || target_lun_is_rdonly(cmd))
  935. spc_modesense_write_protect(&buf[length], type);
  936. /*
  937. * SBC only allows us to enable FUA and DPO together. Fortunately
  938. * DPO is explicitly specified as a hint, so a noop is a perfectly
  939. * valid implementation.
  940. */
  941. if (target_check_fua(dev))
  942. spc_modesense_dpofua(&buf[length], type);
  943. ++length;
  944. /* BLOCK DESCRIPTOR */
  945. /*
  946. * For now we only include a block descriptor for disk (SBC)
  947. * devices; other command sets use a slightly different format.
  948. */
  949. if (!dbd && type == TYPE_DISK) {
  950. u64 blocks = dev->transport->get_blocks(dev);
  951. u32 block_size = dev->dev_attrib.block_size;
  952. if (ten) {
  953. if (llba) {
  954. length += spc_modesense_long_blockdesc(&buf[length],
  955. blocks, block_size);
  956. } else {
  957. length += 3;
  958. length += spc_modesense_blockdesc(&buf[length],
  959. blocks, block_size);
  960. }
  961. } else {
  962. length += spc_modesense_blockdesc(&buf[length], blocks,
  963. block_size);
  964. }
  965. } else {
  966. if (ten)
  967. length += 4;
  968. else
  969. length += 1;
  970. }
  971. if (page == 0x3f) {
  972. if (subpage != 0x00 && subpage != 0xff) {
  973. pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
  974. return TCM_INVALID_CDB_FIELD;
  975. }
  976. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
  977. /*
  978. * Tricky way to say all subpage 00h for
  979. * subpage==0, all subpages for subpage==0xff
  980. * (and we just checked above that those are
  981. * the only two possibilities).
  982. */
  983. if ((modesense_handlers[i].subpage & ~subpage) == 0) {
  984. ret = modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  985. if (!ten && length + ret >= 255)
  986. break;
  987. length += ret;
  988. }
  989. }
  990. goto set_length;
  991. }
  992. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  993. if (modesense_handlers[i].page == page &&
  994. modesense_handlers[i].subpage == subpage) {
  995. length += modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  996. goto set_length;
  997. }
  998. /*
  999. * We don't intend to implement:
  1000. * - obsolete page 03h "format parameters" (checked by Solaris)
  1001. */
  1002. if (page != 0x03)
  1003. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  1004. page, subpage);
  1005. return TCM_UNKNOWN_MODE_PAGE;
  1006. set_length:
  1007. if (ten)
  1008. put_unaligned_be16(length - 2, buf);
  1009. else
  1010. buf[0] = length - 1;
  1011. rbuf = transport_kmap_data_sg(cmd);
  1012. if (rbuf) {
  1013. memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length));
  1014. transport_kunmap_data_sg(cmd);
  1015. }
  1016. target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, length);
  1017. return 0;
  1018. }
  1019. static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
  1020. {
  1021. char *cdb = cmd->t_task_cdb;
  1022. bool ten = cdb[0] == MODE_SELECT_10;
  1023. int off = ten ? 8 : 4;
  1024. bool pf = !!(cdb[1] & 0x10);
  1025. u8 page, subpage;
  1026. unsigned char *buf;
  1027. unsigned char tbuf[SE_MODE_PAGE_BUF];
  1028. int length;
  1029. sense_reason_t ret = 0;
  1030. int i;
  1031. if (!cmd->data_length) {
  1032. target_complete_cmd(cmd, SAM_STAT_GOOD);
  1033. return 0;
  1034. }
  1035. if (cmd->data_length < off + 2)
  1036. return TCM_PARAMETER_LIST_LENGTH_ERROR;
  1037. buf = transport_kmap_data_sg(cmd);
  1038. if (!buf)
  1039. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1040. if (!pf) {
  1041. ret = TCM_INVALID_CDB_FIELD;
  1042. goto out;
  1043. }
  1044. page = buf[off] & 0x3f;
  1045. subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
  1046. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  1047. if (modesense_handlers[i].page == page &&
  1048. modesense_handlers[i].subpage == subpage) {
  1049. memset(tbuf, 0, SE_MODE_PAGE_BUF);
  1050. length = modesense_handlers[i].emulate(cmd, 0, tbuf);
  1051. goto check_contents;
  1052. }
  1053. ret = TCM_UNKNOWN_MODE_PAGE;
  1054. goto out;
  1055. check_contents:
  1056. if (cmd->data_length < off + length) {
  1057. ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
  1058. goto out;
  1059. }
  1060. if (memcmp(buf + off, tbuf, length))
  1061. ret = TCM_INVALID_PARAMETER_LIST;
  1062. out:
  1063. transport_kunmap_data_sg(cmd);
  1064. if (!ret)
  1065. target_complete_cmd(cmd, SAM_STAT_GOOD);
  1066. return ret;
  1067. }
  1068. static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
  1069. {
  1070. unsigned char *cdb = cmd->t_task_cdb;
  1071. unsigned char *rbuf;
  1072. u8 ua_asc = 0, ua_ascq = 0;
  1073. unsigned char buf[SE_SENSE_BUF];
  1074. bool desc_format = target_sense_desc_format(cmd->se_dev);
  1075. memset(buf, 0, SE_SENSE_BUF);
  1076. if (cdb[1] & 0x01) {
  1077. pr_err("REQUEST_SENSE description emulation not"
  1078. " supported\n");
  1079. return TCM_INVALID_CDB_FIELD;
  1080. }
  1081. rbuf = transport_kmap_data_sg(cmd);
  1082. if (!rbuf)
  1083. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1084. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq))
  1085. scsi_build_sense_buffer(desc_format, buf, UNIT_ATTENTION,
  1086. ua_asc, ua_ascq);
  1087. else
  1088. scsi_build_sense_buffer(desc_format, buf, NO_SENSE, 0x0, 0x0);
  1089. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  1090. transport_kunmap_data_sg(cmd);
  1091. target_complete_cmd(cmd, SAM_STAT_GOOD);
  1092. return 0;
  1093. }
  1094. sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
  1095. {
  1096. struct se_dev_entry *deve;
  1097. struct se_session *sess = cmd->se_sess;
  1098. struct se_node_acl *nacl;
  1099. struct scsi_lun slun;
  1100. unsigned char *buf;
  1101. u32 lun_count = 0, offset = 8;
  1102. __be32 len;
  1103. buf = transport_kmap_data_sg(cmd);
  1104. if (cmd->data_length && !buf)
  1105. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1106. /*
  1107. * If no struct se_session pointer is present, this struct se_cmd is
  1108. * coming via a target_core_mod PASSTHROUGH op, and not through
  1109. * a $FABRIC_MOD. In that case, report LUN=0 only.
  1110. */
  1111. if (!sess)
  1112. goto done;
  1113. nacl = sess->se_node_acl;
  1114. rcu_read_lock();
  1115. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  1116. /*
  1117. * We determine the correct LUN LIST LENGTH even once we
  1118. * have reached the initial allocation length.
  1119. * See SPC2-R20 7.19.
  1120. */
  1121. lun_count++;
  1122. if (offset >= cmd->data_length)
  1123. continue;
  1124. int_to_scsilun(deve->mapped_lun, &slun);
  1125. memcpy(buf + offset, &slun,
  1126. min(8u, cmd->data_length - offset));
  1127. offset += 8;
  1128. }
  1129. rcu_read_unlock();
  1130. /*
  1131. * See SPC3 r07, page 159.
  1132. */
  1133. done:
  1134. /*
  1135. * If no LUNs are accessible, report virtual LUN 0.
  1136. */
  1137. if (lun_count == 0) {
  1138. int_to_scsilun(0, &slun);
  1139. if (cmd->data_length > 8)
  1140. memcpy(buf + offset, &slun,
  1141. min(8u, cmd->data_length - offset));
  1142. lun_count = 1;
  1143. }
  1144. if (buf) {
  1145. len = cpu_to_be32(lun_count * 8);
  1146. memcpy(buf, &len, min_t(int, sizeof len, cmd->data_length));
  1147. transport_kunmap_data_sg(cmd);
  1148. }
  1149. target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 8 + lun_count * 8);
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL(spc_emulate_report_luns);
  1153. static sense_reason_t
  1154. spc_emulate_testunitready(struct se_cmd *cmd)
  1155. {
  1156. target_complete_cmd(cmd, SAM_STAT_GOOD);
  1157. return 0;
  1158. }
  1159. static void set_dpofua_usage_bits(u8 *usage_bits, struct se_device *dev)
  1160. {
  1161. if (!target_check_fua(dev))
  1162. usage_bits[1] &= ~0x18;
  1163. else
  1164. usage_bits[1] |= 0x18;
  1165. }
  1166. static void set_dpofua_usage_bits32(u8 *usage_bits, struct se_device *dev)
  1167. {
  1168. if (!target_check_fua(dev))
  1169. usage_bits[10] &= ~0x18;
  1170. else
  1171. usage_bits[10] |= 0x18;
  1172. }
  1173. static const struct target_opcode_descriptor tcm_opcode_read6 = {
  1174. .support = SCSI_SUPPORT_FULL,
  1175. .opcode = READ_6,
  1176. .cdb_size = 6,
  1177. .usage_bits = {READ_6, 0x1f, 0xff, 0xff,
  1178. 0xff, SCSI_CONTROL_MASK},
  1179. };
  1180. static const struct target_opcode_descriptor tcm_opcode_read10 = {
  1181. .support = SCSI_SUPPORT_FULL,
  1182. .opcode = READ_10,
  1183. .cdb_size = 10,
  1184. .usage_bits = {READ_10, 0xf8, 0xff, 0xff,
  1185. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff,
  1186. 0xff, SCSI_CONTROL_MASK},
  1187. .update_usage_bits = set_dpofua_usage_bits,
  1188. };
  1189. static const struct target_opcode_descriptor tcm_opcode_read12 = {
  1190. .support = SCSI_SUPPORT_FULL,
  1191. .opcode = READ_12,
  1192. .cdb_size = 12,
  1193. .usage_bits = {READ_12, 0xf8, 0xff, 0xff,
  1194. 0xff, 0xff, 0xff, 0xff,
  1195. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1196. .update_usage_bits = set_dpofua_usage_bits,
  1197. };
  1198. static const struct target_opcode_descriptor tcm_opcode_read16 = {
  1199. .support = SCSI_SUPPORT_FULL,
  1200. .opcode = READ_16,
  1201. .cdb_size = 16,
  1202. .usage_bits = {READ_16, 0xf8, 0xff, 0xff,
  1203. 0xff, 0xff, 0xff, 0xff,
  1204. 0xff, 0xff, 0xff, 0xff,
  1205. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1206. .update_usage_bits = set_dpofua_usage_bits,
  1207. };
  1208. static const struct target_opcode_descriptor tcm_opcode_write6 = {
  1209. .support = SCSI_SUPPORT_FULL,
  1210. .opcode = WRITE_6,
  1211. .cdb_size = 6,
  1212. .usage_bits = {WRITE_6, 0x1f, 0xff, 0xff,
  1213. 0xff, SCSI_CONTROL_MASK},
  1214. };
  1215. static const struct target_opcode_descriptor tcm_opcode_write10 = {
  1216. .support = SCSI_SUPPORT_FULL,
  1217. .opcode = WRITE_10,
  1218. .cdb_size = 10,
  1219. .usage_bits = {WRITE_10, 0xf8, 0xff, 0xff,
  1220. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff,
  1221. 0xff, SCSI_CONTROL_MASK},
  1222. .update_usage_bits = set_dpofua_usage_bits,
  1223. };
  1224. static const struct target_opcode_descriptor tcm_opcode_write_verify10 = {
  1225. .support = SCSI_SUPPORT_FULL,
  1226. .opcode = WRITE_VERIFY,
  1227. .cdb_size = 10,
  1228. .usage_bits = {WRITE_VERIFY, 0xf0, 0xff, 0xff,
  1229. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff,
  1230. 0xff, SCSI_CONTROL_MASK},
  1231. .update_usage_bits = set_dpofua_usage_bits,
  1232. };
  1233. static const struct target_opcode_descriptor tcm_opcode_write12 = {
  1234. .support = SCSI_SUPPORT_FULL,
  1235. .opcode = WRITE_12,
  1236. .cdb_size = 12,
  1237. .usage_bits = {WRITE_12, 0xf8, 0xff, 0xff,
  1238. 0xff, 0xff, 0xff, 0xff,
  1239. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1240. .update_usage_bits = set_dpofua_usage_bits,
  1241. };
  1242. static const struct target_opcode_descriptor tcm_opcode_write16 = {
  1243. .support = SCSI_SUPPORT_FULL,
  1244. .opcode = WRITE_16,
  1245. .cdb_size = 16,
  1246. .usage_bits = {WRITE_16, 0xf8, 0xff, 0xff,
  1247. 0xff, 0xff, 0xff, 0xff,
  1248. 0xff, 0xff, 0xff, 0xff,
  1249. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1250. .update_usage_bits = set_dpofua_usage_bits,
  1251. };
  1252. static const struct target_opcode_descriptor tcm_opcode_write_verify16 = {
  1253. .support = SCSI_SUPPORT_FULL,
  1254. .opcode = WRITE_VERIFY_16,
  1255. .cdb_size = 16,
  1256. .usage_bits = {WRITE_VERIFY_16, 0xf0, 0xff, 0xff,
  1257. 0xff, 0xff, 0xff, 0xff,
  1258. 0xff, 0xff, 0xff, 0xff,
  1259. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1260. .update_usage_bits = set_dpofua_usage_bits,
  1261. };
  1262. static bool tcm_is_ws_enabled(const struct target_opcode_descriptor *descr,
  1263. struct se_cmd *cmd)
  1264. {
  1265. struct exec_cmd_ops *ops = cmd->protocol_data;
  1266. struct se_device *dev = cmd->se_dev;
  1267. return (dev->dev_attrib.emulate_tpws && !!ops->execute_unmap) ||
  1268. !!ops->execute_write_same;
  1269. }
  1270. static const struct target_opcode_descriptor tcm_opcode_write_same32 = {
  1271. .support = SCSI_SUPPORT_FULL,
  1272. .serv_action_valid = 1,
  1273. .opcode = VARIABLE_LENGTH_CMD,
  1274. .service_action = WRITE_SAME_32,
  1275. .cdb_size = 32,
  1276. .usage_bits = {VARIABLE_LENGTH_CMD, SCSI_CONTROL_MASK, 0x00, 0x00,
  1277. 0x00, 0x00, SCSI_GROUP_NUMBER_MASK, 0x18,
  1278. 0x00, WRITE_SAME_32, 0xe8, 0x00,
  1279. 0xff, 0xff, 0xff, 0xff,
  1280. 0xff, 0xff, 0xff, 0xff,
  1281. 0x00, 0x00, 0x00, 0x00,
  1282. 0x00, 0x00, 0x00, 0x00,
  1283. 0xff, 0xff, 0xff, 0xff},
  1284. .enabled = tcm_is_ws_enabled,
  1285. .update_usage_bits = set_dpofua_usage_bits32,
  1286. };
  1287. static bool tcm_is_atomic_enabled(const struct target_opcode_descriptor *descr,
  1288. struct se_cmd *cmd)
  1289. {
  1290. return cmd->se_dev->dev_attrib.atomic_max_len;
  1291. }
  1292. static struct target_opcode_descriptor tcm_opcode_write_atomic16 = {
  1293. .support = SCSI_SUPPORT_FULL,
  1294. .opcode = WRITE_ATOMIC_16,
  1295. .cdb_size = 16,
  1296. .usage_bits = {WRITE_ATOMIC_16, 0xf8, 0xff, 0xff,
  1297. 0xff, 0xff, 0xff, 0xff,
  1298. 0xff, 0xff, 0xff, 0xff,
  1299. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1300. .enabled = tcm_is_atomic_enabled,
  1301. .update_usage_bits = set_dpofua_usage_bits,
  1302. };
  1303. static bool tcm_is_caw_enabled(const struct target_opcode_descriptor *descr,
  1304. struct se_cmd *cmd)
  1305. {
  1306. struct se_device *dev = cmd->se_dev;
  1307. return dev->dev_attrib.emulate_caw;
  1308. }
  1309. static const struct target_opcode_descriptor tcm_opcode_compare_write = {
  1310. .support = SCSI_SUPPORT_FULL,
  1311. .opcode = COMPARE_AND_WRITE,
  1312. .cdb_size = 16,
  1313. .usage_bits = {COMPARE_AND_WRITE, 0x18, 0xff, 0xff,
  1314. 0xff, 0xff, 0xff, 0xff,
  1315. 0xff, 0xff, 0x00, 0x00,
  1316. 0x00, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1317. .enabled = tcm_is_caw_enabled,
  1318. .update_usage_bits = set_dpofua_usage_bits,
  1319. };
  1320. static const struct target_opcode_descriptor tcm_opcode_read_capacity = {
  1321. .support = SCSI_SUPPORT_FULL,
  1322. .opcode = READ_CAPACITY,
  1323. .cdb_size = 10,
  1324. .usage_bits = {READ_CAPACITY, 0x00, 0xff, 0xff,
  1325. 0xff, 0xff, 0x00, 0x00,
  1326. 0x01, SCSI_CONTROL_MASK},
  1327. };
  1328. static const struct target_opcode_descriptor tcm_opcode_read_capacity16 = {
  1329. .support = SCSI_SUPPORT_FULL,
  1330. .serv_action_valid = 1,
  1331. .opcode = SERVICE_ACTION_IN_16,
  1332. .service_action = SAI_READ_CAPACITY_16,
  1333. .cdb_size = 16,
  1334. .usage_bits = {SERVICE_ACTION_IN_16, SAI_READ_CAPACITY_16, 0x00, 0x00,
  1335. 0x00, 0x00, 0x00, 0x00,
  1336. 0x00, 0x00, 0xff, 0xff,
  1337. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1338. };
  1339. static bool tcm_is_rep_ref_enabled(const struct target_opcode_descriptor *descr,
  1340. struct se_cmd *cmd)
  1341. {
  1342. struct se_device *dev = cmd->se_dev;
  1343. spin_lock(&dev->t10_alua.lba_map_lock);
  1344. if (list_empty(&dev->t10_alua.lba_map_list)) {
  1345. spin_unlock(&dev->t10_alua.lba_map_lock);
  1346. return false;
  1347. }
  1348. spin_unlock(&dev->t10_alua.lba_map_lock);
  1349. return true;
  1350. }
  1351. static const struct target_opcode_descriptor tcm_opcode_read_report_refferals = {
  1352. .support = SCSI_SUPPORT_FULL,
  1353. .serv_action_valid = 1,
  1354. .opcode = SERVICE_ACTION_IN_16,
  1355. .service_action = SAI_REPORT_REFERRALS,
  1356. .cdb_size = 16,
  1357. .usage_bits = {SERVICE_ACTION_IN_16, SAI_REPORT_REFERRALS, 0x00, 0x00,
  1358. 0x00, 0x00, 0x00, 0x00,
  1359. 0x00, 0x00, 0xff, 0xff,
  1360. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1361. .enabled = tcm_is_rep_ref_enabled,
  1362. };
  1363. static const struct target_opcode_descriptor tcm_opcode_sync_cache = {
  1364. .support = SCSI_SUPPORT_FULL,
  1365. .opcode = SYNCHRONIZE_CACHE,
  1366. .cdb_size = 10,
  1367. .usage_bits = {SYNCHRONIZE_CACHE, 0x02, 0xff, 0xff,
  1368. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff,
  1369. 0xff, SCSI_CONTROL_MASK},
  1370. };
  1371. static const struct target_opcode_descriptor tcm_opcode_sync_cache16 = {
  1372. .support = SCSI_SUPPORT_FULL,
  1373. .opcode = SYNCHRONIZE_CACHE_16,
  1374. .cdb_size = 16,
  1375. .usage_bits = {SYNCHRONIZE_CACHE_16, 0x02, 0xff, 0xff,
  1376. 0xff, 0xff, 0xff, 0xff,
  1377. 0xff, 0xff, 0xff, 0xff,
  1378. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1379. };
  1380. static bool tcm_is_unmap_enabled(const struct target_opcode_descriptor *descr,
  1381. struct se_cmd *cmd)
  1382. {
  1383. struct exec_cmd_ops *ops = cmd->protocol_data;
  1384. struct se_device *dev = cmd->se_dev;
  1385. return ops->execute_unmap && dev->dev_attrib.emulate_tpu;
  1386. }
  1387. static const struct target_opcode_descriptor tcm_opcode_unmap = {
  1388. .support = SCSI_SUPPORT_FULL,
  1389. .opcode = UNMAP,
  1390. .cdb_size = 10,
  1391. .usage_bits = {UNMAP, 0x00, 0x00, 0x00,
  1392. 0x00, 0x00, SCSI_GROUP_NUMBER_MASK, 0xff,
  1393. 0xff, SCSI_CONTROL_MASK},
  1394. .enabled = tcm_is_unmap_enabled,
  1395. };
  1396. static const struct target_opcode_descriptor tcm_opcode_write_same = {
  1397. .support = SCSI_SUPPORT_FULL,
  1398. .opcode = WRITE_SAME,
  1399. .cdb_size = 10,
  1400. .usage_bits = {WRITE_SAME, 0xe8, 0xff, 0xff,
  1401. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff,
  1402. 0xff, SCSI_CONTROL_MASK},
  1403. .enabled = tcm_is_ws_enabled,
  1404. };
  1405. static const struct target_opcode_descriptor tcm_opcode_write_same16 = {
  1406. .support = SCSI_SUPPORT_FULL,
  1407. .opcode = WRITE_SAME_16,
  1408. .cdb_size = 16,
  1409. .usage_bits = {WRITE_SAME_16, 0xe8, 0xff, 0xff,
  1410. 0xff, 0xff, 0xff, 0xff,
  1411. 0xff, 0xff, 0xff, 0xff,
  1412. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1413. .enabled = tcm_is_ws_enabled,
  1414. };
  1415. static const struct target_opcode_descriptor tcm_opcode_verify = {
  1416. .support = SCSI_SUPPORT_FULL,
  1417. .opcode = VERIFY,
  1418. .cdb_size = 10,
  1419. .usage_bits = {VERIFY, 0x00, 0xff, 0xff,
  1420. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff,
  1421. 0xff, SCSI_CONTROL_MASK},
  1422. };
  1423. static const struct target_opcode_descriptor tcm_opcode_verify16 = {
  1424. .support = SCSI_SUPPORT_FULL,
  1425. .opcode = VERIFY_16,
  1426. .cdb_size = 16,
  1427. .usage_bits = {VERIFY_16, 0x00, 0xff, 0xff,
  1428. 0xff, 0xff, 0xff, 0xff,
  1429. 0xff, 0xff, 0xff, 0xff,
  1430. 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK},
  1431. };
  1432. static const struct target_opcode_descriptor tcm_opcode_start_stop = {
  1433. .support = SCSI_SUPPORT_FULL,
  1434. .opcode = START_STOP,
  1435. .cdb_size = 6,
  1436. .usage_bits = {START_STOP, 0x01, 0x00, 0x00,
  1437. 0x01, SCSI_CONTROL_MASK},
  1438. };
  1439. static const struct target_opcode_descriptor tcm_opcode_mode_select = {
  1440. .support = SCSI_SUPPORT_FULL,
  1441. .opcode = MODE_SELECT,
  1442. .cdb_size = 6,
  1443. .usage_bits = {MODE_SELECT, 0x10, 0x00, 0x00,
  1444. 0xff, SCSI_CONTROL_MASK},
  1445. };
  1446. static const struct target_opcode_descriptor tcm_opcode_mode_select10 = {
  1447. .support = SCSI_SUPPORT_FULL,
  1448. .opcode = MODE_SELECT_10,
  1449. .cdb_size = 10,
  1450. .usage_bits = {MODE_SELECT_10, 0x10, 0x00, 0x00,
  1451. 0x00, 0x00, 0x00, 0xff,
  1452. 0xff, SCSI_CONTROL_MASK},
  1453. };
  1454. static const struct target_opcode_descriptor tcm_opcode_mode_sense = {
  1455. .support = SCSI_SUPPORT_FULL,
  1456. .opcode = MODE_SENSE,
  1457. .cdb_size = 6,
  1458. .usage_bits = {MODE_SENSE, 0x08, 0xff, 0xff,
  1459. 0xff, SCSI_CONTROL_MASK},
  1460. };
  1461. static const struct target_opcode_descriptor tcm_opcode_mode_sense10 = {
  1462. .support = SCSI_SUPPORT_FULL,
  1463. .opcode = MODE_SENSE_10,
  1464. .cdb_size = 10,
  1465. .usage_bits = {MODE_SENSE_10, 0x18, 0xff, 0xff,
  1466. 0x00, 0x00, 0x00, 0xff,
  1467. 0xff, SCSI_CONTROL_MASK},
  1468. };
  1469. static const struct target_opcode_descriptor tcm_opcode_pri_read_keys = {
  1470. .support = SCSI_SUPPORT_FULL,
  1471. .serv_action_valid = 1,
  1472. .opcode = PERSISTENT_RESERVE_IN,
  1473. .service_action = PRI_READ_KEYS,
  1474. .cdb_size = 10,
  1475. .usage_bits = {PERSISTENT_RESERVE_IN, PRI_READ_KEYS, 0x00, 0x00,
  1476. 0x00, 0x00, 0x00, 0xff,
  1477. 0xff, SCSI_CONTROL_MASK},
  1478. };
  1479. static const struct target_opcode_descriptor tcm_opcode_pri_read_resrv = {
  1480. .support = SCSI_SUPPORT_FULL,
  1481. .serv_action_valid = 1,
  1482. .opcode = PERSISTENT_RESERVE_IN,
  1483. .service_action = PRI_READ_RESERVATION,
  1484. .cdb_size = 10,
  1485. .usage_bits = {PERSISTENT_RESERVE_IN, PRI_READ_RESERVATION, 0x00, 0x00,
  1486. 0x00, 0x00, 0x00, 0xff,
  1487. 0xff, SCSI_CONTROL_MASK},
  1488. };
  1489. static bool tcm_is_pr_enabled(const struct target_opcode_descriptor *descr,
  1490. struct se_cmd *cmd)
  1491. {
  1492. struct se_device *dev = cmd->se_dev;
  1493. if (!dev->dev_attrib.emulate_pr)
  1494. return false;
  1495. if (!(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
  1496. return true;
  1497. switch (descr->opcode) {
  1498. case RESERVE_6:
  1499. case RESERVE_10:
  1500. case RELEASE_6:
  1501. case RELEASE_10:
  1502. /*
  1503. * The pr_ops which are used by the backend modules don't
  1504. * support these commands.
  1505. */
  1506. return false;
  1507. case PERSISTENT_RESERVE_OUT:
  1508. switch (descr->service_action) {
  1509. case PRO_REGISTER_AND_MOVE:
  1510. case PRO_REPLACE_LOST_RESERVATION:
  1511. /*
  1512. * The backend modules don't have access to ports and
  1513. * I_T nexuses so they can't handle these type of
  1514. * requests.
  1515. */
  1516. return false;
  1517. }
  1518. break;
  1519. case PERSISTENT_RESERVE_IN:
  1520. if (descr->service_action == PRI_READ_FULL_STATUS)
  1521. return false;
  1522. break;
  1523. }
  1524. return true;
  1525. }
  1526. static const struct target_opcode_descriptor tcm_opcode_pri_read_caps = {
  1527. .support = SCSI_SUPPORT_FULL,
  1528. .serv_action_valid = 1,
  1529. .opcode = PERSISTENT_RESERVE_IN,
  1530. .service_action = PRI_REPORT_CAPABILITIES,
  1531. .cdb_size = 10,
  1532. .usage_bits = {PERSISTENT_RESERVE_IN, PRI_REPORT_CAPABILITIES, 0x00, 0x00,
  1533. 0x00, 0x00, 0x00, 0xff,
  1534. 0xff, SCSI_CONTROL_MASK},
  1535. .enabled = tcm_is_pr_enabled,
  1536. };
  1537. static const struct target_opcode_descriptor tcm_opcode_pri_read_full_status = {
  1538. .support = SCSI_SUPPORT_FULL,
  1539. .serv_action_valid = 1,
  1540. .opcode = PERSISTENT_RESERVE_IN,
  1541. .service_action = PRI_READ_FULL_STATUS,
  1542. .cdb_size = 10,
  1543. .usage_bits = {PERSISTENT_RESERVE_IN, PRI_READ_FULL_STATUS, 0x00, 0x00,
  1544. 0x00, 0x00, 0x00, 0xff,
  1545. 0xff, SCSI_CONTROL_MASK},
  1546. .enabled = tcm_is_pr_enabled,
  1547. };
  1548. static const struct target_opcode_descriptor tcm_opcode_pro_register = {
  1549. .support = SCSI_SUPPORT_FULL,
  1550. .serv_action_valid = 1,
  1551. .opcode = PERSISTENT_RESERVE_OUT,
  1552. .service_action = PRO_REGISTER,
  1553. .cdb_size = 10,
  1554. .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_REGISTER, 0xff, 0x00,
  1555. 0x00, 0xff, 0xff, 0xff,
  1556. 0xff, SCSI_CONTROL_MASK},
  1557. .enabled = tcm_is_pr_enabled,
  1558. };
  1559. static const struct target_opcode_descriptor tcm_opcode_pro_reserve = {
  1560. .support = SCSI_SUPPORT_FULL,
  1561. .serv_action_valid = 1,
  1562. .opcode = PERSISTENT_RESERVE_OUT,
  1563. .service_action = PRO_RESERVE,
  1564. .cdb_size = 10,
  1565. .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_RESERVE, 0xff, 0x00,
  1566. 0x00, 0xff, 0xff, 0xff,
  1567. 0xff, SCSI_CONTROL_MASK},
  1568. .enabled = tcm_is_pr_enabled,
  1569. };
  1570. static const struct target_opcode_descriptor tcm_opcode_pro_release = {
  1571. .support = SCSI_SUPPORT_FULL,
  1572. .serv_action_valid = 1,
  1573. .opcode = PERSISTENT_RESERVE_OUT,
  1574. .service_action = PRO_RELEASE,
  1575. .cdb_size = 10,
  1576. .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_RELEASE, 0xff, 0x00,
  1577. 0x00, 0xff, 0xff, 0xff,
  1578. 0xff, SCSI_CONTROL_MASK},
  1579. .enabled = tcm_is_pr_enabled,
  1580. };
  1581. static const struct target_opcode_descriptor tcm_opcode_pro_clear = {
  1582. .support = SCSI_SUPPORT_FULL,
  1583. .serv_action_valid = 1,
  1584. .opcode = PERSISTENT_RESERVE_OUT,
  1585. .service_action = PRO_CLEAR,
  1586. .cdb_size = 10,
  1587. .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_CLEAR, 0xff, 0x00,
  1588. 0x00, 0xff, 0xff, 0xff,
  1589. 0xff, SCSI_CONTROL_MASK},
  1590. .enabled = tcm_is_pr_enabled,
  1591. };
  1592. static const struct target_opcode_descriptor tcm_opcode_pro_preempt = {
  1593. .support = SCSI_SUPPORT_FULL,
  1594. .serv_action_valid = 1,
  1595. .opcode = PERSISTENT_RESERVE_OUT,
  1596. .service_action = PRO_PREEMPT,
  1597. .cdb_size = 10,
  1598. .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_PREEMPT, 0xff, 0x00,
  1599. 0x00, 0xff, 0xff, 0xff,
  1600. 0xff, SCSI_CONTROL_MASK},
  1601. .enabled = tcm_is_pr_enabled,
  1602. };
  1603. static const struct target_opcode_descriptor tcm_opcode_pro_preempt_abort = {
  1604. .support = SCSI_SUPPORT_FULL,
  1605. .serv_action_valid = 1,
  1606. .opcode = PERSISTENT_RESERVE_OUT,
  1607. .service_action = PRO_PREEMPT_AND_ABORT,
  1608. .cdb_size = 10,
  1609. .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_PREEMPT_AND_ABORT, 0xff, 0x00,
  1610. 0x00, 0xff, 0xff, 0xff,
  1611. 0xff, SCSI_CONTROL_MASK},
  1612. .enabled = tcm_is_pr_enabled,
  1613. };
  1614. static const struct target_opcode_descriptor tcm_opcode_pro_reg_ign_exist = {
  1615. .support = SCSI_SUPPORT_FULL,
  1616. .serv_action_valid = 1,
  1617. .opcode = PERSISTENT_RESERVE_OUT,
  1618. .service_action = PRO_REGISTER_AND_IGNORE_EXISTING_KEY,
  1619. .cdb_size = 10,
  1620. .usage_bits = {
  1621. PERSISTENT_RESERVE_OUT, PRO_REGISTER_AND_IGNORE_EXISTING_KEY,
  1622. 0xff, 0x00,
  1623. 0x00, 0xff, 0xff, 0xff,
  1624. 0xff, SCSI_CONTROL_MASK},
  1625. .enabled = tcm_is_pr_enabled,
  1626. };
  1627. static const struct target_opcode_descriptor tcm_opcode_pro_register_move = {
  1628. .support = SCSI_SUPPORT_FULL,
  1629. .serv_action_valid = 1,
  1630. .opcode = PERSISTENT_RESERVE_OUT,
  1631. .service_action = PRO_REGISTER_AND_MOVE,
  1632. .cdb_size = 10,
  1633. .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_REGISTER_AND_MOVE, 0xff, 0x00,
  1634. 0x00, 0xff, 0xff, 0xff,
  1635. 0xff, SCSI_CONTROL_MASK},
  1636. .enabled = tcm_is_pr_enabled,
  1637. };
  1638. static const struct target_opcode_descriptor tcm_opcode_release = {
  1639. .support = SCSI_SUPPORT_FULL,
  1640. .opcode = RELEASE_6,
  1641. .cdb_size = 6,
  1642. .usage_bits = {RELEASE_6, 0x00, 0x00, 0x00,
  1643. 0x00, SCSI_CONTROL_MASK},
  1644. .enabled = tcm_is_pr_enabled,
  1645. };
  1646. static const struct target_opcode_descriptor tcm_opcode_release10 = {
  1647. .support = SCSI_SUPPORT_FULL,
  1648. .opcode = RELEASE_10,
  1649. .cdb_size = 10,
  1650. .usage_bits = {RELEASE_10, 0x00, 0x00, 0x00,
  1651. 0x00, 0x00, 0x00, 0xff,
  1652. 0xff, SCSI_CONTROL_MASK},
  1653. .enabled = tcm_is_pr_enabled,
  1654. };
  1655. static const struct target_opcode_descriptor tcm_opcode_reserve = {
  1656. .support = SCSI_SUPPORT_FULL,
  1657. .opcode = RESERVE_6,
  1658. .cdb_size = 6,
  1659. .usage_bits = {RESERVE_6, 0x00, 0x00, 0x00,
  1660. 0x00, SCSI_CONTROL_MASK},
  1661. .enabled = tcm_is_pr_enabled,
  1662. };
  1663. static const struct target_opcode_descriptor tcm_opcode_reserve10 = {
  1664. .support = SCSI_SUPPORT_FULL,
  1665. .opcode = RESERVE_10,
  1666. .cdb_size = 10,
  1667. .usage_bits = {RESERVE_10, 0x00, 0x00, 0x00,
  1668. 0x00, 0x00, 0x00, 0xff,
  1669. 0xff, SCSI_CONTROL_MASK},
  1670. .enabled = tcm_is_pr_enabled,
  1671. };
  1672. static const struct target_opcode_descriptor tcm_opcode_request_sense = {
  1673. .support = SCSI_SUPPORT_FULL,
  1674. .opcode = REQUEST_SENSE,
  1675. .cdb_size = 6,
  1676. .usage_bits = {REQUEST_SENSE, 0x00, 0x00, 0x00,
  1677. 0xff, SCSI_CONTROL_MASK},
  1678. };
  1679. static const struct target_opcode_descriptor tcm_opcode_inquiry = {
  1680. .support = SCSI_SUPPORT_FULL,
  1681. .opcode = INQUIRY,
  1682. .cdb_size = 6,
  1683. .usage_bits = {INQUIRY, 0x01, 0xff, 0xff,
  1684. 0xff, SCSI_CONTROL_MASK},
  1685. };
  1686. static bool tcm_is_3pc_enabled(const struct target_opcode_descriptor *descr,
  1687. struct se_cmd *cmd)
  1688. {
  1689. struct se_device *dev = cmd->se_dev;
  1690. return dev->dev_attrib.emulate_3pc;
  1691. }
  1692. static const struct target_opcode_descriptor tcm_opcode_extended_copy_lid1 = {
  1693. .support = SCSI_SUPPORT_FULL,
  1694. .serv_action_valid = 1,
  1695. .opcode = EXTENDED_COPY,
  1696. .cdb_size = 16,
  1697. .usage_bits = {EXTENDED_COPY, 0x00, 0x00, 0x00,
  1698. 0x00, 0x00, 0x00, 0x00,
  1699. 0x00, 0x00, 0xff, 0xff,
  1700. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1701. .enabled = tcm_is_3pc_enabled,
  1702. };
  1703. static const struct target_opcode_descriptor tcm_opcode_rcv_copy_res_op_params = {
  1704. .support = SCSI_SUPPORT_FULL,
  1705. .serv_action_valid = 1,
  1706. .opcode = RECEIVE_COPY_RESULTS,
  1707. .service_action = RCR_SA_OPERATING_PARAMETERS,
  1708. .cdb_size = 16,
  1709. .usage_bits = {RECEIVE_COPY_RESULTS, RCR_SA_OPERATING_PARAMETERS,
  1710. 0x00, 0x00,
  1711. 0x00, 0x00, 0x00, 0x00,
  1712. 0x00, 0x00, 0xff, 0xff,
  1713. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1714. .enabled = tcm_is_3pc_enabled,
  1715. };
  1716. static const struct target_opcode_descriptor tcm_opcode_report_luns = {
  1717. .support = SCSI_SUPPORT_FULL,
  1718. .opcode = REPORT_LUNS,
  1719. .cdb_size = 12,
  1720. .usage_bits = {REPORT_LUNS, 0x00, 0xff, 0x00,
  1721. 0x00, 0x00, 0xff, 0xff,
  1722. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1723. };
  1724. static const struct target_opcode_descriptor tcm_opcode_test_unit_ready = {
  1725. .support = SCSI_SUPPORT_FULL,
  1726. .opcode = TEST_UNIT_READY,
  1727. .cdb_size = 6,
  1728. .usage_bits = {TEST_UNIT_READY, 0x00, 0x00, 0x00,
  1729. 0x00, SCSI_CONTROL_MASK},
  1730. };
  1731. static const struct target_opcode_descriptor tcm_opcode_report_target_pgs = {
  1732. .support = SCSI_SUPPORT_FULL,
  1733. .serv_action_valid = 1,
  1734. .opcode = MAINTENANCE_IN,
  1735. .service_action = MI_REPORT_TARGET_PGS,
  1736. .cdb_size = 12,
  1737. .usage_bits = {MAINTENANCE_IN, 0xE0 | MI_REPORT_TARGET_PGS, 0x00, 0x00,
  1738. 0x00, 0x00, 0xff, 0xff,
  1739. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1740. };
  1741. static bool spc_rsoc_enabled(const struct target_opcode_descriptor *descr,
  1742. struct se_cmd *cmd)
  1743. {
  1744. struct se_device *dev = cmd->se_dev;
  1745. return dev->dev_attrib.emulate_rsoc;
  1746. }
  1747. static const struct target_opcode_descriptor tcm_opcode_report_supp_opcodes = {
  1748. .support = SCSI_SUPPORT_FULL,
  1749. .serv_action_valid = 1,
  1750. .opcode = MAINTENANCE_IN,
  1751. .service_action = MI_REPORT_SUPPORTED_OPERATION_CODES,
  1752. .cdb_size = 12,
  1753. .usage_bits = {MAINTENANCE_IN, MI_REPORT_SUPPORTED_OPERATION_CODES,
  1754. 0x87, 0xff,
  1755. 0xff, 0xff, 0xff, 0xff,
  1756. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1757. .enabled = spc_rsoc_enabled,
  1758. };
  1759. static struct target_opcode_descriptor tcm_opcode_report_identifying_information = {
  1760. .support = SCSI_SUPPORT_FULL,
  1761. .serv_action_valid = 1,
  1762. .opcode = MAINTENANCE_IN,
  1763. .service_action = MI_REPORT_IDENTIFYING_INFORMATION,
  1764. .cdb_size = 12,
  1765. .usage_bits = {MAINTENANCE_IN, MI_REPORT_IDENTIFYING_INFORMATION,
  1766. 0x00, 0x00,
  1767. 0x00, 0x00, 0xff, 0xff,
  1768. 0xff, 0xff, 0xff, SCSI_CONTROL_MASK},
  1769. };
  1770. static bool tcm_is_set_tpg_enabled(const struct target_opcode_descriptor *descr,
  1771. struct se_cmd *cmd)
  1772. {
  1773. struct t10_alua_tg_pt_gp *l_tg_pt_gp;
  1774. struct se_lun *l_lun = cmd->se_lun;
  1775. rcu_read_lock();
  1776. l_tg_pt_gp = rcu_dereference(l_lun->lun_tg_pt_gp);
  1777. if (!l_tg_pt_gp) {
  1778. rcu_read_unlock();
  1779. return false;
  1780. }
  1781. if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
  1782. rcu_read_unlock();
  1783. return false;
  1784. }
  1785. rcu_read_unlock();
  1786. return true;
  1787. }
  1788. static const struct target_opcode_descriptor tcm_opcode_set_tpg = {
  1789. .support = SCSI_SUPPORT_FULL,
  1790. .serv_action_valid = 1,
  1791. .opcode = MAINTENANCE_OUT,
  1792. .service_action = MO_SET_TARGET_PGS,
  1793. .cdb_size = 12,
  1794. .usage_bits = {MAINTENANCE_OUT, MO_SET_TARGET_PGS, 0x00, 0x00,
  1795. 0x00, 0x00, 0xff, 0xff,
  1796. 0xff, 0xff, 0x00, SCSI_CONTROL_MASK},
  1797. .enabled = tcm_is_set_tpg_enabled,
  1798. };
  1799. static const struct target_opcode_descriptor *tcm_supported_opcodes[] = {
  1800. &tcm_opcode_read6,
  1801. &tcm_opcode_read10,
  1802. &tcm_opcode_read12,
  1803. &tcm_opcode_read16,
  1804. &tcm_opcode_write6,
  1805. &tcm_opcode_write10,
  1806. &tcm_opcode_write_verify10,
  1807. &tcm_opcode_write12,
  1808. &tcm_opcode_write16,
  1809. &tcm_opcode_write_verify16,
  1810. &tcm_opcode_write_same32,
  1811. &tcm_opcode_write_atomic16,
  1812. &tcm_opcode_compare_write,
  1813. &tcm_opcode_read_capacity,
  1814. &tcm_opcode_read_capacity16,
  1815. &tcm_opcode_read_report_refferals,
  1816. &tcm_opcode_sync_cache,
  1817. &tcm_opcode_sync_cache16,
  1818. &tcm_opcode_unmap,
  1819. &tcm_opcode_write_same,
  1820. &tcm_opcode_write_same16,
  1821. &tcm_opcode_verify,
  1822. &tcm_opcode_verify16,
  1823. &tcm_opcode_start_stop,
  1824. &tcm_opcode_mode_select,
  1825. &tcm_opcode_mode_select10,
  1826. &tcm_opcode_mode_sense,
  1827. &tcm_opcode_mode_sense10,
  1828. &tcm_opcode_pri_read_keys,
  1829. &tcm_opcode_pri_read_resrv,
  1830. &tcm_opcode_pri_read_caps,
  1831. &tcm_opcode_pri_read_full_status,
  1832. &tcm_opcode_pro_register,
  1833. &tcm_opcode_pro_reserve,
  1834. &tcm_opcode_pro_release,
  1835. &tcm_opcode_pro_clear,
  1836. &tcm_opcode_pro_preempt,
  1837. &tcm_opcode_pro_preempt_abort,
  1838. &tcm_opcode_pro_reg_ign_exist,
  1839. &tcm_opcode_pro_register_move,
  1840. &tcm_opcode_release,
  1841. &tcm_opcode_release10,
  1842. &tcm_opcode_reserve,
  1843. &tcm_opcode_reserve10,
  1844. &tcm_opcode_request_sense,
  1845. &tcm_opcode_inquiry,
  1846. &tcm_opcode_extended_copy_lid1,
  1847. &tcm_opcode_rcv_copy_res_op_params,
  1848. &tcm_opcode_report_luns,
  1849. &tcm_opcode_test_unit_ready,
  1850. &tcm_opcode_report_target_pgs,
  1851. &tcm_opcode_report_supp_opcodes,
  1852. &tcm_opcode_set_tpg,
  1853. &tcm_opcode_report_identifying_information,
  1854. };
  1855. static int
  1856. spc_rsoc_encode_command_timeouts_descriptor(unsigned char *buf, u8 ctdp,
  1857. const struct target_opcode_descriptor *descr)
  1858. {
  1859. if (!ctdp)
  1860. return 0;
  1861. put_unaligned_be16(0xa, buf);
  1862. buf[3] = descr->specific_timeout;
  1863. put_unaligned_be32(descr->nominal_timeout, &buf[4]);
  1864. put_unaligned_be32(descr->recommended_timeout, &buf[8]);
  1865. return 12;
  1866. }
  1867. static int
  1868. spc_rsoc_encode_command_descriptor(unsigned char *buf, u8 ctdp,
  1869. const struct target_opcode_descriptor *descr)
  1870. {
  1871. int td_size = 0;
  1872. buf[0] = descr->opcode;
  1873. put_unaligned_be16(descr->service_action, &buf[2]);
  1874. buf[5] = (ctdp << 1) | descr->serv_action_valid;
  1875. put_unaligned_be16(descr->cdb_size, &buf[6]);
  1876. td_size = spc_rsoc_encode_command_timeouts_descriptor(&buf[8], ctdp,
  1877. descr);
  1878. return 8 + td_size;
  1879. }
  1880. static int
  1881. spc_rsoc_encode_one_command_descriptor(unsigned char *buf, u8 ctdp,
  1882. const struct target_opcode_descriptor *descr,
  1883. struct se_device *dev)
  1884. {
  1885. int td_size = 0;
  1886. if (!descr) {
  1887. buf[1] = (ctdp << 7) | SCSI_SUPPORT_NOT_SUPPORTED;
  1888. return 2;
  1889. }
  1890. buf[1] = (ctdp << 7) | SCSI_SUPPORT_FULL;
  1891. put_unaligned_be16(descr->cdb_size, &buf[2]);
  1892. memcpy(&buf[4], descr->usage_bits, descr->cdb_size);
  1893. if (descr->update_usage_bits)
  1894. descr->update_usage_bits(&buf[4], dev);
  1895. td_size = spc_rsoc_encode_command_timeouts_descriptor(
  1896. &buf[4 + descr->cdb_size], ctdp, descr);
  1897. return 4 + descr->cdb_size + td_size;
  1898. }
  1899. static sense_reason_t
  1900. spc_rsoc_get_descr(struct se_cmd *cmd, const struct target_opcode_descriptor **opcode)
  1901. {
  1902. const struct target_opcode_descriptor *descr;
  1903. struct se_session *sess = cmd->se_sess;
  1904. unsigned char *cdb = cmd->t_task_cdb;
  1905. u8 opts = cdb[2] & 0x3;
  1906. u8 requested_opcode;
  1907. u16 requested_sa;
  1908. int i;
  1909. requested_opcode = cdb[3];
  1910. requested_sa = ((u16)cdb[4]) << 8 | cdb[5];
  1911. *opcode = NULL;
  1912. if (opts > 3) {
  1913. pr_debug("TARGET_CORE[%s]: Invalid REPORT SUPPORTED OPERATION CODES"
  1914. " with unsupported REPORTING OPTIONS %#x for 0x%08llx from %s\n",
  1915. cmd->se_tfo->fabric_name, opts,
  1916. cmd->se_lun->unpacked_lun,
  1917. sess->se_node_acl->initiatorname);
  1918. return TCM_INVALID_CDB_FIELD;
  1919. }
  1920. for (i = 0; i < ARRAY_SIZE(tcm_supported_opcodes); i++) {
  1921. descr = tcm_supported_opcodes[i];
  1922. if (descr->opcode != requested_opcode)
  1923. continue;
  1924. switch (opts) {
  1925. case 0x1:
  1926. /*
  1927. * If the REQUESTED OPERATION CODE field specifies an
  1928. * operation code for which the device server implements
  1929. * service actions, then the device server shall
  1930. * terminate the command with CHECK CONDITION status,
  1931. * with the sense key set to ILLEGAL REQUEST, and the
  1932. * additional sense code set to INVALID FIELD IN CDB
  1933. */
  1934. if (descr->serv_action_valid)
  1935. return TCM_INVALID_CDB_FIELD;
  1936. if (!descr->enabled || descr->enabled(descr, cmd)) {
  1937. *opcode = descr;
  1938. return TCM_NO_SENSE;
  1939. }
  1940. break;
  1941. case 0x2:
  1942. /*
  1943. * If the REQUESTED OPERATION CODE field specifies an
  1944. * operation code for which the device server does not
  1945. * implement service actions, then the device server
  1946. * shall terminate the command with CHECK CONDITION
  1947. * status, with the sense key set to ILLEGAL REQUEST,
  1948. * and the additional sense code set to INVALID FIELD IN CDB.
  1949. */
  1950. if (descr->serv_action_valid &&
  1951. descr->service_action == requested_sa) {
  1952. if (!descr->enabled || descr->enabled(descr,
  1953. cmd)) {
  1954. *opcode = descr;
  1955. return TCM_NO_SENSE;
  1956. }
  1957. } else if (!descr->serv_action_valid)
  1958. return TCM_INVALID_CDB_FIELD;
  1959. break;
  1960. case 0x3:
  1961. /*
  1962. * The command support data for the operation code and
  1963. * service action a specified in the REQUESTED OPERATION
  1964. * CODE field and REQUESTED SERVICE ACTION field shall
  1965. * be returned in the one_command parameter data format.
  1966. */
  1967. if (descr->service_action == requested_sa)
  1968. if (!descr->enabled || descr->enabled(descr,
  1969. cmd)) {
  1970. *opcode = descr;
  1971. return TCM_NO_SENSE;
  1972. }
  1973. break;
  1974. }
  1975. }
  1976. return TCM_NO_SENSE;
  1977. }
  1978. static sense_reason_t
  1979. spc_emulate_report_supp_op_codes(struct se_cmd *cmd)
  1980. {
  1981. int descr_num = ARRAY_SIZE(tcm_supported_opcodes);
  1982. const struct target_opcode_descriptor *descr = NULL;
  1983. unsigned char *cdb = cmd->t_task_cdb;
  1984. u8 rctd = (cdb[2] >> 7) & 0x1;
  1985. unsigned char *buf = NULL;
  1986. int response_length = 0;
  1987. u8 opts = cdb[2] & 0x3;
  1988. unsigned char *rbuf;
  1989. sense_reason_t ret = 0;
  1990. int i;
  1991. if (!cmd->se_dev->dev_attrib.emulate_rsoc)
  1992. return TCM_UNSUPPORTED_SCSI_OPCODE;
  1993. rbuf = transport_kmap_data_sg(cmd);
  1994. if (cmd->data_length && !rbuf) {
  1995. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1996. goto out;
  1997. }
  1998. if (opts == 0)
  1999. response_length = 4 + (8 + rctd * 12) * descr_num;
  2000. else {
  2001. ret = spc_rsoc_get_descr(cmd, &descr);
  2002. if (ret)
  2003. goto out;
  2004. if (descr)
  2005. response_length = 4 + descr->cdb_size + rctd * 12;
  2006. else
  2007. response_length = 2;
  2008. }
  2009. buf = kzalloc(response_length, GFP_KERNEL);
  2010. if (!buf) {
  2011. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2012. goto out;
  2013. }
  2014. response_length = 0;
  2015. if (opts == 0) {
  2016. response_length += 4;
  2017. for (i = 0; i < ARRAY_SIZE(tcm_supported_opcodes); i++) {
  2018. descr = tcm_supported_opcodes[i];
  2019. if (descr->enabled && !descr->enabled(descr, cmd))
  2020. continue;
  2021. response_length += spc_rsoc_encode_command_descriptor(
  2022. &buf[response_length], rctd, descr);
  2023. }
  2024. put_unaligned_be32(response_length - 4, buf);
  2025. } else {
  2026. response_length = spc_rsoc_encode_one_command_descriptor(
  2027. &buf[response_length], rctd, descr,
  2028. cmd->se_dev);
  2029. }
  2030. memcpy(rbuf, buf, min_t(u32, response_length, cmd->data_length));
  2031. out:
  2032. kfree(buf);
  2033. transport_kunmap_data_sg(cmd);
  2034. if (!ret)
  2035. target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, response_length);
  2036. return ret;
  2037. }
  2038. static sense_reason_t
  2039. spc_fill_pd_text_id_info(struct se_cmd *cmd, u8 *cdb)
  2040. {
  2041. struct se_device *dev = cmd->se_dev;
  2042. unsigned char *buf;
  2043. unsigned char *rbuf;
  2044. u32 buf_len;
  2045. u16 data_len;
  2046. buf_len = get_unaligned_be32(&cdb[6]);
  2047. if (buf_len < PD_TEXT_ID_INFO_HDR_LEN)
  2048. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2049. data_len = strlen(dev->t10_wwn.pd_text_id_info);
  2050. if (data_len > 0)
  2051. /* trailing null */
  2052. data_len += 1;
  2053. data_len = data_len + PD_TEXT_ID_INFO_HDR_LEN;
  2054. if (data_len < buf_len)
  2055. buf_len = data_len;
  2056. buf = kzalloc(buf_len, GFP_KERNEL);
  2057. if (!buf) {
  2058. pr_err("Unable to allocate response buffer for IDENTITY INFO\n");
  2059. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2060. }
  2061. scnprintf(&buf[PD_TEXT_ID_INFO_HDR_LEN], buf_len - PD_TEXT_ID_INFO_HDR_LEN, "%s",
  2062. dev->t10_wwn.pd_text_id_info);
  2063. put_unaligned_be16(data_len, &buf[2]);
  2064. rbuf = transport_kmap_data_sg(cmd);
  2065. if (!rbuf) {
  2066. pr_err("transport_kmap_data_sg() failed in %s\n", __func__);
  2067. kfree(buf);
  2068. return TCM_OUT_OF_RESOURCES;
  2069. }
  2070. memcpy(rbuf, buf, buf_len);
  2071. transport_kunmap_data_sg(cmd);
  2072. kfree(buf);
  2073. target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, buf_len);
  2074. return TCM_NO_SENSE;
  2075. }
  2076. static sense_reason_t
  2077. spc_emulate_report_id_info(struct se_cmd *cmd)
  2078. {
  2079. u8 *cdb = cmd->t_task_cdb;
  2080. sense_reason_t rc;
  2081. switch ((cdb[10] >> 1)) {
  2082. case 2:
  2083. rc = spc_fill_pd_text_id_info(cmd, cdb);
  2084. break;
  2085. default:
  2086. return TCM_UNSUPPORTED_SCSI_OPCODE;
  2087. }
  2088. return rc;
  2089. }
  2090. sense_reason_t
  2091. spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  2092. {
  2093. struct se_device *dev = cmd->se_dev;
  2094. unsigned char *cdb = cmd->t_task_cdb;
  2095. switch (cdb[0]) {
  2096. case RESERVE_6:
  2097. case RESERVE_10:
  2098. case RELEASE_6:
  2099. case RELEASE_10:
  2100. if (!dev->dev_attrib.emulate_pr)
  2101. return TCM_UNSUPPORTED_SCSI_OPCODE;
  2102. if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
  2103. return TCM_UNSUPPORTED_SCSI_OPCODE;
  2104. break;
  2105. case PERSISTENT_RESERVE_IN:
  2106. case PERSISTENT_RESERVE_OUT:
  2107. if (!dev->dev_attrib.emulate_pr)
  2108. return TCM_UNSUPPORTED_SCSI_OPCODE;
  2109. break;
  2110. }
  2111. switch (cdb[0]) {
  2112. case MODE_SELECT:
  2113. *size = cdb[4];
  2114. cmd->execute_cmd = spc_emulate_modeselect;
  2115. break;
  2116. case MODE_SELECT_10:
  2117. *size = get_unaligned_be16(&cdb[7]);
  2118. cmd->execute_cmd = spc_emulate_modeselect;
  2119. break;
  2120. case MODE_SENSE:
  2121. *size = cdb[4];
  2122. cmd->execute_cmd = spc_emulate_modesense;
  2123. break;
  2124. case MODE_SENSE_10:
  2125. *size = get_unaligned_be16(&cdb[7]);
  2126. cmd->execute_cmd = spc_emulate_modesense;
  2127. break;
  2128. case LOG_SELECT:
  2129. case LOG_SENSE:
  2130. *size = get_unaligned_be16(&cdb[7]);
  2131. break;
  2132. case PERSISTENT_RESERVE_IN:
  2133. *size = get_unaligned_be16(&cdb[7]);
  2134. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  2135. break;
  2136. case PERSISTENT_RESERVE_OUT:
  2137. *size = get_unaligned_be32(&cdb[5]);
  2138. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  2139. break;
  2140. case RELEASE_6:
  2141. case RELEASE_10:
  2142. if (cdb[0] == RELEASE_10)
  2143. *size = get_unaligned_be16(&cdb[7]);
  2144. else
  2145. *size = cmd->data_length;
  2146. cmd->execute_cmd = target_scsi2_reservation_release;
  2147. break;
  2148. case RESERVE_6:
  2149. case RESERVE_10:
  2150. /*
  2151. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2152. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2153. */
  2154. if (cdb[0] == RESERVE_10)
  2155. *size = get_unaligned_be16(&cdb[7]);
  2156. else
  2157. *size = cmd->data_length;
  2158. cmd->execute_cmd = target_scsi2_reservation_reserve;
  2159. break;
  2160. case REQUEST_SENSE:
  2161. *size = cdb[4];
  2162. cmd->execute_cmd = spc_emulate_request_sense;
  2163. break;
  2164. case INQUIRY:
  2165. *size = get_unaligned_be16(&cdb[3]);
  2166. /*
  2167. * Do implicit HEAD_OF_QUEUE processing for INQUIRY.
  2168. * See spc4r17 section 5.3
  2169. */
  2170. cmd->sam_task_attr = TCM_HEAD_TAG;
  2171. cmd->execute_cmd = spc_emulate_inquiry;
  2172. break;
  2173. case SECURITY_PROTOCOL_IN:
  2174. case SECURITY_PROTOCOL_OUT:
  2175. *size = get_unaligned_be32(&cdb[6]);
  2176. break;
  2177. case EXTENDED_COPY:
  2178. *size = get_unaligned_be32(&cdb[10]);
  2179. cmd->execute_cmd = target_do_xcopy;
  2180. break;
  2181. case RECEIVE_COPY_RESULTS:
  2182. *size = get_unaligned_be32(&cdb[10]);
  2183. cmd->execute_cmd = target_do_receive_copy_results;
  2184. break;
  2185. case READ_ATTRIBUTE:
  2186. case WRITE_ATTRIBUTE:
  2187. *size = get_unaligned_be32(&cdb[10]);
  2188. break;
  2189. case RECEIVE_DIAGNOSTIC:
  2190. case SEND_DIAGNOSTIC:
  2191. *size = get_unaligned_be16(&cdb[3]);
  2192. break;
  2193. case WRITE_BUFFER:
  2194. *size = get_unaligned_be24(&cdb[6]);
  2195. break;
  2196. case REPORT_LUNS:
  2197. cmd->execute_cmd = spc_emulate_report_luns;
  2198. *size = get_unaligned_be32(&cdb[6]);
  2199. /*
  2200. * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS
  2201. * See spc4r17 section 5.3
  2202. */
  2203. cmd->sam_task_attr = TCM_HEAD_TAG;
  2204. break;
  2205. case TEST_UNIT_READY:
  2206. cmd->execute_cmd = spc_emulate_testunitready;
  2207. *size = 0;
  2208. break;
  2209. case MAINTENANCE_IN:
  2210. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2211. /*
  2212. * MAINTENANCE_IN from SCC-2
  2213. * Check for emulated MI_REPORT_TARGET_PGS
  2214. */
  2215. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  2216. cmd->execute_cmd =
  2217. target_emulate_report_target_port_groups;
  2218. }
  2219. if ((cdb[1] & 0x1f) ==
  2220. MI_REPORT_SUPPORTED_OPERATION_CODES)
  2221. cmd->execute_cmd =
  2222. spc_emulate_report_supp_op_codes;
  2223. if ((cdb[1] & 0x1f) ==
  2224. MI_REPORT_IDENTIFYING_INFORMATION) {
  2225. cmd->execute_cmd =
  2226. spc_emulate_report_id_info;
  2227. }
  2228. *size = get_unaligned_be32(&cdb[6]);
  2229. } else {
  2230. /*
  2231. * GPCMD_SEND_KEY from multi media commands
  2232. */
  2233. *size = get_unaligned_be16(&cdb[8]);
  2234. }
  2235. break;
  2236. case MAINTENANCE_OUT:
  2237. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2238. /*
  2239. * MAINTENANCE_OUT from SCC-2
  2240. * Check for emulated MO_SET_TARGET_PGS.
  2241. */
  2242. if (cdb[1] == MO_SET_TARGET_PGS) {
  2243. cmd->execute_cmd =
  2244. target_emulate_set_target_port_groups;
  2245. }
  2246. *size = get_unaligned_be32(&cdb[6]);
  2247. } else {
  2248. /*
  2249. * GPCMD_SEND_KEY from multi media commands
  2250. */
  2251. *size = get_unaligned_be16(&cdb[8]);
  2252. }
  2253. break;
  2254. default:
  2255. return TCM_UNSUPPORTED_SCSI_OPCODE;
  2256. }
  2257. return 0;
  2258. }
  2259. EXPORT_SYMBOL(spc_parse_cdb);