sd.c 67 KB

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