ch.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SCSI Media Changer device driver for Linux 2.6
  4. *
  5. * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
  6. *
  7. */
  8. #define VERSION "0.25"
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/major.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/completion.h>
  20. #include <linux/compat.h>
  21. #include <linux/chio.h> /* here are all the ioctls */
  22. #include <linux/mutex.h>
  23. #include <linux/idr.h>
  24. #include <linux/slab.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_driver.h>
  28. #include <scsi/scsi_ioctl.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_dbg.h>
  33. #define CH_DT_MAX 16
  34. #define CH_TYPES 8
  35. #define CH_MAX_DEVS 128
  36. MODULE_DESCRIPTION("device driver for scsi media changer devices");
  37. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
  40. MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER);
  41. static int init = 1;
  42. module_param(init, int, 0444);
  43. MODULE_PARM_DESC(init, \
  44. "initialize element status on driver load (default: on)");
  45. static int timeout_move = 300;
  46. module_param(timeout_move, int, 0644);
  47. MODULE_PARM_DESC(timeout_move,"timeout for move commands "
  48. "(default: 300 seconds)");
  49. static int timeout_init = 3600;
  50. module_param(timeout_init, int, 0644);
  51. MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
  52. "(default: 3600 seconds)");
  53. static int verbose = 1;
  54. module_param(verbose, int, 0644);
  55. MODULE_PARM_DESC(verbose,"be verbose (default: on)");
  56. static int debug;
  57. module_param(debug, int, 0644);
  58. MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
  59. "detailed sense codes on scsi errors (default: off)");
  60. static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
  61. static int dt_lun[CH_DT_MAX];
  62. module_param_array(dt_id, int, NULL, 0444);
  63. module_param_array(dt_lun, int, NULL, 0444);
  64. /* tell the driver about vendor-specific slots */
  65. static int vendor_firsts[CH_TYPES-4];
  66. static int vendor_counts[CH_TYPES-4];
  67. module_param_array(vendor_firsts, int, NULL, 0444);
  68. module_param_array(vendor_counts, int, NULL, 0444);
  69. static const char * vendor_labels[CH_TYPES-4] = {
  70. "v0", "v1", "v2", "v3"
  71. };
  72. // module_param_string_array(vendor_labels, NULL, 0444);
  73. #define ch_printk(prefix, ch, fmt, a...) \
  74. sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a)
  75. #define DPRINTK(fmt, arg...) \
  76. do { \
  77. if (debug) \
  78. ch_printk(KERN_DEBUG, ch, fmt, ##arg); \
  79. } while (0)
  80. #define VPRINTK(level, fmt, arg...) \
  81. do { \
  82. if (verbose) \
  83. ch_printk(level, ch, fmt, ##arg); \
  84. } while (0)
  85. /* ------------------------------------------------------------------- */
  86. #define MAX_RETRIES 1
  87. static const struct class ch_sysfs_class = {
  88. .name = "scsi_changer",
  89. };
  90. typedef struct {
  91. struct kref ref;
  92. struct list_head list;
  93. int minor;
  94. char name[8];
  95. struct scsi_device *device;
  96. struct scsi_device **dt; /* ptrs to data transfer elements */
  97. u_int firsts[CH_TYPES];
  98. u_int counts[CH_TYPES];
  99. u_int voltags;
  100. struct mutex lock;
  101. } scsi_changer;
  102. static DEFINE_IDR(ch_index_idr);
  103. static DEFINE_SPINLOCK(ch_index_lock);
  104. static const struct {
  105. unsigned char sense;
  106. unsigned char asc;
  107. unsigned char ascq;
  108. int errno;
  109. } ch_err[] = {
  110. /* Just filled in what looks right. Hav'nt checked any standard paper for
  111. these errno assignments, so they may be wrong... */
  112. {
  113. .sense = ILLEGAL_REQUEST,
  114. .asc = 0x21,
  115. .ascq = 0x01,
  116. .errno = EBADSLT, /* Invalid element address */
  117. },{
  118. .sense = ILLEGAL_REQUEST,
  119. .asc = 0x28,
  120. .ascq = 0x01,
  121. .errno = EBADE, /* Import or export element accessed */
  122. },{
  123. .sense = ILLEGAL_REQUEST,
  124. .asc = 0x3B,
  125. .ascq = 0x0D,
  126. .errno = EXFULL, /* Medium destination element full */
  127. },{
  128. .sense = ILLEGAL_REQUEST,
  129. .asc = 0x3B,
  130. .ascq = 0x0E,
  131. .errno = EBADE, /* Medium source element empty */
  132. },{
  133. .sense = ILLEGAL_REQUEST,
  134. .asc = 0x20,
  135. .ascq = 0x00,
  136. .errno = EBADRQC, /* Invalid command operation code */
  137. },{
  138. /* end of list */
  139. }
  140. };
  141. /* ------------------------------------------------------------------- */
  142. static int ch_find_errno(struct scsi_sense_hdr *sshdr)
  143. {
  144. int i,errno = 0;
  145. /* Check to see if additional sense information is available */
  146. if (scsi_sense_valid(sshdr) &&
  147. sshdr->asc != 0) {
  148. for (i = 0; ch_err[i].errno != 0; i++) {
  149. if (ch_err[i].sense == sshdr->sense_key &&
  150. ch_err[i].asc == sshdr->asc &&
  151. ch_err[i].ascq == sshdr->ascq) {
  152. errno = -ch_err[i].errno;
  153. break;
  154. }
  155. }
  156. }
  157. if (errno == 0)
  158. errno = -EIO;
  159. return errno;
  160. }
  161. static int
  162. ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len,
  163. void *buffer, unsigned int buflength, enum req_op op)
  164. {
  165. int errno = 0, timeout, result;
  166. struct scsi_sense_hdr sshdr;
  167. struct scsi_failure failure_defs[] = {
  168. {
  169. .sense = UNIT_ATTENTION,
  170. .asc = SCMD_FAILURE_ASC_ANY,
  171. .ascq = SCMD_FAILURE_ASCQ_ANY,
  172. .allowed = 3,
  173. .result = SAM_STAT_CHECK_CONDITION,
  174. },
  175. {}
  176. };
  177. struct scsi_failures failures = {
  178. .failure_definitions = failure_defs,
  179. };
  180. const struct scsi_exec_args exec_args = {
  181. .sshdr = &sshdr,
  182. .failures = &failures,
  183. };
  184. timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
  185. ? timeout_init : timeout_move;
  186. result = scsi_execute_cmd(ch->device, cmd, op, buffer, buflength,
  187. timeout * HZ, MAX_RETRIES, &exec_args);
  188. if (result < 0)
  189. return result;
  190. if (scsi_sense_valid(&sshdr)) {
  191. if (debug)
  192. scsi_print_sense_hdr(ch->device, ch->name, &sshdr);
  193. errno = ch_find_errno(&sshdr);
  194. }
  195. return errno;
  196. }
  197. /* ------------------------------------------------------------------------ */
  198. static int
  199. ch_elem_to_typecode(scsi_changer *ch, u_int elem)
  200. {
  201. int i;
  202. for (i = 0; i < CH_TYPES; i++) {
  203. if (elem >= ch->firsts[i] &&
  204. elem < ch->firsts[i] +
  205. ch->counts[i])
  206. return i+1;
  207. }
  208. return 0;
  209. }
  210. static int
  211. ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
  212. {
  213. u_char cmd[12];
  214. u_char *buffer;
  215. int result;
  216. buffer = kmalloc(512, GFP_KERNEL);
  217. if(!buffer)
  218. return -ENOMEM;
  219. retry:
  220. memset(cmd,0,sizeof(cmd));
  221. cmd[0] = READ_ELEMENT_STATUS;
  222. cmd[1] = ((ch->device->lun & 0x7) << 5) |
  223. (ch->voltags ? 0x10 : 0) |
  224. ch_elem_to_typecode(ch,elem);
  225. cmd[2] = (elem >> 8) & 0xff;
  226. cmd[3] = elem & 0xff;
  227. cmd[5] = 1;
  228. cmd[9] = 255;
  229. if (0 == (result = ch_do_scsi(ch, cmd, 12,
  230. buffer, 256, REQ_OP_DRV_IN))) {
  231. if (((buffer[16] << 8) | buffer[17]) != elem) {
  232. DPRINTK("asked for element 0x%02x, got 0x%02x\n",
  233. elem,(buffer[16] << 8) | buffer[17]);
  234. kfree(buffer);
  235. return -EIO;
  236. }
  237. memcpy(data,buffer+16,16);
  238. } else {
  239. if (ch->voltags) {
  240. ch->voltags = 0;
  241. VPRINTK(KERN_INFO, "device has no volume tag support\n");
  242. goto retry;
  243. }
  244. DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);
  245. }
  246. kfree(buffer);
  247. return result;
  248. }
  249. static int
  250. ch_init_elem(scsi_changer *ch)
  251. {
  252. int err;
  253. u_char cmd[6];
  254. VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");
  255. memset(cmd,0,sizeof(cmd));
  256. cmd[0] = INITIALIZE_ELEMENT_STATUS;
  257. cmd[1] = (ch->device->lun & 0x7) << 5;
  258. err = ch_do_scsi(ch, cmd, 6, NULL, 0, REQ_OP_DRV_IN);
  259. VPRINTK(KERN_INFO, "... finished\n");
  260. return err;
  261. }
  262. static int
  263. ch_readconfig(scsi_changer *ch)
  264. {
  265. u_char cmd[10], data[16];
  266. u_char *buffer;
  267. int result,id,lun,i;
  268. u_int elem;
  269. buffer = kzalloc(512, GFP_KERNEL);
  270. if (!buffer)
  271. return -ENOMEM;
  272. memset(cmd,0,sizeof(cmd));
  273. cmd[0] = MODE_SENSE;
  274. cmd[1] = (ch->device->lun & 0x7) << 5;
  275. cmd[2] = 0x1d;
  276. cmd[4] = 255;
  277. result = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
  278. if (0 != result) {
  279. cmd[1] |= (1<<3);
  280. result = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
  281. }
  282. if (0 == result) {
  283. ch->firsts[CHET_MT] =
  284. (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
  285. ch->counts[CHET_MT] =
  286. (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
  287. ch->firsts[CHET_ST] =
  288. (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
  289. ch->counts[CHET_ST] =
  290. (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
  291. ch->firsts[CHET_IE] =
  292. (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
  293. ch->counts[CHET_IE] =
  294. (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
  295. ch->firsts[CHET_DT] =
  296. (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
  297. ch->counts[CHET_DT] =
  298. (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
  299. VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",
  300. ch->firsts[CHET_MT],
  301. ch->counts[CHET_MT]);
  302. VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",
  303. ch->firsts[CHET_ST],
  304. ch->counts[CHET_ST]);
  305. VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",
  306. ch->firsts[CHET_IE],
  307. ch->counts[CHET_IE]);
  308. VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",
  309. ch->firsts[CHET_DT],
  310. ch->counts[CHET_DT]);
  311. } else {
  312. VPRINTK(KERN_INFO, "reading element address assignment page failed!\n");
  313. }
  314. /* vendor specific element types */
  315. for (i = 0; i < 4; i++) {
  316. if (0 == vendor_counts[i])
  317. continue;
  318. if (NULL == vendor_labels[i])
  319. continue;
  320. ch->firsts[CHET_V1+i] = vendor_firsts[i];
  321. ch->counts[CHET_V1+i] = vendor_counts[i];
  322. VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
  323. i+5,i+1,vendor_firsts[i],vendor_counts[i],
  324. vendor_labels[i]);
  325. }
  326. /* look up the devices of the data transfer elements */
  327. ch->dt = kzalloc_objs(*ch->dt, ch->counts[CHET_DT]);
  328. if (!ch->dt) {
  329. kfree(buffer);
  330. return -ENOMEM;
  331. }
  332. for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
  333. id = -1;
  334. lun = 0;
  335. if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
  336. id = dt_id[elem];
  337. lun = dt_lun[elem];
  338. VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",
  339. elem+ch->firsts[CHET_DT]);
  340. } else if (0 != ch_read_element_status
  341. (ch,elem+ch->firsts[CHET_DT],data)) {
  342. VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",
  343. elem+ch->firsts[CHET_DT]);
  344. } else {
  345. VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);
  346. if (data[6] & 0x80) {
  347. VPRINTK(KERN_CONT, "not this SCSI bus\n");
  348. ch->dt[elem] = NULL;
  349. } else if (0 == (data[6] & 0x30)) {
  350. VPRINTK(KERN_CONT, "ID/LUN unknown\n");
  351. ch->dt[elem] = NULL;
  352. } else {
  353. id = ch->device->id;
  354. lun = 0;
  355. if (data[6] & 0x20) id = data[7];
  356. if (data[6] & 0x10) lun = data[6] & 7;
  357. }
  358. }
  359. if (-1 != id) {
  360. VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);
  361. ch->dt[elem] =
  362. scsi_device_lookup(ch->device->host,
  363. ch->device->channel,
  364. id,lun);
  365. if (!ch->dt[elem]) {
  366. /* should not happen */
  367. VPRINTK(KERN_CONT, "Huh? device not found!\n");
  368. } else {
  369. VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",
  370. ch->dt[elem]->vendor,
  371. ch->dt[elem]->model,
  372. ch->dt[elem]->rev);
  373. }
  374. }
  375. }
  376. ch->voltags = 1;
  377. kfree(buffer);
  378. return 0;
  379. }
  380. /* ------------------------------------------------------------------------ */
  381. static int
  382. ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
  383. {
  384. u_char cmd[10];
  385. DPRINTK("position: 0x%x\n",elem);
  386. if (0 == trans)
  387. trans = ch->firsts[CHET_MT];
  388. memset(cmd,0,sizeof(cmd));
  389. cmd[0] = POSITION_TO_ELEMENT;
  390. cmd[1] = (ch->device->lun & 0x7) << 5;
  391. cmd[2] = (trans >> 8) & 0xff;
  392. cmd[3] = trans & 0xff;
  393. cmd[4] = (elem >> 8) & 0xff;
  394. cmd[5] = elem & 0xff;
  395. cmd[8] = rotate ? 1 : 0;
  396. return ch_do_scsi(ch, cmd, 10, NULL, 0, REQ_OP_DRV_IN);
  397. }
  398. static int
  399. ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
  400. {
  401. u_char cmd[12];
  402. DPRINTK("move: 0x%x => 0x%x\n",src,dest);
  403. if (0 == trans)
  404. trans = ch->firsts[CHET_MT];
  405. memset(cmd,0,sizeof(cmd));
  406. cmd[0] = MOVE_MEDIUM;
  407. cmd[1] = (ch->device->lun & 0x7) << 5;
  408. cmd[2] = (trans >> 8) & 0xff;
  409. cmd[3] = trans & 0xff;
  410. cmd[4] = (src >> 8) & 0xff;
  411. cmd[5] = src & 0xff;
  412. cmd[6] = (dest >> 8) & 0xff;
  413. cmd[7] = dest & 0xff;
  414. cmd[10] = rotate ? 1 : 0;
  415. return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
  416. }
  417. static int
  418. ch_exchange(scsi_changer *ch, u_int trans, u_int src,
  419. u_int dest1, u_int dest2, int rotate1, int rotate2)
  420. {
  421. u_char cmd[12];
  422. DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",
  423. src,dest1,dest2);
  424. if (0 == trans)
  425. trans = ch->firsts[CHET_MT];
  426. memset(cmd,0,sizeof(cmd));
  427. cmd[0] = EXCHANGE_MEDIUM;
  428. cmd[1] = (ch->device->lun & 0x7) << 5;
  429. cmd[2] = (trans >> 8) & 0xff;
  430. cmd[3] = trans & 0xff;
  431. cmd[4] = (src >> 8) & 0xff;
  432. cmd[5] = src & 0xff;
  433. cmd[6] = (dest1 >> 8) & 0xff;
  434. cmd[7] = dest1 & 0xff;
  435. cmd[8] = (dest2 >> 8) & 0xff;
  436. cmd[9] = dest2 & 0xff;
  437. cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
  438. return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
  439. }
  440. static void
  441. ch_check_voltag(char *tag)
  442. {
  443. int i;
  444. for (i = 0; i < 32; i++) {
  445. /* restrict to ascii */
  446. if (tag[i] >= 0x7f || tag[i] < 0x20)
  447. tag[i] = ' ';
  448. /* don't allow search wildcards */
  449. if (tag[i] == '?' ||
  450. tag[i] == '*')
  451. tag[i] = ' ';
  452. }
  453. }
  454. static int
  455. ch_set_voltag(scsi_changer *ch, u_int elem,
  456. int alternate, int clear, u_char *tag)
  457. {
  458. u_char cmd[12];
  459. u_char *buffer;
  460. int result;
  461. buffer = kzalloc(512, GFP_KERNEL);
  462. if (!buffer)
  463. return -ENOMEM;
  464. DPRINTK("%s %s voltag: 0x%x => \"%s\"\n",
  465. clear ? "clear" : "set",
  466. alternate ? "alternate" : "primary",
  467. elem, tag);
  468. memset(cmd,0,sizeof(cmd));
  469. cmd[0] = SEND_VOLUME_TAG;
  470. cmd[1] = ((ch->device->lun & 0x7) << 5) |
  471. ch_elem_to_typecode(ch,elem);
  472. cmd[2] = (elem >> 8) & 0xff;
  473. cmd[3] = elem & 0xff;
  474. cmd[5] = clear
  475. ? (alternate ? 0x0d : 0x0c)
  476. : (alternate ? 0x0b : 0x0a);
  477. cmd[9] = 255;
  478. memcpy(buffer,tag,32);
  479. ch_check_voltag(buffer);
  480. result = ch_do_scsi(ch, cmd, 12, buffer, 256, REQ_OP_DRV_OUT);
  481. kfree(buffer);
  482. return result;
  483. }
  484. static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
  485. {
  486. int retval = 0;
  487. u_char data[16];
  488. unsigned int i;
  489. mutex_lock(&ch->lock);
  490. for (i = 0; i < ch->counts[type]; i++) {
  491. if (0 != ch_read_element_status
  492. (ch, ch->firsts[type]+i,data)) {
  493. retval = -EIO;
  494. break;
  495. }
  496. put_user(data[2], dest+i);
  497. if (data[2] & CESTATUS_EXCEPT)
  498. VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n",
  499. ch->firsts[type]+i,
  500. (int)data[4],(int)data[5]);
  501. retval = ch_read_element_status
  502. (ch, ch->firsts[type]+i,data);
  503. if (0 != retval)
  504. break;
  505. }
  506. mutex_unlock(&ch->lock);
  507. return retval;
  508. }
  509. /* ------------------------------------------------------------------------ */
  510. static void ch_destroy(struct kref *ref)
  511. {
  512. scsi_changer *ch = container_of(ref, scsi_changer, ref);
  513. ch->device = NULL;
  514. kfree(ch->dt);
  515. kfree(ch);
  516. }
  517. static int
  518. ch_release(struct inode *inode, struct file *file)
  519. {
  520. scsi_changer *ch = file->private_data;
  521. scsi_device_put(ch->device);
  522. file->private_data = NULL;
  523. kref_put(&ch->ref, ch_destroy);
  524. return 0;
  525. }
  526. static int
  527. ch_open(struct inode *inode, struct file *file)
  528. {
  529. scsi_changer *ch;
  530. int minor = iminor(inode);
  531. spin_lock(&ch_index_lock);
  532. ch = idr_find(&ch_index_idr, minor);
  533. if (ch == NULL || !kref_get_unless_zero(&ch->ref)) {
  534. spin_unlock(&ch_index_lock);
  535. return -ENXIO;
  536. }
  537. spin_unlock(&ch_index_lock);
  538. if (scsi_device_get(ch->device)) {
  539. kref_put(&ch->ref, ch_destroy);
  540. return -ENXIO;
  541. }
  542. /* Synchronize with ch_probe() */
  543. mutex_lock(&ch->lock);
  544. file->private_data = ch;
  545. mutex_unlock(&ch->lock);
  546. return 0;
  547. }
  548. static int
  549. ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
  550. {
  551. if (type >= CH_TYPES || unit >= ch->counts[type])
  552. return -1;
  553. return 0;
  554. }
  555. struct changer_element_status32 {
  556. int ces_type;
  557. compat_uptr_t ces_data;
  558. };
  559. #define CHIOGSTATUS32 _IOW('c', 8, struct changer_element_status32)
  560. static long ch_ioctl(struct file *file,
  561. unsigned int cmd, unsigned long arg)
  562. {
  563. scsi_changer *ch = file->private_data;
  564. int retval;
  565. void __user *argp = (void __user *)arg;
  566. retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd,
  567. file->f_flags & O_NDELAY);
  568. if (retval)
  569. return retval;
  570. switch (cmd) {
  571. case CHIOGPARAMS:
  572. {
  573. struct changer_params params;
  574. params.cp_curpicker = 0;
  575. params.cp_npickers = ch->counts[CHET_MT];
  576. params.cp_nslots = ch->counts[CHET_ST];
  577. params.cp_nportals = ch->counts[CHET_IE];
  578. params.cp_ndrives = ch->counts[CHET_DT];
  579. if (copy_to_user(argp, &params, sizeof(params)))
  580. return -EFAULT;
  581. return 0;
  582. }
  583. case CHIOGVPARAMS:
  584. {
  585. struct changer_vendor_params vparams;
  586. memset(&vparams,0,sizeof(vparams));
  587. if (ch->counts[CHET_V1]) {
  588. vparams.cvp_n1 = ch->counts[CHET_V1];
  589. strscpy(vparams.cvp_label1, vendor_labels[0],
  590. sizeof(vparams.cvp_label1));
  591. }
  592. if (ch->counts[CHET_V2]) {
  593. vparams.cvp_n2 = ch->counts[CHET_V2];
  594. strscpy(vparams.cvp_label2, vendor_labels[1],
  595. sizeof(vparams.cvp_label2));
  596. }
  597. if (ch->counts[CHET_V3]) {
  598. vparams.cvp_n3 = ch->counts[CHET_V3];
  599. strscpy(vparams.cvp_label3, vendor_labels[2],
  600. sizeof(vparams.cvp_label3));
  601. }
  602. if (ch->counts[CHET_V4]) {
  603. vparams.cvp_n4 = ch->counts[CHET_V4];
  604. strscpy(vparams.cvp_label4, vendor_labels[3],
  605. sizeof(vparams.cvp_label4));
  606. }
  607. if (copy_to_user(argp, &vparams, sizeof(vparams)))
  608. return -EFAULT;
  609. return 0;
  610. }
  611. case CHIOPOSITION:
  612. {
  613. struct changer_position pos;
  614. if (copy_from_user(&pos, argp, sizeof (pos)))
  615. return -EFAULT;
  616. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  617. DPRINTK("CHIOPOSITION: invalid parameter\n");
  618. return -EBADSLT;
  619. }
  620. mutex_lock(&ch->lock);
  621. retval = ch_position(ch,0,
  622. ch->firsts[pos.cp_type] + pos.cp_unit,
  623. pos.cp_flags & CP_INVERT);
  624. mutex_unlock(&ch->lock);
  625. return retval;
  626. }
  627. case CHIOMOVE:
  628. {
  629. struct changer_move mv;
  630. if (copy_from_user(&mv, argp, sizeof (mv)))
  631. return -EFAULT;
  632. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  633. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  634. DPRINTK("CHIOMOVE: invalid parameter\n");
  635. return -EBADSLT;
  636. }
  637. mutex_lock(&ch->lock);
  638. retval = ch_move(ch,0,
  639. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  640. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  641. mv.cm_flags & CM_INVERT);
  642. mutex_unlock(&ch->lock);
  643. return retval;
  644. }
  645. case CHIOEXCHANGE:
  646. {
  647. struct changer_exchange mv;
  648. if (copy_from_user(&mv, argp, sizeof (mv)))
  649. return -EFAULT;
  650. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  651. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  652. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  653. DPRINTK("CHIOEXCHANGE: invalid parameter\n");
  654. return -EBADSLT;
  655. }
  656. mutex_lock(&ch->lock);
  657. retval = ch_exchange
  658. (ch,0,
  659. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  660. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  661. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  662. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  663. mutex_unlock(&ch->lock);
  664. return retval;
  665. }
  666. case CHIOGSTATUS:
  667. {
  668. struct changer_element_status ces;
  669. if (copy_from_user(&ces, argp, sizeof (ces)))
  670. return -EFAULT;
  671. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  672. return -EINVAL;
  673. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  674. }
  675. #ifdef CONFIG_COMPAT
  676. case CHIOGSTATUS32:
  677. {
  678. struct changer_element_status32 ces32;
  679. if (copy_from_user(&ces32, argp, sizeof(ces32)))
  680. return -EFAULT;
  681. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  682. return -EINVAL;
  683. return ch_gstatus(ch, ces32.ces_type,
  684. compat_ptr(ces32.ces_data));
  685. }
  686. #endif
  687. case CHIOGELEM:
  688. {
  689. struct changer_get_element cge;
  690. u_char ch_cmd[12];
  691. u_char *buffer;
  692. unsigned int elem;
  693. int result,i;
  694. if (copy_from_user(&cge, argp, sizeof (cge)))
  695. return -EFAULT;
  696. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  697. return -EINVAL;
  698. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  699. buffer = kmalloc(512, GFP_KERNEL);
  700. if (!buffer)
  701. return -ENOMEM;
  702. mutex_lock(&ch->lock);
  703. voltag_retry:
  704. memset(ch_cmd, 0, sizeof(ch_cmd));
  705. ch_cmd[0] = READ_ELEMENT_STATUS;
  706. ch_cmd[1] = ((ch->device->lun & 0x7) << 5) |
  707. (ch->voltags ? 0x10 : 0) |
  708. ch_elem_to_typecode(ch,elem);
  709. ch_cmd[2] = (elem >> 8) & 0xff;
  710. ch_cmd[3] = elem & 0xff;
  711. ch_cmd[5] = 1;
  712. ch_cmd[9] = 255;
  713. result = ch_do_scsi(ch, ch_cmd, 12, buffer, 256, REQ_OP_DRV_IN);
  714. if (!result) {
  715. cge.cge_status = buffer[18];
  716. cge.cge_flags = 0;
  717. if (buffer[18] & CESTATUS_EXCEPT) {
  718. cge.cge_errno = EIO;
  719. }
  720. if (buffer[25] & 0x80) {
  721. cge.cge_flags |= CGE_SRC;
  722. if (buffer[25] & 0x40)
  723. cge.cge_flags |= CGE_INVERT;
  724. elem = (buffer[26]<<8) | buffer[27];
  725. for (i = 0; i < 4; i++) {
  726. if (elem >= ch->firsts[i] &&
  727. elem < ch->firsts[i] + ch->counts[i]) {
  728. cge.cge_srctype = i;
  729. cge.cge_srcunit = elem-ch->firsts[i];
  730. }
  731. }
  732. }
  733. if ((buffer[22] & 0x30) == 0x30) {
  734. cge.cge_flags |= CGE_IDLUN;
  735. cge.cge_id = buffer[23];
  736. cge.cge_lun = buffer[22] & 7;
  737. }
  738. if (buffer[9] & 0x80) {
  739. cge.cge_flags |= CGE_PVOLTAG;
  740. memcpy(cge.cge_pvoltag,buffer+28,36);
  741. }
  742. if (buffer[9] & 0x40) {
  743. cge.cge_flags |= CGE_AVOLTAG;
  744. memcpy(cge.cge_avoltag,buffer+64,36);
  745. }
  746. } else if (ch->voltags) {
  747. ch->voltags = 0;
  748. VPRINTK(KERN_INFO, "device has no volume tag support\n");
  749. goto voltag_retry;
  750. }
  751. kfree(buffer);
  752. mutex_unlock(&ch->lock);
  753. if (copy_to_user(argp, &cge, sizeof (cge)))
  754. return -EFAULT;
  755. return result;
  756. }
  757. case CHIOINITELEM:
  758. {
  759. mutex_lock(&ch->lock);
  760. retval = ch_init_elem(ch);
  761. mutex_unlock(&ch->lock);
  762. return retval;
  763. }
  764. case CHIOSVOLTAG:
  765. {
  766. struct changer_set_voltag csv;
  767. int elem;
  768. if (copy_from_user(&csv, argp, sizeof(csv)))
  769. return -EFAULT;
  770. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  771. DPRINTK("CHIOSVOLTAG: invalid parameter\n");
  772. return -EBADSLT;
  773. }
  774. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  775. mutex_lock(&ch->lock);
  776. retval = ch_set_voltag(ch, elem,
  777. csv.csv_flags & CSV_AVOLTAG,
  778. csv.csv_flags & CSV_CLEARTAG,
  779. csv.csv_voltag);
  780. mutex_unlock(&ch->lock);
  781. return retval;
  782. }
  783. default:
  784. return scsi_ioctl(ch->device, file->f_mode & FMODE_WRITE, cmd,
  785. argp);
  786. }
  787. }
  788. /* ------------------------------------------------------------------------ */
  789. static int ch_probe(struct scsi_device *sd)
  790. {
  791. struct device *dev = &sd->sdev_gendev;
  792. struct device *class_dev;
  793. int ret;
  794. scsi_changer *ch;
  795. if (sd->type != TYPE_MEDIUM_CHANGER)
  796. return -ENODEV;
  797. ch = kzalloc_obj(*ch);
  798. if (NULL == ch)
  799. return -ENOMEM;
  800. idr_preload(GFP_KERNEL);
  801. spin_lock(&ch_index_lock);
  802. ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT);
  803. spin_unlock(&ch_index_lock);
  804. idr_preload_end();
  805. if (ret < 0) {
  806. if (ret == -ENOSPC)
  807. ret = -ENODEV;
  808. goto free_ch;
  809. }
  810. ch->minor = ret;
  811. sprintf(ch->name,"ch%d",ch->minor);
  812. ret = scsi_device_get(sd);
  813. if (ret) {
  814. sdev_printk(KERN_WARNING, sd, "ch%d: failed to get device\n",
  815. ch->minor);
  816. goto remove_idr;
  817. }
  818. mutex_init(&ch->lock);
  819. kref_init(&ch->ref);
  820. ch->device = sd;
  821. class_dev = device_create(&ch_sysfs_class, dev,
  822. MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
  823. "s%s", ch->name);
  824. if (IS_ERR(class_dev)) {
  825. sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n",
  826. ch->minor);
  827. ret = PTR_ERR(class_dev);
  828. goto put_device;
  829. }
  830. mutex_lock(&ch->lock);
  831. ret = ch_readconfig(ch);
  832. if (ret) {
  833. mutex_unlock(&ch->lock);
  834. goto destroy_dev;
  835. }
  836. if (init)
  837. ch_init_elem(ch);
  838. mutex_unlock(&ch->lock);
  839. dev_set_drvdata(dev, ch);
  840. sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
  841. return 0;
  842. destroy_dev:
  843. device_destroy(&ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
  844. put_device:
  845. scsi_device_put(sd);
  846. remove_idr:
  847. idr_remove(&ch_index_idr, ch->minor);
  848. free_ch:
  849. kfree(ch);
  850. return ret;
  851. }
  852. static void ch_remove(struct scsi_device *sd)
  853. {
  854. struct device *dev = &sd->sdev_gendev;
  855. scsi_changer *ch = dev_get_drvdata(dev);
  856. spin_lock(&ch_index_lock);
  857. idr_remove(&ch_index_idr, ch->minor);
  858. dev_set_drvdata(dev, NULL);
  859. spin_unlock(&ch_index_lock);
  860. device_destroy(&ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
  861. scsi_device_put(ch->device);
  862. kref_put(&ch->ref, ch_destroy);
  863. }
  864. static struct scsi_driver ch_template = {
  865. .probe = ch_probe,
  866. .remove = ch_remove,
  867. .gendrv = {
  868. .name = "ch",
  869. .owner = THIS_MODULE,
  870. },
  871. };
  872. static const struct file_operations changer_fops = {
  873. .owner = THIS_MODULE,
  874. .open = ch_open,
  875. .release = ch_release,
  876. .unlocked_ioctl = ch_ioctl,
  877. .compat_ioctl = compat_ptr_ioctl,
  878. .llseek = noop_llseek,
  879. };
  880. static int __init init_ch_module(void)
  881. {
  882. int rc;
  883. printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
  884. rc = class_register(&ch_sysfs_class);
  885. if (rc)
  886. return rc;
  887. rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
  888. if (rc < 0) {
  889. printk("Unable to get major %d for SCSI-Changer\n",
  890. SCSI_CHANGER_MAJOR);
  891. goto fail1;
  892. }
  893. rc = scsi_register_driver(&ch_template);
  894. if (rc < 0)
  895. goto fail2;
  896. return 0;
  897. fail2:
  898. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  899. fail1:
  900. class_unregister(&ch_sysfs_class);
  901. return rc;
  902. }
  903. static void __exit exit_ch_module(void)
  904. {
  905. scsi_unregister_driver(&ch_template);
  906. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  907. class_unregister(&ch_sysfs_class);
  908. idr_destroy(&ch_index_idr);
  909. }
  910. module_init(init_ch_module);
  911. module_exit(exit_ch_module);