sd.c 84 KB

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