pktcdvd.c 76 KB

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