sd.c 82 KB

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