ibmvscsi.c 61 KB

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