pktcdvd.c 77 KB

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