scsi.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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. u16 bd_num;
  1112. bd_num = bd_len / ((llbaa == 0) ?
  1113. SHORT_DESC_BLOCK : LONG_DESC_BLOCK);
  1114. /* Store block descriptor info if a FORMAT UNIT comes later */
  1115. /* TODO Saving 1st BD info; what to do if multiple BD received? */
  1116. if (llbaa == 0) {
  1117. /* Standard Block Descriptor - spc4r34 7.5.5.1 */
  1118. ns->mode_select_num_blocks =
  1119. (parm_list[idx + 1] << 16) +
  1120. (parm_list[idx + 2] << 8) +
  1121. (parm_list[idx + 3]);
  1122. ns->mode_select_block_len =
  1123. (parm_list[idx + 5] << 16) +
  1124. (parm_list[idx + 6] << 8) +
  1125. (parm_list[idx + 7]);
  1126. } else {
  1127. /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */
  1128. ns->mode_select_num_blocks =
  1129. (((u64)parm_list[idx + 0]) << 56) +
  1130. (((u64)parm_list[idx + 1]) << 48) +
  1131. (((u64)parm_list[idx + 2]) << 40) +
  1132. (((u64)parm_list[idx + 3]) << 32) +
  1133. (((u64)parm_list[idx + 4]) << 24) +
  1134. (((u64)parm_list[idx + 5]) << 16) +
  1135. (((u64)parm_list[idx + 6]) << 8) +
  1136. ((u64)parm_list[idx + 7]);
  1137. ns->mode_select_block_len =
  1138. (parm_list[idx + 12] << 24) +
  1139. (parm_list[idx + 13] << 16) +
  1140. (parm_list[idx + 14] << 8) +
  1141. (parm_list[idx + 15]);
  1142. }
  1143. }
  1144. static int nvme_trans_modesel_get_mp(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1145. u8 *mode_page, u8 page_code)
  1146. {
  1147. int res = 0;
  1148. int nvme_sc;
  1149. unsigned dword11;
  1150. switch (page_code) {
  1151. case MODE_PAGE_CACHING:
  1152. dword11 = ((mode_page[2] & CACHING_MODE_PAGE_WCE_MASK) ? 1 : 0);
  1153. nvme_sc = nvme_set_features(ns->ctrl, NVME_FEAT_VOLATILE_WC,
  1154. dword11, NULL, 0, NULL);
  1155. res = nvme_trans_status_code(hdr, nvme_sc);
  1156. break;
  1157. case MODE_PAGE_CONTROL:
  1158. break;
  1159. case MODE_PAGE_POWER_CONDITION:
  1160. /* Verify the OS is not trying to set timers */
  1161. if ((mode_page[2] & 0x01) != 0 || (mode_page[3] & 0x0F) != 0) {
  1162. res = nvme_trans_completion(hdr,
  1163. SAM_STAT_CHECK_CONDITION,
  1164. ILLEGAL_REQUEST,
  1165. SCSI_ASC_INVALID_PARAMETER,
  1166. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1167. break;
  1168. }
  1169. break;
  1170. default:
  1171. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1172. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1173. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1174. break;
  1175. }
  1176. return res;
  1177. }
  1178. static int nvme_trans_modesel_data(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1179. u8 *cmd, u16 parm_list_len, u8 pf,
  1180. u8 sp, u8 cdb10)
  1181. {
  1182. int res;
  1183. u8 *parm_list;
  1184. u16 bd_len;
  1185. u8 llbaa = 0;
  1186. u16 index, saved_index;
  1187. u8 page_code;
  1188. u16 mp_size;
  1189. /* Get parm list from data-in/out buffer */
  1190. parm_list = kmalloc(parm_list_len, GFP_KERNEL);
  1191. if (parm_list == NULL) {
  1192. res = -ENOMEM;
  1193. goto out;
  1194. }
  1195. res = nvme_trans_copy_from_user(hdr, parm_list, parm_list_len);
  1196. if (res)
  1197. goto out_mem;
  1198. nvme_trans_modesel_get_bd_len(parm_list, cdb10, &bd_len, &llbaa);
  1199. index = (cdb10) ? (MODE_SELECT_10_MPH_SIZE) : (MODE_SELECT_6_MPH_SIZE);
  1200. if (bd_len != 0) {
  1201. /* Block Descriptors present, parse */
  1202. nvme_trans_modesel_save_bd(ns, parm_list, index, bd_len, llbaa);
  1203. index += bd_len;
  1204. }
  1205. saved_index = index;
  1206. /* Multiple mode pages may be present; iterate through all */
  1207. /* In 1st Iteration, don't do NVME Command, only check for CDB errors */
  1208. do {
  1209. page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
  1210. mp_size = parm_list[index + 1] + 2;
  1211. if ((page_code != MODE_PAGE_CACHING) &&
  1212. (page_code != MODE_PAGE_CONTROL) &&
  1213. (page_code != MODE_PAGE_POWER_CONDITION)) {
  1214. res = nvme_trans_completion(hdr,
  1215. SAM_STAT_CHECK_CONDITION,
  1216. ILLEGAL_REQUEST,
  1217. SCSI_ASC_INVALID_CDB,
  1218. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1219. goto out_mem;
  1220. }
  1221. index += mp_size;
  1222. } while (index < parm_list_len);
  1223. /* In 2nd Iteration, do the NVME Commands */
  1224. index = saved_index;
  1225. do {
  1226. page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK;
  1227. mp_size = parm_list[index + 1] + 2;
  1228. res = nvme_trans_modesel_get_mp(ns, hdr, &parm_list[index],
  1229. page_code);
  1230. if (res)
  1231. break;
  1232. index += mp_size;
  1233. } while (index < parm_list_len);
  1234. out_mem:
  1235. kfree(parm_list);
  1236. out:
  1237. return res;
  1238. }
  1239. /* Format Unit Helper Functions */
  1240. static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns *ns,
  1241. struct sg_io_hdr *hdr)
  1242. {
  1243. int res = 0;
  1244. int nvme_sc;
  1245. u8 flbas;
  1246. /*
  1247. * SCSI Expects a MODE SELECT would have been issued prior to
  1248. * a FORMAT UNIT, and the block size and number would be used
  1249. * from the block descriptor in it. If a MODE SELECT had not
  1250. * been issued, FORMAT shall use the current values for both.
  1251. */
  1252. if (ns->mode_select_num_blocks == 0 || ns->mode_select_block_len == 0) {
  1253. struct nvme_id_ns *id_ns;
  1254. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  1255. res = nvme_trans_status_code(hdr, nvme_sc);
  1256. if (res)
  1257. return res;
  1258. if (ns->mode_select_num_blocks == 0)
  1259. ns->mode_select_num_blocks = le64_to_cpu(id_ns->ncap);
  1260. if (ns->mode_select_block_len == 0) {
  1261. flbas = (id_ns->flbas) & 0x0F;
  1262. ns->mode_select_block_len =
  1263. (1 << (id_ns->lbaf[flbas].ds));
  1264. }
  1265. kfree(id_ns);
  1266. }
  1267. return 0;
  1268. }
  1269. static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr *hdr, u8 len,
  1270. u8 format_prot_info, u8 *nvme_pf_code)
  1271. {
  1272. int res;
  1273. u8 *parm_list;
  1274. u8 pf_usage, pf_code;
  1275. parm_list = kmalloc(len, GFP_KERNEL);
  1276. if (parm_list == NULL) {
  1277. res = -ENOMEM;
  1278. goto out;
  1279. }
  1280. res = nvme_trans_copy_from_user(hdr, parm_list, len);
  1281. if (res)
  1282. goto out_mem;
  1283. if ((parm_list[FORMAT_UNIT_IMMED_OFFSET] &
  1284. FORMAT_UNIT_IMMED_MASK) != 0) {
  1285. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1286. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1287. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1288. goto out_mem;
  1289. }
  1290. if (len == FORMAT_UNIT_LONG_PARM_LIST_LEN &&
  1291. (parm_list[FORMAT_UNIT_PROT_INT_OFFSET] & 0x0F) != 0) {
  1292. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1293. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1294. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1295. goto out_mem;
  1296. }
  1297. pf_usage = parm_list[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET] &
  1298. FORMAT_UNIT_PROT_FIELD_USAGE_MASK;
  1299. pf_code = (pf_usage << 2) | format_prot_info;
  1300. switch (pf_code) {
  1301. case 0:
  1302. *nvme_pf_code = 0;
  1303. break;
  1304. case 2:
  1305. *nvme_pf_code = 1;
  1306. break;
  1307. case 3:
  1308. *nvme_pf_code = 2;
  1309. break;
  1310. case 7:
  1311. *nvme_pf_code = 3;
  1312. break;
  1313. default:
  1314. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1315. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1316. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1317. break;
  1318. }
  1319. out_mem:
  1320. kfree(parm_list);
  1321. out:
  1322. return res;
  1323. }
  1324. static int nvme_trans_fmt_send_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1325. u8 prot_info)
  1326. {
  1327. int res;
  1328. int nvme_sc;
  1329. struct nvme_id_ns *id_ns;
  1330. u8 i;
  1331. u8 flbas, nlbaf;
  1332. u8 selected_lbaf = 0xFF;
  1333. u32 cdw10 = 0;
  1334. struct nvme_command c;
  1335. /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */
  1336. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  1337. res = nvme_trans_status_code(hdr, nvme_sc);
  1338. if (res)
  1339. return res;
  1340. flbas = (id_ns->flbas) & 0x0F;
  1341. nlbaf = id_ns->nlbaf;
  1342. for (i = 0; i < nlbaf; i++) {
  1343. if (ns->mode_select_block_len == (1 << (id_ns->lbaf[i].ds))) {
  1344. selected_lbaf = i;
  1345. break;
  1346. }
  1347. }
  1348. if (selected_lbaf > 0x0F) {
  1349. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1350. ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
  1351. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1352. }
  1353. if (ns->mode_select_num_blocks != le64_to_cpu(id_ns->ncap)) {
  1354. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1355. ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER,
  1356. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1357. }
  1358. cdw10 |= prot_info << 5;
  1359. cdw10 |= selected_lbaf & 0x0F;
  1360. memset(&c, 0, sizeof(c));
  1361. c.format.opcode = nvme_admin_format_nvm;
  1362. c.format.nsid = cpu_to_le32(ns->ns_id);
  1363. c.format.cdw10 = cpu_to_le32(cdw10);
  1364. nvme_sc = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, NULL, 0);
  1365. res = nvme_trans_status_code(hdr, nvme_sc);
  1366. kfree(id_ns);
  1367. return res;
  1368. }
  1369. static inline u32 nvme_trans_io_get_num_cmds(struct sg_io_hdr *hdr,
  1370. struct nvme_trans_io_cdb *cdb_info,
  1371. u32 max_blocks)
  1372. {
  1373. /* If using iovecs, send one nvme command per vector */
  1374. if (hdr->iovec_count > 0)
  1375. return hdr->iovec_count;
  1376. else if (cdb_info->xfer_len > max_blocks)
  1377. return ((cdb_info->xfer_len - 1) / max_blocks) + 1;
  1378. else
  1379. return 1;
  1380. }
  1381. static u16 nvme_trans_io_get_control(struct nvme_ns *ns,
  1382. struct nvme_trans_io_cdb *cdb_info)
  1383. {
  1384. u16 control = 0;
  1385. /* When Protection information support is added, implement here */
  1386. if (cdb_info->fua > 0)
  1387. control |= NVME_RW_FUA;
  1388. return control;
  1389. }
  1390. static int nvme_trans_do_nvme_io(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1391. struct nvme_trans_io_cdb *cdb_info, u8 is_write)
  1392. {
  1393. int nvme_sc = NVME_SC_SUCCESS;
  1394. u32 num_cmds;
  1395. u64 unit_len;
  1396. u64 unit_num_blocks; /* Number of blocks to xfer in each nvme cmd */
  1397. u32 retcode;
  1398. u32 i = 0;
  1399. u64 nvme_offset = 0;
  1400. void __user *next_mapping_addr;
  1401. struct nvme_command c;
  1402. u8 opcode = (is_write ? nvme_cmd_write : nvme_cmd_read);
  1403. u16 control;
  1404. u32 max_blocks = queue_max_hw_sectors(ns->queue);
  1405. num_cmds = nvme_trans_io_get_num_cmds(hdr, cdb_info, max_blocks);
  1406. /*
  1407. * This loop handles two cases.
  1408. * First, when an SGL is used in the form of an iovec list:
  1409. * - Use iov_base as the next mapping address for the nvme command_id
  1410. * - Use iov_len as the data transfer length for the command.
  1411. * Second, when we have a single buffer
  1412. * - If larger than max_blocks, split into chunks, offset
  1413. * each nvme command accordingly.
  1414. */
  1415. for (i = 0; i < num_cmds; i++) {
  1416. memset(&c, 0, sizeof(c));
  1417. if (hdr->iovec_count > 0) {
  1418. struct sg_iovec sgl;
  1419. retcode = copy_from_user(&sgl, hdr->dxferp +
  1420. i * sizeof(struct sg_iovec),
  1421. sizeof(struct sg_iovec));
  1422. if (retcode)
  1423. return -EFAULT;
  1424. unit_len = sgl.iov_len;
  1425. unit_num_blocks = unit_len >> ns->lba_shift;
  1426. next_mapping_addr = sgl.iov_base;
  1427. } else {
  1428. unit_num_blocks = min((u64)max_blocks,
  1429. (cdb_info->xfer_len - nvme_offset));
  1430. unit_len = unit_num_blocks << ns->lba_shift;
  1431. next_mapping_addr = hdr->dxferp +
  1432. ((1 << ns->lba_shift) * nvme_offset);
  1433. }
  1434. c.rw.opcode = opcode;
  1435. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1436. c.rw.slba = cpu_to_le64(cdb_info->lba + nvme_offset);
  1437. c.rw.length = cpu_to_le16(unit_num_blocks - 1);
  1438. control = nvme_trans_io_get_control(ns, cdb_info);
  1439. c.rw.control = cpu_to_le16(control);
  1440. if (get_capacity(ns->disk) - unit_num_blocks <
  1441. cdb_info->lba + nvme_offset) {
  1442. nvme_sc = NVME_SC_LBA_RANGE;
  1443. break;
  1444. }
  1445. nvme_sc = nvme_submit_user_cmd(ns->queue, &c,
  1446. next_mapping_addr, unit_len, NULL, 0);
  1447. if (nvme_sc)
  1448. break;
  1449. nvme_offset += unit_num_blocks;
  1450. }
  1451. return nvme_trans_status_code(hdr, nvme_sc);
  1452. }
  1453. /* SCSI Command Translation Functions */
  1454. static int nvme_trans_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, u8 is_write,
  1455. u8 *cmd)
  1456. {
  1457. int res = 0;
  1458. struct nvme_trans_io_cdb cdb_info = { 0, };
  1459. u8 opcode = cmd[0];
  1460. u64 xfer_bytes;
  1461. u64 sum_iov_len = 0;
  1462. struct sg_iovec sgl;
  1463. int i;
  1464. size_t not_copied;
  1465. /*
  1466. * The FUA and WPROTECT fields are not supported in 6-byte CDBs,
  1467. * but always in the same place for all others.
  1468. */
  1469. switch (opcode) {
  1470. case WRITE_6:
  1471. case READ_6:
  1472. break;
  1473. default:
  1474. cdb_info.fua = cmd[1] & 0x8;
  1475. cdb_info.prot_info = (cmd[1] & 0xe0) >> 5;
  1476. if (cdb_info.prot_info && !ns->pi_type) {
  1477. return nvme_trans_completion(hdr,
  1478. SAM_STAT_CHECK_CONDITION,
  1479. ILLEGAL_REQUEST,
  1480. SCSI_ASC_INVALID_CDB,
  1481. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1482. }
  1483. }
  1484. switch (opcode) {
  1485. case WRITE_6:
  1486. case READ_6:
  1487. cdb_info.lba = get_unaligned_be24(&cmd[1]);
  1488. cdb_info.xfer_len = cmd[4];
  1489. if (cdb_info.xfer_len == 0)
  1490. cdb_info.xfer_len = 256;
  1491. break;
  1492. case WRITE_10:
  1493. case READ_10:
  1494. cdb_info.lba = get_unaligned_be32(&cmd[2]);
  1495. cdb_info.xfer_len = get_unaligned_be16(&cmd[7]);
  1496. break;
  1497. case WRITE_12:
  1498. case READ_12:
  1499. cdb_info.lba = get_unaligned_be32(&cmd[2]);
  1500. cdb_info.xfer_len = get_unaligned_be32(&cmd[6]);
  1501. break;
  1502. case WRITE_16:
  1503. case READ_16:
  1504. cdb_info.lba = get_unaligned_be64(&cmd[2]);
  1505. cdb_info.xfer_len = get_unaligned_be32(&cmd[10]);
  1506. break;
  1507. default:
  1508. /* Will never really reach here */
  1509. res = -EIO;
  1510. goto out;
  1511. }
  1512. /* Calculate total length of transfer (in bytes) */
  1513. if (hdr->iovec_count > 0) {
  1514. for (i = 0; i < hdr->iovec_count; i++) {
  1515. not_copied = copy_from_user(&sgl, hdr->dxferp +
  1516. i * sizeof(struct sg_iovec),
  1517. sizeof(struct sg_iovec));
  1518. if (not_copied)
  1519. return -EFAULT;
  1520. sum_iov_len += sgl.iov_len;
  1521. /* IO vector sizes should be multiples of block size */
  1522. if (sgl.iov_len % (1 << ns->lba_shift) != 0) {
  1523. res = nvme_trans_completion(hdr,
  1524. SAM_STAT_CHECK_CONDITION,
  1525. ILLEGAL_REQUEST,
  1526. SCSI_ASC_INVALID_PARAMETER,
  1527. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1528. goto out;
  1529. }
  1530. }
  1531. } else {
  1532. sum_iov_len = hdr->dxfer_len;
  1533. }
  1534. /* As Per sg ioctl howto, if the lengths differ, use the lower one */
  1535. xfer_bytes = min(((u64)hdr->dxfer_len), sum_iov_len);
  1536. /* If block count and actual data buffer size dont match, error out */
  1537. if (xfer_bytes != (cdb_info.xfer_len << ns->lba_shift)) {
  1538. res = -EINVAL;
  1539. goto out;
  1540. }
  1541. /* Check for 0 length transfer - it is not illegal */
  1542. if (cdb_info.xfer_len == 0)
  1543. goto out;
  1544. /* Send NVMe IO Command(s) */
  1545. res = nvme_trans_do_nvme_io(ns, hdr, &cdb_info, is_write);
  1546. if (res)
  1547. goto out;
  1548. out:
  1549. return res;
  1550. }
  1551. static int nvme_trans_inquiry(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1552. u8 *cmd)
  1553. {
  1554. int res = 0;
  1555. u8 evpd;
  1556. u8 page_code;
  1557. int alloc_len;
  1558. u8 *inq_response;
  1559. evpd = cmd[1] & 0x01;
  1560. page_code = cmd[2];
  1561. alloc_len = get_unaligned_be16(&cmd[3]);
  1562. inq_response = kmalloc(max(alloc_len, STANDARD_INQUIRY_LENGTH),
  1563. GFP_KERNEL);
  1564. if (inq_response == NULL) {
  1565. res = -ENOMEM;
  1566. goto out_mem;
  1567. }
  1568. if (evpd == 0) {
  1569. if (page_code == INQ_STANDARD_INQUIRY_PAGE) {
  1570. res = nvme_trans_standard_inquiry_page(ns, hdr,
  1571. inq_response, alloc_len);
  1572. } else {
  1573. res = nvme_trans_completion(hdr,
  1574. SAM_STAT_CHECK_CONDITION,
  1575. ILLEGAL_REQUEST,
  1576. SCSI_ASC_INVALID_CDB,
  1577. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1578. }
  1579. } else {
  1580. switch (page_code) {
  1581. case VPD_SUPPORTED_PAGES:
  1582. res = nvme_trans_supported_vpd_pages(ns, hdr,
  1583. inq_response, alloc_len);
  1584. break;
  1585. case VPD_SERIAL_NUMBER:
  1586. res = nvme_trans_unit_serial_page(ns, hdr, inq_response,
  1587. alloc_len);
  1588. break;
  1589. case VPD_DEVICE_IDENTIFIERS:
  1590. res = nvme_trans_device_id_page(ns, hdr, inq_response,
  1591. alloc_len);
  1592. break;
  1593. case VPD_EXTENDED_INQUIRY:
  1594. res = nvme_trans_ext_inq_page(ns, hdr, alloc_len);
  1595. break;
  1596. case VPD_BLOCK_LIMITS:
  1597. res = nvme_trans_bdev_limits_page(ns, hdr, inq_response,
  1598. alloc_len);
  1599. break;
  1600. case VPD_BLOCK_DEV_CHARACTERISTICS:
  1601. res = nvme_trans_bdev_char_page(ns, hdr, alloc_len);
  1602. break;
  1603. default:
  1604. res = nvme_trans_completion(hdr,
  1605. SAM_STAT_CHECK_CONDITION,
  1606. ILLEGAL_REQUEST,
  1607. SCSI_ASC_INVALID_CDB,
  1608. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1609. break;
  1610. }
  1611. }
  1612. kfree(inq_response);
  1613. out_mem:
  1614. return res;
  1615. }
  1616. static int nvme_trans_log_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1617. u8 *cmd)
  1618. {
  1619. int res;
  1620. u16 alloc_len;
  1621. u8 pc;
  1622. u8 page_code;
  1623. if (cmd[1] != LOG_SENSE_CDB_SP_NOT_ENABLED) {
  1624. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1625. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1626. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1627. goto out;
  1628. }
  1629. page_code = cmd[2] & LOG_SENSE_CDB_PAGE_CODE_MASK;
  1630. pc = (cmd[2] & LOG_SENSE_CDB_PC_MASK) >> LOG_SENSE_CDB_PC_SHIFT;
  1631. if (pc != LOG_SENSE_CDB_PC_CUMULATIVE_VALUES) {
  1632. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1633. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1634. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1635. goto out;
  1636. }
  1637. alloc_len = get_unaligned_be16(&cmd[7]);
  1638. switch (page_code) {
  1639. case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE:
  1640. res = nvme_trans_log_supp_pages(ns, hdr, alloc_len);
  1641. break;
  1642. case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE:
  1643. res = nvme_trans_log_info_exceptions(ns, hdr, alloc_len);
  1644. break;
  1645. case LOG_PAGE_TEMPERATURE_PAGE:
  1646. res = nvme_trans_log_temperature(ns, hdr, alloc_len);
  1647. break;
  1648. default:
  1649. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1650. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1651. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1652. break;
  1653. }
  1654. out:
  1655. return res;
  1656. }
  1657. static int nvme_trans_mode_select(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1658. u8 *cmd)
  1659. {
  1660. u8 cdb10 = 0;
  1661. u16 parm_list_len;
  1662. u8 page_format;
  1663. u8 save_pages;
  1664. page_format = cmd[1] & MODE_SELECT_CDB_PAGE_FORMAT_MASK;
  1665. save_pages = cmd[1] & MODE_SELECT_CDB_SAVE_PAGES_MASK;
  1666. if (cmd[0] == MODE_SELECT) {
  1667. parm_list_len = cmd[4];
  1668. } else {
  1669. parm_list_len = cmd[7];
  1670. cdb10 = 1;
  1671. }
  1672. if (parm_list_len != 0) {
  1673. /*
  1674. * According to SPC-4 r24, a paramter list length field of 0
  1675. * shall not be considered an error
  1676. */
  1677. return nvme_trans_modesel_data(ns, hdr, cmd, parm_list_len,
  1678. page_format, save_pages, cdb10);
  1679. }
  1680. return 0;
  1681. }
  1682. static int nvme_trans_mode_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1683. u8 *cmd)
  1684. {
  1685. int res = 0;
  1686. u16 alloc_len;
  1687. u8 cdb10 = 0;
  1688. if (cmd[0] == MODE_SENSE) {
  1689. alloc_len = cmd[4];
  1690. } else {
  1691. alloc_len = get_unaligned_be16(&cmd[7]);
  1692. cdb10 = 1;
  1693. }
  1694. if ((cmd[2] & MODE_SENSE_PAGE_CONTROL_MASK) !=
  1695. MODE_SENSE_PC_CURRENT_VALUES) {
  1696. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1697. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1698. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1699. goto out;
  1700. }
  1701. switch (cmd[2] & MODE_SENSE_PAGE_CODE_MASK) {
  1702. case MODE_PAGE_CACHING:
  1703. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1704. cdb10,
  1705. &nvme_trans_fill_caching_page,
  1706. MODE_PAGE_CACHING_LEN);
  1707. break;
  1708. case MODE_PAGE_CONTROL:
  1709. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1710. cdb10,
  1711. &nvme_trans_fill_control_page,
  1712. MODE_PAGE_CONTROL_LEN);
  1713. break;
  1714. case MODE_PAGE_POWER_CONDITION:
  1715. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1716. cdb10,
  1717. &nvme_trans_fill_pow_cnd_page,
  1718. MODE_PAGE_POW_CND_LEN);
  1719. break;
  1720. case MODE_PAGE_INFO_EXCEP:
  1721. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1722. cdb10,
  1723. &nvme_trans_fill_inf_exc_page,
  1724. MODE_PAGE_INF_EXC_LEN);
  1725. break;
  1726. case MODE_PAGE_RETURN_ALL:
  1727. res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len,
  1728. cdb10,
  1729. &nvme_trans_fill_all_pages,
  1730. MODE_PAGE_ALL_LEN);
  1731. break;
  1732. default:
  1733. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1734. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1735. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1736. break;
  1737. }
  1738. out:
  1739. return res;
  1740. }
  1741. static int nvme_trans_read_capacity(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1742. u8 *cmd, u8 cdb16)
  1743. {
  1744. int res;
  1745. int nvme_sc;
  1746. u32 alloc_len;
  1747. u32 resp_size;
  1748. u32 xfer_len;
  1749. struct nvme_id_ns *id_ns;
  1750. u8 *response;
  1751. if (cdb16) {
  1752. alloc_len = get_unaligned_be32(&cmd[10]);
  1753. resp_size = READ_CAP_16_RESP_SIZE;
  1754. } else {
  1755. alloc_len = READ_CAP_10_RESP_SIZE;
  1756. resp_size = READ_CAP_10_RESP_SIZE;
  1757. }
  1758. nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns);
  1759. res = nvme_trans_status_code(hdr, nvme_sc);
  1760. if (res)
  1761. return res;
  1762. response = kzalloc(resp_size, GFP_KERNEL);
  1763. if (response == NULL) {
  1764. res = -ENOMEM;
  1765. goto out_free_id;
  1766. }
  1767. nvme_trans_fill_read_cap(response, id_ns, cdb16);
  1768. xfer_len = min(alloc_len, resp_size);
  1769. res = nvme_trans_copy_to_user(hdr, response, xfer_len);
  1770. kfree(response);
  1771. out_free_id:
  1772. kfree(id_ns);
  1773. return res;
  1774. }
  1775. static int nvme_trans_report_luns(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1776. u8 *cmd)
  1777. {
  1778. int res;
  1779. int nvme_sc;
  1780. u32 alloc_len, xfer_len, resp_size;
  1781. u8 *response;
  1782. struct nvme_id_ctrl *id_ctrl;
  1783. u32 ll_length, lun_id;
  1784. u8 lun_id_offset = REPORT_LUNS_FIRST_LUN_OFFSET;
  1785. __be32 tmp_len;
  1786. switch (cmd[2]) {
  1787. default:
  1788. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1789. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1790. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1791. case ALL_LUNS_RETURNED:
  1792. case ALL_WELL_KNOWN_LUNS_RETURNED:
  1793. case RESTRICTED_LUNS_RETURNED:
  1794. nvme_sc = nvme_identify_ctrl(ns->ctrl, &id_ctrl);
  1795. res = nvme_trans_status_code(hdr, nvme_sc);
  1796. if (res)
  1797. return res;
  1798. ll_length = le32_to_cpu(id_ctrl->nn) * LUN_ENTRY_SIZE;
  1799. resp_size = ll_length + LUN_DATA_HEADER_SIZE;
  1800. alloc_len = get_unaligned_be32(&cmd[6]);
  1801. if (alloc_len < resp_size) {
  1802. res = nvme_trans_completion(hdr,
  1803. SAM_STAT_CHECK_CONDITION,
  1804. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1805. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1806. goto out_free_id;
  1807. }
  1808. response = kzalloc(resp_size, GFP_KERNEL);
  1809. if (response == NULL) {
  1810. res = -ENOMEM;
  1811. goto out_free_id;
  1812. }
  1813. /* The first LUN ID will always be 0 per the SAM spec */
  1814. for (lun_id = 0; lun_id < le32_to_cpu(id_ctrl->nn); lun_id++) {
  1815. /*
  1816. * Set the LUN Id and then increment to the next LUN
  1817. * location in the parameter data.
  1818. */
  1819. __be64 tmp_id = cpu_to_be64(lun_id);
  1820. memcpy(&response[lun_id_offset], &tmp_id, sizeof(u64));
  1821. lun_id_offset += LUN_ENTRY_SIZE;
  1822. }
  1823. tmp_len = cpu_to_be32(ll_length);
  1824. memcpy(response, &tmp_len, sizeof(u32));
  1825. }
  1826. xfer_len = min(alloc_len, resp_size);
  1827. res = nvme_trans_copy_to_user(hdr, response, xfer_len);
  1828. kfree(response);
  1829. out_free_id:
  1830. kfree(id_ctrl);
  1831. return res;
  1832. }
  1833. static int nvme_trans_request_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1834. u8 *cmd)
  1835. {
  1836. int res;
  1837. u8 alloc_len, xfer_len, resp_size;
  1838. u8 desc_format;
  1839. u8 *response;
  1840. desc_format = cmd[1] & 0x01;
  1841. alloc_len = cmd[4];
  1842. resp_size = ((desc_format) ? (DESC_FMT_SENSE_DATA_SIZE) :
  1843. (FIXED_FMT_SENSE_DATA_SIZE));
  1844. response = kzalloc(resp_size, GFP_KERNEL);
  1845. if (response == NULL) {
  1846. res = -ENOMEM;
  1847. goto out;
  1848. }
  1849. if (desc_format) {
  1850. /* Descriptor Format Sense Data */
  1851. response[0] = DESC_FORMAT_SENSE_DATA;
  1852. response[1] = NO_SENSE;
  1853. /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */
  1854. response[2] = SCSI_ASC_NO_SENSE;
  1855. response[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  1856. /* SDAT_OVFL = 0 | Additional Sense Length = 0 */
  1857. } else {
  1858. /* Fixed Format Sense Data */
  1859. response[0] = FIXED_SENSE_DATA;
  1860. /* Byte 1 = Obsolete */
  1861. response[2] = NO_SENSE; /* FM, EOM, ILI, SDAT_OVFL = 0 */
  1862. /* Bytes 3-6 - Information - set to zero */
  1863. response[7] = FIXED_SENSE_DATA_ADD_LENGTH;
  1864. /* Bytes 8-11 - Cmd Specific Information - set to zero */
  1865. response[12] = SCSI_ASC_NO_SENSE;
  1866. response[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
  1867. /* Byte 14 = Field Replaceable Unit Code = 0 */
  1868. /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */
  1869. }
  1870. xfer_len = min(alloc_len, resp_size);
  1871. res = nvme_trans_copy_to_user(hdr, response, xfer_len);
  1872. kfree(response);
  1873. out:
  1874. return res;
  1875. }
  1876. static int nvme_trans_security_protocol(struct nvme_ns *ns,
  1877. struct sg_io_hdr *hdr,
  1878. u8 *cmd)
  1879. {
  1880. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1881. ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND,
  1882. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1883. }
  1884. static int nvme_trans_synchronize_cache(struct nvme_ns *ns,
  1885. struct sg_io_hdr *hdr)
  1886. {
  1887. int nvme_sc;
  1888. struct nvme_command c;
  1889. memset(&c, 0, sizeof(c));
  1890. c.common.opcode = nvme_cmd_flush;
  1891. c.common.nsid = cpu_to_le32(ns->ns_id);
  1892. nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0);
  1893. return nvme_trans_status_code(hdr, nvme_sc);
  1894. }
  1895. static int nvme_trans_start_stop(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1896. u8 *cmd)
  1897. {
  1898. u8 immed, pcmod, no_flush, start;
  1899. immed = cmd[1] & 0x01;
  1900. pcmod = cmd[3] & 0x0f;
  1901. no_flush = cmd[4] & 0x04;
  1902. start = cmd[4] & 0x01;
  1903. if (immed != 0) {
  1904. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1905. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1906. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1907. } else {
  1908. if (no_flush == 0) {
  1909. /* Issue NVME FLUSH command prior to START STOP UNIT */
  1910. int res = nvme_trans_synchronize_cache(ns, hdr);
  1911. if (res)
  1912. return res;
  1913. }
  1914. return 0;
  1915. }
  1916. }
  1917. static int nvme_trans_format_unit(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1918. u8 *cmd)
  1919. {
  1920. int res;
  1921. u8 parm_hdr_len = 0;
  1922. u8 nvme_pf_code = 0;
  1923. u8 format_prot_info, long_list, format_data;
  1924. format_prot_info = (cmd[1] & 0xc0) >> 6;
  1925. long_list = cmd[1] & 0x20;
  1926. format_data = cmd[1] & 0x10;
  1927. if (format_data != 0) {
  1928. if (format_prot_info != 0) {
  1929. if (long_list == 0)
  1930. parm_hdr_len = FORMAT_UNIT_SHORT_PARM_LIST_LEN;
  1931. else
  1932. parm_hdr_len = FORMAT_UNIT_LONG_PARM_LIST_LEN;
  1933. }
  1934. } else if (format_data == 0 && format_prot_info != 0) {
  1935. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1936. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1937. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1938. goto out;
  1939. }
  1940. /* Get parm header from data-in/out buffer */
  1941. /*
  1942. * According to the translation spec, the only fields in the parameter
  1943. * list we are concerned with are in the header. So allocate only that.
  1944. */
  1945. if (parm_hdr_len > 0) {
  1946. res = nvme_trans_fmt_get_parm_header(hdr, parm_hdr_len,
  1947. format_prot_info, &nvme_pf_code);
  1948. if (res)
  1949. goto out;
  1950. }
  1951. /* Attempt to activate any previously downloaded firmware image */
  1952. res = nvme_trans_send_activate_fw_cmd(ns, hdr, 0);
  1953. /* Determine Block size and count and send format command */
  1954. res = nvme_trans_fmt_set_blk_size_count(ns, hdr);
  1955. if (res)
  1956. goto out;
  1957. res = nvme_trans_fmt_send_cmd(ns, hdr, nvme_pf_code);
  1958. out:
  1959. return res;
  1960. }
  1961. static int nvme_trans_test_unit_ready(struct nvme_ns *ns,
  1962. struct sg_io_hdr *hdr,
  1963. u8 *cmd)
  1964. {
  1965. if (nvme_ctrl_ready(ns->ctrl))
  1966. return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1967. NOT_READY, SCSI_ASC_LUN_NOT_READY,
  1968. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1969. else
  1970. return nvme_trans_completion(hdr, SAM_STAT_GOOD, NO_SENSE, 0, 0);
  1971. }
  1972. static int nvme_trans_write_buffer(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  1973. u8 *cmd)
  1974. {
  1975. int res = 0;
  1976. u32 buffer_offset, parm_list_length;
  1977. u8 buffer_id, mode;
  1978. parm_list_length = get_unaligned_be24(&cmd[6]);
  1979. if (parm_list_length % BYTES_TO_DWORDS != 0) {
  1980. /* NVMe expects Firmware file to be a whole number of DWORDS */
  1981. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1982. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1983. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1984. goto out;
  1985. }
  1986. buffer_id = cmd[2];
  1987. if (buffer_id > NVME_MAX_FIRMWARE_SLOT) {
  1988. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  1989. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  1990. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  1991. goto out;
  1992. }
  1993. mode = cmd[1] & 0x1f;
  1994. buffer_offset = get_unaligned_be24(&cmd[3]);
  1995. switch (mode) {
  1996. case DOWNLOAD_SAVE_ACTIVATE:
  1997. res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
  1998. parm_list_length, buffer_offset,
  1999. buffer_id);
  2000. if (res)
  2001. goto out;
  2002. res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
  2003. break;
  2004. case DOWNLOAD_SAVE_DEFER_ACTIVATE:
  2005. res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw,
  2006. parm_list_length, buffer_offset,
  2007. buffer_id);
  2008. break;
  2009. case ACTIVATE_DEFERRED_MICROCODE:
  2010. res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id);
  2011. break;
  2012. default:
  2013. res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2014. ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB,
  2015. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2016. break;
  2017. }
  2018. out:
  2019. return res;
  2020. }
  2021. struct scsi_unmap_blk_desc {
  2022. __be64 slba;
  2023. __be32 nlb;
  2024. u32 resv;
  2025. };
  2026. struct scsi_unmap_parm_list {
  2027. __be16 unmap_data_len;
  2028. __be16 unmap_blk_desc_data_len;
  2029. u32 resv;
  2030. struct scsi_unmap_blk_desc desc[0];
  2031. };
  2032. static int nvme_trans_unmap(struct nvme_ns *ns, struct sg_io_hdr *hdr,
  2033. u8 *cmd)
  2034. {
  2035. struct scsi_unmap_parm_list *plist;
  2036. struct nvme_dsm_range *range;
  2037. struct nvme_command c;
  2038. int i, nvme_sc, res;
  2039. u16 ndesc, list_len;
  2040. list_len = get_unaligned_be16(&cmd[7]);
  2041. if (!list_len)
  2042. return -EINVAL;
  2043. plist = kmalloc(list_len, GFP_KERNEL);
  2044. if (!plist)
  2045. return -ENOMEM;
  2046. res = nvme_trans_copy_from_user(hdr, plist, list_len);
  2047. if (res)
  2048. goto out;
  2049. ndesc = be16_to_cpu(plist->unmap_blk_desc_data_len) >> 4;
  2050. if (!ndesc || ndesc > 256) {
  2051. res = -EINVAL;
  2052. goto out;
  2053. }
  2054. range = kcalloc(ndesc, sizeof(*range), GFP_KERNEL);
  2055. if (!range) {
  2056. res = -ENOMEM;
  2057. goto out;
  2058. }
  2059. for (i = 0; i < ndesc; i++) {
  2060. range[i].nlb = cpu_to_le32(be32_to_cpu(plist->desc[i].nlb));
  2061. range[i].slba = cpu_to_le64(be64_to_cpu(plist->desc[i].slba));
  2062. range[i].cattr = 0;
  2063. }
  2064. memset(&c, 0, sizeof(c));
  2065. c.dsm.opcode = nvme_cmd_dsm;
  2066. c.dsm.nsid = cpu_to_le32(ns->ns_id);
  2067. c.dsm.nr = cpu_to_le32(ndesc - 1);
  2068. c.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
  2069. nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, range,
  2070. ndesc * sizeof(*range));
  2071. res = nvme_trans_status_code(hdr, nvme_sc);
  2072. kfree(range);
  2073. out:
  2074. kfree(plist);
  2075. return res;
  2076. }
  2077. static int nvme_scsi_translate(struct nvme_ns *ns, struct sg_io_hdr *hdr)
  2078. {
  2079. u8 cmd[BLK_MAX_CDB];
  2080. int retcode;
  2081. unsigned int opcode;
  2082. if (hdr->cmdp == NULL)
  2083. return -EMSGSIZE;
  2084. if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
  2085. return -EFAULT;
  2086. /*
  2087. * Prime the hdr with good status for scsi commands that don't require
  2088. * an nvme command for translation.
  2089. */
  2090. retcode = nvme_trans_status_code(hdr, NVME_SC_SUCCESS);
  2091. if (retcode)
  2092. return retcode;
  2093. opcode = cmd[0];
  2094. switch (opcode) {
  2095. case READ_6:
  2096. case READ_10:
  2097. case READ_12:
  2098. case READ_16:
  2099. retcode = nvme_trans_io(ns, hdr, 0, cmd);
  2100. break;
  2101. case WRITE_6:
  2102. case WRITE_10:
  2103. case WRITE_12:
  2104. case WRITE_16:
  2105. retcode = nvme_trans_io(ns, hdr, 1, cmd);
  2106. break;
  2107. case INQUIRY:
  2108. retcode = nvme_trans_inquiry(ns, hdr, cmd);
  2109. break;
  2110. case LOG_SENSE:
  2111. retcode = nvme_trans_log_sense(ns, hdr, cmd);
  2112. break;
  2113. case MODE_SELECT:
  2114. case MODE_SELECT_10:
  2115. retcode = nvme_trans_mode_select(ns, hdr, cmd);
  2116. break;
  2117. case MODE_SENSE:
  2118. case MODE_SENSE_10:
  2119. retcode = nvme_trans_mode_sense(ns, hdr, cmd);
  2120. break;
  2121. case READ_CAPACITY:
  2122. retcode = nvme_trans_read_capacity(ns, hdr, cmd, 0);
  2123. break;
  2124. case SERVICE_ACTION_IN_16:
  2125. switch (cmd[1]) {
  2126. case SAI_READ_CAPACITY_16:
  2127. retcode = nvme_trans_read_capacity(ns, hdr, cmd, 1);
  2128. break;
  2129. default:
  2130. goto out;
  2131. }
  2132. break;
  2133. case REPORT_LUNS:
  2134. retcode = nvme_trans_report_luns(ns, hdr, cmd);
  2135. break;
  2136. case REQUEST_SENSE:
  2137. retcode = nvme_trans_request_sense(ns, hdr, cmd);
  2138. break;
  2139. case SECURITY_PROTOCOL_IN:
  2140. case SECURITY_PROTOCOL_OUT:
  2141. retcode = nvme_trans_security_protocol(ns, hdr, cmd);
  2142. break;
  2143. case START_STOP:
  2144. retcode = nvme_trans_start_stop(ns, hdr, cmd);
  2145. break;
  2146. case SYNCHRONIZE_CACHE:
  2147. retcode = nvme_trans_synchronize_cache(ns, hdr);
  2148. break;
  2149. case FORMAT_UNIT:
  2150. retcode = nvme_trans_format_unit(ns, hdr, cmd);
  2151. break;
  2152. case TEST_UNIT_READY:
  2153. retcode = nvme_trans_test_unit_ready(ns, hdr, cmd);
  2154. break;
  2155. case WRITE_BUFFER:
  2156. retcode = nvme_trans_write_buffer(ns, hdr, cmd);
  2157. break;
  2158. case UNMAP:
  2159. retcode = nvme_trans_unmap(ns, hdr, cmd);
  2160. break;
  2161. default:
  2162. out:
  2163. retcode = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION,
  2164. ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND,
  2165. SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
  2166. break;
  2167. }
  2168. return retcode;
  2169. }
  2170. int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr)
  2171. {
  2172. struct sg_io_hdr hdr;
  2173. int retcode;
  2174. if (!capable(CAP_SYS_ADMIN))
  2175. return -EACCES;
  2176. if (copy_from_user(&hdr, u_hdr, sizeof(hdr)))
  2177. return -EFAULT;
  2178. if (hdr.interface_id != 'S')
  2179. return -EINVAL;
  2180. if (hdr.cmd_len > BLK_MAX_CDB)
  2181. return -EINVAL;
  2182. /*
  2183. * A positive return code means a NVMe status, which has been
  2184. * translated to sense data.
  2185. */
  2186. retcode = nvme_scsi_translate(ns, &hdr);
  2187. if (retcode < 0)
  2188. return retcode;
  2189. if (copy_to_user(u_hdr, &hdr, sizeof(sg_io_hdr_t)) > 0)
  2190. return -EFAULT;
  2191. return 0;
  2192. }
  2193. int nvme_sg_get_version_num(int __user *ip)
  2194. {
  2195. return put_user(sg_version_num, ip);
  2196. }