scsi.c 65 KB

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