sg.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  1. /*
  2. * History:
  3. * Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
  4. * to allow user process control of SCSI devices.
  5. * Development Sponsored by Killy Corp. NY NY
  6. *
  7. * Original driver (sg.c):
  8. * Copyright (C) 1992 Lawrence Foard
  9. * Version 2 and 3 extensions to driver:
  10. * Copyright (C) 1998 - 2014 Douglas Gilbert
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. */
  18. static int sg_version_num = 30536; /* 2 digits for each component */
  19. #define SG_VERSION_STR "3.5.36"
  20. /*
  21. * D. P. Gilbert (dgilbert@interlog.com), notes:
  22. * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
  23. * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
  24. * (otherwise the macros compile to empty statements).
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/fs.h>
  29. #include <linux/kernel.h>
  30. #include <linux/sched.h>
  31. #include <linux/string.h>
  32. #include <linux/mm.h>
  33. #include <linux/errno.h>
  34. #include <linux/mtio.h>
  35. #include <linux/ioctl.h>
  36. #include <linux/slab.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/init.h>
  39. #include <linux/poll.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/cdev.h>
  42. #include <linux/idr.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/delay.h>
  46. #include <linux/blktrace_api.h>
  47. #include <linux/mutex.h>
  48. #include <linux/ratelimit.h>
  49. #include "scsi.h"
  50. #include <scsi/scsi_dbg.h>
  51. #include <scsi/scsi_host.h>
  52. #include <scsi/scsi_driver.h>
  53. #include <scsi/scsi_ioctl.h>
  54. #include <scsi/sg.h>
  55. #include "scsi_logging.h"
  56. #ifdef CONFIG_SCSI_PROC_FS
  57. #include <linux/proc_fs.h>
  58. static char *sg_version_date = "20140603";
  59. static int sg_proc_init(void);
  60. static void sg_proc_cleanup(void);
  61. #endif
  62. #define SG_ALLOW_DIO_DEF 0
  63. #define SG_MAX_DEVS 32768
  64. /* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
  65. * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
  66. * than 16 bytes are "variable length" whose length is a multiple of 4
  67. */
  68. #define SG_MAX_CDB_SIZE 252
  69. /*
  70. * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
  71. * Then when using 32 bit integers x * m may overflow during the calculation.
  72. * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
  73. * calculates the same, but prevents the overflow when both m and d
  74. * are "small" numbers (like HZ and USER_HZ).
  75. * Of course an overflow is inavoidable if the result of muldiv doesn't fit
  76. * in 32 bits.
  77. */
  78. #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
  79. #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
  80. int sg_big_buff = SG_DEF_RESERVED_SIZE;
  81. /* N.B. This variable is readable and writeable via
  82. /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
  83. of this size (or less if there is not enough memory) will be reserved
  84. for use by this file descriptor. [Deprecated usage: this variable is also
  85. readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
  86. the kernel (i.e. it is not a module).] */
  87. static int def_reserved_size = -1; /* picks up init parameter */
  88. static int sg_allow_dio = SG_ALLOW_DIO_DEF;
  89. static int scatter_elem_sz = SG_SCATTER_SZ;
  90. static int scatter_elem_sz_prev = SG_SCATTER_SZ;
  91. #define SG_SECTOR_SZ 512
  92. static int sg_add(struct device *, struct class_interface *);
  93. static void sg_remove(struct device *, struct class_interface *);
  94. static DEFINE_MUTEX(sg_mutex);
  95. static DEFINE_IDR(sg_index_idr);
  96. static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
  97. file descriptor list for device */
  98. static struct class_interface sg_interface = {
  99. .add_dev = sg_add,
  100. .remove_dev = sg_remove,
  101. };
  102. typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
  103. unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
  104. unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
  105. unsigned bufflen; /* Size of (aggregate) data buffer */
  106. struct page **pages;
  107. int page_order;
  108. char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
  109. unsigned char cmd_opcode; /* first byte of command */
  110. } Sg_scatter_hold;
  111. struct sg_device; /* forward declarations */
  112. struct sg_fd;
  113. typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
  114. struct sg_request *nextrp; /* NULL -> tail request (slist) */
  115. struct sg_fd *parentfp; /* NULL -> not in use */
  116. Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
  117. sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
  118. unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
  119. char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
  120. char orphan; /* 1 -> drop on sight, 0 -> normal */
  121. char sg_io_owned; /* 1 -> packet belongs to SG_IO */
  122. volatile char done; /* 0->before bh, 1->before read, 2->read */
  123. struct request *rq;
  124. struct bio *bio;
  125. struct execute_work ew;
  126. } Sg_request;
  127. typedef struct sg_fd { /* holds the state of a file descriptor */
  128. struct list_head sfd_siblings;
  129. struct sg_device *parentdp; /* owning device */
  130. wait_queue_head_t read_wait; /* queue read until command done */
  131. rwlock_t rq_list_lock; /* protect access to list in req_arr */
  132. int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
  133. int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
  134. Sg_scatter_hold reserve; /* buffer held for this file descriptor */
  135. unsigned save_scat_len; /* original length of trunc. scat. element */
  136. Sg_request *headrp; /* head of request slist, NULL->empty */
  137. struct fasync_struct *async_qp; /* used by asynchronous notification */
  138. Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
  139. char low_dma; /* as in parent but possibly overridden to 1 */
  140. char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
  141. volatile char closed; /* 1 -> fd closed but request(s) outstanding */
  142. char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
  143. unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
  144. char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
  145. char mmap_called; /* 0 -> mmap() never called on this fd */
  146. struct kref f_ref;
  147. struct execute_work ew;
  148. } Sg_fd;
  149. typedef struct sg_device { /* holds the state of each scsi generic device */
  150. struct scsi_device *device;
  151. wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
  152. struct mutex open_rel_lock; /* held when in open() or release() */
  153. int sg_tablesize; /* adapter's max scatter-gather table size */
  154. u32 index; /* device index number */
  155. struct list_head sfds;
  156. volatile char detached; /* 0->attached, 1->detached pending removal */
  157. volatile char exclude; /* opened for exclusive access */
  158. char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
  159. struct gendisk *disk;
  160. struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
  161. struct kref d_ref;
  162. } Sg_device;
  163. /* tasklet or soft irq callback */
  164. static void sg_rq_end_io(struct request *rq, int uptodate);
  165. static int sg_start_req(Sg_request *srp, unsigned char *cmd);
  166. static int sg_finish_rem_req(Sg_request * srp);
  167. static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
  168. static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
  169. Sg_request * srp);
  170. static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
  171. const char __user *buf, size_t count, int blocking,
  172. int read_only, int sg_io_owned, Sg_request **o_srp);
  173. static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
  174. unsigned char *cmnd, int timeout, int blocking);
  175. static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
  176. static void sg_remove_scat(Sg_scatter_hold * schp);
  177. static void sg_build_reserve(Sg_fd * sfp, int req_size);
  178. static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
  179. static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
  180. static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
  181. static void sg_remove_sfp(struct kref *);
  182. static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
  183. static Sg_request *sg_add_request(Sg_fd * sfp);
  184. static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
  185. static int sg_res_in_use(Sg_fd * sfp);
  186. static Sg_device *sg_get_dev(int dev);
  187. static void sg_put_dev(Sg_device *sdp);
  188. #define SZ_SG_HEADER sizeof(struct sg_header)
  189. #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
  190. #define SZ_SG_IOVEC sizeof(sg_iovec_t)
  191. #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
  192. static int sg_allow_access(struct file *filp, unsigned char *cmd)
  193. {
  194. struct sg_fd *sfp = filp->private_data;
  195. if (sfp->parentdp->device->type == TYPE_SCANNER)
  196. return 0;
  197. return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE);
  198. }
  199. static int
  200. sg_open(struct inode *inode, struct file *filp)
  201. {
  202. int dev = iminor(inode);
  203. int flags = filp->f_flags;
  204. struct request_queue *q;
  205. Sg_device *sdp;
  206. Sg_fd *sfp;
  207. int res;
  208. int retval;
  209. mutex_lock(&sg_mutex);
  210. nonseekable_open(inode, filp);
  211. SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
  212. sdp = sg_get_dev(dev);
  213. if (IS_ERR(sdp)) {
  214. retval = PTR_ERR(sdp);
  215. sdp = NULL;
  216. goto sg_put;
  217. }
  218. /* This driver's module count bumped by fops_get in <linux/fs.h> */
  219. /* Prevent the device driver from vanishing while we sleep */
  220. retval = scsi_device_get(sdp->device);
  221. if (retval)
  222. goto sg_put;
  223. retval = scsi_autopm_get_device(sdp->device);
  224. if (retval)
  225. goto sdp_put;
  226. if (!((flags & O_NONBLOCK) ||
  227. scsi_block_when_processing_errors(sdp->device))) {
  228. retval = -ENXIO;
  229. /* we are in error recovery for this device */
  230. goto error_out;
  231. }
  232. if (flags & O_EXCL) {
  233. if (O_RDONLY == (flags & O_ACCMODE)) {
  234. retval = -EPERM; /* Can't lock it with read only access */
  235. goto error_out;
  236. }
  237. if (!list_empty(&sdp->sfds) && (flags & O_NONBLOCK)) {
  238. retval = -EBUSY;
  239. goto error_out;
  240. }
  241. res = 0;
  242. __wait_event_interruptible(sdp->o_excl_wait,
  243. ((!list_empty(&sdp->sfds) || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
  244. if (res) {
  245. retval = res; /* -ERESTARTSYS because signal hit process */
  246. goto error_out;
  247. }
  248. } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
  249. if (flags & O_NONBLOCK) {
  250. retval = -EBUSY;
  251. goto error_out;
  252. }
  253. res = 0;
  254. __wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
  255. res);
  256. if (res) {
  257. retval = res; /* -ERESTARTSYS because signal hit process */
  258. goto error_out;
  259. }
  260. }
  261. if (sdp->detached) {
  262. retval = -ENODEV;
  263. goto error_out;
  264. }
  265. if (list_empty(&sdp->sfds)) { /* no existing opens on this device */
  266. sdp->sgdebug = 0;
  267. q = sdp->device->request_queue;
  268. sdp->sg_tablesize = queue_max_segments(q);
  269. }
  270. if ((sfp = sg_add_sfp(sdp, dev)))
  271. filp->private_data = sfp;
  272. else {
  273. if (flags & O_EXCL) {
  274. sdp->exclude = 0; /* undo if error */
  275. wake_up_interruptible(&sdp->o_excl_wait);
  276. }
  277. retval = -ENOMEM;
  278. goto error_out;
  279. }
  280. retval = 0;
  281. error_out:
  282. if (retval) {
  283. scsi_autopm_put_device(sdp->device);
  284. sdp_put:
  285. scsi_device_put(sdp->device);
  286. }
  287. sg_put:
  288. if (sdp)
  289. sg_put_dev(sdp);
  290. mutex_unlock(&sg_mutex);
  291. return retval;
  292. }
  293. /* Following function was formerly called 'sg_close' */
  294. static int
  295. sg_release(struct inode *inode, struct file *filp)
  296. {
  297. Sg_device *sdp;
  298. Sg_fd *sfp;
  299. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  300. return -ENXIO;
  301. SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
  302. sfp->closed = 1;
  303. sdp->exclude = 0;
  304. wake_up_interruptible(&sdp->o_excl_wait);
  305. scsi_autopm_put_device(sdp->device);
  306. kref_put(&sfp->f_ref, sg_remove_sfp);
  307. return 0;
  308. }
  309. static ssize_t
  310. sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
  311. {
  312. Sg_device *sdp;
  313. Sg_fd *sfp;
  314. Sg_request *srp;
  315. int req_pack_id = -1;
  316. sg_io_hdr_t *hp;
  317. struct sg_header *old_hdr = NULL;
  318. int retval = 0;
  319. if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
  320. return -EINVAL;
  321. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  322. return -ENXIO;
  323. SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
  324. sdp->disk->disk_name, (int) count));
  325. if (!access_ok(VERIFY_WRITE, buf, count))
  326. return -EFAULT;
  327. if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
  328. old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
  329. if (!old_hdr)
  330. return -ENOMEM;
  331. if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
  332. retval = -EFAULT;
  333. goto free_old_hdr;
  334. }
  335. if (old_hdr->reply_len < 0) {
  336. if (count >= SZ_SG_IO_HDR) {
  337. sg_io_hdr_t *new_hdr;
  338. new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
  339. if (!new_hdr) {
  340. retval = -ENOMEM;
  341. goto free_old_hdr;
  342. }
  343. retval =__copy_from_user
  344. (new_hdr, buf, SZ_SG_IO_HDR);
  345. req_pack_id = new_hdr->pack_id;
  346. kfree(new_hdr);
  347. if (retval) {
  348. retval = -EFAULT;
  349. goto free_old_hdr;
  350. }
  351. }
  352. } else
  353. req_pack_id = old_hdr->pack_id;
  354. }
  355. srp = sg_get_rq_mark(sfp, req_pack_id);
  356. if (!srp) { /* now wait on packet to arrive */
  357. if (sdp->detached) {
  358. retval = -ENODEV;
  359. goto free_old_hdr;
  360. }
  361. if (filp->f_flags & O_NONBLOCK) {
  362. retval = -EAGAIN;
  363. goto free_old_hdr;
  364. }
  365. while (1) {
  366. retval = 0; /* following macro beats race condition */
  367. __wait_event_interruptible(sfp->read_wait,
  368. (sdp->detached ||
  369. (srp = sg_get_rq_mark(sfp, req_pack_id))),
  370. retval);
  371. if (sdp->detached) {
  372. retval = -ENODEV;
  373. goto free_old_hdr;
  374. }
  375. if (0 == retval)
  376. break;
  377. /* -ERESTARTSYS as signal hit process */
  378. goto free_old_hdr;
  379. }
  380. }
  381. if (srp->header.interface_id != '\0') {
  382. retval = sg_new_read(sfp, buf, count, srp);
  383. goto free_old_hdr;
  384. }
  385. hp = &srp->header;
  386. if (old_hdr == NULL) {
  387. old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
  388. if (! old_hdr) {
  389. retval = -ENOMEM;
  390. goto free_old_hdr;
  391. }
  392. }
  393. memset(old_hdr, 0, SZ_SG_HEADER);
  394. old_hdr->reply_len = (int) hp->timeout;
  395. old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
  396. old_hdr->pack_id = hp->pack_id;
  397. old_hdr->twelve_byte =
  398. ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
  399. old_hdr->target_status = hp->masked_status;
  400. old_hdr->host_status = hp->host_status;
  401. old_hdr->driver_status = hp->driver_status;
  402. if ((CHECK_CONDITION & hp->masked_status) ||
  403. (DRIVER_SENSE & hp->driver_status))
  404. memcpy(old_hdr->sense_buffer, srp->sense_b,
  405. sizeof (old_hdr->sense_buffer));
  406. switch (hp->host_status) {
  407. /* This setup of 'result' is for backward compatibility and is best
  408. ignored by the user who should use target, host + driver status */
  409. case DID_OK:
  410. case DID_PASSTHROUGH:
  411. case DID_SOFT_ERROR:
  412. old_hdr->result = 0;
  413. break;
  414. case DID_NO_CONNECT:
  415. case DID_BUS_BUSY:
  416. case DID_TIME_OUT:
  417. old_hdr->result = EBUSY;
  418. break;
  419. case DID_BAD_TARGET:
  420. case DID_ABORT:
  421. case DID_PARITY:
  422. case DID_RESET:
  423. case DID_BAD_INTR:
  424. old_hdr->result = EIO;
  425. break;
  426. case DID_ERROR:
  427. old_hdr->result = (srp->sense_b[0] == 0 &&
  428. hp->masked_status == GOOD) ? 0 : EIO;
  429. break;
  430. default:
  431. old_hdr->result = EIO;
  432. break;
  433. }
  434. /* Now copy the result back to the user buffer. */
  435. if (count >= SZ_SG_HEADER) {
  436. if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
  437. retval = -EFAULT;
  438. goto free_old_hdr;
  439. }
  440. buf += SZ_SG_HEADER;
  441. if (count > old_hdr->reply_len)
  442. count = old_hdr->reply_len;
  443. if (count > SZ_SG_HEADER) {
  444. if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
  445. retval = -EFAULT;
  446. goto free_old_hdr;
  447. }
  448. }
  449. } else
  450. count = (old_hdr->result == 0) ? 0 : -EIO;
  451. sg_finish_rem_req(srp);
  452. retval = count;
  453. free_old_hdr:
  454. kfree(old_hdr);
  455. return retval;
  456. }
  457. static ssize_t
  458. sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
  459. {
  460. sg_io_hdr_t *hp = &srp->header;
  461. int err = 0, err2;
  462. int len;
  463. if (count < SZ_SG_IO_HDR) {
  464. err = -EINVAL;
  465. goto err_out;
  466. }
  467. hp->sb_len_wr = 0;
  468. if ((hp->mx_sb_len > 0) && hp->sbp) {
  469. if ((CHECK_CONDITION & hp->masked_status) ||
  470. (DRIVER_SENSE & hp->driver_status)) {
  471. int sb_len = SCSI_SENSE_BUFFERSIZE;
  472. sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
  473. len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
  474. len = (len > sb_len) ? sb_len : len;
  475. if (copy_to_user(hp->sbp, srp->sense_b, len)) {
  476. err = -EFAULT;
  477. goto err_out;
  478. }
  479. hp->sb_len_wr = len;
  480. }
  481. }
  482. if (hp->masked_status || hp->host_status || hp->driver_status)
  483. hp->info |= SG_INFO_CHECK;
  484. if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
  485. err = -EFAULT;
  486. goto err_out;
  487. }
  488. err_out:
  489. err2 = sg_finish_rem_req(srp);
  490. return err ? : err2 ? : count;
  491. }
  492. static ssize_t
  493. sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
  494. {
  495. int mxsize, cmd_size, k;
  496. int input_size, blocking;
  497. unsigned char opcode;
  498. Sg_device *sdp;
  499. Sg_fd *sfp;
  500. Sg_request *srp;
  501. struct sg_header old_hdr;
  502. sg_io_hdr_t *hp;
  503. unsigned char cmnd[SG_MAX_CDB_SIZE];
  504. if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
  505. return -EINVAL;
  506. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  507. return -ENXIO;
  508. SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
  509. sdp->disk->disk_name, (int) count));
  510. if (sdp->detached)
  511. return -ENODEV;
  512. if (!((filp->f_flags & O_NONBLOCK) ||
  513. scsi_block_when_processing_errors(sdp->device)))
  514. return -ENXIO;
  515. if (!access_ok(VERIFY_READ, buf, count))
  516. return -EFAULT; /* protects following copy_from_user()s + get_user()s */
  517. if (count < SZ_SG_HEADER)
  518. return -EIO;
  519. if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
  520. return -EFAULT;
  521. blocking = !(filp->f_flags & O_NONBLOCK);
  522. if (old_hdr.reply_len < 0)
  523. return sg_new_write(sfp, filp, buf, count,
  524. blocking, 0, 0, NULL);
  525. if (count < (SZ_SG_HEADER + 6))
  526. return -EIO; /* The minimum scsi command length is 6 bytes. */
  527. if (!(srp = sg_add_request(sfp))) {
  528. SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
  529. return -EDOM;
  530. }
  531. buf += SZ_SG_HEADER;
  532. __get_user(opcode, buf);
  533. if (sfp->next_cmd_len > 0) {
  534. cmd_size = sfp->next_cmd_len;
  535. sfp->next_cmd_len = 0; /* reset so only this write() effected */
  536. } else {
  537. cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
  538. if ((opcode >= 0xc0) && old_hdr.twelve_byte)
  539. cmd_size = 12;
  540. }
  541. SCSI_LOG_TIMEOUT(4, printk(
  542. "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
  543. /* Determine buffer size. */
  544. input_size = count - cmd_size;
  545. mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
  546. mxsize -= SZ_SG_HEADER;
  547. input_size -= SZ_SG_HEADER;
  548. if (input_size < 0) {
  549. sg_remove_request(sfp, srp);
  550. return -EIO; /* User did not pass enough bytes for this command. */
  551. }
  552. hp = &srp->header;
  553. hp->interface_id = '\0'; /* indicator of old interface tunnelled */
  554. hp->cmd_len = (unsigned char) cmd_size;
  555. hp->iovec_count = 0;
  556. hp->mx_sb_len = 0;
  557. if (input_size > 0)
  558. hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
  559. SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
  560. else
  561. hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
  562. hp->dxfer_len = mxsize;
  563. if (hp->dxfer_direction == SG_DXFER_TO_DEV)
  564. hp->dxferp = (char __user *)buf + cmd_size;
  565. else
  566. hp->dxferp = NULL;
  567. hp->sbp = NULL;
  568. hp->timeout = old_hdr.reply_len; /* structure abuse ... */
  569. hp->flags = input_size; /* structure abuse ... */
  570. hp->pack_id = old_hdr.pack_id;
  571. hp->usr_ptr = NULL;
  572. if (__copy_from_user(cmnd, buf, cmd_size))
  573. return -EFAULT;
  574. /*
  575. * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
  576. * but is is possible that the app intended SG_DXFER_TO_DEV, because there
  577. * is a non-zero input_size, so emit a warning.
  578. */
  579. if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
  580. static char cmd[TASK_COMM_LEN];
  581. if (strcmp(current->comm, cmd)) {
  582. printk_ratelimited(KERN_WARNING
  583. "sg_write: data in/out %d/%d bytes "
  584. "for SCSI command 0x%x-- guessing "
  585. "data in;\n program %s not setting "
  586. "count and/or reply_len properly\n",
  587. old_hdr.reply_len - (int)SZ_SG_HEADER,
  588. input_size, (unsigned int) cmnd[0],
  589. current->comm);
  590. strcpy(cmd, current->comm);
  591. }
  592. }
  593. k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
  594. return (k < 0) ? k : count;
  595. }
  596. static ssize_t
  597. sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
  598. size_t count, int blocking, int read_only, int sg_io_owned,
  599. Sg_request **o_srp)
  600. {
  601. int k;
  602. Sg_request *srp;
  603. sg_io_hdr_t *hp;
  604. unsigned char cmnd[SG_MAX_CDB_SIZE];
  605. int timeout;
  606. unsigned long ul_timeout;
  607. if (count < SZ_SG_IO_HDR)
  608. return -EINVAL;
  609. if (!access_ok(VERIFY_READ, buf, count))
  610. return -EFAULT; /* protects following copy_from_user()s + get_user()s */
  611. sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
  612. if (!(srp = sg_add_request(sfp))) {
  613. SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
  614. return -EDOM;
  615. }
  616. srp->sg_io_owned = sg_io_owned;
  617. hp = &srp->header;
  618. if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
  619. sg_remove_request(sfp, srp);
  620. return -EFAULT;
  621. }
  622. if (hp->interface_id != 'S') {
  623. sg_remove_request(sfp, srp);
  624. return -ENOSYS;
  625. }
  626. if (hp->flags & SG_FLAG_MMAP_IO) {
  627. if (hp->dxfer_len > sfp->reserve.bufflen) {
  628. sg_remove_request(sfp, srp);
  629. return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
  630. }
  631. if (hp->flags & SG_FLAG_DIRECT_IO) {
  632. sg_remove_request(sfp, srp);
  633. return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
  634. }
  635. if (sg_res_in_use(sfp)) {
  636. sg_remove_request(sfp, srp);
  637. return -EBUSY; /* reserve buffer already being used */
  638. }
  639. }
  640. ul_timeout = msecs_to_jiffies(srp->header.timeout);
  641. timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
  642. if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
  643. sg_remove_request(sfp, srp);
  644. return -EMSGSIZE;
  645. }
  646. if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
  647. sg_remove_request(sfp, srp);
  648. return -EFAULT; /* protects following copy_from_user()s + get_user()s */
  649. }
  650. if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
  651. sg_remove_request(sfp, srp);
  652. return -EFAULT;
  653. }
  654. if (read_only && sg_allow_access(file, cmnd)) {
  655. sg_remove_request(sfp, srp);
  656. return -EPERM;
  657. }
  658. k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
  659. if (k < 0)
  660. return k;
  661. if (o_srp)
  662. *o_srp = srp;
  663. return count;
  664. }
  665. static int
  666. sg_common_write(Sg_fd * sfp, Sg_request * srp,
  667. unsigned char *cmnd, int timeout, int blocking)
  668. {
  669. int k, data_dir;
  670. Sg_device *sdp = sfp->parentdp;
  671. sg_io_hdr_t *hp = &srp->header;
  672. srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
  673. hp->status = 0;
  674. hp->masked_status = 0;
  675. hp->msg_status = 0;
  676. hp->info = 0;
  677. hp->host_status = 0;
  678. hp->driver_status = 0;
  679. hp->resid = 0;
  680. SCSI_LOG_TIMEOUT(4, printk("sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
  681. (int) cmnd[0], (int) hp->cmd_len));
  682. k = sg_start_req(srp, cmnd);
  683. if (k) {
  684. SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
  685. sg_finish_rem_req(srp);
  686. return k; /* probably out of space --> ENOMEM */
  687. }
  688. if (sdp->detached) {
  689. if (srp->bio) {
  690. if (srp->rq->cmd != srp->rq->__cmd)
  691. kfree(srp->rq->cmd);
  692. blk_end_request_all(srp->rq, -EIO);
  693. srp->rq = NULL;
  694. }
  695. sg_finish_rem_req(srp);
  696. return -ENODEV;
  697. }
  698. switch (hp->dxfer_direction) {
  699. case SG_DXFER_TO_FROM_DEV:
  700. case SG_DXFER_FROM_DEV:
  701. data_dir = DMA_FROM_DEVICE;
  702. break;
  703. case SG_DXFER_TO_DEV:
  704. data_dir = DMA_TO_DEVICE;
  705. break;
  706. case SG_DXFER_UNKNOWN:
  707. data_dir = DMA_BIDIRECTIONAL;
  708. break;
  709. default:
  710. data_dir = DMA_NONE;
  711. break;
  712. }
  713. hp->duration = jiffies_to_msecs(jiffies);
  714. srp->rq->timeout = timeout;
  715. kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
  716. blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
  717. srp->rq, 1, sg_rq_end_io);
  718. return 0;
  719. }
  720. static int
  721. sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
  722. {
  723. void __user *p = (void __user *)arg;
  724. int __user *ip = p;
  725. int result, val, read_only;
  726. Sg_device *sdp;
  727. Sg_fd *sfp;
  728. Sg_request *srp;
  729. unsigned long iflags;
  730. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  731. return -ENXIO;
  732. SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
  733. sdp->disk->disk_name, (int) cmd_in));
  734. read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
  735. switch (cmd_in) {
  736. case SG_IO:
  737. {
  738. int blocking = 1; /* ignore O_NONBLOCK flag */
  739. if (sdp->detached)
  740. return -ENODEV;
  741. if (!scsi_block_when_processing_errors(sdp->device))
  742. return -ENXIO;
  743. if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
  744. return -EFAULT;
  745. mutex_lock(&sfp->parentdp->open_rel_lock);
  746. result =
  747. sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
  748. blocking, read_only, 1, &srp);
  749. mutex_unlock(&sfp->parentdp->open_rel_lock);
  750. if (result < 0)
  751. return result;
  752. while (1) {
  753. result = 0; /* following macro to beat race condition */
  754. __wait_event_interruptible(sfp->read_wait,
  755. (srp->done || sdp->detached),
  756. result);
  757. if (sdp->detached)
  758. return -ENODEV;
  759. write_lock_irq(&sfp->rq_list_lock);
  760. if (srp->done) {
  761. srp->done = 2;
  762. write_unlock_irq(&sfp->rq_list_lock);
  763. break;
  764. }
  765. srp->orphan = 1;
  766. write_unlock_irq(&sfp->rq_list_lock);
  767. return result; /* -ERESTARTSYS because signal hit process */
  768. }
  769. result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
  770. return (result < 0) ? result : 0;
  771. }
  772. case SG_SET_TIMEOUT:
  773. result = get_user(val, ip);
  774. if (result)
  775. return result;
  776. if (val < 0)
  777. return -EIO;
  778. if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
  779. val = MULDIV (INT_MAX, USER_HZ, HZ);
  780. sfp->timeout_user = val;
  781. sfp->timeout = MULDIV (val, HZ, USER_HZ);
  782. return 0;
  783. case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
  784. /* strange ..., for backward compatibility */
  785. return sfp->timeout_user;
  786. case SG_SET_FORCE_LOW_DMA:
  787. result = get_user(val, ip);
  788. if (result)
  789. return result;
  790. if (val) {
  791. sfp->low_dma = 1;
  792. if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
  793. val = (int) sfp->reserve.bufflen;
  794. mutex_lock(&sfp->parentdp->open_rel_lock);
  795. sg_remove_scat(&sfp->reserve);
  796. sg_build_reserve(sfp, val);
  797. mutex_unlock(&sfp->parentdp->open_rel_lock);
  798. }
  799. } else {
  800. if (sdp->detached)
  801. return -ENODEV;
  802. sfp->low_dma = sdp->device->host->unchecked_isa_dma;
  803. }
  804. return 0;
  805. case SG_GET_LOW_DMA:
  806. return put_user((int) sfp->low_dma, ip);
  807. case SG_GET_SCSI_ID:
  808. if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
  809. return -EFAULT;
  810. else {
  811. sg_scsi_id_t __user *sg_idp = p;
  812. if (sdp->detached)
  813. return -ENODEV;
  814. __put_user((int) sdp->device->host->host_no,
  815. &sg_idp->host_no);
  816. __put_user((int) sdp->device->channel,
  817. &sg_idp->channel);
  818. __put_user((int) sdp->device->id, &sg_idp->scsi_id);
  819. __put_user((int) sdp->device->lun, &sg_idp->lun);
  820. __put_user((int) sdp->device->type, &sg_idp->scsi_type);
  821. __put_user((short) sdp->device->host->cmd_per_lun,
  822. &sg_idp->h_cmd_per_lun);
  823. __put_user((short) sdp->device->queue_depth,
  824. &sg_idp->d_queue_depth);
  825. __put_user(0, &sg_idp->unused[0]);
  826. __put_user(0, &sg_idp->unused[1]);
  827. return 0;
  828. }
  829. case SG_SET_FORCE_PACK_ID:
  830. result = get_user(val, ip);
  831. if (result)
  832. return result;
  833. sfp->force_packid = val ? 1 : 0;
  834. return 0;
  835. case SG_GET_PACK_ID:
  836. if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
  837. return -EFAULT;
  838. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  839. for (srp = sfp->headrp; srp; srp = srp->nextrp) {
  840. if ((1 == srp->done) && (!srp->sg_io_owned)) {
  841. read_unlock_irqrestore(&sfp->rq_list_lock,
  842. iflags);
  843. __put_user(srp->header.pack_id, ip);
  844. return 0;
  845. }
  846. }
  847. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  848. __put_user(-1, ip);
  849. return 0;
  850. case SG_GET_NUM_WAITING:
  851. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  852. for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
  853. if ((1 == srp->done) && (!srp->sg_io_owned))
  854. ++val;
  855. }
  856. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  857. return put_user(val, ip);
  858. case SG_GET_SG_TABLESIZE:
  859. return put_user(sdp->sg_tablesize, ip);
  860. case SG_SET_RESERVED_SIZE:
  861. result = get_user(val, ip);
  862. if (result)
  863. return result;
  864. if (val < 0)
  865. return -EINVAL;
  866. val = min_t(int, val,
  867. queue_max_sectors(sdp->device->request_queue) * 512);
  868. if (val != sfp->reserve.bufflen) {
  869. if (sg_res_in_use(sfp) || sfp->mmap_called)
  870. return -EBUSY;
  871. mutex_lock(&sfp->parentdp->open_rel_lock);
  872. sg_remove_scat(&sfp->reserve);
  873. sg_build_reserve(sfp, val);
  874. mutex_unlock(&sfp->parentdp->open_rel_lock);
  875. }
  876. return 0;
  877. case SG_GET_RESERVED_SIZE:
  878. val = min_t(int, sfp->reserve.bufflen,
  879. queue_max_sectors(sdp->device->request_queue) * 512);
  880. return put_user(val, ip);
  881. case SG_SET_COMMAND_Q:
  882. result = get_user(val, ip);
  883. if (result)
  884. return result;
  885. sfp->cmd_q = val ? 1 : 0;
  886. return 0;
  887. case SG_GET_COMMAND_Q:
  888. return put_user((int) sfp->cmd_q, ip);
  889. case SG_SET_KEEP_ORPHAN:
  890. result = get_user(val, ip);
  891. if (result)
  892. return result;
  893. sfp->keep_orphan = val;
  894. return 0;
  895. case SG_GET_KEEP_ORPHAN:
  896. return put_user((int) sfp->keep_orphan, ip);
  897. case SG_NEXT_CMD_LEN:
  898. result = get_user(val, ip);
  899. if (result)
  900. return result;
  901. if (val > SG_MAX_CDB_SIZE)
  902. return -ENOMEM;
  903. sfp->next_cmd_len = (val > 0) ? val : 0;
  904. return 0;
  905. case SG_GET_VERSION_NUM:
  906. return put_user(sg_version_num, ip);
  907. case SG_GET_ACCESS_COUNT:
  908. /* faked - we don't have a real access count anymore */
  909. val = (sdp->device ? 1 : 0);
  910. return put_user(val, ip);
  911. case SG_GET_REQUEST_TABLE:
  912. if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
  913. return -EFAULT;
  914. else {
  915. sg_req_info_t *rinfo;
  916. unsigned int ms;
  917. rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
  918. GFP_KERNEL);
  919. if (!rinfo)
  920. return -ENOMEM;
  921. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  922. for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
  923. ++val, srp = srp ? srp->nextrp : srp) {
  924. memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
  925. if (srp) {
  926. rinfo[val].req_state = srp->done + 1;
  927. rinfo[val].problem =
  928. srp->header.masked_status &
  929. srp->header.host_status &
  930. srp->header.driver_status;
  931. if (srp->done)
  932. rinfo[val].duration =
  933. srp->header.duration;
  934. else {
  935. ms = jiffies_to_msecs(jiffies);
  936. rinfo[val].duration =
  937. (ms > srp->header.duration) ?
  938. (ms - srp->header.duration) : 0;
  939. }
  940. rinfo[val].orphan = srp->orphan;
  941. rinfo[val].sg_io_owned =
  942. srp->sg_io_owned;
  943. rinfo[val].pack_id =
  944. srp->header.pack_id;
  945. rinfo[val].usr_ptr =
  946. srp->header.usr_ptr;
  947. }
  948. }
  949. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  950. result = __copy_to_user(p, rinfo,
  951. SZ_SG_REQ_INFO * SG_MAX_QUEUE);
  952. result = result ? -EFAULT : 0;
  953. kfree(rinfo);
  954. return result;
  955. }
  956. case SG_EMULATED_HOST:
  957. if (sdp->detached)
  958. return -ENODEV;
  959. return put_user(sdp->device->host->hostt->emulated, ip);
  960. case SG_SCSI_RESET:
  961. if (sdp->detached)
  962. return -ENODEV;
  963. if (filp->f_flags & O_NONBLOCK) {
  964. if (scsi_host_in_recovery(sdp->device->host))
  965. return -EBUSY;
  966. } else if (!scsi_block_when_processing_errors(sdp->device))
  967. return -EBUSY;
  968. result = get_user(val, ip);
  969. if (result)
  970. return result;
  971. if (SG_SCSI_RESET_NOTHING == val)
  972. return 0;
  973. switch (val) {
  974. case SG_SCSI_RESET_DEVICE:
  975. val = SCSI_TRY_RESET_DEVICE;
  976. break;
  977. case SG_SCSI_RESET_TARGET:
  978. val = SCSI_TRY_RESET_TARGET;
  979. break;
  980. case SG_SCSI_RESET_BUS:
  981. val = SCSI_TRY_RESET_BUS;
  982. break;
  983. case SG_SCSI_RESET_HOST:
  984. val = SCSI_TRY_RESET_HOST;
  985. break;
  986. default:
  987. return -EINVAL;
  988. }
  989. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  990. return -EACCES;
  991. return (scsi_reset_provider(sdp->device, val) ==
  992. SUCCESS) ? 0 : -EIO;
  993. case SCSI_IOCTL_SEND_COMMAND:
  994. if (sdp->detached)
  995. return -ENODEV;
  996. if (read_only) {
  997. unsigned char opcode = WRITE_6;
  998. Scsi_Ioctl_Command __user *siocp = p;
  999. if (copy_from_user(&opcode, siocp->data, 1))
  1000. return -EFAULT;
  1001. if (sg_allow_access(filp, &opcode))
  1002. return -EPERM;
  1003. }
  1004. return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
  1005. case SG_SET_DEBUG:
  1006. result = get_user(val, ip);
  1007. if (result)
  1008. return result;
  1009. sdp->sgdebug = (char) val;
  1010. return 0;
  1011. case SCSI_IOCTL_GET_IDLUN:
  1012. case SCSI_IOCTL_GET_BUS_NUMBER:
  1013. case SCSI_IOCTL_PROBE_HOST:
  1014. case SG_GET_TRANSFORM:
  1015. if (sdp->detached)
  1016. return -ENODEV;
  1017. return scsi_ioctl(sdp->device, cmd_in, p);
  1018. case BLKSECTGET:
  1019. return put_user(queue_max_sectors(sdp->device->request_queue) * 512,
  1020. ip);
  1021. case BLKTRACESETUP:
  1022. return blk_trace_setup(sdp->device->request_queue,
  1023. sdp->disk->disk_name,
  1024. MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
  1025. NULL,
  1026. (char *)arg);
  1027. case BLKTRACESTART:
  1028. return blk_trace_startstop(sdp->device->request_queue, 1);
  1029. case BLKTRACESTOP:
  1030. return blk_trace_startstop(sdp->device->request_queue, 0);
  1031. case BLKTRACETEARDOWN:
  1032. return blk_trace_remove(sdp->device->request_queue);
  1033. default:
  1034. if (read_only)
  1035. return -EPERM; /* don't know so take safe approach */
  1036. return scsi_ioctl(sdp->device, cmd_in, p);
  1037. }
  1038. }
  1039. static long
  1040. sg_unlocked_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
  1041. {
  1042. int ret;
  1043. mutex_lock(&sg_mutex);
  1044. ret = sg_ioctl(filp, cmd_in, arg);
  1045. mutex_unlock(&sg_mutex);
  1046. return ret;
  1047. }
  1048. #ifdef CONFIG_COMPAT
  1049. static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
  1050. {
  1051. Sg_device *sdp;
  1052. Sg_fd *sfp;
  1053. struct scsi_device *sdev;
  1054. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  1055. return -ENXIO;
  1056. sdev = sdp->device;
  1057. if (sdev->host->hostt->compat_ioctl) {
  1058. int ret;
  1059. ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
  1060. return ret;
  1061. }
  1062. return -ENOIOCTLCMD;
  1063. }
  1064. #endif
  1065. static unsigned int
  1066. sg_poll(struct file *filp, poll_table * wait)
  1067. {
  1068. unsigned int res = 0;
  1069. Sg_device *sdp;
  1070. Sg_fd *sfp;
  1071. Sg_request *srp;
  1072. int count = 0;
  1073. unsigned long iflags;
  1074. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
  1075. || sfp->closed)
  1076. return POLLERR;
  1077. poll_wait(filp, &sfp->read_wait, wait);
  1078. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  1079. for (srp = sfp->headrp; srp; srp = srp->nextrp) {
  1080. /* if any read waiting, flag it */
  1081. if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
  1082. res = POLLIN | POLLRDNORM;
  1083. ++count;
  1084. }
  1085. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1086. if (sdp->detached)
  1087. res |= POLLHUP;
  1088. else if (!sfp->cmd_q) {
  1089. if (0 == count)
  1090. res |= POLLOUT | POLLWRNORM;
  1091. } else if (count < SG_MAX_QUEUE)
  1092. res |= POLLOUT | POLLWRNORM;
  1093. SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
  1094. sdp->disk->disk_name, (int) res));
  1095. return res;
  1096. }
  1097. static int
  1098. sg_fasync(int fd, struct file *filp, int mode)
  1099. {
  1100. Sg_device *sdp;
  1101. Sg_fd *sfp;
  1102. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  1103. return -ENXIO;
  1104. SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
  1105. sdp->disk->disk_name, mode));
  1106. return fasync_helper(fd, filp, mode, &sfp->async_qp);
  1107. }
  1108. static int
  1109. sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1110. {
  1111. Sg_fd *sfp;
  1112. unsigned long offset, len, sa;
  1113. Sg_scatter_hold *rsv_schp;
  1114. int k, length;
  1115. if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
  1116. return VM_FAULT_SIGBUS;
  1117. rsv_schp = &sfp->reserve;
  1118. offset = vmf->pgoff << PAGE_SHIFT;
  1119. if (offset >= rsv_schp->bufflen)
  1120. return VM_FAULT_SIGBUS;
  1121. SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
  1122. offset, rsv_schp->k_use_sg));
  1123. sa = vma->vm_start;
  1124. length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
  1125. for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
  1126. len = vma->vm_end - sa;
  1127. len = (len < length) ? len : length;
  1128. if (offset < len) {
  1129. struct page *page = nth_page(rsv_schp->pages[k],
  1130. offset >> PAGE_SHIFT);
  1131. get_page(page); /* increment page count */
  1132. vmf->page = page;
  1133. return 0; /* success */
  1134. }
  1135. sa += len;
  1136. offset -= len;
  1137. }
  1138. return VM_FAULT_SIGBUS;
  1139. }
  1140. static const struct vm_operations_struct sg_mmap_vm_ops = {
  1141. .fault = sg_vma_fault,
  1142. };
  1143. static int
  1144. sg_mmap(struct file *filp, struct vm_area_struct *vma)
  1145. {
  1146. Sg_fd *sfp;
  1147. unsigned long req_sz, len, sa;
  1148. Sg_scatter_hold *rsv_schp;
  1149. int k, length;
  1150. if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
  1151. return -ENXIO;
  1152. req_sz = vma->vm_end - vma->vm_start;
  1153. SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
  1154. (void *) vma->vm_start, (int) req_sz));
  1155. if (vma->vm_pgoff)
  1156. return -EINVAL; /* want no offset */
  1157. rsv_schp = &sfp->reserve;
  1158. if (req_sz > rsv_schp->bufflen)
  1159. return -ENOMEM; /* cannot map more than reserved buffer */
  1160. sa = vma->vm_start;
  1161. length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
  1162. for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
  1163. len = vma->vm_end - sa;
  1164. len = (len < length) ? len : length;
  1165. sa += len;
  1166. }
  1167. sfp->mmap_called = 1;
  1168. vma->vm_flags |= VM_RESERVED;
  1169. vma->vm_private_data = sfp;
  1170. vma->vm_ops = &sg_mmap_vm_ops;
  1171. return 0;
  1172. }
  1173. static void sg_rq_end_io_usercontext(struct work_struct *work)
  1174. {
  1175. struct sg_request *srp = container_of(work, struct sg_request, ew.work);
  1176. struct sg_fd *sfp = srp->parentfp;
  1177. sg_finish_rem_req(srp);
  1178. kref_put(&sfp->f_ref, sg_remove_sfp);
  1179. }
  1180. /*
  1181. * This function is a "bottom half" handler that is called by the mid
  1182. * level when a command is completed (or has failed).
  1183. */
  1184. static void sg_rq_end_io(struct request *rq, int uptodate)
  1185. {
  1186. struct sg_request *srp = rq->end_io_data;
  1187. Sg_device *sdp;
  1188. Sg_fd *sfp;
  1189. unsigned long iflags;
  1190. unsigned int ms;
  1191. char *sense;
  1192. int result, resid, done = 1;
  1193. if (WARN_ON(srp->done != 0))
  1194. return;
  1195. sfp = srp->parentfp;
  1196. if (WARN_ON(sfp == NULL))
  1197. return;
  1198. sdp = sfp->parentdp;
  1199. if (unlikely(sdp->detached))
  1200. printk(KERN_INFO "sg_rq_end_io: device detached\n");
  1201. sense = rq->sense;
  1202. result = rq->errors;
  1203. resid = rq->resid_len;
  1204. SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
  1205. sdp->disk->disk_name, srp->header.pack_id, result));
  1206. srp->header.resid = resid;
  1207. ms = jiffies_to_msecs(jiffies);
  1208. srp->header.duration = (ms > srp->header.duration) ?
  1209. (ms - srp->header.duration) : 0;
  1210. if (0 != result) {
  1211. struct scsi_sense_hdr sshdr;
  1212. srp->header.status = 0xff & result;
  1213. srp->header.masked_status = status_byte(result);
  1214. srp->header.msg_status = msg_byte(result);
  1215. srp->header.host_status = host_byte(result);
  1216. srp->header.driver_status = driver_byte(result);
  1217. if ((sdp->sgdebug > 0) &&
  1218. ((CHECK_CONDITION == srp->header.masked_status) ||
  1219. (COMMAND_TERMINATED == srp->header.masked_status)))
  1220. __scsi_print_sense("sg_cmd_done", sense,
  1221. SCSI_SENSE_BUFFERSIZE);
  1222. /* Following if statement is a patch supplied by Eric Youngdale */
  1223. if (driver_byte(result) != 0
  1224. && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
  1225. && !scsi_sense_is_deferred(&sshdr)
  1226. && sshdr.sense_key == UNIT_ATTENTION
  1227. && sdp->device->removable) {
  1228. /* Detected possible disc change. Set the bit - this */
  1229. /* may be used if there are filesystems using this device */
  1230. sdp->device->changed = 1;
  1231. }
  1232. }
  1233. /* Rely on write phase to clean out srp status values, so no "else" */
  1234. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1235. if (unlikely(srp->orphan)) {
  1236. if (sfp->keep_orphan)
  1237. srp->sg_io_owned = 0;
  1238. else
  1239. done = 0;
  1240. }
  1241. srp->done = done;
  1242. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1243. if (likely(done)) {
  1244. /* Now wake up any sg_read() that is waiting for this
  1245. * packet.
  1246. */
  1247. wake_up_interruptible(&sfp->read_wait);
  1248. kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
  1249. kref_put(&sfp->f_ref, sg_remove_sfp);
  1250. } else {
  1251. INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
  1252. schedule_work(&srp->ew.work);
  1253. }
  1254. }
  1255. static const struct file_operations sg_fops = {
  1256. .owner = THIS_MODULE,
  1257. .read = sg_read,
  1258. .write = sg_write,
  1259. .poll = sg_poll,
  1260. .unlocked_ioctl = sg_unlocked_ioctl,
  1261. #ifdef CONFIG_COMPAT
  1262. .compat_ioctl = sg_compat_ioctl,
  1263. #endif
  1264. .open = sg_open,
  1265. .mmap = sg_mmap,
  1266. .release = sg_release,
  1267. .fasync = sg_fasync,
  1268. .llseek = no_llseek,
  1269. };
  1270. static struct class *sg_sysfs_class;
  1271. static int sg_sysfs_valid = 0;
  1272. static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
  1273. {
  1274. struct request_queue *q = scsidp->request_queue;
  1275. Sg_device *sdp;
  1276. unsigned long iflags;
  1277. int error;
  1278. u32 k;
  1279. sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
  1280. if (!sdp) {
  1281. printk(KERN_WARNING "kmalloc Sg_device failure\n");
  1282. return ERR_PTR(-ENOMEM);
  1283. }
  1284. if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
  1285. printk(KERN_WARNING "idr expansion Sg_device failure\n");
  1286. error = -ENOMEM;
  1287. goto out;
  1288. }
  1289. write_lock_irqsave(&sg_index_lock, iflags);
  1290. error = idr_get_new(&sg_index_idr, sdp, &k);
  1291. if (error) {
  1292. write_unlock_irqrestore(&sg_index_lock, iflags);
  1293. printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
  1294. error);
  1295. goto out;
  1296. }
  1297. if (unlikely(k >= SG_MAX_DEVS))
  1298. goto overflow;
  1299. SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
  1300. sprintf(disk->disk_name, "sg%d", k);
  1301. disk->first_minor = k;
  1302. sdp->disk = disk;
  1303. sdp->device = scsidp;
  1304. INIT_LIST_HEAD(&sdp->sfds);
  1305. init_waitqueue_head(&sdp->o_excl_wait);
  1306. sdp->sg_tablesize = queue_max_segments(q);
  1307. sdp->index = k;
  1308. kref_init(&sdp->d_ref);
  1309. mutex_init(&sdp->open_rel_lock);
  1310. write_unlock_irqrestore(&sg_index_lock, iflags);
  1311. error = 0;
  1312. out:
  1313. if (error) {
  1314. kfree(sdp);
  1315. return ERR_PTR(error);
  1316. }
  1317. return sdp;
  1318. overflow:
  1319. idr_remove(&sg_index_idr, k);
  1320. write_unlock_irqrestore(&sg_index_lock, iflags);
  1321. sdev_printk(KERN_WARNING, scsidp,
  1322. "Unable to attach sg device type=%d, minor "
  1323. "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
  1324. error = -ENODEV;
  1325. goto out;
  1326. }
  1327. static int
  1328. sg_add(struct device *cl_dev, struct class_interface *cl_intf)
  1329. {
  1330. struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
  1331. struct gendisk *disk;
  1332. Sg_device *sdp = NULL;
  1333. struct cdev * cdev = NULL;
  1334. int error;
  1335. unsigned long iflags;
  1336. disk = alloc_disk(1);
  1337. if (!disk) {
  1338. printk(KERN_WARNING "alloc_disk failed\n");
  1339. return -ENOMEM;
  1340. }
  1341. disk->major = SCSI_GENERIC_MAJOR;
  1342. error = -ENOMEM;
  1343. cdev = cdev_alloc();
  1344. if (!cdev) {
  1345. printk(KERN_WARNING "cdev_alloc failed\n");
  1346. goto out;
  1347. }
  1348. cdev->owner = THIS_MODULE;
  1349. cdev->ops = &sg_fops;
  1350. sdp = sg_alloc(disk, scsidp);
  1351. if (IS_ERR(sdp)) {
  1352. printk(KERN_WARNING "sg_alloc failed\n");
  1353. error = PTR_ERR(sdp);
  1354. goto out;
  1355. }
  1356. error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
  1357. if (error)
  1358. goto cdev_add_err;
  1359. sdp->cdev = cdev;
  1360. if (sg_sysfs_valid) {
  1361. struct device *sg_class_member;
  1362. sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
  1363. MKDEV(SCSI_GENERIC_MAJOR,
  1364. sdp->index),
  1365. sdp, "%s", disk->disk_name);
  1366. if (IS_ERR(sg_class_member)) {
  1367. printk(KERN_ERR "sg_add: "
  1368. "device_create failed\n");
  1369. error = PTR_ERR(sg_class_member);
  1370. goto cdev_add_err;
  1371. }
  1372. error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
  1373. &sg_class_member->kobj, "generic");
  1374. if (error)
  1375. printk(KERN_ERR "sg_add: unable to make symlink "
  1376. "'generic' back to sg%d\n", sdp->index);
  1377. } else
  1378. printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
  1379. sdev_printk(KERN_NOTICE, scsidp,
  1380. "Attached scsi generic sg%d type %d\n", sdp->index,
  1381. scsidp->type);
  1382. dev_set_drvdata(cl_dev, sdp);
  1383. return 0;
  1384. cdev_add_err:
  1385. write_lock_irqsave(&sg_index_lock, iflags);
  1386. idr_remove(&sg_index_idr, sdp->index);
  1387. write_unlock_irqrestore(&sg_index_lock, iflags);
  1388. kfree(sdp);
  1389. out:
  1390. put_disk(disk);
  1391. if (cdev)
  1392. cdev_del(cdev);
  1393. return error;
  1394. }
  1395. static void sg_device_destroy(struct kref *kref)
  1396. {
  1397. struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
  1398. unsigned long flags;
  1399. /* CAUTION! Note that the device can still be found via idr_find()
  1400. * even though the refcount is 0. Therefore, do idr_remove() BEFORE
  1401. * any other cleanup.
  1402. */
  1403. write_lock_irqsave(&sg_index_lock, flags);
  1404. idr_remove(&sg_index_idr, sdp->index);
  1405. write_unlock_irqrestore(&sg_index_lock, flags);
  1406. SCSI_LOG_TIMEOUT(3,
  1407. printk("sg_device_destroy: %s\n",
  1408. sdp->disk->disk_name));
  1409. put_disk(sdp->disk);
  1410. kfree(sdp);
  1411. }
  1412. static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
  1413. {
  1414. struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
  1415. Sg_device *sdp = dev_get_drvdata(cl_dev);
  1416. unsigned long iflags;
  1417. Sg_fd *sfp;
  1418. if (!sdp || sdp->detached)
  1419. return;
  1420. SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name));
  1421. /* Need a write lock to set sdp->detached. */
  1422. write_lock_irqsave(&sg_index_lock, iflags);
  1423. sdp->detached = 1;
  1424. list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
  1425. wake_up_interruptible(&sfp->read_wait);
  1426. kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
  1427. }
  1428. write_unlock_irqrestore(&sg_index_lock, iflags);
  1429. sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
  1430. device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
  1431. cdev_del(sdp->cdev);
  1432. sdp->cdev = NULL;
  1433. sg_put_dev(sdp);
  1434. }
  1435. module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
  1436. module_param_named(def_reserved_size, def_reserved_size, int,
  1437. S_IRUGO | S_IWUSR);
  1438. module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
  1439. MODULE_AUTHOR("Douglas Gilbert");
  1440. MODULE_DESCRIPTION("SCSI generic (sg) driver");
  1441. MODULE_LICENSE("GPL");
  1442. MODULE_VERSION(SG_VERSION_STR);
  1443. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
  1444. MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
  1445. "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
  1446. MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
  1447. MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
  1448. static int __init
  1449. init_sg(void)
  1450. {
  1451. int rc;
  1452. if (scatter_elem_sz < PAGE_SIZE) {
  1453. scatter_elem_sz = PAGE_SIZE;
  1454. scatter_elem_sz_prev = scatter_elem_sz;
  1455. }
  1456. if (def_reserved_size >= 0)
  1457. sg_big_buff = def_reserved_size;
  1458. else
  1459. def_reserved_size = sg_big_buff;
  1460. rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
  1461. SG_MAX_DEVS, "sg");
  1462. if (rc)
  1463. return rc;
  1464. sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
  1465. if ( IS_ERR(sg_sysfs_class) ) {
  1466. rc = PTR_ERR(sg_sysfs_class);
  1467. goto err_out;
  1468. }
  1469. sg_sysfs_valid = 1;
  1470. rc = scsi_register_interface(&sg_interface);
  1471. if (0 == rc) {
  1472. #ifdef CONFIG_SCSI_PROC_FS
  1473. sg_proc_init();
  1474. #endif /* CONFIG_SCSI_PROC_FS */
  1475. return 0;
  1476. }
  1477. class_destroy(sg_sysfs_class);
  1478. err_out:
  1479. unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
  1480. return rc;
  1481. }
  1482. static void __exit
  1483. exit_sg(void)
  1484. {
  1485. #ifdef CONFIG_SCSI_PROC_FS
  1486. sg_proc_cleanup();
  1487. #endif /* CONFIG_SCSI_PROC_FS */
  1488. scsi_unregister_interface(&sg_interface);
  1489. class_destroy(sg_sysfs_class);
  1490. sg_sysfs_valid = 0;
  1491. unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
  1492. SG_MAX_DEVS);
  1493. idr_destroy(&sg_index_idr);
  1494. }
  1495. static int sg_start_req(Sg_request *srp, unsigned char *cmd)
  1496. {
  1497. int res;
  1498. struct request *rq;
  1499. Sg_fd *sfp = srp->parentfp;
  1500. sg_io_hdr_t *hp = &srp->header;
  1501. int dxfer_len = (int) hp->dxfer_len;
  1502. int dxfer_dir = hp->dxfer_direction;
  1503. unsigned int iov_count = hp->iovec_count;
  1504. Sg_scatter_hold *req_schp = &srp->data;
  1505. Sg_scatter_hold *rsv_schp = &sfp->reserve;
  1506. struct request_queue *q = sfp->parentdp->device->request_queue;
  1507. struct rq_map_data *md, map_data;
  1508. int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
  1509. unsigned char *long_cmdp = NULL;
  1510. SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
  1511. dxfer_len));
  1512. if (hp->cmd_len > BLK_MAX_CDB) {
  1513. long_cmdp = kzalloc(hp->cmd_len, GFP_KERNEL);
  1514. if (!long_cmdp)
  1515. return -ENOMEM;
  1516. }
  1517. rq = blk_get_request(q, rw, GFP_ATOMIC);
  1518. if (!rq) {
  1519. kfree(long_cmdp);
  1520. return -ENOMEM;
  1521. }
  1522. blk_rq_set_block_pc(rq);
  1523. if (hp->cmd_len > BLK_MAX_CDB)
  1524. rq->cmd = long_cmdp;
  1525. memcpy(rq->cmd, cmd, hp->cmd_len);
  1526. rq->cmd_len = hp->cmd_len;
  1527. srp->rq = rq;
  1528. rq->end_io_data = srp;
  1529. rq->sense = srp->sense_b;
  1530. rq->retries = SG_DEFAULT_RETRIES;
  1531. if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
  1532. return 0;
  1533. if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
  1534. dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
  1535. !sfp->parentdp->device->host->unchecked_isa_dma &&
  1536. blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
  1537. md = NULL;
  1538. else
  1539. md = &map_data;
  1540. if (md) {
  1541. if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
  1542. sg_link_reserve(sfp, srp, dxfer_len);
  1543. else {
  1544. res = sg_build_indirect(req_schp, sfp, dxfer_len);
  1545. if (res)
  1546. return res;
  1547. }
  1548. md->pages = req_schp->pages;
  1549. md->page_order = req_schp->page_order;
  1550. md->nr_entries = req_schp->k_use_sg;
  1551. md->offset = 0;
  1552. md->null_mapped = hp->dxferp ? 0 : 1;
  1553. if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
  1554. md->from_user = 1;
  1555. else
  1556. md->from_user = 0;
  1557. }
  1558. if (unlikely(iov_count > UIO_MAXIOV))
  1559. return -EINVAL;
  1560. if (iov_count) {
  1561. int len, size = sizeof(struct sg_iovec) * iov_count;
  1562. struct iovec *iov;
  1563. iov = memdup_user(hp->dxferp, size);
  1564. if (IS_ERR(iov))
  1565. return PTR_ERR(iov);
  1566. len = iov_length(iov, iov_count);
  1567. if (hp->dxfer_len < len) {
  1568. iov_count = iov_shorten(iov, iov_count, hp->dxfer_len);
  1569. len = hp->dxfer_len;
  1570. }
  1571. res = blk_rq_map_user_iov(q, rq, md, (struct sg_iovec *)iov,
  1572. iov_count,
  1573. len, GFP_ATOMIC);
  1574. kfree(iov);
  1575. } else
  1576. res = blk_rq_map_user(q, rq, md, hp->dxferp,
  1577. hp->dxfer_len, GFP_ATOMIC);
  1578. if (!res) {
  1579. srp->bio = rq->bio;
  1580. if (!md) {
  1581. req_schp->dio_in_use = 1;
  1582. hp->info |= SG_INFO_DIRECT_IO;
  1583. }
  1584. }
  1585. return res;
  1586. }
  1587. static int sg_finish_rem_req(Sg_request * srp)
  1588. {
  1589. int ret = 0;
  1590. Sg_fd *sfp = srp->parentfp;
  1591. Sg_scatter_hold *req_schp = &srp->data;
  1592. SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
  1593. if (srp->rq) {
  1594. if (srp->bio)
  1595. ret = blk_rq_unmap_user(srp->bio);
  1596. if (srp->rq->cmd != srp->rq->__cmd)
  1597. kfree(srp->rq->cmd);
  1598. blk_put_request(srp->rq);
  1599. }
  1600. if (srp->res_used)
  1601. sg_unlink_reserve(sfp, srp);
  1602. else
  1603. sg_remove_scat(req_schp);
  1604. sg_remove_request(sfp, srp);
  1605. return ret;
  1606. }
  1607. static int
  1608. sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
  1609. {
  1610. int sg_bufflen = tablesize * sizeof(struct page *);
  1611. gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
  1612. schp->pages = kzalloc(sg_bufflen, gfp_flags);
  1613. if (!schp->pages)
  1614. return -ENOMEM;
  1615. schp->sglist_len = sg_bufflen;
  1616. return tablesize; /* number of scat_gath elements allocated */
  1617. }
  1618. static int
  1619. sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
  1620. {
  1621. int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
  1622. int sg_tablesize = sfp->parentdp->sg_tablesize;
  1623. int blk_size = buff_size, order;
  1624. gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
  1625. if (blk_size < 0)
  1626. return -EFAULT;
  1627. if (0 == blk_size)
  1628. ++blk_size; /* don't know why */
  1629. /* round request up to next highest SG_SECTOR_SZ byte boundary */
  1630. blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
  1631. SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
  1632. buff_size, blk_size));
  1633. /* N.B. ret_sz carried into this block ... */
  1634. mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
  1635. if (mx_sc_elems < 0)
  1636. return mx_sc_elems; /* most likely -ENOMEM */
  1637. num = scatter_elem_sz;
  1638. if (unlikely(num != scatter_elem_sz_prev)) {
  1639. if (num < PAGE_SIZE) {
  1640. scatter_elem_sz = PAGE_SIZE;
  1641. scatter_elem_sz_prev = PAGE_SIZE;
  1642. } else
  1643. scatter_elem_sz_prev = num;
  1644. }
  1645. if (sfp->low_dma)
  1646. gfp_mask |= GFP_DMA;
  1647. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  1648. gfp_mask |= __GFP_ZERO;
  1649. order = get_order(num);
  1650. retry:
  1651. ret_sz = 1 << (PAGE_SHIFT + order);
  1652. for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
  1653. k++, rem_sz -= ret_sz) {
  1654. num = (rem_sz > scatter_elem_sz_prev) ?
  1655. scatter_elem_sz_prev : rem_sz;
  1656. schp->pages[k] = alloc_pages(gfp_mask | __GFP_ZERO, order);
  1657. if (!schp->pages[k])
  1658. goto out;
  1659. if (num == scatter_elem_sz_prev) {
  1660. if (unlikely(ret_sz > scatter_elem_sz_prev)) {
  1661. scatter_elem_sz = ret_sz;
  1662. scatter_elem_sz_prev = ret_sz;
  1663. }
  1664. }
  1665. SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
  1666. "ret_sz=%d\n", k, num, ret_sz));
  1667. } /* end of for loop */
  1668. schp->page_order = order;
  1669. schp->k_use_sg = k;
  1670. SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
  1671. "rem_sz=%d\n", k, rem_sz));
  1672. schp->bufflen = blk_size;
  1673. if (rem_sz > 0) /* must have failed */
  1674. return -ENOMEM;
  1675. return 0;
  1676. out:
  1677. for (i = 0; i < k; i++)
  1678. __free_pages(schp->pages[i], order);
  1679. if (--order >= 0)
  1680. goto retry;
  1681. return -ENOMEM;
  1682. }
  1683. static void
  1684. sg_remove_scat(Sg_scatter_hold * schp)
  1685. {
  1686. SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
  1687. if (schp->pages && schp->sglist_len > 0) {
  1688. if (!schp->dio_in_use) {
  1689. int k;
  1690. for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
  1691. SCSI_LOG_TIMEOUT(5, printk(
  1692. "sg_remove_scat: k=%d, pg=0x%p\n",
  1693. k, schp->pages[k]));
  1694. __free_pages(schp->pages[k], schp->page_order);
  1695. }
  1696. kfree(schp->pages);
  1697. }
  1698. }
  1699. memset(schp, 0, sizeof (*schp));
  1700. }
  1701. static int
  1702. sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
  1703. {
  1704. Sg_scatter_hold *schp = &srp->data;
  1705. int k, num;
  1706. SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
  1707. num_read_xfer));
  1708. if ((!outp) || (num_read_xfer <= 0))
  1709. return 0;
  1710. num = 1 << (PAGE_SHIFT + schp->page_order);
  1711. for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
  1712. if (num > num_read_xfer) {
  1713. if (__copy_to_user(outp, page_address(schp->pages[k]),
  1714. num_read_xfer))
  1715. return -EFAULT;
  1716. break;
  1717. } else {
  1718. if (__copy_to_user(outp, page_address(schp->pages[k]),
  1719. num))
  1720. return -EFAULT;
  1721. num_read_xfer -= num;
  1722. if (num_read_xfer <= 0)
  1723. break;
  1724. outp += num;
  1725. }
  1726. }
  1727. return 0;
  1728. }
  1729. static void
  1730. sg_build_reserve(Sg_fd * sfp, int req_size)
  1731. {
  1732. Sg_scatter_hold *schp = &sfp->reserve;
  1733. SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
  1734. do {
  1735. if (req_size < PAGE_SIZE)
  1736. req_size = PAGE_SIZE;
  1737. if (0 == sg_build_indirect(schp, sfp, req_size))
  1738. return;
  1739. else
  1740. sg_remove_scat(schp);
  1741. req_size >>= 1; /* divide by 2 */
  1742. } while (req_size > (PAGE_SIZE / 2));
  1743. }
  1744. static void
  1745. sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
  1746. {
  1747. Sg_scatter_hold *req_schp = &srp->data;
  1748. Sg_scatter_hold *rsv_schp = &sfp->reserve;
  1749. int k, num, rem;
  1750. srp->res_used = 1;
  1751. SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
  1752. rem = size;
  1753. num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
  1754. for (k = 0; k < rsv_schp->k_use_sg; k++) {
  1755. if (rem <= num) {
  1756. req_schp->k_use_sg = k + 1;
  1757. req_schp->sglist_len = rsv_schp->sglist_len;
  1758. req_schp->pages = rsv_schp->pages;
  1759. req_schp->bufflen = size;
  1760. req_schp->page_order = rsv_schp->page_order;
  1761. break;
  1762. } else
  1763. rem -= num;
  1764. }
  1765. if (k >= rsv_schp->k_use_sg)
  1766. SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
  1767. }
  1768. static void
  1769. sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
  1770. {
  1771. Sg_scatter_hold *req_schp = &srp->data;
  1772. SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
  1773. (int) req_schp->k_use_sg));
  1774. req_schp->k_use_sg = 0;
  1775. req_schp->bufflen = 0;
  1776. req_schp->pages = NULL;
  1777. req_schp->page_order = 0;
  1778. req_schp->sglist_len = 0;
  1779. sfp->save_scat_len = 0;
  1780. srp->res_used = 0;
  1781. }
  1782. static Sg_request *
  1783. sg_get_rq_mark(Sg_fd * sfp, int pack_id)
  1784. {
  1785. Sg_request *resp;
  1786. unsigned long iflags;
  1787. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1788. for (resp = sfp->headrp; resp; resp = resp->nextrp) {
  1789. /* look for requests that are ready + not SG_IO owned */
  1790. if ((1 == resp->done) && (!resp->sg_io_owned) &&
  1791. ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
  1792. resp->done = 2; /* guard against other readers */
  1793. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1794. return resp;
  1795. }
  1796. }
  1797. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1798. return NULL;
  1799. }
  1800. /* always adds to end of list */
  1801. static Sg_request *
  1802. sg_add_request(Sg_fd * sfp)
  1803. {
  1804. int k;
  1805. unsigned long iflags;
  1806. Sg_request *resp;
  1807. Sg_request *rp = sfp->req_arr;
  1808. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1809. resp = sfp->headrp;
  1810. if (!resp) {
  1811. memset(rp, 0, sizeof (Sg_request));
  1812. rp->parentfp = sfp;
  1813. resp = rp;
  1814. sfp->headrp = resp;
  1815. } else {
  1816. if (0 == sfp->cmd_q)
  1817. resp = NULL; /* command queuing disallowed */
  1818. else {
  1819. for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
  1820. if (!rp->parentfp)
  1821. break;
  1822. }
  1823. if (k < SG_MAX_QUEUE) {
  1824. memset(rp, 0, sizeof (Sg_request));
  1825. rp->parentfp = sfp;
  1826. while (resp->nextrp)
  1827. resp = resp->nextrp;
  1828. resp->nextrp = rp;
  1829. resp = rp;
  1830. } else
  1831. resp = NULL;
  1832. }
  1833. }
  1834. if (resp) {
  1835. resp->nextrp = NULL;
  1836. resp->header.duration = jiffies_to_msecs(jiffies);
  1837. }
  1838. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1839. return resp;
  1840. }
  1841. /* Return of 1 for found; 0 for not found */
  1842. static int
  1843. sg_remove_request(Sg_fd * sfp, Sg_request * srp)
  1844. {
  1845. Sg_request *prev_rp;
  1846. Sg_request *rp;
  1847. unsigned long iflags;
  1848. int res = 0;
  1849. if ((!sfp) || (!srp) || (!sfp->headrp))
  1850. return res;
  1851. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1852. prev_rp = sfp->headrp;
  1853. if (srp == prev_rp) {
  1854. sfp->headrp = prev_rp->nextrp;
  1855. prev_rp->parentfp = NULL;
  1856. res = 1;
  1857. } else {
  1858. while ((rp = prev_rp->nextrp)) {
  1859. if (srp == rp) {
  1860. prev_rp->nextrp = rp->nextrp;
  1861. rp->parentfp = NULL;
  1862. res = 1;
  1863. break;
  1864. }
  1865. prev_rp = rp;
  1866. }
  1867. }
  1868. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1869. return res;
  1870. }
  1871. static Sg_fd *
  1872. sg_add_sfp(Sg_device * sdp, int dev)
  1873. {
  1874. Sg_fd *sfp;
  1875. unsigned long iflags;
  1876. int bufflen;
  1877. sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
  1878. if (!sfp)
  1879. return NULL;
  1880. init_waitqueue_head(&sfp->read_wait);
  1881. rwlock_init(&sfp->rq_list_lock);
  1882. kref_init(&sfp->f_ref);
  1883. sfp->timeout = SG_DEFAULT_TIMEOUT;
  1884. sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
  1885. sfp->force_packid = SG_DEF_FORCE_PACK_ID;
  1886. sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
  1887. sdp->device->host->unchecked_isa_dma : 1;
  1888. sfp->cmd_q = SG_DEF_COMMAND_Q;
  1889. sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
  1890. sfp->parentdp = sdp;
  1891. write_lock_irqsave(&sg_index_lock, iflags);
  1892. list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
  1893. write_unlock_irqrestore(&sg_index_lock, iflags);
  1894. SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
  1895. if (unlikely(sg_big_buff != def_reserved_size))
  1896. sg_big_buff = def_reserved_size;
  1897. bufflen = min_t(int, sg_big_buff,
  1898. queue_max_sectors(sdp->device->request_queue) * 512);
  1899. sg_build_reserve(sfp, bufflen);
  1900. SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
  1901. sfp->reserve.bufflen, sfp->reserve.k_use_sg));
  1902. kref_get(&sdp->d_ref);
  1903. __module_get(THIS_MODULE);
  1904. return sfp;
  1905. }
  1906. static void sg_remove_sfp_usercontext(struct work_struct *work)
  1907. {
  1908. struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
  1909. struct sg_device *sdp = sfp->parentdp;
  1910. /* Cleanup any responses which were never read(). */
  1911. while (sfp->headrp)
  1912. sg_finish_rem_req(sfp->headrp);
  1913. if (sfp->reserve.bufflen > 0) {
  1914. SCSI_LOG_TIMEOUT(6,
  1915. printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
  1916. (int) sfp->reserve.bufflen,
  1917. (int) sfp->reserve.k_use_sg));
  1918. sg_remove_scat(&sfp->reserve);
  1919. }
  1920. SCSI_LOG_TIMEOUT(6,
  1921. printk("sg_remove_sfp: %s, sfp=0x%p\n",
  1922. sdp->disk->disk_name,
  1923. sfp));
  1924. kfree(sfp);
  1925. scsi_device_put(sdp->device);
  1926. sg_put_dev(sdp);
  1927. module_put(THIS_MODULE);
  1928. }
  1929. static void sg_remove_sfp(struct kref *kref)
  1930. {
  1931. struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
  1932. struct sg_device *sdp = sfp->parentdp;
  1933. unsigned long iflags;
  1934. write_lock_irqsave(&sg_index_lock, iflags);
  1935. list_del(&sfp->sfd_siblings);
  1936. write_unlock_irqrestore(&sg_index_lock, iflags);
  1937. wake_up_interruptible(&sdp->o_excl_wait);
  1938. INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
  1939. schedule_work(&sfp->ew.work);
  1940. }
  1941. static int
  1942. sg_res_in_use(Sg_fd * sfp)
  1943. {
  1944. const Sg_request *srp;
  1945. unsigned long iflags;
  1946. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  1947. for (srp = sfp->headrp; srp; srp = srp->nextrp)
  1948. if (srp->res_used)
  1949. break;
  1950. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1951. return srp ? 1 : 0;
  1952. }
  1953. #ifdef CONFIG_SCSI_PROC_FS
  1954. static int
  1955. sg_idr_max_id(int id, void *p, void *data)
  1956. {
  1957. int *k = data;
  1958. if (*k < id)
  1959. *k = id;
  1960. return 0;
  1961. }
  1962. static int
  1963. sg_last_dev(void)
  1964. {
  1965. int k = -1;
  1966. unsigned long iflags;
  1967. read_lock_irqsave(&sg_index_lock, iflags);
  1968. idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
  1969. read_unlock_irqrestore(&sg_index_lock, iflags);
  1970. return k + 1; /* origin 1 */
  1971. }
  1972. #endif
  1973. /* must be called with sg_index_lock held */
  1974. static Sg_device *sg_lookup_dev(int dev)
  1975. {
  1976. return idr_find(&sg_index_idr, dev);
  1977. }
  1978. static Sg_device *sg_get_dev(int dev)
  1979. {
  1980. struct sg_device *sdp;
  1981. unsigned long flags;
  1982. read_lock_irqsave(&sg_index_lock, flags);
  1983. sdp = sg_lookup_dev(dev);
  1984. if (!sdp)
  1985. sdp = ERR_PTR(-ENXIO);
  1986. else if (sdp->detached) {
  1987. /* If sdp->detached, then the refcount may already be 0, in
  1988. * which case it would be a bug to do kref_get().
  1989. */
  1990. sdp = ERR_PTR(-ENODEV);
  1991. } else
  1992. kref_get(&sdp->d_ref);
  1993. read_unlock_irqrestore(&sg_index_lock, flags);
  1994. return sdp;
  1995. }
  1996. static void sg_put_dev(struct sg_device *sdp)
  1997. {
  1998. kref_put(&sdp->d_ref, sg_device_destroy);
  1999. }
  2000. #ifdef CONFIG_SCSI_PROC_FS
  2001. static struct proc_dir_entry *sg_proc_sgp = NULL;
  2002. static char sg_proc_sg_dirname[] = "scsi/sg";
  2003. static int sg_proc_seq_show_int(struct seq_file *s, void *v);
  2004. static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
  2005. static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
  2006. size_t count, loff_t *off);
  2007. static const struct file_operations adio_fops = {
  2008. .owner = THIS_MODULE,
  2009. .open = sg_proc_single_open_adio,
  2010. .read = seq_read,
  2011. .llseek = seq_lseek,
  2012. .write = sg_proc_write_adio,
  2013. .release = single_release,
  2014. };
  2015. static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
  2016. static ssize_t sg_proc_write_dressz(struct file *filp,
  2017. const char __user *buffer, size_t count, loff_t *off);
  2018. static const struct file_operations dressz_fops = {
  2019. .owner = THIS_MODULE,
  2020. .open = sg_proc_single_open_dressz,
  2021. .read = seq_read,
  2022. .llseek = seq_lseek,
  2023. .write = sg_proc_write_dressz,
  2024. .release = single_release,
  2025. };
  2026. static int sg_proc_seq_show_version(struct seq_file *s, void *v);
  2027. static int sg_proc_single_open_version(struct inode *inode, struct file *file);
  2028. static const struct file_operations version_fops = {
  2029. .owner = THIS_MODULE,
  2030. .open = sg_proc_single_open_version,
  2031. .read = seq_read,
  2032. .llseek = seq_lseek,
  2033. .release = single_release,
  2034. };
  2035. static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
  2036. static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
  2037. static const struct file_operations devhdr_fops = {
  2038. .owner = THIS_MODULE,
  2039. .open = sg_proc_single_open_devhdr,
  2040. .read = seq_read,
  2041. .llseek = seq_lseek,
  2042. .release = single_release,
  2043. };
  2044. static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
  2045. static int sg_proc_open_dev(struct inode *inode, struct file *file);
  2046. static void * dev_seq_start(struct seq_file *s, loff_t *pos);
  2047. static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
  2048. static void dev_seq_stop(struct seq_file *s, void *v);
  2049. static const struct file_operations dev_fops = {
  2050. .owner = THIS_MODULE,
  2051. .open = sg_proc_open_dev,
  2052. .read = seq_read,
  2053. .llseek = seq_lseek,
  2054. .release = seq_release,
  2055. };
  2056. static const struct seq_operations dev_seq_ops = {
  2057. .start = dev_seq_start,
  2058. .next = dev_seq_next,
  2059. .stop = dev_seq_stop,
  2060. .show = sg_proc_seq_show_dev,
  2061. };
  2062. static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
  2063. static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
  2064. static const struct file_operations devstrs_fops = {
  2065. .owner = THIS_MODULE,
  2066. .open = sg_proc_open_devstrs,
  2067. .read = seq_read,
  2068. .llseek = seq_lseek,
  2069. .release = seq_release,
  2070. };
  2071. static const struct seq_operations devstrs_seq_ops = {
  2072. .start = dev_seq_start,
  2073. .next = dev_seq_next,
  2074. .stop = dev_seq_stop,
  2075. .show = sg_proc_seq_show_devstrs,
  2076. };
  2077. static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
  2078. static int sg_proc_open_debug(struct inode *inode, struct file *file);
  2079. static const struct file_operations debug_fops = {
  2080. .owner = THIS_MODULE,
  2081. .open = sg_proc_open_debug,
  2082. .read = seq_read,
  2083. .llseek = seq_lseek,
  2084. .release = seq_release,
  2085. };
  2086. static const struct seq_operations debug_seq_ops = {
  2087. .start = dev_seq_start,
  2088. .next = dev_seq_next,
  2089. .stop = dev_seq_stop,
  2090. .show = sg_proc_seq_show_debug,
  2091. };
  2092. struct sg_proc_leaf {
  2093. const char * name;
  2094. const struct file_operations * fops;
  2095. };
  2096. static struct sg_proc_leaf sg_proc_leaf_arr[] = {
  2097. {"allow_dio", &adio_fops},
  2098. {"debug", &debug_fops},
  2099. {"def_reserved_size", &dressz_fops},
  2100. {"device_hdr", &devhdr_fops},
  2101. {"devices", &dev_fops},
  2102. {"device_strs", &devstrs_fops},
  2103. {"version", &version_fops}
  2104. };
  2105. static int
  2106. sg_proc_init(void)
  2107. {
  2108. int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
  2109. int k;
  2110. sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
  2111. if (!sg_proc_sgp)
  2112. return 1;
  2113. for (k = 0; k < num_leaves; ++k) {
  2114. struct sg_proc_leaf *leaf = &sg_proc_leaf_arr[k];
  2115. umode_t mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
  2116. proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
  2117. }
  2118. return 0;
  2119. }
  2120. static void
  2121. sg_proc_cleanup(void)
  2122. {
  2123. int k;
  2124. int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
  2125. if (!sg_proc_sgp)
  2126. return;
  2127. for (k = 0; k < num_leaves; ++k)
  2128. remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
  2129. remove_proc_entry(sg_proc_sg_dirname, NULL);
  2130. }
  2131. static int sg_proc_seq_show_int(struct seq_file *s, void *v)
  2132. {
  2133. seq_printf(s, "%d\n", *((int *)s->private));
  2134. return 0;
  2135. }
  2136. static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
  2137. {
  2138. return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
  2139. }
  2140. static ssize_t
  2141. sg_proc_write_adio(struct file *filp, const char __user *buffer,
  2142. size_t count, loff_t *off)
  2143. {
  2144. int err;
  2145. unsigned long num;
  2146. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2147. return -EACCES;
  2148. err = kstrtoul_from_user(buffer, count, 0, &num);
  2149. if (err)
  2150. return err;
  2151. sg_allow_dio = num ? 1 : 0;
  2152. return count;
  2153. }
  2154. static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
  2155. {
  2156. return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
  2157. }
  2158. static ssize_t
  2159. sg_proc_write_dressz(struct file *filp, const char __user *buffer,
  2160. size_t count, loff_t *off)
  2161. {
  2162. int err;
  2163. unsigned long k = ULONG_MAX;
  2164. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2165. return -EACCES;
  2166. err = kstrtoul_from_user(buffer, count, 0, &k);
  2167. if (err)
  2168. return err;
  2169. if (k <= 1048576) { /* limit "big buff" to 1 MB */
  2170. sg_big_buff = k;
  2171. return count;
  2172. }
  2173. return -ERANGE;
  2174. }
  2175. static int sg_proc_seq_show_version(struct seq_file *s, void *v)
  2176. {
  2177. seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
  2178. sg_version_date);
  2179. return 0;
  2180. }
  2181. static int sg_proc_single_open_version(struct inode *inode, struct file *file)
  2182. {
  2183. return single_open(file, sg_proc_seq_show_version, NULL);
  2184. }
  2185. static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
  2186. {
  2187. seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
  2188. "online\n");
  2189. return 0;
  2190. }
  2191. static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
  2192. {
  2193. return single_open(file, sg_proc_seq_show_devhdr, NULL);
  2194. }
  2195. struct sg_proc_deviter {
  2196. loff_t index;
  2197. size_t max;
  2198. };
  2199. static void * dev_seq_start(struct seq_file *s, loff_t *pos)
  2200. {
  2201. struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
  2202. s->private = it;
  2203. if (! it)
  2204. return NULL;
  2205. it->index = *pos;
  2206. it->max = sg_last_dev();
  2207. if (it->index >= it->max)
  2208. return NULL;
  2209. return it;
  2210. }
  2211. static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2212. {
  2213. struct sg_proc_deviter * it = s->private;
  2214. *pos = ++it->index;
  2215. return (it->index < it->max) ? it : NULL;
  2216. }
  2217. static void dev_seq_stop(struct seq_file *s, void *v)
  2218. {
  2219. kfree(s->private);
  2220. }
  2221. static int sg_proc_open_dev(struct inode *inode, struct file *file)
  2222. {
  2223. return seq_open(file, &dev_seq_ops);
  2224. }
  2225. static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
  2226. {
  2227. struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
  2228. Sg_device *sdp;
  2229. struct scsi_device *scsidp;
  2230. unsigned long iflags;
  2231. read_lock_irqsave(&sg_index_lock, iflags);
  2232. sdp = it ? sg_lookup_dev(it->index) : NULL;
  2233. if (sdp && (scsidp = sdp->device) && (!sdp->detached))
  2234. seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
  2235. scsidp->host->host_no, scsidp->channel,
  2236. scsidp->id, scsidp->lun, (int) scsidp->type,
  2237. 1,
  2238. (int) scsidp->queue_depth,
  2239. (int) scsidp->device_busy,
  2240. (int) scsi_device_online(scsidp));
  2241. else
  2242. seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
  2243. read_unlock_irqrestore(&sg_index_lock, iflags);
  2244. return 0;
  2245. }
  2246. static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
  2247. {
  2248. return seq_open(file, &devstrs_seq_ops);
  2249. }
  2250. static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
  2251. {
  2252. struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
  2253. Sg_device *sdp;
  2254. struct scsi_device *scsidp;
  2255. unsigned long iflags;
  2256. read_lock_irqsave(&sg_index_lock, iflags);
  2257. sdp = it ? sg_lookup_dev(it->index) : NULL;
  2258. if (sdp && (scsidp = sdp->device) && (!sdp->detached))
  2259. seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
  2260. scsidp->vendor, scsidp->model, scsidp->rev);
  2261. else
  2262. seq_printf(s, "<no active device>\n");
  2263. read_unlock_irqrestore(&sg_index_lock, iflags);
  2264. return 0;
  2265. }
  2266. /* must be called while holding sg_index_lock */
  2267. static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
  2268. {
  2269. int k, m, new_interface, blen, usg;
  2270. Sg_request *srp;
  2271. Sg_fd *fp;
  2272. const sg_io_hdr_t *hp;
  2273. const char * cp;
  2274. unsigned int ms;
  2275. k = 0;
  2276. list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
  2277. k++;
  2278. read_lock(&fp->rq_list_lock); /* irqs already disabled */
  2279. seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
  2280. "(res)sgat=%d low_dma=%d\n", k,
  2281. jiffies_to_msecs(fp->timeout),
  2282. fp->reserve.bufflen,
  2283. (int) fp->reserve.k_use_sg,
  2284. (int) fp->low_dma);
  2285. seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
  2286. (int) fp->cmd_q, (int) fp->force_packid,
  2287. (int) fp->keep_orphan, (int) fp->closed);
  2288. for (m = 0, srp = fp->headrp;
  2289. srp != NULL;
  2290. ++m, srp = srp->nextrp) {
  2291. hp = &srp->header;
  2292. new_interface = (hp->interface_id == '\0') ? 0 : 1;
  2293. if (srp->res_used) {
  2294. if (new_interface &&
  2295. (SG_FLAG_MMAP_IO & hp->flags))
  2296. cp = " mmap>> ";
  2297. else
  2298. cp = " rb>> ";
  2299. } else {
  2300. if (SG_INFO_DIRECT_IO_MASK & hp->info)
  2301. cp = " dio>> ";
  2302. else
  2303. cp = " ";
  2304. }
  2305. seq_printf(s, cp);
  2306. blen = srp->data.bufflen;
  2307. usg = srp->data.k_use_sg;
  2308. seq_printf(s, srp->done ?
  2309. ((1 == srp->done) ? "rcv:" : "fin:")
  2310. : "act:");
  2311. seq_printf(s, " id=%d blen=%d",
  2312. srp->header.pack_id, blen);
  2313. if (srp->done)
  2314. seq_printf(s, " dur=%d", hp->duration);
  2315. else {
  2316. ms = jiffies_to_msecs(jiffies);
  2317. seq_printf(s, " t_o/elap=%d/%d",
  2318. (new_interface ? hp->timeout :
  2319. jiffies_to_msecs(fp->timeout)),
  2320. (ms > hp->duration ? ms - hp->duration : 0));
  2321. }
  2322. seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
  2323. (int) srp->data.cmd_opcode);
  2324. }
  2325. if (0 == m)
  2326. seq_printf(s, " No requests active\n");
  2327. read_unlock(&fp->rq_list_lock);
  2328. }
  2329. }
  2330. static int sg_proc_open_debug(struct inode *inode, struct file *file)
  2331. {
  2332. return seq_open(file, &debug_seq_ops);
  2333. }
  2334. static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
  2335. {
  2336. struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
  2337. Sg_device *sdp;
  2338. unsigned long iflags;
  2339. if (it && (0 == it->index)) {
  2340. seq_printf(s, "max_active_device=%d(origin 1)\n",
  2341. (int)it->max);
  2342. seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
  2343. }
  2344. read_lock_irqsave(&sg_index_lock, iflags);
  2345. sdp = it ? sg_lookup_dev(it->index) : NULL;
  2346. if (sdp && !list_empty(&sdp->sfds)) {
  2347. struct scsi_device *scsidp = sdp->device;
  2348. seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
  2349. if (sdp->detached)
  2350. seq_printf(s, "detached pending close ");
  2351. else
  2352. seq_printf
  2353. (s, "scsi%d chan=%d id=%d lun=%d em=%d",
  2354. scsidp->host->host_no,
  2355. scsidp->channel, scsidp->id,
  2356. scsidp->lun,
  2357. scsidp->host->hostt->emulated);
  2358. seq_printf(s, " sg_tablesize=%d excl=%d\n",
  2359. sdp->sg_tablesize, sdp->exclude);
  2360. sg_proc_debug_helper(s, sdp);
  2361. }
  2362. read_unlock_irqrestore(&sg_index_lock, iflags);
  2363. return 0;
  2364. }
  2365. #endif /* CONFIG_SCSI_PROC_FS */
  2366. module_init(init_sg);
  2367. module_exit(exit_sg);