sd.c 93 KB

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