sd.c 93 KB

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