sd.c 98 KB

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