aachba.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/sched.h>
  29. #include <linux/pci.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/slab.h>
  32. #include <linux/completion.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/dma-mapping.h>
  35. #include <asm/semaphore.h>
  36. #include <asm/uaccess.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_host.h>
  41. #include "aacraid.h"
  42. /* values for inqd_pdt: Peripheral device type in plain English */
  43. #define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
  44. #define INQD_PDT_PROC 0x03 /* Processor device */
  45. #define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
  46. #define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
  47. #define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
  48. #define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
  49. #define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
  50. #define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
  51. /*
  52. * Sense codes
  53. */
  54. #define SENCODE_NO_SENSE 0x00
  55. #define SENCODE_END_OF_DATA 0x00
  56. #define SENCODE_BECOMING_READY 0x04
  57. #define SENCODE_INIT_CMD_REQUIRED 0x04
  58. #define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
  59. #define SENCODE_INVALID_COMMAND 0x20
  60. #define SENCODE_LBA_OUT_OF_RANGE 0x21
  61. #define SENCODE_INVALID_CDB_FIELD 0x24
  62. #define SENCODE_LUN_NOT_SUPPORTED 0x25
  63. #define SENCODE_INVALID_PARAM_FIELD 0x26
  64. #define SENCODE_PARAM_NOT_SUPPORTED 0x26
  65. #define SENCODE_PARAM_VALUE_INVALID 0x26
  66. #define SENCODE_RESET_OCCURRED 0x29
  67. #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
  68. #define SENCODE_INQUIRY_DATA_CHANGED 0x3F
  69. #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
  70. #define SENCODE_DIAGNOSTIC_FAILURE 0x40
  71. #define SENCODE_INTERNAL_TARGET_FAILURE 0x44
  72. #define SENCODE_INVALID_MESSAGE_ERROR 0x49
  73. #define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
  74. #define SENCODE_OVERLAPPED_COMMAND 0x4E
  75. /*
  76. * Additional sense codes
  77. */
  78. #define ASENCODE_NO_SENSE 0x00
  79. #define ASENCODE_END_OF_DATA 0x05
  80. #define ASENCODE_BECOMING_READY 0x01
  81. #define ASENCODE_INIT_CMD_REQUIRED 0x02
  82. #define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
  83. #define ASENCODE_INVALID_COMMAND 0x00
  84. #define ASENCODE_LBA_OUT_OF_RANGE 0x00
  85. #define ASENCODE_INVALID_CDB_FIELD 0x00
  86. #define ASENCODE_LUN_NOT_SUPPORTED 0x00
  87. #define ASENCODE_INVALID_PARAM_FIELD 0x00
  88. #define ASENCODE_PARAM_NOT_SUPPORTED 0x01
  89. #define ASENCODE_PARAM_VALUE_INVALID 0x02
  90. #define ASENCODE_RESET_OCCURRED 0x00
  91. #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
  92. #define ASENCODE_INQUIRY_DATA_CHANGED 0x03
  93. #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
  94. #define ASENCODE_DIAGNOSTIC_FAILURE 0x80
  95. #define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
  96. #define ASENCODE_INVALID_MESSAGE_ERROR 0x00
  97. #define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
  98. #define ASENCODE_OVERLAPPED_COMMAND 0x00
  99. #define BYTE0(x) (unsigned char)(x)
  100. #define BYTE1(x) (unsigned char)((x) >> 8)
  101. #define BYTE2(x) (unsigned char)((x) >> 16)
  102. #define BYTE3(x) (unsigned char)((x) >> 24)
  103. /*------------------------------------------------------------------------------
  104. * S T R U C T S / T Y P E D E F S
  105. *----------------------------------------------------------------------------*/
  106. /* SCSI inquiry data */
  107. struct inquiry_data {
  108. u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
  109. u8 inqd_dtq; /* RMB | Device Type Qualifier */
  110. u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
  111. u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
  112. u8 inqd_len; /* Additional length (n-4) */
  113. u8 inqd_pad1[2];/* Reserved - must be zero */
  114. u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
  115. u8 inqd_vid[8]; /* Vendor ID */
  116. u8 inqd_pid[16];/* Product ID */
  117. u8 inqd_prl[4]; /* Product Revision Level */
  118. };
  119. /*
  120. * M O D U L E G L O B A L S
  121. */
  122. static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
  123. static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
  124. static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
  125. static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
  126. #ifdef AAC_DETAILED_STATUS_INFO
  127. static char *aac_get_status_string(u32 status);
  128. #endif
  129. /*
  130. * Non dasd selection is handled entirely in aachba now
  131. */
  132. static int nondasd = -1;
  133. static int dacmode = -1;
  134. static int commit = -1;
  135. module_param(nondasd, int, 0);
  136. MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
  137. module_param(dacmode, int, 0);
  138. MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
  139. module_param(commit, int, 0);
  140. MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
  141. int numacb = -1;
  142. module_param(numacb, int, S_IRUGO|S_IWUSR);
  143. MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid\nvalues are 512 and down. Default is to use suggestion from Firmware.");
  144. int acbsize = -1;
  145. module_param(acbsize, int, S_IRUGO|S_IWUSR);
  146. MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512,\n2048, 4096 and 8192. Default is to use suggestion from Firmware.");
  147. /**
  148. * aac_get_config_status - check the adapter configuration
  149. * @common: adapter to query
  150. *
  151. * Query config status, and commit the configuration if needed.
  152. */
  153. int aac_get_config_status(struct aac_dev *dev)
  154. {
  155. int status = 0;
  156. struct fib * fibptr;
  157. if (!(fibptr = aac_fib_alloc(dev)))
  158. return -ENOMEM;
  159. aac_fib_init(fibptr);
  160. {
  161. struct aac_get_config_status *dinfo;
  162. dinfo = (struct aac_get_config_status *) fib_data(fibptr);
  163. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  164. dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
  165. dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
  166. }
  167. status = aac_fib_send(ContainerCommand,
  168. fibptr,
  169. sizeof (struct aac_get_config_status),
  170. FsaNormal,
  171. 1, 1,
  172. NULL, NULL);
  173. if (status < 0 ) {
  174. printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
  175. } else {
  176. struct aac_get_config_status_resp *reply
  177. = (struct aac_get_config_status_resp *) fib_data(fibptr);
  178. dprintk((KERN_WARNING
  179. "aac_get_config_status: response=%d status=%d action=%d\n",
  180. le32_to_cpu(reply->response),
  181. le32_to_cpu(reply->status),
  182. le32_to_cpu(reply->data.action)));
  183. if ((le32_to_cpu(reply->response) != ST_OK) ||
  184. (le32_to_cpu(reply->status) != CT_OK) ||
  185. (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
  186. printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
  187. status = -EINVAL;
  188. }
  189. }
  190. aac_fib_complete(fibptr);
  191. /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
  192. if (status >= 0) {
  193. if (commit == 1) {
  194. struct aac_commit_config * dinfo;
  195. aac_fib_init(fibptr);
  196. dinfo = (struct aac_commit_config *) fib_data(fibptr);
  197. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  198. dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
  199. status = aac_fib_send(ContainerCommand,
  200. fibptr,
  201. sizeof (struct aac_commit_config),
  202. FsaNormal,
  203. 1, 1,
  204. NULL, NULL);
  205. aac_fib_complete(fibptr);
  206. } else if (commit == 0) {
  207. printk(KERN_WARNING
  208. "aac_get_config_status: Foreign device configurations are being ignored\n");
  209. }
  210. }
  211. aac_fib_free(fibptr);
  212. return status;
  213. }
  214. /**
  215. * aac_get_containers - list containers
  216. * @common: adapter to probe
  217. *
  218. * Make a list of all containers on this controller
  219. */
  220. int aac_get_containers(struct aac_dev *dev)
  221. {
  222. struct fsa_dev_info *fsa_dev_ptr;
  223. u32 index;
  224. int status = 0;
  225. struct fib * fibptr;
  226. unsigned instance;
  227. struct aac_get_container_count *dinfo;
  228. struct aac_get_container_count_resp *dresp;
  229. int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
  230. instance = dev->scsi_host_ptr->unique_id;
  231. if (!(fibptr = aac_fib_alloc(dev)))
  232. return -ENOMEM;
  233. aac_fib_init(fibptr);
  234. dinfo = (struct aac_get_container_count *) fib_data(fibptr);
  235. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  236. dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
  237. status = aac_fib_send(ContainerCommand,
  238. fibptr,
  239. sizeof (struct aac_get_container_count),
  240. FsaNormal,
  241. 1, 1,
  242. NULL, NULL);
  243. if (status >= 0) {
  244. dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
  245. maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
  246. aac_fib_complete(fibptr);
  247. }
  248. if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
  249. maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
  250. fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
  251. sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
  252. if (!fsa_dev_ptr) {
  253. aac_fib_free(fibptr);
  254. return -ENOMEM;
  255. }
  256. memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
  257. dev->fsa_dev = fsa_dev_ptr;
  258. dev->maximum_num_containers = maximum_num_containers;
  259. for (index = 0; index < dev->maximum_num_containers; index++) {
  260. struct aac_query_mount *dinfo;
  261. struct aac_mount *dresp;
  262. fsa_dev_ptr[index].devname[0] = '\0';
  263. aac_fib_init(fibptr);
  264. dinfo = (struct aac_query_mount *) fib_data(fibptr);
  265. dinfo->command = cpu_to_le32(VM_NameServe);
  266. dinfo->count = cpu_to_le32(index);
  267. dinfo->type = cpu_to_le32(FT_FILESYS);
  268. status = aac_fib_send(ContainerCommand,
  269. fibptr,
  270. sizeof (struct aac_query_mount),
  271. FsaNormal,
  272. 1, 1,
  273. NULL, NULL);
  274. if (status < 0 ) {
  275. printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
  276. break;
  277. }
  278. dresp = (struct aac_mount *)fib_data(fibptr);
  279. if ((le32_to_cpu(dresp->status) == ST_OK) &&
  280. (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
  281. dinfo->command = cpu_to_le32(VM_NameServe64);
  282. dinfo->count = cpu_to_le32(index);
  283. dinfo->type = cpu_to_le32(FT_FILESYS);
  284. if (aac_fib_send(ContainerCommand,
  285. fibptr,
  286. sizeof(struct aac_query_mount),
  287. FsaNormal,
  288. 1, 1,
  289. NULL, NULL) < 0)
  290. continue;
  291. } else
  292. dresp->mnt[0].capacityhigh = 0;
  293. dprintk ((KERN_DEBUG
  294. "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%llu\n",
  295. (int)index, (int)le32_to_cpu(dresp->status),
  296. (int)le32_to_cpu(dresp->mnt[0].vol),
  297. (int)le32_to_cpu(dresp->mnt[0].state),
  298. ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
  299. (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32)));
  300. if ((le32_to_cpu(dresp->status) == ST_OK) &&
  301. (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
  302. (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
  303. fsa_dev_ptr[index].valid = 1;
  304. fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
  305. fsa_dev_ptr[index].size
  306. = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
  307. (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
  308. if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
  309. fsa_dev_ptr[index].ro = 1;
  310. }
  311. aac_fib_complete(fibptr);
  312. /*
  313. * If there are no more containers, then stop asking.
  314. */
  315. if ((index + 1) >= le32_to_cpu(dresp->count)){
  316. break;
  317. }
  318. }
  319. aac_fib_free(fibptr);
  320. return status;
  321. }
  322. static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
  323. {
  324. void *buf;
  325. unsigned int transfer_len;
  326. struct scatterlist *sg = scsicmd->request_buffer;
  327. if (scsicmd->use_sg) {
  328. buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
  329. transfer_len = min(sg->length, len + offset);
  330. } else {
  331. buf = scsicmd->request_buffer;
  332. transfer_len = min(scsicmd->request_bufflen, len + offset);
  333. }
  334. memcpy(buf + offset, data, transfer_len - offset);
  335. if (scsicmd->use_sg)
  336. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  337. }
  338. static void get_container_name_callback(void *context, struct fib * fibptr)
  339. {
  340. struct aac_get_name_resp * get_name_reply;
  341. struct scsi_cmnd * scsicmd;
  342. scsicmd = (struct scsi_cmnd *) context;
  343. scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
  344. dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
  345. if (fibptr == NULL)
  346. BUG();
  347. get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
  348. /* Failure is irrelevant, using default value instead */
  349. if ((le32_to_cpu(get_name_reply->status) == CT_OK)
  350. && (get_name_reply->data[0] != '\0')) {
  351. char *sp = get_name_reply->data;
  352. sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
  353. while (*sp == ' ')
  354. ++sp;
  355. if (*sp) {
  356. char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
  357. int count = sizeof(d);
  358. char *dp = d;
  359. do {
  360. *dp++ = (*sp) ? *sp++ : ' ';
  361. } while (--count > 0);
  362. aac_internal_transfer(scsicmd, d,
  363. offsetof(struct inquiry_data, inqd_pid), sizeof(d));
  364. }
  365. }
  366. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  367. aac_fib_complete(fibptr);
  368. aac_fib_free(fibptr);
  369. scsicmd->scsi_done(scsicmd);
  370. }
  371. /**
  372. * aac_get_container_name - get container name, none blocking.
  373. */
  374. static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
  375. {
  376. int status;
  377. struct aac_get_name *dinfo;
  378. struct fib * cmd_fibcontext;
  379. struct aac_dev * dev;
  380. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  381. if (!(cmd_fibcontext = aac_fib_alloc(dev)))
  382. return -ENOMEM;
  383. aac_fib_init(cmd_fibcontext);
  384. dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
  385. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  386. dinfo->type = cpu_to_le32(CT_READ_NAME);
  387. dinfo->cid = cpu_to_le32(cid);
  388. dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
  389. status = aac_fib_send(ContainerCommand,
  390. cmd_fibcontext,
  391. sizeof (struct aac_get_name),
  392. FsaNormal,
  393. 0, 1,
  394. (fib_callback) get_container_name_callback,
  395. (void *) scsicmd);
  396. /*
  397. * Check that the command queued to the controller
  398. */
  399. if (status == -EINPROGRESS) {
  400. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  401. return 0;
  402. }
  403. printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
  404. aac_fib_complete(cmd_fibcontext);
  405. aac_fib_free(cmd_fibcontext);
  406. return -1;
  407. }
  408. /**
  409. * aac_probe_container - query a logical volume
  410. * @dev: device to query
  411. * @cid: container identifier
  412. *
  413. * Queries the controller about the given volume. The volume information
  414. * is updated in the struct fsa_dev_info structure rather than returned.
  415. */
  416. int aac_probe_container(struct aac_dev *dev, int cid)
  417. {
  418. struct fsa_dev_info *fsa_dev_ptr;
  419. int status;
  420. struct aac_query_mount *dinfo;
  421. struct aac_mount *dresp;
  422. struct fib * fibptr;
  423. unsigned instance;
  424. fsa_dev_ptr = dev->fsa_dev;
  425. instance = dev->scsi_host_ptr->unique_id;
  426. if (!(fibptr = aac_fib_alloc(dev)))
  427. return -ENOMEM;
  428. aac_fib_init(fibptr);
  429. dinfo = (struct aac_query_mount *)fib_data(fibptr);
  430. dinfo->command = cpu_to_le32(VM_NameServe);
  431. dinfo->count = cpu_to_le32(cid);
  432. dinfo->type = cpu_to_le32(FT_FILESYS);
  433. status = aac_fib_send(ContainerCommand,
  434. fibptr,
  435. sizeof(struct aac_query_mount),
  436. FsaNormal,
  437. 1, 1,
  438. NULL, NULL);
  439. if (status < 0) {
  440. printk(KERN_WARNING "aacraid: aac_probe_container query failed.\n");
  441. goto error;
  442. }
  443. dresp = (struct aac_mount *) fib_data(fibptr);
  444. if ((le32_to_cpu(dresp->status) == ST_OK) &&
  445. (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
  446. dinfo->command = cpu_to_le32(VM_NameServe64);
  447. dinfo->count = cpu_to_le32(cid);
  448. dinfo->type = cpu_to_le32(FT_FILESYS);
  449. if (aac_fib_send(ContainerCommand,
  450. fibptr,
  451. sizeof(struct aac_query_mount),
  452. FsaNormal,
  453. 1, 1,
  454. NULL, NULL) < 0)
  455. goto error;
  456. } else
  457. dresp->mnt[0].capacityhigh = 0;
  458. if ((le32_to_cpu(dresp->status) == ST_OK) &&
  459. (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
  460. (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
  461. fsa_dev_ptr[cid].valid = 1;
  462. fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
  463. fsa_dev_ptr[cid].size
  464. = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
  465. (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
  466. if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
  467. fsa_dev_ptr[cid].ro = 1;
  468. }
  469. error:
  470. aac_fib_complete(fibptr);
  471. aac_fib_free(fibptr);
  472. return status;
  473. }
  474. /* Local Structure to set SCSI inquiry data strings */
  475. struct scsi_inq {
  476. char vid[8]; /* Vendor ID */
  477. char pid[16]; /* Product ID */
  478. char prl[4]; /* Product Revision Level */
  479. };
  480. /**
  481. * InqStrCopy - string merge
  482. * @a: string to copy from
  483. * @b: string to copy to
  484. *
  485. * Copy a String from one location to another
  486. * without copying \0
  487. */
  488. static void inqstrcpy(char *a, char *b)
  489. {
  490. while(*a != (char)0)
  491. *b++ = *a++;
  492. }
  493. static char *container_types[] = {
  494. "None",
  495. "Volume",
  496. "Mirror",
  497. "Stripe",
  498. "RAID5",
  499. "SSRW",
  500. "SSRO",
  501. "Morph",
  502. "Legacy",
  503. "RAID4",
  504. "RAID10",
  505. "RAID00",
  506. "V-MIRRORS",
  507. "PSEUDO R4",
  508. "RAID50",
  509. "RAID5D",
  510. "RAID5D0",
  511. "RAID1E",
  512. "RAID6",
  513. "RAID60",
  514. "Unknown"
  515. };
  516. /* Function: setinqstr
  517. *
  518. * Arguments: [1] pointer to void [1] int
  519. *
  520. * Purpose: Sets SCSI inquiry data strings for vendor, product
  521. * and revision level. Allows strings to be set in platform dependant
  522. * files instead of in OS dependant driver source.
  523. */
  524. static void setinqstr(struct aac_dev *dev, void *data, int tindex)
  525. {
  526. struct scsi_inq *str;
  527. str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
  528. memset(str, ' ', sizeof(*str));
  529. if (dev->supplement_adapter_info.AdapterTypeText[0]) {
  530. char * cp = dev->supplement_adapter_info.AdapterTypeText;
  531. int c = sizeof(str->vid);
  532. while (*cp && *cp != ' ' && --c)
  533. ++cp;
  534. c = *cp;
  535. *cp = '\0';
  536. inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
  537. str->vid);
  538. *cp = c;
  539. while (*cp && *cp != ' ')
  540. ++cp;
  541. while (*cp == ' ')
  542. ++cp;
  543. /* last six chars reserved for vol type */
  544. c = 0;
  545. if (strlen(cp) > sizeof(str->pid)) {
  546. c = cp[sizeof(str->pid)];
  547. cp[sizeof(str->pid)] = '\0';
  548. }
  549. inqstrcpy (cp, str->pid);
  550. if (c)
  551. cp[sizeof(str->pid)] = c;
  552. } else {
  553. struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
  554. inqstrcpy (mp->vname, str->vid);
  555. /* last six chars reserved for vol type */
  556. inqstrcpy (mp->model, str->pid);
  557. }
  558. if (tindex < (sizeof(container_types)/sizeof(char *))){
  559. char *findit = str->pid;
  560. for ( ; *findit != ' '; findit++); /* walk till we find a space */
  561. /* RAID is superfluous in the context of a RAID device */
  562. if (memcmp(findit-4, "RAID", 4) == 0)
  563. *(findit -= 4) = ' ';
  564. if (((findit - str->pid) + strlen(container_types[tindex]))
  565. < (sizeof(str->pid) + sizeof(str->prl)))
  566. inqstrcpy (container_types[tindex], findit + 1);
  567. }
  568. inqstrcpy ("V1.0", str->prl);
  569. }
  570. static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
  571. u8 a_sense_code, u8 incorrect_length,
  572. u8 bit_pointer, u16 field_pointer,
  573. u32 residue)
  574. {
  575. sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
  576. sense_buf[1] = 0; /* Segment number, always zero */
  577. if (incorrect_length) {
  578. sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
  579. sense_buf[3] = BYTE3(residue);
  580. sense_buf[4] = BYTE2(residue);
  581. sense_buf[5] = BYTE1(residue);
  582. sense_buf[6] = BYTE0(residue);
  583. } else
  584. sense_buf[2] = sense_key; /* Sense key */
  585. if (sense_key == ILLEGAL_REQUEST)
  586. sense_buf[7] = 10; /* Additional sense length */
  587. else
  588. sense_buf[7] = 6; /* Additional sense length */
  589. sense_buf[12] = sense_code; /* Additional sense code */
  590. sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
  591. if (sense_key == ILLEGAL_REQUEST) {
  592. sense_buf[15] = 0;
  593. if (sense_code == SENCODE_INVALID_PARAM_FIELD)
  594. sense_buf[15] = 0x80;/* Std sense key specific field */
  595. /* Illegal parameter is in the parameter block */
  596. if (sense_code == SENCODE_INVALID_CDB_FIELD)
  597. sense_buf[15] = 0xc0;/* Std sense key specific field */
  598. /* Illegal parameter is in the CDB block */
  599. sense_buf[15] |= bit_pointer;
  600. sense_buf[16] = field_pointer >> 8; /* MSB */
  601. sense_buf[17] = field_pointer; /* LSB */
  602. }
  603. }
  604. int aac_get_adapter_info(struct aac_dev* dev)
  605. {
  606. struct fib* fibptr;
  607. int rcode;
  608. u32 tmp;
  609. struct aac_adapter_info *info;
  610. struct aac_bus_info *command;
  611. struct aac_bus_info_response *bus_info;
  612. if (!(fibptr = aac_fib_alloc(dev)))
  613. return -ENOMEM;
  614. aac_fib_init(fibptr);
  615. info = (struct aac_adapter_info *) fib_data(fibptr);
  616. memset(info,0,sizeof(*info));
  617. rcode = aac_fib_send(RequestAdapterInfo,
  618. fibptr,
  619. sizeof(*info),
  620. FsaNormal,
  621. -1, 1, /* First `interrupt' command uses special wait */
  622. NULL,
  623. NULL);
  624. if (rcode < 0) {
  625. aac_fib_complete(fibptr);
  626. aac_fib_free(fibptr);
  627. return rcode;
  628. }
  629. memcpy(&dev->adapter_info, info, sizeof(*info));
  630. if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
  631. struct aac_supplement_adapter_info * info;
  632. aac_fib_init(fibptr);
  633. info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
  634. memset(info,0,sizeof(*info));
  635. rcode = aac_fib_send(RequestSupplementAdapterInfo,
  636. fibptr,
  637. sizeof(*info),
  638. FsaNormal,
  639. 1, 1,
  640. NULL,
  641. NULL);
  642. if (rcode >= 0)
  643. memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
  644. }
  645. /*
  646. * GetBusInfo
  647. */
  648. aac_fib_init(fibptr);
  649. bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
  650. memset(bus_info, 0, sizeof(*bus_info));
  651. command = (struct aac_bus_info *)bus_info;
  652. command->Command = cpu_to_le32(VM_Ioctl);
  653. command->ObjType = cpu_to_le32(FT_DRIVE);
  654. command->MethodId = cpu_to_le32(1);
  655. command->CtlCmd = cpu_to_le32(GetBusInfo);
  656. rcode = aac_fib_send(ContainerCommand,
  657. fibptr,
  658. sizeof (*bus_info),
  659. FsaNormal,
  660. 1, 1,
  661. NULL, NULL);
  662. if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
  663. dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
  664. dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
  665. }
  666. tmp = le32_to_cpu(dev->adapter_info.kernelrev);
  667. printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
  668. dev->name,
  669. dev->id,
  670. tmp>>24,
  671. (tmp>>16)&0xff,
  672. tmp&0xff,
  673. le32_to_cpu(dev->adapter_info.kernelbuild),
  674. (int)sizeof(dev->supplement_adapter_info.BuildDate),
  675. dev->supplement_adapter_info.BuildDate);
  676. tmp = le32_to_cpu(dev->adapter_info.monitorrev);
  677. printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
  678. dev->name, dev->id,
  679. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  680. le32_to_cpu(dev->adapter_info.monitorbuild));
  681. tmp = le32_to_cpu(dev->adapter_info.biosrev);
  682. printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
  683. dev->name, dev->id,
  684. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  685. le32_to_cpu(dev->adapter_info.biosbuild));
  686. if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
  687. printk(KERN_INFO "%s%d: serial %x\n",
  688. dev->name, dev->id,
  689. le32_to_cpu(dev->adapter_info.serial[0]));
  690. dev->nondasd_support = 0;
  691. dev->raid_scsi_mode = 0;
  692. if(dev->adapter_info.options & AAC_OPT_NONDASD){
  693. dev->nondasd_support = 1;
  694. }
  695. /*
  696. * If the firmware supports ROMB RAID/SCSI mode and we are currently
  697. * in RAID/SCSI mode, set the flag. For now if in this mode we will
  698. * force nondasd support on. If we decide to allow the non-dasd flag
  699. * additional changes changes will have to be made to support
  700. * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
  701. * changed to support the new dev->raid_scsi_mode flag instead of
  702. * leaching off of the dev->nondasd_support flag. Also in linit.c the
  703. * function aac_detect will have to be modified where it sets up the
  704. * max number of channels based on the aac->nondasd_support flag only.
  705. */
  706. if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
  707. (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
  708. dev->nondasd_support = 1;
  709. dev->raid_scsi_mode = 1;
  710. }
  711. if (dev->raid_scsi_mode != 0)
  712. printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
  713. dev->name, dev->id);
  714. if(nondasd != -1) {
  715. dev->nondasd_support = (nondasd!=0);
  716. }
  717. if(dev->nondasd_support != 0){
  718. printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
  719. }
  720. dev->dac_support = 0;
  721. if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
  722. printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
  723. dev->dac_support = 1;
  724. }
  725. if(dacmode != -1) {
  726. dev->dac_support = (dacmode!=0);
  727. }
  728. if(dev->dac_support != 0) {
  729. if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
  730. !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
  731. printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
  732. dev->name, dev->id);
  733. } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
  734. !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
  735. printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
  736. dev->name, dev->id);
  737. dev->dac_support = 0;
  738. } else {
  739. printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
  740. dev->name, dev->id);
  741. rcode = -ENOMEM;
  742. }
  743. }
  744. /*
  745. * 57 scatter gather elements
  746. */
  747. if (!(dev->raw_io_interface)) {
  748. dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
  749. sizeof(struct aac_fibhdr) -
  750. sizeof(struct aac_write) + sizeof(struct sgentry)) /
  751. sizeof(struct sgentry);
  752. if (dev->dac_support) {
  753. /*
  754. * 38 scatter gather elements
  755. */
  756. dev->scsi_host_ptr->sg_tablesize =
  757. (dev->max_fib_size -
  758. sizeof(struct aac_fibhdr) -
  759. sizeof(struct aac_write64) +
  760. sizeof(struct sgentry64)) /
  761. sizeof(struct sgentry64);
  762. }
  763. dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
  764. if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
  765. /*
  766. * Worst case size that could cause sg overflow when
  767. * we break up SG elements that are larger than 64KB.
  768. * Would be nice if we could tell the SCSI layer what
  769. * the maximum SG element size can be. Worst case is
  770. * (sg_tablesize-1) 4KB elements with one 64KB
  771. * element.
  772. * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
  773. */
  774. dev->scsi_host_ptr->max_sectors =
  775. (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
  776. }
  777. }
  778. aac_fib_complete(fibptr);
  779. aac_fib_free(fibptr);
  780. return rcode;
  781. }
  782. static void io_callback(void *context, struct fib * fibptr)
  783. {
  784. struct aac_dev *dev;
  785. struct aac_read_reply *readreply;
  786. struct scsi_cmnd *scsicmd;
  787. u32 cid;
  788. scsicmd = (struct scsi_cmnd *) context;
  789. scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
  790. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  791. cid = scmd_id(scsicmd);
  792. if (nblank(dprintk(x))) {
  793. u64 lba;
  794. switch (scsicmd->cmnd[0]) {
  795. case WRITE_6:
  796. case READ_6:
  797. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
  798. (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  799. break;
  800. case WRITE_16:
  801. case READ_16:
  802. lba = ((u64)scsicmd->cmnd[2] << 56) |
  803. ((u64)scsicmd->cmnd[3] << 48) |
  804. ((u64)scsicmd->cmnd[4] << 40) |
  805. ((u64)scsicmd->cmnd[5] << 32) |
  806. ((u64)scsicmd->cmnd[6] << 24) |
  807. (scsicmd->cmnd[7] << 16) |
  808. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  809. break;
  810. case WRITE_12:
  811. case READ_12:
  812. lba = ((u64)scsicmd->cmnd[2] << 24) |
  813. (scsicmd->cmnd[3] << 16) |
  814. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  815. break;
  816. default:
  817. lba = ((u64)scsicmd->cmnd[2] << 24) |
  818. (scsicmd->cmnd[3] << 16) |
  819. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  820. break;
  821. }
  822. printk(KERN_DEBUG
  823. "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
  824. smp_processor_id(), (unsigned long long)lba, jiffies);
  825. }
  826. if (fibptr == NULL)
  827. BUG();
  828. if(scsicmd->use_sg)
  829. pci_unmap_sg(dev->pdev,
  830. (struct scatterlist *)scsicmd->buffer,
  831. scsicmd->use_sg,
  832. scsicmd->sc_data_direction);
  833. else if(scsicmd->request_bufflen)
  834. pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
  835. scsicmd->request_bufflen,
  836. scsicmd->sc_data_direction);
  837. readreply = (struct aac_read_reply *)fib_data(fibptr);
  838. if (le32_to_cpu(readreply->status) == ST_OK)
  839. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  840. else {
  841. #ifdef AAC_DETAILED_STATUS_INFO
  842. printk(KERN_WARNING "io_callback: io failed, status = %d\n",
  843. le32_to_cpu(readreply->status));
  844. #endif
  845. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  846. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  847. HARDWARE_ERROR,
  848. SENCODE_INTERNAL_TARGET_FAILURE,
  849. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  850. 0, 0);
  851. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  852. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  853. ? sizeof(scsicmd->sense_buffer)
  854. : sizeof(dev->fsa_dev[cid].sense_data));
  855. }
  856. aac_fib_complete(fibptr);
  857. aac_fib_free(fibptr);
  858. scsicmd->scsi_done(scsicmd);
  859. }
  860. static int aac_read(struct scsi_cmnd * scsicmd, int cid)
  861. {
  862. u64 lba;
  863. u32 count;
  864. int status;
  865. u16 fibsize;
  866. struct aac_dev *dev;
  867. struct fib * cmd_fibcontext;
  868. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  869. /*
  870. * Get block address and transfer length
  871. */
  872. switch (scsicmd->cmnd[0]) {
  873. case READ_6:
  874. dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
  875. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
  876. (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  877. count = scsicmd->cmnd[4];
  878. if (count == 0)
  879. count = 256;
  880. break;
  881. case READ_16:
  882. dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
  883. lba = ((u64)scsicmd->cmnd[2] << 56) |
  884. ((u64)scsicmd->cmnd[3] << 48) |
  885. ((u64)scsicmd->cmnd[4] << 40) |
  886. ((u64)scsicmd->cmnd[5] << 32) |
  887. ((u64)scsicmd->cmnd[6] << 24) |
  888. (scsicmd->cmnd[7] << 16) |
  889. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  890. count = (scsicmd->cmnd[10] << 24) |
  891. (scsicmd->cmnd[11] << 16) |
  892. (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
  893. break;
  894. case READ_12:
  895. dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
  896. lba = ((u64)scsicmd->cmnd[2] << 24) |
  897. (scsicmd->cmnd[3] << 16) |
  898. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  899. count = (scsicmd->cmnd[6] << 24) |
  900. (scsicmd->cmnd[7] << 16) |
  901. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  902. break;
  903. default:
  904. dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
  905. lba = ((u64)scsicmd->cmnd[2] << 24) |
  906. (scsicmd->cmnd[3] << 16) |
  907. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  908. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  909. break;
  910. }
  911. dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
  912. smp_processor_id(), (unsigned long long)lba, jiffies));
  913. if ((!(dev->raw_io_interface) || !(dev->raw_io_64)) &&
  914. (lba & 0xffffffff00000000LL)) {
  915. dprintk((KERN_DEBUG "aac_read: Illegal lba\n"));
  916. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  917. SAM_STAT_CHECK_CONDITION;
  918. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  919. HARDWARE_ERROR,
  920. SENCODE_INTERNAL_TARGET_FAILURE,
  921. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  922. 0, 0);
  923. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  924. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  925. ? sizeof(scsicmd->sense_buffer)
  926. : sizeof(dev->fsa_dev[cid].sense_data));
  927. scsicmd->scsi_done(scsicmd);
  928. return 0;
  929. }
  930. /*
  931. * Alocate and initialize a Fib
  932. */
  933. if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
  934. return -1;
  935. }
  936. aac_fib_init(cmd_fibcontext);
  937. if (dev->raw_io_interface) {
  938. struct aac_raw_io *readcmd;
  939. readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
  940. readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
  941. readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  942. readcmd->count = cpu_to_le32(count<<9);
  943. readcmd->cid = cpu_to_le16(cid);
  944. readcmd->flags = cpu_to_le16(1);
  945. readcmd->bpTotal = 0;
  946. readcmd->bpComplete = 0;
  947. aac_build_sgraw(scsicmd, &readcmd->sg);
  948. fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
  949. if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
  950. BUG();
  951. /*
  952. * Now send the Fib to the adapter
  953. */
  954. status = aac_fib_send(ContainerRawIo,
  955. cmd_fibcontext,
  956. fibsize,
  957. FsaNormal,
  958. 0, 1,
  959. (fib_callback) io_callback,
  960. (void *) scsicmd);
  961. } else if (dev->dac_support == 1) {
  962. struct aac_read64 *readcmd;
  963. readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
  964. readcmd->command = cpu_to_le32(VM_CtHostRead64);
  965. readcmd->cid = cpu_to_le16(cid);
  966. readcmd->sector_count = cpu_to_le16(count);
  967. readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  968. readcmd->pad = 0;
  969. readcmd->flags = 0;
  970. aac_build_sg64(scsicmd, &readcmd->sg);
  971. fibsize = sizeof(struct aac_read64) +
  972. ((le32_to_cpu(readcmd->sg.count) - 1) *
  973. sizeof (struct sgentry64));
  974. BUG_ON (fibsize > (dev->max_fib_size -
  975. sizeof(struct aac_fibhdr)));
  976. /*
  977. * Now send the Fib to the adapter
  978. */
  979. status = aac_fib_send(ContainerCommand64,
  980. cmd_fibcontext,
  981. fibsize,
  982. FsaNormal,
  983. 0, 1,
  984. (fib_callback) io_callback,
  985. (void *) scsicmd);
  986. } else {
  987. struct aac_read *readcmd;
  988. readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
  989. readcmd->command = cpu_to_le32(VM_CtBlockRead);
  990. readcmd->cid = cpu_to_le32(cid);
  991. readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  992. readcmd->count = cpu_to_le32(count * 512);
  993. aac_build_sg(scsicmd, &readcmd->sg);
  994. fibsize = sizeof(struct aac_read) +
  995. ((le32_to_cpu(readcmd->sg.count) - 1) *
  996. sizeof (struct sgentry));
  997. BUG_ON (fibsize > (dev->max_fib_size -
  998. sizeof(struct aac_fibhdr)));
  999. /*
  1000. * Now send the Fib to the adapter
  1001. */
  1002. status = aac_fib_send(ContainerCommand,
  1003. cmd_fibcontext,
  1004. fibsize,
  1005. FsaNormal,
  1006. 0, 1,
  1007. (fib_callback) io_callback,
  1008. (void *) scsicmd);
  1009. }
  1010. /*
  1011. * Check that the command queued to the controller
  1012. */
  1013. if (status == -EINPROGRESS) {
  1014. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1015. return 0;
  1016. }
  1017. printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
  1018. /*
  1019. * For some reason, the Fib didn't queue, return QUEUE_FULL
  1020. */
  1021. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  1022. scsicmd->scsi_done(scsicmd);
  1023. aac_fib_complete(cmd_fibcontext);
  1024. aac_fib_free(cmd_fibcontext);
  1025. return 0;
  1026. }
  1027. static int aac_write(struct scsi_cmnd * scsicmd, int cid)
  1028. {
  1029. u64 lba;
  1030. u32 count;
  1031. int status;
  1032. u16 fibsize;
  1033. struct aac_dev *dev;
  1034. struct fib * cmd_fibcontext;
  1035. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1036. /*
  1037. * Get block address and transfer length
  1038. */
  1039. if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
  1040. {
  1041. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  1042. count = scsicmd->cmnd[4];
  1043. if (count == 0)
  1044. count = 256;
  1045. } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
  1046. dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
  1047. lba = ((u64)scsicmd->cmnd[2] << 56) |
  1048. ((u64)scsicmd->cmnd[3] << 48) |
  1049. ((u64)scsicmd->cmnd[4] << 40) |
  1050. ((u64)scsicmd->cmnd[5] << 32) |
  1051. ((u64)scsicmd->cmnd[6] << 24) |
  1052. (scsicmd->cmnd[7] << 16) |
  1053. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1054. count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
  1055. (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
  1056. } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
  1057. dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
  1058. lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
  1059. | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1060. count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
  1061. | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1062. } else {
  1063. dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
  1064. lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1065. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  1066. }
  1067. dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
  1068. smp_processor_id(), (unsigned long long)lba, jiffies));
  1069. if ((!(dev->raw_io_interface) || !(dev->raw_io_64))
  1070. && (lba & 0xffffffff00000000LL)) {
  1071. dprintk((KERN_DEBUG "aac_write: Illegal lba\n"));
  1072. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1073. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1074. HARDWARE_ERROR,
  1075. SENCODE_INTERNAL_TARGET_FAILURE,
  1076. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  1077. 0, 0);
  1078. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1079. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1080. ? sizeof(scsicmd->sense_buffer)
  1081. : sizeof(dev->fsa_dev[cid].sense_data));
  1082. scsicmd->scsi_done(scsicmd);
  1083. return 0;
  1084. }
  1085. /*
  1086. * Allocate and initialize a Fib then setup a BlockWrite command
  1087. */
  1088. if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
  1089. scsicmd->result = DID_ERROR << 16;
  1090. scsicmd->scsi_done(scsicmd);
  1091. return 0;
  1092. }
  1093. aac_fib_init(cmd_fibcontext);
  1094. if (dev->raw_io_interface) {
  1095. struct aac_raw_io *writecmd;
  1096. writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
  1097. writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
  1098. writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  1099. writecmd->count = cpu_to_le32(count<<9);
  1100. writecmd->cid = cpu_to_le16(cid);
  1101. writecmd->flags = 0;
  1102. writecmd->bpTotal = 0;
  1103. writecmd->bpComplete = 0;
  1104. aac_build_sgraw(scsicmd, &writecmd->sg);
  1105. fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
  1106. if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
  1107. BUG();
  1108. /*
  1109. * Now send the Fib to the adapter
  1110. */
  1111. status = aac_fib_send(ContainerRawIo,
  1112. cmd_fibcontext,
  1113. fibsize,
  1114. FsaNormal,
  1115. 0, 1,
  1116. (fib_callback) io_callback,
  1117. (void *) scsicmd);
  1118. } else if (dev->dac_support == 1) {
  1119. struct aac_write64 *writecmd;
  1120. writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
  1121. writecmd->command = cpu_to_le32(VM_CtHostWrite64);
  1122. writecmd->cid = cpu_to_le16(cid);
  1123. writecmd->sector_count = cpu_to_le16(count);
  1124. writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  1125. writecmd->pad = 0;
  1126. writecmd->flags = 0;
  1127. aac_build_sg64(scsicmd, &writecmd->sg);
  1128. fibsize = sizeof(struct aac_write64) +
  1129. ((le32_to_cpu(writecmd->sg.count) - 1) *
  1130. sizeof (struct sgentry64));
  1131. BUG_ON (fibsize > (dev->max_fib_size -
  1132. sizeof(struct aac_fibhdr)));
  1133. /*
  1134. * Now send the Fib to the adapter
  1135. */
  1136. status = aac_fib_send(ContainerCommand64,
  1137. cmd_fibcontext,
  1138. fibsize,
  1139. FsaNormal,
  1140. 0, 1,
  1141. (fib_callback) io_callback,
  1142. (void *) scsicmd);
  1143. } else {
  1144. struct aac_write *writecmd;
  1145. writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
  1146. writecmd->command = cpu_to_le32(VM_CtBlockWrite);
  1147. writecmd->cid = cpu_to_le32(cid);
  1148. writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  1149. writecmd->count = cpu_to_le32(count * 512);
  1150. writecmd->sg.count = cpu_to_le32(1);
  1151. /* ->stable is not used - it did mean which type of write */
  1152. aac_build_sg(scsicmd, &writecmd->sg);
  1153. fibsize = sizeof(struct aac_write) +
  1154. ((le32_to_cpu(writecmd->sg.count) - 1) *
  1155. sizeof (struct sgentry));
  1156. BUG_ON (fibsize > (dev->max_fib_size -
  1157. sizeof(struct aac_fibhdr)));
  1158. /*
  1159. * Now send the Fib to the adapter
  1160. */
  1161. status = aac_fib_send(ContainerCommand,
  1162. cmd_fibcontext,
  1163. fibsize,
  1164. FsaNormal,
  1165. 0, 1,
  1166. (fib_callback) io_callback,
  1167. (void *) scsicmd);
  1168. }
  1169. /*
  1170. * Check that the command queued to the controller
  1171. */
  1172. if (status == -EINPROGRESS) {
  1173. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1174. return 0;
  1175. }
  1176. printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
  1177. /*
  1178. * For some reason, the Fib didn't queue, return QUEUE_FULL
  1179. */
  1180. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  1181. scsicmd->scsi_done(scsicmd);
  1182. aac_fib_complete(cmd_fibcontext);
  1183. aac_fib_free(cmd_fibcontext);
  1184. return 0;
  1185. }
  1186. static void synchronize_callback(void *context, struct fib *fibptr)
  1187. {
  1188. struct aac_synchronize_reply *synchronizereply;
  1189. struct scsi_cmnd *cmd;
  1190. cmd = context;
  1191. cmd->SCp.phase = AAC_OWNER_MIDLEVEL;
  1192. dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
  1193. smp_processor_id(), jiffies));
  1194. BUG_ON(fibptr == NULL);
  1195. synchronizereply = fib_data(fibptr);
  1196. if (le32_to_cpu(synchronizereply->status) == CT_OK)
  1197. cmd->result = DID_OK << 16 |
  1198. COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1199. else {
  1200. struct scsi_device *sdev = cmd->device;
  1201. struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
  1202. u32 cid = sdev_id(sdev);
  1203. printk(KERN_WARNING
  1204. "synchronize_callback: synchronize failed, status = %d\n",
  1205. le32_to_cpu(synchronizereply->status));
  1206. cmd->result = DID_OK << 16 |
  1207. COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1208. set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
  1209. HARDWARE_ERROR,
  1210. SENCODE_INTERNAL_TARGET_FAILURE,
  1211. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  1212. 0, 0);
  1213. memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1214. min(sizeof(dev->fsa_dev[cid].sense_data),
  1215. sizeof(cmd->sense_buffer)));
  1216. }
  1217. aac_fib_complete(fibptr);
  1218. aac_fib_free(fibptr);
  1219. cmd->scsi_done(cmd);
  1220. }
  1221. static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
  1222. {
  1223. int status;
  1224. struct fib *cmd_fibcontext;
  1225. struct aac_synchronize *synchronizecmd;
  1226. struct scsi_cmnd *cmd;
  1227. struct scsi_device *sdev = scsicmd->device;
  1228. int active = 0;
  1229. unsigned long flags;
  1230. /*
  1231. * Wait for all outstanding queued commands to complete to this
  1232. * specific target (block).
  1233. */
  1234. spin_lock_irqsave(&sdev->list_lock, flags);
  1235. list_for_each_entry(cmd, &sdev->cmd_list, list)
  1236. if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
  1237. ++active;
  1238. break;
  1239. }
  1240. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1241. /*
  1242. * Yield the processor (requeue for later)
  1243. */
  1244. if (active)
  1245. return SCSI_MLQUEUE_DEVICE_BUSY;
  1246. /*
  1247. * Allocate and initialize a Fib
  1248. */
  1249. if (!(cmd_fibcontext =
  1250. aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata)))
  1251. return SCSI_MLQUEUE_HOST_BUSY;
  1252. aac_fib_init(cmd_fibcontext);
  1253. synchronizecmd = fib_data(cmd_fibcontext);
  1254. synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
  1255. synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
  1256. synchronizecmd->cid = cpu_to_le32(cid);
  1257. synchronizecmd->count =
  1258. cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
  1259. /*
  1260. * Now send the Fib to the adapter
  1261. */
  1262. status = aac_fib_send(ContainerCommand,
  1263. cmd_fibcontext,
  1264. sizeof(struct aac_synchronize),
  1265. FsaNormal,
  1266. 0, 1,
  1267. (fib_callback)synchronize_callback,
  1268. (void *)scsicmd);
  1269. /*
  1270. * Check that the command queued to the controller
  1271. */
  1272. if (status == -EINPROGRESS) {
  1273. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1274. return 0;
  1275. }
  1276. printk(KERN_WARNING
  1277. "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
  1278. aac_fib_complete(cmd_fibcontext);
  1279. aac_fib_free(cmd_fibcontext);
  1280. return SCSI_MLQUEUE_HOST_BUSY;
  1281. }
  1282. /**
  1283. * aac_scsi_cmd() - Process SCSI command
  1284. * @scsicmd: SCSI command block
  1285. *
  1286. * Emulate a SCSI command and queue the required request for the
  1287. * aacraid firmware.
  1288. */
  1289. int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
  1290. {
  1291. u32 cid = 0;
  1292. struct Scsi_Host *host = scsicmd->device->host;
  1293. struct aac_dev *dev = (struct aac_dev *)host->hostdata;
  1294. struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
  1295. /*
  1296. * If the bus, id or lun is out of range, return fail
  1297. * Test does not apply to ID 16, the pseudo id for the controller
  1298. * itself.
  1299. */
  1300. if (scmd_id(scsicmd) != host->this_id) {
  1301. if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
  1302. if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
  1303. (scsicmd->device->lun != 0)) {
  1304. scsicmd->result = DID_NO_CONNECT << 16;
  1305. scsicmd->scsi_done(scsicmd);
  1306. return 0;
  1307. }
  1308. cid = scmd_id(scsicmd);
  1309. /*
  1310. * If the target container doesn't exist, it may have
  1311. * been newly created
  1312. */
  1313. if ((fsa_dev_ptr[cid].valid & 1) == 0) {
  1314. switch (scsicmd->cmnd[0]) {
  1315. case SERVICE_ACTION_IN:
  1316. if (!(dev->raw_io_interface) ||
  1317. !(dev->raw_io_64) ||
  1318. ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
  1319. break;
  1320. case INQUIRY:
  1321. case READ_CAPACITY:
  1322. case TEST_UNIT_READY:
  1323. spin_unlock_irq(host->host_lock);
  1324. aac_probe_container(dev, cid);
  1325. if ((fsa_dev_ptr[cid].valid & 1) == 0)
  1326. fsa_dev_ptr[cid].valid = 0;
  1327. spin_lock_irq(host->host_lock);
  1328. if (fsa_dev_ptr[cid].valid == 0) {
  1329. scsicmd->result = DID_NO_CONNECT << 16;
  1330. scsicmd->scsi_done(scsicmd);
  1331. return 0;
  1332. }
  1333. default:
  1334. break;
  1335. }
  1336. }
  1337. /*
  1338. * If the target container still doesn't exist,
  1339. * return failure
  1340. */
  1341. if (fsa_dev_ptr[cid].valid == 0) {
  1342. scsicmd->result = DID_BAD_TARGET << 16;
  1343. scsicmd->scsi_done(scsicmd);
  1344. return 0;
  1345. }
  1346. } else { /* check for physical non-dasd devices */
  1347. if(dev->nondasd_support == 1){
  1348. return aac_send_srb_fib(scsicmd);
  1349. } else {
  1350. scsicmd->result = DID_NO_CONNECT << 16;
  1351. scsicmd->scsi_done(scsicmd);
  1352. return 0;
  1353. }
  1354. }
  1355. }
  1356. /*
  1357. * else Command for the controller itself
  1358. */
  1359. else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
  1360. (scsicmd->cmnd[0] != TEST_UNIT_READY))
  1361. {
  1362. dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
  1363. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1364. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1365. ILLEGAL_REQUEST,
  1366. SENCODE_INVALID_COMMAND,
  1367. ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
  1368. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1369. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1370. ? sizeof(scsicmd->sense_buffer)
  1371. : sizeof(dev->fsa_dev[cid].sense_data));
  1372. scsicmd->scsi_done(scsicmd);
  1373. return 0;
  1374. }
  1375. /* Handle commands here that don't really require going out to the adapter */
  1376. switch (scsicmd->cmnd[0]) {
  1377. case INQUIRY:
  1378. {
  1379. struct inquiry_data inq_data;
  1380. dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
  1381. memset(&inq_data, 0, sizeof (struct inquiry_data));
  1382. inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
  1383. inq_data.inqd_rdf = 2; /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
  1384. inq_data.inqd_len = 31;
  1385. /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
  1386. inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
  1387. /*
  1388. * Set the Vendor, Product, and Revision Level
  1389. * see: <vendor>.c i.e. aac.c
  1390. */
  1391. if (scmd_id(scsicmd) == host->this_id) {
  1392. setinqstr(dev, (void *) (inq_data.inqd_vid), (sizeof(container_types)/sizeof(char *)));
  1393. inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
  1394. aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
  1395. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1396. scsicmd->scsi_done(scsicmd);
  1397. return 0;
  1398. }
  1399. setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
  1400. inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
  1401. aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
  1402. return aac_get_container_name(scsicmd, cid);
  1403. }
  1404. case SERVICE_ACTION_IN:
  1405. if (!(dev->raw_io_interface) ||
  1406. !(dev->raw_io_64) ||
  1407. ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
  1408. break;
  1409. {
  1410. u64 capacity;
  1411. char cp[13];
  1412. dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
  1413. capacity = fsa_dev_ptr[cid].size - 1;
  1414. cp[0] = (capacity >> 56) & 0xff;
  1415. cp[1] = (capacity >> 48) & 0xff;
  1416. cp[2] = (capacity >> 40) & 0xff;
  1417. cp[3] = (capacity >> 32) & 0xff;
  1418. cp[4] = (capacity >> 24) & 0xff;
  1419. cp[5] = (capacity >> 16) & 0xff;
  1420. cp[6] = (capacity >> 8) & 0xff;
  1421. cp[7] = (capacity >> 0) & 0xff;
  1422. cp[8] = 0;
  1423. cp[9] = 0;
  1424. cp[10] = 2;
  1425. cp[11] = 0;
  1426. cp[12] = 0;
  1427. aac_internal_transfer(scsicmd, cp, 0,
  1428. min((unsigned int)scsicmd->cmnd[13], sizeof(cp)));
  1429. if (sizeof(cp) < scsicmd->cmnd[13]) {
  1430. unsigned int len, offset = sizeof(cp);
  1431. memset(cp, 0, offset);
  1432. do {
  1433. len = min(scsicmd->cmnd[13]-offset, sizeof(cp));
  1434. aac_internal_transfer(scsicmd, cp, offset, len);
  1435. } while ((offset += len) < scsicmd->cmnd[13]);
  1436. }
  1437. /* Do not cache partition table for arrays */
  1438. scsicmd->device->removable = 1;
  1439. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1440. scsicmd->scsi_done(scsicmd);
  1441. return 0;
  1442. }
  1443. case READ_CAPACITY:
  1444. {
  1445. u32 capacity;
  1446. char cp[8];
  1447. dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
  1448. if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
  1449. capacity = fsa_dev_ptr[cid].size - 1;
  1450. else
  1451. capacity = (u32)-1;
  1452. cp[0] = (capacity >> 24) & 0xff;
  1453. cp[1] = (capacity >> 16) & 0xff;
  1454. cp[2] = (capacity >> 8) & 0xff;
  1455. cp[3] = (capacity >> 0) & 0xff;
  1456. cp[4] = 0;
  1457. cp[5] = 0;
  1458. cp[6] = 2;
  1459. cp[7] = 0;
  1460. aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
  1461. /* Do not cache partition table for arrays */
  1462. scsicmd->device->removable = 1;
  1463. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1464. scsicmd->scsi_done(scsicmd);
  1465. return 0;
  1466. }
  1467. case MODE_SENSE:
  1468. {
  1469. char mode_buf[4];
  1470. dprintk((KERN_DEBUG "MODE SENSE command.\n"));
  1471. mode_buf[0] = 3; /* Mode data length */
  1472. mode_buf[1] = 0; /* Medium type - default */
  1473. mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
  1474. mode_buf[3] = 0; /* Block descriptor length */
  1475. aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
  1476. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1477. scsicmd->scsi_done(scsicmd);
  1478. return 0;
  1479. }
  1480. case MODE_SENSE_10:
  1481. {
  1482. char mode_buf[8];
  1483. dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
  1484. mode_buf[0] = 0; /* Mode data length (MSB) */
  1485. mode_buf[1] = 6; /* Mode data length (LSB) */
  1486. mode_buf[2] = 0; /* Medium type - default */
  1487. mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
  1488. mode_buf[4] = 0; /* reserved */
  1489. mode_buf[5] = 0; /* reserved */
  1490. mode_buf[6] = 0; /* Block descriptor length (MSB) */
  1491. mode_buf[7] = 0; /* Block descriptor length (LSB) */
  1492. aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
  1493. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1494. scsicmd->scsi_done(scsicmd);
  1495. return 0;
  1496. }
  1497. case REQUEST_SENSE:
  1498. dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
  1499. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
  1500. memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
  1501. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1502. scsicmd->scsi_done(scsicmd);
  1503. return 0;
  1504. case ALLOW_MEDIUM_REMOVAL:
  1505. dprintk((KERN_DEBUG "LOCK command.\n"));
  1506. if (scsicmd->cmnd[4])
  1507. fsa_dev_ptr[cid].locked = 1;
  1508. else
  1509. fsa_dev_ptr[cid].locked = 0;
  1510. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1511. scsicmd->scsi_done(scsicmd);
  1512. return 0;
  1513. /*
  1514. * These commands are all No-Ops
  1515. */
  1516. case TEST_UNIT_READY:
  1517. case RESERVE:
  1518. case RELEASE:
  1519. case REZERO_UNIT:
  1520. case REASSIGN_BLOCKS:
  1521. case SEEK_10:
  1522. case START_STOP:
  1523. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1524. scsicmd->scsi_done(scsicmd);
  1525. return 0;
  1526. }
  1527. switch (scsicmd->cmnd[0])
  1528. {
  1529. case READ_6:
  1530. case READ_10:
  1531. case READ_12:
  1532. case READ_16:
  1533. /*
  1534. * Hack to keep track of ordinal number of the device that
  1535. * corresponds to a container. Needed to convert
  1536. * containers to /dev/sd device names
  1537. */
  1538. if (scsicmd->request->rq_disk)
  1539. strlcpy(fsa_dev_ptr[cid].devname,
  1540. scsicmd->request->rq_disk->disk_name,
  1541. min(sizeof(fsa_dev_ptr[cid].devname),
  1542. sizeof(scsicmd->request->rq_disk->disk_name) + 1));
  1543. return aac_read(scsicmd, cid);
  1544. case WRITE_6:
  1545. case WRITE_10:
  1546. case WRITE_12:
  1547. case WRITE_16:
  1548. return aac_write(scsicmd, cid);
  1549. case SYNCHRONIZE_CACHE:
  1550. /* Issue FIB to tell Firmware to flush it's cache */
  1551. return aac_synchronize(scsicmd, cid);
  1552. default:
  1553. /*
  1554. * Unhandled commands
  1555. */
  1556. dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
  1557. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1558. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1559. ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
  1560. ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
  1561. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1562. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1563. ? sizeof(scsicmd->sense_buffer)
  1564. : sizeof(dev->fsa_dev[cid].sense_data));
  1565. scsicmd->scsi_done(scsicmd);
  1566. return 0;
  1567. }
  1568. }
  1569. static int query_disk(struct aac_dev *dev, void __user *arg)
  1570. {
  1571. struct aac_query_disk qd;
  1572. struct fsa_dev_info *fsa_dev_ptr;
  1573. fsa_dev_ptr = dev->fsa_dev;
  1574. if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
  1575. return -EFAULT;
  1576. if (qd.cnum == -1)
  1577. qd.cnum = qd.id;
  1578. else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
  1579. {
  1580. if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
  1581. return -EINVAL;
  1582. qd.instance = dev->scsi_host_ptr->host_no;
  1583. qd.bus = 0;
  1584. qd.id = CONTAINER_TO_ID(qd.cnum);
  1585. qd.lun = CONTAINER_TO_LUN(qd.cnum);
  1586. }
  1587. else return -EINVAL;
  1588. qd.valid = fsa_dev_ptr[qd.cnum].valid;
  1589. qd.locked = fsa_dev_ptr[qd.cnum].locked;
  1590. qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
  1591. if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
  1592. qd.unmapped = 1;
  1593. else
  1594. qd.unmapped = 0;
  1595. strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
  1596. min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
  1597. if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
  1598. return -EFAULT;
  1599. return 0;
  1600. }
  1601. static int force_delete_disk(struct aac_dev *dev, void __user *arg)
  1602. {
  1603. struct aac_delete_disk dd;
  1604. struct fsa_dev_info *fsa_dev_ptr;
  1605. fsa_dev_ptr = dev->fsa_dev;
  1606. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  1607. return -EFAULT;
  1608. if (dd.cnum >= dev->maximum_num_containers)
  1609. return -EINVAL;
  1610. /*
  1611. * Mark this container as being deleted.
  1612. */
  1613. fsa_dev_ptr[dd.cnum].deleted = 1;
  1614. /*
  1615. * Mark the container as no longer valid
  1616. */
  1617. fsa_dev_ptr[dd.cnum].valid = 0;
  1618. return 0;
  1619. }
  1620. static int delete_disk(struct aac_dev *dev, void __user *arg)
  1621. {
  1622. struct aac_delete_disk dd;
  1623. struct fsa_dev_info *fsa_dev_ptr;
  1624. fsa_dev_ptr = dev->fsa_dev;
  1625. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  1626. return -EFAULT;
  1627. if (dd.cnum >= dev->maximum_num_containers)
  1628. return -EINVAL;
  1629. /*
  1630. * If the container is locked, it can not be deleted by the API.
  1631. */
  1632. if (fsa_dev_ptr[dd.cnum].locked)
  1633. return -EBUSY;
  1634. else {
  1635. /*
  1636. * Mark the container as no longer being valid.
  1637. */
  1638. fsa_dev_ptr[dd.cnum].valid = 0;
  1639. fsa_dev_ptr[dd.cnum].devname[0] = '\0';
  1640. return 0;
  1641. }
  1642. }
  1643. int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
  1644. {
  1645. switch (cmd) {
  1646. case FSACTL_QUERY_DISK:
  1647. return query_disk(dev, arg);
  1648. case FSACTL_DELETE_DISK:
  1649. return delete_disk(dev, arg);
  1650. case FSACTL_FORCE_DELETE_DISK:
  1651. return force_delete_disk(dev, arg);
  1652. case FSACTL_GET_CONTAINERS:
  1653. return aac_get_containers(dev);
  1654. default:
  1655. return -ENOTTY;
  1656. }
  1657. }
  1658. /**
  1659. *
  1660. * aac_srb_callback
  1661. * @context: the context set in the fib - here it is scsi cmd
  1662. * @fibptr: pointer to the fib
  1663. *
  1664. * Handles the completion of a scsi command to a non dasd device
  1665. *
  1666. */
  1667. static void aac_srb_callback(void *context, struct fib * fibptr)
  1668. {
  1669. struct aac_dev *dev;
  1670. struct aac_srb_reply *srbreply;
  1671. struct scsi_cmnd *scsicmd;
  1672. scsicmd = (struct scsi_cmnd *) context;
  1673. scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
  1674. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1675. if (fibptr == NULL)
  1676. BUG();
  1677. srbreply = (struct aac_srb_reply *) fib_data(fibptr);
  1678. scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
  1679. /*
  1680. * Calculate resid for sg
  1681. */
  1682. scsicmd->resid = scsicmd->request_bufflen -
  1683. le32_to_cpu(srbreply->data_xfer_length);
  1684. if(scsicmd->use_sg)
  1685. pci_unmap_sg(dev->pdev,
  1686. (struct scatterlist *)scsicmd->buffer,
  1687. scsicmd->use_sg,
  1688. scsicmd->sc_data_direction);
  1689. else if(scsicmd->request_bufflen)
  1690. pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
  1691. scsicmd->sc_data_direction);
  1692. /*
  1693. * First check the fib status
  1694. */
  1695. if (le32_to_cpu(srbreply->status) != ST_OK){
  1696. int len;
  1697. printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
  1698. len = (le32_to_cpu(srbreply->sense_data_size) >
  1699. sizeof(scsicmd->sense_buffer)) ?
  1700. sizeof(scsicmd->sense_buffer) :
  1701. le32_to_cpu(srbreply->sense_data_size);
  1702. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1703. memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
  1704. }
  1705. /*
  1706. * Next check the srb status
  1707. */
  1708. switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
  1709. case SRB_STATUS_ERROR_RECOVERY:
  1710. case SRB_STATUS_PENDING:
  1711. case SRB_STATUS_SUCCESS:
  1712. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1713. break;
  1714. case SRB_STATUS_DATA_OVERRUN:
  1715. switch(scsicmd->cmnd[0]){
  1716. case READ_6:
  1717. case WRITE_6:
  1718. case READ_10:
  1719. case WRITE_10:
  1720. case READ_12:
  1721. case WRITE_12:
  1722. case READ_16:
  1723. case WRITE_16:
  1724. if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
  1725. printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
  1726. } else {
  1727. printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
  1728. }
  1729. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  1730. break;
  1731. case INQUIRY: {
  1732. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1733. break;
  1734. }
  1735. default:
  1736. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1737. break;
  1738. }
  1739. break;
  1740. case SRB_STATUS_ABORTED:
  1741. scsicmd->result = DID_ABORT << 16 | ABORT << 8;
  1742. break;
  1743. case SRB_STATUS_ABORT_FAILED:
  1744. // Not sure about this one - but assuming the hba was trying to abort for some reason
  1745. scsicmd->result = DID_ERROR << 16 | ABORT << 8;
  1746. break;
  1747. case SRB_STATUS_PARITY_ERROR:
  1748. scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
  1749. break;
  1750. case SRB_STATUS_NO_DEVICE:
  1751. case SRB_STATUS_INVALID_PATH_ID:
  1752. case SRB_STATUS_INVALID_TARGET_ID:
  1753. case SRB_STATUS_INVALID_LUN:
  1754. case SRB_STATUS_SELECTION_TIMEOUT:
  1755. scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  1756. break;
  1757. case SRB_STATUS_COMMAND_TIMEOUT:
  1758. case SRB_STATUS_TIMEOUT:
  1759. scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
  1760. break;
  1761. case SRB_STATUS_BUSY:
  1762. scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  1763. break;
  1764. case SRB_STATUS_BUS_RESET:
  1765. scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
  1766. break;
  1767. case SRB_STATUS_MESSAGE_REJECTED:
  1768. scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
  1769. break;
  1770. case SRB_STATUS_REQUEST_FLUSHED:
  1771. case SRB_STATUS_ERROR:
  1772. case SRB_STATUS_INVALID_REQUEST:
  1773. case SRB_STATUS_REQUEST_SENSE_FAILED:
  1774. case SRB_STATUS_NO_HBA:
  1775. case SRB_STATUS_UNEXPECTED_BUS_FREE:
  1776. case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
  1777. case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
  1778. case SRB_STATUS_DELAYED_RETRY:
  1779. case SRB_STATUS_BAD_FUNCTION:
  1780. case SRB_STATUS_NOT_STARTED:
  1781. case SRB_STATUS_NOT_IN_USE:
  1782. case SRB_STATUS_FORCE_ABORT:
  1783. case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
  1784. default:
  1785. #ifdef AAC_DETAILED_STATUS_INFO
  1786. printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
  1787. le32_to_cpu(srbreply->srb_status) & 0x3F,
  1788. aac_get_status_string(
  1789. le32_to_cpu(srbreply->srb_status) & 0x3F),
  1790. scsicmd->cmnd[0],
  1791. le32_to_cpu(srbreply->scsi_status));
  1792. #endif
  1793. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  1794. break;
  1795. }
  1796. if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
  1797. int len;
  1798. scsicmd->result |= SAM_STAT_CHECK_CONDITION;
  1799. len = (le32_to_cpu(srbreply->sense_data_size) >
  1800. sizeof(scsicmd->sense_buffer)) ?
  1801. sizeof(scsicmd->sense_buffer) :
  1802. le32_to_cpu(srbreply->sense_data_size);
  1803. #ifdef AAC_DETAILED_STATUS_INFO
  1804. printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
  1805. le32_to_cpu(srbreply->status), len);
  1806. #endif
  1807. memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
  1808. }
  1809. /*
  1810. * OR in the scsi status (already shifted up a bit)
  1811. */
  1812. scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
  1813. aac_fib_complete(fibptr);
  1814. aac_fib_free(fibptr);
  1815. scsicmd->scsi_done(scsicmd);
  1816. }
  1817. /**
  1818. *
  1819. * aac_send_scb_fib
  1820. * @scsicmd: the scsi command block
  1821. *
  1822. * This routine will form a FIB and fill in the aac_srb from the
  1823. * scsicmd passed in.
  1824. */
  1825. static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
  1826. {
  1827. struct fib* cmd_fibcontext;
  1828. struct aac_dev* dev;
  1829. int status;
  1830. struct aac_srb *srbcmd;
  1831. u16 fibsize;
  1832. u32 flag;
  1833. u32 timeout;
  1834. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1835. if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
  1836. scsicmd->device->lun > 7) {
  1837. scsicmd->result = DID_NO_CONNECT << 16;
  1838. scsicmd->scsi_done(scsicmd);
  1839. return 0;
  1840. }
  1841. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1842. switch(scsicmd->sc_data_direction){
  1843. case DMA_TO_DEVICE:
  1844. flag = SRB_DataOut;
  1845. break;
  1846. case DMA_BIDIRECTIONAL:
  1847. flag = SRB_DataIn | SRB_DataOut;
  1848. break;
  1849. case DMA_FROM_DEVICE:
  1850. flag = SRB_DataIn;
  1851. break;
  1852. case DMA_NONE:
  1853. default: /* shuts up some versions of gcc */
  1854. flag = SRB_NoDataXfer;
  1855. break;
  1856. }
  1857. /*
  1858. * Allocate and initialize a Fib then setup a BlockWrite command
  1859. */
  1860. if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
  1861. return -1;
  1862. }
  1863. aac_fib_init(cmd_fibcontext);
  1864. srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
  1865. srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
  1866. srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(scsicmd)));
  1867. srbcmd->id = cpu_to_le32(scmd_id(scsicmd));
  1868. srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
  1869. srbcmd->flags = cpu_to_le32(flag);
  1870. timeout = scsicmd->timeout_per_command/HZ;
  1871. if(timeout == 0){
  1872. timeout = 1;
  1873. }
  1874. srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
  1875. srbcmd->retry_limit = 0; /* Obsolete parameter */
  1876. srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
  1877. if( dev->dac_support == 1 ) {
  1878. aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
  1879. srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
  1880. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1881. memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
  1882. /*
  1883. * Build Scatter/Gather list
  1884. */
  1885. fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
  1886. ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
  1887. sizeof (struct sgentry64));
  1888. BUG_ON (fibsize > (dev->max_fib_size -
  1889. sizeof(struct aac_fibhdr)));
  1890. /*
  1891. * Now send the Fib to the adapter
  1892. */
  1893. status = aac_fib_send(ScsiPortCommand64, cmd_fibcontext,
  1894. fibsize, FsaNormal, 0, 1,
  1895. (fib_callback) aac_srb_callback,
  1896. (void *) scsicmd);
  1897. } else {
  1898. aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
  1899. srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
  1900. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1901. memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
  1902. /*
  1903. * Build Scatter/Gather list
  1904. */
  1905. fibsize = sizeof (struct aac_srb) +
  1906. (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
  1907. sizeof (struct sgentry));
  1908. BUG_ON (fibsize > (dev->max_fib_size -
  1909. sizeof(struct aac_fibhdr)));
  1910. /*
  1911. * Now send the Fib to the adapter
  1912. */
  1913. status = aac_fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
  1914. (fib_callback) aac_srb_callback, (void *) scsicmd);
  1915. }
  1916. /*
  1917. * Check that the command queued to the controller
  1918. */
  1919. if (status == -EINPROGRESS) {
  1920. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1921. return 0;
  1922. }
  1923. printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
  1924. aac_fib_complete(cmd_fibcontext);
  1925. aac_fib_free(cmd_fibcontext);
  1926. return -1;
  1927. }
  1928. static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
  1929. {
  1930. struct aac_dev *dev;
  1931. unsigned long byte_count = 0;
  1932. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1933. // Get rid of old data
  1934. psg->count = 0;
  1935. psg->sg[0].addr = 0;
  1936. psg->sg[0].count = 0;
  1937. if (scsicmd->use_sg) {
  1938. struct scatterlist *sg;
  1939. int i;
  1940. int sg_count;
  1941. sg = (struct scatterlist *) scsicmd->request_buffer;
  1942. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  1943. scsicmd->sc_data_direction);
  1944. psg->count = cpu_to_le32(sg_count);
  1945. byte_count = 0;
  1946. for (i = 0; i < sg_count; i++) {
  1947. psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
  1948. psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
  1949. byte_count += sg_dma_len(sg);
  1950. sg++;
  1951. }
  1952. /* hba wants the size to be exact */
  1953. if(byte_count > scsicmd->request_bufflen){
  1954. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  1955. (byte_count - scsicmd->request_bufflen);
  1956. psg->sg[i-1].count = cpu_to_le32(temp);
  1957. byte_count = scsicmd->request_bufflen;
  1958. }
  1959. /* Check for command underflow */
  1960. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  1961. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  1962. byte_count, scsicmd->underflow);
  1963. }
  1964. }
  1965. else if(scsicmd->request_bufflen) {
  1966. dma_addr_t addr;
  1967. addr = pci_map_single(dev->pdev,
  1968. scsicmd->request_buffer,
  1969. scsicmd->request_bufflen,
  1970. scsicmd->sc_data_direction);
  1971. psg->count = cpu_to_le32(1);
  1972. psg->sg[0].addr = cpu_to_le32(addr);
  1973. psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
  1974. scsicmd->SCp.dma_handle = addr;
  1975. byte_count = scsicmd->request_bufflen;
  1976. }
  1977. return byte_count;
  1978. }
  1979. static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
  1980. {
  1981. struct aac_dev *dev;
  1982. unsigned long byte_count = 0;
  1983. u64 addr;
  1984. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1985. // Get rid of old data
  1986. psg->count = 0;
  1987. psg->sg[0].addr[0] = 0;
  1988. psg->sg[0].addr[1] = 0;
  1989. psg->sg[0].count = 0;
  1990. if (scsicmd->use_sg) {
  1991. struct scatterlist *sg;
  1992. int i;
  1993. int sg_count;
  1994. sg = (struct scatterlist *) scsicmd->request_buffer;
  1995. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  1996. scsicmd->sc_data_direction);
  1997. psg->count = cpu_to_le32(sg_count);
  1998. byte_count = 0;
  1999. for (i = 0; i < sg_count; i++) {
  2000. addr = sg_dma_address(sg);
  2001. psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
  2002. psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
  2003. psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
  2004. byte_count += sg_dma_len(sg);
  2005. sg++;
  2006. }
  2007. /* hba wants the size to be exact */
  2008. if(byte_count > scsicmd->request_bufflen){
  2009. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2010. (byte_count - scsicmd->request_bufflen);
  2011. psg->sg[i-1].count = cpu_to_le32(temp);
  2012. byte_count = scsicmd->request_bufflen;
  2013. }
  2014. /* Check for command underflow */
  2015. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2016. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2017. byte_count, scsicmd->underflow);
  2018. }
  2019. }
  2020. else if(scsicmd->request_bufflen) {
  2021. u64 addr;
  2022. addr = pci_map_single(dev->pdev,
  2023. scsicmd->request_buffer,
  2024. scsicmd->request_bufflen,
  2025. scsicmd->sc_data_direction);
  2026. psg->count = cpu_to_le32(1);
  2027. psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
  2028. psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
  2029. psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
  2030. scsicmd->SCp.dma_handle = addr;
  2031. byte_count = scsicmd->request_bufflen;
  2032. }
  2033. return byte_count;
  2034. }
  2035. static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
  2036. {
  2037. struct Scsi_Host *host = scsicmd->device->host;
  2038. struct aac_dev *dev = (struct aac_dev *)host->hostdata;
  2039. unsigned long byte_count = 0;
  2040. // Get rid of old data
  2041. psg->count = 0;
  2042. psg->sg[0].next = 0;
  2043. psg->sg[0].prev = 0;
  2044. psg->sg[0].addr[0] = 0;
  2045. psg->sg[0].addr[1] = 0;
  2046. psg->sg[0].count = 0;
  2047. psg->sg[0].flags = 0;
  2048. if (scsicmd->use_sg) {
  2049. struct scatterlist *sg;
  2050. int i;
  2051. int sg_count;
  2052. sg = (struct scatterlist *) scsicmd->request_buffer;
  2053. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  2054. scsicmd->sc_data_direction);
  2055. for (i = 0; i < sg_count; i++) {
  2056. int count = sg_dma_len(sg);
  2057. u64 addr = sg_dma_address(sg);
  2058. psg->sg[i].next = 0;
  2059. psg->sg[i].prev = 0;
  2060. psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
  2061. psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
  2062. psg->sg[i].count = cpu_to_le32(count);
  2063. psg->sg[i].flags = 0;
  2064. byte_count += count;
  2065. sg++;
  2066. }
  2067. psg->count = cpu_to_le32(sg_count);
  2068. /* hba wants the size to be exact */
  2069. if(byte_count > scsicmd->request_bufflen){
  2070. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2071. (byte_count - scsicmd->request_bufflen);
  2072. psg->sg[i-1].count = cpu_to_le32(temp);
  2073. byte_count = scsicmd->request_bufflen;
  2074. }
  2075. /* Check for command underflow */
  2076. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2077. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2078. byte_count, scsicmd->underflow);
  2079. }
  2080. }
  2081. else if(scsicmd->request_bufflen) {
  2082. int count;
  2083. u64 addr;
  2084. scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
  2085. scsicmd->request_buffer,
  2086. scsicmd->request_bufflen,
  2087. scsicmd->sc_data_direction);
  2088. addr = scsicmd->SCp.dma_handle;
  2089. count = scsicmd->request_bufflen;
  2090. psg->count = cpu_to_le32(1);
  2091. psg->sg[0].next = 0;
  2092. psg->sg[0].prev = 0;
  2093. psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
  2094. psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
  2095. psg->sg[0].count = cpu_to_le32(count);
  2096. psg->sg[0].flags = 0;
  2097. byte_count = scsicmd->request_bufflen;
  2098. }
  2099. return byte_count;
  2100. }
  2101. #ifdef AAC_DETAILED_STATUS_INFO
  2102. struct aac_srb_status_info {
  2103. u32 status;
  2104. char *str;
  2105. };
  2106. static struct aac_srb_status_info srb_status_info[] = {
  2107. { SRB_STATUS_PENDING, "Pending Status"},
  2108. { SRB_STATUS_SUCCESS, "Success"},
  2109. { SRB_STATUS_ABORTED, "Aborted Command"},
  2110. { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
  2111. { SRB_STATUS_ERROR, "Error Event"},
  2112. { SRB_STATUS_BUSY, "Device Busy"},
  2113. { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
  2114. { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
  2115. { SRB_STATUS_NO_DEVICE, "No Device"},
  2116. { SRB_STATUS_TIMEOUT, "Timeout"},
  2117. { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
  2118. { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
  2119. { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
  2120. { SRB_STATUS_BUS_RESET, "Bus Reset"},
  2121. { SRB_STATUS_PARITY_ERROR, "Parity Error"},
  2122. { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
  2123. { SRB_STATUS_NO_HBA, "No HBA"},
  2124. { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
  2125. { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
  2126. { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
  2127. { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
  2128. { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
  2129. { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
  2130. { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
  2131. { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
  2132. { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
  2133. { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
  2134. { SRB_STATUS_NOT_STARTED, "Not Started"},
  2135. { SRB_STATUS_NOT_IN_USE, "Not In Use"},
  2136. { SRB_STATUS_FORCE_ABORT, "Force Abort"},
  2137. { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
  2138. { 0xff, "Unknown Error"}
  2139. };
  2140. char *aac_get_status_string(u32 status)
  2141. {
  2142. int i;
  2143. for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
  2144. if(srb_status_info[i].status == status){
  2145. return srb_status_info[i].str;
  2146. }
  2147. }
  2148. return "Bad Status Code";
  2149. }
  2150. #endif