sd.c 85 KB

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