sd.c 71 KB

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