aachba.c 73 KB

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