sd.c 87 KB

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