cciss_scsi.c 44 KB

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