sd.c 76 KB

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