sd.c 91 KB

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