sd.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  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 <asm/uaccess.h>
  53. #include <asm/unaligned.h>
  54. #include <scsi/scsi.h>
  55. #include <scsi/scsi_cmnd.h>
  56. #include <scsi/scsi_dbg.h>
  57. #include <scsi/scsi_device.h>
  58. #include <scsi/scsi_driver.h>
  59. #include <scsi/scsi_eh.h>
  60. #include <scsi/scsi_host.h>
  61. #include <scsi/scsi_ioctl.h>
  62. #include <scsi/scsicam.h>
  63. #include "sd.h"
  64. #include "scsi_logging.h"
  65. MODULE_AUTHOR("Eric Youngdale");
  66. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  67. MODULE_LICENSE("GPL");
  68. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  69. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  70. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  71. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  80. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  81. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  82. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  83. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  84. MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
  85. MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
  86. MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
  87. #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
  88. #define SD_MINORS 16
  89. #else
  90. #define SD_MINORS 0
  91. #endif
  92. static int sd_revalidate_disk(struct gendisk *);
  93. static void sd_unlock_native_capacity(struct gendisk *disk);
  94. static int sd_probe(struct device *);
  95. static int sd_remove(struct device *);
  96. static void sd_shutdown(struct device *);
  97. static int sd_suspend(struct device *, pm_message_t state);
  98. static int sd_resume(struct device *);
  99. static void sd_rescan(struct device *);
  100. static int sd_done(struct scsi_cmnd *);
  101. static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
  102. static void scsi_disk_release(struct device *cdev);
  103. static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
  104. static void sd_print_result(struct scsi_disk *, int);
  105. static DEFINE_SPINLOCK(sd_index_lock);
  106. static DEFINE_IDA(sd_index_ida);
  107. /* This semaphore is used to mediate the 0->1 reference get in the
  108. * face of object destruction (i.e. we can't allow a get on an
  109. * object after last put) */
  110. static DEFINE_MUTEX(sd_ref_mutex);
  111. static struct kmem_cache *sd_cdb_cache;
  112. static mempool_t *sd_cdb_pool;
  113. static const char *sd_cache_types[] = {
  114. "write through", "none", "write back",
  115. "write back, no read (daft)"
  116. };
  117. static ssize_t
  118. sd_store_cache_type(struct device *dev, struct device_attribute *attr,
  119. const char *buf, size_t count)
  120. {
  121. int i, ct = -1, rcd, wce, sp;
  122. struct scsi_disk *sdkp = to_scsi_disk(dev);
  123. struct scsi_device *sdp = sdkp->device;
  124. char buffer[64];
  125. char *buffer_data;
  126. struct scsi_mode_data data;
  127. struct scsi_sense_hdr sshdr;
  128. int len;
  129. if (sdp->type != TYPE_DISK)
  130. /* no cache control on RBC devices; theoretically they
  131. * can do it, but there's probably so many exceptions
  132. * it's not worth the risk */
  133. return -EINVAL;
  134. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  135. len = strlen(sd_cache_types[i]);
  136. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  137. buf[len] == '\n') {
  138. ct = i;
  139. break;
  140. }
  141. }
  142. if (ct < 0)
  143. return -EINVAL;
  144. rcd = ct & 0x01 ? 1 : 0;
  145. wce = ct & 0x02 ? 1 : 0;
  146. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  147. SD_MAX_RETRIES, &data, NULL))
  148. return -EINVAL;
  149. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  150. data.block_descriptor_length);
  151. buffer_data = buffer + data.header_length +
  152. data.block_descriptor_length;
  153. buffer_data[2] &= ~0x05;
  154. buffer_data[2] |= wce << 2 | rcd;
  155. sp = buffer_data[0] & 0x80 ? 1 : 0;
  156. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  157. SD_MAX_RETRIES, &data, &sshdr)) {
  158. if (scsi_sense_valid(&sshdr))
  159. sd_print_sense_hdr(sdkp, &sshdr);
  160. return -EINVAL;
  161. }
  162. revalidate_disk(sdkp->disk);
  163. return count;
  164. }
  165. static ssize_t
  166. sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
  167. const char *buf, size_t count)
  168. {
  169. struct scsi_disk *sdkp = to_scsi_disk(dev);
  170. struct scsi_device *sdp = sdkp->device;
  171. if (!capable(CAP_SYS_ADMIN))
  172. return -EACCES;
  173. sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
  174. return count;
  175. }
  176. static ssize_t
  177. sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
  178. const char *buf, size_t count)
  179. {
  180. struct scsi_disk *sdkp = to_scsi_disk(dev);
  181. struct scsi_device *sdp = sdkp->device;
  182. if (!capable(CAP_SYS_ADMIN))
  183. return -EACCES;
  184. if (sdp->type != TYPE_DISK)
  185. return -EINVAL;
  186. sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  187. return count;
  188. }
  189. static ssize_t
  190. sd_show_cache_type(struct device *dev, struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct scsi_disk *sdkp = to_scsi_disk(dev);
  194. int ct = sdkp->RCD + 2*sdkp->WCE;
  195. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  196. }
  197. static ssize_t
  198. sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
  199. {
  200. struct scsi_disk *sdkp = to_scsi_disk(dev);
  201. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  202. }
  203. static ssize_t
  204. sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
  205. char *buf)
  206. {
  207. struct scsi_disk *sdkp = to_scsi_disk(dev);
  208. struct scsi_device *sdp = sdkp->device;
  209. return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
  210. }
  211. static ssize_t
  212. sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
  213. char *buf)
  214. {
  215. struct scsi_disk *sdkp = to_scsi_disk(dev);
  216. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  217. }
  218. static ssize_t
  219. sd_show_protection_type(struct device *dev, struct device_attribute *attr,
  220. char *buf)
  221. {
  222. struct scsi_disk *sdkp = to_scsi_disk(dev);
  223. return snprintf(buf, 20, "%u\n", sdkp->protection_type);
  224. }
  225. static ssize_t
  226. sd_show_protection_mode(struct device *dev, struct device_attribute *attr,
  227. char *buf)
  228. {
  229. struct scsi_disk *sdkp = to_scsi_disk(dev);
  230. struct scsi_device *sdp = sdkp->device;
  231. unsigned int dif, dix;
  232. dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  233. dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
  234. if (!dix && scsi_host_dix_capable(sdp->host, SD_DIF_TYPE0_PROTECTION)) {
  235. dif = 0;
  236. dix = 1;
  237. }
  238. if (!dif && !dix)
  239. return snprintf(buf, 20, "none\n");
  240. return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
  241. }
  242. static ssize_t
  243. sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
  244. char *buf)
  245. {
  246. struct scsi_disk *sdkp = to_scsi_disk(dev);
  247. return snprintf(buf, 20, "%u\n", sdkp->ATO);
  248. }
  249. static ssize_t
  250. sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
  251. char *buf)
  252. {
  253. struct scsi_disk *sdkp = to_scsi_disk(dev);
  254. return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning);
  255. }
  256. static struct device_attribute sd_disk_attrs[] = {
  257. __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
  258. sd_store_cache_type),
  259. __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
  260. __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
  261. sd_store_allow_restart),
  262. __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
  263. sd_store_manage_start_stop),
  264. __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
  265. __ATTR(protection_mode, S_IRUGO, sd_show_protection_mode, NULL),
  266. __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
  267. __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
  268. __ATTR_NULL,
  269. };
  270. static struct class sd_disk_class = {
  271. .name = "scsi_disk",
  272. .owner = THIS_MODULE,
  273. .dev_release = scsi_disk_release,
  274. .dev_attrs = sd_disk_attrs,
  275. };
  276. static struct scsi_driver sd_template = {
  277. .owner = THIS_MODULE,
  278. .gendrv = {
  279. .name = "sd",
  280. .probe = sd_probe,
  281. .remove = sd_remove,
  282. .suspend = sd_suspend,
  283. .resume = sd_resume,
  284. .shutdown = sd_shutdown,
  285. },
  286. .rescan = sd_rescan,
  287. .done = sd_done,
  288. };
  289. /*
  290. * Device no to disk mapping:
  291. *
  292. * major disc2 disc p1
  293. * |............|.............|....|....| <- dev_t
  294. * 31 20 19 8 7 4 3 0
  295. *
  296. * Inside a major, we have 16k disks, however mapped non-
  297. * contiguously. The first 16 disks are for major0, the next
  298. * ones with major1, ... Disk 256 is for major0 again, disk 272
  299. * for major1, ...
  300. * As we stay compatible with our numbering scheme, we can reuse
  301. * the well-know SCSI majors 8, 65--71, 136--143.
  302. */
  303. static int sd_major(int major_idx)
  304. {
  305. switch (major_idx) {
  306. case 0:
  307. return SCSI_DISK0_MAJOR;
  308. case 1 ... 7:
  309. return SCSI_DISK1_MAJOR + major_idx - 1;
  310. case 8 ... 15:
  311. return SCSI_DISK8_MAJOR + major_idx - 8;
  312. default:
  313. BUG();
  314. return 0; /* shut up gcc */
  315. }
  316. }
  317. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  318. {
  319. struct scsi_disk *sdkp = NULL;
  320. if (disk->private_data) {
  321. sdkp = scsi_disk(disk);
  322. if (scsi_device_get(sdkp->device) == 0)
  323. get_device(&sdkp->dev);
  324. else
  325. sdkp = NULL;
  326. }
  327. return sdkp;
  328. }
  329. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  330. {
  331. struct scsi_disk *sdkp;
  332. mutex_lock(&sd_ref_mutex);
  333. sdkp = __scsi_disk_get(disk);
  334. mutex_unlock(&sd_ref_mutex);
  335. return sdkp;
  336. }
  337. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  338. {
  339. struct scsi_disk *sdkp;
  340. mutex_lock(&sd_ref_mutex);
  341. sdkp = dev_get_drvdata(dev);
  342. if (sdkp)
  343. sdkp = __scsi_disk_get(sdkp->disk);
  344. mutex_unlock(&sd_ref_mutex);
  345. return sdkp;
  346. }
  347. static void scsi_disk_put(struct scsi_disk *sdkp)
  348. {
  349. struct scsi_device *sdev = sdkp->device;
  350. mutex_lock(&sd_ref_mutex);
  351. put_device(&sdkp->dev);
  352. scsi_device_put(sdev);
  353. mutex_unlock(&sd_ref_mutex);
  354. }
  355. static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
  356. {
  357. unsigned int prot_op = SCSI_PROT_NORMAL;
  358. unsigned int dix = scsi_prot_sg_count(scmd);
  359. if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
  360. if (dif && dix)
  361. prot_op = SCSI_PROT_READ_PASS;
  362. else if (dif && !dix)
  363. prot_op = SCSI_PROT_READ_STRIP;
  364. else if (!dif && dix)
  365. prot_op = SCSI_PROT_READ_INSERT;
  366. } else {
  367. if (dif && dix)
  368. prot_op = SCSI_PROT_WRITE_PASS;
  369. else if (dif && !dix)
  370. prot_op = SCSI_PROT_WRITE_INSERT;
  371. else if (!dif && dix)
  372. prot_op = SCSI_PROT_WRITE_STRIP;
  373. }
  374. scsi_set_prot_op(scmd, prot_op);
  375. scsi_set_prot_type(scmd, dif);
  376. }
  377. /**
  378. * scsi_setup_discard_cmnd - unmap blocks on thinly provisioned device
  379. * @sdp: scsi device to operate one
  380. * @rq: Request to prepare
  381. *
  382. * Will issue either UNMAP or WRITE SAME(16) depending on preference
  383. * indicated by target device.
  384. **/
  385. static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
  386. {
  387. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  388. struct bio *bio = rq->bio;
  389. sector_t sector = bio->bi_sector;
  390. unsigned int nr_sectors = bio_sectors(bio);
  391. unsigned int len;
  392. int ret;
  393. struct page *page;
  394. if (sdkp->device->sector_size == 4096) {
  395. sector >>= 3;
  396. nr_sectors >>= 3;
  397. }
  398. rq->timeout = SD_TIMEOUT;
  399. memset(rq->cmd, 0, rq->cmd_len);
  400. page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
  401. if (!page)
  402. return BLKPREP_DEFER;
  403. if (sdkp->unmap) {
  404. char *buf = page_address(page);
  405. rq->cmd_len = 10;
  406. rq->cmd[0] = UNMAP;
  407. rq->cmd[8] = 24;
  408. put_unaligned_be16(6 + 16, &buf[0]);
  409. put_unaligned_be16(16, &buf[2]);
  410. put_unaligned_be64(sector, &buf[8]);
  411. put_unaligned_be32(nr_sectors, &buf[16]);
  412. len = 24;
  413. } else {
  414. rq->cmd_len = 16;
  415. rq->cmd[0] = WRITE_SAME_16;
  416. rq->cmd[1] = 0x8; /* UNMAP */
  417. put_unaligned_be64(sector, &rq->cmd[2]);
  418. put_unaligned_be32(nr_sectors, &rq->cmd[10]);
  419. len = sdkp->device->sector_size;
  420. }
  421. blk_add_request_payload(rq, page, len);
  422. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  423. rq->buffer = page_address(page);
  424. if (ret != BLKPREP_OK) {
  425. __free_page(page);
  426. rq->buffer = NULL;
  427. }
  428. return ret;
  429. }
  430. static int scsi_setup_flush_cmnd(struct scsi_device *sdp, struct request *rq)
  431. {
  432. rq->timeout = SD_FLUSH_TIMEOUT;
  433. rq->retries = SD_MAX_RETRIES;
  434. rq->cmd[0] = SYNCHRONIZE_CACHE;
  435. rq->cmd_len = 10;
  436. return scsi_setup_blk_pc_cmnd(sdp, rq);
  437. }
  438. static void sd_unprep_fn(struct request_queue *q, struct request *rq)
  439. {
  440. if (rq->cmd_flags & REQ_DISCARD) {
  441. free_page((unsigned long)rq->buffer);
  442. rq->buffer = NULL;
  443. }
  444. }
  445. /**
  446. * sd_init_command - build a scsi (read or write) command from
  447. * information in the request structure.
  448. * @SCpnt: pointer to mid-level's per scsi command structure that
  449. * contains request and into which the scsi command is written
  450. *
  451. * Returns 1 if successful and 0 if error (or cannot be done now).
  452. **/
  453. static int sd_prep_fn(struct request_queue *q, struct request *rq)
  454. {
  455. struct scsi_cmnd *SCpnt;
  456. struct scsi_device *sdp = q->queuedata;
  457. struct gendisk *disk = rq->rq_disk;
  458. struct scsi_disk *sdkp;
  459. sector_t block = blk_rq_pos(rq);
  460. sector_t threshold;
  461. unsigned int this_count = blk_rq_sectors(rq);
  462. int ret, host_dif;
  463. unsigned char protect;
  464. /*
  465. * Discard request come in as REQ_TYPE_FS but we turn them into
  466. * block PC requests to make life easier.
  467. */
  468. if (rq->cmd_flags & REQ_DISCARD) {
  469. ret = scsi_setup_discard_cmnd(sdp, rq);
  470. goto out;
  471. } else if (rq->cmd_flags & REQ_FLUSH) {
  472. ret = scsi_setup_flush_cmnd(sdp, rq);
  473. goto out;
  474. } else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  475. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  476. goto out;
  477. } else if (rq->cmd_type != REQ_TYPE_FS) {
  478. ret = BLKPREP_KILL;
  479. goto out;
  480. }
  481. ret = scsi_setup_fs_cmnd(sdp, rq);
  482. if (ret != BLKPREP_OK)
  483. goto out;
  484. SCpnt = rq->special;
  485. sdkp = scsi_disk(disk);
  486. /* from here on until we're complete, any goto out
  487. * is used for a killable error condition */
  488. ret = BLKPREP_KILL;
  489. SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
  490. "sd_init_command: block=%llu, "
  491. "count=%d\n",
  492. (unsigned long long)block,
  493. this_count));
  494. if (!sdp || !scsi_device_online(sdp) ||
  495. block + blk_rq_sectors(rq) > get_capacity(disk)) {
  496. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  497. "Finishing %u sectors\n",
  498. blk_rq_sectors(rq)));
  499. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  500. "Retry with 0x%p\n", SCpnt));
  501. goto out;
  502. }
  503. if (sdp->changed) {
  504. /*
  505. * quietly refuse to do anything to a changed disc until
  506. * the changed bit has been reset
  507. */
  508. /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
  509. goto out;
  510. }
  511. /*
  512. * Some SD card readers can't handle multi-sector accesses which touch
  513. * the last one or two hardware sectors. Split accesses as needed.
  514. */
  515. threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
  516. (sdp->sector_size / 512);
  517. if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
  518. if (block < threshold) {
  519. /* Access up to the threshold but not beyond */
  520. this_count = threshold - block;
  521. } else {
  522. /* Access only a single hardware sector */
  523. this_count = sdp->sector_size / 512;
  524. }
  525. }
  526. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
  527. (unsigned long long)block));
  528. /*
  529. * If we have a 1K hardware sectorsize, prevent access to single
  530. * 512 byte sectors. In theory we could handle this - in fact
  531. * the scsi cdrom driver must be able to handle this because
  532. * we typically use 1K blocksizes, and cdroms typically have
  533. * 2K hardware sectorsizes. Of course, things are simpler
  534. * with the cdrom, since it is read-only. For performance
  535. * reasons, the filesystems should be able to handle this
  536. * and not force the scsi disk driver to use bounce buffers
  537. * for this.
  538. */
  539. if (sdp->sector_size == 1024) {
  540. if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
  541. scmd_printk(KERN_ERR, SCpnt,
  542. "Bad block number requested\n");
  543. goto out;
  544. } else {
  545. block = block >> 1;
  546. this_count = this_count >> 1;
  547. }
  548. }
  549. if (sdp->sector_size == 2048) {
  550. if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
  551. scmd_printk(KERN_ERR, SCpnt,
  552. "Bad block number requested\n");
  553. goto out;
  554. } else {
  555. block = block >> 2;
  556. this_count = this_count >> 2;
  557. }
  558. }
  559. if (sdp->sector_size == 4096) {
  560. if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
  561. scmd_printk(KERN_ERR, SCpnt,
  562. "Bad block number requested\n");
  563. goto out;
  564. } else {
  565. block = block >> 3;
  566. this_count = this_count >> 3;
  567. }
  568. }
  569. if (rq_data_dir(rq) == WRITE) {
  570. if (!sdp->writeable) {
  571. goto out;
  572. }
  573. SCpnt->cmnd[0] = WRITE_6;
  574. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  575. if (blk_integrity_rq(rq) &&
  576. sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
  577. goto out;
  578. } else if (rq_data_dir(rq) == READ) {
  579. SCpnt->cmnd[0] = READ_6;
  580. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  581. } else {
  582. scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
  583. goto out;
  584. }
  585. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  586. "%s %d/%u 512 byte blocks.\n",
  587. (rq_data_dir(rq) == WRITE) ?
  588. "writing" : "reading", this_count,
  589. blk_rq_sectors(rq)));
  590. /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
  591. host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  592. if (host_dif)
  593. protect = 1 << 5;
  594. else
  595. protect = 0;
  596. if (host_dif == SD_DIF_TYPE2_PROTECTION) {
  597. SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
  598. if (unlikely(SCpnt->cmnd == NULL)) {
  599. ret = BLKPREP_DEFER;
  600. goto out;
  601. }
  602. SCpnt->cmd_len = SD_EXT_CDB_SIZE;
  603. memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
  604. SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
  605. SCpnt->cmnd[7] = 0x18;
  606. SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
  607. SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  608. /* LBA */
  609. SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  610. SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  611. SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  612. SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  613. SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
  614. SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
  615. SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
  616. SCpnt->cmnd[19] = (unsigned char) block & 0xff;
  617. /* Expected Indirect LBA */
  618. SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
  619. SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
  620. SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
  621. SCpnt->cmnd[23] = (unsigned char) block & 0xff;
  622. /* Transfer length */
  623. SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
  624. SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
  625. SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
  626. SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
  627. } else if (block > 0xffffffff) {
  628. SCpnt->cmnd[0] += READ_16 - READ_6;
  629. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  630. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  631. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  632. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  633. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  634. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  635. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  636. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  637. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  638. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  639. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  640. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  641. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  642. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  643. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  644. scsi_device_protection(SCpnt->device) ||
  645. SCpnt->device->use_10_for_rw) {
  646. if (this_count > 0xffff)
  647. this_count = 0xffff;
  648. SCpnt->cmnd[0] += READ_10 - READ_6;
  649. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  650. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  651. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  652. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  653. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  654. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  655. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  656. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  657. } else {
  658. if (unlikely(rq->cmd_flags & REQ_FUA)) {
  659. /*
  660. * This happens only if this drive failed
  661. * 10byte rw command with ILLEGAL_REQUEST
  662. * during operation and thus turned off
  663. * use_10_for_rw.
  664. */
  665. scmd_printk(KERN_ERR, SCpnt,
  666. "FUA write on READ/WRITE(6) drive\n");
  667. goto out;
  668. }
  669. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  670. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  671. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  672. SCpnt->cmnd[4] = (unsigned char) this_count;
  673. SCpnt->cmnd[5] = 0;
  674. }
  675. SCpnt->sdb.length = this_count * sdp->sector_size;
  676. /* If DIF or DIX is enabled, tell HBA how to handle request */
  677. if (host_dif || scsi_prot_sg_count(SCpnt))
  678. sd_prot_op(SCpnt, host_dif);
  679. /*
  680. * We shouldn't disconnect in the middle of a sector, so with a dumb
  681. * host adapter, it's safe to assume that we can at least transfer
  682. * this many bytes between each connect / disconnect.
  683. */
  684. SCpnt->transfersize = sdp->sector_size;
  685. SCpnt->underflow = this_count << 9;
  686. SCpnt->allowed = SD_MAX_RETRIES;
  687. /*
  688. * This indicates that the command is ready from our end to be
  689. * queued.
  690. */
  691. ret = BLKPREP_OK;
  692. out:
  693. return scsi_prep_return(q, rq, ret);
  694. }
  695. /**
  696. * sd_open - open a scsi disk device
  697. * @inode: only i_rdev member may be used
  698. * @filp: only f_mode and f_flags may be used
  699. *
  700. * Returns 0 if successful. Returns a negated errno value in case
  701. * of error.
  702. *
  703. * Note: This can be called from a user context (e.g. fsck(1) )
  704. * or from within the kernel (e.g. as a result of a mount(1) ).
  705. * In the latter case @inode and @filp carry an abridged amount
  706. * of information as noted above.
  707. *
  708. * Locking: called with bdev->bd_mutex held.
  709. **/
  710. static int sd_open(struct block_device *bdev, fmode_t mode)
  711. {
  712. struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
  713. struct scsi_device *sdev;
  714. int retval;
  715. if (!sdkp)
  716. return -ENXIO;
  717. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
  718. sdev = sdkp->device;
  719. retval = scsi_autopm_get_device(sdev);
  720. if (retval)
  721. goto error_autopm;
  722. /*
  723. * If the device is in error recovery, wait until it is done.
  724. * If the device is offline, then disallow any access to it.
  725. */
  726. retval = -ENXIO;
  727. if (!scsi_block_when_processing_errors(sdev))
  728. goto error_out;
  729. if (sdev->removable || sdkp->write_prot)
  730. check_disk_change(bdev);
  731. /*
  732. * If the drive is empty, just let the open fail.
  733. */
  734. retval = -ENOMEDIUM;
  735. if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
  736. goto error_out;
  737. /*
  738. * If the device has the write protect tab set, have the open fail
  739. * if the user expects to be able to write to the thing.
  740. */
  741. retval = -EROFS;
  742. if (sdkp->write_prot && (mode & FMODE_WRITE))
  743. goto error_out;
  744. /*
  745. * It is possible that the disk changing stuff resulted in
  746. * the device being taken offline. If this is the case,
  747. * report this to the user, and don't pretend that the
  748. * open actually succeeded.
  749. */
  750. retval = -ENXIO;
  751. if (!scsi_device_online(sdev))
  752. goto error_out;
  753. if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
  754. if (scsi_block_when_processing_errors(sdev))
  755. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  756. }
  757. return 0;
  758. error_out:
  759. scsi_autopm_put_device(sdev);
  760. error_autopm:
  761. scsi_disk_put(sdkp);
  762. return retval;
  763. }
  764. /**
  765. * sd_release - invoked when the (last) close(2) is called on this
  766. * scsi disk.
  767. * @inode: only i_rdev member may be used
  768. * @filp: only f_mode and f_flags may be used
  769. *
  770. * Returns 0.
  771. *
  772. * Note: may block (uninterruptible) if error recovery is underway
  773. * on this disk.
  774. *
  775. * Locking: called with bdev->bd_mutex held.
  776. **/
  777. static int sd_release(struct gendisk *disk, fmode_t mode)
  778. {
  779. struct scsi_disk *sdkp = scsi_disk(disk);
  780. struct scsi_device *sdev = sdkp->device;
  781. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
  782. if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
  783. if (scsi_block_when_processing_errors(sdev))
  784. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  785. }
  786. /*
  787. * XXX and what if there are packets in flight and this close()
  788. * XXX is followed by a "rmmod sd_mod"?
  789. */
  790. scsi_autopm_put_device(sdev);
  791. scsi_disk_put(sdkp);
  792. return 0;
  793. }
  794. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  795. {
  796. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  797. struct scsi_device *sdp = sdkp->device;
  798. struct Scsi_Host *host = sdp->host;
  799. int diskinfo[4];
  800. /* default to most commonly used values */
  801. diskinfo[0] = 0x40; /* 1 << 6 */
  802. diskinfo[1] = 0x20; /* 1 << 5 */
  803. diskinfo[2] = sdkp->capacity >> 11;
  804. /* override with calculated, extended default, or driver values */
  805. if (host->hostt->bios_param)
  806. host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  807. else
  808. scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  809. geo->heads = diskinfo[0];
  810. geo->sectors = diskinfo[1];
  811. geo->cylinders = diskinfo[2];
  812. return 0;
  813. }
  814. /**
  815. * sd_ioctl - process an ioctl
  816. * @inode: only i_rdev/i_bdev members may be used
  817. * @filp: only f_mode and f_flags may be used
  818. * @cmd: ioctl command number
  819. * @arg: this is third argument given to ioctl(2) system call.
  820. * Often contains a pointer.
  821. *
  822. * Returns 0 if successful (some ioctls return postive numbers on
  823. * success as well). Returns a negated errno value in case of error.
  824. *
  825. * Note: most ioctls are forward onto the block subsystem or further
  826. * down in the scsi subsystem.
  827. **/
  828. static int sd_ioctl(struct block_device *bdev, fmode_t mode,
  829. unsigned int cmd, unsigned long arg)
  830. {
  831. struct gendisk *disk = bdev->bd_disk;
  832. struct scsi_device *sdp = scsi_disk(disk)->device;
  833. void __user *p = (void __user *)arg;
  834. int error;
  835. SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
  836. disk->disk_name, cmd));
  837. /*
  838. * If we are in the middle of error recovery, don't let anyone
  839. * else try and use this device. Also, if error recovery fails, it
  840. * may try and take the device offline, in which case all further
  841. * access to the device is prohibited.
  842. */
  843. error = scsi_nonblockable_ioctl(sdp, cmd, p,
  844. (mode & FMODE_NDELAY) != 0);
  845. if (!scsi_block_when_processing_errors(sdp) || !error)
  846. goto out;
  847. /*
  848. * Send SCSI addressing ioctls directly to mid level, send other
  849. * ioctls to block level and then onto mid level if they can't be
  850. * resolved.
  851. */
  852. switch (cmd) {
  853. case SCSI_IOCTL_GET_IDLUN:
  854. case SCSI_IOCTL_GET_BUS_NUMBER:
  855. error = scsi_ioctl(sdp, cmd, p);
  856. break;
  857. default:
  858. error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
  859. if (error != -ENOTTY)
  860. break;
  861. error = scsi_ioctl(sdp, cmd, p);
  862. break;
  863. }
  864. out:
  865. return error;
  866. }
  867. static void set_media_not_present(struct scsi_disk *sdkp)
  868. {
  869. sdkp->media_present = 0;
  870. sdkp->capacity = 0;
  871. sdkp->device->changed = 1;
  872. }
  873. /**
  874. * sd_media_changed - check if our medium changed
  875. * @disk: kernel device descriptor
  876. *
  877. * Returns 0 if not applicable or no change; 1 if change
  878. *
  879. * Note: this function is invoked from the block subsystem.
  880. **/
  881. static int sd_media_changed(struct gendisk *disk)
  882. {
  883. struct scsi_disk *sdkp = scsi_disk(disk);
  884. struct scsi_device *sdp = sdkp->device;
  885. struct scsi_sense_hdr *sshdr = NULL;
  886. int retval;
  887. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
  888. if (!sdp->removable)
  889. return 0;
  890. /*
  891. * If the device is offline, don't send any commands - just pretend as
  892. * if the command failed. If the device ever comes back online, we
  893. * can deal with it then. It is only because of unrecoverable errors
  894. * that we would ever take a device offline in the first place.
  895. */
  896. if (!scsi_device_online(sdp)) {
  897. set_media_not_present(sdkp);
  898. goto out;
  899. }
  900. /*
  901. * Using TEST_UNIT_READY enables differentiation between drive with
  902. * no cartridge loaded - NOT READY, drive with changed cartridge -
  903. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  904. *
  905. * Drives that auto spin down. eg iomega jaz 1G, will be started
  906. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  907. * sd_revalidate() is called.
  908. */
  909. retval = -ENODEV;
  910. if (scsi_block_when_processing_errors(sdp)) {
  911. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  912. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
  913. sshdr);
  914. }
  915. /*
  916. * Unable to test, unit probably not ready. This usually
  917. * means there is no disc in the drive. Mark as changed,
  918. * and we will figure it out later once the drive is
  919. * available again.
  920. */
  921. if (retval || (scsi_sense_valid(sshdr) &&
  922. /* 0x3a is medium not present */
  923. sshdr->asc == 0x3a)) {
  924. set_media_not_present(sdkp);
  925. goto out;
  926. }
  927. /*
  928. * For removable scsi disk we have to recognise the presence
  929. * of a disk in the drive. This is kept in the struct scsi_disk
  930. * struct and tested at open ! Daniel Roche (dan@lectra.fr)
  931. */
  932. sdkp->media_present = 1;
  933. out:
  934. /*
  935. * Report a media change under the following conditions:
  936. *
  937. * Medium is present now and wasn't present before.
  938. * Medium wasn't present before and is present now.
  939. * Medium was present at all times, but it changed while
  940. * we weren't looking (sdp->changed is set).
  941. *
  942. * If there was no medium before and there is no medium now then
  943. * don't report a change, even if a medium was inserted and removed
  944. * while we weren't looking.
  945. */
  946. retval = (sdkp->media_present != sdkp->previous_state ||
  947. (sdkp->media_present && sdp->changed));
  948. if (retval)
  949. sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
  950. sdkp->previous_state = sdkp->media_present;
  951. /* sdp->changed indicates medium was changed or is not present */
  952. sdp->changed = !sdkp->media_present;
  953. kfree(sshdr);
  954. return retval;
  955. }
  956. static int sd_sync_cache(struct scsi_disk *sdkp)
  957. {
  958. int retries, res;
  959. struct scsi_device *sdp = sdkp->device;
  960. struct scsi_sense_hdr sshdr;
  961. if (!scsi_device_online(sdp))
  962. return -ENODEV;
  963. for (retries = 3; retries > 0; --retries) {
  964. unsigned char cmd[10] = { 0 };
  965. cmd[0] = SYNCHRONIZE_CACHE;
  966. /*
  967. * Leave the rest of the command zero to indicate
  968. * flush everything.
  969. */
  970. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  971. SD_FLUSH_TIMEOUT, SD_MAX_RETRIES, NULL);
  972. if (res == 0)
  973. break;
  974. }
  975. if (res) {
  976. sd_print_result(sdkp, res);
  977. if (driver_byte(res) & DRIVER_SENSE)
  978. sd_print_sense_hdr(sdkp, &sshdr);
  979. }
  980. if (res)
  981. return -EIO;
  982. return 0;
  983. }
  984. static void sd_rescan(struct device *dev)
  985. {
  986. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  987. if (sdkp) {
  988. revalidate_disk(sdkp->disk);
  989. scsi_disk_put(sdkp);
  990. }
  991. }
  992. #ifdef CONFIG_COMPAT
  993. /*
  994. * This gets directly called from VFS. When the ioctl
  995. * is not recognized we go back to the other translation paths.
  996. */
  997. static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
  998. unsigned int cmd, unsigned long arg)
  999. {
  1000. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  1001. /*
  1002. * If we are in the middle of error recovery, don't let anyone
  1003. * else try and use this device. Also, if error recovery fails, it
  1004. * may try and take the device offline, in which case all further
  1005. * access to the device is prohibited.
  1006. */
  1007. if (!scsi_block_when_processing_errors(sdev))
  1008. return -ENODEV;
  1009. if (sdev->host->hostt->compat_ioctl) {
  1010. int ret;
  1011. ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  1012. return ret;
  1013. }
  1014. /*
  1015. * Let the static ioctl translation table take care of it.
  1016. */
  1017. return -ENOIOCTLCMD;
  1018. }
  1019. #endif
  1020. static const struct block_device_operations sd_fops = {
  1021. .owner = THIS_MODULE,
  1022. .open = sd_open,
  1023. .release = sd_release,
  1024. .ioctl = sd_ioctl,
  1025. .getgeo = sd_getgeo,
  1026. #ifdef CONFIG_COMPAT
  1027. .compat_ioctl = sd_compat_ioctl,
  1028. #endif
  1029. .media_changed = sd_media_changed,
  1030. .revalidate_disk = sd_revalidate_disk,
  1031. .unlock_native_capacity = sd_unlock_native_capacity,
  1032. };
  1033. static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
  1034. {
  1035. u64 start_lba = blk_rq_pos(scmd->request);
  1036. u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
  1037. u64 bad_lba;
  1038. int info_valid;
  1039. if (scmd->request->cmd_type != REQ_TYPE_FS)
  1040. return 0;
  1041. info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
  1042. SCSI_SENSE_BUFFERSIZE,
  1043. &bad_lba);
  1044. if (!info_valid)
  1045. return 0;
  1046. if (scsi_bufflen(scmd) <= scmd->device->sector_size)
  1047. return 0;
  1048. if (scmd->device->sector_size < 512) {
  1049. /* only legitimate sector_size here is 256 */
  1050. start_lba <<= 1;
  1051. end_lba <<= 1;
  1052. } else {
  1053. /* be careful ... don't want any overflows */
  1054. u64 factor = scmd->device->sector_size / 512;
  1055. do_div(start_lba, factor);
  1056. do_div(end_lba, factor);
  1057. }
  1058. /* The bad lba was reported incorrectly, we have no idea where
  1059. * the error is.
  1060. */
  1061. if (bad_lba < start_lba || bad_lba >= end_lba)
  1062. return 0;
  1063. /* This computation should always be done in terms of
  1064. * the resolution of the device's medium.
  1065. */
  1066. return (bad_lba - start_lba) * scmd->device->sector_size;
  1067. }
  1068. /**
  1069. * sd_done - bottom half handler: called when the lower level
  1070. * driver has completed (successfully or otherwise) a scsi command.
  1071. * @SCpnt: mid-level's per command structure.
  1072. *
  1073. * Note: potentially run from within an ISR. Must not block.
  1074. **/
  1075. static int sd_done(struct scsi_cmnd *SCpnt)
  1076. {
  1077. int result = SCpnt->result;
  1078. unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
  1079. struct scsi_sense_hdr sshdr;
  1080. struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
  1081. int sense_valid = 0;
  1082. int sense_deferred = 0;
  1083. if (SCpnt->request->cmd_flags & REQ_DISCARD) {
  1084. if (!result)
  1085. scsi_set_resid(SCpnt, 0);
  1086. return good_bytes;
  1087. }
  1088. if (result) {
  1089. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  1090. if (sense_valid)
  1091. sense_deferred = scsi_sense_is_deferred(&sshdr);
  1092. }
  1093. #ifdef CONFIG_SCSI_LOGGING
  1094. SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
  1095. if (sense_valid) {
  1096. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
  1097. "sd_done: sb[respc,sk,asc,"
  1098. "ascq]=%x,%x,%x,%x\n",
  1099. sshdr.response_code,
  1100. sshdr.sense_key, sshdr.asc,
  1101. sshdr.ascq));
  1102. }
  1103. #endif
  1104. if (driver_byte(result) != DRIVER_SENSE &&
  1105. (!sense_valid || sense_deferred))
  1106. goto out;
  1107. switch (sshdr.sense_key) {
  1108. case HARDWARE_ERROR:
  1109. case MEDIUM_ERROR:
  1110. good_bytes = sd_completed_bytes(SCpnt);
  1111. break;
  1112. case RECOVERED_ERROR:
  1113. good_bytes = scsi_bufflen(SCpnt);
  1114. break;
  1115. case NO_SENSE:
  1116. /* This indicates a false check condition, so ignore it. An
  1117. * unknown amount of data was transferred so treat it as an
  1118. * error.
  1119. */
  1120. scsi_print_sense("sd", SCpnt);
  1121. SCpnt->result = 0;
  1122. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1123. break;
  1124. case ABORTED_COMMAND: /* DIF: Target detected corruption */
  1125. case ILLEGAL_REQUEST: /* DIX: Host detected corruption */
  1126. if (sshdr.asc == 0x10)
  1127. good_bytes = sd_completed_bytes(SCpnt);
  1128. break;
  1129. default:
  1130. break;
  1131. }
  1132. out:
  1133. if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
  1134. sd_dif_complete(SCpnt, good_bytes);
  1135. if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
  1136. == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd) {
  1137. /* We have to print a failed command here as the
  1138. * extended CDB gets freed before scsi_io_completion()
  1139. * is called.
  1140. */
  1141. if (result)
  1142. scsi_print_command(SCpnt);
  1143. mempool_free(SCpnt->cmnd, sd_cdb_pool);
  1144. SCpnt->cmnd = NULL;
  1145. SCpnt->cmd_len = 0;
  1146. }
  1147. return good_bytes;
  1148. }
  1149. static int media_not_present(struct scsi_disk *sdkp,
  1150. struct scsi_sense_hdr *sshdr)
  1151. {
  1152. if (!scsi_sense_valid(sshdr))
  1153. return 0;
  1154. /* not invoked for commands that could return deferred errors */
  1155. if (sshdr->sense_key != NOT_READY &&
  1156. sshdr->sense_key != UNIT_ATTENTION)
  1157. return 0;
  1158. if (sshdr->asc != 0x3A) /* medium not present */
  1159. return 0;
  1160. set_media_not_present(sdkp);
  1161. return 1;
  1162. }
  1163. /*
  1164. * spinup disk - called only in sd_revalidate_disk()
  1165. */
  1166. static void
  1167. sd_spinup_disk(struct scsi_disk *sdkp)
  1168. {
  1169. unsigned char cmd[10];
  1170. unsigned long spintime_expire = 0;
  1171. int retries, spintime;
  1172. unsigned int the_result;
  1173. struct scsi_sense_hdr sshdr;
  1174. int sense_valid = 0;
  1175. spintime = 0;
  1176. /* Spin up drives, as required. Only do this at boot time */
  1177. /* Spinup needs to be done for module loads too. */
  1178. do {
  1179. retries = 0;
  1180. do {
  1181. cmd[0] = TEST_UNIT_READY;
  1182. memset((void *) &cmd[1], 0, 9);
  1183. the_result = scsi_execute_req(sdkp->device, cmd,
  1184. DMA_NONE, NULL, 0,
  1185. &sshdr, SD_TIMEOUT,
  1186. SD_MAX_RETRIES, NULL);
  1187. /*
  1188. * If the drive has indicated to us that it
  1189. * doesn't have any media in it, don't bother
  1190. * with any more polling.
  1191. */
  1192. if (media_not_present(sdkp, &sshdr))
  1193. return;
  1194. if (the_result)
  1195. sense_valid = scsi_sense_valid(&sshdr);
  1196. retries++;
  1197. } while (retries < 3 &&
  1198. (!scsi_status_is_good(the_result) ||
  1199. ((driver_byte(the_result) & DRIVER_SENSE) &&
  1200. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  1201. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  1202. /* no sense, TUR either succeeded or failed
  1203. * with a status error */
  1204. if(!spintime && !scsi_status_is_good(the_result)) {
  1205. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1206. sd_print_result(sdkp, the_result);
  1207. }
  1208. break;
  1209. }
  1210. /*
  1211. * The device does not want the automatic start to be issued.
  1212. */
  1213. if (sdkp->device->no_start_on_add)
  1214. break;
  1215. if (sense_valid && sshdr.sense_key == NOT_READY) {
  1216. if (sshdr.asc == 4 && sshdr.ascq == 3)
  1217. break; /* manual intervention required */
  1218. if (sshdr.asc == 4 && sshdr.ascq == 0xb)
  1219. break; /* standby */
  1220. if (sshdr.asc == 4 && sshdr.ascq == 0xc)
  1221. break; /* unavailable */
  1222. /*
  1223. * Issue command to spin up drive when not ready
  1224. */
  1225. if (!spintime) {
  1226. sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  1227. cmd[0] = START_STOP;
  1228. cmd[1] = 1; /* Return immediately */
  1229. memset((void *) &cmd[2], 0, 8);
  1230. cmd[4] = 1; /* Start spin cycle */
  1231. if (sdkp->device->start_stop_pwr_cond)
  1232. cmd[4] |= 1 << 4;
  1233. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  1234. NULL, 0, &sshdr,
  1235. SD_TIMEOUT, SD_MAX_RETRIES,
  1236. NULL);
  1237. spintime_expire = jiffies + 100 * HZ;
  1238. spintime = 1;
  1239. }
  1240. /* Wait 1 second for next try */
  1241. msleep(1000);
  1242. printk(".");
  1243. /*
  1244. * Wait for USB flash devices with slow firmware.
  1245. * Yes, this sense key/ASC combination shouldn't
  1246. * occur here. It's characteristic of these devices.
  1247. */
  1248. } else if (sense_valid &&
  1249. sshdr.sense_key == UNIT_ATTENTION &&
  1250. sshdr.asc == 0x28) {
  1251. if (!spintime) {
  1252. spintime_expire = jiffies + 5 * HZ;
  1253. spintime = 1;
  1254. }
  1255. /* Wait 1 second for next try */
  1256. msleep(1000);
  1257. } else {
  1258. /* we don't understand the sense code, so it's
  1259. * probably pointless to loop */
  1260. if(!spintime) {
  1261. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1262. sd_print_sense_hdr(sdkp, &sshdr);
  1263. }
  1264. break;
  1265. }
  1266. } while (spintime && time_before_eq(jiffies, spintime_expire));
  1267. if (spintime) {
  1268. if (scsi_status_is_good(the_result))
  1269. printk("ready\n");
  1270. else
  1271. printk("not responding...\n");
  1272. }
  1273. }
  1274. /*
  1275. * Determine whether disk supports Data Integrity Field.
  1276. */
  1277. static void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1278. {
  1279. struct scsi_device *sdp = sdkp->device;
  1280. u8 type;
  1281. if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
  1282. return;
  1283. type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
  1284. if (type == sdkp->protection_type || !sdkp->first_scan)
  1285. return;
  1286. sdkp->protection_type = type;
  1287. if (type > SD_DIF_TYPE3_PROTECTION) {
  1288. sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
  1289. "protection type %u. Disabling disk!\n", type);
  1290. sdkp->capacity = 0;
  1291. return;
  1292. }
  1293. if (scsi_host_dif_capable(sdp->host, type))
  1294. sd_printk(KERN_NOTICE, sdkp,
  1295. "Enabling DIF Type %u protection\n", type);
  1296. else
  1297. sd_printk(KERN_NOTICE, sdkp,
  1298. "Disabling DIF Type %u protection\n", type);
  1299. }
  1300. static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1301. struct scsi_sense_hdr *sshdr, int sense_valid,
  1302. int the_result)
  1303. {
  1304. sd_print_result(sdkp, the_result);
  1305. if (driver_byte(the_result) & DRIVER_SENSE)
  1306. sd_print_sense_hdr(sdkp, sshdr);
  1307. else
  1308. sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1309. /*
  1310. * Set dirty bit for removable devices if not ready -
  1311. * sometimes drives will not report this properly.
  1312. */
  1313. if (sdp->removable &&
  1314. sense_valid && sshdr->sense_key == NOT_READY)
  1315. sdp->changed = 1;
  1316. /*
  1317. * We used to set media_present to 0 here to indicate no media
  1318. * in the drive, but some drives fail read capacity even with
  1319. * media present, so we can't do that.
  1320. */
  1321. sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1322. }
  1323. #define RC16_LEN 32
  1324. #if RC16_LEN > SD_BUF_SIZE
  1325. #error RC16_LEN must not be more than SD_BUF_SIZE
  1326. #endif
  1327. #define READ_CAPACITY_RETRIES_ON_RESET 10
  1328. static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1329. unsigned char *buffer)
  1330. {
  1331. unsigned char cmd[16];
  1332. struct scsi_sense_hdr sshdr;
  1333. int sense_valid = 0;
  1334. int the_result;
  1335. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1336. unsigned int alignment;
  1337. unsigned long long lba;
  1338. unsigned sector_size;
  1339. if (sdp->no_read_capacity_16)
  1340. return -EINVAL;
  1341. do {
  1342. memset(cmd, 0, 16);
  1343. cmd[0] = SERVICE_ACTION_IN;
  1344. cmd[1] = SAI_READ_CAPACITY_16;
  1345. cmd[13] = RC16_LEN;
  1346. memset(buffer, 0, RC16_LEN);
  1347. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1348. buffer, RC16_LEN, &sshdr,
  1349. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1350. if (media_not_present(sdkp, &sshdr))
  1351. return -ENODEV;
  1352. if (the_result) {
  1353. sense_valid = scsi_sense_valid(&sshdr);
  1354. if (sense_valid &&
  1355. sshdr.sense_key == ILLEGAL_REQUEST &&
  1356. (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1357. sshdr.ascq == 0x00)
  1358. /* Invalid Command Operation Code or
  1359. * Invalid Field in CDB, just retry
  1360. * silently with RC10 */
  1361. return -EINVAL;
  1362. if (sense_valid &&
  1363. sshdr.sense_key == UNIT_ATTENTION &&
  1364. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1365. /* Device reset might occur several times,
  1366. * give it one more chance */
  1367. if (--reset_retries > 0)
  1368. continue;
  1369. }
  1370. retries--;
  1371. } while (the_result && retries);
  1372. if (the_result) {
  1373. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
  1374. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1375. return -EINVAL;
  1376. }
  1377. sector_size = get_unaligned_be32(&buffer[8]);
  1378. lba = get_unaligned_be64(&buffer[0]);
  1379. sd_read_protection_type(sdkp, buffer);
  1380. if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
  1381. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1382. "kernel compiled with support for large block "
  1383. "devices.\n");
  1384. sdkp->capacity = 0;
  1385. return -EOVERFLOW;
  1386. }
  1387. /* Logical blocks per physical block exponent */
  1388. sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
  1389. /* Lowest aligned logical block */
  1390. alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
  1391. blk_queue_alignment_offset(sdp->request_queue, alignment);
  1392. if (alignment && sdkp->first_scan)
  1393. sd_printk(KERN_NOTICE, sdkp,
  1394. "physical block alignment offset: %u\n", alignment);
  1395. if (buffer[14] & 0x80) { /* TPE */
  1396. struct request_queue *q = sdp->request_queue;
  1397. sdkp->thin_provisioning = 1;
  1398. q->limits.discard_granularity = sdkp->physical_block_size;
  1399. q->limits.max_discard_sectors = 0xffffffff;
  1400. if (buffer[14] & 0x40) /* TPRZ */
  1401. q->limits.discard_zeroes_data = 1;
  1402. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1403. }
  1404. sdkp->capacity = lba + 1;
  1405. return sector_size;
  1406. }
  1407. static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1408. unsigned char *buffer)
  1409. {
  1410. unsigned char cmd[16];
  1411. struct scsi_sense_hdr sshdr;
  1412. int sense_valid = 0;
  1413. int the_result;
  1414. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1415. sector_t lba;
  1416. unsigned sector_size;
  1417. do {
  1418. cmd[0] = READ_CAPACITY;
  1419. memset(&cmd[1], 0, 9);
  1420. memset(buffer, 0, 8);
  1421. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1422. buffer, 8, &sshdr,
  1423. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1424. if (media_not_present(sdkp, &sshdr))
  1425. return -ENODEV;
  1426. if (the_result) {
  1427. sense_valid = scsi_sense_valid(&sshdr);
  1428. if (sense_valid &&
  1429. sshdr.sense_key == UNIT_ATTENTION &&
  1430. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1431. /* Device reset might occur several times,
  1432. * give it one more chance */
  1433. if (--reset_retries > 0)
  1434. continue;
  1435. }
  1436. retries--;
  1437. } while (the_result && retries);
  1438. if (the_result) {
  1439. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
  1440. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1441. return -EINVAL;
  1442. }
  1443. sector_size = get_unaligned_be32(&buffer[4]);
  1444. lba = get_unaligned_be32(&buffer[0]);
  1445. if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
  1446. /* Some buggy (usb cardreader) devices return an lba of
  1447. 0xffffffff when the want to report a size of 0 (with
  1448. which they really mean no media is present) */
  1449. sdkp->capacity = 0;
  1450. sdkp->physical_block_size = sector_size;
  1451. return sector_size;
  1452. }
  1453. if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
  1454. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1455. "kernel compiled with support for large block "
  1456. "devices.\n");
  1457. sdkp->capacity = 0;
  1458. return -EOVERFLOW;
  1459. }
  1460. sdkp->capacity = lba + 1;
  1461. sdkp->physical_block_size = sector_size;
  1462. return sector_size;
  1463. }
  1464. static int sd_try_rc16_first(struct scsi_device *sdp)
  1465. {
  1466. if (sdp->host->max_cmd_len < 16)
  1467. return 0;
  1468. if (sdp->scsi_level > SCSI_SPC_2)
  1469. return 1;
  1470. if (scsi_device_protection(sdp))
  1471. return 1;
  1472. return 0;
  1473. }
  1474. /*
  1475. * read disk capacity
  1476. */
  1477. static void
  1478. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  1479. {
  1480. int sector_size;
  1481. struct scsi_device *sdp = sdkp->device;
  1482. sector_t old_capacity = sdkp->capacity;
  1483. if (sd_try_rc16_first(sdp)) {
  1484. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1485. if (sector_size == -EOVERFLOW)
  1486. goto got_data;
  1487. if (sector_size == -ENODEV)
  1488. return;
  1489. if (sector_size < 0)
  1490. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1491. if (sector_size < 0)
  1492. return;
  1493. } else {
  1494. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1495. if (sector_size == -EOVERFLOW)
  1496. goto got_data;
  1497. if (sector_size < 0)
  1498. return;
  1499. if ((sizeof(sdkp->capacity) > 4) &&
  1500. (sdkp->capacity > 0xffffffffULL)) {
  1501. int old_sector_size = sector_size;
  1502. sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1503. "Trying to use READ CAPACITY(16).\n");
  1504. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1505. if (sector_size < 0) {
  1506. sd_printk(KERN_NOTICE, sdkp,
  1507. "Using 0xffffffff as device size\n");
  1508. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1509. sector_size = old_sector_size;
  1510. goto got_data;
  1511. }
  1512. }
  1513. }
  1514. /* Some devices are known to return the total number of blocks,
  1515. * not the highest block number. Some devices have versions
  1516. * which do this and others which do not. Some devices we might
  1517. * suspect of doing this but we don't know for certain.
  1518. *
  1519. * If we know the reported capacity is wrong, decrement it. If
  1520. * we can only guess, then assume the number of blocks is even
  1521. * (usually true but not always) and err on the side of lowering
  1522. * the capacity.
  1523. */
  1524. if (sdp->fix_capacity ||
  1525. (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
  1526. sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
  1527. "from its reported value: %llu\n",
  1528. (unsigned long long) sdkp->capacity);
  1529. --sdkp->capacity;
  1530. }
  1531. got_data:
  1532. if (sector_size == 0) {
  1533. sector_size = 512;
  1534. sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  1535. "assuming 512.\n");
  1536. }
  1537. if (sector_size != 512 &&
  1538. sector_size != 1024 &&
  1539. sector_size != 2048 &&
  1540. sector_size != 4096 &&
  1541. sector_size != 256) {
  1542. sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  1543. sector_size);
  1544. /*
  1545. * The user might want to re-format the drive with
  1546. * a supported sectorsize. Once this happens, it
  1547. * would be relatively trivial to set the thing up.
  1548. * For this reason, we leave the thing in the table.
  1549. */
  1550. sdkp->capacity = 0;
  1551. /*
  1552. * set a bogus sector size so the normal read/write
  1553. * logic in the block layer will eventually refuse any
  1554. * request on this device without tripping over power
  1555. * of two sector size assumptions
  1556. */
  1557. sector_size = 512;
  1558. }
  1559. blk_queue_logical_block_size(sdp->request_queue, sector_size);
  1560. {
  1561. char cap_str_2[10], cap_str_10[10];
  1562. u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
  1563. string_get_size(sz, STRING_UNITS_2, cap_str_2,
  1564. sizeof(cap_str_2));
  1565. string_get_size(sz, STRING_UNITS_10, cap_str_10,
  1566. sizeof(cap_str_10));
  1567. if (sdkp->first_scan || old_capacity != sdkp->capacity) {
  1568. sd_printk(KERN_NOTICE, sdkp,
  1569. "%llu %d-byte logical blocks: (%s/%s)\n",
  1570. (unsigned long long)sdkp->capacity,
  1571. sector_size, cap_str_10, cap_str_2);
  1572. if (sdkp->physical_block_size != sector_size)
  1573. sd_printk(KERN_NOTICE, sdkp,
  1574. "%u-byte physical blocks\n",
  1575. sdkp->physical_block_size);
  1576. }
  1577. }
  1578. /* Rescale capacity to 512-byte units */
  1579. if (sector_size == 4096)
  1580. sdkp->capacity <<= 3;
  1581. else if (sector_size == 2048)
  1582. sdkp->capacity <<= 2;
  1583. else if (sector_size == 1024)
  1584. sdkp->capacity <<= 1;
  1585. else if (sector_size == 256)
  1586. sdkp->capacity >>= 1;
  1587. blk_queue_physical_block_size(sdp->request_queue,
  1588. sdkp->physical_block_size);
  1589. sdkp->device->sector_size = sector_size;
  1590. }
  1591. /* called with buffer of length 512 */
  1592. static inline int
  1593. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1594. unsigned char *buffer, int len, struct scsi_mode_data *data,
  1595. struct scsi_sense_hdr *sshdr)
  1596. {
  1597. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1598. SD_TIMEOUT, SD_MAX_RETRIES, data,
  1599. sshdr);
  1600. }
  1601. /*
  1602. * read write protect setting, if possible - called only in sd_revalidate_disk()
  1603. * called with buffer of length SD_BUF_SIZE
  1604. */
  1605. static void
  1606. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  1607. {
  1608. int res;
  1609. struct scsi_device *sdp = sdkp->device;
  1610. struct scsi_mode_data data;
  1611. int old_wp = sdkp->write_prot;
  1612. set_disk_ro(sdkp->disk, 0);
  1613. if (sdp->skip_ms_page_3f) {
  1614. sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  1615. return;
  1616. }
  1617. if (sdp->use_192_bytes_for_3f) {
  1618. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1619. } else {
  1620. /*
  1621. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1622. * We have to start carefully: some devices hang if we ask
  1623. * for more than is available.
  1624. */
  1625. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1626. /*
  1627. * Second attempt: ask for page 0 When only page 0 is
  1628. * implemented, a request for page 3F may return Sense Key
  1629. * 5: Illegal Request, Sense Code 24: Invalid field in
  1630. * CDB.
  1631. */
  1632. if (!scsi_status_is_good(res))
  1633. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1634. /*
  1635. * Third attempt: ask 255 bytes, as we did earlier.
  1636. */
  1637. if (!scsi_status_is_good(res))
  1638. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1639. &data, NULL);
  1640. }
  1641. if (!scsi_status_is_good(res)) {
  1642. sd_printk(KERN_WARNING, sdkp,
  1643. "Test WP failed, assume Write Enabled\n");
  1644. } else {
  1645. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1646. set_disk_ro(sdkp->disk, sdkp->write_prot);
  1647. if (sdkp->first_scan || old_wp != sdkp->write_prot) {
  1648. sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  1649. sdkp->write_prot ? "on" : "off");
  1650. sd_printk(KERN_DEBUG, sdkp,
  1651. "Mode Sense: %02x %02x %02x %02x\n",
  1652. buffer[0], buffer[1], buffer[2], buffer[3]);
  1653. }
  1654. }
  1655. }
  1656. /*
  1657. * sd_read_cache_type - called only from sd_revalidate_disk()
  1658. * called with buffer of length SD_BUF_SIZE
  1659. */
  1660. static void
  1661. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1662. {
  1663. int len = 0, res;
  1664. struct scsi_device *sdp = sdkp->device;
  1665. int dbd;
  1666. int modepage;
  1667. struct scsi_mode_data data;
  1668. struct scsi_sense_hdr sshdr;
  1669. int old_wce = sdkp->WCE;
  1670. int old_rcd = sdkp->RCD;
  1671. int old_dpofua = sdkp->DPOFUA;
  1672. if (sdp->skip_ms_page_8) {
  1673. if (sdp->type == TYPE_RBC)
  1674. goto defaults;
  1675. else {
  1676. modepage = 0x3F;
  1677. dbd = 0;
  1678. }
  1679. } else if (sdp->type == TYPE_RBC) {
  1680. modepage = 6;
  1681. dbd = 8;
  1682. } else {
  1683. modepage = 8;
  1684. dbd = 0;
  1685. }
  1686. /* cautiously ask */
  1687. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
  1688. if (!scsi_status_is_good(res))
  1689. goto bad_sense;
  1690. if (!data.header_length) {
  1691. modepage = 6;
  1692. sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
  1693. }
  1694. /* that went OK, now ask for the proper length */
  1695. len = data.length;
  1696. /*
  1697. * We're only interested in the first three bytes, actually.
  1698. * But the data cache page is defined for the first 20.
  1699. */
  1700. if (len < 3)
  1701. goto bad_sense;
  1702. else if (len > SD_BUF_SIZE) {
  1703. sd_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
  1704. "data from %d to %d bytes\n", len, SD_BUF_SIZE);
  1705. len = SD_BUF_SIZE;
  1706. }
  1707. /* Get the data */
  1708. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
  1709. if (scsi_status_is_good(res)) {
  1710. int offset = data.header_length + data.block_descriptor_length;
  1711. while (offset < len) {
  1712. u8 page_code = buffer[offset] & 0x3F;
  1713. u8 spf = buffer[offset] & 0x40;
  1714. if (page_code == 8 || page_code == 6) {
  1715. /* We're interested only in the first 3 bytes.
  1716. */
  1717. if (len - offset <= 2) {
  1718. sd_printk(KERN_ERR, sdkp, "Incomplete "
  1719. "mode parameter data\n");
  1720. goto defaults;
  1721. } else {
  1722. modepage = page_code;
  1723. goto Page_found;
  1724. }
  1725. } else {
  1726. /* Go to the next page */
  1727. if (spf && len - offset > 3)
  1728. offset += 4 + (buffer[offset+2] << 8) +
  1729. buffer[offset+3];
  1730. else if (!spf && len - offset > 1)
  1731. offset += 2 + buffer[offset+1];
  1732. else {
  1733. sd_printk(KERN_ERR, sdkp, "Incomplete "
  1734. "mode parameter data\n");
  1735. goto defaults;
  1736. }
  1737. }
  1738. }
  1739. if (modepage == 0x3F) {
  1740. sd_printk(KERN_ERR, sdkp, "No Caching mode page "
  1741. "present\n");
  1742. goto defaults;
  1743. } else if ((buffer[offset] & 0x3f) != modepage) {
  1744. sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
  1745. goto defaults;
  1746. }
  1747. Page_found:
  1748. if (modepage == 8) {
  1749. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  1750. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  1751. } else {
  1752. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  1753. sdkp->RCD = 0;
  1754. }
  1755. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  1756. if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  1757. sd_printk(KERN_NOTICE, sdkp,
  1758. "Uses READ/WRITE(6), disabling FUA\n");
  1759. sdkp->DPOFUA = 0;
  1760. }
  1761. if (sdkp->first_scan || old_wce != sdkp->WCE ||
  1762. old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
  1763. sd_printk(KERN_NOTICE, sdkp,
  1764. "Write cache: %s, read cache: %s, %s\n",
  1765. sdkp->WCE ? "enabled" : "disabled",
  1766. sdkp->RCD ? "disabled" : "enabled",
  1767. sdkp->DPOFUA ? "supports DPO and FUA"
  1768. : "doesn't support DPO or FUA");
  1769. return;
  1770. }
  1771. bad_sense:
  1772. if (scsi_sense_valid(&sshdr) &&
  1773. sshdr.sense_key == ILLEGAL_REQUEST &&
  1774. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  1775. /* Invalid field in CDB */
  1776. sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  1777. else
  1778. sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
  1779. defaults:
  1780. sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
  1781. sdkp->WCE = 0;
  1782. sdkp->RCD = 0;
  1783. sdkp->DPOFUA = 0;
  1784. }
  1785. /*
  1786. * The ATO bit indicates whether the DIF application tag is available
  1787. * for use by the operating system.
  1788. */
  1789. static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
  1790. {
  1791. int res, offset;
  1792. struct scsi_device *sdp = sdkp->device;
  1793. struct scsi_mode_data data;
  1794. struct scsi_sense_hdr sshdr;
  1795. if (sdp->type != TYPE_DISK)
  1796. return;
  1797. if (sdkp->protection_type == 0)
  1798. return;
  1799. res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
  1800. SD_MAX_RETRIES, &data, &sshdr);
  1801. if (!scsi_status_is_good(res) || !data.header_length ||
  1802. data.length < 6) {
  1803. sd_printk(KERN_WARNING, sdkp,
  1804. "getting Control mode page failed, assume no ATO\n");
  1805. if (scsi_sense_valid(&sshdr))
  1806. sd_print_sense_hdr(sdkp, &sshdr);
  1807. return;
  1808. }
  1809. offset = data.header_length + data.block_descriptor_length;
  1810. if ((buffer[offset] & 0x3f) != 0x0a) {
  1811. sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
  1812. return;
  1813. }
  1814. if ((buffer[offset + 5] & 0x80) == 0)
  1815. return;
  1816. sdkp->ATO = 1;
  1817. return;
  1818. }
  1819. /**
  1820. * sd_read_block_limits - Query disk device for preferred I/O sizes.
  1821. * @disk: disk to query
  1822. */
  1823. static void sd_read_block_limits(struct scsi_disk *sdkp)
  1824. {
  1825. struct request_queue *q = sdkp->disk->queue;
  1826. unsigned int sector_sz = sdkp->device->sector_size;
  1827. const int vpd_len = 64;
  1828. unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
  1829. if (!buffer ||
  1830. /* Block Limits VPD */
  1831. scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
  1832. goto out;
  1833. blk_queue_io_min(sdkp->disk->queue,
  1834. get_unaligned_be16(&buffer[6]) * sector_sz);
  1835. blk_queue_io_opt(sdkp->disk->queue,
  1836. get_unaligned_be32(&buffer[12]) * sector_sz);
  1837. /* Thin provisioning enabled and page length indicates TP support */
  1838. if (sdkp->thin_provisioning && buffer[3] == 0x3c) {
  1839. unsigned int lba_count, desc_count, granularity;
  1840. lba_count = get_unaligned_be32(&buffer[20]);
  1841. desc_count = get_unaligned_be32(&buffer[24]);
  1842. if (lba_count && desc_count) {
  1843. if (sdkp->tpvpd && !sdkp->tpu)
  1844. sdkp->unmap = 0;
  1845. else
  1846. sdkp->unmap = 1;
  1847. }
  1848. if (sdkp->tpvpd && !sdkp->tpu && !sdkp->tpws) {
  1849. sd_printk(KERN_ERR, sdkp, "Thin provisioning is " \
  1850. "enabled but neither TPU, nor TPWS are " \
  1851. "set. Disabling discard!\n");
  1852. goto out;
  1853. }
  1854. if (lba_count)
  1855. q->limits.max_discard_sectors =
  1856. lba_count * sector_sz >> 9;
  1857. granularity = get_unaligned_be32(&buffer[28]);
  1858. if (granularity)
  1859. q->limits.discard_granularity = granularity * sector_sz;
  1860. if (buffer[32] & 0x80)
  1861. q->limits.discard_alignment =
  1862. get_unaligned_be32(&buffer[32]) & ~(1 << 31);
  1863. }
  1864. out:
  1865. kfree(buffer);
  1866. }
  1867. /**
  1868. * sd_read_block_characteristics - Query block dev. characteristics
  1869. * @disk: disk to query
  1870. */
  1871. static void sd_read_block_characteristics(struct scsi_disk *sdkp)
  1872. {
  1873. unsigned char *buffer;
  1874. u16 rot;
  1875. const int vpd_len = 64;
  1876. buffer = kmalloc(vpd_len, GFP_KERNEL);
  1877. if (!buffer ||
  1878. /* Block Device Characteristics VPD */
  1879. scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
  1880. goto out;
  1881. rot = get_unaligned_be16(&buffer[4]);
  1882. if (rot == 1)
  1883. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
  1884. out:
  1885. kfree(buffer);
  1886. }
  1887. /**
  1888. * sd_read_thin_provisioning - Query thin provisioning VPD page
  1889. * @disk: disk to query
  1890. */
  1891. static void sd_read_thin_provisioning(struct scsi_disk *sdkp)
  1892. {
  1893. unsigned char *buffer;
  1894. const int vpd_len = 8;
  1895. if (sdkp->thin_provisioning == 0)
  1896. return;
  1897. buffer = kmalloc(vpd_len, GFP_KERNEL);
  1898. if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
  1899. goto out;
  1900. sdkp->tpvpd = 1;
  1901. sdkp->tpu = (buffer[5] >> 7) & 1; /* UNMAP */
  1902. sdkp->tpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
  1903. out:
  1904. kfree(buffer);
  1905. }
  1906. static int sd_try_extended_inquiry(struct scsi_device *sdp)
  1907. {
  1908. /*
  1909. * Although VPD inquiries can go to SCSI-2 type devices,
  1910. * some USB ones crash on receiving them, and the pages
  1911. * we currently ask for are for SPC-3 and beyond
  1912. */
  1913. if (sdp->scsi_level > SCSI_SPC_2)
  1914. return 1;
  1915. return 0;
  1916. }
  1917. /**
  1918. * sd_revalidate_disk - called the first time a new disk is seen,
  1919. * performs disk spin up, read_capacity, etc.
  1920. * @disk: struct gendisk we care about
  1921. **/
  1922. static int sd_revalidate_disk(struct gendisk *disk)
  1923. {
  1924. struct scsi_disk *sdkp = scsi_disk(disk);
  1925. struct scsi_device *sdp = sdkp->device;
  1926. unsigned char *buffer;
  1927. unsigned flush = 0;
  1928. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
  1929. "sd_revalidate_disk\n"));
  1930. /*
  1931. * If the device is offline, don't try and read capacity or any
  1932. * of the other niceties.
  1933. */
  1934. if (!scsi_device_online(sdp))
  1935. goto out;
  1936. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
  1937. if (!buffer) {
  1938. sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  1939. "allocation failure.\n");
  1940. goto out;
  1941. }
  1942. sd_spinup_disk(sdkp);
  1943. /*
  1944. * Without media there is no reason to ask; moreover, some devices
  1945. * react badly if we do.
  1946. */
  1947. if (sdkp->media_present) {
  1948. sd_read_capacity(sdkp, buffer);
  1949. if (sd_try_extended_inquiry(sdp)) {
  1950. sd_read_thin_provisioning(sdkp);
  1951. sd_read_block_limits(sdkp);
  1952. sd_read_block_characteristics(sdkp);
  1953. }
  1954. sd_read_write_protect_flag(sdkp, buffer);
  1955. sd_read_cache_type(sdkp, buffer);
  1956. sd_read_app_tag_own(sdkp, buffer);
  1957. }
  1958. sdkp->first_scan = 0;
  1959. /*
  1960. * We now have all cache related info, determine how we deal
  1961. * with flush requests.
  1962. */
  1963. if (sdkp->WCE) {
  1964. flush |= REQ_FLUSH;
  1965. if (sdkp->DPOFUA)
  1966. flush |= REQ_FUA;
  1967. }
  1968. blk_queue_flush(sdkp->disk->queue, flush);
  1969. set_capacity(disk, sdkp->capacity);
  1970. kfree(buffer);
  1971. out:
  1972. return 0;
  1973. }
  1974. /**
  1975. * sd_unlock_native_capacity - unlock native capacity
  1976. * @disk: struct gendisk to set capacity for
  1977. *
  1978. * Block layer calls this function if it detects that partitions
  1979. * on @disk reach beyond the end of the device. If the SCSI host
  1980. * implements ->unlock_native_capacity() method, it's invoked to
  1981. * give it a chance to adjust the device capacity.
  1982. *
  1983. * CONTEXT:
  1984. * Defined by block layer. Might sleep.
  1985. */
  1986. static void sd_unlock_native_capacity(struct gendisk *disk)
  1987. {
  1988. struct scsi_device *sdev = scsi_disk(disk)->device;
  1989. if (sdev->host->hostt->unlock_native_capacity)
  1990. sdev->host->hostt->unlock_native_capacity(sdev);
  1991. }
  1992. /**
  1993. * sd_format_disk_name - format disk name
  1994. * @prefix: name prefix - ie. "sd" for SCSI disks
  1995. * @index: index of the disk to format name for
  1996. * @buf: output buffer
  1997. * @buflen: length of the output buffer
  1998. *
  1999. * SCSI disk names starts at sda. The 26th device is sdz and the
  2000. * 27th is sdaa. The last one for two lettered suffix is sdzz
  2001. * which is followed by sdaaa.
  2002. *
  2003. * This is basically 26 base counting with one extra 'nil' entry
  2004. * at the beginning from the second digit on and can be
  2005. * determined using similar method as 26 base conversion with the
  2006. * index shifted -1 after each digit is computed.
  2007. *
  2008. * CONTEXT:
  2009. * Don't care.
  2010. *
  2011. * RETURNS:
  2012. * 0 on success, -errno on failure.
  2013. */
  2014. static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  2015. {
  2016. const int base = 'z' - 'a' + 1;
  2017. char *begin = buf + strlen(prefix);
  2018. char *end = buf + buflen;
  2019. char *p;
  2020. int unit;
  2021. p = end - 1;
  2022. *p = '\0';
  2023. unit = base;
  2024. do {
  2025. if (p == begin)
  2026. return -EINVAL;
  2027. *--p = 'a' + (index % unit);
  2028. index = (index / unit) - 1;
  2029. } while (index >= 0);
  2030. memmove(begin, p, end - p);
  2031. memcpy(buf, prefix, strlen(prefix));
  2032. return 0;
  2033. }
  2034. /*
  2035. * The asynchronous part of sd_probe
  2036. */
  2037. static void sd_probe_async(void *data, async_cookie_t cookie)
  2038. {
  2039. struct scsi_disk *sdkp = data;
  2040. struct scsi_device *sdp;
  2041. struct gendisk *gd;
  2042. u32 index;
  2043. struct device *dev;
  2044. sdp = sdkp->device;
  2045. gd = sdkp->disk;
  2046. index = sdkp->index;
  2047. dev = &sdp->sdev_gendev;
  2048. gd->major = sd_major((index & 0xf0) >> 4);
  2049. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  2050. gd->minors = SD_MINORS;
  2051. gd->fops = &sd_fops;
  2052. gd->private_data = &sdkp->driver;
  2053. gd->queue = sdkp->device->request_queue;
  2054. /* defaults, until the device tells us otherwise */
  2055. sdp->sector_size = 512;
  2056. sdkp->capacity = 0;
  2057. sdkp->media_present = 1;
  2058. sdkp->write_prot = 0;
  2059. sdkp->WCE = 0;
  2060. sdkp->RCD = 0;
  2061. sdkp->ATO = 0;
  2062. sdkp->first_scan = 1;
  2063. sd_revalidate_disk(gd);
  2064. blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
  2065. blk_queue_unprep_rq(sdp->request_queue, sd_unprep_fn);
  2066. gd->driverfs_dev = &sdp->sdev_gendev;
  2067. gd->flags = GENHD_FL_EXT_DEVT;
  2068. if (sdp->removable)
  2069. gd->flags |= GENHD_FL_REMOVABLE;
  2070. add_disk(gd);
  2071. sd_dif_config_host(sdkp);
  2072. sd_revalidate_disk(gd);
  2073. sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  2074. sdp->removable ? "removable " : "");
  2075. scsi_autopm_put_device(sdp);
  2076. put_device(&sdkp->dev);
  2077. }
  2078. /**
  2079. * sd_probe - called during driver initialization and whenever a
  2080. * new scsi device is attached to the system. It is called once
  2081. * for each scsi device (not just disks) present.
  2082. * @dev: pointer to device object
  2083. *
  2084. * Returns 0 if successful (or not interested in this scsi device
  2085. * (e.g. scanner)); 1 when there is an error.
  2086. *
  2087. * Note: this function is invoked from the scsi mid-level.
  2088. * This function sets up the mapping between a given
  2089. * <host,channel,id,lun> (found in sdp) and new device name
  2090. * (e.g. /dev/sda). More precisely it is the block device major
  2091. * and minor number that is chosen here.
  2092. *
  2093. * Assume sd_attach is not re-entrant (for time being)
  2094. * Also think about sd_attach() and sd_remove() running coincidentally.
  2095. **/
  2096. static int sd_probe(struct device *dev)
  2097. {
  2098. struct scsi_device *sdp = to_scsi_device(dev);
  2099. struct scsi_disk *sdkp;
  2100. struct gendisk *gd;
  2101. int index;
  2102. int error;
  2103. error = -ENODEV;
  2104. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  2105. goto out;
  2106. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  2107. "sd_attach\n"));
  2108. error = -ENOMEM;
  2109. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  2110. if (!sdkp)
  2111. goto out;
  2112. gd = alloc_disk(SD_MINORS);
  2113. if (!gd)
  2114. goto out_free;
  2115. do {
  2116. if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
  2117. goto out_put;
  2118. spin_lock(&sd_index_lock);
  2119. error = ida_get_new(&sd_index_ida, &index);
  2120. spin_unlock(&sd_index_lock);
  2121. } while (error == -EAGAIN);
  2122. if (error)
  2123. goto out_put;
  2124. if (index >= SD_MAX_DISKS) {
  2125. error = -ENODEV;
  2126. sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name space exhausted.\n");
  2127. goto out_free_index;
  2128. }
  2129. error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  2130. if (error)
  2131. goto out_free_index;
  2132. sdkp->device = sdp;
  2133. sdkp->driver = &sd_template;
  2134. sdkp->disk = gd;
  2135. sdkp->index = index;
  2136. atomic_set(&sdkp->openers, 0);
  2137. sdkp->previous_state = 1;
  2138. if (!sdp->request_queue->rq_timeout) {
  2139. if (sdp->type != TYPE_MOD)
  2140. blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  2141. else
  2142. blk_queue_rq_timeout(sdp->request_queue,
  2143. SD_MOD_TIMEOUT);
  2144. }
  2145. device_initialize(&sdkp->dev);
  2146. sdkp->dev.parent = dev;
  2147. sdkp->dev.class = &sd_disk_class;
  2148. dev_set_name(&sdkp->dev, dev_name(dev));
  2149. if (device_add(&sdkp->dev))
  2150. goto out_free_index;
  2151. get_device(dev);
  2152. dev_set_drvdata(dev, sdkp);
  2153. get_device(&sdkp->dev); /* prevent release before async_schedule */
  2154. async_schedule(sd_probe_async, sdkp);
  2155. return 0;
  2156. out_free_index:
  2157. spin_lock(&sd_index_lock);
  2158. ida_remove(&sd_index_ida, index);
  2159. spin_unlock(&sd_index_lock);
  2160. out_put:
  2161. put_disk(gd);
  2162. out_free:
  2163. kfree(sdkp);
  2164. out:
  2165. return error;
  2166. }
  2167. /**
  2168. * sd_remove - called whenever a scsi disk (previously recognized by
  2169. * sd_probe) is detached from the system. It is called (potentially
  2170. * multiple times) during sd module unload.
  2171. * @sdp: pointer to mid level scsi device object
  2172. *
  2173. * Note: this function is invoked from the scsi mid-level.
  2174. * This function potentially frees up a device name (e.g. /dev/sdc)
  2175. * that could be re-used by a subsequent sd_probe().
  2176. * This function is not called when the built-in sd driver is "exit-ed".
  2177. **/
  2178. static int sd_remove(struct device *dev)
  2179. {
  2180. struct scsi_disk *sdkp;
  2181. sdkp = dev_get_drvdata(dev);
  2182. scsi_autopm_get_device(sdkp->device);
  2183. async_synchronize_full();
  2184. blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
  2185. blk_queue_unprep_rq(sdkp->device->request_queue, NULL);
  2186. device_del(&sdkp->dev);
  2187. del_gendisk(sdkp->disk);
  2188. sd_shutdown(dev);
  2189. mutex_lock(&sd_ref_mutex);
  2190. dev_set_drvdata(dev, NULL);
  2191. put_device(&sdkp->dev);
  2192. mutex_unlock(&sd_ref_mutex);
  2193. return 0;
  2194. }
  2195. /**
  2196. * scsi_disk_release - Called to free the scsi_disk structure
  2197. * @dev: pointer to embedded class device
  2198. *
  2199. * sd_ref_mutex must be held entering this routine. Because it is
  2200. * called on last put, you should always use the scsi_disk_get()
  2201. * scsi_disk_put() helpers which manipulate the semaphore directly
  2202. * and never do a direct put_device.
  2203. **/
  2204. static void scsi_disk_release(struct device *dev)
  2205. {
  2206. struct scsi_disk *sdkp = to_scsi_disk(dev);
  2207. struct gendisk *disk = sdkp->disk;
  2208. spin_lock(&sd_index_lock);
  2209. ida_remove(&sd_index_ida, sdkp->index);
  2210. spin_unlock(&sd_index_lock);
  2211. disk->private_data = NULL;
  2212. put_disk(disk);
  2213. put_device(&sdkp->device->sdev_gendev);
  2214. kfree(sdkp);
  2215. }
  2216. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  2217. {
  2218. unsigned char cmd[6] = { START_STOP }; /* START_VALID */
  2219. struct scsi_sense_hdr sshdr;
  2220. struct scsi_device *sdp = sdkp->device;
  2221. int res;
  2222. if (start)
  2223. cmd[4] |= 1; /* START */
  2224. if (sdp->start_stop_pwr_cond)
  2225. cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
  2226. if (!scsi_device_online(sdp))
  2227. return -ENODEV;
  2228. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  2229. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  2230. if (res) {
  2231. sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
  2232. sd_print_result(sdkp, res);
  2233. if (driver_byte(res) & DRIVER_SENSE)
  2234. sd_print_sense_hdr(sdkp, &sshdr);
  2235. }
  2236. return res;
  2237. }
  2238. /*
  2239. * Send a SYNCHRONIZE CACHE instruction down to the device through
  2240. * the normal SCSI command structure. Wait for the command to
  2241. * complete.
  2242. */
  2243. static void sd_shutdown(struct device *dev)
  2244. {
  2245. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2246. if (!sdkp)
  2247. return; /* this can happen */
  2248. if (sdkp->WCE) {
  2249. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2250. sd_sync_cache(sdkp);
  2251. }
  2252. if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  2253. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2254. sd_start_stop_device(sdkp, 0);
  2255. }
  2256. scsi_disk_put(sdkp);
  2257. }
  2258. static int sd_suspend(struct device *dev, pm_message_t mesg)
  2259. {
  2260. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2261. int ret = 0;
  2262. if (!sdkp)
  2263. return 0; /* this can happen */
  2264. if (sdkp->WCE) {
  2265. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2266. ret = sd_sync_cache(sdkp);
  2267. if (ret)
  2268. goto done;
  2269. }
  2270. if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
  2271. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2272. ret = sd_start_stop_device(sdkp, 0);
  2273. }
  2274. done:
  2275. scsi_disk_put(sdkp);
  2276. return ret;
  2277. }
  2278. static int sd_resume(struct device *dev)
  2279. {
  2280. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2281. int ret = 0;
  2282. if (!sdkp->device->manage_start_stop)
  2283. goto done;
  2284. sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  2285. ret = sd_start_stop_device(sdkp, 1);
  2286. done:
  2287. scsi_disk_put(sdkp);
  2288. return ret;
  2289. }
  2290. /**
  2291. * init_sd - entry point for this driver (both when built in or when
  2292. * a module).
  2293. *
  2294. * Note: this function registers this driver with the scsi mid-level.
  2295. **/
  2296. static int __init init_sd(void)
  2297. {
  2298. int majors = 0, i, err;
  2299. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  2300. for (i = 0; i < SD_MAJORS; i++)
  2301. if (register_blkdev(sd_major(i), "sd") == 0)
  2302. majors++;
  2303. if (!majors)
  2304. return -ENODEV;
  2305. err = class_register(&sd_disk_class);
  2306. if (err)
  2307. goto err_out;
  2308. err = scsi_register_driver(&sd_template.gendrv);
  2309. if (err)
  2310. goto err_out_class;
  2311. sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  2312. 0, 0, NULL);
  2313. if (!sd_cdb_cache) {
  2314. printk(KERN_ERR "sd: can't init extended cdb cache\n");
  2315. goto err_out_class;
  2316. }
  2317. sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  2318. if (!sd_cdb_pool) {
  2319. printk(KERN_ERR "sd: can't init extended cdb pool\n");
  2320. goto err_out_cache;
  2321. }
  2322. return 0;
  2323. err_out_cache:
  2324. kmem_cache_destroy(sd_cdb_cache);
  2325. err_out_class:
  2326. class_unregister(&sd_disk_class);
  2327. err_out:
  2328. for (i = 0; i < SD_MAJORS; i++)
  2329. unregister_blkdev(sd_major(i), "sd");
  2330. return err;
  2331. }
  2332. /**
  2333. * exit_sd - exit point for this driver (when it is a module).
  2334. *
  2335. * Note: this function unregisters this driver from the scsi mid-level.
  2336. **/
  2337. static void __exit exit_sd(void)
  2338. {
  2339. int i;
  2340. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  2341. mempool_destroy(sd_cdb_pool);
  2342. kmem_cache_destroy(sd_cdb_cache);
  2343. scsi_unregister_driver(&sd_template.gendrv);
  2344. class_unregister(&sd_disk_class);
  2345. for (i = 0; i < SD_MAJORS; i++)
  2346. unregister_blkdev(sd_major(i), "sd");
  2347. }
  2348. module_init(init_sd);
  2349. module_exit(exit_sd);
  2350. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  2351. struct scsi_sense_hdr *sshdr)
  2352. {
  2353. sd_printk(KERN_INFO, sdkp, " ");
  2354. scsi_show_sense_hdr(sshdr);
  2355. sd_printk(KERN_INFO, sdkp, " ");
  2356. scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
  2357. }
  2358. static void sd_print_result(struct scsi_disk *sdkp, int result)
  2359. {
  2360. sd_printk(KERN_INFO, sdkp, " ");
  2361. scsi_show_result(result);
  2362. }