pktcdvd.c 77 KB

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