ibmvscsi.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robbin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <asm/firmware.h>
  73. #include <asm/vio.h>
  74. #include <asm/firmware.h>
  75. #include <scsi/scsi.h>
  76. #include <scsi/scsi_cmnd.h>
  77. #include <scsi/scsi_host.h>
  78. #include <scsi/scsi_device.h>
  79. #include <scsi/scsi_transport_srp.h>
  80. #include "ibmvscsi.h"
  81. /* The values below are somewhat arbitrary default values, but
  82. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  83. * Note that there are 3 bits of channel value, 6 bits of id, and
  84. * 5 bits of LUN.
  85. */
  86. static int max_id = 64;
  87. static int max_channel = 3;
  88. static int init_timeout = 5;
  89. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  90. static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
  91. static struct scsi_transport_template *ibmvscsi_transport_template;
  92. #define IBMVSCSI_VERSION "1.5.8"
  93. static struct ibmvscsi_ops *ibmvscsi_ops;
  94. MODULE_DESCRIPTION("IBM Virtual SCSI");
  95. MODULE_AUTHOR("Dave Boutcher");
  96. MODULE_LICENSE("GPL");
  97. MODULE_VERSION(IBMVSCSI_VERSION);
  98. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  100. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  101. MODULE_PARM_DESC(max_channel, "Largest channel value");
  102. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  103. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  104. module_param_named(max_requests, max_requests, int, S_IRUGO);
  105. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  106. /* ------------------------------------------------------------
  107. * Routines for the event pool and event structs
  108. */
  109. /**
  110. * initialize_event_pool: - Allocates and initializes the event pool for a host
  111. * @pool: event_pool to be initialized
  112. * @size: Number of events in pool
  113. * @hostdata: ibmvscsi_host_data who owns the event pool
  114. *
  115. * Returns zero on success.
  116. */
  117. static int initialize_event_pool(struct event_pool *pool,
  118. int size, struct ibmvscsi_host_data *hostdata)
  119. {
  120. int i;
  121. pool->size = size;
  122. pool->next = 0;
  123. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  124. if (!pool->events)
  125. return -ENOMEM;
  126. pool->iu_storage =
  127. dma_alloc_coherent(hostdata->dev,
  128. pool->size * sizeof(*pool->iu_storage),
  129. &pool->iu_token, 0);
  130. if (!pool->iu_storage) {
  131. kfree(pool->events);
  132. return -ENOMEM;
  133. }
  134. for (i = 0; i < pool->size; ++i) {
  135. struct srp_event_struct *evt = &pool->events[i];
  136. memset(&evt->crq, 0x00, sizeof(evt->crq));
  137. atomic_set(&evt->free, 1);
  138. evt->crq.valid = 0x80;
  139. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  140. evt->crq.IU_data_ptr = pool->iu_token +
  141. sizeof(*evt->xfer_iu) * i;
  142. evt->xfer_iu = pool->iu_storage + i;
  143. evt->hostdata = hostdata;
  144. evt->ext_list = NULL;
  145. evt->ext_list_token = 0;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * release_event_pool: - Frees memory of an event pool of a host
  151. * @pool: event_pool to be released
  152. * @hostdata: ibmvscsi_host_data who owns the even pool
  153. *
  154. * Returns zero on success.
  155. */
  156. static void release_event_pool(struct event_pool *pool,
  157. struct ibmvscsi_host_data *hostdata)
  158. {
  159. int i, in_use = 0;
  160. for (i = 0; i < pool->size; ++i) {
  161. if (atomic_read(&pool->events[i].free) != 1)
  162. ++in_use;
  163. if (pool->events[i].ext_list) {
  164. dma_free_coherent(hostdata->dev,
  165. SG_ALL * sizeof(struct srp_direct_buf),
  166. pool->events[i].ext_list,
  167. pool->events[i].ext_list_token);
  168. }
  169. }
  170. if (in_use)
  171. dev_warn(hostdata->dev, "releasing event pool with %d "
  172. "events still in use?\n", in_use);
  173. kfree(pool->events);
  174. dma_free_coherent(hostdata->dev,
  175. pool->size * sizeof(*pool->iu_storage),
  176. pool->iu_storage, pool->iu_token);
  177. }
  178. /**
  179. * valid_event_struct: - Determines if event is valid.
  180. * @pool: event_pool that contains the event
  181. * @evt: srp_event_struct to be checked for validity
  182. *
  183. * Returns zero if event is invalid, one otherwise.
  184. */
  185. static int valid_event_struct(struct event_pool *pool,
  186. struct srp_event_struct *evt)
  187. {
  188. int index = evt - pool->events;
  189. if (index < 0 || index >= pool->size) /* outside of bounds */
  190. return 0;
  191. if (evt != pool->events + index) /* unaligned */
  192. return 0;
  193. return 1;
  194. }
  195. /**
  196. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  197. * @pool: event_pool that contains the event
  198. * @evt: srp_event_struct to be modified
  199. *
  200. */
  201. static void free_event_struct(struct event_pool *pool,
  202. struct srp_event_struct *evt)
  203. {
  204. if (!valid_event_struct(pool, evt)) {
  205. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  206. "(not in pool %p)\n", evt, pool->events);
  207. return;
  208. }
  209. if (atomic_inc_return(&evt->free) != 1) {
  210. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  211. "which is not in use!\n", evt);
  212. return;
  213. }
  214. }
  215. /**
  216. * get_evt_struct: - Gets the next free event in pool
  217. * @pool: event_pool that contains the events to be searched
  218. *
  219. * Returns the next event in "free" state, and NULL if none are free.
  220. * Note that no synchronization is done here, we assume the host_lock
  221. * will syncrhonze things.
  222. */
  223. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  224. {
  225. int i;
  226. int poolsize = pool->size;
  227. int offset = pool->next;
  228. for (i = 0; i < poolsize; i++) {
  229. offset = (offset + 1) % poolsize;
  230. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  231. pool->next = offset;
  232. return &pool->events[offset];
  233. }
  234. }
  235. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  236. return NULL;
  237. }
  238. /**
  239. * init_event_struct: Initialize fields in an event struct that are always
  240. * required.
  241. * @evt: The event
  242. * @done: Routine to call when the event is responded to
  243. * @format: SRP or MAD format
  244. * @timeout: timeout value set in the CRQ
  245. */
  246. static void init_event_struct(struct srp_event_struct *evt_struct,
  247. void (*done) (struct srp_event_struct *),
  248. u8 format,
  249. int timeout)
  250. {
  251. evt_struct->cmnd = NULL;
  252. evt_struct->cmnd_done = NULL;
  253. evt_struct->sync_srp = NULL;
  254. evt_struct->crq.format = format;
  255. evt_struct->crq.timeout = timeout;
  256. evt_struct->done = done;
  257. }
  258. /* ------------------------------------------------------------
  259. * Routines for receiving SCSI responses from the hosting partition
  260. */
  261. /**
  262. * set_srp_direction: Set the fields in the srp related to data
  263. * direction and number of buffers based on the direction in
  264. * the scsi_cmnd and the number of buffers
  265. */
  266. static void set_srp_direction(struct scsi_cmnd *cmd,
  267. struct srp_cmd *srp_cmd,
  268. int numbuf)
  269. {
  270. u8 fmt;
  271. if (numbuf == 0)
  272. return;
  273. if (numbuf == 1)
  274. fmt = SRP_DATA_DESC_DIRECT;
  275. else {
  276. fmt = SRP_DATA_DESC_INDIRECT;
  277. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  278. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  279. srp_cmd->data_out_desc_cnt = numbuf;
  280. else
  281. srp_cmd->data_in_desc_cnt = numbuf;
  282. }
  283. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  284. srp_cmd->buf_fmt = fmt << 4;
  285. else
  286. srp_cmd->buf_fmt = fmt;
  287. }
  288. static void unmap_sg_list(int num_entries,
  289. struct device *dev,
  290. struct srp_direct_buf *md)
  291. {
  292. int i;
  293. for (i = 0; i < num_entries; ++i)
  294. dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
  295. }
  296. /**
  297. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  298. * @cmd: srp_cmd whose additional_data member will be unmapped
  299. * @dev: device for which the memory is mapped
  300. *
  301. */
  302. static void unmap_cmd_data(struct srp_cmd *cmd,
  303. struct srp_event_struct *evt_struct,
  304. struct device *dev)
  305. {
  306. u8 out_fmt, in_fmt;
  307. out_fmt = cmd->buf_fmt >> 4;
  308. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  309. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  310. return;
  311. else if (out_fmt == SRP_DATA_DESC_DIRECT ||
  312. in_fmt == SRP_DATA_DESC_DIRECT) {
  313. struct srp_direct_buf *data =
  314. (struct srp_direct_buf *) cmd->add_data;
  315. dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
  316. } else {
  317. struct srp_indirect_buf *indirect =
  318. (struct srp_indirect_buf *) cmd->add_data;
  319. int num_mapped = indirect->table_desc.len /
  320. sizeof(struct srp_direct_buf);
  321. if (num_mapped <= MAX_INDIRECT_BUFS) {
  322. unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
  323. return;
  324. }
  325. unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
  326. }
  327. }
  328. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  329. struct srp_direct_buf *md)
  330. {
  331. int i;
  332. struct scatterlist *sg;
  333. u64 total_length = 0;
  334. scsi_for_each_sg(cmd, sg, nseg, i) {
  335. struct srp_direct_buf *descr = md + i;
  336. descr->va = sg_dma_address(sg);
  337. descr->len = sg_dma_len(sg);
  338. descr->key = 0;
  339. total_length += sg_dma_len(sg);
  340. }
  341. return total_length;
  342. }
  343. /**
  344. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  345. * @cmd: Scsi_Cmnd with the scatterlist
  346. * @srp_cmd: srp_cmd that contains the memory descriptor
  347. * @dev: device for which to map dma memory
  348. *
  349. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  350. * Returns 1 on success.
  351. */
  352. static int map_sg_data(struct scsi_cmnd *cmd,
  353. struct srp_event_struct *evt_struct,
  354. struct srp_cmd *srp_cmd, struct device *dev)
  355. {
  356. int sg_mapped;
  357. u64 total_length = 0;
  358. struct srp_direct_buf *data =
  359. (struct srp_direct_buf *) srp_cmd->add_data;
  360. struct srp_indirect_buf *indirect =
  361. (struct srp_indirect_buf *) data;
  362. sg_mapped = scsi_dma_map(cmd);
  363. if (!sg_mapped)
  364. return 1;
  365. else if (sg_mapped < 0)
  366. return 0;
  367. set_srp_direction(cmd, srp_cmd, sg_mapped);
  368. /* special case; we can use a single direct descriptor */
  369. if (sg_mapped == 1) {
  370. map_sg_list(cmd, sg_mapped, data);
  371. return 1;
  372. }
  373. indirect->table_desc.va = 0;
  374. indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
  375. indirect->table_desc.key = 0;
  376. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  377. total_length = map_sg_list(cmd, sg_mapped,
  378. &indirect->desc_list[0]);
  379. indirect->len = total_length;
  380. return 1;
  381. }
  382. /* get indirect table */
  383. if (!evt_struct->ext_list) {
  384. evt_struct->ext_list = (struct srp_direct_buf *)
  385. dma_alloc_coherent(dev,
  386. SG_ALL * sizeof(struct srp_direct_buf),
  387. &evt_struct->ext_list_token, 0);
  388. if (!evt_struct->ext_list) {
  389. if (!firmware_has_feature(FW_FEATURE_CMO))
  390. sdev_printk(KERN_ERR, cmd->device,
  391. "Can't allocate memory "
  392. "for indirect table\n");
  393. scsi_dma_unmap(cmd);
  394. return 0;
  395. }
  396. }
  397. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  398. indirect->len = total_length;
  399. indirect->table_desc.va = evt_struct->ext_list_token;
  400. indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
  401. memcpy(indirect->desc_list, evt_struct->ext_list,
  402. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  403. return 1;
  404. }
  405. /**
  406. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  407. * @cmd: struct scsi_cmnd with the memory to be mapped
  408. * @srp_cmd: srp_cmd that contains the memory descriptor
  409. * @dev: dma device for which to map dma memory
  410. *
  411. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  412. * Returns 1 on success.
  413. */
  414. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  415. struct srp_event_struct *evt_struct,
  416. struct srp_cmd *srp_cmd, struct device *dev)
  417. {
  418. switch (cmd->sc_data_direction) {
  419. case DMA_FROM_DEVICE:
  420. case DMA_TO_DEVICE:
  421. break;
  422. case DMA_NONE:
  423. return 1;
  424. case DMA_BIDIRECTIONAL:
  425. sdev_printk(KERN_ERR, cmd->device,
  426. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  427. return 0;
  428. default:
  429. sdev_printk(KERN_ERR, cmd->device,
  430. "Unknown data direction 0x%02x; can't map!\n",
  431. cmd->sc_data_direction);
  432. return 0;
  433. }
  434. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  435. }
  436. /**
  437. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  438. * @hostdata: the adapter
  439. */
  440. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  441. {
  442. struct srp_event_struct *tmp_evt, *pos;
  443. unsigned long flags;
  444. spin_lock_irqsave(hostdata->host->host_lock, flags);
  445. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  446. list_del(&tmp_evt->list);
  447. del_timer(&tmp_evt->timer);
  448. if (tmp_evt->cmnd) {
  449. tmp_evt->cmnd->result = (error_code << 16);
  450. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  451. tmp_evt,
  452. tmp_evt->hostdata->dev);
  453. if (tmp_evt->cmnd_done)
  454. tmp_evt->cmnd_done(tmp_evt->cmnd);
  455. } else if (tmp_evt->done)
  456. tmp_evt->done(tmp_evt);
  457. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  458. }
  459. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  460. }
  461. /**
  462. * ibmvscsi_reset_host - Reset the connection to the server
  463. * @hostdata: struct ibmvscsi_host_data to reset
  464. */
  465. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  466. {
  467. scsi_block_requests(hostdata->host);
  468. atomic_set(&hostdata->request_limit, 0);
  469. purge_requests(hostdata, DID_ERROR);
  470. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
  471. (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
  472. (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
  473. atomic_set(&hostdata->request_limit, -1);
  474. dev_err(hostdata->dev, "error after reset\n");
  475. }
  476. scsi_unblock_requests(hostdata->host);
  477. }
  478. /**
  479. * ibmvscsi_timeout - Internal command timeout handler
  480. * @evt_struct: struct srp_event_struct that timed out
  481. *
  482. * Called when an internally generated command times out
  483. */
  484. static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
  485. {
  486. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  487. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  488. evt_struct->iu.srp.cmd.opcode);
  489. ibmvscsi_reset_host(hostdata);
  490. }
  491. /* ------------------------------------------------------------
  492. * Routines for sending and receiving SRPs
  493. */
  494. /**
  495. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  496. * @evt_struct: evt_struct to be sent
  497. * @hostdata: ibmvscsi_host_data of host
  498. * @timeout: timeout in seconds - 0 means do not time command
  499. *
  500. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  501. * Note that this routine assumes that host_lock is held for synchronization
  502. */
  503. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  504. struct ibmvscsi_host_data *hostdata,
  505. unsigned long timeout)
  506. {
  507. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  508. int request_status = 0;
  509. int rc;
  510. /* If we have exhausted our request limit, just fail this request,
  511. * unless it is for a reset or abort.
  512. * Note that there are rare cases involving driver generated requests
  513. * (such as task management requests) that the mid layer may think we
  514. * can handle more requests (can_queue) when we actually can't
  515. */
  516. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  517. request_status =
  518. atomic_dec_if_positive(&hostdata->request_limit);
  519. /* If request limit was -1 when we started, it is now even
  520. * less than that
  521. */
  522. if (request_status < -1)
  523. goto send_error;
  524. /* Otherwise, we may have run out of requests. */
  525. /* If request limit was 0 when we started the adapter is in the
  526. * process of performing a login with the server adapter, or
  527. * we may have run out of requests.
  528. */
  529. else if (request_status == -1 &&
  530. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  531. goto send_busy;
  532. /* Abort and reset calls should make it through.
  533. * Nothing except abort and reset should use the last two
  534. * slots unless we had two or less to begin with.
  535. */
  536. else if (request_status < 2 &&
  537. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  538. /* In the case that we have less than two requests
  539. * available, check the server limit as a combination
  540. * of the request limit and the number of requests
  541. * in-flight (the size of the send list). If the
  542. * server limit is greater than 2, return busy so
  543. * that the last two are reserved for reset and abort.
  544. */
  545. int server_limit = request_status;
  546. struct srp_event_struct *tmp_evt;
  547. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  548. server_limit++;
  549. }
  550. if (server_limit > 2)
  551. goto send_busy;
  552. }
  553. }
  554. /* Copy the IU into the transfer area */
  555. *evt_struct->xfer_iu = evt_struct->iu;
  556. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  557. /* Add this to the sent list. We need to do this
  558. * before we actually send
  559. * in case it comes back REALLY fast
  560. */
  561. list_add_tail(&evt_struct->list, &hostdata->sent);
  562. init_timer(&evt_struct->timer);
  563. if (timeout) {
  564. evt_struct->timer.data = (unsigned long) evt_struct;
  565. evt_struct->timer.expires = jiffies + (timeout * HZ);
  566. evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
  567. add_timer(&evt_struct->timer);
  568. }
  569. if ((rc =
  570. ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  571. list_del(&evt_struct->list);
  572. del_timer(&evt_struct->timer);
  573. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  574. * Firmware will send a CRQ with a transport event (0xFF) to
  575. * tell this client what has happened to the transport. This
  576. * will be handled in ibmvscsi_handle_crq()
  577. */
  578. if (rc == H_CLOSED) {
  579. dev_warn(hostdata->dev, "send warning. "
  580. "Receive queue closed, will retry.\n");
  581. goto send_busy;
  582. }
  583. dev_err(hostdata->dev, "send error %d\n", rc);
  584. atomic_inc(&hostdata->request_limit);
  585. goto send_error;
  586. }
  587. return 0;
  588. send_busy:
  589. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  590. free_event_struct(&hostdata->pool, evt_struct);
  591. if (request_status != -1)
  592. atomic_inc(&hostdata->request_limit);
  593. return SCSI_MLQUEUE_HOST_BUSY;
  594. send_error:
  595. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  596. if (evt_struct->cmnd != NULL) {
  597. evt_struct->cmnd->result = DID_ERROR << 16;
  598. evt_struct->cmnd_done(evt_struct->cmnd);
  599. } else if (evt_struct->done)
  600. evt_struct->done(evt_struct);
  601. free_event_struct(&hostdata->pool, evt_struct);
  602. return 0;
  603. }
  604. /**
  605. * handle_cmd_rsp: - Handle responses from commands
  606. * @evt_struct: srp_event_struct to be handled
  607. *
  608. * Used as a callback by when sending scsi cmds.
  609. * Gets called by ibmvscsi_handle_crq()
  610. */
  611. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  612. {
  613. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  614. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  615. if (unlikely(rsp->opcode != SRP_RSP)) {
  616. if (printk_ratelimit())
  617. dev_warn(evt_struct->hostdata->dev,
  618. "bad SRP RSP type %d\n", rsp->opcode);
  619. }
  620. if (cmnd) {
  621. cmnd->result |= rsp->status;
  622. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  623. memcpy(cmnd->sense_buffer,
  624. rsp->data,
  625. rsp->sense_data_len);
  626. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  627. evt_struct,
  628. evt_struct->hostdata->dev);
  629. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  630. scsi_set_resid(cmnd, rsp->data_out_res_cnt);
  631. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  632. scsi_set_resid(cmnd, rsp->data_in_res_cnt);
  633. }
  634. if (evt_struct->cmnd_done)
  635. evt_struct->cmnd_done(cmnd);
  636. }
  637. /**
  638. * lun_from_dev: - Returns the lun of the scsi device
  639. * @dev: struct scsi_device
  640. *
  641. */
  642. static inline u16 lun_from_dev(struct scsi_device *dev)
  643. {
  644. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  645. }
  646. /**
  647. * ibmvscsi_queue: - The queuecommand function of the scsi template
  648. * @cmd: struct scsi_cmnd to be executed
  649. * @done: Callback function to be called when cmd is completed
  650. */
  651. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  652. void (*done) (struct scsi_cmnd *))
  653. {
  654. struct srp_cmd *srp_cmd;
  655. struct srp_event_struct *evt_struct;
  656. struct srp_indirect_buf *indirect;
  657. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  658. u16 lun = lun_from_dev(cmnd->device);
  659. u8 out_fmt, in_fmt;
  660. cmnd->result = (DID_OK << 16);
  661. evt_struct = get_event_struct(&hostdata->pool);
  662. if (!evt_struct)
  663. return SCSI_MLQUEUE_HOST_BUSY;
  664. /* Set up the actual SRP IU */
  665. srp_cmd = &evt_struct->iu.srp.cmd;
  666. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  667. srp_cmd->opcode = SRP_CMD;
  668. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  669. srp_cmd->lun = ((u64) lun) << 48;
  670. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  671. if (!firmware_has_feature(FW_FEATURE_CMO))
  672. sdev_printk(KERN_ERR, cmnd->device,
  673. "couldn't convert cmd to srp_cmd\n");
  674. free_event_struct(&hostdata->pool, evt_struct);
  675. return SCSI_MLQUEUE_HOST_BUSY;
  676. }
  677. init_event_struct(evt_struct,
  678. handle_cmd_rsp,
  679. VIOSRP_SRP_FORMAT,
  680. cmnd->request->timeout/HZ);
  681. evt_struct->cmnd = cmnd;
  682. evt_struct->cmnd_done = done;
  683. /* Fix up dma address of the buffer itself */
  684. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  685. out_fmt = srp_cmd->buf_fmt >> 4;
  686. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  687. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  688. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  689. indirect->table_desc.va == 0) {
  690. indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
  691. offsetof(struct srp_cmd, add_data) +
  692. offsetof(struct srp_indirect_buf, desc_list);
  693. }
  694. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  695. }
  696. /* ------------------------------------------------------------
  697. * Routines for driver initialization
  698. */
  699. /**
  700. * adapter_info_rsp: - Handle response to MAD adapter info request
  701. * @evt_struct: srp_event_struct with the response
  702. *
  703. * Used as a "done" callback by when sending adapter_info. Gets called
  704. * by ibmvscsi_handle_crq()
  705. */
  706. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  707. {
  708. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  709. dma_unmap_single(hostdata->dev,
  710. evt_struct->iu.mad.adapter_info.buffer,
  711. evt_struct->iu.mad.adapter_info.common.length,
  712. DMA_BIDIRECTIONAL);
  713. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  714. dev_err(hostdata->dev, "error %d getting adapter info\n",
  715. evt_struct->xfer_iu->mad.adapter_info.common.status);
  716. } else {
  717. dev_info(hostdata->dev, "host srp version: %s, "
  718. "host partition %s (%d), OS %d, max io %u\n",
  719. hostdata->madapter_info.srp_version,
  720. hostdata->madapter_info.partition_name,
  721. hostdata->madapter_info.partition_number,
  722. hostdata->madapter_info.os_type,
  723. hostdata->madapter_info.port_max_txu[0]);
  724. if (hostdata->madapter_info.port_max_txu[0])
  725. hostdata->host->max_sectors =
  726. hostdata->madapter_info.port_max_txu[0] >> 9;
  727. if (hostdata->madapter_info.os_type == 3 &&
  728. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  729. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  730. hostdata->madapter_info.srp_version);
  731. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  732. MAX_INDIRECT_BUFS);
  733. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  734. }
  735. }
  736. }
  737. /**
  738. * send_mad_adapter_info: - Sends the mad adapter info request
  739. * and stores the result so it can be retrieved with
  740. * sysfs. We COULD consider causing a failure if the
  741. * returned SRP version doesn't match ours.
  742. * @hostdata: ibmvscsi_host_data of host
  743. *
  744. * Returns zero if successful.
  745. */
  746. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  747. {
  748. struct viosrp_adapter_info *req;
  749. struct srp_event_struct *evt_struct;
  750. unsigned long flags;
  751. dma_addr_t addr;
  752. evt_struct = get_event_struct(&hostdata->pool);
  753. if (!evt_struct) {
  754. dev_err(hostdata->dev,
  755. "couldn't allocate an event for ADAPTER_INFO_REQ!\n");
  756. return;
  757. }
  758. init_event_struct(evt_struct,
  759. adapter_info_rsp,
  760. VIOSRP_MAD_FORMAT,
  761. init_timeout);
  762. req = &evt_struct->iu.mad.adapter_info;
  763. memset(req, 0x00, sizeof(*req));
  764. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  765. req->common.length = sizeof(hostdata->madapter_info);
  766. req->buffer = addr = dma_map_single(hostdata->dev,
  767. &hostdata->madapter_info,
  768. sizeof(hostdata->madapter_info),
  769. DMA_BIDIRECTIONAL);
  770. if (dma_mapping_error(hostdata->dev, req->buffer)) {
  771. if (!firmware_has_feature(FW_FEATURE_CMO))
  772. dev_err(hostdata->dev,
  773. "Unable to map request_buffer for "
  774. "adapter_info!\n");
  775. free_event_struct(&hostdata->pool, evt_struct);
  776. return;
  777. }
  778. spin_lock_irqsave(hostdata->host->host_lock, flags);
  779. if (ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2)) {
  780. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  781. dma_unmap_single(hostdata->dev,
  782. addr,
  783. sizeof(hostdata->madapter_info),
  784. DMA_BIDIRECTIONAL);
  785. }
  786. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  787. };
  788. /**
  789. * login_rsp: - Handle response to SRP login request
  790. * @evt_struct: srp_event_struct with the response
  791. *
  792. * Used as a "done" callback by when sending srp_login. Gets called
  793. * by ibmvscsi_handle_crq()
  794. */
  795. static void login_rsp(struct srp_event_struct *evt_struct)
  796. {
  797. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  798. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  799. case SRP_LOGIN_RSP: /* it worked! */
  800. break;
  801. case SRP_LOGIN_REJ: /* refused! */
  802. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  803. evt_struct->xfer_iu->srp.login_rej.reason);
  804. /* Login failed. */
  805. atomic_set(&hostdata->request_limit, -1);
  806. return;
  807. default:
  808. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  809. evt_struct->xfer_iu->srp.login_rsp.opcode);
  810. /* Login failed. */
  811. atomic_set(&hostdata->request_limit, -1);
  812. return;
  813. }
  814. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  815. if (evt_struct->xfer_iu->srp.login_rsp.req_lim_delta < 0)
  816. dev_err(hostdata->dev, "Invalid request_limit.\n");
  817. /* Now we know what the real request-limit is.
  818. * This value is set rather than added to request_limit because
  819. * request_limit could have been set to -1 by this client.
  820. */
  821. atomic_set(&hostdata->request_limit,
  822. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
  823. /* If we had any pending I/Os, kick them */
  824. scsi_unblock_requests(hostdata->host);
  825. send_mad_adapter_info(hostdata);
  826. return;
  827. }
  828. /**
  829. * send_srp_login: - Sends the srp login
  830. * @hostdata: ibmvscsi_host_data of host
  831. *
  832. * Returns zero if successful.
  833. */
  834. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  835. {
  836. int rc;
  837. unsigned long flags;
  838. struct srp_login_req *login;
  839. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  840. if (!evt_struct) {
  841. dev_err(hostdata->dev, "couldn't allocate an event for login req!\n");
  842. return FAILED;
  843. }
  844. init_event_struct(evt_struct,
  845. login_rsp,
  846. VIOSRP_SRP_FORMAT,
  847. init_timeout);
  848. login = &evt_struct->iu.srp.login_req;
  849. memset(login, 0x00, sizeof(struct srp_login_req));
  850. login->opcode = SRP_LOGIN_REQ;
  851. login->req_it_iu_len = sizeof(union srp_iu);
  852. login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
  853. spin_lock_irqsave(hostdata->host->host_lock, flags);
  854. /* Start out with a request limit of 0, since this is negotiated in
  855. * the login request we are just sending and login requests always
  856. * get sent by the driver regardless of request_limit.
  857. */
  858. atomic_set(&hostdata->request_limit, 0);
  859. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2);
  860. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  861. dev_info(hostdata->dev, "sent SRP login\n");
  862. return rc;
  863. };
  864. /**
  865. * sync_completion: Signal that a synchronous command has completed
  866. * Note that after returning from this call, the evt_struct is freed.
  867. * the caller waiting on this completion shouldn't touch the evt_struct
  868. * again.
  869. */
  870. static void sync_completion(struct srp_event_struct *evt_struct)
  871. {
  872. /* copy the response back */
  873. if (evt_struct->sync_srp)
  874. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  875. complete(&evt_struct->comp);
  876. }
  877. /**
  878. * ibmvscsi_abort: Abort a command...from scsi host template
  879. * send this over to the server and wait synchronously for the response
  880. */
  881. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  882. {
  883. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  884. struct srp_tsk_mgmt *tsk_mgmt;
  885. struct srp_event_struct *evt;
  886. struct srp_event_struct *tmp_evt, *found_evt;
  887. union viosrp_iu srp_rsp;
  888. int rsp_rc;
  889. unsigned long flags;
  890. u16 lun = lun_from_dev(cmd->device);
  891. unsigned long wait_switch = 0;
  892. /* First, find this command in our sent list so we can figure
  893. * out the correct tag
  894. */
  895. spin_lock_irqsave(hostdata->host->host_lock, flags);
  896. wait_switch = jiffies + (init_timeout * HZ);
  897. do {
  898. found_evt = NULL;
  899. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  900. if (tmp_evt->cmnd == cmd) {
  901. found_evt = tmp_evt;
  902. break;
  903. }
  904. }
  905. if (!found_evt) {
  906. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  907. return SUCCESS;
  908. }
  909. evt = get_event_struct(&hostdata->pool);
  910. if (evt == NULL) {
  911. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  912. sdev_printk(KERN_ERR, cmd->device,
  913. "failed to allocate abort event\n");
  914. return FAILED;
  915. }
  916. init_event_struct(evt,
  917. sync_completion,
  918. VIOSRP_SRP_FORMAT,
  919. init_timeout);
  920. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  921. /* Set up an abort SRP command */
  922. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  923. tsk_mgmt->opcode = SRP_TSK_MGMT;
  924. tsk_mgmt->lun = ((u64) lun) << 48;
  925. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  926. tsk_mgmt->task_tag = (u64) found_evt;
  927. evt->sync_srp = &srp_rsp;
  928. init_completion(&evt->comp);
  929. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, init_timeout * 2);
  930. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  931. break;
  932. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  933. msleep(10);
  934. spin_lock_irqsave(hostdata->host->host_lock, flags);
  935. } while (time_before(jiffies, wait_switch));
  936. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  937. if (rsp_rc != 0) {
  938. sdev_printk(KERN_ERR, cmd->device,
  939. "failed to send abort() event. rc=%d\n", rsp_rc);
  940. return FAILED;
  941. }
  942. sdev_printk(KERN_INFO, cmd->device,
  943. "aborting command. lun 0x%llx, tag 0x%llx\n",
  944. (((u64) lun) << 48), (u64) found_evt);
  945. wait_for_completion(&evt->comp);
  946. /* make sure we got a good response */
  947. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  948. if (printk_ratelimit())
  949. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  950. srp_rsp.srp.rsp.opcode);
  951. return FAILED;
  952. }
  953. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  954. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  955. else
  956. rsp_rc = srp_rsp.srp.rsp.status;
  957. if (rsp_rc) {
  958. if (printk_ratelimit())
  959. sdev_printk(KERN_WARNING, cmd->device,
  960. "abort code %d for task tag 0x%llx\n",
  961. rsp_rc, tsk_mgmt->task_tag);
  962. return FAILED;
  963. }
  964. /* Because we dropped the spinlock above, it's possible
  965. * The event is no longer in our list. Make sure it didn't
  966. * complete while we were aborting
  967. */
  968. spin_lock_irqsave(hostdata->host->host_lock, flags);
  969. found_evt = NULL;
  970. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  971. if (tmp_evt->cmnd == cmd) {
  972. found_evt = tmp_evt;
  973. break;
  974. }
  975. }
  976. if (found_evt == NULL) {
  977. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  978. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
  979. tsk_mgmt->task_tag);
  980. return SUCCESS;
  981. }
  982. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
  983. tsk_mgmt->task_tag);
  984. cmd->result = (DID_ABORT << 16);
  985. list_del(&found_evt->list);
  986. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  987. found_evt->hostdata->dev);
  988. free_event_struct(&found_evt->hostdata->pool, found_evt);
  989. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  990. atomic_inc(&hostdata->request_limit);
  991. return SUCCESS;
  992. }
  993. /**
  994. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  995. * template send this over to the server and wait synchronously for the
  996. * response
  997. */
  998. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  999. {
  1000. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1001. struct srp_tsk_mgmt *tsk_mgmt;
  1002. struct srp_event_struct *evt;
  1003. struct srp_event_struct *tmp_evt, *pos;
  1004. union viosrp_iu srp_rsp;
  1005. int rsp_rc;
  1006. unsigned long flags;
  1007. u16 lun = lun_from_dev(cmd->device);
  1008. unsigned long wait_switch = 0;
  1009. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1010. wait_switch = jiffies + (init_timeout * HZ);
  1011. do {
  1012. evt = get_event_struct(&hostdata->pool);
  1013. if (evt == NULL) {
  1014. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1015. sdev_printk(KERN_ERR, cmd->device,
  1016. "failed to allocate reset event\n");
  1017. return FAILED;
  1018. }
  1019. init_event_struct(evt,
  1020. sync_completion,
  1021. VIOSRP_SRP_FORMAT,
  1022. init_timeout);
  1023. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1024. /* Set up a lun reset SRP command */
  1025. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1026. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1027. tsk_mgmt->lun = ((u64) lun) << 48;
  1028. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1029. evt->sync_srp = &srp_rsp;
  1030. init_completion(&evt->comp);
  1031. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, init_timeout * 2);
  1032. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1033. break;
  1034. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1035. msleep(10);
  1036. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1037. } while (time_before(jiffies, wait_switch));
  1038. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1039. if (rsp_rc != 0) {
  1040. sdev_printk(KERN_ERR, cmd->device,
  1041. "failed to send reset event. rc=%d\n", rsp_rc);
  1042. return FAILED;
  1043. }
  1044. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
  1045. (((u64) lun) << 48));
  1046. wait_for_completion(&evt->comp);
  1047. /* make sure we got a good response */
  1048. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1049. if (printk_ratelimit())
  1050. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1051. srp_rsp.srp.rsp.opcode);
  1052. return FAILED;
  1053. }
  1054. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1055. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1056. else
  1057. rsp_rc = srp_rsp.srp.rsp.status;
  1058. if (rsp_rc) {
  1059. if (printk_ratelimit())
  1060. sdev_printk(KERN_WARNING, cmd->device,
  1061. "reset code %d for task tag 0x%llx\n",
  1062. rsp_rc, tsk_mgmt->task_tag);
  1063. return FAILED;
  1064. }
  1065. /* We need to find all commands for this LUN that have not yet been
  1066. * responded to, and fail them with DID_RESET
  1067. */
  1068. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1069. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1070. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1071. if (tmp_evt->cmnd)
  1072. tmp_evt->cmnd->result = (DID_RESET << 16);
  1073. list_del(&tmp_evt->list);
  1074. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1075. tmp_evt->hostdata->dev);
  1076. free_event_struct(&tmp_evt->hostdata->pool,
  1077. tmp_evt);
  1078. atomic_inc(&hostdata->request_limit);
  1079. if (tmp_evt->cmnd_done)
  1080. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1081. else if (tmp_evt->done)
  1082. tmp_evt->done(tmp_evt);
  1083. }
  1084. }
  1085. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1086. return SUCCESS;
  1087. }
  1088. /**
  1089. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1090. * @cmd: struct scsi_cmnd having problems
  1091. */
  1092. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1093. {
  1094. unsigned long wait_switch = 0;
  1095. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1096. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1097. ibmvscsi_reset_host(hostdata);
  1098. for (wait_switch = jiffies + (init_timeout * HZ);
  1099. time_before(jiffies, wait_switch) &&
  1100. atomic_read(&hostdata->request_limit) < 2;) {
  1101. msleep(10);
  1102. }
  1103. if (atomic_read(&hostdata->request_limit) <= 0)
  1104. return FAILED;
  1105. return SUCCESS;
  1106. }
  1107. /**
  1108. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1109. * @crq: Command/Response queue
  1110. * @hostdata: ibmvscsi_host_data of host
  1111. *
  1112. */
  1113. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1114. struct ibmvscsi_host_data *hostdata)
  1115. {
  1116. long rc;
  1117. unsigned long flags;
  1118. struct srp_event_struct *evt_struct =
  1119. (struct srp_event_struct *)crq->IU_data_ptr;
  1120. switch (crq->valid) {
  1121. case 0xC0: /* initialization */
  1122. switch (crq->format) {
  1123. case 0x01: /* Initialization message */
  1124. dev_info(hostdata->dev, "partner initialized\n");
  1125. /* Send back a response */
  1126. if ((rc = ibmvscsi_ops->send_crq(hostdata,
  1127. 0xC002000000000000LL, 0)) == 0) {
  1128. /* Now login */
  1129. send_srp_login(hostdata);
  1130. } else {
  1131. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1132. }
  1133. break;
  1134. case 0x02: /* Initialization response */
  1135. dev_info(hostdata->dev, "partner initialization complete\n");
  1136. /* Now login */
  1137. send_srp_login(hostdata);
  1138. break;
  1139. default:
  1140. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1141. }
  1142. return;
  1143. case 0xFF: /* Hypervisor telling us the connection is closed */
  1144. scsi_block_requests(hostdata->host);
  1145. atomic_set(&hostdata->request_limit, 0);
  1146. if (crq->format == 0x06) {
  1147. /* We need to re-setup the interpartition connection */
  1148. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1149. purge_requests(hostdata, DID_REQUEUE);
  1150. if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
  1151. hostdata)) ||
  1152. (ibmvscsi_ops->send_crq(hostdata,
  1153. 0xC001000000000000LL, 0))) {
  1154. atomic_set(&hostdata->request_limit,
  1155. -1);
  1156. dev_err(hostdata->dev, "error after enable\n");
  1157. }
  1158. } else {
  1159. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1160. crq->format);
  1161. purge_requests(hostdata, DID_ERROR);
  1162. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
  1163. hostdata)) ||
  1164. (ibmvscsi_ops->send_crq(hostdata,
  1165. 0xC001000000000000LL, 0))) {
  1166. atomic_set(&hostdata->request_limit,
  1167. -1);
  1168. dev_err(hostdata->dev, "error after reset\n");
  1169. }
  1170. }
  1171. scsi_unblock_requests(hostdata->host);
  1172. return;
  1173. case 0x80: /* real payload */
  1174. break;
  1175. default:
  1176. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1177. crq->valid);
  1178. return;
  1179. }
  1180. /* The only kind of payload CRQs we should get are responses to
  1181. * things we send. Make sure this response is to something we
  1182. * actually sent
  1183. */
  1184. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1185. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1186. (void *)crq->IU_data_ptr);
  1187. return;
  1188. }
  1189. if (atomic_read(&evt_struct->free)) {
  1190. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1191. (void *)crq->IU_data_ptr);
  1192. return;
  1193. }
  1194. if (crq->format == VIOSRP_SRP_FORMAT)
  1195. atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
  1196. &hostdata->request_limit);
  1197. del_timer(&evt_struct->timer);
  1198. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1199. evt_struct->cmnd->result = DID_ERROR << 16;
  1200. if (evt_struct->done)
  1201. evt_struct->done(evt_struct);
  1202. else
  1203. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1204. /*
  1205. * Lock the host_lock before messing with these structures, since we
  1206. * are running in a task context
  1207. */
  1208. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1209. list_del(&evt_struct->list);
  1210. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1211. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1212. }
  1213. /**
  1214. * ibmvscsi_get_host_config: Send the command to the server to get host
  1215. * configuration data. The data is opaque to us.
  1216. */
  1217. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1218. unsigned char *buffer, int length)
  1219. {
  1220. struct viosrp_host_config *host_config;
  1221. struct srp_event_struct *evt_struct;
  1222. unsigned long flags;
  1223. dma_addr_t addr;
  1224. int rc;
  1225. evt_struct = get_event_struct(&hostdata->pool);
  1226. if (!evt_struct) {
  1227. dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
  1228. return -1;
  1229. }
  1230. init_event_struct(evt_struct,
  1231. sync_completion,
  1232. VIOSRP_MAD_FORMAT,
  1233. init_timeout);
  1234. host_config = &evt_struct->iu.mad.host_config;
  1235. /* Set up a lun reset SRP command */
  1236. memset(host_config, 0x00, sizeof(*host_config));
  1237. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1238. host_config->common.length = length;
  1239. host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
  1240. length,
  1241. DMA_BIDIRECTIONAL);
  1242. if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
  1243. if (!firmware_has_feature(FW_FEATURE_CMO))
  1244. dev_err(hostdata->dev,
  1245. "dma_mapping error getting host config\n");
  1246. free_event_struct(&hostdata->pool, evt_struct);
  1247. return -1;
  1248. }
  1249. init_completion(&evt_struct->comp);
  1250. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1251. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2);
  1252. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1253. if (rc == 0)
  1254. wait_for_completion(&evt_struct->comp);
  1255. dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
  1256. return rc;
  1257. }
  1258. /**
  1259. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1260. * @sdev: struct scsi_device device to configure
  1261. *
  1262. * Enable allow_restart for a device if it is a disk. Adjust the
  1263. * queue_depth here also as is required by the documentation for
  1264. * struct scsi_host_template.
  1265. */
  1266. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1267. {
  1268. struct Scsi_Host *shost = sdev->host;
  1269. unsigned long lock_flags = 0;
  1270. spin_lock_irqsave(shost->host_lock, lock_flags);
  1271. if (sdev->type == TYPE_DISK) {
  1272. sdev->allow_restart = 1;
  1273. blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
  1274. }
  1275. scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
  1276. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1277. return 0;
  1278. }
  1279. /**
  1280. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1281. * @sdev: scsi device struct
  1282. * @qdepth: depth to set
  1283. *
  1284. * Return value:
  1285. * actual depth set
  1286. **/
  1287. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1288. {
  1289. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1290. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1291. scsi_adjust_queue_depth(sdev, 0, qdepth);
  1292. return sdev->queue_depth;
  1293. }
  1294. /* ------------------------------------------------------------
  1295. * sysfs attributes
  1296. */
  1297. static ssize_t show_host_srp_version(struct device *dev,
  1298. struct device_attribute *attr, char *buf)
  1299. {
  1300. struct Scsi_Host *shost = class_to_shost(dev);
  1301. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1302. int len;
  1303. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1304. hostdata->madapter_info.srp_version);
  1305. return len;
  1306. }
  1307. static struct device_attribute ibmvscsi_host_srp_version = {
  1308. .attr = {
  1309. .name = "srp_version",
  1310. .mode = S_IRUGO,
  1311. },
  1312. .show = show_host_srp_version,
  1313. };
  1314. static ssize_t show_host_partition_name(struct device *dev,
  1315. struct device_attribute *attr,
  1316. char *buf)
  1317. {
  1318. struct Scsi_Host *shost = class_to_shost(dev);
  1319. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1320. int len;
  1321. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1322. hostdata->madapter_info.partition_name);
  1323. return len;
  1324. }
  1325. static struct device_attribute ibmvscsi_host_partition_name = {
  1326. .attr = {
  1327. .name = "partition_name",
  1328. .mode = S_IRUGO,
  1329. },
  1330. .show = show_host_partition_name,
  1331. };
  1332. static ssize_t show_host_partition_number(struct device *dev,
  1333. struct device_attribute *attr,
  1334. char *buf)
  1335. {
  1336. struct Scsi_Host *shost = class_to_shost(dev);
  1337. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1338. int len;
  1339. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1340. hostdata->madapter_info.partition_number);
  1341. return len;
  1342. }
  1343. static struct device_attribute ibmvscsi_host_partition_number = {
  1344. .attr = {
  1345. .name = "partition_number",
  1346. .mode = S_IRUGO,
  1347. },
  1348. .show = show_host_partition_number,
  1349. };
  1350. static ssize_t show_host_mad_version(struct device *dev,
  1351. struct device_attribute *attr, char *buf)
  1352. {
  1353. struct Scsi_Host *shost = class_to_shost(dev);
  1354. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1355. int len;
  1356. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1357. hostdata->madapter_info.mad_version);
  1358. return len;
  1359. }
  1360. static struct device_attribute ibmvscsi_host_mad_version = {
  1361. .attr = {
  1362. .name = "mad_version",
  1363. .mode = S_IRUGO,
  1364. },
  1365. .show = show_host_mad_version,
  1366. };
  1367. static ssize_t show_host_os_type(struct device *dev,
  1368. struct device_attribute *attr, char *buf)
  1369. {
  1370. struct Scsi_Host *shost = class_to_shost(dev);
  1371. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1372. int len;
  1373. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1374. return len;
  1375. }
  1376. static struct device_attribute ibmvscsi_host_os_type = {
  1377. .attr = {
  1378. .name = "os_type",
  1379. .mode = S_IRUGO,
  1380. },
  1381. .show = show_host_os_type,
  1382. };
  1383. static ssize_t show_host_config(struct device *dev,
  1384. struct device_attribute *attr, char *buf)
  1385. {
  1386. struct Scsi_Host *shost = class_to_shost(dev);
  1387. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1388. /* returns null-terminated host config data */
  1389. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1390. return strlen(buf);
  1391. else
  1392. return 0;
  1393. }
  1394. static struct device_attribute ibmvscsi_host_config = {
  1395. .attr = {
  1396. .name = "config",
  1397. .mode = S_IRUGO,
  1398. },
  1399. .show = show_host_config,
  1400. };
  1401. static struct device_attribute *ibmvscsi_attrs[] = {
  1402. &ibmvscsi_host_srp_version,
  1403. &ibmvscsi_host_partition_name,
  1404. &ibmvscsi_host_partition_number,
  1405. &ibmvscsi_host_mad_version,
  1406. &ibmvscsi_host_os_type,
  1407. &ibmvscsi_host_config,
  1408. NULL
  1409. };
  1410. /* ------------------------------------------------------------
  1411. * SCSI driver registration
  1412. */
  1413. static struct scsi_host_template driver_template = {
  1414. .module = THIS_MODULE,
  1415. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1416. .proc_name = "ibmvscsi",
  1417. .queuecommand = ibmvscsi_queuecommand,
  1418. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1419. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1420. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1421. .slave_configure = ibmvscsi_slave_configure,
  1422. .change_queue_depth = ibmvscsi_change_queue_depth,
  1423. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1424. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1425. .this_id = -1,
  1426. .sg_tablesize = SG_ALL,
  1427. .use_clustering = ENABLE_CLUSTERING,
  1428. .shost_attrs = ibmvscsi_attrs,
  1429. };
  1430. /**
  1431. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1432. *
  1433. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1434. *
  1435. * Return value:
  1436. * Number of bytes of IO data the driver will need to perform well.
  1437. */
  1438. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1439. {
  1440. /* iu_storage data allocated in initialize_event_pool */
  1441. unsigned long desired_io = max_events * sizeof(union viosrp_iu);
  1442. /* add io space for sg data */
  1443. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1444. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1445. return desired_io;
  1446. }
  1447. /**
  1448. * Called by bus code for each adapter
  1449. */
  1450. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1451. {
  1452. struct ibmvscsi_host_data *hostdata;
  1453. struct Scsi_Host *host;
  1454. struct device *dev = &vdev->dev;
  1455. struct srp_rport_identifiers ids;
  1456. struct srp_rport *rport;
  1457. unsigned long wait_switch = 0;
  1458. int rc;
  1459. vdev->dev.driver_data = NULL;
  1460. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1461. if (!host) {
  1462. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1463. goto scsi_host_alloc_failed;
  1464. }
  1465. host->transportt = ibmvscsi_transport_template;
  1466. hostdata = shost_priv(host);
  1467. memset(hostdata, 0x00, sizeof(*hostdata));
  1468. INIT_LIST_HEAD(&hostdata->sent);
  1469. hostdata->host = host;
  1470. hostdata->dev = dev;
  1471. atomic_set(&hostdata->request_limit, -1);
  1472. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1473. rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
  1474. if (rc != 0 && rc != H_RESOURCE) {
  1475. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1476. goto init_crq_failed;
  1477. }
  1478. if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
  1479. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1480. goto init_pool_failed;
  1481. }
  1482. host->max_lun = 8;
  1483. host->max_id = max_id;
  1484. host->max_channel = max_channel;
  1485. if (scsi_add_host(hostdata->host, hostdata->dev))
  1486. goto add_host_failed;
  1487. /* we don't have a proper target_port_id so let's use the fake one */
  1488. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1489. sizeof(ids.port_id));
  1490. ids.roles = SRP_RPORT_ROLE_TARGET;
  1491. rport = srp_rport_add(host, &ids);
  1492. if (IS_ERR(rport))
  1493. goto add_srp_port_failed;
  1494. /* Try to send an initialization message. Note that this is allowed
  1495. * to fail if the other end is not acive. In that case we don't
  1496. * want to scan
  1497. */
  1498. if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1499. || rc == H_RESOURCE) {
  1500. /*
  1501. * Wait around max init_timeout secs for the adapter to finish
  1502. * initializing. When we are done initializing, we will have a
  1503. * valid request_limit. We don't want Linux scanning before
  1504. * we are ready.
  1505. */
  1506. for (wait_switch = jiffies + (init_timeout * HZ);
  1507. time_before(jiffies, wait_switch) &&
  1508. atomic_read(&hostdata->request_limit) < 2;) {
  1509. msleep(10);
  1510. }
  1511. /* if we now have a valid request_limit, initiate a scan */
  1512. if (atomic_read(&hostdata->request_limit) > 0)
  1513. scsi_scan_host(host);
  1514. }
  1515. vdev->dev.driver_data = hostdata;
  1516. return 0;
  1517. add_srp_port_failed:
  1518. scsi_remove_host(hostdata->host);
  1519. add_host_failed:
  1520. release_event_pool(&hostdata->pool, hostdata);
  1521. init_pool_failed:
  1522. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
  1523. init_crq_failed:
  1524. scsi_host_put(host);
  1525. scsi_host_alloc_failed:
  1526. return -1;
  1527. }
  1528. static int ibmvscsi_remove(struct vio_dev *vdev)
  1529. {
  1530. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1531. release_event_pool(&hostdata->pool, hostdata);
  1532. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
  1533. max_events);
  1534. srp_remove_host(hostdata->host);
  1535. scsi_remove_host(hostdata->host);
  1536. scsi_host_put(hostdata->host);
  1537. return 0;
  1538. }
  1539. /**
  1540. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1541. * support.
  1542. */
  1543. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1544. {"vscsi", "IBM,v-scsi"},
  1545. { "", "" }
  1546. };
  1547. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1548. static struct vio_driver ibmvscsi_driver = {
  1549. .id_table = ibmvscsi_device_table,
  1550. .probe = ibmvscsi_probe,
  1551. .remove = ibmvscsi_remove,
  1552. .get_desired_dma = ibmvscsi_get_desired_dma,
  1553. .driver = {
  1554. .name = "ibmvscsi",
  1555. .owner = THIS_MODULE,
  1556. }
  1557. };
  1558. static struct srp_function_template ibmvscsi_transport_functions = {
  1559. };
  1560. int __init ibmvscsi_module_init(void)
  1561. {
  1562. int ret;
  1563. /* Ensure we have two requests to do error recovery */
  1564. driver_template.can_queue = max_requests;
  1565. max_events = max_requests + 2;
  1566. if (firmware_has_feature(FW_FEATURE_ISERIES))
  1567. ibmvscsi_ops = &iseriesvscsi_ops;
  1568. else if (firmware_has_feature(FW_FEATURE_VIO))
  1569. ibmvscsi_ops = &rpavscsi_ops;
  1570. else
  1571. return -ENODEV;
  1572. ibmvscsi_transport_template =
  1573. srp_attach_transport(&ibmvscsi_transport_functions);
  1574. if (!ibmvscsi_transport_template)
  1575. return -ENOMEM;
  1576. ret = vio_register_driver(&ibmvscsi_driver);
  1577. if (ret)
  1578. srp_release_transport(ibmvscsi_transport_template);
  1579. return ret;
  1580. }
  1581. void __exit ibmvscsi_module_exit(void)
  1582. {
  1583. vio_unregister_driver(&ibmvscsi_driver);
  1584. srp_release_transport(ibmvscsi_transport_template);
  1585. }
  1586. module_init(ibmvscsi_module_init);
  1587. module_exit(ibmvscsi_module_exit);