cciss_scsi.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:45k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *    Disk Array driver for Compaq SA53xx Controllers, SCSI Tape module
  3.  *    Copyright 2001 Compaq Computer Corporation
  4.  *
  5.  *    This program is free software; you can redistribute it and/or modify
  6.  *    it under the terms of the GNU General Public License as published by
  7.  *    the Free Software Foundation; either version 2 of the License, or
  8.  *    (at your option) any later version.
  9.  *
  10.  *    This program is distributed in the hope that it will be useful,
  11.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13.  *    NON INFRINGEMENT.  See the GNU General Public License for more details.
  14.  *
  15.  *    You should have received a copy of the GNU General Public License
  16.  *    along with this program; if not, write to the Free Software
  17.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *    Questions/Comments/Bugfixes to arrays@compaq.com
  20.  *
  21.  *    Author: Stephen M. Cameron
  22.  */
  23. #ifdef CONFIG_CISS_SCSI_TAPE
  24. /* Here we have code to present the driver as a scsi driver
  25.    as it is simultaneously presented as a block driver.  The
  26.    reason for doing this is to allow access to SCSI tape drives
  27.    through the array controller.  Note in particular, neither
  28.    physical nor logical disks are presented through the scsi layer. */
  29. #include "../scsi/scsi.h"
  30. #include "../scsi/hosts.h"
  31. #include <asm/atomic.h>
  32. #include <linux/timer.h>
  33. #include "cciss_scsi.h"
  34. /* some prototypes... */
  35. static int sendcmd(
  36. __u8 cmd,
  37. int ctlr,
  38. void *buff,
  39. size_t size,
  40. unsigned int use_unit_num, /* 0: address the controller,
  41.       1: address logical volume log_unit,
  42.       2: address is in scsi3addr */
  43. unsigned int log_unit,
  44. __u8 page_code,
  45. unsigned char *scsi3addr );
  46. int __init cciss_scsi_detect(Scsi_Host_Template *tpnt);
  47. int cciss_scsi_release(struct Scsi_Host *sh);
  48. const char *cciss_scsi_info(struct Scsi_Host *sa);
  49. int cciss_scsi_proc_info(
  50. char *buffer, /* data buffer */
  51. char **start,     /* where data in buffer starts */
  52. off_t offset,    /* offset from start of imaginary file */
  53. int length,     /* length of data in buffer */
  54. int hostnum,     /* which host adapter (always zero for me) */
  55. int func);    /* 0 == read, 1 == write */
  56. int cciss_scsi_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *));
  57. #if 0
  58. int cciss_scsi_abort(Scsi_Cmnd *cmd);
  59. #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
  60. int cciss_scsi_reset(Scsi_Cmnd *cmd, unsigned int reset_flags);
  61. #else
  62. int cciss_scsi_reset(Scsi_Cmnd *cmd);
  63. #endif
  64. #endif
  65. static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
  66. { name: "cciss0", ndevices: 0 },
  67. { name: "cciss1", ndevices: 0 },
  68. { name: "cciss2", ndevices: 0 },
  69. { name: "cciss3", ndevices: 0 },
  70. { name: "cciss4", ndevices: 0 },
  71. { name: "cciss5", ndevices: 0 },
  72. { name: "cciss6", ndevices: 0 },
  73. { name: "cciss7", ndevices: 0 },
  74. };
  75. /* We need one Scsi_Host_Template *per controller* instead of
  76.    the usual one Scsi_Host_Template per controller *type*. This
  77.    is so PCI hot plug could have a remote possibility of still
  78.    working even with the SCSI system.  It's so
  79.    scsi_unregister_module will differentiate the controllers.
  80.    When register_scsi_module is called, each host template is
  81.    customized (name change) in cciss_register_scsi()
  82.    (that's called from cciss.c:cciss_init_one()) */
  83. static
  84. Scsi_Host_Template driver_template[MAX_CTLR] =
  85. {
  86. CCISS_SCSI, CCISS_SCSI, CCISS_SCSI, CCISS_SCSI,
  87. CCISS_SCSI, CCISS_SCSI, CCISS_SCSI, CCISS_SCSI,
  88. };
  89. #pragma pack(1)
  90. struct cciss_scsi_cmd_stack_elem_t {
  91. CommandList_struct cmd;
  92. ErrorInfo_struct Err;
  93. __u32 busaddr; // 32 bits always, must fit through cmd register.
  94. };
  95. #pragma pack()
  96. #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * 
  97. CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
  98. // plus two for init time usage
  99. #pragma pack(1)
  100. struct cciss_scsi_cmd_stack_t {
  101. struct cciss_scsi_cmd_stack_elem_t *pool;
  102. struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
  103. dma_addr_t cmd_pool_handle;
  104. int top;
  105. };
  106. #pragma pack()
  107. struct cciss_scsi_adapter_data_t {
  108. struct Scsi_Host *scsi_host;
  109. struct cciss_scsi_cmd_stack_t cmd_stack;
  110. int registered;
  111. spinlock_t lock; // to protect ccissscsi[ctlr];
  112. };
  113. #if 1
  114. #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( 
  115. &(((struct cciss_scsi_adapter_data_t *) 
  116. hba[ctlr]->scsi_ctlr)->lock), flags);
  117. #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( 
  118. &(((struct cciss_scsi_adapter_data_t *) 
  119. hba[ctlr]->scsi_ctlr)->lock), flags);
  120. #else
  121. #define CPQ_TAPE_LOCK(x,y)
  122. #define CPQ_TAPE_UNLOCK(x,y)
  123. #endif
  124. static CommandList_struct *
  125. scsi_cmd_alloc(ctlr_info_t *h)
  126. {
  127. /* assume only one process in here at a time, locking done by caller. */
  128. /* take the top memory chunk off the stack and return it, if any. */
  129. struct cciss_scsi_cmd_stack_elem_t *c;
  130. struct cciss_scsi_adapter_data_t *sa;
  131. struct cciss_scsi_cmd_stack_t *stk;
  132. u64bit temp64;
  133. sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
  134. stk = &sa->cmd_stack;
  135. if (stk->top < 0)
  136. return NULL;
  137. c = stk->elem[stk->top];
  138. memset(&c->cmd, 0, sizeof(c->cmd));
  139. memset(&c->Err, 0, sizeof(c->Err));
  140. /* set physical addr of cmd and addr of scsi parameters */
  141. c->cmd.busaddr = c->busaddr;
  142. temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
  143. stk->top--;
  144. c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
  145. c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
  146. c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
  147. c->cmd.ctlr = h->ctlr;
  148. c->cmd.err_info = &c->Err;
  149. return (CommandList_struct *) c;
  150. }
  151. static void
  152. scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
  153. {
  154. /* assume only one process in here at a time, locking done by caller. */
  155. /* drop the free memory chunk on top of the stack. */
  156. struct cciss_scsi_adapter_data_t *sa;
  157. struct cciss_scsi_cmd_stack_t *stk;
  158. sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
  159. stk = &sa->cmd_stack;
  160. if (stk->top >= CMD_STACK_SIZE) {
  161. printk("cciss: scsi_cmd_free called too many times.n");
  162. BUG();
  163. }
  164. stk->top++;
  165. stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
  166. }
  167. static int
  168. scsi_cmd_stack_setup(int ctlr)
  169. {
  170. int i;
  171. struct cciss_scsi_adapter_data_t *sa;
  172. struct cciss_scsi_cmd_stack_t *stk;
  173. size_t size;
  174. sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
  175. stk = &sa->cmd_stack;
  176. size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
  177. stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
  178. pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
  179. if (stk->pool == NULL) {
  180. printk("stk->pool is nulln");
  181. return -1;
  182. }
  183. for (i=0; i<CMD_STACK_SIZE; i++) {
  184. stk->elem[i] = &stk->pool[i];
  185. stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
  186. (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
  187. }
  188. stk->top = CMD_STACK_SIZE-1;
  189. return 0;
  190. }
  191. static void
  192. scsi_cmd_stack_free(int ctlr)
  193. {
  194. struct cciss_scsi_adapter_data_t *sa;
  195. struct cciss_scsi_cmd_stack_t *stk;
  196. size_t size;
  197. sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
  198. stk = &sa->cmd_stack;
  199. if (stk->top != CMD_STACK_SIZE-1) {
  200. printk( "cciss: %d scsi commands are still outstanding.n",
  201. CMD_STACK_SIZE - stk->top);
  202. // BUG();
  203. printk("WE HAVE A BUG HERE!!! stk=%pn",
  204. stk);
  205. }
  206. size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
  207. pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
  208. stk->pool = NULL;
  209. }
  210. /* scsi_device_types comes from scsi.h */
  211. #define DEVICETYPE(n) (n<0 || n>MAX_SCSI_DEVICE_CODE) ? 
  212. "Unknown" : scsi_device_types[n]
  213. #if 0
  214. static int xmargin=8;
  215. static int amargin=60;
  216. static void
  217. print_bytes (unsigned char *c, int len, int hex, int ascii)
  218. {
  219. int i;
  220. unsigned char *x;
  221. if (hex)
  222. {
  223. x = c;
  224. for (i=0;i<len;i++)
  225. {
  226. if ((i % xmargin) == 0 && i>0) printk("n");
  227. if ((i % xmargin) == 0) printk("0x%04x:", i);
  228. printk(" %02x", *x);
  229. x++;
  230. }
  231. printk("n");
  232. }
  233. if (ascii)
  234. {
  235. x = c;
  236. for (i=0;i<len;i++)
  237. {
  238. if ((i % amargin) == 0 && i>0) printk("n");
  239. if ((i % amargin) == 0) printk("0x%04x:", i);
  240. if (*x > 26 && *x < 128) printk("%c", *x);
  241. else printk(".");
  242. x++;
  243. }
  244. printk("n");
  245. }
  246. }
  247. static void
  248. print_cmd(CommandList_struct *cp)
  249. {
  250. printk("queue:%dn", cp->Header.ReplyQueue);
  251. printk("sglist:%dn", cp->Header.SGList);
  252. printk("sgtot:%dn", cp->Header.SGTotal);
  253. printk("Tag:0x%08x/0x%08xn", cp->Header.Tag.upper,
  254. cp->Header.Tag.lower);
  255. printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02xn",
  256. cp->Header.LUN.LunAddrBytes[0],
  257. cp->Header.LUN.LunAddrBytes[1],
  258. cp->Header.LUN.LunAddrBytes[2],
  259. cp->Header.LUN.LunAddrBytes[3],
  260. cp->Header.LUN.LunAddrBytes[4],
  261. cp->Header.LUN.LunAddrBytes[5],
  262. cp->Header.LUN.LunAddrBytes[6],
  263. cp->Header.LUN.LunAddrBytes[7]);
  264. printk("CDBLen:%dn", cp->Request.CDBLen);
  265. printk("Type:%dn",cp->Request.Type.Type);
  266. printk("Attr:%dn",cp->Request.Type.Attribute);
  267. printk(" Dir:%dn",cp->Request.Type.Direction);
  268. printk("Timeout:%dn",cp->Request.Timeout);
  269. printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
  270. " %02x %02x %02x %02x %02x %02x %02x %02xn",
  271. cp->Request.CDB[0], cp->Request.CDB[1],
  272. cp->Request.CDB[2], cp->Request.CDB[3],
  273. cp->Request.CDB[4], cp->Request.CDB[5],
  274. cp->Request.CDB[6], cp->Request.CDB[7],
  275. cp->Request.CDB[8], cp->Request.CDB[9],
  276. cp->Request.CDB[10], cp->Request.CDB[11],
  277. cp->Request.CDB[12], cp->Request.CDB[13],
  278. cp->Request.CDB[14], cp->Request.CDB[15]),
  279. printk("edesc.Addr: 0x%08x/0%08x, Len  = %dn",
  280. cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
  281. cp->ErrDesc.Len);
  282. printk("sgs..........Errorinfo:n");
  283. printk("scsistatus:%dn", cp->err_info->ScsiStatus);
  284. printk("senselen:%dn", cp->err_info->SenseLen);
  285. printk("cmd status:%dn", cp->err_info->CommandStatus);
  286. printk("resid cnt:%dn", cp->err_info->ResidualCnt);
  287. printk("offense size:%dn", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
  288. printk("offense byte:%dn", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
  289. printk("offense value:%dn", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
  290. }
  291. #endif
  292. static int
  293. find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
  294. {
  295. /* finds an unused bus, target, lun for a new device */
  296. /* assumes hba[ctlr]->scsi_ctlr->lock is held */
  297. int i, found=0;
  298. unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
  299. memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
  300. # if SELF_SCSI_ID >= 0
  301. target_taken[SELF_SCSI_ID] = 1;
  302. # endif
  303. for (i=0;i<ccissscsi[ctlr].ndevices;i++)
  304. target_taken[ccissscsi[ctlr].dev[i].target] = 1;
  305. for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
  306. if (!target_taken[i]) {
  307. *bus = 0; *target=i; *lun = 0; found=1;
  308. break;
  309. }
  310. }
  311. return (!found);
  312. }
  313. static int
  314. cciss_scsi_add_entry(int ctlr, int hostno,
  315. unsigned char *scsi3addr, int devtype)
  316. {
  317. /* assumes hba[ctlr]->scsi_ctlr->lock is held */
  318. int n = ccissscsi[ctlr].ndevices;
  319. struct cciss_scsi_dev_t *sd;
  320. if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
  321. printk("cciss%d: Too many devices, "
  322. "some will be inaccessible.n", ctlr);
  323. return -1;
  324. }
  325. sd = &ccissscsi[ctlr].dev[n];
  326. if (find_bus_target_lun(ctlr, &sd->bus, &sd->target, &sd->lun) != 0)
  327. return -1;
  328. memcpy(&sd->scsi3addr[0], scsi3addr, 8);
  329. sd->devtype = devtype;
  330. ccissscsi[ctlr].ndevices++;
  331. /* initially, (before registering with scsi layer) we don't
  332.    know our hostno and we don't want to print anything first
  333.    time anyway (the scsi layer's inquiries will show that info) */
  334. if (hostno != -1)
  335. printk("cciss%d: %s device c%db%dt%dl%d added.n",
  336. ctlr, DEVICETYPE(sd->devtype), hostno,
  337. sd->bus, sd->target, sd->lun);
  338. return 0;
  339. }
  340. static void
  341. cciss_scsi_remove_entry(int ctlr, int hostno, int entry)
  342. {
  343. /* assumes hba[ctlr]->scsi_ctlr->lock is held */
  344. int i;
  345. struct cciss_scsi_dev_t sd;
  346. if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
  347. sd = ccissscsi[ctlr].dev[entry];
  348. for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
  349. ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
  350. ccissscsi[ctlr].ndevices--;
  351. printk("cciss%d: %s device c%db%dt%dl%d removed.n",
  352. ctlr, DEVICETYPE(sd.devtype), hostno,
  353. sd.bus, sd.target, sd.lun);
  354. }
  355. #define SCSI3ADDR_EQ(a,b) ( 
  356. (a)[7] == (b)[7] && 
  357. (a)[6] == (b)[6] && 
  358. (a)[5] == (b)[5] && 
  359. (a)[4] == (b)[4] && 
  360. (a)[3] == (b)[3] && 
  361. (a)[2] == (b)[2] && 
  362. (a)[1] == (b)[1] && 
  363. (a)[0] == (b)[0])
  364. static int
  365. adjust_cciss_scsi_table(int ctlr, int hostno,
  366. struct cciss_scsi_dev_t sd[], int nsds)
  367. {
  368. /* sd contains scsi3 addresses and devtypes, but
  369.    bus target and lun are not filled in.  This funciton
  370.    takes what's in sd to be the current and adjusts
  371.    ccissscsi[] to be in line with what's in sd. */
  372. int i,j, found, changes=0;
  373. struct cciss_scsi_dev_t *csd;
  374. unsigned long flags;
  375. CPQ_TAPE_LOCK(ctlr, flags);
  376. /* find any devices in ccissscsi[] that are not in
  377.    sd[] and remove them from ccissscsi[] */
  378. i = 0;
  379. while(i<ccissscsi[ctlr].ndevices) {
  380. csd = &ccissscsi[ctlr].dev[i];
  381. found=0;
  382. for (j=0;j<nsds;j++) {
  383. if (SCSI3ADDR_EQ(sd[j].scsi3addr,
  384. csd->scsi3addr)) {
  385. if (sd[j].devtype == csd->devtype)
  386. found=2;
  387. else
  388. found=1;
  389. break;
  390. }
  391. }
  392. if (found == 0) { /* device no longer present. */
  393. changes++;
  394. /* printk("cciss%d: %s device c%db%dt%dl%d removed.n",
  395. ctlr, DEVICETYPE(csd->devtype), hostno,
  396. csd->bus, csd->target, csd->lun); */
  397. cciss_scsi_remove_entry(ctlr, hostno, i);
  398. /* note, i not incremented */
  399. }
  400. else if (found == 1) { /* device is different kind */
  401. changes++;
  402. printk("cciss%d: device c%db%dt%dl%d type changed "
  403. "(device type now %s).n",
  404. ctlr, hostno, csd->bus, csd->target, csd->lun,
  405. DEVICETYPE(csd->devtype));
  406. csd->devtype = sd[j].devtype;
  407. i++; /* so just move along. */
  408. } else  /* device is same as it ever was, */
  409. i++; /* so just move along. */
  410. }
  411. /* Now, make sure every device listed in sd[] is also
  412.      listed in ccissscsi[], adding them if they aren't found */
  413. for (i=0;i<nsds;i++) {
  414. found=0;
  415. for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
  416. csd = &ccissscsi[ctlr].dev[j];
  417. if (SCSI3ADDR_EQ(sd[i].scsi3addr,
  418. csd->scsi3addr)) {
  419. if (sd[i].devtype == csd->devtype)
  420. found=2; /* found device */
  421. else
  422. found=1;  /* found a bug. */
  423. break;
  424. }
  425. }
  426. if (!found) {
  427. changes++;
  428. if (cciss_scsi_add_entry(ctlr, hostno,
  429. &sd[i].scsi3addr[0], sd[i].devtype) != 0)
  430. break;
  431. } else if (found == 1) {
  432. /* should never happen... */
  433. changes++;
  434. printk("cciss%d: device unexpectedly changed typen",
  435. ctlr);
  436. /* but if it does happen, we just ignore that device */
  437. }
  438. }
  439. CPQ_TAPE_UNLOCK(ctlr, flags);
  440. if (!changes)
  441. printk("cciss%d: No device changes detected.n", ctlr);
  442. return 0;
  443. }
  444. static int
  445. lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
  446. {
  447. int i;
  448. struct cciss_scsi_dev_t *sd;
  449. unsigned long flags;
  450. CPQ_TAPE_LOCK(ctlr, flags);
  451. for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
  452. sd = &ccissscsi[ctlr].dev[i];
  453. if (sd->bus == bus &&
  454.     sd->target == target &&
  455.     sd->lun == lun) {
  456. memcpy(scsi3addr, &sd->scsi3addr[0], 8);
  457. CPQ_TAPE_UNLOCK(ctlr, flags);
  458. return 0;
  459. }
  460. }
  461. CPQ_TAPE_UNLOCK(ctlr, flags);
  462. return -1;
  463. }
  464. static void
  465. cciss_find_non_disk_devices(int cntl_num)
  466. {
  467. ReportLunData_struct *ld_buff;
  468. InquiryData_struct *inq_buff;
  469. int return_code;
  470. int i;
  471. int listlength = 0;
  472. int num_luns;
  473. unsigned char scsi3addr[8];
  474. unsigned long flags;
  475. int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
  476. hba[cntl_num]->scsi_ctlr = (void *)
  477. kmalloc(sizeof(struct cciss_scsi_adapter_data_t),
  478. GFP_KERNEL);
  479. if (hba[cntl_num]->scsi_ctlr == NULL)
  480. return;
  481. ((struct cciss_scsi_adapter_data_t *)
  482. hba[cntl_num]->scsi_ctlr)->scsi_host = NULL;
  483. ((struct cciss_scsi_adapter_data_t *)
  484. hba[cntl_num]->scsi_ctlr)->lock = SPIN_LOCK_UNLOCKED;
  485. ((struct cciss_scsi_adapter_data_t *)
  486. hba[cntl_num]->scsi_ctlr)->registered = 0;
  487. if (scsi_cmd_stack_setup(cntl_num) != 0) {
  488. printk("Trouble, returned non-zero!n");
  489. return;
  490. }
  491. ld_buff = kmalloc(reportlunsize, GFP_KERNEL);
  492. if (ld_buff == NULL) {
  493. printk(KERN_ERR "cciss: out of memoryn");
  494. return;
  495. }
  496. memset(ld_buff, 0, sizeof(ReportLunData_struct));
  497. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  498. if (inq_buff == NULL) {
  499. printk(KERN_ERR "cciss: out of memoryn");
  500. kfree(ld_buff);
  501. return;
  502. }
  503. /* Get the physical luns */
  504. return_code = sendcmd(CISS_REPORT_PHYS, cntl_num, ld_buff,
  505. reportlunsize, 0, 0, 0, NULL );
  506. if( return_code == IO_OK) {
  507. unsigned char *c = &ld_buff->LUNListLength[0];
  508. listlength = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
  509. }
  510. else {  /* getting report of physical luns failed */
  511. printk(KERN_WARNING "cciss: report physical luns"
  512. " command failedn");
  513. listlength = 0;
  514. }
  515. CPQ_TAPE_LOCK(cntl_num, flags);
  516. ccissscsi[cntl_num].ndevices = 0;
  517. num_luns = listlength / 8; // 8 bytes pre entry
  518. /* printk("Found %d LUNsn", num_luns); */
  519. if (num_luns > CISS_MAX_PHYS_LUN)
  520. {
  521. printk(KERN_WARNING
  522. "cciss: Maximum physical LUNs (%d) exceeded.  "
  523. "%d LUNs ignored.n", CISS_MAX_PHYS_LUN,
  524. num_luns - CISS_MAX_PHYS_LUN);
  525. num_luns = CISS_MAX_PHYS_LUN;
  526. }
  527. for(i=0; i<num_luns; i++) {
  528. /* Execute an inquiry to figure the device type */
  529. memset(inq_buff, 0, sizeof(InquiryData_struct));
  530. memcpy(scsi3addr, ld_buff->LUN[i], 8); /* ugly... */
  531. return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
  532. sizeof(InquiryData_struct), 2, 0 ,0, scsi3addr );
  533.    if (return_code == IO_OK) {
  534. if(inq_buff->data_byte[8] == 0xFF)
  535. {
  536.    printk(KERN_WARNING "cciss: inquiry failedn");
  537. } else {
  538.    int devtype;
  539.    /* printk("Inquiry...n");
  540.    print_bytes((unsigned char *) inq_buff, 36, 1, 1); */
  541.    devtype = (inq_buff->data_byte[0] & 0x1f);
  542.    switch (devtype)
  543.    {
  544.     case 0x01: /* sequential access, (tape) */
  545.     case 0x08: /* medium changer */
  546.   /* this is the only kind of dev */
  547.   /* we want to expose here. */
  548. if (cciss_scsi_add_entry(cntl_num, -1,
  549. (unsigned char *) ld_buff->LUN[i],
  550. devtype) != 0)
  551. i=num_luns; // leave loop
  552. break;
  553.     default:
  554. break;
  555.    }
  556. }
  557. }
  558. else printk("cciss: inquiry failed.n");
  559. }
  560. #if 0
  561. for (i=0;i<ccissscsi[cntl_num].ndevices;i++)
  562. printk("Tape device presented at c%db%dt%dl%dn",
  563. cntl_num, // <-- this is wrong
  564. ccissscsi[cntl_num].dev[i].bus,
  565. ccissscsi[cntl_num].dev[i].target,
  566. ccissscsi[cntl_num].dev[i].lun);
  567. #endif
  568. CPQ_TAPE_UNLOCK(cntl_num, flags);
  569. kfree(ld_buff);
  570. kfree(inq_buff);
  571. return;
  572. }
  573. static void
  574. complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
  575. {
  576. Scsi_Cmnd *cmd;
  577. ctlr_info_t *ctlr;
  578. u64bit addr64;
  579. ErrorInfo_struct *ei;
  580. ei = cp->err_info;
  581. /* First, see if it was a message rather than a command */
  582. if (cp->Request.Type.Type == TYPE_MSG)  {
  583. cp->cmd_type = CMD_MSG_DONE;
  584. return;
  585. }
  586. /* we stored ptr to scsi cmd in the buffer head pointer */
  587. cmd = (Scsi_Cmnd *) cp->scsi_cmd;
  588. ctlr = hba[cp->ctlr];
  589. /* undo the DMA mappings */
  590. if (cmd->use_sg) {
  591. pci_unmap_sg(ctlr->pdev,
  592. cmd->buffer, cmd->use_sg,
  593. scsi_to_pci_dma_dir(cmd->sc_data_direction));
  594. }
  595. else if (cmd->request_bufflen) {
  596. addr64.val32.lower = cp->SG[0].Addr.lower;
  597. addr64.val32.upper = cp->SG[0].Addr.upper;
  598. pci_unmap_single(ctlr->pdev, (dma_addr_t) addr64.val,
  599. cmd->request_bufflen,
  600. scsi_to_pci_dma_dir(cmd->sc_data_direction));
  601. }
  602. cmd->result = (DID_OK << 16);  /* host byte */
  603. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  604. /* cmd->result |= (GOOD < 1); */ /* status byte */
  605. cmd->result |= (ei->ScsiStatus);
  606. /* printk("Scsistatus is 0x%02xn", ei->ScsiStatus);  */
  607. /* copy the sense data whether we need to or not. */
  608. memcpy(cmd->sense_buffer, ei->SenseInfo,
  609. ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
  610. SCSI_SENSE_BUFFERSIZE :
  611. ei->SenseLen);
  612. cmd->resid = ei->ResidualCnt;
  613. if(ei->CommandStatus != 0)
  614. { /* an error has occurred */
  615. switch(ei->CommandStatus)
  616. {
  617. case CMD_TARGET_STATUS:
  618. /* Pass it up to the upper layers... */
  619. if( ei->ScsiStatus)
  620. cmd->result |= (ei->ScsiStatus < 1);
  621. else {  /* scsi status is zero??? How??? */
  622. /* Ordinarily, this case should never happen, but there is a bug
  623.    in some released firmware revisions that allows it to happen
  624.    if, for example, a 4100 backplane loses power and the tape
  625.    drive is in it.  We assume that it's a fatal error of some
  626.    kind because we can't show that it wasn't. We will make it
  627.    look like selection timeout since that is the most common
  628.    reason for this to occur, and it's severe enough. */
  629. cmd->result = DID_NO_CONNECT << 16;
  630. }
  631. break;
  632. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  633. break;
  634. case CMD_DATA_OVERRUN:
  635. printk(KERN_WARNING "cciss: cp %p has"
  636. " completed with data overrun "
  637. "reportedn", cp);
  638. break;
  639. case CMD_INVALID: {
  640. /* print_bytes(cp, sizeof(*cp), 1, 0);
  641. print_cmd(cp); */
  642.      /* We get CMD_INVALID if you address a non-existent tape drive instead
  643. of a selection timeout (no response).  You will see this if you yank
  644. out a tape drive, then try to access it. This is kind of a shame
  645. because it means that any other CMD_INVALID (e.g. driver bug) will
  646. get interpreted as a missing target. */
  647. cmd->result = DID_NO_CONNECT << 16;
  648. }
  649. break;
  650. case CMD_PROTOCOL_ERR:
  651. printk(KERN_WARNING "cciss: cp %p has "
  652. "protocol error n", cp);
  653. break;
  654. case CMD_HARDWARE_ERR:
  655. cmd->result = DID_ERROR << 16;
  656. printk(KERN_WARNING "cciss: cp %p had "
  657. " hardware errorn", cp);
  658. break;
  659. case CMD_CONNECTION_LOST:
  660. cmd->result = DID_ERROR << 16;
  661. printk(KERN_WARNING "cciss: cp %p had "
  662. "connection lostn", cp);
  663. break;
  664. case CMD_ABORTED:
  665. cmd->result = DID_ABORT << 16;
  666. printk(KERN_WARNING "cciss: cp %p was "
  667. "abortedn", cp);
  668. break;
  669. case CMD_ABORT_FAILED:
  670. cmd->result = DID_ERROR << 16;
  671. printk(KERN_WARNING "cciss: cp %p reports "
  672. "abort failedn", cp);
  673. break;
  674. case CMD_UNSOLICITED_ABORT:
  675. cmd->result = DID_ABORT << 16;
  676. printk(KERN_WARNING "cciss: cp %p aborted "
  677. "do to an unsolicited abortn", cp);
  678. break;
  679. case CMD_TIMEOUT:
  680. cmd->result = DID_TIME_OUT << 16;
  681. printk(KERN_WARNING "cciss: cp %p timedoutn",
  682. cp);
  683. break;
  684. default:
  685. cmd->result = DID_ERROR << 16;
  686. printk(KERN_WARNING "cciss: cp %p returned "
  687. "unknown status %xn", cp,
  688. ei->CommandStatus);
  689. }
  690. }
  691. cmd->scsi_done(cmd);
  692. scsi_cmd_free(ctlr, cp);
  693. }
  694. /* cciss_scsi_detect is called from the scsi mid layer.
  695.    The scsi mid layer (scsi_register_module) is
  696.    called from cciss.c:cciss_init_one().  */
  697. int __init
  698. cciss_scsi_detect(Scsi_Host_Template *tpnt)
  699. {
  700. int i;
  701. struct Scsi_Host *sh;
  702. /* Tell the kernel we want to be a SCSI driver... */
  703. sh = scsi_register(tpnt, sizeof(struct ctlr_info *));
  704. if (sh == NULL) return 0;
  705. sh->io_port = 0; // good enough?  FIXME,
  706. sh->n_io_port = 0; // I don't think we use these two...
  707. sh->this_id = SELF_SCSI_ID;
  708. /* This is a bit kludgey, using the adapter name to figure out */
  709. /* which scsi host template we've got, won't scale beyond 9 ctlrs. */
  710. i = tpnt->name[5] - '0';
  711. # if MAX_CTLR > 9
  712. # error "cciss_scsi.c: MAX_CTLR > 9, code maintenance needed."
  713. # endif
  714. if (i<0 || i>=MAX_CTLR || hba[i] == NULL) {
  715. /* we didn't find ourself... we shouldn't get here. */
  716. printk("cciss_scsi_detect: could not find ourself in hba[]n");
  717. return 0;
  718. }
  719. ((struct cciss_scsi_adapter_data_t *)
  720. hba[i]->scsi_ctlr)->scsi_host = (void *) sh;
  721. sh->hostdata[0] = (unsigned long) hba[i];
  722. sh->irq = hba[i]->intr;
  723. sh->unique_id = sh->irq;
  724. scsi_set_pci_device(sh, hba[i]->pdev);
  725. return 1; /* Say we have 1 scsi adapter, this will be */
  726. /* called multiple times, once for each adapter */
  727. /* from cciss.c:cciss_init_one().  We do it this */
  728. /* way for PCI-hot plug reasons. (we don't know how */
  729. /* many adapters we have total, so we say we have */
  730. /* 1, each of a unique type.) */
  731. }
  732. static void __exit cleanup_cciss_module(void);
  733. int
  734. cciss_scsi_release(struct Scsi_Host *sh)
  735. {
  736. return 0;
  737. }
  738. static void
  739. cciss_unmap_one(struct pci_dev *pdev,
  740. CommandList_struct *cp,
  741. size_t buflen,
  742. int data_direction)
  743. {
  744. u64bit addr64;
  745. addr64.val32.lower = cp->SG[0].Addr.lower;
  746. addr64.val32.upper = cp->SG[0].Addr.upper;
  747. pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
  748. }
  749. static void
  750. cciss_map_one(struct pci_dev *pdev,
  751. CommandList_struct *cp,
  752. unsigned char *buf,
  753. size_t buflen,
  754. int data_direction)
  755. {
  756. __u64 addr64;
  757. addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
  758. cp->SG[0].Addr.lower =
  759.   (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
  760. cp->SG[0].Addr.upper =
  761.   (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
  762. cp->SG[0].Len = buflen;
  763. cp->Header.SGList = (__u8) 1;   /* no. SGs contig in this cmd */
  764. cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
  765. }
  766. static int
  767. cciss_scsi_do_simple_cmd(ctlr_info_t *c,
  768. CommandList_struct *cp,
  769. unsigned char *scsi3addr,
  770. unsigned char *cdb,
  771. unsigned char cdblen,
  772. unsigned char *buf, int bufsize,
  773. int direction)
  774. {
  775. unsigned long flags;
  776. DECLARE_COMPLETION(wait);
  777. cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
  778. cp->scsi_cmd = NULL;
  779. cp->Header.ReplyQueue = 0;  // unused in simple mode
  780. memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
  781. cp->Header.Tag.lower = cp->busaddr;  // Use k. address of cmd as tag
  782. // Fill in the request block...
  783. /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2n",
  784. scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
  785. scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
  786. memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
  787. memcpy(cp->Request.CDB, cdb, cdblen);
  788. cp->Request.Timeout = 1000; // guarantee completion.
  789. cp->Request.CDBLen = cdblen;
  790. cp->Request.Type.Type = TYPE_CMD;
  791. cp->Request.Type.Attribute = ATTR_SIMPLE;
  792. cp->Request.Type.Direction = direction;
  793. /* Fill in the SG list and do dma mapping */
  794. cciss_map_one(c->pdev, cp,
  795. (unsigned char *) buf, bufsize,
  796. scsi_to_pci_dma_dir(SCSI_DATA_READ));
  797. cp->waiting = &wait;
  798. /* Put the request on the tail of the request queue */
  799. spin_lock_irqsave(&io_request_lock, flags);
  800. addQ(&c->reqQ, cp);
  801. c->Qdepth++;
  802. start_io(c);
  803. spin_unlock_irqrestore(&io_request_lock, flags);
  804. wait_for_completion(&wait);
  805. /* undo the dma mapping */
  806. cciss_unmap_one(c->pdev, cp, bufsize,
  807. scsi_to_pci_dma_dir(SCSI_DATA_READ));
  808. return(0);
  809. }
  810. static void
  811. cciss_scsi_interpret_error(CommandList_struct *cp)
  812. {
  813. ErrorInfo_struct *ei;
  814. ei = cp->err_info;
  815. switch(ei->CommandStatus)
  816. {
  817. case CMD_TARGET_STATUS:
  818. printk(KERN_WARNING "cciss: cmd %p has "
  819. "completed with errorsn", cp);
  820. printk(KERN_WARNING "cciss: cmd %p "
  821. "has SCSI Status = %xn",
  822. cp,
  823. ei->ScsiStatus);
  824. if (ei->ScsiStatus == 0)
  825. printk(KERN_WARNING
  826. "cciss:SCSI status is abnormally zero.  "
  827. "(probably indicates selection timeout "
  828. "reported incorrectly due to a known "
  829. "firmware bug, circa July, 2001.)n");
  830. break;
  831. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  832. printk("UNDERRUNn");
  833. break;
  834. case CMD_DATA_OVERRUN:
  835. printk(KERN_WARNING "cciss: cp %p has"
  836. " completed with data overrun "
  837. "reportedn", cp);
  838. break;
  839. case CMD_INVALID: {
  840. /* controller unfortunately reports SCSI passthru's */
  841. /* to non-existent targets as invalid commands. */
  842. printk(KERN_WARNING "cciss: cp %p is "
  843. "reported invalid (probably means " "target device no longer present)n",
  844. cp);
  845. /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
  846. print_cmd(cp);  */
  847. }
  848. break;
  849. case CMD_PROTOCOL_ERR:
  850. printk(KERN_WARNING "cciss: cp %p has "
  851. "protocol error n", cp);
  852. break;
  853. case CMD_HARDWARE_ERR:
  854. /* cmd->result = DID_ERROR << 16; */
  855. printk(KERN_WARNING "cciss: cp %p had "
  856. " hardware errorn", cp);
  857. break;
  858. case CMD_CONNECTION_LOST:
  859. printk(KERN_WARNING "cciss: cp %p had "
  860. "connection lostn", cp);
  861. break;
  862. case CMD_ABORTED:
  863. printk(KERN_WARNING "cciss: cp %p was "
  864. "abortedn", cp);
  865. break;
  866. case CMD_ABORT_FAILED:
  867. printk(KERN_WARNING "cciss: cp %p reports "
  868. "abort failedn", cp);
  869. break;
  870. case CMD_UNSOLICITED_ABORT:
  871. printk(KERN_WARNING "cciss: cp %p aborted "
  872. "do to an unsolicited abortn", cp);
  873. break;
  874. case CMD_TIMEOUT:
  875. printk(KERN_WARNING "cciss: cp %p timedoutn",
  876. cp);
  877. break;
  878. default:
  879. printk(KERN_WARNING "cciss: cp %p returned "
  880. "unknown status %xn", cp,
  881. ei->CommandStatus);
  882. }
  883. }
  884. static int
  885. cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
  886.  InquiryData_struct *buf)
  887. {
  888. int rc;
  889. CommandList_struct *cp;
  890. char cdb[6];
  891. ErrorInfo_struct *ei;
  892. cp = scsi_cmd_alloc(c);
  893. ei = cp->err_info;
  894. if (cp == NULL) { /* trouble... */
  895. printk("cmd_alloc returned NULL!n");
  896. return -1;
  897. }
  898. cdb[0] = CISS_INQUIRY;
  899. cdb[1] = 0;
  900. cdb[2] = 0;
  901. cdb[3] = 0;
  902. cdb[4] = sizeof(*buf) & 0xff;
  903. cdb[5] = 0;
  904. rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
  905. 6, (unsigned char *) buf,
  906. sizeof(*buf), XFER_READ);
  907. if (rc != 0) return rc; /* something went wrong */
  908. if (ei->CommandStatus != 0 &&
  909.     ei->CommandStatus != CMD_DATA_UNDERRUN) {
  910. cciss_scsi_interpret_error(cp);
  911. scsi_cmd_free(c, cp);
  912. return -1;
  913. }
  914. scsi_cmd_free(c, cp);
  915. return 0;
  916. }
  917. static int
  918. cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
  919. ReportLunData_struct *buf, int bufsize)
  920. {
  921. int rc;
  922. CommandList_struct *cp;
  923. unsigned char cdb[12];
  924. unsigned char scsi3addr[8];
  925. ErrorInfo_struct *ei;
  926. cp = scsi_cmd_alloc(c);
  927. if (cp == NULL) { /* trouble... */
  928. printk("cmd_alloc returned NULL!n");
  929. return -1;
  930. }
  931. memset(&scsi3addr[0], 0, 8); /* address the controller */
  932. cdb[0] = CISS_REPORT_PHYS;
  933. cdb[1] = 0;
  934. cdb[2] = 0;
  935. cdb[3] = 0;
  936. cdb[4] = 0;
  937. cdb[5] = 0;
  938. cdb[6] = (bufsize >> 24) & 0xFF;  //MSB
  939. cdb[7] = (bufsize >> 16) & 0xFF;
  940. cdb[8] = (bufsize >> 8) & 0xFF;
  941. cdb[9] = bufsize & 0xFF;
  942. cdb[10] = 0;
  943. cdb[11] = 0;
  944. rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
  945. cdb, 12,
  946. (unsigned char *) buf,
  947. bufsize, XFER_READ);
  948. if (rc != 0) return rc; /* something went wrong */
  949. ei = cp->err_info;
  950. if (ei->CommandStatus != 0 &&
  951.     ei->CommandStatus != CMD_DATA_UNDERRUN) {
  952. cciss_scsi_interpret_error(cp);
  953. scsi_cmd_free(c, cp);
  954. return -1;
  955. }
  956. scsi_cmd_free(c, cp);
  957. return 0;
  958. }
  959. static void
  960. cciss_update_non_disk_devices(int cntl_num, int hostno)
  961. {
  962. /* the idea here is we could get notified from /proc
  963.    that some devices have changed, so we do a report
  964.    physical luns cmd, and adjust our list of devices
  965.    accordingly.  (We can't rely on the scsi-mid layer just
  966.    doing inquiries, because the "busses" that the scsi
  967.    mid-layer probes are totally fabricated by this driver,
  968.    so new devices wouldn't show up.
  969.    the scsi3addr's of devices won't change so long as the
  970.    adapter is not reset.  That means we can rescan and
  971.    tell which devices we already know about, vs. new
  972.    devices, vs.  disappearing devices.
  973.    Also, if you yank out a tape drive, then put in a disk
  974.    in it's place, (say, a configured volume from another
  975.    array controller for instance)  _don't_ poke this driver
  976.    (so it thinks it's still a tape, but _do_ poke the scsi
  977.    mid layer, so it does an inquiry... the scsi mid layer
  978.    could see the physical disk.  This would be bad.  Need to
  979.    think about how to prevent that.  One idea would be to
  980.    snoop all scsi responses and if an inquiry repsonse comes
  981.    back that reports a disk, chuck it an return selection
  982.    timeout instead and adjust our table...  Not sure i like
  983.    that though.
  984.  */
  985. ReportLunData_struct *ld_buff;
  986. InquiryData_struct *inq_buff;
  987. unsigned char scsi3addr[8];
  988. ctlr_info_t *c;
  989. __u32 num_luns=0;
  990. unsigned char *ch;
  991. /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
  992. struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
  993. int ncurrent=0;
  994. int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
  995. int i;
  996. c = (ctlr_info_t *) hba[cntl_num];
  997. ld_buff = kmalloc(reportlunsize, GFP_KERNEL);
  998. if (ld_buff == NULL) {
  999. printk(KERN_ERR "cciss: out of memoryn");
  1000. return;
  1001. }
  1002. memset(ld_buff, 0, reportlunsize);
  1003. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  1004. if (inq_buff == NULL) {
  1005. printk(KERN_ERR "cciss: out of memoryn");
  1006. kfree(ld_buff);
  1007. return;
  1008. }
  1009. if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
  1010. ch = &ld_buff->LUNListLength[0];
  1011. num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
  1012. if (num_luns > CISS_MAX_PHYS_LUN) {
  1013. printk(KERN_WARNING
  1014. "cciss: Maximum physical LUNs (%d) exceeded.  "
  1015. "%d LUNs ignored.n", CISS_MAX_PHYS_LUN,
  1016. num_luns - CISS_MAX_PHYS_LUN);
  1017. num_luns = CISS_MAX_PHYS_LUN;
  1018. }
  1019. }
  1020. else {
  1021. printk(KERN_ERR  "cciss: Report physical LUNs failed.n");
  1022. return;
  1023. }
  1024. /* adjust our table of devices */
  1025. for(i=0; i<num_luns; i++)
  1026. {
  1027. int devtype;
  1028. /* for each physical lun, do an inquiry */
  1029. memset(inq_buff, 0, sizeof(InquiryData_struct));
  1030. memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
  1031. if (cciss_scsi_do_inquiry(hba[cntl_num],
  1032. scsi3addr, inq_buff) != 0)
  1033. {
  1034. /* Inquiry failed (msg printed already) */
  1035. devtype = 0; /* so we will skip this device. */
  1036. } else /* what kind of device is this? */
  1037. devtype = (inq_buff->data_byte[0] & 0x1f);
  1038. switch (devtype)
  1039. {
  1040.   case 0x01: /* sequential access, (tape) */
  1041.   case 0x08: /* medium changer */
  1042. memcpy(&currentsd[ncurrent].scsi3addr[0],
  1043. &scsi3addr[0], 8);
  1044. currentsd[ncurrent].devtype = devtype;
  1045. currentsd[ncurrent].bus = -1;
  1046. currentsd[ncurrent].target = -1;
  1047. currentsd[ncurrent].lun = -1;
  1048. ncurrent++;
  1049. break;
  1050.   default:
  1051. break;
  1052. }
  1053. }
  1054. adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
  1055. kfree(inq_buff);
  1056. kfree(ld_buff);
  1057. return;
  1058. }
  1059. static int
  1060. is_keyword(char *ptr, int len, char *verb)  // Thanks to ncr53c8xx.c
  1061. {
  1062. int verb_len = strlen(verb);
  1063. if (len >= verb_len && !memcmp(verb,ptr,verb_len))
  1064. return verb_len;
  1065. else
  1066. return 0;
  1067. }
  1068. static int
  1069. cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
  1070. {
  1071. int arg_len;
  1072. if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
  1073. cciss_update_non_disk_devices(ctlr, hostno);
  1074. else
  1075. return -EINVAL;
  1076. return length;
  1077. }
  1078. /* It's a pity that we need this, but, we do... */
  1079. extern struct Scsi_Host *scsi_hostlist;  /* from ../scsi/hosts.c */
  1080. int
  1081. cciss_scsi_proc_info(char *buffer, /* data buffer */
  1082. char **start,     /* where data in buffer starts */
  1083. off_t offset,    /* offset from start of imaginary file */
  1084. int length,     /* length of data in buffer */
  1085. int hostnum,     /* which host adapter (always zero for me) */
  1086. int func)    /* 0 == read, 1 == write */
  1087. {
  1088. int buflen, datalen;
  1089. struct Scsi_Host *sh;
  1090. int found;
  1091. ctlr_info_t *ci;
  1092. int cntl_num;
  1093. /* Lets see if we can find our Scsi_Host...
  1094.    this might be kind of "bad", searching scis_hostlist this way
  1095.    but how else can we find the scsi host?  I think I've seen
  1096.    this coded both ways, (circular list and null terminated list)
  1097.    I coded it to work either way, since I wasn't sure.  */
  1098. sh = scsi_hostlist;
  1099. found=0;
  1100. do {
  1101. if (sh == NULL) break;
  1102. if (sh->host_no == hostnum) {
  1103. found++;
  1104. break;
  1105. }
  1106. sh = sh->next;
  1107. } while (sh != scsi_hostlist && sh != NULL);
  1108. if (sh == NULL || found == 0) /* This really shouldn't ever happen. */
  1109. return -EINVAL;
  1110. ci = (ctlr_info_t *) sh->hostdata[0];
  1111. if (ci == NULL)  /* This really shouldn't ever happen. */
  1112. return -EINVAL;
  1113. cntl_num = ci->ctlr; /* Get our index into the hba[] array */
  1114. if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?*  */
  1115. buflen = sprintf(buffer, "hostnum=%dn", hostnum);
  1116. datalen = buflen - offset;
  1117. if (datalen < 0) {  /* they're reading past EOF. */
  1118. datalen = 0;
  1119. *start = buffer+buflen;
  1120. } else
  1121. *start = buffer + offset;
  1122. return(datalen);
  1123. } else  /* User is writing to /proc/scsi/cciss*?/?*  ... */
  1124. return cciss_scsi_user_command(cntl_num, hostnum,
  1125. buffer, length);
  1126. }
  1127. /* this is via the generic proc support */
  1128. const char *
  1129. cciss_scsi_info(struct Scsi_Host *sa)
  1130. {
  1131. static char buf[300];
  1132. ctlr_info_t *ci;
  1133. /* probably need to work on putting a bit more info in here... */
  1134. /* this is output via the /proc filesystem. */
  1135. ci = (ctlr_info_t *) sa->hostdata[0];
  1136. sprintf(buf, "%s %c%c%c%cn",
  1137. ci->product_name,
  1138. ci->firm_ver[0],
  1139. ci->firm_ver[1],
  1140. ci->firm_ver[2],
  1141. ci->firm_ver[3]);
  1142. return buf;
  1143. }
  1144. /* cciss_scatter_gather takes a Scsi_Cmnd, (cmd), and does the pci
  1145.    dma mapping  and fills in the scatter gather entries of the
  1146.    cciss command, cp. */
  1147. static void
  1148. cciss_scatter_gather(struct pci_dev *pdev,
  1149. CommandList_struct *cp,
  1150. Scsi_Cmnd *cmd)
  1151. {
  1152. unsigned int use_sg, nsegs=0, len;
  1153. struct scatterlist *scatter = (struct scatterlist *) cmd->buffer;
  1154. __u64 addr64;
  1155. /* is it just one virtual address? */
  1156. if (!cmd->use_sg) {
  1157. if (cmd->request_bufflen) { /* anything to xfer? */
  1158. addr64 = (__u64) pci_map_single(pdev,
  1159. cmd->request_buffer,
  1160. cmd->request_bufflen,
  1161. scsi_to_pci_dma_dir(cmd->sc_data_direction));
  1162. cp->SG[0].Addr.lower =
  1163.   (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
  1164. cp->SG[0].Addr.upper =
  1165.   (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
  1166. cp->SG[0].Len = cmd->request_bufflen;
  1167. nsegs=1;
  1168. }
  1169. } /* else, must be a list of virtual addresses.... */
  1170. else if (cmd->use_sg <= MAXSGENTRIES) { /* not too many addrs? */
  1171. use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg,
  1172. scsi_to_pci_dma_dir(cmd->sc_data_direction));
  1173. for (nsegs=0; nsegs < use_sg; nsegs++) {
  1174. addr64 = (__u64) sg_dma_address(&scatter[nsegs]);
  1175. len  = sg_dma_len(&scatter[nsegs]);
  1176. cp->SG[nsegs].Addr.lower =
  1177.   (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
  1178. cp->SG[nsegs].Addr.upper =
  1179.   (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
  1180. cp->SG[nsegs].Len = len;
  1181. cp->SG[nsegs].Ext = 0;  // we are not chaining
  1182. }
  1183. } else BUG();
  1184. cp->Header.SGList = (__u8) nsegs;   /* no. SGs contig in this cmd */
  1185. cp->Header.SGTotal = (__u16) nsegs; /* total sgs in this cmd list */
  1186. return;
  1187. }
  1188. int
  1189. cciss_scsi_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *))
  1190. {
  1191. ctlr_info_t **c;
  1192. int ctlr, rc;
  1193. unsigned char scsi3addr[8];
  1194. CommandList_struct *cp;
  1195. // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
  1196. // We violate cmd->host privacy here.  (Is there another way?)
  1197. c = (ctlr_info_t **) &cmd->host->hostdata[0];
  1198. ctlr = (*c)->ctlr;
  1199. rc = lookup_scsi3addr(ctlr, cmd->channel, cmd->target, cmd->lun,
  1200. scsi3addr);
  1201. if (rc != 0) {
  1202. /* the scsi nexus does not match any that we presented... */
  1203. /* pretend to mid layer that we got selection timeout */
  1204. cmd->result = DID_NO_CONNECT << 16;
  1205. done(cmd);
  1206. /* we might want to think about registering controller itself
  1207.    as a processor device on the bus so sg binds to it. */
  1208. return 0;
  1209. }
  1210. // printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%dn",
  1211. // cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);
  1212. /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
  1213. see what the device thinks of it. */
  1214. cp = scsi_cmd_alloc(*c);
  1215. if (cp == NULL) { /* trouble... */
  1216. printk("scsi_cmd_alloc returned NULL!n");
  1217. /* FIXME: next 3 lines are -> BAD! <- */
  1218. cmd->result = DID_NO_CONNECT << 16;
  1219. done(cmd);
  1220. return 0;
  1221. }
  1222. // Fill in the command list header
  1223. cmd->scsi_done = done;    // save this for use by completion code
  1224. // save cp in case we have to abort it
  1225. cmd->host_scribble = (unsigned char *) cp;
  1226. cp->cmd_type = CMD_SCSI;
  1227. cp->scsi_cmd = cmd;
  1228. cp->Header.ReplyQueue = 0;  // unused in simple mode
  1229. memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  1230. cp->Header.Tag.lower = cp->busaddr;  // Use k. address of cmd as tag
  1231. // Fill in the request block...
  1232. cp->Request.Timeout = 1000; // guarantee completion
  1233. memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
  1234. if (cmd->cmd_len > sizeof(cp->Request.CDB)) BUG();
  1235. cp->Request.CDBLen = cmd->cmd_len;
  1236. memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
  1237. cp->Request.Type.Type = TYPE_CMD;
  1238. cp->Request.Type.Attribute = ATTR_SIMPLE;
  1239. switch(cmd->sc_data_direction)
  1240. {
  1241.   case SCSI_DATA_WRITE: cp->Request.Type.Direction = XFER_WRITE; break;
  1242.   case SCSI_DATA_READ: cp->Request.Type.Direction = XFER_READ; break;
  1243.   case SCSI_DATA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
  1244.   case SCSI_DATA_UNKNOWN:
  1245. // This can happen if a buggy application does a scsi passthru
  1246. // and sets both inlen and outlen to non-zero. ( see
  1247. // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  1248. cp->Request.Type.Direction = XFER_RSVD;
  1249. // This is technically wrong, and cciss controllers should
  1250. // reject it with CMD_INVALID, which is the most correct
  1251. // response, but non-fibre backends appear to let it
  1252. // slide by, and give the same results as if this field
  1253. // were set correctly.  Either way is acceptable for
  1254. // our purposes here.
  1255. break;
  1256.   default:
  1257. printk("cciss: unknown data direction: %dn",
  1258. cmd->sc_data_direction);
  1259. BUG();
  1260. break;
  1261. }
  1262. cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
  1263. /* Put the request on the tail of the request queue */
  1264. addQ(&(*c)->reqQ, cp);
  1265. (*c)->Qdepth++;
  1266. start_io(*c);
  1267. /* the cmd'll come back via intr handler in complete_scsi_command()  */
  1268. return 0;
  1269. }
  1270. static void
  1271. init_driver_template(int ctlr)
  1272. {
  1273. memset(&driver_template[ctlr], 0, sizeof(driver_template[ctlr]));
  1274. driver_template[ctlr].name = ccissscsi[ctlr].name;
  1275. driver_template[ctlr].proc_name = ccissscsi[ctlr].name;
  1276. driver_template[ctlr].detect = cciss_scsi_detect;
  1277. driver_template[ctlr].release = cciss_scsi_release;
  1278. driver_template[ctlr].proc_info = cciss_scsi_proc_info;
  1279. driver_template[ctlr].queuecommand = cciss_scsi_queue_command;
  1280. driver_template[ctlr].eh_abort_handler = NULL;
  1281. driver_template[ctlr].eh_device_reset_handler = NULL;
  1282. driver_template[ctlr].bios_param = scsicam_bios_param;
  1283. driver_template[ctlr].can_queue = SCSI_CCISS_CAN_QUEUE;
  1284. driver_template[ctlr].this_id = SELF_SCSI_ID;
  1285. driver_template[ctlr].sg_tablesize = MAXSGENTRIES;
  1286. driver_template[ctlr].cmd_per_lun = 1;
  1287. driver_template[ctlr].use_new_eh_code = 1;
  1288. driver_template[ctlr].use_clustering = DISABLE_CLUSTERING;
  1289. driver_template[ctlr].module = THIS_MODULE;
  1290. /* set scsi_host to NULL so our detect routine will
  1291.    find us on register */
  1292. ((struct cciss_scsi_adapter_data_t *)
  1293. hba[ctlr]->scsi_ctlr)->scsi_host = NULL;
  1294. }
  1295. static void
  1296. cciss_unregister_scsi(int ctlr)
  1297. {
  1298. struct cciss_scsi_adapter_data_t *sa;
  1299. struct cciss_scsi_cmd_stack_t *stk;
  1300. unsigned long flags;
  1301. /* we are being forcibly unloaded, and may not refuse. */
  1302. spin_lock_irqsave(&io_request_lock, flags);
  1303. sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
  1304. stk = &sa->cmd_stack;
  1305. /* if we weren't ever actually registered, don't unregister */
  1306. if (((struct cciss_scsi_adapter_data_t *)
  1307. hba[ctlr]->scsi_ctlr)->registered) {
  1308. spin_unlock_irqrestore(&io_request_lock, flags);
  1309. scsi_unregister_module(MODULE_SCSI_HA, &driver_template[ctlr]);
  1310. spin_lock_irqsave(&io_request_lock, flags);
  1311. }
  1312. init_driver_template(ctlr);
  1313. scsi_cmd_stack_free(ctlr);
  1314. kfree(hba[ctlr]->scsi_ctlr);
  1315. spin_unlock_irqrestore(&io_request_lock, flags);
  1316. }
  1317. static int
  1318. cciss_register_scsi(int ctlr, int this_is_init_time)
  1319. {
  1320. unsigned long flags;
  1321. CPQ_TAPE_LOCK(ctlr, flags);
  1322. driver_template[ctlr].name = ccissscsi[ctlr].name;
  1323. driver_template[ctlr].proc_name = ccissscsi[ctlr].name;
  1324. driver_template[ctlr].module = THIS_MODULE;;
  1325. /* Since this is really a block driver, the SCSI core may not be
  1326.    initialized yet, in which case, calling scsi_register_module
  1327.    would hang.  instead, we will do it later, via /proc filesystem
  1328.    and rc scripts, when we know SCSI core is good to go. */
  1329. if (this_is_init_time) {
  1330. CPQ_TAPE_UNLOCK(ctlr, flags);
  1331. return 0;
  1332. }
  1333. /* Only register if SCSI devices are detected. */
  1334. if (ccissscsi[ctlr].ndevices != 0) {
  1335. ((struct cciss_scsi_adapter_data_t *)
  1336. hba[ctlr]->scsi_ctlr)->registered = 1;
  1337. CPQ_TAPE_UNLOCK(ctlr, flags);
  1338. return scsi_register_module(MODULE_SCSI_HA,
  1339. &driver_template[ctlr]);
  1340. }
  1341. CPQ_TAPE_UNLOCK(ctlr, flags);
  1342. printk(KERN_INFO
  1343. "cciss%d: No appropriate SCSI device detected, "
  1344. "SCSI subsystem not engaged.n", ctlr);
  1345. return 0;
  1346. }
  1347. static int
  1348. cciss_engage_scsi(int ctlr)
  1349. {
  1350. struct cciss_scsi_adapter_data_t *sa;
  1351. struct cciss_scsi_cmd_stack_t *stk;
  1352. unsigned long flags;
  1353. spin_lock_irqsave(&io_request_lock, flags);
  1354. sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
  1355. stk = &sa->cmd_stack;
  1356. if (((struct cciss_scsi_adapter_data_t *)
  1357. hba[ctlr]->scsi_ctlr)->registered) {
  1358. printk("cciss%d: SCSI subsystem already engaged.n", ctlr);
  1359. spin_unlock_irqrestore(&io_request_lock, flags);
  1360. return ENXIO;
  1361. }
  1362. spin_unlock_irqrestore(&io_request_lock, flags);
  1363. cciss_update_non_disk_devices(ctlr, -1);
  1364. cciss_register_scsi(ctlr, 0);
  1365. return 0;
  1366. }
  1367. static void
  1368. cciss_proc_tape_report(int ctlr, unsigned char *buffer, off_t *pos, off_t *len)
  1369. {
  1370. int size;
  1371. unsigned long flags;
  1372. *pos = *pos -1; *len = *len - 1; // cut off the last trailing newline
  1373. CPQ_TAPE_LOCK(ctlr, flags);
  1374. size = sprintf(buffer + *len,
  1375. "       Sequential access devices: %dnn",
  1376. ccissscsi[ctlr].ndevices);
  1377. CPQ_TAPE_UNLOCK(ctlr, flags);
  1378. *pos += size; *len += size;
  1379. }
  1380. #else /* no CONFIG_CISS_SCSI_TAPE */
  1381. /* If no tape support, then these become defined out of existence */
  1382. #define cciss_find_non_disk_devices(cntl_num)
  1383. #define cciss_unregister_scsi(ctlr)
  1384. #define cciss_register_scsi(ctlr, this_is_init_time)
  1385. #define cciss_proc_tape_report(ctlr, buffer, pos, len)
  1386. #endif /* CONFIG_CISS_SCSI_TAPE */