aachba.c 71 KB

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