scsi.c 68 KB

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