sd.c 101 KB

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