sd.c 69 KB

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