sd.c 87 KB

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