sd.c 59 KB

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