scsi.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574
  1. /*
  2. * NVM Express device driver
  3. * Copyright (c) 2011-2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. /*
  15. * Refer to the SCSI-NVMe Translation spec for details on how
  16. * each command is translated.
  17. */
  18. #include <linux/bio.h>
  19. #include <linux/bitops.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/compat.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/genhd.h>
  26. #include <linux/idr.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/kdev_t.h>
  31. #include <linux/kthread.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/pci.h>
  37. #include <linux/poison.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <asm/unaligned.h>
  42. #include <scsi/sg.h>
  43. #include <scsi/scsi.h>
  44. #include "nvme.h"
  45. static int sg_version_num = 30534; /* 2 digits for each component */
  46. /* VPD Page Codes */
  47. #define VPD_SUPPORTED_PAGES 0x00
  48. #define VPD_SERIAL_NUMBER 0x80
  49. #define VPD_DEVICE_IDENTIFIERS 0x83
  50. #define VPD_EXTENDED_INQUIRY 0x86
  51. #define VPD_BLOCK_LIMITS 0xB0
  52. #define VPD_BLOCK_DEV_CHARACTERISTICS 0xB1
  53. /* format unit paramter list offsets */
  54. #define FORMAT_UNIT_SHORT_PARM_LIST_LEN 4
  55. #define FORMAT_UNIT_LONG_PARM_LIST_LEN 8
  56. #define FORMAT_UNIT_PROT_INT_OFFSET 3
  57. #define FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET 0
  58. #define FORMAT_UNIT_PROT_FIELD_USAGE_MASK 0x07
  59. /* Misc. defines */
  60. #define FIXED_SENSE_DATA 0x70
  61. #define DESC_FORMAT_SENSE_DATA 0x72
  62. #define FIXED_SENSE_DATA_ADD_LENGTH 10
  63. #define LUN_ENTRY_SIZE 8
  64. #define LUN_DATA_HEADER_SIZE 8
  65. #define ALL_LUNS_RETURNED 0x02
  66. #define ALL_WELL_KNOWN_LUNS_RETURNED 0x01
  67. #define RESTRICTED_LUNS_RETURNED 0x00
  68. #define NVME_POWER_STATE_START_VALID 0x00
  69. #define NVME_POWER_STATE_ACTIVE 0x01
  70. #define NVME_POWER_STATE_IDLE 0x02
  71. #define NVME_POWER_STATE_STANDBY 0x03
  72. #define NVME_POWER_STATE_LU_CONTROL 0x07
  73. #define POWER_STATE_0 0
  74. #define POWER_STATE_1 1
  75. #define POWER_STATE_2 2
  76. #define POWER_STATE_3 3
  77. #define DOWNLOAD_SAVE_ACTIVATE 0x05
  78. #define DOWNLOAD_SAVE_DEFER_ACTIVATE 0x0E
  79. #define ACTIVATE_DEFERRED_MICROCODE 0x0F
  80. #define FORMAT_UNIT_IMMED_MASK 0x2
  81. #define FORMAT_UNIT_IMMED_OFFSET 1
  82. #define KELVIN_TEMP_FACTOR 273
  83. #define FIXED_FMT_SENSE_DATA_SIZE 18
  84. #define DESC_FMT_SENSE_DATA_SIZE 8
  85. /* SCSI/NVMe defines and bit masks */
  86. #define INQ_STANDARD_INQUIRY_PAGE 0x00
  87. #define INQ_SUPPORTED_VPD_PAGES_PAGE 0x00
  88. #define INQ_UNIT_SERIAL_NUMBER_PAGE 0x80
  89. #define INQ_DEVICE_IDENTIFICATION_PAGE 0x83
  90. #define INQ_EXTENDED_INQUIRY_DATA_PAGE 0x86
  91. #define INQ_BDEV_LIMITS_PAGE 0xB0
  92. #define INQ_BDEV_CHARACTERISTICS_PAGE 0xB1
  93. #define INQ_SERIAL_NUMBER_LENGTH 0x14
  94. #define INQ_NUM_SUPPORTED_VPD_PAGES 6
  95. #define VERSION_SPC_4 0x06
  96. #define ACA_UNSUPPORTED 0
  97. #define STANDARD_INQUIRY_LENGTH 36
  98. #define ADDITIONAL_STD_INQ_LENGTH 31
  99. #define EXTENDED_INQUIRY_DATA_PAGE_LENGTH 0x3C
  100. #define RESERVED_FIELD 0
  101. /* Mode Sense/Select defines */
  102. #define MODE_PAGE_INFO_EXCEP 0x1C
  103. #define MODE_PAGE_CACHING 0x08
  104. #define MODE_PAGE_CONTROL 0x0A
  105. #define MODE_PAGE_POWER_CONDITION 0x1A
  106. #define MODE_PAGE_RETURN_ALL 0x3F
  107. #define MODE_PAGE_BLK_DES_LEN 0x08
  108. #define MODE_PAGE_LLBAA_BLK_DES_LEN 0x10
  109. #define MODE_PAGE_CACHING_LEN 0x14
  110. #define MODE_PAGE_CONTROL_LEN 0x0C
  111. #define MODE_PAGE_POW_CND_LEN 0x28
  112. #define MODE_PAGE_INF_EXC_LEN 0x0C
  113. #define MODE_PAGE_ALL_LEN 0x54
  114. #define MODE_SENSE6_MPH_SIZE 4
  115. #define MODE_SENSE_PAGE_CONTROL_MASK 0xC0
  116. #define MODE_SENSE_PAGE_CODE_OFFSET 2
  117. #define MODE_SENSE_PAGE_CODE_MASK 0x3F
  118. #define MODE_SENSE_LLBAA_MASK 0x10
  119. #define MODE_SENSE_LLBAA_SHIFT 4
  120. #define MODE_SENSE_DBD_MASK 8
  121. #define MODE_SENSE_DBD_SHIFT 3
  122. #define MODE_SENSE10_MPH_SIZE 8
  123. #define MODE_SELECT_CDB_PAGE_FORMAT_MASK 0x10
  124. #define MODE_SELECT_CDB_SAVE_PAGES_MASK 0x1
  125. #define MODE_SELECT_6_BD_OFFSET 3
  126. #define MODE_SELECT_10_BD_OFFSET 6
  127. #define MODE_SELECT_10_LLBAA_OFFSET 4
  128. #define MODE_SELECT_10_LLBAA_MASK 1
  129. #define MODE_SELECT_6_MPH_SIZE 4
  130. #define MODE_SELECT_10_MPH_SIZE 8
  131. #define CACHING_MODE_PAGE_WCE_MASK 0x04
  132. #define MODE_SENSE_BLK_DESC_ENABLED 0
  133. #define MODE_SENSE_BLK_DESC_COUNT 1
  134. #define MODE_SELECT_PAGE_CODE_MASK 0x3F
  135. #define SHORT_DESC_BLOCK 8
  136. #define LONG_DESC_BLOCK 16
  137. #define MODE_PAGE_POW_CND_LEN_FIELD 0x26
  138. #define MODE_PAGE_INF_EXC_LEN_FIELD 0x0A
  139. #define MODE_PAGE_CACHING_LEN_FIELD 0x12
  140. #define MODE_PAGE_CONTROL_LEN_FIELD 0x0A
  141. #define MODE_SENSE_PC_CURRENT_VALUES 0
  142. /* Log Sense defines */
  143. #define LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE 0x00
  144. #define LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH 0x07
  145. #define LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE 0x2F
  146. #define LOG_PAGE_TEMPERATURE_PAGE 0x0D
  147. #define LOG_SENSE_CDB_SP_NOT_ENABLED 0
  148. #define LOG_SENSE_CDB_PC_MASK 0xC0
  149. #define LOG_SENSE_CDB_PC_SHIFT 6
  150. #define LOG_SENSE_CDB_PC_CUMULATIVE_VALUES 1
  151. #define LOG_SENSE_CDB_PAGE_CODE_MASK 0x3F
  152. #define REMAINING_INFO_EXCP_PAGE_LENGTH 0x8
  153. #define LOG_INFO_EXCP_PAGE_LENGTH 0xC
  154. #define REMAINING_TEMP_PAGE_LENGTH 0xC
  155. #define LOG_TEMP_PAGE_LENGTH 0x10
  156. #define LOG_TEMP_UNKNOWN 0xFF
  157. #define SUPPORTED_LOG_PAGES_PAGE_LENGTH 0x3
  158. /* Read Capacity defines */
  159. #define READ_CAP_10_RESP_SIZE 8
  160. #define READ_CAP_16_RESP_SIZE 32
  161. /* NVMe Namespace and Command Defines */
  162. #define BYTES_TO_DWORDS 4
  163. #define NVME_MAX_FIRMWARE_SLOT 7
  164. /* Report LUNs defines */
  165. #define REPORT_LUNS_FIRST_LUN_OFFSET 8
  166. /* SCSI ADDITIONAL SENSE Codes */
  167. #define SCSI_ASC_NO_SENSE 0x00
  168. #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT 0x03
  169. #define SCSI_ASC_LUN_NOT_READY 0x04
  170. #define SCSI_ASC_WARNING 0x0B
  171. #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED 0x10
  172. #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED 0x10
  173. #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED 0x10
  174. #define SCSI_ASC_UNRECOVERED_READ_ERROR 0x11
  175. #define SCSI_ASC_MISCOMPARE_DURING_VERIFY 0x1D
  176. #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID 0x20
  177. #define SCSI_ASC_ILLEGAL_COMMAND 0x20
  178. #define SCSI_ASC_ILLEGAL_BLOCK 0x21
  179. #define SCSI_ASC_INVALID_CDB 0x24
  180. #define SCSI_ASC_INVALID_LUN 0x25
  181. #define SCSI_ASC_INVALID_PARAMETER 0x26
  182. #define SCSI_ASC_FORMAT_COMMAND_FAILED 0x31
  183. #define SCSI_ASC_INTERNAL_TARGET_FAILURE 0x44
  184. /* SCSI ADDITIONAL SENSE Code Qualifiers */
  185. #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE 0x00
  186. #define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01
  187. #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED 0x01
  188. #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED 0x02
  189. #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED 0x03
  190. #define SCSI_ASCQ_FORMAT_IN_PROGRESS 0x04
  191. #define SCSI_ASCQ_POWER_LOSS_EXPECTED 0x08
  192. #define SCSI_ASCQ_INVALID_LUN_ID 0x09
  193. /* copied from drivers/usb/gadget/function/storage_common.h */
  194. static inline u32 get_unaligned_be24(u8 *buf)
  195. {
  196. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  197. }
  198. /* Struct to gather data that needs to be extracted from a SCSI CDB.
  199. Not conforming to any particular CDB variant, but compatible with all. */
  200. struct nvme_trans_io_cdb {
  201. u8 fua;
  202. u8 prot_info;
  203. u64 lba;
  204. u32 xfer_len;
  205. };
  206. /* Internal Helper Functions */
  207. /* Copy data to userspace memory */
  208. static int nvme_trans_copy_to_user(struct sg_io_hdr *hdr, void *from,
  209. unsigned long n)
  210. {
  211. int i;
  212. void *index = from;
  213. size_t remaining = n;
  214. size_t xfer_len;
  215. if (hdr->iovec_count > 0) {
  216. struct sg_iovec sgl;
  217. for (i = 0; i < hdr->iovec_count; i++) {
  218. if (copy_from_user(&sgl, hdr->dxferp +
  219. i * sizeof(struct sg_iovec),
  220. sizeof(struct sg_iovec)))
  221. return -EFAULT;
  222. xfer_len = min(remaining, sgl.iov_len);
  223. if (copy_to_user(sgl.iov_base, index, xfer_len))
  224. return -EFAULT;
  225. index += xfer_len;
  226. remaining -= xfer_len;
  227. if (remaining == 0)
  228. break;
  229. }
  230. return 0;
  231. }
  232. if (copy_to_user(hdr->dxferp, from, n))
  233. return -EFAULT;
  234. return 0;
  235. }
  236. /* Copy data from userspace memory */
  237. static int nvme_trans_copy_from_user(struct sg_io_hdr *hdr, void *to,
  238. unsigned long n)
  239. {
  240. int i;
  241. void *index = to;
  242. size_t remaining = n;
  243. size_t xfer_len;
  244. if (hdr->iovec_count > 0) {
  245. struct sg_iovec sgl;
  246. for (i = 0; i < hdr->iovec_count; i++) {
  247. if (copy_from_user(&sgl, hdr->dxferp +
  248. i * sizeof(struct sg_iovec),
  249. sizeof(struct sg_iovec)))
  250. return -EFAULT;
  251. xfer_len = min(remaining, sgl.iov_len);
  252. if (copy_from_user(index, sgl.iov_base, xfer_len))
  253. return -EFAULT;
  254. index += xfer_len;
  255. remaining -= xfer_len;
  256. if (remaining == 0)
  257. break;
  258. }
  259. return 0;
  260. }
  261. if (copy_from_user(to, hdr->dxferp, n))
  262. return -EFAULT;
  263. return 0;
  264. }
  265. /* Status/Sense Buffer Writeback */
  266. static int nvme_trans_completion(struct sg_io_hdr *hdr, u8 status, u8 sense_key,
  267. u8 asc, u8 ascq)
  268. {
  269. u8 xfer_len;
  270. u8 resp[DESC_FMT_SENSE_DATA_SIZE];
  271. if (scsi_status_is_good(status)) {
  272. hdr->status = SAM_STAT_GOOD;
  273. hdr->masked_status = GOOD;
  274. hdr->host_status = DID_OK;
  275. hdr->driver_status = DRIVER_OK;
  276. hdr->sb_len_wr = 0;
  277. } else {
  278. hdr->status = status;
  279. hdr->masked_status = status >> 1;
  280. hdr->host_status = DID_OK;
  281. hdr->driver_status = DRIVER_OK;
  282. memset(resp, 0, DESC_FMT_SENSE_DATA_SIZE);
  283. resp[0] = DESC_FORMAT_SENSE_DATA;
  284. resp[1] = sense_key;
  285. resp[2] = asc;
  286. resp[3] = ascq;
  287. xfer_len = min_t(u8, hdr->mx_sb_len, DESC_FMT_SENSE_DATA_SIZE);
  288. hdr->sb_len_wr = xfer_len;
  289. if (copy_to_user(hdr->sbp, resp, xfer_len) > 0)
  290. return -EFAULT;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * Take a status code from a lowlevel routine, and if it was a positive NVMe
  296. * error code update the sense data based on it. In either case the passed
  297. * in value is returned again, unless an -EFAULT from copy_to_user overrides
  298. * it.
  299. */
  300. static int nvme_trans_status_code(struct sg_io_hdr *hdr, int nvme_sc)
  301. {
  302. u8 status, sense_key, asc, ascq;
  303. int res;
  304. /* For non-nvme (Linux) errors, simply return the error code */
  305. if (nvme_sc < 0)
  306. return nvme_sc;
  307. /* Mask DNR, More, and reserved fields */
  308. switch (nvme_sc & 0x7FF) {
  309. /* Generic Command Status */
  310. case NVME_SC_SUCCESS:
  311. status = SAM_STAT_GOOD;
  312. sense_key = NO_SENSE;
  313. asc = SCSI_ASC_NO_SENSE;
  314. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  315. break;
  316. case NVME_SC_INVALID_OPCODE:
  317. status = SAM_STAT_CHECK_CONDITION;
  318. sense_key = ILLEGAL_REQUEST;
  319. asc = SCSI_ASC_ILLEGAL_COMMAND;
  320. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  321. break;
  322. case NVME_SC_INVALID_FIELD:
  323. status = SAM_STAT_CHECK_CONDITION;
  324. sense_key = ILLEGAL_REQUEST;
  325. asc = SCSI_ASC_INVALID_CDB;
  326. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  327. break;
  328. case NVME_SC_DATA_XFER_ERROR:
  329. status = SAM_STAT_CHECK_CONDITION;
  330. sense_key = MEDIUM_ERROR;
  331. asc = SCSI_ASC_NO_SENSE;
  332. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  333. break;
  334. case NVME_SC_POWER_LOSS:
  335. status = SAM_STAT_TASK_ABORTED;
  336. sense_key = ABORTED_COMMAND;
  337. asc = SCSI_ASC_WARNING;
  338. ascq = SCSI_ASCQ_POWER_LOSS_EXPECTED;
  339. break;
  340. case NVME_SC_INTERNAL:
  341. status = SAM_STAT_CHECK_CONDITION;
  342. sense_key = HARDWARE_ERROR;
  343. asc = SCSI_ASC_INTERNAL_TARGET_FAILURE;
  344. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  345. break;
  346. case NVME_SC_ABORT_REQ:
  347. status = SAM_STAT_TASK_ABORTED;
  348. sense_key = ABORTED_COMMAND;
  349. asc = SCSI_ASC_NO_SENSE;
  350. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  351. break;
  352. case NVME_SC_ABORT_QUEUE:
  353. status = SAM_STAT_TASK_ABORTED;
  354. sense_key = ABORTED_COMMAND;
  355. asc = SCSI_ASC_NO_SENSE;
  356. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  357. break;
  358. case NVME_SC_FUSED_FAIL:
  359. status = SAM_STAT_TASK_ABORTED;
  360. sense_key = ABORTED_COMMAND;
  361. asc = SCSI_ASC_NO_SENSE;
  362. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  363. break;
  364. case NVME_SC_FUSED_MISSING:
  365. status = SAM_STAT_TASK_ABORTED;
  366. sense_key = ABORTED_COMMAND;
  367. asc = SCSI_ASC_NO_SENSE;
  368. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  369. break;
  370. case NVME_SC_INVALID_NS:
  371. status = SAM_STAT_CHECK_CONDITION;
  372. sense_key = ILLEGAL_REQUEST;
  373. asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
  374. ascq = SCSI_ASCQ_INVALID_LUN_ID;
  375. break;
  376. case NVME_SC_LBA_RANGE:
  377. status = SAM_STAT_CHECK_CONDITION;
  378. sense_key = ILLEGAL_REQUEST;
  379. asc = SCSI_ASC_ILLEGAL_BLOCK;
  380. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  381. break;
  382. case NVME_SC_CAP_EXCEEDED:
  383. status = SAM_STAT_CHECK_CONDITION;
  384. sense_key = MEDIUM_ERROR;
  385. asc = SCSI_ASC_NO_SENSE;
  386. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  387. break;
  388. case NVME_SC_NS_NOT_READY:
  389. status = SAM_STAT_CHECK_CONDITION;
  390. sense_key = NOT_READY;
  391. asc = SCSI_ASC_LUN_NOT_READY;
  392. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  393. break;
  394. /* Command Specific Status */
  395. case NVME_SC_INVALID_FORMAT:
  396. status = SAM_STAT_CHECK_CONDITION;
  397. sense_key = ILLEGAL_REQUEST;
  398. asc = SCSI_ASC_FORMAT_COMMAND_FAILED;
  399. ascq = SCSI_ASCQ_FORMAT_COMMAND_FAILED;
  400. break;
  401. case NVME_SC_BAD_ATTRIBUTES:
  402. status = SAM_STAT_CHECK_CONDITION;
  403. sense_key = ILLEGAL_REQUEST;
  404. asc = SCSI_ASC_INVALID_CDB;
  405. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  406. break;
  407. /* Media Errors */
  408. case NVME_SC_WRITE_FAULT:
  409. status = SAM_STAT_CHECK_CONDITION;
  410. sense_key = MEDIUM_ERROR;
  411. asc = SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT;
  412. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  413. break;
  414. case NVME_SC_READ_ERROR:
  415. status = SAM_STAT_CHECK_CONDITION;
  416. sense_key = MEDIUM_ERROR;
  417. asc = SCSI_ASC_UNRECOVERED_READ_ERROR;
  418. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  419. break;
  420. case NVME_SC_GUARD_CHECK:
  421. status = SAM_STAT_CHECK_CONDITION;
  422. sense_key = MEDIUM_ERROR;
  423. asc = SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED;
  424. ascq = SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED;
  425. break;
  426. case NVME_SC_APPTAG_CHECK:
  427. status = SAM_STAT_CHECK_CONDITION;
  428. sense_key = MEDIUM_ERROR;
  429. asc = SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED;
  430. ascq = SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED;
  431. break;
  432. case NVME_SC_REFTAG_CHECK:
  433. status = SAM_STAT_CHECK_CONDITION;
  434. sense_key = MEDIUM_ERROR;
  435. asc = SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED;
  436. ascq = SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED;
  437. break;
  438. case NVME_SC_COMPARE_FAILED:
  439. status = SAM_STAT_CHECK_CONDITION;
  440. sense_key = MISCOMPARE;
  441. asc = SCSI_ASC_MISCOMPARE_DURING_VERIFY;
  442. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  443. break;
  444. case NVME_SC_ACCESS_DENIED:
  445. status = SAM_STAT_CHECK_CONDITION;
  446. sense_key = ILLEGAL_REQUEST;
  447. asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID;
  448. ascq = SCSI_ASCQ_INVALID_LUN_ID;
  449. break;
  450. /* Unspecified/Default */
  451. case NVME_SC_CMDID_CONFLICT:
  452. case NVME_SC_CMD_SEQ_ERROR:
  453. case NVME_SC_CQ_INVALID:
  454. case NVME_SC_QID_INVALID:
  455. case NVME_SC_QUEUE_SIZE:
  456. case NVME_SC_ABORT_LIMIT:
  457. case NVME_SC_ABORT_MISSING:
  458. case NVME_SC_ASYNC_LIMIT:
  459. case NVME_SC_FIRMWARE_SLOT:
  460. case NVME_SC_FIRMWARE_IMAGE:
  461. case NVME_SC_INVALID_VECTOR:
  462. case NVME_SC_INVALID_LOG_PAGE:
  463. default:
  464. status = SAM_STAT_CHECK_CONDITION;
  465. sense_key = ILLEGAL_REQUEST;
  466. asc = SCSI_ASC_NO_SENSE;
  467. ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  468. break;
  469. }
  470. res = nvme_trans_completion(hdr, status, sense_key, asc, ascq);
  471. return res ? res : nvme_sc;
  472. }
  473. /* INQUIRY Helper Functions */
  474. static int nvme_trans_standard_inquiry_page(struct nvme_ns *ns,
  475. struct sg_io_hdr *hdr, u8 *inq_response,
  476. int alloc_len)
  477. {
  478. struct nvme_ctrl *ctrl = ns->ctrl;
  479. struct nvme_id_ns *id_ns;
  480. int res;
  481. int nvme_sc;
  482. int xfer_len;
  483. u8 resp_data_format = 0x02;
  484. u8 protect;
  485. u8 cmdque = 0x01 << 1;
  486. u8 fw_offset = sizeof(ctrl->firmware_rev);
  487. /* nvme ns identify - use DPS value for PROTECT field */
  488. nvme_sc = nvme_identify_ns(ctrl, ns->ns_id, &id_ns);
  489. res = nvme_trans_status_code(hdr, nvme_sc);
  490. if (res)
  491. return res;
  492. if (id_ns->dps)
  493. protect = 0x01;
  494. else
  495. protect = 0;
  496. kfree(id_ns);
  497. memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
  498. inq_response[2] = VERSION_SPC_4;
  499. inq_response[3] = resp_data_format; /*normaca=0 | hisup=0 */
  500. inq_response[4] = ADDITIONAL_STD_INQ_LENGTH;
  501. inq_response[5] = protect; /* sccs=0 | acc=0 | tpgs=0 | pc3=0 */
  502. inq_response[7] = cmdque; /* wbus16=0 | sync=0 | vs=0 */
  503. strncpy(&inq_response[8], "NVMe ", 8);
  504. strncpy(&inq_response[16], ctrl->model, 16);
  505. while (ctrl->firmware_rev[fw_offset - 1] == ' ' && fw_offset > 4)
  506. fw_offset--;
  507. fw_offset -= 4;
  508. strncpy(&inq_response[32], ctrl->firmware_rev + fw_offset, 4);
  509. xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
  510. return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
  511. }
  512. static int nvme_trans_supported_vpd_pages(struct nvme_ns *ns,
  513. struct sg_io_hdr *hdr, u8 *inq_response,
  514. int alloc_len)
  515. {
  516. int xfer_len;
  517. memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
  518. inq_response[1] = INQ_SUPPORTED_VPD_PAGES_PAGE; /* Page Code */
  519. inq_response[3] = INQ_NUM_SUPPORTED_VPD_PAGES; /* Page Length */
  520. inq_response[4] = INQ_SUPPORTED_VPD_PAGES_PAGE;
  521. inq_response[5] = INQ_UNIT_SERIAL_NUMBER_PAGE;
  522. inq_response[6] = INQ_DEVICE_IDENTIFICATION_PAGE;
  523. inq_response[7] = INQ_EXTENDED_INQUIRY_DATA_PAGE;
  524. inq_response[8] = INQ_BDEV_CHARACTERISTICS_PAGE;
  525. inq_response[9] = INQ_BDEV_LIMITS_PAGE;
  526. xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
  527. return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
  528. }
  529. static int nvme_trans_unit_serial_page(struct nvme_ns *ns,
  530. struct sg_io_hdr *hdr, u8 *inq_response,
  531. int alloc_len)
  532. {
  533. int xfer_len;
  534. memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
  535. inq_response[1] = INQ_UNIT_SERIAL_NUMBER_PAGE; /* Page Code */
  536. inq_response[3] = INQ_SERIAL_NUMBER_LENGTH; /* Page Length */
  537. strncpy(&inq_response[4], ns->ctrl->serial, INQ_SERIAL_NUMBER_LENGTH);
  538. xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH);
  539. return nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
  540. }
  541. static int nvme_fill_device_id_eui64(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  542. u8 *inq_response, int alloc_len)
  543. {
  544. struct nvme_id_ns *id_ns;
  545. int nvme_sc, res;
  546. size_t len;
  547. void *eui;
  548. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  549. res = nvme_trans_status_code(hdr, nvme_sc);
  550. if (res)
  551. return res;
  552. eui = id_ns->eui64;
  553. len = sizeof(id_ns->eui64);
  554. if (ns->ctrl->vs >= NVME_VS(1, 2)) {
  555. if (bitmap_empty(eui, len * 8)) {
  556. eui = id_ns->nguid;
  557. len = sizeof(id_ns->nguid);
  558. }
  559. }
  560. if (bitmap_empty(eui, len * 8)) {
  561. res = -EOPNOTSUPP;
  562. goto out_free_id;
  563. }
  564. memset(inq_response, 0, alloc_len);
  565. inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE;
  566. inq_response[3] = 4 + len; /* Page Length */
  567. /* Designation Descriptor start */
  568. inq_response[4] = 0x01; /* Proto ID=0h | Code set=1h */
  569. inq_response[5] = 0x02; /* PIV=0b | Asso=00b | Designator Type=2h */
  570. inq_response[6] = 0x00; /* Rsvd */
  571. inq_response[7] = len; /* Designator Length */
  572. memcpy(&inq_response[8], eui, len);
  573. res = nvme_trans_copy_to_user(hdr, inq_response, alloc_len);
  574. out_free_id:
  575. kfree(id_ns);
  576. return res;
  577. }
  578. static int nvme_fill_device_id_scsi_string(struct nvme_ns *ns,
  579. struct sg_io_hdr *hdr, u8 *inq_response, int alloc_len)
  580. {
  581. struct nvme_ctrl *ctrl = ns->ctrl;
  582. struct nvme_id_ctrl *id_ctrl;
  583. int nvme_sc, res;
  584. if (alloc_len < 72) {
  585. return nvme_trans_completion(hdr,
  586. SAM_STAT_CHECK_CONDITION,
  587. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  588. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  589. }
  590. nvme_sc = nvme_identify_ctrl(ctrl, &id_ctrl);
  591. res = nvme_trans_status_code(hdr, nvme_sc);
  592. if (res)
  593. return res;
  594. memset(inq_response, 0, alloc_len);
  595. inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE;
  596. inq_response[3] = 0x48; /* Page Length */
  597. /* Designation Descriptor start */
  598. inq_response[4] = 0x03; /* Proto ID=0h | Code set=3h */
  599. inq_response[5] = 0x08; /* PIV=0b | Asso=00b | Designator Type=8h */
  600. inq_response[6] = 0x00; /* Rsvd */
  601. inq_response[7] = 0x44; /* Designator Length */
  602. sprintf(&inq_response[8], "%04x", le16_to_cpu(id_ctrl->vid));
  603. memcpy(&inq_response[12], ctrl->model, sizeof(ctrl->model));
  604. sprintf(&inq_response[52], "%04x", cpu_to_be32(ns->ns_id));
  605. memcpy(&inq_response[56], ctrl->serial, sizeof(ctrl->serial));
  606. res = nvme_trans_copy_to_user(hdr, inq_response, alloc_len);
  607. kfree(id_ctrl);
  608. return res;
  609. }
  610. static int nvme_trans_device_id_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  611. u8 *resp, int alloc_len)
  612. {
  613. int res;
  614. if (ns->ctrl->vs >= NVME_VS(1, 1)) {
  615. res = nvme_fill_device_id_eui64(ns, hdr, resp, alloc_len);
  616. if (res != -EOPNOTSUPP)
  617. return res;
  618. }
  619. return nvme_fill_device_id_scsi_string(ns, hdr, resp, alloc_len);
  620. }
  621. static int nvme_trans_ext_inq_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  622. int alloc_len)
  623. {
  624. u8 *inq_response;
  625. int res;
  626. int nvme_sc;
  627. struct nvme_ctrl *ctrl = ns->ctrl;
  628. struct nvme_id_ctrl *id_ctrl;
  629. struct nvme_id_ns *id_ns;
  630. int xfer_len;
  631. u8 microcode = 0x80;
  632. u8 spt;
  633. u8 spt_lut[8] = {0, 0, 2, 1, 4, 6, 5, 7};
  634. u8 grd_chk, app_chk, ref_chk, protect;
  635. u8 uask_sup = 0x20;
  636. u8 v_sup;
  637. u8 luiclr = 0x01;
  638. inq_response = kmalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL);
  639. if (inq_response == NULL)
  640. return -ENOMEM;
  641. nvme_sc = nvme_identify_ns(ctrl, ns->ns_id, &id_ns);
  642. res = nvme_trans_status_code(hdr, nvme_sc);
  643. if (res)
  644. goto out_free_inq;
  645. spt = spt_lut[id_ns->dpc & 0x07] << 3;
  646. if (id_ns->dps)
  647. protect = 0x01;
  648. else
  649. protect = 0;
  650. kfree(id_ns);
  651. grd_chk = protect << 2;
  652. app_chk = protect << 1;
  653. ref_chk = protect;
  654. nvme_sc = nvme_identify_ctrl(ctrl, &id_ctrl);
  655. res = nvme_trans_status_code(hdr, nvme_sc);
  656. if (res)
  657. goto out_free_inq;
  658. v_sup = id_ctrl->vwc;
  659. kfree(id_ctrl);
  660. memset(inq_response, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
  661. inq_response[1] = INQ_EXTENDED_INQUIRY_DATA_PAGE; /* Page Code */
  662. inq_response[2] = 0x00; /* Page Length MSB */
  663. inq_response[3] = 0x3C; /* Page Length LSB */
  664. inq_response[4] = microcode | spt | grd_chk | app_chk | ref_chk;
  665. inq_response[5] = uask_sup;
  666. inq_response[6] = v_sup;
  667. inq_response[7] = luiclr;
  668. inq_response[8] = 0;
  669. inq_response[9] = 0;
  670. xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
  671. res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
  672. out_free_inq:
  673. kfree(inq_response);
  674. return res;
  675. }
  676. static int nvme_trans_bdev_limits_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  677. u8 *inq_response, int alloc_len)
  678. {
  679. __be32 max_sectors = cpu_to_be32(
  680. nvme_block_nr(ns, queue_max_hw_sectors(ns->queue)));
  681. __be32 max_discard = cpu_to_be32(ns->queue->limits.max_discard_sectors);
  682. __be32 discard_desc_count = cpu_to_be32(0x100);
  683. memset(inq_response, 0, STANDARD_INQUIRY_LENGTH);
  684. inq_response[1] = VPD_BLOCK_LIMITS;
  685. inq_response[3] = 0x3c; /* Page Length */
  686. memcpy(&inq_response[8], &max_sectors, sizeof(u32));
  687. memcpy(&inq_response[20], &max_discard, sizeof(u32));
  688. if (max_discard)
  689. memcpy(&inq_response[24], &discard_desc_count, sizeof(u32));
  690. return nvme_trans_copy_to_user(hdr, inq_response, 0x3c);
  691. }
  692. static int nvme_trans_bdev_char_page(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  693. int alloc_len)
  694. {
  695. u8 *inq_response;
  696. int res;
  697. int xfer_len;
  698. inq_response = kzalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL);
  699. if (inq_response == NULL) {
  700. res = -ENOMEM;
  701. goto out_mem;
  702. }
  703. inq_response[1] = INQ_BDEV_CHARACTERISTICS_PAGE; /* Page Code */
  704. inq_response[2] = 0x00; /* Page Length MSB */
  705. inq_response[3] = 0x3C; /* Page Length LSB */
  706. inq_response[4] = 0x00; /* Medium Rotation Rate MSB */
  707. inq_response[5] = 0x01; /* Medium Rotation Rate LSB */
  708. inq_response[6] = 0x00; /* Form Factor */
  709. xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH);
  710. res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len);
  711. kfree(inq_response);
  712. out_mem:
  713. return res;
  714. }
  715. /* LOG SENSE Helper Functions */
  716. static int nvme_trans_log_supp_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  717. int alloc_len)
  718. {
  719. int res;
  720. int xfer_len;
  721. u8 *log_response;
  722. log_response = kzalloc(LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH, GFP_KERNEL);
  723. if (log_response == NULL) {
  724. res = -ENOMEM;
  725. goto out_mem;
  726. }
  727. log_response[0] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE;
  728. /* Subpage=0x00, Page Length MSB=0 */
  729. log_response[3] = SUPPORTED_LOG_PAGES_PAGE_LENGTH;
  730. log_response[4] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE;
  731. log_response[5] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE;
  732. log_response[6] = LOG_PAGE_TEMPERATURE_PAGE;
  733. xfer_len = min(alloc_len, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH);
  734. res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
  735. kfree(log_response);
  736. out_mem:
  737. return res;
  738. }
  739. static int nvme_trans_log_info_exceptions(struct nvme_ns *ns,
  740. struct sg_io_hdr *hdr, int alloc_len)
  741. {
  742. int res;
  743. int xfer_len;
  744. u8 *log_response;
  745. struct nvme_smart_log *smart_log;
  746. u8 temp_c;
  747. u16 temp_k;
  748. log_response = kzalloc(LOG_INFO_EXCP_PAGE_LENGTH, GFP_KERNEL);
  749. if (log_response == NULL)
  750. return -ENOMEM;
  751. res = nvme_get_log_page(ns->ctrl, &smart_log);
  752. if (res < 0)
  753. goto out_free_response;
  754. if (res != NVME_SC_SUCCESS) {
  755. temp_c = LOG_TEMP_UNKNOWN;
  756. } else {
  757. temp_k = (smart_log->temperature[1] << 8) +
  758. (smart_log->temperature[0]);
  759. temp_c = temp_k - KELVIN_TEMP_FACTOR;
  760. }
  761. kfree(smart_log);
  762. log_response[0] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE;
  763. /* Subpage=0x00, Page Length MSB=0 */
  764. log_response[3] = REMAINING_INFO_EXCP_PAGE_LENGTH;
  765. /* Informational Exceptions Log Parameter 1 Start */
  766. /* Parameter Code=0x0000 bytes 4,5 */
  767. log_response[6] = 0x23; /* DU=0, TSD=1, ETC=0, TMC=0, FMT_AND_LNK=11b */
  768. log_response[7] = 0x04; /* PARAMETER LENGTH */
  769. /* Add sense Code and qualifier = 0x00 each */
  770. /* Use Temperature from NVMe Get Log Page, convert to C from K */
  771. log_response[10] = temp_c;
  772. xfer_len = min(alloc_len, LOG_INFO_EXCP_PAGE_LENGTH);
  773. res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
  774. out_free_response:
  775. kfree(log_response);
  776. return res;
  777. }
  778. static int nvme_trans_log_temperature(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  779. int alloc_len)
  780. {
  781. int res;
  782. int xfer_len;
  783. u8 *log_response;
  784. struct nvme_smart_log *smart_log;
  785. u32 feature_resp;
  786. u8 temp_c_cur, temp_c_thresh;
  787. u16 temp_k;
  788. log_response = kzalloc(LOG_TEMP_PAGE_LENGTH, GFP_KERNEL);
  789. if (log_response == NULL)
  790. return -ENOMEM;
  791. res = nvme_get_log_page(ns->ctrl, &smart_log);
  792. if (res < 0)
  793. goto out_free_response;
  794. if (res != NVME_SC_SUCCESS) {
  795. temp_c_cur = LOG_TEMP_UNKNOWN;
  796. } else {
  797. temp_k = (smart_log->temperature[1] << 8) +
  798. (smart_log->temperature[0]);
  799. temp_c_cur = temp_k - KELVIN_TEMP_FACTOR;
  800. }
  801. kfree(smart_log);
  802. /* Get Features for Temp Threshold */
  803. res = nvme_get_features(ns->ctrl, NVME_FEAT_TEMP_THRESH, 0, 0,
  804. &feature_resp);
  805. if (res != NVME_SC_SUCCESS)
  806. temp_c_thresh = LOG_TEMP_UNKNOWN;
  807. else
  808. temp_c_thresh = (feature_resp & 0xFFFF) - KELVIN_TEMP_FACTOR;
  809. log_response[0] = LOG_PAGE_TEMPERATURE_PAGE;
  810. /* Subpage=0x00, Page Length MSB=0 */
  811. log_response[3] = REMAINING_TEMP_PAGE_LENGTH;
  812. /* Temperature Log Parameter 1 (Temperature) Start */
  813. /* Parameter Code = 0x0000 */
  814. log_response[6] = 0x01; /* Format and Linking = 01b */
  815. log_response[7] = 0x02; /* Parameter Length */
  816. /* Use Temperature from NVMe Get Log Page, convert to C from K */
  817. log_response[9] = temp_c_cur;
  818. /* Temperature Log Parameter 2 (Reference Temperature) Start */
  819. log_response[11] = 0x01; /* Parameter Code = 0x0001 */
  820. log_response[12] = 0x01; /* Format and Linking = 01b */
  821. log_response[13] = 0x02; /* Parameter Length */
  822. /* Use Temperature Thresh from NVMe Get Log Page, convert to C from K */
  823. log_response[15] = temp_c_thresh;
  824. xfer_len = min(alloc_len, LOG_TEMP_PAGE_LENGTH);
  825. res = nvme_trans_copy_to_user(hdr, log_response, xfer_len);
  826. out_free_response:
  827. kfree(log_response);
  828. return res;
  829. }
  830. /* MODE SENSE Helper Functions */
  831. static int nvme_trans_fill_mode_parm_hdr(u8 *resp, int len, u8 cdb10, u8 llbaa,
  832. u16 mode_data_length, u16 blk_desc_len)
  833. {
  834. /* Quick check to make sure I don't stomp on my own memory... */
  835. if ((cdb10 && len < 8) || (!cdb10 && len < 4))
  836. return -EINVAL;
  837. if (cdb10) {
  838. resp[0] = (mode_data_length & 0xFF00) >> 8;
  839. resp[1] = (mode_data_length & 0x00FF);
  840. resp[3] = 0x10 /* DPOFUA */;
  841. resp[4] = llbaa;
  842. resp[5] = RESERVED_FIELD;
  843. resp[6] = (blk_desc_len & 0xFF00) >> 8;
  844. resp[7] = (blk_desc_len & 0x00FF);
  845. } else {
  846. resp[0] = (mode_data_length & 0x00FF);
  847. resp[2] = 0x10 /* DPOFUA */;
  848. resp[3] = (blk_desc_len & 0x00FF);
  849. }
  850. return 0;
  851. }
  852. static int nvme_trans_fill_blk_desc(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  853. u8 *resp, int len, u8 llbaa)
  854. {
  855. int res;
  856. int nvme_sc;
  857. struct nvme_id_ns *id_ns;
  858. u8 flbas;
  859. u32 lba_length;
  860. if (llbaa == 0 && len < MODE_PAGE_BLK_DES_LEN)
  861. return -EINVAL;
  862. else if (llbaa > 0 && len < MODE_PAGE_LLBAA_BLK_DES_LEN)
  863. return -EINVAL;
  864. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  865. res = nvme_trans_status_code(hdr, nvme_sc);
  866. if (res)
  867. return res;
  868. flbas = (id_ns->flbas) & 0x0F;
  869. lba_length = (1 << (id_ns->lbaf[flbas].ds));
  870. if (llbaa == 0) {
  871. __be32 tmp_cap = cpu_to_be32(le64_to_cpu(id_ns->ncap));
  872. /* Byte 4 is reserved */
  873. __be32 tmp_len = cpu_to_be32(lba_length & 0x00FFFFFF);
  874. memcpy(resp, &tmp_cap, sizeof(u32));
  875. memcpy(&resp[4], &tmp_len, sizeof(u32));
  876. } else {
  877. __be64 tmp_cap = cpu_to_be64(le64_to_cpu(id_ns->ncap));
  878. __be32 tmp_len = cpu_to_be32(lba_length);
  879. memcpy(resp, &tmp_cap, sizeof(u64));
  880. /* Bytes 8, 9, 10, 11 are reserved */
  881. memcpy(&resp[12], &tmp_len, sizeof(u32));
  882. }
  883. kfree(id_ns);
  884. return res;
  885. }
  886. static int nvme_trans_fill_control_page(struct nvme_ns *ns,
  887. struct sg_io_hdr *hdr, u8 *resp,
  888. int len)
  889. {
  890. if (len < MODE_PAGE_CONTROL_LEN)
  891. return -EINVAL;
  892. resp[0] = MODE_PAGE_CONTROL;
  893. resp[1] = MODE_PAGE_CONTROL_LEN_FIELD;
  894. resp[2] = 0x0E; /* TST=000b, TMF_ONLY=0, DPICZ=1,
  895. * D_SENSE=1, GLTSD=1, RLEC=0 */
  896. resp[3] = 0x12; /* Q_ALGO_MODIFIER=1h, NUAR=0, QERR=01b */
  897. /* Byte 4: VS=0, RAC=0, UA_INT=0, SWP=0 */
  898. resp[5] = 0x40; /* ATO=0, TAS=1, ATMPE=0, RWWP=0, AUTOLOAD=0 */
  899. /* resp[6] and [7] are obsolete, thus zero */
  900. resp[8] = 0xFF; /* Busy timeout period = 0xffff */
  901. resp[9] = 0xFF;
  902. /* Bytes 10,11: Extended selftest completion time = 0x0000 */
  903. return 0;
  904. }
  905. static int nvme_trans_fill_caching_page(struct nvme_ns *ns,
  906. struct sg_io_hdr *hdr,
  907. u8 *resp, int len)
  908. {
  909. int res = 0;
  910. int nvme_sc;
  911. u32 feature_resp;
  912. u8 vwc;
  913. if (len < MODE_PAGE_CACHING_LEN)
  914. return -EINVAL;
  915. nvme_sc = nvme_get_features(ns->ctrl, NVME_FEAT_VOLATILE_WC, 0, 0,
  916. &feature_resp);
  917. res = nvme_trans_status_code(hdr, nvme_sc);
  918. if (res)
  919. return res;
  920. vwc = feature_resp & 0x00000001;
  921. resp[0] = MODE_PAGE_CACHING;
  922. resp[1] = MODE_PAGE_CACHING_LEN_FIELD;
  923. resp[2] = vwc << 2;
  924. return 0;
  925. }
  926. static int nvme_trans_fill_pow_cnd_page(struct nvme_ns *ns,
  927. struct sg_io_hdr *hdr, u8 *resp,
  928. int len)
  929. {
  930. if (len < MODE_PAGE_POW_CND_LEN)
  931. return -EINVAL;
  932. resp[0] = MODE_PAGE_POWER_CONDITION;
  933. resp[1] = MODE_PAGE_POW_CND_LEN_FIELD;
  934. /* All other bytes are zero */
  935. return 0;
  936. }
  937. static int nvme_trans_fill_inf_exc_page(struct nvme_ns *ns,
  938. struct sg_io_hdr *hdr, u8 *resp,
  939. int len)
  940. {
  941. if (len < MODE_PAGE_INF_EXC_LEN)
  942. return -EINVAL;
  943. resp[0] = MODE_PAGE_INFO_EXCEP;
  944. resp[1] = MODE_PAGE_INF_EXC_LEN_FIELD;
  945. resp[2] = 0x88;
  946. /* All other bytes are zero */
  947. return 0;
  948. }
  949. static int nvme_trans_fill_all_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  950. u8 *resp, int len)
  951. {
  952. int res;
  953. u16 mode_pages_offset_1 = 0;
  954. u16 mode_pages_offset_2, mode_pages_offset_3, mode_pages_offset_4;
  955. mode_pages_offset_2 = mode_pages_offset_1 + MODE_PAGE_CACHING_LEN;
  956. mode_pages_offset_3 = mode_pages_offset_2 + MODE_PAGE_CONTROL_LEN;
  957. mode_pages_offset_4 = mode_pages_offset_3 + MODE_PAGE_POW_CND_LEN;
  958. res = nvme_trans_fill_caching_page(ns, hdr, &resp[mode_pages_offset_1],
  959. MODE_PAGE_CACHING_LEN);
  960. if (res)
  961. return res;
  962. res = nvme_trans_fill_control_page(ns, hdr, &resp[mode_pages_offset_2],
  963. MODE_PAGE_CONTROL_LEN);
  964. if (res)
  965. return res;
  966. res = nvme_trans_fill_pow_cnd_page(ns, hdr, &resp[mode_pages_offset_3],
  967. MODE_PAGE_POW_CND_LEN);
  968. if (res)
  969. return res;
  970. return nvme_trans_fill_inf_exc_page(ns, hdr, &resp[mode_pages_offset_4],
  971. MODE_PAGE_INF_EXC_LEN);
  972. }
  973. static inline int nvme_trans_get_blk_desc_len(u8 dbd, u8 llbaa)
  974. {
  975. if (dbd == MODE_SENSE_BLK_DESC_ENABLED) {
  976. /* SPC-4: len = 8 x Num_of_descriptors if llbaa = 0, 16x if 1 */
  977. return 8 * (llbaa + 1) * MODE_SENSE_BLK_DESC_COUNT;
  978. } else {
  979. return 0;
  980. }
  981. }
  982. static int nvme_trans_mode_page_create(struct nvme_ns *ns,
  983. struct sg_io_hdr *hdr, u8 *cmd,
  984. u16 alloc_len, u8 cdb10,
  985. int (*mode_page_fill_func)
  986. (struct nvme_ns *,
  987. struct sg_io_hdr *hdr, u8 *, int),
  988. u16 mode_pages_tot_len)
  989. {
  990. int res;
  991. int xfer_len;
  992. u8 *response;
  993. u8 dbd, llbaa;
  994. u16 resp_size;
  995. int mph_size;
  996. u16 mode_pages_offset_1;
  997. u16 blk_desc_len, blk_desc_offset, mode_data_length;
  998. dbd = (cmd[1] & MODE_SENSE_DBD_MASK) >> MODE_SENSE_DBD_SHIFT;
  999. llbaa = (cmd[1] & MODE_SENSE_LLBAA_MASK) >> MODE_SENSE_LLBAA_SHIFT;
  1000. mph_size = cdb10 ? MODE_SENSE10_MPH_SIZE : MODE_SENSE6_MPH_SIZE;
  1001. blk_desc_len = nvme_trans_get_blk_desc_len(dbd, llbaa);
  1002. resp_size = mph_size + blk_desc_len + mode_pages_tot_len;
  1003. /* Refer spc4r34 Table 440 for calculation of Mode data Length field */
  1004. mode_data_length = 3 + (3 * cdb10) + blk_desc_len + mode_pages_tot_len;
  1005. blk_desc_offset = mph_size;
  1006. mode_pages_offset_1 = blk_desc_offset + blk_desc_len;
  1007. response = kzalloc(resp_size, GFP_KERNEL);
  1008. if (response == NULL) {
  1009. res = -ENOMEM;
  1010. goto out_mem;
  1011. }
  1012. res = nvme_trans_fill_mode_parm_hdr(&response[0], mph_size, cdb10,
  1013. llbaa, mode_data_length, blk_desc_len);
  1014. if (res)
  1015. goto out_free;
  1016. if (blk_desc_len > 0) {
  1017. res = nvme_trans_fill_blk_desc(ns, hdr,
  1018. &response[blk_desc_offset],
  1019. blk_desc_len, llbaa);
  1020. if (res)
  1021. goto out_free;
  1022. }
  1023. res = mode_page_fill_func(ns, hdr, &response[mode_pages_offset_1],
  1024. mode_pages_tot_len);
  1025. if (res)
  1026. goto out_free;
  1027. xfer_len = min(alloc_len, resp_size);
  1028. res = nvme_trans_copy_to_user(hdr, response, xfer_len);
  1029. out_free:
  1030. kfree(response);
  1031. out_mem:
  1032. return res;
  1033. }
  1034. /* Read Capacity Helper Functions */
  1035. static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns,
  1036. u8 cdb16)
  1037. {
  1038. u8 flbas;
  1039. u32 lba_length;
  1040. u64 rlba;
  1041. u8 prot_en;
  1042. u8 p_type_lut[4] = {0, 0, 1, 2};
  1043. __be64 tmp_rlba;
  1044. __be32 tmp_rlba_32;
  1045. __be32 tmp_len;
  1046. flbas = (id_ns->flbas) & 0x0F;
  1047. lba_length = (1 << (id_ns->lbaf[flbas].ds));
  1048. rlba = le64_to_cpup(&id_ns->nsze) - 1;
  1049. (id_ns->dps) ? (prot_en = 0x01) : (prot_en = 0);
  1050. if (!cdb16) {
  1051. if (rlba > 0xFFFFFFFF)
  1052. rlba = 0xFFFFFFFF;
  1053. tmp_rlba_32 = cpu_to_be32(rlba);
  1054. tmp_len = cpu_to_be32(lba_length);
  1055. memcpy(response, &tmp_rlba_32, sizeof(u32));
  1056. memcpy(&response[4], &tmp_len, sizeof(u32));
  1057. } else {
  1058. tmp_rlba = cpu_to_be64(rlba);
  1059. tmp_len = cpu_to_be32(lba_length);
  1060. memcpy(response, &tmp_rlba, sizeof(u64));
  1061. memcpy(&response[8], &tmp_len, sizeof(u32));
  1062. response[12] = (p_type_lut[id_ns->dps & 0x3] << 1) | prot_en;
  1063. /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */
  1064. /* LBPME = 0 | LBPRZ = 0 | LALBA = 0x00 */
  1065. /* Bytes 16-31 - Reserved */
  1066. }
  1067. }
  1068. /* Start Stop Unit Helper Functions */
  1069. static int nvme_trans_power_state(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1070. u8 pc, u8 pcmod, u8 start)
  1071. {
  1072. int res;
  1073. int nvme_sc;
  1074. struct nvme_id_ctrl *id_ctrl;
  1075. int lowest_pow_st; /* max npss = lowest power consumption */
  1076. unsigned ps_desired = 0;
  1077. nvme_sc = nvme_identify_ctrl(ns->ctrl, &id_ctrl);
  1078. res = nvme_trans_status_code(hdr, nvme_sc);
  1079. if (res)
  1080. return res;
  1081. lowest_pow_st = max(POWER_STATE_0, (int)(id_ctrl->npss - 1));
  1082. kfree(id_ctrl);
  1083. switch (pc) {
  1084. case NVME_POWER_STATE_START_VALID:
  1085. /* Action unspecified if POWER CONDITION MODIFIER != 0 */
  1086. if (pcmod == 0 && start == 0x1)
  1087. ps_desired = POWER_STATE_0;
  1088. if (pcmod == 0 && start == 0x0)
  1089. ps_desired = lowest_pow_st;
  1090. break;
  1091. case NVME_POWER_STATE_ACTIVE:
  1092. /* Action unspecified if POWER CONDITION MODIFIER != 0 */
  1093. if (pcmod == 0)
  1094. ps_desired = POWER_STATE_0;
  1095. break;
  1096. case NVME_POWER_STATE_IDLE:
  1097. /* Action unspecified if POWER CONDITION MODIFIER != [0,1,2] */
  1098. if (pcmod == 0x0)
  1099. ps_desired = POWER_STATE_1;
  1100. else if (pcmod == 0x1)
  1101. ps_desired = POWER_STATE_2;
  1102. else if (pcmod == 0x2)
  1103. ps_desired = POWER_STATE_3;
  1104. break;
  1105. case NVME_POWER_STATE_STANDBY:
  1106. /* Action unspecified if POWER CONDITION MODIFIER != [0,1] */
  1107. if (pcmod == 0x0)
  1108. ps_desired = max(POWER_STATE_0, (lowest_pow_st - 2));
  1109. else if (pcmod == 0x1)
  1110. ps_desired = max(POWER_STATE_0, (lowest_pow_st - 1));
  1111. break;
  1112. case NVME_POWER_STATE_LU_CONTROL:
  1113. default:
  1114. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1115. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1116. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1117. break;
  1118. }
  1119. nvme_sc = nvme_set_features(ns->ctrl, NVME_FEAT_POWER_MGMT, ps_desired, 0,
  1120. NULL);
  1121. return nvme_trans_status_code(hdr, nvme_sc);
  1122. }
  1123. static int nvme_trans_send_activate_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1124. u8 buffer_id)
  1125. {
  1126. struct nvme_command c;
  1127. int nvme_sc;
  1128. memset(&c, 0, sizeof(c));
  1129. c.common.opcode = nvme_admin_activate_fw;
  1130. c.common.cdw10[0] = cpu_to_le32(buffer_id | NVME_FWACT_REPL_ACTV);
  1131. nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0);
  1132. return nvme_trans_status_code(hdr, nvme_sc);
  1133. }
  1134. static int nvme_trans_send_download_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1135. u8 opcode, u32 tot_len, u32 offset,
  1136. u8 buffer_id)
  1137. {
  1138. int nvme_sc;
  1139. struct nvme_command c;
  1140. if (hdr->iovec_count > 0) {
  1141. /* Assuming SGL is not allowed for this command */
  1142. return nvme_trans_completion(hdr,
  1143. SAM_STAT_CHECK_CONDITION,
  1144. ILLEGAL_REQUEST,
  1145. SCSI_ASC_INVALID_CDB,
  1146. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1147. }
  1148. memset(&c, 0, sizeof(c));
  1149. c.common.opcode = nvme_admin_download_fw;
  1150. c.dlfw.numd = cpu_to_le32((tot_len/BYTES_TO_DWORDS) - 1);
  1151. c.dlfw.offset = cpu_to_le32(offset/BYTES_TO_DWORDS);
  1152. nvme_sc = nvme_submit_user_cmd(ns->ctrl->admin_q, &c,
  1153. hdr->dxferp, tot_len, NULL, 0);
  1154. return nvme_trans_status_code(hdr, nvme_sc);
  1155. }
  1156. /* Mode Select Helper Functions */
  1157. static inline void nvme_trans_modesel_get_bd_len(u8 *parm_list, u8 cdb10,
  1158. u16 *bd_len, u8 *llbaa)
  1159. {
  1160. if (cdb10) {
  1161. /* 10 Byte CDB */
  1162. *bd_len = (parm_list[MODE_SELECT_10_BD_OFFSET] << 8) +
  1163. parm_list[MODE_SELECT_10_BD_OFFSET + 1];
  1164. *llbaa = parm_list[MODE_SELECT_10_LLBAA_OFFSET] &
  1165. MODE_SELECT_10_LLBAA_MASK;
  1166. } else {
  1167. /* 6 Byte CDB */
  1168. *bd_len = parm_list[MODE_SELECT_6_BD_OFFSET];
  1169. }
  1170. }
  1171. static void nvme_trans_modesel_save_bd(struct nvme_ns *ns, u8 *parm_list,
  1172. u16 idx, u16 bd_len, u8 llbaa)
  1173. {
  1174. u16 bd_num;
  1175. bd_num = bd_len / ((llbaa == 0) ?
  1176. SHORT_DESC_BLOCK : LONG_DESC_BLOCK);
  1177. /* Store block descriptor info if a FORMAT UNIT comes later */
  1178. /* TODO Saving 1st BD info; what to do if multiple BD received? */
  1179. if (llbaa == 0) {
  1180. /* Standard Block Descriptor - spc4r34 7.5.5.1 */
  1181. ns->mode_select_num_blocks =
  1182. (parm_list[idx + 1] << 16) +
  1183. (parm_list[idx + 2] << 8) +
  1184. (parm_list[idx + 3]);
  1185. ns->mode_select_block_len =
  1186. (parm_list[idx + 5] << 16) +
  1187. (parm_list[idx + 6] << 8) +
  1188. (parm_list[idx + 7]);
  1189. } else {
  1190. /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */
  1191. ns->mode_select_num_blocks =
  1192. (((u64)parm_list[idx + 0]) << 56) +
  1193. (((u64)parm_list[idx + 1]) << 48) +
  1194. (((u64)parm_list[idx + 2]) << 40) +
  1195. (((u64)parm_list[idx + 3]) << 32) +
  1196. (((u64)parm_list[idx + 4]) << 24) +
  1197. (((u64)parm_list[idx + 5]) << 16) +
  1198. (((u64)parm_list[idx + 6]) << 8) +
  1199. ((u64)parm_list[idx + 7]);
  1200. ns->mode_select_block_len =
  1201. (parm_list[idx + 12] << 24) +
  1202. (parm_list[idx + 13] << 16) +
  1203. (parm_list[idx + 14] << 8) +
  1204. (parm_list[idx + 15]);
  1205. }
  1206. }
  1207. static int nvme_trans_modesel_get_mp(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1208. u8 *mode_page, u8 page_code)
  1209. {
  1210. int res = 0;
  1211. int nvme_sc;
  1212. unsigned dword11;
  1213. switch (page_code) {
  1214. case MODE_PAGE_CACHING:
  1215. dword11 = ((mode_page[2] & CACHING_MODE_PAGE_WCE_MASK) ? 1 : 0);
  1216. nvme_sc = nvme_set_features(ns->ctrl, NVME_FEAT_VOLATILE_WC,
  1217. dword11, 0, NULL);
  1218. res = nvme_trans_status_code(hdr, nvme_sc);
  1219. break;
  1220. case MODE_PAGE_CONTROL:
  1221. break;
  1222. case MODE_PAGE_POWER_CONDITION:
  1223. /* Verify the OS is not trying to set timers */
  1224. if ((mode_page[2] & 0x01) != 0 || (mode_page[3] & 0x0F) != 0) {
  1225. res = nvme_trans_completion(hdr,
  1226. SAM_STAT_CHECK_CONDITION,
  1227. ILLEGAL_REQUEST,
  1228. SCSI_ASC_INVALID_PARAMETER,
  1229. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1230. break;
  1231. }
  1232. break;
  1233. default:
  1234. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1235. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1236. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1237. break;
  1238. }
  1239. return res;
  1240. }
  1241. static int nvme_trans_modesel_data(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1242. u8 *cmd, u16 parm_list_len, u8 pf,
  1243. u8 sp, u8 cdb10)
  1244. {
  1245. int res;
  1246. u8 *parm_list;
  1247. u16 bd_len;
  1248. u8 llbaa = 0;
  1249. u16 index, saved_index;
  1250. u8 page_code;
  1251. u16 mp_size;
  1252. /* Get parm list from data-in/out buffer */
  1253. parm_list = kmalloc(parm_list_len, GFP_KERNEL);
  1254. if (parm_list == NULL) {
  1255. res = -ENOMEM;
  1256. goto out;
  1257. }
  1258. res = nvme_trans_copy_from_user(hdr, parm_list, parm_list_len);
  1259. if (res)
  1260. goto out_mem;
  1261. nvme_trans_modesel_get_bd_len(parm_list, cdb10, &bd_len, &llbaa);
  1262. index = (cdb10) ? (MODE_SELECT_10_MPH_SIZE) : (MODE_SELECT_6_MPH_SIZE);
  1263. if (bd_len != 0) {
  1264. /* Block Descriptors present, parse */
  1265. nvme_trans_modesel_save_bd(ns, parm_list, index, bd_len, llbaa);
  1266. index += bd_len;
  1267. }
  1268. saved_index = index;
  1269. /* Multiple mode pages may be present; iterate through all */
  1270. /* In 1st Iteration, don't do NVME Command, only check for CDB errors */
  1271. do {
  1272. page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
  1273. mp_size = parm_list[index + 1] + 2;
  1274. if ((page_code != MODE_PAGE_CACHING) &&
  1275. (page_code != MODE_PAGE_CONTROL) &&
  1276. (page_code != MODE_PAGE_POWER_CONDITION)) {
  1277. res = nvme_trans_completion(hdr,
  1278. SAM_STAT_CHECK_CONDITION,
  1279. ILLEGAL_REQUEST,
  1280. SCSI_ASC_INVALID_CDB,
  1281. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1282. goto out_mem;
  1283. }
  1284. index += mp_size;
  1285. } while (index < parm_list_len);
  1286. /* In 2nd Iteration, do the NVME Commands */
  1287. index = saved_index;
  1288. do {
  1289. page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
  1290. mp_size = parm_list[index + 1] + 2;
  1291. res = nvme_trans_modesel_get_mp(ns, hdr, &parm_list[index],
  1292. page_code);
  1293. if (res)
  1294. break;
  1295. index += mp_size;
  1296. } while (index < parm_list_len);
  1297. out_mem:
  1298. kfree(parm_list);
  1299. out:
  1300. return res;
  1301. }
  1302. /* Format Unit Helper Functions */
  1303. static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns *ns,
  1304. struct sg_io_hdr *hdr)
  1305. {
  1306. int res = 0;
  1307. int nvme_sc;
  1308. u8 flbas;
  1309. /*
  1310. * SCSI Expects a MODE SELECT would have been issued prior to
  1311. * a FORMAT UNIT, and the block size and number would be used
  1312. * from the block descriptor in it. If a MODE SELECT had not
  1313. * been issued, FORMAT shall use the current values for both.
  1314. */
  1315. if (ns->mode_select_num_blocks == 0 || ns->mode_select_block_len == 0) {
  1316. struct nvme_id_ns *id_ns;
  1317. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  1318. res = nvme_trans_status_code(hdr, nvme_sc);
  1319. if (res)
  1320. return res;
  1321. if (ns->mode_select_num_blocks == 0)
  1322. ns->mode_select_num_blocks = le64_to_cpu(id_ns->ncap);
  1323. if (ns->mode_select_block_len == 0) {
  1324. flbas = (id_ns->flbas) & 0x0F;
  1325. ns->mode_select_block_len =
  1326. (1 << (id_ns->lbaf[flbas].ds));
  1327. }
  1328. kfree(id_ns);
  1329. }
  1330. return 0;
  1331. }
  1332. static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr *hdr, u8 len,
  1333. u8 format_prot_info, u8 *nvme_pf_code)
  1334. {
  1335. int res;
  1336. u8 *parm_list;
  1337. u8 pf_usage, pf_code;
  1338. parm_list = kmalloc(len, GFP_KERNEL);
  1339. if (parm_list == NULL) {
  1340. res = -ENOMEM;
  1341. goto out;
  1342. }
  1343. res = nvme_trans_copy_from_user(hdr, parm_list, len);
  1344. if (res)
  1345. goto out_mem;
  1346. if ((parm_list[FORMAT_UNIT_IMMED_OFFSET] &
  1347. FORMAT_UNIT_IMMED_MASK) != 0) {
  1348. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1349. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1350. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1351. goto out_mem;
  1352. }
  1353. if (len == FORMAT_UNIT_LONG_PARM_LIST_LEN &&
  1354. (parm_list[FORMAT_UNIT_PROT_INT_OFFSET] & 0x0F) != 0) {
  1355. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1356. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1357. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1358. goto out_mem;
  1359. }
  1360. pf_usage = parm_list[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET] &
  1361. FORMAT_UNIT_PROT_FIELD_USAGE_MASK;
  1362. pf_code = (pf_usage << 2) | format_prot_info;
  1363. switch (pf_code) {
  1364. case 0:
  1365. *nvme_pf_code = 0;
  1366. break;
  1367. case 2:
  1368. *nvme_pf_code = 1;
  1369. break;
  1370. case 3:
  1371. *nvme_pf_code = 2;
  1372. break;
  1373. case 7:
  1374. *nvme_pf_code = 3;
  1375. break;
  1376. default:
  1377. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1378. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1379. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1380. break;
  1381. }
  1382. out_mem:
  1383. kfree(parm_list);
  1384. out:
  1385. return res;
  1386. }
  1387. static int nvme_trans_fmt_send_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1388. u8 prot_info)
  1389. {
  1390. int res;
  1391. int nvme_sc;
  1392. struct nvme_id_ns *id_ns;
  1393. u8 i;
  1394. u8 flbas, nlbaf;
  1395. u8 selected_lbaf = 0xFF;
  1396. u32 cdw10 = 0;
  1397. struct nvme_command c;
  1398. /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */
  1399. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  1400. res = nvme_trans_status_code(hdr, nvme_sc);
  1401. if (res)
  1402. return res;
  1403. flbas = (id_ns->flbas) & 0x0F;
  1404. nlbaf = id_ns->nlbaf;
  1405. for (i = 0; i < nlbaf; i++) {
  1406. if (ns->mode_select_block_len == (1 << (id_ns->lbaf[i].ds))) {
  1407. selected_lbaf = i;
  1408. break;
  1409. }
  1410. }
  1411. if (selected_lbaf > 0x0F) {
  1412. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1413. ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
  1414. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1415. }
  1416. if (ns->mode_select_num_blocks != le64_to_cpu(id_ns->ncap)) {
  1417. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1418. ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
  1419. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1420. }
  1421. cdw10 |= prot_info << 5;
  1422. cdw10 |= selected_lbaf & 0x0F;
  1423. memset(&c, 0, sizeof(c));
  1424. c.format.opcode = nvme_admin_format_nvm;
  1425. c.format.nsid = cpu_to_le32(ns->ns_id);
  1426. c.format.cdw10 = cpu_to_le32(cdw10);
  1427. nvme_sc = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, NULL, 0);
  1428. res = nvme_trans_status_code(hdr, nvme_sc);
  1429. kfree(id_ns);
  1430. return res;
  1431. }
  1432. static inline u32 nvme_trans_io_get_num_cmds(struct sg_io_hdr *hdr,
  1433. struct nvme_trans_io_cdb *cdb_info,
  1434. u32 max_blocks)
  1435. {
  1436. /* If using iovecs, send one nvme command per vector */
  1437. if (hdr->iovec_count > 0)
  1438. return hdr->iovec_count;
  1439. else if (cdb_info->xfer_len > max_blocks)
  1440. return ((cdb_info->xfer_len - 1) / max_blocks) + 1;
  1441. else
  1442. return 1;
  1443. }
  1444. static u16 nvme_trans_io_get_control(struct nvme_ns *ns,
  1445. struct nvme_trans_io_cdb *cdb_info)
  1446. {
  1447. u16 control = 0;
  1448. /* When Protection information support is added, implement here */
  1449. if (cdb_info->fua > 0)
  1450. control |= NVME_RW_FUA;
  1451. return control;
  1452. }
  1453. static int nvme_trans_do_nvme_io(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1454. struct nvme_trans_io_cdb *cdb_info, u8 is_write)
  1455. {
  1456. int nvme_sc = NVME_SC_SUCCESS;
  1457. u32 num_cmds;
  1458. u64 unit_len;
  1459. u64 unit_num_blocks; /* Number of blocks to xfer in each nvme cmd */
  1460. u32 retcode;
  1461. u32 i = 0;
  1462. u64 nvme_offset = 0;
  1463. void __user *next_mapping_addr;
  1464. struct nvme_command c;
  1465. u8 opcode = (is_write ? nvme_cmd_write : nvme_cmd_read);
  1466. u16 control;
  1467. u32 max_blocks = queue_max_hw_sectors(ns->queue);
  1468. num_cmds = nvme_trans_io_get_num_cmds(hdr, cdb_info, max_blocks);
  1469. /*
  1470. * This loop handles two cases.
  1471. * First, when an SGL is used in the form of an iovec list:
  1472. * - Use iov_base as the next mapping address for the nvme command_id
  1473. * - Use iov_len as the data transfer length for the command.
  1474. * Second, when we have a single buffer
  1475. * - If larger than max_blocks, split into chunks, offset
  1476. * each nvme command accordingly.
  1477. */
  1478. for (i = 0; i < num_cmds; i++) {
  1479. memset(&c, 0, sizeof(c));
  1480. if (hdr->iovec_count > 0) {
  1481. struct sg_iovec sgl;
  1482. retcode = copy_from_user(&sgl, hdr->dxferp +
  1483. i * sizeof(struct sg_iovec),
  1484. sizeof(struct sg_iovec));
  1485. if (retcode)
  1486. return -EFAULT;
  1487. unit_len = sgl.iov_len;
  1488. unit_num_blocks = unit_len >> ns->lba_shift;
  1489. next_mapping_addr = sgl.iov_base;
  1490. } else {
  1491. unit_num_blocks = min((u64)max_blocks,
  1492. (cdb_info->xfer_len - nvme_offset));
  1493. unit_len = unit_num_blocks << ns->lba_shift;
  1494. next_mapping_addr = hdr->dxferp +
  1495. ((1 << ns->lba_shift) * nvme_offset);
  1496. }
  1497. c.rw.opcode = opcode;
  1498. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1499. c.rw.slba = cpu_to_le64(cdb_info->lba + nvme_offset);
  1500. c.rw.length = cpu_to_le16(unit_num_blocks - 1);
  1501. control = nvme_trans_io_get_control(ns, cdb_info);
  1502. c.rw.control = cpu_to_le16(control);
  1503. if (get_capacity(ns->disk) - unit_num_blocks <
  1504. cdb_info->lba + nvme_offset) {
  1505. nvme_sc = NVME_SC_LBA_RANGE;
  1506. break;
  1507. }
  1508. nvme_sc = nvme_submit_user_cmd(ns->queue, &c,
  1509. next_mapping_addr, unit_len, NULL, 0);
  1510. if (nvme_sc)
  1511. break;
  1512. nvme_offset += unit_num_blocks;
  1513. }
  1514. return nvme_trans_status_code(hdr, nvme_sc);
  1515. }
  1516. /* SCSI Command Translation Functions */
  1517. static int nvme_trans_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, u8 is_write,
  1518. u8 *cmd)
  1519. {
  1520. int res = 0;
  1521. struct nvme_trans_io_cdb cdb_info = { 0, };
  1522. u8 opcode = cmd[0];
  1523. u64 xfer_bytes;
  1524. u64 sum_iov_len = 0;
  1525. struct sg_iovec sgl;
  1526. int i;
  1527. size_t not_copied;
  1528. /*
  1529. * The FUA and WPROTECT fields are not supported in 6-byte CDBs,
  1530. * but always in the same place for all others.
  1531. */
  1532. switch (opcode) {
  1533. case WRITE_6:
  1534. case READ_6:
  1535. break;
  1536. default:
  1537. cdb_info.fua = cmd[1] & 0x8;
  1538. cdb_info.prot_info = (cmd[1] & 0xe0) >> 5;
  1539. if (cdb_info.prot_info && !ns->pi_type) {
  1540. return nvme_trans_completion(hdr,
  1541. SAM_STAT_CHECK_CONDITION,
  1542. ILLEGAL_REQUEST,
  1543. SCSI_ASC_INVALID_CDB,
  1544. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1545. }
  1546. }
  1547. switch (opcode) {
  1548. case WRITE_6:
  1549. case READ_6:
  1550. cdb_info.lba = get_unaligned_be24(&cmd[1]);
  1551. cdb_info.xfer_len = cmd[4];
  1552. if (cdb_info.xfer_len == 0)
  1553. cdb_info.xfer_len = 256;
  1554. break;
  1555. case WRITE_10:
  1556. case READ_10:
  1557. cdb_info.lba = get_unaligned_be32(&cmd[2]);
  1558. cdb_info.xfer_len = get_unaligned_be16(&cmd[7]);
  1559. break;
  1560. case WRITE_12:
  1561. case READ_12:
  1562. cdb_info.lba = get_unaligned_be32(&cmd[2]);
  1563. cdb_info.xfer_len = get_unaligned_be32(&cmd[6]);
  1564. break;
  1565. case WRITE_16:
  1566. case READ_16:
  1567. cdb_info.lba = get_unaligned_be64(&cmd[2]);
  1568. cdb_info.xfer_len = get_unaligned_be32(&cmd[10]);
  1569. break;
  1570. default:
  1571. /* Will never really reach here */
  1572. res = -EIO;
  1573. goto out;
  1574. }
  1575. /* Calculate total length of transfer (in bytes) */
  1576. if (hdr->iovec_count > 0) {
  1577. for (i = 0; i < hdr->iovec_count; i++) {
  1578. not_copied = copy_from_user(&sgl, hdr->dxferp +
  1579. i * sizeof(struct sg_iovec),
  1580. sizeof(struct sg_iovec));
  1581. if (not_copied)
  1582. return -EFAULT;
  1583. sum_iov_len += sgl.iov_len;
  1584. /* IO vector sizes should be multiples of block size */
  1585. if (sgl.iov_len % (1 << ns->lba_shift) != 0) {
  1586. res = nvme_trans_completion(hdr,
  1587. SAM_STAT_CHECK_CONDITION,
  1588. ILLEGAL_REQUEST,
  1589. SCSI_ASC_INVALID_PARAMETER,
  1590. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1591. goto out;
  1592. }
  1593. }
  1594. } else {
  1595. sum_iov_len = hdr->dxfer_len;
  1596. }
  1597. /* As Per sg ioctl howto, if the lengths differ, use the lower one */
  1598. xfer_bytes = min(((u64)hdr->dxfer_len), sum_iov_len);
  1599. /* If block count and actual data buffer size dont match, error out */
  1600. if (xfer_bytes != (cdb_info.xfer_len << ns->lba_shift)) {
  1601. res = -EINVAL;
  1602. goto out;
  1603. }
  1604. /* Check for 0 length transfer - it is not illegal */
  1605. if (cdb_info.xfer_len == 0)
  1606. goto out;
  1607. /* Send NVMe IO Command(s) */
  1608. res = nvme_trans_do_nvme_io(ns, hdr, &cdb_info, is_write);
  1609. if (res)
  1610. goto out;
  1611. out:
  1612. return res;
  1613. }
  1614. static int nvme_trans_inquiry(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1615. u8 *cmd)
  1616. {
  1617. int res = 0;
  1618. u8 evpd;
  1619. u8 page_code;
  1620. int alloc_len;
  1621. u8 *inq_response;
  1622. evpd = cmd[1] & 0x01;
  1623. page_code = cmd[2];
  1624. alloc_len = get_unaligned_be16(&cmd[3]);
  1625. inq_response = kmalloc(max(alloc_len, STANDARD_INQUIRY_LENGTH),
  1626. GFP_KERNEL);
  1627. if (inq_response == NULL) {
  1628. res = -ENOMEM;
  1629. goto out_mem;
  1630. }
  1631. if (evpd == 0) {
  1632. if (page_code == INQ_STANDARD_INQUIRY_PAGE) {
  1633. res = nvme_trans_standard_inquiry_page(ns, hdr,
  1634. inq_response, alloc_len);
  1635. } else {
  1636. res = nvme_trans_completion(hdr,
  1637. SAM_STAT_CHECK_CONDITION,
  1638. ILLEGAL_REQUEST,
  1639. SCSI_ASC_INVALID_CDB,
  1640. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1641. }
  1642. } else {
  1643. switch (page_code) {
  1644. case VPD_SUPPORTED_PAGES:
  1645. res = nvme_trans_supported_vpd_pages(ns, hdr,
  1646. inq_response, alloc_len);
  1647. break;
  1648. case VPD_SERIAL_NUMBER:
  1649. res = nvme_trans_unit_serial_page(ns, hdr, inq_response,
  1650. alloc_len);
  1651. break;
  1652. case VPD_DEVICE_IDENTIFIERS:
  1653. res = nvme_trans_device_id_page(ns, hdr, inq_response,
  1654. alloc_len);
  1655. break;
  1656. case VPD_EXTENDED_INQUIRY:
  1657. res = nvme_trans_ext_inq_page(ns, hdr, alloc_len);
  1658. break;
  1659. case VPD_BLOCK_LIMITS:
  1660. res = nvme_trans_bdev_limits_page(ns, hdr, inq_response,
  1661. alloc_len);
  1662. break;
  1663. case VPD_BLOCK_DEV_CHARACTERISTICS:
  1664. res = nvme_trans_bdev_char_page(ns, hdr, alloc_len);
  1665. break;
  1666. default:
  1667. res = nvme_trans_completion(hdr,
  1668. SAM_STAT_CHECK_CONDITION,
  1669. ILLEGAL_REQUEST,
  1670. SCSI_ASC_INVALID_CDB,
  1671. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1672. break;
  1673. }
  1674. }
  1675. kfree(inq_response);
  1676. out_mem:
  1677. return res;
  1678. }
  1679. static int nvme_trans_log_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1680. u8 *cmd)
  1681. {
  1682. int res;
  1683. u16 alloc_len;
  1684. u8 pc;
  1685. u8 page_code;
  1686. if (cmd[1] != LOG_SENSE_CDB_SP_NOT_ENABLED) {
  1687. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1688. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1689. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1690. goto out;
  1691. }
  1692. page_code = cmd[2] & LOG_SENSE_CDB_PAGE_CODE_MASK;
  1693. pc = (cmd[2] & LOG_SENSE_CDB_PC_MASK) >> LOG_SENSE_CDB_PC_SHIFT;
  1694. if (pc != LOG_SENSE_CDB_PC_CUMULATIVE_VALUES) {
  1695. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1696. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1697. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1698. goto out;
  1699. }
  1700. alloc_len = get_unaligned_be16(&cmd[7]);
  1701. switch (page_code) {
  1702. case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE:
  1703. res = nvme_trans_log_supp_pages(ns, hdr, alloc_len);
  1704. break;
  1705. case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE:
  1706. res = nvme_trans_log_info_exceptions(ns, hdr, alloc_len);
  1707. break;
  1708. case LOG_PAGE_TEMPERATURE_PAGE:
  1709. res = nvme_trans_log_temperature(ns, hdr, alloc_len);
  1710. break;
  1711. default:
  1712. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1713. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1714. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1715. break;
  1716. }
  1717. out:
  1718. return res;
  1719. }
  1720. static int nvme_trans_mode_select(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1721. u8 *cmd)
  1722. {
  1723. u8 cdb10 = 0;
  1724. u16 parm_list_len;
  1725. u8 page_format;
  1726. u8 save_pages;
  1727. page_format = cmd[1] & MODE_SELECT_CDB_PAGE_FORMAT_MASK;
  1728. save_pages = cmd[1] & MODE_SELECT_CDB_SAVE_PAGES_MASK;
  1729. if (cmd[0] == MODE_SELECT) {
  1730. parm_list_len = cmd[4];
  1731. } else {
  1732. parm_list_len = cmd[7];
  1733. cdb10 = 1;
  1734. }
  1735. if (parm_list_len != 0) {
  1736. /*
  1737. * According to SPC-4 r24, a paramter list length field of 0
  1738. * shall not be considered an error
  1739. */
  1740. return nvme_trans_modesel_data(ns, hdr, cmd, parm_list_len,
  1741. page_format, save_pages, cdb10);
  1742. }
  1743. return 0;
  1744. }
  1745. static int nvme_trans_mode_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1746. u8 *cmd)
  1747. {
  1748. int res = 0;
  1749. u16 alloc_len;
  1750. u8 cdb10 = 0;
  1751. if (cmd[0] == MODE_SENSE) {
  1752. alloc_len = cmd[4];
  1753. } else {
  1754. alloc_len = get_unaligned_be16(&cmd[7]);
  1755. cdb10 = 1;
  1756. }
  1757. if ((cmd[2] & MODE_SENSE_PAGE_CONTROL_MASK) !=
  1758. MODE_SENSE_PC_CURRENT_VALUES) {
  1759. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1760. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1761. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1762. goto out;
  1763. }
  1764. switch (cmd[2] & MODE_SENSE_PAGE_CODE_MASK) {
  1765. case MODE_PAGE_CACHING:
  1766. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1767. cdb10,
  1768. &nvme_trans_fill_caching_page,
  1769. MODE_PAGE_CACHING_LEN);
  1770. break;
  1771. case MODE_PAGE_CONTROL:
  1772. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1773. cdb10,
  1774. &nvme_trans_fill_control_page,
  1775. MODE_PAGE_CONTROL_LEN);
  1776. break;
  1777. case MODE_PAGE_POWER_CONDITION:
  1778. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1779. cdb10,
  1780. &nvme_trans_fill_pow_cnd_page,
  1781. MODE_PAGE_POW_CND_LEN);
  1782. break;
  1783. case MODE_PAGE_INFO_EXCEP:
  1784. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1785. cdb10,
  1786. &nvme_trans_fill_inf_exc_page,
  1787. MODE_PAGE_INF_EXC_LEN);
  1788. break;
  1789. case MODE_PAGE_RETURN_ALL:
  1790. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1791. cdb10,
  1792. &nvme_trans_fill_all_pages,
  1793. MODE_PAGE_ALL_LEN);
  1794. break;
  1795. default:
  1796. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1797. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1798. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1799. break;
  1800. }
  1801. out:
  1802. return res;
  1803. }
  1804. static int nvme_trans_read_capacity(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1805. u8 *cmd, u8 cdb16)
  1806. {
  1807. int res;
  1808. int nvme_sc;
  1809. u32 alloc_len;
  1810. u32 resp_size;
  1811. u32 xfer_len;
  1812. struct nvme_id_ns *id_ns;
  1813. u8 *response;
  1814. if (cdb16) {
  1815. alloc_len = get_unaligned_be32(&cmd[10]);
  1816. resp_size = READ_CAP_16_RESP_SIZE;
  1817. } else {
  1818. alloc_len = READ_CAP_10_RESP_SIZE;
  1819. resp_size = READ_CAP_10_RESP_SIZE;
  1820. }
  1821. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  1822. res = nvme_trans_status_code(hdr, nvme_sc);
  1823. if (res)
  1824. return res;
  1825. response = kzalloc(resp_size, GFP_KERNEL);
  1826. if (response == NULL) {
  1827. res = -ENOMEM;
  1828. goto out_free_id;
  1829. }
  1830. nvme_trans_fill_read_cap(response, id_ns, cdb16);
  1831. xfer_len = min(alloc_len, resp_size);
  1832. res = nvme_trans_copy_to_user(hdr, response, xfer_len);
  1833. kfree(response);
  1834. out_free_id:
  1835. kfree(id_ns);
  1836. return res;
  1837. }
  1838. static int nvme_trans_report_luns(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1839. u8 *cmd)
  1840. {
  1841. int res;
  1842. int nvme_sc;
  1843. u32 alloc_len, xfer_len, resp_size;
  1844. u8 *response;
  1845. struct nvme_id_ctrl *id_ctrl;
  1846. u32 ll_length, lun_id;
  1847. u8 lun_id_offset = REPORT_LUNS_FIRST_LUN_OFFSET;
  1848. __be32 tmp_len;
  1849. switch (cmd[2]) {
  1850. default:
  1851. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1852. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1853. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1854. case ALL_LUNS_RETURNED:
  1855. case ALL_WELL_KNOWN_LUNS_RETURNED:
  1856. case RESTRICTED_LUNS_RETURNED:
  1857. nvme_sc = nvme_identify_ctrl(ns->ctrl, &id_ctrl);
  1858. res = nvme_trans_status_code(hdr, nvme_sc);
  1859. if (res)
  1860. return res;
  1861. ll_length = le32_to_cpu(id_ctrl->nn) * LUN_ENTRY_SIZE;
  1862. resp_size = ll_length + LUN_DATA_HEADER_SIZE;
  1863. alloc_len = get_unaligned_be32(&cmd[6]);
  1864. if (alloc_len < resp_size) {
  1865. res = nvme_trans_completion(hdr,
  1866. SAM_STAT_CHECK_CONDITION,
  1867. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1868. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1869. goto out_free_id;
  1870. }
  1871. response = kzalloc(resp_size, GFP_KERNEL);
  1872. if (response == NULL) {
  1873. res = -ENOMEM;
  1874. goto out_free_id;
  1875. }
  1876. /* The first LUN ID will always be 0 per the SAM spec */
  1877. for (lun_id = 0; lun_id < le32_to_cpu(id_ctrl->nn); lun_id++) {
  1878. /*
  1879. * Set the LUN Id and then increment to the next LUN
  1880. * location in the parameter data.
  1881. */
  1882. __be64 tmp_id = cpu_to_be64(lun_id);
  1883. memcpy(&response[lun_id_offset], &tmp_id, sizeof(u64));
  1884. lun_id_offset += LUN_ENTRY_SIZE;
  1885. }
  1886. tmp_len = cpu_to_be32(ll_length);
  1887. memcpy(response, &tmp_len, sizeof(u32));
  1888. }
  1889. xfer_len = min(alloc_len, resp_size);
  1890. res = nvme_trans_copy_to_user(hdr, response, xfer_len);
  1891. kfree(response);
  1892. out_free_id:
  1893. kfree(id_ctrl);
  1894. return res;
  1895. }
  1896. static int nvme_trans_request_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1897. u8 *cmd)
  1898. {
  1899. int res;
  1900. u8 alloc_len, xfer_len, resp_size;
  1901. u8 desc_format;
  1902. u8 *response;
  1903. desc_format = cmd[1] & 0x01;
  1904. alloc_len = cmd[4];
  1905. resp_size = ((desc_format) ? (DESC_FMT_SENSE_DATA_SIZE) :
  1906. (FIXED_FMT_SENSE_DATA_SIZE));
  1907. response = kzalloc(resp_size, GFP_KERNEL);
  1908. if (response == NULL) {
  1909. res = -ENOMEM;
  1910. goto out;
  1911. }
  1912. if (desc_format) {
  1913. /* Descriptor Format Sense Data */
  1914. response[0] = DESC_FORMAT_SENSE_DATA;
  1915. response[1] = NO_SENSE;
  1916. /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */
  1917. response[2] = SCSI_ASC_NO_SENSE;
  1918. response[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  1919. /* SDAT_OVFL = 0 | Additional Sense Length = 0 */
  1920. } else {
  1921. /* Fixed Format Sense Data */
  1922. response[0] = FIXED_SENSE_DATA;
  1923. /* Byte 1 = Obsolete */
  1924. response[2] = NO_SENSE; /* FM, EOM, ILI, SDAT_OVFL = 0 */
  1925. /* Bytes 3-6 - Information - set to zero */
  1926. response[7] = FIXED_SENSE_DATA_ADD_LENGTH;
  1927. /* Bytes 8-11 - Cmd Specific Information - set to zero */
  1928. response[12] = SCSI_ASC_NO_SENSE;
  1929. response[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  1930. /* Byte 14 = Field Replaceable Unit Code = 0 */
  1931. /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */
  1932. }
  1933. xfer_len = min(alloc_len, resp_size);
  1934. res = nvme_trans_copy_to_user(hdr, response, xfer_len);
  1935. kfree(response);
  1936. out:
  1937. return res;
  1938. }
  1939. static int nvme_trans_security_protocol(struct nvme_ns *ns,
  1940. struct sg_io_hdr *hdr,
  1941. u8 *cmd)
  1942. {
  1943. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1944. ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND,
  1945. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1946. }
  1947. static int nvme_trans_synchronize_cache(struct nvme_ns *ns,
  1948. struct sg_io_hdr *hdr)
  1949. {
  1950. int nvme_sc;
  1951. struct nvme_command c;
  1952. memset(&c, 0, sizeof(c));
  1953. c.common.opcode = nvme_cmd_flush;
  1954. c.common.nsid = cpu_to_le32(ns->ns_id);
  1955. nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0);
  1956. return nvme_trans_status_code(hdr, nvme_sc);
  1957. }
  1958. static int nvme_trans_start_stop(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1959. u8 *cmd)
  1960. {
  1961. u8 immed, pcmod, pc, no_flush, start;
  1962. immed = cmd[1] & 0x01;
  1963. pcmod = cmd[3] & 0x0f;
  1964. pc = (cmd[4] & 0xf0) >> 4;
  1965. no_flush = cmd[4] & 0x04;
  1966. start = cmd[4] & 0x01;
  1967. if (immed != 0) {
  1968. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1969. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1970. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1971. } else {
  1972. if (no_flush == 0) {
  1973. /* Issue NVME FLUSH command prior to START STOP UNIT */
  1974. int res = nvme_trans_synchronize_cache(ns, hdr);
  1975. if (res)
  1976. return res;
  1977. }
  1978. /* Setup the expected power state transition */
  1979. return nvme_trans_power_state(ns, hdr, pc, pcmod, start);
  1980. }
  1981. }
  1982. static int nvme_trans_format_unit(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1983. u8 *cmd)
  1984. {
  1985. int res;
  1986. u8 parm_hdr_len = 0;
  1987. u8 nvme_pf_code = 0;
  1988. u8 format_prot_info, long_list, format_data;
  1989. format_prot_info = (cmd[1] & 0xc0) >> 6;
  1990. long_list = cmd[1] & 0x20;
  1991. format_data = cmd[1] & 0x10;
  1992. if (format_data != 0) {
  1993. if (format_prot_info != 0) {
  1994. if (long_list == 0)
  1995. parm_hdr_len = FORMAT_UNIT_SHORT_PARM_LIST_LEN;
  1996. else
  1997. parm_hdr_len = FORMAT_UNIT_LONG_PARM_LIST_LEN;
  1998. }
  1999. } else if (format_data == 0 && format_prot_info != 0) {
  2000. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2001. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  2002. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2003. goto out;
  2004. }
  2005. /* Get parm header from data-in/out buffer */
  2006. /*
  2007. * According to the translation spec, the only fields in the parameter
  2008. * list we are concerned with are in the header. So allocate only that.
  2009. */
  2010. if (parm_hdr_len > 0) {
  2011. res = nvme_trans_fmt_get_parm_header(hdr, parm_hdr_len,
  2012. format_prot_info, &nvme_pf_code);
  2013. if (res)
  2014. goto out;
  2015. }
  2016. /* Attempt to activate any previously downloaded firmware image */
  2017. res = nvme_trans_send_activate_fw_cmd(ns, hdr, 0);
  2018. /* Determine Block size and count and send format command */
  2019. res = nvme_trans_fmt_set_blk_size_count(ns, hdr);
  2020. if (res)
  2021. goto out;
  2022. res = nvme_trans_fmt_send_cmd(ns, hdr, nvme_pf_code);
  2023. out:
  2024. return res;
  2025. }
  2026. static int nvme_trans_test_unit_ready(struct nvme_ns *ns,
  2027. struct sg_io_hdr *hdr,
  2028. u8 *cmd)
  2029. {
  2030. if (nvme_ctrl_ready(ns->ctrl))
  2031. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2032. NOT_READY, SCSI_ASC_LUN_NOT_READY,
  2033. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2034. else
  2035. return nvme_trans_completion(hdr, SAM_STAT_GOOD, NO_SENSE, 0, 0);
  2036. }
  2037. static int nvme_trans_write_buffer(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  2038. u8 *cmd)
  2039. {
  2040. int res = 0;
  2041. u32 buffer_offset, parm_list_length;
  2042. u8 buffer_id, mode;
  2043. parm_list_length = get_unaligned_be24(&cmd[6]);
  2044. if (parm_list_length % BYTES_TO_DWORDS != 0) {
  2045. /* NVMe expects Firmware file to be a whole number of DWORDS */
  2046. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2047. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  2048. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2049. goto out;
  2050. }
  2051. buffer_id = cmd[2];
  2052. if (buffer_id > NVME_MAX_FIRMWARE_SLOT) {
  2053. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2054. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  2055. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2056. goto out;
  2057. }
  2058. mode = cmd[1] & 0x1f;
  2059. buffer_offset = get_unaligned_be24(&cmd[3]);
  2060. switch (mode) {
  2061. case DOWNLOAD_SAVE_ACTIVATE:
  2062. res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
  2063. parm_list_length, buffer_offset,
  2064. buffer_id);
  2065. if (res)
  2066. goto out;
  2067. res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
  2068. break;
  2069. case DOWNLOAD_SAVE_DEFER_ACTIVATE:
  2070. res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
  2071. parm_list_length, buffer_offset,
  2072. buffer_id);
  2073. break;
  2074. case ACTIVATE_DEFERRED_MICROCODE:
  2075. res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
  2076. break;
  2077. default:
  2078. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2079. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  2080. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2081. break;
  2082. }
  2083. out:
  2084. return res;
  2085. }
  2086. struct scsi_unmap_blk_desc {
  2087. __be64 slba;
  2088. __be32 nlb;
  2089. u32 resv;
  2090. };
  2091. struct scsi_unmap_parm_list {
  2092. __be16 unmap_data_len;
  2093. __be16 unmap_blk_desc_data_len;
  2094. u32 resv;
  2095. struct scsi_unmap_blk_desc desc[0];
  2096. };
  2097. static int nvme_trans_unmap(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  2098. u8 *cmd)
  2099. {
  2100. struct scsi_unmap_parm_list *plist;
  2101. struct nvme_dsm_range *range;
  2102. struct nvme_command c;
  2103. int i, nvme_sc, res;
  2104. u16 ndesc, list_len;
  2105. list_len = get_unaligned_be16(&cmd[7]);
  2106. if (!list_len)
  2107. return -EINVAL;
  2108. plist = kmalloc(list_len, GFP_KERNEL);
  2109. if (!plist)
  2110. return -ENOMEM;
  2111. res = nvme_trans_copy_from_user(hdr, plist, list_len);
  2112. if (res)
  2113. goto out;
  2114. ndesc = be16_to_cpu(plist->unmap_blk_desc_data_len) >> 4;
  2115. if (!ndesc || ndesc > 256) {
  2116. res = -EINVAL;
  2117. goto out;
  2118. }
  2119. range = kcalloc(ndesc, sizeof(*range), GFP_KERNEL);
  2120. if (!range) {
  2121. res = -ENOMEM;
  2122. goto out;
  2123. }
  2124. for (i = 0; i < ndesc; i++) {
  2125. range[i].nlb = cpu_to_le32(be32_to_cpu(plist->desc[i].nlb));
  2126. range[i].slba = cpu_to_le64(be64_to_cpu(plist->desc[i].slba));
  2127. range[i].cattr = 0;
  2128. }
  2129. memset(&c, 0, sizeof(c));
  2130. c.dsm.opcode = nvme_cmd_dsm;
  2131. c.dsm.nsid = cpu_to_le32(ns->ns_id);
  2132. c.dsm.nr = cpu_to_le32(ndesc - 1);
  2133. c.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
  2134. nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, range,
  2135. ndesc * sizeof(*range));
  2136. res = nvme_trans_status_code(hdr, nvme_sc);
  2137. kfree(range);
  2138. out:
  2139. kfree(plist);
  2140. return res;
  2141. }
  2142. static int nvme_scsi_translate(struct nvme_ns *ns, struct sg_io_hdr *hdr)
  2143. {
  2144. u8 cmd[BLK_MAX_CDB];
  2145. int retcode;
  2146. unsigned int opcode;
  2147. if (hdr->cmdp == NULL)
  2148. return -EMSGSIZE;
  2149. if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
  2150. return -EFAULT;
  2151. /*
  2152. * Prime the hdr with good status for scsi commands that don't require
  2153. * an nvme command for translation.
  2154. */
  2155. retcode = nvme_trans_status_code(hdr, NVME_SC_SUCCESS);
  2156. if (retcode)
  2157. return retcode;
  2158. opcode = cmd[0];
  2159. switch (opcode) {
  2160. case READ_6:
  2161. case READ_10:
  2162. case READ_12:
  2163. case READ_16:
  2164. retcode = nvme_trans_io(ns, hdr, 0, cmd);
  2165. break;
  2166. case WRITE_6:
  2167. case WRITE_10:
  2168. case WRITE_12:
  2169. case WRITE_16:
  2170. retcode = nvme_trans_io(ns, hdr, 1, cmd);
  2171. break;
  2172. case INQUIRY:
  2173. retcode = nvme_trans_inquiry(ns, hdr, cmd);
  2174. break;
  2175. case LOG_SENSE:
  2176. retcode = nvme_trans_log_sense(ns, hdr, cmd);
  2177. break;
  2178. case MODE_SELECT:
  2179. case MODE_SELECT_10:
  2180. retcode = nvme_trans_mode_select(ns, hdr, cmd);
  2181. break;
  2182. case MODE_SENSE:
  2183. case MODE_SENSE_10:
  2184. retcode = nvme_trans_mode_sense(ns, hdr, cmd);
  2185. break;
  2186. case READ_CAPACITY:
  2187. retcode = nvme_trans_read_capacity(ns, hdr, cmd, 0);
  2188. break;
  2189. case SERVICE_ACTION_IN_16:
  2190. switch (cmd[1]) {
  2191. case SAI_READ_CAPACITY_16:
  2192. retcode = nvme_trans_read_capacity(ns, hdr, cmd, 1);
  2193. break;
  2194. default:
  2195. goto out;
  2196. }
  2197. break;
  2198. case REPORT_LUNS:
  2199. retcode = nvme_trans_report_luns(ns, hdr, cmd);
  2200. break;
  2201. case REQUEST_SENSE:
  2202. retcode = nvme_trans_request_sense(ns, hdr, cmd);
  2203. break;
  2204. case SECURITY_PROTOCOL_IN:
  2205. case SECURITY_PROTOCOL_OUT:
  2206. retcode = nvme_trans_security_protocol(ns, hdr, cmd);
  2207. break;
  2208. case START_STOP:
  2209. retcode = nvme_trans_start_stop(ns, hdr, cmd);
  2210. break;
  2211. case SYNCHRONIZE_CACHE:
  2212. retcode = nvme_trans_synchronize_cache(ns, hdr);
  2213. break;
  2214. case FORMAT_UNIT:
  2215. retcode = nvme_trans_format_unit(ns, hdr, cmd);
  2216. break;
  2217. case TEST_UNIT_READY:
  2218. retcode = nvme_trans_test_unit_ready(ns, hdr, cmd);
  2219. break;
  2220. case WRITE_BUFFER:
  2221. retcode = nvme_trans_write_buffer(ns, hdr, cmd);
  2222. break;
  2223. case UNMAP:
  2224. retcode = nvme_trans_unmap(ns, hdr, cmd);
  2225. break;
  2226. default:
  2227. out:
  2228. retcode = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2229. ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND,
  2230. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2231. break;
  2232. }
  2233. return retcode;
  2234. }
  2235. int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr)
  2236. {
  2237. struct sg_io_hdr hdr;
  2238. int retcode;
  2239. if (!capable(CAP_SYS_ADMIN))
  2240. return -EACCES;
  2241. if (copy_from_user(&hdr, u_hdr, sizeof(hdr)))
  2242. return -EFAULT;
  2243. if (hdr.interface_id != 'S')
  2244. return -EINVAL;
  2245. if (hdr.cmd_len > BLK_MAX_CDB)
  2246. return -EINVAL;
  2247. /*
  2248. * A positive return code means a NVMe status, which has been
  2249. * translated to sense data.
  2250. */
  2251. retcode = nvme_scsi_translate(ns, &hdr);
  2252. if (retcode < 0)
  2253. return retcode;
  2254. if (copy_to_user(u_hdr, &hdr, sizeof(sg_io_hdr_t)) > 0)
  2255. return -EFAULT;
  2256. return 0;
  2257. }
  2258. int nvme_sg_get_version_num(int __user *ip)
  2259. {
  2260. return put_user(sg_version_num, ip);
  2261. }