sd.c 94 KB

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