sd.c 59 KB

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