pktcdvd.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121
  1. /*
  2. * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
  3. * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
  4. * Copyright (C) 2006 Thomas Maier <balagi@justmail.de>
  5. *
  6. * May be copied or modified under the terms of the GNU General Public
  7. * License. See linux/COPYING for more information.
  8. *
  9. * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
  10. * DVD-RAM devices.
  11. *
  12. * Theory of operation:
  13. *
  14. * At the lowest level, there is the standard driver for the CD/DVD device,
  15. * typically ide-cd.c or sr.c. This driver can handle read and write requests,
  16. * but it doesn't know anything about the special restrictions that apply to
  17. * packet writing. One restriction is that write requests must be aligned to
  18. * packet boundaries on the physical media, and the size of a write request
  19. * must be equal to the packet size. Another restriction is that a
  20. * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
  21. * command, if the previous command was a write.
  22. *
  23. * The purpose of the packet writing driver is to hide these restrictions from
  24. * higher layers, such as file systems, and present a block device that can be
  25. * randomly read and written using 2kB-sized blocks.
  26. *
  27. * The lowest layer in the packet writing driver is the packet I/O scheduler.
  28. * Its data is defined by the struct packet_iosched and includes two bio
  29. * queues with pending read and write requests. These queues are processed
  30. * by the pkt_iosched_process_queue() function. The write requests in this
  31. * queue are already properly aligned and sized. This layer is responsible for
  32. * issuing the flush cache commands and scheduling the I/O in a good order.
  33. *
  34. * The next layer transforms unaligned write requests to aligned writes. This
  35. * transformation requires reading missing pieces of data from the underlying
  36. * block device, assembling the pieces to full packets and queuing them to the
  37. * packet I/O scheduler.
  38. *
  39. * At the top layer there is a custom make_request_fn function that forwards
  40. * read requests directly to the iosched queue and puts write requests in the
  41. * unaligned write queue. A kernel thread performs the necessary read
  42. * gathering to convert the unaligned writes to aligned writes and then feeds
  43. * them to the packet I/O scheduler.
  44. *
  45. *************************************************************************/
  46. #include <linux/pktcdvd.h>
  47. #include <linux/module.h>
  48. #include <linux/types.h>
  49. #include <linux/kernel.h>
  50. #include <linux/compat.h>
  51. #include <linux/kthread.h>
  52. #include <linux/errno.h>
  53. #include <linux/spinlock.h>
  54. #include <linux/file.h>
  55. #include <linux/proc_fs.h>
  56. #include <linux/seq_file.h>
  57. #include <linux/miscdevice.h>
  58. #include <linux/freezer.h>
  59. #include <linux/mutex.h>
  60. #include <linux/slab.h>
  61. #include <scsi/scsi_cmnd.h>
  62. #include <scsi/scsi_ioctl.h>
  63. #include <scsi/scsi.h>
  64. #include <linux/debugfs.h>
  65. #include <linux/device.h>
  66. #include <asm/uaccess.h>
  67. #define DRIVER_NAME "pktcdvd"
  68. #if PACKET_DEBUG
  69. #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
  70. #else
  71. #define DPRINTK(fmt, args...)
  72. #endif
  73. #if PACKET_DEBUG > 1
  74. #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
  75. #else
  76. #define VPRINTK(fmt, args...)
  77. #endif
  78. #define MAX_SPEED 0xffff
  79. #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
  80. static DEFINE_MUTEX(pktcdvd_mutex);
  81. static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
  82. static struct proc_dir_entry *pkt_proc;
  83. static int pktdev_major;
  84. static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
  85. static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
  86. static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
  87. static mempool_t *psd_pool;
  88. static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
  89. static struct dentry *pkt_debugfs_root = NULL; /* /sys/kernel/debug/pktcdvd */
  90. /* forward declaration */
  91. static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
  92. static int pkt_remove_dev(dev_t pkt_dev);
  93. static int pkt_seq_show(struct seq_file *m, void *p);
  94. /*
  95. * create and register a pktcdvd kernel object.
  96. */
  97. static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
  98. const char* name,
  99. struct kobject* parent,
  100. struct kobj_type* ktype)
  101. {
  102. struct pktcdvd_kobj *p;
  103. int error;
  104. p = kzalloc(sizeof(*p), GFP_KERNEL);
  105. if (!p)
  106. return NULL;
  107. p->pd = pd;
  108. error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
  109. if (error) {
  110. kobject_put(&p->kobj);
  111. return NULL;
  112. }
  113. kobject_uevent(&p->kobj, KOBJ_ADD);
  114. return p;
  115. }
  116. /*
  117. * remove a pktcdvd kernel object.
  118. */
  119. static void pkt_kobj_remove(struct pktcdvd_kobj *p)
  120. {
  121. if (p)
  122. kobject_put(&p->kobj);
  123. }
  124. /*
  125. * default release function for pktcdvd kernel objects.
  126. */
  127. static void pkt_kobj_release(struct kobject *kobj)
  128. {
  129. kfree(to_pktcdvdkobj(kobj));
  130. }
  131. /**********************************************************
  132. *
  133. * sysfs interface for pktcdvd
  134. * by (C) 2006 Thomas Maier <balagi@justmail.de>
  135. *
  136. **********************************************************/
  137. #define DEF_ATTR(_obj,_name,_mode) \
  138. static struct attribute _obj = { .name = _name, .mode = _mode }
  139. /**********************************************************
  140. /sys/class/pktcdvd/pktcdvd[0-7]/
  141. stat/reset
  142. stat/packets_started
  143. stat/packets_finished
  144. stat/kb_written
  145. stat/kb_read
  146. stat/kb_read_gather
  147. write_queue/size
  148. write_queue/congestion_off
  149. write_queue/congestion_on
  150. **********************************************************/
  151. DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
  152. DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
  153. DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
  154. DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
  155. DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
  156. DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
  157. static struct attribute *kobj_pkt_attrs_stat[] = {
  158. &kobj_pkt_attr_st1,
  159. &kobj_pkt_attr_st2,
  160. &kobj_pkt_attr_st3,
  161. &kobj_pkt_attr_st4,
  162. &kobj_pkt_attr_st5,
  163. &kobj_pkt_attr_st6,
  164. NULL
  165. };
  166. DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
  167. DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
  168. DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
  169. static struct attribute *kobj_pkt_attrs_wqueue[] = {
  170. &kobj_pkt_attr_wq1,
  171. &kobj_pkt_attr_wq2,
  172. &kobj_pkt_attr_wq3,
  173. NULL
  174. };
  175. static ssize_t kobj_pkt_show(struct kobject *kobj,
  176. struct attribute *attr, char *data)
  177. {
  178. struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
  179. int n = 0;
  180. int v;
  181. if (strcmp(attr->name, "packets_started") == 0) {
  182. n = sprintf(data, "%lu\n", pd->stats.pkt_started);
  183. } else if (strcmp(attr->name, "packets_finished") == 0) {
  184. n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
  185. } else if (strcmp(attr->name, "kb_written") == 0) {
  186. n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
  187. } else if (strcmp(attr->name, "kb_read") == 0) {
  188. n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
  189. } else if (strcmp(attr->name, "kb_read_gather") == 0) {
  190. n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
  191. } else if (strcmp(attr->name, "size") == 0) {
  192. spin_lock(&pd->lock);
  193. v = pd->bio_queue_size;
  194. spin_unlock(&pd->lock);
  195. n = sprintf(data, "%d\n", v);
  196. } else if (strcmp(attr->name, "congestion_off") == 0) {
  197. spin_lock(&pd->lock);
  198. v = pd->write_congestion_off;
  199. spin_unlock(&pd->lock);
  200. n = sprintf(data, "%d\n", v);
  201. } else if (strcmp(attr->name, "congestion_on") == 0) {
  202. spin_lock(&pd->lock);
  203. v = pd->write_congestion_on;
  204. spin_unlock(&pd->lock);
  205. n = sprintf(data, "%d\n", v);
  206. }
  207. return n;
  208. }
  209. static void init_write_congestion_marks(int* lo, int* hi)
  210. {
  211. if (*hi > 0) {
  212. *hi = max(*hi, 500);
  213. *hi = min(*hi, 1000000);
  214. if (*lo <= 0)
  215. *lo = *hi - 100;
  216. else {
  217. *lo = min(*lo, *hi - 100);
  218. *lo = max(*lo, 100);
  219. }
  220. } else {
  221. *hi = -1;
  222. *lo = -1;
  223. }
  224. }
  225. static ssize_t kobj_pkt_store(struct kobject *kobj,
  226. struct attribute *attr,
  227. const char *data, size_t len)
  228. {
  229. struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
  230. int val;
  231. if (strcmp(attr->name, "reset") == 0 && len > 0) {
  232. pd->stats.pkt_started = 0;
  233. pd->stats.pkt_ended = 0;
  234. pd->stats.secs_w = 0;
  235. pd->stats.secs_rg = 0;
  236. pd->stats.secs_r = 0;
  237. } else if (strcmp(attr->name, "congestion_off") == 0
  238. && sscanf(data, "%d", &val) == 1) {
  239. spin_lock(&pd->lock);
  240. pd->write_congestion_off = val;
  241. init_write_congestion_marks(&pd->write_congestion_off,
  242. &pd->write_congestion_on);
  243. spin_unlock(&pd->lock);
  244. } else if (strcmp(attr->name, "congestion_on") == 0
  245. && sscanf(data, "%d", &val) == 1) {
  246. spin_lock(&pd->lock);
  247. pd->write_congestion_on = val;
  248. init_write_congestion_marks(&pd->write_congestion_off,
  249. &pd->write_congestion_on);
  250. spin_unlock(&pd->lock);
  251. }
  252. return len;
  253. }
  254. static const struct sysfs_ops kobj_pkt_ops = {
  255. .show = kobj_pkt_show,
  256. .store = kobj_pkt_store
  257. };
  258. static struct kobj_type kobj_pkt_type_stat = {
  259. .release = pkt_kobj_release,
  260. .sysfs_ops = &kobj_pkt_ops,
  261. .default_attrs = kobj_pkt_attrs_stat
  262. };
  263. static struct kobj_type kobj_pkt_type_wqueue = {
  264. .release = pkt_kobj_release,
  265. .sysfs_ops = &kobj_pkt_ops,
  266. .default_attrs = kobj_pkt_attrs_wqueue
  267. };
  268. static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
  269. {
  270. if (class_pktcdvd) {
  271. pd->dev = device_create(class_pktcdvd, NULL, MKDEV(0, 0), NULL,
  272. "%s", pd->name);
  273. if (IS_ERR(pd->dev))
  274. pd->dev = NULL;
  275. }
  276. if (pd->dev) {
  277. pd->kobj_stat = pkt_kobj_create(pd, "stat",
  278. &pd->dev->kobj,
  279. &kobj_pkt_type_stat);
  280. pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
  281. &pd->dev->kobj,
  282. &kobj_pkt_type_wqueue);
  283. }
  284. }
  285. static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
  286. {
  287. pkt_kobj_remove(pd->kobj_stat);
  288. pkt_kobj_remove(pd->kobj_wqueue);
  289. if (class_pktcdvd)
  290. device_unregister(pd->dev);
  291. }
  292. /********************************************************************
  293. /sys/class/pktcdvd/
  294. add map block device
  295. remove unmap packet dev
  296. device_map show mappings
  297. *******************************************************************/
  298. static void class_pktcdvd_release(struct class *cls)
  299. {
  300. kfree(cls);
  301. }
  302. static ssize_t class_pktcdvd_show_map(struct class *c,
  303. struct class_attribute *attr,
  304. char *data)
  305. {
  306. int n = 0;
  307. int idx;
  308. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  309. for (idx = 0; idx < MAX_WRITERS; idx++) {
  310. struct pktcdvd_device *pd = pkt_devs[idx];
  311. if (!pd)
  312. continue;
  313. n += sprintf(data+n, "%s %u:%u %u:%u\n",
  314. pd->name,
  315. MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
  316. MAJOR(pd->bdev->bd_dev),
  317. MINOR(pd->bdev->bd_dev));
  318. }
  319. mutex_unlock(&ctl_mutex);
  320. return n;
  321. }
  322. static ssize_t class_pktcdvd_store_add(struct class *c,
  323. struct class_attribute *attr,
  324. const char *buf,
  325. size_t count)
  326. {
  327. unsigned int major, minor;
  328. if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
  329. /* pkt_setup_dev() expects caller to hold reference to self */
  330. if (!try_module_get(THIS_MODULE))
  331. return -ENODEV;
  332. pkt_setup_dev(MKDEV(major, minor), NULL);
  333. module_put(THIS_MODULE);
  334. return count;
  335. }
  336. return -EINVAL;
  337. }
  338. static ssize_t class_pktcdvd_store_remove(struct class *c,
  339. struct class_attribute *attr,
  340. const char *buf,
  341. size_t count)
  342. {
  343. unsigned int major, minor;
  344. if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
  345. pkt_remove_dev(MKDEV(major, minor));
  346. return count;
  347. }
  348. return -EINVAL;
  349. }
  350. static struct class_attribute class_pktcdvd_attrs[] = {
  351. __ATTR(add, 0200, NULL, class_pktcdvd_store_add),
  352. __ATTR(remove, 0200, NULL, class_pktcdvd_store_remove),
  353. __ATTR(device_map, 0444, class_pktcdvd_show_map, NULL),
  354. __ATTR_NULL
  355. };
  356. static int pkt_sysfs_init(void)
  357. {
  358. int ret = 0;
  359. /*
  360. * create control files in sysfs
  361. * /sys/class/pktcdvd/...
  362. */
  363. class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
  364. if (!class_pktcdvd)
  365. return -ENOMEM;
  366. class_pktcdvd->name = DRIVER_NAME;
  367. class_pktcdvd->owner = THIS_MODULE;
  368. class_pktcdvd->class_release = class_pktcdvd_release;
  369. class_pktcdvd->class_attrs = class_pktcdvd_attrs;
  370. ret = class_register(class_pktcdvd);
  371. if (ret) {
  372. kfree(class_pktcdvd);
  373. class_pktcdvd = NULL;
  374. printk(DRIVER_NAME": failed to create class pktcdvd\n");
  375. return ret;
  376. }
  377. return 0;
  378. }
  379. static void pkt_sysfs_cleanup(void)
  380. {
  381. if (class_pktcdvd)
  382. class_destroy(class_pktcdvd);
  383. class_pktcdvd = NULL;
  384. }
  385. /********************************************************************
  386. entries in debugfs
  387. /sys/kernel/debug/pktcdvd[0-7]/
  388. info
  389. *******************************************************************/
  390. static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
  391. {
  392. return pkt_seq_show(m, p);
  393. }
  394. static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
  395. {
  396. return single_open(file, pkt_debugfs_seq_show, inode->i_private);
  397. }
  398. static const struct file_operations debug_fops = {
  399. .open = pkt_debugfs_fops_open,
  400. .read = seq_read,
  401. .llseek = seq_lseek,
  402. .release = single_release,
  403. .owner = THIS_MODULE,
  404. };
  405. static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
  406. {
  407. if (!pkt_debugfs_root)
  408. return;
  409. pd->dfs_f_info = NULL;
  410. pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
  411. if (IS_ERR(pd->dfs_d_root)) {
  412. pd->dfs_d_root = NULL;
  413. return;
  414. }
  415. pd->dfs_f_info = debugfs_create_file("info", S_IRUGO,
  416. pd->dfs_d_root, pd, &debug_fops);
  417. if (IS_ERR(pd->dfs_f_info)) {
  418. pd->dfs_f_info = NULL;
  419. return;
  420. }
  421. }
  422. static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
  423. {
  424. if (!pkt_debugfs_root)
  425. return;
  426. if (pd->dfs_f_info)
  427. debugfs_remove(pd->dfs_f_info);
  428. pd->dfs_f_info = NULL;
  429. if (pd->dfs_d_root)
  430. debugfs_remove(pd->dfs_d_root);
  431. pd->dfs_d_root = NULL;
  432. }
  433. static void pkt_debugfs_init(void)
  434. {
  435. pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
  436. if (IS_ERR(pkt_debugfs_root)) {
  437. pkt_debugfs_root = NULL;
  438. return;
  439. }
  440. }
  441. static void pkt_debugfs_cleanup(void)
  442. {
  443. if (!pkt_debugfs_root)
  444. return;
  445. debugfs_remove(pkt_debugfs_root);
  446. pkt_debugfs_root = NULL;
  447. }
  448. /* ----------------------------------------------------------*/
  449. static void pkt_bio_finished(struct pktcdvd_device *pd)
  450. {
  451. BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
  452. if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
  453. VPRINTK(DRIVER_NAME": queue empty\n");
  454. atomic_set(&pd->iosched.attention, 1);
  455. wake_up(&pd->wqueue);
  456. }
  457. }
  458. static void pkt_bio_destructor(struct bio *bio)
  459. {
  460. kfree(bio->bi_io_vec);
  461. kfree(bio);
  462. }
  463. static struct bio *pkt_bio_alloc(int nr_iovecs)
  464. {
  465. struct bio_vec *bvl = NULL;
  466. struct bio *bio;
  467. bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
  468. if (!bio)
  469. goto no_bio;
  470. bio_init(bio);
  471. bvl = kcalloc(nr_iovecs, sizeof(struct bio_vec), GFP_KERNEL);
  472. if (!bvl)
  473. goto no_bvl;
  474. bio->bi_max_vecs = nr_iovecs;
  475. bio->bi_io_vec = bvl;
  476. bio->bi_destructor = pkt_bio_destructor;
  477. return bio;
  478. no_bvl:
  479. kfree(bio);
  480. no_bio:
  481. return NULL;
  482. }
  483. /*
  484. * Allocate a packet_data struct
  485. */
  486. static struct packet_data *pkt_alloc_packet_data(int frames)
  487. {
  488. int i;
  489. struct packet_data *pkt;
  490. pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
  491. if (!pkt)
  492. goto no_pkt;
  493. pkt->frames = frames;
  494. pkt->w_bio = pkt_bio_alloc(frames);
  495. if (!pkt->w_bio)
  496. goto no_bio;
  497. for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
  498. pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
  499. if (!pkt->pages[i])
  500. goto no_page;
  501. }
  502. spin_lock_init(&pkt->lock);
  503. bio_list_init(&pkt->orig_bios);
  504. for (i = 0; i < frames; i++) {
  505. struct bio *bio = pkt_bio_alloc(1);
  506. if (!bio)
  507. goto no_rd_bio;
  508. pkt->r_bios[i] = bio;
  509. }
  510. return pkt;
  511. no_rd_bio:
  512. for (i = 0; i < frames; i++) {
  513. struct bio *bio = pkt->r_bios[i];
  514. if (bio)
  515. bio_put(bio);
  516. }
  517. no_page:
  518. for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
  519. if (pkt->pages[i])
  520. __free_page(pkt->pages[i]);
  521. bio_put(pkt->w_bio);
  522. no_bio:
  523. kfree(pkt);
  524. no_pkt:
  525. return NULL;
  526. }
  527. /*
  528. * Free a packet_data struct
  529. */
  530. static void pkt_free_packet_data(struct packet_data *pkt)
  531. {
  532. int i;
  533. for (i = 0; i < pkt->frames; i++) {
  534. struct bio *bio = pkt->r_bios[i];
  535. if (bio)
  536. bio_put(bio);
  537. }
  538. for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
  539. __free_page(pkt->pages[i]);
  540. bio_put(pkt->w_bio);
  541. kfree(pkt);
  542. }
  543. static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
  544. {
  545. struct packet_data *pkt, *next;
  546. BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
  547. list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
  548. pkt_free_packet_data(pkt);
  549. }
  550. INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
  551. }
  552. static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
  553. {
  554. struct packet_data *pkt;
  555. BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
  556. while (nr_packets > 0) {
  557. pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
  558. if (!pkt) {
  559. pkt_shrink_pktlist(pd);
  560. return 0;
  561. }
  562. pkt->id = nr_packets;
  563. pkt->pd = pd;
  564. list_add(&pkt->list, &pd->cdrw.pkt_free_list);
  565. nr_packets--;
  566. }
  567. return 1;
  568. }
  569. static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
  570. {
  571. struct rb_node *n = rb_next(&node->rb_node);
  572. if (!n)
  573. return NULL;
  574. return rb_entry(n, struct pkt_rb_node, rb_node);
  575. }
  576. static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
  577. {
  578. rb_erase(&node->rb_node, &pd->bio_queue);
  579. mempool_free(node, pd->rb_pool);
  580. pd->bio_queue_size--;
  581. BUG_ON(pd->bio_queue_size < 0);
  582. }
  583. /*
  584. * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
  585. */
  586. static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
  587. {
  588. struct rb_node *n = pd->bio_queue.rb_node;
  589. struct rb_node *next;
  590. struct pkt_rb_node *tmp;
  591. if (!n) {
  592. BUG_ON(pd->bio_queue_size > 0);
  593. return NULL;
  594. }
  595. for (;;) {
  596. tmp = rb_entry(n, struct pkt_rb_node, rb_node);
  597. if (s <= tmp->bio->bi_sector)
  598. next = n->rb_left;
  599. else
  600. next = n->rb_right;
  601. if (!next)
  602. break;
  603. n = next;
  604. }
  605. if (s > tmp->bio->bi_sector) {
  606. tmp = pkt_rbtree_next(tmp);
  607. if (!tmp)
  608. return NULL;
  609. }
  610. BUG_ON(s > tmp->bio->bi_sector);
  611. return tmp;
  612. }
  613. /*
  614. * Insert a node into the pd->bio_queue rb tree.
  615. */
  616. static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
  617. {
  618. struct rb_node **p = &pd->bio_queue.rb_node;
  619. struct rb_node *parent = NULL;
  620. sector_t s = node->bio->bi_sector;
  621. struct pkt_rb_node *tmp;
  622. while (*p) {
  623. parent = *p;
  624. tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
  625. if (s < tmp->bio->bi_sector)
  626. p = &(*p)->rb_left;
  627. else
  628. p = &(*p)->rb_right;
  629. }
  630. rb_link_node(&node->rb_node, parent, p);
  631. rb_insert_color(&node->rb_node, &pd->bio_queue);
  632. pd->bio_queue_size++;
  633. }
  634. /*
  635. * Send a packet_command to the underlying block device and
  636. * wait for completion.
  637. */
  638. static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
  639. {
  640. struct request_queue *q = bdev_get_queue(pd->bdev);
  641. struct request *rq;
  642. int ret = 0;
  643. rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
  644. WRITE : READ, __GFP_WAIT);
  645. if (cgc->buflen) {
  646. if (blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, __GFP_WAIT))
  647. goto out;
  648. }
  649. rq->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
  650. memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
  651. rq->timeout = 60*HZ;
  652. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  653. rq->cmd_flags |= REQ_HARDBARRIER;
  654. if (cgc->quiet)
  655. rq->cmd_flags |= REQ_QUIET;
  656. blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
  657. if (rq->errors)
  658. ret = -EIO;
  659. out:
  660. blk_put_request(rq);
  661. return ret;
  662. }
  663. /*
  664. * A generic sense dump / resolve mechanism should be implemented across
  665. * all ATAPI + SCSI devices.
  666. */
  667. static void pkt_dump_sense(struct packet_command *cgc)
  668. {
  669. static char *info[9] = { "No sense", "Recovered error", "Not ready",
  670. "Medium error", "Hardware error", "Illegal request",
  671. "Unit attention", "Data protect", "Blank check" };
  672. int i;
  673. struct request_sense *sense = cgc->sense;
  674. printk(DRIVER_NAME":");
  675. for (i = 0; i < CDROM_PACKET_SIZE; i++)
  676. printk(" %02x", cgc->cmd[i]);
  677. printk(" - ");
  678. if (sense == NULL) {
  679. printk("no sense\n");
  680. return;
  681. }
  682. printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
  683. if (sense->sense_key > 8) {
  684. printk(" (INVALID)\n");
  685. return;
  686. }
  687. printk(" (%s)\n", info[sense->sense_key]);
  688. }
  689. /*
  690. * flush the drive cache to media
  691. */
  692. static int pkt_flush_cache(struct pktcdvd_device *pd)
  693. {
  694. struct packet_command cgc;
  695. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  696. cgc.cmd[0] = GPCMD_FLUSH_CACHE;
  697. cgc.quiet = 1;
  698. /*
  699. * the IMMED bit -- we default to not setting it, although that
  700. * would allow a much faster close, this is safer
  701. */
  702. #if 0
  703. cgc.cmd[1] = 1 << 1;
  704. #endif
  705. return pkt_generic_packet(pd, &cgc);
  706. }
  707. /*
  708. * speed is given as the normal factor, e.g. 4 for 4x
  709. */
  710. static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
  711. unsigned write_speed, unsigned read_speed)
  712. {
  713. struct packet_command cgc;
  714. struct request_sense sense;
  715. int ret;
  716. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  717. cgc.sense = &sense;
  718. cgc.cmd[0] = GPCMD_SET_SPEED;
  719. cgc.cmd[2] = (read_speed >> 8) & 0xff;
  720. cgc.cmd[3] = read_speed & 0xff;
  721. cgc.cmd[4] = (write_speed >> 8) & 0xff;
  722. cgc.cmd[5] = write_speed & 0xff;
  723. if ((ret = pkt_generic_packet(pd, &cgc)))
  724. pkt_dump_sense(&cgc);
  725. return ret;
  726. }
  727. /*
  728. * Queue a bio for processing by the low-level CD device. Must be called
  729. * from process context.
  730. */
  731. static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
  732. {
  733. spin_lock(&pd->iosched.lock);
  734. if (bio_data_dir(bio) == READ)
  735. bio_list_add(&pd->iosched.read_queue, bio);
  736. else
  737. bio_list_add(&pd->iosched.write_queue, bio);
  738. spin_unlock(&pd->iosched.lock);
  739. atomic_set(&pd->iosched.attention, 1);
  740. wake_up(&pd->wqueue);
  741. }
  742. /*
  743. * Process the queued read/write requests. This function handles special
  744. * requirements for CDRW drives:
  745. * - A cache flush command must be inserted before a read request if the
  746. * previous request was a write.
  747. * - Switching between reading and writing is slow, so don't do it more often
  748. * than necessary.
  749. * - Optimize for throughput at the expense of latency. This means that streaming
  750. * writes will never be interrupted by a read, but if the drive has to seek
  751. * before the next write, switch to reading instead if there are any pending
  752. * read requests.
  753. * - Set the read speed according to current usage pattern. When only reading
  754. * from the device, it's best to use the highest possible read speed, but
  755. * when switching often between reading and writing, it's better to have the
  756. * same read and write speeds.
  757. */
  758. static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
  759. {
  760. if (atomic_read(&pd->iosched.attention) == 0)
  761. return;
  762. atomic_set(&pd->iosched.attention, 0);
  763. for (;;) {
  764. struct bio *bio;
  765. int reads_queued, writes_queued;
  766. spin_lock(&pd->iosched.lock);
  767. reads_queued = !bio_list_empty(&pd->iosched.read_queue);
  768. writes_queued = !bio_list_empty(&pd->iosched.write_queue);
  769. spin_unlock(&pd->iosched.lock);
  770. if (!reads_queued && !writes_queued)
  771. break;
  772. if (pd->iosched.writing) {
  773. int need_write_seek = 1;
  774. spin_lock(&pd->iosched.lock);
  775. bio = bio_list_peek(&pd->iosched.write_queue);
  776. spin_unlock(&pd->iosched.lock);
  777. if (bio && (bio->bi_sector == pd->iosched.last_write))
  778. need_write_seek = 0;
  779. if (need_write_seek && reads_queued) {
  780. if (atomic_read(&pd->cdrw.pending_bios) > 0) {
  781. VPRINTK(DRIVER_NAME": write, waiting\n");
  782. break;
  783. }
  784. pkt_flush_cache(pd);
  785. pd->iosched.writing = 0;
  786. }
  787. } else {
  788. if (!reads_queued && writes_queued) {
  789. if (atomic_read(&pd->cdrw.pending_bios) > 0) {
  790. VPRINTK(DRIVER_NAME": read, waiting\n");
  791. break;
  792. }
  793. pd->iosched.writing = 1;
  794. }
  795. }
  796. spin_lock(&pd->iosched.lock);
  797. if (pd->iosched.writing)
  798. bio = bio_list_pop(&pd->iosched.write_queue);
  799. else
  800. bio = bio_list_pop(&pd->iosched.read_queue);
  801. spin_unlock(&pd->iosched.lock);
  802. if (!bio)
  803. continue;
  804. if (bio_data_dir(bio) == READ)
  805. pd->iosched.successive_reads += bio->bi_size >> 10;
  806. else {
  807. pd->iosched.successive_reads = 0;
  808. pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
  809. }
  810. if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
  811. if (pd->read_speed == pd->write_speed) {
  812. pd->read_speed = MAX_SPEED;
  813. pkt_set_speed(pd, pd->write_speed, pd->read_speed);
  814. }
  815. } else {
  816. if (pd->read_speed != pd->write_speed) {
  817. pd->read_speed = pd->write_speed;
  818. pkt_set_speed(pd, pd->write_speed, pd->read_speed);
  819. }
  820. }
  821. atomic_inc(&pd->cdrw.pending_bios);
  822. generic_make_request(bio);
  823. }
  824. }
  825. /*
  826. * Special care is needed if the underlying block device has a small
  827. * max_phys_segments value.
  828. */
  829. static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
  830. {
  831. if ((pd->settings.size << 9) / CD_FRAMESIZE
  832. <= queue_max_segments(q)) {
  833. /*
  834. * The cdrom device can handle one segment/frame
  835. */
  836. clear_bit(PACKET_MERGE_SEGS, &pd->flags);
  837. return 0;
  838. } else if ((pd->settings.size << 9) / PAGE_SIZE
  839. <= queue_max_segments(q)) {
  840. /*
  841. * We can handle this case at the expense of some extra memory
  842. * copies during write operations
  843. */
  844. set_bit(PACKET_MERGE_SEGS, &pd->flags);
  845. return 0;
  846. } else {
  847. printk(DRIVER_NAME": cdrom max_phys_segments too small\n");
  848. return -EIO;
  849. }
  850. }
  851. /*
  852. * Copy CD_FRAMESIZE bytes from src_bio into a destination page
  853. */
  854. static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
  855. {
  856. unsigned int copy_size = CD_FRAMESIZE;
  857. while (copy_size > 0) {
  858. struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
  859. void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
  860. src_bvl->bv_offset + offs;
  861. void *vto = page_address(dst_page) + dst_offs;
  862. int len = min_t(int, copy_size, src_bvl->bv_len - offs);
  863. BUG_ON(len < 0);
  864. memcpy(vto, vfrom, len);
  865. kunmap_atomic(vfrom, KM_USER0);
  866. seg++;
  867. offs = 0;
  868. dst_offs += len;
  869. copy_size -= len;
  870. }
  871. }
  872. /*
  873. * Copy all data for this packet to pkt->pages[], so that
  874. * a) The number of required segments for the write bio is minimized, which
  875. * is necessary for some scsi controllers.
  876. * b) The data can be used as cache to avoid read requests if we receive a
  877. * new write request for the same zone.
  878. */
  879. static void pkt_make_local_copy(struct packet_data *pkt, struct bio_vec *bvec)
  880. {
  881. int f, p, offs;
  882. /* Copy all data to pkt->pages[] */
  883. p = 0;
  884. offs = 0;
  885. for (f = 0; f < pkt->frames; f++) {
  886. if (bvec[f].bv_page != pkt->pages[p]) {
  887. void *vfrom = kmap_atomic(bvec[f].bv_page, KM_USER0) + bvec[f].bv_offset;
  888. void *vto = page_address(pkt->pages[p]) + offs;
  889. memcpy(vto, vfrom, CD_FRAMESIZE);
  890. kunmap_atomic(vfrom, KM_USER0);
  891. bvec[f].bv_page = pkt->pages[p];
  892. bvec[f].bv_offset = offs;
  893. } else {
  894. BUG_ON(bvec[f].bv_offset != offs);
  895. }
  896. offs += CD_FRAMESIZE;
  897. if (offs >= PAGE_SIZE) {
  898. offs = 0;
  899. p++;
  900. }
  901. }
  902. }
  903. static void pkt_end_io_read(struct bio *bio, int err)
  904. {
  905. struct packet_data *pkt = bio->bi_private;
  906. struct pktcdvd_device *pd = pkt->pd;
  907. BUG_ON(!pd);
  908. VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
  909. (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
  910. if (err)
  911. atomic_inc(&pkt->io_errors);
  912. if (atomic_dec_and_test(&pkt->io_wait)) {
  913. atomic_inc(&pkt->run_sm);
  914. wake_up(&pd->wqueue);
  915. }
  916. pkt_bio_finished(pd);
  917. }
  918. static void pkt_end_io_packet_write(struct bio *bio, int err)
  919. {
  920. struct packet_data *pkt = bio->bi_private;
  921. struct pktcdvd_device *pd = pkt->pd;
  922. BUG_ON(!pd);
  923. VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
  924. pd->stats.pkt_ended++;
  925. pkt_bio_finished(pd);
  926. atomic_dec(&pkt->io_wait);
  927. atomic_inc(&pkt->run_sm);
  928. wake_up(&pd->wqueue);
  929. }
  930. /*
  931. * Schedule reads for the holes in a packet
  932. */
  933. static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
  934. {
  935. int frames_read = 0;
  936. struct bio *bio;
  937. int f;
  938. char written[PACKET_MAX_SIZE];
  939. BUG_ON(bio_list_empty(&pkt->orig_bios));
  940. atomic_set(&pkt->io_wait, 0);
  941. atomic_set(&pkt->io_errors, 0);
  942. /*
  943. * Figure out which frames we need to read before we can write.
  944. */
  945. memset(written, 0, sizeof(written));
  946. spin_lock(&pkt->lock);
  947. bio_list_for_each(bio, &pkt->orig_bios) {
  948. int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
  949. int num_frames = bio->bi_size / CD_FRAMESIZE;
  950. pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
  951. BUG_ON(first_frame < 0);
  952. BUG_ON(first_frame + num_frames > pkt->frames);
  953. for (f = first_frame; f < first_frame + num_frames; f++)
  954. written[f] = 1;
  955. }
  956. spin_unlock(&pkt->lock);
  957. if (pkt->cache_valid) {
  958. VPRINTK("pkt_gather_data: zone %llx cached\n",
  959. (unsigned long long)pkt->sector);
  960. goto out_account;
  961. }
  962. /*
  963. * Schedule reads for missing parts of the packet.
  964. */
  965. for (f = 0; f < pkt->frames; f++) {
  966. struct bio_vec *vec;
  967. int p, offset;
  968. if (written[f])
  969. continue;
  970. bio = pkt->r_bios[f];
  971. vec = bio->bi_io_vec;
  972. bio_init(bio);
  973. bio->bi_max_vecs = 1;
  974. bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
  975. bio->bi_bdev = pd->bdev;
  976. bio->bi_end_io = pkt_end_io_read;
  977. bio->bi_private = pkt;
  978. bio->bi_io_vec = vec;
  979. bio->bi_destructor = pkt_bio_destructor;
  980. p = (f * CD_FRAMESIZE) / PAGE_SIZE;
  981. offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
  982. VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
  983. f, pkt->pages[p], offset);
  984. if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
  985. BUG();
  986. atomic_inc(&pkt->io_wait);
  987. bio->bi_rw = READ;
  988. pkt_queue_bio(pd, bio);
  989. frames_read++;
  990. }
  991. out_account:
  992. VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
  993. frames_read, (unsigned long long)pkt->sector);
  994. pd->stats.pkt_started++;
  995. pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
  996. }
  997. /*
  998. * Find a packet matching zone, or the least recently used packet if
  999. * there is no match.
  1000. */
  1001. static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
  1002. {
  1003. struct packet_data *pkt;
  1004. list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
  1005. if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
  1006. list_del_init(&pkt->list);
  1007. if (pkt->sector != zone)
  1008. pkt->cache_valid = 0;
  1009. return pkt;
  1010. }
  1011. }
  1012. BUG();
  1013. return NULL;
  1014. }
  1015. static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
  1016. {
  1017. if (pkt->cache_valid) {
  1018. list_add(&pkt->list, &pd->cdrw.pkt_free_list);
  1019. } else {
  1020. list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
  1021. }
  1022. }
  1023. /*
  1024. * recover a failed write, query for relocation if possible
  1025. *
  1026. * returns 1 if recovery is possible, or 0 if not
  1027. *
  1028. */
  1029. static int pkt_start_recovery(struct packet_data *pkt)
  1030. {
  1031. /*
  1032. * FIXME. We need help from the file system to implement
  1033. * recovery handling.
  1034. */
  1035. return 0;
  1036. #if 0
  1037. struct request *rq = pkt->rq;
  1038. struct pktcdvd_device *pd = rq->rq_disk->private_data;
  1039. struct block_device *pkt_bdev;
  1040. struct super_block *sb = NULL;
  1041. unsigned long old_block, new_block;
  1042. sector_t new_sector;
  1043. pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
  1044. if (pkt_bdev) {
  1045. sb = get_super(pkt_bdev);
  1046. bdput(pkt_bdev);
  1047. }
  1048. if (!sb)
  1049. return 0;
  1050. if (!sb->s_op || !sb->s_op->relocate_blocks)
  1051. goto out;
  1052. old_block = pkt->sector / (CD_FRAMESIZE >> 9);
  1053. if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
  1054. goto out;
  1055. new_sector = new_block * (CD_FRAMESIZE >> 9);
  1056. pkt->sector = new_sector;
  1057. pkt->bio->bi_sector = new_sector;
  1058. pkt->bio->bi_next = NULL;
  1059. pkt->bio->bi_flags = 1 << BIO_UPTODATE;
  1060. pkt->bio->bi_idx = 0;
  1061. BUG_ON(pkt->bio->bi_rw != REQ_WRITE);
  1062. BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
  1063. BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
  1064. BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
  1065. BUG_ON(pkt->bio->bi_private != pkt);
  1066. drop_super(sb);
  1067. return 1;
  1068. out:
  1069. drop_super(sb);
  1070. return 0;
  1071. #endif
  1072. }
  1073. static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
  1074. {
  1075. #if PACKET_DEBUG > 1
  1076. static const char *state_name[] = {
  1077. "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
  1078. };
  1079. enum packet_data_state old_state = pkt->state;
  1080. VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
  1081. state_name[old_state], state_name[state]);
  1082. #endif
  1083. pkt->state = state;
  1084. }
  1085. /*
  1086. * Scan the work queue to see if we can start a new packet.
  1087. * returns non-zero if any work was done.
  1088. */
  1089. static int pkt_handle_queue(struct pktcdvd_device *pd)
  1090. {
  1091. struct packet_data *pkt, *p;
  1092. struct bio *bio = NULL;
  1093. sector_t zone = 0; /* Suppress gcc warning */
  1094. struct pkt_rb_node *node, *first_node;
  1095. struct rb_node *n;
  1096. int wakeup;
  1097. VPRINTK("handle_queue\n");
  1098. atomic_set(&pd->scan_queue, 0);
  1099. if (list_empty(&pd->cdrw.pkt_free_list)) {
  1100. VPRINTK("handle_queue: no pkt\n");
  1101. return 0;
  1102. }
  1103. /*
  1104. * Try to find a zone we are not already working on.
  1105. */
  1106. spin_lock(&pd->lock);
  1107. first_node = pkt_rbtree_find(pd, pd->current_sector);
  1108. if (!first_node) {
  1109. n = rb_first(&pd->bio_queue);
  1110. if (n)
  1111. first_node = rb_entry(n, struct pkt_rb_node, rb_node);
  1112. }
  1113. node = first_node;
  1114. while (node) {
  1115. bio = node->bio;
  1116. zone = ZONE(bio->bi_sector, pd);
  1117. list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
  1118. if (p->sector == zone) {
  1119. bio = NULL;
  1120. goto try_next_bio;
  1121. }
  1122. }
  1123. break;
  1124. try_next_bio:
  1125. node = pkt_rbtree_next(node);
  1126. if (!node) {
  1127. n = rb_first(&pd->bio_queue);
  1128. if (n)
  1129. node = rb_entry(n, struct pkt_rb_node, rb_node);
  1130. }
  1131. if (node == first_node)
  1132. node = NULL;
  1133. }
  1134. spin_unlock(&pd->lock);
  1135. if (!bio) {
  1136. VPRINTK("handle_queue: no bio\n");
  1137. return 0;
  1138. }
  1139. pkt = pkt_get_packet_data(pd, zone);
  1140. pd->current_sector = zone + pd->settings.size;
  1141. pkt->sector = zone;
  1142. BUG_ON(pkt->frames != pd->settings.size >> 2);
  1143. pkt->write_size = 0;
  1144. /*
  1145. * Scan work queue for bios in the same zone and link them
  1146. * to this packet.
  1147. */
  1148. spin_lock(&pd->lock);
  1149. VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
  1150. while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
  1151. bio = node->bio;
  1152. VPRINTK("pkt_handle_queue: found zone=%llx\n",
  1153. (unsigned long long)ZONE(bio->bi_sector, pd));
  1154. if (ZONE(bio->bi_sector, pd) != zone)
  1155. break;
  1156. pkt_rbtree_erase(pd, node);
  1157. spin_lock(&pkt->lock);
  1158. bio_list_add(&pkt->orig_bios, bio);
  1159. pkt->write_size += bio->bi_size / CD_FRAMESIZE;
  1160. spin_unlock(&pkt->lock);
  1161. }
  1162. /* check write congestion marks, and if bio_queue_size is
  1163. below, wake up any waiters */
  1164. wakeup = (pd->write_congestion_on > 0
  1165. && pd->bio_queue_size <= pd->write_congestion_off);
  1166. spin_unlock(&pd->lock);
  1167. if (wakeup) {
  1168. clear_bdi_congested(&pd->disk->queue->backing_dev_info,
  1169. BLK_RW_ASYNC);
  1170. }
  1171. pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
  1172. pkt_set_state(pkt, PACKET_WAITING_STATE);
  1173. atomic_set(&pkt->run_sm, 1);
  1174. spin_lock(&pd->cdrw.active_list_lock);
  1175. list_add(&pkt->list, &pd->cdrw.pkt_active_list);
  1176. spin_unlock(&pd->cdrw.active_list_lock);
  1177. return 1;
  1178. }
  1179. /*
  1180. * Assemble a bio to write one packet and queue the bio for processing
  1181. * by the underlying block device.
  1182. */
  1183. static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
  1184. {
  1185. struct bio *bio;
  1186. int f;
  1187. int frames_write;
  1188. struct bio_vec *bvec = pkt->w_bio->bi_io_vec;
  1189. for (f = 0; f < pkt->frames; f++) {
  1190. bvec[f].bv_page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
  1191. bvec[f].bv_offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
  1192. }
  1193. /*
  1194. * Fill-in bvec with data from orig_bios.
  1195. */
  1196. frames_write = 0;
  1197. spin_lock(&pkt->lock);
  1198. bio_list_for_each(bio, &pkt->orig_bios) {
  1199. int segment = bio->bi_idx;
  1200. int src_offs = 0;
  1201. int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
  1202. int num_frames = bio->bi_size / CD_FRAMESIZE;
  1203. BUG_ON(first_frame < 0);
  1204. BUG_ON(first_frame + num_frames > pkt->frames);
  1205. for (f = first_frame; f < first_frame + num_frames; f++) {
  1206. struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
  1207. while (src_offs >= src_bvl->bv_len) {
  1208. src_offs -= src_bvl->bv_len;
  1209. segment++;
  1210. BUG_ON(segment >= bio->bi_vcnt);
  1211. src_bvl = bio_iovec_idx(bio, segment);
  1212. }
  1213. if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
  1214. bvec[f].bv_page = src_bvl->bv_page;
  1215. bvec[f].bv_offset = src_bvl->bv_offset + src_offs;
  1216. } else {
  1217. pkt_copy_bio_data(bio, segment, src_offs,
  1218. bvec[f].bv_page, bvec[f].bv_offset);
  1219. }
  1220. src_offs += CD_FRAMESIZE;
  1221. frames_write++;
  1222. }
  1223. }
  1224. pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
  1225. spin_unlock(&pkt->lock);
  1226. VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
  1227. frames_write, (unsigned long long)pkt->sector);
  1228. BUG_ON(frames_write != pkt->write_size);
  1229. if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
  1230. pkt_make_local_copy(pkt, bvec);
  1231. pkt->cache_valid = 1;
  1232. } else {
  1233. pkt->cache_valid = 0;
  1234. }
  1235. /* Start the write request */
  1236. bio_init(pkt->w_bio);
  1237. pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
  1238. pkt->w_bio->bi_sector = pkt->sector;
  1239. pkt->w_bio->bi_bdev = pd->bdev;
  1240. pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
  1241. pkt->w_bio->bi_private = pkt;
  1242. pkt->w_bio->bi_io_vec = bvec;
  1243. pkt->w_bio->bi_destructor = pkt_bio_destructor;
  1244. for (f = 0; f < pkt->frames; f++)
  1245. if (!bio_add_page(pkt->w_bio, bvec[f].bv_page, CD_FRAMESIZE, bvec[f].bv_offset))
  1246. BUG();
  1247. VPRINTK(DRIVER_NAME": vcnt=%d\n", pkt->w_bio->bi_vcnt);
  1248. atomic_set(&pkt->io_wait, 1);
  1249. pkt->w_bio->bi_rw = WRITE;
  1250. pkt_queue_bio(pd, pkt->w_bio);
  1251. }
  1252. static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
  1253. {
  1254. struct bio *bio;
  1255. if (!uptodate)
  1256. pkt->cache_valid = 0;
  1257. /* Finish all bios corresponding to this packet */
  1258. while ((bio = bio_list_pop(&pkt->orig_bios)))
  1259. bio_endio(bio, uptodate ? 0 : -EIO);
  1260. }
  1261. static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
  1262. {
  1263. int uptodate;
  1264. VPRINTK("run_state_machine: pkt %d\n", pkt->id);
  1265. for (;;) {
  1266. switch (pkt->state) {
  1267. case PACKET_WAITING_STATE:
  1268. if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
  1269. return;
  1270. pkt->sleep_time = 0;
  1271. pkt_gather_data(pd, pkt);
  1272. pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
  1273. break;
  1274. case PACKET_READ_WAIT_STATE:
  1275. if (atomic_read(&pkt->io_wait) > 0)
  1276. return;
  1277. if (atomic_read(&pkt->io_errors) > 0) {
  1278. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  1279. } else {
  1280. pkt_start_write(pd, pkt);
  1281. }
  1282. break;
  1283. case PACKET_WRITE_WAIT_STATE:
  1284. if (atomic_read(&pkt->io_wait) > 0)
  1285. return;
  1286. if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
  1287. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  1288. } else {
  1289. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  1290. }
  1291. break;
  1292. case PACKET_RECOVERY_STATE:
  1293. if (pkt_start_recovery(pkt)) {
  1294. pkt_start_write(pd, pkt);
  1295. } else {
  1296. VPRINTK("No recovery possible\n");
  1297. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  1298. }
  1299. break;
  1300. case PACKET_FINISHED_STATE:
  1301. uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
  1302. pkt_finish_packet(pkt, uptodate);
  1303. return;
  1304. default:
  1305. BUG();
  1306. break;
  1307. }
  1308. }
  1309. }
  1310. static void pkt_handle_packets(struct pktcdvd_device *pd)
  1311. {
  1312. struct packet_data *pkt, *next;
  1313. VPRINTK("pkt_handle_packets\n");
  1314. /*
  1315. * Run state machine for active packets
  1316. */
  1317. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1318. if (atomic_read(&pkt->run_sm) > 0) {
  1319. atomic_set(&pkt->run_sm, 0);
  1320. pkt_run_state_machine(pd, pkt);
  1321. }
  1322. }
  1323. /*
  1324. * Move no longer active packets to the free list
  1325. */
  1326. spin_lock(&pd->cdrw.active_list_lock);
  1327. list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
  1328. if (pkt->state == PACKET_FINISHED_STATE) {
  1329. list_del(&pkt->list);
  1330. pkt_put_packet_data(pd, pkt);
  1331. pkt_set_state(pkt, PACKET_IDLE_STATE);
  1332. atomic_set(&pd->scan_queue, 1);
  1333. }
  1334. }
  1335. spin_unlock(&pd->cdrw.active_list_lock);
  1336. }
  1337. static void pkt_count_states(struct pktcdvd_device *pd, int *states)
  1338. {
  1339. struct packet_data *pkt;
  1340. int i;
  1341. for (i = 0; i < PACKET_NUM_STATES; i++)
  1342. states[i] = 0;
  1343. spin_lock(&pd->cdrw.active_list_lock);
  1344. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1345. states[pkt->state]++;
  1346. }
  1347. spin_unlock(&pd->cdrw.active_list_lock);
  1348. }
  1349. /*
  1350. * kcdrwd is woken up when writes have been queued for one of our
  1351. * registered devices
  1352. */
  1353. static int kcdrwd(void *foobar)
  1354. {
  1355. struct pktcdvd_device *pd = foobar;
  1356. struct packet_data *pkt;
  1357. long min_sleep_time, residue;
  1358. set_user_nice(current, -20);
  1359. set_freezable();
  1360. for (;;) {
  1361. DECLARE_WAITQUEUE(wait, current);
  1362. /*
  1363. * Wait until there is something to do
  1364. */
  1365. add_wait_queue(&pd->wqueue, &wait);
  1366. for (;;) {
  1367. set_current_state(TASK_INTERRUPTIBLE);
  1368. /* Check if we need to run pkt_handle_queue */
  1369. if (atomic_read(&pd->scan_queue) > 0)
  1370. goto work_to_do;
  1371. /* Check if we need to run the state machine for some packet */
  1372. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1373. if (atomic_read(&pkt->run_sm) > 0)
  1374. goto work_to_do;
  1375. }
  1376. /* Check if we need to process the iosched queues */
  1377. if (atomic_read(&pd->iosched.attention) != 0)
  1378. goto work_to_do;
  1379. /* Otherwise, go to sleep */
  1380. if (PACKET_DEBUG > 1) {
  1381. int states[PACKET_NUM_STATES];
  1382. pkt_count_states(pd, states);
  1383. VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
  1384. states[0], states[1], states[2], states[3],
  1385. states[4], states[5]);
  1386. }
  1387. min_sleep_time = MAX_SCHEDULE_TIMEOUT;
  1388. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1389. if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
  1390. min_sleep_time = pkt->sleep_time;
  1391. }
  1392. generic_unplug_device(bdev_get_queue(pd->bdev));
  1393. VPRINTK("kcdrwd: sleeping\n");
  1394. residue = schedule_timeout(min_sleep_time);
  1395. VPRINTK("kcdrwd: wake up\n");
  1396. /* make swsusp happy with our thread */
  1397. try_to_freeze();
  1398. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1399. if (!pkt->sleep_time)
  1400. continue;
  1401. pkt->sleep_time -= min_sleep_time - residue;
  1402. if (pkt->sleep_time <= 0) {
  1403. pkt->sleep_time = 0;
  1404. atomic_inc(&pkt->run_sm);
  1405. }
  1406. }
  1407. if (kthread_should_stop())
  1408. break;
  1409. }
  1410. work_to_do:
  1411. set_current_state(TASK_RUNNING);
  1412. remove_wait_queue(&pd->wqueue, &wait);
  1413. if (kthread_should_stop())
  1414. break;
  1415. /*
  1416. * if pkt_handle_queue returns true, we can queue
  1417. * another request.
  1418. */
  1419. while (pkt_handle_queue(pd))
  1420. ;
  1421. /*
  1422. * Handle packet state machine
  1423. */
  1424. pkt_handle_packets(pd);
  1425. /*
  1426. * Handle iosched queues
  1427. */
  1428. pkt_iosched_process_queue(pd);
  1429. }
  1430. return 0;
  1431. }
  1432. static void pkt_print_settings(struct pktcdvd_device *pd)
  1433. {
  1434. printk(DRIVER_NAME": %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
  1435. printk("%u blocks, ", pd->settings.size >> 2);
  1436. printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
  1437. }
  1438. static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
  1439. {
  1440. memset(cgc->cmd, 0, sizeof(cgc->cmd));
  1441. cgc->cmd[0] = GPCMD_MODE_SENSE_10;
  1442. cgc->cmd[2] = page_code | (page_control << 6);
  1443. cgc->cmd[7] = cgc->buflen >> 8;
  1444. cgc->cmd[8] = cgc->buflen & 0xff;
  1445. cgc->data_direction = CGC_DATA_READ;
  1446. return pkt_generic_packet(pd, cgc);
  1447. }
  1448. static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
  1449. {
  1450. memset(cgc->cmd, 0, sizeof(cgc->cmd));
  1451. memset(cgc->buffer, 0, 2);
  1452. cgc->cmd[0] = GPCMD_MODE_SELECT_10;
  1453. cgc->cmd[1] = 0x10; /* PF */
  1454. cgc->cmd[7] = cgc->buflen >> 8;
  1455. cgc->cmd[8] = cgc->buflen & 0xff;
  1456. cgc->data_direction = CGC_DATA_WRITE;
  1457. return pkt_generic_packet(pd, cgc);
  1458. }
  1459. static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
  1460. {
  1461. struct packet_command cgc;
  1462. int ret;
  1463. /* set up command and get the disc info */
  1464. init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
  1465. cgc.cmd[0] = GPCMD_READ_DISC_INFO;
  1466. cgc.cmd[8] = cgc.buflen = 2;
  1467. cgc.quiet = 1;
  1468. if ((ret = pkt_generic_packet(pd, &cgc)))
  1469. return ret;
  1470. /* not all drives have the same disc_info length, so requeue
  1471. * packet with the length the drive tells us it can supply
  1472. */
  1473. cgc.buflen = be16_to_cpu(di->disc_information_length) +
  1474. sizeof(di->disc_information_length);
  1475. if (cgc.buflen > sizeof(disc_information))
  1476. cgc.buflen = sizeof(disc_information);
  1477. cgc.cmd[8] = cgc.buflen;
  1478. return pkt_generic_packet(pd, &cgc);
  1479. }
  1480. static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
  1481. {
  1482. struct packet_command cgc;
  1483. int ret;
  1484. init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
  1485. cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
  1486. cgc.cmd[1] = type & 3;
  1487. cgc.cmd[4] = (track & 0xff00) >> 8;
  1488. cgc.cmd[5] = track & 0xff;
  1489. cgc.cmd[8] = 8;
  1490. cgc.quiet = 1;
  1491. if ((ret = pkt_generic_packet(pd, &cgc)))
  1492. return ret;
  1493. cgc.buflen = be16_to_cpu(ti->track_information_length) +
  1494. sizeof(ti->track_information_length);
  1495. if (cgc.buflen > sizeof(track_information))
  1496. cgc.buflen = sizeof(track_information);
  1497. cgc.cmd[8] = cgc.buflen;
  1498. return pkt_generic_packet(pd, &cgc);
  1499. }
  1500. static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
  1501. long *last_written)
  1502. {
  1503. disc_information di;
  1504. track_information ti;
  1505. __u32 last_track;
  1506. int ret = -1;
  1507. if ((ret = pkt_get_disc_info(pd, &di)))
  1508. return ret;
  1509. last_track = (di.last_track_msb << 8) | di.last_track_lsb;
  1510. if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
  1511. return ret;
  1512. /* if this track is blank, try the previous. */
  1513. if (ti.blank) {
  1514. last_track--;
  1515. if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
  1516. return ret;
  1517. }
  1518. /* if last recorded field is valid, return it. */
  1519. if (ti.lra_v) {
  1520. *last_written = be32_to_cpu(ti.last_rec_address);
  1521. } else {
  1522. /* make it up instead */
  1523. *last_written = be32_to_cpu(ti.track_start) +
  1524. be32_to_cpu(ti.track_size);
  1525. if (ti.free_blocks)
  1526. *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
  1527. }
  1528. return 0;
  1529. }
  1530. /*
  1531. * write mode select package based on pd->settings
  1532. */
  1533. static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
  1534. {
  1535. struct packet_command cgc;
  1536. struct request_sense sense;
  1537. write_param_page *wp;
  1538. char buffer[128];
  1539. int ret, size;
  1540. /* doesn't apply to DVD+RW or DVD-RAM */
  1541. if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
  1542. return 0;
  1543. memset(buffer, 0, sizeof(buffer));
  1544. init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
  1545. cgc.sense = &sense;
  1546. if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
  1547. pkt_dump_sense(&cgc);
  1548. return ret;
  1549. }
  1550. size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
  1551. pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
  1552. if (size > sizeof(buffer))
  1553. size = sizeof(buffer);
  1554. /*
  1555. * now get it all
  1556. */
  1557. init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
  1558. cgc.sense = &sense;
  1559. if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
  1560. pkt_dump_sense(&cgc);
  1561. return ret;
  1562. }
  1563. /*
  1564. * write page is offset header + block descriptor length
  1565. */
  1566. wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
  1567. wp->fp = pd->settings.fp;
  1568. wp->track_mode = pd->settings.track_mode;
  1569. wp->write_type = pd->settings.write_type;
  1570. wp->data_block_type = pd->settings.block_mode;
  1571. wp->multi_session = 0;
  1572. #ifdef PACKET_USE_LS
  1573. wp->link_size = 7;
  1574. wp->ls_v = 1;
  1575. #endif
  1576. if (wp->data_block_type == PACKET_BLOCK_MODE1) {
  1577. wp->session_format = 0;
  1578. wp->subhdr2 = 0x20;
  1579. } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
  1580. wp->session_format = 0x20;
  1581. wp->subhdr2 = 8;
  1582. #if 0
  1583. wp->mcn[0] = 0x80;
  1584. memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
  1585. #endif
  1586. } else {
  1587. /*
  1588. * paranoia
  1589. */
  1590. printk(DRIVER_NAME": write mode wrong %d\n", wp->data_block_type);
  1591. return 1;
  1592. }
  1593. wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
  1594. cgc.buflen = cgc.cmd[8] = size;
  1595. if ((ret = pkt_mode_select(pd, &cgc))) {
  1596. pkt_dump_sense(&cgc);
  1597. return ret;
  1598. }
  1599. pkt_print_settings(pd);
  1600. return 0;
  1601. }
  1602. /*
  1603. * 1 -- we can write to this track, 0 -- we can't
  1604. */
  1605. static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
  1606. {
  1607. switch (pd->mmc3_profile) {
  1608. case 0x1a: /* DVD+RW */
  1609. case 0x12: /* DVD-RAM */
  1610. /* The track is always writable on DVD+RW/DVD-RAM */
  1611. return 1;
  1612. default:
  1613. break;
  1614. }
  1615. if (!ti->packet || !ti->fp)
  1616. return 0;
  1617. /*
  1618. * "good" settings as per Mt Fuji.
  1619. */
  1620. if (ti->rt == 0 && ti->blank == 0)
  1621. return 1;
  1622. if (ti->rt == 0 && ti->blank == 1)
  1623. return 1;
  1624. if (ti->rt == 1 && ti->blank == 0)
  1625. return 1;
  1626. printk(DRIVER_NAME": bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
  1627. return 0;
  1628. }
  1629. /*
  1630. * 1 -- we can write to this disc, 0 -- we can't
  1631. */
  1632. static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
  1633. {
  1634. switch (pd->mmc3_profile) {
  1635. case 0x0a: /* CD-RW */
  1636. case 0xffff: /* MMC3 not supported */
  1637. break;
  1638. case 0x1a: /* DVD+RW */
  1639. case 0x13: /* DVD-RW */
  1640. case 0x12: /* DVD-RAM */
  1641. return 1;
  1642. default:
  1643. VPRINTK(DRIVER_NAME": Wrong disc profile (%x)\n", pd->mmc3_profile);
  1644. return 0;
  1645. }
  1646. /*
  1647. * for disc type 0xff we should probably reserve a new track.
  1648. * but i'm not sure, should we leave this to user apps? probably.
  1649. */
  1650. if (di->disc_type == 0xff) {
  1651. printk(DRIVER_NAME": Unknown disc. No track?\n");
  1652. return 0;
  1653. }
  1654. if (di->disc_type != 0x20 && di->disc_type != 0) {
  1655. printk(DRIVER_NAME": Wrong disc type (%x)\n", di->disc_type);
  1656. return 0;
  1657. }
  1658. if (di->erasable == 0) {
  1659. printk(DRIVER_NAME": Disc not erasable\n");
  1660. return 0;
  1661. }
  1662. if (di->border_status == PACKET_SESSION_RESERVED) {
  1663. printk(DRIVER_NAME": Can't write to last track (reserved)\n");
  1664. return 0;
  1665. }
  1666. return 1;
  1667. }
  1668. static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
  1669. {
  1670. struct packet_command cgc;
  1671. unsigned char buf[12];
  1672. disc_information di;
  1673. track_information ti;
  1674. int ret, track;
  1675. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
  1676. cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
  1677. cgc.cmd[8] = 8;
  1678. ret = pkt_generic_packet(pd, &cgc);
  1679. pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
  1680. memset(&di, 0, sizeof(disc_information));
  1681. memset(&ti, 0, sizeof(track_information));
  1682. if ((ret = pkt_get_disc_info(pd, &di))) {
  1683. printk("failed get_disc\n");
  1684. return ret;
  1685. }
  1686. if (!pkt_writable_disc(pd, &di))
  1687. return -EROFS;
  1688. pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
  1689. track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
  1690. if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
  1691. printk(DRIVER_NAME": failed get_track\n");
  1692. return ret;
  1693. }
  1694. if (!pkt_writable_track(pd, &ti)) {
  1695. printk(DRIVER_NAME": can't write to this track\n");
  1696. return -EROFS;
  1697. }
  1698. /*
  1699. * we keep packet size in 512 byte units, makes it easier to
  1700. * deal with request calculations.
  1701. */
  1702. pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
  1703. if (pd->settings.size == 0) {
  1704. printk(DRIVER_NAME": detected zero packet size!\n");
  1705. return -ENXIO;
  1706. }
  1707. if (pd->settings.size > PACKET_MAX_SECTORS) {
  1708. printk(DRIVER_NAME": packet size is too big\n");
  1709. return -EROFS;
  1710. }
  1711. pd->settings.fp = ti.fp;
  1712. pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
  1713. if (ti.nwa_v) {
  1714. pd->nwa = be32_to_cpu(ti.next_writable);
  1715. set_bit(PACKET_NWA_VALID, &pd->flags);
  1716. }
  1717. /*
  1718. * in theory we could use lra on -RW media as well and just zero
  1719. * blocks that haven't been written yet, but in practice that
  1720. * is just a no-go. we'll use that for -R, naturally.
  1721. */
  1722. if (ti.lra_v) {
  1723. pd->lra = be32_to_cpu(ti.last_rec_address);
  1724. set_bit(PACKET_LRA_VALID, &pd->flags);
  1725. } else {
  1726. pd->lra = 0xffffffff;
  1727. set_bit(PACKET_LRA_VALID, &pd->flags);
  1728. }
  1729. /*
  1730. * fine for now
  1731. */
  1732. pd->settings.link_loss = 7;
  1733. pd->settings.write_type = 0; /* packet */
  1734. pd->settings.track_mode = ti.track_mode;
  1735. /*
  1736. * mode1 or mode2 disc
  1737. */
  1738. switch (ti.data_mode) {
  1739. case PACKET_MODE1:
  1740. pd->settings.block_mode = PACKET_BLOCK_MODE1;
  1741. break;
  1742. case PACKET_MODE2:
  1743. pd->settings.block_mode = PACKET_BLOCK_MODE2;
  1744. break;
  1745. default:
  1746. printk(DRIVER_NAME": unknown data mode\n");
  1747. return -EROFS;
  1748. }
  1749. return 0;
  1750. }
  1751. /*
  1752. * enable/disable write caching on drive
  1753. */
  1754. static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
  1755. int set)
  1756. {
  1757. struct packet_command cgc;
  1758. struct request_sense sense;
  1759. unsigned char buf[64];
  1760. int ret;
  1761. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
  1762. cgc.sense = &sense;
  1763. cgc.buflen = pd->mode_offset + 12;
  1764. /*
  1765. * caching mode page might not be there, so quiet this command
  1766. */
  1767. cgc.quiet = 1;
  1768. if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
  1769. return ret;
  1770. buf[pd->mode_offset + 10] |= (!!set << 2);
  1771. cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
  1772. ret = pkt_mode_select(pd, &cgc);
  1773. if (ret) {
  1774. printk(DRIVER_NAME": write caching control failed\n");
  1775. pkt_dump_sense(&cgc);
  1776. } else if (!ret && set)
  1777. printk(DRIVER_NAME": enabled write caching on %s\n", pd->name);
  1778. return ret;
  1779. }
  1780. static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
  1781. {
  1782. struct packet_command cgc;
  1783. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  1784. cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  1785. cgc.cmd[4] = lockflag ? 1 : 0;
  1786. return pkt_generic_packet(pd, &cgc);
  1787. }
  1788. /*
  1789. * Returns drive maximum write speed
  1790. */
  1791. static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
  1792. unsigned *write_speed)
  1793. {
  1794. struct packet_command cgc;
  1795. struct request_sense sense;
  1796. unsigned char buf[256+18];
  1797. unsigned char *cap_buf;
  1798. int ret, offset;
  1799. cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
  1800. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
  1801. cgc.sense = &sense;
  1802. ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  1803. if (ret) {
  1804. cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
  1805. sizeof(struct mode_page_header);
  1806. ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  1807. if (ret) {
  1808. pkt_dump_sense(&cgc);
  1809. return ret;
  1810. }
  1811. }
  1812. offset = 20; /* Obsoleted field, used by older drives */
  1813. if (cap_buf[1] >= 28)
  1814. offset = 28; /* Current write speed selected */
  1815. if (cap_buf[1] >= 30) {
  1816. /* If the drive reports at least one "Logical Unit Write
  1817. * Speed Performance Descriptor Block", use the information
  1818. * in the first block. (contains the highest speed)
  1819. */
  1820. int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
  1821. if (num_spdb > 0)
  1822. offset = 34;
  1823. }
  1824. *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
  1825. return 0;
  1826. }
  1827. /* These tables from cdrecord - I don't have orange book */
  1828. /* standard speed CD-RW (1-4x) */
  1829. static char clv_to_speed[16] = {
  1830. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1831. 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1832. };
  1833. /* high speed CD-RW (-10x) */
  1834. static char hs_clv_to_speed[16] = {
  1835. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1836. 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1837. };
  1838. /* ultra high speed CD-RW */
  1839. static char us_clv_to_speed[16] = {
  1840. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1841. 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
  1842. };
  1843. /*
  1844. * reads the maximum media speed from ATIP
  1845. */
  1846. static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
  1847. unsigned *speed)
  1848. {
  1849. struct packet_command cgc;
  1850. struct request_sense sense;
  1851. unsigned char buf[64];
  1852. unsigned int size, st, sp;
  1853. int ret;
  1854. init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
  1855. cgc.sense = &sense;
  1856. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  1857. cgc.cmd[1] = 2;
  1858. cgc.cmd[2] = 4; /* READ ATIP */
  1859. cgc.cmd[8] = 2;
  1860. ret = pkt_generic_packet(pd, &cgc);
  1861. if (ret) {
  1862. pkt_dump_sense(&cgc);
  1863. return ret;
  1864. }
  1865. size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
  1866. if (size > sizeof(buf))
  1867. size = sizeof(buf);
  1868. init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
  1869. cgc.sense = &sense;
  1870. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  1871. cgc.cmd[1] = 2;
  1872. cgc.cmd[2] = 4;
  1873. cgc.cmd[8] = size;
  1874. ret = pkt_generic_packet(pd, &cgc);
  1875. if (ret) {
  1876. pkt_dump_sense(&cgc);
  1877. return ret;
  1878. }
  1879. if (!(buf[6] & 0x40)) {
  1880. printk(DRIVER_NAME": Disc type is not CD-RW\n");
  1881. return 1;
  1882. }
  1883. if (!(buf[6] & 0x4)) {
  1884. printk(DRIVER_NAME": A1 values on media are not valid, maybe not CDRW?\n");
  1885. return 1;
  1886. }
  1887. st = (buf[6] >> 3) & 0x7; /* disc sub-type */
  1888. sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
  1889. /* Info from cdrecord */
  1890. switch (st) {
  1891. case 0: /* standard speed */
  1892. *speed = clv_to_speed[sp];
  1893. break;
  1894. case 1: /* high speed */
  1895. *speed = hs_clv_to_speed[sp];
  1896. break;
  1897. case 2: /* ultra high speed */
  1898. *speed = us_clv_to_speed[sp];
  1899. break;
  1900. default:
  1901. printk(DRIVER_NAME": Unknown disc sub-type %d\n",st);
  1902. return 1;
  1903. }
  1904. if (*speed) {
  1905. printk(DRIVER_NAME": Max. media speed: %d\n",*speed);
  1906. return 0;
  1907. } else {
  1908. printk(DRIVER_NAME": Unknown speed %d for sub-type %d\n",sp,st);
  1909. return 1;
  1910. }
  1911. }
  1912. static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
  1913. {
  1914. struct packet_command cgc;
  1915. struct request_sense sense;
  1916. int ret;
  1917. VPRINTK(DRIVER_NAME": Performing OPC\n");
  1918. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  1919. cgc.sense = &sense;
  1920. cgc.timeout = 60*HZ;
  1921. cgc.cmd[0] = GPCMD_SEND_OPC;
  1922. cgc.cmd[1] = 1;
  1923. if ((ret = pkt_generic_packet(pd, &cgc)))
  1924. pkt_dump_sense(&cgc);
  1925. return ret;
  1926. }
  1927. static int pkt_open_write(struct pktcdvd_device *pd)
  1928. {
  1929. int ret;
  1930. unsigned int write_speed, media_write_speed, read_speed;
  1931. if ((ret = pkt_probe_settings(pd))) {
  1932. VPRINTK(DRIVER_NAME": %s failed probe\n", pd->name);
  1933. return ret;
  1934. }
  1935. if ((ret = pkt_set_write_settings(pd))) {
  1936. DPRINTK(DRIVER_NAME": %s failed saving write settings\n", pd->name);
  1937. return -EIO;
  1938. }
  1939. pkt_write_caching(pd, USE_WCACHING);
  1940. if ((ret = pkt_get_max_speed(pd, &write_speed)))
  1941. write_speed = 16 * 177;
  1942. switch (pd->mmc3_profile) {
  1943. case 0x13: /* DVD-RW */
  1944. case 0x1a: /* DVD+RW */
  1945. case 0x12: /* DVD-RAM */
  1946. DPRINTK(DRIVER_NAME": write speed %ukB/s\n", write_speed);
  1947. break;
  1948. default:
  1949. if ((ret = pkt_media_speed(pd, &media_write_speed)))
  1950. media_write_speed = 16;
  1951. write_speed = min(write_speed, media_write_speed * 177);
  1952. DPRINTK(DRIVER_NAME": write speed %ux\n", write_speed / 176);
  1953. break;
  1954. }
  1955. read_speed = write_speed;
  1956. if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
  1957. DPRINTK(DRIVER_NAME": %s couldn't set write speed\n", pd->name);
  1958. return -EIO;
  1959. }
  1960. pd->write_speed = write_speed;
  1961. pd->read_speed = read_speed;
  1962. if ((ret = pkt_perform_opc(pd))) {
  1963. DPRINTK(DRIVER_NAME": %s Optimum Power Calibration failed\n", pd->name);
  1964. }
  1965. return 0;
  1966. }
  1967. /*
  1968. * called at open time.
  1969. */
  1970. static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
  1971. {
  1972. int ret;
  1973. long lba;
  1974. struct request_queue *q;
  1975. /*
  1976. * We need to re-open the cdrom device without O_NONBLOCK to be able
  1977. * to read/write from/to it. It is already opened in O_NONBLOCK mode
  1978. * so bdget() can't fail.
  1979. */
  1980. bdget(pd->bdev->bd_dev);
  1981. if ((ret = blkdev_get(pd->bdev, FMODE_READ)))
  1982. goto out;
  1983. if ((ret = bd_claim(pd->bdev, pd)))
  1984. goto out_putdev;
  1985. if ((ret = pkt_get_last_written(pd, &lba))) {
  1986. printk(DRIVER_NAME": pkt_get_last_written failed\n");
  1987. goto out_unclaim;
  1988. }
  1989. set_capacity(pd->disk, lba << 2);
  1990. set_capacity(pd->bdev->bd_disk, lba << 2);
  1991. bd_set_size(pd->bdev, (loff_t)lba << 11);
  1992. q = bdev_get_queue(pd->bdev);
  1993. if (write) {
  1994. if ((ret = pkt_open_write(pd)))
  1995. goto out_unclaim;
  1996. /*
  1997. * Some CDRW drives can not handle writes larger than one packet,
  1998. * even if the size is a multiple of the packet size.
  1999. */
  2000. spin_lock_irq(q->queue_lock);
  2001. blk_queue_max_hw_sectors(q, pd->settings.size);
  2002. spin_unlock_irq(q->queue_lock);
  2003. set_bit(PACKET_WRITABLE, &pd->flags);
  2004. } else {
  2005. pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
  2006. clear_bit(PACKET_WRITABLE, &pd->flags);
  2007. }
  2008. if ((ret = pkt_set_segment_merging(pd, q)))
  2009. goto out_unclaim;
  2010. if (write) {
  2011. if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
  2012. printk(DRIVER_NAME": not enough memory for buffers\n");
  2013. ret = -ENOMEM;
  2014. goto out_unclaim;
  2015. }
  2016. printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
  2017. }
  2018. return 0;
  2019. out_unclaim:
  2020. bd_release(pd->bdev);
  2021. out_putdev:
  2022. blkdev_put(pd->bdev, FMODE_READ);
  2023. out:
  2024. return ret;
  2025. }
  2026. /*
  2027. * called when the device is closed. makes sure that the device flushes
  2028. * the internal cache before we close.
  2029. */
  2030. static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
  2031. {
  2032. if (flush && pkt_flush_cache(pd))
  2033. DPRINTK(DRIVER_NAME": %s not flushing cache\n", pd->name);
  2034. pkt_lock_door(pd, 0);
  2035. pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
  2036. bd_release(pd->bdev);
  2037. blkdev_put(pd->bdev, FMODE_READ);
  2038. pkt_shrink_pktlist(pd);
  2039. }
  2040. static struct pktcdvd_device *pkt_find_dev_from_minor(unsigned int dev_minor)
  2041. {
  2042. if (dev_minor >= MAX_WRITERS)
  2043. return NULL;
  2044. return pkt_devs[dev_minor];
  2045. }
  2046. static int pkt_open(struct block_device *bdev, fmode_t mode)
  2047. {
  2048. struct pktcdvd_device *pd = NULL;
  2049. int ret;
  2050. VPRINTK(DRIVER_NAME": entering open\n");
  2051. mutex_lock(&pktcdvd_mutex);
  2052. mutex_lock(&ctl_mutex);
  2053. pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev));
  2054. if (!pd) {
  2055. ret = -ENODEV;
  2056. goto out;
  2057. }
  2058. BUG_ON(pd->refcnt < 0);
  2059. pd->refcnt++;
  2060. if (pd->refcnt > 1) {
  2061. if ((mode & FMODE_WRITE) &&
  2062. !test_bit(PACKET_WRITABLE, &pd->flags)) {
  2063. ret = -EBUSY;
  2064. goto out_dec;
  2065. }
  2066. } else {
  2067. ret = pkt_open_dev(pd, mode & FMODE_WRITE);
  2068. if (ret)
  2069. goto out_dec;
  2070. /*
  2071. * needed here as well, since ext2 (among others) may change
  2072. * the blocksize at mount time
  2073. */
  2074. set_blocksize(bdev, CD_FRAMESIZE);
  2075. }
  2076. mutex_unlock(&ctl_mutex);
  2077. mutex_unlock(&pktcdvd_mutex);
  2078. return 0;
  2079. out_dec:
  2080. pd->refcnt--;
  2081. out:
  2082. VPRINTK(DRIVER_NAME": failed open (%d)\n", ret);
  2083. mutex_unlock(&ctl_mutex);
  2084. mutex_unlock(&pktcdvd_mutex);
  2085. return ret;
  2086. }
  2087. static int pkt_close(struct gendisk *disk, fmode_t mode)
  2088. {
  2089. struct pktcdvd_device *pd = disk->private_data;
  2090. int ret = 0;
  2091. mutex_lock(&pktcdvd_mutex);
  2092. mutex_lock(&ctl_mutex);
  2093. pd->refcnt--;
  2094. BUG_ON(pd->refcnt < 0);
  2095. if (pd->refcnt == 0) {
  2096. int flush = test_bit(PACKET_WRITABLE, &pd->flags);
  2097. pkt_release_dev(pd, flush);
  2098. }
  2099. mutex_unlock(&ctl_mutex);
  2100. mutex_unlock(&pktcdvd_mutex);
  2101. return ret;
  2102. }
  2103. static void pkt_end_io_read_cloned(struct bio *bio, int err)
  2104. {
  2105. struct packet_stacked_data *psd = bio->bi_private;
  2106. struct pktcdvd_device *pd = psd->pd;
  2107. bio_put(bio);
  2108. bio_endio(psd->bio, err);
  2109. mempool_free(psd, psd_pool);
  2110. pkt_bio_finished(pd);
  2111. }
  2112. static int pkt_make_request(struct request_queue *q, struct bio *bio)
  2113. {
  2114. struct pktcdvd_device *pd;
  2115. char b[BDEVNAME_SIZE];
  2116. sector_t zone;
  2117. struct packet_data *pkt;
  2118. int was_empty, blocked_bio;
  2119. struct pkt_rb_node *node;
  2120. pd = q->queuedata;
  2121. if (!pd) {
  2122. printk(DRIVER_NAME": %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
  2123. goto end_io;
  2124. }
  2125. /*
  2126. * Clone READ bios so we can have our own bi_end_io callback.
  2127. */
  2128. if (bio_data_dir(bio) == READ) {
  2129. struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
  2130. struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
  2131. psd->pd = pd;
  2132. psd->bio = bio;
  2133. cloned_bio->bi_bdev = pd->bdev;
  2134. cloned_bio->bi_private = psd;
  2135. cloned_bio->bi_end_io = pkt_end_io_read_cloned;
  2136. pd->stats.secs_r += bio->bi_size >> 9;
  2137. pkt_queue_bio(pd, cloned_bio);
  2138. return 0;
  2139. }
  2140. if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
  2141. printk(DRIVER_NAME": WRITE for ro device %s (%llu)\n",
  2142. pd->name, (unsigned long long)bio->bi_sector);
  2143. goto end_io;
  2144. }
  2145. if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
  2146. printk(DRIVER_NAME": wrong bio size\n");
  2147. goto end_io;
  2148. }
  2149. blk_queue_bounce(q, &bio);
  2150. zone = ZONE(bio->bi_sector, pd);
  2151. VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
  2152. (unsigned long long)bio->bi_sector,
  2153. (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
  2154. /* Check if we have to split the bio */
  2155. {
  2156. struct bio_pair *bp;
  2157. sector_t last_zone;
  2158. int first_sectors;
  2159. last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
  2160. if (last_zone != zone) {
  2161. BUG_ON(last_zone != zone + pd->settings.size);
  2162. first_sectors = last_zone - bio->bi_sector;
  2163. bp = bio_split(bio, first_sectors);
  2164. BUG_ON(!bp);
  2165. pkt_make_request(q, &bp->bio1);
  2166. pkt_make_request(q, &bp->bio2);
  2167. bio_pair_release(bp);
  2168. return 0;
  2169. }
  2170. }
  2171. /*
  2172. * If we find a matching packet in state WAITING or READ_WAIT, we can
  2173. * just append this bio to that packet.
  2174. */
  2175. spin_lock(&pd->cdrw.active_list_lock);
  2176. blocked_bio = 0;
  2177. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  2178. if (pkt->sector == zone) {
  2179. spin_lock(&pkt->lock);
  2180. if ((pkt->state == PACKET_WAITING_STATE) ||
  2181. (pkt->state == PACKET_READ_WAIT_STATE)) {
  2182. bio_list_add(&pkt->orig_bios, bio);
  2183. pkt->write_size += bio->bi_size / CD_FRAMESIZE;
  2184. if ((pkt->write_size >= pkt->frames) &&
  2185. (pkt->state == PACKET_WAITING_STATE)) {
  2186. atomic_inc(&pkt->run_sm);
  2187. wake_up(&pd->wqueue);
  2188. }
  2189. spin_unlock(&pkt->lock);
  2190. spin_unlock(&pd->cdrw.active_list_lock);
  2191. return 0;
  2192. } else {
  2193. blocked_bio = 1;
  2194. }
  2195. spin_unlock(&pkt->lock);
  2196. }
  2197. }
  2198. spin_unlock(&pd->cdrw.active_list_lock);
  2199. /*
  2200. * Test if there is enough room left in the bio work queue
  2201. * (queue size >= congestion on mark).
  2202. * If not, wait till the work queue size is below the congestion off mark.
  2203. */
  2204. spin_lock(&pd->lock);
  2205. if (pd->write_congestion_on > 0
  2206. && pd->bio_queue_size >= pd->write_congestion_on) {
  2207. set_bdi_congested(&q->backing_dev_info, BLK_RW_ASYNC);
  2208. do {
  2209. spin_unlock(&pd->lock);
  2210. congestion_wait(BLK_RW_ASYNC, HZ);
  2211. spin_lock(&pd->lock);
  2212. } while(pd->bio_queue_size > pd->write_congestion_off);
  2213. }
  2214. spin_unlock(&pd->lock);
  2215. /*
  2216. * No matching packet found. Store the bio in the work queue.
  2217. */
  2218. node = mempool_alloc(pd->rb_pool, GFP_NOIO);
  2219. node->bio = bio;
  2220. spin_lock(&pd->lock);
  2221. BUG_ON(pd->bio_queue_size < 0);
  2222. was_empty = (pd->bio_queue_size == 0);
  2223. pkt_rbtree_insert(pd, node);
  2224. spin_unlock(&pd->lock);
  2225. /*
  2226. * Wake up the worker thread.
  2227. */
  2228. atomic_set(&pd->scan_queue, 1);
  2229. if (was_empty) {
  2230. /* This wake_up is required for correct operation */
  2231. wake_up(&pd->wqueue);
  2232. } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
  2233. /*
  2234. * This wake up is not required for correct operation,
  2235. * but improves performance in some cases.
  2236. */
  2237. wake_up(&pd->wqueue);
  2238. }
  2239. return 0;
  2240. end_io:
  2241. bio_io_error(bio);
  2242. return 0;
  2243. }
  2244. static int pkt_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
  2245. struct bio_vec *bvec)
  2246. {
  2247. struct pktcdvd_device *pd = q->queuedata;
  2248. sector_t zone = ZONE(bmd->bi_sector, pd);
  2249. int used = ((bmd->bi_sector - zone) << 9) + bmd->bi_size;
  2250. int remaining = (pd->settings.size << 9) - used;
  2251. int remaining2;
  2252. /*
  2253. * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
  2254. * boundary, pkt_make_request() will split the bio.
  2255. */
  2256. remaining2 = PAGE_SIZE - bmd->bi_size;
  2257. remaining = max(remaining, remaining2);
  2258. BUG_ON(remaining < 0);
  2259. return remaining;
  2260. }
  2261. static void pkt_init_queue(struct pktcdvd_device *pd)
  2262. {
  2263. struct request_queue *q = pd->disk->queue;
  2264. blk_queue_make_request(q, pkt_make_request);
  2265. blk_queue_logical_block_size(q, CD_FRAMESIZE);
  2266. blk_queue_max_hw_sectors(q, PACKET_MAX_SECTORS);
  2267. blk_queue_merge_bvec(q, pkt_merge_bvec);
  2268. q->queuedata = pd;
  2269. }
  2270. static int pkt_seq_show(struct seq_file *m, void *p)
  2271. {
  2272. struct pktcdvd_device *pd = m->private;
  2273. char *msg;
  2274. char bdev_buf[BDEVNAME_SIZE];
  2275. int states[PACKET_NUM_STATES];
  2276. seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
  2277. bdevname(pd->bdev, bdev_buf));
  2278. seq_printf(m, "\nSettings:\n");
  2279. seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
  2280. if (pd->settings.write_type == 0)
  2281. msg = "Packet";
  2282. else
  2283. msg = "Unknown";
  2284. seq_printf(m, "\twrite type:\t\t%s\n", msg);
  2285. seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
  2286. seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
  2287. seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
  2288. if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
  2289. msg = "Mode 1";
  2290. else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
  2291. msg = "Mode 2";
  2292. else
  2293. msg = "Unknown";
  2294. seq_printf(m, "\tblock mode:\t\t%s\n", msg);
  2295. seq_printf(m, "\nStatistics:\n");
  2296. seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
  2297. seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
  2298. seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
  2299. seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
  2300. seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
  2301. seq_printf(m, "\nMisc:\n");
  2302. seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
  2303. seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
  2304. seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
  2305. seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
  2306. seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
  2307. seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
  2308. seq_printf(m, "\nQueue state:\n");
  2309. seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
  2310. seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
  2311. seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
  2312. pkt_count_states(pd, states);
  2313. seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
  2314. states[0], states[1], states[2], states[3], states[4], states[5]);
  2315. seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
  2316. pd->write_congestion_off,
  2317. pd->write_congestion_on);
  2318. return 0;
  2319. }
  2320. static int pkt_seq_open(struct inode *inode, struct file *file)
  2321. {
  2322. return single_open(file, pkt_seq_show, PDE(inode)->data);
  2323. }
  2324. static const struct file_operations pkt_proc_fops = {
  2325. .open = pkt_seq_open,
  2326. .read = seq_read,
  2327. .llseek = seq_lseek,
  2328. .release = single_release
  2329. };
  2330. static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
  2331. {
  2332. int i;
  2333. int ret = 0;
  2334. char b[BDEVNAME_SIZE];
  2335. struct block_device *bdev;
  2336. if (pd->pkt_dev == dev) {
  2337. printk(DRIVER_NAME": Recursive setup not allowed\n");
  2338. return -EBUSY;
  2339. }
  2340. for (i = 0; i < MAX_WRITERS; i++) {
  2341. struct pktcdvd_device *pd2 = pkt_devs[i];
  2342. if (!pd2)
  2343. continue;
  2344. if (pd2->bdev->bd_dev == dev) {
  2345. printk(DRIVER_NAME": %s already setup\n", bdevname(pd2->bdev, b));
  2346. return -EBUSY;
  2347. }
  2348. if (pd2->pkt_dev == dev) {
  2349. printk(DRIVER_NAME": Can't chain pktcdvd devices\n");
  2350. return -EBUSY;
  2351. }
  2352. }
  2353. bdev = bdget(dev);
  2354. if (!bdev)
  2355. return -ENOMEM;
  2356. ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY);
  2357. if (ret)
  2358. return ret;
  2359. /* This is safe, since we have a reference from open(). */
  2360. __module_get(THIS_MODULE);
  2361. pd->bdev = bdev;
  2362. set_blocksize(bdev, CD_FRAMESIZE);
  2363. pkt_init_queue(pd);
  2364. atomic_set(&pd->cdrw.pending_bios, 0);
  2365. pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
  2366. if (IS_ERR(pd->cdrw.thread)) {
  2367. printk(DRIVER_NAME": can't start kernel thread\n");
  2368. ret = -ENOMEM;
  2369. goto out_mem;
  2370. }
  2371. proc_create_data(pd->name, 0, pkt_proc, &pkt_proc_fops, pd);
  2372. DPRINTK(DRIVER_NAME": writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
  2373. return 0;
  2374. out_mem:
  2375. blkdev_put(bdev, FMODE_READ | FMODE_NDELAY);
  2376. /* This is safe: open() is still holding a reference. */
  2377. module_put(THIS_MODULE);
  2378. return ret;
  2379. }
  2380. static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
  2381. {
  2382. struct pktcdvd_device *pd = bdev->bd_disk->private_data;
  2383. int ret;
  2384. VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd,
  2385. MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
  2386. mutex_lock(&pktcdvd_mutex);
  2387. switch (cmd) {
  2388. case CDROMEJECT:
  2389. /*
  2390. * The door gets locked when the device is opened, so we
  2391. * have to unlock it or else the eject command fails.
  2392. */
  2393. if (pd->refcnt == 1)
  2394. pkt_lock_door(pd, 0);
  2395. /* fallthru */
  2396. /*
  2397. * forward selected CDROM ioctls to CD-ROM, for UDF
  2398. */
  2399. case CDROMMULTISESSION:
  2400. case CDROMREADTOCENTRY:
  2401. case CDROM_LAST_WRITTEN:
  2402. case CDROM_SEND_PACKET:
  2403. case SCSI_IOCTL_SEND_COMMAND:
  2404. ret = __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg);
  2405. break;
  2406. default:
  2407. VPRINTK(DRIVER_NAME": Unknown ioctl for %s (%x)\n", pd->name, cmd);
  2408. ret = -ENOTTY;
  2409. }
  2410. mutex_unlock(&pktcdvd_mutex);
  2411. return ret;
  2412. }
  2413. static int pkt_media_changed(struct gendisk *disk)
  2414. {
  2415. struct pktcdvd_device *pd = disk->private_data;
  2416. struct gendisk *attached_disk;
  2417. if (!pd)
  2418. return 0;
  2419. if (!pd->bdev)
  2420. return 0;
  2421. attached_disk = pd->bdev->bd_disk;
  2422. if (!attached_disk)
  2423. return 0;
  2424. return attached_disk->fops->media_changed(attached_disk);
  2425. }
  2426. static const struct block_device_operations pktcdvd_ops = {
  2427. .owner = THIS_MODULE,
  2428. .open = pkt_open,
  2429. .release = pkt_close,
  2430. .ioctl = pkt_ioctl,
  2431. .media_changed = pkt_media_changed,
  2432. };
  2433. static char *pktcdvd_devnode(struct gendisk *gd, mode_t *mode)
  2434. {
  2435. return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name);
  2436. }
  2437. /*
  2438. * Set up mapping from pktcdvd device to CD-ROM device.
  2439. */
  2440. static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
  2441. {
  2442. int idx;
  2443. int ret = -ENOMEM;
  2444. struct pktcdvd_device *pd;
  2445. struct gendisk *disk;
  2446. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2447. for (idx = 0; idx < MAX_WRITERS; idx++)
  2448. if (!pkt_devs[idx])
  2449. break;
  2450. if (idx == MAX_WRITERS) {
  2451. printk(DRIVER_NAME": max %d writers supported\n", MAX_WRITERS);
  2452. ret = -EBUSY;
  2453. goto out_mutex;
  2454. }
  2455. pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
  2456. if (!pd)
  2457. goto out_mutex;
  2458. pd->rb_pool = mempool_create_kmalloc_pool(PKT_RB_POOL_SIZE,
  2459. sizeof(struct pkt_rb_node));
  2460. if (!pd->rb_pool)
  2461. goto out_mem;
  2462. INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
  2463. INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
  2464. spin_lock_init(&pd->cdrw.active_list_lock);
  2465. spin_lock_init(&pd->lock);
  2466. spin_lock_init(&pd->iosched.lock);
  2467. bio_list_init(&pd->iosched.read_queue);
  2468. bio_list_init(&pd->iosched.write_queue);
  2469. sprintf(pd->name, DRIVER_NAME"%d", idx);
  2470. init_waitqueue_head(&pd->wqueue);
  2471. pd->bio_queue = RB_ROOT;
  2472. pd->write_congestion_on = write_congestion_on;
  2473. pd->write_congestion_off = write_congestion_off;
  2474. disk = alloc_disk(1);
  2475. if (!disk)
  2476. goto out_mem;
  2477. pd->disk = disk;
  2478. disk->major = pktdev_major;
  2479. disk->first_minor = idx;
  2480. disk->fops = &pktcdvd_ops;
  2481. disk->flags = GENHD_FL_REMOVABLE;
  2482. strcpy(disk->disk_name, pd->name);
  2483. disk->devnode = pktcdvd_devnode;
  2484. disk->private_data = pd;
  2485. disk->queue = blk_alloc_queue(GFP_KERNEL);
  2486. if (!disk->queue)
  2487. goto out_mem2;
  2488. pd->pkt_dev = MKDEV(pktdev_major, idx);
  2489. ret = pkt_new_dev(pd, dev);
  2490. if (ret)
  2491. goto out_new_dev;
  2492. add_disk(disk);
  2493. pkt_sysfs_dev_new(pd);
  2494. pkt_debugfs_dev_new(pd);
  2495. pkt_devs[idx] = pd;
  2496. if (pkt_dev)
  2497. *pkt_dev = pd->pkt_dev;
  2498. mutex_unlock(&ctl_mutex);
  2499. return 0;
  2500. out_new_dev:
  2501. blk_cleanup_queue(disk->queue);
  2502. out_mem2:
  2503. put_disk(disk);
  2504. out_mem:
  2505. if (pd->rb_pool)
  2506. mempool_destroy(pd->rb_pool);
  2507. kfree(pd);
  2508. out_mutex:
  2509. mutex_unlock(&ctl_mutex);
  2510. printk(DRIVER_NAME": setup of pktcdvd device failed\n");
  2511. return ret;
  2512. }
  2513. /*
  2514. * Tear down mapping from pktcdvd device to CD-ROM device.
  2515. */
  2516. static int pkt_remove_dev(dev_t pkt_dev)
  2517. {
  2518. struct pktcdvd_device *pd;
  2519. int idx;
  2520. int ret = 0;
  2521. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2522. for (idx = 0; idx < MAX_WRITERS; idx++) {
  2523. pd = pkt_devs[idx];
  2524. if (pd && (pd->pkt_dev == pkt_dev))
  2525. break;
  2526. }
  2527. if (idx == MAX_WRITERS) {
  2528. DPRINTK(DRIVER_NAME": dev not setup\n");
  2529. ret = -ENXIO;
  2530. goto out;
  2531. }
  2532. if (pd->refcnt > 0) {
  2533. ret = -EBUSY;
  2534. goto out;
  2535. }
  2536. if (!IS_ERR(pd->cdrw.thread))
  2537. kthread_stop(pd->cdrw.thread);
  2538. pkt_devs[idx] = NULL;
  2539. pkt_debugfs_dev_remove(pd);
  2540. pkt_sysfs_dev_remove(pd);
  2541. blkdev_put(pd->bdev, FMODE_READ | FMODE_NDELAY);
  2542. remove_proc_entry(pd->name, pkt_proc);
  2543. DPRINTK(DRIVER_NAME": writer %s unmapped\n", pd->name);
  2544. del_gendisk(pd->disk);
  2545. blk_cleanup_queue(pd->disk->queue);
  2546. put_disk(pd->disk);
  2547. mempool_destroy(pd->rb_pool);
  2548. kfree(pd);
  2549. /* This is safe: open() is still holding a reference. */
  2550. module_put(THIS_MODULE);
  2551. out:
  2552. mutex_unlock(&ctl_mutex);
  2553. return ret;
  2554. }
  2555. static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
  2556. {
  2557. struct pktcdvd_device *pd;
  2558. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2559. pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
  2560. if (pd) {
  2561. ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
  2562. ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
  2563. } else {
  2564. ctrl_cmd->dev = 0;
  2565. ctrl_cmd->pkt_dev = 0;
  2566. }
  2567. ctrl_cmd->num_devices = MAX_WRITERS;
  2568. mutex_unlock(&ctl_mutex);
  2569. }
  2570. static long pkt_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2571. {
  2572. void __user *argp = (void __user *)arg;
  2573. struct pkt_ctrl_command ctrl_cmd;
  2574. int ret = 0;
  2575. dev_t pkt_dev = 0;
  2576. if (cmd != PACKET_CTRL_CMD)
  2577. return -ENOTTY;
  2578. if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
  2579. return -EFAULT;
  2580. switch (ctrl_cmd.command) {
  2581. case PKT_CTRL_CMD_SETUP:
  2582. if (!capable(CAP_SYS_ADMIN))
  2583. return -EPERM;
  2584. ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
  2585. ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
  2586. break;
  2587. case PKT_CTRL_CMD_TEARDOWN:
  2588. if (!capable(CAP_SYS_ADMIN))
  2589. return -EPERM;
  2590. ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
  2591. break;
  2592. case PKT_CTRL_CMD_STATUS:
  2593. pkt_get_status(&ctrl_cmd);
  2594. break;
  2595. default:
  2596. return -ENOTTY;
  2597. }
  2598. if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
  2599. return -EFAULT;
  2600. return ret;
  2601. }
  2602. #ifdef CONFIG_COMPAT
  2603. static long pkt_ctl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2604. {
  2605. return pkt_ctl_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  2606. }
  2607. #endif
  2608. static const struct file_operations pkt_ctl_fops = {
  2609. .open = nonseekable_open,
  2610. .unlocked_ioctl = pkt_ctl_ioctl,
  2611. #ifdef CONFIG_COMPAT
  2612. .compat_ioctl = pkt_ctl_compat_ioctl,
  2613. #endif
  2614. .owner = THIS_MODULE,
  2615. .llseek = no_llseek,
  2616. };
  2617. static struct miscdevice pkt_misc = {
  2618. .minor = MISC_DYNAMIC_MINOR,
  2619. .name = DRIVER_NAME,
  2620. .nodename = "pktcdvd/control",
  2621. .fops = &pkt_ctl_fops
  2622. };
  2623. static int __init pkt_init(void)
  2624. {
  2625. int ret;
  2626. mutex_init(&ctl_mutex);
  2627. psd_pool = mempool_create_kmalloc_pool(PSD_POOL_SIZE,
  2628. sizeof(struct packet_stacked_data));
  2629. if (!psd_pool)
  2630. return -ENOMEM;
  2631. ret = register_blkdev(pktdev_major, DRIVER_NAME);
  2632. if (ret < 0) {
  2633. printk(DRIVER_NAME": Unable to register block device\n");
  2634. goto out2;
  2635. }
  2636. if (!pktdev_major)
  2637. pktdev_major = ret;
  2638. ret = pkt_sysfs_init();
  2639. if (ret)
  2640. goto out;
  2641. pkt_debugfs_init();
  2642. ret = misc_register(&pkt_misc);
  2643. if (ret) {
  2644. printk(DRIVER_NAME": Unable to register misc device\n");
  2645. goto out_misc;
  2646. }
  2647. pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
  2648. return 0;
  2649. out_misc:
  2650. pkt_debugfs_cleanup();
  2651. pkt_sysfs_cleanup();
  2652. out:
  2653. unregister_blkdev(pktdev_major, DRIVER_NAME);
  2654. out2:
  2655. mempool_destroy(psd_pool);
  2656. return ret;
  2657. }
  2658. static void __exit pkt_exit(void)
  2659. {
  2660. remove_proc_entry("driver/"DRIVER_NAME, NULL);
  2661. misc_deregister(&pkt_misc);
  2662. pkt_debugfs_cleanup();
  2663. pkt_sysfs_cleanup();
  2664. unregister_blkdev(pktdev_major, DRIVER_NAME);
  2665. mempool_destroy(psd_pool);
  2666. }
  2667. MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
  2668. MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
  2669. MODULE_LICENSE("GPL");
  2670. module_init(pkt_init);
  2671. module_exit(pkt_exit);