esas2r_main.c 48 KB

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