sd.c 71 KB

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