sd.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476
  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 <linux/pr.h>
  54. #include <asm/uaccess.h>
  55. #include <asm/unaligned.h>
  56. #include <scsi/scsi.h>
  57. #include <scsi/scsi_cmnd.h>
  58. #include <scsi/scsi_dbg.h>
  59. #include <scsi/scsi_device.h>
  60. #include <scsi/scsi_driver.h>
  61. #include <scsi/scsi_eh.h>
  62. #include <scsi/scsi_host.h>
  63. #include <scsi/scsi_ioctl.h>
  64. #include <scsi/scsicam.h>
  65. #include "sd.h"
  66. #include "scsi_priv.h"
  67. #include "scsi_logging.h"
  68. MODULE_AUTHOR("Eric Youngdale");
  69. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  70. MODULE_LICENSE("GPL");
  71. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  80. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  81. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  82. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  83. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  84. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  85. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  86. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  87. MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
  88. MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
  89. MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
  90. #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
  91. #define SD_MINORS 16
  92. #else
  93. #define SD_MINORS 0
  94. #endif
  95. static void sd_config_discard(struct scsi_disk *, unsigned int);
  96. static void sd_config_write_same(struct scsi_disk *);
  97. static int sd_revalidate_disk(struct gendisk *);
  98. static void sd_unlock_native_capacity(struct gendisk *disk);
  99. static int sd_probe(struct device *);
  100. static int sd_remove(struct device *);
  101. static void sd_shutdown(struct device *);
  102. static int sd_suspend_system(struct device *);
  103. static int sd_suspend_runtime(struct device *);
  104. static int sd_resume(struct device *);
  105. static void sd_rescan(struct device *);
  106. static int sd_init_command(struct scsi_cmnd *SCpnt);
  107. static void sd_uninit_command(struct scsi_cmnd *SCpnt);
  108. static int sd_done(struct scsi_cmnd *);
  109. static int sd_eh_action(struct scsi_cmnd *, int);
  110. static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
  111. static void scsi_disk_release(struct device *cdev);
  112. static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
  113. static void sd_print_result(const struct scsi_disk *, const char *, int);
  114. static DEFINE_SPINLOCK(sd_index_lock);
  115. static DEFINE_IDA(sd_index_ida);
  116. /* This semaphore is used to mediate the 0->1 reference get in the
  117. * face of object destruction (i.e. we can't allow a get on an
  118. * object after last put) */
  119. static DEFINE_MUTEX(sd_ref_mutex);
  120. static struct kmem_cache *sd_cdb_cache;
  121. static mempool_t *sd_cdb_pool;
  122. static const char *sd_cache_types[] = {
  123. "write through", "none", "write back",
  124. "write back, no read (daft)"
  125. };
  126. static void sd_set_flush_flag(struct scsi_disk *sdkp)
  127. {
  128. unsigned flush = 0;
  129. if (sdkp->WCE) {
  130. flush |= REQ_FLUSH;
  131. if (sdkp->DPOFUA)
  132. flush |= REQ_FUA;
  133. }
  134. blk_queue_flush(sdkp->disk->queue, flush);
  135. }
  136. static ssize_t
  137. cache_type_store(struct device *dev, struct device_attribute *attr,
  138. const char *buf, size_t count)
  139. {
  140. int i, ct = -1, rcd, wce, sp;
  141. struct scsi_disk *sdkp = to_scsi_disk(dev);
  142. struct scsi_device *sdp = sdkp->device;
  143. char buffer[64];
  144. char *buffer_data;
  145. struct scsi_mode_data data;
  146. struct scsi_sense_hdr sshdr;
  147. static const char temp[] = "temporary ";
  148. int len;
  149. if (sdp->type != TYPE_DISK)
  150. /* no cache control on RBC devices; theoretically they
  151. * can do it, but there's probably so many exceptions
  152. * it's not worth the risk */
  153. return -EINVAL;
  154. if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
  155. buf += sizeof(temp) - 1;
  156. sdkp->cache_override = 1;
  157. } else {
  158. sdkp->cache_override = 0;
  159. }
  160. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  161. len = strlen(sd_cache_types[i]);
  162. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  163. buf[len] == '\n') {
  164. ct = i;
  165. break;
  166. }
  167. }
  168. if (ct < 0)
  169. return -EINVAL;
  170. rcd = ct & 0x01 ? 1 : 0;
  171. wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0;
  172. if (sdkp->cache_override) {
  173. sdkp->WCE = wce;
  174. sdkp->RCD = rcd;
  175. sd_set_flush_flag(sdkp);
  176. return count;
  177. }
  178. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  179. SD_MAX_RETRIES, &data, NULL))
  180. return -EINVAL;
  181. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  182. data.block_descriptor_length);
  183. buffer_data = buffer + data.header_length +
  184. data.block_descriptor_length;
  185. buffer_data[2] &= ~0x05;
  186. buffer_data[2] |= wce << 2 | rcd;
  187. sp = buffer_data[0] & 0x80 ? 1 : 0;
  188. buffer_data[0] &= ~0x80;
  189. /*
  190. * Ensure WP, DPOFUA, and RESERVED fields are cleared in
  191. * received mode parameter buffer before doing MODE SELECT.
  192. */
  193. data.device_specific = 0;
  194. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  195. SD_MAX_RETRIES, &data, &sshdr)) {
  196. if (scsi_sense_valid(&sshdr))
  197. sd_print_sense_hdr(sdkp, &sshdr);
  198. return -EINVAL;
  199. }
  200. revalidate_disk(sdkp->disk);
  201. return count;
  202. }
  203. static ssize_t
  204. manage_start_stop_show(struct device *dev, struct device_attribute *attr,
  205. char *buf)
  206. {
  207. struct scsi_disk *sdkp = to_scsi_disk(dev);
  208. struct scsi_device *sdp = sdkp->device;
  209. return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
  210. }
  211. static ssize_t
  212. manage_start_stop_store(struct device *dev, struct device_attribute *attr,
  213. const char *buf, size_t count)
  214. {
  215. struct scsi_disk *sdkp = to_scsi_disk(dev);
  216. struct scsi_device *sdp = sdkp->device;
  217. bool v;
  218. if (!capable(CAP_SYS_ADMIN))
  219. return -EACCES;
  220. if (kstrtobool(buf, &v))
  221. return -EINVAL;
  222. sdp->manage_start_stop = v;
  223. return count;
  224. }
  225. static DEVICE_ATTR_RW(manage_start_stop);
  226. static ssize_t
  227. allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf)
  228. {
  229. struct scsi_disk *sdkp = to_scsi_disk(dev);
  230. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  231. }
  232. static ssize_t
  233. allow_restart_store(struct device *dev, struct device_attribute *attr,
  234. const char *buf, size_t count)
  235. {
  236. bool v;
  237. struct scsi_disk *sdkp = to_scsi_disk(dev);
  238. struct scsi_device *sdp = sdkp->device;
  239. if (!capable(CAP_SYS_ADMIN))
  240. return -EACCES;
  241. if (sdp->type != TYPE_DISK)
  242. return -EINVAL;
  243. if (kstrtobool(buf, &v))
  244. return -EINVAL;
  245. sdp->allow_restart = v;
  246. return count;
  247. }
  248. static DEVICE_ATTR_RW(allow_restart);
  249. static ssize_t
  250. cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  251. {
  252. struct scsi_disk *sdkp = to_scsi_disk(dev);
  253. int ct = sdkp->RCD + 2*sdkp->WCE;
  254. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  255. }
  256. static DEVICE_ATTR_RW(cache_type);
  257. static ssize_t
  258. FUA_show(struct device *dev, struct device_attribute *attr, char *buf)
  259. {
  260. struct scsi_disk *sdkp = to_scsi_disk(dev);
  261. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  262. }
  263. static DEVICE_ATTR_RO(FUA);
  264. static ssize_t
  265. protection_type_show(struct device *dev, struct device_attribute *attr,
  266. char *buf)
  267. {
  268. struct scsi_disk *sdkp = to_scsi_disk(dev);
  269. return snprintf(buf, 20, "%u\n", sdkp->protection_type);
  270. }
  271. static ssize_t
  272. protection_type_store(struct device *dev, struct device_attribute *attr,
  273. const char *buf, size_t count)
  274. {
  275. struct scsi_disk *sdkp = to_scsi_disk(dev);
  276. unsigned int val;
  277. int err;
  278. if (!capable(CAP_SYS_ADMIN))
  279. return -EACCES;
  280. err = kstrtouint(buf, 10, &val);
  281. if (err)
  282. return err;
  283. if (val >= 0 && val <= SD_DIF_TYPE3_PROTECTION)
  284. sdkp->protection_type = val;
  285. return count;
  286. }
  287. static DEVICE_ATTR_RW(protection_type);
  288. static ssize_t
  289. protection_mode_show(struct device *dev, struct device_attribute *attr,
  290. char *buf)
  291. {
  292. struct scsi_disk *sdkp = to_scsi_disk(dev);
  293. struct scsi_device *sdp = sdkp->device;
  294. unsigned int dif, dix;
  295. dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  296. dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
  297. if (!dix && scsi_host_dix_capable(sdp->host, SD_DIF_TYPE0_PROTECTION)) {
  298. dif = 0;
  299. dix = 1;
  300. }
  301. if (!dif && !dix)
  302. return snprintf(buf, 20, "none\n");
  303. return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
  304. }
  305. static DEVICE_ATTR_RO(protection_mode);
  306. static ssize_t
  307. app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf)
  308. {
  309. struct scsi_disk *sdkp = to_scsi_disk(dev);
  310. return snprintf(buf, 20, "%u\n", sdkp->ATO);
  311. }
  312. static DEVICE_ATTR_RO(app_tag_own);
  313. static ssize_t
  314. thin_provisioning_show(struct device *dev, struct device_attribute *attr,
  315. char *buf)
  316. {
  317. struct scsi_disk *sdkp = to_scsi_disk(dev);
  318. return snprintf(buf, 20, "%u\n", sdkp->lbpme);
  319. }
  320. static DEVICE_ATTR_RO(thin_provisioning);
  321. static const char *lbp_mode[] = {
  322. [SD_LBP_FULL] = "full",
  323. [SD_LBP_UNMAP] = "unmap",
  324. [SD_LBP_WS16] = "writesame_16",
  325. [SD_LBP_WS10] = "writesame_10",
  326. [SD_LBP_ZERO] = "writesame_zero",
  327. [SD_LBP_DISABLE] = "disabled",
  328. };
  329. static ssize_t
  330. provisioning_mode_show(struct device *dev, struct device_attribute *attr,
  331. char *buf)
  332. {
  333. struct scsi_disk *sdkp = to_scsi_disk(dev);
  334. return snprintf(buf, 20, "%s\n", lbp_mode[sdkp->provisioning_mode]);
  335. }
  336. static ssize_t
  337. provisioning_mode_store(struct device *dev, struct device_attribute *attr,
  338. const char *buf, size_t count)
  339. {
  340. struct scsi_disk *sdkp = to_scsi_disk(dev);
  341. struct scsi_device *sdp = sdkp->device;
  342. if (!capable(CAP_SYS_ADMIN))
  343. return -EACCES;
  344. if (sdp->type != TYPE_DISK)
  345. return -EINVAL;
  346. if (!strncmp(buf, lbp_mode[SD_LBP_UNMAP], 20))
  347. sd_config_discard(sdkp, SD_LBP_UNMAP);
  348. else if (!strncmp(buf, lbp_mode[SD_LBP_WS16], 20))
  349. sd_config_discard(sdkp, SD_LBP_WS16);
  350. else if (!strncmp(buf, lbp_mode[SD_LBP_WS10], 20))
  351. sd_config_discard(sdkp, SD_LBP_WS10);
  352. else if (!strncmp(buf, lbp_mode[SD_LBP_ZERO], 20))
  353. sd_config_discard(sdkp, SD_LBP_ZERO);
  354. else if (!strncmp(buf, lbp_mode[SD_LBP_DISABLE], 20))
  355. sd_config_discard(sdkp, SD_LBP_DISABLE);
  356. else
  357. return -EINVAL;
  358. return count;
  359. }
  360. static DEVICE_ATTR_RW(provisioning_mode);
  361. static ssize_t
  362. max_medium_access_timeouts_show(struct device *dev,
  363. struct device_attribute *attr, char *buf)
  364. {
  365. struct scsi_disk *sdkp = to_scsi_disk(dev);
  366. return snprintf(buf, 20, "%u\n", sdkp->max_medium_access_timeouts);
  367. }
  368. static ssize_t
  369. max_medium_access_timeouts_store(struct device *dev,
  370. struct device_attribute *attr, const char *buf,
  371. size_t count)
  372. {
  373. struct scsi_disk *sdkp = to_scsi_disk(dev);
  374. int err;
  375. if (!capable(CAP_SYS_ADMIN))
  376. return -EACCES;
  377. err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts);
  378. return err ? err : count;
  379. }
  380. static DEVICE_ATTR_RW(max_medium_access_timeouts);
  381. static ssize_t
  382. max_write_same_blocks_show(struct device *dev, struct device_attribute *attr,
  383. char *buf)
  384. {
  385. struct scsi_disk *sdkp = to_scsi_disk(dev);
  386. return snprintf(buf, 20, "%u\n", sdkp->max_ws_blocks);
  387. }
  388. static ssize_t
  389. max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
  390. const char *buf, size_t count)
  391. {
  392. struct scsi_disk *sdkp = to_scsi_disk(dev);
  393. struct scsi_device *sdp = sdkp->device;
  394. unsigned long max;
  395. int err;
  396. if (!capable(CAP_SYS_ADMIN))
  397. return -EACCES;
  398. if (sdp->type != TYPE_DISK)
  399. return -EINVAL;
  400. err = kstrtoul(buf, 10, &max);
  401. if (err)
  402. return err;
  403. if (max == 0)
  404. sdp->no_write_same = 1;
  405. else if (max <= SD_MAX_WS16_BLOCKS) {
  406. sdp->no_write_same = 0;
  407. sdkp->max_ws_blocks = max;
  408. }
  409. sd_config_write_same(sdkp);
  410. return count;
  411. }
  412. static DEVICE_ATTR_RW(max_write_same_blocks);
  413. static struct attribute *sd_disk_attrs[] = {
  414. &dev_attr_cache_type.attr,
  415. &dev_attr_FUA.attr,
  416. &dev_attr_allow_restart.attr,
  417. &dev_attr_manage_start_stop.attr,
  418. &dev_attr_protection_type.attr,
  419. &dev_attr_protection_mode.attr,
  420. &dev_attr_app_tag_own.attr,
  421. &dev_attr_thin_provisioning.attr,
  422. &dev_attr_provisioning_mode.attr,
  423. &dev_attr_max_write_same_blocks.attr,
  424. &dev_attr_max_medium_access_timeouts.attr,
  425. NULL,
  426. };
  427. ATTRIBUTE_GROUPS(sd_disk);
  428. static struct class sd_disk_class = {
  429. .name = "scsi_disk",
  430. .owner = THIS_MODULE,
  431. .dev_release = scsi_disk_release,
  432. .dev_groups = sd_disk_groups,
  433. };
  434. static const struct dev_pm_ops sd_pm_ops = {
  435. .suspend = sd_suspend_system,
  436. .resume = sd_resume,
  437. .poweroff = sd_suspend_system,
  438. .restore = sd_resume,
  439. .runtime_suspend = sd_suspend_runtime,
  440. .runtime_resume = sd_resume,
  441. };
  442. static struct scsi_driver sd_template = {
  443. .gendrv = {
  444. .name = "sd",
  445. .owner = THIS_MODULE,
  446. .probe = sd_probe,
  447. .remove = sd_remove,
  448. .shutdown = sd_shutdown,
  449. .pm = &sd_pm_ops,
  450. },
  451. .rescan = sd_rescan,
  452. .init_command = sd_init_command,
  453. .uninit_command = sd_uninit_command,
  454. .done = sd_done,
  455. .eh_action = sd_eh_action,
  456. };
  457. /*
  458. * Dummy kobj_map->probe function.
  459. * The default ->probe function will call modprobe, which is
  460. * pointless as this module is already loaded.
  461. */
  462. static struct kobject *sd_default_probe(dev_t devt, int *partno, void *data)
  463. {
  464. return NULL;
  465. }
  466. /*
  467. * Device no to disk mapping:
  468. *
  469. * major disc2 disc p1
  470. * |............|.............|....|....| <- dev_t
  471. * 31 20 19 8 7 4 3 0
  472. *
  473. * Inside a major, we have 16k disks, however mapped non-
  474. * contiguously. The first 16 disks are for major0, the next
  475. * ones with major1, ... Disk 256 is for major0 again, disk 272
  476. * for major1, ...
  477. * As we stay compatible with our numbering scheme, we can reuse
  478. * the well-know SCSI majors 8, 65--71, 136--143.
  479. */
  480. static int sd_major(int major_idx)
  481. {
  482. switch (major_idx) {
  483. case 0:
  484. return SCSI_DISK0_MAJOR;
  485. case 1 ... 7:
  486. return SCSI_DISK1_MAJOR + major_idx - 1;
  487. case 8 ... 15:
  488. return SCSI_DISK8_MAJOR + major_idx - 8;
  489. default:
  490. BUG();
  491. return 0; /* shut up gcc */
  492. }
  493. }
  494. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  495. {
  496. struct scsi_disk *sdkp = NULL;
  497. mutex_lock(&sd_ref_mutex);
  498. if (disk->private_data) {
  499. sdkp = scsi_disk(disk);
  500. if (scsi_device_get(sdkp->device) == 0)
  501. get_device(&sdkp->dev);
  502. else
  503. sdkp = NULL;
  504. }
  505. mutex_unlock(&sd_ref_mutex);
  506. return sdkp;
  507. }
  508. static void scsi_disk_put(struct scsi_disk *sdkp)
  509. {
  510. struct scsi_device *sdev = sdkp->device;
  511. mutex_lock(&sd_ref_mutex);
  512. put_device(&sdkp->dev);
  513. scsi_device_put(sdev);
  514. mutex_unlock(&sd_ref_mutex);
  515. }
  516. static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
  517. unsigned int dix, unsigned int dif)
  518. {
  519. struct bio *bio = scmd->request->bio;
  520. unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
  521. unsigned int protect = 0;
  522. if (dix) { /* DIX Type 0, 1, 2, 3 */
  523. if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
  524. scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
  525. if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
  526. scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
  527. }
  528. if (dif != SD_DIF_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */
  529. scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;
  530. if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
  531. scmd->prot_flags |= SCSI_PROT_REF_CHECK;
  532. }
  533. if (dif) { /* DIX/DIF Type 1, 2, 3 */
  534. scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;
  535. if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
  536. protect = 3 << 5; /* Disable target PI checking */
  537. else
  538. protect = 1 << 5; /* Enable target PI checking */
  539. }
  540. scsi_set_prot_op(scmd, prot_op);
  541. scsi_set_prot_type(scmd, dif);
  542. scmd->prot_flags &= sd_prot_flag_mask(prot_op);
  543. return protect;
  544. }
  545. static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
  546. {
  547. struct request_queue *q = sdkp->disk->queue;
  548. unsigned int logical_block_size = sdkp->device->sector_size;
  549. unsigned int max_blocks = 0;
  550. q->limits.discard_zeroes_data = 0;
  551. /*
  552. * When LBPRZ is reported, discard alignment and granularity
  553. * must be fixed to the logical block size. Otherwise the block
  554. * layer will drop misaligned portions of the request which can
  555. * lead to data corruption. If LBPRZ is not set, we honor the
  556. * device preference.
  557. */
  558. if (sdkp->lbprz) {
  559. q->limits.discard_alignment = 0;
  560. q->limits.discard_granularity = logical_block_size;
  561. } else {
  562. q->limits.discard_alignment = sdkp->unmap_alignment *
  563. logical_block_size;
  564. q->limits.discard_granularity =
  565. max(sdkp->physical_block_size,
  566. sdkp->unmap_granularity * logical_block_size);
  567. }
  568. sdkp->provisioning_mode = mode;
  569. switch (mode) {
  570. case SD_LBP_DISABLE:
  571. blk_queue_max_discard_sectors(q, 0);
  572. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  573. return;
  574. case SD_LBP_UNMAP:
  575. max_blocks = min_not_zero(sdkp->max_unmap_blocks,
  576. (u32)SD_MAX_WS16_BLOCKS);
  577. break;
  578. case SD_LBP_WS16:
  579. max_blocks = min_not_zero(sdkp->max_ws_blocks,
  580. (u32)SD_MAX_WS16_BLOCKS);
  581. q->limits.discard_zeroes_data = sdkp->lbprz;
  582. break;
  583. case SD_LBP_WS10:
  584. max_blocks = min_not_zero(sdkp->max_ws_blocks,
  585. (u32)SD_MAX_WS10_BLOCKS);
  586. q->limits.discard_zeroes_data = sdkp->lbprz;
  587. break;
  588. case SD_LBP_ZERO:
  589. max_blocks = min_not_zero(sdkp->max_ws_blocks,
  590. (u32)SD_MAX_WS10_BLOCKS);
  591. q->limits.discard_zeroes_data = 1;
  592. break;
  593. }
  594. blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
  595. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  596. }
  597. /**
  598. * sd_setup_discard_cmnd - unmap blocks on thinly provisioned device
  599. * @sdp: scsi device to operate one
  600. * @rq: Request to prepare
  601. *
  602. * Will issue either UNMAP or WRITE SAME(16) depending on preference
  603. * indicated by target device.
  604. **/
  605. static int sd_setup_discard_cmnd(struct scsi_cmnd *cmd)
  606. {
  607. struct request *rq = cmd->request;
  608. struct scsi_device *sdp = cmd->device;
  609. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  610. sector_t sector = blk_rq_pos(rq);
  611. unsigned int nr_sectors = blk_rq_sectors(rq);
  612. unsigned int nr_bytes = blk_rq_bytes(rq);
  613. unsigned int len;
  614. int ret;
  615. char *buf;
  616. struct page *page;
  617. sector >>= ilog2(sdp->sector_size) - 9;
  618. nr_sectors >>= ilog2(sdp->sector_size) - 9;
  619. page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
  620. if (!page)
  621. return BLKPREP_DEFER;
  622. switch (sdkp->provisioning_mode) {
  623. case SD_LBP_UNMAP:
  624. buf = page_address(page);
  625. cmd->cmd_len = 10;
  626. cmd->cmnd[0] = UNMAP;
  627. cmd->cmnd[8] = 24;
  628. put_unaligned_be16(6 + 16, &buf[0]);
  629. put_unaligned_be16(16, &buf[2]);
  630. put_unaligned_be64(sector, &buf[8]);
  631. put_unaligned_be32(nr_sectors, &buf[16]);
  632. len = 24;
  633. break;
  634. case SD_LBP_WS16:
  635. cmd->cmd_len = 16;
  636. cmd->cmnd[0] = WRITE_SAME_16;
  637. cmd->cmnd[1] = 0x8; /* UNMAP */
  638. put_unaligned_be64(sector, &cmd->cmnd[2]);
  639. put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
  640. len = sdkp->device->sector_size;
  641. break;
  642. case SD_LBP_WS10:
  643. case SD_LBP_ZERO:
  644. cmd->cmd_len = 10;
  645. cmd->cmnd[0] = WRITE_SAME;
  646. if (sdkp->provisioning_mode == SD_LBP_WS10)
  647. cmd->cmnd[1] = 0x8; /* UNMAP */
  648. put_unaligned_be32(sector, &cmd->cmnd[2]);
  649. put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
  650. len = sdkp->device->sector_size;
  651. break;
  652. default:
  653. ret = BLKPREP_KILL;
  654. goto out;
  655. }
  656. rq->completion_data = page;
  657. rq->timeout = SD_TIMEOUT;
  658. cmd->transfersize = len;
  659. cmd->allowed = SD_MAX_RETRIES;
  660. /*
  661. * Initially __data_len is set to the amount of data that needs to be
  662. * transferred to the target. This amount depends on whether WRITE SAME
  663. * or UNMAP is being used. After the scatterlist has been mapped by
  664. * scsi_init_io() we set __data_len to the size of the area to be
  665. * discarded on disk. This allows us to report completion on the full
  666. * amount of blocks described by the request.
  667. */
  668. blk_add_request_payload(rq, page, len);
  669. ret = scsi_init_io(cmd);
  670. rq->__data_len = nr_bytes;
  671. out:
  672. if (ret != BLKPREP_OK)
  673. __free_page(page);
  674. return ret;
  675. }
  676. static void sd_config_write_same(struct scsi_disk *sdkp)
  677. {
  678. struct request_queue *q = sdkp->disk->queue;
  679. unsigned int logical_block_size = sdkp->device->sector_size;
  680. if (sdkp->device->no_write_same) {
  681. sdkp->max_ws_blocks = 0;
  682. goto out;
  683. }
  684. /* Some devices can not handle block counts above 0xffff despite
  685. * supporting WRITE SAME(16). Consequently we default to 64k
  686. * blocks per I/O unless the device explicitly advertises a
  687. * bigger limit.
  688. */
  689. if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)
  690. sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
  691. (u32)SD_MAX_WS16_BLOCKS);
  692. else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes)
  693. sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
  694. (u32)SD_MAX_WS10_BLOCKS);
  695. else {
  696. sdkp->device->no_write_same = 1;
  697. sdkp->max_ws_blocks = 0;
  698. }
  699. out:
  700. blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks *
  701. (logical_block_size >> 9));
  702. }
  703. /**
  704. * sd_setup_write_same_cmnd - write the same data to multiple blocks
  705. * @cmd: command to prepare
  706. *
  707. * Will issue either WRITE SAME(10) or WRITE SAME(16) depending on
  708. * preference indicated by target device.
  709. **/
  710. static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
  711. {
  712. struct request *rq = cmd->request;
  713. struct scsi_device *sdp = cmd->device;
  714. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  715. struct bio *bio = rq->bio;
  716. sector_t sector = blk_rq_pos(rq);
  717. unsigned int nr_sectors = blk_rq_sectors(rq);
  718. unsigned int nr_bytes = blk_rq_bytes(rq);
  719. int ret;
  720. if (sdkp->device->no_write_same)
  721. return BLKPREP_KILL;
  722. BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
  723. sector >>= ilog2(sdp->sector_size) - 9;
  724. nr_sectors >>= ilog2(sdp->sector_size) - 9;
  725. rq->timeout = SD_WRITE_SAME_TIMEOUT;
  726. if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) {
  727. cmd->cmd_len = 16;
  728. cmd->cmnd[0] = WRITE_SAME_16;
  729. put_unaligned_be64(sector, &cmd->cmnd[2]);
  730. put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
  731. } else {
  732. cmd->cmd_len = 10;
  733. cmd->cmnd[0] = WRITE_SAME;
  734. put_unaligned_be32(sector, &cmd->cmnd[2]);
  735. put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
  736. }
  737. cmd->transfersize = sdp->sector_size;
  738. cmd->allowed = SD_MAX_RETRIES;
  739. /*
  740. * For WRITE_SAME the data transferred in the DATA IN buffer is
  741. * different from the amount of data actually written to the target.
  742. *
  743. * We set up __data_len to the amount of data transferred from the
  744. * DATA IN buffer so that blk_rq_map_sg set up the proper S/G list
  745. * to transfer a single sector of data first, but then reset it to
  746. * the amount of data to be written right after so that the I/O path
  747. * knows how much to actually write.
  748. */
  749. rq->__data_len = sdp->sector_size;
  750. ret = scsi_init_io(cmd);
  751. rq->__data_len = nr_bytes;
  752. return ret;
  753. }
  754. static int sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
  755. {
  756. struct request *rq = cmd->request;
  757. /* flush requests don't perform I/O, zero the S/G table */
  758. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  759. cmd->cmnd[0] = SYNCHRONIZE_CACHE;
  760. cmd->cmd_len = 10;
  761. cmd->transfersize = 0;
  762. cmd->allowed = SD_MAX_RETRIES;
  763. rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER;
  764. return BLKPREP_OK;
  765. }
  766. static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
  767. {
  768. struct request *rq = SCpnt->request;
  769. struct scsi_device *sdp = SCpnt->device;
  770. struct gendisk *disk = rq->rq_disk;
  771. struct scsi_disk *sdkp;
  772. sector_t block = blk_rq_pos(rq);
  773. sector_t threshold;
  774. unsigned int this_count = blk_rq_sectors(rq);
  775. unsigned int dif, dix;
  776. int ret;
  777. unsigned char protect;
  778. ret = scsi_init_io(SCpnt);
  779. if (ret != BLKPREP_OK)
  780. goto out;
  781. SCpnt = rq->special;
  782. sdkp = scsi_disk(disk);
  783. /* from here on until we're complete, any goto out
  784. * is used for a killable error condition */
  785. ret = BLKPREP_KILL;
  786. SCSI_LOG_HLQUEUE(1,
  787. scmd_printk(KERN_INFO, SCpnt,
  788. "%s: block=%llu, count=%d\n",
  789. __func__, (unsigned long long)block, this_count));
  790. if (!sdp || !scsi_device_online(sdp) ||
  791. block + blk_rq_sectors(rq) > get_capacity(disk)) {
  792. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  793. "Finishing %u sectors\n",
  794. blk_rq_sectors(rq)));
  795. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  796. "Retry with 0x%p\n", SCpnt));
  797. goto out;
  798. }
  799. if (sdp->changed) {
  800. /*
  801. * quietly refuse to do anything to a changed disc until
  802. * the changed bit has been reset
  803. */
  804. /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
  805. goto out;
  806. }
  807. /*
  808. * Some SD card readers can't handle multi-sector accesses which touch
  809. * the last one or two hardware sectors. Split accesses as needed.
  810. */
  811. threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
  812. (sdp->sector_size / 512);
  813. if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
  814. if (block < threshold) {
  815. /* Access up to the threshold but not beyond */
  816. this_count = threshold - block;
  817. } else {
  818. /* Access only a single hardware sector */
  819. this_count = sdp->sector_size / 512;
  820. }
  821. }
  822. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
  823. (unsigned long long)block));
  824. /*
  825. * If we have a 1K hardware sectorsize, prevent access to single
  826. * 512 byte sectors. In theory we could handle this - in fact
  827. * the scsi cdrom driver must be able to handle this because
  828. * we typically use 1K blocksizes, and cdroms typically have
  829. * 2K hardware sectorsizes. Of course, things are simpler
  830. * with the cdrom, since it is read-only. For performance
  831. * reasons, the filesystems should be able to handle this
  832. * and not force the scsi disk driver to use bounce buffers
  833. * for this.
  834. */
  835. if (sdp->sector_size == 1024) {
  836. if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
  837. scmd_printk(KERN_ERR, SCpnt,
  838. "Bad block number requested\n");
  839. goto out;
  840. } else {
  841. block = block >> 1;
  842. this_count = this_count >> 1;
  843. }
  844. }
  845. if (sdp->sector_size == 2048) {
  846. if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
  847. scmd_printk(KERN_ERR, SCpnt,
  848. "Bad block number requested\n");
  849. goto out;
  850. } else {
  851. block = block >> 2;
  852. this_count = this_count >> 2;
  853. }
  854. }
  855. if (sdp->sector_size == 4096) {
  856. if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
  857. scmd_printk(KERN_ERR, SCpnt,
  858. "Bad block number requested\n");
  859. goto out;
  860. } else {
  861. block = block >> 3;
  862. this_count = this_count >> 3;
  863. }
  864. }
  865. if (rq_data_dir(rq) == WRITE) {
  866. SCpnt->cmnd[0] = WRITE_6;
  867. if (blk_integrity_rq(rq))
  868. sd_dif_prepare(SCpnt);
  869. } else if (rq_data_dir(rq) == READ) {
  870. SCpnt->cmnd[0] = READ_6;
  871. } else {
  872. scmd_printk(KERN_ERR, SCpnt, "Unknown command %llx\n", (unsigned long long) rq->cmd_flags);
  873. goto out;
  874. }
  875. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  876. "%s %d/%u 512 byte blocks.\n",
  877. (rq_data_dir(rq) == WRITE) ?
  878. "writing" : "reading", this_count,
  879. blk_rq_sectors(rq)));
  880. dix = scsi_prot_sg_count(SCpnt);
  881. dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type);
  882. if (dif || dix)
  883. protect = sd_setup_protect_cmnd(SCpnt, dix, dif);
  884. else
  885. protect = 0;
  886. if (protect && sdkp->protection_type == SD_DIF_TYPE2_PROTECTION) {
  887. SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
  888. if (unlikely(SCpnt->cmnd == NULL)) {
  889. ret = BLKPREP_DEFER;
  890. goto out;
  891. }
  892. SCpnt->cmd_len = SD_EXT_CDB_SIZE;
  893. memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
  894. SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
  895. SCpnt->cmnd[7] = 0x18;
  896. SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
  897. SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  898. /* LBA */
  899. SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  900. SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  901. SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  902. SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  903. SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
  904. SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
  905. SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
  906. SCpnt->cmnd[19] = (unsigned char) block & 0xff;
  907. /* Expected Indirect LBA */
  908. SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
  909. SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
  910. SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
  911. SCpnt->cmnd[23] = (unsigned char) block & 0xff;
  912. /* Transfer length */
  913. SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
  914. SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
  915. SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
  916. SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
  917. } else if (sdp->use_16_for_rw || (this_count > 0xffff)) {
  918. SCpnt->cmnd[0] += READ_16 - READ_6;
  919. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  920. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  921. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  922. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  923. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  924. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  925. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  926. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  927. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  928. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  929. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  930. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  931. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  932. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  933. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  934. scsi_device_protection(SCpnt->device) ||
  935. SCpnt->device->use_10_for_rw) {
  936. SCpnt->cmnd[0] += READ_10 - READ_6;
  937. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  938. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  939. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  940. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  941. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  942. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  943. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  944. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  945. } else {
  946. if (unlikely(rq->cmd_flags & REQ_FUA)) {
  947. /*
  948. * This happens only if this drive failed
  949. * 10byte rw command with ILLEGAL_REQUEST
  950. * during operation and thus turned off
  951. * use_10_for_rw.
  952. */
  953. scmd_printk(KERN_ERR, SCpnt,
  954. "FUA write on READ/WRITE(6) drive\n");
  955. goto out;
  956. }
  957. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  958. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  959. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  960. SCpnt->cmnd[4] = (unsigned char) this_count;
  961. SCpnt->cmnd[5] = 0;
  962. }
  963. SCpnt->sdb.length = this_count * sdp->sector_size;
  964. /*
  965. * We shouldn't disconnect in the middle of a sector, so with a dumb
  966. * host adapter, it's safe to assume that we can at least transfer
  967. * this many bytes between each connect / disconnect.
  968. */
  969. SCpnt->transfersize = sdp->sector_size;
  970. SCpnt->underflow = this_count << 9;
  971. SCpnt->allowed = SD_MAX_RETRIES;
  972. /*
  973. * This indicates that the command is ready from our end to be
  974. * queued.
  975. */
  976. ret = BLKPREP_OK;
  977. out:
  978. return ret;
  979. }
  980. static int sd_init_command(struct scsi_cmnd *cmd)
  981. {
  982. struct request *rq = cmd->request;
  983. if (rq->cmd_flags & REQ_DISCARD)
  984. return sd_setup_discard_cmnd(cmd);
  985. else if (rq->cmd_flags & REQ_WRITE_SAME)
  986. return sd_setup_write_same_cmnd(cmd);
  987. else if (rq->cmd_flags & REQ_FLUSH)
  988. return sd_setup_flush_cmnd(cmd);
  989. else
  990. return sd_setup_read_write_cmnd(cmd);
  991. }
  992. static void sd_uninit_command(struct scsi_cmnd *SCpnt)
  993. {
  994. struct request *rq = SCpnt->request;
  995. if (rq->cmd_flags & REQ_DISCARD)
  996. __free_page(rq->completion_data);
  997. if (SCpnt->cmnd != rq->cmd) {
  998. mempool_free(SCpnt->cmnd, sd_cdb_pool);
  999. SCpnt->cmnd = NULL;
  1000. SCpnt->cmd_len = 0;
  1001. }
  1002. }
  1003. /**
  1004. * sd_open - open a scsi disk device
  1005. * @inode: only i_rdev member may be used
  1006. * @filp: only f_mode and f_flags may be used
  1007. *
  1008. * Returns 0 if successful. Returns a negated errno value in case
  1009. * of error.
  1010. *
  1011. * Note: This can be called from a user context (e.g. fsck(1) )
  1012. * or from within the kernel (e.g. as a result of a mount(1) ).
  1013. * In the latter case @inode and @filp carry an abridged amount
  1014. * of information as noted above.
  1015. *
  1016. * Locking: called with bdev->bd_mutex held.
  1017. **/
  1018. static int sd_open(struct block_device *bdev, fmode_t mode)
  1019. {
  1020. struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
  1021. struct scsi_device *sdev;
  1022. int retval;
  1023. if (!sdkp)
  1024. return -ENXIO;
  1025. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
  1026. sdev = sdkp->device;
  1027. /*
  1028. * If the device is in error recovery, wait until it is done.
  1029. * If the device is offline, then disallow any access to it.
  1030. */
  1031. retval = -ENXIO;
  1032. if (!scsi_block_when_processing_errors(sdev))
  1033. goto error_out;
  1034. if (sdev->removable || sdkp->write_prot)
  1035. check_disk_change(bdev);
  1036. /*
  1037. * If the drive is empty, just let the open fail.
  1038. */
  1039. retval = -ENOMEDIUM;
  1040. if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
  1041. goto error_out;
  1042. /*
  1043. * If the device has the write protect tab set, have the open fail
  1044. * if the user expects to be able to write to the thing.
  1045. */
  1046. retval = -EROFS;
  1047. if (sdkp->write_prot && (mode & FMODE_WRITE))
  1048. goto error_out;
  1049. /*
  1050. * It is possible that the disk changing stuff resulted in
  1051. * the device being taken offline. If this is the case,
  1052. * report this to the user, and don't pretend that the
  1053. * open actually succeeded.
  1054. */
  1055. retval = -ENXIO;
  1056. if (!scsi_device_online(sdev))
  1057. goto error_out;
  1058. if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
  1059. if (scsi_block_when_processing_errors(sdev))
  1060. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  1061. }
  1062. return 0;
  1063. error_out:
  1064. scsi_disk_put(sdkp);
  1065. return retval;
  1066. }
  1067. /**
  1068. * sd_release - invoked when the (last) close(2) is called on this
  1069. * scsi disk.
  1070. * @inode: only i_rdev member may be used
  1071. * @filp: only f_mode and f_flags may be used
  1072. *
  1073. * Returns 0.
  1074. *
  1075. * Note: may block (uninterruptible) if error recovery is underway
  1076. * on this disk.
  1077. *
  1078. * Locking: called with bdev->bd_mutex held.
  1079. **/
  1080. static void sd_release(struct gendisk *disk, fmode_t mode)
  1081. {
  1082. struct scsi_disk *sdkp = scsi_disk(disk);
  1083. struct scsi_device *sdev = sdkp->device;
  1084. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
  1085. if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
  1086. if (scsi_block_when_processing_errors(sdev))
  1087. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  1088. }
  1089. scsi_disk_put(sdkp);
  1090. }
  1091. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1092. {
  1093. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  1094. struct scsi_device *sdp = sdkp->device;
  1095. struct Scsi_Host *host = sdp->host;
  1096. sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
  1097. int diskinfo[4];
  1098. /* default to most commonly used values */
  1099. diskinfo[0] = 0x40; /* 1 << 6 */
  1100. diskinfo[1] = 0x20; /* 1 << 5 */
  1101. diskinfo[2] = capacity >> 11;
  1102. /* override with calculated, extended default, or driver values */
  1103. if (host->hostt->bios_param)
  1104. host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
  1105. else
  1106. scsicam_bios_param(bdev, capacity, diskinfo);
  1107. geo->heads = diskinfo[0];
  1108. geo->sectors = diskinfo[1];
  1109. geo->cylinders = diskinfo[2];
  1110. return 0;
  1111. }
  1112. /**
  1113. * sd_ioctl - process an ioctl
  1114. * @inode: only i_rdev/i_bdev members may be used
  1115. * @filp: only f_mode and f_flags may be used
  1116. * @cmd: ioctl command number
  1117. * @arg: this is third argument given to ioctl(2) system call.
  1118. * Often contains a pointer.
  1119. *
  1120. * Returns 0 if successful (some ioctls return positive numbers on
  1121. * success as well). Returns a negated errno value in case of error.
  1122. *
  1123. * Note: most ioctls are forward onto the block subsystem or further
  1124. * down in the scsi subsystem.
  1125. **/
  1126. static int sd_ioctl(struct block_device *bdev, fmode_t mode,
  1127. unsigned int cmd, unsigned long arg)
  1128. {
  1129. struct gendisk *disk = bdev->bd_disk;
  1130. struct scsi_disk *sdkp = scsi_disk(disk);
  1131. struct scsi_device *sdp = sdkp->device;
  1132. void __user *p = (void __user *)arg;
  1133. int error;
  1134. SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
  1135. "cmd=0x%x\n", disk->disk_name, cmd));
  1136. error = scsi_verify_blk_ioctl(bdev, cmd);
  1137. if (error < 0)
  1138. return error;
  1139. /*
  1140. * If we are in the middle of error recovery, don't let anyone
  1141. * else try and use this device. Also, if error recovery fails, it
  1142. * may try and take the device offline, in which case all further
  1143. * access to the device is prohibited.
  1144. */
  1145. error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
  1146. (mode & FMODE_NDELAY) != 0);
  1147. if (error)
  1148. goto out;
  1149. /*
  1150. * Send SCSI addressing ioctls directly to mid level, send other
  1151. * ioctls to block level and then onto mid level if they can't be
  1152. * resolved.
  1153. */
  1154. switch (cmd) {
  1155. case SCSI_IOCTL_GET_IDLUN:
  1156. case SCSI_IOCTL_GET_BUS_NUMBER:
  1157. error = scsi_ioctl(sdp, cmd, p);
  1158. break;
  1159. default:
  1160. error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
  1161. if (error != -ENOTTY)
  1162. break;
  1163. error = scsi_ioctl(sdp, cmd, p);
  1164. break;
  1165. }
  1166. out:
  1167. return error;
  1168. }
  1169. static void set_media_not_present(struct scsi_disk *sdkp)
  1170. {
  1171. if (sdkp->media_present)
  1172. sdkp->device->changed = 1;
  1173. if (sdkp->device->removable) {
  1174. sdkp->media_present = 0;
  1175. sdkp->capacity = 0;
  1176. }
  1177. }
  1178. static int media_not_present(struct scsi_disk *sdkp,
  1179. struct scsi_sense_hdr *sshdr)
  1180. {
  1181. if (!scsi_sense_valid(sshdr))
  1182. return 0;
  1183. /* not invoked for commands that could return deferred errors */
  1184. switch (sshdr->sense_key) {
  1185. case UNIT_ATTENTION:
  1186. case NOT_READY:
  1187. /* medium not present */
  1188. if (sshdr->asc == 0x3A) {
  1189. set_media_not_present(sdkp);
  1190. return 1;
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. /**
  1196. * sd_check_events - check media events
  1197. * @disk: kernel device descriptor
  1198. * @clearing: disk events currently being cleared
  1199. *
  1200. * Returns mask of DISK_EVENT_*.
  1201. *
  1202. * Note: this function is invoked from the block subsystem.
  1203. **/
  1204. static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
  1205. {
  1206. struct scsi_disk *sdkp = scsi_disk_get(disk);
  1207. struct scsi_device *sdp;
  1208. struct scsi_sense_hdr *sshdr = NULL;
  1209. int retval;
  1210. if (!sdkp)
  1211. return 0;
  1212. sdp = sdkp->device;
  1213. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
  1214. /*
  1215. * If the device is offline, don't send any commands - just pretend as
  1216. * if the command failed. If the device ever comes back online, we
  1217. * can deal with it then. It is only because of unrecoverable errors
  1218. * that we would ever take a device offline in the first place.
  1219. */
  1220. if (!scsi_device_online(sdp)) {
  1221. set_media_not_present(sdkp);
  1222. goto out;
  1223. }
  1224. /*
  1225. * Using TEST_UNIT_READY enables differentiation between drive with
  1226. * no cartridge loaded - NOT READY, drive with changed cartridge -
  1227. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  1228. *
  1229. * Drives that auto spin down. eg iomega jaz 1G, will be started
  1230. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  1231. * sd_revalidate() is called.
  1232. */
  1233. retval = -ENODEV;
  1234. if (scsi_block_when_processing_errors(sdp)) {
  1235. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  1236. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
  1237. sshdr);
  1238. }
  1239. /* failed to execute TUR, assume media not present */
  1240. if (host_byte(retval)) {
  1241. set_media_not_present(sdkp);
  1242. goto out;
  1243. }
  1244. if (media_not_present(sdkp, sshdr))
  1245. goto out;
  1246. /*
  1247. * For removable scsi disk we have to recognise the presence
  1248. * of a disk in the drive.
  1249. */
  1250. if (!sdkp->media_present)
  1251. sdp->changed = 1;
  1252. sdkp->media_present = 1;
  1253. out:
  1254. /*
  1255. * sdp->changed is set under the following conditions:
  1256. *
  1257. * Medium present state has changed in either direction.
  1258. * Device has indicated UNIT_ATTENTION.
  1259. */
  1260. kfree(sshdr);
  1261. retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
  1262. sdp->changed = 0;
  1263. scsi_disk_put(sdkp);
  1264. return retval;
  1265. }
  1266. static int sd_sync_cache(struct scsi_disk *sdkp)
  1267. {
  1268. int retries, res;
  1269. struct scsi_device *sdp = sdkp->device;
  1270. const int timeout = sdp->request_queue->rq_timeout
  1271. * SD_FLUSH_TIMEOUT_MULTIPLIER;
  1272. struct scsi_sense_hdr sshdr;
  1273. if (!scsi_device_online(sdp))
  1274. return -ENODEV;
  1275. for (retries = 3; retries > 0; --retries) {
  1276. unsigned char cmd[10] = { 0 };
  1277. cmd[0] = SYNCHRONIZE_CACHE;
  1278. /*
  1279. * Leave the rest of the command zero to indicate
  1280. * flush everything.
  1281. */
  1282. res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0,
  1283. &sshdr, timeout, SD_MAX_RETRIES,
  1284. NULL, REQ_PM);
  1285. if (res == 0)
  1286. break;
  1287. }
  1288. if (res) {
  1289. sd_print_result(sdkp, "Synchronize Cache(10) failed", res);
  1290. if (driver_byte(res) & DRIVER_SENSE)
  1291. sd_print_sense_hdr(sdkp, &sshdr);
  1292. /* we need to evaluate the error return */
  1293. if (scsi_sense_valid(&sshdr) &&
  1294. (sshdr.asc == 0x3a || /* medium not present */
  1295. sshdr.asc == 0x20)) /* invalid command */
  1296. /* this is no error here */
  1297. return 0;
  1298. switch (host_byte(res)) {
  1299. /* ignore errors due to racing a disconnection */
  1300. case DID_BAD_TARGET:
  1301. case DID_NO_CONNECT:
  1302. return 0;
  1303. /* signal the upper layer it might try again */
  1304. case DID_BUS_BUSY:
  1305. case DID_IMM_RETRY:
  1306. case DID_REQUEUE:
  1307. case DID_SOFT_ERROR:
  1308. return -EBUSY;
  1309. default:
  1310. return -EIO;
  1311. }
  1312. }
  1313. return 0;
  1314. }
  1315. static void sd_rescan(struct device *dev)
  1316. {
  1317. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  1318. revalidate_disk(sdkp->disk);
  1319. }
  1320. #ifdef CONFIG_COMPAT
  1321. /*
  1322. * This gets directly called from VFS. When the ioctl
  1323. * is not recognized we go back to the other translation paths.
  1324. */
  1325. static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1326. unsigned int cmd, unsigned long arg)
  1327. {
  1328. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  1329. int error;
  1330. error = scsi_ioctl_block_when_processing_errors(sdev, cmd,
  1331. (mode & FMODE_NDELAY) != 0);
  1332. if (error)
  1333. return error;
  1334. /*
  1335. * Let the static ioctl translation table take care of it.
  1336. */
  1337. if (!sdev->host->hostt->compat_ioctl)
  1338. return -ENOIOCTLCMD;
  1339. return sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  1340. }
  1341. #endif
  1342. static char sd_pr_type(enum pr_type type)
  1343. {
  1344. switch (type) {
  1345. case PR_WRITE_EXCLUSIVE:
  1346. return 0x01;
  1347. case PR_EXCLUSIVE_ACCESS:
  1348. return 0x03;
  1349. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  1350. return 0x05;
  1351. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  1352. return 0x06;
  1353. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  1354. return 0x07;
  1355. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  1356. return 0x08;
  1357. default:
  1358. return 0;
  1359. }
  1360. };
  1361. static int sd_pr_command(struct block_device *bdev, u8 sa,
  1362. u64 key, u64 sa_key, u8 type, u8 flags)
  1363. {
  1364. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  1365. struct scsi_sense_hdr sshdr;
  1366. int result;
  1367. u8 cmd[16] = { 0, };
  1368. u8 data[24] = { 0, };
  1369. cmd[0] = PERSISTENT_RESERVE_OUT;
  1370. cmd[1] = sa;
  1371. cmd[2] = type;
  1372. put_unaligned_be32(sizeof(data), &cmd[5]);
  1373. put_unaligned_be64(key, &data[0]);
  1374. put_unaligned_be64(sa_key, &data[8]);
  1375. data[20] = flags;
  1376. result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
  1377. &sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1378. if ((driver_byte(result) & DRIVER_SENSE) &&
  1379. (scsi_sense_valid(&sshdr))) {
  1380. sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result);
  1381. scsi_print_sense_hdr(sdev, NULL, &sshdr);
  1382. }
  1383. return result;
  1384. }
  1385. static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
  1386. u32 flags)
  1387. {
  1388. if (flags & ~PR_FL_IGNORE_KEY)
  1389. return -EOPNOTSUPP;
  1390. return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00,
  1391. old_key, new_key, 0,
  1392. (1 << 0) /* APTPL */ |
  1393. (1 << 2) /* ALL_TG_PT */);
  1394. }
  1395. static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
  1396. u32 flags)
  1397. {
  1398. if (flags)
  1399. return -EOPNOTSUPP;
  1400. return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0);
  1401. }
  1402. static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  1403. {
  1404. return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0);
  1405. }
  1406. static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
  1407. enum pr_type type, bool abort)
  1408. {
  1409. return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key,
  1410. sd_pr_type(type), 0);
  1411. }
  1412. static int sd_pr_clear(struct block_device *bdev, u64 key)
  1413. {
  1414. return sd_pr_command(bdev, 0x03, key, 0, 0, 0);
  1415. }
  1416. static const struct pr_ops sd_pr_ops = {
  1417. .pr_register = sd_pr_register,
  1418. .pr_reserve = sd_pr_reserve,
  1419. .pr_release = sd_pr_release,
  1420. .pr_preempt = sd_pr_preempt,
  1421. .pr_clear = sd_pr_clear,
  1422. };
  1423. static const struct block_device_operations sd_fops = {
  1424. .owner = THIS_MODULE,
  1425. .open = sd_open,
  1426. .release = sd_release,
  1427. .ioctl = sd_ioctl,
  1428. .getgeo = sd_getgeo,
  1429. #ifdef CONFIG_COMPAT
  1430. .compat_ioctl = sd_compat_ioctl,
  1431. #endif
  1432. .check_events = sd_check_events,
  1433. .revalidate_disk = sd_revalidate_disk,
  1434. .unlock_native_capacity = sd_unlock_native_capacity,
  1435. .pr_ops = &sd_pr_ops,
  1436. };
  1437. /**
  1438. * sd_eh_action - error handling callback
  1439. * @scmd: sd-issued command that has failed
  1440. * @eh_disp: The recovery disposition suggested by the midlayer
  1441. *
  1442. * This function is called by the SCSI midlayer upon completion of an
  1443. * error test command (currently TEST UNIT READY). The result of sending
  1444. * the eh command is passed in eh_disp. We're looking for devices that
  1445. * fail medium access commands but are OK with non access commands like
  1446. * test unit ready (so wrongly see the device as having a successful
  1447. * recovery)
  1448. **/
  1449. static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
  1450. {
  1451. struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
  1452. if (!scsi_device_online(scmd->device) ||
  1453. !scsi_medium_access_command(scmd) ||
  1454. host_byte(scmd->result) != DID_TIME_OUT ||
  1455. eh_disp != SUCCESS)
  1456. return eh_disp;
  1457. /*
  1458. * The device has timed out executing a medium access command.
  1459. * However, the TEST UNIT READY command sent during error
  1460. * handling completed successfully. Either the device is in the
  1461. * process of recovering or has it suffered an internal failure
  1462. * that prevents access to the storage medium.
  1463. */
  1464. sdkp->medium_access_timed_out++;
  1465. /*
  1466. * If the device keeps failing read/write commands but TEST UNIT
  1467. * READY always completes successfully we assume that medium
  1468. * access is no longer possible and take the device offline.
  1469. */
  1470. if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) {
  1471. scmd_printk(KERN_ERR, scmd,
  1472. "Medium access timeout failure. Offlining disk!\n");
  1473. scsi_device_set_state(scmd->device, SDEV_OFFLINE);
  1474. return FAILED;
  1475. }
  1476. return eh_disp;
  1477. }
  1478. static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
  1479. {
  1480. u64 start_lba = blk_rq_pos(scmd->request);
  1481. u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
  1482. u64 factor = scmd->device->sector_size / 512;
  1483. u64 bad_lba;
  1484. int info_valid;
  1485. /*
  1486. * resid is optional but mostly filled in. When it's unused,
  1487. * its value is zero, so we assume the whole buffer transferred
  1488. */
  1489. unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
  1490. unsigned int good_bytes;
  1491. if (scmd->request->cmd_type != REQ_TYPE_FS)
  1492. return 0;
  1493. info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
  1494. SCSI_SENSE_BUFFERSIZE,
  1495. &bad_lba);
  1496. if (!info_valid)
  1497. return 0;
  1498. if (scsi_bufflen(scmd) <= scmd->device->sector_size)
  1499. return 0;
  1500. /* be careful ... don't want any overflows */
  1501. do_div(start_lba, factor);
  1502. do_div(end_lba, factor);
  1503. /* The bad lba was reported incorrectly, we have no idea where
  1504. * the error is.
  1505. */
  1506. if (bad_lba < start_lba || bad_lba >= end_lba)
  1507. return 0;
  1508. /* This computation should always be done in terms of
  1509. * the resolution of the device's medium.
  1510. */
  1511. good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
  1512. return min(good_bytes, transferred);
  1513. }
  1514. /**
  1515. * sd_done - bottom half handler: called when the lower level
  1516. * driver has completed (successfully or otherwise) a scsi command.
  1517. * @SCpnt: mid-level's per command structure.
  1518. *
  1519. * Note: potentially run from within an ISR. Must not block.
  1520. **/
  1521. static int sd_done(struct scsi_cmnd *SCpnt)
  1522. {
  1523. int result = SCpnt->result;
  1524. unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
  1525. struct scsi_sense_hdr sshdr;
  1526. struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
  1527. struct request *req = SCpnt->request;
  1528. int sense_valid = 0;
  1529. int sense_deferred = 0;
  1530. unsigned char op = SCpnt->cmnd[0];
  1531. unsigned char unmap = SCpnt->cmnd[1] & 8;
  1532. if (req->cmd_flags & REQ_DISCARD || req->cmd_flags & REQ_WRITE_SAME) {
  1533. if (!result) {
  1534. good_bytes = blk_rq_bytes(req);
  1535. scsi_set_resid(SCpnt, 0);
  1536. } else {
  1537. good_bytes = 0;
  1538. scsi_set_resid(SCpnt, blk_rq_bytes(req));
  1539. }
  1540. }
  1541. if (result) {
  1542. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  1543. if (sense_valid)
  1544. sense_deferred = scsi_sense_is_deferred(&sshdr);
  1545. }
  1546. sdkp->medium_access_timed_out = 0;
  1547. if (driver_byte(result) != DRIVER_SENSE &&
  1548. (!sense_valid || sense_deferred))
  1549. goto out;
  1550. switch (sshdr.sense_key) {
  1551. case HARDWARE_ERROR:
  1552. case MEDIUM_ERROR:
  1553. good_bytes = sd_completed_bytes(SCpnt);
  1554. break;
  1555. case RECOVERED_ERROR:
  1556. good_bytes = scsi_bufflen(SCpnt);
  1557. break;
  1558. case NO_SENSE:
  1559. /* This indicates a false check condition, so ignore it. An
  1560. * unknown amount of data was transferred so treat it as an
  1561. * error.
  1562. */
  1563. SCpnt->result = 0;
  1564. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1565. break;
  1566. case ABORTED_COMMAND:
  1567. if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
  1568. good_bytes = sd_completed_bytes(SCpnt);
  1569. break;
  1570. case ILLEGAL_REQUEST:
  1571. if (sshdr.asc == 0x10) /* DIX: Host detected corruption */
  1572. good_bytes = sd_completed_bytes(SCpnt);
  1573. /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
  1574. if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
  1575. switch (op) {
  1576. case UNMAP:
  1577. sd_config_discard(sdkp, SD_LBP_DISABLE);
  1578. break;
  1579. case WRITE_SAME_16:
  1580. case WRITE_SAME:
  1581. if (unmap)
  1582. sd_config_discard(sdkp, SD_LBP_DISABLE);
  1583. else {
  1584. sdkp->device->no_write_same = 1;
  1585. sd_config_write_same(sdkp);
  1586. good_bytes = 0;
  1587. req->__data_len = blk_rq_bytes(req);
  1588. req->cmd_flags |= REQ_QUIET;
  1589. }
  1590. }
  1591. }
  1592. break;
  1593. default:
  1594. break;
  1595. }
  1596. out:
  1597. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
  1598. "sd_done: completed %d of %d bytes\n",
  1599. good_bytes, scsi_bufflen(SCpnt)));
  1600. if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
  1601. sd_dif_complete(SCpnt, good_bytes);
  1602. return good_bytes;
  1603. }
  1604. /*
  1605. * spinup disk - called only in sd_revalidate_disk()
  1606. */
  1607. static void
  1608. sd_spinup_disk(struct scsi_disk *sdkp)
  1609. {
  1610. unsigned char cmd[10];
  1611. unsigned long spintime_expire = 0;
  1612. int retries, spintime;
  1613. unsigned int the_result;
  1614. struct scsi_sense_hdr sshdr;
  1615. int sense_valid = 0;
  1616. spintime = 0;
  1617. /* Spin up drives, as required. Only do this at boot time */
  1618. /* Spinup needs to be done for module loads too. */
  1619. do {
  1620. retries = 0;
  1621. do {
  1622. cmd[0] = TEST_UNIT_READY;
  1623. memset((void *) &cmd[1], 0, 9);
  1624. the_result = scsi_execute_req(sdkp->device, cmd,
  1625. DMA_NONE, NULL, 0,
  1626. &sshdr, SD_TIMEOUT,
  1627. SD_MAX_RETRIES, NULL);
  1628. /*
  1629. * If the drive has indicated to us that it
  1630. * doesn't have any media in it, don't bother
  1631. * with any more polling.
  1632. */
  1633. if (media_not_present(sdkp, &sshdr))
  1634. return;
  1635. if (the_result)
  1636. sense_valid = scsi_sense_valid(&sshdr);
  1637. retries++;
  1638. } while (retries < 3 &&
  1639. (!scsi_status_is_good(the_result) ||
  1640. ((driver_byte(the_result) & DRIVER_SENSE) &&
  1641. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  1642. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  1643. /* no sense, TUR either succeeded or failed
  1644. * with a status error */
  1645. if(!spintime && !scsi_status_is_good(the_result)) {
  1646. sd_print_result(sdkp, "Test Unit Ready failed",
  1647. the_result);
  1648. }
  1649. break;
  1650. }
  1651. /*
  1652. * The device does not want the automatic start to be issued.
  1653. */
  1654. if (sdkp->device->no_start_on_add)
  1655. break;
  1656. if (sense_valid && sshdr.sense_key == NOT_READY) {
  1657. if (sshdr.asc == 4 && sshdr.ascq == 3)
  1658. break; /* manual intervention required */
  1659. if (sshdr.asc == 4 && sshdr.ascq == 0xb)
  1660. break; /* standby */
  1661. if (sshdr.asc == 4 && sshdr.ascq == 0xc)
  1662. break; /* unavailable */
  1663. if (sshdr.asc == 4 && sshdr.ascq == 0x1b)
  1664. break; /* sanitize in progress */
  1665. /*
  1666. * Issue command to spin up drive when not ready
  1667. */
  1668. if (!spintime) {
  1669. sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  1670. cmd[0] = START_STOP;
  1671. cmd[1] = 1; /* Return immediately */
  1672. memset((void *) &cmd[2], 0, 8);
  1673. cmd[4] = 1; /* Start spin cycle */
  1674. if (sdkp->device->start_stop_pwr_cond)
  1675. cmd[4] |= 1 << 4;
  1676. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  1677. NULL, 0, &sshdr,
  1678. SD_TIMEOUT, SD_MAX_RETRIES,
  1679. NULL);
  1680. spintime_expire = jiffies + 100 * HZ;
  1681. spintime = 1;
  1682. }
  1683. /* Wait 1 second for next try */
  1684. msleep(1000);
  1685. printk(".");
  1686. /*
  1687. * Wait for USB flash devices with slow firmware.
  1688. * Yes, this sense key/ASC combination shouldn't
  1689. * occur here. It's characteristic of these devices.
  1690. */
  1691. } else if (sense_valid &&
  1692. sshdr.sense_key == UNIT_ATTENTION &&
  1693. sshdr.asc == 0x28) {
  1694. if (!spintime) {
  1695. spintime_expire = jiffies + 5 * HZ;
  1696. spintime = 1;
  1697. }
  1698. /* Wait 1 second for next try */
  1699. msleep(1000);
  1700. } else {
  1701. /* we don't understand the sense code, so it's
  1702. * probably pointless to loop */
  1703. if(!spintime) {
  1704. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1705. sd_print_sense_hdr(sdkp, &sshdr);
  1706. }
  1707. break;
  1708. }
  1709. } while (spintime && time_before_eq(jiffies, spintime_expire));
  1710. if (spintime) {
  1711. if (scsi_status_is_good(the_result))
  1712. printk("ready\n");
  1713. else
  1714. printk("not responding...\n");
  1715. }
  1716. }
  1717. /*
  1718. * Determine whether disk supports Data Integrity Field.
  1719. */
  1720. static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1721. {
  1722. struct scsi_device *sdp = sdkp->device;
  1723. u8 type;
  1724. int ret = 0;
  1725. if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
  1726. return ret;
  1727. type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
  1728. if (type > SD_DIF_TYPE3_PROTECTION)
  1729. ret = -ENODEV;
  1730. else if (scsi_host_dif_capable(sdp->host, type))
  1731. ret = 1;
  1732. if (sdkp->first_scan || type != sdkp->protection_type)
  1733. switch (ret) {
  1734. case -ENODEV:
  1735. sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \
  1736. " protection type %u. Disabling disk!\n",
  1737. type);
  1738. break;
  1739. case 1:
  1740. sd_printk(KERN_NOTICE, sdkp,
  1741. "Enabling DIF Type %u protection\n", type);
  1742. break;
  1743. case 0:
  1744. sd_printk(KERN_NOTICE, sdkp,
  1745. "Disabling DIF Type %u protection\n", type);
  1746. break;
  1747. }
  1748. sdkp->protection_type = type;
  1749. return ret;
  1750. }
  1751. static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1752. struct scsi_sense_hdr *sshdr, int sense_valid,
  1753. int the_result)
  1754. {
  1755. if (driver_byte(the_result) & DRIVER_SENSE)
  1756. sd_print_sense_hdr(sdkp, sshdr);
  1757. else
  1758. sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1759. /*
  1760. * Set dirty bit for removable devices if not ready -
  1761. * sometimes drives will not report this properly.
  1762. */
  1763. if (sdp->removable &&
  1764. sense_valid && sshdr->sense_key == NOT_READY)
  1765. set_media_not_present(sdkp);
  1766. /*
  1767. * We used to set media_present to 0 here to indicate no media
  1768. * in the drive, but some drives fail read capacity even with
  1769. * media present, so we can't do that.
  1770. */
  1771. sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1772. }
  1773. #define RC16_LEN 32
  1774. #if RC16_LEN > SD_BUF_SIZE
  1775. #error RC16_LEN must not be more than SD_BUF_SIZE
  1776. #endif
  1777. #define READ_CAPACITY_RETRIES_ON_RESET 10
  1778. /*
  1779. * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set
  1780. * and the reported logical block size is bigger than 512 bytes. Note
  1781. * that last_sector is a u64 and therefore logical_to_sectors() is not
  1782. * applicable.
  1783. */
  1784. static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
  1785. {
  1786. u64 last_sector = (lba + 1ULL) << (ilog2(sector_size) - 9);
  1787. if (sizeof(sector_t) == 4 && last_sector > U32_MAX)
  1788. return false;
  1789. return true;
  1790. }
  1791. static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1792. unsigned char *buffer)
  1793. {
  1794. unsigned char cmd[16];
  1795. struct scsi_sense_hdr sshdr;
  1796. int sense_valid = 0;
  1797. int the_result;
  1798. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1799. unsigned int alignment;
  1800. unsigned long long lba;
  1801. unsigned sector_size;
  1802. if (sdp->no_read_capacity_16)
  1803. return -EINVAL;
  1804. do {
  1805. memset(cmd, 0, 16);
  1806. cmd[0] = SERVICE_ACTION_IN_16;
  1807. cmd[1] = SAI_READ_CAPACITY_16;
  1808. cmd[13] = RC16_LEN;
  1809. memset(buffer, 0, RC16_LEN);
  1810. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1811. buffer, RC16_LEN, &sshdr,
  1812. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1813. if (media_not_present(sdkp, &sshdr))
  1814. return -ENODEV;
  1815. if (the_result) {
  1816. sense_valid = scsi_sense_valid(&sshdr);
  1817. if (sense_valid &&
  1818. sshdr.sense_key == ILLEGAL_REQUEST &&
  1819. (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1820. sshdr.ascq == 0x00)
  1821. /* Invalid Command Operation Code or
  1822. * Invalid Field in CDB, just retry
  1823. * silently with RC10 */
  1824. return -EINVAL;
  1825. if (sense_valid &&
  1826. sshdr.sense_key == UNIT_ATTENTION &&
  1827. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1828. /* Device reset might occur several times,
  1829. * give it one more chance */
  1830. if (--reset_retries > 0)
  1831. continue;
  1832. }
  1833. retries--;
  1834. } while (the_result && retries);
  1835. if (the_result) {
  1836. sd_print_result(sdkp, "Read Capacity(16) failed", the_result);
  1837. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1838. return -EINVAL;
  1839. }
  1840. sector_size = get_unaligned_be32(&buffer[8]);
  1841. lba = get_unaligned_be64(&buffer[0]);
  1842. if (sd_read_protection_type(sdkp, buffer) < 0) {
  1843. sdkp->capacity = 0;
  1844. return -ENODEV;
  1845. }
  1846. if (!sd_addressable_capacity(lba, sector_size)) {
  1847. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1848. "kernel compiled with support for large block "
  1849. "devices.\n");
  1850. sdkp->capacity = 0;
  1851. return -EOVERFLOW;
  1852. }
  1853. /* Logical blocks per physical block exponent */
  1854. sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
  1855. /* Lowest aligned logical block */
  1856. alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
  1857. blk_queue_alignment_offset(sdp->request_queue, alignment);
  1858. if (alignment && sdkp->first_scan)
  1859. sd_printk(KERN_NOTICE, sdkp,
  1860. "physical block alignment offset: %u\n", alignment);
  1861. if (buffer[14] & 0x80) { /* LBPME */
  1862. sdkp->lbpme = 1;
  1863. if (buffer[14] & 0x40) /* LBPRZ */
  1864. sdkp->lbprz = 1;
  1865. sd_config_discard(sdkp, SD_LBP_WS16);
  1866. }
  1867. sdkp->capacity = lba + 1;
  1868. return sector_size;
  1869. }
  1870. static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1871. unsigned char *buffer)
  1872. {
  1873. unsigned char cmd[16];
  1874. struct scsi_sense_hdr sshdr;
  1875. int sense_valid = 0;
  1876. int the_result;
  1877. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1878. sector_t lba;
  1879. unsigned sector_size;
  1880. do {
  1881. cmd[0] = READ_CAPACITY;
  1882. memset(&cmd[1], 0, 9);
  1883. memset(buffer, 0, 8);
  1884. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1885. buffer, 8, &sshdr,
  1886. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1887. if (media_not_present(sdkp, &sshdr))
  1888. return -ENODEV;
  1889. if (the_result) {
  1890. sense_valid = scsi_sense_valid(&sshdr);
  1891. if (sense_valid &&
  1892. sshdr.sense_key == UNIT_ATTENTION &&
  1893. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1894. /* Device reset might occur several times,
  1895. * give it one more chance */
  1896. if (--reset_retries > 0)
  1897. continue;
  1898. }
  1899. retries--;
  1900. } while (the_result && retries);
  1901. if (the_result) {
  1902. sd_print_result(sdkp, "Read Capacity(10) failed", the_result);
  1903. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1904. return -EINVAL;
  1905. }
  1906. sector_size = get_unaligned_be32(&buffer[4]);
  1907. lba = get_unaligned_be32(&buffer[0]);
  1908. if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
  1909. /* Some buggy (usb cardreader) devices return an lba of
  1910. 0xffffffff when the want to report a size of 0 (with
  1911. which they really mean no media is present) */
  1912. sdkp->capacity = 0;
  1913. sdkp->physical_block_size = sector_size;
  1914. return sector_size;
  1915. }
  1916. if (!sd_addressable_capacity(lba, sector_size)) {
  1917. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1918. "kernel compiled with support for large block "
  1919. "devices.\n");
  1920. sdkp->capacity = 0;
  1921. return -EOVERFLOW;
  1922. }
  1923. sdkp->capacity = lba + 1;
  1924. sdkp->physical_block_size = sector_size;
  1925. return sector_size;
  1926. }
  1927. static int sd_try_rc16_first(struct scsi_device *sdp)
  1928. {
  1929. if (sdp->host->max_cmd_len < 16)
  1930. return 0;
  1931. if (sdp->try_rc_10_first)
  1932. return 0;
  1933. if (sdp->scsi_level > SCSI_SPC_2)
  1934. return 1;
  1935. if (scsi_device_protection(sdp))
  1936. return 1;
  1937. return 0;
  1938. }
  1939. /*
  1940. * read disk capacity
  1941. */
  1942. static void
  1943. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  1944. {
  1945. int sector_size;
  1946. struct scsi_device *sdp = sdkp->device;
  1947. sector_t old_capacity = sdkp->capacity;
  1948. if (sd_try_rc16_first(sdp)) {
  1949. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1950. if (sector_size == -EOVERFLOW)
  1951. goto got_data;
  1952. if (sector_size == -ENODEV)
  1953. return;
  1954. if (sector_size < 0)
  1955. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1956. if (sector_size < 0)
  1957. return;
  1958. } else {
  1959. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1960. if (sector_size == -EOVERFLOW)
  1961. goto got_data;
  1962. if (sector_size < 0)
  1963. return;
  1964. if ((sizeof(sdkp->capacity) > 4) &&
  1965. (sdkp->capacity > 0xffffffffULL)) {
  1966. int old_sector_size = sector_size;
  1967. sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1968. "Trying to use READ CAPACITY(16).\n");
  1969. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1970. if (sector_size < 0) {
  1971. sd_printk(KERN_NOTICE, sdkp,
  1972. "Using 0xffffffff as device size\n");
  1973. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1974. sector_size = old_sector_size;
  1975. goto got_data;
  1976. }
  1977. }
  1978. }
  1979. /* Some devices are known to return the total number of blocks,
  1980. * not the highest block number. Some devices have versions
  1981. * which do this and others which do not. Some devices we might
  1982. * suspect of doing this but we don't know for certain.
  1983. *
  1984. * If we know the reported capacity is wrong, decrement it. If
  1985. * we can only guess, then assume the number of blocks is even
  1986. * (usually true but not always) and err on the side of lowering
  1987. * the capacity.
  1988. */
  1989. if (sdp->fix_capacity ||
  1990. (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
  1991. sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
  1992. "from its reported value: %llu\n",
  1993. (unsigned long long) sdkp->capacity);
  1994. --sdkp->capacity;
  1995. }
  1996. got_data:
  1997. if (sector_size == 0) {
  1998. sector_size = 512;
  1999. sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  2000. "assuming 512.\n");
  2001. }
  2002. if (sector_size != 512 &&
  2003. sector_size != 1024 &&
  2004. sector_size != 2048 &&
  2005. sector_size != 4096) {
  2006. sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  2007. sector_size);
  2008. /*
  2009. * The user might want to re-format the drive with
  2010. * a supported sectorsize. Once this happens, it
  2011. * would be relatively trivial to set the thing up.
  2012. * For this reason, we leave the thing in the table.
  2013. */
  2014. sdkp->capacity = 0;
  2015. /*
  2016. * set a bogus sector size so the normal read/write
  2017. * logic in the block layer will eventually refuse any
  2018. * request on this device without tripping over power
  2019. * of two sector size assumptions
  2020. */
  2021. sector_size = 512;
  2022. }
  2023. blk_queue_logical_block_size(sdp->request_queue, sector_size);
  2024. {
  2025. char cap_str_2[10], cap_str_10[10];
  2026. string_get_size(sdkp->capacity, sector_size,
  2027. STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  2028. string_get_size(sdkp->capacity, sector_size,
  2029. STRING_UNITS_10, cap_str_10,
  2030. sizeof(cap_str_10));
  2031. if (sdkp->first_scan || old_capacity != sdkp->capacity) {
  2032. sd_printk(KERN_NOTICE, sdkp,
  2033. "%llu %d-byte logical blocks: (%s/%s)\n",
  2034. (unsigned long long)sdkp->capacity,
  2035. sector_size, cap_str_10, cap_str_2);
  2036. if (sdkp->physical_block_size != sector_size)
  2037. sd_printk(KERN_NOTICE, sdkp,
  2038. "%u-byte physical blocks\n",
  2039. sdkp->physical_block_size);
  2040. }
  2041. }
  2042. if (sdkp->capacity > 0xffffffff)
  2043. sdp->use_16_for_rw = 1;
  2044. blk_queue_physical_block_size(sdp->request_queue,
  2045. sdkp->physical_block_size);
  2046. sdkp->device->sector_size = sector_size;
  2047. }
  2048. /* called with buffer of length 512 */
  2049. static inline int
  2050. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  2051. unsigned char *buffer, int len, struct scsi_mode_data *data,
  2052. struct scsi_sense_hdr *sshdr)
  2053. {
  2054. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  2055. SD_TIMEOUT, SD_MAX_RETRIES, data,
  2056. sshdr);
  2057. }
  2058. /*
  2059. * read write protect setting, if possible - called only in sd_revalidate_disk()
  2060. * called with buffer of length SD_BUF_SIZE
  2061. */
  2062. static void
  2063. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  2064. {
  2065. int res;
  2066. struct scsi_device *sdp = sdkp->device;
  2067. struct scsi_mode_data data;
  2068. int disk_ro = get_disk_ro(sdkp->disk);
  2069. int old_wp = sdkp->write_prot;
  2070. set_disk_ro(sdkp->disk, 0);
  2071. if (sdp->skip_ms_page_3f) {
  2072. sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  2073. return;
  2074. }
  2075. if (sdp->use_192_bytes_for_3f) {
  2076. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  2077. } else {
  2078. /*
  2079. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  2080. * We have to start carefully: some devices hang if we ask
  2081. * for more than is available.
  2082. */
  2083. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  2084. /*
  2085. * Second attempt: ask for page 0 When only page 0 is
  2086. * implemented, a request for page 3F may return Sense Key
  2087. * 5: Illegal Request, Sense Code 24: Invalid field in
  2088. * CDB.
  2089. */
  2090. if (!scsi_status_is_good(res))
  2091. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  2092. /*
  2093. * Third attempt: ask 255 bytes, as we did earlier.
  2094. */
  2095. if (!scsi_status_is_good(res))
  2096. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  2097. &data, NULL);
  2098. }
  2099. if (!scsi_status_is_good(res)) {
  2100. sd_first_printk(KERN_WARNING, sdkp,
  2101. "Test WP failed, assume Write Enabled\n");
  2102. } else {
  2103. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  2104. set_disk_ro(sdkp->disk, sdkp->write_prot || disk_ro);
  2105. if (sdkp->first_scan || old_wp != sdkp->write_prot) {
  2106. sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  2107. sdkp->write_prot ? "on" : "off");
  2108. sd_printk(KERN_DEBUG, sdkp,
  2109. "Mode Sense: %02x %02x %02x %02x\n",
  2110. buffer[0], buffer[1], buffer[2], buffer[3]);
  2111. }
  2112. }
  2113. }
  2114. /*
  2115. * sd_read_cache_type - called only from sd_revalidate_disk()
  2116. * called with buffer of length SD_BUF_SIZE
  2117. */
  2118. static void
  2119. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  2120. {
  2121. int len = 0, res;
  2122. struct scsi_device *sdp = sdkp->device;
  2123. int dbd;
  2124. int modepage;
  2125. int first_len;
  2126. struct scsi_mode_data data;
  2127. struct scsi_sense_hdr sshdr;
  2128. int old_wce = sdkp->WCE;
  2129. int old_rcd = sdkp->RCD;
  2130. int old_dpofua = sdkp->DPOFUA;
  2131. if (sdkp->cache_override)
  2132. return;
  2133. first_len = 4;
  2134. if (sdp->skip_ms_page_8) {
  2135. if (sdp->type == TYPE_RBC)
  2136. goto defaults;
  2137. else {
  2138. if (sdp->skip_ms_page_3f)
  2139. goto defaults;
  2140. modepage = 0x3F;
  2141. if (sdp->use_192_bytes_for_3f)
  2142. first_len = 192;
  2143. dbd = 0;
  2144. }
  2145. } else if (sdp->type == TYPE_RBC) {
  2146. modepage = 6;
  2147. dbd = 8;
  2148. } else {
  2149. modepage = 8;
  2150. dbd = 0;
  2151. }
  2152. /* cautiously ask */
  2153. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
  2154. &data, &sshdr);
  2155. if (!scsi_status_is_good(res))
  2156. goto bad_sense;
  2157. if (!data.header_length) {
  2158. modepage = 6;
  2159. first_len = 0;
  2160. sd_first_printk(KERN_ERR, sdkp,
  2161. "Missing header in MODE_SENSE response\n");
  2162. }
  2163. /* that went OK, now ask for the proper length */
  2164. len = data.length;
  2165. /*
  2166. * We're only interested in the first three bytes, actually.
  2167. * But the data cache page is defined for the first 20.
  2168. */
  2169. if (len < 3)
  2170. goto bad_sense;
  2171. else if (len > SD_BUF_SIZE) {
  2172. sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
  2173. "data from %d to %d bytes\n", len, SD_BUF_SIZE);
  2174. len = SD_BUF_SIZE;
  2175. }
  2176. if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
  2177. len = 192;
  2178. /* Get the data */
  2179. if (len > first_len)
  2180. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
  2181. &data, &sshdr);
  2182. if (scsi_status_is_good(res)) {
  2183. int offset = data.header_length + data.block_descriptor_length;
  2184. while (offset < len) {
  2185. u8 page_code = buffer[offset] & 0x3F;
  2186. u8 spf = buffer[offset] & 0x40;
  2187. if (page_code == 8 || page_code == 6) {
  2188. /* We're interested only in the first 3 bytes.
  2189. */
  2190. if (len - offset <= 2) {
  2191. sd_first_printk(KERN_ERR, sdkp,
  2192. "Incomplete mode parameter "
  2193. "data\n");
  2194. goto defaults;
  2195. } else {
  2196. modepage = page_code;
  2197. goto Page_found;
  2198. }
  2199. } else {
  2200. /* Go to the next page */
  2201. if (spf && len - offset > 3)
  2202. offset += 4 + (buffer[offset+2] << 8) +
  2203. buffer[offset+3];
  2204. else if (!spf && len - offset > 1)
  2205. offset += 2 + buffer[offset+1];
  2206. else {
  2207. sd_first_printk(KERN_ERR, sdkp,
  2208. "Incomplete mode "
  2209. "parameter data\n");
  2210. goto defaults;
  2211. }
  2212. }
  2213. }
  2214. sd_first_printk(KERN_ERR, sdkp, "No Caching mode page found\n");
  2215. goto defaults;
  2216. Page_found:
  2217. if (modepage == 8) {
  2218. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  2219. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  2220. } else {
  2221. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  2222. sdkp->RCD = 0;
  2223. }
  2224. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  2225. if (sdp->broken_fua) {
  2226. sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n");
  2227. sdkp->DPOFUA = 0;
  2228. } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw &&
  2229. !sdkp->device->use_16_for_rw) {
  2230. sd_first_printk(KERN_NOTICE, sdkp,
  2231. "Uses READ/WRITE(6), disabling FUA\n");
  2232. sdkp->DPOFUA = 0;
  2233. }
  2234. /* No cache flush allowed for write protected devices */
  2235. if (sdkp->WCE && sdkp->write_prot)
  2236. sdkp->WCE = 0;
  2237. if (sdkp->first_scan || old_wce != sdkp->WCE ||
  2238. old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
  2239. sd_printk(KERN_NOTICE, sdkp,
  2240. "Write cache: %s, read cache: %s, %s\n",
  2241. sdkp->WCE ? "enabled" : "disabled",
  2242. sdkp->RCD ? "disabled" : "enabled",
  2243. sdkp->DPOFUA ? "supports DPO and FUA"
  2244. : "doesn't support DPO or FUA");
  2245. return;
  2246. }
  2247. bad_sense:
  2248. if (scsi_sense_valid(&sshdr) &&
  2249. sshdr.sense_key == ILLEGAL_REQUEST &&
  2250. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  2251. /* Invalid field in CDB */
  2252. sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  2253. else
  2254. sd_first_printk(KERN_ERR, sdkp,
  2255. "Asking for cache data failed\n");
  2256. defaults:
  2257. if (sdp->wce_default_on) {
  2258. sd_first_printk(KERN_NOTICE, sdkp,
  2259. "Assuming drive cache: write back\n");
  2260. sdkp->WCE = 1;
  2261. } else {
  2262. sd_first_printk(KERN_ERR, sdkp,
  2263. "Assuming drive cache: write through\n");
  2264. sdkp->WCE = 0;
  2265. }
  2266. sdkp->RCD = 0;
  2267. sdkp->DPOFUA = 0;
  2268. }
  2269. /*
  2270. * The ATO bit indicates whether the DIF application tag is available
  2271. * for use by the operating system.
  2272. */
  2273. static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
  2274. {
  2275. int res, offset;
  2276. struct scsi_device *sdp = sdkp->device;
  2277. struct scsi_mode_data data;
  2278. struct scsi_sense_hdr sshdr;
  2279. if (sdp->type != TYPE_DISK)
  2280. return;
  2281. if (sdkp->protection_type == 0)
  2282. return;
  2283. res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
  2284. SD_MAX_RETRIES, &data, &sshdr);
  2285. if (!scsi_status_is_good(res) || !data.header_length ||
  2286. data.length < 6) {
  2287. sd_first_printk(KERN_WARNING, sdkp,
  2288. "getting Control mode page failed, assume no ATO\n");
  2289. if (scsi_sense_valid(&sshdr))
  2290. sd_print_sense_hdr(sdkp, &sshdr);
  2291. return;
  2292. }
  2293. offset = data.header_length + data.block_descriptor_length;
  2294. if ((buffer[offset] & 0x3f) != 0x0a) {
  2295. sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
  2296. return;
  2297. }
  2298. if ((buffer[offset + 5] & 0x80) == 0)
  2299. return;
  2300. sdkp->ATO = 1;
  2301. return;
  2302. }
  2303. /**
  2304. * sd_read_block_limits - Query disk device for preferred I/O sizes.
  2305. * @disk: disk to query
  2306. */
  2307. static void sd_read_block_limits(struct scsi_disk *sdkp)
  2308. {
  2309. unsigned int sector_sz = sdkp->device->sector_size;
  2310. const int vpd_len = 64;
  2311. unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
  2312. if (!buffer ||
  2313. /* Block Limits VPD */
  2314. scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
  2315. goto out;
  2316. blk_queue_io_min(sdkp->disk->queue,
  2317. get_unaligned_be16(&buffer[6]) * sector_sz);
  2318. sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]);
  2319. sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]);
  2320. if (buffer[3] == 0x3c) {
  2321. unsigned int lba_count, desc_count;
  2322. sdkp->max_ws_blocks = (u32)get_unaligned_be64(&buffer[36]);
  2323. if (!sdkp->lbpme)
  2324. goto out;
  2325. lba_count = get_unaligned_be32(&buffer[20]);
  2326. desc_count = get_unaligned_be32(&buffer[24]);
  2327. if (lba_count && desc_count)
  2328. sdkp->max_unmap_blocks = lba_count;
  2329. sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
  2330. if (buffer[32] & 0x80)
  2331. sdkp->unmap_alignment =
  2332. get_unaligned_be32(&buffer[32]) & ~(1 << 31);
  2333. if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
  2334. if (sdkp->max_unmap_blocks)
  2335. sd_config_discard(sdkp, SD_LBP_UNMAP);
  2336. else
  2337. sd_config_discard(sdkp, SD_LBP_WS16);
  2338. } else { /* LBP VPD page tells us what to use */
  2339. if (sdkp->lbpu && sdkp->max_unmap_blocks && !sdkp->lbprz)
  2340. sd_config_discard(sdkp, SD_LBP_UNMAP);
  2341. else if (sdkp->lbpws)
  2342. sd_config_discard(sdkp, SD_LBP_WS16);
  2343. else if (sdkp->lbpws10)
  2344. sd_config_discard(sdkp, SD_LBP_WS10);
  2345. else if (sdkp->lbpu && sdkp->max_unmap_blocks)
  2346. sd_config_discard(sdkp, SD_LBP_UNMAP);
  2347. else
  2348. sd_config_discard(sdkp, SD_LBP_DISABLE);
  2349. }
  2350. }
  2351. out:
  2352. kfree(buffer);
  2353. }
  2354. /**
  2355. * sd_read_block_characteristics - Query block dev. characteristics
  2356. * @disk: disk to query
  2357. */
  2358. static void sd_read_block_characteristics(struct scsi_disk *sdkp)
  2359. {
  2360. unsigned char *buffer;
  2361. u16 rot;
  2362. const int vpd_len = 64;
  2363. buffer = kmalloc(vpd_len, GFP_KERNEL);
  2364. if (!buffer ||
  2365. /* Block Device Characteristics VPD */
  2366. scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
  2367. goto out;
  2368. rot = get_unaligned_be16(&buffer[4]);
  2369. if (rot == 1) {
  2370. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
  2371. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, sdkp->disk->queue);
  2372. }
  2373. out:
  2374. kfree(buffer);
  2375. }
  2376. /**
  2377. * sd_read_block_provisioning - Query provisioning VPD page
  2378. * @disk: disk to query
  2379. */
  2380. static void sd_read_block_provisioning(struct scsi_disk *sdkp)
  2381. {
  2382. unsigned char *buffer;
  2383. const int vpd_len = 8;
  2384. if (sdkp->lbpme == 0)
  2385. return;
  2386. buffer = kmalloc(vpd_len, GFP_KERNEL);
  2387. if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
  2388. goto out;
  2389. sdkp->lbpvpd = 1;
  2390. sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
  2391. sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
  2392. sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
  2393. out:
  2394. kfree(buffer);
  2395. }
  2396. static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)
  2397. {
  2398. struct scsi_device *sdev = sdkp->device;
  2399. if (sdev->host->no_write_same) {
  2400. sdev->no_write_same = 1;
  2401. return;
  2402. }
  2403. if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY) < 0) {
  2404. /* too large values might cause issues with arcmsr */
  2405. int vpd_buf_len = 64;
  2406. sdev->no_report_opcodes = 1;
  2407. /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
  2408. * CODES is unsupported and the device has an ATA
  2409. * Information VPD page (SAT).
  2410. */
  2411. if (!scsi_get_vpd_page(sdev, 0x89, buffer, vpd_buf_len))
  2412. sdev->no_write_same = 1;
  2413. }
  2414. if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16) == 1)
  2415. sdkp->ws16 = 1;
  2416. if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME) == 1)
  2417. sdkp->ws10 = 1;
  2418. }
  2419. static int sd_try_extended_inquiry(struct scsi_device *sdp)
  2420. {
  2421. /* Attempt VPD inquiry if the device blacklist explicitly calls
  2422. * for it.
  2423. */
  2424. if (sdp->try_vpd_pages)
  2425. return 1;
  2426. /*
  2427. * Although VPD inquiries can go to SCSI-2 type devices,
  2428. * some USB ones crash on receiving them, and the pages
  2429. * we currently ask for are for SPC-3 and beyond
  2430. */
  2431. if (sdp->scsi_level > SCSI_SPC_2 && !sdp->skip_vpd_pages)
  2432. return 1;
  2433. return 0;
  2434. }
  2435. /**
  2436. * sd_revalidate_disk - called the first time a new disk is seen,
  2437. * performs disk spin up, read_capacity, etc.
  2438. * @disk: struct gendisk we care about
  2439. **/
  2440. static int sd_revalidate_disk(struct gendisk *disk)
  2441. {
  2442. struct scsi_disk *sdkp = scsi_disk(disk);
  2443. struct scsi_device *sdp = sdkp->device;
  2444. struct request_queue *q = sdkp->disk->queue;
  2445. unsigned char *buffer;
  2446. unsigned int dev_max, rw_max;
  2447. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
  2448. "sd_revalidate_disk\n"));
  2449. /*
  2450. * If the device is offline, don't try and read capacity or any
  2451. * of the other niceties.
  2452. */
  2453. if (!scsi_device_online(sdp))
  2454. goto out;
  2455. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
  2456. if (!buffer) {
  2457. sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  2458. "allocation failure.\n");
  2459. goto out;
  2460. }
  2461. sd_spinup_disk(sdkp);
  2462. /*
  2463. * Without media there is no reason to ask; moreover, some devices
  2464. * react badly if we do.
  2465. */
  2466. if (sdkp->media_present) {
  2467. sd_read_capacity(sdkp, buffer);
  2468. if (sd_try_extended_inquiry(sdp)) {
  2469. sd_read_block_provisioning(sdkp);
  2470. sd_read_block_limits(sdkp);
  2471. sd_read_block_characteristics(sdkp);
  2472. }
  2473. sd_read_write_protect_flag(sdkp, buffer);
  2474. sd_read_cache_type(sdkp, buffer);
  2475. sd_read_app_tag_own(sdkp, buffer);
  2476. sd_read_write_same(sdkp, buffer);
  2477. }
  2478. /*
  2479. * We now have all cache related info, determine how we deal
  2480. * with flush requests.
  2481. */
  2482. sd_set_flush_flag(sdkp);
  2483. /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
  2484. dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
  2485. /* Some devices report a maximum block count for READ/WRITE requests. */
  2486. dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
  2487. q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
  2488. /*
  2489. * Determine the device's preferred I/O size for reads and writes
  2490. * unless the reported value is unreasonably small, large, or
  2491. * garbage.
  2492. */
  2493. if (sdkp->opt_xfer_blocks &&
  2494. sdkp->opt_xfer_blocks <= dev_max &&
  2495. sdkp->opt_xfer_blocks <= SD_DEF_XFER_BLOCKS &&
  2496. logical_to_bytes(sdp, sdkp->opt_xfer_blocks) >= PAGE_CACHE_SIZE) {
  2497. q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
  2498. rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
  2499. } else
  2500. rw_max = min_not_zero(logical_to_sectors(sdp, dev_max),
  2501. (sector_t)BLK_DEF_MAX_SECTORS);
  2502. /* Do not exceed controller limit */
  2503. rw_max = min(rw_max, queue_max_hw_sectors(q));
  2504. /*
  2505. * Only update max_sectors if previously unset or if the current value
  2506. * exceeds the capabilities of the hardware.
  2507. */
  2508. if (sdkp->first_scan ||
  2509. q->limits.max_sectors > q->limits.max_dev_sectors ||
  2510. q->limits.max_sectors > q->limits.max_hw_sectors)
  2511. q->limits.max_sectors = rw_max;
  2512. sdkp->first_scan = 0;
  2513. set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
  2514. sd_config_write_same(sdkp);
  2515. kfree(buffer);
  2516. out:
  2517. return 0;
  2518. }
  2519. /**
  2520. * sd_unlock_native_capacity - unlock native capacity
  2521. * @disk: struct gendisk to set capacity for
  2522. *
  2523. * Block layer calls this function if it detects that partitions
  2524. * on @disk reach beyond the end of the device. If the SCSI host
  2525. * implements ->unlock_native_capacity() method, it's invoked to
  2526. * give it a chance to adjust the device capacity.
  2527. *
  2528. * CONTEXT:
  2529. * Defined by block layer. Might sleep.
  2530. */
  2531. static void sd_unlock_native_capacity(struct gendisk *disk)
  2532. {
  2533. struct scsi_device *sdev = scsi_disk(disk)->device;
  2534. if (sdev->host->hostt->unlock_native_capacity)
  2535. sdev->host->hostt->unlock_native_capacity(sdev);
  2536. }
  2537. /**
  2538. * sd_format_disk_name - format disk name
  2539. * @prefix: name prefix - ie. "sd" for SCSI disks
  2540. * @index: index of the disk to format name for
  2541. * @buf: output buffer
  2542. * @buflen: length of the output buffer
  2543. *
  2544. * SCSI disk names starts at sda. The 26th device is sdz and the
  2545. * 27th is sdaa. The last one for two lettered suffix is sdzz
  2546. * which is followed by sdaaa.
  2547. *
  2548. * This is basically 26 base counting with one extra 'nil' entry
  2549. * at the beginning from the second digit on and can be
  2550. * determined using similar method as 26 base conversion with the
  2551. * index shifted -1 after each digit is computed.
  2552. *
  2553. * CONTEXT:
  2554. * Don't care.
  2555. *
  2556. * RETURNS:
  2557. * 0 on success, -errno on failure.
  2558. */
  2559. static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  2560. {
  2561. const int base = 'z' - 'a' + 1;
  2562. char *begin = buf + strlen(prefix);
  2563. char *end = buf + buflen;
  2564. char *p;
  2565. int unit;
  2566. p = end - 1;
  2567. *p = '\0';
  2568. unit = base;
  2569. do {
  2570. if (p == begin)
  2571. return -EINVAL;
  2572. *--p = 'a' + (index % unit);
  2573. index = (index / unit) - 1;
  2574. } while (index >= 0);
  2575. memmove(begin, p, end - p);
  2576. memcpy(buf, prefix, strlen(prefix));
  2577. return 0;
  2578. }
  2579. /*
  2580. * The asynchronous part of sd_probe
  2581. */
  2582. static void sd_probe_async(void *data, async_cookie_t cookie)
  2583. {
  2584. struct scsi_disk *sdkp = data;
  2585. struct scsi_device *sdp;
  2586. struct gendisk *gd;
  2587. u32 index;
  2588. struct device *dev;
  2589. sdp = sdkp->device;
  2590. gd = sdkp->disk;
  2591. index = sdkp->index;
  2592. dev = &sdp->sdev_gendev;
  2593. gd->major = sd_major((index & 0xf0) >> 4);
  2594. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  2595. gd->minors = SD_MINORS;
  2596. gd->fops = &sd_fops;
  2597. gd->private_data = &sdkp->driver;
  2598. gd->queue = sdkp->device->request_queue;
  2599. /* defaults, until the device tells us otherwise */
  2600. sdp->sector_size = 512;
  2601. sdkp->capacity = 0;
  2602. sdkp->media_present = 1;
  2603. sdkp->write_prot = 0;
  2604. sdkp->cache_override = 0;
  2605. sdkp->WCE = 0;
  2606. sdkp->RCD = 0;
  2607. sdkp->ATO = 0;
  2608. sdkp->first_scan = 1;
  2609. sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
  2610. sd_revalidate_disk(gd);
  2611. gd->driverfs_dev = &sdp->sdev_gendev;
  2612. gd->flags = GENHD_FL_EXT_DEVT;
  2613. if (sdp->removable) {
  2614. gd->flags |= GENHD_FL_REMOVABLE;
  2615. gd->events |= DISK_EVENT_MEDIA_CHANGE;
  2616. }
  2617. blk_pm_runtime_init(sdp->request_queue, dev);
  2618. add_disk(gd);
  2619. if (sdkp->capacity)
  2620. sd_dif_config_host(sdkp);
  2621. sd_revalidate_disk(gd);
  2622. sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  2623. sdp->removable ? "removable " : "");
  2624. scsi_autopm_put_device(sdp);
  2625. put_device(&sdkp->dev);
  2626. }
  2627. /**
  2628. * sd_probe - called during driver initialization and whenever a
  2629. * new scsi device is attached to the system. It is called once
  2630. * for each scsi device (not just disks) present.
  2631. * @dev: pointer to device object
  2632. *
  2633. * Returns 0 if successful (or not interested in this scsi device
  2634. * (e.g. scanner)); 1 when there is an error.
  2635. *
  2636. * Note: this function is invoked from the scsi mid-level.
  2637. * This function sets up the mapping between a given
  2638. * <host,channel,id,lun> (found in sdp) and new device name
  2639. * (e.g. /dev/sda). More precisely it is the block device major
  2640. * and minor number that is chosen here.
  2641. *
  2642. * Assume sd_probe is not re-entrant (for time being)
  2643. * Also think about sd_probe() and sd_remove() running coincidentally.
  2644. **/
  2645. static int sd_probe(struct device *dev)
  2646. {
  2647. struct scsi_device *sdp = to_scsi_device(dev);
  2648. struct scsi_disk *sdkp;
  2649. struct gendisk *gd;
  2650. int index;
  2651. int error;
  2652. scsi_autopm_get_device(sdp);
  2653. error = -ENODEV;
  2654. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  2655. goto out;
  2656. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  2657. "sd_probe\n"));
  2658. error = -ENOMEM;
  2659. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  2660. if (!sdkp)
  2661. goto out;
  2662. gd = alloc_disk(SD_MINORS);
  2663. if (!gd)
  2664. goto out_free;
  2665. do {
  2666. if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
  2667. goto out_put;
  2668. spin_lock(&sd_index_lock);
  2669. error = ida_get_new(&sd_index_ida, &index);
  2670. spin_unlock(&sd_index_lock);
  2671. } while (error == -EAGAIN);
  2672. if (error) {
  2673. sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
  2674. goto out_put;
  2675. }
  2676. error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  2677. if (error) {
  2678. sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
  2679. goto out_free_index;
  2680. }
  2681. sdkp->device = sdp;
  2682. sdkp->driver = &sd_template;
  2683. sdkp->disk = gd;
  2684. sdkp->index = index;
  2685. atomic_set(&sdkp->openers, 0);
  2686. atomic_set(&sdkp->device->ioerr_cnt, 0);
  2687. if (!sdp->request_queue->rq_timeout) {
  2688. if (sdp->type != TYPE_MOD)
  2689. blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  2690. else
  2691. blk_queue_rq_timeout(sdp->request_queue,
  2692. SD_MOD_TIMEOUT);
  2693. }
  2694. device_initialize(&sdkp->dev);
  2695. sdkp->dev.parent = dev;
  2696. sdkp->dev.class = &sd_disk_class;
  2697. dev_set_name(&sdkp->dev, "%s", dev_name(dev));
  2698. error = device_add(&sdkp->dev);
  2699. if (error)
  2700. goto out_free_index;
  2701. get_device(dev);
  2702. dev_set_drvdata(dev, sdkp);
  2703. get_device(&sdkp->dev); /* prevent release before async_schedule */
  2704. async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
  2705. return 0;
  2706. out_free_index:
  2707. spin_lock(&sd_index_lock);
  2708. ida_remove(&sd_index_ida, index);
  2709. spin_unlock(&sd_index_lock);
  2710. out_put:
  2711. put_disk(gd);
  2712. out_free:
  2713. kfree(sdkp);
  2714. out:
  2715. scsi_autopm_put_device(sdp);
  2716. return error;
  2717. }
  2718. /**
  2719. * sd_remove - called whenever a scsi disk (previously recognized by
  2720. * sd_probe) is detached from the system. It is called (potentially
  2721. * multiple times) during sd module unload.
  2722. * @sdp: pointer to mid level scsi device object
  2723. *
  2724. * Note: this function is invoked from the scsi mid-level.
  2725. * This function potentially frees up a device name (e.g. /dev/sdc)
  2726. * that could be re-used by a subsequent sd_probe().
  2727. * This function is not called when the built-in sd driver is "exit-ed".
  2728. **/
  2729. static int sd_remove(struct device *dev)
  2730. {
  2731. struct scsi_disk *sdkp;
  2732. dev_t devt;
  2733. sdkp = dev_get_drvdata(dev);
  2734. devt = disk_devt(sdkp->disk);
  2735. scsi_autopm_get_device(sdkp->device);
  2736. async_synchronize_full_domain(&scsi_sd_pm_domain);
  2737. async_synchronize_full_domain(&scsi_sd_probe_domain);
  2738. device_del(&sdkp->dev);
  2739. del_gendisk(sdkp->disk);
  2740. sd_shutdown(dev);
  2741. blk_register_region(devt, SD_MINORS, NULL,
  2742. sd_default_probe, NULL, NULL);
  2743. mutex_lock(&sd_ref_mutex);
  2744. dev_set_drvdata(dev, NULL);
  2745. put_device(&sdkp->dev);
  2746. mutex_unlock(&sd_ref_mutex);
  2747. return 0;
  2748. }
  2749. /**
  2750. * scsi_disk_release - Called to free the scsi_disk structure
  2751. * @dev: pointer to embedded class device
  2752. *
  2753. * sd_ref_mutex must be held entering this routine. Because it is
  2754. * called on last put, you should always use the scsi_disk_get()
  2755. * scsi_disk_put() helpers which manipulate the semaphore directly
  2756. * and never do a direct put_device.
  2757. **/
  2758. static void scsi_disk_release(struct device *dev)
  2759. {
  2760. struct scsi_disk *sdkp = to_scsi_disk(dev);
  2761. struct gendisk *disk = sdkp->disk;
  2762. struct request_queue *q = disk->queue;
  2763. spin_lock(&sd_index_lock);
  2764. ida_remove(&sd_index_ida, sdkp->index);
  2765. spin_unlock(&sd_index_lock);
  2766. /*
  2767. * Wait until all requests that are in progress have completed.
  2768. * This is necessary to avoid that e.g. scsi_end_request() crashes
  2769. * due to clearing the disk->private_data pointer. Wait from inside
  2770. * scsi_disk_release() instead of from sd_release() to avoid that
  2771. * freezing and unfreezing the request queue affects user space I/O
  2772. * in case multiple processes open a /dev/sd... node concurrently.
  2773. */
  2774. blk_mq_freeze_queue(q);
  2775. blk_mq_unfreeze_queue(q);
  2776. disk->private_data = NULL;
  2777. put_disk(disk);
  2778. put_device(&sdkp->device->sdev_gendev);
  2779. kfree(sdkp);
  2780. }
  2781. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  2782. {
  2783. unsigned char cmd[6] = { START_STOP }; /* START_VALID */
  2784. struct scsi_sense_hdr sshdr;
  2785. struct scsi_device *sdp = sdkp->device;
  2786. int res;
  2787. if (start)
  2788. cmd[4] |= 1; /* START */
  2789. if (sdp->start_stop_pwr_cond)
  2790. cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
  2791. if (!scsi_device_online(sdp))
  2792. return -ENODEV;
  2793. res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  2794. SD_TIMEOUT, SD_MAX_RETRIES, NULL, REQ_PM);
  2795. if (res) {
  2796. sd_print_result(sdkp, "Start/Stop Unit failed", res);
  2797. if (driver_byte(res) & DRIVER_SENSE)
  2798. sd_print_sense_hdr(sdkp, &sshdr);
  2799. if (scsi_sense_valid(&sshdr) &&
  2800. /* 0x3a is medium not present */
  2801. sshdr.asc == 0x3a)
  2802. res = 0;
  2803. }
  2804. /* SCSI error codes must not go to the generic layer */
  2805. if (res)
  2806. return -EIO;
  2807. return 0;
  2808. }
  2809. /*
  2810. * Send a SYNCHRONIZE CACHE instruction down to the device through
  2811. * the normal SCSI command structure. Wait for the command to
  2812. * complete.
  2813. */
  2814. static void sd_shutdown(struct device *dev)
  2815. {
  2816. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  2817. if (!sdkp)
  2818. return; /* this can happen */
  2819. if (pm_runtime_suspended(dev))
  2820. return;
  2821. if (sdkp->WCE && sdkp->media_present) {
  2822. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2823. sd_sync_cache(sdkp);
  2824. }
  2825. if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  2826. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2827. sd_start_stop_device(sdkp, 0);
  2828. }
  2829. }
  2830. static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
  2831. {
  2832. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  2833. int ret = 0;
  2834. if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
  2835. return 0;
  2836. if (sdkp->WCE && sdkp->media_present) {
  2837. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2838. ret = sd_sync_cache(sdkp);
  2839. if (ret) {
  2840. /* ignore OFFLINE device */
  2841. if (ret == -ENODEV)
  2842. ret = 0;
  2843. goto done;
  2844. }
  2845. }
  2846. if (sdkp->device->manage_start_stop) {
  2847. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2848. /* an error is not worth aborting a system sleep */
  2849. ret = sd_start_stop_device(sdkp, 0);
  2850. if (ignore_stop_errors)
  2851. ret = 0;
  2852. }
  2853. done:
  2854. return ret;
  2855. }
  2856. static int sd_suspend_system(struct device *dev)
  2857. {
  2858. return sd_suspend_common(dev, true);
  2859. }
  2860. static int sd_suspend_runtime(struct device *dev)
  2861. {
  2862. return sd_suspend_common(dev, false);
  2863. }
  2864. static int sd_resume(struct device *dev)
  2865. {
  2866. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  2867. if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
  2868. return 0;
  2869. if (!sdkp->device->manage_start_stop)
  2870. return 0;
  2871. sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  2872. return sd_start_stop_device(sdkp, 1);
  2873. }
  2874. /**
  2875. * init_sd - entry point for this driver (both when built in or when
  2876. * a module).
  2877. *
  2878. * Note: this function registers this driver with the scsi mid-level.
  2879. **/
  2880. static int __init init_sd(void)
  2881. {
  2882. int majors = 0, i, err;
  2883. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  2884. for (i = 0; i < SD_MAJORS; i++) {
  2885. if (register_blkdev(sd_major(i), "sd") != 0)
  2886. continue;
  2887. majors++;
  2888. blk_register_region(sd_major(i), SD_MINORS, NULL,
  2889. sd_default_probe, NULL, NULL);
  2890. }
  2891. if (!majors)
  2892. return -ENODEV;
  2893. err = class_register(&sd_disk_class);
  2894. if (err)
  2895. goto err_out;
  2896. sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  2897. 0, 0, NULL);
  2898. if (!sd_cdb_cache) {
  2899. printk(KERN_ERR "sd: can't init extended cdb cache\n");
  2900. err = -ENOMEM;
  2901. goto err_out_class;
  2902. }
  2903. sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  2904. if (!sd_cdb_pool) {
  2905. printk(KERN_ERR "sd: can't init extended cdb pool\n");
  2906. err = -ENOMEM;
  2907. goto err_out_cache;
  2908. }
  2909. err = scsi_register_driver(&sd_template.gendrv);
  2910. if (err)
  2911. goto err_out_driver;
  2912. return 0;
  2913. err_out_driver:
  2914. mempool_destroy(sd_cdb_pool);
  2915. err_out_cache:
  2916. kmem_cache_destroy(sd_cdb_cache);
  2917. err_out_class:
  2918. class_unregister(&sd_disk_class);
  2919. err_out:
  2920. for (i = 0; i < SD_MAJORS; i++)
  2921. unregister_blkdev(sd_major(i), "sd");
  2922. return err;
  2923. }
  2924. /**
  2925. * exit_sd - exit point for this driver (when it is a module).
  2926. *
  2927. * Note: this function unregisters this driver from the scsi mid-level.
  2928. **/
  2929. static void __exit exit_sd(void)
  2930. {
  2931. int i;
  2932. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  2933. scsi_unregister_driver(&sd_template.gendrv);
  2934. mempool_destroy(sd_cdb_pool);
  2935. kmem_cache_destroy(sd_cdb_cache);
  2936. class_unregister(&sd_disk_class);
  2937. for (i = 0; i < SD_MAJORS; i++) {
  2938. blk_unregister_region(sd_major(i), SD_MINORS);
  2939. unregister_blkdev(sd_major(i), "sd");
  2940. }
  2941. }
  2942. module_init(init_sd);
  2943. module_exit(exit_sd);
  2944. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  2945. struct scsi_sense_hdr *sshdr)
  2946. {
  2947. scsi_print_sense_hdr(sdkp->device,
  2948. sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
  2949. }
  2950. static void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
  2951. int result)
  2952. {
  2953. const char *hb_string = scsi_hostbyte_string(result);
  2954. const char *db_string = scsi_driverbyte_string(result);
  2955. if (hb_string || db_string)
  2956. sd_printk(KERN_INFO, sdkp,
  2957. "%s: Result: hostbyte=%s driverbyte=%s\n", msg,
  2958. hb_string ? hb_string : "invalid",
  2959. db_string ? db_string : "invalid");
  2960. else
  2961. sd_printk(KERN_INFO, sdkp,
  2962. "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
  2963. msg, host_byte(result), driver_byte(result));
  2964. }