sd.c 56 KB

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