sd.c 98 KB

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