ibmvscsi.c 60 KB

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