sd.c 69 KB

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