cciss_scsi.c 39 KB

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