pktcdvd.c 77 KB

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