pktcdvd.c 76 KB

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