pktcdvd.c 76 KB

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