esas2r_main.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_main.c
  3. * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
  4. *
  5. * Copyright (c) 2001-2013 ATTO Technology, Inc.
  6. * (mailto:linuxdrivers@attotech.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. *
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include "esas2r.h"
  44. MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
  45. MODULE_AUTHOR("ATTO Technology, Inc.");
  46. MODULE_LICENSE("GPL");
  47. MODULE_VERSION(ESAS2R_VERSION_STR);
  48. /* global definitions */
  49. static int found_adapters;
  50. struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
  51. #define ESAS2R_VDA_EVENT_PORT1 54414
  52. #define ESAS2R_VDA_EVENT_PORT2 54415
  53. #define ESAS2R_VDA_EVENT_SOCK_COUNT 2
  54. static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
  55. {
  56. struct device *dev = container_of(kobj, struct device, kobj);
  57. struct Scsi_Host *host = class_to_shost(dev);
  58. return (struct esas2r_adapter *)host->hostdata;
  59. }
  60. static ssize_t read_fw(struct file *file, struct kobject *kobj,
  61. struct bin_attribute *attr,
  62. char *buf, loff_t off, size_t count)
  63. {
  64. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  65. return esas2r_read_fw(a, buf, off, count);
  66. }
  67. static ssize_t write_fw(struct file *file, struct kobject *kobj,
  68. struct bin_attribute *attr,
  69. char *buf, loff_t off, size_t count)
  70. {
  71. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  72. return esas2r_write_fw(a, buf, off, count);
  73. }
  74. static ssize_t read_fs(struct file *file, struct kobject *kobj,
  75. struct bin_attribute *attr,
  76. char *buf, loff_t off, size_t count)
  77. {
  78. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  79. return esas2r_read_fs(a, buf, off, count);
  80. }
  81. static ssize_t write_fs(struct file *file, struct kobject *kobj,
  82. struct bin_attribute *attr,
  83. char *buf, loff_t off, size_t count)
  84. {
  85. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  86. int length = min(sizeof(struct esas2r_ioctl_fs), count);
  87. int result = 0;
  88. result = esas2r_write_fs(a, buf, off, count);
  89. if (result < 0)
  90. result = 0;
  91. return length;
  92. }
  93. static ssize_t read_vda(struct file *file, struct kobject *kobj,
  94. struct bin_attribute *attr,
  95. char *buf, loff_t off, size_t count)
  96. {
  97. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  98. return esas2r_read_vda(a, buf, off, count);
  99. }
  100. static ssize_t write_vda(struct file *file, struct kobject *kobj,
  101. struct bin_attribute *attr,
  102. char *buf, loff_t off, size_t count)
  103. {
  104. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  105. return esas2r_write_vda(a, buf, off, count);
  106. }
  107. static ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
  108. struct bin_attribute *attr,
  109. char *buf, loff_t off, size_t count)
  110. {
  111. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  112. int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
  113. memcpy(buf, a->nvram, length);
  114. return length;
  115. }
  116. static ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
  117. struct bin_attribute *attr,
  118. char *buf, loff_t off, size_t count)
  119. {
  120. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  121. struct esas2r_request *rq;
  122. int result = -EFAULT;
  123. rq = esas2r_alloc_request(a);
  124. if (rq == NULL)
  125. return -ENOMEM;
  126. if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
  127. result = count;
  128. esas2r_free_request(a, rq);
  129. return result;
  130. }
  131. static ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
  132. struct bin_attribute *attr,
  133. char *buf, loff_t off, size_t count)
  134. {
  135. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  136. esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
  137. return sizeof(struct esas2r_sas_nvram);
  138. }
  139. static ssize_t read_hw(struct file *file, struct kobject *kobj,
  140. struct bin_attribute *attr,
  141. char *buf, loff_t off, size_t count)
  142. {
  143. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  144. int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
  145. if (!a->local_atto_ioctl)
  146. return -ENOMEM;
  147. if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
  148. return -ENOMEM;
  149. memcpy(buf, a->local_atto_ioctl, length);
  150. return length;
  151. }
  152. static ssize_t write_hw(struct file *file, struct kobject *kobj,
  153. struct bin_attribute *attr,
  154. char *buf, loff_t off, size_t count)
  155. {
  156. struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  157. int length = min(sizeof(struct atto_ioctl), count);
  158. if (!a->local_atto_ioctl) {
  159. a->local_atto_ioctl = kzalloc(sizeof(struct atto_ioctl),
  160. GFP_KERNEL);
  161. if (a->local_atto_ioctl == NULL) {
  162. esas2r_log(ESAS2R_LOG_WARN,
  163. "write_hw kzalloc failed for %d bytes",
  164. sizeof(struct atto_ioctl));
  165. return -ENOMEM;
  166. }
  167. }
  168. memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
  169. memcpy(a->local_atto_ioctl, buf, length);
  170. return length;
  171. }
  172. #define ESAS2R_RW_BIN_ATTR(_name) \
  173. struct bin_attribute bin_attr_ ## _name = { \
  174. .attr = \
  175. { .name = __stringify(_name), .mode = S_IRUSR | S_IWUSR }, \
  176. .size = 0, \
  177. .read = read_ ## _name, \
  178. .write = write_ ## _name }
  179. ESAS2R_RW_BIN_ATTR(fw);
  180. ESAS2R_RW_BIN_ATTR(fs);
  181. ESAS2R_RW_BIN_ATTR(vda);
  182. ESAS2R_RW_BIN_ATTR(hw);
  183. ESAS2R_RW_BIN_ATTR(live_nvram);
  184. struct bin_attribute bin_attr_default_nvram = {
  185. .attr = { .name = "default_nvram", .mode = S_IRUGO },
  186. .size = 0,
  187. .read = read_default_nvram,
  188. .write = NULL
  189. };
  190. static struct scsi_host_template driver_template = {
  191. .module = THIS_MODULE,
  192. .show_info = esas2r_show_info,
  193. .name = ESAS2R_LONGNAME,
  194. .release = esas2r_release,
  195. .info = esas2r_info,
  196. .ioctl = esas2r_ioctl,
  197. .queuecommand = esas2r_queuecommand,
  198. .eh_abort_handler = esas2r_eh_abort,
  199. .eh_device_reset_handler = esas2r_device_reset,
  200. .eh_bus_reset_handler = esas2r_bus_reset,
  201. .eh_host_reset_handler = esas2r_host_reset,
  202. .eh_target_reset_handler = esas2r_target_reset,
  203. .can_queue = 128,
  204. .this_id = -1,
  205. .sg_tablesize = SCSI_MAX_SG_SEGMENTS,
  206. .cmd_per_lun =
  207. ESAS2R_DEFAULT_CMD_PER_LUN,
  208. .present = 0,
  209. .unchecked_isa_dma = 0,
  210. .use_clustering = ENABLE_CLUSTERING,
  211. .emulated = 0,
  212. .proc_name = ESAS2R_DRVR_NAME,
  213. .slave_configure = esas2r_slave_configure,
  214. .slave_alloc = esas2r_slave_alloc,
  215. .slave_destroy = esas2r_slave_destroy,
  216. .change_queue_depth = esas2r_change_queue_depth,
  217. .change_queue_type = esas2r_change_queue_type,
  218. .max_sectors = 0xFFFF,
  219. };
  220. int sgl_page_size = 512;
  221. module_param(sgl_page_size, int, 0);
  222. MODULE_PARM_DESC(sgl_page_size,
  223. "Scatter/gather list (SGL) page size in number of S/G "
  224. "entries. If your application is doing a lot of very large "
  225. "transfers, you may want to increase the SGL page size. "
  226. "Default 512.");
  227. int num_sg_lists = 1024;
  228. module_param(num_sg_lists, int, 0);
  229. MODULE_PARM_DESC(num_sg_lists,
  230. "Number of scatter/gather lists. Default 1024.");
  231. int sg_tablesize = SCSI_MAX_SG_SEGMENTS;
  232. module_param(sg_tablesize, int, 0);
  233. MODULE_PARM_DESC(sg_tablesize,
  234. "Maximum number of entries in a scatter/gather table.");
  235. int num_requests = 256;
  236. module_param(num_requests, int, 0);
  237. MODULE_PARM_DESC(num_requests,
  238. "Number of requests. Default 256.");
  239. int num_ae_requests = 4;
  240. module_param(num_ae_requests, int, 0);
  241. MODULE_PARM_DESC(num_ae_requests,
  242. "Number of VDA asynchromous event requests. Default 4.");
  243. int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
  244. module_param(cmd_per_lun, int, 0);
  245. MODULE_PARM_DESC(cmd_per_lun,
  246. "Maximum number of commands per LUN. Default "
  247. DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
  248. int can_queue = 128;
  249. module_param(can_queue, int, 0);
  250. MODULE_PARM_DESC(can_queue,
  251. "Maximum number of commands per adapter. Default 128.");
  252. int esas2r_max_sectors = 0xFFFF;
  253. module_param(esas2r_max_sectors, int, 0);
  254. MODULE_PARM_DESC(esas2r_max_sectors,
  255. "Maximum number of disk sectors in a single data transfer. "
  256. "Default 65535 (largest possible setting).");
  257. int interrupt_mode = 1;
  258. module_param(interrupt_mode, int, 0);
  259. MODULE_PARM_DESC(interrupt_mode,
  260. "Defines the interrupt mode to use. 0 for legacy"
  261. ", 1 for MSI. Default is MSI (1).");
  262. static struct pci_device_id
  263. esas2r_pci_table[] = {
  264. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x0049,
  265. 0,
  266. 0, 0 },
  267. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004A,
  268. 0,
  269. 0, 0 },
  270. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004B,
  271. 0,
  272. 0, 0 },
  273. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004C,
  274. 0,
  275. 0, 0 },
  276. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004D,
  277. 0,
  278. 0, 0 },
  279. { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004E,
  280. 0,
  281. 0, 0 },
  282. { 0, 0, 0, 0,
  283. 0,
  284. 0, 0 }
  285. };
  286. MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
  287. static int
  288. esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
  289. static void
  290. esas2r_remove(struct pci_dev *pcid);
  291. static struct pci_driver
  292. esas2r_pci_driver = {
  293. .name = ESAS2R_DRVR_NAME,
  294. .id_table = esas2r_pci_table,
  295. .probe = esas2r_probe,
  296. .remove = esas2r_remove,
  297. .suspend = esas2r_suspend,
  298. .resume = esas2r_resume,
  299. };
  300. static int esas2r_probe(struct pci_dev *pcid,
  301. const struct pci_device_id *id)
  302. {
  303. struct Scsi_Host *host = NULL;
  304. struct esas2r_adapter *a;
  305. int err;
  306. size_t host_alloc_size = sizeof(struct esas2r_adapter)
  307. + ((num_requests) +
  308. 1) * sizeof(struct esas2r_request);
  309. esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
  310. "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
  311. pcid->vendor,
  312. pcid->device,
  313. pcid->subsystem_vendor,
  314. pcid->subsystem_device);
  315. esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
  316. "before pci_enable_device() "
  317. "enable_cnt: %d",
  318. pcid->enable_cnt.counter);
  319. err = pci_enable_device(pcid);
  320. if (err != 0) {
  321. esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
  322. "pci_enable_device() FAIL (%d)",
  323. err);
  324. return -ENODEV;
  325. }
  326. esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
  327. "pci_enable_device() OK");
  328. esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
  329. "after pci_device_enable() enable_cnt: %d",
  330. pcid->enable_cnt.counter);
  331. host = scsi_host_alloc(&driver_template, host_alloc_size);
  332. if (host == NULL) {
  333. esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
  334. return -ENODEV;
  335. }
  336. memset(host->hostdata, 0, host_alloc_size);
  337. a = (struct esas2r_adapter *)host->hostdata;
  338. esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
  339. /* override max LUN and max target id */
  340. host->max_id = ESAS2R_MAX_ID + 1;
  341. host->max_lun = 255;
  342. /* we can handle 16-byte CDbs */
  343. host->max_cmd_len = 16;
  344. host->can_queue = can_queue;
  345. host->cmd_per_lun = cmd_per_lun;
  346. host->this_id = host->max_id + 1;
  347. host->max_channel = 0;
  348. host->unique_id = found_adapters;
  349. host->sg_tablesize = sg_tablesize;
  350. host->max_sectors = esas2r_max_sectors;
  351. /* set to bus master for BIOses that don't do it for us */
  352. esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
  353. pci_set_master(pcid);
  354. if (!esas2r_init_adapter(host, pcid, found_adapters)) {
  355. esas2r_log(ESAS2R_LOG_CRIT,
  356. "unable to initialize device at PCI bus %x:%x",
  357. pcid->bus->number,
  358. pcid->devfn);
  359. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  360. "scsi_host_put() called");
  361. scsi_host_put(host);
  362. return 0;
  363. }
  364. esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
  365. host->hostdata);
  366. pci_set_drvdata(pcid, host);
  367. esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
  368. err = scsi_add_host(host, &pcid->dev);
  369. if (err) {
  370. esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
  371. esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
  372. "scsi_add_host() FAIL");
  373. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  374. "scsi_host_put() called");
  375. scsi_host_put(host);
  376. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  377. "pci_set_drvdata(%p, NULL) called",
  378. pcid);
  379. pci_set_drvdata(pcid, NULL);
  380. return -ENODEV;
  381. }
  382. esas2r_fw_event_on(a);
  383. esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
  384. "scsi_scan_host() called");
  385. scsi_scan_host(host);
  386. /* Add sysfs binary files */
  387. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
  388. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  389. "Failed to create sysfs binary file: fw");
  390. else
  391. a->sysfs_fw_created = 1;
  392. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
  393. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  394. "Failed to create sysfs binary file: fs");
  395. else
  396. a->sysfs_fs_created = 1;
  397. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
  398. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  399. "Failed to create sysfs binary file: vda");
  400. else
  401. a->sysfs_vda_created = 1;
  402. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
  403. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  404. "Failed to create sysfs binary file: hw");
  405. else
  406. a->sysfs_hw_created = 1;
  407. if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
  408. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  409. "Failed to create sysfs binary file: live_nvram");
  410. else
  411. a->sysfs_live_nvram_created = 1;
  412. if (sysfs_create_bin_file(&host->shost_dev.kobj,
  413. &bin_attr_default_nvram))
  414. esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
  415. "Failed to create sysfs binary file: default_nvram");
  416. else
  417. a->sysfs_default_nvram_created = 1;
  418. found_adapters++;
  419. return 0;
  420. }
  421. static void esas2r_remove(struct pci_dev *pdev)
  422. {
  423. struct Scsi_Host *host;
  424. int index;
  425. if (pdev == NULL) {
  426. esas2r_log(ESAS2R_LOG_WARN, "esas2r_remove pdev==NULL");
  427. return;
  428. }
  429. host = pci_get_drvdata(pdev);
  430. if (host == NULL) {
  431. /*
  432. * this can happen if pci_set_drvdata was already called
  433. * to clear the host pointer. if this is the case, we
  434. * are okay; this channel has already been cleaned up.
  435. */
  436. return;
  437. }
  438. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  439. "esas2r_remove(%p) called; "
  440. "host:%p", pdev,
  441. host);
  442. index = esas2r_cleanup(host);
  443. if (index < 0)
  444. esas2r_log_dev(ESAS2R_LOG_WARN, &(pdev->dev),
  445. "unknown host in %s",
  446. __func__);
  447. found_adapters--;
  448. /* if this was the last adapter, clean up the rest of the driver */
  449. if (found_adapters == 0)
  450. esas2r_cleanup(NULL);
  451. }
  452. static int __init esas2r_init(void)
  453. {
  454. int i;
  455. esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
  456. /* verify valid parameters */
  457. if (can_queue < 1) {
  458. esas2r_log(ESAS2R_LOG_WARN,
  459. "warning: can_queue must be at least 1, value "
  460. "forced.");
  461. can_queue = 1;
  462. } else if (can_queue > 2048) {
  463. esas2r_log(ESAS2R_LOG_WARN,
  464. "warning: can_queue must be no larger than 2048, "
  465. "value forced.");
  466. can_queue = 2048;
  467. }
  468. if (cmd_per_lun < 1) {
  469. esas2r_log(ESAS2R_LOG_WARN,
  470. "warning: cmd_per_lun must be at least 1, value "
  471. "forced.");
  472. cmd_per_lun = 1;
  473. } else if (cmd_per_lun > 2048) {
  474. esas2r_log(ESAS2R_LOG_WARN,
  475. "warning: cmd_per_lun must be no larger than "
  476. "2048, value forced.");
  477. cmd_per_lun = 2048;
  478. }
  479. if (sg_tablesize < 32) {
  480. esas2r_log(ESAS2R_LOG_WARN,
  481. "warning: sg_tablesize must be at least 32, "
  482. "value forced.");
  483. sg_tablesize = 32;
  484. }
  485. if (esas2r_max_sectors < 1) {
  486. esas2r_log(ESAS2R_LOG_WARN,
  487. "warning: esas2r_max_sectors must be at least "
  488. "1, value forced.");
  489. esas2r_max_sectors = 1;
  490. } else if (esas2r_max_sectors > 0xffff) {
  491. esas2r_log(ESAS2R_LOG_WARN,
  492. "warning: esas2r_max_sectors must be no larger "
  493. "than 0xffff, value forced.");
  494. esas2r_max_sectors = 0xffff;
  495. }
  496. sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
  497. if (sgl_page_size < SGL_PG_SZ_MIN)
  498. sgl_page_size = SGL_PG_SZ_MIN;
  499. else if (sgl_page_size > SGL_PG_SZ_MAX)
  500. sgl_page_size = SGL_PG_SZ_MAX;
  501. if (num_sg_lists < NUM_SGL_MIN)
  502. num_sg_lists = NUM_SGL_MIN;
  503. else if (num_sg_lists > NUM_SGL_MAX)
  504. num_sg_lists = NUM_SGL_MAX;
  505. if (num_requests < NUM_REQ_MIN)
  506. num_requests = NUM_REQ_MIN;
  507. else if (num_requests > NUM_REQ_MAX)
  508. num_requests = NUM_REQ_MAX;
  509. if (num_ae_requests < NUM_AE_MIN)
  510. num_ae_requests = NUM_AE_MIN;
  511. else if (num_ae_requests > NUM_AE_MAX)
  512. num_ae_requests = NUM_AE_MAX;
  513. /* set up other globals */
  514. for (i = 0; i < MAX_ADAPTERS; i++)
  515. esas2r_adapters[i] = NULL;
  516. /* initialize */
  517. driver_template.module = THIS_MODULE;
  518. if (pci_register_driver(&esas2r_pci_driver) != 0)
  519. esas2r_log(ESAS2R_LOG_CRIT, "pci_register_driver FAILED");
  520. else
  521. esas2r_log(ESAS2R_LOG_INFO, "pci_register_driver() OK");
  522. if (!found_adapters) {
  523. pci_unregister_driver(&esas2r_pci_driver);
  524. esas2r_cleanup(NULL);
  525. esas2r_log(ESAS2R_LOG_CRIT,
  526. "driver will not be loaded because no ATTO "
  527. "%s devices were found",
  528. ESAS2R_DRVR_NAME);
  529. return -1;
  530. } else {
  531. esas2r_log(ESAS2R_LOG_INFO, "found %d adapters",
  532. found_adapters);
  533. }
  534. return 0;
  535. }
  536. /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
  537. static const struct file_operations esas2r_proc_fops = {
  538. .compat_ioctl = esas2r_proc_ioctl,
  539. .unlocked_ioctl = esas2r_proc_ioctl,
  540. };
  541. static struct Scsi_Host *esas2r_proc_host;
  542. static int esas2r_proc_major;
  543. long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  544. {
  545. return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
  546. (int)cmd, (void __user *)arg);
  547. }
  548. static void __exit esas2r_exit(void)
  549. {
  550. esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
  551. if (esas2r_proc_major > 0) {
  552. esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
  553. remove_proc_entry(ATTONODE_NAME,
  554. esas2r_proc_host->hostt->proc_dir);
  555. unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
  556. esas2r_proc_major = 0;
  557. }
  558. esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
  559. pci_unregister_driver(&esas2r_pci_driver);
  560. }
  561. int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
  562. {
  563. struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
  564. struct esas2r_target *t;
  565. int dev_count = 0;
  566. esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
  567. seq_printf(m, ESAS2R_LONGNAME "\n"
  568. "Driver version: "ESAS2R_VERSION_STR "\n"
  569. "Flash version: %s\n"
  570. "Firmware version: %s\n"
  571. "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
  572. "http://www.attotech.com\n"
  573. "\n",
  574. a->flash_rev,
  575. a->fw_rev[0] ? a->fw_rev : "(none)");
  576. seq_printf(m, "Adapter information:\n"
  577. "--------------------\n"
  578. "Model: %s\n"
  579. "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
  580. esas2r_get_model_name(a),
  581. a->nvram->sas_addr[0],
  582. a->nvram->sas_addr[1],
  583. a->nvram->sas_addr[2],
  584. a->nvram->sas_addr[3],
  585. a->nvram->sas_addr[4],
  586. a->nvram->sas_addr[5],
  587. a->nvram->sas_addr[6],
  588. a->nvram->sas_addr[7]);
  589. seq_puts(m, "\n"
  590. "Discovered devices:\n"
  591. "\n"
  592. " # Target ID\n"
  593. "---------------\n");
  594. for (t = a->targetdb; t < a->targetdb_end; t++)
  595. if (t->buffered_target_state == TS_PRESENT) {
  596. seq_printf(m, " %3d %3d\n",
  597. ++dev_count,
  598. (u16)(uintptr_t)(t - a->targetdb));
  599. }
  600. if (dev_count == 0)
  601. seq_puts(m, "none\n");
  602. seq_puts(m, "\n");
  603. return 0;
  604. }
  605. int esas2r_release(struct Scsi_Host *sh)
  606. {
  607. esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
  608. "esas2r_release() called");
  609. esas2r_cleanup(sh);
  610. if (sh->irq)
  611. free_irq(sh->irq, NULL);
  612. scsi_unregister(sh);
  613. return 0;
  614. }
  615. const char *esas2r_info(struct Scsi_Host *sh)
  616. {
  617. struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
  618. static char esas2r_info_str[512];
  619. esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
  620. "esas2r_info() called");
  621. /*
  622. * if we haven't done so already, register as a char driver
  623. * and stick a node under "/proc/scsi/esas2r/ATTOnode"
  624. */
  625. if (esas2r_proc_major <= 0) {
  626. esas2r_proc_host = sh;
  627. esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
  628. &esas2r_proc_fops);
  629. esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
  630. "register_chrdev (major %d)",
  631. esas2r_proc_major);
  632. if (esas2r_proc_major > 0) {
  633. struct proc_dir_entry *pde;
  634. pde = proc_create(ATTONODE_NAME, 0,
  635. sh->hostt->proc_dir,
  636. &esas2r_proc_fops);
  637. if (!pde) {
  638. esas2r_log_dev(ESAS2R_LOG_WARN,
  639. &(sh->shost_gendev),
  640. "failed to create_proc_entry");
  641. esas2r_proc_major = -1;
  642. }
  643. }
  644. }
  645. sprintf(esas2r_info_str,
  646. ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
  647. " driver version: "ESAS2R_VERSION_STR " firmware version: "
  648. "%s\n",
  649. a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
  650. a->fw_rev[0] ? a->fw_rev : "(none)");
  651. return esas2r_info_str;
  652. }
  653. /* Callback for building a request scatter/gather list */
  654. static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
  655. {
  656. u32 len;
  657. if (likely(sgc->cur_offset == sgc->exp_offset)) {
  658. /*
  659. * the normal case: caller used all bytes from previous call, so
  660. * expected offset is the same as the current offset.
  661. */
  662. if (sgc->sgel_count < sgc->num_sgel) {
  663. /* retrieve next segment, except for first time */
  664. if (sgc->exp_offset > (u8 *)0) {
  665. /* advance current segment */
  666. sgc->cur_sgel = sg_next(sgc->cur_sgel);
  667. ++(sgc->sgel_count);
  668. }
  669. len = sg_dma_len(sgc->cur_sgel);
  670. (*addr) = sg_dma_address(sgc->cur_sgel);
  671. /* save the total # bytes returned to caller so far */
  672. sgc->exp_offset += len;
  673. } else {
  674. len = 0;
  675. }
  676. } else if (sgc->cur_offset < sgc->exp_offset) {
  677. /*
  678. * caller did not use all bytes from previous call. need to
  679. * compute the address based on current segment.
  680. */
  681. len = sg_dma_len(sgc->cur_sgel);
  682. (*addr) = sg_dma_address(sgc->cur_sgel);
  683. sgc->exp_offset -= len;
  684. /* calculate PA based on prev segment address and offsets */
  685. *addr = *addr +
  686. (sgc->cur_offset - sgc->exp_offset);
  687. sgc->exp_offset += len;
  688. /* re-calculate length based on offset */
  689. len = lower_32_bits(
  690. sgc->exp_offset - sgc->cur_offset);
  691. } else { /* if ( sgc->cur_offset > sgc->exp_offset ) */
  692. /*
  693. * we don't expect the caller to skip ahead.
  694. * cur_offset will never exceed the len we return
  695. */
  696. len = 0;
  697. }
  698. return len;
  699. }
  700. int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  701. {
  702. struct esas2r_adapter *a =
  703. (struct esas2r_adapter *)cmd->device->host->hostdata;
  704. struct esas2r_request *rq;
  705. struct esas2r_sg_context sgc;
  706. unsigned bufflen;
  707. /* Assume success, if it fails we will fix the result later. */
  708. cmd->result = DID_OK << 16;
  709. if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
  710. cmd->result = DID_NO_CONNECT << 16;
  711. cmd->scsi_done(cmd);
  712. return 0;
  713. }
  714. rq = esas2r_alloc_request(a);
  715. if (unlikely(rq == NULL)) {
  716. esas2r_debug("esas2r_alloc_request failed");
  717. return SCSI_MLQUEUE_HOST_BUSY;
  718. }
  719. rq->cmd = cmd;
  720. bufflen = scsi_bufflen(cmd);
  721. if (likely(bufflen != 0)) {
  722. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  723. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
  724. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  725. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
  726. }
  727. memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
  728. rq->vrq->scsi.length = cpu_to_le32(bufflen);
  729. rq->target_id = cmd->device->id;
  730. rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
  731. rq->sense_buf = cmd->sense_buffer;
  732. rq->sense_len = SCSI_SENSE_BUFFERSIZE;
  733. esas2r_sgc_init(&sgc, a, rq, NULL);
  734. sgc.length = bufflen;
  735. sgc.cur_offset = NULL;
  736. sgc.cur_sgel = scsi_sglist(cmd);
  737. sgc.exp_offset = NULL;
  738. sgc.num_sgel = scsi_dma_map(cmd);
  739. sgc.sgel_count = 0;
  740. if (unlikely(sgc.num_sgel < 0)) {
  741. esas2r_free_request(a, rq);
  742. return SCSI_MLQUEUE_HOST_BUSY;
  743. }
  744. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
  745. if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
  746. scsi_dma_unmap(cmd);
  747. esas2r_free_request(a, rq);
  748. return SCSI_MLQUEUE_HOST_BUSY;
  749. }
  750. esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
  751. (int)cmd->device->lun);
  752. esas2r_start_request(a, rq);
  753. return 0;
  754. }
  755. static void complete_task_management_request(struct esas2r_adapter *a,
  756. struct esas2r_request *rq)
  757. {
  758. (*rq->task_management_status_ptr) = rq->req_stat;
  759. esas2r_free_request(a, rq);
  760. }
  761. /**
  762. * Searches the specified queue for the specified queue for the command
  763. * to abort.
  764. *
  765. * @param [in] a
  766. * @param [in] abort_request
  767. * @param [in] cmd
  768. * t
  769. * @return 0 on failure, 1 if command was not found, 2 if command was found
  770. */
  771. static int esas2r_check_active_queue(struct esas2r_adapter *a,
  772. struct esas2r_request **abort_request,
  773. struct scsi_cmnd *cmd,
  774. struct list_head *queue)
  775. {
  776. bool found = false;
  777. struct esas2r_request *ar = *abort_request;
  778. struct esas2r_request *rq;
  779. struct list_head *element, *next;
  780. list_for_each_safe(element, next, queue) {
  781. rq = list_entry(element, struct esas2r_request, req_list);
  782. if (rq->cmd == cmd) {
  783. /* Found the request. See what to do with it. */
  784. if (queue == &a->active_list) {
  785. /*
  786. * We are searching the active queue, which
  787. * means that we need to send an abort request
  788. * to the firmware.
  789. */
  790. ar = esas2r_alloc_request(a);
  791. if (ar == NULL) {
  792. esas2r_log_dev(ESAS2R_LOG_WARN,
  793. &(a->host->shost_gendev),
  794. "unable to allocate an abort request for cmd %p",
  795. cmd);
  796. return 0; /* Failure */
  797. }
  798. /*
  799. * Task management request must be formatted
  800. * with a lock held.
  801. */
  802. ar->sense_len = 0;
  803. ar->vrq->scsi.length = 0;
  804. ar->target_id = rq->target_id;
  805. ar->vrq->scsi.flags |= cpu_to_le32(
  806. (u8)le32_to_cpu(rq->vrq->scsi.flags));
  807. memset(ar->vrq->scsi.cdb, 0,
  808. sizeof(ar->vrq->scsi.cdb));
  809. ar->vrq->scsi.flags |= cpu_to_le32(
  810. FCP_CMND_TRM);
  811. ar->vrq->scsi.u.abort_handle =
  812. rq->vrq->scsi.handle;
  813. } else {
  814. /*
  815. * The request is pending but not active on
  816. * the firmware. Just free it now and we'll
  817. * report the successful abort below.
  818. */
  819. list_del_init(&rq->req_list);
  820. esas2r_free_request(a, rq);
  821. }
  822. found = true;
  823. break;
  824. }
  825. }
  826. if (!found)
  827. return 1; /* Not found */
  828. return 2; /* found */
  829. }
  830. int esas2r_eh_abort(struct scsi_cmnd *cmd)
  831. {
  832. struct esas2r_adapter *a =
  833. (struct esas2r_adapter *)cmd->device->host->hostdata;
  834. struct esas2r_request *abort_request = NULL;
  835. unsigned long flags;
  836. struct list_head *queue;
  837. int result;
  838. esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
  839. if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
  840. cmd->result = DID_ABORT << 16;
  841. scsi_set_resid(cmd, 0);
  842. cmd->scsi_done(cmd);
  843. return 0;
  844. }
  845. spin_lock_irqsave(&a->queue_lock, flags);
  846. /*
  847. * Run through the defer and active queues looking for the request
  848. * to abort.
  849. */
  850. queue = &a->defer_list;
  851. check_active_queue:
  852. result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
  853. if (!result) {
  854. spin_unlock_irqrestore(&a->queue_lock, flags);
  855. return FAILED;
  856. } else if (result == 2 && (queue == &a->defer_list)) {
  857. queue = &a->active_list;
  858. goto check_active_queue;
  859. }
  860. spin_unlock_irqrestore(&a->queue_lock, flags);
  861. if (abort_request) {
  862. u8 task_management_status = RS_PENDING;
  863. /*
  864. * the request is already active, so we need to tell
  865. * the firmware to abort it and wait for the response.
  866. */
  867. abort_request->comp_cb = complete_task_management_request;
  868. abort_request->task_management_status_ptr =
  869. &task_management_status;
  870. esas2r_start_request(a, abort_request);
  871. if (atomic_read(&a->disable_cnt) == 0)
  872. esas2r_do_deferred_processes(a);
  873. while (task_management_status == RS_PENDING)
  874. msleep(10);
  875. /*
  876. * Once we get here, the original request will have been
  877. * completed by the firmware and the abort request will have
  878. * been cleaned up. we're done!
  879. */
  880. return SUCCESS;
  881. }
  882. /*
  883. * If we get here, either we found the inactive request and
  884. * freed it, or we didn't find it at all. Either way, success!
  885. */
  886. cmd->result = DID_ABORT << 16;
  887. scsi_set_resid(cmd, 0);
  888. cmd->scsi_done(cmd);
  889. return SUCCESS;
  890. }
  891. static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
  892. {
  893. struct esas2r_adapter *a =
  894. (struct esas2r_adapter *)cmd->device->host->hostdata;
  895. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  896. return FAILED;
  897. if (host_reset)
  898. esas2r_reset_adapter(a);
  899. else
  900. esas2r_reset_bus(a);
  901. /* above call sets the AF_OS_RESET flag. wait for it to clear. */
  902. while (test_bit(AF_OS_RESET, &a->flags)) {
  903. msleep(10);
  904. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  905. return FAILED;
  906. }
  907. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  908. return FAILED;
  909. return SUCCESS;
  910. }
  911. int esas2r_host_reset(struct scsi_cmnd *cmd)
  912. {
  913. esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
  914. return esas2r_host_bus_reset(cmd, true);
  915. }
  916. int esas2r_bus_reset(struct scsi_cmnd *cmd)
  917. {
  918. esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
  919. return esas2r_host_bus_reset(cmd, false);
  920. }
  921. static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
  922. {
  923. struct esas2r_adapter *a =
  924. (struct esas2r_adapter *)cmd->device->host->hostdata;
  925. struct esas2r_request *rq;
  926. u8 task_management_status = RS_PENDING;
  927. bool completed;
  928. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  929. return FAILED;
  930. retry:
  931. rq = esas2r_alloc_request(a);
  932. if (rq == NULL) {
  933. if (target_reset) {
  934. esas2r_log(ESAS2R_LOG_CRIT,
  935. "unable to allocate a request for a "
  936. "target reset (%d)!",
  937. cmd->device->id);
  938. } else {
  939. esas2r_log(ESAS2R_LOG_CRIT,
  940. "unable to allocate a request for a "
  941. "device reset (%d:%d)!",
  942. cmd->device->id,
  943. cmd->device->lun);
  944. }
  945. return FAILED;
  946. }
  947. rq->target_id = cmd->device->id;
  948. rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
  949. rq->req_stat = RS_PENDING;
  950. rq->comp_cb = complete_task_management_request;
  951. rq->task_management_status_ptr = &task_management_status;
  952. if (target_reset) {
  953. esas2r_debug("issuing target reset (%p) to id %d", rq,
  954. cmd->device->id);
  955. completed = esas2r_send_task_mgmt(a, rq, 0x20);
  956. } else {
  957. esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
  958. cmd->device->id, cmd->device->lun);
  959. completed = esas2r_send_task_mgmt(a, rq, 0x10);
  960. }
  961. if (completed) {
  962. /* Task management cmd completed right away, need to free it. */
  963. esas2r_free_request(a, rq);
  964. } else {
  965. /*
  966. * Wait for firmware to complete the request. Completion
  967. * callback will free it.
  968. */
  969. while (task_management_status == RS_PENDING)
  970. msleep(10);
  971. }
  972. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  973. return FAILED;
  974. if (task_management_status == RS_BUSY) {
  975. /*
  976. * Busy, probably because we are flashing. Wait a bit and
  977. * try again.
  978. */
  979. msleep(100);
  980. goto retry;
  981. }
  982. return SUCCESS;
  983. }
  984. int esas2r_device_reset(struct scsi_cmnd *cmd)
  985. {
  986. esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
  987. return esas2r_dev_targ_reset(cmd, false);
  988. }
  989. int esas2r_target_reset(struct scsi_cmnd *cmd)
  990. {
  991. esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
  992. return esas2r_dev_targ_reset(cmd, true);
  993. }
  994. int esas2r_change_queue_depth(struct scsi_device *dev, int depth, int reason)
  995. {
  996. esas2r_log(ESAS2R_LOG_INFO, "change_queue_depth %p, %d", dev, depth);
  997. scsi_adjust_queue_depth(dev, scsi_get_tag_type(dev), depth);
  998. return dev->queue_depth;
  999. }
  1000. int esas2r_change_queue_type(struct scsi_device *dev, int type)
  1001. {
  1002. esas2r_log(ESAS2R_LOG_INFO, "change_queue_type %p, %d", dev, type);
  1003. if (dev->tagged_supported) {
  1004. scsi_set_tag_type(dev, type);
  1005. if (type)
  1006. scsi_activate_tcq(dev, dev->queue_depth);
  1007. else
  1008. scsi_deactivate_tcq(dev, dev->queue_depth);
  1009. } else {
  1010. type = 0;
  1011. }
  1012. return type;
  1013. }
  1014. int esas2r_slave_alloc(struct scsi_device *dev)
  1015. {
  1016. return 0;
  1017. }
  1018. int esas2r_slave_configure(struct scsi_device *dev)
  1019. {
  1020. esas2r_log_dev(ESAS2R_LOG_INFO, &(dev->sdev_gendev),
  1021. "esas2r_slave_configure()");
  1022. if (dev->tagged_supported) {
  1023. scsi_set_tag_type(dev, MSG_SIMPLE_TAG);
  1024. scsi_activate_tcq(dev, cmd_per_lun);
  1025. } else {
  1026. scsi_set_tag_type(dev, 0);
  1027. scsi_deactivate_tcq(dev, cmd_per_lun);
  1028. }
  1029. return 0;
  1030. }
  1031. void esas2r_slave_destroy(struct scsi_device *dev)
  1032. {
  1033. esas2r_log_dev(ESAS2R_LOG_INFO, &(dev->sdev_gendev),
  1034. "esas2r_slave_destroy()");
  1035. }
  1036. void esas2r_log_request_failure(struct esas2r_adapter *a,
  1037. struct esas2r_request *rq)
  1038. {
  1039. u8 reqstatus = rq->req_stat;
  1040. if (reqstatus == RS_SUCCESS)
  1041. return;
  1042. if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
  1043. if (reqstatus == RS_SCSI_ERROR) {
  1044. if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
  1045. esas2r_log(ESAS2R_LOG_WARN,
  1046. "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
  1047. rq->sense_buf[2], rq->sense_buf[12],
  1048. rq->sense_buf[13],
  1049. rq->vrq->scsi.cdb[0]);
  1050. } else {
  1051. esas2r_log(ESAS2R_LOG_WARN,
  1052. "request failure - SCSI error CDB:%x\n",
  1053. rq->vrq->scsi.cdb[0]);
  1054. }
  1055. } else if ((rq->vrq->scsi.cdb[0] != INQUIRY
  1056. && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
  1057. || (reqstatus != RS_SEL
  1058. && reqstatus != RS_SEL2)) {
  1059. if ((reqstatus == RS_UNDERRUN) &&
  1060. (rq->vrq->scsi.cdb[0] == INQUIRY)) {
  1061. /* Don't log inquiry underruns */
  1062. } else {
  1063. esas2r_log(ESAS2R_LOG_WARN,
  1064. "request failure - cdb:%x reqstatus:%d target:%d",
  1065. rq->vrq->scsi.cdb[0], reqstatus,
  1066. rq->target_id);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
  1072. {
  1073. u32 starttime;
  1074. u32 timeout;
  1075. starttime = jiffies_to_msecs(jiffies);
  1076. timeout = rq->timeout ? rq->timeout : 5000;
  1077. while (true) {
  1078. esas2r_polled_interrupt(a);
  1079. if (rq->req_stat != RS_STARTED)
  1080. break;
  1081. schedule_timeout_interruptible(msecs_to_jiffies(100));
  1082. if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
  1083. esas2r_hdebug("request TMO");
  1084. esas2r_bugon();
  1085. rq->req_stat = RS_TIMEOUT;
  1086. esas2r_local_reset_adapter(a);
  1087. return;
  1088. }
  1089. }
  1090. }
  1091. u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
  1092. {
  1093. u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
  1094. u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
  1095. if (a->window_base != base) {
  1096. esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
  1097. base | MVRPW1R_ENABLE);
  1098. esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
  1099. a->window_base = base;
  1100. }
  1101. return offset;
  1102. }
  1103. /* Read a block of data from chip memory */
  1104. bool esas2r_read_mem_block(struct esas2r_adapter *a,
  1105. void *to,
  1106. u32 from,
  1107. u32 size)
  1108. {
  1109. u8 *end = (u8 *)to;
  1110. while (size) {
  1111. u32 len;
  1112. u32 offset;
  1113. u32 iatvr;
  1114. iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
  1115. esas2r_map_data_window(a, iatvr);
  1116. offset = from & (MW_DATA_WINDOW_SIZE - 1);
  1117. len = size;
  1118. if (len > MW_DATA_WINDOW_SIZE - offset)
  1119. len = MW_DATA_WINDOW_SIZE - offset;
  1120. from += len;
  1121. size -= len;
  1122. while (len--) {
  1123. *end++ = esas2r_read_data_byte(a, offset);
  1124. offset++;
  1125. }
  1126. }
  1127. return true;
  1128. }
  1129. void esas2r_nuxi_mgt_data(u8 function, void *data)
  1130. {
  1131. struct atto_vda_grp_info *g;
  1132. struct atto_vda_devinfo *d;
  1133. struct atto_vdapart_info *p;
  1134. struct atto_vda_dh_info *h;
  1135. struct atto_vda_metrics_info *m;
  1136. struct atto_vda_schedule_info *s;
  1137. struct atto_vda_buzzer_info *b;
  1138. u8 i;
  1139. switch (function) {
  1140. case VDAMGT_BUZZER_INFO:
  1141. case VDAMGT_BUZZER_SET:
  1142. b = (struct atto_vda_buzzer_info *)data;
  1143. b->duration = le32_to_cpu(b->duration);
  1144. break;
  1145. case VDAMGT_SCHEDULE_INFO:
  1146. case VDAMGT_SCHEDULE_EVENT:
  1147. s = (struct atto_vda_schedule_info *)data;
  1148. s->id = le32_to_cpu(s->id);
  1149. break;
  1150. case VDAMGT_DEV_INFO:
  1151. case VDAMGT_DEV_CLEAN:
  1152. case VDAMGT_DEV_PT_INFO:
  1153. case VDAMGT_DEV_FEATURES:
  1154. case VDAMGT_DEV_PT_FEATURES:
  1155. case VDAMGT_DEV_OPERATION:
  1156. d = (struct atto_vda_devinfo *)data;
  1157. d->capacity = le64_to_cpu(d->capacity);
  1158. d->block_size = le32_to_cpu(d->block_size);
  1159. d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
  1160. d->target_id = le16_to_cpu(d->target_id);
  1161. d->lun = le16_to_cpu(d->lun);
  1162. d->features = le16_to_cpu(d->features);
  1163. break;
  1164. case VDAMGT_GRP_INFO:
  1165. case VDAMGT_GRP_CREATE:
  1166. case VDAMGT_GRP_DELETE:
  1167. case VDAMGT_ADD_STORAGE:
  1168. case VDAMGT_MEMBER_ADD:
  1169. case VDAMGT_GRP_COMMIT:
  1170. case VDAMGT_GRP_REBUILD:
  1171. case VDAMGT_GRP_COMMIT_INIT:
  1172. case VDAMGT_QUICK_RAID:
  1173. case VDAMGT_GRP_FEATURES:
  1174. case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
  1175. case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
  1176. case VDAMGT_SPARE_LIST:
  1177. case VDAMGT_SPARE_ADD:
  1178. case VDAMGT_SPARE_REMOVE:
  1179. case VDAMGT_LOCAL_SPARE_ADD:
  1180. case VDAMGT_GRP_OPERATION:
  1181. g = (struct atto_vda_grp_info *)data;
  1182. g->capacity = le64_to_cpu(g->capacity);
  1183. g->block_size = le32_to_cpu(g->block_size);
  1184. g->interleave = le32_to_cpu(g->interleave);
  1185. g->features = le16_to_cpu(g->features);
  1186. for (i = 0; i < 32; i++)
  1187. g->members[i] = le16_to_cpu(g->members[i]);
  1188. break;
  1189. case VDAMGT_PART_INFO:
  1190. case VDAMGT_PART_MAP:
  1191. case VDAMGT_PART_UNMAP:
  1192. case VDAMGT_PART_AUTOMAP:
  1193. case VDAMGT_PART_SPLIT:
  1194. case VDAMGT_PART_MERGE:
  1195. p = (struct atto_vdapart_info *)data;
  1196. p->part_size = le64_to_cpu(p->part_size);
  1197. p->start_lba = le32_to_cpu(p->start_lba);
  1198. p->block_size = le32_to_cpu(p->block_size);
  1199. p->target_id = le16_to_cpu(p->target_id);
  1200. break;
  1201. case VDAMGT_DEV_HEALTH_REQ:
  1202. h = (struct atto_vda_dh_info *)data;
  1203. h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
  1204. h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
  1205. break;
  1206. case VDAMGT_DEV_METRICS:
  1207. m = (struct atto_vda_metrics_info *)data;
  1208. for (i = 0; i < 32; i++)
  1209. m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
  1210. break;
  1211. default:
  1212. break;
  1213. }
  1214. }
  1215. void esas2r_nuxi_cfg_data(u8 function, void *data)
  1216. {
  1217. struct atto_vda_cfg_init *ci;
  1218. switch (function) {
  1219. case VDA_CFG_INIT:
  1220. case VDA_CFG_GET_INIT:
  1221. case VDA_CFG_GET_INIT2:
  1222. ci = (struct atto_vda_cfg_init *)data;
  1223. ci->date_time.year = le16_to_cpu(ci->date_time.year);
  1224. ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
  1225. ci->vda_version = le32_to_cpu(ci->vda_version);
  1226. ci->epoch_time = le32_to_cpu(ci->epoch_time);
  1227. ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
  1228. ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
  1229. break;
  1230. default:
  1231. break;
  1232. }
  1233. }
  1234. void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
  1235. {
  1236. struct atto_vda_ae_raid *r = &ae->raid;
  1237. struct atto_vda_ae_lu *l = &ae->lu;
  1238. switch (ae->hdr.bytype) {
  1239. case VDAAE_HDR_TYPE_RAID:
  1240. r->dwflags = le32_to_cpu(r->dwflags);
  1241. break;
  1242. case VDAAE_HDR_TYPE_LU:
  1243. l->dwevent = le32_to_cpu(l->dwevent);
  1244. l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
  1245. l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
  1246. if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
  1247. + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
  1248. l->id.tgtlun_raid.dwinterleave
  1249. = le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
  1250. l->id.tgtlun_raid.dwblock_size
  1251. = le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
  1252. }
  1253. break;
  1254. case VDAAE_HDR_TYPE_DISK:
  1255. default:
  1256. break;
  1257. }
  1258. }
  1259. void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
  1260. {
  1261. unsigned long flags;
  1262. esas2r_rq_destroy_request(rq, a);
  1263. spin_lock_irqsave(&a->request_lock, flags);
  1264. list_add(&rq->comp_list, &a->avail_request);
  1265. spin_unlock_irqrestore(&a->request_lock, flags);
  1266. }
  1267. struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
  1268. {
  1269. struct esas2r_request *rq;
  1270. unsigned long flags;
  1271. spin_lock_irqsave(&a->request_lock, flags);
  1272. if (unlikely(list_empty(&a->avail_request))) {
  1273. spin_unlock_irqrestore(&a->request_lock, flags);
  1274. return NULL;
  1275. }
  1276. rq = list_first_entry(&a->avail_request, struct esas2r_request,
  1277. comp_list);
  1278. list_del(&rq->comp_list);
  1279. spin_unlock_irqrestore(&a->request_lock, flags);
  1280. esas2r_rq_init_request(rq, a);
  1281. return rq;
  1282. }
  1283. void esas2r_complete_request_cb(struct esas2r_adapter *a,
  1284. struct esas2r_request *rq)
  1285. {
  1286. esas2r_debug("completing request %p\n", rq);
  1287. scsi_dma_unmap(rq->cmd);
  1288. if (unlikely(rq->req_stat != RS_SUCCESS)) {
  1289. esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
  1290. rq->req_stat,
  1291. rq->func_rsp.scsi_rsp.scsi_stat,
  1292. rq->cmd);
  1293. rq->cmd->result =
  1294. ((esas2r_req_status_to_error(rq->req_stat) << 16)
  1295. | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
  1296. if (rq->req_stat == RS_UNDERRUN)
  1297. scsi_set_resid(rq->cmd,
  1298. le32_to_cpu(rq->func_rsp.scsi_rsp.
  1299. residual_length));
  1300. else
  1301. scsi_set_resid(rq->cmd, 0);
  1302. }
  1303. rq->cmd->scsi_done(rq->cmd);
  1304. esas2r_free_request(a, rq);
  1305. }
  1306. /* Run tasklet to handle stuff outside of interrupt context. */
  1307. void esas2r_adapter_tasklet(unsigned long context)
  1308. {
  1309. struct esas2r_adapter *a = (struct esas2r_adapter *)context;
  1310. if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
  1311. clear_bit(AF2_TIMER_TICK, &a->flags2);
  1312. esas2r_timer_tick(a);
  1313. }
  1314. if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
  1315. clear_bit(AF2_INT_PENDING, &a->flags2);
  1316. esas2r_adapter_interrupt(a);
  1317. }
  1318. if (esas2r_is_tasklet_pending(a))
  1319. esas2r_do_tasklet_tasks(a);
  1320. if (esas2r_is_tasklet_pending(a)
  1321. || (test_bit(AF2_INT_PENDING, &a->flags2))
  1322. || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
  1323. clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
  1324. esas2r_schedule_tasklet(a);
  1325. } else {
  1326. clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
  1327. }
  1328. }
  1329. static void esas2r_timer_callback(unsigned long context);
  1330. void esas2r_kickoff_timer(struct esas2r_adapter *a)
  1331. {
  1332. init_timer(&a->timer);
  1333. a->timer.function = esas2r_timer_callback;
  1334. a->timer.data = (unsigned long)a;
  1335. a->timer.expires = jiffies +
  1336. msecs_to_jiffies(100);
  1337. add_timer(&a->timer);
  1338. }
  1339. static void esas2r_timer_callback(unsigned long context)
  1340. {
  1341. struct esas2r_adapter *a = (struct esas2r_adapter *)context;
  1342. set_bit(AF2_TIMER_TICK, &a->flags2);
  1343. esas2r_schedule_tasklet(a);
  1344. esas2r_kickoff_timer(a);
  1345. }
  1346. /*
  1347. * Firmware events need to be handled outside of interrupt context
  1348. * so we schedule a delayed_work to handle them.
  1349. */
  1350. static void
  1351. esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
  1352. {
  1353. unsigned long flags;
  1354. struct esas2r_adapter *a = fw_event->a;
  1355. spin_lock_irqsave(&a->fw_event_lock, flags);
  1356. list_del(&fw_event->list);
  1357. kfree(fw_event);
  1358. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1359. }
  1360. void
  1361. esas2r_fw_event_off(struct esas2r_adapter *a)
  1362. {
  1363. unsigned long flags;
  1364. spin_lock_irqsave(&a->fw_event_lock, flags);
  1365. a->fw_events_off = 1;
  1366. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1367. }
  1368. void
  1369. esas2r_fw_event_on(struct esas2r_adapter *a)
  1370. {
  1371. unsigned long flags;
  1372. spin_lock_irqsave(&a->fw_event_lock, flags);
  1373. a->fw_events_off = 0;
  1374. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1375. }
  1376. static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
  1377. {
  1378. int ret;
  1379. struct scsi_device *scsi_dev;
  1380. scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
  1381. if (scsi_dev) {
  1382. esas2r_log_dev(
  1383. ESAS2R_LOG_WARN,
  1384. &(scsi_dev->
  1385. sdev_gendev),
  1386. "scsi device already exists at id %d", target_id);
  1387. scsi_device_put(scsi_dev);
  1388. } else {
  1389. esas2r_log_dev(
  1390. ESAS2R_LOG_INFO,
  1391. &(a->host->
  1392. shost_gendev),
  1393. "scsi_add_device() called for 0:%d:0",
  1394. target_id);
  1395. ret = scsi_add_device(a->host, 0, target_id, 0);
  1396. if (ret) {
  1397. esas2r_log_dev(
  1398. ESAS2R_LOG_CRIT,
  1399. &(a->host->
  1400. shost_gendev),
  1401. "scsi_add_device failed with %d for id %d",
  1402. ret, target_id);
  1403. }
  1404. }
  1405. }
  1406. static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
  1407. {
  1408. struct scsi_device *scsi_dev;
  1409. scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
  1410. if (scsi_dev) {
  1411. scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
  1412. esas2r_log_dev(
  1413. ESAS2R_LOG_INFO,
  1414. &(scsi_dev->
  1415. sdev_gendev),
  1416. "scsi_remove_device() called for 0:%d:0",
  1417. target_id);
  1418. scsi_remove_device(scsi_dev);
  1419. esas2r_log_dev(
  1420. ESAS2R_LOG_INFO,
  1421. &(scsi_dev->
  1422. sdev_gendev),
  1423. "scsi_device_put() called");
  1424. scsi_device_put(scsi_dev);
  1425. } else {
  1426. esas2r_log_dev(
  1427. ESAS2R_LOG_WARN,
  1428. &(a->host->shost_gendev),
  1429. "no target found at id %d",
  1430. target_id);
  1431. }
  1432. }
  1433. /*
  1434. * Sends a firmware asynchronous event to anyone who happens to be
  1435. * listening on the defined ATTO VDA event ports.
  1436. */
  1437. static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
  1438. {
  1439. struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
  1440. char *type;
  1441. switch (ae->vda_ae.hdr.bytype) {
  1442. case VDAAE_HDR_TYPE_RAID:
  1443. type = "RAID group state change";
  1444. break;
  1445. case VDAAE_HDR_TYPE_LU:
  1446. type = "Mapped destination LU change";
  1447. break;
  1448. case VDAAE_HDR_TYPE_DISK:
  1449. type = "Physical disk inventory change";
  1450. break;
  1451. case VDAAE_HDR_TYPE_RESET:
  1452. type = "Firmware reset";
  1453. break;
  1454. case VDAAE_HDR_TYPE_LOG_INFO:
  1455. type = "Event Log message (INFO level)";
  1456. break;
  1457. case VDAAE_HDR_TYPE_LOG_WARN:
  1458. type = "Event Log message (WARN level)";
  1459. break;
  1460. case VDAAE_HDR_TYPE_LOG_CRIT:
  1461. type = "Event Log message (CRIT level)";
  1462. break;
  1463. case VDAAE_HDR_TYPE_LOG_FAIL:
  1464. type = "Event Log message (FAIL level)";
  1465. break;
  1466. case VDAAE_HDR_TYPE_NVC:
  1467. type = "NVCache change";
  1468. break;
  1469. case VDAAE_HDR_TYPE_TLG_INFO:
  1470. type = "Time stamped log message (INFO level)";
  1471. break;
  1472. case VDAAE_HDR_TYPE_TLG_WARN:
  1473. type = "Time stamped log message (WARN level)";
  1474. break;
  1475. case VDAAE_HDR_TYPE_TLG_CRIT:
  1476. type = "Time stamped log message (CRIT level)";
  1477. break;
  1478. case VDAAE_HDR_TYPE_PWRMGT:
  1479. type = "Power management";
  1480. break;
  1481. case VDAAE_HDR_TYPE_MUTE:
  1482. type = "Mute button pressed";
  1483. break;
  1484. case VDAAE_HDR_TYPE_DEV:
  1485. type = "Device attribute change";
  1486. break;
  1487. default:
  1488. type = "Unknown";
  1489. break;
  1490. }
  1491. esas2r_log(ESAS2R_LOG_WARN,
  1492. "An async event of type \"%s\" was received from the firmware. The event contents are:",
  1493. type);
  1494. esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
  1495. ae->vda_ae.hdr.bylength);
  1496. }
  1497. static void
  1498. esas2r_firmware_event_work(struct work_struct *work)
  1499. {
  1500. struct esas2r_fw_event_work *fw_event =
  1501. container_of(work, struct esas2r_fw_event_work, work.work);
  1502. struct esas2r_adapter *a = fw_event->a;
  1503. u16 target_id = *(u16 *)&fw_event->data[0];
  1504. if (a->fw_events_off)
  1505. goto done;
  1506. switch (fw_event->type) {
  1507. case fw_event_null:
  1508. break; /* do nothing */
  1509. case fw_event_lun_change:
  1510. esas2r_remove_device(a, target_id);
  1511. esas2r_add_device(a, target_id);
  1512. break;
  1513. case fw_event_present:
  1514. esas2r_add_device(a, target_id);
  1515. break;
  1516. case fw_event_not_present:
  1517. esas2r_remove_device(a, target_id);
  1518. break;
  1519. case fw_event_vda_ae:
  1520. esas2r_send_ae_event(fw_event);
  1521. break;
  1522. }
  1523. done:
  1524. esas2r_free_fw_event(fw_event);
  1525. }
  1526. void esas2r_queue_fw_event(struct esas2r_adapter *a,
  1527. enum fw_event_type type,
  1528. void *data,
  1529. int data_sz)
  1530. {
  1531. struct esas2r_fw_event_work *fw_event;
  1532. unsigned long flags;
  1533. fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
  1534. if (!fw_event) {
  1535. esas2r_log(ESAS2R_LOG_WARN,
  1536. "esas2r_queue_fw_event failed to alloc");
  1537. return;
  1538. }
  1539. if (type == fw_event_vda_ae) {
  1540. struct esas2r_vda_ae *ae =
  1541. (struct esas2r_vda_ae *)fw_event->data;
  1542. ae->signature = ESAS2R_VDA_EVENT_SIG;
  1543. ae->bus_number = a->pcid->bus->number;
  1544. ae->devfn = a->pcid->devfn;
  1545. memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
  1546. } else {
  1547. memcpy(fw_event->data, data, data_sz);
  1548. }
  1549. fw_event->type = type;
  1550. fw_event->a = a;
  1551. spin_lock_irqsave(&a->fw_event_lock, flags);
  1552. list_add_tail(&fw_event->list, &a->fw_event_list);
  1553. INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
  1554. queue_delayed_work_on(
  1555. smp_processor_id(), a->fw_event_q, &fw_event->work,
  1556. msecs_to_jiffies(1));
  1557. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  1558. }
  1559. void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
  1560. u8 state)
  1561. {
  1562. if (state == TS_LUN_CHANGE)
  1563. esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
  1564. sizeof(targ_id));
  1565. else if (state == TS_PRESENT)
  1566. esas2r_queue_fw_event(a, fw_event_present, &targ_id,
  1567. sizeof(targ_id));
  1568. else if (state == TS_NOT_PRESENT)
  1569. esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
  1570. sizeof(targ_id));
  1571. }
  1572. /* Translate status to a Linux SCSI mid-layer error code */
  1573. int esas2r_req_status_to_error(u8 req_stat)
  1574. {
  1575. switch (req_stat) {
  1576. case RS_OVERRUN:
  1577. case RS_UNDERRUN:
  1578. case RS_SUCCESS:
  1579. /*
  1580. * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
  1581. * it will check the scsi_stat value in the completion anyway.
  1582. */
  1583. case RS_SCSI_ERROR:
  1584. return DID_OK;
  1585. case RS_SEL:
  1586. case RS_SEL2:
  1587. return DID_NO_CONNECT;
  1588. case RS_RESET:
  1589. return DID_RESET;
  1590. case RS_ABORTED:
  1591. return DID_ABORT;
  1592. case RS_BUSY:
  1593. return DID_BUS_BUSY;
  1594. }
  1595. /* everything else is just an error. */
  1596. return DID_ERROR;
  1597. }
  1598. module_init(esas2r_init);
  1599. module_exit(esas2r_exit);