sd.c 83 KB

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