sd.c 56 KB

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