sd.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804
  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);
  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. error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  2936. if (error) {
  2937. sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
  2938. goto out_free_index;
  2939. }
  2940. sdkp->device = sdp;
  2941. sdkp->driver = &sd_template;
  2942. sdkp->disk = gd;
  2943. sdkp->index = index;
  2944. atomic_set(&sdkp->openers, 0);
  2945. atomic_set(&sdkp->device->ioerr_cnt, 0);
  2946. if (!sdp->request_queue->rq_timeout) {
  2947. if (sdp->type != TYPE_MOD)
  2948. blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  2949. else
  2950. blk_queue_rq_timeout(sdp->request_queue,
  2951. SD_MOD_TIMEOUT);
  2952. }
  2953. device_initialize(&sdkp->dev);
  2954. sdkp->dev.parent = dev;
  2955. sdkp->dev.class = &sd_disk_class;
  2956. dev_set_name(&sdkp->dev, "%s", dev_name(dev));
  2957. error = device_add(&sdkp->dev);
  2958. if (error)
  2959. goto out_free_index;
  2960. get_device(dev);
  2961. dev_set_drvdata(dev, sdkp);
  2962. get_device(&sdkp->dev); /* prevent release before async_schedule */
  2963. async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
  2964. return 0;
  2965. out_free_index:
  2966. ida_free(&sd_index_ida, index);
  2967. out_put:
  2968. put_disk(gd);
  2969. out_free:
  2970. kfree(sdkp);
  2971. out:
  2972. scsi_autopm_put_device(sdp);
  2973. return error;
  2974. }
  2975. /**
  2976. * sd_remove - called whenever a scsi disk (previously recognized by
  2977. * sd_probe) is detached from the system. It is called (potentially
  2978. * multiple times) during sd module unload.
  2979. * @dev: pointer to device object
  2980. *
  2981. * Note: this function is invoked from the scsi mid-level.
  2982. * This function potentially frees up a device name (e.g. /dev/sdc)
  2983. * that could be re-used by a subsequent sd_probe().
  2984. * This function is not called when the built-in sd driver is "exit-ed".
  2985. **/
  2986. static int sd_remove(struct device *dev)
  2987. {
  2988. struct scsi_disk *sdkp;
  2989. dev_t devt;
  2990. sdkp = dev_get_drvdata(dev);
  2991. devt = disk_devt(sdkp->disk);
  2992. scsi_autopm_get_device(sdkp->device);
  2993. async_synchronize_full_domain(&scsi_sd_pm_domain);
  2994. async_synchronize_full_domain(&scsi_sd_probe_domain);
  2995. device_del(&sdkp->dev);
  2996. del_gendisk(sdkp->disk);
  2997. sd_shutdown(dev);
  2998. sd_zbc_remove(sdkp);
  2999. free_opal_dev(sdkp->opal_dev);
  3000. blk_register_region(devt, SD_MINORS, NULL,
  3001. sd_default_probe, NULL, NULL);
  3002. mutex_lock(&sd_ref_mutex);
  3003. dev_set_drvdata(dev, NULL);
  3004. put_device(&sdkp->dev);
  3005. mutex_unlock(&sd_ref_mutex);
  3006. return 0;
  3007. }
  3008. /**
  3009. * scsi_disk_release - Called to free the scsi_disk structure
  3010. * @dev: pointer to embedded class device
  3011. *
  3012. * sd_ref_mutex must be held entering this routine. Because it is
  3013. * called on last put, you should always use the scsi_disk_get()
  3014. * scsi_disk_put() helpers which manipulate the semaphore directly
  3015. * and never do a direct put_device.
  3016. **/
  3017. static void scsi_disk_release(struct device *dev)
  3018. {
  3019. struct scsi_disk *sdkp = to_scsi_disk(dev);
  3020. struct gendisk *disk = sdkp->disk;
  3021. struct request_queue *q = disk->queue;
  3022. ida_free(&sd_index_ida, sdkp->index);
  3023. /*
  3024. * Wait until all requests that are in progress have completed.
  3025. * This is necessary to avoid that e.g. scsi_end_request() crashes
  3026. * due to clearing the disk->private_data pointer. Wait from inside
  3027. * scsi_disk_release() instead of from sd_release() to avoid that
  3028. * freezing and unfreezing the request queue affects user space I/O
  3029. * in case multiple processes open a /dev/sd... node concurrently.
  3030. */
  3031. blk_mq_freeze_queue(q);
  3032. blk_mq_unfreeze_queue(q);
  3033. disk->private_data = NULL;
  3034. put_disk(disk);
  3035. put_device(&sdkp->device->sdev_gendev);
  3036. kfree(sdkp);
  3037. }
  3038. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  3039. {
  3040. unsigned char cmd[6] = { START_STOP }; /* START_VALID */
  3041. struct scsi_sense_hdr sshdr;
  3042. struct scsi_device *sdp = sdkp->device;
  3043. int res;
  3044. if (start)
  3045. cmd[4] |= 1; /* START */
  3046. if (sdp->start_stop_pwr_cond)
  3047. cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
  3048. if (!scsi_device_online(sdp))
  3049. return -ENODEV;
  3050. res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
  3051. SD_TIMEOUT, SD_MAX_RETRIES, 0, RQF_PM, NULL);
  3052. if (res) {
  3053. sd_print_result(sdkp, "Start/Stop Unit failed", res);
  3054. if (driver_byte(res) == DRIVER_SENSE)
  3055. sd_print_sense_hdr(sdkp, &sshdr);
  3056. if (scsi_sense_valid(&sshdr) &&
  3057. /* 0x3a is medium not present */
  3058. sshdr.asc == 0x3a)
  3059. res = 0;
  3060. }
  3061. /* SCSI error codes must not go to the generic layer */
  3062. if (res)
  3063. return -EIO;
  3064. return 0;
  3065. }
  3066. /*
  3067. * Send a SYNCHRONIZE CACHE instruction down to the device through
  3068. * the normal SCSI command structure. Wait for the command to
  3069. * complete.
  3070. */
  3071. static void sd_shutdown(struct device *dev)
  3072. {
  3073. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  3074. if (!sdkp)
  3075. return; /* this can happen */
  3076. if (pm_runtime_suspended(dev))
  3077. return;
  3078. if (sdkp->WCE && sdkp->media_present) {
  3079. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  3080. sd_sync_cache(sdkp, NULL);
  3081. }
  3082. if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  3083. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  3084. sd_start_stop_device(sdkp, 0);
  3085. }
  3086. }
  3087. static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
  3088. {
  3089. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  3090. struct scsi_sense_hdr sshdr;
  3091. int ret = 0;
  3092. if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
  3093. return 0;
  3094. if (sdkp->WCE && sdkp->media_present) {
  3095. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  3096. ret = sd_sync_cache(sdkp, &sshdr);
  3097. if (ret) {
  3098. /* ignore OFFLINE device */
  3099. if (ret == -ENODEV)
  3100. return 0;
  3101. if (!scsi_sense_valid(&sshdr) ||
  3102. sshdr.sense_key != ILLEGAL_REQUEST)
  3103. return ret;
  3104. /*
  3105. * sshdr.sense_key == ILLEGAL_REQUEST means this drive
  3106. * doesn't support sync. There's not much to do and
  3107. * suspend shouldn't fail.
  3108. */
  3109. ret = 0;
  3110. }
  3111. }
  3112. if (sdkp->device->manage_start_stop) {
  3113. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  3114. /* an error is not worth aborting a system sleep */
  3115. ret = sd_start_stop_device(sdkp, 0);
  3116. if (ignore_stop_errors)
  3117. ret = 0;
  3118. }
  3119. return ret;
  3120. }
  3121. static int sd_suspend_system(struct device *dev)
  3122. {
  3123. return sd_suspend_common(dev, true);
  3124. }
  3125. static int sd_suspend_runtime(struct device *dev)
  3126. {
  3127. return sd_suspend_common(dev, false);
  3128. }
  3129. static int sd_resume(struct device *dev)
  3130. {
  3131. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  3132. int ret;
  3133. if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
  3134. return 0;
  3135. if (!sdkp->device->manage_start_stop)
  3136. return 0;
  3137. sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  3138. ret = sd_start_stop_device(sdkp, 1);
  3139. if (!ret)
  3140. opal_unlock_from_suspend(sdkp->opal_dev);
  3141. return ret;
  3142. }
  3143. /**
  3144. * init_sd - entry point for this driver (both when built in or when
  3145. * a module).
  3146. *
  3147. * Note: this function registers this driver with the scsi mid-level.
  3148. **/
  3149. static int __init init_sd(void)
  3150. {
  3151. int majors = 0, i, err;
  3152. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  3153. for (i = 0; i < SD_MAJORS; i++) {
  3154. if (register_blkdev(sd_major(i), "sd") != 0)
  3155. continue;
  3156. majors++;
  3157. blk_register_region(sd_major(i), SD_MINORS, NULL,
  3158. sd_default_probe, NULL, NULL);
  3159. }
  3160. if (!majors)
  3161. return -ENODEV;
  3162. err = class_register(&sd_disk_class);
  3163. if (err)
  3164. goto err_out;
  3165. sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  3166. 0, 0, NULL);
  3167. if (!sd_cdb_cache) {
  3168. printk(KERN_ERR "sd: can't init extended cdb cache\n");
  3169. err = -ENOMEM;
  3170. goto err_out_class;
  3171. }
  3172. sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  3173. if (!sd_cdb_pool) {
  3174. printk(KERN_ERR "sd: can't init extended cdb pool\n");
  3175. err = -ENOMEM;
  3176. goto err_out_cache;
  3177. }
  3178. sd_page_pool = mempool_create_page_pool(SD_MEMPOOL_SIZE, 0);
  3179. if (!sd_page_pool) {
  3180. printk(KERN_ERR "sd: can't init discard page pool\n");
  3181. err = -ENOMEM;
  3182. goto err_out_ppool;
  3183. }
  3184. err = scsi_register_driver(&sd_template.gendrv);
  3185. if (err)
  3186. goto err_out_driver;
  3187. return 0;
  3188. err_out_driver:
  3189. mempool_destroy(sd_page_pool);
  3190. err_out_ppool:
  3191. mempool_destroy(sd_cdb_pool);
  3192. err_out_cache:
  3193. kmem_cache_destroy(sd_cdb_cache);
  3194. err_out_class:
  3195. class_unregister(&sd_disk_class);
  3196. err_out:
  3197. for (i = 0; i < SD_MAJORS; i++)
  3198. unregister_blkdev(sd_major(i), "sd");
  3199. return err;
  3200. }
  3201. /**
  3202. * exit_sd - exit point for this driver (when it is a module).
  3203. *
  3204. * Note: this function unregisters this driver from the scsi mid-level.
  3205. **/
  3206. static void __exit exit_sd(void)
  3207. {
  3208. int i;
  3209. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  3210. scsi_unregister_driver(&sd_template.gendrv);
  3211. mempool_destroy(sd_cdb_pool);
  3212. mempool_destroy(sd_page_pool);
  3213. kmem_cache_destroy(sd_cdb_cache);
  3214. class_unregister(&sd_disk_class);
  3215. for (i = 0; i < SD_MAJORS; i++) {
  3216. blk_unregister_region(sd_major(i), SD_MINORS);
  3217. unregister_blkdev(sd_major(i), "sd");
  3218. }
  3219. }
  3220. module_init(init_sd);
  3221. module_exit(exit_sd);
  3222. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  3223. struct scsi_sense_hdr *sshdr)
  3224. {
  3225. scsi_print_sense_hdr(sdkp->device,
  3226. sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
  3227. }
  3228. static void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
  3229. int result)
  3230. {
  3231. const char *hb_string = scsi_hostbyte_string(result);
  3232. const char *db_string = scsi_driverbyte_string(result);
  3233. if (hb_string || db_string)
  3234. sd_printk(KERN_INFO, sdkp,
  3235. "%s: Result: hostbyte=%s driverbyte=%s\n", msg,
  3236. hb_string ? hb_string : "invalid",
  3237. db_string ? db_string : "invalid");
  3238. else
  3239. sd_printk(KERN_INFO, sdkp,
  3240. "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
  3241. msg, host_byte(result), driver_byte(result));
  3242. }