sd.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  1. /*
  2. * sd.c Copyright (C) 1992 Drew Eckhardt
  3. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  4. *
  5. * Linux scsi disk driver
  6. * Initial versions: Drew Eckhardt
  7. * Subsequent revisions: Eric Youngdale
  8. * Modification history:
  9. * - Drew Eckhardt <drew@colorado.edu> original
  10. * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
  11. * outstanding request, and other enhancements.
  12. * Support loadable low-level scsi drivers.
  13. * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
  14. * eight major numbers.
  15. * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
  16. * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
  17. * sd_init and cleanups.
  18. * - Alex Davis <letmein@erols.com> Fix problem where partition info
  19. * not being read in sd_open. Fix problem where removable media
  20. * could be ejected after sd_open.
  21. * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
  22. * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
  23. * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
  24. * Support 32k/1M disks.
  25. *
  26. * Logging policy (needs CONFIG_SCSI_LOGGING defined):
  27. * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
  28. * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
  29. * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
  30. * - entering other commands: SCSI_LOG_HLQUEUE level 3
  31. * Note: when the logging level is set by the user, it must be greater
  32. * than the level indicated above to trigger output.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/fs.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mm.h>
  38. #include <linux/bio.h>
  39. #include <linux/genhd.h>
  40. #include <linux/hdreg.h>
  41. #include <linux/errno.h>
  42. #include <linux/idr.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/init.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/blkpg.h>
  47. #include <linux/delay.h>
  48. #include <linux/mutex.h>
  49. #include <linux/string_helpers.h>
  50. #include <linux/async.h>
  51. #include <linux/slab.h>
  52. #include <linux/pm_runtime.h>
  53. #include <asm/uaccess.h>
  54. #include <asm/unaligned.h>
  55. #include <scsi/scsi.h>
  56. #include <scsi/scsi_cmnd.h>
  57. #include <scsi/scsi_dbg.h>
  58. #include <scsi/scsi_device.h>
  59. #include <scsi/scsi_driver.h>
  60. #include <scsi/scsi_eh.h>
  61. #include <scsi/scsi_host.h>
  62. #include <scsi/scsi_ioctl.h>
  63. #include <scsi/scsicam.h>
  64. #include "sd.h"
  65. #include "scsi_logging.h"
  66. MODULE_AUTHOR("Eric Youngdale");
  67. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  68. MODULE_LICENSE("GPL");
  69. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  70. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  71. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  80. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  81. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  82. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  83. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  84. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  85. MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
  86. MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
  87. MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
  88. #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
  89. #define SD_MINORS 16
  90. #else
  91. #define SD_MINORS 0
  92. #endif
  93. static void sd_config_discard(struct scsi_disk *, unsigned int);
  94. static int sd_revalidate_disk(struct gendisk *);
  95. static void sd_unlock_native_capacity(struct gendisk *disk);
  96. static int sd_probe(struct device *);
  97. static int sd_remove(struct device *);
  98. static void sd_shutdown(struct device *);
  99. static int sd_suspend(struct device *, pm_message_t state);
  100. static int sd_resume(struct device *);
  101. static void sd_rescan(struct device *);
  102. static int sd_done(struct scsi_cmnd *);
  103. static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
  104. static void scsi_disk_release(struct device *cdev);
  105. static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
  106. static void sd_print_result(struct scsi_disk *, int);
  107. static DEFINE_SPINLOCK(sd_index_lock);
  108. static DEFINE_IDA(sd_index_ida);
  109. /* This semaphore is used to mediate the 0->1 reference get in the
  110. * face of object destruction (i.e. we can't allow a get on an
  111. * object after last put) */
  112. static DEFINE_MUTEX(sd_ref_mutex);
  113. static struct kmem_cache *sd_cdb_cache;
  114. static mempool_t *sd_cdb_pool;
  115. static const char *sd_cache_types[] = {
  116. "write through", "none", "write back",
  117. "write back, no read (daft)"
  118. };
  119. static ssize_t
  120. sd_store_cache_type(struct device *dev, struct device_attribute *attr,
  121. const char *buf, size_t count)
  122. {
  123. int i, ct = -1, rcd, wce, sp;
  124. struct scsi_disk *sdkp = to_scsi_disk(dev);
  125. struct scsi_device *sdp = sdkp->device;
  126. char buffer[64];
  127. char *buffer_data;
  128. struct scsi_mode_data data;
  129. struct scsi_sense_hdr sshdr;
  130. int len;
  131. if (sdp->type != TYPE_DISK)
  132. /* no cache control on RBC devices; theoretically they
  133. * can do it, but there's probably so many exceptions
  134. * it's not worth the risk */
  135. return -EINVAL;
  136. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  137. len = strlen(sd_cache_types[i]);
  138. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  139. buf[len] == '\n') {
  140. ct = i;
  141. break;
  142. }
  143. }
  144. if (ct < 0)
  145. return -EINVAL;
  146. rcd = ct & 0x01 ? 1 : 0;
  147. wce = ct & 0x02 ? 1 : 0;
  148. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  149. SD_MAX_RETRIES, &data, NULL))
  150. return -EINVAL;
  151. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  152. data.block_descriptor_length);
  153. buffer_data = buffer + data.header_length +
  154. data.block_descriptor_length;
  155. buffer_data[2] &= ~0x05;
  156. buffer_data[2] |= wce << 2 | rcd;
  157. sp = buffer_data[0] & 0x80 ? 1 : 0;
  158. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  159. SD_MAX_RETRIES, &data, &sshdr)) {
  160. if (scsi_sense_valid(&sshdr))
  161. sd_print_sense_hdr(sdkp, &sshdr);
  162. return -EINVAL;
  163. }
  164. revalidate_disk(sdkp->disk);
  165. return count;
  166. }
  167. static ssize_t
  168. sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
  169. const char *buf, size_t count)
  170. {
  171. struct scsi_disk *sdkp = to_scsi_disk(dev);
  172. struct scsi_device *sdp = sdkp->device;
  173. if (!capable(CAP_SYS_ADMIN))
  174. return -EACCES;
  175. sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
  176. return count;
  177. }
  178. static ssize_t
  179. sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
  180. const char *buf, size_t count)
  181. {
  182. struct scsi_disk *sdkp = to_scsi_disk(dev);
  183. struct scsi_device *sdp = sdkp->device;
  184. if (!capable(CAP_SYS_ADMIN))
  185. return -EACCES;
  186. if (sdp->type != TYPE_DISK)
  187. return -EINVAL;
  188. sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  189. return count;
  190. }
  191. static ssize_t
  192. sd_show_cache_type(struct device *dev, struct device_attribute *attr,
  193. char *buf)
  194. {
  195. struct scsi_disk *sdkp = to_scsi_disk(dev);
  196. int ct = sdkp->RCD + 2*sdkp->WCE;
  197. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  198. }
  199. static ssize_t
  200. sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
  201. {
  202. struct scsi_disk *sdkp = to_scsi_disk(dev);
  203. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  204. }
  205. static ssize_t
  206. sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
  207. char *buf)
  208. {
  209. struct scsi_disk *sdkp = to_scsi_disk(dev);
  210. struct scsi_device *sdp = sdkp->device;
  211. return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
  212. }
  213. static ssize_t
  214. sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
  215. char *buf)
  216. {
  217. struct scsi_disk *sdkp = to_scsi_disk(dev);
  218. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  219. }
  220. static ssize_t
  221. sd_show_protection_type(struct device *dev, struct device_attribute *attr,
  222. char *buf)
  223. {
  224. struct scsi_disk *sdkp = to_scsi_disk(dev);
  225. return snprintf(buf, 20, "%u\n", sdkp->protection_type);
  226. }
  227. static ssize_t
  228. sd_show_protection_mode(struct device *dev, struct device_attribute *attr,
  229. char *buf)
  230. {
  231. struct scsi_disk *sdkp = to_scsi_disk(dev);
  232. struct scsi_device *sdp = sdkp->device;
  233. unsigned int dif, dix;
  234. dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  235. dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
  236. if (!dix && scsi_host_dix_capable(sdp->host, SD_DIF_TYPE0_PROTECTION)) {
  237. dif = 0;
  238. dix = 1;
  239. }
  240. if (!dif && !dix)
  241. return snprintf(buf, 20, "none\n");
  242. return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
  243. }
  244. static ssize_t
  245. sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
  246. char *buf)
  247. {
  248. struct scsi_disk *sdkp = to_scsi_disk(dev);
  249. return snprintf(buf, 20, "%u\n", sdkp->ATO);
  250. }
  251. static ssize_t
  252. sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
  253. char *buf)
  254. {
  255. struct scsi_disk *sdkp = to_scsi_disk(dev);
  256. return snprintf(buf, 20, "%u\n", sdkp->lbpme);
  257. }
  258. static const char *lbp_mode[] = {
  259. [SD_LBP_FULL] = "full",
  260. [SD_LBP_UNMAP] = "unmap",
  261. [SD_LBP_WS16] = "writesame_16",
  262. [SD_LBP_WS10] = "writesame_10",
  263. [SD_LBP_ZERO] = "writesame_zero",
  264. [SD_LBP_DISABLE] = "disabled",
  265. };
  266. static ssize_t
  267. sd_show_provisioning_mode(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct scsi_disk *sdkp = to_scsi_disk(dev);
  271. return snprintf(buf, 20, "%s\n", lbp_mode[sdkp->provisioning_mode]);
  272. }
  273. static ssize_t
  274. sd_store_provisioning_mode(struct device *dev, struct device_attribute *attr,
  275. const char *buf, size_t count)
  276. {
  277. struct scsi_disk *sdkp = to_scsi_disk(dev);
  278. struct scsi_device *sdp = sdkp->device;
  279. if (!capable(CAP_SYS_ADMIN))
  280. return -EACCES;
  281. if (sdp->type != TYPE_DISK)
  282. return -EINVAL;
  283. if (!strncmp(buf, lbp_mode[SD_LBP_UNMAP], 20))
  284. sd_config_discard(sdkp, SD_LBP_UNMAP);
  285. else if (!strncmp(buf, lbp_mode[SD_LBP_WS16], 20))
  286. sd_config_discard(sdkp, SD_LBP_WS16);
  287. else if (!strncmp(buf, lbp_mode[SD_LBP_WS10], 20))
  288. sd_config_discard(sdkp, SD_LBP_WS10);
  289. else if (!strncmp(buf, lbp_mode[SD_LBP_ZERO], 20))
  290. sd_config_discard(sdkp, SD_LBP_ZERO);
  291. else if (!strncmp(buf, lbp_mode[SD_LBP_DISABLE], 20))
  292. sd_config_discard(sdkp, SD_LBP_DISABLE);
  293. else
  294. return -EINVAL;
  295. return count;
  296. }
  297. static struct device_attribute sd_disk_attrs[] = {
  298. __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
  299. sd_store_cache_type),
  300. __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
  301. __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
  302. sd_store_allow_restart),
  303. __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
  304. sd_store_manage_start_stop),
  305. __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
  306. __ATTR(protection_mode, S_IRUGO, sd_show_protection_mode, NULL),
  307. __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
  308. __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
  309. __ATTR(provisioning_mode, S_IRUGO|S_IWUSR, sd_show_provisioning_mode,
  310. sd_store_provisioning_mode),
  311. __ATTR_NULL,
  312. };
  313. static struct class sd_disk_class = {
  314. .name = "scsi_disk",
  315. .owner = THIS_MODULE,
  316. .dev_release = scsi_disk_release,
  317. .dev_attrs = sd_disk_attrs,
  318. };
  319. static struct scsi_driver sd_template = {
  320. .owner = THIS_MODULE,
  321. .gendrv = {
  322. .name = "sd",
  323. .probe = sd_probe,
  324. .remove = sd_remove,
  325. .suspend = sd_suspend,
  326. .resume = sd_resume,
  327. .shutdown = sd_shutdown,
  328. },
  329. .rescan = sd_rescan,
  330. .done = sd_done,
  331. };
  332. /*
  333. * Device no to disk mapping:
  334. *
  335. * major disc2 disc p1
  336. * |............|.............|....|....| <- dev_t
  337. * 31 20 19 8 7 4 3 0
  338. *
  339. * Inside a major, we have 16k disks, however mapped non-
  340. * contiguously. The first 16 disks are for major0, the next
  341. * ones with major1, ... Disk 256 is for major0 again, disk 272
  342. * for major1, ...
  343. * As we stay compatible with our numbering scheme, we can reuse
  344. * the well-know SCSI majors 8, 65--71, 136--143.
  345. */
  346. static int sd_major(int major_idx)
  347. {
  348. switch (major_idx) {
  349. case 0:
  350. return SCSI_DISK0_MAJOR;
  351. case 1 ... 7:
  352. return SCSI_DISK1_MAJOR + major_idx - 1;
  353. case 8 ... 15:
  354. return SCSI_DISK8_MAJOR + major_idx - 8;
  355. default:
  356. BUG();
  357. return 0; /* shut up gcc */
  358. }
  359. }
  360. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  361. {
  362. struct scsi_disk *sdkp = NULL;
  363. if (disk->private_data) {
  364. sdkp = scsi_disk(disk);
  365. if (scsi_device_get(sdkp->device) == 0)
  366. get_device(&sdkp->dev);
  367. else
  368. sdkp = NULL;
  369. }
  370. return sdkp;
  371. }
  372. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  373. {
  374. struct scsi_disk *sdkp;
  375. mutex_lock(&sd_ref_mutex);
  376. sdkp = __scsi_disk_get(disk);
  377. mutex_unlock(&sd_ref_mutex);
  378. return sdkp;
  379. }
  380. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  381. {
  382. struct scsi_disk *sdkp;
  383. mutex_lock(&sd_ref_mutex);
  384. sdkp = dev_get_drvdata(dev);
  385. if (sdkp)
  386. sdkp = __scsi_disk_get(sdkp->disk);
  387. mutex_unlock(&sd_ref_mutex);
  388. return sdkp;
  389. }
  390. static void scsi_disk_put(struct scsi_disk *sdkp)
  391. {
  392. struct scsi_device *sdev = sdkp->device;
  393. mutex_lock(&sd_ref_mutex);
  394. put_device(&sdkp->dev);
  395. scsi_device_put(sdev);
  396. mutex_unlock(&sd_ref_mutex);
  397. }
  398. static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
  399. {
  400. unsigned int prot_op = SCSI_PROT_NORMAL;
  401. unsigned int dix = scsi_prot_sg_count(scmd);
  402. if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
  403. if (dif && dix)
  404. prot_op = SCSI_PROT_READ_PASS;
  405. else if (dif && !dix)
  406. prot_op = SCSI_PROT_READ_STRIP;
  407. else if (!dif && dix)
  408. prot_op = SCSI_PROT_READ_INSERT;
  409. } else {
  410. if (dif && dix)
  411. prot_op = SCSI_PROT_WRITE_PASS;
  412. else if (dif && !dix)
  413. prot_op = SCSI_PROT_WRITE_INSERT;
  414. else if (!dif && dix)
  415. prot_op = SCSI_PROT_WRITE_STRIP;
  416. }
  417. scsi_set_prot_op(scmd, prot_op);
  418. scsi_set_prot_type(scmd, dif);
  419. }
  420. static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
  421. {
  422. struct request_queue *q = sdkp->disk->queue;
  423. unsigned int logical_block_size = sdkp->device->sector_size;
  424. unsigned int max_blocks = 0;
  425. q->limits.discard_zeroes_data = sdkp->lbprz;
  426. q->limits.discard_alignment = sdkp->unmap_alignment *
  427. logical_block_size;
  428. q->limits.discard_granularity =
  429. max(sdkp->physical_block_size,
  430. sdkp->unmap_granularity * logical_block_size);
  431. switch (mode) {
  432. case SD_LBP_DISABLE:
  433. q->limits.max_discard_sectors = 0;
  434. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  435. return;
  436. case SD_LBP_UNMAP:
  437. max_blocks = min_not_zero(sdkp->max_unmap_blocks, 0xffffffff);
  438. break;
  439. case SD_LBP_WS16:
  440. max_blocks = min_not_zero(sdkp->max_ws_blocks, 0xffffffff);
  441. break;
  442. case SD_LBP_WS10:
  443. max_blocks = min_not_zero(sdkp->max_ws_blocks, (u32)0xffff);
  444. break;
  445. case SD_LBP_ZERO:
  446. max_blocks = min_not_zero(sdkp->max_ws_blocks, (u32)0xffff);
  447. q->limits.discard_zeroes_data = 1;
  448. break;
  449. }
  450. q->limits.max_discard_sectors = max_blocks * (logical_block_size >> 9);
  451. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  452. sdkp->provisioning_mode = mode;
  453. }
  454. /**
  455. * scsi_setup_discard_cmnd - unmap blocks on thinly provisioned device
  456. * @sdp: scsi device to operate one
  457. * @rq: Request to prepare
  458. *
  459. * Will issue either UNMAP or WRITE SAME(16) depending on preference
  460. * indicated by target device.
  461. **/
  462. static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
  463. {
  464. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  465. struct bio *bio = rq->bio;
  466. sector_t sector = bio->bi_sector;
  467. unsigned int nr_sectors = bio_sectors(bio);
  468. unsigned int len;
  469. int ret;
  470. char *buf;
  471. struct page *page;
  472. if (sdkp->device->sector_size == 4096) {
  473. sector >>= 3;
  474. nr_sectors >>= 3;
  475. }
  476. rq->timeout = SD_TIMEOUT;
  477. memset(rq->cmd, 0, rq->cmd_len);
  478. page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
  479. if (!page)
  480. return BLKPREP_DEFER;
  481. switch (sdkp->provisioning_mode) {
  482. case SD_LBP_UNMAP:
  483. buf = page_address(page);
  484. rq->cmd_len = 10;
  485. rq->cmd[0] = UNMAP;
  486. rq->cmd[8] = 24;
  487. put_unaligned_be16(6 + 16, &buf[0]);
  488. put_unaligned_be16(16, &buf[2]);
  489. put_unaligned_be64(sector, &buf[8]);
  490. put_unaligned_be32(nr_sectors, &buf[16]);
  491. len = 24;
  492. break;
  493. case SD_LBP_WS16:
  494. rq->cmd_len = 16;
  495. rq->cmd[0] = WRITE_SAME_16;
  496. rq->cmd[1] = 0x8; /* UNMAP */
  497. put_unaligned_be64(sector, &rq->cmd[2]);
  498. put_unaligned_be32(nr_sectors, &rq->cmd[10]);
  499. len = sdkp->device->sector_size;
  500. break;
  501. case SD_LBP_WS10:
  502. case SD_LBP_ZERO:
  503. rq->cmd_len = 10;
  504. rq->cmd[0] = WRITE_SAME;
  505. if (sdkp->provisioning_mode == SD_LBP_WS10)
  506. rq->cmd[1] = 0x8; /* UNMAP */
  507. put_unaligned_be32(sector, &rq->cmd[2]);
  508. put_unaligned_be16(nr_sectors, &rq->cmd[7]);
  509. len = sdkp->device->sector_size;
  510. break;
  511. default:
  512. ret = BLKPREP_KILL;
  513. goto out;
  514. }
  515. blk_add_request_payload(rq, page, len);
  516. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  517. rq->buffer = page_address(page);
  518. out:
  519. if (ret != BLKPREP_OK) {
  520. __free_page(page);
  521. rq->buffer = NULL;
  522. }
  523. return ret;
  524. }
  525. static int scsi_setup_flush_cmnd(struct scsi_device *sdp, struct request *rq)
  526. {
  527. rq->timeout = SD_FLUSH_TIMEOUT;
  528. rq->retries = SD_MAX_RETRIES;
  529. rq->cmd[0] = SYNCHRONIZE_CACHE;
  530. rq->cmd_len = 10;
  531. return scsi_setup_blk_pc_cmnd(sdp, rq);
  532. }
  533. static void sd_unprep_fn(struct request_queue *q, struct request *rq)
  534. {
  535. if (rq->cmd_flags & REQ_DISCARD) {
  536. free_page((unsigned long)rq->buffer);
  537. rq->buffer = NULL;
  538. }
  539. }
  540. /**
  541. * sd_init_command - build a scsi (read or write) command from
  542. * information in the request structure.
  543. * @SCpnt: pointer to mid-level's per scsi command structure that
  544. * contains request and into which the scsi command is written
  545. *
  546. * Returns 1 if successful and 0 if error (or cannot be done now).
  547. **/
  548. static int sd_prep_fn(struct request_queue *q, struct request *rq)
  549. {
  550. struct scsi_cmnd *SCpnt;
  551. struct scsi_device *sdp = q->queuedata;
  552. struct gendisk *disk = rq->rq_disk;
  553. struct scsi_disk *sdkp;
  554. sector_t block = blk_rq_pos(rq);
  555. sector_t threshold;
  556. unsigned int this_count = blk_rq_sectors(rq);
  557. int ret, host_dif;
  558. unsigned char protect;
  559. /*
  560. * Discard request come in as REQ_TYPE_FS but we turn them into
  561. * block PC requests to make life easier.
  562. */
  563. if (rq->cmd_flags & REQ_DISCARD) {
  564. ret = scsi_setup_discard_cmnd(sdp, rq);
  565. goto out;
  566. } else if (rq->cmd_flags & REQ_FLUSH) {
  567. ret = scsi_setup_flush_cmnd(sdp, rq);
  568. goto out;
  569. } else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  570. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  571. goto out;
  572. } else if (rq->cmd_type != REQ_TYPE_FS) {
  573. ret = BLKPREP_KILL;
  574. goto out;
  575. }
  576. ret = scsi_setup_fs_cmnd(sdp, rq);
  577. if (ret != BLKPREP_OK)
  578. goto out;
  579. SCpnt = rq->special;
  580. sdkp = scsi_disk(disk);
  581. /* from here on until we're complete, any goto out
  582. * is used for a killable error condition */
  583. ret = BLKPREP_KILL;
  584. SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
  585. "sd_init_command: block=%llu, "
  586. "count=%d\n",
  587. (unsigned long long)block,
  588. this_count));
  589. if (!sdp || !scsi_device_online(sdp) ||
  590. block + blk_rq_sectors(rq) > get_capacity(disk)) {
  591. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  592. "Finishing %u sectors\n",
  593. blk_rq_sectors(rq)));
  594. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  595. "Retry with 0x%p\n", SCpnt));
  596. goto out;
  597. }
  598. if (sdp->changed) {
  599. /*
  600. * quietly refuse to do anything to a changed disc until
  601. * the changed bit has been reset
  602. */
  603. /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
  604. goto out;
  605. }
  606. /*
  607. * Some SD card readers can't handle multi-sector accesses which touch
  608. * the last one or two hardware sectors. Split accesses as needed.
  609. */
  610. threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
  611. (sdp->sector_size / 512);
  612. if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
  613. if (block < threshold) {
  614. /* Access up to the threshold but not beyond */
  615. this_count = threshold - block;
  616. } else {
  617. /* Access only a single hardware sector */
  618. this_count = sdp->sector_size / 512;
  619. }
  620. }
  621. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
  622. (unsigned long long)block));
  623. /*
  624. * If we have a 1K hardware sectorsize, prevent access to single
  625. * 512 byte sectors. In theory we could handle this - in fact
  626. * the scsi cdrom driver must be able to handle this because
  627. * we typically use 1K blocksizes, and cdroms typically have
  628. * 2K hardware sectorsizes. Of course, things are simpler
  629. * with the cdrom, since it is read-only. For performance
  630. * reasons, the filesystems should be able to handle this
  631. * and not force the scsi disk driver to use bounce buffers
  632. * for this.
  633. */
  634. if (sdp->sector_size == 1024) {
  635. if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
  636. scmd_printk(KERN_ERR, SCpnt,
  637. "Bad block number requested\n");
  638. goto out;
  639. } else {
  640. block = block >> 1;
  641. this_count = this_count >> 1;
  642. }
  643. }
  644. if (sdp->sector_size == 2048) {
  645. if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
  646. scmd_printk(KERN_ERR, SCpnt,
  647. "Bad block number requested\n");
  648. goto out;
  649. } else {
  650. block = block >> 2;
  651. this_count = this_count >> 2;
  652. }
  653. }
  654. if (sdp->sector_size == 4096) {
  655. if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
  656. scmd_printk(KERN_ERR, SCpnt,
  657. "Bad block number requested\n");
  658. goto out;
  659. } else {
  660. block = block >> 3;
  661. this_count = this_count >> 3;
  662. }
  663. }
  664. if (rq_data_dir(rq) == WRITE) {
  665. if (!sdp->writeable) {
  666. goto out;
  667. }
  668. SCpnt->cmnd[0] = WRITE_6;
  669. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  670. if (blk_integrity_rq(rq) &&
  671. sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
  672. goto out;
  673. } else if (rq_data_dir(rq) == READ) {
  674. SCpnt->cmnd[0] = READ_6;
  675. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  676. } else {
  677. scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
  678. goto out;
  679. }
  680. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  681. "%s %d/%u 512 byte blocks.\n",
  682. (rq_data_dir(rq) == WRITE) ?
  683. "writing" : "reading", this_count,
  684. blk_rq_sectors(rq)));
  685. /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
  686. host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  687. if (host_dif)
  688. protect = 1 << 5;
  689. else
  690. protect = 0;
  691. if (host_dif == SD_DIF_TYPE2_PROTECTION) {
  692. SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
  693. if (unlikely(SCpnt->cmnd == NULL)) {
  694. ret = BLKPREP_DEFER;
  695. goto out;
  696. }
  697. SCpnt->cmd_len = SD_EXT_CDB_SIZE;
  698. memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
  699. SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
  700. SCpnt->cmnd[7] = 0x18;
  701. SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
  702. SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  703. /* LBA */
  704. SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  705. SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  706. SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  707. SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  708. SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
  709. SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
  710. SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
  711. SCpnt->cmnd[19] = (unsigned char) block & 0xff;
  712. /* Expected Indirect LBA */
  713. SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
  714. SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
  715. SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
  716. SCpnt->cmnd[23] = (unsigned char) block & 0xff;
  717. /* Transfer length */
  718. SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
  719. SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
  720. SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
  721. SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
  722. } else if (block > 0xffffffff) {
  723. SCpnt->cmnd[0] += READ_16 - READ_6;
  724. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  725. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  726. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  727. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  728. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  729. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  730. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  731. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  732. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  733. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  734. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  735. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  736. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  737. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  738. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  739. scsi_device_protection(SCpnt->device) ||
  740. SCpnt->device->use_10_for_rw) {
  741. if (this_count > 0xffff)
  742. this_count = 0xffff;
  743. SCpnt->cmnd[0] += READ_10 - READ_6;
  744. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  745. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  746. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  747. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  748. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  749. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  750. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  751. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  752. } else {
  753. if (unlikely(rq->cmd_flags & REQ_FUA)) {
  754. /*
  755. * This happens only if this drive failed
  756. * 10byte rw command with ILLEGAL_REQUEST
  757. * during operation and thus turned off
  758. * use_10_for_rw.
  759. */
  760. scmd_printk(KERN_ERR, SCpnt,
  761. "FUA write on READ/WRITE(6) drive\n");
  762. goto out;
  763. }
  764. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  765. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  766. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  767. SCpnt->cmnd[4] = (unsigned char) this_count;
  768. SCpnt->cmnd[5] = 0;
  769. }
  770. SCpnt->sdb.length = this_count * sdp->sector_size;
  771. /* If DIF or DIX is enabled, tell HBA how to handle request */
  772. if (host_dif || scsi_prot_sg_count(SCpnt))
  773. sd_prot_op(SCpnt, host_dif);
  774. /*
  775. * We shouldn't disconnect in the middle of a sector, so with a dumb
  776. * host adapter, it's safe to assume that we can at least transfer
  777. * this many bytes between each connect / disconnect.
  778. */
  779. SCpnt->transfersize = sdp->sector_size;
  780. SCpnt->underflow = this_count << 9;
  781. SCpnt->allowed = SD_MAX_RETRIES;
  782. /*
  783. * This indicates that the command is ready from our end to be
  784. * queued.
  785. */
  786. ret = BLKPREP_OK;
  787. out:
  788. return scsi_prep_return(q, rq, ret);
  789. }
  790. /**
  791. * sd_open - open a scsi disk device
  792. * @inode: only i_rdev member may be used
  793. * @filp: only f_mode and f_flags may be used
  794. *
  795. * Returns 0 if successful. Returns a negated errno value in case
  796. * of error.
  797. *
  798. * Note: This can be called from a user context (e.g. fsck(1) )
  799. * or from within the kernel (e.g. as a result of a mount(1) ).
  800. * In the latter case @inode and @filp carry an abridged amount
  801. * of information as noted above.
  802. *
  803. * Locking: called with bdev->bd_mutex held.
  804. **/
  805. static int sd_open(struct block_device *bdev, fmode_t mode)
  806. {
  807. struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
  808. struct scsi_device *sdev;
  809. int retval;
  810. if (!sdkp)
  811. return -ENXIO;
  812. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
  813. sdev = sdkp->device;
  814. retval = scsi_autopm_get_device(sdev);
  815. if (retval)
  816. goto error_autopm;
  817. /*
  818. * If the device is in error recovery, wait until it is done.
  819. * If the device is offline, then disallow any access to it.
  820. */
  821. retval = -ENXIO;
  822. if (!scsi_block_when_processing_errors(sdev))
  823. goto error_out;
  824. if (sdev->removable || sdkp->write_prot)
  825. check_disk_change(bdev);
  826. /*
  827. * If the drive is empty, just let the open fail.
  828. */
  829. retval = -ENOMEDIUM;
  830. if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
  831. goto error_out;
  832. /*
  833. * If the device has the write protect tab set, have the open fail
  834. * if the user expects to be able to write to the thing.
  835. */
  836. retval = -EROFS;
  837. if (sdkp->write_prot && (mode & FMODE_WRITE))
  838. goto error_out;
  839. /*
  840. * It is possible that the disk changing stuff resulted in
  841. * the device being taken offline. If this is the case,
  842. * report this to the user, and don't pretend that the
  843. * open actually succeeded.
  844. */
  845. retval = -ENXIO;
  846. if (!scsi_device_online(sdev))
  847. goto error_out;
  848. if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
  849. if (scsi_block_when_processing_errors(sdev))
  850. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  851. }
  852. return 0;
  853. error_out:
  854. scsi_autopm_put_device(sdev);
  855. error_autopm:
  856. scsi_disk_put(sdkp);
  857. return retval;
  858. }
  859. /**
  860. * sd_release - invoked when the (last) close(2) is called on this
  861. * scsi disk.
  862. * @inode: only i_rdev member may be used
  863. * @filp: only f_mode and f_flags may be used
  864. *
  865. * Returns 0.
  866. *
  867. * Note: may block (uninterruptible) if error recovery is underway
  868. * on this disk.
  869. *
  870. * Locking: called with bdev->bd_mutex held.
  871. **/
  872. static int sd_release(struct gendisk *disk, fmode_t mode)
  873. {
  874. struct scsi_disk *sdkp = scsi_disk(disk);
  875. struct scsi_device *sdev = sdkp->device;
  876. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
  877. if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
  878. if (scsi_block_when_processing_errors(sdev))
  879. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  880. }
  881. /*
  882. * XXX and what if there are packets in flight and this close()
  883. * XXX is followed by a "rmmod sd_mod"?
  884. */
  885. scsi_autopm_put_device(sdev);
  886. scsi_disk_put(sdkp);
  887. return 0;
  888. }
  889. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  890. {
  891. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  892. struct scsi_device *sdp = sdkp->device;
  893. struct Scsi_Host *host = sdp->host;
  894. int diskinfo[4];
  895. /* default to most commonly used values */
  896. diskinfo[0] = 0x40; /* 1 << 6 */
  897. diskinfo[1] = 0x20; /* 1 << 5 */
  898. diskinfo[2] = sdkp->capacity >> 11;
  899. /* override with calculated, extended default, or driver values */
  900. if (host->hostt->bios_param)
  901. host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  902. else
  903. scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  904. geo->heads = diskinfo[0];
  905. geo->sectors = diskinfo[1];
  906. geo->cylinders = diskinfo[2];
  907. return 0;
  908. }
  909. /**
  910. * sd_ioctl - process an ioctl
  911. * @inode: only i_rdev/i_bdev members may be used
  912. * @filp: only f_mode and f_flags may be used
  913. * @cmd: ioctl command number
  914. * @arg: this is third argument given to ioctl(2) system call.
  915. * Often contains a pointer.
  916. *
  917. * Returns 0 if successful (some ioctls return positive numbers on
  918. * success as well). Returns a negated errno value in case of error.
  919. *
  920. * Note: most ioctls are forward onto the block subsystem or further
  921. * down in the scsi subsystem.
  922. **/
  923. static int sd_ioctl(struct block_device *bdev, fmode_t mode,
  924. unsigned int cmd, unsigned long arg)
  925. {
  926. struct gendisk *disk = bdev->bd_disk;
  927. struct scsi_disk *sdkp = scsi_disk(disk);
  928. struct scsi_device *sdp = sdkp->device;
  929. void __user *p = (void __user *)arg;
  930. int error;
  931. SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
  932. "cmd=0x%x\n", disk->disk_name, cmd));
  933. /*
  934. * If we are in the middle of error recovery, don't let anyone
  935. * else try and use this device. Also, if error recovery fails, it
  936. * may try and take the device offline, in which case all further
  937. * access to the device is prohibited.
  938. */
  939. error = scsi_nonblockable_ioctl(sdp, cmd, p,
  940. (mode & FMODE_NDELAY) != 0);
  941. if (!scsi_block_when_processing_errors(sdp) || !error)
  942. goto out;
  943. /*
  944. * Send SCSI addressing ioctls directly to mid level, send other
  945. * ioctls to block level and then onto mid level if they can't be
  946. * resolved.
  947. */
  948. switch (cmd) {
  949. case SCSI_IOCTL_GET_IDLUN:
  950. case SCSI_IOCTL_GET_BUS_NUMBER:
  951. error = scsi_ioctl(sdp, cmd, p);
  952. break;
  953. default:
  954. error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
  955. if (error != -ENOTTY)
  956. break;
  957. error = scsi_ioctl(sdp, cmd, p);
  958. break;
  959. }
  960. out:
  961. return error;
  962. }
  963. static void set_media_not_present(struct scsi_disk *sdkp)
  964. {
  965. if (sdkp->media_present)
  966. sdkp->device->changed = 1;
  967. if (sdkp->device->removable) {
  968. sdkp->media_present = 0;
  969. sdkp->capacity = 0;
  970. }
  971. }
  972. static int media_not_present(struct scsi_disk *sdkp,
  973. struct scsi_sense_hdr *sshdr)
  974. {
  975. if (!scsi_sense_valid(sshdr))
  976. return 0;
  977. /* not invoked for commands that could return deferred errors */
  978. switch (sshdr->sense_key) {
  979. case UNIT_ATTENTION:
  980. case NOT_READY:
  981. /* medium not present */
  982. if (sshdr->asc == 0x3A) {
  983. set_media_not_present(sdkp);
  984. return 1;
  985. }
  986. }
  987. return 0;
  988. }
  989. /**
  990. * sd_check_events - check media events
  991. * @disk: kernel device descriptor
  992. * @clearing: disk events currently being cleared
  993. *
  994. * Returns mask of DISK_EVENT_*.
  995. *
  996. * Note: this function is invoked from the block subsystem.
  997. **/
  998. static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
  999. {
  1000. struct scsi_disk *sdkp = scsi_disk(disk);
  1001. struct scsi_device *sdp = sdkp->device;
  1002. struct scsi_sense_hdr *sshdr = NULL;
  1003. int retval;
  1004. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
  1005. /*
  1006. * If the device is offline, don't send any commands - just pretend as
  1007. * if the command failed. If the device ever comes back online, we
  1008. * can deal with it then. It is only because of unrecoverable errors
  1009. * that we would ever take a device offline in the first place.
  1010. */
  1011. if (!scsi_device_online(sdp)) {
  1012. set_media_not_present(sdkp);
  1013. goto out;
  1014. }
  1015. /*
  1016. * Using TEST_UNIT_READY enables differentiation between drive with
  1017. * no cartridge loaded - NOT READY, drive with changed cartridge -
  1018. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  1019. *
  1020. * Drives that auto spin down. eg iomega jaz 1G, will be started
  1021. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  1022. * sd_revalidate() is called.
  1023. */
  1024. retval = -ENODEV;
  1025. if (scsi_block_when_processing_errors(sdp)) {
  1026. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  1027. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
  1028. sshdr);
  1029. }
  1030. /* failed to execute TUR, assume media not present */
  1031. if (host_byte(retval)) {
  1032. set_media_not_present(sdkp);
  1033. goto out;
  1034. }
  1035. if (media_not_present(sdkp, sshdr))
  1036. goto out;
  1037. /*
  1038. * For removable scsi disk we have to recognise the presence
  1039. * of a disk in the drive.
  1040. */
  1041. if (!sdkp->media_present)
  1042. sdp->changed = 1;
  1043. sdkp->media_present = 1;
  1044. out:
  1045. /*
  1046. * sdp->changed is set under the following conditions:
  1047. *
  1048. * Medium present state has changed in either direction.
  1049. * Device has indicated UNIT_ATTENTION.
  1050. */
  1051. kfree(sshdr);
  1052. retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
  1053. sdp->changed = 0;
  1054. return retval;
  1055. }
  1056. static int sd_sync_cache(struct scsi_disk *sdkp)
  1057. {
  1058. int retries, res;
  1059. struct scsi_device *sdp = sdkp->device;
  1060. struct scsi_sense_hdr sshdr;
  1061. if (!scsi_device_online(sdp))
  1062. return -ENODEV;
  1063. for (retries = 3; retries > 0; --retries) {
  1064. unsigned char cmd[10] = { 0 };
  1065. cmd[0] = SYNCHRONIZE_CACHE;
  1066. /*
  1067. * Leave the rest of the command zero to indicate
  1068. * flush everything.
  1069. */
  1070. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  1071. SD_FLUSH_TIMEOUT, SD_MAX_RETRIES, NULL);
  1072. if (res == 0)
  1073. break;
  1074. }
  1075. if (res) {
  1076. sd_print_result(sdkp, res);
  1077. if (driver_byte(res) & DRIVER_SENSE)
  1078. sd_print_sense_hdr(sdkp, &sshdr);
  1079. }
  1080. if (res)
  1081. return -EIO;
  1082. return 0;
  1083. }
  1084. static void sd_rescan(struct device *dev)
  1085. {
  1086. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  1087. if (sdkp) {
  1088. revalidate_disk(sdkp->disk);
  1089. scsi_disk_put(sdkp);
  1090. }
  1091. }
  1092. #ifdef CONFIG_COMPAT
  1093. /*
  1094. * This gets directly called from VFS. When the ioctl
  1095. * is not recognized we go back to the other translation paths.
  1096. */
  1097. static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1098. unsigned int cmd, unsigned long arg)
  1099. {
  1100. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  1101. /*
  1102. * If we are in the middle of error recovery, don't let anyone
  1103. * else try and use this device. Also, if error recovery fails, it
  1104. * may try and take the device offline, in which case all further
  1105. * access to the device is prohibited.
  1106. */
  1107. if (!scsi_block_when_processing_errors(sdev))
  1108. return -ENODEV;
  1109. if (sdev->host->hostt->compat_ioctl) {
  1110. int ret;
  1111. ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  1112. return ret;
  1113. }
  1114. /*
  1115. * Let the static ioctl translation table take care of it.
  1116. */
  1117. return -ENOIOCTLCMD;
  1118. }
  1119. #endif
  1120. static const struct block_device_operations sd_fops = {
  1121. .owner = THIS_MODULE,
  1122. .open = sd_open,
  1123. .release = sd_release,
  1124. .ioctl = sd_ioctl,
  1125. .getgeo = sd_getgeo,
  1126. #ifdef CONFIG_COMPAT
  1127. .compat_ioctl = sd_compat_ioctl,
  1128. #endif
  1129. .check_events = sd_check_events,
  1130. .revalidate_disk = sd_revalidate_disk,
  1131. .unlock_native_capacity = sd_unlock_native_capacity,
  1132. };
  1133. static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
  1134. {
  1135. u64 start_lba = blk_rq_pos(scmd->request);
  1136. u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
  1137. u64 bad_lba;
  1138. int info_valid;
  1139. /*
  1140. * resid is optional but mostly filled in. When it's unused,
  1141. * its value is zero, so we assume the whole buffer transferred
  1142. */
  1143. unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
  1144. unsigned int good_bytes;
  1145. if (scmd->request->cmd_type != REQ_TYPE_FS)
  1146. return 0;
  1147. info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
  1148. SCSI_SENSE_BUFFERSIZE,
  1149. &bad_lba);
  1150. if (!info_valid)
  1151. return 0;
  1152. if (scsi_bufflen(scmd) <= scmd->device->sector_size)
  1153. return 0;
  1154. if (scmd->device->sector_size < 512) {
  1155. /* only legitimate sector_size here is 256 */
  1156. start_lba <<= 1;
  1157. end_lba <<= 1;
  1158. } else {
  1159. /* be careful ... don't want any overflows */
  1160. u64 factor = scmd->device->sector_size / 512;
  1161. do_div(start_lba, factor);
  1162. do_div(end_lba, factor);
  1163. }
  1164. /* The bad lba was reported incorrectly, we have no idea where
  1165. * the error is.
  1166. */
  1167. if (bad_lba < start_lba || bad_lba >= end_lba)
  1168. return 0;
  1169. /* This computation should always be done in terms of
  1170. * the resolution of the device's medium.
  1171. */
  1172. good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
  1173. return min(good_bytes, transferred);
  1174. }
  1175. /**
  1176. * sd_done - bottom half handler: called when the lower level
  1177. * driver has completed (successfully or otherwise) a scsi command.
  1178. * @SCpnt: mid-level's per command structure.
  1179. *
  1180. * Note: potentially run from within an ISR. Must not block.
  1181. **/
  1182. static int sd_done(struct scsi_cmnd *SCpnt)
  1183. {
  1184. int result = SCpnt->result;
  1185. unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
  1186. struct scsi_sense_hdr sshdr;
  1187. struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
  1188. int sense_valid = 0;
  1189. int sense_deferred = 0;
  1190. unsigned char op = SCpnt->cmnd[0];
  1191. if ((SCpnt->request->cmd_flags & REQ_DISCARD) && !result)
  1192. scsi_set_resid(SCpnt, 0);
  1193. if (result) {
  1194. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  1195. if (sense_valid)
  1196. sense_deferred = scsi_sense_is_deferred(&sshdr);
  1197. }
  1198. #ifdef CONFIG_SCSI_LOGGING
  1199. SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
  1200. if (sense_valid) {
  1201. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
  1202. "sd_done: sb[respc,sk,asc,"
  1203. "ascq]=%x,%x,%x,%x\n",
  1204. sshdr.response_code,
  1205. sshdr.sense_key, sshdr.asc,
  1206. sshdr.ascq));
  1207. }
  1208. #endif
  1209. if (driver_byte(result) != DRIVER_SENSE &&
  1210. (!sense_valid || sense_deferred))
  1211. goto out;
  1212. switch (sshdr.sense_key) {
  1213. case HARDWARE_ERROR:
  1214. case MEDIUM_ERROR:
  1215. good_bytes = sd_completed_bytes(SCpnt);
  1216. break;
  1217. case RECOVERED_ERROR:
  1218. good_bytes = scsi_bufflen(SCpnt);
  1219. break;
  1220. case NO_SENSE:
  1221. /* This indicates a false check condition, so ignore it. An
  1222. * unknown amount of data was transferred so treat it as an
  1223. * error.
  1224. */
  1225. scsi_print_sense("sd", SCpnt);
  1226. SCpnt->result = 0;
  1227. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1228. break;
  1229. case ABORTED_COMMAND:
  1230. if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
  1231. good_bytes = sd_completed_bytes(SCpnt);
  1232. break;
  1233. case ILLEGAL_REQUEST:
  1234. if (sshdr.asc == 0x10) /* DIX: Host detected corruption */
  1235. good_bytes = sd_completed_bytes(SCpnt);
  1236. /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
  1237. if ((sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1238. (op == UNMAP || op == WRITE_SAME_16 || op == WRITE_SAME))
  1239. sd_config_discard(sdkp, SD_LBP_DISABLE);
  1240. break;
  1241. default:
  1242. break;
  1243. }
  1244. out:
  1245. if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
  1246. sd_dif_complete(SCpnt, good_bytes);
  1247. if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
  1248. == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd) {
  1249. /* We have to print a failed command here as the
  1250. * extended CDB gets freed before scsi_io_completion()
  1251. * is called.
  1252. */
  1253. if (result)
  1254. scsi_print_command(SCpnt);
  1255. mempool_free(SCpnt->cmnd, sd_cdb_pool);
  1256. SCpnt->cmnd = NULL;
  1257. SCpnt->cmd_len = 0;
  1258. }
  1259. return good_bytes;
  1260. }
  1261. /*
  1262. * spinup disk - called only in sd_revalidate_disk()
  1263. */
  1264. static void
  1265. sd_spinup_disk(struct scsi_disk *sdkp)
  1266. {
  1267. unsigned char cmd[10];
  1268. unsigned long spintime_expire = 0;
  1269. int retries, spintime;
  1270. unsigned int the_result;
  1271. struct scsi_sense_hdr sshdr;
  1272. int sense_valid = 0;
  1273. spintime = 0;
  1274. /* Spin up drives, as required. Only do this at boot time */
  1275. /* Spinup needs to be done for module loads too. */
  1276. do {
  1277. retries = 0;
  1278. do {
  1279. cmd[0] = TEST_UNIT_READY;
  1280. memset((void *) &cmd[1], 0, 9);
  1281. the_result = scsi_execute_req(sdkp->device, cmd,
  1282. DMA_NONE, NULL, 0,
  1283. &sshdr, SD_TIMEOUT,
  1284. SD_MAX_RETRIES, NULL);
  1285. /*
  1286. * If the drive has indicated to us that it
  1287. * doesn't have any media in it, don't bother
  1288. * with any more polling.
  1289. */
  1290. if (media_not_present(sdkp, &sshdr))
  1291. return;
  1292. if (the_result)
  1293. sense_valid = scsi_sense_valid(&sshdr);
  1294. retries++;
  1295. } while (retries < 3 &&
  1296. (!scsi_status_is_good(the_result) ||
  1297. ((driver_byte(the_result) & DRIVER_SENSE) &&
  1298. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  1299. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  1300. /* no sense, TUR either succeeded or failed
  1301. * with a status error */
  1302. if(!spintime && !scsi_status_is_good(the_result)) {
  1303. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1304. sd_print_result(sdkp, the_result);
  1305. }
  1306. break;
  1307. }
  1308. /*
  1309. * The device does not want the automatic start to be issued.
  1310. */
  1311. if (sdkp->device->no_start_on_add)
  1312. break;
  1313. if (sense_valid && sshdr.sense_key == NOT_READY) {
  1314. if (sshdr.asc == 4 && sshdr.ascq == 3)
  1315. break; /* manual intervention required */
  1316. if (sshdr.asc == 4 && sshdr.ascq == 0xb)
  1317. break; /* standby */
  1318. if (sshdr.asc == 4 && sshdr.ascq == 0xc)
  1319. break; /* unavailable */
  1320. /*
  1321. * Issue command to spin up drive when not ready
  1322. */
  1323. if (!spintime) {
  1324. sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  1325. cmd[0] = START_STOP;
  1326. cmd[1] = 1; /* Return immediately */
  1327. memset((void *) &cmd[2], 0, 8);
  1328. cmd[4] = 1; /* Start spin cycle */
  1329. if (sdkp->device->start_stop_pwr_cond)
  1330. cmd[4] |= 1 << 4;
  1331. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  1332. NULL, 0, &sshdr,
  1333. SD_TIMEOUT, SD_MAX_RETRIES,
  1334. NULL);
  1335. spintime_expire = jiffies + 100 * HZ;
  1336. spintime = 1;
  1337. }
  1338. /* Wait 1 second for next try */
  1339. msleep(1000);
  1340. printk(".");
  1341. /*
  1342. * Wait for USB flash devices with slow firmware.
  1343. * Yes, this sense key/ASC combination shouldn't
  1344. * occur here. It's characteristic of these devices.
  1345. */
  1346. } else if (sense_valid &&
  1347. sshdr.sense_key == UNIT_ATTENTION &&
  1348. sshdr.asc == 0x28) {
  1349. if (!spintime) {
  1350. spintime_expire = jiffies + 5 * HZ;
  1351. spintime = 1;
  1352. }
  1353. /* Wait 1 second for next try */
  1354. msleep(1000);
  1355. } else {
  1356. /* we don't understand the sense code, so it's
  1357. * probably pointless to loop */
  1358. if(!spintime) {
  1359. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1360. sd_print_sense_hdr(sdkp, &sshdr);
  1361. }
  1362. break;
  1363. }
  1364. } while (spintime && time_before_eq(jiffies, spintime_expire));
  1365. if (spintime) {
  1366. if (scsi_status_is_good(the_result))
  1367. printk("ready\n");
  1368. else
  1369. printk("not responding...\n");
  1370. }
  1371. }
  1372. /*
  1373. * Determine whether disk supports Data Integrity Field.
  1374. */
  1375. static void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1376. {
  1377. struct scsi_device *sdp = sdkp->device;
  1378. u8 type;
  1379. if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
  1380. return;
  1381. type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
  1382. if (type == sdkp->protection_type || !sdkp->first_scan)
  1383. return;
  1384. sdkp->protection_type = type;
  1385. if (type > SD_DIF_TYPE3_PROTECTION) {
  1386. sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
  1387. "protection type %u. Disabling disk!\n", type);
  1388. sdkp->capacity = 0;
  1389. return;
  1390. }
  1391. if (scsi_host_dif_capable(sdp->host, type))
  1392. sd_printk(KERN_NOTICE, sdkp,
  1393. "Enabling DIF Type %u protection\n", type);
  1394. else
  1395. sd_printk(KERN_NOTICE, sdkp,
  1396. "Disabling DIF Type %u protection\n", type);
  1397. }
  1398. static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1399. struct scsi_sense_hdr *sshdr, int sense_valid,
  1400. int the_result)
  1401. {
  1402. sd_print_result(sdkp, the_result);
  1403. if (driver_byte(the_result) & DRIVER_SENSE)
  1404. sd_print_sense_hdr(sdkp, sshdr);
  1405. else
  1406. sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1407. /*
  1408. * Set dirty bit for removable devices if not ready -
  1409. * sometimes drives will not report this properly.
  1410. */
  1411. if (sdp->removable &&
  1412. sense_valid && sshdr->sense_key == NOT_READY)
  1413. set_media_not_present(sdkp);
  1414. /*
  1415. * We used to set media_present to 0 here to indicate no media
  1416. * in the drive, but some drives fail read capacity even with
  1417. * media present, so we can't do that.
  1418. */
  1419. sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1420. }
  1421. #define RC16_LEN 32
  1422. #if RC16_LEN > SD_BUF_SIZE
  1423. #error RC16_LEN must not be more than SD_BUF_SIZE
  1424. #endif
  1425. #define READ_CAPACITY_RETRIES_ON_RESET 10
  1426. static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1427. unsigned char *buffer)
  1428. {
  1429. unsigned char cmd[16];
  1430. struct scsi_sense_hdr sshdr;
  1431. int sense_valid = 0;
  1432. int the_result;
  1433. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1434. unsigned int alignment;
  1435. unsigned long long lba;
  1436. unsigned sector_size;
  1437. if (sdp->no_read_capacity_16)
  1438. return -EINVAL;
  1439. do {
  1440. memset(cmd, 0, 16);
  1441. cmd[0] = SERVICE_ACTION_IN;
  1442. cmd[1] = SAI_READ_CAPACITY_16;
  1443. cmd[13] = RC16_LEN;
  1444. memset(buffer, 0, RC16_LEN);
  1445. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1446. buffer, RC16_LEN, &sshdr,
  1447. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1448. if (media_not_present(sdkp, &sshdr))
  1449. return -ENODEV;
  1450. if (the_result) {
  1451. sense_valid = scsi_sense_valid(&sshdr);
  1452. if (sense_valid &&
  1453. sshdr.sense_key == ILLEGAL_REQUEST &&
  1454. (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1455. sshdr.ascq == 0x00)
  1456. /* Invalid Command Operation Code or
  1457. * Invalid Field in CDB, just retry
  1458. * silently with RC10 */
  1459. return -EINVAL;
  1460. if (sense_valid &&
  1461. sshdr.sense_key == UNIT_ATTENTION &&
  1462. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1463. /* Device reset might occur several times,
  1464. * give it one more chance */
  1465. if (--reset_retries > 0)
  1466. continue;
  1467. }
  1468. retries--;
  1469. } while (the_result && retries);
  1470. if (the_result) {
  1471. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
  1472. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1473. return -EINVAL;
  1474. }
  1475. sector_size = get_unaligned_be32(&buffer[8]);
  1476. lba = get_unaligned_be64(&buffer[0]);
  1477. sd_read_protection_type(sdkp, buffer);
  1478. if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
  1479. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1480. "kernel compiled with support for large block "
  1481. "devices.\n");
  1482. sdkp->capacity = 0;
  1483. return -EOVERFLOW;
  1484. }
  1485. /* Logical blocks per physical block exponent */
  1486. sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
  1487. /* Lowest aligned logical block */
  1488. alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
  1489. blk_queue_alignment_offset(sdp->request_queue, alignment);
  1490. if (alignment && sdkp->first_scan)
  1491. sd_printk(KERN_NOTICE, sdkp,
  1492. "physical block alignment offset: %u\n", alignment);
  1493. if (buffer[14] & 0x80) { /* LBPME */
  1494. sdkp->lbpme = 1;
  1495. if (buffer[14] & 0x40) /* LBPRZ */
  1496. sdkp->lbprz = 1;
  1497. sd_config_discard(sdkp, SD_LBP_WS16);
  1498. }
  1499. sdkp->capacity = lba + 1;
  1500. return sector_size;
  1501. }
  1502. static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1503. unsigned char *buffer)
  1504. {
  1505. unsigned char cmd[16];
  1506. struct scsi_sense_hdr sshdr;
  1507. int sense_valid = 0;
  1508. int the_result;
  1509. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1510. sector_t lba;
  1511. unsigned sector_size;
  1512. do {
  1513. cmd[0] = READ_CAPACITY;
  1514. memset(&cmd[1], 0, 9);
  1515. memset(buffer, 0, 8);
  1516. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1517. buffer, 8, &sshdr,
  1518. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1519. if (media_not_present(sdkp, &sshdr))
  1520. return -ENODEV;
  1521. if (the_result) {
  1522. sense_valid = scsi_sense_valid(&sshdr);
  1523. if (sense_valid &&
  1524. sshdr.sense_key == UNIT_ATTENTION &&
  1525. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1526. /* Device reset might occur several times,
  1527. * give it one more chance */
  1528. if (--reset_retries > 0)
  1529. continue;
  1530. }
  1531. retries--;
  1532. } while (the_result && retries);
  1533. if (the_result) {
  1534. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
  1535. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1536. return -EINVAL;
  1537. }
  1538. sector_size = get_unaligned_be32(&buffer[4]);
  1539. lba = get_unaligned_be32(&buffer[0]);
  1540. if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
  1541. /* Some buggy (usb cardreader) devices return an lba of
  1542. 0xffffffff when the want to report a size of 0 (with
  1543. which they really mean no media is present) */
  1544. sdkp->capacity = 0;
  1545. sdkp->physical_block_size = sector_size;
  1546. return sector_size;
  1547. }
  1548. if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
  1549. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1550. "kernel compiled with support for large block "
  1551. "devices.\n");
  1552. sdkp->capacity = 0;
  1553. return -EOVERFLOW;
  1554. }
  1555. sdkp->capacity = lba + 1;
  1556. sdkp->physical_block_size = sector_size;
  1557. return sector_size;
  1558. }
  1559. static int sd_try_rc16_first(struct scsi_device *sdp)
  1560. {
  1561. if (sdp->host->max_cmd_len < 16)
  1562. return 0;
  1563. if (sdp->scsi_level > SCSI_SPC_2)
  1564. return 1;
  1565. if (scsi_device_protection(sdp))
  1566. return 1;
  1567. return 0;
  1568. }
  1569. /*
  1570. * read disk capacity
  1571. */
  1572. static void
  1573. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  1574. {
  1575. int sector_size;
  1576. struct scsi_device *sdp = sdkp->device;
  1577. sector_t old_capacity = sdkp->capacity;
  1578. if (sd_try_rc16_first(sdp)) {
  1579. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1580. if (sector_size == -EOVERFLOW)
  1581. goto got_data;
  1582. if (sector_size == -ENODEV)
  1583. return;
  1584. if (sector_size < 0)
  1585. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1586. if (sector_size < 0)
  1587. return;
  1588. } else {
  1589. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1590. if (sector_size == -EOVERFLOW)
  1591. goto got_data;
  1592. if (sector_size < 0)
  1593. return;
  1594. if ((sizeof(sdkp->capacity) > 4) &&
  1595. (sdkp->capacity > 0xffffffffULL)) {
  1596. int old_sector_size = sector_size;
  1597. sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1598. "Trying to use READ CAPACITY(16).\n");
  1599. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1600. if (sector_size < 0) {
  1601. sd_printk(KERN_NOTICE, sdkp,
  1602. "Using 0xffffffff as device size\n");
  1603. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1604. sector_size = old_sector_size;
  1605. goto got_data;
  1606. }
  1607. }
  1608. }
  1609. /* Some devices are known to return the total number of blocks,
  1610. * not the highest block number. Some devices have versions
  1611. * which do this and others which do not. Some devices we might
  1612. * suspect of doing this but we don't know for certain.
  1613. *
  1614. * If we know the reported capacity is wrong, decrement it. If
  1615. * we can only guess, then assume the number of blocks is even
  1616. * (usually true but not always) and err on the side of lowering
  1617. * the capacity.
  1618. */
  1619. if (sdp->fix_capacity ||
  1620. (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
  1621. sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
  1622. "from its reported value: %llu\n",
  1623. (unsigned long long) sdkp->capacity);
  1624. --sdkp->capacity;
  1625. }
  1626. got_data:
  1627. if (sector_size == 0) {
  1628. sector_size = 512;
  1629. sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  1630. "assuming 512.\n");
  1631. }
  1632. if (sector_size != 512 &&
  1633. sector_size != 1024 &&
  1634. sector_size != 2048 &&
  1635. sector_size != 4096 &&
  1636. sector_size != 256) {
  1637. sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  1638. sector_size);
  1639. /*
  1640. * The user might want to re-format the drive with
  1641. * a supported sectorsize. Once this happens, it
  1642. * would be relatively trivial to set the thing up.
  1643. * For this reason, we leave the thing in the table.
  1644. */
  1645. sdkp->capacity = 0;
  1646. /*
  1647. * set a bogus sector size so the normal read/write
  1648. * logic in the block layer will eventually refuse any
  1649. * request on this device without tripping over power
  1650. * of two sector size assumptions
  1651. */
  1652. sector_size = 512;
  1653. }
  1654. blk_queue_logical_block_size(sdp->request_queue, sector_size);
  1655. {
  1656. char cap_str_2[10], cap_str_10[10];
  1657. u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
  1658. string_get_size(sz, STRING_UNITS_2, cap_str_2,
  1659. sizeof(cap_str_2));
  1660. string_get_size(sz, STRING_UNITS_10, cap_str_10,
  1661. sizeof(cap_str_10));
  1662. if (sdkp->first_scan || old_capacity != sdkp->capacity) {
  1663. sd_printk(KERN_NOTICE, sdkp,
  1664. "%llu %d-byte logical blocks: (%s/%s)\n",
  1665. (unsigned long long)sdkp->capacity,
  1666. sector_size, cap_str_10, cap_str_2);
  1667. if (sdkp->physical_block_size != sector_size)
  1668. sd_printk(KERN_NOTICE, sdkp,
  1669. "%u-byte physical blocks\n",
  1670. sdkp->physical_block_size);
  1671. }
  1672. }
  1673. /* Rescale capacity to 512-byte units */
  1674. if (sector_size == 4096)
  1675. sdkp->capacity <<= 3;
  1676. else if (sector_size == 2048)
  1677. sdkp->capacity <<= 2;
  1678. else if (sector_size == 1024)
  1679. sdkp->capacity <<= 1;
  1680. else if (sector_size == 256)
  1681. sdkp->capacity >>= 1;
  1682. blk_queue_physical_block_size(sdp->request_queue,
  1683. sdkp->physical_block_size);
  1684. sdkp->device->sector_size = sector_size;
  1685. }
  1686. /* called with buffer of length 512 */
  1687. static inline int
  1688. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1689. unsigned char *buffer, int len, struct scsi_mode_data *data,
  1690. struct scsi_sense_hdr *sshdr)
  1691. {
  1692. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1693. SD_TIMEOUT, SD_MAX_RETRIES, data,
  1694. sshdr);
  1695. }
  1696. /*
  1697. * read write protect setting, if possible - called only in sd_revalidate_disk()
  1698. * called with buffer of length SD_BUF_SIZE
  1699. */
  1700. static void
  1701. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  1702. {
  1703. int res;
  1704. struct scsi_device *sdp = sdkp->device;
  1705. struct scsi_mode_data data;
  1706. int old_wp = sdkp->write_prot;
  1707. set_disk_ro(sdkp->disk, 0);
  1708. if (sdp->skip_ms_page_3f) {
  1709. sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  1710. return;
  1711. }
  1712. if (sdp->use_192_bytes_for_3f) {
  1713. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1714. } else {
  1715. /*
  1716. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1717. * We have to start carefully: some devices hang if we ask
  1718. * for more than is available.
  1719. */
  1720. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1721. /*
  1722. * Second attempt: ask for page 0 When only page 0 is
  1723. * implemented, a request for page 3F may return Sense Key
  1724. * 5: Illegal Request, Sense Code 24: Invalid field in
  1725. * CDB.
  1726. */
  1727. if (!scsi_status_is_good(res))
  1728. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1729. /*
  1730. * Third attempt: ask 255 bytes, as we did earlier.
  1731. */
  1732. if (!scsi_status_is_good(res))
  1733. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1734. &data, NULL);
  1735. }
  1736. if (!scsi_status_is_good(res)) {
  1737. sd_printk(KERN_WARNING, sdkp,
  1738. "Test WP failed, assume Write Enabled\n");
  1739. } else {
  1740. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1741. set_disk_ro(sdkp->disk, sdkp->write_prot);
  1742. if (sdkp->first_scan || old_wp != sdkp->write_prot) {
  1743. sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  1744. sdkp->write_prot ? "on" : "off");
  1745. sd_printk(KERN_DEBUG, sdkp,
  1746. "Mode Sense: %02x %02x %02x %02x\n",
  1747. buffer[0], buffer[1], buffer[2], buffer[3]);
  1748. }
  1749. }
  1750. }
  1751. /*
  1752. * sd_read_cache_type - called only from sd_revalidate_disk()
  1753. * called with buffer of length SD_BUF_SIZE
  1754. */
  1755. static void
  1756. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1757. {
  1758. int len = 0, res;
  1759. struct scsi_device *sdp = sdkp->device;
  1760. int dbd;
  1761. int modepage;
  1762. int first_len;
  1763. struct scsi_mode_data data;
  1764. struct scsi_sense_hdr sshdr;
  1765. int old_wce = sdkp->WCE;
  1766. int old_rcd = sdkp->RCD;
  1767. int old_dpofua = sdkp->DPOFUA;
  1768. first_len = 4;
  1769. if (sdp->skip_ms_page_8) {
  1770. if (sdp->type == TYPE_RBC)
  1771. goto defaults;
  1772. else {
  1773. if (sdp->skip_ms_page_3f)
  1774. goto defaults;
  1775. modepage = 0x3F;
  1776. if (sdp->use_192_bytes_for_3f)
  1777. first_len = 192;
  1778. dbd = 0;
  1779. }
  1780. } else if (sdp->type == TYPE_RBC) {
  1781. modepage = 6;
  1782. dbd = 8;
  1783. } else {
  1784. modepage = 8;
  1785. dbd = 0;
  1786. }
  1787. /* cautiously ask */
  1788. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
  1789. &data, &sshdr);
  1790. if (!scsi_status_is_good(res))
  1791. goto bad_sense;
  1792. if (!data.header_length) {
  1793. modepage = 6;
  1794. first_len = 0;
  1795. sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
  1796. }
  1797. /* that went OK, now ask for the proper length */
  1798. len = data.length;
  1799. /*
  1800. * We're only interested in the first three bytes, actually.
  1801. * But the data cache page is defined for the first 20.
  1802. */
  1803. if (len < 3)
  1804. goto bad_sense;
  1805. else if (len > SD_BUF_SIZE) {
  1806. sd_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
  1807. "data from %d to %d bytes\n", len, SD_BUF_SIZE);
  1808. len = SD_BUF_SIZE;
  1809. }
  1810. if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
  1811. len = 192;
  1812. /* Get the data */
  1813. if (len > first_len)
  1814. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
  1815. &data, &sshdr);
  1816. if (scsi_status_is_good(res)) {
  1817. int offset = data.header_length + data.block_descriptor_length;
  1818. while (offset < len) {
  1819. u8 page_code = buffer[offset] & 0x3F;
  1820. u8 spf = buffer[offset] & 0x40;
  1821. if (page_code == 8 || page_code == 6) {
  1822. /* We're interested only in the first 3 bytes.
  1823. */
  1824. if (len - offset <= 2) {
  1825. sd_printk(KERN_ERR, sdkp, "Incomplete "
  1826. "mode parameter data\n");
  1827. goto defaults;
  1828. } else {
  1829. modepage = page_code;
  1830. goto Page_found;
  1831. }
  1832. } else {
  1833. /* Go to the next page */
  1834. if (spf && len - offset > 3)
  1835. offset += 4 + (buffer[offset+2] << 8) +
  1836. buffer[offset+3];
  1837. else if (!spf && len - offset > 1)
  1838. offset += 2 + buffer[offset+1];
  1839. else {
  1840. sd_printk(KERN_ERR, sdkp, "Incomplete "
  1841. "mode parameter data\n");
  1842. goto defaults;
  1843. }
  1844. }
  1845. }
  1846. if (modepage == 0x3F) {
  1847. sd_printk(KERN_ERR, sdkp, "No Caching mode page "
  1848. "present\n");
  1849. goto defaults;
  1850. } else if ((buffer[offset] & 0x3f) != modepage) {
  1851. sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
  1852. goto defaults;
  1853. }
  1854. Page_found:
  1855. if (modepage == 8) {
  1856. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  1857. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  1858. } else {
  1859. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  1860. sdkp->RCD = 0;
  1861. }
  1862. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  1863. if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  1864. sd_printk(KERN_NOTICE, sdkp,
  1865. "Uses READ/WRITE(6), disabling FUA\n");
  1866. sdkp->DPOFUA = 0;
  1867. }
  1868. if (sdkp->first_scan || old_wce != sdkp->WCE ||
  1869. old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
  1870. sd_printk(KERN_NOTICE, sdkp,
  1871. "Write cache: %s, read cache: %s, %s\n",
  1872. sdkp->WCE ? "enabled" : "disabled",
  1873. sdkp->RCD ? "disabled" : "enabled",
  1874. sdkp->DPOFUA ? "supports DPO and FUA"
  1875. : "doesn't support DPO or FUA");
  1876. return;
  1877. }
  1878. bad_sense:
  1879. if (scsi_sense_valid(&sshdr) &&
  1880. sshdr.sense_key == ILLEGAL_REQUEST &&
  1881. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  1882. /* Invalid field in CDB */
  1883. sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  1884. else
  1885. sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
  1886. defaults:
  1887. sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
  1888. sdkp->WCE = 0;
  1889. sdkp->RCD = 0;
  1890. sdkp->DPOFUA = 0;
  1891. }
  1892. /*
  1893. * The ATO bit indicates whether the DIF application tag is available
  1894. * for use by the operating system.
  1895. */
  1896. static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
  1897. {
  1898. int res, offset;
  1899. struct scsi_device *sdp = sdkp->device;
  1900. struct scsi_mode_data data;
  1901. struct scsi_sense_hdr sshdr;
  1902. if (sdp->type != TYPE_DISK)
  1903. return;
  1904. if (sdkp->protection_type == 0)
  1905. return;
  1906. res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
  1907. SD_MAX_RETRIES, &data, &sshdr);
  1908. if (!scsi_status_is_good(res) || !data.header_length ||
  1909. data.length < 6) {
  1910. sd_printk(KERN_WARNING, sdkp,
  1911. "getting Control mode page failed, assume no ATO\n");
  1912. if (scsi_sense_valid(&sshdr))
  1913. sd_print_sense_hdr(sdkp, &sshdr);
  1914. return;
  1915. }
  1916. offset = data.header_length + data.block_descriptor_length;
  1917. if ((buffer[offset] & 0x3f) != 0x0a) {
  1918. sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
  1919. return;
  1920. }
  1921. if ((buffer[offset + 5] & 0x80) == 0)
  1922. return;
  1923. sdkp->ATO = 1;
  1924. return;
  1925. }
  1926. /**
  1927. * sd_read_block_limits - Query disk device for preferred I/O sizes.
  1928. * @disk: disk to query
  1929. */
  1930. static void sd_read_block_limits(struct scsi_disk *sdkp)
  1931. {
  1932. unsigned int sector_sz = sdkp->device->sector_size;
  1933. const int vpd_len = 64;
  1934. unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
  1935. if (!buffer ||
  1936. /* Block Limits VPD */
  1937. scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
  1938. goto out;
  1939. blk_queue_io_min(sdkp->disk->queue,
  1940. get_unaligned_be16(&buffer[6]) * sector_sz);
  1941. blk_queue_io_opt(sdkp->disk->queue,
  1942. get_unaligned_be32(&buffer[12]) * sector_sz);
  1943. if (buffer[3] == 0x3c) {
  1944. unsigned int lba_count, desc_count;
  1945. sdkp->max_ws_blocks =
  1946. (u32) min_not_zero(get_unaligned_be64(&buffer[36]),
  1947. (u64)0xffffffff);
  1948. if (!sdkp->lbpme)
  1949. goto out;
  1950. lba_count = get_unaligned_be32(&buffer[20]);
  1951. desc_count = get_unaligned_be32(&buffer[24]);
  1952. if (lba_count && desc_count)
  1953. sdkp->max_unmap_blocks = lba_count;
  1954. sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
  1955. if (buffer[32] & 0x80)
  1956. sdkp->unmap_alignment =
  1957. get_unaligned_be32(&buffer[32]) & ~(1 << 31);
  1958. if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
  1959. if (sdkp->max_unmap_blocks)
  1960. sd_config_discard(sdkp, SD_LBP_UNMAP);
  1961. else
  1962. sd_config_discard(sdkp, SD_LBP_WS16);
  1963. } else { /* LBP VPD page tells us what to use */
  1964. if (sdkp->lbpu && sdkp->max_unmap_blocks)
  1965. sd_config_discard(sdkp, SD_LBP_UNMAP);
  1966. else if (sdkp->lbpws)
  1967. sd_config_discard(sdkp, SD_LBP_WS16);
  1968. else if (sdkp->lbpws10)
  1969. sd_config_discard(sdkp, SD_LBP_WS10);
  1970. else
  1971. sd_config_discard(sdkp, SD_LBP_DISABLE);
  1972. }
  1973. }
  1974. out:
  1975. kfree(buffer);
  1976. }
  1977. /**
  1978. * sd_read_block_characteristics - Query block dev. characteristics
  1979. * @disk: disk to query
  1980. */
  1981. static void sd_read_block_characteristics(struct scsi_disk *sdkp)
  1982. {
  1983. unsigned char *buffer;
  1984. u16 rot;
  1985. const int vpd_len = 64;
  1986. buffer = kmalloc(vpd_len, GFP_KERNEL);
  1987. if (!buffer ||
  1988. /* Block Device Characteristics VPD */
  1989. scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
  1990. goto out;
  1991. rot = get_unaligned_be16(&buffer[4]);
  1992. if (rot == 1)
  1993. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
  1994. out:
  1995. kfree(buffer);
  1996. }
  1997. /**
  1998. * sd_read_block_provisioning - Query provisioning VPD page
  1999. * @disk: disk to query
  2000. */
  2001. static void sd_read_block_provisioning(struct scsi_disk *sdkp)
  2002. {
  2003. unsigned char *buffer;
  2004. const int vpd_len = 8;
  2005. if (sdkp->lbpme == 0)
  2006. return;
  2007. buffer = kmalloc(vpd_len, GFP_KERNEL);
  2008. if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
  2009. goto out;
  2010. sdkp->lbpvpd = 1;
  2011. sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
  2012. sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
  2013. sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
  2014. out:
  2015. kfree(buffer);
  2016. }
  2017. static int sd_try_extended_inquiry(struct scsi_device *sdp)
  2018. {
  2019. /*
  2020. * Although VPD inquiries can go to SCSI-2 type devices,
  2021. * some USB ones crash on receiving them, and the pages
  2022. * we currently ask for are for SPC-3 and beyond
  2023. */
  2024. if (sdp->scsi_level > SCSI_SPC_2)
  2025. return 1;
  2026. return 0;
  2027. }
  2028. /**
  2029. * sd_revalidate_disk - called the first time a new disk is seen,
  2030. * performs disk spin up, read_capacity, etc.
  2031. * @disk: struct gendisk we care about
  2032. **/
  2033. static int sd_revalidate_disk(struct gendisk *disk)
  2034. {
  2035. struct scsi_disk *sdkp = scsi_disk(disk);
  2036. struct scsi_device *sdp = sdkp->device;
  2037. unsigned char *buffer;
  2038. unsigned flush = 0;
  2039. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
  2040. "sd_revalidate_disk\n"));
  2041. /*
  2042. * If the device is offline, don't try and read capacity or any
  2043. * of the other niceties.
  2044. */
  2045. if (!scsi_device_online(sdp))
  2046. goto out;
  2047. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
  2048. if (!buffer) {
  2049. sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  2050. "allocation failure.\n");
  2051. goto out;
  2052. }
  2053. sd_spinup_disk(sdkp);
  2054. /*
  2055. * Without media there is no reason to ask; moreover, some devices
  2056. * react badly if we do.
  2057. */
  2058. if (sdkp->media_present) {
  2059. sd_read_capacity(sdkp, buffer);
  2060. if (sd_try_extended_inquiry(sdp)) {
  2061. sd_read_block_provisioning(sdkp);
  2062. sd_read_block_limits(sdkp);
  2063. sd_read_block_characteristics(sdkp);
  2064. }
  2065. sd_read_write_protect_flag(sdkp, buffer);
  2066. sd_read_cache_type(sdkp, buffer);
  2067. sd_read_app_tag_own(sdkp, buffer);
  2068. }
  2069. sdkp->first_scan = 0;
  2070. /*
  2071. * We now have all cache related info, determine how we deal
  2072. * with flush requests.
  2073. */
  2074. if (sdkp->WCE) {
  2075. flush |= REQ_FLUSH;
  2076. if (sdkp->DPOFUA)
  2077. flush |= REQ_FUA;
  2078. }
  2079. blk_queue_flush(sdkp->disk->queue, flush);
  2080. set_capacity(disk, sdkp->capacity);
  2081. kfree(buffer);
  2082. out:
  2083. return 0;
  2084. }
  2085. /**
  2086. * sd_unlock_native_capacity - unlock native capacity
  2087. * @disk: struct gendisk to set capacity for
  2088. *
  2089. * Block layer calls this function if it detects that partitions
  2090. * on @disk reach beyond the end of the device. If the SCSI host
  2091. * implements ->unlock_native_capacity() method, it's invoked to
  2092. * give it a chance to adjust the device capacity.
  2093. *
  2094. * CONTEXT:
  2095. * Defined by block layer. Might sleep.
  2096. */
  2097. static void sd_unlock_native_capacity(struct gendisk *disk)
  2098. {
  2099. struct scsi_device *sdev = scsi_disk(disk)->device;
  2100. if (sdev->host->hostt->unlock_native_capacity)
  2101. sdev->host->hostt->unlock_native_capacity(sdev);
  2102. }
  2103. /**
  2104. * sd_format_disk_name - format disk name
  2105. * @prefix: name prefix - ie. "sd" for SCSI disks
  2106. * @index: index of the disk to format name for
  2107. * @buf: output buffer
  2108. * @buflen: length of the output buffer
  2109. *
  2110. * SCSI disk names starts at sda. The 26th device is sdz and the
  2111. * 27th is sdaa. The last one for two lettered suffix is sdzz
  2112. * which is followed by sdaaa.
  2113. *
  2114. * This is basically 26 base counting with one extra 'nil' entry
  2115. * at the beginning from the second digit on and can be
  2116. * determined using similar method as 26 base conversion with the
  2117. * index shifted -1 after each digit is computed.
  2118. *
  2119. * CONTEXT:
  2120. * Don't care.
  2121. *
  2122. * RETURNS:
  2123. * 0 on success, -errno on failure.
  2124. */
  2125. static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  2126. {
  2127. const int base = 'z' - 'a' + 1;
  2128. char *begin = buf + strlen(prefix);
  2129. char *end = buf + buflen;
  2130. char *p;
  2131. int unit;
  2132. p = end - 1;
  2133. *p = '\0';
  2134. unit = base;
  2135. do {
  2136. if (p == begin)
  2137. return -EINVAL;
  2138. *--p = 'a' + (index % unit);
  2139. index = (index / unit) - 1;
  2140. } while (index >= 0);
  2141. memmove(begin, p, end - p);
  2142. memcpy(buf, prefix, strlen(prefix));
  2143. return 0;
  2144. }
  2145. /*
  2146. * The asynchronous part of sd_probe
  2147. */
  2148. static void sd_probe_async(void *data, async_cookie_t cookie)
  2149. {
  2150. struct scsi_disk *sdkp = data;
  2151. struct scsi_device *sdp;
  2152. struct gendisk *gd;
  2153. u32 index;
  2154. struct device *dev;
  2155. sdp = sdkp->device;
  2156. gd = sdkp->disk;
  2157. index = sdkp->index;
  2158. dev = &sdp->sdev_gendev;
  2159. gd->major = sd_major((index & 0xf0) >> 4);
  2160. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  2161. gd->minors = SD_MINORS;
  2162. gd->fops = &sd_fops;
  2163. gd->private_data = &sdkp->driver;
  2164. gd->queue = sdkp->device->request_queue;
  2165. /* defaults, until the device tells us otherwise */
  2166. sdp->sector_size = 512;
  2167. sdkp->capacity = 0;
  2168. sdkp->media_present = 1;
  2169. sdkp->write_prot = 0;
  2170. sdkp->WCE = 0;
  2171. sdkp->RCD = 0;
  2172. sdkp->ATO = 0;
  2173. sdkp->first_scan = 1;
  2174. sd_revalidate_disk(gd);
  2175. blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
  2176. blk_queue_unprep_rq(sdp->request_queue, sd_unprep_fn);
  2177. gd->driverfs_dev = &sdp->sdev_gendev;
  2178. gd->flags = GENHD_FL_EXT_DEVT;
  2179. if (sdp->removable) {
  2180. gd->flags |= GENHD_FL_REMOVABLE;
  2181. gd->events |= DISK_EVENT_MEDIA_CHANGE;
  2182. }
  2183. add_disk(gd);
  2184. sd_dif_config_host(sdkp);
  2185. sd_revalidate_disk(gd);
  2186. sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  2187. sdp->removable ? "removable " : "");
  2188. scsi_autopm_put_device(sdp);
  2189. put_device(&sdkp->dev);
  2190. }
  2191. /**
  2192. * sd_probe - called during driver initialization and whenever a
  2193. * new scsi device is attached to the system. It is called once
  2194. * for each scsi device (not just disks) present.
  2195. * @dev: pointer to device object
  2196. *
  2197. * Returns 0 if successful (or not interested in this scsi device
  2198. * (e.g. scanner)); 1 when there is an error.
  2199. *
  2200. * Note: this function is invoked from the scsi mid-level.
  2201. * This function sets up the mapping between a given
  2202. * <host,channel,id,lun> (found in sdp) and new device name
  2203. * (e.g. /dev/sda). More precisely it is the block device major
  2204. * and minor number that is chosen here.
  2205. *
  2206. * Assume sd_attach is not re-entrant (for time being)
  2207. * Also think about sd_attach() and sd_remove() running coincidentally.
  2208. **/
  2209. static int sd_probe(struct device *dev)
  2210. {
  2211. struct scsi_device *sdp = to_scsi_device(dev);
  2212. struct scsi_disk *sdkp;
  2213. struct gendisk *gd;
  2214. int index;
  2215. int error;
  2216. error = -ENODEV;
  2217. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  2218. goto out;
  2219. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  2220. "sd_attach\n"));
  2221. error = -ENOMEM;
  2222. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  2223. if (!sdkp)
  2224. goto out;
  2225. gd = alloc_disk(SD_MINORS);
  2226. if (!gd)
  2227. goto out_free;
  2228. do {
  2229. if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
  2230. goto out_put;
  2231. spin_lock(&sd_index_lock);
  2232. error = ida_get_new(&sd_index_ida, &index);
  2233. spin_unlock(&sd_index_lock);
  2234. } while (error == -EAGAIN);
  2235. if (error) {
  2236. sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
  2237. goto out_put;
  2238. }
  2239. error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  2240. if (error) {
  2241. sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
  2242. goto out_free_index;
  2243. }
  2244. sdkp->device = sdp;
  2245. sdkp->driver = &sd_template;
  2246. sdkp->disk = gd;
  2247. sdkp->index = index;
  2248. atomic_set(&sdkp->openers, 0);
  2249. if (!sdp->request_queue->rq_timeout) {
  2250. if (sdp->type != TYPE_MOD)
  2251. blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  2252. else
  2253. blk_queue_rq_timeout(sdp->request_queue,
  2254. SD_MOD_TIMEOUT);
  2255. }
  2256. device_initialize(&sdkp->dev);
  2257. sdkp->dev.parent = dev;
  2258. sdkp->dev.class = &sd_disk_class;
  2259. dev_set_name(&sdkp->dev, dev_name(dev));
  2260. if (device_add(&sdkp->dev))
  2261. goto out_free_index;
  2262. get_device(dev);
  2263. dev_set_drvdata(dev, sdkp);
  2264. get_device(&sdkp->dev); /* prevent release before async_schedule */
  2265. async_schedule(sd_probe_async, sdkp);
  2266. return 0;
  2267. out_free_index:
  2268. spin_lock(&sd_index_lock);
  2269. ida_remove(&sd_index_ida, index);
  2270. spin_unlock(&sd_index_lock);
  2271. out_put:
  2272. put_disk(gd);
  2273. out_free:
  2274. kfree(sdkp);
  2275. out:
  2276. return error;
  2277. }
  2278. /**
  2279. * sd_remove - called whenever a scsi disk (previously recognized by
  2280. * sd_probe) is detached from the system. It is called (potentially
  2281. * multiple times) during sd module unload.
  2282. * @sdp: pointer to mid level scsi device object
  2283. *
  2284. * Note: this function is invoked from the scsi mid-level.
  2285. * This function potentially frees up a device name (e.g. /dev/sdc)
  2286. * that could be re-used by a subsequent sd_probe().
  2287. * This function is not called when the built-in sd driver is "exit-ed".
  2288. **/
  2289. static int sd_remove(struct device *dev)
  2290. {
  2291. struct scsi_disk *sdkp;
  2292. sdkp = dev_get_drvdata(dev);
  2293. scsi_autopm_get_device(sdkp->device);
  2294. async_synchronize_full();
  2295. blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
  2296. blk_queue_unprep_rq(sdkp->device->request_queue, NULL);
  2297. device_del(&sdkp->dev);
  2298. del_gendisk(sdkp->disk);
  2299. sd_shutdown(dev);
  2300. mutex_lock(&sd_ref_mutex);
  2301. dev_set_drvdata(dev, NULL);
  2302. put_device(&sdkp->dev);
  2303. mutex_unlock(&sd_ref_mutex);
  2304. return 0;
  2305. }
  2306. /**
  2307. * scsi_disk_release - Called to free the scsi_disk structure
  2308. * @dev: pointer to embedded class device
  2309. *
  2310. * sd_ref_mutex must be held entering this routine. Because it is
  2311. * called on last put, you should always use the scsi_disk_get()
  2312. * scsi_disk_put() helpers which manipulate the semaphore directly
  2313. * and never do a direct put_device.
  2314. **/
  2315. static void scsi_disk_release(struct device *dev)
  2316. {
  2317. struct scsi_disk *sdkp = to_scsi_disk(dev);
  2318. struct gendisk *disk = sdkp->disk;
  2319. spin_lock(&sd_index_lock);
  2320. ida_remove(&sd_index_ida, sdkp->index);
  2321. spin_unlock(&sd_index_lock);
  2322. disk->private_data = NULL;
  2323. put_disk(disk);
  2324. put_device(&sdkp->device->sdev_gendev);
  2325. kfree(sdkp);
  2326. }
  2327. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  2328. {
  2329. unsigned char cmd[6] = { START_STOP }; /* START_VALID */
  2330. struct scsi_sense_hdr sshdr;
  2331. struct scsi_device *sdp = sdkp->device;
  2332. int res;
  2333. if (start)
  2334. cmd[4] |= 1; /* START */
  2335. if (sdp->start_stop_pwr_cond)
  2336. cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
  2337. if (!scsi_device_online(sdp))
  2338. return -ENODEV;
  2339. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  2340. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  2341. if (res) {
  2342. sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
  2343. sd_print_result(sdkp, res);
  2344. if (driver_byte(res) & DRIVER_SENSE)
  2345. sd_print_sense_hdr(sdkp, &sshdr);
  2346. }
  2347. return res;
  2348. }
  2349. /*
  2350. * Send a SYNCHRONIZE CACHE instruction down to the device through
  2351. * the normal SCSI command structure. Wait for the command to
  2352. * complete.
  2353. */
  2354. static void sd_shutdown(struct device *dev)
  2355. {
  2356. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2357. if (!sdkp)
  2358. return; /* this can happen */
  2359. if (pm_runtime_suspended(dev))
  2360. goto exit;
  2361. if (sdkp->WCE) {
  2362. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2363. sd_sync_cache(sdkp);
  2364. }
  2365. if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  2366. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2367. sd_start_stop_device(sdkp, 0);
  2368. }
  2369. exit:
  2370. scsi_disk_put(sdkp);
  2371. }
  2372. static int sd_suspend(struct device *dev, pm_message_t mesg)
  2373. {
  2374. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2375. int ret = 0;
  2376. if (!sdkp)
  2377. return 0; /* this can happen */
  2378. if (sdkp->WCE) {
  2379. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2380. ret = sd_sync_cache(sdkp);
  2381. if (ret)
  2382. goto done;
  2383. }
  2384. if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
  2385. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2386. ret = sd_start_stop_device(sdkp, 0);
  2387. }
  2388. done:
  2389. scsi_disk_put(sdkp);
  2390. return ret;
  2391. }
  2392. static int sd_resume(struct device *dev)
  2393. {
  2394. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2395. int ret = 0;
  2396. if (!sdkp->device->manage_start_stop)
  2397. goto done;
  2398. sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  2399. ret = sd_start_stop_device(sdkp, 1);
  2400. done:
  2401. scsi_disk_put(sdkp);
  2402. return ret;
  2403. }
  2404. /**
  2405. * init_sd - entry point for this driver (both when built in or when
  2406. * a module).
  2407. *
  2408. * Note: this function registers this driver with the scsi mid-level.
  2409. **/
  2410. static int __init init_sd(void)
  2411. {
  2412. int majors = 0, i, err;
  2413. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  2414. for (i = 0; i < SD_MAJORS; i++)
  2415. if (register_blkdev(sd_major(i), "sd") == 0)
  2416. majors++;
  2417. if (!majors)
  2418. return -ENODEV;
  2419. err = class_register(&sd_disk_class);
  2420. if (err)
  2421. goto err_out;
  2422. err = scsi_register_driver(&sd_template.gendrv);
  2423. if (err)
  2424. goto err_out_class;
  2425. sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  2426. 0, 0, NULL);
  2427. if (!sd_cdb_cache) {
  2428. printk(KERN_ERR "sd: can't init extended cdb cache\n");
  2429. goto err_out_class;
  2430. }
  2431. sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  2432. if (!sd_cdb_pool) {
  2433. printk(KERN_ERR "sd: can't init extended cdb pool\n");
  2434. goto err_out_cache;
  2435. }
  2436. return 0;
  2437. err_out_cache:
  2438. kmem_cache_destroy(sd_cdb_cache);
  2439. err_out_class:
  2440. class_unregister(&sd_disk_class);
  2441. err_out:
  2442. for (i = 0; i < SD_MAJORS; i++)
  2443. unregister_blkdev(sd_major(i), "sd");
  2444. return err;
  2445. }
  2446. /**
  2447. * exit_sd - exit point for this driver (when it is a module).
  2448. *
  2449. * Note: this function unregisters this driver from the scsi mid-level.
  2450. **/
  2451. static void __exit exit_sd(void)
  2452. {
  2453. int i;
  2454. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  2455. mempool_destroy(sd_cdb_pool);
  2456. kmem_cache_destroy(sd_cdb_cache);
  2457. scsi_unregister_driver(&sd_template.gendrv);
  2458. class_unregister(&sd_disk_class);
  2459. for (i = 0; i < SD_MAJORS; i++)
  2460. unregister_blkdev(sd_major(i), "sd");
  2461. }
  2462. module_init(init_sd);
  2463. module_exit(exit_sd);
  2464. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  2465. struct scsi_sense_hdr *sshdr)
  2466. {
  2467. sd_printk(KERN_INFO, sdkp, " ");
  2468. scsi_show_sense_hdr(sshdr);
  2469. sd_printk(KERN_INFO, sdkp, " ");
  2470. scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
  2471. }
  2472. static void sd_print_result(struct scsi_disk *sdkp, int result)
  2473. {
  2474. sd_printk(KERN_INFO, sdkp, " ");
  2475. scsi_show_result(result);
  2476. }