scsi.c 65 KB

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