sd.c 84 KB

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